Gprof使用介绍

来源:互联网 发布:c语言ceil 编辑:程序博客网 时间:2024/05/27 19:27
Gprof功能:打印出程序运行中各个函数消耗的时间,可以帮助程序员找出众多函数中耗时最多的函数。产生程序运行时候的函数调用关系,包括调用次数,可以帮助程序员分析程序的运行流程。
有了函数的调用关系,这会让开发人员大大提高工作效率,不用费心地去一点点找出程序的运行流程,这对小程序来说可能效果不是很明显,但对于有几万,几十万代码量的工程来说,效率是毋庸置疑的!而且这个功能对于维护旧代码或者是分析OpenSource来说那是相当诱人的,有了调用图,对程序的运行框架也就有了一个大体了解,知道了程序的“骨架“,分析它也就不会再那么茫然,尤其是对自己不熟悉的代码和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 –pgtest.c –o test , 则gcc会生成一个名为test的可执行文件,在命令行下输入[linux /home/test]$./test

2.执行你的应用程序使之生成供gprof 分析的数据。  命令行里面输入如下命令后,会在当前目录下生成一个gmon.out 文件, 这个文件就是供gprof 分析使用的。

[linux /home/test]$a.outmain() function()+--- call b() function+---call a() function+---call c() function[linux /home/test]$
3.使用gprof 程序分析你的应用程序生成的数据。

命令行里面输入:

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

%     cumulative    self              self     totaltime   seconds     seconds    calls  Ts/call  Ts/call  name0.00      0.00     0.00        1     0.00     0.00  a0.00      0.00     0.00        1     0.00     0.00  b0.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     name0.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 秒。

4.gProf参数解释内容如下:
 %                        the percentage of the total running time of the
time                     program used by this function.
                           函数使用时间占所有时间的百分比。
cumulative          a running sum of the number of seconds accounted
 seconds             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 this
                          listing.
                          函数本身所执行的时间。
calls                   the number of times this function was invoked, if
                          this 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.
                          每一次调用花费在函数的时间microseconds。
 total                  the average number of milliseconds spent in this
ms/call               function and its descendents per call, if this
                          function is profiled, else blank.
                          每一次调用,花费在函数及其衍生函数的平均时间microseconds。
name                 the name of the function.  This is the minor sort
                          for this listing. The index shows the location of
                          the function in the gprof listing. If the index is
                          in parenthesis it shows where it would appear in
                          the gprof listing if it were to be printed.
                          函数名