gprof實際上只是一個用於讀取profile結果文件的工具。gprof採用混合方法來收集程序的統計信息,他使用檢測方法,在編譯過程當中在函數入口處插入計數器用於收集每一個函數的被調用狀況和被調用次數;也使用採樣方法,在運行時按必定間隔去檢查程序計數器並在分析時找出程序計數器對應的函數來統計函數佔用的時間。css
Gprof具備如下優缺點:多線程
1) 優勢:app
a) GNU工具,人手一個;函數
b) 混合方法採集信息。工具
2) 缺點:性能
a) 須要編譯選項支持:測試
i. 使用gcc/cc編譯和連接時須要加入-pg選項優化
ii. 使用ld連接時須要用/lib/gcrt0.o代替crt0.o做爲第一個input文件this
iii. 若是要調試libc庫須要使用-lc_p代替-lc參數命令行
b) 調試多線程程序只能統計主線程的信息(因此不能用於kingbase)。
使用gcc/cc編譯和連接時須要加入-pg選項
使用ld連接時須要用/lib/gcrt0.o代替crt0.o做爲第一個input文件
若是要調試libc庫須要使用-lc_p代替-lc參數
正常運行編譯好的程序,程序正常結束後會在當前目錄生成統計信息文件gmon.out。
程序必須正常退出(調用exit或從main中返回)才能生成統計信息。
當前目錄下若是有另外叫gmon.out的文件,內容將被本次運行生成的統計信息覆蓋,屢次運行統一程序請將前一次的gmon.out更名。
命令格式:
gprof options [executable-file [profile-data-files...]] [> outfile]
經常使用參數介紹:
symspec表示須要加入或排除的函數名,和gdb指定斷點時的格式相同。
1) 輸出相關:
a) -A[symspec]或--annotated-source[=symspec]:進行源碼關聯,只關聯symspec指定的函數,不指定爲所有關聯。
b) -I dirs或--directory-path=dirs:添加搜索源碼的文件夾,修改環境變量GPROF_PATH也能夠。
c) -p[symspec]或--flat-profile[=symspec]:默認選項,輸出統計信息,只統計symspec指定的函數,不指定爲所有統計。
d) -P[symspec]或--no-flat-profile[=symspec]:排除統計symspec指定的函數
e) -q[symspec]或--graph[=symspec]:默認選項,輸出函數調用信息,只統計symspec指定的函數,不指定爲所有統計。
f) -Q[symspec]或--no-graph[=symspec]:排除統計symspec指定的函數
g) -b或--brief:不輸出對各個參數含義的解釋;
2) 分析相關:
a) -a或--no-static:定義爲static的函數將不顯示,函數的被調用次數將被計算在調用它的不是static的函數中;
b) -m num或--min-count=num:不顯示被調用次數小於num的函數;
c) -z或--display-unused-functions:顯示沒有被調用的函數;
編譯測試文件:
gcc –g –o test test.c –pg
執行程序:
./test
查看統計信息:
gprof -b -A -p -q test gmon.out > pg
% 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.
函數名
1.4 結論
咱們可使用程序概要分析快速的找到一個程序裏面值得優化的地方。
1.5 原理
1.5 缺點