Skip to content

Lesson 11 of 13 · 17m

eBPF for Production Questions

bcc tools and bpftrace one-liners answer syscall, block-I/O, and scheduling questions with in-kernel aggregation prod can afford.

eBPF is the mechanism that made production tracing safe. Programs written for it are verified before loading — the in-kernel verifier proves bounded execution (bounded loops allowed since kernel 5.3), memory safety, no wild pointers — and they attach to well-defined hooks: tracepoints (a stable kernel ABI), kprobes (any kernel function, version-fragile), plus uprobes and USDT for userspace. Aggregation happens in-kernel, so a tool emits counts and histograms instead of a log line per event. That last property is the entire trick: strace prints a line per syscall; an eBPF counter increments a map entry.

The toolkit

bcc's suite — packaged as bpfcc-tools on Debian — is a shelf of one-purpose answers: execsnoop for new processes, opensnoop for file opens, biolatency for block-I/O histograms, runqlat and runqslower for scheduler queue latency, offcputime for blocked stacks, tcplife for TCP session stats, syscount for syscall counting, funclatency and funccount for function timing. bpftrace is the same power as a one-liner language:

bpftrace -e 'kprobe:vfs_read { @[comm] = count() }'
bpftrace -e 'tracepoint:syscalls:sys_enter_openat /pid == 4242/ { @[args.filename] = count() }'

Prefer a tracepoint whenever one exists for your question. kprobes compile against the kernel's current symbol layout and silently break across upgrades; tracepoints are an ABI.

What the aggregation looks like

bcc tools print histograms and counters — biolatency emits a usecs bucket table per device, runqlat the distribution of scheduler queue waits, offcputime folded stacks with microsecond counts. bpftrace -l 'tracepoint:syscalls:*' lists the stable probes on your running kernel; bpftrace -lv adds each probe's argument fields so you know which args-> names exist. Aggregations live in BPF maps — @, hist(), lhist(), stats() — that userspace reads at the end. The per-event cost is a few hundred nanoseconds, not a formatted line.

USDT covers the userspace side: the JVM, PostgreSQL, glibc, and runtimes like Node ship static tracepoints (usdt: provider) that fire on GC pauses, query boundaries, and lock waits without a restart or recompile — tplist -l enumerates what a binary carries. Getting started is apt install bpfcc-tools bpftrace on a recent kernel; every tool lands as *-bpfcc with a man page. Kernel 4.9 is the floor for useful eBPF; most tools want 5.x and BTF. opensnoop and execsnoop print per event, which is fine on quiet paths — on a busy box switch to the aggregating tools or the per-event prints become their own overhead problem. bpftrace --no-annotation trims output when you script around it.

Evidence

This is the stack Netflix ran fleet-wide, and the stack Gregg documented in BPF Performance Tools (2020). Cloudflare uses the same mechanism at the packet layer — XDP programs drop DDoS traffic in the NIC driver path before it reaches the kernel's network stack. The common thread is instrumenting where events happen, at a per-event cost production tolerates.

Where it fails

Unprivileged BPF is disabled by default (kernel.unprivileged_bpf_disabled=1) since the speculative-execution CVEs; these tools run as root. The verifier rejects cleverness — unbounded work and pointer arithmetic it cannot prove safe. kprobe-based tools rot silently when kernel internals shift under them, and minimal container images ship without the BTF and headers some scripts want — the CO-RE path (bpftrace, libbpf tools) dodges most of that; raw bcc Python dodges less.

The practice

  • Answer the question with the named bcc tool first; write bpftrace only when no tool fits.
  • Tracepoint over kprobe for anything that must survive a kernel upgrade.
  • Aggregate in-kernel — @ = count(), hist() — and never print per event on a busy box.
  • Root and a reasonably new kernel are prerequisites; check them before blaming a script.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.