A bpftime hot probe slows to microseconds per hit from its maps, not from bpftime
Symptom#
A userspace uprobe run by bpftime costs several microseconds per hit, while an empty bpftime program costs about 0.4 us on the same machine. A latency benchmark under the tracer is about 2x slow in the first repetition and about 6x slow in every later one. Counts from probes in aggregate mode are lost or missing.
Cause#
Four separate causes, each in how the tracer uses bpftime's maps:
- The tracer mirrors its kernel maps into bpftime shared memory, but its server drained only the kernel aggregation map. The shared-memory copy filled (it holds 4096 buckets), and bpftime's fixed-size hash then scanned every slot twice per hit (lookup miss, rejected update): about 20 us per hit.
- bpftime's fixed-size hash deleted an entry by leaving a plain empty slot in a linear-probing table. An entry past a later hole is hidden, the writer inserts a duplicate, and a
get_next_keywalk cycles on it. Counts are lost. - bpftime runs
BPF_MAP_TYPE_LRU_HASHaslru_var_hash_map, a boost map with list nodes allocated from the shared-memory allocator (rbtree_best_fit). A probe that inserts an entry at function entry and deletes it at exit pays two allocator calls per hit: about 2.6 us. - bpftime's shared-memory spinlock never gives up the CPU. With two traced threads on one core, a waiter spins out its whole time slice while the holder is preempted: millisecond stalls.
Fix#
- Drain the bpftime copy of every aggregation map at the same cadence as the kernel copy (
bpftime_map_get_next_key,bpftime_map_lookup_elem,bpftime_map_delete_elemby its shm id). Report when a drain finds the map full. - Delete from the fixed-size hash by backward shift (Knuth vol. 3, Algorithm R): after emptying slot i, move back each later entry of the cluster whose home slot is not cyclically in (i, j].
- Mirror an LRU hash used for entry/exit pairing as a fixed-size
BPF_MAP_TYPE_HASHin bpftime. A fixed map does not evict, so count a failed insert and report it. - In the spinlock, call
sched_yield()every 64 failed spins. - Build bpftime with
-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_INFO. Its default isSPDLOG_LEVEL_TRACE, which keeps about 180 debug and trace macros on the map path.
Evidence#
dpucrumbs on a BlueField-3 Arm, an RDMA proxy (buddy) with hot probes on two per-message functions. A microbenchmark of a noinline function, 1M calls: no probe 1.8 ns, empty bpftime program entry and exit 377 ns, the tracer's program with the LRU map 3.9 to 4.4 us, with a fixed hash 1.27 to 1.30 us. Latency p50, untraced 32.9 us: before the fixes 62 us in repetition 0 and 195 to 215 us after; after all fixes 44.2 us in every repetition, p99 127 to 168 us. A hash test with 200k random operations at 4096 entries: the original delete failed 318519 checks, backward shift 0. Aggregated send_imm counted 7785 of 20000 posts with the plain-hole delete, 19985 to 20000 after.
Also found by searching
- bpftime uprobe costs microseconds per hit instead of a few hundred nanoseconds
- traced latency jumps after the first repetition and stays high
- aggregated hot probe counts are missing from the trace
- lru_var_hash_map rbtree_best_fit hot in perf of a bpftime agent