使用gprof和oprofile查找性能瓶頸

【轉】有些時候,咱們特別關注程序的性能,特別是底層軟件,好比驅動程序,OS等。爲了更好的優化程序性能,咱們必須找到性能瓶頸點,「好鋼用在刀刃上」才能取得好的效果,不然可能白作工做。爲了找到關鍵路徑,咱們可使用profilng技術,在linux平臺上,咱們可使用gprof和oprofile工具。html

  • 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/
相關文章
相關標籤/搜索