Linux认证 百分网手机站

Linux下程序的Profile工具

时间:2018-05-08 18:04:36 Linux认证 我要投稿

Linux下程序的Profile工具

  我们在写嵌入式程序时,通常需要对程序的性能进行分析,以便程序能够更快更好地运行,达到实时(real-time)的目的。如果程序很大,分析起来就很困难。如果有个工具能够自动进行程序的性能分析,那就最好了。这里介绍一种Linux下程序的Profiling工具----GNU profiler。

  gprof的基本用法:

  1. 使用 -pg 选项编译和链接你的应用程序

  在gcc编译程序的时候,加上-pg选项,例如:

  gcc -pg -o test test.c

  这样就生成了可执行文件test。如果是大项目,就在makefile里面修改编译选项,-pg放在那里都行。

  2. 执行你的应用程序使之生成供gprof 分析的数据

  运行刚才的程序:./test,这样就生成了一个gmon.out文件,该文件就包含了profiling的数据。

  3. 使用gprof 分析你的应用程序生成的数据

  gprof test gmon.out > profile.txt

  使用上面的命令,gprof就可以分析程序test的性能,将profiling的结果放在profile.txt文件中,打开就可以看到分析的结果。通过对结果的分析来改进我们的程序,从而达到我们的目的。

  GNU gprof是个很不错的工具,大家写程序时可以多用用。我现在用gprof来profiling我的程序,把耗时最多的函数或运算找出来,用FPGA芯片实现,从而达到real-time的目的。

  为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 gmon.out 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.

  函数名

  prof 实现原理:

  通过在编译和链接你的.程序的时候(使用 -pg 编译和链接选项),gcc 在你应用程序的每个函数中都加入了一个名为mcount ( or “_mcount” , or “__mcount” , 依赖于编译器或操作系统)的函数,也就是说你的应用程序里的每一个函数都会调用mcount, 而mcount 会在内存中保存一张函数调用图,并通过函数调用堆栈的形式查找子函数和父函数的地址。这张调用图也保存了所有与函数相关的调用时间、调用次数等等的所有信息。

  Gprof 简单使用:

  让我们简单的举个例子来看看Gprof是如何使用的。

  1.打开linux终端。新建一个test.c文件,并生用-pg 编译和链接该文件。

  test.c 文件内容如下:

  引文:

  #include "stdio.h"

  #include "stdlib.h"

  void a(){

  printf("\t\t+---call a() function\n");

  }

  void c(){

  printf("\t\t+---call c() function\n");

  }

  int b() {

  printf("\t+--- call b() function\n");

  a();

  c();

  return 0;

  }

  int main(){

  printf(" main() function()\n");

  b();