🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 概述 它不但可以分析指定应用程序的性能问题 (per thread),也可以用来分析内核的性能问题,当然也可以同时分析应用代码和内核,从而全面理解应用程序中的性能瓶颈 ## 原理 tracepoints是散落在内核源码中的一些hook,它们可以在特定的代码被执行到时触发,这一特定可以被各种trace/debug工具所使用。 ## 语法 ### perf list 列出性能事件 性能事件是指在处理器或操作系统中发生的,可能影响到程序性能的硬件事件或软件事件 ### perf top 实时显示系统/进程的性能统计信息 ``` perf top [options] options: -e:指定性能事件 -a:显示在所有CPU上的性能统计信息 -C:显示在指定CPU上的性能统计信息 -p:指定进程PID -t:指定线程TID -K:隐藏内核统计信息 -U:隐藏用户空间的统计信息 -s:指定待解析的符号信息 -g: 得到函数的调用关系图 -d <n>:界面的刷新周期,默认为2s,因为perf top默认每2s从mmap的内存区域读取一次性能数据。 --comms: 指定进程名称 ``` ### perf stat 执行一个命令并收集其运行过程中的各个数据,它可以提供一个程序运行情况的总体概览。 <details> <summary>输出格式</summary> ``` user@localhost:~$ perf stat hostname localhost Performance counter stats for 'hostname': 0.313464 task-clock (msec) # 0.481 CPUs utilized 2 context-switches # 0.006 M/sec 0 cpu-migrations # 0.000 K/sec 153 page-faults # 0.488 M/sec 896,723 cycles # 2.861 GHz 620,709 instructions # 0.69 insn per cycle 121,143 branches # 386.465 M/sec 6,247 branch-misses # 5.16% of all branches 0.000651441 seconds time elapsed task-clock-msecs:cpu 使用率 context-switches:进程切换次数 page-faults:发生缺页的次数 cpu-migrations:表示进程运行过程中发生了多少次CPU迁移,即被调度器从一个CPU转移到另外一个CPU上运行 cycles:处理器时钟,一条机器指令可能需要多个cycles instructions: 机器指令数目 branches:遇到的分支指令数 branch-misses是预测错误的分支指令数 ``` </details> <br/> ``` perf stat [options] options: -e:选择性能事件 -i:禁止子任务继承父任务的性能计数器。 -r:重复执行 n 次目标程序,并给出性能指标在n 次执行中的变化范围。 -n:仅输出目标程序的执行时间,而不开启任何性能计数器。 -a:指定全部cpu -C:指定某个cpu -A:将给出每个处理器上相应的信息。 -p:指定待分析的进程id -t:指定待分析的线程id ``` ## perf record 记录一段时间内系统/进程的性能时间 ``` perf record[options] options: -e:选择性能事件 -p:待分析进程的id -t:待分析线程的id -a:分析整个系统的性能 -C:只采集指定CPU数据 -c:事件的采样周期 -o:指定输出文件,默认为perf.data -A:以append的方式写输出文件 -f:以OverWrite的方式写输出文件 -g:记录函数间的调用关系 ``` ## perf report 读取perf record生成的数据文件,并显示分析数据 ``` perf report[options] options: -i:输入的数据文件 -v:显示每个符号的地址 -d <dos>:只显示指定dos的符号 -C:只显示指定comm的信息(Comm. 触发事件的进程名) -S:只考虑指定符号 -U:只显示已解析的符号 -g[type,min,order]:显示调用关系,具体等同于perf top命令中的-g -c:只显示指定cpu采样信息 -M:以指定汇编指令风格显示 -g:记录函数间的调用关系 –source:以汇编和source的形式进行显示 -p<regex>:用指定正则表达式过滤调用函数 ``` ## 实例 ### 整个系统的性能情况 ``` perf top -a ``` ### 用户服务的性能情况 ``` perf top -K ``` ### 查看某个服务的性能情况 ``` //方式一 > ps -ef | grep UpAntbizServer > perf top -p 6704 方式二 > perf top --comms UpAntbizServer ``` 输出内容 ``` 0.23% UpAntbizServer [.] runtime.netpoll 0.23% [kernel] [k] tcp_v4_rcv 0.22% UpAntbizServer [.] fmt.(*pp).doPrintf 0.21% UpAntbizServer [.] github.com/astaxie/beego.(*ControllerRegister).ServeHTTP 0.21% [kernel] [k] clear_page 0.21% [kernel] [k] sock_has_perm 0.21% [kernel] [k] enqueue_to_backlog 0.21% UpAntbizServer [.] runtime.readvarint ``` ### 查看某个服务的函数调用关系图[常用] ``` + 7.09% 0.00% UpAntbizServer [.] runtime.call32 + 7.08% 0.00% UpAntbizServer [.] antbiz/controllers.(*ListController).ApiFunc - 3.64% 0.00% UpAntbizServer [.] antbiz/controllers.GetRoleIdByUserId - antbiz/controllers.GetRoleIdByUserId - 1.99% antbiz/model.GetRoleIdIdByUserId - 1.74% xorm.io/xorm.(*Session).Fin + 1.73% xorm.io/xorm.(*Session).find ``` ### record 并 report 性能数据 在当前目录生成 data.perf 数据,并通过report 进行分析 ``` > perf record -a -g -p 23315 -p参数为pid > perf report [-i perf.data ] ``` ### 查看CPU过高的堆栈信息 ``` > perf top -e cycles -g -p 5745 Samples: 89K of event 'cycles:u', 4000 Hz, Event count (approx.): 38012908527 Children Self Shared Object Symbol - 55.56% 4.26% BigAnt5 [.] _ZN26StyleDocumentLayoutPrivate10layoutFlowEN10QTextFrame8iteratorEP17QTextLayoutStructii6QFixed ◆ + 46.39% _ZN26StyleDocumentLayoutPrivate10layoutFlowEN10QTextFrame8iteratorEP17QTextLayoutStructii6QFixed ▒ - 13.03% 3.26% BigAnt5 [.] _ZN26StyleDocumentLayoutPrivate11layoutBlockERK10QTextBlockiRK16QTextBlockFormatP17QTextLayoutStru▒ + 6.85% _ZN26StyleDocumentLayoutPrivate11layoutBlockERK10QTextBlockiRK16QTextBlockFormatP17QTextLayoutStructiiPS4_ ▒ + 3.23% _ZN26StyleDocumentLayoutPrivate10layoutFlowEN10QTextFrame8iteratorEP17QTextLayoutStructii6QFixed ▒ + 6.88% 6.88% libQt5Gui.so.5 [.] _ZN10QTextFrame8iteratorppEv ▒ + 6.02% 6.02% libQt5Gui.so.5 [.] _ZNK11QTextFormat11intPropertyEi ▒ + 5.33% 5.33% libQt5Gui.so.5 [.] _ZNK10QTextFrame13firstPositionEv ▒ + 4.51% 4.51% libQt5Gui.so.5 [.] _ZNK10QTextBlock8positionEv ▒ + 3.79% 3.79% libQt5Gui.so.5 [.] _ZNK10QTextBlock13textDirectionEv ▒ + 3.76% 0.28% BigAnt5 [.] _ZL10markFramesP10QTextFrameiii ▒ + 2.98% 2.98% libQt5Gui.so.5 [.] _ZNK20QTextDocumentPrivate14objectForIndexEi ▒ + 2.27% 2.27% libQt5Core.so.5 [.] _ZNK8QVariant8userTypeEv ▒ + 2.22% 2.22% libQt5Gui.so.5 [.] _ZNK11QTextObject6formatEv ▒ + 2.13% 1.89% BigAnt5 [.] _ZN6QFixed9fromFixedEi ▒ + 1.95% 1.95% libQt5Gui.so.5 [.] 0x00000000002f32a8 ▒ + 1.94% 1.40% BigAnt5 [.] _ZNK5QListIP10QTextFrameE2atEi ▒ + 1.87% 0.23% BigAnt5 [.] _ZN26StyleDocumentLayoutPrivate11layoutFrameEP10QTextFrameii6QFixedS2_S2_ ▒ + 1.79% 1.79% libQt5Gui.so.5 [.] _ZNK10QTextFrame12lastPositionEv ▒ + 1.77% 1.77% libQt5Gui.so.5 [.] _ZNK21QTextFormatCollection6formatEi ```