<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • Linux下程序的Profile工具

    時間:2024-09-23 06:38:20 Linux認(rèn)證 我要投稿
    • 相關(guān)推薦

    Linux下程序的Profile工具

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

      gprof的基本用法:

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

      在gcc編譯程序的時候,加上-pg選項,例如:

      gcc -pg -o test test.c

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

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

      運行剛才的程序:./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是個很不錯的工具,大家寫程序時可以多用用。我現(xiàn)在用gprof來profiling我的程序,把耗時最多的函數(shù)或運算找出來,用FPGA芯片實現(xiàn),從而達(dá)到real-time的目的。

      為gprof編譯程序

      在編譯或鏈接源程序的時候在編譯器的命令行參數(shù)中加入“-pg”選項,編譯時編譯器會自動在目標(biāo)代碼中插入用于性能測試的代碼片斷,這些代碼在程序在運行時采集并記錄函數(shù)的調(diào)用關(guān)系和調(diào)用次數(shù),以及采集并記錄函數(shù)自身執(zhí)行時間和子函數(shù)的調(diào)用時間,程序運行結(jié)束后,會在程序退出的路徑下生成一個gmon.out文件。這個文件就是記錄并保存下來的監(jiān)控數(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ù)使用時間占所有時間的百分比。

      cumulative a running sum of the number of seconds accounted

      seconds for by this function and those listed above it.

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

      self the number of seconds accounted for by this

      seconds function alone. This is the major sort for this

      listing.

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

      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)用花費在函數(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)用,花費在函數(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 實現(xiàn)原理:

      通過在編譯和鏈接你的程序的時候(使用 -pg 編譯和鏈接選項),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)用時間、調(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選項,gcc 會默認(rèn)進(jìn)行編譯并鏈接生成a.out:

      引文:

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

      如果沒有編譯錯誤,gcc會在當(dāng)前目錄下生成一個a.out文件,當(dāng)然你也可以使用 –o 選項給生成的文件起一個別的名字,像 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ù)的消耗時間都是0 秒。

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

    常用的Linux網(wǎng)絡(luò)工具08-02

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

    最簡單的Linux驅(qū)動程序09-09

    Linux系統(tǒng)下ftp的管理08-19

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

    Linux Shell文本處理工具10-08

    java程序中如何調(diào)用linux命令08-27

    linux下etc/fstab文件的簡介10-23

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

    主站蜘蛛池模板: 国产乱子伦精品免费视频| 久久精品综合一区二区三区| 国产成人精品在线观看| 亚洲一日韩欧美中文字幕欧美日韩在线精品一区二 | 国产91精品一区二区麻豆亚洲| 国内精品久久久久伊人av| 亚洲AV无码国产精品麻豆天美 | 亚州日韩精品专区久久久| 久久996热精品xxxx| 国产在线精品一区二区中文| 日本精品夜色视频一区二区| 91人前露出精品国产| 国产精品999| 日韩欧国产精品一区综合无码| 国产精品免费看久久久 | 久久夜色撩人精品国产| 免费欧美精品a在线| 伊人久久精品无码av一区| 国产成人精品一区在线| 色花堂国产精品第一页| 国产精品小黄鸭一区二区三区| 久久精品夜色噜噜亚洲A∨| 亚洲国产精品一区二区久久hs| 成人午夜精品视频在线观看| 国产综合精品久久亚洲| 久久丫精品国产亚洲av不卡| 2024国产精品极品色在线| 中文字幕乱码中文乱码51精品| 久久久91精品国产一区二区三区 | 国产午夜精品一区二区三区| 99热这里只有精品国产66 | 久久久久久噜噜精品免费直播| 久久久久女人精品毛片| 国产精品玖玖美女张开腿让男人桶爽免费看| 日韩精品欧美亚洲| 99热热久久这里只有精品68| 国产欧美精品AAAAAA片| 无码8090精品久久一区| 欧美精品福利视频| 97久久精品人妻人人搡人人玩| 亚洲国产精品无码久久久蜜芽|