Using perf stat to Measure Linux CPU Performance
The perf stat command is a powerful profiling tool in
the Linux operating system designed to collect and aggregate hardware
and software performance counter statistics. This article explains the
primary purpose of perf stat, outlines the critical
hardware events it monitors—such as instruction counts, cache misses,
and branch mispredictions—and demonstrates how engineers use it to
diagnose bottlenecks and evaluate execution efficiency across
workloads.
Understanding the Purpose of perf stat
Part of the standard Linux perf subsystem,
perf stat acts as a high-level profiling utility that runs
a command and gathers performance counter statistics until that command
terminates. Instead of performing detailed call-graph or sample-based
profiling, perf stat provides an aggregated summary of
resource utilization. Its primary purpose is to deliver immediate,
low-overhead insight into how efficiently a program interacts with the
underlying CPU architecture.
Accessing Hardware Performance Counters
Modern processors include a Performance Monitoring Unit (PMU)
containing specialized hardware counters that track low-level CPU
microarchitectural events. The perf stat command interfaces
directly with the PMU through the Linux kernel to read these counters.
By default, running a command through perf stat monitors
several critical metrics:
- Task Clock and CPU Utilization: Measures the actual time the target task spent executing on the CPU, distinguishing compute time from I/O wait states.
- Context Switches and CPU Migrations: Tracks how frequently the operating system pauses the process to run another thread or moves the thread to a different physical core.
- Cycles and Instructions: Records the raw number of CPU clock cycles elapsed and the total number of hardware instructions executed.
- Instructions Per Cycle (IPC): Automatically calculates IPC (instructions divided by cycles), which serves as a primary indicator of CPU throughput. A low IPC (e.g., below 1.0) often suggests that the processor is stalling while waiting for memory or encountering branch mispredictions.
- Cache Misses: Measures L1, L2, or Last-Level Cache (LLC) accesses and misses, indicating whether performance is degraded by memory access latency.
- Branch Mispredictions: Quantifies how often the CPU's branch predictor guessed an execution path incorrectly, leading to pipeline stalls and flushed execution pipelines.
Primary Use Cases
- Benchmarking and Workload Characterization:
Developers use
perf statto establish performance baselines. By comparing runs before and after an optimization, engineers can verify whether changes reduced instruction counts or improved cache locality. - Diagnosing CPU vs. Memory Bottlenecks: By analyzing
the relationship between cycles, instructions, and cache misses,
perf statrapidly indicates whether an application is bound by compute complexity or memory bandwidth. - Whole-System and Process Monitoring: Beyond
wrapping individual commands,
perf statcan attach to an existing process using the-pflag or monitor the entire system across all cores using the-aflag for a defined duration.
By abstracting the complexity of CPU hardware performance monitoring
units into simple, readable terminal output, perf stat
serves as the foundational first step for performance engineering in
Linux environments.