/
easypprof.go
197 lines (171 loc) · 4.13 KB
/
easypprof.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package easypprof
import (
"fmt"
"io"
"os"
"path"
"runtime"
"runtime/pprof"
"runtime/trace"
"time"
"github.com/felixge/fgprof"
)
// Profile modes.
const (
CpuMode = "cpu"
TraceMode = "trace"
HeapMode = "heap"
AllocsMode = "allocs"
MutexMode = "mutex"
BlockMode = "block"
ThreadCreateMode = "threadcreate"
GoroutineMode = "goroutine"
FgprofMode = "fgprof"
)
// Start profiling based on a config.
func Start(cfg Config) *Profiler {
p, err := newProfiler(cfg)
if err != nil {
panic(fmt.Errorf("easypprof: %w", err))
}
if err := p.run(); err != nil {
panic(fmt.Errorf("easypprof: %w", err))
}
return p
}
// Profiler for the Go programs.
type Profiler struct {
cfg Config
mode string
output io.WriteCloser
fgprofStop func() error // a bit hacky but simple
memProfileRate int
blockProfileRate int
mutexProfileFraction int
fgprofFormat fgprof.Format
}
// Config of the profiler.
type Config struct {
// Disable the profiler. Easy to set as a command-line flag.
// Default is false.
Disable bool
// Mode is one of cpu, goroutine, threadcreate, heap, allocs, block, mutex.
// Default is empty string and is treated as cpu.
Mode string
// OutputDir where profile must be saved.
// Default is empty string and is treated as ".".
OutputDir string
// FilePrefix for the output file.
// If prefix is specified an additional '_' will be added to the prefix.
FilePrefix string
// UseTextFormat of the resulting pprof file.
// Default is false.
UseTextFormat bool
// FgprofFormat is a format of the output file.
// Default is fgprof.FormatPprof.
FgprofFormat string
// MutexProfileFraction represents the fraction of mutex contention events.
// Default is 10.
MutexProfileFraction int
// BlockProfileRate represents the fraction of goroutine blocking events.
// Default is 10_000.
BlockProfileRate int
}
// newProfiler creates new profiler based on a config.
func newProfiler(cfg Config) (*Profiler, error) {
if cfg.Mode == "" {
cfg.Mode = CpuMode
}
switch cfg.Mode {
case CpuMode, TraceMode, HeapMode, AllocsMode, MutexMode,
BlockMode, ThreadCreateMode, GoroutineMode, FgprofMode:
// pass
default:
return nil, fmt.Errorf("unknown profile mode: %s", cfg.Mode)
}
if cfg.OutputDir == "" {
cfg.OutputDir = "."
}
if cfg.FgprofFormat == "" {
cfg.FgprofFormat = string(fgprof.FormatPprof)
}
if cfg.MutexProfileFraction == 0 {
cfg.MutexProfileFraction = 10
}
if cfg.BlockProfileRate == 0 {
cfg.BlockProfileRate = 10_000
}
var prefix string
if cfg.FilePrefix != "" {
prefix = cfg.FilePrefix + "_"
}
now := time.Now().Format("20060102-15:04:05")
filename := fmt.Sprintf("%s%s_%s.pprof", prefix, cfg.Mode, now)
if err := os.MkdirAll(cfg.OutputDir, 0777); err != nil {
return nil, err
}
output, err := os.Create(path.Join(cfg.OutputDir, filename))
if err != nil {
return nil, err
}
p := &Profiler{
cfg: cfg,
mode: cfg.Mode,
output: output,
}
return p, nil
}
// Run the profiler.
func (p *Profiler) run() error {
switch p.mode {
case CpuMode:
return pprof.StartCPUProfile(p.output)
case TraceMode:
return trace.Start(p.output)
case MutexMode:
runtime.SetMutexProfileFraction(p.mutexProfileFraction)
case BlockMode:
runtime.SetBlockProfileRate(p.blockProfileRate)
case FgprofMode:
p.fgprofStop = fgprof.Start(p.output, p.fgprofFormat)
case HeapMode, AllocsMode, ThreadCreateMode, GoroutineMode:
// pass
}
return nil
}
// Stop the profiler and save the result.
func (p *Profiler) Stop() error {
switch p.mode {
case CpuMode:
pprof.StopCPUProfile()
case TraceMode:
trace.Stop()
case MutexMode:
runtime.SetMutexProfileFraction(0)
case BlockMode:
runtime.SetBlockProfileRate(0)
case FgprofMode:
if err := p.fgprofStop(); err != nil {
return err
}
}
switch p.mode {
case CpuMode, TraceMode, FgprofMode:
// skip
default:
profile := pprof.Lookup(p.mode)
if err := profile.WriteTo(p.output, bool2int(p.cfg.UseTextFormat)); err != nil {
return err
}
}
if err := p.output.Close(); err != nil {
return err
}
return nil
}
func bool2int(b bool) int {
if b {
return 1
}
return 0
}