-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_pprof.go
56 lines (49 loc) · 1.1 KB
/
main_pprof.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
//go:build !no_pprof
// +build !no_pprof
package main
import (
"flag"
"os"
"runtime/pprof"
"fortio.org/log"
)
var (
cpuprofile = flag.String("profile-cpu", "", "write cpu profile to `file`")
memprofile = flag.String("profile-mem", "", "write memory profile to `file`")
)
func init() {
hookBefore = pprofBeforeHook
hookAfter = pprofAfterHook
}
func pprofBeforeHook() int {
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
return log.FErrf("can't open file for cpu profile: %v", err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
return log.FErrf("can't start cpu profile: %v", err)
}
log.Infof("Writing cpu profile to %s", *cpuprofile)
}
return 0
}
func pprofAfterHook() int {
if *cpuprofile != "" {
pprof.StopCPUProfile()
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
return log.FErrf("can't open file for mem profile: %v", err)
}
err = pprof.WriteHeapProfile(f)
if err != nil {
return log.FErrf("can't write mem profile: %v", err)
}
log.Infof("Wrote memory profile to %s", *memprofile)
f.Close()
}
return 0
}