pref是一款内置的性能分析工具,一般可用于性能瓶颈分析
比如内核消耗cpu高,具体是哪块代码,某个内核函数调用频率是多少等等
perf常用命令:
perf listperf statperf topperf record/report
perf list
查看当前perf可用的事件
cpu-cycles:统计cpu周期数,cpu周期:指一条指令的操作时间。instructions: 机器指令数目cache-references: cache命中次数cache-misses: cache失效次数branch-instructions: 分支预测成功次数branch-misses: 分支预测失败次数alignment-faults: 统计内存对齐错误发生的次数, 当访问的非对齐的内存地址时,内核会进行处理,已保存不会发生问题,但会降低性能context-switches: 上下文切换次数,cpu-clock: cpu clock的统计,每个cpu都有一个高精度定时器task-clock :cpu clock中有task运行的统计cpu-migrations:进程运行过程中从一个cpu迁移到另一cpu的次数page-faults: 页错误的统计major-faults:页错误,内存页已经被swap到硬盘上,需要I/O换回minor-faults :页错误,内存页在物理内存中,只是没有和逻辑页进行映射
perf stat
分析程序的整体消耗情况
$ perf stat lsPerformance counter stats for 'ls':0.68 msec task-clock # 0.545 CPUs utilized6 context-switches # 0.009 M/sec0 cpu-migrations # 0.000 K/sec99 page-faults # 0.145 M/sec<not supported> cycles<not supported> instructions<not supported> branches<not supported> branch-misses0.001249473 seconds time elapsed0.000000000 seconds user0.000884000 seconds sys
task-clock (msec): cpu处理task所消耗的时间,单位ms0.545 CPUs utilized:表示cpu使用率为54.5%instructions:执行的指令条数stalled-cycles-frontend和stalled-cycles-backend:表示CPU停滞统计
perf top
实时的查看当前系统各个进程的各个函数的性能计数分析
-e:统计特定的event-g:得到函数的调用关系图-p:分析特定的进程-d:刷新周期,默认2sperf top -g -p <pid>
perf record
perf report
对已运行的进程进行性能分析,并将分析结果输出到perf.data中,通过perf report进行分析
perf record -g -p <pid>perf report
