simpleperf 使用记录
近期处理CPU loading过高问题,使用simpleperf生成火焰图的方式进行分析,相较于以前查看record的方式直观了不少,方法记录如下:
1. simpleperf 命令查看
通用开源工具一般都可以通过 -h的方式查看其命令支持情况:
console:/ # simpleperf -h
Usage: simpleperf [common options] subcommand [args_for_subcommand]
common options:
-h/–help Print this help information.
–log Set the minimum severity of logging. Possible severities
include verbose, debug, warning, info, error, fatal.
Default is info.
–version Print version of simpleperf.
subcommands:
debug-unwind Debug/test offline unwinding.
dump dump perf record file
help print help information for simpleperf
kmem collect kernel memory allocation information
list list available event types
record record sampling info in perf.data
report report sampling information in perf.data
report-sample report raw sample information in perf.data
stat gather performance counter information
对于平时分析问题,经常使用的几个命令是:
-
simpleperf stat:获取一段时间内CPU 的执行状态统计
上图即统计10s内系统的状态统计,可以看到这段时间内CPU跑了多少个cycle,多少条指令,分支指令以及分支fail的概率等等 -
simpleperf list:罗列出来系统支持event(与系统强相关,在kernel中打开了哪些配置)
这里可以查看系统支持event的情况,根据系统的不同而不同,根据场景我们可以具体统计某些具体的event情况,比如cache:
在record指令后跟-e参数即可;
-
simpleperf record:录制命令,可以将上述配置的event保存下来,默认生成在/data/perf.data
常用参数:
simpleperf record -g -p 1234 --duration 10 -o /data/perf.data
-
simpleperf report:将上述record的数据生成report报告,以供查看,缺点是不够直观
simpleperf report -g -i /data/perf.data > /data/perf_report.txt
缺点就是不是很直观,但是该有的信息都在里边;
2. 生成火山图
上述所说,获取到的信息不是很直观,这里使用simpleperf源码中支持的script将其构造为易于观看的方式:火山图
-
录制数据:
simpleperf record -g -p 2729 --duration 10 -o /sdcard/perf.data
-
report_sample.py 脚本解析,解析为report格式数据
此脚本在源码中(simpleperf目录下),如果常用可以将其中scripts工具拷贝出来使用:
python report_sample.py > 1.perf
此操作默认解析perf.data 文件,可以添加lib库信息:
此步骤解析后可以看到抓取的地址信息和函数名信息:
-
stackcollapse-perf.pl 脚本处理,将stack信息按照需要格式排列
./stackcollapse-perf.pl 1.perf > 1.fold
这个格式看不出来什么,这边理解是flamegraph脚本所需要的格式
-
flamegraph.pl脚本处理,将栈帧构造为图形
./flamegraph.pl 1.fold > 1.svg
-
使用chrome查看
以典型的分析CPU时间花费到哪个函数的on-cpu火焰图为例来展开。CPU火焰图中的每一个方框是一个函数,方框的长度,代表了它的执行时间,所以越宽的函数,执行越久。火焰图的楼层每高一层,就是更深一级的函数被调用,最顶层的函数,是叶子函数。
具体查看某个进程的情况:
3. 相关工具获取方法
-
simpleperf 执行文件以及相应script
-
解析为图形工具的脚本从github获取:
https://github.com/brendangregg/FlameGraph
-
以上,近期使用这个火焰图分析系统性能问题,感觉效率提升了不少 0^0
4. 解析方式更新
update 于 2021.09.07
有发现源码中有提供另一个解析工具,可以直接将perf.data解析为网页形式,其中包含饼状图、火焰图、表格统计,表达更加直观
4.1 表现形式
-
主界面
-
饼图可以点击详细显示
-
可以查看调用堆栈和占比(火焰图)
-
以表格形式查看func占用比例
这样来看的话,这个脚本信息要详细很多,也要直观的多;
4.2 使用方法
图示已经很吸引人了,接下来来看如何使用,其实非常简单,上述2中有提到从源码中获取到相应工具,在script中有个Python脚本,值得一用:
python report_html.py -i perf.data -o 2.html
操作如此简单只需要一步就可以生成上述的直观的文件了。。。