xref: /aosp_15_r20/external/perfetto/docs/design-docs/extensions.md (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1*6dbdd20aSAndroid Build Coastguard WorkerExtensions: adding new types to traces
2*6dbdd20aSAndroid Build Coastguard Worker======================================
3*6dbdd20aSAndroid Build Coastguard Worker
4*6dbdd20aSAndroid Build Coastguard WorkerNOTE: **extensions are work-in-progress and are not ready to be used at the
5*6dbdd20aSAndroid Build Coastguard Workermoment**
6*6dbdd20aSAndroid Build Coastguard Worker
7*6dbdd20aSAndroid Build Coastguard WorkerCurrently, it is not possible to add new types to traces while using Perfetto
8*6dbdd20aSAndroid Build Coastguard Workerwithout modifying Perfetto proto message definitions upstream.
9*6dbdd20aSAndroid Build Coastguard Worker
10*6dbdd20aSAndroid Build Coastguard WorkerThis page describes ongoing work to use [protobuf
11*6dbdd20aSAndroid Build Coastguard Workerextensions](https://developers.google.com/protocol-buffers/docs/overview#extensions)
12*6dbdd20aSAndroid Build Coastguard Workerin order to make it possible to define new typed messages outside of the
13*6dbdd20aSAndroid Build Coastguard WorkerPerfetto repository.
14*6dbdd20aSAndroid Build Coastguard Worker
15*6dbdd20aSAndroid Build Coastguard WorkerProtozero support
16*6dbdd20aSAndroid Build Coastguard Worker-----------------
17*6dbdd20aSAndroid Build Coastguard Worker
18*6dbdd20aSAndroid Build Coastguard WorkerPerfetto uses its own implementation of code generation for protocol buffer
19*6dbdd20aSAndroid Build Coastguard Workermessages called [Protozero](/docs/design-docs/protozero.md), which is not a
20*6dbdd20aSAndroid Build Coastguard Workerfull-fledged protobuf implementation. Implementation of extensions is fairly
21*6dbdd20aSAndroid Build Coastguard Workerlimited, and all extensions are supposed to be nested inside a message that is
22*6dbdd20aSAndroid Build Coastguard Workerused in order to provide the class name for generated code.
23*6dbdd20aSAndroid Build Coastguard Worker
24*6dbdd20aSAndroid Build Coastguard WorkerFor example,
25*6dbdd20aSAndroid Build Coastguard Worker
26*6dbdd20aSAndroid Build Coastguard Worker    message MyEvent {
27*6dbdd20aSAndroid Build Coastguard Worker        extend TrackEvent {
28*6dbdd20aSAndroid Build Coastguard Worker            optional string custom_string = 1000;
29*6dbdd20aSAndroid Build Coastguard Worker        }
30*6dbdd20aSAndroid Build Coastguard Worker    }
31*6dbdd20aSAndroid Build Coastguard Worker
32*6dbdd20aSAndroid Build Coastguard WorkerIs going to generate a subclass of `TrackEvent` called `MyEvent`, that is going
33*6dbdd20aSAndroid Build Coastguard Workerto have a new method to set `custom_string` in addition to all other protobuf
34*6dbdd20aSAndroid Build Coastguard Workerfields defined in `TrackEvent`.
35*6dbdd20aSAndroid Build Coastguard Worker
36*6dbdd20aSAndroid Build Coastguard WorkerDeserialization
37*6dbdd20aSAndroid Build Coastguard Worker---------------
38*6dbdd20aSAndroid Build Coastguard Worker
39*6dbdd20aSAndroid Build Coastguard WorkerWhen analyzing traces, protos are not used directly and instead are parsed into
40*6dbdd20aSAndroid Build Coastguard Workerdatabase, which can be queried by SQL. In order to make it possible, Perfetto
41*6dbdd20aSAndroid Build Coastguard Workerhas to know field descriptors (the binary representation of the extended proto
42*6dbdd20aSAndroid Build Coastguard Workerschema) of the extensions. Currently, the only way to do that is to add an
43*6dbdd20aSAndroid Build Coastguard Worker[ExtensionDescriptor
44*6dbdd20aSAndroid Build Coastguard Workerpacket](reference/trace-packet-proto.autogen#ExtensionDescriptor). In the
45*6dbdd20aSAndroid Build Coastguard Workerfuture, there is going to be a way to specify protobuf extensions at compile
46*6dbdd20aSAndroid Build Coastguard Workertime in order to be able to avoid this overhead in every single trace.
47*6dbdd20aSAndroid Build Coastguard Worker
48*6dbdd20aSAndroid Build Coastguard WorkerHowever, an ability to specify extension descriptors in the trace itself will
49*6dbdd20aSAndroid Build Coastguard Workerstill be useful in order to be able to use a pre-compiled trace processor in the
50*6dbdd20aSAndroid Build Coastguard WorkerUI when adding new typed messages during local development.
51*6dbdd20aSAndroid Build Coastguard Worker
52*6dbdd20aSAndroid Build Coastguard WorkerDeserialization of protobuf extension are supported only for TrackEvent message
53*6dbdd20aSAndroid Build Coastguard Workerat the moment, and is implemented in trace processor via ProtoToArgsUtils class.
54*6dbdd20aSAndroid Build Coastguard WorkerThe extensions will appear in args table, similar to other trace event
55*6dbdd20aSAndroid Build Coastguard Workerarguments.
56*6dbdd20aSAndroid Build Coastguard Worker
57*6dbdd20aSAndroid Build Coastguard WorkerTesting extensions support inside Perfetto
58*6dbdd20aSAndroid Build Coastguard Worker------------------------------------------
59*6dbdd20aSAndroid Build Coastguard Worker
60*6dbdd20aSAndroid Build Coastguard WorkerPerfetto trace processor is mostly tested by integration tests, where input
61*6dbdd20aSAndroid Build Coastguard Workertraces are specified most frequently in textproto format. Textproto format
62*6dbdd20aSAndroid Build Coastguard Workersupports extensions, but the parser has to be aware of all the extensions used.
63*6dbdd20aSAndroid Build Coastguard WorkerIn order to make it possible, all the extensions that are used in integration
64*6dbdd20aSAndroid Build Coastguard Workertests have to be specified in the `test_extensions.proto` file. Since this file
65*6dbdd20aSAndroid Build Coastguard Workeris only used in the testing harness and is parsed by protoc, it does not have to
66*6dbdd20aSAndroid Build Coastguard Workeradhere to the convention of all extensions being inside a wrapper message, which
67*6dbdd20aSAndroid Build Coastguard Workerhelps with making extension identifiers more concise.
68