gprof使用介紹

前兩天嘗試了一下gprof,感覺還挺好,寫點東西介紹一下

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 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.
                          函數名 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章