-
Notifications
You must be signed in to change notification settings - Fork 0
/
peak.go
121 lines (106 loc) · 2.05 KB
/
peak.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package peak
import (
"fmt"
"os"
"runtime"
"sync"
"time"
)
var m2 sync.RWMutex
var interval = time.Second * 1
var m sync.RWMutex
var peakMemory uint64
var peakGoRoutines uint64
var peakFileDescriptors uint64
func init() {
go update()
}
func update() {
for {
updatePeakMemory()
updatePeakGoroutines()
updateFileDescriptors()
m2.RLock()
time.Sleep(interval)
m2.RUnlock()
}
}
func updatePeakMemory() {
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
m.Lock()
defer m.Unlock()
if mem.Alloc > peakMemory {
peakMemory = mem.Alloc
}
}
func updatePeakGoroutines() {
count := uint64(runtime.NumGoroutine())
m.Lock()
defer m.Unlock()
if count > peakGoRoutines {
peakGoRoutines = count
}
}
func updateFileDescriptors() {
var fdPath string
switch runtime.GOOS {
case "darwin":
fdPath = "/dev/fd"
case "linux", "freebsd":
fdPath = "/proc/self/fd"
default:
return
}
entries, _ := os.ReadDir(fdPath)
m.Lock()
defer m.Unlock()
if peakFileDescriptors < uint64(len(entries)) {
peakFileDescriptors = uint64(len(entries))
}
}
// PeakMemory return highest number of bytes in use
func PeakMemory() uint64 {
m.RLock()
defer m.RUnlock()
return peakMemory
}
// PeakMemoryString return highest memory usage in human readable string
func PeakMemoryString() string {
m.RLock()
defer m.RUnlock()
mb := float64(peakMemory) / 1024 / 1024
if mb < 1024 {
return fmt.Sprintf("%.2f MB", mb)
} else {
return fmt.Sprintf("%.2f GB", mb/1024)
}
}
// PeakGoRoutines return highest number of observed goroutines
func PeakGoRoutines() uint64 {
m.RLock()
defer m.RUnlock()
return peakGoRoutines
}
// PeakFileDescriptors return highest number of observed file descriptors
func PeakFileDescriptors() uint64 {
m.RLock()
defer m.RUnlock()
return peakFileDescriptors
}
// Reset set metrics back to 0
func Reset() {
m.Lock()
defer m.Unlock()
peakMemory = 0
peakGoRoutines = 0
peakFileDescriptors = 0
}
// SetInterval override default sampling interval
func SetInterval(dur time.Duration) {
if dur > 0 {
m2.Lock()
interval = dur
m2.Unlock()
}
}