public final class TraceSink extends AbstractTraceSink


The trace sink that writes BufferedSink, to a new file per trace session.

This implementation converts androidx.tracing.TraceEvents into binary protos using the Wire library.

The outputs created by TraceSink can be visualized with ui.perfetto.dev, and queried by TraceProcessor from the androidx.benchmark:benchmark-traceprocessor library, the C++ tool it's built on, or the 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

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

Constructs an instance of TraceSink.

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

Public methods

void

Close the AbstractTraceSink, completing any enqueued writes.

void

Enqueue a PooledTracePacketArray to be written to the trace.

void

Flush any enqueued trace events to the AbstractTraceSink.

void

Called when the AbstractTraceSink cannot keep up with incoming trace events from Tracks.

Public constructors

TraceSink

Added in 2.0.0-alpha09
public TraceSink(
    @IntRange(from = 1) int sequenceId,
    @NonNull BufferedSink bufferedSink,
    @NonNull CoroutineContext coroutineContext
)

Constructs an instance of TraceSink.

Parameters
@IntRange(from = 1) int sequenceId

ID which uniquely identifies the trace capture system. Value must be greater than 0.

@NonNull BufferedSink bufferedSink

The BufferedSink that the trace will be written to.

@NonNull CoroutineContext coroutineContext

The CoroutineContext 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.0-alpha09
public void close()

Close the AbstractTraceSink, completing any enqueued writes.

This function may be called from any thread.

enqueue

Added in 2.0.0-alpha09
public void enqueue(@NonNull PooledTracePacketArray pooledPacketArray)

Enqueue a PooledTracePacketArray to be written to the trace.

This function may be called from any thread.

flush

Added in 2.0.0-alpha09
public void flush()

Flush any enqueued trace events to the 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.0-alpha09
public void onDroppedTraceEvent()

Called when the AbstractTraceSink cannot keep up with incoming trace events from Tracks.

This function may be called from any thread.