-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
117 lines (104 loc) · 3.05 KB
/
strategy.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package profiler
import (
"runtime"
"runtime/pprof"
"runtime/trace"
"github.com/felixge/fgprof"
)
// StrategyFunc is the custom type for an implementation
// that controls pre/post profiling setup and teardown.
type StrategyFunc func(p *Profiler) (FinalizerFunc, error)
var StrategyMap = map[Mode]StrategyFunc{
CPUMode: cpuStrategyFn,
MemoryHeapMode: heapStrategyFn,
MemoryAllocMode: allocStrategyFn,
MutexMode: mutexStrategyFn,
BlockMode: blockStrategyFn,
GoroutineMode: goroutineStrategyFn,
ThreadCreateMode: threadCreateStrategyFn,
TraceMode: traceStrategyFn,
ClockMode: clockStrategyFn,
}
// cpuStrategyFn handles configuring the cpu profiler and
// deferring it's teardown.
// the output of using this strategy is a `cpu.pprof`
// file written to disk.
func cpuStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(CPUFileName)
if err := pprof.StartCPUProfile(p.profileFile); err != nil {
return nil, err
}
return func() {
pprof.StopCPUProfile()
p.profileFile.Close()
}, nil
}
func heapStrategyFn(p *Profiler) (FinalizerFunc, error) {
rate := runtime.MemProfileRate
p.SetProfileFile(MemoryFileName)
runtime.MemProfileRate = p.memoryProfileRate
return func() {
_ = pprof.Lookup(heapProfileName).WriteTo(p.profileFile, 0)
runtime.GC()
p.profileFile.Close()
runtime.MemProfileRate = rate
}, nil
}
func allocStrategyFn(p *Profiler) (FinalizerFunc, error) {
rate := runtime.MemProfileRate
p.SetProfileFile(MemoryFileName)
runtime.MemProfileRate = p.memoryProfileRate
return func() {
_ = pprof.Lookup(allocProfileName).WriteTo(p.profileFile, 0)
runtime.GC()
p.profileFile.Close()
runtime.MemProfileRate = rate
}, nil
}
func mutexStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(MutexFileName)
_ = pprof.Lookup("mutex").WriteTo(p.profileFile, 0)
return func() {
p.profileFile.Close()
}, nil
}
func blockStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(BlockFileName)
// for now, we do not allow customising the runtime.SetBlockProfileRate
// if it is useful in future, change is welcome here.
return func() {
_ = pprof.Lookup("block").WriteTo(p.profileFile, 0)
p.profileFile.Close()
runtime.SetBlockProfileRate(0)
}, nil
}
func goroutineStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(GoroutineFileName)
_ = pprof.Lookup("goroutine").WriteTo(p.profileFile, 0)
return func() {
p.profileFile.Close()
}, nil
}
func threadCreateStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(ThreadCreateFileName)
return func() {
_ = pprof.Lookup("threadcreate").WriteTo(p.profileFile, 0)
p.profileFile.Close()
}, nil
}
func traceStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(TraceFileName)
trace.Start(p.profileFile)
return func() {
p.profileFile.Close()
trace.Stop()
}, nil
}
func clockStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(ClockFileName)
teardown := fgprof.Start(p.profileFile, fgprof.FormatPprof)
return func() {
p.profileFile.Close()
teardown()
}, nil
}