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)

gprof输出内容解释相关推荐

  1. gprof 输出内容解释

    gprof输出内容示例 Each sample counts as 0.01 seconds .% cumulative self self total time seconds seconds ca ...

  2. lsmod 输出内容解释

    lsmod 第一列(Module):模块名称 第二列(Size):模块大小,单位:字节 第三列.第四列(Used by):模块实例个数,以及被谁实例化(被谁使用) 举例一: snd_pcm 11468 ...

  3. MFC匿名管道原理详解、函数总结、调用实例(用MFC的匿名管道读取CMD输出内容)(C++语言)

    本博客主要总结MFC中匿名管道的原理和具体调用实例,以及调用匿名管道三个核心函数各个参数用法详解,具体的如下所述. 博主在做项目时,遇到一个问题.用程序调用一个进程,然后读取进程输出信息.但是,博主用 ...

  4. linux将屏幕输出到文件,Linux命令执行的屏幕输出内容重定向到日志文件

    摘要: 作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 快速mark一下这个命令细节,免得以后使用又忘记了 大家都知道可以用echo来输出内容到 ...

  5. JavaScript知识笔记(一)——入门、语句、注释、变量、函数、输出内容、对话框、窗口

    JavaScript可以提供漂亮的网页.令用户满意的上网体验. 1.增强页面动态效果(如:下拉菜单.图片轮播.信息滚动等) 2.实现页面与用户之间的实时.动态交互(如:用户注册.登陆验证等) 引用Ja ...

  6. 使用feof()判断文件结束时会多输出内容的原因

    这是原来的代码: #include <stdio.h> int main() {     FILE * fp;     int ch;     fp = fopen("d:\\a ...

  7. HAproxy增加日志记录功能和自定义日志输出内容、格式

    http://blog.51cto.com/eric1/1854574 一.增加haproxy日志记录功能   1.1 由于数据分析的需要,我们必须打开haproxy日志,记录相关信息. 在配置前,我 ...

  8. securecrt导出linux日志文件,secureCRT保存屏幕输出内容

    1.有时执行一个mysql语句,屏幕打印行过多,需要设置翻动最大行数 打开Options Session Options–>Terminal–>Emulation,在Scrollback输 ...

  9. ThinkPHP模版引擎之变量输出具体解释

    ThinkPHP模版引擎之变量输出具体解释 使用ThinkPHP开发有一定时间了,今日对ThinkPHP的模板引擎变量解析深入了解了一下.做出一些总结,分享给大家供大家參考. 详细分析例如以下: 我们 ...

最新文章

  1. 易宝典文章——如何在Exchange Server 2010 SP1下将用户邮箱导出到PST中
  2. SAP Spartacus 3.0 的一些变化
  3. Vue.js2.0开发环境搭建(二)
  4. C语言麻将递归,C++数据结构与算法——麻将胡牌算法(二:完全胡牌算法)
  5. 微型计算机接口与技术期末,微机原理与接口技术期末考试试题及答案.pdf
  6. java for 面试题_Java面试题整理
  7. The DotNet Garbage Collection
  8. 转行 AI 成为技术大牛,你需要理解这两项技术!
  9. RHEL7.0手动安装
  10. 解决办法:string in namespace std does not name a type
  11. 对图像 香农费诺编码 matlab 实现,香农编码费诺编码.doc
  12. 暗时间--平凡与优秀间的距离
  13. 阿拉伯数字转中文数字(大写或小写)
  14. phpstudy安装yar扩展
  15. npm run dev报错 Class constructor ServeCommand cannot be invoked without new
  16. C++问答2 三大特性
  17. linux从零基础开始
  18. float a=1.0f 这里的1.0f中的 f 代表什么?有什么意思?
  19. Python数据可视化——图型参数介绍
  20. Windows 7 Windows10 纯净正版下载

热门文章

  1. 地理新教材降难度-小学生只需明白地球是圆的-人教社-新课改-教材
  2. android 访问内网ip_android 获取局域网IP与MAC 地址 毫秒级(详解)
  3. 浅谈几种主流数控机床的数据采集技术
  4. Adept机器人坐标系
  5. 如何彻底清除上网痕迹
  6. 医院信息系统建设技术参数及资质要求招标公告
  7. python实现3d扫描_基于3D扫描、机器人和物联技术的自动化逆向工程解决方案
  8. 关于 chrome 谷歌浏览器 最小化后 页面被限制,不能运行的解决方案
  9. LeetCode-287-寻找重复数
  10. redhad 下载)转