# TraceProcessor

Artifact: [androidx.benchmark:benchmark-traceprocessor](https://developer.android.com/jetpack/androidx/releases/benchmark) [View Source](https://cs.android.com/search?q=file:androidx/benchmark/traceprocessor/TraceProcessor.kt+class:androidx.benchmark.traceprocessor.TraceProcessor) Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

*** ** * ** ***


```
public final class TraceProcessor
```

<br />

*** ** * ** ***

Kotlin API for [Perfetto Trace Processor](https://perfetto.dev/docs/analysis/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](https://ui.perfetto.dev), it can be queried from this API.

```kotlin
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:

```kotlin
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:

- [Trace Processor overview](https://perfetto.dev/docs/analysis/trace-processor)

- [Common queries](https://perfetto.dev/docs/analysis/common-queries)

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/benchmark/traceprocessor/PerfettoTrace` |   |

## Summary

| ### Nested types |
|---|
| `public static class https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Companion` |
| `public interface https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.EventCallback` |
| `public static class https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.EventCallback.Noop implements https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.EventCallback` |
| `public final class https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Handle implements https://developer.android.com/reference/java/lang/AutoCloseable.html` |
| `public final class https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session` Handle to query sql data from a `https://developer.android.com/reference/androidx/benchmark/traceprocessor/PerfettoTrace`. |
| `public final class https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session.Handle implements https://developer.android.com/reference/java/lang/AutoCloseable.html` |
| `public class https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Tracer` |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor#loadTrace(androidx.benchmark.traceprocessor.PerfettoTrace,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/PerfettoTrace trace, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session, @https://developer.android.com/reference/androidx/annotation/NonNull T> block )` Loads a PerfettoTrace into the trace processor server to query data out of the trace. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `@https://developer.android.com/reference/androidx/benchmark/traceprocessor/ExperimentalTraceProcessorApi <T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor#runServer(androidx.benchmark.traceprocessor.ServerLifecycleManager,androidx.benchmark.traceprocessor.TraceProcessor.EventCallback,androidx.benchmark.traceprocessor.TraceProcessor.Tracer,kotlin.Long,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/ServerLifecycleManager serverLifecycleManager, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.EventCallback eventCallback, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Tracer tracer, long timeoutMs, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor, @https://developer.android.com/reference/androidx/annotation/NonNull T> block )` Starts a Perfetto trace processor shell server in http mode, loads a trace and executes the given block. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Handle` | `@https://developer.android.com/reference/androidx/benchmark/traceprocessor/ExperimentalTraceProcessorApi https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor#startServer(androidx.benchmark.traceprocessor.ServerLifecycleManager,androidx.benchmark.traceprocessor.TraceProcessor.EventCallback,androidx.benchmark.traceprocessor.TraceProcessor.Tracer,kotlin.Long)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/ServerLifecycleManager serverLifecycleManager, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.EventCallback eventCallback, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Tracer tracer, long timeoutMs )` Starts a Perfetto trace processor shell server in http mode, and returns a `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Handle` which can be used to access and close the `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor` server instance. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session.Handle` | `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor#startSession(androidx.benchmark.traceprocessor.PerfettoTrace)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/PerfettoTrace trace)` Loads a PerfettoTrace into the TraceProcessor server, and returns a `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session.Handle` which can be used to access and close a `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session`. |

## Public methods

### loadTrace

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#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](https://developer.android.com/jetpack/androidx/releases/benchmark#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.

```kotlin
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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/ServerLifecycleManager serverLifecycleManager` | controls starting and stopping the TraceProcessor process. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.EventCallback eventCallback` | callback for events such as trace load failure. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor, @https://developer.android.com/reference/androidx/annotation/NonNull T> block` | Command to execute using trace processor |

### startServer

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#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 `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Handle` which can be used to access and close the `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor` server instance.

```kotlin
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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/ServerLifecycleManager serverLifecycleManager` | controls starting and stopping the TraceProcessor process. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.EventCallback eventCallback` | callback for events such as trace load failure. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/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](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
public final @NonNull TraceProcessor.Session.Handle startSession(@NonNull PerfettoTrace trace)
```

Loads a PerfettoTrace into the TraceProcessor server, and returns a `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session.Handle` which can be used to access and close a `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session`.