性能分析工具

软件的性能是软件质量的重要考察点,不论是在线服务程序还是离线程序,甚至是终端应用,性能都是用户体验的关键。这里说的性能重大的范畴来讲包括了性能和稳定性两个方面,我们在做软件测试的时候也是要重点测试版本的性能表现和稳定性的。对于软件测试过程中发现的性能问题,如何定位有很多的方法。基本的方法可能是开发者对代码进行review,或者是使用一些工具对代码进行性能分析。常见的性能分析tuning工具有哪些呢?下面两篇文章做了详细的总结:

在我工作中主要是关于Linux C++程序代码的性能分析,gprof是可用于Linux C++代码性能profiling的工具之一,本文主要讲讲我对gprof的学习和使用过程。

Gprof的基本原理

gprof能够让你知道你的代码哪些地方是比较耗时的,哪些函数是被调用次数很多的,并且能够让你一目了然的看到函数与函数之间的调用关系。gprof是gcc/g++编译器支持的一种性能诊断工具。只要在编译时加上-pg选项,编译器就会在编译程序时在每个函数的开头加一个mcount函数调用,在每一个函数调用之前都会先调用这个mcount函数,在mcount中会保存函数的调用关系图和函数的调用时间和被调次数等信息。最终在程序退出时保存在gmon.out文件中,需要注意的是程序必须是正常退出或者通过exit调用退出,因为只要在exit()被调用时才会触发程序写gmon.out文件。

那么,gprof的使用方法主要以下三步:

  • 会用-pg参数编译程序
  • 运行程序,并正常退出
  • 查看gmon.out文件
 

Gprof使用实例

  1. #include<iostream>
  2. using namespace std;
  3. int add(int a, int b)
  4. {
  5. return a+b;
  6. }
  7. int sub(int a, int b)
  8. {
  9. return a-b;
  10. }
  11. int call ()
  12. {
  13. std::cout << add(1,2) << std::endl;
  14. std::cout << sub(2,4) << std::endl;
  15. }
  16. int main()
  17. {
  18. int a=1, b=2;
  19. cout << add(a,b) << endl;
  20. for (int i=0; i<10000; i++)
  21. call();
  22. return 0;
  23. }

使用g++编译并加上-pg参数:

  1. g++ -o hello hello_grof.cpp -pg -g

得到可执行文件,我们可以使用readelf查看一下它的符号表里和没有-pg时编译的有啥不同:readelf -r ./hello和readelf -r ./hello_normal得出的结果对比。

左边为有-pg参数编译的结果。可以看出多了三个函数符号_mcount, __monstartup, _mcleanup都是和gprof相关的调用。

 
使用gdb调试hello程序,在mcount函数中打断点也可以看到其调用关系,在add函数执行前先调用mcount函数:
 
接下来运行程序./hello,会在当前目录下生成gmon.out文件。使用gprof查看文件信息:
  1. gprof -b ./hello gmon.out

