From afed5c3703d29774bad0759ac7138c8b5634b494 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 13 Apr 2023 12:38:54 -0500 Subject: [PATCH] add back in missing DebugInfo --- templates/lib_template.go | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/templates/lib_template.go b/templates/lib_template.go index 72ba227..c962d54 100644 --- a/templates/lib_template.go +++ b/templates/lib_template.go @@ -20,6 +20,7 @@ import ( "os/user" path "path/filepath" "runtime" + "runtime/pprof" "strings" "time" mrand "math/rand" @@ -510,6 +511,48 @@ func UpdateSettings(settingsJson string) { {{EXPERIMENT_UPDATESETTINGS}} } +//export c_GetDebugInfo +func c_GetDebugInfo() *C.char { + return C.CString(GetDebugInfo()) +} + +type DebugInfo struct { + BuildVersion string + BuildDate string + HeapAllocated float64 + HeapInUse float64 + HeapReleased float64 + HeapObjects uint64 + NumThreads uint64 + SystemMemory float64 +} + +func GetDebugInfo() string { + + var memstats runtime.MemStats + runtime.ReadMemStats(&memstats) + + const MegaByte = 1024.0 * 1024.0 + + debugInfo := new(DebugInfo) + debugInfo.BuildVersion = buildVer + debugInfo.BuildDate = buildDate + debugInfo.HeapAllocated = float64(memstats.HeapAlloc) / MegaByte + debugInfo.HeapObjects = memstats.HeapObjects + debugInfo.NumThreads = uint64(runtime.NumGoroutine()) + debugInfo.HeapReleased = float64(memstats.HeapReleased) / MegaByte + debugInfo.HeapInUse = float64(memstats.HeapInuse) / MegaByte + debugInfo.SystemMemory = float64(memstats.Sys) / MegaByte + + if os.Getenv("CWTCH_PROFILE") == "1" { + pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) + f, _ := os.Create("mem.prof") + pprof.WriteHeapProfile(f) + } + + data, _ := json.Marshal(debugInfo) + return string(data) +} {{BINDINGS}}