Analyzing UI Performance with Systrace

In this document

  1. Overview          简介
  2. Generating a Trace       生成Systrace文件(html)
    1. Tracing on Android 4.3 and higher   4.3以上
    2. Tracing on Android 4.2 and lower    4.2以下
    3. graphical user interface                 用工具生成Systrace 文件
  3. Analyzing a Trace                       分析一个systrace.html文件,其中非绿色帧就有问题。
    1. Inspecting Frames    检查有问题的帧,非绿色就有问题
    2. Investigating Alerts    根据警报信息的提示,解决问题
  4. Tracing Application Code    在代码中添加systrace点,详细记录代码工作以便分析

  See also

  1. Systrace

  While developing your application, you should check that user interactions are buttery smooth, running at a consistent 60 frames per second. If something goes wrong, and a frame gets dropped, the first step in fixing the problem is understanding what the system is doing.

 UI交互时,通常60帧/秒可能保证平滑显示。如果某帧出了问题,可以用Systrace工具分析它。

  The Systrace tool allows you to collect and inspect timing information across an entire Android device, which is called a trace. It shows where time and CPU cycles are being spent, displaying what each thread and process is doing at any given time. It also inpects the captured tracing information to highlight problems that it observes, from list item recycling to rendering content, and provide recommendations about how to fix them. This document explains how to navigate the trace files produced by the tool, and use them to analyze the performance of an application's user interface (UI).

  Systrace是Android4.1中新增的系统行为跟踪工具。Systrace允许你监视和跟踪Android系统的行为(trace)。它会告诉你系统都在哪些工作上花费时间、CPU周期都用在哪里,甚至你可以看到每个线程、进程在指定时间内都在干嘛。它同时还会突出观测到的问题,从垃圾回收到渲染内容都可能是问题对象,甚至提供给你建议的解决方案。但是不能确定具体有问题的代码在哪里,这时可用traceview工具。在Android平台中,Systrace主要由3部分组成:   
  • 内核部分: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.

1.Overview 简介

  Systrace helps you analyze how the execution of your application fits into the many running systems on an Android device. It puts together system and application thread execution on a common timeline. In order to analyze your app with Systrace, you first collect a trace log of your app, and the system activity. The generated trace allows you to view highly detailed, interactive reports showing everything happening in the system for the traced duration.

 使用Systrace前,要有一个 systrace文件。

          Figure 1. An example Systrace, showing 5 seconds of scrolling an app when it is not performing well.

  Figure 1. shows a trace captured while scrolling an app that is not rendering smoothly. By default, a zoomed out view of the traced duration is shown. The horizontal axis is time, and trace events are grouped by process, and then by thread on the vertical axis.

 上图是一个ui卡顿的trace记录,横轴是时间,坚轴是线程/进程。

  The groupings are in the order Kernel, SurfaceFlinger (the android compositor process), followed by apps, each labeled by package name. Each app process contains all of the tracing signals from each thread it contains, including a hierarchy of high level tracing events based on the enabled tracing categories.

2.生成systrace文件(html)

  In order to create a trace of your application, you must perform a few setup steps. First, you must have a device running Android 4.1 (API 16) or higher. Set up the device for debugging, connect it to your development system, and install your application. Some types of trace information, specifically disk activity and kernel work queues, require that you have root access to the device. However, most Systrace log data only requires that the device be enabled for developer debugging.

 生成systrace日志的条件:必需要在4.1以上,并且开启调试模式,有些trace(磁盘活动、内存工作)需要root权限。可以用命令行生成,也可用 graphical user interface 生成。

  Systrace traces can be run either from a command line or from a graphical user interface. This guide focuses on using the command line options.

2.1 在4.3以上设备生成systrace文件(html)

  To run a trace on Android 4.3 and higher devices:

  1. Make sure the device is connected through a USB cable and is enabled for debugging.
  2. Run the trace with the options you want, for example:运行之后,会在 android-sdk/platform-tools/systrace 目录下生成一个mynewtrace.html文件
    $ cd android-sdk/platform-tools/systrace
    $ python systrace.py --time=10 -o mynewtrace.html sched gfx view wm
  3. On the device, execute any user actions you want be included in the trace.

  For more information on the available options for running Systrace, see the Systrace help page.

 Systrace的帮助文档在 Systrace ,其中有各参数的意义。

