Highly recommended article: Welcome Favorite
Android Dry Share
Systrace
Allow all the processes running on your system-level collection and inspection equipment timing information. It will come from Android
data kernel (such as CPU调度
a program, disk activity and application threads) are combined to generate a HTML
report.
This article describes the Android
part of the development of knowledge by reading this article, you will reap the following:
- About Systrace
- Use Systrace
- Using the command line to fetch Systrace
- Systrace detection performance using the UI
- Use Systrace detection warning and dropped frames issue
- View shortcuts trace file
- Specific analysis codes to add trace marks problem
- Use TraceView analysis of trace Log
1.Systrace Profile
Systrace allow all the processes running on your system-level collection and inspection equipment timing information. It will come from Android
data kernel (such as CPU调度
a program, disk activity and application threads) combined to generate an HTML report.
2.Systrace use
Grab method Systrace
If you want to analyze Android
the system or app
problem, we first need to grab Systrace
the file system to analyze and identify the cause Caton, or app反应慢
reasons, then solve the problems caused by slow on the source.
Grab Systrace method:
- First phone link, opens
Android Device Monitor
, select the process to be analyzed (for examplecom.google.process.gapps
), clickCapture system wide trace using Android systrace
(where the arrow at the top right corner)
- Configure content needs to grab the Systrace
At this time, according to different needs, different crawl configuration trace
time (time do not too long, otherwise it will lead crawls partial loss), and other content, then click OK
the operating system to analyze Caton or app
slow running section, you'll runtime information collected automatically, and then Chrome 浏览器打
open the resulting trace
file.
Note:
If the crawl several times trace, in order to avoid data loss, please clear the contents of the cache, and clean up the place inAndroid Device Monitor
the lower right corner, as shown below
With Chrome 浏览器
open generated trace 文件
, as shown below, which will include each CPU,
and graphics rendering, input event content and the like.
Crawl Trace
report provides Android
system processes the whole situation in a specific time period. It checks the tracking information captured and highlight its examination to the issue, such as when displaying motion or animation UI粗糙
, and provide advice on how to solve these problems.
However, it systrace
does not collect information about code execution in the application process. About which methods your application performs and how many CPU资源
more detailed information, please use Android Studio
the built-in CPU分析器
, or generate a trace log and used Traceview
to view them.
3. Use the command line to fetch Systrace
This document describes how the command line to generate systrace
reports, browse trace files generated by the tool, and use them to analyze and improve the application's user interface (UI)
performance.
Crawl systrace
before, complete the following steps:
- Download and install
Android SDK Tools
- Mounted
Python ,
and contained in the execution path of the workstation. - Connect your phone, open the developer options in the
USB Debug
options. Systrace
Storage path followsandroid-sdk/platform-tools/systrace/
使用命令行抓取 Systrace
的语法如下:
python systrace.py [options] [categories]
例如,以下命令调用systrace
在10
秒钟内记录设备进程,包括图形进程,并生成一个名为mynewtrace
的HTML
报告:
python systrace.py --time=10 -o mynewtrace.html gfx
如果不指定任何类别或选项,systrace
将生成包含所有可用类别的报告,并使用默认设置。 可用的类别取决于您使用的连接设备。
4.使用Systrace 检测UI 性能
systrace
对于检查应用程序的UI性能
特别有用,因为它可以分析您的代码和帧速率以识别问题区域并提供可能的解决方案。 开始,按照以下步骤进行:
- 连接手机并运行您的
app
。 - 使用以下命令运行systrace:
python systrace.py view --time = 10
- 操作您的应用 10秒后,
systrace
生成一个HTML报告。 - 使用网络浏览器打开HTML报告。
您现在可以与报告进行交互,以便在记录期间检查设备CPU
使用情况。 以下部分介绍如何检查报告中的信息以查找和修复UI性能问题
。
5. 使用Systrace 检测警告以及掉帧问题
如下报告列出了每个进程呈现UI帧,并指示沿着时间线的每个渲染帧。 在绿色框架
圆圈中指示在16.6毫秒内呈现的帧以保持每秒稳定60帧。 花费16.6毫秒以上渲染的帧用黄色
或红色
框圈表示。
注意:
在运行Android 5.0(API级别21)
或更高版本的设备上, UI 渲染的工作是在UI Thread
和RenderThread
两个线程完成。 在之前的版本中,创建框架的所有工作都是在UI Thread
上完成的。
点击一个F框架圈
可以提供有关系统为渲染该框架而完成的工作的其他信息,包括警报。 它还向您展示了在渲染该框架时系统正在执行的方法,因此您可以调查这些方法是否导致UI jank
。
选择慢帧后,您可能会在报告的底部窗格中看到警报。 上图中显示的Alert
提出,框架的主要问题是在ListView
回收和重新绑定中花费了太多的时间。 跟踪中有相关事件的链接可以解释更多关于系统在这段时间内正在做什么的事情。
要查看工具在trace
中发现的每个Alert
以及设备触发Alert
的次数,请单击窗口最右侧的Alerts
选项卡,如下图所示。
Alerts
面板可帮助您查看发生了哪些问题,以及发生的频率。 将Alert
面板看作是要修复的错误列表, 通常情况下,一个区域的微小变化或改进可以消除应用程序中的全部警报。
如果你在UI Thread
上做太多的工作,你需要找出哪些方法消耗了太多的CPU时间
。
一种方法是添加跟踪标记(请参阅检测应用代码)到您认为会导致这些瓶颈的方法,以查看这些函数调用是否显示在systrace
中。 如果您不确定哪些方法可能会在UI线程上造成瓶颈,请使用Android Studio
的内置CPU分析器,
或者生成跟踪日志并使用Traceview
查看它们。
6. 查看trace 文件的快捷键
7. 代码中添加trace 标记具体分析问题所在
由于systrace
是在系统级显示有关进程的信息,因此很难在HTML
报告中的某个特定时间知道您的应用程序正在执行什么方法。 在Android 4.3(API级别18)
及更高版本中,您可以使用代码中的Trace
类在HTML
报告中标记执行事件。 您不需要用代码来记录systrace
的跟踪记录,但是这样做可以帮助您查看应用程序代码的哪些部分可能会导致线程挂起或UI断线。 这种方法与使用Debug
类不同,Trace
类简单地将标志添加到systrace
报告中,而Debug
类可帮助您通过生成.trace
文件来检查详细的app CPU
使用情况。
要生成包含已检测的跟踪事件的systrace HTML
报告,需要使用-a
或--app
命令行选项运行systrace
,并指定应用程序的包名称。
通常我们在怀疑引起jank代码地方,添加如下内容:
Trace.beginSection("MyAdapter.onCreateViewHolder");
和 Trace.endSection();
,主要这两个是成对出现的。
多次调用beginSection(String)
时,调用endSection()
只会结束最近调用的beginSection(String)
方法。 因此,对于嵌套的调用,例如上面示例中的调用,您需要确保通过调用endSection()
将每个调用正确匹配到beginSection()
。
此外,您不能在一个线程上调用beginSection()
并从另一个线程结束 - 您必须从同一线程调用endSection()
。
8. 使用TraceView 分析trace Log
Traceview
是提供跟踪日志的图形表示的工具。 您可以通过使用Debug
类来设置代码来生成日志。 这种跟踪方法非常精确,因为您可以准确指定要启动的代码中的哪个位置,并停止记录跟踪数据。 如果尚未生成这些跟踪日志并将其从连接的设备保存到本地计算机,请转至通过检测应用程序生成跟踪日志。 使用Traceview
检查这些日志可帮助您调试您的应用程序并剖析其性能。
提示:可以使用命令行中的dmtracedump
来生成跟踪日志文件的图形调用堆栈图。
如果您不需要查看通过使用Debug
类检测应用程序来记录的跟踪日志,则可以使用Android Studio 3.0
及更高版本中包含的CPU
分析器来查看应用程序的线程和记录方法跟踪。
使用Android Device Monitor
可以查看trace Log内容,步骤如下,打开Android Device Monitor
,选择File
,然后打开*.trace log分析。
当然,你也可以使用Android Device Monitor
的图形按键进行trace
的抓取与查看。
打开跟踪日志后,Traceview
使用以下两个窗格显示日志数据:
-
- 时间轴窗格:
描述每个线程何时进入和退出方法的时间轴窗格
- 时间轴窗格:
-
- 配置文件窗格:
总结每个线程在跟踪日志的执行期间的配置文件窗格
以下各节提供有关traceview输出窗格的附加信息。
- 配置文件窗格:
1.时间轴窗格
每个线程的执行都显示在自己的进程中,并且时间向右增加。 每种方法都以不同的颜色显示。 第一行下方的细线显示所选方法的子项(从入口到出口),如下图所示。
2. 配置文件窗格
As shown below, the time profile of each pane provides a system executing a method of tracking log during the execution list and the method used.
Another method of calling a method called the parent class method calls the method is referred to as the parent of its children. When you choose a method by clicking method, it will show its parent and children under two separate nodes.
For each method (top node), and contains the table show exclusive time (in milliseconds) and the percentage of the total time. Exclusive time is the time to execute their own code method, and includes a time is the time to perform the method of adding their own code execution time of the subroutine. Timing information are also CPU时间
reports and real-time manner. CPU时间
Only consider the use of active threads CPU时间的时间
, real-time information to provide absolute, time entry method from your application to exit the method of time - whether the thread is active or dormant state.
For each profile pane top-level node, table Calls + Rec
, Calls / Total
column (not shown in FIG. 2) and the number of recursive calls to report the number of calls method. Alternatively, the parent and child methods, this method is column shows the number of calls the child or parent in the methods of the top node.
Traceview Known Issues
Traceview
Logging does not handle threads, cause the following problems:
1. If the thread exits during the analysis, the thread's name will not be launched
(在Android 5.1及更高版本中被修复)
.-
- VM reuse thread ID. If a thread to stop, start another thread, they may get the same ID.
So far herein, this has ended, if the wrong place, welcome your suggestions and corrections. At the same time look forward to your attention, thank you for reading, thank you!