google开源的C++性能分析工具 - gperftools

jopen 9年前
gperftools是Google提供的一套工具,其中的一个功能是CPU profiler,用于分析程序性能,找到程序的性能瓶颈。
安装
64位操作系统需要安装libunwind,官方推荐版本是libunwind-0.99-beta
安装过程:./configure [--disable-shared] &&make && make install
Graphviz是一个由AT&T实验室启动的开源工具包,用于绘制DOT语言脚本描述的图形,gperftools依靠此工具生成图形分析结果。
安装命令:yum install graphviz
1.编译libunwind库
因为使用的是X86_64的Linux系统,因此需要安装libunwind库。
安装方法很简单,常见的configure,make,make install的套路。
tarxvzf libunwind-0.99-beta.tar.gz
cd libunwind-0.99-beta
./configure
make
makeinstall
因为默认的libunwind安装在/usr/local/lib目录下,需要将这个目录添加到系统动态库缓存中。
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
libunwind的最新版本是1.0.1,那为什么 不选择最新版本呢?google perftools的INSTALL文件中给了说明。版本低于0.99-beta的libunwind与preftools一起工作可能不正常,但是高于 0.99-beta的版本中可能包含一些与perftools不兼容的代码(因为libunwind会调用malloc,可能会导致死锁)。 libunwind在X86_64平台上和perftools有不少问题,不过不会影响核心的tcmalloc库,但是会影响perftools中的工 具,例如cpu-profiler,heap-checker,heap-profiler。
2.编译google-perftools
因为我们只需要tcmalloc功能,因此不编译google-perftools中的其他工具。
tarxvzf google-perftools-1.9.1.tar.gz
cd google-perftools-1.9.1
./configure --disable-cpu-profiler --disable-heap-profiler --disable-heap-checker --enable-minimal--disable-dependency-tracking
make
makeinstall
/sbin/ldconfig
用法
1.目标程序中引入头文件<google/profiler.h>,链接libprofiler库,64位操作系统同时链接libunwind库,在需要分析代码的起点和终点调用ProfilerStart()函数和ProfilerStop()函数
2.编译链接,运行程序
分析输出
pprof脚本用于分析profile文件并输出结果,包括文本和图形两种输出风格。
例如:demo是目标程序,my.prof是profile文件
生成文本风格结果:pprof --text ./demo my.prof >profile.txt
生成图形风格结果:pprof --pdf ./demo my.prof > profile.pdf
对于一个函数的CPU使用时间分析,分为两个部分:
1.整个函数消耗的CPU时间,包括函数内部其他函数调用所消耗的CPU时间
2.不包含内部其他函数调用所消耗的CPU时间(内联函数除外)
关于文本风格输出结果
  
序号
  
说明
1
分析样本数量(不包含其他函数调用)
2
分析样本百分比(不包含其他函数调用)
3
目前为止的分析样本百分比(不包含其他函数调用)
4
分析样本数量(包含其他函数调用)
5
分析样本百分比(包含其他函数调用)
6
函数名
关于图形风格输出结果
1.节点
每个节点代表一个函数,节点数据格式:
  
Class Name
  
Method Name
  
local (percentage)
  
of cumulative (percentage)
  
local时间是函数直接执行的指令所消耗的CPU时间(包括内联函数);性能分析通过抽样方法完成,默认是1秒100个样本,一个样本是10毫秒,即时间单位是10毫秒;
cumulative时间是local时间与其他函数调用的总和;
如果cumulative时间与local时间相同,则不打印cumulative时间项。
2.有向边
调用者指向被调用者,有向边上的时间表示被调用者所消耗的CPU时间
示例
代码如下,可以看出,CPU消耗集中在func1()func2()两个函数,func2()消耗时间约为func1()的两倍。
#include <google/profiler.h>
#include <iostream>
using namespace std;
void func1() {
   int i = 0;
   while (i < 100000) {
       ++i;
   }  
}
void func2() {
   int i = 0;
   while (i < 200000) {
       ++i;
   }  
}
void func3() {
   for (int i = 0; i < 1000; ++i) {
       func1();
       func2();
   }  
}
int main(){
   ProfilerStart("my.prof"); // 指定所生成的profile文件名
   func3();
   ProfilerStop(); // 结束profiling
   return 0;
}
然后编译链接运行,使用pprof生成分析结果
g++-o demo demo.cpp -lprofiler -lunwind
pprof--text ./demo my.prof > output.txt
pprof--pdf ./demo my.prof > output.pdf
查看分析结果,程序是122个时间样本,其中,func1()是40个时间样本,约为400毫秒;func2()是82个时间样本,约为820毫秒。
  
Total: 122 samples
  
      82   67.2%  67.2%       82  67.2% func2
  
      40   32.8% 100.0%       40  32.8% func1
  
       0    0.0% 100.0%      122 100.0% __libc_start_main
  
       0    0.0% 100.0%      122 100.0% _start
  
       0    0.0% 100.0%      122 100.0% func3
  
       0    0.0% 100.0%      122 100.0% main