1. 程式人生 > >程式效能調優之 怎樣使用gprof和oprofile來分析 linux程式的效能(每個函式的呼叫次數與耗時)

程式效能調優之 怎樣使用gprof和oprofile來分析 linux程式的效能(每個函式的呼叫次數與耗時)

有些時候,我們特別關注程式的效能,特別是底層軟體,比如驅動程式,OS等。為了更好的優化程式效能,我們必須找到效能瓶頸點,“好鋼用在刀刃上”才能取 得好的效果,否則可能白做工作。為了找到關鍵路徑,我們可以使用profilng技術,在linux平臺上,我們可以使用gprof和oprofile工 具。

  • gprof是GNU工具之一,它在編譯的時候在每個函式的出入口加入了profiling的程式碼,執行時統計程式在使用者態的 執行資訊,可以得到每個函式的呼叫次數,執行時間,呼叫關係等資訊,簡單易懂。適合於查詢使用者級程式的效能瓶頸,對於很多時間都在核心態執行的程 序,gprof不適合。
  • oprofile也是一個開源的profiling工具,它使用硬體除錯暫存器來統計資訊,進 行profiling的開銷比較小,而且可以對核心進行profiling。它統計的資訊非常的多,可以得到cache的缺失率,memory的訪存信 息,分支預測錯誤率等等,這些資訊gprof是得不到的,但是對於函式呼叫次數,它是不能夠得到的。。

簡單來說,gprof簡單,適合於查詢使用者級程式的瓶頸,而oprofile稍顯複雜,但是得到的資訊更多,更適合除錯系統軟體。
我們以編譯執行hello.c為例,來說明如何使用這兩個工具,這裡不解釋具體結果的含義,要想詳細瞭解每個結果代表什麼意思,可以看一下參考資料中官方 站點上的doc資訊,裡面會給你詳盡的解釋。
gprof Quick Start
gprof是gnu binutils工具之一,預設情況下linux系統當中都帶有這個工具。

  1. 使用 -pg 選項來編譯hello.c,如果要得到帶註釋的原始碼清單,則需要增加 -g 選項。執行: gcc -pg -g -o hello hello.c
  2. 執行應用程式: ./hello 會在當前目錄下產生gmon.out檔案
  3. 使用gprof來分析 gmon.out檔案,需要把它和產生它的應用程式關聯起來:
    1. gprof hello gmon.out -p 得到每個函式佔用的執行時間
    2. gprof hello gmon.out -q 得到call graph,包含了每個函式的呼叫關係,呼叫次數,執行時間等資訊。
    3. gprof hello gmon.out -A 得到一個帶註釋的“原始碼清單”,它會註釋原始碼,指出每個函式的執行次數。這需要在編譯的時候增加 -g選項。

oprofile Quick Start
oprofile是sourceforge上面的一個開源專案,在2.6核心上帶有這個工具,好像只有smp系統才有。比較老的系統,需要自己安裝,重新 編譯核心。
    oprofile是一套工具,分別完成不同的事情。
op_help: 列出所有支援的事件。
opcontrol:設定需要收集的 事件。
opreport: 對結果進行統計輸出。
opannaotate:產生帶註釋的源/彙編檔案,源語言級的註釋需要編譯原始檔時的 支援。
opstack:    產生呼叫圖profile,但要求x86/2.6的平臺,並且linux2.6安裝了call-graph patch
opgprof:    產生如gprof相似的結果。
oparchive: 將所有的原始資料檔案收集打包,可以到另一臺機器上進行分析。
op_import: 將取樣的資料庫檔案從另一種abi轉化成本地格式。    執行oprofile需要root許可權,因為它要載入profile模組,啟動oprofiled後臺程式等。所以在執行之前,就需要切換到root。

  1. opcontrol --init 載入模組,mout /dev/oprofile 建立必需的檔案和目錄
  2. opcontrol --no-vmlinux 或者 opcontrol --vmlinux=/boot/vmlinux-`uname -r` 決定是否對kernel進行profiling
  3. opcontrol --reset 清楚當前會話中的資料
  4. opcontrol --start 開始profiling
  5. ./hello 執行應用程式,oprofile會對它進行profiling
  6. opcontrol --dump 把收集到的資料寫入檔案
  7. opcontrol --stop 停止profiling
  8. opcotrol -h 關閉守護程序oprofiled
  9. opcontrol --shutdown 停止oprofiled
  10. opcontrol --deinit 解除安裝模組

常用的是3→7這幾個過程,得到效能資料之後,可以使用 opreport, opstack, opgprof, opannotate幾個工具進行分析,我常用的是opreport, opannotate進行分析。

  1. opreport使用 http://oprofile.sourceforge.net/doc/opreport.html
  2. opannotate使用 http://oprofile.sourceforge.net/doc/opannotate.html
  3. opgprof使用 http://oprofile.sourceforge.net/doc/opgprof.html

最常用的是 opreport,這個可以給出image和symbols的資訊,比如我想得到每個函式的執行時間佔用比例等資訊,用來發現系統性能瓶頸。 opannotate可以對原始碼進行註釋,指出哪個地方佔用時間比較多。常用命令如下:

  • opreport -l /bin/bash --exclude-depand --threshold 1 , 用來發現系統瓶頸。
  • opannotate --source --output-dir=annotated /usr/local/oprofile-pp/bin/oprofiled
  • opannotate --source --base-dirs=/tmp/build/libfoo/ --search-dirs=/home/user/libfoo/ --output-dir=annotated/ /lib/libfoo.so

網路資源

    1. gprof 使用者手冊 http://sourceware.org/binutils/docs-2.17/gprof/index.html
    2. oprofile官方站點 http://oprofile.sourceforge.net/
    3. 使用 GNU profiler 來提高程式碼執行速度 http://www-128.ibm.com/developerworks/cn/linux/l-gnuprof.html
    4. 使用 OProfile for Linux on POWER 識別效能瓶頸 http://www-128.ibm.com/developerworks/cn/linux/l-pow-oprofile/