-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpprof.go
More file actions
58 lines (52 loc) · 1.04 KB
/
pprof.go
File metadata and controls
58 lines (52 loc) · 1.04 KB
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
package main
import (
"os"
"runtime"
"runtime/pprof"
)
type ProfInstance struct {
memprof, cpuprof *os.File
}
func NewProf(memprof, cpuprof string) (p *ProfInstance, err error) {
p = &ProfInstance{}
if memprof != "" {
if p.memprof, err = os.Create(memprof); err != nil {
p = nil
return
}
}
if cpuprof != "" {
if p.cpuprof, err = os.Create(cpuprof); err != nil {
// close all files on error
if p.memprof != nil {
p.memprof.Close()
}
p = nil
return
}
}
return
}
// startProfiling enables memory and/or CPU profiling if the
// appropriate command line flags have been set.
func (p *ProfInstance) Start() {
// if we've passed in filenames to dump profiling data too,
// start collecting profiling data.
if p.memprof != nil {
runtime.MemProfileRate = 1
}
if p.cpuprof != nil {
pprof.StartCPUProfile(p.cpuprof)
}
}
func (p *ProfInstance) Stop() {
if p.memprof != nil {
runtime.GC()
pprof.WriteHeapProfile(p.memprof)
p.memprof.Close()
}
if p.cpuprof != nil {
pprof.StopCPUProfile()
p.cpuprof.Close()
}
}