freenode
Kernel & Low-Level

Perf finally measures what its tests reach, and finds real bugs

A clang coverage harness for the profiler turned up annotation mistakes and tests that fail when the machine is merely different.

Nothing in the perf test suite measured how much of the tool it actually exercised. Arnaldo Carvalho de Melo has added a clang source-based coverage harness that builds an instrumented perf, runs the full suite, and reports line and function coverage, plus a view of the largest gaps across the tree rather than only the files tests already touch.

The first numbers are modest. On an AMD host with clang and DWARF 4, the suite reached 47.53% of lines in the instrumented tree and 49.61% inside perf itself: 170 tests passed, 27 were skipped, and one known tracepoint failure remained. A sequential run as root on a Raspberry Pi 4 reached 43.41% of lines and 44.31% of perf. Those totals include libbpf and the dlfilters. A Rust workload is built and run but not instrumented. The known failure still makes the coverage target exit with an error after the report is written.

What the runs found matters more than the percentages. Perf's data-type annotation, which maps samples back onto C types, was dropping x86 details that show up constantly in a clang kernel build. Absolute addresses were missing from the resolver. The movabs instruction was not recognized, so its operands were never split. A clang allmodconfig kernel image contains hundreds of thousands of those instructions, all immediate loads of the KASAN shadow offset. Wide immediates were stored in a 32-bit field and truncated before register tracking recorded them. Instruction statistics were printed before samples were counted. A flexible-array member could show 100% of samples while its parent struct showed 0%, because the struct only counted its declared size.

The same runs exposed tests that fail when the environment differs, not when perf is wrong. Intel PT's virtual last-branch-record check lost every sample when the host's locked-memory limit left a one-page data ring. A dlfilter test tripped on samples whose instruction pointer the kernel zeroes when a user-only event skids into kernel mode, a leak fix since Linux 7.3 that root runs had not been hitting. An inline-unwinding check treated "this PMU has a branch stack" as "call-stack mode works," so AMD's newer branch-record hardware failed the test instead of skipping. BPF summary tests failed on kernels without BTF even when perf itself was built with libbpf. The offline-CPU stat test treated a missing hotplug file as an offline processor, which is how kernels without CPU hotplug look.

New tests cover the x86 operand parser, globals reached through clang's DWARF 5 address indexes (gcc emits a plain address), flexible arrays, a struct pointer kept in a register at high optimization, and the annotation statistics. The coverage build also needed the GTK interface linked with the coverage flags, or its counters never appeared.