Systrace的抓取方法Google官方教程

和你一起终身学习,这里是程序员 Android

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

一、前言
二、Capture a system trace on the command line
三、Capture a system trace on a device
四、总结

一、前言

不要畏惧看不懂,我们要往懂的看

首先不要抗拒英文文章,本文是转载Google 官方文档,故英文偏多,只有阅读原汁原味的内容,才可以真正提升自己。

二、Capture a system trace on the command line

1.introduction to command line grabbing systrace

The systrace command invokes the Systrace tool:https://developer.android.com/topic/performance/tracing, which allows you to collect and inspect timing information across all processes running on your device at the system level.

This document explains how to generate Systrace reports from the command line. On devices running Android 9 (API level 28) or higher, you can also generate Systrace reports using the System Tracing system app:https://developer.android.com/topic/performance/tracing/on-device.

In order to run systrace, complete the following steps:

  • 1.From Android Studio, download and install the latest Android SDK Tools.
  • 2.Install Python and include it in your workstation's PATH environment variable.
  • 3.Add android-sdk /platform-tools/ to your PATH environment variable. This directory contains the Android Debug Bridge binary (adb), which is called by the systrace program.
  • 4.Connect a device running Android 4.3 (API level 18) or higher to your development system using a USB debugging connection.

The systrace command is provided in the Android SDK Tools package and is located in android-sdk/platform-tools/systrace/.

2.Syntax

To generate the HTML report for app, you need to run systrace from the command line using the following syntax:

$ python systrace.py [options] [categories]

For example, the following command calls systrace to record device activity and generate a HTML report named mynewtrace.html. This list of categories is a reasonable default list for most devices.

$ python systrace.py -o mynewtrace.html sched freq idle am wm gfx view \
    binder_driver hal dalvik camera input res

Tip: If you want to see the names of tasks in the trace output, you must include the sched category in your command parameters.

To view the list of categories that your connected device supports, run the following command:

$ python systrace.py --list-categories

If you don't specify any categories or options, systrace generates a report that includes all available categories and uses default settings. The categories available depend on the connected device you're using.

3.Global options

4.Commands and command options

三、Capture a system trace on a device

3.1 introduction

Devices running Android 9 (API level 28) or higher include a system-level app called System Tracing. This app is similar to the systrace command-line utility, but the app allows you to record traces directly from a test device itself, without needing to plug in the device and connect to it over ADB. You can then use the app to share results from these traces with your development team.

It's particularly helpful to record traces when addressing performance-related bugs in your app, such as slow startup, slow transitions, or UI jank.

3.2 Record a system trace

The System Tracing app allows you to record a system trace using a Quick Settings tile or a menu within the app itself. The following sections describe how to complete the recording process using these interfaces.

Note: As part of your development workflow, you might submit an on-device bug report. It's important to file this type of bug report after you've finished recording a system trace. That way, the bug report process itself isn't included in the recorded trace.

3.2 Record using Quick Settings tile

The Quick Settings tile is usually the more convenient way to complete the on-device system tracing process.

Set up tile

If you're using System Tracing for the first time on your test device, or if you don't see the System Tracing tile in your device's Quick Settings panel (Figure 1), complete the following setup steps:

  • 1.Enable developer options, if you haven't done so already.
  • 2.Open the Developer Options settings screen.
  • 3.In the Debugging section, select System Tracing. The System Tracing app opens, showing the app menu.
  • 4.From the app menu, enable Show Quick Settings tile, as shown in Figure 2. The system adds the System Tracing tile to the Quick Settings panel, which appears in Figure 1:
Figure 1. The System Tracing tile within the Quick Settings panel
Figure 2. The Show Quick Settings tile switch in the System Tracing app

Note: By default, the system adds the System Tracing tile as the first tile in the Quick Settings panel. If you'd like the tile to appear in a different position, use the panel's edit mode to move the tile.

3.3 Complete a system trace recording

To record a system trace using the Quick Settings panel, complete the following steps:

  • 1.Tap the System Tracing tile, which has the label "Record trace". The tile becomes enabled, and a persistent notification appears to notify you that the system is now recording a trace, as shown in Figure 3:
Figure 3. Persistent notification that appears after starting an on-device system trace
  • 2.Perform the actions in your app that you'd like the system to inspect.

Note: You can record bugs that are difficult to reproduce by leaving System Tracing running in the background, then stopping System Tracing soon after the bug occurs. System Tracing saves a device's activity to a rolling buffer, which holds 10-30 seconds' worth of events.

  • 3.When you've completed these actions, stop tracing by tapping either the System Tracing tile in the Quick Settings panel or on the System Tracing notification.

The system displays a new notification that contains the message "Saving trace". When saving is complete, the system dismisses the notification and displays a third notification, confirming that your trace has been saved and that you're ready to share the system trace, as shown in Figure 4:

