Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 移植 oprofile 效能監測工具

編輯:關於 Android 程式設計

本文記錄在 Ubuntu Server 10.04 平台上交叉編譯 oprofile 並移植到 Android 的完整步驟。編譯環境為:build 端 Ubuntu Server 10.04、host 端 Android-2.2r2、交叉編譯器 mips-linux-gnu-gcc 4.3.2、CPU 架構 mips。由於 oprofile 依賴 popt 與 binutils,需一併編譯。

一、下載原始碼

popt-1.14.tar.gz
binutils-2.21.tar.gz
oprofile-0.9.6.tar.gz

二、建立編譯環境

export PATH=/home/gzshun/share/mips-4.3/bin:$PATH  # 改為自己的工具鏈路徑
export CC="mips-linux-gnu-gcc -EL"
export CXX="mips-linux-gnu-g++ -EL"
export CFLAGS=-static
export CXXFLAGS=-static
export CPPFLAGS=-static

執行 source env.sh 使其生效。

三、編譯 popt

tar zxvf popt-1.14.tar.gz
cd popt-1.14
ac_cv_va_copy=yes ./configure --with-kernel-support --host=mips-linux-gnu --prefix=/home/gzshun/popt
make && make install

四、編譯 binutils(靜態)

tar zxvf binutils-2.21.tar.gz
cd binutils-2.21
./configure --with-kernel-support --host=mips-linux-gnu --prefix=/home/gzshun/binutils --enable-shared
make configure-host
make LDFLAGS="-all-static"
make install

五、編譯 oprofile

tar zxvf oprofile-0.9.6.tar.gz
cd oprofile-0.9.6
./configure --with-kernel-support --host=mips-linux-gnu \
    --prefix=/home/gzshun/oprofile/ \
    --with-extra-libs=/home/gzshun/popt/lib/ \
    --with-extra-includes=/home/gzshun/popt/include/ \
    --with-binutils=/home/gzshun/binutils
make LDFLAGS="-all-static -L/home/gzshun/binutils/lib -Xlinker -R -Xlinker /home/gzshun/binutils/lib -L/home/gzshun/popt/lib/"
make install

六、內核加入 oprofile 模組

在 kernel 設定中開啟:

General setup --->
    [*] Profiling support (EXPERIMENTAL)
    <*> OProfile system profiling (EXPERIMENTAL)

可編進內核,也可編為模組。

開發板上的注意事項

a. oprofile 在 Ubuntu 上的生成路徑是 /home/gzshun/oprofile,因此開發板也要建立相同路徑並把整個目錄複製過去——因為編譯時會依原路徑尋找相依的 lib 庫。

b. 修改 oprofile/bin/opcontrol 第 1968 行,把 /usr/bin 改為 /bin。

c. 在 Android 的 /etc 目錄下新建 mtab 檔,加入:nodev /dev/oprofile oprofilefs rw 0 0。

d. 設定 oprofile 環境變數時要放在 PATH 最前面,因為 Android 系統本身也有同名命令。

e. 報告預設輸出在 /var/lib/oprofile。

效能測試流程

由於 Android 沒有现成的 vmlinux,需從虛擬位址取得核心符號範圍:

grep "_stext" /proc/kallsyms   # 取得起始位址,例如 84000400
grep "_etext" /proc/kallsyms   # 取得結束位址,例如 84342a18

mount -t oprofilefs nodev /dev/oprofile  # 掛載 oprofilefs
opcontrol --init
opcontrol --reset
echo 0 > /data/vmlinux
opcontrol --callgraph=2 --vmlinux=/data/vmlinux --kernel-range=0x84000400,0x84342a18
# 或不追蹤內核: opcontrol --setup --callgraph=2 --no-vmlinux
opcontrol --setup --event=CYCLES:7500:0:0:1
opcontrol --start
opcontrol --status
# ...等待測試...
opcontrol --dump
opreport
opcontrol --stop
opcontrol --shutdown
opcontrol --deinit

報告解讀

opreport 輸出的每一行由三部分組成:Samples(採樣到的次數)、%(佔比)、Symbol name(函數或符號名稱)。透過佔比高低,就能快速找出最耗 CPU 的函式與函式庫,例如範例中 opreport、libdvm.so、libc.so、libskia.so 等佔比較高,代表是效能瓶頸所在,可作為後續優化的依據。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved