收藏
0有用+1
0
用于帮助程序员找出众多函数中耗时最多的函数的工具
gprof,打印出程序运行中各个函数消耗的时间,可以帮助程序员找出众多函数中耗时最多的函数。
中文名
gprof
外文名
gprof
优    点
GNU工具,人手一个
类    别
工具

简介

播报
编辑
产生程序运乌驼欢寻汗市行宙趋寒乎时候的函数调用背兆关系,包括调用符承次数,可以帮助程序员分蜜凝叠析程序的运行流程。
有了函数的调用关系,这会让开发人员大大提高工作效率,不用费心地去一点点找出程序的运行流程,这对小程序朽己糊来说可能效果不是很明显,但对于有几万,几十万代码量的工程来说,效率是毋庸置疑的!而且这个功能对于维护旧代码或者是分析Open Source来说那是相当诱人的,有了调用图,对程序的运行框架也就有了一个大体了解誉体腿,知道了程序的"骨架",分析它也就不会再那么茫然,尤其是对自己不熟悉的代码和Open Source。费话不多说了,让我们开始我们的分析之旅吧!

优缺点

播报
编辑
Gprof具有以下优缺点:
(1) 优点:
(a) GNU工具,人手一个;
(b) 混合方法采集信息。
(2) 缺点:
(a) 需要编译选项支持:
i. 使用gcc/cc编译和链接时需要加入-pg选项
ii. 使用ld链接时需要用/lib/gcrt0.o代替crt0.o作为第一个input文件
iii. 如果要调试libc库需要使用-lc_p代替-lc参数
(b) 调试多线程程序只能统计主线程的信息(所以不能用于kingbase)。

原理

播报
编辑
通过在编译和链接你的程序的时候(使用 -pg 编译和链接选项),gcc 在你应用程序的每个函数中都加入了一个名为mcount ( or "_mcount" , or "__mcount" , 依赖于编译器或操作系统)的函数,也就是说你的应用程序里的每一个函数都会调用mcount, 而mcount 会在内存中保存一张函数调用图,并通过函数调用堆栈的形式查找子函数和父函数的地址。这张调用图也保存了所有与函数相关的调用时间,调用次数等等的所有信息。

命令

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

示例

播报
编辑
Test.c
#include <stdio.h>
int prime(int n)
{
int i;
for (i=2; i<n; i++)
{
if (n%i == 0)
return 0;
return 1;
}
}
void main(void)
{
int i, n;
n = 1000;
for (i=2; i<=n; i++)
{
if (prime(i))
printf("%d\n", i);
}
}
Gcc -pg -o test test.c
./test
gprof -b test gmon.out |less
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
100.00 0.02 0.02 main
0.00 0.02 0.00 999 0.00 0.00 prime
^L
Call graph
granularity: each sample hit covers 4 byte(s) for 50.00% of 0.02 seconds
index % time self children called name
<spontaneous>
[1] 100.0 0.02 0.00 main [1]
0.00 0.00 999/999 prime [2]
-----------------------------------------------
0.00 0.00 999/999 main [1]
[2] 0.0 0.00 0.00 999 prime [2]
-----------------------------------------------
^L
Index by function name
[1] main [2] prime

注意

播报
编辑
(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文件。

信息

播报
编辑
% 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.
每一次调用花费在函数的时间(毫秒)。
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.
函数名