gprof实际上只是一个用于读取profile结果文件的工具。gprof采用混合方法来收集程序的统计信息,他使用检测方法,在编译过程当中在函数入口处插入计数器用于收集每一个函数的被调用状况和被调用次数;也使用采样方法,在运行时按必定间隔去检查程序计数器并在分析时找出程序计数器对应的函数来统计函数占用的时间。css
Gprof具备如下优缺点:多线程
1) 优势:app
a) GNU工具,人手一个;函数
b) 混合方法采集信息。工具
2) 缺点:性能
a) 须要编译选项支持:测试
i. 使用gcc/cc编译和连接时须要加入-pg选项优化
ii. 使用ld连接时须要用/lib/gcrt0.o代替crt0.o做为第一个input文件this
iii. 若是要调试libc库须要使用-lc_p代替-lc参数命令行
b) 调试多线程程序只能统计主线程的信息(因此不能用于kingbase)。
使用gcc/cc编译和连接时须要加入-pg选项
使用ld连接时须要用/lib/gcrt0.o代替crt0.o做为第一个input文件
若是要调试libc库须要使用-lc_p代替-lc参数
正常运行编译好的程序,程序正常结束后会在当前目录生成统计信息文件gmon.out。
程序必须正常退出(调用exit或从main中返回)才能生成统计信息。
当前目录下若是有另外叫gmon.out的文件,内容将被本次运行生成的统计信息覆盖,屡次运行统一程序请将前一次的gmon.out更名。
命令格式:
gprof options [executable-file [profile-data-files...]] [> outfile]
经常使用参数介绍:
symspec表示须要加入或排除的函数名,和gdb指定断点时的格式相同。
1) 输出相关:
a) -A[symspec]或--annotated-source[=symspec]:进行源码关联,只关联symspec指定的函数,不指定为所有关联。
b) -I dirs或--directory-path=dirs:添加搜索源码的文件夹,修改环境变量GPROF_PATH也能够。
c) -p[symspec]或--flat-profile[=symspec]:默认选项,输出统计信息,只统计symspec指定的函数,不指定为所有统计。
d) -P[symspec]或--no-flat-profile[=symspec]:排除统计symspec指定的函数
e) -q[symspec]或--graph[=symspec]:默认选项,输出函数调用信息,只统计symspec指定的函数,不指定为所有统计。
f) -Q[symspec]或--no-graph[=symspec]:排除统计symspec指定的函数
g) -b或--brief:不输出对各个参数含义的解释;
2) 分析相关:
a) -a或--no-static:定义为static的函数将不显示,函数的被调用次数将被计算在调用它的不是static的函数中;
b) -m num或--min-count=num:不显示被调用次数小于num的函数;
c) -z或--display-unused-functions:显示没有被调用的函数;
编译测试文件:
gcc –g –o test test.c –pg
执行程序:
./test
查看统计信息:
gprof -b -A -p -q test gmon.out > pg
% 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.
函数名
1.4 结论
咱们可使用程序概要分析快速的找到一个程序里面值得优化的地方。
1.5 原理
1.5 缺点