lightbulb_outline Please take our October 2018 developer survey. Start survey

Perform on-device system tracing

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.

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.

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

Figure 2. The Show Quick Settings tile switch in the System Tracing app

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

    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.

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:

    Notification with the message 'Trace is being recorded. Tap to stop
  tracing.'
    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.

  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:

    Notification with the message 'Trace saved. Tap to share your
  trace.'
    Figure 4. Persistent notification that appears after the system has finished saving a recorded trace

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. Optionally, choose the Categories of system and sensor calls to trace, and choose a 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.

    Figure 5. The Record trace switch in the System Tracing app
  4. 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).

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

  6. 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.

Share a system trace

The System Tracing app helps you share system trace results as part of several different workflows.

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.

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:

$ cd /path-to-traces-on-my-dev-machine
$ adb pull /data/local/traces/ .

Create an HTML report

When sharing your trace through a message or through ADB, the report itself resides in a .ctrace file. Using this file, you can generate an HTML report of the trace. To do so, run the following commands in a terminal window:

$ cd /path-to-traces-on-my-dev-machine
$ systrace --from-file trace-file-name.ctrace

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.