Lesson 3 of 9 · 24m
Measure the Machine: perf, IPC, and Cache Misses
How to use hardware performance counters to separate memory stalls, branch misses, and instruction overhead instead of optimizing blind.
The worst performance conversations sound like religion: someone prefers one loop shape, someone else blames allocation, and nobody has data. Hardware counters end those arguments. They do not tell the whole truth, but they tell enough truth to stop you from shaving microseconds off the wrong nanosecond.
Start with perf stat
On Linux, perf stat runs a program and summarizes counters:
perf stat -e cycles,instructions,cache-references,cache-misses,branch-misses ./benchA typical output includes:
8,412,330,104 cycles
10,104,551,003 instructions # 1.20 insn per cycle
440,120,992 cache-references
112,501,382 cache-misses # 25.56% of all cache refs
31,904,511 branch-missesThe first number to inspect is instructions per cycle (IPC). It is not a score where higher always means better, but it is a clue. A simple loop running at IPC 0.2 is probably stalled; IPC 3 means the core is retiring substantial work every cycle. Interpret it alongside instruction count — vectorized code may retire fewer instructions while doing more work.
Cache misses are a symptom, not a verdict
A high cache-miss rate often means poor locality, but you need context. A program that touches gigabytes once may have unavoidable misses. A program reusing a small structure should not miss constantly. Compare miss counts per logical operation rather than raw percentages.
perf stat -e L1-dcache-load-misses,LLC-load-misses ./bench
perf stat -e cycles,stalled-cycles-frontend,stalled-cycles-backend ./benchExact event names vary by CPU. Run perf list to see what your machine exposes. When names differ, use the generic cache-misses and branch-misses first.
Use perf record when the phase matters
A whole-program average can hide the hot spot. Sampling tells you where cycles went:
perf record -g ./bench
perf reportCompile with frame pointers or use a build that preserves useful unwind information. For optimized C/C++, -fno-omit-frame-pointer is often worth keeping in production because observability beats a tiny register-saving optimization.
Microbenchmarks lie confidently
A benchmark loop can be optimized away, hoisted, vectorized differently, or measured while data is unrealistically hot in cache. Guard against all four:
- Print or consume the result so the computation is observable.
- Benchmark realistic input sizes, not only data that fits in L1.
- Pin frequency noise down where possible; expect variance rather than pretending it is absent.
- Compare counters, not just wall time.
Field notes
- Never tune from a single run. Use repeated runs and look for stable direction, not decimal precision.
perfcounter availability changes under virtual machines and restrictiveperf_event_paranoidsettings.- IPC low plus high cache misses points to memory; IPC low plus high branch misses points to control flow.
- A profiler can tell you where time goes, but only understanding the workload tells you whether that time is necessary.
- Record the CPU model, kernel, compiler, and input. Performance numbers without context are folklore.
Was this lesson helpful?