`
小滔哥
  • 浏览: 58109 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

google-perf-tools使用笔记

 
阅读更多
  • 1.Perftools 介绍

    项目地址 http://code.google.com/p/gperftools/

      引用原文里的一段英文介绍

      Perftools is a collection of a high-performance multi-threaded malloc() implementation, plus some pretty nifty performance analysis tools.

      Perftools 是一个高效的多线程 malloc 实现,附加一些很好的性能分析工具。使用这个工具进行分析 cpu profiling memory leak 分析是很不错的。

 

 

  • 2.Perftools 下载、安装

    下载地址 http://code.google.com/p/gperftools/downloads/list ,从这里面选择一个版本,下载后解压,然后进行安装。安装步骤如下

./configure –enable-frame-pointers
Make
Make install

    不出问题的话,上面就可以完成安装了,如有问题,看下 INSTALL,README 文档进行解决。

 

 

  • 3.Perftools使用方法

    使用方法有两种

    第一种 设置 LD_PRELOAD

          假设要分析的执行文件为 run.out

          Export LD_PRELOAD=/usr/local/lib/libprofiler.so:/usr/local/lib/libtcmalloc.so

          其中第一个库是做 cpu profiling 需要的库,第二个是对 memory profiling 需要的库

          然后执行 CPUPROFILE=/tmp/prof.out HEAPCHECK=strict /path/to/run.out

          便会在 /tmp/ 目录下生成 prof.out /tmp/run.out.xxxend.heap. 这样的文件

 

    第二种在编译的时候添加 -lprofiler –ltcmalloc 选项

          例如 gcc –o run.so test.c –lprofiler –ltcmalloc ,这样生成的 run.out 在运行的时候也能得到 profling 的结果文件

          CPUPROFILE=/tmp/prof.out HEAPCHECK=strict /path/to/run.out

          同样会在 /tmp 目录下生成第一种方法那样的两个文件

 

 

  • 4.分析结果

      使用 pprof profiling 的结果文件进行分析

      对 cpu profiling 的结果文件执行下面的命令

      pprof --text ./a.out /tmp/prof.out

      会得到类似下面的输出,从中可以看到各个函数执行时间的比例

Using local file ./a.out.

Using local file /tmp/prof.out.

Removing killpg from all stack traces.

Total: 2255 samples

    1677  74.4%  74.4%     1677  74.4% consumeSomeCPUTime1

     578  25.6% 100.0%     1131  50.2% consumeSomeCPUTime2

       0   0.0% 100.0%     2255 100.0% __libc_start_main

       0   0.0% 100.0%     2255 100.0% main

       0   0.0% 100.0%     2255 100.0% stupidComputing 
 

      每一列的含义如下所示

      1.Number of profiling samples in this function

      2.Percentage of profiling samples in this function

      3.Percentage of profiling samples in the functions printed so far

      4.Number of profiling samples in this function and its callees

      5.Percentage of profiling samples in this function and its callees

      6.Function name

 

      对 memory   profiling 的结果文件执行下面的命令

      pprof --text ./run.out /tmp/run.out.29318._main_-end.heap

      会得到类似下面的输出,给出了 memory leak 产生的函数及所占比例

Total: 0.0 MB

     0.0  66.7%  66.7%      0.0  66.7% b

     0.0  33.3% 100.0%      0.0  33.3% a

     0.0   0.0% 100.0%      0.0 100.0% __libc_start_main

     0.0   0.0% 100.0%      0.0 100.0% main 

      每一列的含义同上面类似,将 leak 的比例函数执行时间的比例即可。

      当然,还可以选择其他的生成方式,例如将--text换成--pdf,--gv等这样的,生成pdf和图片方式。

 

 

    • 5.总结

             Perftools 是个很不错的工具,对无论性能分析,还是对内存进行分析,都能给开发者提供很好的解决问题思路。而且工具是开源的,如果需要加一些什么功能或者修复什么 bug ,也是比较方便的,我们这边有同学就加了一点功能。以后把这个工具用起来,再慢慢谈使用这个工具的一些经验吧。

      分享到:
      评论

      相关推荐

      Global site tag (gtag.js) - Google Analytics