gprof输出内容解释

gprof输出内容示例

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total         
 time   seconds   seconds    calls  ms/call  ms/call  name  
 81.13      0.43     0.43       11    39.09    39.09  fun_2()
 18.87      0.53     0.10       11     9.09     9.09  fun_1()
  0.00      0.53     0.00        1     0.00   530.00  fun_3()

self ms/call : 函数自身,不带子函数,每次call消耗时间, ms单位:ms/call
calls   : 函数在程序生命期内的总的调用次数。(注!递归调用不计数;多次递归调用的时间算在单次的调用时间里面)
self seconds : 函数自身,不带子函数,程序生命周期中全部call消耗时间,秒单位;= (self ms/call) * calls / 1000
% time   : 以%形式展示 self seconds 列;
total ms/call   : 函数自身,带子函数,每次call消耗时间, ms单位:ms/call

从上图的看点
1) 看%time列, 或者 "self ms/call"列, 这里消耗时间最多的函数就是最耗费CPU的函数了. 也是最值得优化的函数了. (消耗仅统计函数自身的代码消耗, 不统计子函数的消耗)
   从图中可知 fun_2 消耗最多, fun_1 其次, fun_2几乎是fun_1的四倍.
2) 看"total ms/call"列, 找到包含子函数在内, 最消耗时间的函数是 fun_3()
3) 从"self ms/call"列和"total ms/call"列对比可知, 由于self ms/call列的值很小:0.00, 所以推测虽然fun_3()消耗时间很多, 但时间都是fun_3调用的子函数消耗的.

对应源码:

    #include <stdio.h> 
     
    #define MAX_NUM 10000000 
    int g_count = 10; 
    void fun_1() 
    { 
        int i=0; 
        int g=0; 
        for(; i<MAX_NUM; ++i) 
        { 
            g += i*i; 
        } 
    } 
     
    void fun_2() 
    { 
        int i=0; 
        int g=0; 
        for(; i<MAX_NUM*4; ++i) 
        { 
            g += i*i; 
        } 
    } 
     
    void fun_3() 
    { 
        if (g_count < 0) 
        { 
            return; 
        } 
     
        g_count--;                                                                                                                       
     
        fun_1(); 
        fun_2(); 
        fun_3(); 
    } 
     
    int main() 
    { 
        printf("hello world\n"); 
     
        fun_3(); 
        return 0; 
    } 

=========================================
gprof使用场景
    进程在用户态下占用时间较长的场景;

gprof手册
 英文: http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html
 中文: http://hi.baidu.com/daisycheung/blog/item/72c0f9276465690f908f9df2.html

怎么产生gmon.out文件?
    编译程序是加上 -pg 参数, 然后, 程序正常退出时会产生gmon.out
    举例:g++ -Wall -g -pg 1.cpp -o 1

怎么分析gmon.out
    举例:gprof 进程名 gmon.out

为何分析gmon.out文件报错: gprof: gmon.out file is missing call-graphdata
    函数过于简单,其中没有什么函数调用导致的。

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

使用注意:
 1)一般gprof只能查看用户函数信息。如果想查看库函 数的信息,需要在编译是再加入“-lc_p”编 译参数代替“-lc”编译参数,这样程序会链接libc_p.a库, 才可以产生库函数的profiling信息。
 2) gprof只能在程序正常结束退出之后才 能生成程序测评报告,原因是gprof通过在atexit()里 注册了一个函数来产生结果信息,任何非正常退出都不会执行atexit()的动作,所以不会产生gmon.out文件。如果你的程序是一个不会退出的服务程序,那就只有修改代码来达到目的。如果不想改变程 序的运行方式,可以添加一个信号处理函数解决问题(这样对代码修改最少),例如:
 static void sighandler( int sig_no )  
 {  
  exit(0);  
 }  
 signal( SIGUSR1, sighandler );
 当使用kill -USR1 pid 后,程序退出,生成gmon.out文件。


常用的gprof命 令选项:
 -b            不再输出统计图表中每个字段的详细描述。
 -p            只 输出函数的调用图(Call graph的那部分信息)。
 -q            只输出函数的时间消耗列表。
 -e Name       不 再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。
 -E Name       不再输出函数Name 及其子函数的调用图,此标志类似 于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。
 -f Name       输 出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。
 -F Name       输出 函数Name 及其子函数的调用图,它类似于 -f 标 志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。
 -z           显示使用次数为零的例程(按照调用计数和累积时间计算)。

