-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmonitor.go
72 lines (62 loc) · 1.64 KB
/
monitor.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
package main
import (
"log"
"runtime"
"time"
)
//Monitor objects hold all the performance stats we intend to monitor.
type Monitor struct {
Alloc,
TotalAlloc,
Sys,
Mallocs,
Frees,
LiveObjects,
PauseTotalNs uint64
NumGC uint32
NumGoroutine int
}
/*NewMonitor creates a new monitor object and starts monitoring.
Should be called with the go- prefix, otherwise it will halt flow.*/
func NewMonitor(duration int) {
var m Monitor
var rtm runtime.MemStats
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()
// 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
log.Printf(`
%d - number of goroutines
%f - MB of allocated heap objects
%f - cumulative MB allocated for heap objects (does not decrease when objects are freed)
%f - MB memory obtained from the OS reserved by the Go runtime
%d - cumulative count of heap objects allocated
%d - cumulative count of heap objects freed
%d - count of live heap objects
%f - cumulative seconds in stop-the-world pauses
%d - number of completed GC cycles
`,
m.NumGoroutine,
float64(m.Alloc)/1000000,
float64(m.TotalAlloc)/1000000,
float64(m.Sys)/1000000,
m.Mallocs, m.Frees, m.LiveObjects,
float64(m.PauseTotalNs)/1000000000,
m.NumGC)
}
}