perf Command
The perf command is a powerful performance analysis tool for Linux. It provides a rich set of functionalities to profile CPU usage, analyze system calls, track events, and more, helping developers and system administrators optimize application and kernel performance.
Syntax
Description
The perf tool is part of the Linux kernel source tree and is designed to provide a low-overhead way to collect and analyze performance data. It can be used to understand where CPU cycles are being spent, identify bottlenecks, and gain insights into system behavior.
Common uses include:
- Profiling CPU usage of applications and the kernel.
- Analyzing system calls and their performance.
- Tracking hardware and software events.
- Generating flame graphs for visual performance analysis.
Common Commands and Options
| Command/Option | Description |
|---|---|
record |
Record performance data |
report |
Display performance data |
stat |
Run a command and gather performance statistics |
list |
List available events |
top |
System-wide performance monitoring in real-time |
probe |
Define new dynamic tracepoints |
-e <event> |
Select event(s) to sample |
-F <freq> |
Sampling frequency |
-g |
Record callgraph |
-p <pid> |
Profile a specific PID |
Examples
Record CPU cycles for a command
Records CPU cycles for the ls command.
Report performance data from a previous record
Analyzes the perf.data file (created by perf record) and displays a performance report.
Get system-wide statistics
Runs the sleep 5 command and displays performance statistics for its execution.
List all available performance events
Lists all hardware and software events that can be profiled with perf.
Record call graph for a running process
Records a call graph for the specified process ID (PID), useful for identifying function-level bottlenecks.