Kotlin API for Perfetto Trace Processor, which enables SQL querying against the data stored in a Perfetto trace.

This includes synchronous and async trace sections, kernel-level scheduling timing, binder events... If it's displayed in Android Studio system trace or ui.perfetto.dev, it can be queried from this API.

// Collect the duration of all slices named "activityStart" in the trace
val activityStartDurationNs = TraceProcessor.runServer {
loadTrace(trace) {
query("SELECT dur FROM slice WHERE name LIKE \"activityStart\"").toList {
it.long("dur")
}
}
}

Note that traces generally hold events from multiple apps, services and processes, so it's recommended to filter potentially common trace events to the process you're interested in. See the following example which queries Choreographer#doFrame slices (labelled spans of time) only for a given package name:

query("""
|SELECT
| slice.name,slice.ts,slice.dur
|FROM slice
| INNER JOIN thread_track on slice.track_id = thread_track.id
| INNER JOIN thread USING(utid)
| INNER JOIN process USING(upid)
|WHERE
| slice.name LIKE "Choreographer#doFrame%" AND
| process.name LIKE "
$packageName"
"""
.trimMargin()
)

See also Perfetto project documentation:

See also
PerfettoTrace

Summary

Public companion functions

T
@ExperimentalTraceProcessorApi
@<Error class: unknown class>
<T : Any?> runServer(
    serverLifecycleManager: ServerLifecycleManager,
    eventCallback: TraceProcessor.EventCallback,
    tracer: TraceProcessor.Tracer,
    timeout: Duration,
    block: TraceProcessor.() -> T
)

Starts a Perfetto trace processor shell server in http mode, loads a trace and executes the given block.

Cmn

Public constructors

@ExperimentalTraceProcessorApi
TraceProcessor(
    serverLifecycleManager: ServerLifecycleManager,
    tracer: TraceProcessor.Tracer,
    eventCallback: TraceProcessor.EventCallback
)
Cmn

Public functions

T
<T : Any?> loadTrace(trace: PerfettoTrace, block: TraceProcessor.Session.() -> T)

Loads a PerfettoTrace into the trace processor server to query data out of the trace.

Cmn

Public companion functions

runServer

@ExperimentalTraceProcessorApi
@<Error class: unknown class>
fun <T : Any?> runServer(
    serverLifecycleManager: ServerLifecycleManager,
    eventCallback: TraceProcessor.EventCallback,
    tracer: TraceProcessor.Tracer,
    timeout: Duration = SERVER_START_TIMEOUT_MS,
    block: TraceProcessor.() -> T
): T

Starts a Perfetto trace processor shell server in http mode, loads a trace and executes the given block.

Parameters
serverLifecycleManager: ServerLifecycleManager

controls starting and stopping the TraceProcessor process.

eventCallback: TraceProcessor.EventCallback

callback for events such as trace load failure.

tracer: TraceProcessor.Tracer

used to trace begin and end of significant events within this managed run.

timeout: Duration = SERVER_START_TIMEOUT_MS

waiting for the server to start. If less or equal to zero use 60 seconds

block: TraceProcessor.() -> T

Command to execute using trace processor

Public constructors

TraceProcessor

@ExperimentalTraceProcessorApi
TraceProcessor(
    serverLifecycleManager: ServerLifecycleManager,
    tracer: TraceProcessor.Tracer = Tracer(),
    eventCallback: TraceProcessor.EventCallback = EventCallback.Noop
)

Public functions

loadTrace

fun <T : Any?> loadTrace(trace: PerfettoTrace, block: TraceProcessor.Session.() -> T): T

Loads a PerfettoTrace into the trace processor server to query data out of the trace.