使用bootchart 對 高通Android 進行性能分析


使用bootchart 對 高通Android 進行性能分析

Android版本:7.0

適用平台:高通和MTK

參考:

bootchart 簡介

bootchart 是一個用於 linux 啟動過程性能分析的開源工具軟件,在系統啟動過程中自動收集 CPU 占用率、磁盤吞吐率、進程等信息,並以圖形方式顯示分析結果,可用作指導優化系統啟動過程。

bootchart 讓用戶可以很直觀的查看系統啟動的過程和各個過程耗費的時間,以便讓用戶能夠分析啟動過程,從而進行優化以提高啟動時間。

它由 bootchartd 服務和 bootchart-render 兩部分組成,后者主要負責生成啟動流程的分析結果圖。

Android 系統源碼中有 bootchart 的實現,路徑在 system/core/init/bootchart.cpp 中, bootchart 通過內嵌在 init 進程中實現,在后台執行測量。不過 bootchart 的測量時段是 init 進程啟動之后,不包含 uboot 和 kernel 的啟動時間。

參考文檔:system/core/init/readme.txt中的Bootcharting這一章

Bootcharting
------------
This version of init contains code to perform "bootcharting": generating log
files that can be later processed by the tools provided by www.bootchart.org.

On the emulator, use the -bootchart <timeout> option to boot with bootcharting
activated for <timeout> seconds.

On a device, create /data/bootchart/start with a command like the following:

  adb shell 'echo $TIMEOUT > /data/bootchart/start'

Where the value of $TIMEOUT corresponds to the desired bootcharted period in
seconds. Bootcharting will stop after that many seconds have elapsed.
You can also stop the bootcharting at any moment by doing the following:

  adb shell 'echo 1 > /data/bootchart/stop'

Note that /data/bootchart/stop is deleted automatically by init at the end of
the bootcharting. This is not the case with /data/bootchart/start, so don't
forget to delete it when you're done collecting data.

The log files are written to /data/bootchart/. A script is provided to
retrieve them and create a bootchart.tgz file that can be used with the
bootchart command-line utility:

  sudo apt-get install pybootchartgui
  # grab-bootchart.sh uses $ANDROID_SERIAL.
  $ANDROID_BUILD_TOP/system/core/init/grab-bootchart.sh

One thing to watch for is that the bootchart will show init as if it started
running at 0s. You'll have to look at dmesg to work out when the kernel
actually started init.

Comparing two bootcharts
------------------------
A handy script named compare-bootcharts.py can be used to compare the
start/end time of selected processes. The aforementioned grab-bootchart.sh
will leave a bootchart tarball named bootchart.tgz at /tmp/android-bootchart.
If two such barballs are preserved on the host machine under different
directories, the script can list the timestamps differences. For example:

Usage: system/core/init/compare-bootcharts.py base_bootchart_dir
       exp_bootchart_dir

process: baseline experiment (delta)
 - Unit is ms (a jiffy is 10 ms on the system)
------------------------------------
/init: 50 40 (-10)
/system/bin/surfaceflinger: 4320 4470 (+150)
/system/bin/bootanimation: 6980 6990 (+10)
zygote64: 10410 10640 (+230)
zygote: 10410 10640 (+230)
system_server: 15350 15150 (-200)
bootanimation ends at: 33790 31230 (-2560)

bootchart 在 android 平台的使用步驟

編譯 bootchart

在 Android 5.1 之前 bootchart 是沒有編譯進系統的,需要使用下面的宏手動打開編譯,在 Android 6.0 以上系統默認已經編譯了 bootchart,

可以 adb shell 命令進入文件系統,可以看到 /data/bootchart 目錄存在。

打開 bootchart 收集開機數據

0、修改高通代碼:system/core/init/Android.mk,刪掉下列這一行,重新編譯燒錄boot.imgsystem.img

如果沒有此步驟,將導致手機不斷重啟,無法開機。

LOCAL_SANITIZE := integer

安卓7.1的代碼。一直不斷重啟:system/core/init/bootchart.cpp 這個文件有bug

  • stat.replace(open + 1, close - open - 1, full_name); 這個函數注釋不跑就不會一直重啟了,但是這樣對你bootchart造成多大影響你看看,是否還能分析。

1、首先使能 bootchart,bootchart 操作的前提是存在 enable 標記

所以 在你不需要收集數據的時候別忘了刪除這個標記。

adb shell 'touch /data/bootchart/enabled'

2、抓取

adb shell 'echo $TIME_OUT > /data/bootchart/start' # 添加 bootchart timeout 時間
adb shell reboot # 重啟后生效

其中$TIMEOUT是期望采樣的時間,單位為秒,例如要采樣兩分鍾,則執行:

adb shell 'echo 120 > /data/bootchart-start'

3、提取

可以看到 bootchart 生成的數據文件和 log 都被保存在 /data/bootchart 路徑下,打包以后使用 adb pull 命令將文件拷貝出來

下列的命令也可以使用:system/core/init/grab-bootchart.sh代替

adb shell 'cd /data/bootchart ;tar -zcf boochart.tgz *'

在 HOST 機上分析 bootchart 圖表

PC 機安裝 bootchart 工具

sudo apt-get install -y pybootchartgui

生成 bootchar 圖表

拷貝 bootchart.tgz 到 PC 中,並執行下面的命令生成圖表

bootchart bootchart.tgz

bootchart 圖形分析小技巧

整個圖表以時間線為橫軸,圖標上方為 CPU 和 磁盤的利用情況,下方是各進程的運行狀態條,顯示各個進程的開始時間與結束時間以及對 CPU、I/O 的利用情況,我們關心的各個進程的運行時間以及 CPU 的使用情況,進而優化系統。

可以通過 Laucher 的啟動時間判斷開機完成完成時間

通過進程的時間長短判斷是否存在異常,如圖表上的 第一個 Zygote 進程啟動時間很短,顯示存在異常,后又成功重新啟動了一遍。


免責聲明!

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



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