- 相關(guān)推薦
Linux下程序的Profile工具
我們?cè)趯?xiě)嵌入式程序時(shí),通常需要對(duì)程序的性能進(jìn)行分析,以便程序能夠更快更好地運(yùn)行,達(dá)到實(shí)時(shí)(real-time)的目的。如果程序很大,分析起來(lái)就很困難。如果有個(gè)工具能夠自動(dòng)進(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,這樣就生成了一個(gè)gmon.out文件,該文件就包含了profiling的數(shù)據(jù)。
3. 使用gprof 分析你的應(yīng)用程序生成的數(shù)據(jù)
gprof test gmon.out > profile.txt
使用上面的命令,gprof就可以分析程序test的性能,將profiling的結(jié)果放在profile.txt文件中,打開(kāi)就可以看到分析的結(jié)果。通過(guò)對(duì)結(jié)果的分析來(lái)改進(jìn)我們的程序,從而達(dá)到我們的目的。
GNU gprof是個(gè)很不錯(cuò)的工具,大家寫(xiě)程序時(shí)可以多用用。我現(xiàn)在用gprof來(lái)profiling我的程序,把耗時(shí)最多的函數(shù)或運(yùn)算找出來(lái),用FPGA芯片實(shí)現(xiàn),從而達(dá)到real-time的目的。
為gprof編譯程序
在編譯或鏈接源程序的時(shí)候在編譯器的命令行參數(shù)中加入“-pg”選項(xiàng),編譯時(shí)編譯器會(huì)自動(dòng)在目標(biāo)代碼中插入用于性能測(cè)試的代碼片斷,這些代碼在程序在運(yùn)行時(shí)采集并記錄函數(shù)的調(diào)用關(guān)系和調(diào)用次數(shù),以及采集并記錄函數(shù)自身執(zhí)行時(shí)間和子函數(shù)的調(diào)用時(shí)間,程序運(yùn)行結(jié)束后,會(huì)在程序退出的路徑下生成一個(gè)gmon.out文件。這個(gè)文件就是記錄并保存下來(lái)的監(jiān)控?cái)?shù)據(jù)?梢酝ㄟ^(guò)命令行方式的gprof或圖形化的Kprof來(lái)解讀這些數(shù)據(jù)并對(duì)程序的性能進(jìn)行分析。另外,如果想查看庫(kù)函數(shù)的profiling,需要在編譯是再加入“-lc_p”編譯參數(shù)代替“-lc”編譯參數(shù),這樣程序會(huì)鏈接libc_p.a庫(kù),才可以產(chǎn)生庫(kù)函數(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)原理:
通過(guò)在編譯和鏈接你的程序的時(shí)候(使用 -pg 編譯和鏈接選項(xiàng)),gcc 在你應(yīng)用程序的每個(gè)函數(shù)中都加入了一個(gè)名為mcount ( or “_mcount” , or “__mcount” , 依賴(lài)于編譯器或操作系統(tǒng))的函數(shù),也就是說(shuō)你的應(yīng)用程序里的每一個(gè)函數(shù)都會(huì)調(diào)用mcount, 而mcount 會(huì)在內(nèi)存中保存一張函數(shù)調(diào)用圖,并通過(guò)函數(shù)調(diào)用堆棧的形式查找子函數(shù)和父函數(shù)的地址。這張調(diào)用圖也保存了所有與函數(shù)相關(guān)的調(diào)用時(shí)間、調(diào)用次數(shù)等等的所有信息。
Gprof 簡(jiǎn)單使用:
讓我們簡(jiǎn)單的舉個(gè)例子來(lái)看看Gprof是如何使用的。
1.打開(kāi)linux終端。新建一個(gè)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();
}
命令行里面輸入下面命令,沒(méi)加-c選項(xiàng),gcc 會(huì)默認(rèn)進(jìn)行編譯并鏈接生成a.out:
引文:
[linux /home/test]$gcc -pg test.c
如果沒(méi)有編譯錯(cuò)誤,gcc會(huì)在當(dāng)前目錄下生成一個(gè)a.out文件,當(dāng)然你也可以使用 –o 選項(xiàng)給生成的文件起一個(gè)別的名字,像 gcc –pg test.c –o test , 則gcc會(huì)生成一個(gè)名為test的可執(zhí)行文件,在命令行下輸入[linux /home/test]$./test ,就可以執(zhí)行該程序了,記住一定要加上 ./ 否則程序看上去可能是執(zhí)行,可是什么輸出都沒(méi)有。
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]$
你會(huì)在當(dāng)前目錄下看到一個(gè)gmon.out 文件, 這個(gè)文件就是供gprof 分析使用的。
3.使用gprof 程序分析你的應(yīng)用程序生成的數(shù)據(jù)。
命令行里面輸入:
引文:
[linux /home/test]$ gprof -b a.out gmon.out | less
由于gprof輸出的信息比較多,這里使用了 less 命令,該命令可以讓我們通過(guò)上下方向鍵查看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
從上面的輸出我們能明顯的看出來(lái),main 調(diào)用了 b 函數(shù), 而b 函數(shù)分別調(diào)用了a 和 c 函數(shù)。由于我們的函數(shù)只是簡(jiǎn)單的輸出了一個(gè)字串,故每個(gè)函數(shù)的消耗時(shí)間都是0 秒。
【Linux下程序的Profile工具】相關(guān)文章:
最簡(jiǎn)單的Linux驅(qū)動(dòng)程序09-09
Linux系統(tǒng)下ftp的管理08-19
Linux認(rèn)證系統(tǒng)管理:linux下搭建ftp10-08
Linux Shell文本處理工具10-08