• gprof——GNU性能分析工具


    gprof介绍

    gprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。

    为gprof编译程序


    在编译或链接源程序的时候在编译器的命令行参数中加入“-pg”选项,编译时编译器会自动在目标代码中插入用于性能测试的代码片断,这些代码在程序在运行时采集并记录函数的调用关系和调用次数,以及采集并记录函数自身执行时间和子函数的调用时间,程序运行结束后,会在程序退出的路径下生成一个gmon.out文件。这个文件就是记录并保存下来的监控数据。可以通过命令行方式的gprof或图形化的Kprof来解读这些数据并对程序的性能进行分析。另外,如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。如果想执行一行一行的profiling,还需要加入“-g”编译参数。
    例如如下命令行:gcc -Wall -g -pg -lc_p example.c -o example

    Gprof基本用法:

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

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

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

    $gprof -b a.out 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        1     0.00     0.00  function

                            Call graph

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

    index % time    self  children    called     name
                    0.00    0.00       1/1           main [8]
    [1]      0.0    0.00    0.00       1         function [1]
    -----------------------------------------------

    Index by function name

       [1] function

    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.
                              函数名

    命令格式

    gprof [可执行文件] [gmon.out文件] [其它参数]

    方括号中的内容可以省略。如果省略了“可执行文件”,gprof会在当前目录下搜索a.out文件作为可执行文件,而如果省略了gmon.out文件,gprof也会在当前目录下寻找gmon.out。其它参数可以控制gprof输出内容的格式等信息。最常用的参数如下:

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

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

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

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

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

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

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

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

    不过,gprof不能显示对象之间的继承关系,这也是它的弱点.

  • 相关阅读:
    信号学习第一课--基础知识
    进程学习第一课--基本操作
    执行sql语句后的数据处理api
    如何持续改善你的开车技术?
    刹车失灵不用怕 常见故障分析及应对方法
    原地打方向原来很伤车?看完还敢吗?
    这儿有四个开车小细节,新手司机一定要牢记
    毁车隐患多 真正的老司机绝不会这样停车
    80%的事故发生在这些路段!想保命快点我
    流言终结者!自动挡用车误区,你可能每天都在犯错
  • 原文地址:https://www.cnblogs.com/feisky/p/1681997.html
Copyright © 2020-2023  润新知