-
Notifications
You must be signed in to change notification settings - Fork 116
/
profile_dump.go
59 lines (51 loc) · 1.36 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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"
)
// GetTimeStamp returns 'readable' timestamp with no spaces 'YYYYMMDDHHMMSS'
func GetTimeStamp() string {
tnow := time.Now()
return fmt.Sprintf("%d%02d%02d%02d%02d%02d",
tnow.Year(), tnow.Month(), tnow.Day(),
tnow.Hour(), tnow.Minute(), tnow.Second())
}
// GetHostNamePrefix return '<hostname>' to use as prefix.
func GetHostNamePrefix() string {
hname, _ := os.Hostname()
return hname
}
// 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+GetHostNamePrefix()+"-"+GetTimeStamp()+".stack", trace[:len], 0644)
}
func DumpHeap() {
f, err := os.Create(path + GetHostNamePrefix() + "-" + GetTimeStamp() + ".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)
}
}