得到如下输出:

  1. Flat profile:
  2. Each sample counts as 0.01 seconds.
  3. no time accumulated
  4. %   cumulative   self              self     total
  5. time   seconds   seconds    calls  Ts/call  Ts/call  name
  6. 0.00      0.00     0.00    10001     0.00     0.00  add(int, int)
  7. 0.00      0.00     0.00    10000     0.00     0.00  sub(int, int)
  8. 0.00      0.00     0.00    10000     0.00     0.00  call()
  9. 0.00      0.00     0.00        1     0.00     0.00  global constructors keyed to _Z3addii
  10. 0.00      0.00     0.00        1     0.00     0.00  __static_initialization_and_destruction_0(int, int)
  11. Call graph
  12. granularity: each sample hit covers 2 byte(s) no time propagated
  13. index % time    self  children    called     name
  14. 0.00    0.00       1/10001       main [7]
  15. 0.00    0.00   10000/10001       call() [10]
  16. [8]      0.0    0.00    0.00   10001         add(int, int) [8]
  17. -----------------------------------------------
  18. 0.00    0.00   10000/10000       call() [10]
  19. [9]      0.0    0.00    0.00   10000         sub(int, int) [9]
  20. -----------------------------------------------
  21. 0.00    0.00   10000/10000       main [7]
  22. [10]     0.0    0.00    0.00   10000         call() [10]
  23. 0.00    0.00   10000/10001       add(int, int) [8]
  24. 0.00    0.00   10000/10000       sub(int, int) [9]
  25. -----------------------------------------------
  26. 0.00    0.00       1/1           __do_global_ctors_aux [13]
  27. [11]     0.0    0.00    0.00       1         global constructors keyed to _Z3addii [11]
  28. 0.00    0.00       1/1           __static_initialization_and_destruction_0(int, int) [12]
  29. -----------------------------------------------
  30. 0.00    0.00       1/1           global constructors keyed to _Z3addii [11]
  31. [12]     0.0    0.00    0.00       1         __static_initialization_and_destruction_0(int, int) [12]
  32. -----------------------------------------------
  33. Index by function name
  34. [11] global constructors keyed to _Z3addii (hello_grof.cpp) [9] sub(int, int) [10] call()
  35. [8] add(int, int)          [12] __static_initialization_and_destruction_0(int, int) (hello_grof.cpp)

可以使用运行命令:

  1. gprof -b ./hello  gmon.out | gprof2doc.py > ~WWW/hello.dot

生成dot格式的调用关系图文件,可以使用windows版的GVEdit for Graphviz软件查看调用关系图:

 
附上一张比较复杂的程序调用关系图:
对于调用的关系和调用热点一目了然。
 

Gprof输出解读

这部分内容可将gprof -b ./hello中的-b参数去掉,可以显示字段的详细含义描述:

  1. 14  %         the percentage of the total running time of the
  2. 15 time       program used by this function.
  3. 16
  4. 17 cumulative a running sum of the number of seconds accounted
  5. 18  seconds   for by this function and those listed above it.
  6. 19
  7. 20  self      the number of seconds accounted for by this
  8. 21 seconds    function alone.  This is the major sort for this
  9. 22            listing.
  10. 23
  11. 24 calls      the number of times this function was invoked, if
  12. 25            this function is profiled, else blank.
  13. 26
  14. 27  self      the average number of milliseconds spent in this
  15. 28 ms/call    function per call, if this function is profiled,
  16. 29        else blank.
  17. 30
  18. 31  total     the average number of milliseconds spent in this
  19. 32 ms/call    function and its descendents per call, if this
  20. 33        function is profiled, else blank.
  21. 34
  22. 35 name       the name of the function.  This is the minor sort
  23. 36            for this listing. The index shows the location of
  24. 37        the function in the gprof listing. If the index is
  25. 38        in parenthesis it shows where it would appear in
  26. 39        the gprof listing if it were to be printed.

总结

gprof是常见的性能分析工具,在此罗列一下它的一些不足,也是从网上看的:

  • 1、对多线程支持不好,不准确
  • 2、必须退出exit()才行
  • 3、它只能分析应用程序在运行过程中所消耗掉的用户时间,无法得到程序内核空间的运行时间。对内核态的调用分析无能为力。如果程序系统调用比率比较大,就不适合。

