《unix-os教學課件》第六章-unix系統(tǒng)編程基礎(chǔ)-補充2-gprof-gcov_第1頁
《unix-os教學課件》第六章-unix系統(tǒng)編程基礎(chǔ)-補充2-gprof-gcov_第2頁
《unix-os教學課件》第六章-unix系統(tǒng)編程基礎(chǔ)-補充2-gprof-gcov_第3頁
《unix-os教學課件》第六章-unix系統(tǒng)編程基礎(chǔ)-補充2-gprof-gcov_第4頁
《unix-os教學課件》第六章-unix系統(tǒng)編程基礎(chǔ)-補充2-gprof-gcov_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、gprof,gcov資料電子科技大學計算機學院.軟件學院1目錄Gprof 簡介Gprof 實現(xiàn)原理Gprof 使用方法編譯程序運行程序使用Gprof查看結(jié)果命令格式使用Gprof的完整例子Gprof產(chǎn)生的信息解析Gprof使用注意2Gprof 簡介Gprof實際上只是一個用于讀取Profile結(jié)果文件的工具。Gprof采用混合方法來收集程序的統(tǒng)計信息,他使用檢測方法,在編譯過程中在函數(shù)入口處插入計數(shù)器用于收集每個函數(shù)的被調(diào)用情況和被調(diào)用次數(shù);也使用采樣方法,在運行時按一定間隔去檢查程序計數(shù)器并在分析時找出程序計數(shù)器對應的函數(shù)來統(tǒng)計函數(shù)占用的時間。 3Gprof 實現(xiàn)原理通過在編譯和鏈接你的程序

2、的時候(使用 -pg 編譯和鏈接選項),gcc 在你應用程序的每個函數(shù)中都加入了一個名為mcount ( or “_mcount” , or “_mcount” , 依賴于編譯器或操作系統(tǒng))的函數(shù)。應用程序里的每一個函數(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ù)等等的所有信息。 4Gprof 使用方法編譯程序使用gcc/cc編譯和鏈接時需要加入-pg選項。使用ld鏈接時需要用/lib/gcrt0.o代替crt0.o作為第一個input文件。如果要調(diào)試libc庫需要使

3、用-lc_p代替-lc參數(shù)。5Gprof 使用方法運行程序正常運行編譯好的程序,程序正常結(jié)束后會在當前目錄生成統(tǒng)計信息文件gmon.out。程序必須正常退出(調(diào)用exit或從main中返回)才能生成統(tǒng)計信息。當前目錄下如果有另外叫g(shù)mon.out的文件,內(nèi)容將被本次運行生成的統(tǒng)計信息覆蓋,多次運行統(tǒng)一程序請將前一次的gmon.out改名。6Gprof 使用方法使用Gprof查看結(jié)果命令格式gprof options executable-file profile-data-files. outfile輸出相關(guān): symspec 表示需要加入或排除的函數(shù)名a)-Asymspec或-annotat

4、ed-source=symspec:進行源碼關(guān)聯(lián),只關(guān)聯(lián)symspec指定的函數(shù),不指定為全部關(guān)聯(lián)。b)-I dirs或-directory-path=dirs:添加搜索源碼的文件夾,修改環(huán)境變量GPROF_PATH也可以。c)-psymspec或-flat-profile=symspec:默認選項,輸出統(tǒng)計信息,只統(tǒng)計symspec指定的函數(shù),不指定為全部統(tǒng)計。7Gprof 使用方法使用Gprof查看結(jié)果命令格式d)-Psymspec或-no-flat-profile=symspec:排除統(tǒng)計symspec指定的函數(shù)e)-qsymspec或-graph=symspec:默認選項,輸出函數(shù)調(diào)用

5、信息,只統(tǒng)計symspec指定的函數(shù),不指定為全部統(tǒng)計。f)-Qsymspec或-no-graph=symspec:排除統(tǒng)計symspec指定的函數(shù)g)-b或-brief:不輸出對各個參數(shù)含義的解釋;8Gprof 使用方法使用Gprof查看結(jié)果命令格式分析相關(guān):a)-a或-no-static:定義為static的函數(shù)將不顯示,函數(shù)的被調(diào)用次數(shù)將被計算在調(diào)用它的不是static的函數(shù)中;b)-m num或-min-count=num:不顯示被調(diào)用次數(shù)小于num的函數(shù);c)-z或-display-unused-functions:顯示沒有被調(diào)用的函數(shù)9Gprof 使用方法Gprof使用的完整例子編

6、譯測試文件:gcc g o test test.c pg執(zhí)行程序:./test查看統(tǒng)計信息:gprof -b -A -p -q test gmon.out pg下面是一個運行Gprof后的截圖10Gprof 使用方法11Gprof 產(chǎn)生的信息解析Flat profile: % the percentage of the total running time of thetime program used by this function. 函數(shù)使用時間占所有時間的百分比。cumulative a running sum of the number of seconds accountedsec

7、onds for by this function and those listed above it. 函數(shù)和上列函數(shù)累計執(zhí)行的時間。self the number of seconds accounted for by thisseconds 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ù)12Gpr

