From 41f388bfed4a1f87222abfe393aea828cc6d8655 Mon Sep 17 00:00:00 2001 From: Vladimir Barsukov Date: Sun, 19 Nov 2023 09:13:07 +0200 Subject: [PATCH] fix --- zdebug/zdebug.go | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/zdebug/zdebug.go b/zdebug/zdebug.go index 3e79f67..50cf713 100644 --- a/zdebug/zdebug.go +++ b/zdebug/zdebug.go @@ -2,7 +2,6 @@ package zdebug import ( "fmt" - "git.barsukov.pro/barsukov/zgo/zjson" "git.barsukov.pro/barsukov/zgo/zutils" "runtime" "strings" @@ -10,32 +9,39 @@ import ( ) type Monitor struct { - Alloc, - Sys, - LiveObjects uint64 + Alloc uint64 `json:",omitempty"` + HeapInuse uint64 `json:",omitempty"` + MaxAlloc uint64 `json:",omitempty"` + MaxSys uint64 `json:",omitempty"` - NumGC uint32 - NumGoroutine int + LiveObjects uint64 `json:",omitempty"` + + NumGC uint32 `json:",omitempty"` + NumGoroutine int `json:",omitempty"` } +const mb = 1024 * 1024 + func NewMonitor(secs int) { go func() { - var m Monitor var rtm runtime.MemStats - var interval = time.Duration(secs) * time.Second + var memMax uint64 + for { - <-time.After(interval) + <-time.After(time.Duration(secs) * time.Second) runtime.ReadMemStats(&rtm) - m.NumGoroutine = runtime.NumGoroutine() - - m.Alloc = rtm.Alloc / 1e6 - m.Sys = rtm.Sys / 1e6 - m.LiveObjects = rtm.Mallocs - rtm.Frees - m.NumGC = rtm.NumGC + mem := ((rtm.HeapIdle - rtm.HeapReleased) + (rtm.Alloc + rtm.HeapInuse)) / 1e6 + memMax = max(memMax, mem) - fmt.Println(zjson.MustString(m)) + fmt.Printf("Z_DEBUG: MEM: %v; MEM_MAX: %v; OBJ: %v, GO: %v, GC: %v\n", + mem, + memMax, + (rtm.Mallocs-rtm.Frees)/1e3, + runtime.NumGoroutine(), + rtm.NumGC, + ) } }() } -- GitLab