Life of a Perfetto tracing session
This document explains how producer, service and consumer interact end-to-end during a tracing session, with references to code and IPC requests / responses.
- One or more producers connect to the tracing service and sets up their IPC channels.
- Each producer advertises one or more data sources through the
RegisterDataSource
IPC. Nothing more happens on the Producer until this point. Nothing traces by default. - A consumer connects to the tracing service and sets up the IPC channel.
- The consumer starts a tracing session sending a
trace config to the service through the
EnableTracing
IPC. - The service creates as many new trace buffers as specified in the config.
- The service iterates through the
data_sources
section of the trace config: for each entry, if a matching data source is found in the producer(s) (accordingly to what advertised in step 2): - The service sends a
SetupTracing
IPC message, passing a shared memory buffer to the producer(s) (only once for each producer). - The service sends a
StartDataSource
IPC message to each producer, for each data source configured in the trace config and present in the producer (if any). - The producer creates one or more data source instance, as instructed in the previous step.
- Each data source instance creates one or more
TraceWriter
(typically one for each thread). - Each
TraceWriter
writes one or moreTracePacket
. - While doing so, each
TraceWriter
takes ownership of shared memory buffer's chunks, using theSharedMemoryArbiter
. - While writing a
TracePacket
, theTraceWriter
will unavoidably cross the chunk boundary (typically 4KB, but can configured to be smaller). - When this happens the
TraceWriter
will release the current chunk and acquire a new chunk through theSharedMemoryArbiter
. - The
SharedMemoryArbiter
will, out-of-band, send aCommitDataRequest
to the service, requesting it to move some chunks of the shared memory buffer into the final trace buffer. - If one or more long
TracePacket
were fragmented over several chunks, it is possible that some of these chunks are gone from the shared memory buffer and committed into the final trace buffer (step 16). In this case, theSharedMemoryArbiter
will send an otherCommitDataRequest
IPC message to request the out-of-band patching of the chunk data into the final trace buffer. - The service will check if the given chunk, identified by the tuple
{ProducerID (unspoofable), WriterID, ChunkID}
is still present in the trace buffer and if so will proceed to patch it (% checks). - The consumer sends a
FlushRequest
to the service, asking it commit all data on flight in the trace buffers. - The service, in turn, issues a
Flush
request to all producers involved in the trace session. - The producer(s) will
Flush()
all theirTraceWriter
and reply to the service flush request. - Once the service has received an ACK to all flush requests from all
producers (or the
flush timeout has expired)
it replies to the consumer's
FlushRequest
- The consumer optionally sends a
DisableTracing
IPC request to stop tracing and freeze the content of the trace buffers. - The service will in turn broadcast a
StopDataSource
request for each data source in each Producer. - At this point the Consumer issues a
ReadBuffers
IPC request (unless it did previously pass a file descriptor to the service when enabling the trace through thewrite_into_file
field of the trace config). - The service reads the trace buffers and streams all the
TracePacket(s)
back to the consumer. - If a trace packet stored in the trace buffer is incomplete (e.g., a fragment
is missing) or is marked as pending out-of-band patching, the given writer
sequence is interrupted and no more packets for that sequence are read.
Other packets for other
TraceWriter
sequences will be unaffected. - The consumer sends a
FreeBuffers
(or simply disconnects). - The service tears down all the trace buffers for the session.