-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprofile.go
83 lines (74 loc) · 1.43 KB
/
profile.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
package utils
// adapted from https://github.com/henrycg/prio/master/utils/profile.go
import (
"log"
"os"
"os/signal"
"runtime"
"runtime/pprof"
)
func StartProfiling(filename string) {
f, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
// Stop on ^C
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, os.Kill)
go func() {
for range c {
// sig is a ^C, handle it
pprof.StopCPUProfile()
os.Exit(0)
}
}()
}
func StopProfiling() {
// Stop when process exits
pprof.StopCPUProfile()
}
func writeMemProfile(filename string) {
f, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
log.Printf("Writing memory profile")
pprof.WriteHeapProfile(f)
f.Close()
}
func StartMemProfiling(filename string) {
// Stop on ^C
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
// sig is a ^C, handle it
writeMemProfile(filename)
os.Exit(0)
}
}()
}
func writeBlockProfile(filename string) {
f, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
log.Printf("Writing block profile")
pprof.Lookup("block").WriteTo(f, 0)
f.Close()
}
func StartBlockProfiling(filename string) {
// Stop on ^C
runtime.SetBlockProfileRate(1)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
// sig is a ^C, handle it
writeBlockProfile(filename)
os.Exit(0)
}
}()
}