🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

perf Command

Advanced Performance & Debugging man(1)

Linux kernel performance profiler and analyzer

📅 Updated: Mar 16, 2026
SYNTAX
perf [OPTIONS] COMMAND [ARGS]

What Does perf Do?

The perf command is the official Linux kernel performance analysis tool. It provides access to hardware performance counters (CPU cycles, cache misses, branch mispredictions), software events (page faults, context switches), tracepoints, and dynamic probes for comprehensive system and application profiling.

perf is the most powerful performance analysis tool available on Linux, used by kernel developers, application developers, and system administrators to identify bottlenecks, optimize code, and debug performance regressions. It operates with minimal overhead compared to traditional profilers, making it suitable for production system analysis.

Key capabilities include CPU profiling (finding which functions consume the most CPU time), memory profiling (cache miss analysis), I/O latency analysis, lock contention detection, and system-wide tracing. perf can profile both user-space applications and kernel code, providing end-to-end visibility.

The perf tool suite includes several subcommands: perf stat (event counting), perf record/report (sampling profiler), perf top (live profiling), perf trace (syscall tracer), perf bench (benchmarks), and perf script (for generating FlameGraphs).

Options & Flags

OptionDescriptionExample
stat Count events for a command (cycles, instructions, cache misses) sudo perf stat ./myapp
record Sample and record profiling data sudo perf record -g ./myapp
report Display profiling report from recorded data sudo perf report
top Live system-wide profiling (like top for CPU functions) sudo perf top
trace Trace system calls (like strace but faster) sudo perf trace ls
bench Run built-in benchmarks (sched, mem, numa) perf bench sched messaging
script Output raw trace data for post-processing sudo perf script > trace.txt
list List available events and tracepoints perf list

Practical Examples

#1 Quick performance overview

Count CPU cycles, instructions, cache misses, and branch mispredictions. The -d flag adds detailed hardware counters.
$ sudo perf stat -d ./myapp

#2 Profile CPU usage

Record CPU samples with call graphs for 30 seconds on a running process, then display the report.
$ sudo perf record -g -p $(pgrep myapp) -- sleep 30 && sudo perf report

#3 Live system-wide profiling

Real-time view of which kernel and user-space functions consume the most CPU. Like top but shows functions instead of processes.
$ sudo perf top -g

#4 Generate FlameGraph data

Record and export trace data. Process with FlameGraph tools: stackcollapse-perf.pl perf.data.txt | flamegraph.pl > flame.svg
$ sudo perf record -g -p $(pgrep myapp) -- sleep 30 && sudo perf script > perf.data.txt

#5 Trace system calls

Trace all system calls made by nginx for 10 seconds. Faster and lower overhead than strace.
$ sudo perf trace -p $(pgrep nginx) -- sleep 10

#6 Analyze cache misses

Count cache misses for an application. High miss rates indicate poor memory access patterns.
$ sudo perf stat -e cache-misses,cache-references,L1-dcache-load-misses ./myapp

#7 Profile specific events

Count specific software events. Many context switches may indicate lock contention.
$ sudo perf stat -e context-switches,cpu-migrations,page-faults ./myapp

Tips & Best Practices

Install debug symbols: For meaningful function names in reports, install debug symbols: apt install linux-tools-$(uname -r) and application-specific -dbg packages.
Requires root or perf_event_paranoid: Most perf commands need root. For user access: sysctl kernel.perf_event_paranoid=-1 (security risk) or add user to perf_users group.
FlameGraphs for visualization: perf data is best visualized as FlameGraphs. Clone github.com/brendangregg/FlameGraph, then: perf script | stackcollapse-perf.pl | flamegraph.pl > flame.svg
Low overhead: perf has ~1-5% overhead compared to strace (~25-50%). Safe to use on production systems with sampling mode (perf record).

Frequently Asked Questions

How do I profile a program on Linux?
sudo perf record -g ./program, then sudo perf report. Shows which functions consume the most CPU time with call stacks. Use -g for call graph information.
What is a FlameGraph?
A FlameGraph is a visualization of profiled stack traces. Width represents CPU time. Generate with: sudo perf record -g -p PID -- sleep 30 && sudo perf script | stackcollapse-perf.pl | flamegraph.pl > flame.svg
How do I install perf?
Debian/Ubuntu: apt install linux-tools-$(uname -r). RHEL/Fedora: dnf install perf. The package name varies because perf must match your kernel version.
What is the difference between perf and strace?
strace traces system calls with high overhead (~25-50%). perf is a sampling profiler with low overhead (~1-5%) that profiles CPU, cache, memory, and more. perf trace is a faster strace alternative.
Can I use perf in a container?
Yes, but with restrictions. You need SYS_ADMIN capability or host perf_event access. Run with: docker run --cap-add SYS_ADMIN --privileged. The perf binary must match the host kernel version.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →