一、簡(jiǎn)介
有些時(shí)候,我們特別關(guān)注程序的性能,特別是底層軟件,比如驅(qū)動(dòng)程序,OS等。為了更好的優(yōu)化程序性能,我們必須找到性能瓶頸點(diǎn),“好鋼用在刀刃上”才能取 得好的效果,否則可能白做工作。為了找到關(guān)鍵路徑,我們可以使用profilng技術(shù),在linux平臺(tái)上,我們可以使用gprof和oprofile工 具。
gprof是GNU工具之一,它在編譯的時(shí)候在每個(gè)函數(shù)的出入口加入了profiling的代碼,運(yùn)行時(shí)統(tǒng)計(jì)程序在用戶態(tài)的 執(zhí)行信息,可以得到每個(gè)函數(shù)的調(diào)用次數(shù),執(zhí)行時(shí)間,調(diào)用關(guān)系等信息,簡(jiǎn)單易懂。適合于查找用戶級(jí)程序的性能瓶頸,對(duì)于很多時(shí)間都在內(nèi)核態(tài)執(zhí)行的程 序,gprof不適合。
oProfile是Linux平臺(tái)上的一個(gè)功能強(qiáng)大的性能分析工具,支持兩種采樣(sampling)方式:基于事件的采樣(eventbased)和基于時(shí)間的采樣(timebased),它可以工作在不同的體系結(jié)構(gòu)上,包括MipS、ARM、IA32、IA64和AMD。
參考:
http://blog.chinaunix.net/uid-21768364-id-186057.html
http://blog.csdn.net/cybertan/article/details/8015611
http://www.CUOXin.com/bangerlee/archive/2012/08/30/2659435.html
二、gprof使用方法
gprof是gnu binutils工具之一,默認(rèn)情況下linux系統(tǒng)當(dāng)中都帶有這個(gè)工具
使用 -pg 選項(xiàng)來(lái)編譯hello.c,如果要得到帶注釋的源碼清單,則需要增加 -g 選項(xiàng)。運(yùn)行: gcc -pg -g -o hello hello.c運(yùn)行應(yīng)用程序: ./hello 會(huì)在當(dāng)前目錄下產(chǎn)生gmon.out文件,使用gprof來(lái)分析gmon.out文件時(shí),需要把它和產(chǎn)生它的應(yīng)用程序關(guān)聯(lián)起來(lái):
gprof hello gmon.out -p 得到每個(gè)函數(shù)占用的執(zhí)行時(shí)間 gprof hello gmon.out -q 得到call graph,包含了每個(gè)函數(shù)的調(diào)用關(guān)系,調(diào)用次數(shù),執(zhí)行時(shí)間等信息。 gprof hello gmon.out -A 得到 一個(gè)帶注釋的“源代碼清單”,它會(huì)注釋源碼,指出每個(gè)函數(shù)的執(zhí)行次數(shù)。這需要在編譯的時(shí)候增加 -g選項(xiàng)。
三、oprofile安裝步驟
1、打開(kāi)內(nèi)核OPROFILE選項(xiàng),否則運(yùn)行oProfile將提示:
[root@localhost oprofile-0.9.6]# opcontrol --init FATAL: Module oprofile not found. FATAL: Module oprofile not found. Kernel doesn't support oprofile2、編輯內(nèi)核配置文件:.config,將其中的# CONFIG_OPROFILE is not set改為CONFIG_OPROFILE=m(或者y)
[root@localhost ~]# cd /usr/src/linux-2.6.37.2 [root@localhost linux-2.6.37.2]# vi .config
如下:
CONFIG_PROFILING=y CONFIG_X86_LOCAL_APIC=y CONFIG_X86_IO_APIC=y CONFIG_PCI_IOAPIC=y
3、編譯內(nèi)核并重啟機(jī)器
4、下載源碼,編譯安裝
wget http://cznic.dl.sourceforge.net/project/oprofile/oprofile/oprofile-1.0.0/oprofile-1.0.0.tar.gz
tar -zxvf oprofile-1.0.0.tar.gzcd oprofile-1.0.0./configuremakemake install
四、oprofile工具集
op_help: 列出所有支持的事件。opcontrol: 設(shè)置需要收集的事件。opreport: 對(duì)結(jié)果進(jìn)行統(tǒng)計(jì)輸出。opannaotate:產(chǎn)生帶注釋的源/匯編文件,源語(yǔ)言級(jí)的注釋需要編譯源文件時(shí)的支持。opstack: 產(chǎn)生調(diào)用圖profile,但要求x86/2.6的平臺(tái),并且linux2.6安裝了call-graph patchopgprof: 產(chǎn)生如gprof相似的結(jié)果。oparchive: 將所有的原始數(shù)據(jù)文件收集打包,可以到另一臺(tái)機(jī)器上進(jìn)行分析。op_import: 將采樣的數(shù)據(jù)庫(kù)文件從另一種abi轉(zhuǎn)化成本地格式。
五、oprofile使用方法
測(cè)試文件:multiply.c
#include <stdio.h> int fast_multiply(x, y) { return x * y; } int slow_multiply(x, y) { int i, j, z; for (i = 0, z = 0; i < x; i++) z = z + y; return z; } int main(int argc, char *argv[]) { int i,j; int x,y; for (i = 0; i < 200; i ++) { for (j = 0; j < 30 ; j++) { x = fast_multiply(i, j); y = slow_multiply(i, j); } } printf("x=%d, y=%d/n", x, y); return 0; }編譯
gcc multiply.c -g -o multiply
測(cè)試
modprobe oprofile timer=1
opcontrol --no-vmlinux
opcontrol --separate=kernel
opcontrol --init opcontrol --reset opcontrol --start./multiplyopcontrol --dumpopcontrol --stopopcontrol --shutdownopcontrol --deinitopannotate --source ./multiply
opreport -l ./multiply
opreport

新聞熱點(diǎn)
疑難解答
圖片精選