1*6dbdd20aSAndroid Build Coastguard Worker# Life of a Perfetto tracing session 2*6dbdd20aSAndroid Build Coastguard Worker 3*6dbdd20aSAndroid Build Coastguard WorkerThis document explains how producer, service and consumer interact end-to-end 4*6dbdd20aSAndroid Build Coastguard Workerduring a tracing session, with references to code and IPC requests / responses. 5*6dbdd20aSAndroid Build Coastguard Worker 6*6dbdd20aSAndroid Build Coastguard Worker1. One or more producers connect to the tracing service and sets up their IPC 7*6dbdd20aSAndroid Build Coastguard Worker channels. 8*6dbdd20aSAndroid Build Coastguard Worker2. Each producer advertises one or more data sources through the 9*6dbdd20aSAndroid Build Coastguard Worker [`RegisterDataSource`](/protos/perfetto/ipc/producer_port.proto#34) IPC. 10*6dbdd20aSAndroid Build Coastguard Worker Nothing more happens on the Producer until this point. Nothing traces by 11*6dbdd20aSAndroid Build Coastguard Worker default. 12*6dbdd20aSAndroid Build Coastguard Worker3. A consumer connects to the tracing service and sets up the IPC channel. 13*6dbdd20aSAndroid Build Coastguard Worker4. The consumer starts a tracing session sending a 14*6dbdd20aSAndroid Build Coastguard Worker [trace config](/docs/concepts/config.md) to the service through the 15*6dbdd20aSAndroid Build Coastguard Worker [`EnableTracing`](/protos/perfetto/ipc/consumer_port.proto#65) IPC. 16*6dbdd20aSAndroid Build Coastguard Worker6. The service creates as many new trace buffers as specified in the config. 17*6dbdd20aSAndroid Build Coastguard Worker7. The service iterates through the 18*6dbdd20aSAndroid Build Coastguard Worker [`data_sources`](/protos/perfetto/config/trace_config.proto#50) section of 19*6dbdd20aSAndroid Build Coastguard Worker the trace config: for each entry, if a matching data source is found in the 20*6dbdd20aSAndroid Build Coastguard Worker producer(s) (accordingly to what advertised in step 2): 21*6dbdd20aSAndroid Build Coastguard Worker8. The service sends a 22*6dbdd20aSAndroid Build Coastguard Worker [`SetupTracing`](/protos/perfetto/ipc/producer_port.proto#112) IPC message, 23*6dbdd20aSAndroid Build Coastguard Worker passing a shared memory buffer to the producer(s) (only once for each 24*6dbdd20aSAndroid Build Coastguard Worker producer). 25*6dbdd20aSAndroid Build Coastguard Worker9. The service sends a 26*6dbdd20aSAndroid Build Coastguard Worker [`StartDataSource`](/protos/perfetto/ipc/producer_port.proto#105) IPC message 27*6dbdd20aSAndroid Build Coastguard Worker to each producer, for each data source configured in the trace config and 28*6dbdd20aSAndroid Build Coastguard Worker present in the producer (if any). 29*6dbdd20aSAndroid Build Coastguard Worker10. The producer creates one or more data source instance, as instructed in 30*6dbdd20aSAndroid Build Coastguard Worker the previous step. 31*6dbdd20aSAndroid Build Coastguard Worker11. Each data source instance creates one or more 32*6dbdd20aSAndroid Build Coastguard Worker [`TraceWriter`](/include/perfetto/ext/tracing/core/trace_writer.h) (typically 33*6dbdd20aSAndroid Build Coastguard Worker one for each thread). 34*6dbdd20aSAndroid Build Coastguard Worker12. Each `TraceWriter` writes one or more 35*6dbdd20aSAndroid Build Coastguard Worker [`TracePacket`](/protos/perfetto/trace/trace_packet.proto). 36*6dbdd20aSAndroid Build Coastguard Worker13. While doing so, each `TraceWriter` takes ownership of shared memory buffer's 37*6dbdd20aSAndroid Build Coastguard Worker chunks, using the [`SharedMemoryArbiter`](/include/perfetto/ext/tracing/core/shared_memory_arbiter.h). 38*6dbdd20aSAndroid Build Coastguard Worker14. While writing a `TracePacket`, the `TraceWriter` will unavoidably cross the 39*6dbdd20aSAndroid Build Coastguard Worker chunk boundary (typically 4KB, but can configured to be smaller). 40*6dbdd20aSAndroid Build Coastguard Worker15. When this happens the `TraceWriter` will release the current chunk and 41*6dbdd20aSAndroid Build Coastguard Worker acquire a new chunk through the `SharedMemoryArbiter`. 42*6dbdd20aSAndroid Build Coastguard Worker16. The `SharedMemoryArbiter` will, out-of-band, send a 43*6dbdd20aSAndroid Build Coastguard Worker [`CommitDataRequest`](/protos/perfetto/ipc/producer_port.proto#41) to the 44*6dbdd20aSAndroid Build Coastguard Worker service, requesting it to move some chunks of the shared memory buffer into 45*6dbdd20aSAndroid Build Coastguard Worker the final trace buffer. 46*6dbdd20aSAndroid Build Coastguard Worker17. If one or more long `TracePacket` were fragmented over several chunks, it is 47*6dbdd20aSAndroid Build Coastguard Worker possible that some of these chunks are gone from the shared memory 48*6dbdd20aSAndroid Build Coastguard Worker buffer and committed into the final trace buffer (step 16). In this case, 49*6dbdd20aSAndroid Build Coastguard Worker the `SharedMemoryArbiter` will send an other `CommitDataRequest` IPC message 50*6dbdd20aSAndroid Build Coastguard Worker to request the out-of-band patching of the chunk data into the final trace 51*6dbdd20aSAndroid Build Coastguard Worker buffer. 52*6dbdd20aSAndroid Build Coastguard Worker18. The service will check if the given chunk, identified by the tuple 53*6dbdd20aSAndroid Build Coastguard Worker `{ProducerID (unspoofable), WriterID, ChunkID}` is still present in the 54*6dbdd20aSAndroid Build Coastguard Worker trace buffer and if so will proceed to patch it (% checks). 55*6dbdd20aSAndroid Build Coastguard Worker19. The consumer sends a [`FlushRequest`](/protos/perfetto/ipc/consumer_port.proto#52) 56*6dbdd20aSAndroid Build Coastguard Worker to the service, asking it commit all data on flight in the trace buffers. 57*6dbdd20aSAndroid Build Coastguard Worker20. The service, in turn, issues a 58*6dbdd20aSAndroid Build Coastguard Worker [`Flush`](/protos/perfetto/ipc/producer_port.proto#132) request to all 59*6dbdd20aSAndroid Build Coastguard Worker producers involved in the trace session. 60*6dbdd20aSAndroid Build Coastguard Worker21. The producer(s) will `Flush()` all their `TraceWriter` and reply to the 61*6dbdd20aSAndroid Build Coastguard Worker service flush request. 62*6dbdd20aSAndroid Build Coastguard Worker22. Once the service has received an ACK to all flush requests from all 63*6dbdd20aSAndroid Build Coastguard Worker producers (or the 64*6dbdd20aSAndroid Build Coastguard Worker [flush timeout](/protos/perfetto/ipc/consumer_port.proto#117) has expired) 65*6dbdd20aSAndroid Build Coastguard Worker it replies to the consumer's `FlushRequest` 66*6dbdd20aSAndroid Build Coastguard Worker23. The consumer optionally sends a 67*6dbdd20aSAndroid Build Coastguard Worker [`DisableTracing`](/protos/perfetto/ipc/consumer_port.proto#38) IPC request 68*6dbdd20aSAndroid Build Coastguard Worker to stop tracing and freeze the content of the trace buffers. 69*6dbdd20aSAndroid Build Coastguard Worker24. The service will in turn broadcast a 70*6dbdd20aSAndroid Build Coastguard Worker [`StopDataSource`](/protos/perfetto/ipc/producer_port.proto#110) request for 71*6dbdd20aSAndroid Build Coastguard Worker each data source in each Producer. 72*6dbdd20aSAndroid Build Coastguard Worker23. At this point the Consumer issues a 73*6dbdd20aSAndroid Build Coastguard Worker [`ReadBuffers`](/protos/perfetto/ipc/consumer_port.proto#41) IPC request 74*6dbdd20aSAndroid Build Coastguard Worker (unless it did previously pass a file descriptor to the service when 75*6dbdd20aSAndroid Build Coastguard Worker enabling the trace through the `write_into_file` field of the trace config). 76*6dbdd20aSAndroid Build Coastguard Worker24. The service reads the trace buffers and streams all the `TracePacket(s)` 77*6dbdd20aSAndroid Build Coastguard Worker back to the consumer. 78*6dbdd20aSAndroid Build Coastguard Worker25. If a trace packet stored in the trace buffer is incomplete (e.g., a fragment 79*6dbdd20aSAndroid Build Coastguard Worker is missing) or is marked as pending out-of-band patching, the given writer 80*6dbdd20aSAndroid Build Coastguard Worker sequence is interrupted and no more packets for that sequence are read. 81*6dbdd20aSAndroid Build Coastguard Worker Other packets for other `TraceWriter` sequences will be unaffected. 82*6dbdd20aSAndroid Build Coastguard Worker26. The consumer sends a `FreeBuffers` (or simply disconnects). 83*6dbdd20aSAndroid Build Coastguard Worker27. The service tears down all the trace buffers for the session. 84