Site Tools


wiki:perf-stat

Table of Contents

perf stat

perf stat counts hardware events over the full runtime of a program and prints a summary when it exits. It's the fastest way to get a quantitative characterization of CPU behavior without sampling overhead.

perf stat ./program
perf stat -e cycles,instructions,cache-misses ./program
perf stat -e cycles,instructions,cache-misses,L1-dcache-load-misses ./program

Without -e, perf uses a default set of useful high-level metrics: cycles, instructions, branches, cache behavior, context switches, page faults. With -e, you select specific events to count. Multiple events can be specified separated by commas or with multiple -e flags.

The output shows event counts with derived metrics (calculated ratios):

Performance counter stats for './program':

    16,847,203,412 cycles                  # 4.107 GHz
    12,334,901,088 instructions            # 0.73 insn per cycle
     2,301,445,221 branches                # 561.035 M/sec
        48,223,901 branch-misses           # 2.09% of all branches
     1,203,887,654 cache-references        # 293.461 M/sec
       401,292,771 cache-misses            # 33.33% of cache refs

The comment after each line shows a derived metric. IPC (instructions per cycle) is computed as instructions/cycles. Cache miss rate is cache-misses/cache-references.

Common event sets for profiling:

# Memory behavior (cache and bandwidth)
perf stat -e cycles,instructions,cache-references,cache-misses ./program
 
# Branch prediction
perf stat -e cycles,branches,branch-misses ./program
 
# Front-end stalls (instruction fetch)
perf stat -e cycles,instructions,stalled-cycles-frontend ./program
 
# Back-end stalls (execution and memory)
perf stat -e cycles,instructions,stalled-cycles-backend ./program

For multithreaded programs:

perf stat -a ./program          # count system-wide across all CPUs
perf stat -p PID                # attach to running process by PID

-a (all CPUs) counts events on every core; useful for seeing total CPU utilization. -p attaches to a running process (identified by its PID) and profiles it without restarting. Detach with Ctrl+C.

Repeat runs:

perf stat -r 10 ./program       # run 10 times and average results

This is useful for noisy measurements or programs with variable runtime. perf reports the average and standard deviation.

wiki/perf-stat.md · Last modified: by 127.0.0.1