找回密码
 注册
关于网站域名变更的通知
查看: 792|回复: 1
打印 上一主题 下一主题

GNU性能分析工具gprof

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-4-3 09:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
) u8 p7 f$ v* P7 Q- E
gprof介绍
5 J) O) \% \: jgprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。
& i( j. i3 z3 C  t! b
- F$ p7 Z" Y7 `) b为gprof编译程序6 t; z- X8 j3 v3 S

9 E7 }4 ]: ]( [' ~1 N在编译或链接源程序的时候在编译器的命令行参数中加入“-pg”选项,编译时编译器会自动在目标代码中插入用于性能测试的代码片断,这些代码在程序在运行时采集并记录函数的调用关系和调用次数,以及采集并记录函数自身执行时间和子函数的调用时间,程序运行结束后,会在程序退出的路径下生成一个gmon.out文件。这个文件就是记录并保存下来的监控数据。可以通过命令行方式的gprof或图形化的Kprof来解读这些数据并对程序的性能进行分析。另外,如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。如果想执行一行一行的profiling,还需要加入“-g”编译参数。7 Q( n. N: {4 G+ v. D5 t
例如如下命令行:gcc -Wall -g -pg -lc_p example.c -o example
* Y: A$ }, Z: x3 U1 Q9 F) ]6 w$ M, \
Gprof基本用法:
/ t9 y+ X8 k6 i, t2 b  u  Q1. 使用 -pg 编译和链接你的应用程序。
$ e' M4 \2 J" t. J! T: y6 G
% V0 l/ N& ^6 }2 a& E2. 执行你的应用程序使之生成供gprof 分析的数据。& l9 t3 @0 d7 d+ ]9 u/ x

" b/ p! L2 t0 S# r; G3. 使用gprof 程序分析你的应用程序生成的数据。
  R; v6 q2 b  |+ i0 ?2 I- s. r+ y: X4 f+ H# i4 N9 W
