/
mstats.go
46 lines (37 loc) · 944 Bytes
/
mstats.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
package mstats
import (
"runtime"
"strconv"
"sync/atomic"
"time"
)
// Var is an atomic variable satisfying expvar.Var
type Var struct {
atomic.Value
}
func (a *Var) String() string {
v := a.Load()
if v == nil {
return "0"
}
return strconv.FormatUint(v.(uint64), 10)
}
// PauseNS is the total number of nanoseconds the GC has paused the application
var PauseNS Var
// NumGC is the number of collections
var NumGC Var
// Alloc is the number of bytes allocated and not yet freed by the application
var Alloc Var
// TotalAlloc is the total number of bytes allocated by the application
var TotalAlloc Var
// Start polls runtime.ReadMemStats with interval d and updates the package level variables
func Start(d time.Duration) {
for range time.Tick(d) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
PauseNS.Store(m.PauseTotalNs)
Alloc.Store(m.Alloc)
TotalAlloc.Store(m.TotalAlloc)
NumGC.Store(uint64(m.NumGC))
}
}