您的位置:首页 > 其它

需要练习的----gprof使用备忘

2012-07-04 22:19 465 查看

gprof使用备忘

zzz: http://blog.csdn.net/gengoo/article/details/5264346
分类: C++编程2010-01-28 10:24 1095人阅读 评论(1) 收藏 举报

gprof介绍

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

为gprof编译程序

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

例如如下命令行:

gcc -Wall -g -pg -lc_p example.c -oexample

执行gprof

执行如下命令行,即可执行gprof:

gprof OPTIONS EXECUTABLE-FILE gmon.outBB-DATA [YET-MORE-PROFILE-DATA-FILES...] [>OUTFILE]

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 forthis

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 minorsort

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对于profiling非常有用,不过初学者可能会遇到一些障碍。下面把步骤一一列出,应该比较容易明白了。

$ uname -a

Linux dev 2.4.21-9.30AXsmp #1 SMP Wed May 26 23:37:09 EDT 2004 i686i686 i386 GNU/Linux

$ gprof --version

GNU gprof 2.14.90.0.4

Based on BSD gprof, copyright 1983 Regents of the University ofCalifornia.

This program is free software. This program has absolutely nowarranty.

$ cat foo.c

int main(void)

{

printf("helloworld/n");

return0;

}

$ gcc -g -pg foo.c

$ ./a.out

hello world

$ gprof

gprof: gmon.out file is missing call-graphdata

这个结果有点莫名其妙。难道这个简单的一个例子都过不了?其实这是gprof的一个“feature”,也许是专门用来迷惑初学者的。出错的原因参见:http://gcc.gnu.org/ml/gcc-help/2006-06/msg00037.html,是我们的函数过于简单,其中没有什么函数调用导致的。

让我们换个复杂一点的例子看看。

$ cat bar.c

static int sub(void);

int main(void)

{

inti;

printf("helloworld/n");

for(i = 0; i < 10; i++)

sub();

return0;

}

static int sub(void)

{

return0;

}

$ gcc -g -pg bar.c

$ ./a.out

hello world

注意这次输出的信息正常了,不过显得有点罗嗦。后面我们将采用“gprof-b”简化之。命令“gprof”其实就是“gprof a.out gmon.out”。

$ gprof

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 10 0.00 0.00 sub

% the percentage of the total running time of the

time program used by this function.

cumulative a running sum of the number of secondsaccounted

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.

total the average number of milliseconds spent in this

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) no timepropagated

index % time self children called name

0.00 0.00 10/10 main [8]

[1] 0.0 0.00 0.00 10 sub [1]

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

This table describes the call tree of the program, and wassorted by

the total amount of time spent in each function and itschildren.

Each entry in this table consists of several lines. The linewith the

index number at the left hand margin lists the currentfunction.

The lines above it list the functions that called thisfunction,

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 wasspent

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 thisfunction.

children This is the total amount of time propagated intothis

function by its children.

called This is the number of times the function wascalled.

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 followingmeanings:

self This is the amount of time that was propagateddirectly

from the function into this parent.

children This is the amount of time that was propagatedfrom

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, theword

`<spontaneous>' is printed in the `name' field, and all theother

fields are blank.

For the function's children, the fields have the followingmeanings:

self This is the amount of time that was propagateddirectly

from the child into the function.

children This is the amount of time that was propagated fromthe

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 isan

entry for the cycle-as-a-whole. This entry shows who calledthe

cycle (as parents) and the members of the cycle (aschildren.)

