forked from DataDog/dd-trace-go
/
profiler.go
446 lines (413 loc) · 13.6 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package profiler
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"sync"
"time"
"github.com/lannguyen-c0x12c/dd-trace-go/internal"
"github.com/lannguyen-c0x12c/dd-trace-go/internal/log"
"github.com/lannguyen-c0x12c/dd-trace-go/internal/traceprof"
)
// outChannelSize specifies the size of the profile output channel.
const outChannelSize = 5
var (
mu sync.Mutex
activeProfiler *profiler
containerID = internal.ContainerID() // replaced in tests
)
// Start starts the profiler. If the profiler is already running, it will be
// stopped and restarted with the given options.
//
// It may return an error if an API key is not provided by means of the
// WithAPIKey option, or if a hostname is not found.
func Start(opts ...Option) error {
mu.Lock()
defer mu.Unlock()
if activeProfiler != nil {
activeProfiler.stop()
}
p, err := newProfiler(opts...)
if err != nil {
return err
}
activeProfiler = p
activeProfiler.run()
return nil
}
// Stop cancels any ongoing profiling or upload operations and returns after
// everything has been stopped.
func Stop() {
mu.Lock()
if activeProfiler != nil {
activeProfiler.stop()
activeProfiler = nil
}
mu.Unlock()
}
// profiler collects and sends preset profiles to the Datadog API at a given frequency
// using a given configuration.
type profiler struct {
cfg *config // profile configuration
out chan batch // upload queue
uploadFunc func(batch) error // defaults to (*profiler).upload; replaced in tests
exit chan struct{} // exit signals the profiler to stop; it is closed after stopping
stopOnce sync.Once // stopOnce ensures the profiler is stopped exactly once.
wg sync.WaitGroup // wg waits for all goroutines to exit when stopping.
met *metrics // metric collector state
deltas map[ProfileType]deltaProfiler
seq uint64 // seq is the value of the profile_seq tag
pendingProfiles sync.WaitGroup // signal that profile collection is done, for stopping CPU profiling
testHooks testHooks
// lastTrace is the last time an execution trace was collected
lastTrace time.Time
}
func (p *profiler) shouldTrace() bool {
return p.cfg.traceEnabled && time.Since(p.lastTrace) > p.cfg.traceConfig.Period
}
// testHooks are functions that are replaced during testing which would normally
// depend on accessing runtime state that is not needed/available for the test
type testHooks struct {
startCPUProfile func(w io.Writer) error
stopCPUProfile func()
lookupProfile func(name string, w io.Writer, debug int) error
}
func (p *profiler) startCPUProfile(w io.Writer) error {
if p.testHooks.startCPUProfile != nil {
return p.testHooks.startCPUProfile(w)
}
return pprof.StartCPUProfile(w)
}
func (p *profiler) stopCPUProfile() {
if p.testHooks.startCPUProfile != nil {
p.testHooks.stopCPUProfile()
return
}
pprof.StopCPUProfile()
}
func (p *profiler) lookupProfile(name string, w io.Writer, debug int) error {
if p.testHooks.lookupProfile != nil {
return p.testHooks.lookupProfile(name, w, debug)
}
prof := pprof.Lookup(name)
if prof == nil {
return errors.New("profile not found")
}
return prof.WriteTo(w, debug)
}
// newProfiler creates a new, unstarted profiler.
func newProfiler(opts ...Option) (*profiler, error) {
cfg, err := defaultConfig()
if err != nil {
return nil, err
}
for _, opt := range opts {
opt(cfg)
}
// TODO(fg) remove this after making expGoroutineWaitProfile public.
if os.Getenv("DD_PROFILING_WAIT_PROFILE") != "" {
cfg.addProfileType(expGoroutineWaitProfile)
}
// Agentless upload is disabled by default as of v1.30.0, but
// WithAgentlessUpload can be used to enable it for testing and debugging.
if cfg.agentless {
if !isAPIKeyValid(cfg.apiKey) {
return nil, errors.New("profiler.WithAgentlessUpload requires a valid API key. Use profiler.WithAPIKey or the DD_API_KEY env variable to set it")
}
// Always warn people against using this mode for now. All customers should
// use agent based uploading at this point.
log.Warn("profiler.WithAgentlessUpload is currently for internal usage only and not officially supported.")
cfg.targetURL = cfg.apiURL
} else {
// Historically people could use an API Key to enable agentless uploading.
// As of v1.30.0 customers the default behavior is to use agent based
// uploading regardless of the presence of an API key. So if we see an API
// key configured, we warn the customers that this is probably a
// misconfiguration.
if cfg.apiKey != "" {
log.Warn("You are currently setting profiler.WithAPIKey or the DD_API_KEY env variable, but as of dd-trace-go v1.30.0 this value is getting ignored by the profiler. Please see the profiler.WithAPIKey go docs and verify that your integration is still working. If you can't remove DD_API_KEY from your environment, you can use WithAPIKey(\"\") to silence this warning.")
}
cfg.targetURL = cfg.agentURL
}
if cfg.hostname == "" {
hostname, err := os.Hostname()
if err != nil {
if cfg.targetURL == cfg.apiURL {
return nil, fmt.Errorf("could not obtain hostname: %v", err)
}
log.Warn("unable to look up hostname: %v", err)
}
cfg.hostname = hostname
}
// uploadTimeout defaults to DefaultUploadTimeout, but in theory a user might
// set it to 0 or a negative value. However, it's not clear what this should
// mean, and most meanings we could assign seem to be bad: Not having a
// timeout is dangerous, having a timeout that fires immediately breaks
// uploading, and silently defaulting to the default timeout is confusing.
// So let's just stay clear of all of this by not allowing such values.
//
// see similar discussion: https://github.com/golang/go/issues/39177
if cfg.uploadTimeout <= 0 {
return nil, fmt.Errorf("invalid upload timeout, must be > 0: %s", cfg.uploadTimeout)
}
for pt := range cfg.types {
if _, ok := profileTypes[pt]; !ok {
return nil, fmt.Errorf("unknown profile type: %d", pt)
}
}
if cfg.cpuDuration > cfg.period {
cfg.cpuDuration = cfg.period
}
if cfg.logStartup {
logStartup(cfg)
}
p := profiler{
cfg: cfg,
out: make(chan batch, outChannelSize),
exit: make(chan struct{}),
met: newMetrics(),
deltas: make(map[ProfileType]deltaProfiler),
}
for pt := range cfg.types {
if d := profileTypes[pt].DeltaValues; len(d) > 0 {
p.deltas[pt] = newDeltaProfiler(p.cfg, d...)
}
}
p.uploadFunc = p.upload
return &p, nil
}
// run runs the profiler.
func (p *profiler) run() {
profileEnabled := func(t ProfileType) bool {
_, ok := p.cfg.types[t]
return ok
}
if profileEnabled(MutexProfile) {
runtime.SetMutexProfileFraction(p.cfg.mutexFraction)
}
if profileEnabled(BlockProfile) {
runtime.SetBlockProfileRate(p.cfg.blockRate)
}
startTelemetry(p.cfg)
p.wg.Add(1)
go func() {
defer p.wg.Done()
tick := time.NewTicker(p.cfg.period)
defer tick.Stop()
p.met.reset(now()) // collect baseline metrics at profiler start
p.collect(tick.C)
}()
p.wg.Add(1)
go func() {
defer p.wg.Done()
p.send()
}()
}
// collect runs the profile types found in the configuration whenever the ticker receives
// an item.
func (p *profiler) collect(ticker <-chan time.Time) {
defer close(p.out)
var (
// mu guards completed
mu sync.Mutex
completed []*profile
wg sync.WaitGroup
)
// Enable endpoint counting (if configured). This causes some minimal
// overhead to the tracer, see BenchmarkEndpointCounter.
endpointCounter := traceprof.GlobalEndpointCounter()
endpointCounter.SetEnabled(p.cfg.endpointCountEnabled)
// Disable and reset when func returns (profiler stopped) to remove tracer
// overhead, free up the counter map, and avoid it from growing again.
defer func() {
endpointCounter.SetEnabled(false)
endpointCounter.GetAndReset()
}()
for {
bat := batch{
seq: p.seq,
host: p.cfg.hostname,
start: now(),
}
p.seq++
completed = completed[:0]
// We need to increment pendingProfiles for every non-CPU
// profile _before_ entering the next loop so that we know CPU
// profiling will not complete until every other profile is
// finished (because p.pendingProfiles will have been
// incremented to count every non-CPU profile before CPU
// profiling starts)
profileTypes := p.enabledProfileTypes()
if p.shouldTrace() {
profileTypes = append(profileTypes, executionTrace)
}
for _, t := range profileTypes {
if t != CPUProfile {
p.pendingProfiles.Add(1)
}
}
for _, t := range profileTypes {
wg.Add(1)
go func(t ProfileType) {
defer wg.Done()
if t != CPUProfile {
defer p.pendingProfiles.Done()
}
profs, err := p.runProfile(t)
if err != nil {
log.Error("Error getting %s profile: %v; skipping.", t, err)
tags := append(p.cfg.tags.Slice(), t.Tag())
p.cfg.statsd.Count("datadog.profiling.go.collect_error", 1, tags, 1)
}
mu.Lock()
defer mu.Unlock()
completed = append(completed, profs...)
}(t)
}
wg.Wait()
for _, prof := range completed {
bat.addProfile(prof)
}
// Wait until the next profiling period starts or the profiler is stopped.
select {
case <-ticker:
// Usually ticker triggers right away because the non-CPU profiles cause
// the wg.Wait above to sleep until the end of the profiling period.
// Edge case: If only the CPU profile is enabled, and the cpu duration is
// is less than the configured profiling period, the ticker will block
// until the end of the profiling period.
case <-p.exit:
return
}
// Include endpoint hits from tracer in profile `event.json`.
// Also reset the counters for the next profile period.
bat.endpointCounts = endpointCounter.GetAndReset()
// Record the end time of the profile.
// This is used by the backend to upscale the endpoint counts if the cpu
// duration is less than the profile duration. The formula is:
//
// factor = (end - start) / cpuDuration
// counts = counts * factor
//
// The default configuration of the profiler (cpu duration = profiling
// period) results in a factor of 1.
bat.end = time.Now()
// Upload profiling data.
p.enqueueUpload(bat)
}
}
// enabledProfileTypes returns the enabled profile types in a deterministic
// order. The CPU profile always comes first because people might spot
// interesting events in there and then try to look for the counter-part event
// in the mutex/heap/block profile. Deterministic ordering is also important
// for delta profiles, otherwise they'd cover varying profiling periods.
func (p *profiler) enabledProfileTypes() []ProfileType {
order := []ProfileType{
CPUProfile,
HeapProfile,
BlockProfile,
MutexProfile,
GoroutineProfile,
expGoroutineWaitProfile,
MetricsProfile,
executionTrace,
}
enabled := []ProfileType{}
for _, t := range order {
if _, ok := p.cfg.types[t]; ok {
enabled = append(enabled, t)
}
}
return enabled
}
// enqueueUpload pushes a batch of profiles onto the queue to be uploaded. If there is no room, it will
// evict the oldest profile to make some. Typically a batch would be one of each enabled profile.
func (p *profiler) enqueueUpload(bat batch) {
for {
select {
case p.out <- bat:
return // 👍
default:
// queue is full; evict oldest
select {
case <-p.out:
p.cfg.statsd.Count("datadog.profiling.go.queue_full", 1, p.cfg.tags.Slice(), 1)
log.Warn("Evicting one profile batch from the upload queue to make room.")
default:
// this case should be almost impossible to trigger, it would require a
// full p.out to completely drain within nanoseconds or extreme
// scheduling decisions by the runtime.
}
}
}
}
// send takes profiles from the output queue and uploads them.
func (p *profiler) send() {
for {
select {
case <-p.exit:
return
case bat := <-p.out:
if err := p.outputDir(bat); err != nil {
log.Error("Failed to output profile to dir: %v", err)
}
if err := p.uploadFunc(bat); err != nil {
log.Error("Failed to upload profile: %v", err)
}
}
}
}
func (p *profiler) outputDir(bat batch) error {
if p.cfg.outputDir == "" {
return nil
}
// Basic ISO 8601 Format in UTC as the name for the directories.
dir := bat.end.UTC().Format("20060102T150405Z")
dirPath := filepath.Join(p.cfg.outputDir, dir)
// 0755 is what mkdir does, should be reasonable for the use cases here.
if err := os.MkdirAll(dirPath, 0755); err != nil {
return err
}
for _, prof := range bat.profiles {
filePath := filepath.Join(dirPath, prof.name)
// 0644 is what touch does, should be reasonable for the use cases here.
if err := os.WriteFile(filePath, prof.data, 0644); err != nil {
return err
}
}
return nil
}
// interruptibleSleep sleeps for the given duration or until interrupted by the
// p.exit channel being closed.
func (p *profiler) interruptibleSleep(d time.Duration) {
select {
case <-p.exit:
case <-time.After(d):
}
}
// stop stops the profiler.
func (p *profiler) stop() {
p.stopOnce.Do(func() {
close(p.exit)
})
p.wg.Wait()
if p.cfg.logStartup {
log.Info("Profiling stopped")
}
}
// StatsdClient implementations can count and time certain event occurrences that happen
// in the profiler.
type StatsdClient interface {
// Count counts how many times an event happened, at the given rate using the given tags.
Count(event string, times int64, tags []string, rate float64) error
// Timing creates a histogram metric of the values registered as the duration of a certain event.
Timing(event string, duration time.Duration, tags []string, rate float64) error
}