tencent cloud

TencentOS Server

perf Performance Analysis

Baixar
Modo Foco
Tamanho da Fonte
Última atualização: 2026-10-19 17:22:51
Traduzido por IA

Introduction

perf is a lightweight performance analysis tool that can profile and trace both kernel and user space programs.

Installation

The kernel has been compiled with support for it, so you can directly install the user space software package:
dnf install perf
Note: You can use perf --help to view perf subcommands.

Using perf

perf Events

perf supports hardware events (such as cycles and cache-misses), software events (such as page-faults), kprobes/uprobes, and tracepoints. You can use perf list to view the specific events supported by perf, and use perf list --help to view more detailed usage.
View the tracepoints supported by perf:
perf list tracepoint
Output example (excerpt):

List of pre-defined events (to be used in -e or -M):

alarmtimer:alarmtimer_cancel [Tracepoint event]
alarmtimer:alarmtimer_fired [Tracepoint event]
alarmtimer:alarmtimer_start [Tracepoint event]
alarmtimer:alarmtimer_suspend [Tracepoint event]
avc:selinux_audited [Tracepoint event]
block:block_bio_backmerge [Tracepoint event]
block:block_bio_bounce [Tracepoint event]
block:block_bio_complete [Tracepoint event]
block:block_bio_frontmerge [Tracepoint event]

perf stat

perf has two modes:
In counting mode, selected events are counted through PMC registers.
In sampling mode, the number of times functions are executing at sampling intervals is counted. The longer a function runs, the higher the probability that it is sampled, and therefore the higher its CPU usage.
perf stat counts events and displays a summary, but does not record them to the perf.data file. The following example counts system calls:
perf stat -e "syscalls:sys_enter_*" -a -- sleep 10
Output example:

Performance counter stats for 'system wide':

1 syscalls:sys_enter_socket
0 syscalls:sys_enter_socketpair
0 syscalls:sys_enter_bind
0 syscalls:sys_enter_listen
1 syscalls:sys_enter_accept4
0 syscalls:sys_enter_accept
1 syscalls:sys_enter_connect
0 syscalls:sys_enter_getsockname
0 syscalls:sys_enter_getpeername
2 syscalls:sys_enter_sendto
8 syscalls:sys_enter_recvfrom
3 syscalls:sys_enter_setsockopt
4 syscalls:sys_enter_getsockopt

perf record

perf record samples events and records them to the perf.data file, and can also provide information such as call stacks.
Sample all CPUs as follows:
perf record -a -g -- sleep 5
Generate the perf.data file in the current directory where the command is run.

perf report

perf report analyzes the performance data in the perf.data file.
Run perf report directly in the directory where the perf.data file is located to enter the interactive mode, which is similar to the top command. Follow the prompts to perform operations in the window:
perf report
Output example:
Samples: 7K of event 'cpu-clock:pppH', Event count (approx.): 1945500000
Children Self Command Shared Object Symbol
+ 99.88% 0.00% swapper [kernel.vmlinux] [k] secondary_startup_64_no_verify
+ 99.88% 0.00% swapper [kernel.vmlinux] [k] cpu_startup_entry
+ 99.88% 0.01% swapper [kernel.vmlinux] [k] do_idle
+ 99.87% 0.01% swapper [kernel.vmlinux] [k] cpuidle_idle_call
+ 99.86% 0.01% swapper [kernel.vmlinux] [k] default_idle_call
+ 99.85% 99.73% swapper [kernel.vmlinux] [k] __cpuidle_text_start
+ 82.83% 0.00% swapper [kernel.vmlinux] [k] start_secondary
+ 17.05% 0.00% swapper [kernel.vmlinux] [k] start_kernel
+ 17.05% 0.00% swapper [kernel.vmlinux] [k] arch_call_rest_init
+ 17.05% 0.00% swapper [kernel.vmlinux] [k] rest_init
0.12% 0.00% swapper [kernel.vmlinux] [k] __irq_exit_rcu
Run perf report --stdio to enter the non-interactive mode, which is suitable for redirecting analysis results to a text file:
perf report --stdio
Output example (excerpt):
# Total Lost Samples: 0
#
# Samples: 7K of event 'cpu-clock:pppH'
# Event count (approx.): 1945500000
#
# Children Self Command Shared Object Symbol
# ........ ........ ............... .................... ........................................
#
99.88% 0.00% swapper [kernel.vmlinux] [k] secondary_startup_64_no_verify
|

---secondary_startup_64_no_verify
|

|--82.83%--start_secondary

| cpu_startup_entry

| do_idle

| |

| --82.82%--cpuidle_idle_call

| |

| --82.81%--default_idle_call

| |

| --82.79%--__cpuidle_text_start

perf script

perf script provides the functionality of many scripts in the perf software package to assist with performance data analysis and output, including the well-known flame graph.
Run the command directly in the directory where the perf.data file is located to display the collected events and call stacks:
perf script
Output example (excerpt):
swapper 0 [000] 23750.198473: 250000 cpu-clock:pppH:
ffffffffb1373120 __cpuidle_text_start+0x10 (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
ffffffffb1373303 default_idle_call+0x33 (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
ffffffffb0946468 cpuidle_idle_call+0x158 (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
ffffffffb0946536 do_idle+0x76 (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
ffffffffb0946759 cpu_startup_entry+0x19 (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
ffffffffb1363a5a rest_init+0xca (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
ffffffffb2c444c6 arch_call_rest_init+0xa (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
ffffffffb2c44979 start_kernel+0x48b (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
ffffffffb080015a secondary_startup_64_no_verify+0xe5 (/boot/vmlinux-6.6.6-2401.0.1.tl4.x86_64)
To use the flame graph, install the js-d3-flame-graph software package first:
dnf install js-d3-flame-graph
Then run perf script report flamegraph in the directory where perf.data is located to generate the flame graph file flamegraph.html, and open it in a browser on the desktop environment.

Ajuda e Suporte

Esta página foi útil?

comentários