package zdebug import ( "fmt" "git.barsukov.pro/barsukov/zgo/zjson" "git.barsukov.pro/barsukov/zgo/zutils" "runtime" "strings" "time" ) type Monitor struct { Alloc, TotalAlloc, Sys, LiveObjects uint64 NumGC uint32 NumGoroutine int } func NewMonitor(secs int) { go func() { var m Monitor var rtm runtime.MemStats var interval = time.Duration(secs) * time.Second for { <-time.After(interval) runtime.ReadMemStats(&rtm) m.NumGoroutine = runtime.NumGoroutine() m.Alloc = rtm.Alloc / 1e6 m.TotalAlloc = rtm.TotalAlloc / 1e6 m.Sys = rtm.Sys / 1e6 m.LiveObjects = rtm.Mallocs - rtm.Frees m.NumGC = rtm.NumGC fmt.Println(zjson.MustString(m)) } }() } func Trace() []string { var out []string pc := make([]uintptr, 10) runtime.Callers(2, pc) for i := range pc { if f := runtime.FuncForPC(pc[i]); f != nil { file, line := f.FileLine(pc[i]) if strings.Contains(file, "go/pkg") { continue } out = append(out, fmt.Sprintf("%s:%d", f.Name(), line)) } } return zutils.ArrayUnique(out) }