Chinaunix首页 | 论坛 | 博客
  • 博客访问: 374716
  • 博文数量: 124
  • 博客积分: 2911
  • 博客等级: 少校
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 15:57
文章分类

全部博文(124)

文章存档

2012年(6)

2011年(26)

2010年(92)

我的朋友

分类:

2010-10-25 18:57:26

方法1:显示程序每一个函数占用的时间:编译时加上参数 -g -pg , 执行后会在当前目录生成gmon.out,然后执行shell命令gprof > lima,就会在当前目录生成lima的文件。把该文件上传到自己机子上用UE看,该文件大致分2块:前面是函数之间的调用层次关系; 后面是函数花费的时间的比例,是一个按占用百分比排序的。
方法2:使用gdb等调试工具,一个一个部分的往下走,走完每个部分,用ps -efo ruser,pid,ppid,comm,rssize | grep something 来看这时候程序占用的内存。如果走到某一步后,内存突然变的很大,基本就是那部分吃内存了。


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 BB-DATA [YET-MORE-PROFILE-DATA-FILES...] [> OUTFILE]

gprof产生的信息
 %                        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.
                          每一次调用花费在函数的时间microseconds。
 total                  the average number of milliseconds spent in this
ms/call               function and its descendents per call, if this
                          function is profiled, else blank.
                          每一次调用,花费在函数及其衍生函数的平均时间microseconds。
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.
                          函数名
阅读(2851) | 评论(0) | 转发(0) |
0

上一篇:Mass_Data_Process_II

下一篇:POSIX.1 ~ ISOC

给主人留下些什么吧!~~