$gprof -b a.out gmon.out       ' q/ d$ n- e& d1 Y8 @) k- c9 d- ^7 I
Flat profile:
/ _7 H0 F' k2 G
1 [  F% y: V9 S) f, I4 JEach sample counts as 0.01 seconds.
4 J$ L! y4 O2 Q5 Y& Vno time accumulated
+ ^* Z3 g4 c- |  c& q" O$ F8 B
; \+ e) A1 ]# N- w* b  %   cumulative   self              self     total           
( _! O& v1 e# `& w& Ltime   seconds   seconds    calls  Ts/call  Ts/call  name   
" F) [) F* a* d1 P1 T+ m0 Y  0.00      0.00     0.00        1     0.00     0.00  function
7 e0 g, S% g' q( F4 n4 E! s. C- s4 n* c" @* \
                        Call graph' M( O3 L! P! M4 O

, W9 p3 S+ ?7 u: q1 igranularity: each sample hit covers 2 byte(s) no time propagated: g# ]% i( S3 L! I6 p! d! o
/ ~  j# u+ O; X3 ?2 G$ S3 F
index % time    self  children    called     name
: D6 `9 d1 t; [* w: R7 w                0.00    0.00       1/1           main [8]" o! W* U+ g( s& o
[1]      0.0    0.00    0.00       1         function [1]
' W8 w. g5 s! F- n6 r-----------------------------------------------; G% S& K; P, f4 _6 K8 {

( i& H/ k- j0 d7 ]* L9 s% J2 LIndex by function name, _. {* |2 }1 G" Q7 k" L  N

$ p6 _+ ~9 g  w1 E# n   [1] function$ K. |/ Z* y; T( n2 g4 ~+ w' m

5 Q/ u' @4 r* b" Fgprof产生的信息+ ~0 `2 Y9 {* Y& E4 M* v6 w

' ]0 D+ t; d3 `7 u%                        the percentage of the total running time of the3 h% a0 s# Z/ C  R, N
time                     program used by this function.4 F" N+ Y9 y$ K% @- C
                           函数使用时间占所有时间的百分比。3 L+ p; y, @7 c2 Z" \
cumulative          a running sum of the number of seconds accounted2 Y* z* G4 H8 S
seconds             for by this function and those listed above it./ r5 z3 O% z9 z' u; a7 L- a
                           函数和上列函数累计执行的时间。
/ s$ P2 ]/ F( N& G7 A# qself                    the number of seconds accounted for by this/ L, _9 l2 Z4 Y7 ?; E
seconds             function alone.  This is the major sort for this
" l- c! r" L! t7 P0 g, V" `                          listing.4 V' x$ s' a# _9 [. a9 _
                          函数本身所执行的时间。% r- B, U* n% q, C5 c
calls                   the number of times this function was invoked, if
* s2 J4 S* z, y9 x- ?  X1 E                          this function is profiled, else blank.
5 j4 v1 u& o, y6 g                          函数被调用的次数% e8 f8 W1 n( |/ K5 e/ [
self                   the average number of milliseconds spent in this
1 R# Q2 x8 w. X' o. j7 ~2 O$ Gms/call               function per call, if this function is profiled,2 l5 B; x( Y' }5 I# D
                         else blank.( P& h3 `* r' c' J1 z2 n7 R9 n: D8 n- B
                          每一次调用花费在函数的时间microseconds。* ?: V6 R7 A" l! A: p' M
total                  the average number of milliseconds spent in this- ^' D  X" E- d# ^* i
ms/call               function and its descendents per call, if this
; J6 M1 ~1 n8 G4 ^                          function is profiled, else blank.
) d+ X$ Y5 l0 T                          每一次调用,花费在函数及其衍生函数的平均时间microseconds。
1 Q* m2 M& ]4 T. uname                 the name of the function.  This is the minor sort
, A$ J" |: d# J$ O                          for this listing. The index shows the location of6 J* B& o- f) h9 e" R* B
                          the function in the gprof listing. If the index is
' u5 d8 y8 `- \( A, z: Q                          in parenthesis it shows where it would appear in3 m$ [0 Y  ]9 z6 Z8 n
                          the gprof listing if it were to be printed.! ?$ D% V. H+ B! ?9 i
                          函数名3 L1 l- A5 @9 u' V! F2 l& M% n

0 D) I; ^( t, S3 J2 |命令格式3 H) z4 o- \! y2 E! d
gprof [可执行文件] [gmon.out文件] [其它参数]
& B3 J, o" X: i; b2 L7 T2 a' N5 N% C$ _% |# Q7 e
方括号中的内容可以省略。如果省略了“可执行文件”,gprof会在当前目录下搜索a.out文件作为可执行文件,而如果省略了gmon.out文件,gprof也会在当前目录下寻找gmon.out。其它参数可以控制gprof输出内容的格式等信息。最常用的参数如下:
9 z1 E! Y* |4 E% m7 P" f* Z
, z! {  B3 Q% ^: Ll -b 不再输出统计图表中每个字段的详细描述。1 D5 L4 Y% M  D! k( x2 n
2 r& G% p( b7 q* s5 j
l -p 只输出函数的调用图(Call graph的那部分信息)。
4 @/ k# R) m4 R5 M; L6 N% a9 G
4 _" u$ N1 `, C: B! Z+ zl -q 只输出函数的时间消耗列表。
7 Y4 G+ d$ a8 t* e3 e% O, o1 z6 j. x
l -e Name 不再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。
! M; {1 ?- n( e$ D( L* O" h1 U4 H- ]; P" F6 }2 T) n
l -E Name 不再输出函数Name 及其子函数的调用图,此标志类似于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。
" g& S2 n& N6 d) n) A9 P! t/ S. u& z  W
l -f Name 输出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。
/ X' [8 @% ^; I7 |  |$ W2 o* c$ o/ p+ h( F6 ~5 p1 o/ d/ O7 G9 L
l -F Name 输出函数Name 及其子函数的调用图,它类似于 -f 标志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。7 z- {+ ?2 U" B0 S  P+ S/ W6 f

! j+ z6 B& [9 a" f# t! v; Al -z 显示使用次数为零的例程(按照调用计数和累积时间计算)。- |) V* i' i/ x9 W3 n, N+ r8 T

8 G8 W6 F$ C8 H) a+ p不过,gprof不能显示对象之间的继承关系,这也是它的弱点.

该用户从未签到

2#
发表于 2020-4-3 18:25 | 只看该作者
GNU性能分析工具gprof
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-26 02:59 , Processed in 0.140625 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表