2.2 在4.2以下生成systrace文件。

  To use Systrace effectively with devices running Android 4.2 and lower, you must configure the types of processes you want to trace before running a trace. The tool can gather the following types of process information:

  • General system processes such as graphics, audio and input processes (selected using trace category tags).
  • Low level system information such as CPU, kernel and disk activity (selected using options).

  To set trace tags for Systrace using the command-line:

  1. Use the --set-tags option:

    $ cd android-sdk/platform-tools/systrace
    $ python systrace.py --set-tags=gfx,view,wm
  2. Stop and restart the adb shell to enable tracing of these processes.
    $ adb shell stop
    $ adb shell start

  To set trace tags for Systrace using the device user interface:

  1. On the device connected for tracing, navigate to: Settings > Developer options > Monitoring > Enable traces.
  2. Select the categories of processes to be traced and click OK.

  Note: The adb shell does not have to be stopped and restarted when selecting trace tags using this method.

  After you have configured the category tags for your trace, you can start collecting information for analysis.

  To run a trace using the current trace tag settings:

  1. Make sure the device is connected through a USB cable and is enabled for debugging.
  2. Run the trace with the low-level system trace options and limits you want, for example:
    $ python systrace.py --cpu-freq --cpu-load --time=10 -o mytracefile.html
  3. On the device, execute any user actions you want be included in the trace.

  For more information on the available options for running Systrace, see the Systrace help page.

2.3 用工具生成Systrace 文件

本小节是从其它地方引入的。

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.5.1

  1. In Android Studio, open an Android application project.
    1. Open the Device Monitor by selecting Tools > Android > Android Device 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.
      设置记录参数,注意其中的时间,这里是10秒,设置好之后开始计时,10秒后自动停止记录,然后在/home/wefe/目录下生成wx_trace.html

Using Device Monitor 直接启动 android 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.

3.分析一个systrace.html文件

  After you have generated a trace, open the output html file using a web browser. This section explains how to analyze and interpret the information that the tool produces to find and fix UI performance problems.

3.1 Inspecting Frames  检察有问题的帧,非绿色就有问题。

  Each app that is rendering frames shows a row of frame circles, which are typically colored green. Circles that are colored yellow or red, exceeding the 16.6 millisecond run time limit required to maintain a stable 60 frames per second. Zoom in using the 'w' key to see the frames of your application, and look for long-running frames getting in the way of smoothness.

 正常绘制是1秒60帧,大约一帧16.6毫秒,在这个值以下是正常颜色绿色,如果超过它就会变成红色、黄色。非绿色的都说明有问题。单点击后可查看详情,根据警报提示解决问题。
 在systrace.html中,按w键放大,s键缩小,a向左移动,d向右移动。“?”键可以查看全部其它键的功能。如果打不开html,换浏览器。

  Note: Hit the '?' key, or the button in the top right for help navigating the trace.

              Figure 2. Systrace display after zooming in on a long-running frame.

  Clicking on one such frame highlights it, focusing only on the work done by the system for that frame. On devices running Android 5.0 (API level 21) or higher, this work is split between the UI Thread and RenderThread. On prior versions, all work in creating a frame is done on the UI Thread.

 在上图中,点击一个帧时,只会显示系统在该帧下的工作。在5.0以前,这些工作都在UI Thread线程中,5.0以后,分到了两个线程中,一个是UI Thread ,一个是 RenderThread。

  Click on individual components of the frame to see how long they took to run. Some events, such as performTraversals, describe what the system is doing in that method when you select it. Selecting a frame displays any alerts present in that frame.

