-
Notifications
You must be signed in to change notification settings - Fork 18
/
runtime.go
84 lines (70 loc) · 2.68 KB
/
runtime.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
package stats
import (
"runtime"
)
type runtimeStats struct {
alloc Gauge // bytes allocated and not yet freed
totalAlloc Counter // bytes allocated (even if freed)
sys Gauge // bytes obtained from system (sum of XxxSys below)
lookups Counter // number of pointer lookups
mallocs Counter // number of mallocs
frees Counter // number of frees
// Main allocation heap statistics
heapAlloc Gauge // bytes allocated and not yet freed (same as Alloc above)
heapSys Gauge // bytes obtained from system
heapIdle Gauge // bytes in idle spans
heapInuse Gauge // bytes in non-idle span
heapReleased Gauge // bytes released to the OS
heapObjects Gauge // total number of allocated objects
// Garbage collector statistics.
nextGC Gauge // next collection will happen when HeapAlloc ≥ this amount
lastGC Gauge // end time of last collection (nanoseconds since 1970)
pauseTotalNs Counter
numGC Counter
gcCPUPercent Gauge
numGoroutine Gauge
}
// NewRuntimeStats returns a StatGenerator with common Go runtime stats like memory allocated,
// total mallocs, total frees, etc.
func NewRuntimeStats(scope Scope) StatGenerator {
return runtimeStats{
alloc: scope.NewGauge("alloc"),
totalAlloc: scope.NewCounter("totalAlloc"),
sys: scope.NewGauge("sys"),
lookups: scope.NewCounter("lookups"),
mallocs: scope.NewCounter("mallocs"),
frees: scope.NewCounter("frees"),
heapAlloc: scope.NewGauge("heapAlloc"),
heapSys: scope.NewGauge("heapSys"),
heapIdle: scope.NewGauge("heapIdle"),
heapInuse: scope.NewGauge("heapInuse"),
heapReleased: scope.NewGauge("heapReleased"),
heapObjects: scope.NewGauge("heapObjects"),
nextGC: scope.NewGauge("nextGC"),
lastGC: scope.NewGauge("lastGC"),
pauseTotalNs: scope.NewCounter("pauseTotalNs"),
numGC: scope.NewCounter("numGC"),
gcCPUPercent: scope.NewGauge("gcCPUPercent"),
numGoroutine: scope.NewGauge("numGoroutine"),
}
}
func (r runtimeStats) GenerateStats() {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
r.alloc.Set(memStats.Alloc)
r.totalAlloc.Set(memStats.TotalAlloc)
r.mallocs.Set(memStats.Mallocs)
r.frees.Set(memStats.Frees)
r.heapAlloc.Set(memStats.HeapAlloc)
r.heapSys.Set(memStats.HeapSys)
r.heapIdle.Set(memStats.HeapIdle)
r.heapInuse.Set(memStats.HeapInuse)
r.heapReleased.Set(memStats.HeapReleased)
r.heapObjects.Set(memStats.HeapObjects)
r.nextGC.Set(memStats.NextGC)
r.lastGC.Set(memStats.LastGC)
r.pauseTotalNs.Set(memStats.PauseTotalNs)
r.numGC.Set(uint64(memStats.NumGC))
r.gcCPUPercent.Set(uint64(memStats.GCCPUFraction * 100))
r.numGoroutine.Set(uint64(runtime.NumGoroutine()))
}