性能测试工具 CPU profiler gperftools 的使用心得
编译安装 gperftools
gperftools 是一个工具包,CPU profiler 是其中的工具之一,用于程序性能分析。要使用 CPU profiler 就要先编译安装 gperftools,下面是简单的编译安装过程.
# 从 github 下载 gperftools 源码并解压 wget https://github.com/gperftools/gperftools/archive/gperftools-2.7.tar.gz tar xvf gperftools-2.7.tar.gz # 解压文件夹改名 mv gperftools-gperftools-2.7 gperftools-2.7 cd gperftools-2.7 ./autogen.sh ./configure make -j8 # 安装到系统文件夹 sudo make install
注意:在 64 位操作系统下需要 libunwind 支持,如果没有安装 libunwind,还要先编译安装 libunwind。
使用 cpu profiler
根据 CPU profiler 的官方说明( https://gperftools.github.io/gperftools/cpuprofile.html ),对一个程序进行测试是很简单的。
只要加上-lprofiler 对程序重新编译一次,再执行程序就可以了,如下是官网给出简单步骤:
# 加上-lprofiler 编译自己的程序 gcc [...] -o myprogram -lprofiler # 设置环境变量 CPUPROFILER 指定生成的性能报告文件,并执行自己的程序 CPUPROFILE=/tmp/profile ./myprogram
这个简单办法,不需要修改自己的程序,还可以在程序开始和结束位置分别加入 ProfilerStart,ProfilerStop
函数,示例如下:
#include <gperftools/profiler.h> .... int main(int argc, const char* argv[]){ ProfilerStart("test_capture.prof"); ..... ProfilerStop(); }
因为在代码中加入了 ProfilerStart 函数指定了生成性能报告的文件名,所以在执行程序时就不一定需要 CPUPROFILE=/tmp/profile 再指定性能报告文件名。
程序运行结束会在当前文件夹生成名为 test_capture.prof 的性能报告。
性能报告
有了性能报告 ,就可以用 gperftools 提供的性能分析工具 pprof 生成直观可读的文件形式。
Browser
# 生成性能报告(层次调用节点有向图)输出到 web 浏览器显示 # 第一个参数为你的可执行程序或动态库文件名,第二个参数为上一步生成的性能报告文件 pprof ./test_capture test_capture.prof --web
下面的图形输出报告中,大字体显示的节点就是 CPU 使用率的’热点’,一目了然。
节点信息说明:图形风格的性能报告由节点和有向边组成, 每个节点代表一个函数,节点数据格式:
字段名 | 描述 |
Class Name | 类名,非类成员函数此项为空 |
Method Name | 函数名 |
local (percentage) | 当前函数直接执行的指令所消耗的 CPU 时间(包括内联函数)(百分比) |
of cumulative (percentage) | 当前函数的 local 时间及其调用的函数的 local 时间总和(百分比),如果与 local 相同,则不显示 |
节点数据格式
有向边:调用者指向被调用者,有向边上的时间表示被调用者所消耗的 CPU 时间
性能分析通过抽样方法完成,默认是 1 秒 100 个样本,一个样本是 10 毫秒,即时间单位是 10 毫秒;可以通过环境变量 CPUPROFILE_FREQUENCY 设置采样频率。
更多详细说明参见 CPU profiler Node Information 一节
./test_capture test_capture.prof --text
# 生成 pdf 格式的性能报告(层次调用节点有向图) pprof ./test_capture test_capture.prof --pdf > prof.pdf
Text
# 生成文本格式的性能报告输出到控制台 ./test_capture test_capture.prof --text
输出内容如下,在 CPU 使用率降序输出所有的函数名:
Using local file ./test_capture. Using local file test_capture.prof. Total: 20 samples 4 20.0% 20.0% 4 20.0% CanFaceCcd 4 20.0% 40.0% 4 20.0% cimg_library::CImg::RGBtoYUV 3 15.0% 55.0% 5 25.0% cimg_library::CImg::CImg 2 10.0% 65.0% 2 10.0% StepImageHalfNormal 2 10.0% 75.0% 2 10.0% __nss_passwd_lookup 2 10.0% 85.0% 2 10.0% decode_mcu 1 5.0% 90.0% 1 5.0% GetIntegralOfIma 1 5.0% 95.0% 1 5.0% RunCascade
每行包含 6 列数据,依次为:
- 分析样本数量(不包含其他函数调用)
- 分析样本百分比(不包含其他函数调用)
- 目前为止的分析样本百分比(不包含其他函数调用)
- 分析样本数量(包含其他函数调用)
- 分析样本百分比(包含其他函数调用)
- 函数名
More Output Type
pprof 还支持更多输出格式,执行 pprof --help
也可查看所支持的输出格式
Output type: --text Generate text report --stacks Generate stack traces similar to the heap profiler (requires --text) --callgrind Generate callgrind format to stdout --gv Generate Postscript and display --evince Generate PDF and display --web Generate SVG and display --list=<regexp> Generate source listing of matching routines --disasm=<regexp> Generate disassembly of matching routines --symbols Print demangled symbol names found at given addresses --dot Generate DOT file to stdout --ps Generate Postcript to stdout --pdf Generate PDF to stdout --svg Generate SVG to stdout --gif Generate GIF to stdout --raw Generate symbolized pprof data (useful with remote fetch)
静态连接 profiler
有的时候我们需要静态连接 profiler
库(比如在嵌入式系统下做性能分析). 根据 gperftools 的官方说明: README ,静态连接 profiler 不能使用 profiler.a
静态库,要用 libtcmalloc_and_profiler.a
替代。
EVERYTHING IN ONE ----------------- If you want the CPU profiler, heap profiler, and heap leak-checker to all be available for your application, you can do: gcc -o myapp ... -lprofiler -ltcmalloc However, if you have a reason to use the static versions of the library, this two-library linking won't work: gcc -o myapp ... /usr/lib/libprofiler.a /usr/lib/libtcmalloc.a # errors! Instead, use the special libtcmalloc_and_profiler library, which we make for just this purpose: gcc -o myapp ... /usr/lib/libtcmalloc_and_profiler.a
关于采样次数
CPU profiler 是基于采样工作的。所以采样次数影响着性能报告的准确性。
如果采样次数过少,则你会发现同样的程序同样的数据,每次输出的性能报告中的热点都不一样。
所以在我的实际应用中,通过循环运行测试程序函数,大幅度提高采样次数。这样才能获得一个稳定的准确的性能报告。
参考资料
https://github.com/gperftools/gperftools/blob/master/README
https://gperftools.github.io/gperftools/cpuprofile.html
https://github.com/gperftools/gperftools/wiki
https://blog.csdn.net/okiwilldoit/article/details/50884670
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: bisect — 数组二分查找算法
下一篇: 谈谈自己对于 AOP 的了解
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论