google-perftools+kcachegrind profiler your program

  我之间用过一些开源的profiler工具,自己也做过一些工具,除了自己写的工具可以简单获取自己想要的那一部分数据之外,其他的工具都不那么容易,但是perftools却是个例外,    它是一款针对 C/C++ 程序的性能分析工具,它是一个遵守 BSD 协议的开源项目。使用该工具可以对 CPU 时间片、内存等系统资源的分配和使用进行分析,本文将重点介绍如何进行 CPU 时间片的剖
        这里我将将介绍 如何进行 CPU 时间片的剖析,从安装到数据的呈现,enjoy it。
    一,获取google perftools 安装包。
    您可以在 google-perftools 的网站 (http://code.google.com/p/google-perftools/downloads/list) 上下载最新版的安装包。我用的是gperftools-2.0.tar.gz

    二,install perftools
 1, 先配置安装环境,   ./configure --prefix=/usr --enable-frame-pointers
    --prefix是指定安装路径,如果你不指定默认安装在/usr/local/lib下面,而且可能会有在运行时找不到 
 libprofiler.so文件的可能。--enable-frame-pointer为64位系统准备的。
  
  2,make , make install

  3,kcachegrind安装,sudo apt-get install kcachegrind,这是为了使用它查看生成的信息。

  三,得到结果
  1,compiler
    在你的目标程序中嵌入libprofiler,具体怎么加这要看你是使用makefile还是cmake了。

  2,运行
    运行有两种方式,第一种是静态的在你的要测量的代码的开始的位置和结束位置加上ProfilerStart("filename"),ProfilerStop(), 这样在目标程序运行时就可以对这段时间内程序实际占用的 CPU 时间片进行统计和分析。随便你自己选择了。但是相对而言,工具中添加灵活性更好,开始/结束函数加到代码中显得比较死,我这里使用的工具是gdb。
a,在代码中加入函数
void function(){ 
  //TODO
  //...
  ProfilerStart("CPUProfile");
  //TODO
  //...  
  
  ProfilerStop();
 } 
当然你可以将这两个函数加到任意的地方,不必非得是同一个函数
b,   使用调试工具 gdb 在程序中手动运行性能工具的启动 / 终止函数

命令 功能
ctrl+c 暂停程序的运行
c 继续程序的运行
b 添加函数断点(参数可以是源代码中的行号或者一个函数名)
p 打印某个量的值或者执行一个函数调用

for example:

gdb YOUR_PROGRAM // 启动 gdb 并选择你的程序为 gdb 的启动目标 
(gdb)b main1.c:num // 对应于耗时模块的起始点 
(gdb)b main1.c:num // 对应于耗时模块的终止点 
(gdb)r // 运行 
(gdb)p ProfilerStart("MyProfile")
(gdb)c // 继续程序运行 
(gdb)p ProfilerStop()

这两个函数可以在任意两个点之间开始和结束,只要你的程序能捕捉到更多的点,随意添加都不会有太大的为题。

 四,分析结果

1, pprof --text ./your_execute_file  ./MyProfile

Using local file ./bin/nbd_google_pertools_n.

Using local file asyn_profile_2.

Removing _L_unlock_16 from all stack traces.

Total: 30 samples

       2   6.7%   6.7%        2   6.7% *__GI___libc_malloc

       1   3.3%  10.0%        1   3.3% *__GI___libc_free

       1   3.3%  13.3%        1   3.3% _List_impl

       1   3.3%  16.7%        1   3.3% _Vector_impl

       1   3.3%  20.0%        1   3.3% __fdatasync_nocancel

       1   3.3%  23.3%        1   3.3% __gnu_cxx::new_allocator::construct

       1   3.3%  26.7%        1   3.3% __gnu_cxx::new_allocator::destroy

       1   3.3%  30.0%        1   3.3% __mutex_alloc (inline)

       1   3.3%  33.3%        1   3.3% __pthread_mutex_lock

       1   3.3%  36.7%        1   3.3% boost::detail::shared_count::swap

       1   3.3%  40.0%        1   3.3% boost::shared_ptr::operator->

       1   3.3%  43.3%        1   3.3% boost::unordered_detail::hash_table::find

       1   3.3%  46.7%        2   6.7% exec_implement::print_implement

       1   3.3%  50.0%        1   3.3% execution_base::begin_incoming_ins_call

       1   3.3%  53.3%        1   3.3% memcpy

......................................

具体每一项的意思你可以去查下tutorial


2, pprof --callgrind ./your_execute_file ./MyProfile >you_prifiler.callgrind

kcachegrind you_profiler.callgrind


注意事项:

由于perftools使用的是SIGPROF信号的handler来获取stack信息的,你的程序不能屏蔽signal,否则会出现以下情况:PROFILE: interrupts/evictions/bytes = 0/0/64,文件自然就不会有任何内容,因为我当初遇到过这样的问题,望各位不要重蹈覆辙。


1:首先下载libunwind-1.1 # wget http://down.yunwei8.com/soft/linux/libunwind-1.1.tar.gz 然后安装 # tar zxvf libunwind-1.1.tar.gz # cd libunwind-1.1 # CFLAGS=-fPIC ./configure # make CFLAGS=-fPIC # make CFLAGS=-fPIC install 2:安装google-perftools: 首先下载gperftools-2.0 # wget http://down.yunwei8.com/soft/linux/gperftools-2.0.tar.gz 然后安装 # tar zxvf gperftools-2.0.tar.gz # cd gperftools-2.0/ # ./configure # make && make install # echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf # /sbin/ldconfig 3:重新编译nginx 不会安装的看这里 http://www.yunwei8.com/nginx124/ # cd /soft/nginx-1.2.4 # ./configure --user=www --group=www --prefix=/usr/local/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-google_perftools_module # make && make install 安装参数说明看这里:http://www.yunwei8.com/nginxcs 4:为tcmalloc添加目录,并且赋予适当权限 # mkdir -p /tmp/tcmalloc/ # chown -R www:www /tmp/tcmalloc/ 5:修改 nginx.conf ,令nginx可以 google-perftools实现加速 # vi /usr/local/server/nginx/conf/nginx.conf 修改前面几行为: user www www; worker_processes 8; error_log /web/logs/nginx_error.log crit; pid logs/nginx.pid; google_perftools_profiles /tmp/tcmalloc/; events{ use epoll; worker_connections 65535; } 6:测试和运行 # /usr/local/server/nginx/sbin/nginx -t 如果显示下面信息,即表示配置没问题 nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful 输入代码运行nginx服务 # /usr/local/server/nginx/sbin/nginx # ps aux|grep [n]ginx 如果显以类似下面的信息,即表示nginx已经启动 root 22900 0.0 0.1 43216 1576 ? Ss 08:23 0:00 nginx: master process /usr/local/server/nginx/sbin/nginx www 23019 0.0 2.6 68816 27160 ? S 08:48 0:00 nginx: worker process www 23020 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23021 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23022 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23023 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23024 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23025 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23026 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process 输入代码检测是否支持加速 # lsof -n | grep tcmalloc 如果显示类似下面的信息,即表示支持tcmalloc加速 (nginx八个线程都支持) nginx 31655 www 9w REG 8,1 0 479533 /tmp/tcmalloc/.31655 nginx 31656 www 11w REG 8,1 0 479534 /tmp/tcmalloc/.31656 nginx 31657 www 13w REG 8,1 0 479535 /tmp/tcmalloc/.31657 nginx 31658 www 15w REG 8,1 0 479536 /tmp/tcmalloc/.31658 nginx 31659 www 17w REG 8,1 0 479537 /tmp/tcmalloc/.31659 nginx 31660 www 19w REG 8,1 0 479540 /tmp/tcmalloc/.31660 nginx 31661 www 21w REG 8,1 0 479538 /tmp/tcmalloc/.31661 nginx 31662 www 23w REG 8,1 0 479539 /tmp/tcmalloc/.31662 优化完成
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值