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

GNU性能分析工具gprof

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
$ j' H" r6 g- @6 M4 T
gprof介绍3 a& O6 y* k/ D& Y0 A
gprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。
' _& V$ Z6 l% x+ K. V* H
+ O$ B# x$ _2 k/ L* ?  A! Q为gprof编译程序$ Z3 ]. O; j  ^8 w6 g, P

2 |3 O- f2 {% F/ K. U在编译或链接源程序的时候在编译器的命令行参数中加入“-pg”选项,编译时编译器会自动在目标代码中插入用于性能测试的代码片断,这些代码在程序在运行时采集并记录函数的调用关系和调用次数,以及采集并记录函数自身执行时间和子函数的调用时间,程序运行结束后,会在程序退出的路径下生成一个gmon.out文件。这个文件就是记录并保存下来的监控数据。可以通过命令行方式的gprof或图形化的Kprof来解读这些数据并对程序的性能进行分析。另外,如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。如果想执行一行一行的profiling,还需要加入“-g”编译参数。
  x5 O0 t. M5 T# p! {( n例如如下命令行:gcc -Wall -g -pg -lc_p example.c -o example
5 D" ]# t( [! N7 c2 S: P8 m7 g, t( z8 [' {& a' u+ i- D) _
Gprof基本用法:
# ~5 `: Y' x. `1. 使用 -pg 编译和链接你的应用程序。
* G. L8 H, y* L% h. i2 m) O
9 c( j$ Y& V! M) s/ A1 u2. 执行你的应用程序使之生成供gprof 分析的数据。
/ Q3 c/ M8 c: X* J3 g" Z% ~, i4 e$ V8 M2 c! f2 B
3. 使用gprof 程序分析你的应用程序生成的数据。; }, ]7 f2 V1 O$ S1 D; @0 m7 N5 s: k: f

4 A  S1 p. d& j4 R: ?$gprof -b a.out gmon.out      
0 Q  S" `5 Y% f5 v, zFlat profile:; ?; f- [6 D# S
  L: j+ J8 ?) o- P( u" |2 s5 A
Each sample counts as 0.01 seconds.
; a( R" c5 g$ K  j2 p. @) nno time accumulated
/ s, F/ V* P2 t
# P* [0 W9 X- ~/ v  %   cumulative   self              self     total           - x0 K- i  a6 J$ ~  C4 ~; D* T
time   seconds   seconds    calls  Ts/call  Ts/call  name    ) o+ u; ^+ G- b: }, X2 [
  0.00      0.00     0.00        1     0.00     0.00  function
7 E2 f  Z: ]6 @$ Y# ~2 I! Z. x0 G' e7 P- O7 e& `8 j; J6 ^0 W0 |- M
                        Call graph
" A2 v; C# ?/ H. G) L' f) A2 f, D8 T0 ~
granularity: each sample hit covers 2 byte(s) no time propagated' a) C3 W! v& l' m

* ^  K; j$ B/ `& N# S+ L* xindex % time    self  children    called     name
2 i  c0 m2 z: E7 _# i6 C+ s                0.00    0.00       1/1           main [8]8 D# O0 S' |% e3 T. ~
[1]      0.0    0.00    0.00       1         function [1]
8 S( c2 ~9 A! }* l-----------------------------------------------  P$ S8 q! k% s& L$ @9 M  g
% q9 L6 I1 o: p' G, m
Index by function name9 h) N+ D$ _9 d4 @) Q

1 V2 U/ O4 b5 D7 ~6 `# v+ [   [1] function
  V6 S" v2 r7 }
7 ?  [6 A- C( ~3 \) Q- l3 egprof产生的信息) ]1 q0 e/ F+ A- C5 u9 r9 }0 Z1 {

3 D6 ?0 O$ w! N  k+ O) d2 @- G%                        the percentage of the total running time of the! U7 X5 P1 P) u! g0 U
time                     program used by this function.
3 j* B! X+ [9 c  o- R/ N1 q                           函数使用时间占所有时间的百分比。1 k6 n( M* a. h1 i& H
cumulative          a running sum of the number of seconds accounted4 o  ?% j' ^3 N* Z" q
seconds             for by this function and those listed above it.. A- a) `1 R. V6 [% ^( }' ?
                           函数和上列函数累计执行的时间。
: V  U7 c3 u. \self                    the number of seconds accounted for by this
4 v. |" e/ t" E) U( k- ^% w5 Gseconds             function alone.  This is the major sort for this
$ M% d; q8 J0 Z' D                          listing.: a( M1 ?4 Y- `% p. L4 ~
                          函数本身所执行的时间。: Y& e3 I) }! s/ B: u1 H/ A
