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:

Primary Use Cases

  1. Benchmarking and Workload Characterization: Developers use perf stat to establish performance baselines. By comparing runs before and after an optimization, engineers can verify whether changes reduced instruction counts or improved cache locality.
  2. Diagnosing CPU vs. Memory Bottlenecks: By analyzing the relationship between cycles, instructions, and cache misses, perf stat rapidly indicates whether an application is bound by compute complexity or memory bandwidth.
  3. Whole-System and Process Monitoring: Beyond wrapping individual commands, perf stat can attach to an existing process using the -p flag or monitor the entire system across all cores using the -a flag 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.