-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
85 lines (74 loc) · 2.12 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Package util - Content managed by Project Forge, see [projectforge.md] for details.
package util
import (
"fmt"
"os"
"runtime"
"runtime/debug"
"runtime/pprof"
"strings"
"time"
)
type DebugInfo struct {
ServerTags *OrderedMap[string]
AppTags *OrderedMap[string]
RuntimeTags *OrderedMap[string]
Mods []*debug.Module
}
func DebugGetInfo(version string, started time.Time) *DebugInfo {
bi := DebugBuildInfo()
serverTags := NewOrderedMap[string](false, 10)
hostname, _ := os.Hostname()
serverTags.Append("Machine Name", hostname)
serverTags.Append("CPU Architecture", runtime.GOARCH)
serverTags.Append("Operating System", runtime.GOOS)
serverTags.Append("CPU Count", fmt.Sprint(runtime.NumCPU()))
appTags := NewOrderedMap[string](false, 10)
appTags.Append("Name", AppName)
exec, _ := os.Executable()
wind, exec := StringSplitLast(exec, '/', true)
if exec == "" {
_, exec = StringSplitLast(wind, '\\', true)
}
appTags.Append("Executable", exec)
appTags.Append("Version", version)
appTags.Append("Go Version", runtime.Version())
appTags.Append("Compiler", runtime.Compiler)
runtimeTags := NewOrderedMap[string](false, 10)
args := os.Args
if len(args) > 0 {
args = args[1:]
}
runtimeTags.Append("Running Since", TimeRelative(&started))
runtimeTags.Append("Arguments", strings.Join(args, " "))
runtimeTags.Append("Max Processes", fmt.Sprint(runtime.GOMAXPROCS(-1)))
runtimeTags.Append("Go Routines", fmt.Sprint(runtime.NumGoroutine()))
runtimeTags.Append("Cgo Calls", fmt.Sprint(runtime.NumCgoCall()))
return &DebugInfo{ServerTags: serverTags, AppTags: appTags, RuntimeTags: runtimeTags, Mods: bi.Deps}
}
func DebugBuildInfo() *debug.BuildInfo {
mods, ok := debug.ReadBuildInfo()
if !ok {
return nil
}
return mods
}
func DebugStartCPUProfile() error {
f, err := os.Create("./tmp/cpu.pprof")
if err != nil {
return err
}
return pprof.StartCPUProfile(f)
}
func DebugTakeHeapProfile() error {
f, err := os.Create("./tmp/mem.pprof")
if err != nil {
return err
}
return pprof.WriteHeapProfile(f)
}
func DebugMemStats() *runtime.MemStats {
x := &runtime.MemStats{}
runtime.ReadMemStats(x)
return x
}