-
Notifications
You must be signed in to change notification settings - Fork 116
/
profile_dump.go
45 lines (39 loc) · 973 Bytes
/
profile_dump.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
package dbg
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/sirupsen/logrus"
)
const (
path = "/var/cores/"
fnameFmt = "2006-01-02T15:04:05.999999-0700MST"
)
// DumpGoMemoryTrace output memory profile to logs.
func DumpGoMemoryTrace() {
m := &runtime.MemStats{}
runtime.ReadMemStats(m)
res := fmt.Sprintf("%#v", m)
logrus.Infof("==== Dumping Memory Profile ===")
logrus.Infof(res)
}
// DumpGoProfile output goroutines to file.
func DumpGoProfile() error {
trace := make([]byte, 5120*1024)
len := runtime.Stack(trace, true)
return ioutil.WriteFile(path+time.Now().Format(fnameFmt)+".stack", trace[:len], 0644)
}
func DumpHeap() {
f, err := os.Create(path + time.Now().Format(fnameFmt) + ".heap")
if err != nil {
logrus.Errorf("could not create memory profile: %v", err)
return
}
defer f.Close()
if err := pprof.WriteHeapProfile(f); err != nil {
logrus.Errorf("could not write memory profile: %v", err)
}
}