亚洲国产日韩欧美在线a乱码,国产精品路线1路线2路线,亚洲视频一区,精品国产自,www狠狠,国产情侣激情在线视频免费看,亚洲成年网站在线观看

Linux下程序的Profile工具

時(shí)間:2023-01-24 02:00:05 Linux認(rèn)證 我要投稿
  • 相關(guān)推薦

Linux下程序的Profile工具

  我們在寫嵌入式程序時(shí),通常需要對程序的性能進(jìn)行分析,以便程序能夠更快更好地運(yùn)行,達(dá)到實(shí)時(shí)(real-time)的目的。如果程序很大,分析起來就很困難。如果有個工具能夠自動進(jìn)行程序的性能分析,那就最好了。這里介紹一種Linux下程序的Profiling工具----GNU profiler。

  gprof的基本用法:

  1. 使用 -pg 選項(xiàng)編譯和鏈接你的應(yīng)用程序

  在gcc編譯程序的時(shí)候,加上-pg選項(xiàng),例如:

  gcc -pg -o test test.c

  這樣就生成了可執(zhí)行文件test。如果是大項(xiàng)目,就在makefile里面修改編譯選項(xiàng),-pg放在那里都行。

  2. 執(zhí)行你的應(yīng)用程序使之生成供gprof 分析的數(shù)據(jù)

  運(yùn)行剛才的程序:./test,這樣就生成了一個gmon.out文件,該文件就包含了profiling的數(shù)據(jù)。

  3. 使用gprof 分析你的應(yīng)用程序生成的數(shù)據(jù)

  gprof test gmon.out > profile.txt

  使用上面的命令,gprof就可以分析程序test的性能,將profiling的結(jié)果放在profile.txt文件中,打開就可以看到分析的結(jié)果。通過對結(jié)果的分析來改進(jìn)我們的程序,從而達(dá)到我們的目的。

  GNU gprof是個很不錯的工具,大家寫程序時(shí)可以多用用。我現(xiàn)在用gprof來profiling我的程序,把耗時(shí)最多的函數(shù)或運(yùn)算找出來,用FPGA芯片實(shí)現(xiàn),從而達(dá)到real-time的目的。

  為gprof編譯程序

  在編譯或鏈接源程序的時(shí)候在編譯器的命令行參數(shù)中加入“-pg”選項(xiàng),編譯時(shí)編譯器會自動在目標(biāo)代碼中插入用于性能測試的代碼片斷,這些代碼在程序在運(yùn)行時(shí)采集并記錄函數(shù)的調(diào)用關(guān)系和調(diào)用次數(shù),以及采集并記錄函數(shù)自身執(zhí)行時(shí)間和子函數(shù)的調(diào)用時(shí)間,程序運(yùn)行結(jié)束后,會在程序退出的路徑下生成一個gmon.out文件。這個文件就是記錄并保存下來的監(jiān)控?cái)?shù)據(jù)?梢酝ㄟ^命令行方式的gprof或圖形化的Kprof來解讀這些數(shù)據(jù)并對程序的性能進(jìn)行分析。另外,如果想查看庫函數(shù)的profiling,需要在編譯是再加入“-lc_p”編譯參數(shù)代替“-lc”編譯參數(shù),這樣程序會鏈接libc_p.a庫,才可以產(chǎn)生庫函數(shù)的profiling信息。如果想執(zhí)行一行一行的profiling,還需要加入“-g”編譯參數(shù)。

  例如如下命令行:

  gcc -Wall -g -pg -lc_p example.c -o example

  執(zhí)行g(shù)prof

  執(zhí)行如下命令行,即可執(zhí)行g(shù)prof:

  gprof OPTIONS EXECUTABLE-FILE gmon.out BB-DATA [YET-MORE-PROFILE-DATA -FILES...] [> OUTFILE]

  gprof產(chǎn)生的信息

  % the percentage of the total running time of the

  time program used by this function.

  函數(shù)使用時(shí)間占所有時(shí)間的百分比。

  cumulative a running sum of the number of seconds accounted

  seconds for by this function and those listed above it.

  函數(shù)和上列函數(shù)累計(jì)執(zhí)行的時(shí)間。

  self the number of seconds accounted for by this

  seconds function alone. This is the major sort for this

  listing.

  函數(shù)本身所執(zhí)行的時(shí)間。

  calls the number of times this function was invoked, if

  this function is profiled, else blank.

  函數(shù)被調(diào)用的次數(shù)

  self the average number of milliseconds spent in this

  ms/call function per call, if this function is profiled,

  else blank.

  每一次調(diào)用花費(fèi)在函數(shù)的時(shí)間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.

  每一次調(diào)用,花費(fèi)在函數(shù)及其衍生函數(shù)的平均時(shí)間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.

  函數(shù)名

  prof 實(shí)現(xiàn)原理:

  通過在編譯和鏈接你的程序的時(shí)候(使用 -pg 編譯和鏈接選項(xiàng)),gcc 在你應(yīng)用程序的每個函數(shù)中都加入了一個名為mcount ( or “_mcount” , or “__mcount” , 依賴于編譯器或操作系統(tǒng))的函數(shù),也就是說你的應(yīng)用程序里的每一個函數(shù)都會調(diào)用mcount, 而mcount 會在內(nèi)存中保存一張函數(shù)調(diào)用圖,并通過函數(shù)調(diào)用堆棧的形式查找子函數(shù)和父函數(shù)的地址。這張調(diào)用圖也保存了所有與函數(shù)相關(guān)的調(diào)用時(shí)間、調(diào)用次數(shù)等等的所有信息。

  Gprof 簡單使用:

  讓我們簡單的舉個例子來看看Gprof是如何使用的。

  1.打開linux終端。新建一個test.c文件,并生用-pg 編譯和鏈接該文件。

  test.c 文件內(nèi)容如下:

  引文:

  #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();

  }

  命令行里面輸入下面命令,沒加-c選項(xiàng),gcc 會默認(rèn)進(jìn)行編譯并鏈接生成a.out:

  引文:

  [linux /home/test]$gcc -pg test.c

  如果沒有編譯錯誤,gcc會在當(dāng)前目錄下生成一個a.out文件,當(dāng)然你也可以使用 –o 選項(xiàng)給生成的文件起一個別的名字,像 gcc –pg test.c –o test , 則gcc會生成一個名為test的可執(zhí)行文件,在命令行下輸入[linux /home/test]$./test ,就可以執(zhí)行該程序了,記住一定要加上 ./ 否則程序看上去可能是執(zhí)行,可是什么輸出都沒有。

  2.執(zhí)行你的應(yīng)用程序使之生成供gprof 分析的數(shù)據(jù)。 命令行里面輸入:

  引文:

  [linux /home/test]$a.out

  main() function()

  +--- call b() function

  +---call a() function

  +---call c() function

  [linux /home/test]$

  你會在當(dāng)前目錄下看到一個gmon.out 文件, 這個文件就是供gprof 分析使用的。

  3.使用gprof 程序分析你的應(yīng)用程序生成的數(shù)據(jù)。

  命令行里面輸入:

  引文:

  [linux /home/test]$ gprof -b a.out gmon.out | less

  由于gprof輸出的信息比較多,這里使用了 less 命令,該命令可以讓我們通過上下方向鍵查看gprof產(chǎn)生的輸出,|表示gprof -b a.out gmon.out 的輸出作為 less的輸入。下面是我從gprof輸出中摘抄出的與我們有關(guān)的一些詳細(xì)信息。

  引文:

  Flat profile:

  Each sample counts as 0.01 seconds.

  no time accumulated

  % cumulative self self total

  time seconds seconds calls Ts/call Ts/call name

  0.00 0.00 0.00 1 0.00 0.00 a

  0.00 0.00 0.00 1 0.00 0.00 b

  0.00 0.00 0.00 1 0.00 0.00 c

  Call graph

  granularity: each sample hit covers 4 byte(s) no time propagated

  index % time self children called name

  0.00 0.00 1/1 b [2]

  [1] 0.0 0.00 0.00 1 a [1]

  -----------------------------------------------

  0.00 0.00 1/1 main [10]

  [2] 0.0 0.00 0.00 1 b [2]

  0.00 0.00 1/1 c [3]

  0.00 0.00 1/1 a [1]

  -----------------------------------------------

  0.00 0.00 1/1 b [2]

  [3] 0.0 0.00 0.00 1 c [3]

  -----------------------------------------------

  Index by function name

  [1] a [2] b [3] c

  從上面的輸出我們能明顯的看出來,main 調(diào)用了 b 函數(shù), 而b 函數(shù)分別調(diào)用了a 和 c 函數(shù)。由于我們的函數(shù)只是簡單的輸出了一個字串,故每個函數(shù)的消耗時(shí)間都是0 秒。

【Linux下程序的Profile工具】相關(guān)文章:

Linux文件查找工具find全解讀05-24

Linux系統(tǒng)shell工具打印輸出05-11

Linux下top命令詳解06-23

Linux認(rèn)證系統(tǒng)管理:linux下搭建ftp08-26

Linux Shell文本處理工具08-11

linux下搭建ftp服務(wù)器05-13

Linux系統(tǒng)下如何刪除文件夾06-03

Linux下硬盤分區(qū)具體步驟04-21

Linux下的兩種分層存儲方案05-14