/
runtime_stats.go
105 lines (93 loc) · 2.33 KB
/
runtime_stats.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Copyright 2018 gd Author. All Rights Reserved.
* Author: Chuck1024
*/
package helper
import (
"encoding/json"
"fmt"
"github.com/chuck1024/gd/utls"
"runtime"
"time"
)
func nsToMs(ns uint64) float64 {
return float64(ns) / float64(time.Millisecond/time.Nanosecond)
}
func gcPause(m1, m2 *runtime.MemStats) (p []uint64) {
gc := m2.NumGC - m1.NumGC
if gc > 0 {
for i := uint32(0); i < gc; i++ {
p = append(p, m2.PauseNs[(m2.NumGC-i+255)%256])
}
}
return
}
func calcMaxAvg(a []uint64) (uint64, uint64) {
if len(a) == 0 {
return 0, 0
}
var max, avg uint64
for _, u := range a {
if u > max {
max = u
}
avg += u
}
return max, avg / uint64(len(a))
}
func formatGCPause(m1, m2 *runtime.MemStats) string {
p := gcPause(m1, m2)
max, avg := calcMaxAvg(p)
return fmt.Sprintf("%.2f+%.2fms", nsToMs(max), nsToMs(avg))
}
func FormatMemStats(m1, m2 *runtime.MemStats) string {
return fmt.Sprintf("Goroutines: %d, GCPause: %s, HeapObjects: %d, HeapAlloc: %s, Mallocs: %d",
runtime.NumGoroutine(), formatGCPause(m1, m2), m2.HeapObjects, utls.HumanSize(m2.HeapAlloc), m2.Mallocs)
}
// get runtime stats,contained goroutine numbers,gc pause,heap objects,heap alloc and malloc
func RuntimeStats(d time.Duration, f func(string)) {
var m1, m2 runtime.MemStats
runtime.ReadMemStats(&m1)
for {
time.Sleep(d)
runtime.ReadMemStats(&m2)
s := FormatMemStats(&m1, &m2)
if f == nil {
fmt.Println(s)
} else {
f(s)
}
m1 = m2
}
}
func GcStats() string {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// in ms
lastGC := time.Unix(int64(m.LastGC/1000/1000/1000), 0)
pauseNs := m.PauseNs
numGC := m.NumGC
lastPause := int64(pauseNs[(numGC+255)%256] / 1000 / 1000)
gcStats := fmt.Sprintf("%dms,%s", lastPause, lastGC)
return gcStats
}
func MemStats2() string {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// in mb
memStats, _ := json.Marshal(m)
return string(memStats)
}
func MemStats() string {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// in mb
alloc := int(m.Alloc / 1024 / 1024)
sys := int(m.Sys / 1024 / 1024)
heapSys := int(m.HeapSys / 1024 / 1024)
heapAlloc := int(m.HeapAlloc / 1024 / 1024)
heapIdle := int(m.HeapIdle / 1024 / 1024)
heapReleased := int(m.HeapReleased / 1024 / 1024)
memStats := fmt.Sprintf("%d,%d,%d,%d,%d,%d", sys, alloc, heapSys, heapAlloc, heapIdle, heapReleased)
return memStats
}