[转]程序进行性能分析工具gprof使用入门
性能分析工具
软件的性能是软件质量的重要考察点,不论是在线服务程序还是离线程序,甚至是终端应用,性能都是用户体验的关键。这里说的性能重大的范畴来讲包括了性能和稳定性两个方面,我们在做软件测试的时候也是要重点测试版本的性能表现和稳定性的。对于软件测试过程中发现的性能问题,如何定位有很多的方法。基本的方法可能是开发者对代码进行review,或者是使用一些工具对代码进行性能分析。常见的性能分析tuning工具有哪些呢?下面两篇文章做了详细的总结:
- https://computing.llnl.gov/tutorials/performance_tools/#Considerations
- http://en.wikipedia.org/wiki/List_of_performance_analysis_tools
Gprof的基本原理
gprof能够让你知道你的代码哪些地方是比较耗时的,哪些函数是被调用次数很多的,并且能够让你一目了然的看到函数与函数之间的调用关系。gprof是gcc/g++编译器支持的一种性能诊断工具。只要在编译时加上-pg选项,编译器就会在编译程序时在每个函数的开头加一个mcount函数调用,在每一个函数调用之前都会先调用这个mcount函数,在mcount中会保存函数的调用关系图和函数的调用时间和被调次数等信息。最终在程序退出时保存在gmon.out文件中,需要注意的是程序必须是正常退出或者通过exit调用退出,因为只要在exit()被调用时才会触发程序写gmon.out文件。
那么,gprof的使用方法主要以下三步:
- 会用-pg参数编译程序
- 运行程序,并正常退出
- 查看gmon.out文件
Gprof使用实例
#include<iostream>
using namespace std; int add(int a, int b)
{
return a+b;
} int sub(int a, int b)
{
return a-b;
} int call ()
{
std::cout << add(1,2) << std::endl;
std::cout << sub(2,4) << std::endl;
} int main()
{
int a=1, b=2;
cout << add(a,b) << endl;
for (int i=0; i<10000; i++)
call();
return 0;
}
使用g++编译并加上-pg参数:
g++ -o hello hello_grof.cpp -pg -g
得到可执行文件,我们可以使用readelf查看一下它的符号表里和没有-pg时编译的有啥不同:readelf -r ./hello和readelf -r ./hello_normal得出的结果对比。
左边为有-pg参数编译的结果。可以看出多了三个函数符号_mcount, __monstartup, _mcleanup都是和gprof相关的调用。
gprof -b ./hello gmon.out
得到如下输出:
Flat profile: Each sample counts as 0.01 seconds.
no time accumulated % cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 10001 0.00 0.00 add(int, int)
0.00 0.00 0.00 10000 0.00 0.00 sub(int, int)
0.00 0.00 0.00 10000 0.00 0.00 call()
0.00 0.00 0.00 1 0.00 0.00 global constructors keyed to _Z3addii
0.00 0.00 0.00 1 0.00 0.00 __static_initialization_and_destruction_0(int, int) Call graph granularity: each sample hit covers 2 byte(s) no time propagated index % time self children called name
0.00 0.00 1/10001 main [7]
0.00 0.00 10000/10001 call() [10]
[8] 0.0 0.00 0.00 10001 add(int, int) [8]
-----------------------------------------------
0.00 0.00 10000/10000 call() [10]
[9] 0.0 0.00 0.00 10000 sub(int, int) [9]
-----------------------------------------------
0.00 0.00 10000/10000 main [7]
[10] 0.0 0.00 0.00 10000 call() [10]
0.00 0.00 10000/10001 add(int, int) [8]
0.00 0.00 10000/10000 sub(int, int) [9]
-----------------------------------------------
0.00 0.00 1/1 __do_global_ctors_aux [13]
[11] 0.0 0.00 0.00 1 global constructors keyed to _Z3addii [11]
0.00 0.00 1/1 __static_initialization_and_destruction_0(int, int) [12]
-----------------------------------------------
0.00 0.00 1/1 global constructors keyed to _Z3addii [11]
[12] 0.0 0.00 0.00 1 __static_initialization_and_destruction_0(int, int) [12]
----------------------------------------------- Index by function name [11] global constructors keyed to _Z3addii (hello_grof.cpp) [9] sub(int, int) [10] call()
[8] add(int, int) [12] __static_initialization_and_destruction_0(int, int) (hello_grof.cpp)
可以使用运行命令:
gprof -b ./hello gmon.out | gprof2doc.py > ~WWW/hello.dot
Gprof输出解读
这部分内容可将gprof -b ./hello中的-b参数去掉,可以显示字段的详细含义描述:
14 % the percentage of the total running time of the
15 time program used by this function.
16
17 cumulative a running sum of the number of seconds accounted
18 seconds for by this function and those listed above it.
19
20 self the number of seconds accounted for by this
21 seconds function alone. This is the major sort for this
22 listing.
23
24 calls the number of times this function was invoked, if
25 this function is profiled, else blank.
26
27 self the average number of milliseconds spent in this
28 ms/call function per call, if this function is profiled,
29 else blank.
30
31 total the average number of milliseconds spent in this
32 ms/call function and its descendents per call, if this
33 function is profiled, else blank.
34
35 name the name of the function. This is the minor sort
36 for this listing. The index shows the location of
37 the function in the gprof listing. If the index is
38 in parenthesis it shows where it would appear in
39 the gprof listing if it were to be printed.
总结
gprof是常见的性能分析工具,在此罗列一下它的一些不足,也是从网上看的:
- 1、对多线程支持不好,不准确
- 2、必须退出exit()才行
- 3、它只能分析应用程序在运行过程中所消耗掉的用户时间,无法得到程序内核空间的运行时间。对内核态的调用分析无能为力。如果程序系统调用比率比较大,就不适合。
[转]程序进行性能分析工具gprof使用入门的更多相关文章
- Linux C++程序进行性能分析工具gprof使用入门
性能分析工具 软件的性能是软件质量的重要考察点,不论是在线服务程序还是离线程序,甚至是终端应用,性能都是用户体验的关键.这里说的性能重大的范畴来讲包括了性能和稳定性两个方面,我们在做软件测试的时候也是 ...
- xDebug + webgrind 对 php 程序进行性能分析
环境 macOs Sierra php 7.0.8 MAMP Pro 集成环境 背景 最近有一个需要在微信朋友圈上线的 h5,本人做了一个抽奖的接口,也没多想,直接上 php ci(CodeIgnit ...
- 使用xhprof对php7程序进行性能分析
Xhprof是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开关来控制是否进行profile. 对于还在使用php5的 ...
- 实验-使用VisualVM或JConsole进行对程序进行性能分析
参考资料: 性能分析神器VisualVM java可视化监控工具 完成下列任务: 1.分析内存堆 使用+进行频繁的字符串拼接 2.CPU性能分析 3.线程分析 编程比较以下几个方法所创建的线程 Exe ...
- ubuntu上编译和使用easy_profiler对C++程序进行性能分析
本文首发于个人博客https://kezunlin.me/post/91b7cf13/,欢迎阅读最新内容! tutorial to compile and use esay profiler with ...
- 用 dotTrace 进行性能分析时,各种不同性能分析选项的含义和用途
对 .NET 程序进行性能分析,dotTrace 能应对绝大多数的场景.在开启一个进程进行性能分析之前,我们会看到一些性能分析选项(Profiler Options).本文将介绍这几个选项的含义,并用 ...
- hadoop中使用hprof工具进行性能分析
在编写完成MapReduce程序之后,调优就成为了一个大问题.如何使用现有工具快速地分析出任务的性能? 对于本地的java应用程序,进行分析可能稍微简单,但是hadoop是一个分布式框架,MapR ...
- 性能分析工具gprof介绍(转载)
性能分析工具gprof介绍Ver:1.0 目录1. GPROF介绍 42. 使用步骤 43. 使用举例 43.1 测试环境 43.2 测试代码 43.3 数据分析 53.3.1 flat profil ...
- 使用VisualVM进行性能分析及调优(转)
VisualVM 是一款免费的\集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成和分析海量数据.跟踪内存泄漏.监控垃圾回 ...
随机推荐
- Python高级特性:Python迭代、生成器、列表生成式
迭代 给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历称为迭代(Iteration). 在java和C语言中,迭代是通过循环list的下标来完成的,Pyth ...
- android获取view宽高的几种方法
在onCreate方法中我们通过mView.getWidth()和mView.getHeight()获取到的view的宽高都是0,那么下面几种方法就可以在onCreate方法中获取到view的宽高. ...
- P2219 [HAOI2007]修筑绿化带
我是题面 这道题跟理想的正方形很像,不大明白蛤OI是怎么想的,一年出两道这么相近的题 这道题有两个矩形,所以就有了两种做法(说是两种做法,其实只是维护的矩形不同) 一种是维护大矩形,一种是维护小矩形, ...
- 【刷题】BZOJ 3668 [Noi2014]起床困难综合症
Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找 ...
- Swift使用SDWebImage处理远程图片资源
第一步:配置SDWebImage 打开github,https://github.com/rs/SDWebImage,将SDWebImage下载到本地 用Xcode创建一个swift的singleVi ...
- 【转】嵌入式Linux驱动面试题三道
题一: Linux设备中字符设备与块设备有什么主要的区别? 字符设备:字符设备是个能够像字节流(类似文件)一样被访问的设备,由字符设备驱动程序来实现这种特性.字符设备驱动程序通常至少实现open,cl ...
- 【线段树合并】【P2824】 [HEOI2016/TJOI2016]排序
Description 给定一个长度为 \(n\) 的排列,有 \(m\) 次操作,每次选取一段局部进行升序或降序排序,问你一波操作后某个位置上的数字是几 Hint \(1~\leq~n,~m~\le ...
- Fibonacci数列时间复杂度之美妙
Fibonacci数列: fib(0)=1 fib(1)=1 fib(n)=fib(n-1)+fib(n-2) 上课老师出了一道题,求下列函数的时间复杂度: int fib(int d) { ) ; ...
- [poj 1533]最长上升子序列nlogn树状数组
题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...
- Python 函数01
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...