8、of 產(chǎn)生的信息解析self the average number of milliseconds spent in thisms/call function per call, if this function is profiled, else blank. 每一次調(diào)用花費在函數(shù)的時間microseconds。total the average number of milliseconds spent in thisms/call function and its descendents per call, if this function is profiled, else blank.

9、 每一次調(diào)用,花費在函數(shù)及其衍生函數(shù)的平均時間 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ù)名 13Gprof 產(chǎn)生的

10、信息解析Call Graphindex A unique number given to each element of the table. Index numbers are sorted numerically. The index number is printed next to every function name so it is easier to look up where the function in the table.% time This is the percentage of the total time that was spent in this func

11、tion and its children. Note that due to different viewpoints, functions excluded by options, etc, these numbers will NOT add up to 100%.14Gprof 產(chǎn)生的信息解析self This is the total amount of time spent in this function.children This is the total amount of time propagated into this function by its children.

12、called This is the number of times the function was called. If the function called itself recursively, the number only includes non-recursive calls, and is followed by a + and the number of recursive The name of the current function. The index number is printed after it. If the function i

13、s a member of a cycle, the cycle number is printed between the functions name and the index number.15Gprof 使用注意 一般gprof只能查看用戶函數(shù)信息。如果想查看庫函數(shù)的信息,需要在編譯是再加入“-lc_p”編譯參數(shù)代替“-lc”編譯參數(shù),這樣程序會鏈接libc_p.a庫,才可以產(chǎn)生庫函數(shù)的profiling信息。Gprof只能在程序正常結(jié)束退出之后才能生成程序測評報告,原因是gprof通過在atexit()里注冊了一個函數(shù)來產(chǎn)生結(jié)果信息,任何非正常退出都不會執(zhí)行atexit()的動作,

14、所以不會產(chǎn)生gmon.out文件。如果你的程序是一個不會退出的服務(wù)程序,那就只有修改代碼來達到目的。如果不想改變程序的運行方式,可以添加一個信號處理函數(shù)解決問題(這樣對代碼修改最少),例如:16Gprof 使用注意static void sighandler( int sig_no )exit(0);signal( SIGUSR1, sighandler );當使用kill -USR1 pid 后,程序退出,生成gmon.out文件。 17gcov 簡介 gcovis a test coverage program. Use it in concert with GCC to analyze

15、your programs to help create more efficient, faster running code and to discover untested parts of your program. You can usegcovas a profiling tool to help discover where your optimization efforts will best affect your code. You can also usegcovalong with the other profiling tool,gprof, to assess wh

16、ich parts of your code use the greatest amount of computing time. 18基本功能how often each line of code executeswhat lines of code are actually executedhow much computing time each section of code uses Software developers also use coverage testing in concert with testsuites, to make sure software is act

17、ually good enough for a release. Testsuites can verify that a program works as expected; a coverage program tests to see how much of the program is exercised by the testsuite. Developers can then determine what kinds of test cases need to be added to the testsuites to create both better testing and

18、a better final product. 19命令介紹suse89: # gcovUsage: gcov OPTION. SOURCEFILE -h, -help Print this help, then exit -v, -version Print version number, then exit -a, -all-blocks Show information for every basic block -b, -branch-probabilities Include branch probabilities in output -c, -branch-counts Give

19、n counts of branches taken rather than percentages -n, -no-output Do not create an output file -l, -long-file-names Use long output file names for included source files -f, -function-summaries Output summaries for each function -o, -object-directory DIR|FILE Search for object files in DIR or called

20、FILE -p, -preserve-paths Preserve all pathname components -u, -unconditional-branches Show unconditional branch counts too20使用方法$ gcc -fprofile-arcs -ftest-coverage tmp.c $ a.out Running the program will cause profile output to be generated. For each source file compiled with-fprofile-arcs, an accom

21、panying.gcdafile will be placed in the object file directory. $ gcov tmp.c Runninggcovwith your programs source file names as arguments will now produce a listing of the code along with frequency of execution for each line $ vi tmp.c.gcov21tmp.c.gcov分析 -: 0:Source:tmp.c -: 0:Graph:tmp.gcno -: 0:Data

22、:tmp.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:#include -: 2: -: 3:int main (void) 1: 4: 1: 5: int i, total; 5: int i, total; -: 6: 1: 7: total = 0; -: 8: 11: 9: for (i = 0; i 10; i+) 10: 10: total += i; -: 11: 1: 12: if (total != 45) #: 13: printf (Failuren); -: 14: else 1: 15: printf (Successn); 1: 16: return 0; -: 17:22gcov -b tmp.c $ gcov -b tmp.c 90.00% of 10 source lines executed in file tmp.c 80.00% of 5 branches executed in file tmp.c 80.00% of 5 branches taken at least once in file tmp.c 50.00% of 2 calls executed in file tmp.c Creating tmp.c.gcov.23統(tǒng)計函數(shù)調(diào)用 -: 0:Sourc

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論