-
Notifications
You must be signed in to change notification settings - Fork 175
/
profiler.go
339 lines (283 loc) · 8.81 KB
/
profiler.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package profiler
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
"github.com/google/pprof/profile"
"github.com/rs/zerolog"
"go.uber.org/atomic"
"go.uber.org/multierr"
pb "google.golang.org/genproto/googleapis/devtools/cloudprofiler/v2"
"github.com/onflow/flow-go/engine"
)
type timedProfileFunc func(io.Writer, time.Duration) error
type profileDef struct {
profileName string
profileType pb.ProfileType
profileFunc timedProfileFunc
}
// ProfilerConfig profiler parameters.
type ProfilerConfig struct {
Enabled bool
UploaderEnabled bool
Dir string
Interval time.Duration
Duration time.Duration
}
type AutoProfiler struct {
unit *engine.Unit
dir string
log zerolog.Logger
interval time.Duration
duration time.Duration
uploader Uploader
enabled *atomic.Bool
// used to trigger a profile run for a given duration
trigger chan time.Duration
}
// New creates a new AutoProfiler instance performing profiling every interval for duration.
func New(log zerolog.Logger, uploader Uploader, cfg ProfilerConfig) (*AutoProfiler, error) {
err := os.MkdirAll(cfg.Dir, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("could not create profile dir %v: %w", cfg.Dir, err)
}
// add 50% jitter to the interval
jitter := time.Duration(rand.Int63n(int64(cfg.Interval)))
interval := cfg.Interval/2 + jitter
p := &AutoProfiler{
unit: engine.NewUnit(),
log: log.With().Str("component", "profiler").Logger(),
dir: cfg.Dir,
interval: interval,
duration: cfg.Duration,
uploader: uploader,
enabled: atomic.NewBool(cfg.Enabled),
trigger: make(chan time.Duration),
}
go p.runForever()
return p, nil
}
// SetEnabled sets whether the profiler is active.
// No errors are expected during normal operation.
func (p *AutoProfiler) SetEnabled(enabled bool) error {
p.enabled.Store(enabled)
return nil
}
// Enabled returns the current enabled state of the profiler.
func (p *AutoProfiler) Enabled() bool {
return p.enabled.Load()
}
// TriggerRun manually triggers a profile run if one is not already running.
func (p *AutoProfiler) TriggerRun(d time.Duration) error {
select {
case p.trigger <- d:
return nil
default:
return errors.New("profiling is already in progress")
}
}
func (p *AutoProfiler) runForever() {
t := time.NewTicker(p.interval)
defer t.Stop()
for {
select {
case <-t.C:
if p.Enabled() {
p.runOnce(p.duration)
}
case d := <-p.trigger:
p.runOnce(d)
case <-p.unit.Quit():
return
}
}
}
func (p *AutoProfiler) Ready() <-chan struct{} {
if p.Enabled() {
p.log.Info().Dur("duration", p.duration).Time("nextRunAt", time.Now().Add(p.interval)).Msg("AutoProfiler has started")
} else {
p.log.Info().Msg("AutoProfiler has started, profiler is disabled")
}
return p.unit.Ready()
}
func (p *AutoProfiler) Done() <-chan struct{} {
return p.unit.Done()
}
func (p *AutoProfiler) runOnce(d time.Duration) {
startTime := time.Now()
p.log.Info().Msg("starting profile trace")
for _, prof := range [...]profileDef{
{profileName: "goroutine", profileType: pb.ProfileType_THREADS, profileFunc: func(w io.Writer, _ time.Duration) error { return newProfileFunc("goroutine")(w) }},
{profileName: "heap", profileType: pb.ProfileType_HEAP, profileFunc: p.pprofHeap},
{profileName: "allocs", profileType: pb.ProfileType_HEAP_ALLOC, profileFunc: p.pprofAllocs},
{profileName: "block", profileType: pb.ProfileType_CONTENTION, profileFunc: p.pprofBlock},
{profileName: "cpu", profileType: pb.ProfileType_WALL, profileFunc: p.pprofCpu},
} {
path := filepath.Join(p.dir, fmt.Sprintf("%s-%s", prof.profileName, time.Now().Format(time.RFC3339)))
logger := p.log.With().Str("profileName", prof.profileName).Str("profilePath", path).Logger()
logger.Info().Str("file", path).Msg("capturing")
f, err := os.CreateTemp(p.dir, "profile")
if err != nil {
logger.Err(err).Msg("failed to create temp profile")
continue
}
logger = logger.With().Str("tempFile", f.Name()).Logger()
// Remove temp file if it still exists.
defer func(logger zerolog.Logger, tmpName string) {
if _, err := os.Stat(tmpName); errors.Is(err, os.ErrNotExist) {
return
}
if err := os.Remove(tmpName); err != nil {
logger.Warn().Err(err).Msg("failed to remove profile")
}
}(logger, f.Name())
err = p.pprof(f, prof.profileFunc, d)
if err != nil {
logger.Error().Err(err).Msg("failed to generate profile")
continue
}
// default CreateTemp permissions are 0600.
err = os.Chmod(f.Name(), 0644)
if err != nil {
logger.Error().Err(err).Msg("failed to set profile permissions")
continue
}
err = os.Rename(f.Name(), path)
if err != nil {
logger.Error().Err(err).Msg("failed to rename profile")
continue
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
err = p.uploader.Upload(ctx, path, prof.profileType)
if err != nil {
logger.Warn().Err(err).Msg("failed to upload profile")
continue
}
}
p.log.Info().Dur("duration", time.Since(startTime)).Msg("finished profile trace")
}
func (p *AutoProfiler) pprof(f *os.File, fn timedProfileFunc, d time.Duration) (err error) {
defer func() {
multierr.AppendInto(&err, f.Close())
}()
return fn(f, d)
}
type profileFunc func(io.Writer) error
func newProfileFunc(name string) profileFunc {
return func(w io.Writer) error {
return pprof.Lookup(name).WriteTo(w, 0)
}
}
func (p *AutoProfiler) goHeapProfile(sampleTypes ...string) (*profile.Profile, error) {
if len(sampleTypes) == 0 {
return nil, fmt.Errorf("no sample types specified")
}
// Forces the GC before taking each of the heap profiles and improves the profile accuracy.
// Autoprofiler runs very infrequently so performance impact is minimal.
runtime.GC()
buf := &bytes.Buffer{}
err := newProfileFunc("heap")(buf)
if err != nil {
return nil, fmt.Errorf("failed to generate allocs profile: %w", err)
}
prof, err := profile.Parse(buf)
if err != nil {
return nil, fmt.Errorf("failed to parse allocs profile: %w", err)
}
prof.TimeNanos = time.Now().UnixNano()
selectedSampleTypes := make([]int, 0, len(sampleTypes))
for _, name := range sampleTypes {
for i, sampleType := range prof.SampleType {
if sampleType.Type == name {
selectedSampleTypes = append(selectedSampleTypes, i)
}
}
}
if len(selectedSampleTypes) != len(sampleTypes) {
return nil, fmt.Errorf("failed to find all sample types: want: %+v, got: %+v", sampleTypes, prof.SampleType)
}
newSampleType := make([]*profile.ValueType, 0, len(selectedSampleTypes))
for _, j := range selectedSampleTypes {
newSampleType = append(newSampleType, prof.SampleType[j])
}
prof.SampleType = newSampleType
prof.DefaultSampleType = prof.SampleType[0].Type
for _, s := range prof.Sample {
newValue := make([]int64, 0, len(selectedSampleTypes))
for _, j := range selectedSampleTypes {
newValue = append(newValue, s.Value[j])
}
s.Value = newValue
}
// Merge profile with itself to remove empty samples.
prof, err = profile.Merge([]*profile.Profile{prof})
if err != nil {
return nil, fmt.Errorf("failed to merge profile: %w", err)
}
return prof, nil
}
// pprofHeap produces cumulative heap profile since the program start.
func (p *AutoProfiler) pprofHeap(w io.Writer, _ time.Duration) error {
prof, err := p.goHeapProfile("inuse_objects", "inuse_space")
if err != nil {
return fmt.Errorf("failed to get heap profile: %w", err)
}
return prof.Write(w)
}
// pprofAllocs produces differential allocs profile for the given duration.
func (p *AutoProfiler) pprofAllocs(w io.Writer, d time.Duration) (err error) {
p1, err := p.goHeapProfile("alloc_objects", "alloc_space")
if err != nil {
return fmt.Errorf("failed to get allocs profile: %w", err)
}
select {
case <-time.After(d):
case <-p.unit.Quit():
return context.Canceled
}
p2, err := p.goHeapProfile("alloc_objects", "alloc_space")
if err != nil {
return fmt.Errorf("failed to get allocs profile: %w", err)
}
// multiply values by -1 and merge to get differential profile.
p1.Scale(-1)
diff, err := profile.Merge([]*profile.Profile{p1, p2})
if err != nil {
return fmt.Errorf("failed to merge allocs profiles: %w", err)
}
diff.TimeNanos = time.Now().UnixNano()
diff.DurationNanos = d.Nanoseconds()
return diff.Write(w)
}
func (p *AutoProfiler) pprofBlock(w io.Writer, d time.Duration) error {
runtime.SetBlockProfileRate(100)
defer runtime.SetBlockProfileRate(0)
select {
case <-time.After(d):
return newProfileFunc("block")(w)
case <-p.unit.Quit():
return context.Canceled
}
}
func (p *AutoProfiler) pprofCpu(w io.Writer, d time.Duration) error {
err := pprof.StartCPUProfile(w)
if err != nil {
return fmt.Errorf("failed to start CPU profile: %w", err)
}
defer pprof.StopCPUProfile()
select {
case <-time.After(d):
return nil
case <-p.unit.Quit():
return context.Canceled
}
}