diff --git a/cmd/crm/monitor.go b/cmd/crm/monitor.go new file mode 100644 index 000000000..e0e382862 --- /dev/null +++ b/cmd/crm/monitor.go @@ -0,0 +1,59 @@ +package main + +import ( + "encoding/json" + "expvar" + "fmt" + "runtime" + "time" +) + +type Monitor struct { + Alloc, + TotalAlloc, + Sys, + Mallocs, + Frees, + LiveObjects, + PauseTotalNs uint64 + + NumGC uint32 + NumGoroutine int +} + +func NewMonitor(duration int) { + var ( + m = Monitor{} + rtm runtime.MemStats + goroutines = expvar.NewInt("num_goroutine") + ) + var interval = time.Duration(duration) * time.Second + for { + <-time.After(interval) + + // Read full mem stats + runtime.ReadMemStats(&rtm) + + // Number of goroutines + m.NumGoroutine = runtime.NumGoroutine() + goroutines.Set(int64(m.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)) + } +}