google perftools分析程序性能
2013-05-28 19:39
681 查看
Google perftools
1、功能简介
它的主要功能就是通过采样的方式,给程序中cpu的使用情况进行“画像”,通过它所输出的结果,我们可以对程序中各个函数(得到函数之间的调用关系)耗时情况一目了然。在对程序做性能优化的时候,这个是很重要的,先把最耗时的若干个操作优化好,程序的整体性能提升应该十分明显,这也是做性能优化的一个最为基本的原则—先优化最耗时的。
2、安装
1、下载gperftools
Wget https://code.google.com/p/gperftools/downloads/detail?name=gperftools-2.0.tar.gz
2、tar –xzf gperftools-2.0.tar.gz
3、cd gperftools-2.0
4、./configure --prefix=/usr/local –enable-frame-pointers
5、make && make install
ps:编译时打开了 –enable-frame-pointers ,这要求被测试的程序在编译时要加上gcc编译选项,否则某些多线程程序可能会 core:
CCFLAGS=-fno-omit-frame-pointer
ps:perftools对2.4内核的多线程支持不是很好,只能分析主线程,但是2.6内核解决了这个问题。
安装图形化分析工具kcachegrind:
kcachegrind用来分析产生的profiling文件,linux环境下使用。
kcachegrind install:sudo apt-get install kcachegrind
3、使用
方法有三种:
1、直接调用提供的api:这种方式比较适用于对于程序的某个局部来做分析的情况,直接在要做分析的局部调用相关的api即可。
方式:调用函数:
2、链接静态库:这种方式是最为常用的方式,后面会有详细的介绍。
方式:在代码link过程中添加参数
–lprofiler
For example:gcc […] -o helloworld
–lprofiler
运行程序:env CPUPROFILE=./helloworld.prof ./helloworld
指定要profile的程序为helloworld,并且指定产生的分析结果文件的路径为./helloworld.prof
3、链接动态库:这种方式和静态库的方式差不多,但通常不推荐使用,除非使用者不想额外链一个静态库(因为链接静态库会增大binary的大小)的情况,可以考虑使用这种方式。
方式:运行时使用LD_PRELOAD,e.g.
4、 查看收集数据结果
查看profile结果:pprof工具,它是一个perl的脚本,通过这个工具,可以将google-perftool的输出结果分析得更为直观,输出为图片、pdf等格式。
Ps:在使用pprof之前需要先安装运行per15,如果要进行图标输出则需要安装dot,如果需要--gv模式的输出则需要安装gv。
调用pprof分析数据文件:
% pprof /bin/ls ls.prof
Enters
"interactive" mode
% pprof --text
/bin/ls ls.prof
Outputs one line per
procedure
% pprof --gv
/bin/ls ls.prof
Displays annotated
call-graph via 'gv'
% pprof --gv
--focus=Mutex /bin/ls ls.prof
Restricts to code paths
including a .*Mutex.* entry
% pprof --gv
--focus=Mutex --ignore=string /bin/ls ls.prof
Code paths including Mutex but
not string
% pprof
--list=getdir /bin/ls ls.prof
(Per-line) annotated
source listing for getdir()
% pprof
--disasm=getdir /bin/ls ls.prof
(Per-PC) annotated
disassembly for getdir()
% pprof --text
localhost:1234
Outputs one line per
procedure for localhost:1234
% pprof
--callgrind /bin/ls ls.prof
Outputs the call
information in callgrind format
分析callgrind的输出:
使用kcachegrind工具来对.callgrind输出进行分析
e.g. %
pprof --callgrind /bin/ls ls.prof > ls.callgrind
% kcachegrind ls.callgrind
4、举例
事例一:cpu_profiler_example.cpp,在代码中插入标签,可以针对某个函数进行特定的profile
代码如下:
![](http://images.cnitblog.com/blog/352788/201305/28193152-44709536290a4483b86d0954f64790e1.jpg)
关注两个函数:
Makefile:
![](http://images.cnitblog.com/blog/352788/201305/28193258-69cd7c9bf65c46e6b8ba5c58c6fbf601.jpg)
-L 动态链接库地址,但是有可能程序执行的时候,找不到动态链接库,所以得
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:"/home/work/alex/tools/gperftools/lib"
1)执行./cpu_profile_example
![](http://images.cnitblog.com/blog/352788/201305/28193352-fc30476d2e3b4ccb8dd9760a2e163ae5.jpg)
生成一个性能数据文件:
cpu_profiler_example_29502.prof
Ps:当然指定性能数据文件生成的路径和文件名:
CPUPROFILE=/tmp/profile
./myprogram
将在/tmp目录下产生profile性能数据文件
2)分析性能数据
pprof -text cpu_profiler_example
cpu_profiler_example_3875.prof
![](http://images.cnitblog.com/blog/352788/201305/28193430-ab556cdaa14c44688d93f35c52984391.jpg)
Text输出结果分析:
14 2.1% 17.2% 58 8.7% std::_Rb_tree::find
含义如下:
14:find函数花费了14个profiling samples
2.1%:find函数花费的profiling samples占总的profiling samples的比例
17.2%:到find函数为止,已经运行的函数占总的profiling samples的比例
58:find函数加上find函数里的被调用者总共花费的profiling samples
8.7%:find函数加上find函数里的被调用者总共花费的profiling samples占总的profiling samples的比例
std::_Rb_tree::find:表示profile的函数
ps: 100 samples a second,所以得出的结果除以100,得秒单位
Ldd可以查看一个程序要链接那些动态库:
![](http://images.cnitblog.com/blog/352788/201305/28193506-862f1e91082e444488d41d620774eab1.jpg)
事例二:cpu_profiler_example.cpp,不需要在代码里添加任何标签,将profile所有的函数。
代码如下:
![](http://images.cnitblog.com/blog/352788/201305/28193535-dcc503beab114079aaf9b2d5eee1095d.jpg)
Makefile:
![](http://images.cnitblog.com/blog/352788/201305/28193613-7a0d8b0f5ec34d4c978abcb5ba6e837a.jpg)
1)执行程序,生成性能数据文件
CPUPROFILE=/tmp/profile ./cpu_profiler_example
2)分析数据文件
1)pprof -text cpu_profiler_example profile
![](http://images.cnitblog.com/blog/352788/201305/28193642-548894144a7643abb389c20255e56912.jpg)
2)命令行交互模式
![](http://images.cnitblog.com/blog/352788/201305/28193701-32075a297ef244adb11208db08012343.jpg)
事例三:由于我们的程序有可能是服务程序,而服务程序不会自动执行完退出,如果以ctrl+c退出也不是正常的exit(0)的方式退出,而这会导致我们在profile的时候,收集到的数据不全甚至是空的,采用如下解决办法:
将ProfilerStart和ProfilerStop这2个函数封装到两个信号处理函数中,给服务程序发信号SIGUSR1,就开始profile,给服务程序发信号SIGUSR2,就停止profile。这样我们可以随时对程序进行profiling,并获得数据。
5、心得
最后,补充一点,要用google-perftool来分析程序,必须保证程序能正常退出。
采用kcachegrind查看函数之间依赖,并分析程序性能
![](http://images.cnitblog.com/blog/352788/201305/28193831-135ef8597dd0472ca80965dbc86536a1.jpg)
1、功能简介
它的主要功能就是通过采样的方式,给程序中cpu的使用情况进行“画像”,通过它所输出的结果,我们可以对程序中各个函数(得到函数之间的调用关系)耗时情况一目了然。在对程序做性能优化的时候,这个是很重要的,先把最耗时的若干个操作优化好,程序的整体性能提升应该十分明显,这也是做性能优化的一个最为基本的原则—先优化最耗时的。
2、安装
1、下载gperftools
Wget https://code.google.com/p/gperftools/downloads/detail?name=gperftools-2.0.tar.gz
2、tar –xzf gperftools-2.0.tar.gz
3、cd gperftools-2.0
4、./configure --prefix=/usr/local –enable-frame-pointers
5、make && make install
ps:编译时打开了 –enable-frame-pointers ,这要求被测试的程序在编译时要加上gcc编译选项,否则某些多线程程序可能会 core:
CCFLAGS=-fno-omit-frame-pointer
ps:perftools对2.4内核的多线程支持不是很好,只能分析主线程,但是2.6内核解决了这个问题。
安装图形化分析工具kcachegrind:
kcachegrind用来分析产生的profiling文件,linux环境下使用。
kcachegrind install:sudo apt-get install kcachegrind
3、使用
方法有三种:
1、直接调用提供的api:这种方式比较适用于对于程序的某个局部来做分析的情况,直接在要做分析的局部调用相关的api即可。
方式:调用函数:
ProfilerStart()and
ProfilerStop()
2、链接静态库:这种方式是最为常用的方式,后面会有详细的介绍。
方式:在代码link过程中添加参数
–lprofiler
For example:gcc […] -o helloworld
–lprofiler
运行程序:env CPUPROFILE=./helloworld.prof ./helloworld
指定要profile的程序为helloworld,并且指定产生的分析结果文件的路径为./helloworld.prof
3、链接动态库:这种方式和静态库的方式差不多,但通常不推荐使用,除非使用者不想额外链一个静态库(因为链接静态库会增大binary的大小)的情况,可以考虑使用这种方式。
方式:运行时使用LD_PRELOAD,e.g.
% env LD_PRELOAD="/usr/lib/libprofiler.so" <binary>
(不推荐这种方式)。
Ps
:env是linux下插入环境变量的shell命令
4、 查看收集数据结果
查看profile结果:pprof工具,它是一个perl的脚本,通过这个工具,可以将google-perftool的输出结果分析得更为直观,输出为图片、pdf等格式。
Ps:在使用pprof之前需要先安装运行per15,如果要进行图标输出则需要安装dot,如果需要--gv模式的输出则需要安装gv。
调用pprof分析数据文件:
% pprof /bin/ls ls.prof
Enters
"interactive" mode
% pprof --text
/bin/ls ls.prof
Outputs one line per
procedure
% pprof --gv
/bin/ls ls.prof
Displays annotated
call-graph via 'gv'
% pprof --gv
--focus=Mutex /bin/ls ls.prof
Restricts to code paths
including a .*Mutex.* entry
% pprof --gv
--focus=Mutex --ignore=string /bin/ls ls.prof
Code paths including Mutex but
not string
% pprof
--list=getdir /bin/ls ls.prof
(Per-line) annotated
source listing for getdir()
% pprof
--disasm=getdir /bin/ls ls.prof
(Per-PC) annotated
disassembly for getdir()
% pprof --text
localhost:1234
Outputs one line per
procedure for localhost:1234
% pprof
--callgrind /bin/ls ls.prof
Outputs the call
information in callgrind format
分析callgrind的输出:
使用kcachegrind工具来对.callgrind输出进行分析
e.g. %
pprof --callgrind /bin/ls ls.prof > ls.callgrind
% kcachegrind ls.callgrind
4、举例
事例一:cpu_profiler_example.cpp,在代码中插入标签,可以针对某个函数进行特定的profile
代码如下:
![](http://images.cnitblog.com/blog/352788/201305/28193152-44709536290a4483b86d0954f64790e1.jpg)
关注两个函数:
ProfilerStart()and
ProfilerStop()
Makefile:
![](http://images.cnitblog.com/blog/352788/201305/28193258-69cd7c9bf65c46e6b8ba5c58c6fbf601.jpg)
-L 动态链接库地址,但是有可能程序执行的时候,找不到动态链接库,所以得
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:"/home/work/alex/tools/gperftools/lib"
1)执行./cpu_profile_example
![](http://images.cnitblog.com/blog/352788/201305/28193352-fc30476d2e3b4ccb8dd9760a2e163ae5.jpg)
生成一个性能数据文件:
cpu_profiler_example_29502.prof
Ps:当然指定性能数据文件生成的路径和文件名:
CPUPROFILE=/tmp/profile
./myprogram
将在/tmp目录下产生profile性能数据文件
2)分析性能数据
pprof -text cpu_profiler_example
cpu_profiler_example_3875.prof
![](http://images.cnitblog.com/blog/352788/201305/28193430-ab556cdaa14c44688d93f35c52984391.jpg)
Text输出结果分析:
14 2.1% 17.2% 58 8.7% std::_Rb_tree::find
含义如下:
14:find函数花费了14个profiling samples
2.1%:find函数花费的profiling samples占总的profiling samples的比例
17.2%:到find函数为止,已经运行的函数占总的profiling samples的比例
58:find函数加上find函数里的被调用者总共花费的profiling samples
8.7%:find函数加上find函数里的被调用者总共花费的profiling samples占总的profiling samples的比例
std::_Rb_tree::find:表示profile的函数
ps: 100 samples a second,所以得出的结果除以100,得秒单位
Ldd可以查看一个程序要链接那些动态库:
![](http://images.cnitblog.com/blog/352788/201305/28193506-862f1e91082e444488d41d620774eab1.jpg)
事例二:cpu_profiler_example.cpp,不需要在代码里添加任何标签,将profile所有的函数。
代码如下:
![](http://images.cnitblog.com/blog/352788/201305/28193535-dcc503beab114079aaf9b2d5eee1095d.jpg)
Makefile:
![](http://images.cnitblog.com/blog/352788/201305/28193613-7a0d8b0f5ec34d4c978abcb5ba6e837a.jpg)
1)执行程序,生成性能数据文件
CPUPROFILE=/tmp/profile ./cpu_profiler_example
2)分析数据文件
1)pprof -text cpu_profiler_example profile
![](http://images.cnitblog.com/blog/352788/201305/28193642-548894144a7643abb389c20255e56912.jpg)
2)命令行交互模式
![](http://images.cnitblog.com/blog/352788/201305/28193701-32075a297ef244adb11208db08012343.jpg)
事例三:由于我们的程序有可能是服务程序,而服务程序不会自动执行完退出,如果以ctrl+c退出也不是正常的exit(0)的方式退出,而这会导致我们在profile的时候,收集到的数据不全甚至是空的,采用如下解决办法:
将ProfilerStart和ProfilerStop这2个函数封装到两个信号处理函数中,给服务程序发信号SIGUSR1,就开始profile,给服务程序发信号SIGUSR2,就停止profile。这样我们可以随时对程序进行profiling,并获得数据。
代码如下:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <signal.h> #include <google/profiler.h> //SIGUSR1: start profiling //SIGUSR2: stop profiling static void gprof_callback(int signum) { if (signum == SIGUSR1) { printf("Catch the signal ProfilerStart\n"); ProfilerStart("bs.prof"); } else if (signum == SIGUSR2) { printf("Catch the signal ProfilerStop\n"); ProfilerStop(); } } static void setup_signal() { struct sigaction profstat; profstat.sa_handler = gprof_callback; profstat.sa_flags = 0; sigemptyset(&profstat.sa_mask); sigaddset(&profstat.sa_mask, SIGUSR1); sigaddset(&profstat.sa_mask, SIGUSR2); if ( sigaction(SIGUSR1, &profstat,NULL) < 0 ) { fprintf(stderr, "Fail to connect signal SIGUSR1 with start profiling"); } if ( sigaction(SIGUSR2, &profstat,NULL) < 0 ) { fprintf(stderr, "Fail to connect signal SIGUSR2 with stop profiling"); } } int loopop_callee() { int n=0; for(int i=0; i<10000; i++) { for(int j=0; j<10000; j++) { n |= i%100 + j/100; } } return n; } int loopop() { int n=0; while(1) { for(int i=0; i<10000; i++) { for(int j=0; j<10000; j++) { n |= i%100 + j/100; } } printf("result: %d\n", (loopop_callee)() ); } return n; } int main(int argc,char** argv) { char program[1024]={0}; //snprintf(program,1023,"%s_%d.prof",argv[0],getpid()); setup_signal(); printf("result: %d\n", (loopop)() ); return 0; }
关注两个函数gprof_callback和setup_signal。
启动程序,可以采用kill -s SIGUSR1 5722和kill -s SIGUSR2 5722来开始采集和停止采集,5722是进程pid。
5、心得
最后,补充一点,要用google-perftool来分析程序,必须保证程序能正常退出。
采用kcachegrind查看函数之间依赖,并分析程序性能
![](http://images.cnitblog.com/blog/352788/201305/28193831-135ef8597dd0472ca80965dbc86536a1.jpg)
![](http://images.cnitblog.com/blog/352788/201305/28193857-611d2666433143a2b687ee113cc13a99.jpg)
相关文章推荐
- google perftools分析程序性能
- google perftools分析程序性能
- google perftools分析程序性能
- google perftools分析程序性能
- google perftools分析程序性能
- google perftools分析程序性能
- google perftools分析程序性能
- google perftools分析程序性能
- 使用gperftools对程序进行性能分析
- google开源的C++性能分析工具 - gperftools
- [C++]google perftools分析程序CPU / 内存的使用
- 使用 google-perftools 剖析程序性能瓶颈
- 使用 google-perftools 剖析程序性能瓶颈
- google-perftools分析程序瓶颈(最耗cpu的代码块)
- 使用 google-perftools 剖析程序性能瓶颈
- google开源的C++性能分析工具 - gperftools
- 使用 google-perftools 剖析程序性能瓶颈
- 使用 google-perftools 剖析程序性能瓶颈
- ceph存储 Google perftools工具内存检测以及性能分析
- 使用 google-perftools 剖析程序性能瓶颈