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,這樣就生成了一個檔案,該檔案就包含了profiling的資料。
3. 使用gprof 分析你的應用程式生成的資料
gprof test >
使用上面的命令,gprof就可以分析程式test的效能,將profiling的結果放在檔案中,開啟就可以看到分析的結果。通過對結果的分析來改進我們的程式,從而達到我們的目的。
GNU gprof是個很不錯的工具,大家寫程式時可以多用用。我現在用gprof來profiling我的程式,把耗時最多的函式或運算找出來,用FPGA晶片實現,從而達到real-time的目的。
為gprof編譯程式
在編譯或連結源程式的時候在編譯器的命令列引數中加入“-pg”選項,編譯時編譯器會自動在目標程式碼中插入用於效能測試的程式碼片斷,這些程式碼在程式在執行時採集並記錄函式的呼叫關係和呼叫次數,以及採集並記錄函式自身執行時間和子函式的呼叫時間,程式執行結束後,會在程式退出的路徑下生成一個檔案。這個檔案就是記錄並儲存下來的監控資料。可以通過命令列方式的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.
函式名
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("tt+---call a() functionn");
}
void c(){
printf("tt+---call c() functionn");
}
int b() {
printf("t+--- call b() functionn");
a();
c();
return 0;
}
int main(){
printf(" main() function()n");
b();
相關文章
-
Linux下如何編寫第一個C語言hello world程式
我想很多人不知道Linux下編寫第一個C語言hello world程式是怎麼做的,下面是小編收集整理的Linux下如何編寫第一個C語言hello world程式,希望對大家有幫助~~ Linux下編寫第一個C語言hello world程式的方法進入Linux系 -
託福英語寫作範文:Examinations exert a pernicious influence on educat
We might marvel at the progress made in every field of study, but the methods of testing a person’s knowledge and ability remain as primitive as ever they were. It really is extraordinary that after -
Linux系統下的ipmitool工具應該怎樣使用
1折成三角形2折成萎形,把角按下去3把小三角向裡折4摺好後兩邊三角形向裡折5粘膠主要材料:彩紙 60所需工具:膠水或雙面膠 適量製作步驟:第1步:準備一張正方形的紙第2步:對摺成三角形第3步:兩邊向上折第4步:開啟摺好的三角 -
Linux中Firefox如何安裝Flash外掛
因為Linux系統不再支援Flash外掛,導致在使用Firefox瀏覽器的時候因缺少Flash外掛而觀看不了Flash,那麼要如何為Firefox瀏覽器安裝Flash外掛呢?隨小編一起來詳細瞭解下吧。早前Adobe已經宣佈取消用於Linux的'flash外掛支 -
高中英語作文:My approach to difficulties in learning
某校英文報社開設了一個專欄:experience . 本期話題是如何解決學習中遇到的`困難。請你以“my approach to difficulties in leaing” 為題,用英語寫一篇短文,談談自己的一些做法。 範文: My approach to difficulties -
Wonderful life in winter holiday作文(精選14篇)
在平凡的學習、工作、生活中,說到作文,大家肯定都不陌生吧,藉助作文可以宣洩心中的情感,調節自己的心情。還是對作文一籌莫展嗎?以下是小編為大家整理的Wonderful life in winter holiday作文(精選14篇),希望能夠幫助到大家。 -
高中英語作文:Wonderful life in winter holiday
英語作文from the sixteenth of january to the seventh of february is our winter holiday. i think everybody did a lot of things in the winter holiday. but i didn’t. let you to listen to my story of -
Different People Like Different Movies國中英語作文
Different people like different movies. Mary likes comedies, she thinks they are funny. She likes documentaries too, because she thinks documentaries are exciting . She doesn’t like thrillers. She th -
英語閱讀範文參考:LovePeopleInAllKindsOfWeather
LovePeopleInAllKindsOfWeatherMake sure your love is unconditional. Make sure you love people in all kinds of "weather". Or else what is the use if we love a person only when he is good or she is nice? -
brought rain in April Mayflowe英語作文
from the golden-tipped fields of mid-west america to the ancient kingdoms of verdant palestine, there is a happy truth to be shared with all who would take heed. in more recent times, this truth has b