Traceview和dmtracedump分析工具
Traceview的界面佈局
- 時間線面板 ---- 描述每個線程和方法何時起止
- 分析面板 ---- 提供每個方法的情況概要
時間線面板
分析面板
Traceview文件格式
data文件格式
key文件格式
創建跟蹤文件
- 在代碼中,使用Debug類,調用這個類的方法來開始和結束跟蹤,並將跟蹤信息寫入到磁盤。這種方式很精確,因爲你可以在代碼中精確的指定在什麼位置開始和結束跟蹤數據。
- 使用DDMS的方法分析特性。這種方法不那麼精確,因爲你沒有更改代碼,而只是通過DDMS指定什麼時候開始和結束log。儘管這種方式,不能精確控制log的地方,但是如果你無法接觸程序代碼,或者不需要第一種方式那麼精確時,這仍然是一種很有用的方法。
- 如果你使用Debug類,你的設備或者模擬器需要有SD卡,並且程序有寫入SD卡的權限。
- 如果你使用DDMS,Android 1.5的設備是不支持的。
- 如果你使用DDMS,Android2.1或之前的設備,必須有SD卡,並且程序有寫入SD卡的權限。
- 如果你使用DDMS,Android2.2或之後的設備,不需要SD卡,trace log文件直接傳到了你的開發機上。
// start tracing to "/sdcard/calc.trace" Debug.startMethodTracing("calc"); // ... // stop tracing Debug.stopMethodTracing();程序調用startMethodTracing()方法,系統會創建名爲.trace的log文件。這個文件包含二進制的方法跟蹤數據和線程方法名稱映射表。
複製跟蹤文件到主機
adb pull /sdcard/calc.trace /tmp
用Traceview查看trace文件
traceview /tmp/calc注意:如果你要查看那些使用了Proguard的程序(Release模式編譯)生成的log文件,一些方法和成員名稱可能會被混淆。你可以使用Proguard的mapping.txt來計算出原始的未混淆的名稱。關於該文件的更多信息,請查看Proguard文檔。
使用dmtracdedump
- ---- 調用的參考編號,trace log中用到的。
- ---- Inclusive消耗時間(方法,包括所有child方法消耗的毫秒數)
- ---- Exclusive消耗時間(方法,不包括任何child方法消耗的毫秒數)
- ----
調用數
dmtracedump [-ho] [-s sortable] [-d trace-base-name] [-g outfile] <</span>trace-base-name>dmtracedump從 .data和 .key文件中載入trace log數據。下表列出了dmtracedump的選項。
Option | Description |
---|---|
-d |
Diff with this trace name |
-g |
Generate output to |
-h |
Turn on HTML output |
-o |
Dump the trace file instead of profiling |
-d |
URL base to the location of the sortable javascript file |
-t |
Minimum threshold for including child nodes in the graph (child's inclusive time as a percentage of parent inclusive time). If this option is not used, the default threshold is 20%. |
$ dmtracedump.exe
Copyright (C) 2006 The Android Open Source Project
usage: G:\android-sdk-windows\tools\dmtracedump.exe [-ho] [-s sortable] [-d trace-file-name] [-g outfile] trace-file-name
-d trace-file-name - Diff with this trace
-g outfile - Write graph to 'outfile'
-k - When writing a graph, keep the intermediate DOT file
-h - Turn on HTML output
-o - Dump the dmtrace file instead of profiling
-s - URL base to where the sortable javascript file
-t threshold - Threshold percentage for including nodes in the graph
Traceview的已知問題
- 如果在分析期間,線程退出,那麼該線程名字不會出現。
- VM重用線程ID。如果一個線程退出,又起了另一個線程,它們的ID可能相同。
$ traceview.bat ./ddms61735.trace
The standalone version of traceview is deprecated.
Please use Android Device Monitor (tools/monitor) instead.
trace file './ddms61735.trace' not found
$ monitor.bat
大家可以看到一般一個虛擬機實例進程都包含有如下7個通用的線程,下面大致講一下這些線程的作用和創建流程。
1. main
這個就是主線程了。具體流程待細述。
2. HeapWorker
一個異步的工作線程,處理那些需要在單獨線程裏面做的避免同步問題的堆操作。其源代碼在dalvik/vm/alloc/HeapWorker.*部分。
3. Signal Catcher
這個線程是用來捕獲linux信號和做一些後續處理的。比如說,當一個SIGQUIT (Ctrl-\)信號到達後,這個線程就會掛起虛擬機,並且將所有線程的狀態信息輸出到log。其源代碼在dalvik/vm/SignalCatcher.*部分。
4. JDWP
這個線程是用來實現Java Debug Wire Protocol的。如果命令行調試器的參數爲"suspend=y",這樣會暫停虛擬機。這個估計和eclipse的調試和ddms等調試工具相關。其源代碼在dalvik/vm/jdwp/*部分。
5. Stdio Converter
這個線程從標準輸出和標準錯誤輸出讀取信息並將它們轉換爲log信息。其源代碼在dalvik/vm/StdioConverter.*部分。
6. Compiler
Android's Jit獨立於目標平臺的部分。其源代碼在dalvik/vm/compiler/Compiler.*和dalvik/vm/interp/Jit.*等部分。
7. Binder Thread #%d
使用binder進行通訊時用到的線程。其源代碼在frameworks/base/libs/binder/*等部分。
以下的線程屬於system_server和應用程序專有線程,視具體應用的需求而定。
8. system_server專有
android.server.ServerThread
ActivityManager
ProcessStats
PackageManager
FileObserver
AccountManagerService
SyncHandlerThread
UEventObserver
PowerManagerService
AlarmManager
WindowManager
InputDeviceReader
WindowManagerPolicy
InputDispatcher
ConnectivityThread
WifiService
WifiWatchdogThread
LocationManagerService
AudioService
GpsEventThread
GpsNetworkThread
android.hardware.SensorManager$SensorThread
watchdog
Wallpaper
com.android.server.MountListener
9. misc
其他部分線程由java層的api提供,Thread等。