您的位置:首页 > 运维架构 > Linux

Linux 平台上的 Oprofile 性能分析工具(二)

2010-09-10 17:35 621 查看
如果设置如下:
root@wls-desktop:~/TestAMRDec# opcontrol –vmlinux=/src/urc/linux-source-2.6.27/vmlinux
(图略)



kprof介绍



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
执行如下命令行,即可执行gprof:
gprof OPTIONS EXECUTABLE-FILE gmon.out BB-DATA [YET-MORE-PROFILE-DATA-FILES...] [> OUTFILE]
-L指定库的路径,-l指定库的名字 libxml2 可以这样写-lxm2

头文件的路径用-I,库文件路径用-L,比如说-I/usr/myinclude -L/usr/mylib
但是我看nslookup.c应该是一个完整的工程文件包的其中一个文件吧?你光找到这么一个文件估计是不能编译的,一定要有其它的文件支持才行。

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

Call graph (explanation follows)

granularity: each sample hit covers 4 byte(s) for 33.33% of 0.03 seconds

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.

一开始./configure时,我加入了--enable-debug --disable-strip --enable-gprof
结果还是不行,出现上面一样的提示(不知道是不是因为的ffmpeg版本不对)。

于是,我阅读了一下Makefile,发现即使加入了--disable-strip,生成的ffmpeg依然被strip了
然后我把Makefile中对ffmpeg的strip命令去掉,然后重新make,一切就OK了

现在,综合大家的经验总结下,如果要使用gprof进行分析,必须要在gcc 的编译选项中加入 -p -g (-g我是推测的,不知道是否必须,望指正),然后对生成的目标文件不能进行strip。

我在使用的时候只是加了—disable-strip这个开关,并且在所有的Makefile文件里加入了-pg的选项,就可以了。
gcov:

通过gcov+GCC,我们可以计算每行代码的运行次数,并标记出没有被执行的代码。

使用gcov时,需要打开GCC的fprofile-arcs和ftest-coverage两个选项。它们会在编译时加入一些 gcov所需的附加信息。其中,gcov中常用的选项有-b分支统计信息。与gcov类似的商业软件还有Bullseye Coverage和CTC++ Test Coverage Analyser等。

示例:我们有一个源程序temp.cpp
1)打开gcc相应编译选项,编译该文件
g++ -g temp.cpp -o hello -fprofile-arcs -ftest-coverage
它将生成一个.gcno文件
2)运行生成的可执行文件
./hello
它将生成一个.gcda文件
3)运行gcov
gcov temp.cpp
它将生成我们所需的.cpp.gcov报告文件。

参考: http://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_8.html http://ftp.gnu.org/pub/old-gnu/Manuals/gprof-2.9.1/
http://www.ibm.com/developerworks/cn/linux/l-gnuprof.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: