Analyzing UI Performance with Systrace 使用systrace工具分析ui性能
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.
//修复性能问题就需要知道系统正在做什么
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工具展示了什么地方是cpu回收和被消耗的时间,显示了每一个线程和进程在任何时候都做了什么。它也会捕捉到一些问题并且高亮显示给你,也会提供一些修复这些问题的建议。
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帮助你分析你的应用是如何运行的。它会把系统和应用的线程执行放在一个同样的时间线中。为了分析你的应用,你首先要手机trace log,生成的trace允许你看到更多详细的交互报告,显示出在log跟踪的时间段内系统做了什么
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.
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.
Generating a Trace
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 traces can be run either from a command line or from a graphical user interface. This guide focuses on using the command line options.
Tracing on Android 4.3 and higher
To run a trace on Android 4.3 and higher devices:
- Make sure the device is connected through a USB cable and is enabled for debugging.
- Run the trace with the options you want, for example:
$ cd android-sdk/platform-tools/systrace
$ python systrace.py --time=10 -o mynewtrace.html sched gfx view wm - 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.
Tracing on Android 4.2 and lower
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:
- Use the
--set-tags
option:$ cd android-sdk/platform-tools/systrace
$ python systrace.py --set-tags=gfx,view,wm - 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:
- On the device connected for tracing, navigate to: Settings > Developer options > Monitoring > Enable traces.
- 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:
- Make sure the device is connected through a USB cable and is enabled for debugging.
- 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 //注:命令
python systrace.py -t 30 -o name.html
- 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.
Analyzing a Trace
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.
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.
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.
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.
Investigating Alerts
Systrace does automatic analysis of the events in the trace, and highlights many performance problems as alerts, suggesting what to do next.
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.
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.
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!
Tracing Application Code
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.
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.
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.
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.
Analyzing UI Performance with Systrace 使用systrace工具分析ui性能的更多相关文章
- 使用ANTS Performance Profiler&ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题
一.前言 最近一段时间,网站经常出现两个问题: 1.内存占用率一点点增高,直到将服务器内存占满. 2.访问某个页面时,页面响应过慢,CPU居高不下. 初步判断内存一点点增多可能是因为有未释放的资源一直 ...
- 利用vs自带工具分析程序性能
测试程序写好后可以通过VS2010分析菜单里选择启用性能向导 选择CPU采样后就选择需要分析的项目 测试项目选择完成后就可以运行分析,结束分析后VS2010会提供个详细报告文档 从分析结果来看GetC ...
- Android 性能优化(25)*性能工具之「Systrace」Analyzing UI Performance with Systrace:用Systrace得到ui性能报告
Analyzing UI Performance with Systrace In this document Overview 简介 Generating a Trace 生成Systrace文件 ...
- 使用Systrace分析UI性能
开发应用的时候,应该检查它是否有流畅的用户体验,即60fps的帧率.如果由于某种原因丢帧,我们首先要做的就是知道系统在做什么(造成丢帧的原因). Systrace允许你监视和跟踪Android系统的行 ...
- [Android systrace系列] systrace入门第一式
转载请注明出处:https://www.cnblogs.com/zzcperf/p/13978915.html Android systrace是分析性能问题最称手的工具之一,可以提供丰富的手机运行信 ...
- QMUI UI库 控件 弹窗 列表 工具类 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例
Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和WeUI的组件库 ...
- [Android systrace系列] systrace的信息从哪里来
-------------------------------------------------------------- 这篇文章的小目标: 1. systrace是怎么抓出来的 2. 这些信息的 ...
- (转)[Unity3D]UI方案及制作细节(NGUI/EZGUI/原生UI系统) 内附unused-assets清除实例
转载请留下本文原始链接,谢谢.本文会不定期更新维护,最近更新于2013.09.17. http://blog.sina.com.cn/s/blog_5b6cb9500101bplv.html ...
随机推荐
- Python自动化运维之24、JQuery
jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, ...
- Python中几种数据结构的整理,列表、字典、元组、集合
列表:shoplist = ['apple', 'mango', 'carrot', 'banana']字典:di = {'a':123,'b':'something'}集合:jihe = {'app ...
- RunLoop-b
RunLoop 是 iOS 和 OSX 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何利 ...
- 转:PHP非阻塞模式
你可以任意转摘“PHP非阻塞模式”,但请保留本文出处和版权信息.作者:尘缘,QQ:130775,来源:http://www.4wei.cn/archives/1002336 让PHP不再阻塞当PHP作 ...
- Truck History
poj1789:http://poj.org/problem?id=1789 题意大概是这样的:用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一 ...
- ms08-067
http://wenku.baidu.com/view/f473355077232f60ddcca119.html
- Java类加载的时机
类是什么时候初始化的?类初始化的时候会执行static块,这个是我们知道的.那么我们可以用static块来做个实验. 上面代码输出是: hello worldinitthis is a test这说明 ...
- 分页SQL模板
select * from ( select rownum as rn ,a.* from ( select * from page a where object_id >1000 and ow ...
- Strange Way to Express Integers (一般模线性方程组)
Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8476 Accepted: 2554 Description Elin ...
- 「Poetize9」升降梯口
3056: 升降梯口 Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 43 Solved: 42[Submit][Status] Description ...