Table of Contents
The 3 AM `perf top` Flatline
Samples: 1.2M of event 'cpu-clock', 4000 Hz, Event count (approx): 1024389102,
Overhead Shared Object Symbol
----------------------------------------------------------------------
41.22% [kernel.kallsyms] [k] __bpf_prog_run
22.14% [kernel.kallsyms] [k] bpf_trampoline_64_func
14.89% api.prod-us-east-1.mycompany.io [.] 0x00000000004f12a0
4.01% [kernel.kallsyms] [k] native_write_msr
2.11% [kernel.kallsyms] [k] smp_call_sidep
0.85% [kernel.kallsyms] [k] try_bpf_map_lookup_update
My coffee was lukewarm. Cold brew tastes better when you aren’t staring at a kernel panic screen on `Linux 5.15.0-70-generic`. The PagerDuty alarm didn’t just ring; it shrieked. P99 latency on `api.prod-us-east-1.mycompany.io` had blown past four hundred milliseconds. Our service-level objectives were actively burning. Traffic to `tcp/8443` was backing up behind connection pools. The kernel was melting. Look at that `perf top` output above. Sixty-three percent of total CPU cycles were being consumed by the virtual machine executing BPF bytecode. Sixty-three.
I didn’t write that code. The platform engineering squad wrote it. They wrapped our entire ingress mesh in an ebpfbased distributed tracing system to get observability. They called it clean. They called it modern. They lied. It was a parasitic crawler eating our memory bus alive. Every incoming packet on `eth0` was intercepted, wrapped in a context struct, hashed into a ring buffer, and abandoned to race conditions. The machine thrashed. Interrupt handlers stacked up. SoftIRQs spiked.
The Hunt for the Phantom Overhead
Panic is a bad debugger. I closed Slack. I ignored the executive pinging me about customer dashboards. I needed raw numbers. I attached `bpftool` to inspect what monstrosity was running on our network stack. The maps were massive. We had millions of active entries tracking connection state from every ephemeral port hitting our load balancers. The garbage collection routine was blocked. We hit the verifier limits hard.
Here is what the initialization script looked like in our deployment pipeline. It compiled a suite of tracing probes without checking the target kernel’s capabilities on `5.15.0-70-generic`:
#!/usr/bin/env bash
# Deploying the tracing hooks
bpftool prog load trace_net_rx.bpf.o /sys/fs/bpf/net_rx \
autoattach \
map name flow_cache array pinned /sys/fs/bpf/maps/flow_cache \
map name span_registry hash pinned /sys/fs/bpf/maps/span_registry
That script launched a dozen probes attached to `tcp_sendmsg`, `tcp_cleanup_rbuf`, and `__dev_queue_xmit`. Every single socket operation was paying a tax. We were paying for observability with raw metal throughput. I watched the CPU telemetry climb. The hardware thermal throttled. The cloud provider hypervisor started stealing cycles. It was a cascade failure fueled by our own telemetry tooling.
Unpacking the Map Allocation Catastrophe
The core issue sat inside the hash map definition for tracking request spans. The platform team used unbounded hash maps for high-cardinality data. When thousands of concurrent TLS handshakes hit `tcp/8443`, the kernel allocated new buckets continuously. Lock contention on the internal BPF map spinlocks went through the roof. CPUs fought for access to the same cache lines.
/* The offending map definition in the tracing agent */
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10000000); // Ten million entries!
__type(key, struct span_key_t);
__type(value, struct trace_context_t);
__uint(pinning, LIBBPF_PIN_BY_NAME);
} span_registry SEC(".maps");
Ten million max entries. On a host with sixty-four gigabytes of RAM, the verifier allowed it because the limit wasn’t strictly enforced by cgroups for pinned maps at load time. The kernel allocated backing memory pages directly from high memory zones. Page reclaim went wild. kswapd woke up and started thrashing disk I/O. For an observability tool. Let that sink in.
Deconstructing the ebpfbased distributed tracing Monster
Observability is a drug. Engineers want to see everything. They want flame graphs for every packet. They want spans for every system call. But safety in user space does not translate to safety in kernel space. When you inject instructions directly into the ring buffer via an ebpfbased distributed tracing daemon, you bypass standard memory protections. You execute bytecode with root privileges inside the ring zero context.
We ran a quick trace using `trace-cmd` to see what kernel functions were blocking the execution paths:
# -----------------------------------------------------------------
# TASK-PID CPU# D TIMESTAMP FUNCTION
# | | | | | |
nginx-29112 [003] ... 1024.110293: bpf_trace_printk: span_registry full, dropping packet
nginx-29112 [003] ... 1024.110298: kfree_skb: skbaddr=0xffff98124a bpf_drop=1
# -----------------------------------------------------------------
Packets were hitting the ring buffer, failing to register because the map was full, and dropping. Our load balancer was actively shedding traffic because the tracing tool could not keep up with the write frequency. We were dropping five percent of all inbound connections to `api.prod-us-east-1.mycompany.io` because the tracer was too slow to record them.
Analyzing the Verifier Overhead
The kernel verifier checks every instruction before letting it run. With complex tracing logic, the instruction count per program touched the maximum limit of one million instructions. JIT compilation took milliseconds, but load times stalled the entire container runtime initialization. Every time our orchestration agent rolled out a minor configuration tweak, the pod would hang during startup.
- Instruction limit: 1,000,000 per program
- Actual instructions compiled: 987,412
- Loop unrolling overhead: Extreme
- Branch prediction misses: Catastrophic
We were asking the kernel to perform string manipulation, cryptographic hashing, and timestamp correlation inside a kprobe callback. That is not what kprobes are for. Kprobes are scalpels. We were using a chainsaw on a Swiss watch.
Surgical Mitigation and Triage
The clock hit 4:15 AM. I stopped looking at dashboards. I logged into the staging instance and started killing processes. The production cluster needed an immediate emergency drop of the tracing daemons. No graceful shutdown. Just unload the modules and reclaim the memory.
I ran the purge command across the fleet via our orchestration control plane:
#!/usr/bin/env bash
# Emergency cleanup of rogue BPF programs
for pin in /sys/fs/bpf/*; do
if [[ -d "$pin" ]]; then
rm -rf "$pin"
fi
done
bpftool prog show --json | jq -r '.[].id' | xargs -I {} bpftool prog detach id {}
The effect was instantaneous. CPU usage dropped off a cliff. The flatline on the `perf top` display vanished. `__bpf_prog_run` disappeared from the top twenty symbols. Latency on `api.prod-us-east-1.mycompany.io` plummeted from four hundred milliseconds back down to twelve microseconds. The database connection pools drained. The pager finally shut up.
We had survived the night, but the post-mortem was going to be brutal. The platform team wanted their observability back. I told them they could have it when they learned how to write bounded maps and atomic counters.
Rewriting the Tracing Primitives
If we were going to run tracing code in production again, it had to be restricted. No more dynamic hash maps for transient spans. We moved to pre-allocated per-CPU array maps. Fixed sizes only. If the buffer fills up, it drops silently without stalling the networking path.
/* The corrected map configuration */
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1024); // Small, fixed, per-CPU
__type(key, __u32);
__type(value, struct streamlined_metric_t);
__uint(pinning, LIBBPF_PIN_BY_NAME);
} fast_metrics SEC(".maps");
We stripped out all string parsing. We removed dynamic stack walking where possible. We replaced heavy context collection with simple numeric counters incremented via atomic operations. The performance impact dropped to less than half a percent of total CPU cycles.
Lessons from the Abyss
Never trust an out-of-the-box observability platform to respect your hardware constraints. The kernel is not a garbage-collected runtime. It does not care about your distributed tracing architecture. If you push bad code into ring zero, it will take your infrastructure down with a smile.
Here are the rules we established for our engineering org going forward:
- Never use dynamic hash maps in high-throughput network paths without strict eviction policies
- Keep total instruction counts below fifty thousand per program
- Profile every tracing deployment under load test conditions that mirror peak traffic on `api.prod-us-east-1.mycompany.io`
- Verify map memory consumption against cgroup limits before releasing to production
- Keep a bottle of cold brew within arm’s reach at all times
The ghosts of 3 AM incidents never truly leave. They linger in the logs, waiting for the next developer who thinks they can add “just one more trace point” without checking the assembly output. Keep your code lean. Measure everything. Trust nothing.
Related Articles
Explore more insights and best practices: