一、gprof的安装和说明

在前面谈过了gperftools的安装,今天来看一下gprof的安装。虽然说gperftools安装比较复杂,但是gprof就好说了,因为只要你的机器上装有GCC,那么自然就带了这个软件。如果没有的话,就按照以下的方法安装一下新的gcc即可。不过一般来说,系统都会自带相对最新的gcc,这个不用太担心。
https://blog.csdn.net/fpcc/article/details/99698783?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164531868816780264056766%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=164531868816780264056766&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~rank_v31_ecpm-1-99698783.nonecase&utm_term=GCC&spm=1018.2226.3001.4450

同样,为了安装可视化工具可以采用下面的方法:(gprof2dot需要python2.7及以上版本,同样还需要安装Graphviz)
具体的安装过程如下:
1、去清华或者阿里,或者官网下载Anaconda 并安装:https://www.anaconda.com/products/individual
2、安装完成后,关闭当前终端,再次打开,即可使用conda和 python命令,同时pip和pip3工具也安装成功。
3、安装可视化工具pip3 install gprof2dot
4、sudo apt-get install graphviz(默认一般已经安装)

更多可参看gprof相关的文档:
http://sourceware.org/binutils/docs-2.17/gprof/index.html

二、gprof的使用说明

gprof只记录执行时间超过0.01秒即10毫秒的函数调用时间,所以其实对服务器端的应用受到了比较大的限制。gprof在应用时需要注意以下几点:

1、gprof使用时必须增加下列编译选项,否则无法生成gmon.out文件:
“ -pg Generate extra code to write profile information suitable for the
analysis program gprof. You must use this option when compiling
the source files you want data about, and you must also use it when
linking.”
2、gprof原生并不支持多线程应用,只能对主线程数据进行采集。
为了使其支持多线程,需要在每个线程中增加支持ITIMER_PROF信号的桩函数,感兴趣的可以去网上搜索相关资料。
3、必须保证程序正常结束,否则无法生成gmon.out文件
应用程序的崩溃、非主程序退出、忽略SIGPROF信号或者运行时间非常短,也即低于10毫秒,则会产生这种现象。同样在虚拟机中运行(本次未有出现),也有可能出现这种情况。对于运行时间过短的,可以在应用程序结束位置增加类似下面的代码来获得gmon.out文件:

static void sighandler( int sig_no ){exit(0);}signal( SIGUSR1, sighandler );

这样,在使用kill -USR1 pid 后,同样会生成分析文件gmon.out.

三、例程

在原有的例程基础上修改一下:

#include <iostream>
using namespace std;
void t1(){int i = 0;while (i < 1000){i++;}
}void t2()
{int i = 0;while (i < 2000){i++;}
}void t3()
{for (int i = 0; i < 100000; ++i){t1();t2();}
}int main()
{t3();printf("OK!");return 0;
}

执行编译成功的应用程序,即可得到gmon.out文件。然后就可以使用命令分析相关信息:

f:~/gprof$ gprof ./test_gprof gmon.out|less -SFlat profile:Each sample counts as 0.01 seconds.%   cumulative   self              self     totaltime   seconds   seconds    calls  ms/call  ms/call  name47.21      0.20     0.20   100000     0.00     0.00  t2()37.76      0.37     0.16   100000     0.00     0.00  t1()0.00      0.37     0.00        1     0.00     0.00  _GLOBAL__sub_I__Z2t1v0.00      0.37     0.00        1     0.00   365.37  t3()0.00      0.37     0.00        1     0.00     0.00  __static_initialization_and_destruction_0(int, int)%         the percentage of the total running time of the
time       program used by this function.cumulative a running sum of the number of seconds accountedseconds   for by this function and those listed above it.self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for thislisting.calls      the number of times this function was invoked, ifthis function is profiled, else blank.self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,else blank.total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if thisfunction is profiled, else blank.name       the name of the function.  This is the minor sortfor this listing. The index shows the location ofthe function in the gprof listing. If the index isin parenthesis it shows where it would appear inthe gprof listing if it were to be printed.
^L
Copyright (C) 2012-2020 Free Software Foundation, Inc.Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
^LCall graph (explanation follows)granularity: each sample hit covers 2 byte(s) for 2.74% of 0.37 secondsindex % time    self  children    called     name<spontaneous>
[1]    100.0    0.00    0.37                 main [1]0.00    0.37       1/1           t3() [2]
-----------------------------------------------0.00    0.37       1/1           main [1]
[2]    100.0    0.00    0.37       1         t3() [2]0.20    0.00  100000/100000      t2() [3]0.16    0.00  100000/100000      t1() [4]
-----------------------------------------------0.20    0.00  100000/100000      t3() [2]
[3]     55.6    0.20    0.00  100000         t2() [3]
-----------------------------------------------0.16    0.00  100000/100000      t3() [2].......

转成图像分析:

