blob: a345ea1927a6a8a08e6dc9c93d956436d1ad69e6 [file] [log] [blame] [view]
Primiano Tucci1e96f3b2019-06-04 12:08:16 +01001# Perfetto public API surface
2
3**This API surface is not stable yet, don't depend on it**
4
5This folder contains the public perfetto API headers. This allows an app to
Daniele Di Proietto8b46d692022-04-22 15:59:40 +01006inject trace events into perfetto with ~10 lines of code (see examples in
7examples/sdk/).
Primiano Tucci1e96f3b2019-06-04 12:08:16 +01008
9The ext/ subdirectory expose the API-unstable classes and types that are
10exposed to emvbedders that have exceptional requirements in terms of interposing
11their own custom IPC layer. To the day the only case is chromium. Nothing else
Daniele Di Proietto8b46d692022-04-22 15:59:40 +010012should depend on ext/. Contact perfetto-dev@ if you think you need to
Primiano Tucci1e96f3b2019-06-04 12:08:16 +010013depend on an ext/ header.
14
15Headers in this folder must be hermetic. No ext/ perfetto header must be
16leaked from the includes.
17
18What is a client supposed to do to use tracing? See example below in this page.
19
20
21Source code layout: what goes where?
22------------------------------------
23
Daniele Di Proietto8b46d692022-04-22 15:59:40 +010024**include/perfetto:**
Primiano Tucci1e96f3b2019-06-04 12:08:16 +010025Embedders are allowed to access and depend on any folder of this but ext/.
26This contains classes to: (i) use tracing; (ii) extend the tracing internals
27(i.e. implement the Platform).
28
29Rules:
30- This directory should contain only .h files and no .cc files.
31- Corresponding .cc files go into `src/`.
32- .h files in here can depend only on `include/perfetto/` but not on
33 `include/perfetto/ext/`,
34
35**include/perfetto/tracing/internal:**
36This directory contains headers that are required to implement the public-facing
37tracing API efficiently but that are not part of the API surface.
38In an ideal world there would be no need of these headers and everything would
39be handle via forward-declarations and PIMPL patterns. Unfortunately, however,
40PIMPL cannot be used for inline functions, where the implementation needs to be
41exposed in the public headers, which in turn need to depend on the memory layout
42of structs/classes.
43
44Rules:
45- All classes / types declared in this folder must be wrapped in the
46 ::perfetto::internal namespace.
47- Both public and internal .h headers must not pull other perfetto headers
48 from ext/.
49- .cc files instead can depend on other perfetto classes, as well as .h headers
50 located in src/.
51- Embedders must not depend on the perfetto::internal namespace.
52- Internal types cannot be used as input, output or return arguments of public
53 API functions.
54- Internal types cannot be directly exposed to virtual methods that are
55 intended to be called or overridden by the embedder (e.g. TracingBackend's
56 methods). For those the solution is to create a matching non-internal base
57 class with a static factory method.
58- We don't guarantee binary compatibility between versions (i.e. this client
59 library can only be statically linked) but we guarantee source-level
60 compatibility and ABI of the UNIX socket and shared memory buffers.
61
Daniele Di Proietto8b46d692022-04-22 15:59:40 +010062**include/perfetto/public:**
63This contains headers that can be used when dynamic linking with the perfetto
64shared library.
65
66These headers are not supposed to be exposed as part of the shared library ABI.
67
Daniele Di Proietto92a03232022-04-22 15:59:40 +010068All symbols, macros and types here start with the `perfetto_`, `PERFETTO_` or
69`Perfetto` prefix. These prefixes are reserved and should not be used elsewhere
Daniele Di Proietto8b46d692022-04-22 15:59:40 +010070when linking with the perfetto shared library.
71
72**include/perfetto/public/abi:**
73Subset of headers that are part of the shared library ABI (**not stable yet**).
74These headers are supposed to be hermetic (i.e. they should not include anything
75outside the abi directory).
Primiano Tucci1e96f3b2019-06-04 12:08:16 +010076
77Usage example
78-------------
791. Call `perfetto::Tracing::Initialize(...)` once, when starting the app.
80 While doing so the app can chose the tracing model:
81 - Fully in-process: the service runs in a thread within the same process.
82 - System: connects to the traced system daemon via a UNIX socket. This allows
83 the app to join system-wide tracing sessions. This is available only on
84 Linux/Android/MacOS for now.
85 - Private dedicated process: similar to the in-process case, but the service
86 runs in a dedicated process rather than a thread. This is for performance,
87 stability and security isolation. Also, this is not implemented yet.
88 - Custom backend: this is for peculiar cases (mainly chromium) where the
89 embedder is multi-process but wants to use a different IPC mechanism. The
90 embedder needs to deal with the larger and clunkier set of perfetto APIs.
91 Reach out to the team before using this mode. It's very unlikely you need
92 this unless you are a project rolled into chromium.
93
942. Define and register one or more data sources, like this:
95```cpp
96 #include "perfetto/tracing.h"
97
98 class MyDataSource : public perfetto::DataSource<MyDataSource> {
Florian Mayer10b35482019-07-22 18:43:27 +010099 void OnSetup(const SetupArgs&) override {}
100 void OnStart(const StartArgs&) override {}
101 void OnStop(const StopArgs&) override {}
Primiano Tucci1e96f3b2019-06-04 12:08:16 +0100102 };
103 ...
Primiano Tucci1e96f3b2019-06-04 12:08:16 +0100104 perfetto::DataSourceDescriptor dsd;
105 dsd.set_name("my_data_source");
106 MyDataSource::Register(dsd);
107```
108
1093. Optionally define a new proto schema in `trace_packet.proto`
110
1114. Emit trace events
112```cpp
Florian Mayerf337c7b2019-07-23 13:05:42 +0100113 MyDataSource::Trace([](MyDataSource::TraceContext ctx) {
Primiano Tucci1e96f3b2019-06-04 12:08:16 +0100114 auto trace_packet = ctx.NewTracePacket();
Florian Mayerf337c7b2019-07-23 13:05:42 +0100115 trace_packet->set_timestamp(...);
116 auto* my_custom_proto = trace_packet->set_my_custom_proto();
Primiano Tucci1e96f3b2019-06-04 12:08:16 +0100117 });
118```
119
Primiano Tucci66b08c32020-07-23 12:37:48 +0200120The passed lambda will be called only if tracing is enabled and the data source
Primiano Tucci1e96f3b2019-06-04 12:08:16 +0100121was enabled in the trace config. It might be called multiple times, one for each
122active tracing session, in case of concurrent tracing sessions (or even within a
123single tracing session, if the data source is listed twice in the trace config).