Linux perf command
perf is a performance analysis tool set for Linux systems, short for Performance Event Counters. It is based on the Linux kernel's perf_events subsystem and can provide performance analysis capabilities at both the hardware and software levels.
The main functions of perf include:
- CPU performance analysis
- function call tracing
- hardware event statistics
- software event monitoring
- system call tracing
perf basic syntax
The basic syntax of the perf command is:
perf [--version] [--help] COMMAND [ARGS]
Common subcommands include:
stat: performance counter statisticsrecord: record performance datareport: analyze recorded datatop: real-time performance monitoringlist: list available eventsannotate: source-level analysis
perf common subcommands in detail
perf stat
Counts various hardware and software events during command execution.
perf stat [options] command [command-options]
Common options:
-e: specify the events to monitor-p: monitor a specified process ID-a: monitor all CPUs-r: run repeatedly and display the average-d: display more detailed events
Example:
Example
Count the execution of the ls command
perf
stat
ls
Monitor a specified process
perf
stat
-p
1234
Monitor specific events
perf
stat
-e
cycles,instructions,cache-misses
ls
perf record
Record performance data to a file (perf.data by default).
perf record [options] command [command-options]
Common options:
-g: record the call graph-F: sampling frequency (Hz)-p: record a specified process-o: Specify the output file-e: specify the events to record
Example:
Example
Record the execution of the ls command
perf record
ls
Record process 1234 at a frequency of 99Hz
perf record
-F
99
-p
1234
-g
perf report
Analyze the data recorded by perf record.
perf report [options]
Common options:
-i: specify the input file-n: display the number of samples--stdio: output in text mode-g: display the call graph-s: sort by a specified field
Example:
Example
Analyze the default perf.data file
perf report
Analyze a specified file and output in text mode
perf report
-i
perf.data.old
--stdio
perf top
Display the functions that consume the most resources in the system in real time.
perf top [options]
Common options:
-e: specify the monitoring event-p: monitor a specified process-K: hide kernel symbols-U: hide user-space symbols-g: display the call graph
Example:
Example
Monitor system performance in real time
perf top
Monitor specific events
perf top
-e
cache-misses
perf list
List all monitorable events.
perf list [hw|sw|cache|tracepoint|pmu|event_glob]
Example:
Example
List all events
perf list
List hardware cache events
perf list cache
perf event types
perf can monitor many types of events:
| Event type | Description | Example |
|---|---|---|
| Hardware | CPU hardware events | cycles, instructions |
| Software | kernel software events | context-switches, page-faults |
| Cache | cache-related events | cache-references, cache-misses |
| Tracepoints | kernel static tracepoints | syscalls, block, sched |
| PMU | processor-specific events | (vendor specific) |
| Breakpoints | breakpoint events | mem:[:access] |
perf practical application examples
1. Analyze program performance bottlenecks
Example
Record program execution
perf record
-g
.
/
my_program
Analyze the results
perf report
-g
2. Find CPU hot-spot functions
perf top -p $(pidof my_program)
3. Compare the performance difference between two runs
Example
First run
perf record
-o
perf.data.1 .
/
my_program input1
Second run
perf record
-o
perf.data.2 .
/
my_program input2
Compare differences
perf
diff
perf.data.1 perf.data.2
4. Analyze system calls
Example
List available system call tracepoints
perf list
'syscalls:*'
Trace the open system call
perf
stat
-e
'syscalls:sys_enter_open'
-a
sleep
10
perf usage tips
-
Reducing overhead: for long-running performance analysis, appropriately lower the sampling frequency (e.g., -F 99)
-
Symbol resolution: ensure debug symbols are available, by installing a debug package or compiling the program with -g
-
Flame graph generation: combine with the FlameGraph tool to generate intuitive performance analysis graphs
-
Multi-core analysis: use the -a option to monitor all CPUs, or -C to specify a particular CPU
-
User/kernel space separation: use the -k and -u options to analyze kernel and user space separately
Common Problem Solutions
-
Permission issues:
echo -1 > /proc/sys/kernel/perf_event_paranoid
or run perf with sudo
-
Missing symbol information:
-
Sampling data is too large:
-
Cannot resolve high-level languages such as Java/Python:
Advanced learning resources
-
Official documentation: the tools/perf/Documentation directory in the Linux kernel source
-
Book recommendations:
-
Online resources:
-
Related tools:
By mastering the perf command, you can gain deep insight into Linux system performance characteristics, quickly locate performance bottlenecks, and optimize applications and system configuration.
Linux Command Reference