1

gperftools的使用

 2 years ago
source link: https://charon-cheung.github.io/2022/08/01/%E5%B8%B8%E7%94%A8%E5%B7%A5%E5%85%B7/gperftools%E7%9A%84%E4%BD%BF%E7%94%A8/#%E4%BD%BF%E7%94%A8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
gperftools的使用
2022-08-01|常用工具|
Word count: 343|Reading time: 1 min

下载和安装

安装必备库: sudo apt-get install -y libunwind8-dev kcachegrind graphviz gv

github上找到源码,下载并解压

# 64位机器需要先安装
sudo apt-get install libunwind8-dev
./configure
make -j8
sudo make install
sudo ldconfig

CMakeListstarget_link_libraries部分加入-lprofiler, 以及LINK_DIRECTORIES(/usr/local/lib)

代码如下使用,加在main函数首尾

#include "/usr/local/include/gperftools/profiler.h"
// ....
int main(int argc, const char* argv[])
{
ProfilerStart("test_capture.prof");
.....
ProfilerStop();
}

再用gperftools提供的性能分析工具pprof生成直观可读的文件形式

# 以下都需要输入文件的完整路径
pprof ./test_capture test_capture.prof --web
# 转为 callgrind 文件,用kcachegrind打开,可读性更好
pprof --callgrind ./myprogram xxx.prof > xxx.callgrind
kcachegrind xxx.callgrind

# 使用较少
pprof ./test_capture test_capture.prof --pdf > prof.pdf
./test_capture test_capture.prof --text

多数使用web文件,大字体显示的节点就是CPU使用率高的函数。kcachegrind的功能更强大,可以详细看到所有函数占用CPU和调用者等信息。

运行时如果出现:No nodes to print。程序逻辑使用CPU次数太少,无法分析,也就是程序太简单了。

CPU profiler是基于采样工作的。所以采样次数影响着性能报告的准确性。如果采样次数过少,则你会发现同样的程序同样的数据,每次输出的性能报告中的热点都不一样。

参考:kcachegrind介绍


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK