Linux Command Guide: perf

perf is the Linux performance analysis framework for profiling CPU usage, hardware counters, call graphs, and kernel events. It is extremely powerful for low-level performance tuning and investigation.

What perf is good for

Install

Install perf (varies by kernel version)
# Debian/Ubuntu
sudo apt install linux-tools-common linux-tools-$(uname -r)

# RHEL/CentOS/Fedora
sudo dnf install perf

# Arch
sudo pacman -S perf

10 Practical Examples

1) Basic CPU profiling
perf stat ls
2) Profile a running PID
sudo perf top -p 1234
3) Record performance data
sudo perf record -p 1234
4) Report recorded data
sudo perf report
5) Profile entire system for 10 seconds
sudo perf record -a sleep 10
6) Show hardware cache misses
perf stat -e cache-misses ls
7) Generate call graph
sudo perf record -g ./your_program
sudo perf report
8) Trace specific kernel event
sudo perf trace -e syscalls:sys_enter_open
9) Measure CPU cycles
perf stat -e cycles ./your_program
10) List available events
perf list