public final class TraceProcessor


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.

import androidx.benchmark.runServer
import androidx.benchmark.traceprocessor.PerfettoTrace
import androidx.benchmark.traceprocessor.TraceProcessor

// Collect the duration of all slices named "activityStart" in the trace
val activityStartDurNs =
    TraceProcessor.runServer {
        loadTrace(PerfettoTrace("/path/to/trace.perfetto-trace")) {
                query("SELECT dur FROM slice WHERE name = 'activityStart'").map {
                    it.long("dur")
                }
            }
            .toList()
    }
return activityStartDurNs

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:

import androidx.benchmark.traceprocessor.PerfettoTrace

loadTrace(PerfettoTrace("/path/to/trace.perfetto-trace")) {
    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 = "$packageName"
        """
                .trimMargin()
        )
        .forEach {
            // process each observed doFrame slice
            doFrameCallback(it.string("name"), it.long("ts"), it.long("dur"))
        }
}

See also Perfetto project documentation:

See also
PerfettoTrace

Summary

Nested types

public static class TraceProcessor.Companion
public final class TraceProcessor.Handle implements AutoCloseable
public final class TraceProcessor.Session

Handle to query sql data from a PerfettoTrace.

public final class TraceProcessor.Session.Handle implements AutoCloseable

Public methods

final @NonNull T
<T extends Object> loadTrace(
    @NonNull PerfettoTrace trace,
    @NonNull Function1<@NonNull TraceProcessor.Session, @NonNull T> block
)

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

static final @NonNull T
@ExperimentalTraceProcessorApi
<T extends Object> runServer(
    @NonNull ServerLifecycleManager serverLifecycleManager,
    @NonNull TraceProcessor.EventCallback eventCallback,
    @NonNull TraceProcessor.Tracer tracer,
    long timeoutMs,
    @NonNull Function1<@NonNull TraceProcessor, @NonNull T> block
)

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

static final @NonNull TraceProcessor.Handle
@ExperimentalTraceProcessorApi
startServer(
    @NonNull ServerLifecycleManager serverLifecycleManager,
    @NonNull TraceProcessor.EventCallback eventCallback,
    @NonNull TraceProcessor.Tracer tracer,
    long timeoutMs
)

Starts a Perfetto trace processor shell server in http mode, and returns a Handle which can be used to access and close the TraceProcessor server instance.

final @NonNull TraceProcessor.Session.Handle

Loads a PerfettoTrace into the TraceProcessor server, and returns a Session.Handle which can be used to access and close a Session.

Public methods

loadTrace

Added in 1.4.0
public final @NonNull T <T extends Object> loadTrace(
    @NonNull PerfettoTrace trace,
    @NonNull Function1<@NonNull TraceProcessor.Session, @NonNull T> block
)

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

runServer

Added in 1.4.0
@ExperimentalTraceProcessorApi
public static final @NonNull T <T extends Object> runServer(
    @NonNull ServerLifecycleManager serverLifecycleManager,
    @NonNull TraceProcessor.EventCallback eventCallback,
    @NonNull TraceProcessor.Tracer tracer,
    long timeoutMs,
    @NonNull Function1<@NonNull TraceProcessor, @NonNull T> block
)

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

import androidx.benchmark.runServer
import androidx.benchmark.traceprocessor.PerfettoTrace
import androidx.benchmark.traceprocessor.TraceProcessor

// Collect the duration of all slices named "activityStart" in the trace
val activityStartDurNs =
    TraceProcessor.runServer {
        loadTrace(PerfettoTrace("/path/to/trace.perfetto-trace")) {
                query("SELECT dur FROM slice WHERE name = 'activityStart'").map {
                    it.long("dur")
                }
            }
            .toList()
    }
return activityStartDurNs
Parameters
@NonNull ServerLifecycleManager serverLifecycleManager

controls starting and stopping the TraceProcessor process.

@NonNull TraceProcessor.EventCallback eventCallback

callback for events such as trace load failure.

@NonNull TraceProcessor.Tracer tracer

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

long timeoutMs

maximum duration in milliseconds for waiting for operations like loading the server, or querying a trace.

@NonNull Function1<@NonNull TraceProcessor, @NonNull T> block

Command to execute using trace processor

startServer

Added in 1.4.0
@ExperimentalTraceProcessorApi
public static final @NonNull TraceProcessor.Handle startServer(
    @NonNull ServerLifecycleManager serverLifecycleManager,
    @NonNull TraceProcessor.EventCallback eventCallback,
    @NonNull TraceProcessor.Tracer tracer,
    long timeoutMs
)

Starts a Perfetto trace processor shell server in http mode, and returns a Handle which can be used to access and close the TraceProcessor server instance.

import androidx.benchmark.startServer
import androidx.benchmark.traceprocessor.PerfettoTrace
import androidx.benchmark.traceprocessor.TraceProcessor

// Collect the duration of all slices named "activityStart" in the trace
val activityStartDurNs =
    TraceProcessor.startServer().use {
        it.traceProcessor.startSession(PerfettoTrace("/path/to/trace.perfetto-trace")).use {
            it.session
                .query("SELECT dur FROM slice WHERE name = 'activityStart'")
                .map { it.long("dur") }
                .toList()
        }
    }
return activityStartDurNs
Parameters
@NonNull ServerLifecycleManager serverLifecycleManager

controls starting and stopping the TraceProcessor process.

@NonNull TraceProcessor.EventCallback eventCallback

callback for events such as trace load failure.

@NonNull TraceProcessor.Tracer tracer

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

long timeoutMs

maximum duration in milliseconds for waiting for operations like loading the server, or querying a trace.

startSession

Added in 1.4.0
public final @NonNull TraceProcessor.Session.Handle startSession(@NonNull PerfettoTrace trace)

Loads a PerfettoTrace into the TraceProcessor server, and returns a Session.Handle which can be used to access and close a Session.