bpftrace: Unable to perform more than 4096 instructions in BEGIN
CentOS 7.7, using the following repo to build/package/install Bpftrace:
https://FrauBSD.org/el7-bpf-specs
NOTE: Forked from https://github.com/fbs/el7-bpf-specs
I am running into an issue with handling large BEGIN. To reproduce:
printf "BEGIN{\n%s\n}\n\nEND{clear(@a)}\n" "$(for n in {1..400};do printf "\t@a[$n] = 1;\n"; done)" | bpftrace /dev/stdin
Results in Bpftrace prematurely exiting with this error:
Error loading program: BEGIN (try -v)
Adding -v as suggested, lowering from {1..400} to an acceptable {1..372} so that it runs, and adding a grep for some registers does indeed point us at the issue:
$ printf "BEGIN{\n%s\n}\n\nEND{clear(@a)}\n" "$(for n in {1..372};do printf "\t@a[$n] = 1;\n"; done)" | bpftrace -v /dev/stdin | grep "(b7) r1 ="
Attaching END
1: (b7) r1 = 24896
3: (b7) r1 = 30002
5: (b7) r1 = 0
11: (b7) r1 = 2
22: (b7) r1 = 3
33: (b7) r1 = 4
44: (b7) r1 = 5
55: (b7) r1 = 6
66: (b7) r1 = 7
77: (b7) r1 = 8
88: (b7) r1 = 9
99: (b7) r1 = 10
110: (b7) r1 = 11
...
4026: (b7) r1 = 367
4037: (b7) r1 = 368
4048: (b7) r1 = 369
4059: (b7) r1 = 370
4070: (b7) r1 = 371
4081: (b7) r1 = 372
It appears to take 11 instructions for each @a[<N>] = 1 line and the 373rd attempt pushes us past some limit of 4096 instructions per function (BEGIN in this case).
Is there a way to increase the limit?
Ideally, for testing I would like to increase it (in a local compile of LLVM for example) to (65535 * 11 + 4096) for a total of 724,981 instructions allowed per-function. This would allow me to – at the very worse-case scenario – flag each/every PID in the system for tracing.
What I am doing is analyzing running jobs outside of Bpftrace for some condition and then stuffing @flag[pid] = integer; into the BEGIN block of Bpftrace to seed the running system state into an analyzer that continues with realtime events. I am usually just a few hundred instructions over the limit and even a small increase would be greatly helpful, but handling the worst-case scenario of every pid in the system matching the pre-trace condition would be nice.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 39 (15 by maintainers)
nice hack 😃
I’m not entirely sure what you’re building here so it might not apply to your use case. But as a work around that doesn’t require patching the kernel you could consider using bpftool to write to the map, instead of using a
BEGINprobe.Thanks so much! Here’s the patch that we applied to 3.10.0-1062.12.1 (latest kernel release on CentOS 7.7):
With this patch and only this patch we are able to get past our problem. The original test-program of:
No longer has an issue starting. No recompile of bpftool, bcc, llvm, or bpftrace was required, just that kernel patch.