Android效能測試工具–Oprofile

來源:互聯網
上載者:User

 Android應用的效能如何測試?JAVA層面可以用TraceView,可是用NDK開發出來的是so,TraceView跟蹤不了怎麼辦?問了Google大神,答案是OProfile!Oprofile 是Linux系統下一個低開銷的系統全域的效能監控工具,利用處理器上所包含的專用的效能監控硬體(若沒有效能監控硬體則使用一個基於計時器的代用品)來收集與效能相關的資料樣品。它獲得關於核心以及系統上的可執行檔的資訊,例如記憶體是何時被引用的;L2緩衝請求的數量;收到的硬體中斷數量等。Oprofile的特點如下:l         無需重新編譯原始碼,如果不進行原始碼及分析,連調試資訊(-g option to gcc)也不是必須的。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.gzoprofile-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 executableopannotate: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not strippedoparchive:  ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not strippedopcontrol:  a /system/bin/sh script text executableopgprof:    ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not strippedophelp:     ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not strippedopimport:   ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not strippedopjitconv:  ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not strippedopreport:   ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not strippedoprofiled:  ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped二、編譯linux核心映像a)準備交叉編譯工具鏈android代碼樹中有一個prebuilt項目,包含了我們編譯核心所需的交叉編譯工具。b)設定環境變數$ emacs ~/.bashrc增加如下兩行:export PATH=$PATH:~/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/binexport ARCH=arm儲存後,同步變化:$ source ~/.bashrcc)獲得合適的核心原始碼$ cd ~/android獲得核心原始碼倉庫$ git clone git://android.git.kernel.org/kernel/common.git kernel$ cd kernel$ git branch顯示* android-2.6.27說明你現在在android-2.6.27這個分支上,也是kernel/common.git的預設主分支。顯示所有head分支:$ git branch -a顯示* android-2.6.27remotes/origin/HEAD -> origin/android-2.6.27remotes/origin/android-2.6.25remotes/origin/android-2.6.27remotes/origin/android-2.6.29remotes/origin/android-goldfish-2.6.27remotes/origin/android-goldfish-2.6.29我們選取最新的android-goldfish-2.6.29,其中goldfish是android的模擬器類比的CPU。$ git checkout -b android-goldfish-2.6.29 origin/android-goldfish-2.6.29$ git branch顯示android-2.6.27* android-goldfish-2.6.29我們已經工作在android-goldfish-2.6.29分支上了。d)設定交叉編譯參數開啟kernel目錄下的Makefile檔案,把CROSS_COMPILE指向剛才下載的prebuilt中的arm-eabi編譯器CROSS_COMPILE ?= arm-eabi-把LDFLAGS_BUILD_ID = $(patsubst -Wl$(comma)%,%,/$(call ld-option, -Wl$(comma)–build-id,))這一行注釋掉,並且添加一個空的LDFLAGS_BUILD_ID定義,如下:LDFLAGS_BUILD_ID =e)編譯核心映像$ cd ~/android/kernel$ make goldfish_defconfig$ make menuconfig修改核心配置如下General setup --->[*] Profiling support (EXPERIMENTAL)[ ] Activate markers[*] OProfile system profiling (EXPERIMENTAL) 這是把OProfile直接編進核心,也可以選擇[M] OProfile system profiling (EXPERIMENTAL)會在arch/arm/oprofile檔案夾下產生oprofile.ko,oprofile.ko需要用insmod載入。$make f)測試產生的核心映像$ emulator -avd myavd -kernel ~/android/kernel/arch/arm/boot/zImage三、Oprofile在android模擬器中的使用1.先看一下opcontrol的參數# opcontrolopcontrol: usage:   -l/--list-events list event types and unit masks   -?/--help        this message   -v/--version     show version   --init           loads the oprofile module and oprofilefs   --setup          give setup arguments (may be omitted)   --status         show configuration   --start-daemon   start daemon without starting profiling   -s/--start       start data collection   -d/--dump        flush the collected profiling data   -t/--stop        stop data collection   -h/--shutdown    stop data collection and kill daemon   -V/--verbose[=all,sfile,arcs,samples,module,misc,ext]                    be verbose in the daemon log   --reset          clears out data from current session   --save=name      save data from current session to session_name   --deinit         unload the oprofile module and oprofilefs   -e/--event=eventspec      Choose an event. May be specified multiple times. Of the form      "default" or "name:count:unitmask:kernel:user", where :      name:     event name, e.g. CPU_CLK_UNHALTED or RTC_INTERRUPTS      count:    reset counter value e.g. 100000      unitmask: hardware unit mask e.g. 0x0f      kernel:   whether to profile kernel: 0 or 1      user:     whether to profile userspace: 0 or 1   -p/--separate=type,[types]       Separate profiles as follows :       none:     no profile separation       library:  separate shared library profiles per-application       kernel:   same as library, plus kernel profiles       thread:   per-thread/process profiles       cpu:      per CPU profiles       all:      all of the above   -c/--callgraph=#depth         enable callgraph sample collection with a maximum depth.                                 Use 0 to disable callgraph profiling.   --session-dir=dir             place sample database in dir instead of                                 default location (/var/lib/oprofile)   -i/--image=name[,names]       list of binaries to profile (default is "all")   --vmlinux=file                vmlinux kernel image   --no-vmlinux                  no kernel image (vmlinux) available   --kernel-range=start,end      kernel range vma address in hexadecimal   --buffer-size=num             kernel buffer size in sample units   --buffer-watershed            kernel buffer watershed in sample units (2.6 only=   --cpu-buffer-size=num         per-cpu buffer size in units (2.6 only)   --note-table-size             kernel notes buffer size in notes units (2.4 only)   --xen                         Xen image (for Xen only)   --active-domains=<list>       List of domains in profiling session (for Xen only)                                 (list contains domain ids separated by commas)2.使用方法將我們之前編譯好的oprofile和busybox裝入模擬器執行oprofile目錄中的install.sh 將oprofile裝入模擬器adb push busybox /data/busybox$adb shell  //進入模擬器shell#chmod 777 /data/busybox# /data/busybox --install /data/busybox#export PATH=/data/busybox:$PATH:/data/oprofile# mount -o remount rw /# mount -o rw,remount -t yaffs2 /dev/mtdblock3 /system# touch /etc/mtab# echo nodev /dev/oprofile oprofilefs rw 0 0>/etc/mtab# mkdir /dev/oprofile# mount -t oprofilefs nodev /dev/oprofile    //這一句很重要,沒有這一句會出現下面的錯誤# opcontrol --init      cat: can't open '/dev/oprofile/cpu_type': No such file or directoryUnable to open cpu_type file for readingMake sure you have done opcontrol --initcpu_type 'unset' is not validyou should upgrade oprofile or force the use of timer mode# opcontrol --init     //初始化,只需運行一次# opcontrol --setup --callgraph=2 --session-dir=/data/first --no-vmlinuxUsing 2.6+ OProfile kernel interface.Using log file /data/first/samples/oprofiled.logDaemon started.Profiler running.# opcontrol --statusDaemon running: pid 637Separate options: nonevmlinux file: noneImage filter: noneCall-graph depth: 2# opcontrol --start     //啟動profilerUsing 2.6+ OProfile kernel interface.Using log file /var/lib/oprofile/samples/oprofiled.logDaemon started.Profiler running.# /data/test/test     //運行我們的程式 ( 我的測試程式通過這條指令編譯arm-none-linux-gnueabi-gcc -g -o test test.c -static -fno-omit-frame-pointer)in cin ain bin ain cin bin ain a# opcontrol --dump   //收集採樣資料# opcontrol --stop //停止profilerStopping profiling.#opreport --session-dir=/data/first -l /data/test/test       //查看報告CPU: CPU with timer interrupt, speed 0 MHz (estimated)Profiling through timer interruptsamples  %        symbol name11291    79.9589  a1129      7.9952  b853       6.0406  main848       6.0052  c現在我們就可以根據oprofile的輸出對我們的程式進行最佳化了。如果有哪位同學也想試一把的話,一定要用linux。這種移植環境很重要,我之前就在測試機(win7+cygwin)上浪費了很多時間。這裡有打包好的工具,大家可以下載。其中kernel-qemu就是我們之前編譯好的核心,替換掉Android SDK中的kernel-qemu就行了。祝各位好運!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.