Define custom events

System tracing shows you information about processes only at the system level, so it's sometimes difficult to know which of your app or game's methods are executing at a given time relative to system events.

Jetpack provides a tracing API that you can use to label a particular section of code. This information is then reported in traces captured on the device. Macrobenchmark captures traces with custom trace points automatically.

When using Perfetto to capture system traces, make sure your application is configured as profileable. This way, your app's custom trace events appear in the system trace report.

fun loadAndProcessData() {
    trace("loadAndProcessData") {
        val data = trace("queryDatabase") {
            queryDatabase()
        }
        trace("processData") {
            processData(data)
        }
    }
}

The trace function automatically ends the trace when the lambda completes. This removes the risk of forgetting to end the tracing.

You can also use an NDK API for custom trace events. To learn about using this API for your native code, see Custom trace events in native code.

Additional resources

Views content