-
Notifications
You must be signed in to change notification settings - Fork 67
/
cpu.go
1257 lines (1097 loc) · 40.9 KB
/
cpu.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022-2024 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 cpu
import "C" //nolint:all
import (
"bytes"
"context"
"debug/elf"
"encoding/binary"
"errors"
"fmt"
"os"
"regexp"
"strings"
"sync"
"syscall"
"time"
"unsafe"
libbpf "github.com/aquasecurity/libbpfgo"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
"github.com/puzpuzpuz/xsync/v3"
"golang.org/x/sys/unix"
"github.com/parca-dev/parca-agent/pkg/buildid"
"github.com/parca-dev/parca-agent/pkg/byteorder"
"github.com/parca-dev/parca-agent/pkg/cache"
"github.com/parca-dev/parca-agent/pkg/cpuinfo"
parcalogger "github.com/parca-dev/parca-agent/pkg/logger"
"github.com/parca-dev/parca-agent/pkg/metadata/labels"
"github.com/parca-dev/parca-agent/pkg/objectfile"
"github.com/parca-dev/parca-agent/pkg/pprof"
"github.com/parca-dev/parca-agent/pkg/profile"
"github.com/parca-dev/parca-agent/pkg/profiler"
bpfmaps "github.com/parca-dev/parca-agent/pkg/profiler/cpu/bpf/maps"
bpfmetrics "github.com/parca-dev/parca-agent/pkg/profiler/cpu/bpf/metrics"
bpfprograms "github.com/parca-dev/parca-agent/pkg/profiler/cpu/bpf/programs"
"github.com/parca-dev/parca-agent/pkg/rlimit"
"github.com/parca-dev/parca-agent/pkg/runtime"
"github.com/parca-dev/parca-agent/pkg/stack/unwind"
)
const (
configKey = "unwinder_config"
)
// UnwinderConfig must be synced to the C definition.
type UnwinderConfig struct {
FilterProcesses bool
VerboseLogging bool
MixedStackWalking bool
PythonEnable bool
RubyEnabled bool
CollectTraceID bool
Padding1 bool
Padding2 bool
RateLimitUnwindInfo uint32
RateLimitProcessMappings uint32
RateLimitRefreshProcessInfo uint32
}
type Config struct {
ProfilingDuration time.Duration
ProfilingSamplingFrequency uint64
PerfEventBufferPollInterval time.Duration
PerfEventBufferProcessingInterval time.Duration
PerfEventBufferWorkerCount int
MemlockRlimit uint64
DebugProcessNames []string
DWARFUnwindingDisabled bool
DWARFUnwindingMixedModeEnabled bool
BPFVerboseLoggingEnabled bool
BPFEventsBufferSize uint32
PythonUnwindingEnabled bool
RubyUnwindingEnabled bool
RateLimitUnwindInfo uint32
RateLimitProcessMappings uint32
RateLimitRefreshProcessInfo uint32
CollectTraceID bool
}
func (c Config) DebugModeEnabled() bool {
return len(c.DebugProcessNames) > 0
}
type CPU struct {
config *Config
logger log.Logger
reg prometheus.Registerer
metrics *metrics
processInfoManager profiler.ProcessInfoManager
profileConverter *pprof.Manager
profileStore profiler.ProfileStore
// Notify that the BPF program was loaded.
bpfProgramLoaded chan bool
bpfMaps *bpfmaps.Maps
framePointerCache unwind.FramePointerCache
interpSymTab profile.InterpreterSymbolTable
byteOrder binary.ByteOrder
mtx *sync.RWMutex
lastError error
processLastErrors map[int]error
processErrorTracker *errorTracker
lastSuccessfulProfileStartedAt time.Time
lastProfileStartedAt time.Time
objFilePool *objectfile.Pool
}
func NewCPUProfiler(
logger log.Logger,
reg prometheus.Registerer,
processInfoManager profiler.ProcessInfoManager,
compilerInfoManager *runtime.CompilerInfoManager,
profileConverter *pprof.Manager,
profileWriter profiler.ProfileStore,
config *Config,
bpfProgramLoaded chan bool,
objFilePool *objectfile.Pool,
) *CPU {
return &CPU{
config: config,
logger: logger,
reg: reg,
metrics: newMetrics(reg),
processInfoManager: processInfoManager,
profileConverter: profileConverter,
profileStore: profileWriter,
// CPU profiler specific caches.
framePointerCache: unwind.NewHasFramePointersCache(logger, reg, compilerInfoManager),
byteOrder: byteorder.GetHostByteOrder(),
mtx: &sync.RWMutex{},
// increase cache length if needed to track more errors
processErrorTracker: newErrorTracker(logger, reg, "no_text_section_error_tracker"),
bpfProgramLoaded: bpfProgramLoaded,
objFilePool: objFilePool,
}
}
func (p *CPU) Name() string {
return "parca_agent_cpu"
}
func (p *CPU) LastProfileStartedAt() time.Time {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.lastProfileStartedAt
}
func (p *CPU) LastError() error {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.lastError
}
func (p *CPU) ProcessLastErrors() map[int]error {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.processLastErrors
}
// loadBPFModules loads the BPF programs and maps.
// Also adjusts the unwind shards to the highest possible value.
// And configures shared maps between BPF programs.
func loadBPFModules(logger log.Logger, reg prometheus.Registerer, memlockRlimit uint64, config Config, ofp *objectfile.Pool) (*libbpf.Module, *bpfmaps.Maps, error) {
var lerr error
maxLoadAttempts := 10
unwindShards := uint32(bpfmaps.MaxUnwindShards)
bpfObj, err := bpfprograms.OpenNative()
if err != nil {
return nil, nil, err
}
libbpf.SetLoggerCbs(parcalogger.NewLibbpfLogCallbacks(logger))
var (
rbperf *libbpf.Module
pyperf *libbpf.Module
)
if config.RubyUnwindingEnabled {
// rbperf
rbperfBpfObj, err := bpfprograms.OpenRuby()
if err != nil {
return nil, nil, err
}
rbperf, err = libbpf.NewModuleFromBufferArgs(libbpf.NewModuleArgs{
BPFObjBuff: rbperfBpfObj,
BPFObjName: "parca-rbperf",
})
if err != nil {
return nil, nil, fmt.Errorf("new bpf module: %w", err)
}
level.Info(logger).Log("msg", "loaded rbperf BPF module")
}
if config.PythonUnwindingEnabled {
// pyperf
pyperfBpfObj, err := bpfprograms.OpenPython()
if err != nil {
return nil, nil, err
}
pyperf, err = libbpf.NewModuleFromBufferArgs(libbpf.NewModuleArgs{
BPFObjBuff: pyperfBpfObj,
BPFObjName: "parca-pyperf",
})
if err != nil {
return nil, nil, fmt.Errorf("new bpf module: %w", err)
}
level.Info(logger).Log("msg", "loaded pyperf BPF module")
}
bpfmapMetrics := bpfmaps.NewMetrics(reg)
bpfmapsProcessCache := bpfmaps.NewProcessCache(logger, reg)
syncedIntepreters := cache.NewLRUCache[int, runtime.Interpreter](
prometheus.WrapRegistererWith(prometheus.Labels{"cache": "synced_interpreters"}, reg),
bpfmaps.MaxCachedProcesses/10,
)
// Adaptive unwind shard count sizing.
for i := 0; i < maxLoadAttempts; i++ {
native, err := libbpf.NewModuleFromBufferArgs(libbpf.NewModuleArgs{
BPFObjBuff: bpfObj,
BPFObjName: "parca-native",
})
if err != nil {
return nil, nil, fmt.Errorf("new bpf module: %w", err)
}
// Must be called after bpf.NewModuleFromBufferArgs to avoid limit override.
rLimit, err := rlimit.BumpMemlock(memlockRlimit, memlockRlimit)
if err != nil {
return nil, nil, fmt.Errorf("bump memlock: %w", err)
}
level.Debug(logger).Log("msg", "actual memory locked rlimit", "cur", rlimit.HumanizeRLimit(rLimit.Cur), "max", rlimit.HumanizeRLimit(rLimit.Max))
modules := map[bpfmaps.ProfilerModuleType]*libbpf.Module{
bpfmaps.NativeModule: native,
bpfmaps.RbperfModule: rbperf,
bpfmaps.PyperfModule: pyperf,
}
// Maps must be initialized before loading the BPF code.
bpfMaps, err := bpfmaps.New(
logger,
bpfmapMetrics,
modules,
ofp,
bpfmapsProcessCache,
syncedIntepreters,
)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize eBPF maps: %w", err)
}
if config.DWARFUnwindingDisabled {
// Even if DWARF-based unwinding is disabled, either due to the user passing the flag to disable it or running on arm64, still
// create a handful of shards to ensure that when it is enabled we can at least create some shards. Basically we want to ensure
// that we catch any potential issues as early as possible.
unwindShards = uint32(5)
}
level.Debug(logger).Log("msg", "attempting to create unwind shards", "count", unwindShards)
if err := bpfMaps.AdjustMapSizes(config.DebugModeEnabled(), unwindShards, config.BPFEventsBufferSize); err != nil {
return nil, nil, fmt.Errorf("failed to adjust map sizes: %w", err)
}
level.Debug(logger).Log("msg", "created unwind shards", "count", unwindShards)
level.Debug(logger).Log("msg", "initializing BPF global variables")
if err := native.InitGlobalVariable(configKey, UnwinderConfig{
FilterProcesses: config.DebugModeEnabled(),
VerboseLogging: config.BPFVerboseLoggingEnabled,
MixedStackWalking: config.DWARFUnwindingMixedModeEnabled,
PythonEnable: config.PythonUnwindingEnabled,
RubyEnabled: config.RubyUnwindingEnabled,
CollectTraceID: config.CollectTraceID,
Padding1: false,
Padding2: false,
RateLimitUnwindInfo: config.RateLimitUnwindInfo,
RateLimitProcessMappings: config.RateLimitProcessMappings,
RateLimitRefreshProcessInfo: config.RateLimitRefreshProcessInfo,
}); err != nil {
return nil, nil, fmt.Errorf("init global variable: %w", err)
}
if config.RubyUnwindingEnabled {
if err := rbperf.InitGlobalVariable("verbose", config.BPFVerboseLoggingEnabled); err != nil {
return nil, nil, fmt.Errorf("rbperf: init global variable: %w", err)
}
}
if config.PythonUnwindingEnabled {
if err := pyperf.InitGlobalVariable("verbose", config.BPFVerboseLoggingEnabled); err != nil {
return nil, nil, fmt.Errorf("pyperf: init global variable: %w", err)
}
}
level.Debug(logger).Log("msg", "loading BPF object for native unwinder")
lerr = native.BPFLoadObject()
if lerr == nil {
// Must be called before loading the interpreter stack walkers.
err := bpfMaps.ReuseMaps()
if err != nil {
return nil, nil, fmt.Errorf("failed to reuse maps: %w", err)
}
if config.RubyUnwindingEnabled {
level.Debug(logger).Log("msg", "loading BPF object for ruby unwinder")
err = rbperf.BPFLoadObject()
if err != nil {
return nil, nil, fmt.Errorf("failed to load rbperf: %w", err)
}
}
if config.PythonUnwindingEnabled {
level.Debug(logger).Log("msg", "loading BPF object for python unwinder")
err = pyperf.BPFLoadObject()
if err != nil {
return nil, nil, fmt.Errorf("failed to load pyperf: %w", err)
}
}
level.Debug(logger).Log("msg", "updating programs map")
err = bpfMaps.UpdateTailCallsMap()
if err != nil {
return nil, nil, fmt.Errorf("failed to update programs map: %w", err)
}
level.Debug(logger).Log("msg", "updating interpreter data")
err = bpfMaps.SetInterpreterData()
if err != nil {
return nil, nil, fmt.Errorf("failed to set interpreter data: %w", err)
}
return native, bpfMaps, nil
}
// There's not enough free memory for these many unwind shards, let's retry with half
// as many.
if errors.Is(lerr, syscall.ENOMEM) {
if err := bpfMaps.Close(); err != nil { // Only required when we want to retry.
return nil, nil, fmt.Errorf("failed to cleanup previously created bpfmaps: %w", err)
}
unwindShards /= 2
} else {
break
}
}
level.Error(logger).Log("msg", "could not create unwind info shards", "lastError", lerr)
return nil, nil, lerr
}
// listenEvents listens for events from the BPF program and handles them.
// It also listens for lost events and logs them.
func (p *CPU) listenEvents(ctx context.Context, eventsChan <-chan []byte, lostChan <-chan uint64, requestUnwindInfoChan chan<- int) {
prefetch := make(chan int, p.config.PerfEventBufferWorkerCount*4)
refresh := make(chan int, p.config.PerfEventBufferWorkerCount*2)
defer func() {
close(prefetch)
close(refresh)
}()
var (
fetchInProgress = xsync.NewMapOf[int, struct{}]()
refreshInProgress = xsync.NewMapOf[int, struct{}]()
)
for i := 0; i < p.config.PerfEventBufferWorkerCount; i++ {
go func() {
for {
select {
case pid, open := <-prefetch:
if !open {
return
}
_ = p.prefetchProcessInfo(ctx, pid)
fetchInProgress.Delete(pid)
case pid, open := <-refresh:
if !open {
return
}
if err := p.fetchProcessInfoWithFreshMappings(ctx, pid); err != nil {
return
}
executable := fmt.Sprintf("/proc/%d/exe", pid)
shouldUseFPByDefault, err := p.framePointerCache.HasFramePointers(executable) // nolint:contextcheck
if err != nil {
// It might not exist as reading procfs is racy. If the executable has no symbols
// that we use as a heuristic to detect whether it has frame pointers or not,
// we assume it does not and that we should generate the unwind information.
level.Debug(p.logger).Log("msg", "frame pointer detection failed", "executable", executable, "err", err)
if !errors.Is(err, os.ErrNotExist) && !errors.Is(err, elf.ErrNoSymbols) {
return
}
}
// Process information has been refreshed, now refresh the mappings and their unwind info.
p.bpfMaps.RefreshProcessInfo(pid, shouldUseFPByDefault)
refreshInProgress.Delete(pid)
}
}
}()
}
for {
select {
case receivedBytes, open := <-eventsChan:
if !open {
return
}
if len(receivedBytes) == 0 {
p.metrics.eventsReceived.WithLabelValues(labelEventEmpty).Inc()
continue
}
payload := binary.LittleEndian.Uint64(receivedBytes)
// Get the 4 more significant bytes and convert to int as they are different types.
// On x86_64:
// - unsafe.Sizeof(int(0)) = 8
// - unsafe.Sizeof(uint32(0)) = 4
pid := int(int32(payload))
switch {
case payload&bpfmaps.RequestUnwindInformation == bpfmaps.RequestUnwindInformation:
if p.config.DWARFUnwindingDisabled {
continue
}
p.metrics.eventsReceived.WithLabelValues(labelEventUnwindInfo).Inc()
// See onDemandUnwindInfoBatcher for consumer.
requestUnwindInfoChan <- pid
case payload&bpfmaps.RequestProcessMappings == bpfmaps.RequestProcessMappings:
p.metrics.eventsReceived.WithLabelValues(labelEventProcessMappings).Inc()
if _, exists := fetchInProgress.LoadOrStore(pid, struct{}{}); exists {
continue
}
prefetch <- pid
case payload&bpfmaps.RequestRefreshProcInfo == bpfmaps.RequestRefreshProcInfo:
p.metrics.eventsReceived.WithLabelValues(labelEventRefreshProcInfo).Inc()
// Refresh mappings and their unwind info if they've changed.
if _, exists := refreshInProgress.LoadOrStore(pid, struct{}{}); exists {
continue
}
refresh <- pid
}
case lost, open := <-lostChan:
if !open {
return
}
p.metrics.eventsLost.Inc()
level.Warn(p.logger).Log("msg", "lost events", "count", lost)
default:
time.Sleep(p.config.PerfEventBufferProcessingInterval)
}
}
}
func (p *CPU) prefetchProcessInfo(ctx context.Context, pid int) error {
procInfo, err := p.processInfoManager.Fetch(ctx, pid)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to prefetch process info", "pid", pid, "err", err)
return fmt.Errorf("failed to prefetch process info: %w", err)
}
if procInfo.Interpreter != nil {
// AddInterpreter is idempotent.
err := p.bpfMaps.AddInterpreter(pid, *procInfo.Interpreter)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to call AddInterpreter", "pid", pid, "err", err)
return fmt.Errorf("failed to call AddInterpreter: %w", err)
}
}
return nil
}
// fetchProcessInfoWithFreshMappings fetches process information and makes sure its mappings are up-to-date.
func (p *CPU) fetchProcessInfoWithFreshMappings(ctx context.Context, pid int) error {
procInfo, err := p.processInfoManager.FetchWithFreshMappings(ctx, pid)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to fetch process info", "pid", pid, "err", err)
return fmt.Errorf("failed to fetch process info: %w", err)
}
if procInfo.Interpreter != nil {
// AddInterpreter is idempotent.
err := p.bpfMaps.AddInterpreter(pid, *procInfo.Interpreter)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to call AddInterpreter", "pid", pid, "err", err)
return fmt.Errorf("failed to call AddInterpreter: %w", err)
}
}
return nil
}
// onDemandUnwindInfoBatcher batches PIDs sent from the BPF program when
// frame pointers and unwind information are not present.
//
// Waiting for as long as `duration` is important because `PersistUnwindTable`
// must be called to write the in-flight shard to the BPF map. This has been
// a hot path in the CPU profiles we take in Demo when we persisted the unwind
// tables after adding every pid.
func (p *CPU) onDemandUnwindInfoBatcher(ctx context.Context, requestUnwindInfoChannel <-chan int) {
processEventBatcher(ctx, requestUnwindInfoChannel, 150*time.Millisecond, func(pids []int) {
for _, pid := range pids {
p.addUnwindTableForProcess(ctx, pid)
}
// Must be called after all the calls to `addUnwindTableForProcess`, as it's possible
// that the current in-flight shard hasn't been written to the BPF map, yet.
err := p.bpfMaps.PersistUnwindTable()
if err != nil {
if errors.Is(err, bpfmaps.ErrNeedMoreProfilingRounds) {
p.metrics.unwindTablePersistErrors.WithLabelValues(labelNeedMoreProfilingRounds).Inc()
level.Debug(p.logger).Log("msg", "PersistUnwindTable called to soon", "err", err)
} else {
p.metrics.unwindTablePersistErrors.WithLabelValues(labelOther).Inc()
level.Error(p.logger).Log("msg", "PersistUnwindTable failed", "err", err)
}
}
})
}
func (p *CPU) addUnwindTableForProcess(ctx context.Context, pid int) {
executable := fmt.Sprintf("/proc/%d/exe", pid)
shouldUseFPByDefault, err := p.framePointerCache.HasFramePointers(executable) // nolint:contextcheck
if err != nil {
// It might not exist as reading procfs is racy. If the executable has no symbols
// that we use as a heuristic to detect whether it has frame pointers or not,
// we assume it does not and that we should generate the unwind information.
level.Debug(p.logger).Log("msg", "frame pointer detection failed", "executable", executable, "err", err)
if !errors.Is(err, os.ErrNotExist) && !errors.Is(err, elf.ErrNoSymbols) {
return
}
}
level.Debug(p.logger).Log("msg", "prefetching process info", "pid", pid)
if err := p.prefetchProcessInfo(ctx, pid); err != nil {
return
}
level.Debug(p.logger).Log("msg", "adding unwind tables", "pid", pid)
if err = p.bpfMaps.AddUnwindTableForProcess(pid, nil, true, shouldUseFPByDefault); err == nil {
// Happy path.
return
}
// Error handling,
switch {
case errors.Is(err, bpfmaps.ErrNeedMoreProfilingRounds):
p.metrics.unwindTableAddErrors.WithLabelValues(labelNeedMoreProfilingRounds).Inc()
level.Debug(p.logger).Log("msg", "PersistUnwindTable called to soon", "err", err)
case errors.Is(err, os.ErrNotExist):
p.metrics.unwindTableAddErrors.WithLabelValues(labelProcfsRace).Inc()
level.Debug(p.logger).Log("msg", "failed to add unwind table due to a procfs race", "pid", pid, "err", err)
case errors.Is(err, bpfmaps.ErrTooManyExecutableMappings):
p.metrics.unwindTableAddErrors.WithLabelValues(labelTooManyMappings).Inc()
level.Warn(p.logger).Log("msg", "failed to add unwind table due to having too many executable mappings", "pid", pid, "err", err)
case errors.Is(err, buildid.ErrTextSectionNotFound):
p.processErrorTracker.Track(pid, err)
default:
p.metrics.unwindTableAddErrors.WithLabelValues(labelOther).Inc()
level.Warn(p.logger).Log("msg", "failed to add unwind table", "pid", pid, "err", err)
}
}
// processEventBatcher batches PIDs sent from the BPF program.
//
// Waits for as long as `duration` and calls the `callback` function with a slice of PIDs.
func processEventBatcher(ctx context.Context, eventsChannel <-chan int, duration time.Duration, callback func([]int)) {
batch := make([]int, 0)
timerOn := false
timer := &time.Timer{}
for {
select {
case <-ctx.Done():
return
case pid := <-eventsChannel:
// We want to set a deadline whenever an event is received, if there is
// no other deadline in progress. During this time period we'll batch
// all the events received. Once time's up, we will pass the batch to
// the callback.
if !timerOn {
timerOn = true
timer = time.NewTimer(duration)
}
batch = append(batch, pid)
case <-timer.C:
callback(batch)
batch = batch[:0]
timerOn = false
timer.Stop()
}
}
}
func (p *CPU) watchProcesses(ctx context.Context, pfs procfs.FS, matchers []*regexp.Regexp) {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
pids := []int{}
allThreads := func() procfs.Procs {
allProcs, err := pfs.AllProcs()
if err != nil {
level.Error(p.logger).Log("msg", "failed to list processes", "err", err)
return nil
}
allThreads := make(procfs.Procs, len(allProcs))
for _, proc := range allProcs {
threads, err := pfs.AllThreads(proc.PID)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to list threads", "err", err)
continue
}
allThreads = append(allThreads, threads...)
}
return allThreads
}
// Filter processes if needed.
if p.config.DebugModeEnabled() {
level.Debug(p.logger).Log("msg", "debug process matchers found, starting process watcher")
for _, thread := range allThreads() {
if thread.PID == 0 {
continue
}
comm, err := thread.Comm()
if err != nil {
level.Debug(p.logger).Log("msg", "failed to read process name", "pid", thread.PID, "err", err)
continue
}
if comm == "" {
continue
}
for _, m := range matchers {
if m.MatchString(comm) {
level.Info(p.logger).Log("msg", "match found; debugging process", "pid", thread.PID, "comm", comm)
pids = append(pids, thread.PID)
}
}
}
if len(pids) > 0 {
level.Debug(p.logger).Log("msg", "updating debug pids map", "pids", fmt.Sprintf("%v", pids))
// Only meant to be used for debugging, it is not safe to use in production.
if err := p.bpfMaps.SetDebugPIDs(pids); err != nil {
level.Error(p.logger).Log("msg", "failed to update debug pids map", "err", err)
}
} else {
level.Debug(p.logger).Log("msg", "no processes matched the provided regex")
if err := p.bpfMaps.SetDebugPIDs(nil); err != nil {
level.Error(p.logger).Log("msg", "failed to update debug pids map", "err", err)
}
}
} else {
for _, thread := range allThreads() {
pids = append(pids, thread.PID)
}
}
}
}
func bpfCheck() error {
var result error
if support, err := libbpf.BPFProgramTypeIsSupported(libbpf.BPFProgTypePerfEvent); !support {
result = errors.Join(result, fmt.Errorf("perf event program type not supported: %w", err))
}
if support, err := libbpf.BPFMapTypeIsSupported(libbpf.MapTypeStackTrace); !support {
result = errors.Join(result, fmt.Errorf("stack trace map type not supported: %w", err))
}
if support, err := libbpf.BPFMapTypeIsSupported(libbpf.MapTypeHash); !support {
result = errors.Join(result, fmt.Errorf("hash map type not supported: %w", err))
}
return result
}
func (p *CPU) Run(ctx context.Context) error {
level.Debug(p.logger).Log("msg", "starting cpu profiler")
err := bpfCheck()
if err != nil {
return fmt.Errorf("bpf check: %w", err)
}
var matchers []*regexp.Regexp
if p.config.DebugModeEnabled() {
level.Info(p.logger).Log("msg", "process names specified, debugging processes", "matchers", strings.Join(p.config.DebugProcessNames, ", "))
for _, exp := range p.config.DebugProcessNames {
regex, err := regexp.Compile(exp)
if err != nil {
return fmt.Errorf("failed to compile regex: %w", err)
}
matchers = append(matchers, regex)
}
}
level.Debug(p.logger).Log("msg", "loading BPF modules")
native, bpfMaps, err := loadBPFModules(p.logger, p.reg, p.config.MemlockRlimit, *p.config, p.objFilePool)
if err != nil {
return fmt.Errorf("load bpf program: %w", err)
}
defer native.Close()
level.Debug(p.logger).Log("msg", "BPF modules loaded")
p.bpfProgramLoaded <- true
p.bpfMaps = bpfMaps
// Get bpf metrics
agentProc, err := procfs.Self() // pid of parca-agent
if err != nil {
level.Debug(p.logger).Log("msg", "error getting parca-agent pid", "err", err)
}
p.reg.MustRegister(bpfmetrics.NewCollector(p.logger, native, bpfmaps.PerCPUStatsMapName, agentProc.PID))
// Period is the number of events between sampled occurrences.
// By default we sample at 19Hz (19 times per second),
// which is every ~0.05s or 52,631,578 nanoseconds (1 Hz = 1e9 ns).
samplingPeriod := int64(1e9 / p.config.ProfilingSamplingFrequency)
cpus, err := cpuinfo.NumCPU()
if err != nil {
return fmt.Errorf("get number of CPUs: %w", err)
}
level.Debug(p.logger).Log("msg", "attaching perf event to all CPUs")
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: p.config.ProfilingSamplingFrequency,
Bits: unix.PerfBitDisabled | unix.PerfBitFreq,
}, -1 /* pid */, i /* cpu id */, -1 /* group */, 0 /* flags */)
if err != nil {
return fmt.Errorf("open perf event: %w", err)
}
// Do not close this fd manually as it will result in an error in the
// best case, if the FD doesn't exist and in the worst case it will
// close the wrong FD.
//
// The `Close` method on the module calls `bpf_link__destroy`, which calls
// the link's `detach` function[2], that eventually, through the `bpf_link__detach_fd`
// function it closes the link's FD[3].
// [1]: https://github.com/aquasecurity/libbpfgo/blob/64458ba5a32013dda2d4f88838dde8456922333d/libbpfgo.go#L420
// [2]: https://github.com/libbpf/libbpf/blob/master/src/libbpf.c#L9762
// [3]: https://github.com/libbpf/libbpf/blob/master/src/libbpf.c#L9785
prog, err := native.GetProgram(bpfprograms.ProgramName)
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.
_, err = prog.AttachPerfEvent(fd)
// Do not call `link.Destroy()`[1] as closing the module takes care of
// it[2].
// [1]: https://github.com/aquasecurity/libbpfgo/blob/64458ba5a32013dda2d4f88838dde8456922333d/libbpfgo.go#L240
// [2]: https://github.com/aquasecurity/libbpfgo/blob/64458ba5a32013dda2d4f88838dde8456922333d/libbpfgo.go#L420
if err != nil {
return fmt.Errorf("attach perf event: %w", err)
}
}
// Record start time for first profile.
p.mtx.Lock()
p.lastProfileStartedAt = time.Now()
p.mtx.Unlock()
prog, err := native.GetProgram(bpfprograms.NativeUnwinderProgramName)
if err != nil {
return fmt.Errorf("get bpf program: %w", err)
}
programs, err := native.GetMap(bpfmaps.ProgramsMapName)
if err != nil {
return fmt.Errorf("get programs map: %w", err)
}
fd := prog.FileDescriptor()
if err := programs.Update(unsafe.Pointer(&bpfprograms.NativeProgramFD), unsafe.Pointer(&fd)); err != nil {
return fmt.Errorf("failure updating: %w", err)
}
if err := p.bpfMaps.Create(); err != nil {
return fmt.Errorf("failed to create maps: %w", err)
}
pfs, err := procfs.NewDefaultFS()
if err != nil {
return fmt.Errorf("failed to create procfs: %w", err)
}
if len(matchers) > 0 {
// Update the debug pids map.
go p.watchProcesses(ctx, pfs, matchers)
}
// Process BPF events.
var (
eventsChan = make(chan []byte, 30)
lostChannel = make(chan uint64, 10)
)
perfBuf, err := native.InitPerfBuf("events", eventsChan, lostChannel, 64)
if err != nil {
return fmt.Errorf("failed to init perf buffer: %w", err)
}
perfBuf.Poll(int(p.config.PerfEventBufferPollInterval.Milliseconds()))
requestUnwindInfoChannel := make(chan int, 30)
go p.listenEvents(ctx, eventsChan, lostChannel, requestUnwindInfoChannel)
go p.onDemandUnwindInfoBatcher(ctx, requestUnwindInfoChannel)
ticker := time.NewTicker(p.config.ProfilingDuration)
defer ticker.Stop()
level.Debug(p.logger).Log("msg", "start profiling loop")
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
obtainStart := time.Now()
rawData, err := p.obtainRawData(ctx)
if err != nil {
p.metrics.obtainAttempts.WithLabelValues(labelError).Inc()
level.Warn(p.logger).Log("msg", "failed to obtain profiles from eBPF maps", "err", err)
continue
}
p.metrics.obtainAttempts.WithLabelValues(labelSuccess).Inc()
p.metrics.obtainDuration.Observe(time.Since(obtainStart).Seconds())
groupedRawData := make(map[int]profile.ProcessRawData)
for _, perThreadRawData := range rawData {
pid := int(perThreadRawData.PID)
data, ok := groupedRawData[pid]
if !ok {
groupedRawData[pid] = profile.ProcessRawData{
PID: perThreadRawData.PID,
RawSamples: perThreadRawData.RawSamples,
}
continue
}
groupedRawData[pid] = profile.ProcessRawData{
PID: perThreadRawData.PID,
RawSamples: append(data.RawSamples, perThreadRawData.RawSamples...),
}
}
processLastErrors := map[int]error{}
for pid, perProcessRawData := range groupedRawData {
processLastErrors[pid] = nil
pi, err := p.processInfoManager.Info(ctx, pid)
if err != nil {
p.metrics.profileDrop.WithLabelValues(labelProfileDropReasonProcessInfo).Inc()
level.Debug(p.logger).Log("msg", "failed to get process info", "pid", pid, "err", err)
processLastErrors[pid] = err
continue
}
interpreterSymbolTable, err := p.interpreterSymbolTable(perProcessRawData.RawSamples)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to get interpreter symbol table", "pid", pid, "err", err)
}
pprof, executableInfos, err := p.profileConverter.NewConverter(
pfs,
pid,
pi.Mappings.Executables(),
p.LastProfileStartedAt(),
samplingPeriod,
interpreterSymbolTable,
).Convert(ctx, perProcessRawData.RawSamples)
if err != nil {
level.Warn(p.logger).Log("msg", "failed to convert profile to pprof", "pid", pid, "err", err)
processLastErrors[pid] = err
continue
}
labelSet, err := pi.Labels(ctx)
if err != nil {
level.Warn(p.logger).Log("msg", "failed to get process labels", "pid", pid, "err", err)
processLastErrors[pid] = err
continue
}
if len(labelSet) == 0 {
level.Debug(p.logger).Log("msg", "profile dropped", "pid", pid)
continue
}
// Add the profiler name as a label.
// Uses labels.Merge under the hood, so it re-allocates the label set.
// If we want to drop/disable a profiler, we should do it with another mechanism besides relabelling.
labelSet = labels.WithProfilerName(labelSet, p.Name())
if err := p.profileStore.Store(ctx, labelSet, pprof, executableInfos); err != nil {
level.Warn(p.logger).Log("msg", "failed to write profile", "pid", pid, "err", err)
processLastErrors[pid] = err
continue
}
}
p.report(err, processLastErrors)
}
}
func (p *CPU) report(lastError error, processLastErrors map[int]error) {
p.mtx.Lock()
defer p.mtx.Unlock()
if lastError == nil {
p.lastSuccessfulProfileStartedAt = p.lastProfileStartedAt
p.lastProfileStartedAt = time.Now()
}
p.lastError = lastError
p.processLastErrors = processLastErrors
}
type (
// stackCountKey mirrors the struct in BPF program.
// NOTICE: The memory layout and alignment of the struct currently matches the struct in BPF program.
// However, keep in mind that Go compiler injects padding to align the struct fields to be a multiple of 8 bytes.
// The Go spec says the address of a struct’s fields must be naturally aligned.
// https://dave.cheney.net/2015/10/09/padding-is-hard
// TODO(https://github.com/parca-dev/parca-agent/issues/207)
stackCountKey struct {
PID int32
TID int32
UserStackID uint64
KernelStackID uint64
InterpreterStackID uint64
TraceID [16]byte
}
)
type profileKey struct {
pid int32
tid int32
traceID [16]byte
}
// interpreterSymbolTable returns an up-to-date symbol table for the interpreter.
func (p *CPU) interpreterSymbolTable(samples []profile.RawSample) (profile.InterpreterSymbolTable, error) {
if !p.config.RubyUnwindingEnabled && !p.config.PythonUnwindingEnabled {
return nil, nil
}