perf
Introduction
perf is a powerful Linux performance analysis tool. You can use this tool to obtain the call information and resource consumption information in a process and search for and analyze hotspot functions.
Command examples:
Command |
Description |
|---|---|
perf top |
Checks hotspot functions in the current system. |
perf sched record -- sleep 1 -p $PID |
Records the system call of a process within 1s. |
perf sched latency --sort max |
Views the results recorded in the previous step and sorts the results by scheduling latency. |
Installation Method
Take CentOS as an example. Run the following command to install the tool:
1 | # yum -y install perf
|
How to Use
- Run the perf top command to search for hotspot functions.
This command collects statistics on the heat of each function in a performance event. By default, the CPU usage is displayed. You can use the -e option to monitor other events.
- Overhead indicates the proportion of the current event to all events.
- Shared object indicates the current event producer, such as the kernel, a perf command, and a C language library function.
- Symbol indicates the function name corresponding to the hot event.
By using hotspot functions, we can find behaviors that consume a lot of resources and optimize them accordingly.

- Collect thread calling information in a period.
The perf sched record command is used to record the process calling information in a period of time. -p is followed by the process ID, and sleep is followed by the statistics duration in seconds. The collected information is automatically stored in the perf.data file in the current directory.

- Parse the collected thread scheduling information.
The perf sched latency command is used to parse the perf.data file in the current directory. -s indicates sorting, and max indicates sorting based on the maximum delay.
