Android: performance analysis by systrace

About a .Systrace

  Systrace allows you to collect and timing information of all processes running on the device at the system level (such as SurfaceFlinger, WindowManagerService some of the key modules such as Framework, service, View system, etc.). It will come from Androiddata kernel (such as CPU调度a program, disk activity and application threads) combined to generate an HTML report.

 

Second, grab Systrace method:

Method a: Use Android Device Monitor line fetch Systrace

  1. Start Android Device Monitor tool, because the Android studio 3.1 monitor with little thought, they removed the menu bar, start button, the tool can only run the command.
    Tool is located android-sdk directory, such as my local SDK directory is "C: \ Users \ drago \ AppData \ Local \ Android \ Sdk", then monitor.bat in the tools directory is the startup script, double-click to run .
  2. Monitor interface as follows:

     

  3. Click the Capture button and configuration information needed to crawl:

    File Where do you want : to develop trace.html file generated address
    Trace DURATION : crawl time, usually set for 5 seconds, and reproduce the problem within five seconds, the time is too short can cause the problem recurs when not being caught, too long It will lead JavaHeap enough and can not be saved. Therefore, in case of a problem to catch the point of time as small as possible.
    Buffer Size Trace : storage Systrace of size, is also too small to cause loss of information, too long can cause Java Heap is not enough and can not be saved. If the test results are abnormal, please try to adjust the size of the Buffer Size.
    The Application Traces from the Enable : detection applications, the default selection none, select the application where they need to be detected.
    Used Tag Commonly : commonly used tags, which enable all TAG part. Show only concerned with the case, and only select the Graphics View System can be.
    Options Advanced : Advanced Options. If the device root, you can see more of TAG, such as eMMC commands, Synchonization, Kernel Workqueues.

    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:

  4. After configuration (such as crawl camera-related trace) Click OK to start operating the phone, it will automatically generate reports (trace.html) file in time to the post.

Method two: using the command line to fetch Systrace

  1. Download and installAndroid SDK Tools(C:\Users\drago\AppData\Local\Android\Sdk\platform-tools\systrace\systrace.py),安装Python

    Note: Python is no access to windows system comes with the API library, you need to download. Name of the library called pywin32, can be downloaded directly from the Internet:

      https://github.com/mhammond/pywin32/releases (download for your version of Python)

  2. Connect your phone, open the developer options in the USB Debugoptions, use the command line to fetch Systrace,syntax is as follows:
    python systrace.py [options] [categories]

     Example 1: call systracein 10within seconds of recording device process, including process graphics, and generates a file called mynewtracethe HTMLreport:

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

     Example 2: Detection Performance UI:

       python systrace.py view --time=10

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

  3. SystraceStorage path followsandroid-sdk/platform-tools/systrace/

 

Three, trace.html file analysis:

Use Google Chrome (other browsers probably not open) to open the file for analysis, the interface is as follows:
There is a very fine progress bar in the above process, including the status of the thread:
  gray
: sleep.
  Blue : You can run (it can run, but has not yet been scheduled to run).
  Green : Running (scheduler think it is running).

  Red
: uninterrupted sleep (usually occurs in the kernel lock), pointed out I / O load, for debugging performance problems are helpful
  Orange : As the I / O load caused by uninterrupted sleep.
  To see why uninterrupted sleep (available from sched_blocked_reason get the tracking point), choose red slice uninterrupted sleep.

  Shortcut: w: for large s: for narrow d: Mobile left a: moving the right
  
  and then amplified by w, find F (i.e. Frames), and the interval between F., 16ms interval exceeds if there are problem, 16ms is actually corresponding 60fps (1 / 60≈16ms), because collaboration between the human eye and brain can not perceive more than 60fps screen update. Android system every 16ms releasing the VSYNC signal to trigger the UI rendering, the entire process to ensure that if less than 16ms will be able to achieve a smooth picture. So if the operation exceeds 16ms following happens, VSYNC signal event of a system, but this time can not be rendered, still doing other operations, it will lead to dropped frames, (APP aware Caton time, you can take a look at logcat console will drop frames similar warning). Wherein F circle 绿色represents a frame presented in a 16.6 ms, 16.6 ms rendering takes more than a frame 黄色or 红色frame represented by circles.

  

  After amplification time 1420 ~ 1500ms can be seen, the spacing between the two F about 70ms, significantly more than 16ms, and then continue to enlarge see the specific object of long and occupy:

  At this point click "F" icon will prompt the relevant content in the next column as follows:

  Tips are listview in recycling / rebinding of low efficiency, then click Alerts:

  Click on the expanded accordingly, and gives analysis and performance optimization suggestions:

  If you want to see the tool in trace each discovery Alert and triggering device Alert number, click the window at the far right of the Alerts tab, as shown below:
  

  如果在 UI Thread上做太多的工作,需要找出哪些方法消耗了太多的 CPU时间。一种方法是添加跟踪标记到您认为会导致这些瓶颈的方法,以查看这些函数调用是否显示在 systrace中。 如果您不确定哪些方法可能会在UI线程上造成瓶颈,请使用 Android Studio的内置 CPU分析器,或者生成跟踪日志并使用 Traceview查看它们。
   虽然Systrace无法定位到某一行需要优化的代码,但通过Alerts和Frames以根据TraceView分析具体函数花了多长时间来进一步优化代码提高性能。

代码中添加标记生成 trace log

  由于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)方法。

 
使 用TraceView 分析trace Log

  Traceview是提供跟踪日志的图形工具。您可以通过使用Debug类来设置代码来生成日志。 这种跟踪方法非常精确,因为您可以准确指定要启动的代码中的哪个位置,并停止记录跟踪数据。 如果尚未生成这些跟踪日志并将其从连接的设备保存到本地计算机,请转至通过检测应用程序生成跟踪日志。 使用Traceview检查这些日志可帮助您调试您的应用程序并剖析其性能。

  如果您不需要查看通过使用Debug类检测应用程序来记录的跟踪日志,则可以使用Android Studio 3.0及更高版本中包含的CPU分析器来查看应用程序的线程和记录方法跟踪。使用Android Device Monitor可以查看trace Log内容,

  提示:可以使用命令行中的dmtracedump来生成跟踪日志文件的图形调用堆栈图。

  打开跟踪日志后,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.

 
 


-end-

Guess you like

Origin www.cnblogs.com/blogs-of-lxl/p/10926824.html