The `+' recursive calls entry shows the number of function callsthat

were internal to the cycle, and the calls entry for each membershows,

for that member, how many times it was called from other membersof

the cycle.



Index by function name

[1] sub (bar.c)

$ gprof -b

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 10 0.00 0.00 sub

Call graph

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

index % time self children called name

0.00 0.00 10/10 main [8]

[1] 0.0 0.00 0.00 10 sub [1]

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

Index by function name

[1] sub (bar.c)

gprof的这个特性很容易让初学者迷惑,可能一上来就把人吓跑了。我们再写个复杂一点点的例子,应该更容易看出gprof的用处了。

$cat bar.c

static int sub(void);

static int sub2(void);

int main(void)

{

sub();

sub2();

return0;

}

static int sub(void)

{

inti;

intres = 0;

for(i = 0; i < 10000000; i++)

res+= i;

returnres;

}

static int sub2(void)

{

inti;

intres = 0;

for(i = 0; i < 10000000; i++)

res*= i;

returnres;

}

$gcc -g -pg bar.c

$./a.out

$gprof -b

Flat profile:

Each sample counts as 0.01 seconds.

% cumulative self self total

time seconds seconds calls ms/call ms/call name

53.33 0.08 0.08 1 80.00 80.00sub2

46.67 0.15 0.07 1 70.00 70.00sub

Call graph

granularity: each sample hit covers 4 byte(s) for 6.67% of 0.15seconds

index % time self children called name

<spontaneous>

[1] 100.0 0.00 0.15 main [1]

0.08 0.00 1/1 sub2 [2]

0.07 0.00 1/1 sub [3]

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

0.08 0.00 1/1 main [1]

[2] 53.3 0.08 0.00 1 sub2 [2]

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

0.07 0.00 1/1 main [1]

[3] 46.7 0.07 0.00 1 sub [3]

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

Index by function name

[3] sub (bar.c) [2] sub2 (bar.c)



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mydo/archive/2008/06/08/2523273.aspx



使用gprof和oprofile查找性能瓶颈
有些时候,我们特别关注程序的性能,特别是底层软件,比如驱动程序、OS等。为了更好的优化程序性能,我们必须找到性能瓶颈点,“好 钢用在刀刃上”才能取得好的效果,否则可能白做工作。为了找到关键路径,我们可以使用profilng技术,在linux平台上,我们可以使 用gprof和oprofile工具。

gprof是GNU工具之一,它在编译的时候在每个函数的出入口加入了profiling的代码,运行时统计程序在用户态的执行信息,可以得到每个函数的调用次数、执行时间、调用关 系等信息,简单易懂。适合于查找用户级程序的性能瓶颈,对于很多时间都在内核态执行的程序,gprof不 适合。
oprofile也是一个开源的profiling工具,它使用硬件调试寄存器来统计信息,进行 profiling的开销比较小,而且可以对内核进行profiling。 它统计的信息非常的多,可以得到cache的缺失率,memory的 访存信息, 分支预测错误率等等,这些信息gprof是 得不到的,但是对于函数调用次数,它是不能够得到的。。

简单来说,gprof简 单,适合于查找用户级程序的瓶颈,而oprofile稍显复杂,但是得到的信息更多,更适合调试系 统软件。

我们以编译运行hello.c为 例,来说明如何使用这两个工具,这里不解释具体结果的含义,要想详细了解每个结果代表什么意思,可以看一下参考资料中官方站点上的doc信息,里面会给你详尽的解释。
gprof Quick Start
gprof是gnu binutils工 具之一,默认情况下linux系统当中都带有这个工具。

使用 -pg 选 项来编译hello.c,如果要得到带注释的源码清单,则需要增加 -g 选项。运行: gcc -pg -g -o hello hello.c
运行应用程序: ./hello 会 在当前目录下产生gmon.out文件
使用gprof来 分析gmon.out文件,需要把它和产生它的应用程序关联起来:

gprof hello gmon.out -p 得到每个函数占用的执行时间
gprof hello gmon.out -q 得到call graph, 包含了每个函数的调用关系,调用次数,执行时间等信息。
gprof hello gmon.out -A 得到一个带注释的“源 代码清单”,它会注释源码,指出每个函数的执行次数。这需要在编译的时候增加 -g选项。

oprofile Quick Start
oprofile是sourceforge上 面的一个开源项目,在2.6内核上带有这个工具,好像只有smp系 统才有。比较老的系统,需要自己安装,重新编译内核。

oprofile是一套工具,分别完成不同的事情。
op_help: 列出所有支持的事件。

opcontrol:设置需要收集的事件。

opreport: 对结果进行统计输出。

opannaotate: 产生带注释的源/汇编文件,源语言级的注释需要编译源文件时的支持。

opstack: 产生调用图profile,但要求x86/2.6的平台,并且linux2.6安装了call-graph patch

opgprof: 产生如gprof相似的结果。

oparchive: 将所有的原始数据文件 收集打包,可以到另一台机器上进行分析。

op_import: 将采样的数据库文件从另一种abi转化成本地格式。
运行oprofile需要root权限,因为它要加载profile模块,启动oprofiled后台程序等。所以在运行之前,就需要切换到root。

opcontrol --init 加载模块,mout /dev/oprofile 创 建必需的文件和目录
opcontrol --no-vmlinux 或者 opcontrol --vmlinux=/boot/vmlinux-`uname -r` 决 定是否对kernel进行profiling
opcontrol --reset 清楚当前会话中的数据
opcontrol --start 开始profiling
./hello 运行应用程序,oprofile会 对它进行profiling
opcontrol --dump 把收集到的数据写入文件
opcontrol --stop 停止profiling
opcotrol -h 关闭守护进程oprofiled
opcontrol --shutdown 停止oprofiled
opcontrol --deinit 卸载模块

常用的是3→7这 几个过程,得到性能数据之后,可以使用opreport, opstack, opgprof, opannotate几 个工具进行分析,我常用的是opreport, opannotate进行分析。

opreport使用 http://oprofile.sourceforge.net/doc/opreport.html
opannotate使用 http://oprofile.sourceforge.net/doc/opannotate.html
opgprof使用 http://oprofile.sourceforge.net/doc/opgprof.html

最常用的是opreport,这个可以给出image和symbols的信息,比如我想得到每个函数的执行时间占用比例等信息,用来发现系统性能瓶颈。opannotate可以对源码进行注释,指出哪个地方占用时间比较多。常用命令如下:

opreport -l /bin/bash --exclude-dependent --threshold 1 , 用来发现系统瓶颈。

指 定查看/bin/bash的profiling信 息,占用总体执行时间1%以上的函数列表
opannotate --source --output-dir=annotated /usr/local/oprofile-pp/bin/oprofiled
opannotate --source --base-dirs=/tmp/build/libfoo/ --search-dirs=/home/user/libfoo/ --output-dir=annotated/ /lib/libfoo.so

网络资源

gprof 用户手册 http://sourceware.org/binutils/docs-2.17/gprof/index.html
oprofile官方站点 http://oprofile.sourceforge.net/
使用 GNU profiler 来提高代码运行速度 http://www-128.ibm.com/developerworks/cn/linux/l-gnuprof.html
使 用 OProfile for Linux on POWER 识别性能瓶颈 http://www-128.ibm.com/developerworks/cn/linux/l-pow-oprofile/


如何通过gprof 调试makefile编译后生成的执行文件



编译和链接

上面的例子中,程序比较简单,只有一个文件。如果源代码有多个文件,或者代码结构比较复杂,编译过程中先生成若干个目标文件,然后又由链接器将这些目标文件链接到一起,这时该怎么使用gprof呢?

对于由多个源文件组成的程序,编译时需要在生成每个.o文件的时候加上-pg参数,同时在链接的时候也要加上-pg参数。对于链接器不是GCC的情况,如ld,又有特殊的要求。

同时,-pg参数只能记录源代码中各个函数的调用关系,而不能记录库函数的调用情况。要想记录每个库函数的调用情况,链接的时候必须指定库函数的动态(或者静态)链接库libc_p.a,即加上-lc_p,而不是-lc。

还要说明的是,如果有一部分代码在编译时指定了-pg参数,而另一部分代码没有指定,则生成的gmon.out文件中将缺少一部分函数,也没有那些函数的调用关系。但是并不影响gprof对其它函数进行记录。

运行

编译好的程序运行时和运行一般的程序没有什么不同,只是比正常的程序多生成了一个文件gmon.out。注意,这个文件名是固定的,没法通过参数的设置进行改变。如果程序目录中已经有一个gmon.out,则它会被新的gmon.out覆盖掉。

关于生成的gmon.out文件所在的目录,也有以下约定:程序退出时所运行的文件所在目录就是生成的gmon.out文件所在的目录。如果一个程序执行过程中调用了另一个程序,并在另一个程序的运行中终止,则gmon.out会在另一个程序所在的目录中生成。

还有一点要注意的就是当程序非正常终止时不会生成gmon.out文件,也因此就没法查看程序运行时的信息。只有当程序从main函数中正常退出,或者通过系统调用exit()函数而退出时,才会生成gmon.out文件。而通过底层调用如_exit()等退出时不会生成gmon.out。

查看

查看程序运行信息的命令是gprof,它以gmon.out文件作为输入,也就是将gmon.out文件翻译成可读的形式展现给用户。其命令格式如下:

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不能显示对象之间的继承关系,这也是它的弱点
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: