/
profiler.go
685 lines (593 loc) · 18.8 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// Copyright (c) 2022 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package profiler
import (
"bytes"
"context"
_ "embed"
"encoding/binary"
"errors"
"fmt"
"os"
"runtime"
"runtime/debug"
"strings"
"sync"
"syscall"
"time"
"unsafe"
"C" //nolint:typecheck
bpf "github.com/aquasecurity/libbpfgo"
"github.com/dustin/go-humanize"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/google/pprof/profile"
profilestorepb "github.com/parca-dev/parca/gen/proto/go/parca/profilestore/v1alpha1"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/common/model"
"golang.org/x/sys/unix"
"github.com/parca-dev/parca-agent/pkg/agent"
"github.com/parca-dev/parca-agent/pkg/byteorder"
"github.com/parca-dev/parca-agent/pkg/debuginfo"
"github.com/parca-dev/parca-agent/pkg/ksym"
"github.com/parca-dev/parca-agent/pkg/maps"
"github.com/parca-dev/parca-agent/pkg/objectfile"
"github.com/parca-dev/parca-agent/pkg/perf"
)
//go:embed parca-agent.bpf.o
var bpfObj []byte
const (
stackDepth = 127 // Always needs to be sync with MAX_STACK_DEPTH in parca-agent.bpf.c
doubleStackDepth = 254
defaultRlimit = 1024 << 20 // ~1GB
)
// stackCountKey mirrors the struct in parca-agent.bpf.c
// TODO: https://github.com/parca-dev/parca-agent/issues/207
type stackCountKey struct {
pid uint32
userStackID int32
kernelStackID int32
}
type bpfMaps struct {
counts *bpf.BPFMap
traces *bpf.BPFMap
}
type CgroupProfiler struct {
logger log.Logger
reg prometheus.Registerer
mtx *sync.RWMutex
cancel func()
pidMappingFileCache *maps.PIDMappingFileCache
perfCache *perf.Cache
ksymCache *ksym.Cache
objCache objectfile.Cache
bpfMaps *bpfMaps
countKeys []stackCountKey
missingStacks *prometheus.CounterVec
lastError error
lastProfileTakenAt time.Time
writeClient profilestorepb.ProfileStoreServiceClient
debugInfo *debuginfo.DebugInfo
target model.LabelSet
profilingDuration time.Duration
profileBufferPool sync.Pool
}
func NewCgroupProfiler(
logger log.Logger,
reg prometheus.Registerer,
ksymCache *ksym.Cache,
objCache objectfile.Cache,
writeClient profilestorepb.ProfileStoreServiceClient,
debugInfoClient debuginfo.Client,
target model.LabelSet,
profilingDuration time.Duration,
tmp string,
) *CgroupProfiler {
return &CgroupProfiler{
logger: log.With(logger, "labels", target.String()),
reg: reg,
mtx: &sync.RWMutex{},
target: target,
profilingDuration: profilingDuration,
writeClient: writeClient,
ksymCache: ksymCache,
pidMappingFileCache: maps.NewPIDMappingFileCache(logger),
perfCache: perf.NewPerfCache(logger),
objCache: objCache,
debugInfo: debuginfo.New(
log.With(logger, "component", "debuginfo"),
debugInfoClient,
tmp,
),
missingStacks: promauto.With(reg).NewCounterVec(
prometheus.CounterOpts{
Name: "parca_agent_profiler_missing_stacks_total",
Help: "Number of missing profile stacks",
ConstLabels: map[string]string{"target": target.String()},
},
[]string{"type"},
),
profileBufferPool: sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
},
}
}
func (p *CgroupProfiler) LastProfileTakenAt() time.Time {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.lastProfileTakenAt
}
func (p *CgroupProfiler) LastError() error {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.lastError
}
func (p *CgroupProfiler) Stop() {
p.mtx.Lock()
defer p.mtx.Unlock()
level.Debug(p.logger).Log("msg", "stopping cgroup profiler")
if !p.reg.Unregister(p.missingStacks) {
level.Debug(p.logger).Log("msg", "cannot unregister metric")
}
if p.cancel != nil {
p.cancel()
}
}
func (p *CgroupProfiler) Labels() model.LabelSet {
labels := model.LabelSet{
"__name__": "parca_agent_cpu",
}
for labelname, labelvalue := range p.target {
if !strings.HasPrefix(string(labelname), "__") {
labels[labelname] = labelvalue
}
}
return labels
}
func (p *CgroupProfiler) Run(ctx context.Context) error {
level.Debug(p.logger).Log("msg", "starting cgroup profiler")
p.mtx.Lock()
ctx, p.cancel = context.WithCancel(ctx)
p.mtx.Unlock()
m, err := bpf.NewModuleFromBufferArgs(bpf.NewModuleArgs{
BPFObjBuff: bpfObj,
BPFObjName: "parca",
})
if err != nil {
return fmt.Errorf("new bpf module: %w", err)
}
defer m.Close()
// Always need to be used after bpf.NewModuleFromBufferArgs to avoid limit override.
if err := p.bumpMemlockRlimit(); err != nil {
return fmt.Errorf("bump memlock rlimit: %w", err)
}
if err := m.BPFLoadObject(); err != nil {
return fmt.Errorf("load bpf object: %w", err)
}
cgroup, err := os.Open(string(p.target[agent.CgroupPathLabelName]))
if err != nil {
return fmt.Errorf("open cgroup: %w", err)
}
defer cgroup.Close()
cpus := runtime.NumCPU()
for i := 0; i < cpus; i++ {
fd, err := unix.PerfEventOpen(&unix.PerfEventAttr{
Type: unix.PERF_TYPE_SOFTWARE,
Config: unix.PERF_COUNT_SW_CPU_CLOCK,
Size: uint32(unsafe.Sizeof(unix.PerfEventAttr{})),
Sample: 100,
Bits: unix.PerfBitDisabled | unix.PerfBitFreq,
}, int(cgroup.Fd()), i, -1, unix.PERF_FLAG_PID_CGROUP)
if err != nil {
return fmt.Errorf("open perf event: %w", err)
}
defer func() {
if err := syscall.Close(fd); err != nil {
level.Error(p.logger).Log("msg", "close perf event", "err", err)
}
}()
prog, err := m.GetProgram("do_sample")
if err != nil {
return fmt.Errorf("get bpf program: %w", err)
}
// Because this is fd based, even if our program crashes or is ended
// without proper shutdown, things get cleaned up appropriately.
link, err := prog.AttachPerfEvent(fd)
if err != nil {
return fmt.Errorf("attach perf event: %w", err)
}
defer func() {
if err := link.Destroy(); err != nil {
level.Error(p.logger).Log("msg", "destroy perf event link", "err", err)
}
}()
}
counts, err := m.GetMap("counts")
if err != nil {
return fmt.Errorf("get counts map: %w", err)
}
traces, err := m.GetMap("stack_traces")
if err != nil {
return fmt.Errorf("get stack traces map: %w", err)
}
p.bpfMaps = &bpfMaps{counts: counts, traces: traces}
// Allocate this here, so it's only allocated once instead of every
// time that p.profileLoop is called below. This is because, as of now,
// this slice will be around 122Kb. We allocate enough to read the entire
// map instead of using the batch iteration feature because it vastly
// simplifies the code in profileLoop and the batch operations are a bit tricky to get right.
// If allocating this much memory upfront is a problem we can always revisit and use
// smaller batch sizes.
p.countKeys = make([]stackCountKey, counts.GetMaxEntries())
ticker := time.NewTicker(p.profilingDuration)
defer ticker.Stop()
level.Debug(p.logger).Log("msg", "start profiling loop")
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
captureTime := time.Now()
err := p.profileLoop(ctx, captureTime)
if err != nil {
level.Warn(p.logger).Log("msg", "profile loop error", "err", err)
}
p.loopReport(captureTime, err)
}
}
func (p *CgroupProfiler) profileLoop(ctx context.Context, captureTime time.Time) error {
defer func() {
if err := recover(); err != nil {
level.Error(p.logger).Log("msg", "profile loop recovered from panic", "err", err)
debug.PrintStack()
}
}()
var (
prof = &profile.Profile{
SampleType: []*profile.ValueType{{
Type: "samples",
Unit: "count",
}},
TimeNanos: captureTime.UnixNano(),
DurationNanos: int64(p.profilingDuration),
// We sample at 100Hz, which is every 10 Million nanoseconds.
PeriodType: &profile.ValueType{
Type: "cpu",
Unit: "nanoseconds",
},
Period: 10000000,
}
mapping = maps.NewMapping(p.pidMappingFileCache)
kernelMapping = &profile.Mapping{
// TODO(kakkoyun): Check if this conflicts with https://github.com/google/pprof/pull/675/files
File: "[kernel.kallsyms]",
}
kernelFunctions = map[uint64]*profile.Function{}
userFunctions = map[[2]uint64]*profile.Function{} // 2 uint64 1 for PID and 1 for Addr
locations = []*profile.Location{}
kernelLocations = []*profile.Location{}
kernelAddresses = map[uint64]struct{}{}
locationIndices = map[[2]uint64]int{}
samples = map[[doubleStackDepth]uint64]*profile.Sample{}
byteOrder = byteorder.GetHostByteOrder()
// Variables needed for eBPF map batch iteration.
countKeysPtr = unsafe.Pointer(&p.countKeys[0])
nextCountKey = uintptr(1)
)
// Reset count keys before collecting new traces from the kernel.
memsetCountKeys(p.countKeys, stackCountKey{})
// TODO(kakkoyun): Is there a neat way to find out the number of entries in the map?
batchSize := 0
it := p.bpfMaps.counts.Iterator()
for it.Next() {
batchSize++
}
if err := it.Err(); err != nil {
return fmt.Errorf("iterate over counts map: %w", err)
}
if batchSize == 0 {
return nil
}
level.Debug(p.logger).Log("msg", "fetching stack trace counts in batch", "batchSize", batchSize)
time.Sleep(1 * time.Second)
var (
values [][]byte
err error
)
values, err = p.bpfMaps.counts.GetValueAndDeleteBatch(countKeysPtr, nil, unsafe.Pointer(&nextCountKey), uint32(batchSize))
if err != nil {
switch {
case errors.Is(err, syscall.EPERM):
level.Error(p.logger).Log("msg", "get value and delete batch: requested number of items is probably greater than existed", "err", err)
//return fmt.Errorf("get value and delete batch: requested number of items is probably greater than existed: %w", err)
return nil
case errors.Is(err, syscall.ENOENT):
level.Debug(p.logger).Log("msg", "no values in batch")
return nil
default:
return fmt.Errorf("get value and delete batch: %w", err)
}
}
if len(values) == 0 {
level.Debug(p.logger).Log("msg", "no values in batch")
return nil
}
for i, key := range p.countKeys {
var (
pid = key.pid
userStackID = key.userStackID
kernelStackID = key.kernelStackID
)
if pid == 0 {
break
}
value := byteOrder.Uint64(values[i])
stackTraces := p.bpfMaps.traces
stackBytes, err := stackTraces.GetValue(unsafe.Pointer(&userStackID))
if err != nil {
p.missingStacks.WithLabelValues("user").Inc()
continue
}
if err := stackTraces.DeleteKey(unsafe.Pointer(&userStackID)); err != nil {
return fmt.Errorf("unable to delete stack trace key: %w", err)
}
// Twice the stack depth because we have a user and a potential Kernel stack.
stack := [doubleStackDepth]uint64{}
err = binary.Read(bytes.NewBuffer(stackBytes), byteOrder, stack[:stackDepth])
if err != nil {
return fmt.Errorf("read user stack trace: %w", err)
}
if kernelStackID >= 0 {
stackBytes, err = stackTraces.GetValue(unsafe.Pointer(&kernelStackID))
if err != nil {
p.missingStacks.WithLabelValues("kernel").Inc()
continue
}
if err := stackTraces.DeleteKey(unsafe.Pointer(&kernelStackID)); err != nil {
return fmt.Errorf("unable to delete stack trace key: %w", err)
}
err = binary.Read(bytes.NewBuffer(stackBytes), byteOrder, stack[stackDepth:])
if err != nil {
return fmt.Errorf("read kernel stack trace: %w", err)
}
}
sample, ok := samples[stack]
if ok {
// We already have a sample with this stack trace, so just add
// it to the previous one.
sample.Value[0] += int64(value)
continue
}
sampleLocations := []*profile.Location{}
// Collect Kernel stack trace samples.
for _, addr := range stack[stackDepth:] {
if addr != uint64(0) {
key := [2]uint64{0, addr}
// PID 0 not possible so we'll use it to identify the kernel.
locationIndex, ok := locationIndices[key]
if !ok {
locationIndex = len(locations)
l := &profile.Location{
ID: uint64(locationIndex + 1),
Address: addr,
Mapping: kernelMapping,
}
locations = append(locations, l)
kernelLocations = append(kernelLocations, l)
kernelAddresses[addr] = struct{}{}
locationIndices[key] = locationIndex
}
sampleLocations = append(sampleLocations, locations[locationIndex])
}
}
perfMap, err := p.perfCache.CacheForPID(pid)
if err != nil {
// We expect only a minority of processes to have a JIT and produce
// the perf map.
if !errors.Is(err, perf.ErrNotFound) {
level.Warn(p.logger).Log("msg", "failed to obtain perf map for pid", "pid", pid, "err", err)
}
}
// Collect User stack trace samples.
for _, addr := range stack[:stackDepth] {
if addr != uint64(0) {
key := [2]uint64{uint64(pid), addr}
locationIndex, ok := locationIndices[key]
if !ok {
locationIndex = len(locations)
m, err := mapping.PIDAddrMapping(pid, addr)
if err != nil {
if !errors.Is(err, maps.ErrNotFound) {
level.Warn(p.logger).Log("msg", "failed to get process mapping", "err", err)
}
}
l := &profile.Location{
ID: uint64(locationIndex + 1),
// Try to normalize the address for a symbol for position independent code.
Address: p.normalizeAddress(m, pid, addr),
Mapping: m,
}
// Resolve JIT symbols using perf maps.s
if perfMap != nil {
// TODO(zecke): Log errors other than perf.ErrNoSymbolFound
jitFunction, ok := userFunctions[key]
if !ok {
if sym, err := perfMap.Lookup(addr); err == nil {
jitFunction = &profile.Function{Name: sym}
userFunctions[key] = jitFunction
}
}
if jitFunction != nil {
l.Line = []profile.Line{{Function: jitFunction}}
}
}
locations = append(locations, l)
locationIndices[key] = locationIndex
}
sampleLocations = append(sampleLocations, locations[locationIndex])
}
}
sample = &profile.Sample{
Value: []int64{int64(value)},
Location: sampleLocations,
}
samples[stack] = sample
}
// Build Profile from samples, locations and mappings.
for _, s := range samples {
prof.Sample = append(prof.Sample, s)
}
var mappedFiles []maps.ProcessMapping
prof.Mapping, mappedFiles = mapping.AllMappings()
prof.Location = locations
// Upload debug information of the discovered object files.
go func() {
var objFiles []*objectfile.MappedObjectFile
for _, mf := range mappedFiles {
objFile, err := p.objCache.ObjectFileForProcess(mf.PID, mf.Mapping)
if err != nil {
continue
}
objFiles = append(objFiles, objFile)
}
p.debugInfo.EnsureUploaded(ctx, objFiles)
}()
// Resolve Kernel function names.
kernelSymbols, err := p.ksymCache.Resolve(kernelAddresses)
if err != nil {
return fmt.Errorf("resolve kernel symbols: %w", err)
}
for _, l := range kernelLocations {
kernelFunction, ok := kernelFunctions[l.Address]
if !ok {
name := kernelSymbols[l.Address]
if name == "" {
name = "not found"
}
kernelFunction = &profile.Function{
Name: name,
}
kernelFunctions[l.Address] = kernelFunction
}
if kernelFunction != nil {
l.Line = []profile.Line{{Function: kernelFunction}}
}
}
for _, f := range kernelFunctions {
f.ID = uint64(len(prof.Function)) + 1
prof.Function = append(prof.Function, f)
}
kernelMapping.ID = uint64(len(prof.Mapping)) + 1
prof.Mapping = append(prof.Mapping, kernelMapping)
// Resolve user function names.
for _, f := range userFunctions {
f.ID = uint64(len(prof.Function)) + 1
prof.Function = append(prof.Function, f)
}
if err := p.sendProfile(ctx, prof); err != nil {
level.Error(p.logger).Log("msg", "failed to send profile", "err", err)
}
return nil
}
func (p *CgroupProfiler) loopReport(lastProfileTakenAt time.Time, lastError error) {
p.mtx.Lock()
defer p.mtx.Unlock()
p.lastProfileTakenAt = lastProfileTakenAt
p.lastError = lastError
}
func (p *CgroupProfiler) normalizeAddress(m *profile.Mapping, pid uint32, addr uint64) uint64 {
if m == nil {
return addr
}
logger := log.With(p.logger, "pid", pid, "buildID", m.BuildID)
if m.Unsymbolizable() {
level.Debug(logger).Log("msg", "mapping is unsymbolizable")
return addr
}
objFile, err := p.objCache.ObjectFileForProcess(pid, m)
if err != nil {
level.Debug(logger).Log("msg", "failed to open object file", "err", err)
return addr
}
// Transform the address by normalizing Kernel memory offsets.
normalizedAddr, err := objFile.ObjAddr(addr)
if err != nil {
level.Debug(logger).Log("msg", "failed to get normalized address from object file", "err", err)
return addr
}
return normalizedAddr
}
func (p *CgroupProfiler) sendProfile(ctx context.Context, prof *profile.Profile) error {
buf := p.profileBufferPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
p.profileBufferPool.Put(buf)
}()
if err := prof.Write(buf); err != nil {
return err
}
var labeloldformat []*profilestorepb.Label
for key, value := range p.Labels() {
labeloldformat = append(labeloldformat,
&profilestorepb.Label{
Name: string(key),
Value: string(value),
})
}
// NOTICE: This is a batch client, so nothing will be sent immediately.
// Make sure that the batch write client has the correct behaviour if you change any parameters.
_, err := p.writeClient.WriteRaw(ctx, &profilestorepb.WriteRawRequest{
Normalized: true,
Series: []*profilestorepb.RawProfileSeries{{
Labels: &profilestorepb.LabelSet{Labels: labeloldformat},
Samples: []*profilestorepb.RawSample{{
RawProfile: buf.Bytes(),
}},
}},
})
return err
}
// bumpMemlockRlimit increases the current memlock limit to a value more reasonable for the profiler's needs.
func (p *CgroupProfiler) bumpMemlockRlimit() error {
rLimit := syscall.Rlimit{
Cur: uint64(defaultRlimit),
Max: uint64(defaultRlimit),
}
// RLIMIT_MEMLOCK is 0x8.
if err := syscall.Setrlimit(unix.RLIMIT_MEMLOCK, &rLimit); err != nil {
return fmt.Errorf("failed to increase rlimit: %w", err)
}
rLimit = syscall.Rlimit{}
if err := syscall.Getrlimit(unix.RLIMIT_MEMLOCK, &rLimit); err != nil {
return fmt.Errorf("failed to get rlimit: %w", err)
}
level.Debug(p.logger).Log("msg", "increased max memory locked rlimit", "limit", humanize.Bytes(rLimit.Cur))
return nil
}
// memsetCountKeys will reset the given slice to the given value.
// This function makes use of the highly optimized copy builtin function
// and is able to fill the entire slice in O(log n) time.
func memsetCountKeys(in []stackCountKey, v stackCountKey) {
if len(in) == 0 {
return
}
in[0] = v
for bp := 1; bp < len(in); bp *= 2 {
copy(in[bp:], in[:bp])
}
}