Linux Command: perf
Linux kernel performance profiler and analyzer
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).
Syntax
perf [OPTIONS] COMMAND [ARGS]Key Options
statโ Count events for a command (cycles, instructions, cache misses)recordโ Sample and record profiling datareportโ Display profiling report from recorded datatopโ Live system-wide profiling (like top for CPU functions)traceโ Trace system calls (like strace but faster)benchโ Run built-in benchmarks (sched, mem, numa)
Examples
Quick performance overview
sudo perf stat -d ./myappProfile CPU usage
sudo perf record -g -p $(pgrep myapp) -- sleep 30 && sudo perf reportLive system-wide profiling
sudo perf top -gPro Tips
- For meaningful function names in reports, install debug symbols: apt install linux-tools-$(uname -r) and application-specific -dbg packages.
- Most perf commands need root. For user access: sysctl kernel.perf_event_paranoid=-1 (security risk) or add user to perf_users group.
- perf data is best visualized as FlameGraphs. Clone github.com/brendangregg/FlameGraph, then: perf script | stackcollapse-perf.pl | flamegraph.pl > flame.svg
Learn more: Full perf reference โ
Related Resources