perf是一款Linux系统性能分析工具,可用于分析CPU性能、内存使用、IO性能等方面,它基于内核的事件采样机制,可以对系统进行各种性能分析,包括函数调用耗时、指令执行频率、缓存命中率、IO操作耗时等等。
centos7中默认未安装,请运行如下命令:
yum -y install perf
下面是一些perf命令的使用示例及其显示信息的含义:
perf top:实时监测系统性能,显示最耗费CPU的函数和指令,按照CPU时间从高到低排列。
完毕!
[root@localhost ~]# perf top
Samples: 1K of event 'cpu-clock', 4000 Hz, Event count (approx.): 44445280 lost: 0/0 drop: 0/0
Overhead Shared Object Symbol
35.50% [kernel] [k] _raw_spin_unlock_irqrestore
6.40% libglib-2.0.so.0.5600.1 [.] 0x0000000000049607
5.03% [kernel] [k] mpt_put_msg_frame
4.91% [kernel] [k] __do_softirq
3.36% [kernel] [k] tick_nohz_idle_enter
2.15% [kernel] [k] e1000_xmit_frame
1.28% libelf-0.176.so [.] gelf_getsym
1.26% [kernel] [k] ata_sff_pio_task
1.09% [kernel] [k] _raw_qspin_lock
0.95% libelf-0.176.so [.] gelf_getrela
0.87% perf [.] 0x00000000001be3e4
0.82% libslang.so.2.2.4 [.] SLsmg_write_chars
0.81% [kernel] [k] finish_task_switch
0.80% [kernel] [k] __d_lookup
0.76% [kernel] [k] generic_exec_single
0.61% libglib-2.0.so.0.5600.1 [.] 0x00000000000495e7
0.57% [kernel] [k] queue_delayed_work_on
0.56% [kernel] [k] watchdog
0.54% libvmtools.so.0.0.0 [.] Backdoor_InOut
0.52% perf [.] perf_evsel__parse_sample
0.49% [kernel] [k] sysret_signal
0.49% [kernel] [k] xfsaild
0.49% libc-2.17.so [.] __GI___printf_fp_l
0.49% libglib-2.0.so.0.5600.1 [.] 0x00000000000495d7
0.47% perf [.] dso__find_symbol
0.46% [kernel] [k] ioread8_rep
0.45% libc-2.17.so [.] __times
0.45% libglib-2.0.so.0.5600.1 [.] g_main_context_check
0.45% libmozjs-52.so.0.0.0 [.] 0x00000000004a7578
0.44% libmozjs-52.so.0.0.0 [.] 0x00000000004f05c5
0.43% [kernel] [k] module_put
0.43% [kernel] [k] native_set_pte_at
perf stat:统计指定命令的性能数据,包括CPU周期、指令数、缓存命中率等,还可以指定事件类型进行监测,如cache-references、cache-misses、page-faults等。
Performance counter stats for 'system wide':
767,060.61 msec cpu-clock # 2.000 CPUs utilized
38,056 context-switches # 0.050 K/sec
1,666 cpu-migrations # 0.002 K/sec
33,795 page-faults # 0.044 K/sec
<not supported> cycles
0 stalled-cycles-frontend
<not supported> instructions
<not supported> branches
<not supported> branch-misses
383.535103505 seconds time elapsed
perf record:记录应用程序的性能事件,可以使用perf report分析结果,找出性能瓶颈,同时也可以用于进行CPU调试。perf report:显示perf record命令生成的性能数据报告,包括各种事件的数量、执行时间、调用栈信息等,以帮助分析性能问题。
perf annotate:显示函数源代码及相关指令的注释信息,以帮助定位代码问题。
perf trace:监测系统调用和信号,可以显示系统调用执行时间、返回码以及相关进程的信息等。
perf probe:动态添加探测点,收集函数调用信息,可以通过perf report命令来显示信息。
通过以上命令,可以方便地分析系统性能瓶颈,查找问题的根源,并进行优化。当然,需要根据具体情况选择使用相应的命令,从而达到更好的性能优化效果。