# TraceSink

Artifact: [androidx.tracing:tracing-wire](https://developer.android.com/jetpack/androidx/releases/tracing) [View Source](https://cs.android.com/search?q=file:androidx/tracing/wire/TraceSink.kt+class:androidx.tracing.wire.TraceSink) Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

*** ** * ** ***


```
public final class TraceSink extends AbstractTraceSink
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.tracing.AbstractTraceSink](https://developer.android.com/reference/androidx/tracing/AbstractTraceSink) ||
|   | ↳ | [androidx.tracing.wire.TraceSink](https://developer.android.com/reference/androidx/tracing/wire/TraceSink) |

*** ** * ** ***

The trace sink that writes `https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html`, to a new file per trace session.

This implementation converts `https://developer.android.com/reference/androidx/tracing/TraceEvent`s into binary protos using [the Wire library](https://square.github.io/wire/).

The outputs created by `https://developer.android.com/reference/androidx/tracing/wire/TraceSink` can be visualized with [ui.perfetto.dev](https://ui.perfetto.dev/), and queried by [TraceProcessor](https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor) from the `androidx.benchmark:benchmark-traceprocessor` library, the [C++](https://perfetto.dev/docs/analysis/trace-processor) tool it's built on, or the [Python](https://perfetto.dev/docs/analysis/trace-processor-python) wrapper.

As binary protos embed strings as UTF-8, note that any strings serialized by TraceSink will be serialized as UTF-8.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/tracing/wire/TraceSink#TraceSink(kotlin.Int,okio.BufferedSink,kotlin.coroutines.CoroutineContext)( @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int sequenceId, @https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html bufferedSink, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext )` Constructs an instance of `https://developer.android.com/reference/androidx/tracing/wire/TraceSink`. |
| `https://developer.android.com/reference/androidx/tracing/wire/TraceSink#TraceSink(kotlin.Int,kotlin.Function0,kotlin.coroutines.CoroutineContext)( @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int sequenceId, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html> sinkProvider, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext )` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/tracing/wire/TraceSink#close()()` Close the `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink`, completing any enqueued writes. |
| `void` | `https://developer.android.com/reference/androidx/tracing/wire/TraceSink#enqueue(androidx.tracing.PooledTracePacketArray)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/tracing/PooledTracePacketArray pooledPacketArray)` Enqueue a `https://developer.android.com/reference/androidx/tracing/PooledTracePacketArray` to be written to the trace. |
| `void` | `https://developer.android.com/reference/androidx/tracing/wire/TraceSink#flush()()` Flush any enqueued trace events to the `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink`. |
| `void` | `https://developer.android.com/reference/androidx/tracing/wire/TraceSink#onDroppedTraceEvent()()` Called when the `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink` cannot keep up with incoming trace events from `https://developer.android.com/reference/androidx/tracing/Track`s. |

## Public constructors

### TraceSink

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public TraceSink(
    @IntRange(from = 1) int sequenceId,
    @NonNull BufferedSink bufferedSink,
    @NonNull CoroutineContext coroutineContext
)
```

Constructs an instance of `https://developer.android.com/reference/androidx/tracing/wire/TraceSink`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int sequenceId` | ID which uniquely identifies the trace capture system. Value must be greater than 0. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html bufferedSink` | The `https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html` that the trace will be written to. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` to execute the serialization on. |

### TraceSink

```
public TraceSink(
    @IntRange(from = 1) int sequenceId,
    @NonNull Function0<@NonNull BufferedSink> sinkProvider,
    @NonNull CoroutineContext coroutineContext
)
```

## Public methods

### close

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public void close()
```

Close the `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink`, completing any enqueued writes.

This function may be called from any thread.

### enqueue

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public void enqueue(@NonNull PooledTracePacketArray pooledPacketArray)
```

Enqueue a `https://developer.android.com/reference/androidx/tracing/PooledTracePacketArray` to be written to the trace.

This function may be called from any thread.

### flush

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public void flush()
```

Flush any enqueued trace events to the `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink`.

If the trace sink writes to persistent storage, this forces any buffered events to be written immediately. For in-memory sinks, this is typically a no-op because they do not have an underlying persistent storage destination by default.

This function may be called from any thread.

### onDroppedTraceEvent

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public void onDroppedTraceEvent()
```

Called when the `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink` cannot keep up with incoming trace events from `https://developer.android.com/reference/androidx/tracing/Track`s.

This function may be called from any thread.