Android調試工具之Traceview

轉自:http://www.cnblogs.com/devinzhang/archive/2011/12/18/2291592.html


Traceview是android平臺配備的一個很好的性能分析工具。它可以通過圖形化的方式讓我們瞭解我們要跟蹤的程序的性能,並且能具體到method。
進行Traceview的版本限制
對於Android 1.5及以下的版本:不支持
對於Android 1.5以上2.1下(含2.1)的版本:受限支持。trace文件只能生成到SD卡,且必須在程序中加入代碼。
對於Android 2.2上(含2.2)的版本:全支持。可以不用SD卡,不用在程序中加代碼,直接自己用DDMS就可以進程Traceview。
一、Android 1.5以上2.1下(含2.1)的版本中Traceview的使用
首先,必須在程序當中加入代碼,以便生成trace文件,有了這個trace文件我們纔可以將其轉化爲圖形。
1.1、啓動追蹤
使用Debug的以下靜態方法方法來啓動:
static void startMethodTracing(String traceName)
Start method tracing, specifying the trace log file name.
使用指定trace文件的名字和默認最大容量(8M)的方式開始方法的追蹤
static void startMethodTracing()
Start method tracing with default log name and buffer size.
使用默認trace文件的名字(dmtrace.trace)和默認最大容量(8M)的方式開始方法的追蹤
static void startMethodTracing(String traceName, int bufferSize, int flags)
Start method tracing, specifying the trace log file name and the buffer size.
使用指定trace文件的名字和最大容量的方式開始方法的追蹤。並可指定flags.
注:int flags好像沒意義。一般都用0.
static void startMethodTracing(String traceName, int bufferSize)
Start method tracing, specifying the trace log file name and the buffer size.
使用指定trace文件的名字和最大容量的方式開始方法的追蹤。
注1:以上的方法的文件都會創建於SD卡下,即"/sdcard/"下,對默認文件名的就是"/sdcard/dmtrace.trace"
如果沒SD卡,以上方法會拋異常致使程序crash.
注2:如果文件名沒有指定類型,系統爲其加上類型.trace
1.2、停止追蹤
使用Debug的靜態方法方法來停止:
public static void stopMethodTracing ()。
例如在activity的onCreate()中添加Debug.startMethodTracing(), 而在onDestroy()中添加Debug.stopMethodTracing(),
如下:
    @Override
    public void onCreate(Bundle savedInstanceState) {
     Debug.startMethodTracing();
        super.onCreate(savedInstanceState);
     ..............................
     }
protected void onDestroy() {
super.onDestroy();
.................
Debug.stopMethodTracing();

}
對於模擬器我們還得創建一個帶有SD card的AVD,這樣才能使trace文件保存到/sdcard/...當中。
可以在命令中分別單獨創建,也可以在創建avd的時候一起將sdcard創建。創建之後通過DDMS file explore我們就可以看到/sdcard/目錄下有一個trace文件,如果沒有在Debug語句中設置名字則默認爲dmtrace.trace.
1.3、 把trace文件從SD卡拷到電腦上
現在我們把這個文件copy到我們的電腦上指定的目錄:
adb pull /sdcard/dmtrace.trace d:
1.4、啓動traceview可視界面
現在就可以通過命令行來執行traceview了。進入SDK的tools目錄後,執行traceview,如下: 
traceview D:\dmtrace.trace.
之後即可以看到圖形界面了。
1.5、分析traceview結果
Timeline Panel
窗口的上半部分是時間軸面圖(Timeline Panel)
The image below shows a close up of the timeline panel. Each thread’s execution is shown in its own row, with time increasing to the right. 
Each method is shown in another color (colors are reused in a round-robin fashion starting with the methods that have the most inclusive time). 
The thin lines underneath the first row show the extent (entry to exit) of all the calls to the selected method.
界面上方的尺子代表了MethodTracing的時間段(從Debug.startMethodTracing()到Debug.stopMethodTracing()的時間)。
每個線程的函數執行時間圖處於和線程名同一行的右側。
注1線寬度代表執行該函數本身操作所用的時間
注2:函數所調用的子函數時間線夾雜在該函數本身操作所用的時間線之間。
注3:時間線的高度不知道有什麼意義。
注4:函數本身是嵌套的。
注5每行下面粗的線段標註了Profile Panel中被選中函數調用所消耗的時間段。每個線段對應一次函數的運行。
在下面的圖中我們可以看到有11次對LoadListener.nativeFinished()的調用,其中有一次調用耗時特別的多。這裏的11次和圖2並不一致,應該是google的疏忽。
圖1The Traceview Timeline Panel
Traceview的使用 - hubingforever - 民主與科學
 
