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
RegisterDataSourceIPC. 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
EnableTracingIPC. - The service creates as many new trace buffers as specified in the config.
 - The service iterates through the
data_sourcessection 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
SetupTracingIPC message, passing a shared memory buffer to the producer(s) (only once for each producer). - The service sends a
StartDataSourceIPC 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 
TraceWriterwrites one or moreTracePacket. - While doing so, each 
TraceWritertakes ownership of shared memory buffer's chunks, using theSharedMemoryArbiter. - While writing a 
TracePacket, theTraceWriterwill unavoidably cross the chunk boundary (typically 4KB, but can configured to be smaller). - When this happens the 
TraceWriterwill release the current chunk and acquire a new chunk through theSharedMemoryArbiter. - The 
SharedMemoryArbiterwill, out-of-band, send aCommitDataRequestto the service, requesting it to move some chunks of the shared memory buffer into the final trace buffer. - If one or more long 
TracePacketwere 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, theSharedMemoryArbiterwill send an otherCommitDataRequestIPC 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 
FlushRequestto the service, asking it commit all data on flight in the trace buffers. - The service, in turn, issues a
Flushrequest to all producers involved in the trace session. - The producer(s) will 
Flush()all theirTraceWriterand 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
DisableTracingIPC request to stop tracing and freeze the content of the trace buffers. - The service will in turn broadcast a
StopDataSourcerequest for each data source in each Producer. - At this point the Consumer issues a
ReadBuffersIPC request (unless it did previously pass a file descriptor to the service when enabling the trace through thewrite_into_filefield 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 
TraceWritersequences will be unaffected. - The consumer sends a 
FreeBuffers(or simply disconnects). - The service tears down all the trace buffers for the session.