This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
memory_reporter.go
81 lines (66 loc) · 3.05 KB
/
memory_reporter.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
package stats
import (
"os"
"runtime"
"strconv"
"time"
"github.com/grafana/metrictank/util"
)
// MemoryReporter sources memory stats from the runtime and reports them
// It also reports gcPercent based on the GOGC environment variable
type MemoryReporter struct {
mem runtime.MemStats
gcCyclesTotal uint32
timeBoundGetMemStats func() interface{}
}
func NewMemoryReporter() *MemoryReporter {
reporter := registry.getOrAdd("memory", &MemoryReporter{}).(*MemoryReporter)
reporter.timeBoundGetMemStats = util.TimeBoundWithCacheFunc(func() interface{} {
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
return mem
}, 5*time.Second, 1*time.Minute)
return reporter
}
func getGcPercent() int {
// follow standard runtime:
// unparseable or not set -> 100
// "off" -> -1
gogc := os.Getenv("GOGC")
if gogc == "" {
return 100
}
if gogc == "off" {
return -1
}
val, err := strconv.Atoi(gogc)
if err != nil {
return 100
}
return val
}
func (m *MemoryReporter) WriteGraphiteLine(buf, prefix []byte, now time.Time) []byte {
m.mem = m.timeBoundGetMemStats().(runtime.MemStats)
gcPercent := getGcPercent()
// metric memory.total_bytes_allocated is a counter of total number of bytes allocated during process lifetime
buf = WriteUint64(buf, prefix, []byte("memory.total_bytes_allocated.counter64"), nil, nil, m.mem.TotalAlloc, now)
// metric memory.bytes_allocated_on_heap is a gauge of currently allocated (within the runtime) memory.
buf = WriteUint64(buf, prefix, []byte("memory.bytes.allocated_in_heap.gauge64"), nil, nil, m.mem.Alloc, now)
// metric memory.bytes.obtained_from_sys is the number of bytes currently obtained from the system by the process. This is what the profiletrigger looks at.
buf = WriteUint64(buf, prefix, []byte("memory.bytes.obtained_from_sys.gauge64"), nil, nil, m.mem.Sys, now)
// metric memory.total_gc_cycles is a counter of the number of GC cycles since process start
buf = WriteUint32(buf, prefix, []byte("memory.total_gc_cycles.counter64"), nil, nil, m.mem.NumGC, now)
// metric memory.gc.cpu_fraction is how much cpu is consumed by the GC across process lifetime, in pro-mille
buf = WriteUint32(buf, prefix, []byte("memory.gc.cpu_fraction.gauge32"), nil, nil, uint32(1000*m.mem.GCCPUFraction), now)
// metric memory.gc.heap_objects is how many objects are allocated on the heap, it's a key indicator for GC workload
buf = WriteUint64(buf, prefix, []byte("memory.gc.heap_objects.gauge64"), nil, nil, m.mem.HeapObjects, now)
// there was no new GC run, we should only report points to represent actual runs
if m.gcCyclesTotal != m.mem.NumGC {
// metric memory.gc.last_duration is the duration of the last GC STW pause in nanoseconds
buf = WriteUint64(buf, prefix, []byte("memory.gc.last_duration.gauge64"), nil, nil, m.mem.PauseNs[(m.mem.NumGC+255)%256], now)
m.gcCyclesTotal = m.mem.NumGC
}
// metric memory.gc.gogc is the current GOGC value (derived from the GOGC environment variable)
buf = WriteInt32(buf, prefix, []byte("memory.gc.gogc.sgauge32"), nil, nil, int32(gcPercent), now)
return buf
}