使用Gnu gprof进行Linux平台下的程序分析 (转)

os posted @ 2013年8月28日 18:59 in gprof , 1890 阅读

Gprof 简介:

Gprof功能:打印出程序运行中各个函数消耗的时间,可以帮助程序员找出众多函数中耗时最多的函数。产生程序运行时候的函数调用关系,包括调用次数,可以帮助程序员分析程序的运行流程。

有了函数的调用关系,这会让开发人员大大提高工作效率,不用费心地去一点点找出程序的运行流程,这对小程序来说可能效果不是很明显,但对于有几万,几十万代码量的工程来说,效率是毋庸置疑的!而且这个功能对于维护旧代码或者是分析Open Source来说那是相当诱人的,有了调用图,对程序的运行框架也就有了一个大体了解,知道了程序的“骨架“,分析它也就不会再那么茫然,尤其是对自己不熟悉的代码和Open Source。费话不多说了,让我们开始我们的分析之旅吧!

Gprof 实现原理:

通过在编译和链接你的程序的时候(使用 -pg 编译和链接选项),gcc 在你应用程序的每个函数中都加入了一个名为mcount ( or “_mcount” , or “__mcount” , 依赖于编译器或操作系统)的函数,也就是说你的应用程序里的每一个函数都会调用mcount, 而mcount 会在内存中保存一张函数调用图,并通过函数调用堆栈的形式查找子函数和父函数的地址。这张调用图也保存了所有与函数相关的调用时间,调用次数等等的所有信息。

Gprof基本用法:

1. 使用 -pg 编译和链接你的应用程序。

2. 执行你的应用程序使之生成供gprof 分析的数据。

3. 使用gprof 程序分析你的应用程序生成的数据。

Gprof 简单使用:

让我们简单的举个例子来看看Gprof是如何使用的。

1.打开linux终端。新建一个test.c文件,并生用-pg 编译和链接该文件。 test.c 文件内容如下:

 

#include "stdio.h"

#include "stdlib.h"

void a(){

  printf("\t\t+---call a() function\n");

}

void c(){

  printf("\t\t+---call c() function\n");

}

int b(){

printf("\t+--- call b() function\n");

a();

c();

return 0;

}

int main(){

printf(" main() function()\n");

b();

}



命令行里面输入下面命令,没加-c选项,gcc 会默认进行编译并链接生成a.out:

[linux /home/test]$gcc -pg test.c



如果没有编译错误,gcc会在当前目录下生成一个a.out文件,当然你也可以使用 –o 选项给生成的文件起一个别的名字,像 gcc –pg test.c –o test , 则gcc会生成一个名为test的可执行文件,在命令行下输入[linux /home/test]$./test , 就可以执行该程序了,记住一定要加上 ./ 否则程序看上去可能是执行,可是什么输出都没有。

 

2.执行你的应用程序使之生成供gprof 分析的数据。 命令行里面输入:

 

[linux /home/test]$a.out

main() function()

+--- call b() function

+---call a() function

+---call c() function

[linux /home/test]$

你会在当前目录下看到一个gmon.out 文件, 这个文件就是供gprof 分析使用的。

 

3.使用gprof 程序分析你的应用程序生成的数据。

命令行里面输入:

 

[linux /home/test]$ gprof -b a.out gmon.out | less

由于gprof输出的信息比较多,这里使用了 less 命令,该命令可以让我们通过上下方向建查看gprof产生的输出,| 表示gprof -b a.out gmon.out 的输出作为 less的输入。下面是我从gprof输出中摘抄出的与我们有关的一些详细信息。


				

% cumulative self self total

time seconds seconds calls Ts/call Ts/call name

0.00 0.00 0.00 1 0.00 0.00 a

0.00 0.00 0.00 1 0.00 0.00 b

0.00 0.00 0.00 1 0.00 0.00 c

Call graph

granularity: each sample hit covers 4 byte(s) no time propagated

index % time self children called name

0.00 0.00 1/1 b [2]

[1] 0.0 0.00 0.00 1 a [1]

-----------------------------------------------

0.00 0.00 1/1 main [10]

[2] 0.0 0.00 0.00 1 b [2]

0.00 0.00 1/1 a [1]

0.00 0.00 1/1 c [3]

-----------------------------------------------