Figure 4. Persistent notification that appears after the system has finished saving a recorded trace

3.4 Record using app menu

The app menu allows you to configure several advanced settings related to system tracing and provides a switch for starting and stopping a system trace.

To record a system trace using the System Tracing app menu, complete the following steps:

1.Enable developer options, if you haven't done so already.
2.Open the Developer Options settings screen. In the Debugging section, select System Tracing. The System Tracing app opens.

Alternatively, if you've set up the System Tracing tile, you can long-tap on the tile to enter the System Tracing app.

3.Make sure Trace debuggable applications is selected to include applications that have debugging enabled in the system trace.

4.Optionally, choose the Categories of system and sensor calls to trace, and choose a Per-CPU buffer size (in KB). Choose categories that correspond to the use case that you're testing, such as the Audio category for testing Bluetooth operations or the Memory category for heap allocations.

Note: These categories serve as app-level settings, so the system uses these categories when using the Quick Settings tile, too. In addition, these settings persist across device reboots.

5.Optionally, select Long traces to enable traces that are saved continuously to device storage. For this option, you must set limits for the Maximum long trace size and Maximum long trace duration.

6.Enable the Record trace switch, highlighted in Figure 5. The tile becomes enabled, and a persistent notification appears to notify you that the system is now recording a trace (Figure 3).

7.Perform the actions in your app that you'd like the system to inspect.

Note: You can record bugs that are difficult to reproduce by leaving System Tracing running in the background, then stopping System Tracing soon after the bug occurs. By default, System Tracing saves a device's activity to a rolling buffer, which holds 10-30 seconds' worth of events. If you have Long traces enabled, the device's activity is saved continuously to device storage up to the limits you set.

Figure 5. The Record trace switch in the System Tracing app

8.When you've completed these actions, stop tracing by disabling the Record trace switch.

The system displays a new notification that contains the message "Saving trace". When saving is complete, the system dismisses the notification and displays a third notification, confirming that your trace has been saved and that you're ready to share the system trace, as shown in Figure 4.

3.5 Share a system trace

The System Tracing app helps you share system trace results as part of several different workflows. On a device running Android 10 (API level 29) or later, trace files are saved with the .perfetto-trace filename extension and can be opened in the Perfetto UI. On a device running an earlier version of Android, trace files are saved with the .ctrace filename extension, which denotes the Systrace format.

Share as a message

System Tracing allows you to share your collected trace with other apps on your device. In doing so, you can send the trace to your development team through an email or a bug-tracking app without needing to connect a device to your development machine.

After you've recorded a system trace, tap on the notification that appears on the device (see Figure 4). The platform's intent picker appears, allowing you to share your trace using the messaging app of your choice.

Share from the Files app

On devices running Android 10 (API level 29), traces are shown in the Files app. If desired, you can share a trace from this app.

Download report using ADB

If desired, you can also extract a system trace from a device using ADB. Connect the device that recorded the trace to your development machine, then run the following commands in a terminal window

C:\Users\Administrator>adb pull /data/local/traces .
/data/local/traces/: 1 file pulled. 20.0 MB/s (1004415 bytes in 0.048s)

C:\Users\Administrator>

3.6 Converting between trace formats

You can convert Perfetto trace files to the Systrace format. See Converting between trace formats for more information.

3.7 Create an HTML report

When sharing your trace, the report itself resides in a .perfetto-trace file (on devices running Android 10 or higher) or a .ctrace file (for all other versions).

Create an HTML report from the trace file using a web-based UI or from the command line.

Web-based UI

Use the Perfetto UI to open the trace file and generate the report.

For a Perfetto file, click Open trace file. For a Systrace file, click Open with legacy UI. The legacy UI has the same look and feel as the Systrace report.

Command line

Run the following commands in a terminal window to generate an HTML report from the trace file:

$ cd /path-to-traces-on-my-dev-machine && \
  systrace --from-file trace-file-name{.ctrace | .perfetto-trace}

If you don't already have the systrace command-line program, you can download it from the Catapult project on GitHub, or directly from the Android Open Source Project.

四、总结

1.命令行抓取

python systrace.py -t 5 -o trace2.html am wm view app res ss database freq sched gfx input idle binder_driver binder_lock dalvik

2.手机开发者模式抓取

步骤:
设置 --->系统---> 开发者模式--->系统跟踪

原文链接如下:

Capture a system trace on the command line:
https://developer.android.com/topic/performance/tracing/command-line
Capture a system trace on a device
https://developer.android.com/topic/performance/tracing/on-device

至此,本篇已结束。转载网络的文章,小编觉得很优秀,欢迎点击阅读原文,支持原创作者,如有侵权,恳请联系小编删除,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

你可能感兴趣的:(Systrace的抓取方法Google官方教程)