|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
, ?7 c/ @8 P/ ?( {gprof介绍
7 D9 D6 i3 g# N8 Y7 r( D% H3 x4 Ogprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。
' C) I- C. T+ X8 ]! u
: n4 U1 B8 F; ]* `3 Y为gprof编译程序0 B; O0 p6 F4 x: O( Q: R
. d3 n& U' m6 K; U) l
在编译或链接源程序的时候在编译器的命令行参数中加入“-pg”选项,编译时编译器会自动在目标代码中插入用于性能测试的代码片断,这些代码在程序在运行时采集并记录函数的调用关系和调用次数,以及采集并记录函数自身执行时间和子函数的调用时间,程序运行结束后,会在程序退出的路径下生成一个gmon.out文件。这个文件就是记录并保存下来的监控数据。可以通过命令行方式的gprof或图形化的Kprof来解读这些数据并对程序的性能进行分析。另外,如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。如果想执行一行一行的profiling,还需要加入“-g”编译参数。
+ _6 x6 r% s$ [% p7 _6 w j例如如下命令行:gcc -Wall -g -pg -lc_p example.c -o example
y! Y. ~$ T2 B# I3 } h+ j$ @* V! U! |+ _! \+ a
Gprof基本用法:
6 l& W( |9 y( m7 @$ \9 x( v/ P1. 使用 -pg 编译和链接你的应用程序。
$ U0 z: i7 n! A, ~1 b
3 h u2 P8 h# F2. 执行你的应用程序使之生成供gprof 分析的数据。
6 D5 t4 Q: ~ V2 f P7 T
8 f0 H1 ~! B+ S6 Q( m3. 使用gprof 程序分析你的应用程序生成的数据。6 i5 c$ K5 a* ^) {* e
8 Y; c1 Z/ @3 X0 _" k3 s9 u, i$gprof -b a.out gmon.out
6 Q! W3 L5 w2 j+ o2 e! XFlat profile:
2 ~# M* ]) v" p* j
3 [& e' V9 S ^Each sample counts as 0.01 seconds.
1 J7 m4 P: u2 }8 Q& E9 ano time accumulated
' i* D5 r% k( W6 \, t5 l8 r {$ \
+ y) D. @- r) V0 t7 w Z % cumulative self self total
' d3 \3 ?# `. G9 X/ ltime seconds seconds calls Ts/call Ts/call name
0 `+ \$ t: V% g# f% b& V# u0 b 0.00 0.00 0.00 1 0.00 0.00 function
* M# p! c1 r) G5 b, A" q x. T, ?4 \( h0 a
Call graph) c0 r2 v- t/ o; T% n: d
6 j; z* `( Y' l/ J
granularity: each sample hit covers 2 byte(s) no time propagated
% U1 S/ B' h# L/ }9 b3 O& y9 g9 I; f6 o
index % time self children called name6 o/ ~7 y( {5 l, f/ [' v
0.00 0.00 1/1 main [8]
1 s( T0 q: \9 b/ b0 h- K2 @[1] 0.0 0.00 0.00 1 function [1]/ J2 q, e0 P+ j! p. Q/ C9 T& y
-----------------------------------------------
* T2 R+ c$ d e
& b% H" v4 e: X* {& n5 M7 S/ RIndex by function name
% M. ]8 ~4 E2 }# j7 h- x z$ l
" |) s2 O3 u5 @' N7 y0 J" W [1] function
) i: \ K6 t% |& B9 Z4 I2 s- m6 U y# Y; ^/ }7 p
gprof产生的信息
) ^# q5 r+ ?3 X- Z( W# Q& |8 b8 A3 T4 Q
% the percentage of the total running time of the" } S2 c% q" B: ?+ \9 Y
time program used by this function.4 @' K% `. `4 p0 r. l+ I& m! \7 U
函数使用时间占所有时间的百分比。
( n2 ~0 j2 X: V/ Q$ Gcumulative a running sum of the number of seconds accounted
' G$ z8 H Z# ]0 f' B* o# r' T" }seconds for by this function and those listed above it.
5 B3 x0 U5 b# ]4 m8 I7 m, N/ F& b8 U 函数和上列函数累计执行的时间。& Y- \. U) r8 Y8 G1 u
self the number of seconds accounted for by this
9 w( d! i( f' \" dseconds function alone. This is the major sort for this, Q0 t- z7 k" y1 V# c( {# `! U
listing.
! @' p" N7 h. e8 j, G8 M 函数本身所执行的时间。
! n1 J/ A/ ^* e8 c$ ]calls the number of times this function was invoked, if* X( x0 l( e4 E+ j2 D5 {# p
this function is profiled, else blank.
! T1 b( {5 C S/ I! p+ ~/ T 函数被调用的次数
+ j& D/ p, w9 i0 ]self the average number of milliseconds spent in this
, l% }! ?& J h% J5 Xms/call function per call, if this function is profiled," m% J, G3 b9 c* {
else blank.
- w$ D0 |* F8 ~6 b' w! N- A 每一次调用花费在函数的时间microseconds。
8 F; a) ~$ m/ x/ s+ E' wtotal the average number of milliseconds spent in this3 C6 f5 R* i8 }* s$ _
ms/call function and its descendents per call, if this
! N; D0 D# `- D function is profiled, else blank.& b/ y3 ?% g c2 j' h4 H4 Y
每一次调用,花费在函数及其衍生函数的平均时间microseconds。! h: v1 P6 r8 S2 j/ ]
name the name of the function. This is the minor sort
' ]. K& D' t4 @- h; e# S% n for this listing. The index shows the location of
$ o s6 \: r$ T0 F' ^' _ the function in the gprof listing. If the index is! t) b. X, q/ j
in parenthesis it shows where it would appear in
( l& i; ^) T" e4 @* G the gprof listing if it were to be printed.+ @$ J- G7 ]% s6 b x9 a
函数名4 S6 X5 ^& h" U9 g0 l
, t8 O- t( J, t0 t5 R( M命令格式; E# L$ O* a8 K! k( |4 a% j0 a( w1 X
gprof [可执行文件] [gmon.out文件] [其它参数]" T/ ]: D7 ~6 w; u3 y1 X* T; k' _
! g" r# u+ S$ H! ~" b/ n$ j
方括号中的内容可以省略。如果省略了“可执行文件”,gprof会在当前目录下搜索a.out文件作为可执行文件,而如果省略了gmon.out文件,gprof也会在当前目录下寻找gmon.out。其它参数可以控制gprof输出内容的格式等信息。最常用的参数如下:
: B6 I% v& m# P3 y2 Z; {6 f
8 }/ b$ i# I4 G: ]1 S9 d) bl -b 不再输出统计图表中每个字段的详细描述。
3 w) t: {& A: A, L5 r+ o
, k9 p$ w0 R6 O/ j: J; c8 g4 nl -p 只输出函数的调用图(Call graph的那部分信息)。
8 \& l5 z5 S- j r9 p6 z3 e# b7 d7 k
( f) q5 q5 q! J& ~) M4 F6 al -q 只输出函数的时间消耗列表。7 r: ?6 r$ H% f0 M, w) s
" B- e/ {8 x/ @l -e Name 不再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。
5 N7 l3 P/ S- G% o6 L
F) w' E* c7 f) T' nl -E Name 不再输出函数Name 及其子函数的调用图,此标志类似于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。0 {+ o: m. }; W8 A% i( G( t
/ Q9 a! }, \7 B m, h% Fl -f Name 输出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。" x- i% O3 m& W) X' c. z
/ I1 t# p8 P2 {, X3 _$ ^6 I Wl -F Name 输出函数Name 及其子函数的调用图,它类似于 -f 标志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。
! P$ J3 K7 s, ^$ U1 B6 [0 `! p; c r
5 z, k2 e' Y* |0 ?l -z 显示使用次数为零的例程(按照调用计数和累积时间计算)。
! M" I- G# n2 {6 J* d3 H/ j; J& n4 ]# c2 B0 h; E
不过,gprof不能显示对象之间的继承关系,这也是它的弱点. |
|