3.2 分析警报信息,解决问题

  Systrace does automatic analysis of the events in the trace, and highlights many performance problems as alerts, suggesting what to do next.

 Systrace以警报信息方式提示问题。

          Figure 3. Selecting the problematic frame, an alert is shown identifying a problem.

  After you select a slow frame such as the one shown in Figure 3, an alert may be displayed. In the case above, it calls out that the primary problem with the frame is too much work being done inside ListView recycling and rebinding. There are links to the relevant events in the trace, which can be followed to explain more about what the system is doing during this time.

 选中其中某一个非绿色(F) 或 (▲) 之后,可以看见相关的警报信息,如名称,消耗,描述等。知道警报信息之后,可以用Traceview分析具体出问题的函数。

  If you see too much work being done on the UI thread, as in this case with this ListView work, you can use Traceview, the app code profiling tool, to investigate exactly what is taking so much time.

  Note that you can also find about every alert in the trace by clicking the Alerts tab to the far right of the window. Doing so expands the Alerts panel, where you can see every alert that the tool discovered in your trace, along with an occurrence count.

 可以打开右侧的 Alerts 面板,快速查看所有警报。如下图青色部分。

                Figure 4. Clicking the Alert button to the right reveals the alert tab.

  The Alerts panel helps you see which problems occur in the trace, and how often they contribute to jank. Think of the alerts panel as a list of bugs to be fixed, often a tiny change or improvement in one area can eliminate an entire class of alerts from your application!

4.在代码中添加systrace记录点

  The tracing signals defined by the framework do not have visibility into everything your application is doing, so you may want to add your own. In Android 4.3 (API level 18) and higher, you can use the methods of the Trace class to add signals to your code. This technique can help you see what work your application's threads are doing at any given time. Tracing begin and end events do add overhead while a trace is being captured, a few microseconds each, but sprinkling in a few per frame, or per worker thread task can go a long way to adding context to a trace of your app.

 在4.3以后,可以使用Trace类在代码中打Systrace,来详细记录代码的行为。通常Trace类要一定的开销。下面代码是示例:

  The following code example shows how to use the Trace class to track execution of an application method, including two nested code blocks within that method.

 public void ProcessPeople() {
Trace.beginSection("ProcessPeople");
try {
Trace.beginSection("Processing Jane");
try {
// code for Jane task...
} finally {
Trace.endSection(); // ends "Processing Jane"
} Trace.beginSection("Processing John");
try {
// code for John task...
} finally {
Trace.endSection(); // ends "Processing John"
}
} finally {
Trace.endSection(); // ends "ProcessPeople"
}
}

  Note: When you nest trace calls within each other, the endSection() method ends the most recently called beginSection(String) method. This means that a trace started within another trace cannot extend beyond the end of the enclosing trace, so make sure your beginning and ending method calls are properly matched to measure your applications processing.

 注意:在同一个线程中,用 beginSection(String)endSection() 成对出现可打代码记录,并且它们总是与最近的配对,注意是同一个线程中。

  Note: Traces must begin and end on the same thread. Do not call beginSection(String) on one thread of execution and then attempt to end the trace with a call to endSection() on another thread.

  When using application-level tracing with Systrace, you must specify the package name of your application in the user interface or specify the -a or --app= options on the command line. For more information, see the Systrace usage guide.

 Systrace 使用指南在: Systrace usage guide  。当在使用代码记录systrace时,必需在命令行中指定 -a --app=xxxxx 参数。

  You should enable app level tracing when profiling your app, even if you have not added signals yourself. Library code can include very useful tracing signals when you enable application-level tracing. The RecyclerView class is a great example of this, providing information about several important stages of work it executes.