f:~/gprof$ gprof ./test_gprof gmon.out|gprof2dot |dot -T png -o t.png

四、总结

gprof用起来不如gperftools爽,但总是有胜于无吧。估计大佬们会继续更新完善,后续就会越来越好用。工具有很多种,哪种用得方便,就用哪个,以实现目的为目标,不能僵化教条。一定要明白的是,工具是为开发服务的,而不是增加开发的难度的。

C++性能测试工具gprof安装和应用相关推荐

  1. php性能测试下载,PHP性能测试工具xhprof安装与使用方法详解

    本文实例分析了PHP性能测试工具xhprof安装与使用方法.分享给大家供大家参考,具体如下: xhprof概述: XHProf是一个分层PHP性能分析工具.它报告函数级别的请求次数和各种指标,包括阻塞 ...

  2. HTTP性能测试工具wrk安装及使用

    wrk 是一个很简单的 http 性能测试工具,没有Load Runner那么复杂,他和 apache benchmark(ab)同属于HTTP性能测试工具,但是比 ab 功能更加强大,并且可以支持l ...

  3. 性能测试工具 wrk 安装与初步使用

    开源的性能测试工具 wrk,简单易用,没有Load Runner那么复杂,他和 apache benchmark(ab)同属于性能测试工具,但是比 ab 功能更加强大,并且可以支持lua脚本来创建复杂 ...

  4. 性能测试工具Gprof

    前段时间做产品的性能测试,用了一段时间gprof,感觉很强大. 1. gprof介绍 gprof是GNU profiler工具.可以显示程序运行的"flat profile",包括 ...

  5. Fabric性能测试工具Caliper安装使用

    文章目录 Pre-requisites 安装 make,g++ 编译工具 安装node.js 安装 node-gyp 安装 Docker 安装 Docker-compose Clone caliper ...

  6. 性能测试工具wrk安装使用详解

    建议练习时可以使用FastApi或Flask等Web服务端框架在本地搭建一个HTTP服务. wrk简介 wrk是一个用于HTTP协议的基准测试工具.基准测试是一种性能测试方法,它通过设计科学的测试方法 ...

  7. PAPI性能测试工具的安装、使用及实例

    一.PAPI简介 PAPI是田纳西大学创新计算实验室开发的一组可以在多个处理器平台上对硬件性能计数器进行访问的标准接口,它的目标是方便用户在程序运行时监测和采集由硬件性能计数器记录的处理器事件信息.用 ...

  8. linux下 C++性能测试工具 gprof 和 gprof2dot

    http://www.4ucode.com/Study/Topic/1909519 http://coolaj86.info/articles/super-simple-gprof.html http ...

  9. influxDB性能测试工具 influx-stress安装使用

    influxdata提供的测试工具,influx-stress用于写入压力测试, 安装使用步骤: 1.GO运行环境 详见另一篇博客: https://blog.csdn.net/rclijia/art ...

最新文章

  1. 12、mybatis返回map单条及多条记录
  2. JVM学习笔记之-执行引擎(Execution Engine)
  3. mysql数据库check命令_利用mysqlcheck命令快速修复mysql数据库
  4. C++工作笔记-结构体与类的进一步探究(在C++中的结构体,非C语言结构体)
  5. android-pageviewer实现linearlayout的切换
  6. android金币动效_Android 仿余额宝数字跳动动画效果完整代码
  7. mysql更改安装路径命令_如何修改mysql的安装路径
  8. Android中getDimension,getDimensionPixelOffset和getDimensionPixelSize 区别
  9. 高效记忆/形象记忆(07)110数字编码表 11-20
  10. 如何用计算机看苹果手机的文件,苹果手机备忘录在哪个文件夹?如何查看苹果手机文件...
  11. C语言有哪些冷知识?
  12. 冬天OS(八):加入调度
  13. 复制链接到safari浏览器打开_苹果手机把链接复制到浏览器打不开也下载不了这怎么回事...
  14. MCNP6在windows下的安装
  15. WOL远程开机,实际落地成功。
  16. matlab手写输入,基于matlab的手写输入板
  17. 超详细MySQL(免安装版)安装与配置
  18. 使用python开发 百度网盘接口
  19. 安卓快速修改包名 -.- 备忘
  20. Vue路由--无痕浏览 NodeJs环境搭建

热门文章

  1. html内联元素和块元素的特性,HTML中块级元素和内联元素的特性详解
  2. 【Python人工智能】Python全栈体系(十五)
  3. 如何用WordPress+SRS做直播网站
  4. Promise系列学习
  5. 用IDEA打开文件夹
  6. linux smartctl 命令,Linux下硬盘检测工具smartmontools(smartctl)使用方法
  7. 《锋利的jQuery》笔记 第1章 认识jQuery
  8. 在win8上构建按拼音排序的GridView控件
  9. windows7自己加载的驱动在注册表中的位置
  10. php 页面关了还在执行,php关闭页面仍执行的实现方法