https://blog.csdn.net/10km/article/details/83820080
https://blog.51cto.com/wulingdong/2043898
https://www.jianshu.com/p/1611205f2c01
需要的组件:
编译安装gperftools
从https://github.com/gperftools/gperftools/releases下载最新版本的gperftools源码包
解压到/usr/local/src目录
cd 解压源码目录
./autogen.sh ./configure make -j6 make install
可能遇到的问题:gperftools Error: substr outside of string at /usr/local/bin/pprof line XXXX
可视化:
graphviz: https://graphviz.gitlab.io/_pages/Download/Download_source.html
webdot: https://graphviz.gitlab.io/_pages/Download/Download_source.html
转PDF: ghostscript:https://www.ghostscript.com/download/gsdnld.html
示例代码:
#include <gperftools/profiler.h> #include <stdlib.h> void f() { int i; for (i=0; i<1024*1024; ++i) { char *p = (char*)malloc(1024*1024*120); free(p); } } void fun1() { f(); } void fun2() { f(); } int main() { ProfilerStart("test.prof");//开启性能分析 fun1(); fun2(); ProfilerStop();//停止性能分析 return 0; }
编译:
g++ test_pprof1.cpp -o test -ltcmalloc -lprofiler
执行:
./test
生成了test.prof文件
查看性能报告:text版
pprof ./test test.prof --text
可见如下结果:
生成性能报告:PDF
pprof ./test test.prof --pdf > test.pdf