Android Systrace use


Highly recommended article: Welcome Favorite
Android Dry Share

SystraceAllow all the processes running on your system-level collection and inspection equipment timing information. It will come from Androiddata kernel (such as CPU调度a program, disk activity and application threads) are combined to generate a HTMLreport.

This article describes the Androidpart of the development of knowledge by reading this article, you will reap the following:

  1. About Systrace
  2. Use Systrace
  3. Using the command line to fetch Systrace
  4. Systrace detection performance using the UI
  5. Use Systrace detection warning and dropped frames issue
  6. View shortcuts trace file
  7. Specific analysis codes to add trace marks problem
  8. 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 Androiddata 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 Androidthe system or appproblem, we first need to grab Systracethe 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:

  1. First phone link, opens Android Device Monitor, select the process to be analyzed (for example com.google.process.gapps), click Capture system wide trace using Android systrace(where the arrow at the top right corner)

Grab method Systrace

  1. Configure content needs to grab the Systrace

At this time, according to different needs, different crawl configuration tracetime (time do not too long, otherwise it will lead crawls partial loss), and other content, then click OKthe operating system to analyze Caton or appslow running section, you'll runtime information collected automatically, and then Chrome 浏览器打open the resulting tracefile.

Depending on the requirements, configuration crawl different trace time, content, etc.

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 in Android Device Monitorthe lower right corner, as shown below

Timely and clear information in the cache, crawling trace prevent invalid content

With Chrome 浏览器open generated trace 文件, as shown below, which will include each CPU,and graphics rendering, input event content and the like.

Open the resulting trace file using Chrome browser

Crawl Tracereport provides Androidsystem 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 systracedoes 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 Studiothe built-in CPU分析器, or generate a trace log and used Traceviewto view them.

3. Use the command line to fetch Systrace

This document describes how the command line to generate systracereports, browse trace files generated by the tool, and use them to analyze and improve the application's user interface (UI)performance.

Crawl systracebefore, complete the following steps:

  1. Download and installAndroid SDK Tools
  2. Mounted Python ,and contained in the execution path of the workstation.
  3. Connect your phone, open the developer options in the USB Debugoptions.
  4. SystraceStorage path followsandroid-sdk/platform-tools/systrace/

使用命令行抓取 Systrace的语法如下:
python systrace.py [options] [categories]

例如,以下命令调用systrace10秒钟内记录设备进程,包括图形进程,并生成一个名为mynewtraceHTML报告:

python systrace.py --time=10 -o mynewtrace.html gfx

如果不指定任何类别或选项,systrace将生成包含所有可用类别的报告,并使用默认设置。 可用的类别取决于您使用的连接设备。
Global options

Commands and command options

4.使用Systrace 检测UI 性能

systrace对于检查应用程序的UI性能特别有用,因为它可以分析您的代码和帧速率以识别问题区域并提供可能的解决方案。 开始,按照以下步骤进行:

  1. 连接手机并运行您的app
  2. 使用以下命令运行systrace:
    python systrace.py view --time = 10
  3. 操作您的应用 10秒后,systrace生成一个HTML报告。
  4. 使用网络浏览器打开HTML报告。

您现在可以与报告进行交互,以便在记录期间检查设备CPU使用情况。 以下部分介绍如何检查报告中的信息以查找和修复UI性能问题

5. 使用Systrace 检测警告以及掉帧问题

如下报告列出了每个进程呈现UI帧,并指示沿着时间线的每个渲染帧。 在绿色框架圆圈中指示在16.6毫秒内呈现的帧以保持每秒稳定60帧。 花费16.6毫秒以上渲染的帧用黄色红色框圈表示。

UI rendering frame rate view

注意:
在运行Android 5.0(API级别21)或更高版本的设备上, UI 渲染的工作是在UI ThreadRenderThread两个线程完成。 在之前的版本中,创建框架的所有工作都是在UI Thread上完成的。

点击一个F框架圈可以提供有关系统为渲染该框架而完成的工作的其他信息,包括警报。 它还向您展示了在渲染该框架时系统正在执行的方法,因此您可以调查这些方法是否导致UI jank

Click on the yellow button or red frame, this frame will be prompted to analyze information Caton

选择慢帧后,您可能会在报告的底部窗格中看到警报。 上图中显示的Alert提出,框架的主要问题是在ListView回收和重新绑定中花费了太多的时间。 跟踪中有相关事件的链接可以解释更多关于系统在这段时间内正在做什么的事情。

要查看工具在trace中发现的每个Alert以及设备触发Alert的次数,请单击窗口最右侧的Alerts选项卡,如下图所示。
Alerts面板可帮助您查看发生了哪些问题,以及发生的频率。 将Alert面板看作是要修复的错误列表, 通常情况下,一个区域的微小变化或改进可以消除应用程序中的全部警报。

Alert Analysis panel

Frame Input File Metrics Alerts panel, etc.

如果你在UI Thread上做太多的工作,你需要找出哪些方法消耗了太多的CPU时间
一种方法是添加跟踪标记(请参阅检测应用代码)到您认为会导致这些瓶颈的方法,以查看这些函数调用是否显示在systrace中。 如果您不确定哪些方法可能会在UI线程上造成瓶颈,请使用Android Studio的内置CPU分析器,或者生成跟踪日志并使用Traceview查看它们。

6. 查看trace 文件的快捷键

查看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的抓取与查看。

使用Android Device Monitor 查看trace Log

打开跟踪日志后,Traceview使用以下两个窗格显示日志数据:

    1. 时间轴窗格:
      描述每个线程何时进入和退出方法的时间轴窗格
    1. 配置文件窗格:
      总结每个线程在跟踪日志的执行期间的配置文件窗格
      以下各节提供有关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 / Totalcolumn (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.

image.png

Traceview Known Issues

TraceviewLogging 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及更高版本中被修复).

    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!

微信关注公众号: 程序员Android,领福利

Guess you like

Origin www.cnblogs.com/wangjie1990/p/11327220.html