perf · Linux tool

Also known as: perf_events

On this page 7

perf is the Linux tool for performance analysis built on the kernel’s perf_events subsystem, which collects hardware performance counters, software events, tracepoints and sampled call stacks from the CPU and the kernel. In Linux monitoring, it answers a question that utilities such as top or vmstat cannot: where exactly the CPU time is spent, down to individual functions and instructions.

The tool lives in the kernel source tree under tools/perf and is usually packaged as linux-perf or linux-tools. Its user-space command and the kernel interface are developed together, so the perf version should match the running kernel as closely as possible. Not to be confused with unrelated benchmarking programs with similar names.

How perf collects performance data

perf works by programming event sources through the perf_event_open system call and then either counting occurrences or sampling the system state when a counter overflows. Counting gives exact totals with very low overhead; sampling records the instruction pointer, process ID and optionally the call chain at regular intervals, which reveals hot code paths statistically.

Hardware events come from the Performance Monitoring Unit (PMU) of the processor, which counts cycles, retired instructions, cache misses and branch mispredictions. Software events and tracepoints come from the kernel itself, while kprobes and uprobes allow dynamic instrumentation of almost any kernel or user-space function. The table below summarizes the main event sources.

Event sourceExamplesTypical use
Hardware eventscycles, instructions, cache-missesCPU efficiency and microarchitecture analysis
Software eventscontext-switches, page-faults, cpu-clockScheduler and memory behavior
Tracepointssched:sched_switch, block:block_rq_issueStable kernel instrumentation points
Dynamic probeskprobes, uprobesAd hoc tracing of chosen functions
Raw PMU eventsVendor-specific codesDetailed hardware investigations
Event sources available to perf

Core perf subcommands

perf is organized as a single binary with subcommands, similar to git. A typical workflow starts with an overview from perf stat, continues with perf top or perf record to find hot functions, and ends with perf report or perf annotate to inspect the details.

SubcommandWhat it doesOutput
perf statCounts events for a command or systemTotals, ratios such as instructions per cycle
perf topLive sampling of the whole systemContinuously updated list of hot functions
perf recordSamples events and writes perf.dataBinary profile file
perf reportAnalyzes a recorded profileInteractive or text breakdown by symbol
perf annotateMaps samples to source or assemblyPer-instruction cost
perf traceTraces system calls with low overheadstrace-like event stream
perf schedAnalyzes scheduler latencyWake-up and run queue delays
Frequently used perf subcommands

A common command is perf record -F 99 -a -g -- sleep 30, which samples all CPUs at 99 Hz with call graphs for 30 seconds. The slightly odd frequency avoids lockstep sampling with periodic system activity running at round intervals such as 100 Hz.

perf compared with other Linux monitoring tools

perf occupies the middle ground between simple resource monitors and fully programmable tracers. Tools such as top and pidstat show which process uses the CPU, while perf shows which functions inside that process consume it; bpftrace and other eBPF tools go further by running custom logic in the kernel.

ToolGranularityOverheadBest for
top and htopProcess levelVery lowQuick view of resource consumers
perfFunction and instruction levelLow with samplingCPU profiling and hardware counters
straceSystem call levelHighDebugging the behavior of one process
bpftraceCustom, any probeLow to moderateAd hoc questions needing in-kernel logic
ltraceLibrary call levelHighTracing shared library usage
perf and related monitoring tools

perf and eBPF are complementary rather than competing. perf can load and run BPF programs in some modes, and both rely on the same kernel probe infrastructure; the practical difference is that perf excels at statistical CPU profiling, while eBPF tools excel at custom aggregation in the kernel.

Producing useful profiles with perf

Profile quality depends heavily on symbol and stack information. Without debug symbols, reports show raw addresses; without frame pointers or unwinding data, call graphs are truncated. perf supports three unwinding methods through the --call-graph option, each with different costs.

  • Frame pointers (fp): cheapest and most reliable when binaries are compiled with -fno-omit-frame-pointer.
  • DWARF unwinding (dwarf): works without frame pointers but copies stack memory and produces large files.
  • Last Branch Record (lbr): uses Intel hardware to capture short call chains with minimal overhead.

Output from perf script is commonly converted into flame graphs, which display sampled stacks as stacked bars so the widest functions immediately stand out. Just-in-time runtimes such as the JVM or Node.js need perf map files or runtime-specific agents so that generated code can be resolved to function names.

Limitations, permissions and common pitfalls

Access to perf events is controlled by the sysctl kernel.perf_event_paranoid. Higher values restrict unprivileged users, and many distributions set a restrictive default because performance counters can leak information useful for side-channel attacks. Since kernel 5.8, the CAP_PERFMON capability grants perf access without full root privileges.

SymptomLikely causeFix
Hex addresses instead of namesMissing debug symbolsInstall debuginfo packages or build with symbols
Broken or one-frame stacksOmitted frame pointersRebuild with frame pointers or use DWARF unwinding
Permission deniedRestrictive paranoid settingGrant CAP_PERFMON or adjust the sysctl carefully
Hardware events not supportedVirtual machine without PMU accessEnable PMU passthrough or use software events
Common perf problems and fixes

Sampling is statistical, so short profiles of brief workloads can mislead; collecting enough samples matters more than a high frequency. Very high frequencies increase overhead and may distort the behavior being measured, which is the most frequent mistake made by new users.

The simple explanation

perf works like a stopwatch combined with a photographer inside the computer. Many times every second, it takes a quick snapshot of what the processor is doing at that instant.

After thousands of snapshots, a clear picture emerges: if one piece of a program appears in half of the photos, that piece is using about half of the processor’s time. This tells engineers exactly which part of the software to fix instead of guessing.

How it is used in practice

A site reliability engineer at an online retailer notices that a Linux API server runs at 90 percent CPU during peak hours, although request volume has barely changed. Process monitors show only that the Java service is busy, so the engineer runs perf record -F 99 -g -p against the service for 60 seconds, with the JVM configured to emit a perf map file.

The resulting flame graph shows that nearly 40 percent of samples sit in a regular expression compiled on every request inside a logging filter. After the developers move the compilation to startup, CPU usage at peak falls to about 55 percent, and the team postpones a planned hardware upgrade because the bottleneck turned out to be code rather than capacity.