diff --git a/cmd/sam/main.go b/cmd/sam/main.go index db7cf2899..3a77efed6 100644 --- a/cmd/sam/main.go +++ b/cmd/sam/main.go @@ -30,7 +30,7 @@ func main() { // log to stdout not stderr log.SetOutput(os.Stdout) - NewMonitor(config.monitorInterval) + go NewMonitor(config.monitorInterval) // set up database connection factory.Database.Add("default", config.dbDSN) diff --git a/cmd/sam/monitor.go b/cmd/sam/monitor.go index dd1dea662..4f1fa41af 100644 --- a/cmd/sam/monitor.go +++ b/cmd/sam/monitor.go @@ -8,40 +8,46 @@ import ( ) type Monitor struct { - runtime.MemStats + Alloc, + TotalAlloc, + Sys, + Mallocs, + Frees, + LiveObjects, + PauseTotalNs uint64 + + NumGC uint32 NumGoroutine int } -func (m Monitor) MarshalJSON() ([]byte, error) { - result := struct { - Alloc, - TotalAlloc, - Sys, - Mallocs, - Frees uint64 - - NumGC uint32 - NumGoroutine int - }{ - m.MemStats.Alloc, - m.MemStats.TotalAlloc, - m.MemStats.Sys, - m.MemStats.Mallocs, - m.MemStats.Frees, - - m.MemStats.NumGC, - m.NumGoroutine, - } - return json.Marshal(result) -} - func NewMonitor(duration int) { var m Monitor + var rtm runtime.MemStats var interval = time.Duration(duration) * time.Second for { <-time.After(interval) - runtime.ReadMemStats(&m.MemStats) + + // Read full mem stats + runtime.ReadMemStats(&rtm) + + // Number of goroutines m.NumGoroutine = runtime.NumGoroutine() + + // Misc memory stats + m.Alloc = rtm.Alloc + m.TotalAlloc = rtm.TotalAlloc + m.Sys = rtm.Sys + m.Mallocs = rtm.Mallocs + m.Frees = rtm.Frees + + // Live objects = Mallocs - Frees + m.LiveObjects = m.Mallocs - m.Frees + + // GC Stats + m.PauseTotalNs = rtm.PauseTotalNs + m.NumGC = rtm.NumGC + + // Just encode to json and print b, _ := json.Marshal(m) fmt.Println(string(b)) }