xref: /aosp_15_r20/external/perfetto/docs/contributing/common-tasks.md (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1*6dbdd20aSAndroid Build Coastguard Worker# Common tasks
2*6dbdd20aSAndroid Build Coastguard Worker
3*6dbdd20aSAndroid Build Coastguard WorkerThe checklists below show how to achieve some common tasks in the codebase.
4*6dbdd20aSAndroid Build Coastguard Worker
5*6dbdd20aSAndroid Build Coastguard Worker## Add a new ftrace event
6*6dbdd20aSAndroid Build Coastguard Worker
7*6dbdd20aSAndroid Build Coastguard Worker1. Find the `format` file for your event. The location of the file depends where `tracefs` is mounted but can often be found at `/sys/kernel/debug/tracing/events/EVENT_GROUP/EVENT_NAME/format`.
8*6dbdd20aSAndroid Build Coastguard Worker2. Copy the format file into the codebase at `src/traced/probes/ftrace/test/data/synthetic/events/EVENT_GROUP/EVENT_NAME/format`.
9*6dbdd20aSAndroid Build Coastguard Worker3. Add the event to [src/tools/ftrace_proto_gen/event_list](/src/tools/ftrace_proto_gen/event_list).
10*6dbdd20aSAndroid Build Coastguard Worker4. Run `tools/run_ftrace_proto_gen`. This will update `protos/perfetto/trace/ftrace/ftrace_event.proto` and `protos/perfetto/trace/ftrace/GROUP_NAME.proto`.
11*6dbdd20aSAndroid Build Coastguard Worker5. Run `tools/gen_all out/YOUR_BUILD_DIRECTORY`. This will update `src/traced/probes/ftrace/event_info.cc` and `protos/perfetto/trace/perfetto_trace.proto`.
12*6dbdd20aSAndroid Build Coastguard Worker6. If special handling in `trace_processor` is desired update [src/trace_processor/importers/ftrace/ftrace_parser.cc](/src/trace_processor/importers/ftrace/ftrace_parser.cc) to parse the event.
13*6dbdd20aSAndroid Build Coastguard Worker7. Upload and land your change as normal.
14*6dbdd20aSAndroid Build Coastguard Worker
15*6dbdd20aSAndroid Build Coastguard WorkerHere is an [example change](https://android-review.googlesource.com/c/platform/external/perfetto/+/1290645) which added the `ion/ion_stat` event.
16*6dbdd20aSAndroid Build Coastguard Worker
17*6dbdd20aSAndroid Build Coastguard Worker## Contribute to SQL standard library
18*6dbdd20aSAndroid Build Coastguard Worker
19*6dbdd20aSAndroid Build Coastguard Worker1. Add or edit an SQL file inside `perfetto/src/trace_processor/stdlib/`. This SQL file will be a new standard library module.
20*6dbdd20aSAndroid Build Coastguard Worker2. For a new file inside an existing package add the file to the corresponding `BUILD.gn`.
21*6dbdd20aSAndroid Build Coastguard Worker3. For a new package (subdirectory of `/stdlib/`), the package name (directory name) has to be added to the list in `/stdlib/BUILD.gn`.
22*6dbdd20aSAndroid Build Coastguard Worker
23*6dbdd20aSAndroid Build Coastguard WorkerFiles inside the standard library have to be formatted in a very specific way, as its structure is used to generate documentation. There are presubmit checks, but they are not infallible.
24*6dbdd20aSAndroid Build Coastguard Worker
25*6dbdd20aSAndroid Build Coastguard Worker- Running the file cannot generate any data. There can be only `CREATE PERFETTO {FUNCTION|TABLE|VIEW|MACRO}` statements inside.
26*6dbdd20aSAndroid Build Coastguard Worker- The name of each standard library object needs to start with `{module_name}_` or be prefixed with an underscore(`_`) for internal objects.
27*6dbdd20aSAndroid Build Coastguard Worker  The names must only contain lower and upper case letters and underscores. When a module is included (using the `INCLUDE PERFETTO MODULE`) the internal objects  should not be treated as an API.
28*6dbdd20aSAndroid Build Coastguard Worker- Every table or view should have [a schema](/docs/analysis/perfetto-sql-syntax.md#tableview-schema).
29*6dbdd20aSAndroid Build Coastguard Worker
30*6dbdd20aSAndroid Build Coastguard Worker### Documentation
31*6dbdd20aSAndroid Build Coastguard Worker
32*6dbdd20aSAndroid Build Coastguard Worker- Every non internal object, as well as its function arguments and columns in its schema have to be prefixed with an SQL comment documenting it.
33*6dbdd20aSAndroid Build Coastguard Worker- Any text is going to be parsed as markdown, so usage of markdown functionality (code, links, lists) is encouraged.
34*6dbdd20aSAndroid Build Coastguard WorkerWhitespaces in anything apart from descriptions are ignored, so comments can be formatted neatly.
35*6dbdd20aSAndroid Build Coastguard WorkerIf the line with description exceeds 80 chars, description can be continued in following lines.
36*6dbdd20aSAndroid Build Coastguard Worker  - **Table/view**: each has to have schema, object description and a comment above each column's definition in the schema.
37*6dbdd20aSAndroid Build Coastguard Worker    - Description is any text in the comment above `CREATE PERFETTO {TABLE,VIEW}` statement.
38*6dbdd20aSAndroid Build Coastguard Worker    - Column's comment is the text immediately above column definition in the schema.
39*6dbdd20aSAndroid Build Coastguard Worker  - **Scalar Functions**: each has to have a function description and description of return value in this order.
40*6dbdd20aSAndroid Build Coastguard Worker    - Function description is any text in the comment above `CREATE PERFETTO FUNCTION` statement.
41*6dbdd20aSAndroid Build Coastguard Worker    - For each argument there has to be a comment line immediately above argument definition.
42*6dbdd20aSAndroid Build Coastguard Worker    - Return comment should immediately precede `RETURNS`.
43*6dbdd20aSAndroid Build Coastguard Worker  - **Table Functions**: each has to have a function description, list of arguments (names, types, description) and list of columns.
44*6dbdd20aSAndroid Build Coastguard Worker    - Function description is any text in the comment above `CREATE PERFETTO FUNCTION` statement.
45*6dbdd20aSAndroid Build Coastguard Worker    - For each argument there has to be a comment line immediately above argument definition.
46*6dbdd20aSAndroid Build Coastguard Worker    - For each column there has to be a comment line immediately above column definition.
47*6dbdd20aSAndroid Build Coastguard Worker
48*6dbdd20aSAndroid Build Coastguard WorkerNOTE: Break lines outside of import description will be ignored.
49*6dbdd20aSAndroid Build Coastguard Worker
50*6dbdd20aSAndroid Build Coastguard WorkerExample of properly formatted view in module `android`:
51*6dbdd20aSAndroid Build Coastguard Worker```sql
52*6dbdd20aSAndroid Build Coastguard Worker-- Count Binder transactions per process.
53*6dbdd20aSAndroid Build Coastguard WorkerCREATE PERFETTO VIEW android_binder_metrics_by_process(
54*6dbdd20aSAndroid Build Coastguard Worker  -- Name of the process that started the binder transaction.
55*6dbdd20aSAndroid Build Coastguard Worker  process_name STRING,
56*6dbdd20aSAndroid Build Coastguard Worker  -- PID of the process that started the binder transaction.
57*6dbdd20aSAndroid Build Coastguard Worker  pid INT,
58*6dbdd20aSAndroid Build Coastguard Worker-- Name of the slice with binder transaction.
59*6dbdd20aSAndroid Build Coastguard Worker  slice_name STRING,
60*6dbdd20aSAndroid Build Coastguard Worker-- Number of binder transactions in process in slice.
61*6dbdd20aSAndroid Build Coastguard Worker  event_count INT
62*6dbdd20aSAndroid Build Coastguard Worker) AS
63*6dbdd20aSAndroid Build Coastguard WorkerSELECT
64*6dbdd20aSAndroid Build Coastguard Worker  process.name AS process_name,
65*6dbdd20aSAndroid Build Coastguard Worker  process.pid AS pid,
66*6dbdd20aSAndroid Build Coastguard Worker  slice.name AS slice_name,
67*6dbdd20aSAndroid Build Coastguard Worker  COUNT(*) AS event_count
68*6dbdd20aSAndroid Build Coastguard WorkerFROM slice
69*6dbdd20aSAndroid Build Coastguard WorkerJOIN thread_track ON slice.track_id = thread_track.id
70*6dbdd20aSAndroid Build Coastguard WorkerJOIN thread ON thread.utid = thread_track.utid
71*6dbdd20aSAndroid Build Coastguard WorkerJOIN process ON thread.upid = process.upid
72*6dbdd20aSAndroid Build Coastguard WorkerWHERE
73*6dbdd20aSAndroid Build Coastguard Worker  slice.name GLOB 'binder*'
74*6dbdd20aSAndroid Build Coastguard WorkerGROUP BY
75*6dbdd20aSAndroid Build Coastguard Worker  process_name,
76*6dbdd20aSAndroid Build Coastguard Worker  slice_name;
77*6dbdd20aSAndroid Build Coastguard Worker```
78*6dbdd20aSAndroid Build Coastguard Worker
79*6dbdd20aSAndroid Build Coastguard WorkerExample of table function in module `android`:
80*6dbdd20aSAndroid Build Coastguard Worker```sql
81*6dbdd20aSAndroid Build Coastguard Worker-- Given a launch id and GLOB for a slice name, returns columns for matching slices.
82*6dbdd20aSAndroid Build Coastguard WorkerCREATE PERFETTO FUNCTION ANDROID_SLICES_FOR_LAUNCH_AND_SLICE_NAME(
83*6dbdd20aSAndroid Build Coastguard Worker  -- Id of launch.
84*6dbdd20aSAndroid Build Coastguard Worker  launch_id INT,
85*6dbdd20aSAndroid Build Coastguard Worker  -- Name of slice with launch.
86*6dbdd20aSAndroid Build Coastguard Worker  slice_name STRING
87*6dbdd20aSAndroid Build Coastguard Worker)
88*6dbdd20aSAndroid Build Coastguard WorkerRETURNS TABLE(
89*6dbdd20aSAndroid Build Coastguard Worker  -- Name of slice with launch.
90*6dbdd20aSAndroid Build Coastguard Worker  slice_name STRING,
91*6dbdd20aSAndroid Build Coastguard Worker  -- Timestamp of slice start.
92*6dbdd20aSAndroid Build Coastguard Worker  slice_ts INT,
93*6dbdd20aSAndroid Build Coastguard Worker  -- Duration of slice.
94*6dbdd20aSAndroid Build Coastguard Worker  slice_dur INT,
95*6dbdd20aSAndroid Build Coastguard Worker  -- Name of thread with slice.
96*6dbdd20aSAndroid Build Coastguard Worker  thread_name STRING,
97*6dbdd20aSAndroid Build Coastguard Worker  -- Arg set id.
98*6dbdd20aSAndroid Build Coastguard Worker  arg_set_id INT
99*6dbdd20aSAndroid Build Coastguard Worker)
100*6dbdd20aSAndroid Build Coastguard WorkerAS
101*6dbdd20aSAndroid Build Coastguard WorkerSELECT
102*6dbdd20aSAndroid Build Coastguard Worker  slice_name,
103*6dbdd20aSAndroid Build Coastguard Worker  slice_ts,
104*6dbdd20aSAndroid Build Coastguard Worker  slice_dur,
105*6dbdd20aSAndroid Build Coastguard Worker  thread_name,
106*6dbdd20aSAndroid Build Coastguard Worker  arg_set_id
107*6dbdd20aSAndroid Build Coastguard WorkerFROM thread_slices_for_all_launches
108*6dbdd20aSAndroid Build Coastguard WorkerWHERE launch_id = $launch_id AND slice_name GLOB $slice_name;
109*6dbdd20aSAndroid Build Coastguard Worker```
110*6dbdd20aSAndroid Build Coastguard Worker
111*6dbdd20aSAndroid Build Coastguard Worker
112*6dbdd20aSAndroid Build Coastguard Worker## {#new-metric} Add a new trace-based metric
113*6dbdd20aSAndroid Build Coastguard Worker
114*6dbdd20aSAndroid Build Coastguard Worker1. Create the proto file containing the metric in the [protos/perfetto/metrics](/protos/perfetto/metrics) folder. The appropriate` BUILD.gn` file should be updated as well.
115*6dbdd20aSAndroid Build Coastguard Worker2. Import the proto in [protos/perfetto/metrics/metrics.proto](/protos/perfetto/metrics/metrics.proto) and add a field for the new message.
116*6dbdd20aSAndroid Build Coastguard Worker3. Run `tools/gen_all out/YOUR_BUILD_DIRECTORY`. This will update the generated headers containing the descriptors for the proto.
117*6dbdd20aSAndroid Build Coastguard Worker  * *Note: this step has to be performed any time any metric-related proto is modified.*
118*6dbdd20aSAndroid Build Coastguard Worker  * If you don't see anything inside the `out/` directory you might have to
119*6dbdd20aSAndroid Build Coastguard Worker  rerun `tools/setup_all_configs.py`.
120*6dbdd20aSAndroid Build Coastguard Worker4. Add a new SQL file for the metric to [src/trace_processor/metrics](/src/trace_processor/metrics). The appropriate `BUILD.gn` file should be updated as well.
121*6dbdd20aSAndroid Build Coastguard Worker  * To learn how to write new metrics, see the [trace-based metrics documentation](/docs/analysis/metrics.md).
122*6dbdd20aSAndroid Build Coastguard Worker5. Build all targets in your out directory with `tools/ninja -C out/YOUR_BUILD_DIRECTORY`.
123*6dbdd20aSAndroid Build Coastguard Worker6. Add a new diff test for the metric. This can be done by adding files to
124*6dbdd20aSAndroid Build Coastguard Workerthe `tests.*.py` files in a proper [test/trace_processor](/test/trace_processor) subfolder.
125*6dbdd20aSAndroid Build Coastguard Worker1. Run the newly added test with `tools/diff_test_trace_processor.py <path to trace processor binary>`.
126*6dbdd20aSAndroid Build Coastguard Worker2. Upload and land your change as normal.
127*6dbdd20aSAndroid Build Coastguard Worker
128*6dbdd20aSAndroid Build Coastguard WorkerHere is an [example change](https://android-review.googlesource.com/c/platform/external/perfetto/+/1290643) which added the `time_in_state` metric.
129*6dbdd20aSAndroid Build Coastguard Worker
130*6dbdd20aSAndroid Build Coastguard Worker## Add a new trace processor table
131*6dbdd20aSAndroid Build Coastguard Worker
132*6dbdd20aSAndroid Build Coastguard Worker1. Create the new table in the appropriate header file in [src/trace_processor/tables](/src/trace_processor/tables) by copying one of the existing macro definitions.
133*6dbdd20aSAndroid Build Coastguard Worker  * Make sure to understand whether a root or derived table is needed and copy the appropriate one. For more information see the [trace processor](/docs/analysis/trace-processor.md) documentation.
134*6dbdd20aSAndroid Build Coastguard Worker2. Register the table with the trace processor in the constructor for the [TraceProcessorImpl class](/src/trace_processor/trace_processor_impl.cc).
135*6dbdd20aSAndroid Build Coastguard Worker3. If also implementing ingestion of events into the table:
136*6dbdd20aSAndroid Build Coastguard Worker  1. Modify the appropriate parser class in [src/trace_processor/importers](/src/trace_processor/importers) and add the code to add rows to the newly added table.
137*6dbdd20aSAndroid Build Coastguard Worker  2. Add a new diff test for the added parsing code and table.
138*6dbdd20aSAndroid Build Coastguard Worker  3. Run the newly added test with `tools/diff_test_trace_processor.py <path to trace processor shell binary>`.
139*6dbdd20aSAndroid Build Coastguard Worker4. Upload and land your change as normal.
140*6dbdd20aSAndroid Build Coastguard Worker
141*6dbdd20aSAndroid Build Coastguard Worker
142*6dbdd20aSAndroid Build Coastguard Worker## Update `TRACE_PROCESSOR_CURRENT_API_VERSION`
143*6dbdd20aSAndroid Build Coastguard Worker
144*6dbdd20aSAndroid Build Coastguard WorkerGenerally you do not have to worry about version skew between the UI
145*6dbdd20aSAndroid Build Coastguard Workerand the `trace_processor` since they are built together at the same
146*6dbdd20aSAndroid Build Coastguard Workercommit. However version skew can occur when using the `--httpd` mode
147*6dbdd20aSAndroid Build Coastguard Workerwhich allows a native `trace_processor` instance to be used with the UI.
148*6dbdd20aSAndroid Build Coastguard Worker
149*6dbdd20aSAndroid Build Coastguard WorkerA common case is when the UI is more recent than `trace_processor`
150*6dbdd20aSAndroid Build Coastguard Workerand depends on a new table definition. With older versions of
151*6dbdd20aSAndroid Build Coastguard Worker`trace_processor` in `--httpd` mode the UI crashes attempting to query
152*6dbdd20aSAndroid Build Coastguard Workera non-existant table. To avoid this we use a version number. If the
153*6dbdd20aSAndroid Build Coastguard Workerversion number `trace_processor` reports is older than the one the UI
154*6dbdd20aSAndroid Build Coastguard Workerwas built with we prompt the user to update.
155*6dbdd20aSAndroid Build Coastguard Worker
156*6dbdd20aSAndroid Build Coastguard Worker1. Go to `protos/perfetto/trace_processor/trace_processor.proto`
157*6dbdd20aSAndroid Build Coastguard Worker2. Increment `TRACE_PROCESSOR_CURRENT_API_VERSION`
158*6dbdd20aSAndroid Build Coastguard Worker3. Add a comment explaining what has changed.
159*6dbdd20aSAndroid Build Coastguard Worker
160*6dbdd20aSAndroid Build Coastguard Worker## Update statsd descriptor
161*6dbdd20aSAndroid Build Coastguard Worker
162*6dbdd20aSAndroid Build Coastguard WorkerPerfetto has limited support for statsd atoms it does not know about.
163*6dbdd20aSAndroid Build Coastguard Worker
164*6dbdd20aSAndroid Build Coastguard Worker* Must be referred to using `raw_atom_id` in the config.
165*6dbdd20aSAndroid Build Coastguard Worker* Show up as `atom_xxx.field_yyy` in trace processor.
166*6dbdd20aSAndroid Build Coastguard Worker* Only top level messages are parsed.
167*6dbdd20aSAndroid Build Coastguard Worker
168*6dbdd20aSAndroid Build Coastguard WorkerTo update Perfetto's descriptor and handle new atoms from AOSP without these
169*6dbdd20aSAndroid Build Coastguard Workerlimitations:
170*6dbdd20aSAndroid Build Coastguard Worker
171*6dbdd20aSAndroid Build Coastguard Worker1. Run `tools/update-statsd-descriptor`.
172*6dbdd20aSAndroid Build Coastguard Worker2. Upload and land your change as normal.
173