0.00 0.00 1/1 b [2]

[3] 0.0 0.00 0.00 1 c [3]

 

从上面的输出我们能明显的看出来,main 调用了 b 函数, 而b 函数分别调用了a 和 c 函数。由于我们的函数只是简单的输出了一个字串,故每个函数的消耗时间都是0 秒。

gprof产生的信息解释如下:

 

 

使用Gnu gprof进行Linux平台下的程序分析(图一)
gprof产生的信息解释

 

常用的Gprof 命令选项解释:

-b不再输出统计图表中每个字段的详细描述。

-p 只输出函数的调用图(Call graph 的那部分信息)。

-q 只输出函数的时间消耗列表。

-E Name不再输出函数Name 及其子函数的调用图,此标志类似于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。

-e Name 不再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。

-F Name 输出函数Name 及其子函数的调用图,它类似于 -f 标志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。

-f Name输出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。

-z 显示使用次数为零的例程(按照调用计数和累积时间计算)。

到这为止你可能对gprof 有了一个比较感性的认识了,你可能会问如何用它去分析一个真正的Open Source 呢!下面就让我们去用gprof去分析一个Open Source,看看如何去在真实的环境中使用它。

使用Gprof 分析 Cflow开源项目

CFlow 是程序流程分析工具,该工具可以通过分析C源代码,产生程序调用图!有点跟Gprof差不多,不过CFlow是通过源代码进行的静态分析并且 不能分析C++ 程序,你可以到http://www.gnu.org/software/cflow/去下载源代码。

假设你已经下载了该源代码(cflow-1.1.tar.gz),并把它放置在/home目录下,让我们看看如何在这个应用上使用gprof。

1. 使用 -pg 编译和链接该应用程序,请输入下列命令。

 

  [linux /home/]tar zxvf cflow-1.1.tar.gz

[linux /home/cflow-1.1/src]$./configure

[linux /home]$make CFLAGS=-pg LDFLAGS=-pg 

如果没有出错你会在/home/cflow-1.1/src 目录下发行一个名为cflow的可执行文件,这就是我们加入-pg编译选项后编译出来的可以产生供gprof提取信息的可执行文件。记住一定要在编译和链接的时候都使用-pg选项,否则可能不会产生gmon.out文件。对于cflow项目,CFLAGS=-pg 是设置它的编译选项,LDFLAGS=-pg是设置它的链接选项。当然你也可以直接修改它的Makefile来达到上述相同的目的,不过一定要记住编译和链接都要使用-pg选项。

 

2. 运行cflow 程序使之生成gmon.out 文件供gprof使用。

 

[linux /home/cflow-1.1/src]$cflow parser.c

查看/home/cflow-1.1/src目录下有没有产生gmon.out文件,如果没有请重复第一步,并确认你已经在编译和链接程序的时候使用了-pg 选项。Cflow的使用请参考http://www.gnu.org/software/cflow/manual/cflow.html。

 

3. 使用gprof分析程序

[linux /home/cflow-1.1/src]$gprof -b cflow gmon.out | less

恭喜你,不出意外你会在屏幕上看到gprof的输出,函数消耗时间和函数调用图,下面是我从我的输出中摘抄出来的一小段。

 


				

% cumulative self self total

time seconds seconds calls Ts/call Ts/call name

0.00 0.00 0.00 118262 0.00 0.00 include_symbol

0.00 0.00 0.00 92896 0.00 0.00 is_printable

0.00 0.00 0.00 28704 0.00 0.00 set_level_mark

0.00 0.00 0.00 28703 0.00 0.00 is_last

0.00 0.00 0.00 19615 0.00 0.00 auto_processor

0.00 0.00 0.00 15494 0.00 0.00 gnu_output_handler

0.00 0.00 0.00 12286 0.00 0.00 delete_parm_processor

0.00 0.00 0.00 7728 0.00 0.00 newline

0.00 0.00 0.00 7728 0.00 0.00 print_function_name

0.00 0.00 0.00 7728 0.00 0.00 print_level

。。。。。。

。。。。。。

Call graph

granularity: each sample hit covers 4 byte(s) no time propagated

index % time self children called name

[1] 0.0 0.00 0.00 79+855 [1]

0.00 0.00 166 dcl [52]

0.00 0.00 163 parse_dcl [53]

