forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
82 lines (73 loc) · 1.74 KB
/
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
package stats
import (
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"time"
log "github.com/sirupsen/logrus"
)
// StartStatsTicker starts a goroutine which dumps stats at a specified interval
func StartStatsTicker(d time.Duration) {
ticker := time.NewTicker(d)
go func() {
for {
<-ticker.C
LogStats()
}
}()
}
// RegisterStackDumper spawns a goroutine which dumps stack trace upon a SIGUSR1
func RegisterStackDumper() {
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGUSR1)
for {
<-sigs
LogStack()
}
}()
}
// RegisterHeapDumper spawns a goroutine which dumps heap profile upon a SIGUSR2
func RegisterHeapDumper(filePath string) {
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGUSR2)
for {
<-sigs
runtime.GC()
if _, err := os.Stat(filePath); err == nil {
err = os.Remove(filePath)
if err != nil {
log.Warnf("could not delete heap profile file: %v", err)
return
}
}
f, err := os.Create(filePath)
if err != nil {
log.Warnf("could not create heap profile file: %v", err)
return
}
if err := pprof.WriteHeapProfile(f); err != nil {
log.Warnf("could not write heap profile: %v", err)
return
} else {
log.Infof("dumped heap profile to %s", filePath)
}
}
}()
}
// LogStats logs runtime statistics
func LogStats() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Infof("Alloc=%v TotalAlloc=%v Sys=%v NumGC=%v Goroutines=%d",
m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC, runtime.NumGoroutine())
}
// LogStack will log the current stack
func LogStack() {
buf := make([]byte, 1<<20)
stacklen := runtime.Stack(buf, true)
log.Infof("*** goroutine dump...\n%s\n*** end\n", buf[:stacklen])
}