# 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)

*** ** * ** ***

Android/JVM
- android

  ```
  class TraceSink : AbstractTraceSink
  ```

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

| ### Public functions |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/TraceSink#close()()` Close the `https://developer.android.com/reference/kotlin/androidx/tracing/AbstractTraceSink`, completing any enqueued writes. | android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/TraceSink#enqueue(androidx.tracing.PooledTracePacketArray)(pooledPacketArray: https://developer.android.com/reference/kotlin/androidx/tracing/PooledTracePacketArray)` Enqueue a `https://developer.android.com/reference/kotlin/androidx/tracing/PooledTracePacketArray` to be written to the trace. | android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/TraceSink#flush()()` Flush any enqueued trace events to the `https://developer.android.com/reference/kotlin/androidx/tracing/AbstractTraceSink`. | android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/TraceSink#onDroppedTraceEvent()()` Called when the `https://developer.android.com/reference/kotlin/androidx/tracing/AbstractTraceSink` cannot keep up with incoming trace events from `https://developer.android.com/reference/kotlin/androidx/tracing/Track`s. | android |

## Public constructors

### TraceSink

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

```
TraceSink(
    sequenceId: @IntRange(from = 1) Int,
    bufferedSink: BufferedSink,
    coroutineContext: CoroutineContext
)
```

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

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

### TraceSink

android

```
TraceSink(
    sequenceId: @IntRange(from = 1) Int,
    sinkProvider: () -> BufferedSink,
    coroutineContext: CoroutineContext = NonCancellable + Dispatchers.IO
)
```

## Public functions

### close

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

```
open fun close(): Unit
```

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

This function may be called from any thread.

### enqueue

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

```
open fun enqueue(pooledPacketArray: PooledTracePacketArray): Unit
```

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

This function may be called from any thread.

### flush

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

```
open fun flush(): Unit
```

Flush any enqueued trace events to the `https://developer.android.com/reference/kotlin/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

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

```
open fun onDroppedTraceEvent(): Unit
```

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

This function may be called from any thread.