0.00 0.00 150 dirdcl [56]

0.00 0.00 129 parse_declaration [63]

0.00 0.00 98 parse_variable_declaration [66]

0.00 0.00 63 maybe_parm_list [69]

0.00 0.00 63 parse_function_declaration [70]

0.00 0.00 39 func_body [74]

。。。。。。

。。。。。。

 

通过分析%time你就知道了那个函数消耗的时间最多,你可以根据这个输出信息做有目的的优化,不过cflow执行的速度是在是太快了,以至%time都是0 (消耗时间是以秒为单位进行统计的)。

生成图形化的函数调用图

1.Graphviz 工具

看到这里你也可能觉得上面的函数调用图实在是不方便察看,也看不出来一个程序调用的整体框架。没有关系,我再介绍一个有用的工具给你,使用 Graphviz,Graphviz or Graph Visualization 是由 AT&T 开发的一个开源的图形可视化工具。它提供了多种画图能力,但是我们重点关注的是它使用 Dot 语言直连图的能力。在这里,将简单介绍如何使用 Dot 来创建一个图形,并展示如何将分析数据转换成 Graphviz 可以使用的规范, Dot 使用的图形规范。

使用 Dot 语言,你可以指定三种对象:图、节点和边。为了让你理解这些对象的含义,我将构建一个例子来展示这些元素的用法。

下图给出了一个简单的定向图(directed graph),其中包含 3 个节点。第一行声明这个图为 G,并且声明了该图的类型(digraph)。接下来的三行代码用于创建该图的节点,这些节点分别名为 node1、node2 和 node3。节点是在它们的名字出现在图规范中时创建的。边是在在两个节点使用边操作(->)连接在一起时创建的,如第 6 行到第 8 行所示。我还对边使用了一个可选的属性 label,用它来表示边在图中的名称。最后,在第 9 行完成对该图规范的定义。

使用 Dot 符号表示的示例图(test.dot)

 

 1: digraph G {

2:   node1;

3:   node2;

4:   node3;

5:

6:   node1 -> node2 [label="edge_1_2"];

7:   node1 -> node3 [label="edge_1_3"];

8:   node2 -> node3 [label="edge_2_3"];

9: }

要将这个 .dot 文件转换成一个图形映像,则需要使用 Dot 工具,这个工具是在 Graphviz 包中提供的。清单 6 介绍了这种转换。

清单 6. 使用 Dot 来创建 JPG 映像

[linux /home]$ dot -Tjpg test.dot -o test.jpg

在这段代码中,我告诉 Dot 使用 test.dot 图形规范,并生成一个 JPG 图像,将其保存在文件 test.jpg 中。所生成的图像如图1所示。在此处,我使用了 JPG 格式,但是 Dot 工具也可以支持其他格式,其中包括 GIF、PNG 和 postscript等等。

 

使用Gnu gprof进行Linux平台下的程序分析(图二)
图 1. Dot 创建的示例图



Dot 语言还可以支持其他一些选项,包括外形、颜色和很多属性。有兴趣可以查看graphviz相关文档。

2.从gprof的输出中提取调用图信息,产生可供Graphviz使用的dot文件。

这样的脚本有人已经实现了,我们只要下载一个现成的就可以了,首先从http://www.ioplex.com/~miallen/ 网站下载一个mkgraph脚本。解压该脚本到包含gmon.out文件的目录下。使用mkgraph0.sh产生调用的jpg图像文件。例如:使用上面的例子,生成cflow的调用图。

[linux /home/cflow-1.1/src]$ mkgraph0.sh cflow gmon.out

部分调用图如下,有了这个图是不是对程序整体框架有了个清晰地了解,如果你对生成的调用图效果不满意,你还可以通过修改mkgraph0脚本使之产生合适的dot文件即可:

 

 

 

使用Gnu gprof进行Linux平台下的程序分析(图三)
部分调用图如下



总结:

使用gprof , Graphviz , mkgraph 生成函数调用图

1. 使用 -pg 编译和链接你的应用程序。

2. 执行你的应用程序使之生成供gprof 分析的数据。

3. 使用mkgraph脚本生成图形化的函数调用图。

 

相关资料:

文档:用 Graphviz 可视化函数调用

文档:Speed your code with the GNU profiler

