This document provides essential instructions and best practices for developing in the Perfetto codebase. Adhere to these guidelines to ensure consistency and quality.
The perfetto repo contains several projects. These are the major ones
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.
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.
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).
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.
A bunch of other tools and utilities used rarely, in tools/ and src/tools.
Follow these principles when writing and modifying code.
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.
Use the following commands to build the project for different configurations. All commands should be run from the root of the repository.
The output folder where code is built lives in out/xxx. Different people use different output folders. Pick the output folder as follows
export OUT=out/$(ls -t1 out | head -n1)Our primary build system is “gn” (GenerateNinja) + “ninja”. These tools are checked in and accessible via wrapper scripts in our repo, tools/gn and tools/ninja.
tools/gn gen --check $OUTWhen adding/removing source files, keep BUILD.gn files updated. Usually there is a BUILD.gn file in each directory. If not, look at closer parent dirs for precedent.
Never bother manually updating Android.bp files or bazel BUILD files. Those are autogenerated later when uploading the pull request via tools/gen_all $OUT, but humans will take care of that.
To build one or more targets:
tools/ninja -C $OUT -k 10000 trace_processor_shell perfetto_unittests
All the C++ projects share the same “base” target (include/perfetto/base, include/ext/perfetto/base) and can share some other targets (See GN).
We mainly adhere to the Google C++ style guide, which you can consult here https://google.github.io/styleguide/cppguide.html
Highlights:
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 .
Frequently used includes you should look into are, under include/:
When creating new files, this is where you put headers:
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).
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.
Note that every PERFETTO_BUILDFLAG_DEFINE_XXX that you see there must be used via the PERFETTO_BUILDFLAG(XXX) wrapper. For example PERFETTO_BUILDFLAG(PERFETTO_OS_QNX) when you see PERFETTO_BUILDFLAG_DEFINE_PERFETTO_OS_QNX.
Perfetto uses the Google Test framework. You will see c++ sources like
TEST(ProtozeroToJsonTest, Foo) { ... }
ProtozeroToJsonTest is the test suite name, Foo the test name.
You can run all the tests in a test suite by doing:
$OUT/perfetto_unittests --gtest_brief=1 --gtest_filter="ProtozeroToJsonTest.*"
Or if you touch a specific test, you can run only that one doing
$OUT/perfetto_unittests --gtest_brief=1 --gtest_filter="ProtozeroToJsonTest.Foo"
Same goes for perfetto_integrationtests.
For perfetto_benchmarks you need instead to run
$OUT/perfetto_benchmarks --benchmark_filter='.*BM_RtMutex_NoContention.*'
Note that unlike Google Test, where the filter is a glob, in Google Benchmarks the filter is a regex.
Trace Processor Diff Tests (or diff tests for short) are executed by running the following command:
tools/diff_test_trace_processor.py $OUT$/trace_processor_shell --keep-input --quiet --name-filter="<regex of test names>"
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.
Note: The --name-filter argument is optional.
Note: When using the --name-filter flag, 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.
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.
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.
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.
When asked to “get a diff” or “read the current diff”, run the following command:
git diff $(git config branch.$(git rev-parse --abbrev-ref HEAD).parent)
When asked to fix GN dependencies, run the following command and fix any errors that are reported:
tools/gn check $OUT
Note: When fixing include errors, do not add dependencies to public_deps unless explicitly instructed to by the user. Instead, add a direct dependency to the target that requires it.
To build with ASan for memory error detection:
tools/ninja -C out/linux_asan -k 10000 trace_processor_shell perfetto_unittests
To build with MSan for uninitialized read detection:
tools/ninja -C out/linux_msan -k 10000 trace_processor_shell perfetto_unittests
Note: Ensure the ASAN_SYMBOLIZER_PATH is set correctly.
ASAN_SYMBOLIZER_PATH="$(pwd)/buildtools/linux64/clang/bin/llvm-symbolizer" \ out/linux_asan/perfetto_unittests --gtest_brief=1 --gtest_filter="<TestSuiteName.*>"
Note: Ensure the MSAN_SYMBOLIZER_PATH is set correctly.
MSAN_SYMBOLIZER_PATH="$(pwd)/buildtools/linux64/clang/bin/llvm-symbolizer" \ out/linux_msan/perfetto_unittests --gtest_brief=1 --gtest_filter="<TestSuiteName.*>" ## Creating Pull Requests When creating a pull request, follow these steps: 1. **Create a new branch:** Use the command `git new-branch dev/$USER$/<name-of-branch>` to create a new branch for your pull request. 2. **Create a stacked/dependent pull request:** 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>`. **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. ## Commit Messages When writing commit messages, follow these guidelines: - Prefix your commits: Prefix changes to Trace Processor code with `tp:`, UI code with `ui:`, and general Perfetto changes with `perfetto:`. - 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.