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 source | Examples | Typical use |
|---|---|---|
| Hardware events | cycles, instructions, cache-misses | CPU efficiency and microarchitecture analysis |
| Software events | context-switches, page-faults, cpu-clock | Scheduler and memory behavior |
| Tracepoints | sched:sched_switch, block:block_rq_issue | Stable kernel instrumentation points |
| Dynamic probes | kprobes, uprobes | Ad hoc tracing of chosen functions |
| Raw PMU events | Vendor-specific codes | Detailed hardware investigations |
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.
| Subcommand | What it does | Output |
|---|---|---|
| perf stat | Counts events for a command or system | Totals, ratios such as instructions per cycle |
| perf top | Live sampling of the whole system | Continuously updated list of hot functions |
| perf record | Samples events and writes perf.data | Binary profile file |
| perf report | Analyzes a recorded profile | Interactive or text breakdown by symbol |
| perf annotate | Maps samples to source or assembly | Per-instruction cost |
| perf trace | Traces system calls with low overhead | strace-like event stream |
| perf sched | Analyzes scheduler latency | Wake-up and run queue delays |
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.
| Tool | Granularity | Overhead | Best for |
|---|---|---|---|
| top and htop | Process level | Very low | Quick view of resource consumers |
| perf | Function and instruction level | Low with sampling | CPU profiling and hardware counters |
| strace | System call level | High | Debugging the behavior of one process |
| bpftrace | Custom, any probe | Low to moderate | Ad hoc questions needing in-kernel logic |
| ltrace | Library call level | High | Tracing shared library usage |
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.
| Symptom | Likely cause | Fix |
|---|---|---|
| Hex addresses instead of names | Missing debug symbols | Install debuginfo packages or build with symbols |
| Broken or one-frame stacks | Omitted frame pointers | Rebuild with frame pointers or use DWARF unwinding |
| Permission denied | Restrictive paranoid setting | Grant CAP_PERFMON or adjust the sysctl carefully |
| Hardware events not supported | Virtual machine without PMU access | Enable PMU passthrough or use software events |
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.