Android Performance

Android Performance Optimization - Introduction to Systrace (Part 1)

Word count: 1.3kReading time: 8 min
2015/01/30
loading

Note: This content is outdated. Please refer to the new Systrace Series Articles for updated information.

This is the first article in the Android Performance Optimization Tools series. This series mainly introduces the tools used during the Android performance optimization process, how to use these tools to discover problems, and how to solve them. In terms of performance optimization, Android provides many performance tools for everyone to use. Following our consistent “discover problem - solve problem” thinking, discovering the problem is the most important part. Trying to solve a problem without first identifying it properly often leads to half the effort for twice the result.

In this article, we’ll start with a brief introduction to the Systrace tool.

Introduction to Systrace

Systrace is a performance data sampling and analysis tool introduced in Android 4.1. It helps developers collect execution information from key Android subsystems (such as SurfaceFlinger, WindowManagerService, and other critical Framework modules, services, and the View system), allowing for a more intuitive analysis of system bottlenecks and performance improvements.

Systrace’s capabilities include tracking system I/O operations, kernel workqueues, CPU load, and the health of various Android subsystems. On the Android platform, it’s composed of three main parts:

  • Kernel Space: Systrace leverages the ftrace feature in the Linux Kernel. Therefore, to use Systrace, the relevant ftrace modules in the kernel must be enabled.
  • Data Collection: Android defines a Trace class that applications can use to output statistical information to ftrace. Additionally, the atrace program in Android reads statistical info from ftrace and passes it to data analysis tools.
  • Data Analysis Tools: Android provides systrace.py (a Python script located in Android SDK directory/platform-tools/systrace that calls atrace internally) to configure data collection (such as tags, output filename, etc.), collect ftrace statistics, and generate a resulting HTML file for user viewing. Essentially, Systrace is a wrapper around the Linux Kernel’s ftrace. Applications need to utilize the Trace class provided by Android to use Systrace.

Official documentation and usage for Systrace can be found here: Systrace

Simple Usage of Systrace

Before using Systrace, understand how to use it on various platforms. Given the widespread use of Eclipse and Android Studio, I’ll quote the official usage methods, although the process is the same regardless of the tool:

  • Prepare the UI you want to trace on the phone.
  • Click to start capturing (or execute the command in the CLI).
  • Perform operations on the phone.
  • Once the preset time is up, a Trace file will be generated. Open this file in Chrome for analysis.

Using Eclipse

  1. In Eclipse, open an Android application project.

    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.

    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.

    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

The command-line approach is more flexible and faster. Once configured, you can get results quickly (highly recommended).

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

You can see the location of the Systrace tool from the command above. You just need to configure the corresponding path and Alias in Bash, and it will be very fast to use. Also, User builds cannot capture Traces; only ENG or Userdebug builds can.

After capturing, a corresponding Trace file will be generated. Note that this file can only be opened by Chrome. We will discuss how to analyze Trace files in the sections below. Regardless of the tool used, you will be asked to select parameters before capturing. Here’s what those parameters mean:

  • -h, –help Show the help message.

  • -o Write the HTML trace report to the specified file (i.e., the output filename).

  • -t N, –time=N Trace activity for N seconds. The default value is 5 seconds. (Trace capture duration, e.g., -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.

While there are many parameters, you don’t need to consider all of them when using tools; just check the corresponding boxes. For the command line, you’ll manually add the parameters.

We usually configure this command as an 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’

This way, you can just type st-start when using it. Of course, to distinguish and maintain each file, you also need to add -o xxx.Trace. You don’t need to understand all the commands and parameters at once; just remember the simple usage. You’ll become familiar with them during the analysis process.

About Me && Blog

Below is my personal intro and related links. I look forward to exchanging ideas with fellow professionals. “When three walk together, one can always be my teacher!”

  1. Blogger Intro
  2. Blog Content Navigation: A guide for my blog content.
  3. Curated Excellent Blog Articles - Android Performance Optimization Must-Knows
  4. Android Performance Optimization Knowledge Planet

One walks faster alone, but a group walks further together.

Scan WeChat QR Code

CATALOG
  1. 1. Introduction to Systrace
  2. 2. Simple Usage of Systrace
    1. 2.0.1. Using Eclipse
    2. 2.0.2. Using ** Android Studio**
    3. 2.0.3. Using Device Monitor
    4. 2.0.4. Command Line Usage
  • About Me && Blog