Android 性能优化(25)*性能工具之「Systrace」Analyzing UI Performance with Systrace:用Systrace得到ui性能报告的更多相关文章

  1. Android开发学习之路--性能优化之常用工具

      android性能优化相关的开发工具有很多很多种,这里对如下六个工具做个简单的使用介绍,主要有Android开发者选项,分析具体耗时的Trace view,布局复杂度工具Hierarchy Vie ...

  2. Android 性能优化(19)*代码优化11条技巧:Performance Tips

    Performance Tips 1.In this document Avoid Creating Unnecessary Objects 避免多余的对象 Prefer Static Over Vi ...

  3. Android 性能优化(1)性能工具之「 lint 」 :Improving Your Code with lint:优化代码

    Improving Your Code with lint 1.See Also lint (reference) Using Android Annotations In addition to t ...

  4. PLSQL_性能优化效能跟踪工具DBMS_PROFILER分析(案例)

    2014-06-01 Created By BaoXinjian

  5. PLSQL_性能优化效能跟踪工具SQL Trace分析(案例)

    2014-06-25 Created By BaoXinjian

  6. Android 性能优化(6)网络优化( 2) Analyzing Network Traffic Data:分析网络数据

    Analyzing Network Traffic Data 1.This lesson teaches you to Analyze App Network Traffic Analyze Netw ...

  7. Android内存管理(12)*「实例」用Monitor 生成.hprof文件 并分析内存泄漏

    参考 http://blog.csdn.net/xiaanming/article/details/42396507 基本步骤: 1,准备一个有内存泄漏的代码 2,如何发现内存泄漏 3,生成.hpro ...

  8. Mysql性能优化全揭秘-庖丁解牛

    「为什么写」 一直想写数据库相关的文章,最直接的原因是数据库这块我们工作中每天都会用到,也是面试求职绕不开的话题,无论你是何种测试,优秀的数据库能力都会非常加分,最近我在总结数据库性能优化这块内容,性 ...

  9. Android性能优化的浅谈

    一.概要: 本文主要以Android的渲染机制.UI优化.多线程的处理.缓存处理.电量优化以及代码规范等几方面来简述Android的性能优化 二.渲染机制的优化: 大多数用户感知到的卡顿等性能问题的最 ...

随机推荐

  1. readdir() 获取文件类型

    readdir()获取文件类型 //// 字符设备文件 type =2, filename207=tty0 crw-rw----  1 root root     4,  0 04-10 16:28 ...

  2. android view自定义

    转载自:http://blog.csdn.net/iispring/article/details/50708044

  3. hdu - 1254 推箱子 (bfs+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目意思很简单,只要思路对就好. 首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工 ...

  4. 2014ACM/ICPC亚洲区西安站现场赛 F color(二项式反演)

    题意:小球排成一排,从m种颜色中选取k种颜色给n个球上色,要求相邻的球的颜色不同,求可行的方案数,答案模1e9+7.T组数据,1<= n, m <= 1e9, 1 <= k < ...

  5. hadoop(2)hadoop配置

    hadoop入门(二) hadoop的配置 1.本地模式 2.伪分布式 3.分布式     一.配置linux环境: 1打开虚拟网络编辑器,选择 VMnet1 仅主机模式, 子网 IP 设为 192. ...

  6. Systemtap工具查看系统资源使用

    http://www.xbwolf.com/507 http://blog.csdn.net/kafeiflynn/article/details/6429976 安装步骤yum install sy ...

  7. SqlServer2008发布订阅(数据同步)

    目录 1. 发布必备条件 1.1. 数据库故障还原模型必需为完全还原模型 1.2. 数据库被同步的数据表必须有主键 1.3. 计算机名称来进行SQLServer服务器的注册 1.4. SQLServe ...

  8. [Jexus系列] 一、安装并运行 Jexus

    注意,本教程使用的jexus版本为5.8.3专业版,操作系统为 Ubunutu 16.04 64位 一.创建默认站点 不熟悉vim的可以看这个: vim超简单入门教程 sudo mkdir -p /v ...

  9. NA路由①

    Cisco设备的端口:     在Cisco的路由器上都有一个带外网管口(Console/AUX):     Con口主要用于本地的con线进行本地网管:     AUX口主要与Modem连接通过固话 ...

  10. HTML5:理解head

    HTML文档的head部分,通常包含指定页面标题.为搜索引擎提供关于页面本身的信息.加载样式表,以及加载JavaScript文件(出于性能考虑.多数时候放在页面底部</body>标签结束前 ...