Android應用的性能如何測試?JAVA層面可以用TraceView,可是用NDK開發出來的是so,TraceView跟蹤不了怎么辦?問了Google大神,答案是OProfile!
Oprofile 是Linux系統下一個低開銷的系統全局的性能監視工具,利用處理器上所包含的專用的性能監視硬件(若沒有性能監視硬件則使用一個基于計時器的代用品)來收集與性能相關的數據樣品。它獲得關于內核以及系統上的可執行文件的信息,例如內存是何時被引用的;L2緩存請求的數量;收到的硬件中斷數量等。
Oprofile的特點如下:
l 無需重新編譯源代碼,如果不進行源代碼及分析,連調試信息(-g option to gclearcase/" target="_blank" >cc)也不是必須的。
l 只在內核中插入一個模塊。
l 可以分析運行于系統之上的所有代碼(禁用中斷的代碼除外)
l 系統的額外開銷小,Oprofile會增加1%-8%的系統開銷(取決于采樣頻率)
l 兼容所有2.2,2.4,2.6內核,可以運行在SMP系統之上
l 支持主流CPU架構,包括X86、arm、AVR32、mips、powerpc等
Oprofile要想跑在Andorid上,要滿足下面的條件:
1.內核要支持
2.要將Oprofile移植到Arm平臺上
下面是移植的全過程:
一、Oprofile移植
用到的交叉編譯工具如下:
arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2
用到的庫如下:
popt-1.14.tar.gz
binutils-2.21.tar.gz
oprofile-0.9.6.tar.gz
$ tar xvfz arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 -C ~/
修改~/.bashrc,添加
export PATH=${PATH}:/home/louieli/arm-2010.09/bin
$ tar zxvf popt-1.14.tar.gz
$ cd popt-1.14
$ ac_cv_va_copy=yes ./configure --with-kernel-support --host=arm-none-linux-gnueabi --prefix=/home/louieli/work/popt
$ make
$ make install
$ tar zxvf binutils-2.21.tar.gz
$ cd binutils-2.21/
$ ./configure --with-kernel-support --host=arm-none-linux-gnueabi --prefix=/home/louieli/work/binutils --enable-shared
$ make LDFLAGS="-all-static"
可能會出現 cc1: warnings being treated as errors,找到出錯文件的Makefile文件,將-Werror去掉
$ make install
$ tar zxvf oprofile-0.9.6.tar.gz
$ cd oprofile-0.9.6/
$ ./configure --with-kernel-support --host=arm-none-linux-gnueabi --prefix=/home/louieli/work/oprofile/ --with-extra-libs=/home/louieli/work/popt/lib/ --with-extra-includes=/home/louieli/work/popt/include/ --with-binutils=/home/louieli/work/binutils
$ make LDFLAGS="-all-static -L/home/louieli/work/binutils/lib -Xlinker -R -Xlinker /home/louieli/work/binutils/lib -L/home/louieli/work/popt/lib/"
$ make install
用file 命令查看,我們需要的oprofile文件都已經變成可以在android上跑的靜態鏈接文件了
install.sh: Bourne-Again shell script text executable
opannotate: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
oparchive: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
opcontrol: a /system/bin/sh script text executable
opgprof: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
ophelp: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
opimport: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
opjitconv: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
opreport: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
oprofiled: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped