|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
3 O8 ]; { z% | B" v, c9 B" Ugprof介绍
7 R! Y5 D4 \- X7 S* H$ Rgprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。
' x, R- [8 z9 k: R3 h! a
; r9 Y, }8 j5 J: V5 `为gprof编译程序$ Y) Q; H' X" j0 x" d2 `# n
* I$ x! u% P C3 }% H7 ?. t0 Q- y S/ R
在编译或链接源程序的时候在编译器的命令行参数中加入“-pg”选项,编译时编译器会自动在目标代码中插入用于性能测试的代码片断,这些代码在程序在运行时采集并记录函数的调用关系和调用次数,以及采集并记录函数自身执行时间和子函数的调用时间,程序运行结束后,会在程序退出的路径下生成一个gmon.out文件。这个文件就是记录并保存下来的监控数据。可以通过命令行方式的gprof或图形化的Kprof来解读这些数据并对程序的性能进行分析。另外,如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。如果想执行一行一行的profiling,还需要加入“-g”编译参数。
5 J! `7 T: `! R5 o% m1 h' I例如如下命令行:gcc -Wall -g -pg -lc_p example.c -o example4 }2 w4 I. T; X
$ [3 [2 }% N0 GGprof基本用法:
: N% f$ R5 t+ C. Z/ p1. 使用 -pg 编译和链接你的应用程序。 ~6 I! @+ Q* B+ F4 `8 S b8 m
, D) k0 D8 V4 n2 P" b# _# Z/ ?2. 执行你的应用程序使之生成供gprof 分析的数据。
" J1 ?0 L# |" k* z# c/ r* r! ]! {3 j- A1 s. h0 v/ P& L
3. 使用gprof 程序分析你的应用程序生成的数据。) t8 f5 v$ t" O' y' M
, X- G3 n9 w' a0 J, U) d8 q$gprof -b a.out gmon.out 3 ^+ {+ d8 i+ R4 E+ P+ p) u0 Q
Flat profile:1 ], G- I* ~, K8 C8 L ~- x0 O7 S
9 A# Y q' M) w) y' z* v
Each sample counts as 0.01 seconds.5 r1 C3 l# [- b, [0 J; v
no time accumulated, u3 z! r0 r$ W4 J: B- n
9 B& z1 r" o/ u3 s
% cumulative self self total
% }$ y% R; ^" m% d% H, Ytime seconds seconds calls Ts/call Ts/call name 4 C$ h/ A) Z# a8 V/ b! _
0.00 0.00 0.00 1 0.00 0.00 function
% ~+ J/ R5 \! G0 [: T1 v, y, i8 |8 N' b! j- y* h
Call graph1 y/ x2 s6 |" J7 l
! a7 J- F. \3 z! H; R
granularity: each sample hit covers 2 byte(s) no time propagated7 `8 b# a* N% g" u2 C) M, ~, w+ p
5 i' v9 |- V2 C! n( S; \, z5 G
index % time self children called name6 i1 L. Z4 I v. m6 g, O* f0 s
0.00 0.00 1/1 main [8], y( D; N2 u L# g0 ~; d0 v) f) B
[1] 0.0 0.00 0.00 1 function [1]
/ K! y* Z, W4 w, F$ [-----------------------------------------------) a# m' F) f5 c9 V5 U" m1 x
0 O% b, F% ]6 c2 gIndex by function name/ B" V5 K) h9 |# _$ X/ Z( K' r
0 K* d) W& D% _- d) } [1] function
2 I/ W* L- w, i. Q6 }/ H" [7 s# e; k. c' g+ B; ?/ x/ u
gprof产生的信息! x* q7 s, E( T$ k q7 j9 Z
, X$ U0 |6 ^8 a: H; K2 L
% the percentage of the total running time of the
2 U5 g1 q9 \3 k0 Z3 @" h R4 c( xtime program used by this function.$ F p& B! I, [, c @' Q" S2 ?- ^+ ?
函数使用时间占所有时间的百分比。
! \$ y/ S+ P2 Q- n# o) r; Ecumulative a running sum of the number of seconds accounted
+ h0 }* v+ c4 n3 Useconds for by this function and those listed above it.- M }0 X: r+ P$ _' T
函数和上列函数累计执行的时间。+ }% p4 @- t( i# V2 g
self the number of seconds accounted for by this
+ n' v; _! k/ o+ Eseconds function alone. This is the major sort for this
* a3 z! @+ r7 _4 G8 O, o: [ listing. Q w" X; [1 i: t4 F8 Z
函数本身所执行的时间。5 }9 m' ]5 X/ j# u K
calls the number of times this function was invoked, if5 {" b4 x; Z7 X
this function is profiled, else blank.
! O1 v" ?- o) o! j# W% ^* c+ e8 D 函数被调用的次数4 G+ m3 C2 }* R! Y7 f
self the average number of milliseconds spent in this
" _. D6 ] [. `* y% Rms/call function per call, if this function is profiled,$ l8 |! v) p3 D: G
else blank.* d; o6 _+ R- d3 l5 ^
每一次调用花费在函数的时间microseconds。/ c- N8 D n% |- F5 O- V9 W& U
total the average number of milliseconds spent in this0 S* d2 `" [& D
ms/call function and its descendents per call, if this/ B5 e3 s1 t( B1 b; G- ]# V/ h7 u
function is profiled, else blank.$ B, a. |5 C3 S; l
每一次调用,花费在函数及其衍生函数的平均时间microseconds。1 i0 X- ~# }0 J# J" u# u
name the name of the function. This is the minor sort
, }2 A/ ~% w8 C6 b for this listing. The index shows the location of
% q6 f* H! I3 V2 A. D8 R( |; @$ } the function in the gprof listing. If the index is7 o8 L- `/ G0 N' \# {* H
in parenthesis it shows where it would appear in
+ B0 V$ x* N W6 g! ^ the gprof listing if it were to be printed.! c5 X0 \& S0 B& H8 o% r
函数名6 H5 N( N. v0 r8 G3 v: X1 M
% y* C0 M0 w. G4 C1 U8 u6 n( B, X命令格式
. r2 T( D, \& O6 ?2 Y+ igprof [可执行文件] [gmon.out文件] [其它参数]
: W. _ D8 H$ L
4 C' y0 A! w; c- ]方括号中的内容可以省略。如果省略了“可执行文件”,gprof会在当前目录下搜索a.out文件作为可执行文件,而如果省略了gmon.out文件,gprof也会在当前目录下寻找gmon.out。其它参数可以控制gprof输出内容的格式等信息。最常用的参数如下:( d9 H) V0 t( B+ E1 {$ x% ^
9 i8 v( e* L6 a7 q! tl -b 不再输出统计图表中每个字段的详细描述。! E/ g& q. i" {3 C4 ^9 X
8 g# F& m4 [ G6 Z7 {/ _+ L, F
l -p 只输出函数的调用图(Call graph的那部分信息)。
* E H7 E2 O/ R4 T8 i6 ]2 a; O$ n7 w5 E+ F- F* r$ G5 C7 q
l -q 只输出函数的时间消耗列表。
* T) s ?9 p8 U( Q7 U8 Y$ H7 u1 ?: J4 d! E; F' t3 ?- t
l -e Name 不再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。8 h" `! A( B! e6 A1 ^2 K
/ q$ F5 ^$ t/ W0 O6 X% Q1 Y0 @. Q/ z
l -E Name 不再输出函数Name 及其子函数的调用图,此标志类似于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。
+ K# ]% `& f: T4 a [; u8 \! d' \& u7 P, M2 z
l -f Name 输出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。
& e1 L. ~" {+ F- w: v) |0 F& l# N: t
l -F Name 输出函数Name 及其子函数的调用图,它类似于 -f 标志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。
2 u0 K9 C6 {2 g& [6 B9 K
1 @5 U* w$ |0 _1 ~/ W" V- dl -z 显示使用次数为零的例程(按照调用计数和累积时间计算)。
1 O0 N8 q* T# G! i/ C5 a$ l. B$ q7 S* ?% L, ?% c9 d
不过,gprof不能显示对象之间的继承关系,这也是它的弱点. |
|