calls                   the number of times this function was invoked, if# s) E/ F0 r  \* A$ ]. j, S
                          this function is profiled, else blank.
' [2 e( _# X+ \4 o$ T' H3 q/ J2 r                          函数被调用的次数" A3 I4 b2 K7 Z0 j8 E1 u
self                   the average number of milliseconds spent in this3 B: C& H' ~. }% w6 r0 J
ms/call               function per call, if this function is profiled,
1 [/ e2 y( X( Z: O2 L                         else blank.
  J% s3 o- ^& l' N                          每一次调用花费在函数的时间microseconds。4 D5 K. D# m) c4 g+ R
total                  the average number of milliseconds spent in this
3 m# F2 W$ l" u& ^: q7 g: t2 ^3 _6 H- }ms/call               function and its descendents per call, if this2 [! k9 g2 o# l& u
                          function is profiled, else blank.
% v5 J- B4 q  e6 v+ [                          每一次调用,花费在函数及其衍生函数的平均时间microseconds。
( Y' @5 w6 `% G8 g% vname                 the name of the function.  This is the minor sort
% M. N. }0 H& q! z/ k                          for this listing. The index shows the location of8 W4 J9 W! i% |: S6 l( a0 `4 O! ^
                          the function in the gprof listing. If the index is: n. [6 B0 R  O1 E1 v9 |3 I
                          in parenthesis it shows where it would appear in
7 l9 N$ }) {$ B/ Z2 U& }: v4 U                          the gprof listing if it were to be printed.5 z2 }; I9 m: T
                          函数名
+ c' I5 [4 h  _4 K' K' @' S4 Z% H- H4 Q* O3 r2 H7 M+ r
命令格式
' i6 R+ m: _0 @7 q- Zgprof [可执行文件] [gmon.out文件] [其它参数]
  Z: }* O- H7 n9 b4 Z
+ D6 D/ E" ^7 R1 a方括号中的内容可以省略。如果省略了“可执行文件”,gprof会在当前目录下搜索a.out文件作为可执行文件,而如果省略了gmon.out文件,gprof也会在当前目录下寻找gmon.out。其它参数可以控制gprof输出内容的格式等信息。最常用的参数如下:% E, k( u5 A/ E& Z
( g. v6 k2 g! E" F3 Z; s0 `
l -b 不再输出统计图表中每个字段的详细描述。
5 @; l* P" h2 y" S2 E4 Y# G2 \& s
6 A# n' \6 O' J" }l -p 只输出函数的调用图(Call graph的那部分信息)。
- C% [% u- W+ W( N! r
" x8 S( c2 W7 r& V# hl -q 只输出函数的时间消耗列表。
& o, \9 [# X* A: e  N
( n" k1 T, t5 V1 k* j( o, jl -e Name 不再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。
+ @6 \7 j/ Q$ @
. j9 g" l+ D% Yl -E Name 不再输出函数Name 及其子函数的调用图,此标志类似于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。
$ b, U1 \  Z/ E; Z$ @( O- ^4 j6 L3 C0 P& g1 q
l -f Name 输出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。
& C% M# H! M0 u5 U; j3 t( s( @% [7 j. }. x& w, v& n8 K
l -F Name 输出函数Name 及其子函数的调用图,它类似于 -f 标志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。
" |# _2 B1 @  e- \2 t6 L* J/ k' e! E! c; y3 J
l -z 显示使用次数为零的例程(按照调用计数和累积时间计算)。# i$ @- D0 t! L. B

+ E7 P- g3 b4 A7 n3 A不过,gprof不能显示对象之间的继承关系,这也是它的弱点.

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-11-25 21:15 , Processed in 0.156250 second(s), 24 queries , Gzip On.

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

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

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