文档:gropf 帮助文件

Mkgraph 脚本:http://www.ioplex.com/~miallen/

Graphviz 工具:http://www.graphviz.org

Cflow :http://www.gnu.org/software/cflow/

Avatar_small
cleaning companies 说:
2019年9月23日 17:34

This site offers a full to line of residential cleaning up services devised for nearly any need. Most people clean dwellings with well-trained coaches and teams for easy, efficient cleaning up that specializes in healthy cleaning up. From one-room recording studio apartments to your largest properties, our collection of cleaning expert services has a person's cleaning demands covered. Keep reading to understand which service is advisable for you your home.

Avatar_small
shopping batalha 说:
2019年11月16日 18:11

A professional residential appraiser should come to your residence, measure the house, take records and photographs, research information regarding any terrain parcels, and assemble a listing of comparable sales within your neighborhood to determine the value of your property.

Avatar_small
clean tech laws 说:
2020年3月26日 23:31

Legal issues of Gravity is an example of a General Law. So is the Law of Conservation of energy, that is, energy cannot be created or destroyed, only transformed from form to another. However, unlike human laws, which can vary from country to country, Universal Laws are consistent and unchanging. You interact with these laws with every breath you take. They govern your existence. <a href="http://www.cleantechlaws.com">www.cleantechlaws.com</a>

Avatar_small
bank ruptcy law merc 说:
2020年3月26日 23:32

Legal issues of Gravity is an example of a General Law. So is the Law of Conservation of energy, that is, energy cannot be created or destroyed, only transformed from form to another. However, unlike human laws, which can vary from country to country, Universal Laws are consistent and unchanging. You interact with these laws with every breath you take. They govern your existence. <a href="http://www.bankruptcylawmerced.com">www.bankruptcylawmerced.com</a>

Avatar_small
dui lawyer montreal 说:
2020年3月26日 23:32

Everyone is subject to these same natural Laws of the Universe, regardless of race, colour, creed or gender. The laws have to put out their influence without our consent or awareness. When we choose the behaviour, we choose the consequences. Interestingly, the people who are frustrated in life consistently try to defy the natural laws. And, obviously, successful people live in harmony with the natural Laws of the Universe. <a href="http://www.duilawyermontreal.com">www.duilawyermontreal.com</a>

Avatar_small
wendy wood law 说:
2020年3月26日 23:32

I grew up in the 1980s when it seemed that everyone wanted to be a lawyer like the ones on LA Law. The 1980s, 1990s, and 2000s (up until 2007) was the era of Big Law when the promise of a $100, 000 to $160, 000 salary was, it seemed, extended to anyone graduating from a top 20 school and to many people graduating from a top 50 law school with great grades and clerkships. <a href="http://www.wendywoodlaw.com">www.wendywoodlaw.com</a>

Avatar_small
star lite shopping m 说:
2020年3月26日 23:33

Online shopping is the perfect solution for the home bound and for those who find shopping a painful task. Online shopping has turned out to be a boon for those who live in rural areas too. Whether you're buying directly from a business online retailer, an individual online shopping site, or an Internet auction site, shopping online can be fun, simple, time saving and economical. <a href="http://www.starliteshoppingmall.com">www.starliteshoppingmall.com</a>

Avatar_small
maid agency dubai 说:
2020年4月30日 19:37

Basic pressures of current day life you would like help and you don't have to possibly be very rich for getting help. You might get a maid for a couple of days within a week that can help with the property chores and regain some of your energy. Having some sort of maid that can help close to your house will besides free up your efforts, it is additionally a wonderful strategy to relieve pressure and lower depression.

Avatar_small
wall painting servic 说:
2020年4月30日 19:37

In case you have vaulted ceilings, you will be wondering how you will will colour the rooms. You just might barely reach that has a long hierarchy, assuming you get one, but you won't need to necessarily have the capacity to do a reputable job. Impress ones guests by means of keeping paint within the walls and journey ceiling, even with high rooms that sound impossible to realize.

Avatar_small
full time maids in d 说:
2021年6月07日 18:17

It will be best in the event you always chose house cleaning from your early morning hours. Because, through the daytime, even a tiny food particle is seen more plainly. However, any time it receives dark, identifying the particular dirt or perhaps grimes can be impossible.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter