2 releases

0.1.1 Jun 14, 2024
0.1.0 Apr 3, 2024

#129 in Debugging

Download history 100/week @ 2024-08-18 139/week @ 2024-08-25 83/week @ 2024-09-01 142/week @ 2024-09-08 132/week @ 2024-09-15 131/week @ 2024-09-22 103/week @ 2024-09-29 144/week @ 2024-10-06 171/week @ 2024-10-13 208/week @ 2024-10-20 110/week @ 2024-10-27 189/week @ 2024-11-03 157/week @ 2024-11-10 164/week @ 2024-11-17 368/week @ 2024-11-24 152/week @ 2024-12-01

885 downloads per month

Apache-2.0 OR MIT

35KB
380 lines

Tracing Android Trace

Write tracing spans to Android NDK Tracing

Latest published version. Documentation build status. Apache 2.0 or MIT license. tracing-subscriber version
Linebender Zulip chat. GitHub Actions CI status. Dependency staleness status.

⚠️ Tracing Android Trace only supports Android

Tracing Android Trace provides several tracing_subscriber::Layers for Android NDK Tracing, using ATrace_beginSection and ATrace_endSection. This allows viewing spans created using the tracing macros in Android GPU Inspector.

Note that this does not currently support tracing events, only spans. This limitation is due to the underlying Android platform APIs.

Screenshot showing a thread timeline including spans of a single thread.

Tracing spans for Vello shown in Android GPU Inspector

Significant changes are documented in the changelog.

Quickstart

Add a dependency on Android Trace (and on tracing_subscriber).

[target.'cfg(target_os = "android")'.dependencies]
android_trace = "0.1.0"

You can then add an Android Tracing layer to the registry subscriber:

use tracing_subscriber::prelude::*;

fn main(){ 
  tracing_subscriber::registry()
    .with(tracing_android_trace::AndroidTraceLayer::new())
    .try_init()
    .unwrap();
}

Available Layers

NDK Tracing supports three kinds of tracing, with different API level requirements.

Thread-Matched sections

The first API added, which is useful for tracking time spent in a thread, was ATrace_beginSection and ATrace_endSection. This has been available since Android API level 23.

AndroidTraceLayer uses this API, and is the preferred layer from this crate - it was used to produce the screenshot above.

Note that if entering and exiting of spans are interleaved, this layer will produce discontinuous traces. This is required to work around the limitations of the NDK API. See the documentation on the layer for more details.

Async

This crate also includes an async layer AndroidTraceAsyncLayer, which uses ATrace_beginAsyncSection and ATrace_endAsyncSection. It is recommended to filter the use of this API to only your async tasks. See the documentation on the layer for an example of how to do so.

This is necessary because Android Tracing does not allow async tasks to be associated with each other. This means that each task will be shown in their own line in the trace, which is rarely a useful UI. It is also recommended to not associate any fields with these spans, as lines in the trace will not be re-used.

Counters

The underlying API also supports setting counter values, however this is not yet implemented. If you require this, please open an issue.

Android API levels

This crate uses android_trace to call the NDK functions. Therefore, this crate can support any Android API level on the target device, although by default it requires an API level of 23 (corresponding to Android 6, codename Marshmallow, released in 2015).

To support Android API versions less than 23, you should disable default features:

[target.'cfg(target_os = "android")'.dependencies]
tracing_android_trace = { version = "0.1.0", default-features = false }

Crate feature flags

The following feature flags are available:

  • api_level_23 (enabled by default): Require Android API level 23, to avoid some runtime symbol resolution
  • api_level_29: Require Android API level 29, disabling runtime symbol resolution entirely

Minimum supported Rust Version (MSRV)

This version of Tracing Android Trace has been verified to compile with Rust 1.77 and later.

Future versions of Tracing Android Trace might increase the Rust version requirement. It will not be treated as a breaking change and as such can even happen with small patch releases.

Click here if compiling fails.

As time has passed, some of Tracing Android Trace's dependencies could have released versions with a higher Rust requirement. If you encounter a compilation issue due to a dependency and don't want to upgrade your Rust toolchain, then you could downgrade the dependency.

# Use the problematic dependency's name and version
cargo update -p package_name --precise 0.1.1

Community

Discussion of Android Trace development happens in the Linebender Zulip, specifically in #general > Android Tracing. All public content can be read without logging in.

Contributions are welcome by pull request. The Rust code of conduct applies.

License

Licensed under either of

at your option.

Dependencies

~1.5MB
~21K SLoC