In this guide, you'll learn how to:
ATrace
instrumentation to your Android application or platform code.ATrace
events in the Perfetto UI.ATrace
and the Perfetto Tracing SDK.This page is mainly intended for:
Atrace is an API introduced in Android 4.3 that predates that allows you to add instrumentation to your code. It is still supported and in use, and interoperates well with Perfetto.
Under the hoods, Atrace forwards events up to the kernel ftrace ring-buffer and gets fetched together with the rest of scheduling data and other system-level trace data. Atrace is both:
The main difference between the two is that the private platform API allows specifying a tag (also known as category), while the SDK/NDK interface implicitly uses TRACE_TAG_APP.
In both cases, Atrace allows you to manually add instrumentation around code wall timing and numeric values, e.g. to annotate the beginning or end of functions, logical user journeys, state changes.
Slices are used to create rectangles around the execution of code and visually form a pseudo-callstack.
Semantic and constraints:
Semantic and constraints:
Async slices allow to trace logical operations that might begin and end on different threads. They are the same concept of track events in the Perfetto SDK.
Because begin/end can happen on different thread, you need to pass a cookie to each begin/end function. The cookie is just an integer number used to match begin/end pairs. The cookie is usually derived from a pointer or a unique ID that represents the logical operation being traced (e.g. a job id).
Semantic and constraints:
...ForTrack
functions allow you to specify a track name, and all events with the same track name will be grouped in the same process-scoped track in the UI. Within a track, nesting is controlled by the cookie
parameter. The SDK/NDK API does not support nesting, and the track is derived from the event name.At the time of writing, there isn't a clear-cut answer to this question. Our team is working on providing a replacement SDK that can subsume all the atrace use cases, but we are not there yet. So the answer is: depends.
When to prefer Atrace | When to prefer the Tracing SDK |
---|---|
You need something simple that just works. | You need more advanced features (e.g. flows). |
You are okay with one on/off toggle for the whole app. (If you are in the Android system you can only se a limited set of tags) | You need fine-grained control over tracing categories. |
You are okay with events being multiplexed in the main ftace buffer. | You want control over muxing vents in different buffers. |
Instrumentation overhead is not a big concern, your trace points are hit sporadically. | You want ininmal overhead for your instrumentation points. Your trace points are frequent (every 10ms or less) |
You should consider using androidx.tracing from Jetpack. We work closely with the Jetpack project. Using androidx.tracing is going to lead to a smoother migration path once we improve our SDK.
In order to record atrace you must enable the linux.ftrace
data source and add in the ftrace_config
:
atrace_categories: tag_name
atrace_apps: "com.myapp"
or atrace_apps: "*"
for all apps.You can see the full list of atrace categories here.
Now that you've learned how to instrument your code with ATrace
, here are some other documents you might find useful: