InMemoryRingBufferTraceSink


@ExperimentalRingBufferApi
public final class InMemoryRingBufferTraceSink extends TraceSink


A TraceSink that stores TraceEvents in a fixed-size ring buffer to minimize allocations.

This buffer is designed to hold trace events temporarily before they are flushed. When the buffer is full, it overwrites the oldest events.

A standard flush or close will simply clear the buffer and drop the events. To persist the trace data, you must provide a BufferedSink by calling flushTo or closeAndFlushTo.

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

Summary

Public constructors

InMemoryRingBufferTraceSink(
    @IntRange(from = 1) int sequenceId,
    long capacityInBytes
)

Public methods

void

Marks the InMemoryRingBufferTraceSink as closed and clears any remaining buffered events without writing them out.

final void

Marks the InMemoryRingBufferTraceSink as closed and flushes the remaining buffered events to the provided BufferedSink but does not close it.

void

Enqueue a PooledTracePacketArray to be written to the trace.

void

Clears the current buffer and drops the events without writing them out.

final void
flushTo(@NonNull BufferedSink bufferedSink)

Flushes the current buffer to the provided BufferedSink.

void

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

Public constructors

InMemoryRingBufferTraceSink

Added in 2.0.0-alpha03
public InMemoryRingBufferTraceSink(
    @IntRange(from = 1) int sequenceId,
    long capacityInBytes
)

Public methods

close

Added in 2.0.0-alpha03
public void close()

Marks the InMemoryRingBufferTraceSink as closed and clears any remaining buffered events without writing them out.

closeAndFlushTo

Added in 2.0.0-alpha03
public final void closeAndFlushTo(@NonNull BufferedSink bufferedSink)

Marks the InMemoryRingBufferTraceSink as closed and flushes the remaining buffered events to the provided BufferedSink but does not close it.

enqueue

Added in 2.0.0-alpha03
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-alpha03
public void flush()

Clears the current buffer and drops the events without writing them out.

flushTo

Added in 2.0.0-alpha03
public final void flushTo(@NonNull BufferedSink bufferedSink)

Flushes the current buffer to the provided BufferedSink.

This writes out the ring buffer's contents to the sink and then clears the ring buffer.

onDroppedTraceEvent

Added in 2.0.0-alpha03
public void onDroppedTraceEvent()

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

This function may be called from any thread.