Linux C++程序进行性能分析工具gprof使用入门的更多相关文章

  1. [转]程序进行性能分析工具gprof使用入门

    性能分析工具 软件的性能是软件质量的重要考察点,不论是在线服务程序还是离线程序,甚至是终端应用,性能都是用户体验的关键.这里说的性能重大的范畴来讲包括了性能和稳定性两个方面,我们在做软件测试的时候也是 ...

  2. Linux使用sar进行性能分析

    转:https://blog.csdn.net/xusensen/article/details/54606401#sar%E7%AE%80%E4%BB%8B Linux使用sar进行性能分析 Lin ...

  3. xDebug + webgrind 对 php 程序进行性能分析

    环境 macOs Sierra php 7.0.8 MAMP Pro 集成环境 背景 最近有一个需要在微信朋友圈上线的 h5,本人做了一个抽奖的接口,也没多想,直接上 php ci(CodeIgnit ...

  4. linux环境下 C++性能测试工具 gprof + kprof + gprof2dot

    1.gprof 很有名了,google下很多教程 g++ -pg -g  -o test test.cc ./test   //会生成gmon.out gprof ./test > prof.l ...

  5. Linux下程序包管理工具RPM

    实验环境: CentOS release 6.6 (Final)  一台 IP地址:172.16.249.230 RPM 是 Red Hat Package Manager 的缩写,本意是Red Ha ...

  6. 使用xhprof对php7程序进行性能分析

    Xhprof是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开关来控制是否进行profile. 对于还在使用php5的 ...

  7. linux后台程序开发常用工具

    linux开发工具: 1.编辑工具:1)sourceInsight2)Notepad++3)UltraEdit4)Altova XMLSpy 2.linux服务器访问工具:1)FileZilla2)X ...

  8. 实验-使用VisualVM或JConsole进行对程序进行性能分析

    参考资料: 性能分析神器VisualVM java可视化监控工具 完成下列任务: 1.分析内存堆 使用+进行频繁的字符串拼接 2.CPU性能分析 3.线程分析 编程比较以下几个方法所创建的线程 Exe ...

  9. ubuntu上编译和使用easy_profiler对C++程序进行性能分析

    本文首发于个人博客https://kezunlin.me/post/91b7cf13/,欢迎阅读最新内容! tutorial to compile and use esay profiler with ...

随机推荐

  1. NAVICAT 拒绝链接的问题

    处理步骤 1.先在原有的建一个新的用户 2.成功后,再点击原有的,即可使用

  2. Android4.0图库Gallery2代码分析(二) 数据管理和数据加载

    Android4.0图库Gallery2代码分析(二) 数据管理和数据加载 2012-09-07 11:19 8152人阅读 评论(12) 收藏 举报 代码分析android相册优化工作 Androi ...

  3. extJS4.2.0 tabPanel学习(三)

    了解添加tab的函数 这里设置为自动添加,菜单是从后台获取的数据,前台进行双击的时候,添加tab页 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

  4. 简单制作 OS X Yosemite 10.10 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)

    原文地址: http://www.iplaysoft.com/osx-yosemite.html 简单制作 Mac OS X Yosemite 正式版 USB 启动盘的方法教程: 其实制作 OS X ...

  5. Xcode删除证书

    手动删除 xcode5 provisioning profile path: ~/Library/MobileDevice/Provisioning Profiles

  6. tensorflow的Virtualenv安装方式安装

    本文介绍了如何在ubuntu上以virtualenv方式安装tensorflow. 安装pip和virtualenv: # Ubuntu/Linux 64-bit sudo apt-get insta ...

  7. 使用Pushlet来实现服务器端向客户端推送信息

        使用Pushlet来实现服务器端向客户端推送信息 1.   实现方式: 有两种实现方式: 1.         通过配置文件来实现定时的从服务器端向客户端推送信息 2.         通过A ...

  8. Linux下的强大工具之一sed(转),Shell必备

    sed命令基本用法sed是一个非交互式文本编辑器,它可以对文本文件和标准输入进行编辑,标准输入可以是来自键盘输入.文件重定向.字符串.变量.来自管道的文本等等.sed从文本的一个文本行或标准输入中读取 ...

  9. Android 中内容提供者的使用

    在Android中内容提供者主要是用于不同程序之间的数据共享.内容提供器的用法一般有两种,一种是使用现有的内容提供器来读取和操作相应程序的数据,另一种是创建自己的内容提供器,供其他的程序访问. 使用现 ...

  10. jqGrid简述

    转自:http://www.blogjava.net/ilovebabyfat/archive/2012/04/06/373456.html jqGrid学习之 ------------- 安装 1. ...