/
util.go
67 lines (59 loc) · 1.15 KB
/
util.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
package gc
import (
"os"
"runtime"
"runtime/pprof"
"strconv"
)
func (n *Node) Line() string {
return Ctxt.LineHist.LineString(int(n.Lineno))
}
func atoi(s string) int {
// NOTE: Not strconv.Atoi, accepts hex and octal prefixes.
n, _ := strconv.ParseInt(s, 0, 0)
return int(n)
}
var atExitFuncs []func()
func AtExit(f func()) {
atExitFuncs = append(atExitFuncs, f)
}
func Exit(code int) {
for i := len(atExitFuncs) - 1; i >= 0; i-- {
f := atExitFuncs[i]
atExitFuncs = atExitFuncs[:i]
f()
}
os.Exit(code)
}
var (
cpuprofile string
memprofile string
memprofilerate int64
)
func startProfile() {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
Fatalf("%v", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
Fatalf("%v", err)
}
AtExit(pprof.StopCPUProfile)
}
if memprofile != "" {
if memprofilerate != 0 {
runtime.MemProfileRate = int(memprofilerate)
}
f, err := os.Create(memprofile)
if err != nil {
Fatalf("%v", err)
}
AtExit(func() {
runtime.GC() // profile all outstanding allocations
if err := pprof.WriteHeapProfile(f); err != nil {
Fatalf("%v", err)
}
})
}
}