Profile Panel
窗口的下半界面是對各個函數調用的彙總圖Profile Panel
  Figure 2 shows the profile pane, a summary of all the time spent in a method. 
The table shows both the inclusive and exclusive times (as well as the percentage of the total time). 
Exclusive time is the time spent in the method. 
Inclusive time is the time spent in the method plus the time spent in any called functions. 
We refer to calling methods as "parents" and called methods as "children." When a method is selected (by clicking on it), 
it expands to show the parents and children. Parents are shown with a purple background and children with a yellow background. 
The last column in the table shows the number of calls to this method plus the number of recursive calls. 
The last column shows the number of calls out of the total number of calls made to that method. 
In this view, we can see that there were 14 calls to LoadListener.nativeFinished(); 
looking at the timeline panel shows that one of those calls took an unusually long time.
圖2是對各個函數調用的彙總圖Profile Panel。該表給出了the inclusive and exclusive times及他們所佔有的百分比。
Exclusive time是該函數本身基本操作不包括子函數調用)的時間。
Inclusive time是該函數調用所用的時間(包括子函數調用)的時間。
列1:"Name"表示函數名。
雙擊函數名,可以看到在上半界面是時間軸面圖(Timeline Panel)看他的所消耗的時間段。(用粗的線段標註)。
雙擊函數名左邊的"+"展開後可以看到,該函數的"parents"和"children"
列2"incl%"表示函數的Inclusive time整個MethodTracing時間裏佔的百分比
列3:"Inclusive"表示Inclusive time
列4:"Excl%"表示函數的Exclusive time整個MethodTracing時間裏佔的百分比
列5:"Exclusive"表示Exclusive time
列6:"Calls+RecurCalls/Total"表示對函數的調用次數包括遞歸調用)。如圖2的nativeFinished()那列爲"14+0"表示14次非遞歸調用,0次遞歸調用.
列7:新的版本(比如2.1)還有"time/calls"表示平均的調用時間(即Inclusive time/ total calls)。如圖3。來自google文檔的圖2感覺有老了。
:如果函數A調用函數B那麼函數A稱爲函數B的"parents",函數B稱爲函數A的"children."

以下是翻譯

英語 中文
Incl 調用方法佔用時間百分比
Inclusive 調用方法時間(ms)(包括了所有方法的調用)
Excl 執行方法佔用時間百分比
Exclusive 執行方法佔用時間(ms)(不包括子方法的調用)
Calls+Recur Calls/Total 調用和重複調用的次數
Time/Call 總的時間(ms)
 
圖2Profile PanelTraceview的使用 - hubingforever - 民主與科學
圖3Profile Panel
Traceview的使用 - hubingforever - 民主與科學
 
二、Android 2.2以上(含2.2)的版本中Traceview的簡化使用。
Android 2.2除了像2.1當中那樣使用Traceview,還可以在DDMS使用Traceview,比以前簡化的不少。
* The trace log files are streamed directly to your development machine.
在Android 2.2後可以不用SD卡。Traceview文件流是直接指向開發調試的主機(即DDMS所在的電腦)
To start method profiling:
   1. On the Devices tab, select the process that you want to enable method profiling for.
   2. Click the Start Method Profiling button.
   3. Interact with your application to start the methods that you want to profile.
   4. Click the Stop Method Profiling button. DDMS stops profiling your application and opens Traceview with the method profiling information that was collected between the time you clicked on Start Method Profiling and Stop Method Profiling.
 在DDMS中如何進行Traceview。
 1,在設備表中選中你想進行method trace的進程
 2,單擊Method Profiling按鈕開始method trace。該按鈕在窗口的左上方,它在"stop"按鈕的左方,"Device菜單的正下方"。
 3,method trace進行中
 4,單擊Method Profiling按鈕停止method trace。緊接着系統會自動彈出Traceview窗口來顯示剛纔的method trace結果。
三、Traceview文件格式
本文參照來源:
http://wbdban.javaeye.com/blog/564309
http://developer.android.com/guide/developing/debugging/debugging-tracing.html
http://developer.android.com/guide/developing/debugging/ddms.html#profiling

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章