Observation and diagnostics · Intermediate

Measuring execution with perf stat

Interpret instructions, cycles, context switches, and faults alongside elapsed time when comparing Linux workloads.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Turn one timing result into several observations
  2. Select events and scope deliberately
  3. Use a ratio to ask the next question
  4. Retain uncertainty and restrictions
  5. Ask Logeia about this articleCheck your understanding

Turn one timing result into several observations

perf stat aggregates selected Linux performance events while a command runs. Instructions, cycles, context switches, and page faults describe different aspects of execution. The result concerns a selected scope and interval; it does not automatically identify the individual function responsible for a delay.

Define the comparison before measuring. Two programs must process equivalent work and produce acceptable results before fewer instructions can count as an improvement. Preserve the CPU model, kernel and perf versions, command, input, and relevant conditions so that the comparison can be repeated and challenged.

Select events and scope deliberately

The example launches a test program named demo five times. Use perf list to discover available events and choose alternatives when a requested event is unavailable. Per-task, per-CPU, and system-wide observations cover different work, so matching the command alone is not sufficient to establish matching measurement scope.

Hardware counter resources are finite. Requesting many events may cause time multiplexing, so inspect how much of the interval an event was actually measured. Reduce the event set when necessary. Ratios are useful only when their component counts represent sufficiently comparable work and observation intervals.

shell
perf list
perf stat -r 5 \
  -e task-clock,context-switches,page-faults,cycles,instructions \
  -- ./demo

Use a ratio to ask the next question

Suppose a run retires two hundred million instructions in one hundred million cycles. Its instructions-per-cycle ratio is two. This is arithmetic on two observed quantities, not a universal performance score across processors or workloads. Compare completed work and elapsed time alongside the ratio, especially when an optimization changes the instruction count.

Many context switches can motivate investigation of waits or thread organization, but the count alone does not prove a bottleneck. Likewise, a page fault need not involve a physical disk read. Use aggregate events to select a hypothesis, then narrow the investigation with an appropriate profile or operating-system observation.

Retain uncertainty and restrictions

Repeat the workload and inspect variation rather than reporting only the best run. Cache state, frequency changes, background activity, and input layout can change results. In multithreaded work, accumulated CPU time and elapsed wall time are also different quantities and should not be substituted for one another.

Availability depends on permissions, virtualization, and processor support. An unavailable counter does not mean that no such events occurred. Measure within the allowed scope, document unsupported events separately from program failures, and avoid making a stronger causal claim than the collected observations support.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 7

Prepare for AI systems · STEP 4

Choose a learning path