Overview
perf is an essential tool for developers and system administrators to diagnose and optimize performance issues. Through various subcommands, it offers a wide range of performance analysis capabilities, including statistics collection, event recording, and report generation.
Key Features
- CPU Profiling (CPU usage, call stack analysis)
- Hardware Event Monitoring (cache misses, branch mispredictions, etc.)
- Software Event Monitoring (scheduling events, page faults, etc.)
- System Call Tracing and Analysis
- Dynamic Tracing (kprobes, uprobes)
Key Options (Subcommands)
perf performs its functions through various subcommands. Each subcommand has its own unique options.
perf stat: Collect Statistics
perf record: Record Performance Data
perf report: Analyze Recorded Data
perf list: List Available Events
Generated command:
Try combining the commands.
Description:
`perf` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Various usage examples of the perf command.
View Statistics for 'ls' Command Execution
perf stat ls
Check basic performance statistics such as CPU cycles, instruction count, and cache misses during the execution of the 'ls' command.
Monitor a Specific Process for 5 Seconds
perf stat -p 1234 sleep 5
Monitor the performance statistics of the process with PID 1234 for 5 seconds. (Replace with the actual PID)
Record Call Graph During Application Execution
perf record -g ./my_app
Records performance data, including function call stack information, during the execution of my_app. The recorded data is saved to the perf.data file.
Analyze Recorded Data
perf report
Interactively analyze the perf.data file generated by perf record to visually identify performance bottlenecks.
View Available Hardware Events
perf list hw
Check the list of all hardware performance events that perf can monitor on the current system.
Installation
perf may not be installed by default on most Linux distributions. You can install it using the following commands.
Debian/Ubuntu
sudo apt update
sudo apt install linux-tools-common linux-tools-$(uname -r)
Commands to install perf on Debian or Ubuntu-based systems. You need to install the linux-tools package corresponding to your kernel version.
CentOS/RHEL/Fedora
sudo yum install perf
# or sudo dnf install perf (Fedora)
Commands to install perf on CentOS, RHEL, or Fedora-based systems.
Tips & Precautions
Useful tips and precautions when using perf.
Root Privileges
Most perf commands require root privileges (sudo) as they collect system-wide performance data.
- If you encounter an 'Operation not permitted' error when running perf commands, try using sudo.
Loading Kernel Symbols
If function names appear as 'unknown' in perf report, you need to load kernel symbols. This can be done by installing the 'kernel-debuginfo' or 'kernel-devel' packages.
- CentOS/RHEL: `sudo debuginfo-install kernel`
- Ubuntu/Debian: `sudo apt install linux-image-$(uname -r)-dbg`
Overhead
perf collects very detailed data, which can cause some overhead on the system, especially when using `perf record` with a high sampling frequency. Use with caution in production environments.
- On performance-sensitive systems, start with a lower sampling frequency (-F option) to minimize overhead.
Utilizing perf top
`perf top` is a useful subcommand that shows functions with high CPU usage in real-time. It is suitable for simple performance monitoring.
- Use `perf top` to quickly identify the functions consuming the most CPU time on your current system.