-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.go
123 lines (110 loc) · 3.4 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
118
119
120
121
122
123
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() (err error) {
defer func() { err = p.profileFile.Close() }()
pprof.StopCPUProfile()
return nil
}, nil
}
func heapStrategyFn(p *Profiler) (FinalizerFunc, error) {
rate := runtime.MemProfileRate
p.SetProfileFile(MemoryFileName)
runtime.MemProfileRate = p.memoryProfileRate
return func() (err error) {
defer func() { runtime.MemProfileRate = rate }()
defer func() { err = p.profileFile.Close() }()
_ = pprof.Lookup(heapProfileName).WriteTo(p.profileFile, 0)
runtime.GC()
return nil
}, nil
}
func allocStrategyFn(p *Profiler) (FinalizerFunc, error) {
rate := runtime.MemProfileRate
p.SetProfileFile(MemoryFileName)
runtime.MemProfileRate = p.memoryProfileRate
return func() (err error) {
defer func() { runtime.MemProfileRate = rate }()
defer func() { err = p.profileFile.Close() }()
_ = pprof.Lookup(allocProfileName).WriteTo(p.profileFile, 0)
runtime.GC()
return nil
}, nil
}
func mutexStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(MutexFileName)
_ = pprof.Lookup("mutex").WriteTo(p.profileFile, 0)
return func() error {
return 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() error {
defer runtime.SetBlockProfileRate(0)
_ = pprof.Lookup("block").WriteTo(p.profileFile, 0)
return p.profileFile.Close()
}, nil
}
func goroutineStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(GoroutineFileName)
_ = pprof.Lookup("goroutine").WriteTo(p.profileFile, 0)
return func() error {
return p.profileFile.Close()
}, nil
}
func threadCreateStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(ThreadCreateFileName)
return func() (err error) {
defer func() { err = p.profileFile.Close() }()
_ = pprof.Lookup("threadcreate").WriteTo(p.profileFile, 0)
return nil
}, nil
}
func traceStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(TraceFileName)
if err := trace.Start(p.profileFile); err != nil {
return nil, err
}
return func() error {
trace.Stop()
return nil
}, nil
}
func clockStrategyFn(p *Profiler) (FinalizerFunc, error) {
p.SetProfileFile(ClockFileName)
teardown := fgprof.Start(p.profileFile, fgprof.FormatPprof)
return func() (err error) {
defer func() { err = p.profileFile.Close() }()
return teardown()
}, nil
}