linux 平台性能分析工具


Linux平台下面有不少性能分析的工具,每個工具有何優劣卻很難找到一個完整的列表,這里做一下記錄,以便參考。

 

1. Intel VTune http://software.intel.com/en-us/intel-vtune-amplifier-xe/

大名鼎鼎的分析工具,可以直接啟動一個程序來分析,

比如

$vtuneHome/amplxe-cl  -collect hotspots -duration 600 -r /apsara/save_result ./myapp para1 para2

也可以針對運行中的進程來分析,

$vtuneHome/amplxe-cl  -collect hotspots -duration 600 -r /apsara/save_result -target-pid $myapp_pid

運行結束后,就可以使用gui工具來展示結果,直觀易懂,非常方便。

VTune不僅支持對熱點函數(hotspots)的分析,還支持對並發、鎖等待、不同類型的CPU內存訪問(都是Intel自家的)、讀寫帶寬等進行分析,功能強大。

總之,VTune唯一不足的就是收費,免費試用一個月就過期,過期了需要重新申請,比較麻煩。

 

2. OProfile http://en.wikipedia.org/wiki/OProfile

OProfile最大的好處是方便,一般OS都自帶了這個工具,不用做任何准備就可以使用。

該工具設計初衷是針對事件進行采樣,比如CPU時鍾,L2 cache miss等,其將整個系統當做一個整體來看,對於分析kernel或者系統級別的問題比較有用,而如果用於分析個人開發的應用程序,其顯得不足,主要表現在其callgraph不清晰。

比如OProfile告訴你std::find調用占了30%,如果你的程序只有幾百行,那么你很快就能定位到使用std::find的地方;而如果程序有幾萬行,而OProfile的Callgraph很不給力,那么想知道這些std::find都是誰調用的就很困難了。

一句話,對於千行以內的程序或者很少使用stl的程序,OProfile能發揮作用;對於萬行程序或者大量使用了stl的程序,OProfile不那么給力。

 

其基本的使用方法是,

opcontrol --no-vmlinux : 指示oprofile啟動檢測后,不記錄內核模塊、內核代碼相關統計數據
opcontrol --init : 加載oprofile模塊、oprofile驅動程序
opcontrol --start : 指示oprofile啟動檢測
opcontrol --dump : 指示將oprofile檢測到的數據寫入文件
啟動你的應用程序;
opcontrol --stop

opreport -D smart -l > /tmp/report : 寫入分析結果,不包括callgraph,如果需要callgraph,則使用
opreport -c -D smart -l > /tmp/report : 這一步很慢;

還可以分析源代碼,會將每行代碼標上所耗費CPU的比例;
opannotate -s /lib64/libc-2.4.so : 以代碼的角度,針對libc-2.4.so庫顯示檢測結果

還有一些用法,man可以看到更詳細的解釋;
opcontrol --reset : 清空之前檢測的數據記錄
opcontrol -h : 關閉oprofile進程

 

有時候opreport會報告說buffersize不夠丟了一些采樣點,這時候可以調整buffer size。buffer size不是以byte計數,而是以能做多少sample來計數的;分為兩級,一個是總的buffer size,一個是每個cpu的buffer size,如果超過了bufer watershed就flush到磁盤文件中。如下,是修改之后的:

$sudo opcontrol --status
Daemon not running
Session-dir: /var/lib/oprofile
Separate options: library
vmlinux file: none
Image filter: none
Call-graph depth: 25
Buffer size: 1000000
CPU buffer watershed: 256000
CPU buffer size: 32000
 
在安裝OProfile的過程中,也可能碰到各種各樣的問題,總結如下:
install libiberty.h
checking for libiberty.h... no
checking for cplus_demangle in -liberty... no
configure: error: liberty library not found
安裝  binutils-devel,uname -a 確認是x86_64 還是 386
 
如果出現類似, op_cpu_type.c:259:39: error: 'AT_BASE_PLATFORM' undeclared (first use in this function),如果你是X86_64平台,打上patch屏蔽掉PPC平台的編譯;
 
Warning: QT version 3 was requested but not found. No GUI will be built.
Warning: You requested to build with the '--with-kernel' option, but your kernel
headers were not accessible at the given location. Be sure you have run the following
command from within your kernel source tree:
     make headers_install INSTALL_HDR_PATH=<kernel-hdrs-install-dir>
Then pass <kernel-hdrs-install-dir> to oprofile's '--with-kernel' configure option.

If you run 'make' now, only the legacy ocontrol-based profiler will be built.
 
這個原因是因為內部不支持perf event,這時候無法只針對單獨的進程進行采樣。
 
 

GProf用起來很麻煩,編譯時候需要加入-pg 選項,而且默認只能針對單線程程序(這個patch可以支持多線程,http://sam.zoy.org/writings/programming/gprof.html),總之極其不便;

 

4. Google Perf Tools https://code.google.com/p/gperftools/?redir=1

強大免費的工具來了,Gperf 里面包含內存分配器,內存泄露分析器,CPU使用分析器,callgraph也比較精准,用起來簡單方便(只要鏈接lib然后配置一個環境變量就好了),並且自己可以使用代碼精確控制profile的配置,具體參考鏈接左邊的幾個文章。

 

還有一些其他的分析方法和工具,比如連續做幾次pstack看看大部分線程在干嗎也可以初步判斷可能是哪里出了問題。

 

總之,如果簡單的分析一下,那么OProfile是可以勝任的;如果想比較認真仔細的做性能調優,最好使用Google perf tools。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM