xref: /aosp_15_r20/external/perfetto/docs/concepts/service-model.md (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1*6dbdd20aSAndroid Build Coastguard Worker# Service-based model
2*6dbdd20aSAndroid Build Coastguard Worker
3*6dbdd20aSAndroid Build Coastguard Worker![Perfetto Stack](https://storage.googleapis.com/perfetto/markdown_img/producer-service-consumer.png)
4*6dbdd20aSAndroid Build Coastguard Worker
5*6dbdd20aSAndroid Build Coastguard Worker## Service
6*6dbdd20aSAndroid Build Coastguard Worker
7*6dbdd20aSAndroid Build Coastguard WorkerThe tracing service is a long-lived entity (a system daemon on Linux/Android,
8*6dbdd20aSAndroid Build Coastguard Workera service in Chrome) that has the following responsibilities:
9*6dbdd20aSAndroid Build Coastguard Worker
10*6dbdd20aSAndroid Build Coastguard Worker* Maintains a registry of active producers and their data sources.
11*6dbdd20aSAndroid Build Coastguard Worker* Owns the trace buffers.
12*6dbdd20aSAndroid Build Coastguard Worker* Handles multiplexing of several tracing sessions.
13*6dbdd20aSAndroid Build Coastguard Worker* Routes the trace config from the consumers to the corresponding producers.
14*6dbdd20aSAndroid Build Coastguard Worker* Tells the Producers when and what to trace.
15*6dbdd20aSAndroid Build Coastguard Worker* Moves data from the Producer's shared memory buffer to the central non-shared
16*6dbdd20aSAndroid Build Coastguard Worker  trace buffers.
17*6dbdd20aSAndroid Build Coastguard Worker
18*6dbdd20aSAndroid Build Coastguard Worker## Producer
19*6dbdd20aSAndroid Build Coastguard Worker
20*6dbdd20aSAndroid Build Coastguard WorkerA producer is an untrusted entity that offers the ability to contribute to the
21*6dbdd20aSAndroid Build Coastguard Workertrace. In a multiprocess model, a producer almost always corresponds to a client
22*6dbdd20aSAndroid Build Coastguard Workerprocess of the tracing service. It advertises its ability to contribute to the trace with one or more data sources.
23*6dbdd20aSAndroid Build Coastguard WorkerEach producer has exactly:
24*6dbdd20aSAndroid Build Coastguard Worker
25*6dbdd20aSAndroid Build Coastguard Worker* One shared memory buffer, shared exclusively with the tracing service.
26*6dbdd20aSAndroid Build Coastguard Worker* One IPC channel with the tracing service.
27*6dbdd20aSAndroid Build Coastguard Worker
28*6dbdd20aSAndroid Build Coastguard WorkerA producer is completely decoupled (both technically and conceptually) from
29*6dbdd20aSAndroid Build Coastguard Workerconsumer(s). A producer knows nothing about:
30*6dbdd20aSAndroid Build Coastguard Worker
31*6dbdd20aSAndroid Build Coastguard Worker* How many consumer(s) are connected to the service.
32*6dbdd20aSAndroid Build Coastguard Worker* How many tracing sessions are active.
33*6dbdd20aSAndroid Build Coastguard Worker* How many other producer(s) are registered or active.
34*6dbdd20aSAndroid Build Coastguard Worker* Trace data written by other producer(s).
35*6dbdd20aSAndroid Build Coastguard Worker
36*6dbdd20aSAndroid Build Coastguard WorkerNOTE: In rare circumstances a process can host more than one producer and hence more
37*6dbdd20aSAndroid Build Coastguard Workerthan one shared memory buffer. This can be the case for a process bundling
38*6dbdd20aSAndroid Build Coastguard Workerthird-party libraries that in turn include the Perfetto client library.
39*6dbdd20aSAndroid Build Coastguard WorkerConcrete example: at some point in the future Chrome might expose one Producer for tracing within the main project, one for V8 and one for Skia (for each child
40*6dbdd20aSAndroid Build Coastguard Workerprocess).
41*6dbdd20aSAndroid Build Coastguard Worker
42*6dbdd20aSAndroid Build Coastguard Worker## Consumer
43*6dbdd20aSAndroid Build Coastguard WorkerA consumer is a trusted entity (a cmdline client on Linux/Android, an interface
44*6dbdd20aSAndroid Build Coastguard Workerof the Browser process in Chrome) that controls (non-exclusively) the tracing service and reads back (destructively) the trace buffers.
45*6dbdd20aSAndroid Build Coastguard WorkerA consumer has the ability to:
46*6dbdd20aSAndroid Build Coastguard Worker* Send a [trace config](#) to the service, determining:
47*6dbdd20aSAndroid Build Coastguard Worker * How many trace buffers to create.
48*6dbdd20aSAndroid Build Coastguard Worker * How big the trace buffers should be.
49*6dbdd20aSAndroid Build Coastguard Worker * The policy for each buffer (*ring-buffer* or *stop-when-full*).
50*6dbdd20aSAndroid Build Coastguard Worker * Which data sources to enable.
51*6dbdd20aSAndroid Build Coastguard Worker * The configuration for each data source.
52*6dbdd20aSAndroid Build Coastguard Worker * The target buffer for the data produced by each data source configured.
53*6dbdd20aSAndroid Build Coastguard Worker* Enable and disable tracing.
54*6dbdd20aSAndroid Build Coastguard Worker* Read back the trace buffers:
55*6dbdd20aSAndroid Build Coastguard Worker  * Streaming data over the IPC channel.
56*6dbdd20aSAndroid Build Coastguard Worker  * Passing a file descriptor to the service and instructing it to periodically
57*6dbdd20aSAndroid Build Coastguard Worker    save the trace buffers into the file.
58*6dbdd20aSAndroid Build Coastguard Worker
59*6dbdd20aSAndroid Build Coastguard Worker## Data source
60*6dbdd20aSAndroid Build Coastguard Worker
61*6dbdd20aSAndroid Build Coastguard WorkerA data source is a capability, exposed by a Producer, of providing some tracing
62*6dbdd20aSAndroid Build Coastguard Workerdata. A data source almost always defines its own schema (a protobuf) consisting
63*6dbdd20aSAndroid Build Coastguard Workerof:
64*6dbdd20aSAndroid Build Coastguard Worker* At most one `DataSourceConfig` sub-message:
65*6dbdd20aSAndroid Build Coastguard Worker
66*6dbdd20aSAndroid Build Coastguard Worker  ([example](/protos/perfetto/config/ftrace/ftrace_config.proto))
67*6dbdd20aSAndroid Build Coastguard Worker* One or more `TracePacket` sub-messages
68*6dbdd20aSAndroid Build Coastguard Worker  ([example](/protos/perfetto/trace/ps/process_tree.proto))
69*6dbdd20aSAndroid Build Coastguard Worker
70*6dbdd20aSAndroid Build Coastguard WorkerDifferent producers may expose the same data source. Concrete example:
71*6dbdd20aSAndroid Build Coastguard Worker*** aside
72*6dbdd20aSAndroid Build Coastguard WorkerAt some point in the near future we might offer, as part of Perfetto, a library
73*6dbdd20aSAndroid Build Coastguard Workerfor in-process heap profiling. In such case more than one producer, linking
74*6dbdd20aSAndroid Build Coastguard Workeragainst the updated Perfetto library, will expose the heap profiler data source,
75*6dbdd20aSAndroid Build Coastguard Workerfor its own process.
76*6dbdd20aSAndroid Build Coastguard Worker**
77*6dbdd20aSAndroid Build Coastguard Worker
78*6dbdd20aSAndroid Build Coastguard Worker## IPC channel
79*6dbdd20aSAndroid Build Coastguard WorkerIn a multiprocess scenario, each producer and each consumer interact with the
80*6dbdd20aSAndroid Build Coastguard Workerservice using an IPC channel. IPC is used only in non-fast-path interactions,
81*6dbdd20aSAndroid Build Coastguard Workermostly handshakes such as enabling/disabling trace (consumer), (un)registering
82*6dbdd20aSAndroid Build Coastguard Workerand starting/stopping data sources (producer). The IPC is typically NOT employed
83*6dbdd20aSAndroid Build Coastguard Workerto transport the protobufs for the trace.
84*6dbdd20aSAndroid Build Coastguard WorkerPerfetto provides a POSIX-friendly IPC implementation, based on protobufs over a
85*6dbdd20aSAndroid Build Coastguard WorkerUNIX socket (see
86*6dbdd20aSAndroid Build Coastguard Worker[Socket protocol](/docs/design-docs/api-and-abi#socket-protocol)).
87*6dbdd20aSAndroid Build Coastguard Worker
88*6dbdd20aSAndroid Build Coastguard WorkerThat IPC implementation is not mandated. Perfetto allows the embedder:
89*6dbdd20aSAndroid Build Coastguard Worker
90*6dbdd20aSAndroid Build Coastguard Worker* Wrap its own IPC subsystem (e.g., Perfetto in Chromium uses Mojo)
91*6dbdd20aSAndroid Build Coastguard Worker* Not use an IPC mechanism at all and just short circuit the
92*6dbdd20aSAndroid Build Coastguard Worker  Producer <> Service <> Consumer interaction via `PostTask(s)`.
93*6dbdd20aSAndroid Build Coastguard Worker
94*6dbdd20aSAndroid Build Coastguard Worker## Shared memory buffer
95*6dbdd20aSAndroid Build Coastguard WorkerProducer(s) write tracing data, in the form of protobuf-encoded binary blobs,
96*6dbdd20aSAndroid Build Coastguard Workerdirectly into its shared memory buffer, using a special library called
97*6dbdd20aSAndroid Build Coastguard Worker[ProtoZero](/docs/design-docs/protozero.md). The shared memory buffer:
98*6dbdd20aSAndroid Build Coastguard Worker
99*6dbdd20aSAndroid Build Coastguard Worker* Has a fixed and typically small size (configurable, default: 128 KB).
100*6dbdd20aSAndroid Build Coastguard Worker* Is an ABI and must maintain backwards compatibility.
101*6dbdd20aSAndroid Build Coastguard Worker* Is shared by all data sources of the producer.
102*6dbdd20aSAndroid Build Coastguard Worker* Is independent of the number and the size of the trace buffers.
103*6dbdd20aSAndroid Build Coastguard Worker* Is independent of the number of Consumer(s).
104*6dbdd20aSAndroid Build Coastguard Worker* Is partitioned in *chunks* of variable size.
105*6dbdd20aSAndroid Build Coastguard Worker
106*6dbdd20aSAndroid Build Coastguard WorkerEach chunk:
107*6dbdd20aSAndroid Build Coastguard Worker
108*6dbdd20aSAndroid Build Coastguard Worker* Is owned exclusively by one Producer thread (or shared through a mutex).
109*6dbdd20aSAndroid Build Coastguard Worker* Contains a linear sequence of `TracePacket(s)`, or
110*6dbdd20aSAndroid Build Coastguard Worker  fragments of that. A `TracePacket` can span across several chunks, the
111*6dbdd20aSAndroid Build Coastguard Worker  fragmentation is not exposed to the consumers (consumers always see whole
112*6dbdd20aSAndroid Build Coastguard Worker  packets as if they were never fragmented).
113*6dbdd20aSAndroid Build Coastguard Worker* Can be owned and written by exactly one `TraceWriter`.
114*6dbdd20aSAndroid Build Coastguard Worker* Is part of a reliable and ordered sequence, identified by the `WriterID`:
115*6dbdd20aSAndroid Build Coastguard Worker  packets in a sequence are guaranteed to be read back in order, without gaps
116*6dbdd20aSAndroid Build Coastguard Worker  and without repetitions.
117*6dbdd20aSAndroid Build Coastguard Worker
118*6dbdd20aSAndroid Build Coastguard WorkerSee the comments in
119*6dbdd20aSAndroid Build Coastguard Worker[shared_memory_abi.h](/include/perfetto/ext/tracing/core/shared_memory_abi.h)
120*6dbdd20aSAndroid Build Coastguard Workerfor more details about the binary format of this buffer.
121