Android性能優化之Systrace工具介紹(一) _&& Systrace生成的trace.html打開空白或者打不開的解決辦法

1.必須用Chrome打開html

2.在mac電腦上,可能Chrome打開也是空白,解決辦法是:在chrome地址欄中輸入」chrome:tracing」,而後點擊load按鈕load你的trace.html文件。python

 

Systrace簡單介紹

Systrace是Android4.1中新增的性能數據採樣和分析工具。它可幫助開發者收集Android關鍵子系統(如surfaceflinger、WindowManagerService等Framework部分關鍵模塊、服務,View系統等)的運行信息,從而幫助開發者更直觀的分析系統瓶頸,改進性能。android

Systrace的功能包括跟蹤系統的I/O操做、內核工做隊列、CPU負載以及Android各個子系統的運行情況等。在Android平臺中,它主要由3部分組成:web

  • 內核部分:Systrace利用了Linux Kernel中的ftrace功能。因此,若是要使用Systrace的話,必須開啓kernel中和ftrace相關的模塊。
  • 數據採集部分:Android定義了一個Trace類。應用程序可利用該類把統計信息輸出給ftrace。同時,Android還有一個atrace程序,它能夠從ftrace中讀取統計信息而後交給數據分析工具來處理。
  • 數據分析工具:Android提供一個systrace.py(python腳本文件,位於Android SDK目錄/tools/systrace中,其內部將調用atrace程序)用來配置數據採集的方式(如採集數據的標籤、輸出文件名等)和收集ftrace統計數據並生成一個結果網頁文件供用戶查看。 從本質上說,Systrace是對Linux Kernel中ftrace的封裝。應用進程須要利用Android提供的Trace類來使用Systrace.
    關於Systrace的官方介紹和使用能夠看這裏:Systrace

Systrace簡單使用

使用Systrace前,要先了解一下Systrace在各個平臺上的使用方法,鑑於你們使用Eclipse和Android Studio的居多,因此直接摘抄官網關於這個的使用方法,不過無論是什麼工具,流程是同樣的:chrome

  • 手機準備好你要進行抓取的界面
  • 點擊開始抓取(命令行的話就是開始執行命令)
  • 手機上開始操做
  • 設定好的時間到了以後,會將生成Trace文件,使用Chrome將這個文件打開進行分析

Using Eclipse

  1. In Eclipse, open an Android application project.android-studio

    1. Switch to the DDMS perspective, by selecting Window > Perspectives > DDMS.
    2. In the Devices tab, select the device on which to run a trace. If no devices are listed, make sure your device is connected via USB cable and that debugging is enabled on the device.
    3. Click the Systrace icon at the top of the Devices panel to configure tracing.
    4. Set the tracing options and click OK to start the trace.

Using Android Studio

  1. In Android Studio, open an Android application project.app

    1. Open the Device Monitor by selecting Tools > Android > Monitor.
    2. In the Devices tab, select the device on which to run a trace. If no devices are listed, make sure your device is connected via USB cable and that debugging is enabled on the device.
    3. Click the Systrace icon at the top of the Devices panel to configure tracing.
    4. Set the tracing options and click OK to start the trace.

Using Device Monitor

  1. Navigate to your SDK tools/ directory.ide

    1. Run the monitor program.
    2. In the Devices tab, select the device on which to run a trace. If no devices are listed, make sure your device is connected via USB cable and that debugging is enabled on the device.
    3. Click the Systrace icon at the top of the Devices panel to configure tracing.
    4. Set the tracing options and click OK to start the trace.

Command Line Usage

命令行形式比較靈活,速度也比較快,一次性配置好以後,之後再使用的時候就會很快就出結果(強烈推薦)工具

1
2 
$ cd android-sdk/platform-tools/systrace
$ python systrace.py --time=10 -o mynewtrace.html sched gfx view wm 

從上面的命令能夠看到Systrace工具的位置,只須要在Bash中配置好對應的路徑和Alias,使用起來仍是很快速的。另外User版本是不能夠抓Trace的,只有ENG版本或者Userdebug版本才能夠。性能

抓取結束後,會生成對應的Trace文件,注意這個文件只能被Chrome打開。關於如何分析Trace文件,咱們下面的章節會講。不論使用那種工具,在抓取以前都會讓選擇參數,下面說一下這些參數的意思:

  • -h, –help Show the help message.(幫助)
  • -o Write the HTML trace report to the specified file.(即輸出文件名,)
  • -t N, –time=N Trace activity for N seconds. The default value is 5 seconds. (Trace抓取的時間,通常是 : -t 8)
  • -b N, –buf-size=N Use a trace buffer size of N kilobytes. This option lets you limit the total size of the data collected during a trace.
  • -k
  • —ktrace= Trace the activity of specific kernel functions, specified in a comma-separated list.
  • -l, –list-categories List the available tracing category tags. The available tags are(下面的參數不用翻譯了估計你們也看得懂,貼官方的解釋也會比較權威,後面分析的時候咱們會看到這些參數的做業的):

    • gfx - Graphics
    • input - Input
    • view - View
    • webview - WebView
    • wm - Window Manager
    • am - Activity Manager
    • audio - Audio
    • video - Video
    • camera - Camera
    • hal - Hardware Modules
    • res - Resource Loading
    • dalvik - Dalvik VM
    • rs - RenderScript
    • sched - CPU Scheduling
    • freq - CPU Frequency
    • membus - Memory Bus Utilization
    • idle - CPU Idle
    • disk - Disk input and output
    • load - CPU Load
    • sync - Synchronization Manager
    • workq - Kernel Workqueues Note: Some trace categories are not supported on all devices. Tip: If you want to see the names of tasks in the trace output, you must include the sched category in your command parameters.
  • -a

  • —app= Enable tracing for applications, specified as a comma-separated list of package names. The apps must contain tracing instrumentation calls from the Trace class. For more information, see Analyzing Display and Performance.
  • —link-assets Link to the original CSS or JavaScript resources instead of embedding them in the HTML trace report.
  • —from-file= Create the interactive Systrace report from a file, instead of running a live trace.
  • —asset-dir= Specify a directory for the trace report assets. This option is useful for maintaining a single set of assets for multiple Systrace reports.
  • -e
  • —serial= Conduct the trace on a specific connected device, identified by its device serial number.
    上面的參數雖然比較多,但使用工具的時候不需考慮這麼多,在對應的項目前打鉤便可,命令行的時候纔會去手動加參數:

咱們通常會把這個命令配置成Alias,配置以下:

1
2 
alias st-start='python /home/gaojianwu/Software/android-studio/sdk/platform-tools/systrace/systrace.py' alias st-start-gfx-trace = ‘st-start -t 8 gfx input view sched freq wm am hwui workq res dalvik sync disk load perf hal rs idle mmc’ 

這樣在使用的時候,能夠直接敲 st-start-gfx-mx4 便可,固然爲了區分和保持各個文件,還須要加上 -o xxx.Trace .上面的命令和參數沒必要一次就理解,只須要記住如何簡單使用便可,在分析的過程當中,這些東西都會慢慢熟悉的。

 

轉自:http://blog.csdn.net/hard_working1/article/details/50602345

相關文章
相關標籤/搜索