You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Perfetto Project Development Guidelines for AI Agents
2
2
3
-
This document provides essential instructions and best practices for developing
4
-
in the Perfetto codebase. Adhere to these guidelines to ensure consistency and
5
-
quality.
3
+
This document provides essential instructions and best practices for developing in the Perfetto codebase. Adhere to these guidelines to ensure consistency and quality.
6
4
7
-
## 1. Building the Project
5
+
## Overview
6
+
7
+
The perfetto repo contains several projects. These are the major ones
8
+
9
+
1. Tracing services: a set of C++ projects that ship on the target device being traced. They live in src/{traced, traced_probes, tracing}. Target names: perfetto, traced, traced_probes, traced_perf, heapprofd.
10
+
11
+
2. Tracing SDK: this lives in src/tracing. They are used by apps that want to use Perfetto tro emit trace events. There are two flavours of this SDK.
12
+
- The (older) C++ SDK: reachable via include/perfetto/tracing/.
13
+
- The (newer) C SDK: reachable via include/perfetto/public.
14
+
15
+
3. TraceProcessor: a C++ project that lives in src/trace_processor/. This code is typically NOT shipped on device and is used by offline tools. It is internally based on sqlite and it extends its query engine via the vtable API. The UI uses this building in Wasm (Web Assembly).
16
+
17
+
4. Perfetto UI: This is a Single Page Web Application, client-only (no server component) written in TypeScript that powers ui.perfetto.dev. It lives in ui/. It embeds TraceProcessor via Wasm.
18
+
19
+
5. A bunch of other tools and utilities used rarely, in tools/ and src/tools.
20
+
21
+
## Core Software Engineering Principles
22
+
23
+
Follow these principles when writing and modifying code.
24
+
25
+
- Avoid code duplication: Before writing a new function, search the codebase for existing functions that provide similar functionality.
26
+
- Reuse and refactor: If a suitable function exists, reuse it. If it's close but not an exact match, consider refactoring the existing function to accommodate the new use case instead of creating a copy.
27
+
- Consult if unsure: If you are considering duplicating a function or a significant block of code, consult with the user first.
28
+
29
+
## C++ projects overview
30
+
31
+
GN supports different configurations, one per out/* folder. You can inspect them by looking at out/xxx/args.gn. Typically when building/running tests while developing, we target our local machine (linux or mac) and we do NOT use android targets.
8
32
9
33
Use the following commands to build the project for different configurations.
10
34
All commands should be run from the root of the repository.
11
35
12
-
The output folder where code is built lives in out/xxx. Different people use
13
-
different output folders. Pick the output folder as follows
36
+
The output folder where code is built lives in out/xxx. Different people use different output folders.
37
+
Pick the output folder as follows
38
+
14
39
- The $OUT env var should contain the path of the output folder. If it exists respect that.
15
-
- If $OUT is empty/unset, set it by looking at the most recent out/ subdir, i.e.
16
-
`OUT=out/$(ls -t1 out | head -n1)`
40
+
- If $OUT is empty/unset, set it by looking at the most recent out/ subdir, i.e. `export OUT=out/$(ls -t1 out | head -n1)`
17
41
18
42
### Building C++ code
19
43
20
-
Our C++ sources use tools/gn and tools/ninja to build.
44
+
Our primary build system is "gn" (GenerateNinja) + "ninja".
45
+
These tools are checked in and accessible via wrapper scripts in our repo, tools/gn and tools/ninja.
21
46
22
47
- If you touch a .gn or .gni file, rerun `tools/gn gen --check $OUT`
23
48
- Afterwards, you can build code running `tools/ninja -C $OUT TARGET_NAME"
All the C++ projects share the same "base" target (include/perfetto/base, include/ext/perfetto/base) and can share some other targets (See GN).
68
+
69
+
### C++ Code style
70
+
71
+
We mainly adhere to the Google C++ style guide, which you can consult here
72
+
https://google.github.io/styleguide/cppguide.html
73
+
74
+
Highlights:
75
+
76
+
- Use C++17.
77
+
- Don't use exceptions, don't bother with try/catch.
78
+
- Try to keep template usage to a minimum.
79
+
- Prefer forward declarations in header files, and #include the needed deps in the .cc files.
80
+
- We fail fast. If something shouldn't happen add a PERFETTO_DCHECK() (debug only) or a PERFETTO_CHECK() (production).
81
+
- Remember to never put code with side effects inside a PERFETTO_DCHECK, as those become no-ops in release builds.
82
+
- If a function argument is out or inout, pass it by pointer, not by reference.
83
+
- Delete copy and move constructors unless they are really needed.
84
+
- If you need a copy/move constructor use the same pattern seen in include/perfetto/ext/base/circular_queue.h
85
+
- Use PERFETTO_DLOG (debug only), PERFETTO_LOG/ILOG/ELOG() for logging.
86
+
- Variable names should be proportional to the scope and distance. Variables used in a small loop can be called i,j; Variables scoped to a function should be shorter; Variables/function used across different files should be longer (within reasonable limits) and less prone to collisions when code-searching.
87
+
- Avoid libraries other than STL, posix/Unix common headers, and other libraries that we already pull in buildtools/. If you think you need a new library, ask the user.
88
+
- Generally, be consistent with the style of the file.
89
+
90
+
When possible try to use data structures and constructs available in include/perfetto/base/ and include/perfetto/ext/base/. Generally look for precedent in the codebase. If in doubt, ask the .
91
+
92
+
Frequently used includes you should look into are, under include/:
93
+
94
+
- perfetto/base/task_runner.h"
95
+
- perfetto/base/compiler.h"
96
+
- perfetto/base/time.h"
97
+
- perfetto/ext/base/status_or.h"
98
+
- perfetto/ext/base/scoped_file.h"
99
+
- perfetto/ext/base/file_utils.h"
100
+
- perfetto/ext/base/flat_hash_map.h"
101
+
- perfetto/ext/base/utils.h"
102
+
- perfetto/ext/base/string_view.h"
103
+
- perfetto/ext/base/string_utils.h"
104
+
- perfetto/base/status.h"
105
+
- perfetto/base/logging.h"
106
+
107
+
When creating new files, this is where you put headers:
108
+
109
+
- .cc files always go under src/, with the exception of some test/ code.
110
+
- If possible, keep .h headers private and keep them alongside the .cc file.
111
+
- If needed you can put headers in include/perfetto/ext, as that is a non-public API surface.
112
+
- In rare cases, if the user says so, you can put new headers in include/perfetto/public, but that's only for C-SDK cases.
113
+
- Note that "include/" is in the include path, so you never need to type #include "include/perfetto/foo" but only #include "perfetto/foo".
114
+
115
+
### Supporting different OSes in C++
116
+
117
+
We generally support all the major platforms (Linux, Android, MacOS/iOS, Windows) in our codebase, with the exception of src/traced which is supported only on Linux/Android (and few parts on MacOS).
118
+
119
+
If you need to split code for different platforms, you must use perfetto/base/build_config.h, and specifically the macros therein defined as `#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)` and so on.
120
+
121
+
Note that every PERFETTO_BUILDFLAG_DEFINE_XXX that you see there must be used via the PERFETTO_BUILDFLAG(XXX) wrapper.
122
+
For example PERFETTO_BUILDFLAG(PERFETTO_OS_QNX) when you see PERFETTO_BUILDFLAG_DEFINE_PERFETTO_OS_QNX.
123
+
124
+
### Running C++ Tests
44
125
45
126
Perfetto uses the Google Test framework. You will see c++ sources like
46
127
```cpp
@@ -69,55 +150,33 @@ For perfetto_benchmarks you need instead to run
Note that unlike Google Test, where the filter is a glob, in Google Benchmarks the filter is a regex.
153
+
Note that unlike Google Test, where the filter is a glob, in Google Benchmarks
154
+
the filter is a regex.
73
155
74
156
### Trace Processor Diff Tests
75
157
76
-
Trace Processor Diff Tests (or diff tests for short) are executed by running the
77
-
following command:
158
+
Trace Processor Diff Tests (or diff tests for short) are executed by running the following command:
78
159
79
160
```sh
80
161
tools/diff_test_trace_processor.py $OUT$/trace_processor_shell --keep-input --quiet --name-filter="<regex of test names>"
81
162
```
82
163
83
-
**Note:** These tests can also be run with ASan or MSan builds by changing the
84
-
path from `out/linux_clang_release/` to `out/linux_asan/` or `out/linux_msan/`
85
-
respectively. **Note:** The `--name-filter` argument is optional. **Note:** When
86
-
using the `--name-filter` flag, do not include `test_` in the filter. The test
87
-
runner automatically drops this prefix. For example, to run `test_my_cool_test`,
88
-
use the filter `MyTestSuite.my_cool_test`.
89
-
164
+
**Note:** These tests can also be run with ASan or MSan builds by changing the path from `out/linux_clang_release/` to `out/linux_asan/` or `out/linux_msan/` respectively.
90
165
91
-
### Test Guidelines
92
-
93
-
- **Prefer test suites over individual tests.** When using the `--gtest_filter`
94
-
flag, specify a whole test suite (e.g., `"MyTestSuite.*"`) instead of a single
95
-
test case (e.g., `"MyTestSuite.MySpecificTest"`). This ensures broader test
96
-
coverage.
97
-
- **Do not test unstable IDs.** When writing diff tests, do not include columns
98
-
that contain unstable IDs (e.g. `upid`, `utid`, `id`, etc) in the output. These
99
-
IDs can change between different runs of the same test, which will cause the
100
-
test to fail.
101
-
- **Remove `test_` prefix for diff tests.** When using the `--name-filter` flag
102
-
for diff tests, do not include `test_` in the filter. The test
103
-
runner automatically drops this prefix. For example, to run `test_my_cool_test`,
104
-
use the filter `MyTestSuite.my_cool_test`.
105
-
106
-
## 3. Core Software Engineering Principles
166
+
**Note:** The `--name-filter` argument is optional.
107
167
108
-
Follow these principles when writing and modifying code.
168
+
**Note:** When using the `--name-filter` flag, do not include `test_` in the filter. The test
169
+
runner automatically drops this prefix.
170
+
For example, to run `test_my_cool_test`, use the filter `MyTestSuite.my_cool_test`.
109
171
110
-
### Principle 1: Don't Repeat Yourself (DRY)
172
+
### Test Guidelines
111
173
112
-
- **Avoid code duplication.** Before writing a new function, search the codebase
113
-
for existing functions that provide similar functionality.
114
-
- **Reuse and refactor.** If a suitable function exists, reuse it. If it's close
115
-
but not an exact match, consider refactoring the existing function to
116
-
accommodate the new use case instead of creating a copy.
117
-
- **Consult if unsure.** If you are considering duplicating a function or a
118
-
significant block of code, consult with the user first.
174
+
- **Prefer test suites over individual tests.** When using the `--gtest_filter` flag, specify a whole test suite (e.g., `"MyTestSuite.*"`) instead of a single test case (e.g., `"MyTestSuite.MySpecificTest"`). This ensures broader test coverage.
175
+
- **Do not test unstable IDs.** When writing diff tests, do not include columns that contain unstable IDs (e.g. `upid`, `utid`, `id`, etc) in the output. These IDs can change between different runs of the same test, which will cause the test to fail.
176
+
177
+
- **Remove `test_` prefix for diff tests.** When using the `--name-filter` flag for diff tests, do not include `test_` in the filter. The test runner automatically drops this prefix. For example, to run `test_my_cool_test`, use the filter `MyTestSuite.my_cool_test`.
119
178
120
-
## 4. Getting Diffs
179
+
## Getting Diffs
121
180
122
181
When asked to "get a diff" or "read the current diff", run the following
Use the command `git new-branch dev/lalitm/<name-of-branch>` to create a new branch for your pull request.
241
+
Use the command `git new-branch dev/$USER$/<name-of-branch>` to create a new branch for your pull request.
183
242
184
243
2. **Create a stacked/dependent pull request:**
185
244
To create a pull request that depends on another, use the command `git new-branch --parent <name-of-parent-branch> dev/lalitm/<name-of-branch>`.
186
245
187
-
**Note:** The `git new-branch` command only creates and switches to a new
188
-
branch. The normal `git add` and `git commit` workflow should be used to add
189
-
changes to the branch.
246
+
**Note:** The `git new-branch` command only creates and switches to a new branch. The normal `git add` and `git commit` workflow should be used to add changes to the branch.
190
247
191
-
## 8. Commit Messages
248
+
## Commit Messages
192
249
193
250
When writing commit messages, follow these guidelines:
194
251
195
-
- **Prefix your commits.** Prefix changes to Trace Processor code with `tp:`,
196
-
UI code with `ui:`, and general Perfetto changes with `perfetto:`.
197
-
- **Keep it concise.** A short one-line summary followed by a paragraph
198
-
describing the change is the best commit message.
252
+
- Prefix your commits: Prefix changes to Trace Processor code with `tp:`, UI code with `ui:`, and general Perfetto changes with `perfetto:`.
253
+
- Keep it concise: A short one-line summary followed by a \n then a paragraph describing the change is the best commit message. Wrap the commit message at 72 cols.
0 commit comments