Trace

public final class Trace
extends Object

java.lang.Object
   ↳ androidx.tracing.Trace


Writes trace events to the system trace buffer.

These trace events can be collected and visualized using the Android Studio System Trace, Perfetto, and Systrace tools.

Tracing should generally be performed in a non-debuggable app for more accurate measurements, representative of real user experience. In a non-debuggable app, tracing is enabled if a trace is currently being captured, as well as one of the following:

  • Android 12 (API 31) or greater: On by default, unless
    <profileable enabled=false/>
    or
    <profileable shell=false/>
    is set in the manifest.
  • Android 10 or 11 (API 29 or 30):
    <profileable shell=true/>
    is set in the manifest, or forceEnableAppTracing() has been called
  • JellyBean through Android 11 (API 18 through API 28): forceEnableAppTracing() has been called

This tracing mechanism is independent of the method tracing mechanism offered by Debug.startMethodTracing(). In particular, it enables tracing of events that occur across multiple processes.

For information see Overview of system tracing.

Summary

Public methods

static void beginAsyncSection(String methodName, int cookie)

Writes a trace message to indicate that a given section of code has begun.

static void beginSection(String label)

Writes a trace message to indicate that a given section of code has begun.

static void endAsyncSection(String methodName, int cookie)

Writes a trace message to indicate that the current method has ended.

static void endSection()

Writes a trace message to indicate that a given section of code has ended.

static void forceEnableAppTracing()

Enables the app tracing tag in a non-debuggable process.

static boolean isEnabled()

Checks whether or not tracing is currently enabled.

static void setCounter(String counterName, int counterValue)

Writes trace message to indicate the value of a given counter.

Inherited methods

Public methods

beginAsyncSection

public static void beginAsyncSection (String methodName, 
                int cookie)

Writes a trace message to indicate that a given section of code has begun.

Must be followed by a call to endAsyncSection(String, int) with the same methodName and cookie. Unlike beginSection(String) and endSection(), asynchronous events do not need to be nested. The name and cookie used to begin an event must be used to end it. The cookie must be unique to any overlapping events. If events don't overlap, you can simply always pass the same integer (e.g. `0`). If they do overlap, the cookie is used to disambiguate between overlapping events, like the following scenario:

 [==========================]
           [=====================================]
                                      [====]
 
Without unique cookies, these start/stop timestamps could be misinterpreted by the trace display like the following, to show very different ranges:
 [=========================================]
           [================]
                                      [==========]
 

Parameters
methodName String: The method name to appear in the trace.

cookie int: Unique identifier for distinguishing simultaneous events with the same methodName

beginSection

public static void beginSection (String label)

Writes a trace message to indicate that a given section of code has begun.

This call must be followed by a corresponding call to endSection() on the same thread.

At this time the vertical bar character '|', newline character '\n', and null character '\0' are used internally by the tracing mechanism. If sectionName contains these characters they will be replaced with a space character in the trace.

Parameters
label String: The name of the code section to appear in the trace.

endAsyncSection

public static void endAsyncSection (String methodName, 
                int cookie)

Writes a trace message to indicate that the current method has ended.

Must be called exactly once for each call to beginAsyncSection(String, int) using the same name and cookie.

Parameters
methodName String: The method name to appear in the trace.

cookie int: Unique identifier for distinguishing simultaneous events with the same methodName

endSection

public static void endSection ()

Writes a trace message to indicate that a given section of code has ended.

This call must be preceded by a corresponding call to beginSection(String). Calling this method will mark the end of the most recently begun section of code, so care must be taken to ensure that beginSection / endSection pairs are properly nested and called from the same thread.

forceEnableAppTracing

public static void forceEnableAppTracing ()

Enables the app tracing tag in a non-debuggable process. Beginning in Android 12 (API 31), app tracing - custom tracing performed by app code via this class or android.os.Trace - is always enabled in all apps. Prior to this, app tracing was only enabled in debuggable apps (as well as profileable apps, on API 29/30). Calling this method enables the app to record custom trace content without debuggable=true on any platform version that supports tracing. Tracing of non-debuggable apps is highly recommended, to ensure accurate performance measurements. As app tracing is always enabled on Android 12 (API 31) and above, this does nothing after API 31.

isEnabled

public static boolean isEnabled ()

Checks whether or not tracing is currently enabled.

This is useful to avoid intermediate string creation for trace sections that require formatting. It is not necessary to guard all Trace method calls as they internally already check this. However it is recommended to use this to prevent creating any temporary objects that would then be passed to those methods to reduce runtime cost when tracing isn't enabled.

Returns
boolean true if tracing is currently enabled, false otherwise

setCounter

public static void setCounter (String counterName, 
                int counterValue)

Writes trace message to indicate the value of a given counter.

Parameters
counterName String: The counter name to appear in the trace.

counterValue int: The counter value.