gprof的分析结果如何解读?

nemo@vm04_sles10:gprof_test$ !gprof
gprof 1 gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total          
 time   seconds   seconds    calls  ms/call  ms/call  name   
100.00      0.33     0.33     1003     0.33     0.33  cpu_task()
  0.00      0.33     0.00     1003     0.00     0.33  foo1()
  0.00      0.33     0.00       50     0.00     0.00  foo_empty(int)
  0.00      0.33     0.00        1     0.00   329.01  foo2()

 %         the percentage of the total running time of the  ===========< 有用, 可以直接定位到最消耗CPU的函数的时间
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  ===========< 有用, 可以看出一个函数本身的cpu执行时间(不含其调用的子函数的执行时间)(不含sleep时间)(类似于%time列)
ms/call    function per call, if this function is profiled,
    else blank.

 total     the average number of milliseconds spent in this  ===========< 有用, 从进入到离开函数的消耗CPU时间(不含sleep时间)
ms/call    function and its descendents per call, if this
    function is profiled, else blank.

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.


       Call graph (explanation follows)


granularity: each sample hit covers 4 byte(s) for 3.03% of 0.33 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0    0.00    0.33                 main [1]    // 关于main.  被调:没有; 调用了foo1 3次; foo2 1次; foo_empty 50次
                0.00    0.33       1/1           foo2() [4]
                0.00    0.00       3/1003        foo1() [2]
                0.00    0.00      50/50          foo_empty(int) [11]
-----------------------------------------------
                0.00    0.00       3/1003        main [1]
                0.00    0.33    1000/1003        foo2() [4]
[2]    100.0    0.00    0.33    1003         foo1() [2]    // 关于foo1. 被调:被foo2()调用1000次, 被main调用3次; 主调:调用cputask 1003次
                0.33    0.00    1003/1003        cpu_task() [3]
-----------------------------------------------
                0.33    0.00    1003/1003        foo1() [2]
[3]    100.0    0.33    0.00    1003         cpu_task() [3]   // 关于cpu_task, 被调:被foo1调用1003次. 主调:没有
-----------------------------------------------
                0.00    0.33       1/1           main [1]
[4]     99.7    0.00    0.33       1         foo2() [4]    // 关于foo2. 被调:main()调用1次; 主调:调foo1 1000次; (其中foo1总共被掉了1003次, 有三次是其他人调的)
                0.00    0.33    1000/1003        foo1() [2]
-----------------------------------------------
                0.00    0.00      50/50          main [1]
[11]     0.0    0.00    0.00      50         foo_empty(int) [11] // 关于foo_empty. 被调:main()调用50次; 主调:没有
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index A unique number given to each element of the table.
  Index numbers are sorted numerically.
  The index number is printed next to every function name so
  it is easier to look up where the function in the table.

     % time This is the percentage of the `total' time that was spent
  in this function and its children.  Note that due to
  different viewpoints, functions excluded by options, etc,
  these numbers will NOT add up to 100%.

     self This is the total amount of time spent in this function.

     children This is the total amount of time propagated into this
  function by its children.

     called This is the number of times the function was called.
  If the function called itself recursively, the number
  only includes non-recursive calls, and is followed by
  a `+' and the number of recursive calls.

     name The name of the current function.  The index number is
  printed after it.  If the function is a member of a
  cycle, the cycle number is printed between the
  function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self This is the amount of time that was propagated directly
  from the function into this parent.

     children This is the amount of time that was propagated from
  the function's children into this parent.

     called This is the number of times this parent called the
  function `/' the total number of times the function
  was called.  Recursive calls to the function are not
  included in the number after the `/'.

     name This is the name of the parent.  The parent's index
  number is printed after it.  If the parent is a
  member of a cycle, the cycle number is printed between
  the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self This is the amount of time that was propagated directly
  from the child into the function.

     children This is the amount of time that was propagated from the
  child's children to the function.

     called This is the number of times the function called
  this child `/' the total number of times the child
  was called.  Recursive calls by the child are not
  listed in the number after the `/'.

     name This is the name of the child.  The child's index
  number is printed after it.  If the child is a
  member of a cycle, the cycle number is printed
  between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.

 

Index by function name

   [2] foo1()                  [3] cpu_task()
   [4] foo2()                 [11] foo_empty(int)

 

你可能感兴趣的:(测试,each,fun)