xref: /aosp_15_r20/external/pigweed/pw_trace_tokenized/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_trace_tokenized:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker==================
4*61c4878aSAndroid Build Coastguard Workerpw_trace_tokenized
5*61c4878aSAndroid Build Coastguard Worker==================
6*61c4878aSAndroid Build Coastguard WorkerPigweed's tracing module provides facilities for applications to trace
7*61c4878aSAndroid Build Coastguard Workerinformation about the execution of their application. The module is split into
8*61c4878aSAndroid Build Coastguard Workertwo components:
9*61c4878aSAndroid Build Coastguard Worker
10*61c4878aSAndroid Build Coastguard Worker1. The facade, provided elsewhere, which is only a macro interface layer
11*61c4878aSAndroid Build Coastguard Worker2. The backend (this module), is one implemention of the low level tracing.
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard Worker------
14*61c4878aSAndroid Build Coastguard WorkerStatus
15*61c4878aSAndroid Build Coastguard Worker------
16*61c4878aSAndroid Build Coastguard WorkerThis module is currently in development, and is therefore still undergoing
17*61c4878aSAndroid Build Coastguard Workersignificant changes.
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard WorkerFuture work will:
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker1. Add a more complete API for how to retrieve data from ring_buffer.
22*61c4878aSAndroid Build Coastguard Worker2. Add a Python library to decode the trace data.
23*61c4878aSAndroid Build Coastguard Worker3. Add examples with sample output (especially for filtering and triggering).
24*61c4878aSAndroid Build Coastguard Worker4. Add tools to retrieve trace data.
25*61c4878aSAndroid Build Coastguard Worker5. Add more sinks, such as RTT.
26*61c4878aSAndroid Build Coastguard Worker6. Add support to more platforms.
27*61c4878aSAndroid Build Coastguard Worker7. Improve the locking behaviour and provide default trace locking
28*61c4878aSAndroid Build Coastguard Worker   implementions.
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Worker--------
31*61c4878aSAndroid Build Coastguard WorkerOverview
32*61c4878aSAndroid Build Coastguard Worker--------
33*61c4878aSAndroid Build Coastguard WorkerThe tokenized trace backend aims to be a reasonable tradeoff of trace features
34*61c4878aSAndroid Build Coastguard Workerand event size for most applications. It works by encoding all compile time data
35*61c4878aSAndroid Build Coastguard Workerfor a trace event into a tokenized number. This provides a good amount of
36*61c4878aSAndroid Build Coastguard Workercompression, while maintaining the full trace feature set.
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard WorkerIn addition the tokenized trace backend adds flexibility through callbacks,
39*61c4878aSAndroid Build Coastguard Workerwhich allows the application to do things such as filtering trace_events and
40*61c4878aSAndroid Build Coastguard Workertriggering tracing to turn on and off. This flexibility can help maximize the
41*61c4878aSAndroid Build Coastguard Workereffectiveness of a limited trace buffer as well as be a valuable tool while
42*61c4878aSAndroid Build Coastguard Workerdebugging.
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard WorkerCompatibility
46*61c4878aSAndroid Build Coastguard Worker-------------
47*61c4878aSAndroid Build Coastguard WorkerMost of this module is compatible with C and C++, the only exception to this is
48*61c4878aSAndroid Build Coastguard Workerthe ``RegisterCallbackWhenCreated`` helper class.
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard WorkerDependencies
51*61c4878aSAndroid Build Coastguard Worker------------
52*61c4878aSAndroid Build Coastguard Worker``pw_assert``
53*61c4878aSAndroid Build Coastguard Worker``pw_log``
54*61c4878aSAndroid Build Coastguard Worker``pw_preprocessor``
55*61c4878aSAndroid Build Coastguard Worker``pw_status``
56*61c4878aSAndroid Build Coastguard Worker``pw_tokenizer``
57*61c4878aSAndroid Build Coastguard Worker``pw_trace:facade``
58*61c4878aSAndroid Build Coastguard Worker``pw_varint``
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker---------
61*61c4878aSAndroid Build Coastguard WorkerMacro API
62*61c4878aSAndroid Build Coastguard Worker---------
63*61c4878aSAndroid Build Coastguard WorkerAll code should use the trace API facade directly. This backend fully
64*61c4878aSAndroid Build Coastguard Workerimplements all features of the tracing facade.
65*61c4878aSAndroid Build Coastguard Worker
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard WorkerEvent Callbacks & Data Sinks
68*61c4878aSAndroid Build Coastguard Worker----------------------------
69*61c4878aSAndroid Build Coastguard WorkerThe tokenized trace module adds both event callbacks and data sinks which
70*61c4878aSAndroid Build Coastguard Workerprovide hooks into tracing.
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard WorkerThe *event callbacks* are called when trace events occur, with the trace event
73*61c4878aSAndroid Build Coastguard Workerdata, before the event is encoded or sent to the sinks. The callbacks may
74*61c4878aSAndroid Build Coastguard Workermodify the run-time fields of the trace event, i.e. ``trace_id``,
75*61c4878aSAndroid Build Coastguard Worker``data_buffer`` and ``data_size``. Using the return flags, these callbacks can
76*61c4878aSAndroid Build Coastguard Workerbe used to adjust the trace behaviour at runtime in response to specific events.
77*61c4878aSAndroid Build Coastguard Worker
78*61c4878aSAndroid Build Coastguard WorkerIf requested (using ``called_on_every_event``) the callback will be called on
79*61c4878aSAndroid Build Coastguard Workerevery trace event regardless if tracing is currently enabled or not. Using this,
80*61c4878aSAndroid Build Coastguard Workerthe application can trigger tracing on or off when specific traces or patterns
81*61c4878aSAndroid Build Coastguard Workerof traces are observed, or can selectively filter traces to preserve the trace
82*61c4878aSAndroid Build Coastguard Workerbuffer.
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard WorkerThe event callback is called in the context of the traced task. It must be
85*61c4878aSAndroid Build Coastguard WorkerISR-safe to support tracing within ISRs. It must be lightweight to prevent
86*61c4878aSAndroid Build Coastguard Workerperformance issues in the trace tasks.
87*61c4878aSAndroid Build Coastguard Worker
88*61c4878aSAndroid Build Coastguard WorkerThe return flags ``pw_trace_TraceEventReturnFlags`` support the following
89*61c4878aSAndroid Build Coastguard Workerbehaviors:
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Worker* ``PW_TRACE_EVENT_RETURN_FLAGS_SKIP_EVENT`` can be set true to skip this
92*61c4878aSAndroid Build Coastguard Worker  sample.
93*61c4878aSAndroid Build Coastguard Worker* ``PW_TRACE_EVENT_RETURN_FLAGS_DISABLE_AFTER_PROCESSING`` can be set true to
94*61c4878aSAndroid Build Coastguard Worker  disable tracing after this sample.
95*61c4878aSAndroid Build Coastguard Worker
96*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: pw_trace_TraceEventReturnFlags pw_trace_EventCallback( \
97*61c4878aSAndroid Build Coastguard Worker    void* user_data,  \
98*61c4878aSAndroid Build Coastguard Worker    pw_trace_tokenized_TraceEvent* event)
99*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: pw_Status pw_trace_RegisterEventCallback( \
100*61c4878aSAndroid Build Coastguard Worker    pw_trace_EventCallback callback, \
101*61c4878aSAndroid Build Coastguard Worker    pw_trace_EventCallbackFlags flags, \
102*61c4878aSAndroid Build Coastguard Worker    void* user_data, \
103*61c4878aSAndroid Build Coastguard Worker    pw_trace_EventCallbackHandle* handle)
104*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: pw_Status pw_trace_UnregisterEventCallback( \
105*61c4878aSAndroid Build Coastguard Worker    pw_trace_EventCallbackHandle handle)
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard Worker
108*61c4878aSAndroid Build Coastguard WorkerThe *data sinks* are called only for trace events which get processed (tracing
109*61c4878aSAndroid Build Coastguard Workeris enabled, and the sample not skipped). The sink callback is called with the
110*61c4878aSAndroid Build Coastguard Workerencoded bytes of the trace event, which can be used by the application to
111*61c4878aSAndroid Build Coastguard Workerconnect different data sinks. The callback is broken into three callbacks
112*61c4878aSAndroid Build Coastguard Worker``pw_trace_SinkStartBlock``, ``pw_trace_SinkAddBytes``, and
113*61c4878aSAndroid Build Coastguard Worker``pw_trace_SinkEndBlock``. ``Start`` is called with the size of the block,
114*61c4878aSAndroid Build Coastguard Workerbefore any bytes are emitted and can be used if needed to allocate space.
115*61c4878aSAndroid Build Coastguard Worker``AddBytes`` is then called multiple times with chunks of bytes. Finally ``End``
116*61c4878aSAndroid Build Coastguard Workeris called to allow any cleanup to be done by the sink if neccessary. Not all
117*61c4878aSAndroid Build Coastguard Workercallbacks are required, it is acceptible to provide nullptr for any callbacks
118*61c4878aSAndroid Build Coastguard Workerwhich you don't require.
119*61c4878aSAndroid Build Coastguard Worker
120*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: void pw_trace_SinkStartBlock(void* user_data, size_t size)
121*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: void pw_trace_SinkAddBytes( \
122*61c4878aSAndroid Build Coastguard Worker    void* user_data, \
123*61c4878aSAndroid Build Coastguard Worker    const void* bytes, \
124*61c4878aSAndroid Build Coastguard Worker    size_t size)
125*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: void pw_trace_SinkEndBlock(void* user_data)
126*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: pw_Status pw_trace_RegisterSink( \
127*61c4878aSAndroid Build Coastguard Worker    pw_trace_SinkStartBlock start, \
128*61c4878aSAndroid Build Coastguard Worker    pw_trace_SinkAddBytes add_bytes, \
129*61c4878aSAndroid Build Coastguard Worker    pw_trace_SinkEndBlock end_block, \
130*61c4878aSAndroid Build Coastguard Worker    void* user_data, \
131*61c4878aSAndroid Build Coastguard Worker    pw_trace_SinkHandle* handle)
132*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: pw_Status pw_trace_UnregisterSink(pw_trace_SinkHandle handle)
133*61c4878aSAndroid Build Coastguard Worker
134*61c4878aSAndroid Build Coastguard WorkerTrace Reference
135*61c4878aSAndroid Build Coastguard Worker---------------
136*61c4878aSAndroid Build Coastguard WorkerSome use-cases might involve referencing a specific trace event, for example
137*61c4878aSAndroid Build Coastguard Workerto use it as a trigger or filtering. Since the trace events are tokenized, a
138*61c4878aSAndroid Build Coastguard Workermacro is provided to generate the token to use as a reference. All the fields
139*61c4878aSAndroid Build Coastguard Workermust match exactly to generate the correct trace reference. If the trace does
140*61c4878aSAndroid Build Coastguard Workernot have a group, use ``PW_TRACE_GROUP_LABEL_DEFAULT``.
141*61c4878aSAndroid Build Coastguard Worker
142*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_REF(event_type, module, label, flags, group)
143*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_REF_DATA( \
144*61c4878aSAndroid Build Coastguard Worker   event_type, module, label, flags, group, type)
145*61c4878aSAndroid Build Coastguard Worker
146*61c4878aSAndroid Build Coastguard Worker
147*61c4878aSAndroid Build Coastguard Worker-----------
148*61c4878aSAndroid Build Coastguard WorkerTime source
149*61c4878aSAndroid Build Coastguard Worker-----------
150*61c4878aSAndroid Build Coastguard WorkerTracing requires the platform to provide the time source for tracing, this can
151*61c4878aSAndroid Build Coastguard Workerbe done in one of a few ways.
152*61c4878aSAndroid Build Coastguard Worker
153*61c4878aSAndroid Build Coastguard Worker1. Create a file with the default time functions, and provide as build variable
154*61c4878aSAndroid Build Coastguard Worker   ``pw_trace_tokenized_time``, which will get pulled in as a dependency.
155*61c4878aSAndroid Build Coastguard Worker2. Provide time functions elsewhere in project, and ensure they are included.
156*61c4878aSAndroid Build Coastguard Worker3. Provide definitions of the following trace time functions.
157*61c4878aSAndroid Build Coastguard Worker
158*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_TIME_TYPE pw_trace_GetTraceTime()
159*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: size_t pw_trace_GetTraceTimeTicksPerSecond()
160*61c4878aSAndroid Build Coastguard Worker
161*61c4878aSAndroid Build Coastguard Worker
162*61c4878aSAndroid Build Coastguard Worker------
163*61c4878aSAndroid Build Coastguard WorkerBuffer
164*61c4878aSAndroid Build Coastguard Worker------
165*61c4878aSAndroid Build Coastguard WorkerThe optional trace buffer adds a ring buffer which contains the encoded trace
166*61c4878aSAndroid Build Coastguard Workerdata. This is still a work in progress, in particular better methods for
167*61c4878aSAndroid Build Coastguard Workerretrieving the data still need to be added. Currently there is an accessor for
168*61c4878aSAndroid Build Coastguard Workerthe underlying ring buffer object, but this is a short term solution.
169*61c4878aSAndroid Build Coastguard Worker
170*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: void ClearBuffer()
171*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: pw::ring_buffer::PrefixedEntryRingBuffer* GetBuffer()
172*61c4878aSAndroid Build Coastguard Worker
173*61c4878aSAndroid Build Coastguard WorkerThe buffer has two configurable options:
174*61c4878aSAndroid Build Coastguard Worker
175*61c4878aSAndroid Build Coastguard Worker1. PW_TRACE_BUFFER_SIZE_BYTES: The total size of the ring buffer in bytes.
176*61c4878aSAndroid Build Coastguard Worker2. PW_TRACE_BUFFER_MAX_BLOCK_SIZE_BYTES: The maximum single trace object size.
177*61c4878aSAndroid Build Coastguard Worker   Including the token, time, and any attached data. Any trace object larger
178*61c4878aSAndroid Build Coastguard Worker   then this will be dropped.
179*61c4878aSAndroid Build Coastguard Worker
180*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: ConstByteSpan DeringAndViewRawBuffer()
181*61c4878aSAndroid Build Coastguard Worker
182*61c4878aSAndroid Build Coastguard WorkerThe DeringAndViewRawBuffer function can be used to get bulk access of the full
183*61c4878aSAndroid Build Coastguard Workerderinged prefixed-ring-buffer data. This might be neccessary for large zero-copy
184*61c4878aSAndroid Build Coastguard Workerbulk transfers. It is the caller's responsibility to disable tracing during
185*61c4878aSAndroid Build Coastguard Workeraccess to the buffer. The data in the block is defined by the
186*61c4878aSAndroid Build Coastguard Workerprefixed-ring-buffer format without any user-preamble.
187*61c4878aSAndroid Build Coastguard Worker
188*61c4878aSAndroid Build Coastguard Worker
189*61c4878aSAndroid Build Coastguard WorkerAdded dependencies
190*61c4878aSAndroid Build Coastguard Worker------------------
191*61c4878aSAndroid Build Coastguard Worker``pw_ring_buffer``
192*61c4878aSAndroid Build Coastguard Worker``pw_varint``
193*61c4878aSAndroid Build Coastguard Worker
194*61c4878aSAndroid Build Coastguard Worker
195*61c4878aSAndroid Build Coastguard Worker-------
196*61c4878aSAndroid Build Coastguard WorkerLogging
197*61c4878aSAndroid Build Coastguard Worker-------
198*61c4878aSAndroid Build Coastguard WorkerThe optional trace buffer logging adds support to dump trace buffers to the log.
199*61c4878aSAndroid Build Coastguard WorkerBuffers are converted to base64-encoding then split across log lines. Trace logs
200*61c4878aSAndroid Build Coastguard Workerare surrounded by 'begin' and 'end' tags.
201*61c4878aSAndroid Build Coastguard Worker
202*61c4878aSAndroid Build Coastguard WorkerEx. Invoking PW_TRACE_INSTANT with 'test1' and 'test2', then calling this
203*61c4878aSAndroid Build Coastguard Workerfunction would produce this in the output logs:
204*61c4878aSAndroid Build Coastguard Worker
205*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
206*61c4878aSAndroid Build Coastguard Worker
207*61c4878aSAndroid Build Coastguard Worker   [TRACE] begin
208*61c4878aSAndroid Build Coastguard Worker   [TRACE] data: BWdDMRoABWj52YMB
209*61c4878aSAndroid Build Coastguard Worker   [TRACE] end
210*61c4878aSAndroid Build Coastguard Worker
211*61c4878aSAndroid Build Coastguard WorkerAdded dependencies
212*61c4878aSAndroid Build Coastguard Worker------------------
213*61c4878aSAndroid Build Coastguard Worker``pw_base64``
214*61c4878aSAndroid Build Coastguard Worker``pw_log``
215*61c4878aSAndroid Build Coastguard Worker``pw_ring_buffer``
216*61c4878aSAndroid Build Coastguard Worker``pw_string``
217*61c4878aSAndroid Build Coastguard Worker``pw_tokenizer``
218*61c4878aSAndroid Build Coastguard Worker``pw_varint``
219*61c4878aSAndroid Build Coastguard Worker
220*61c4878aSAndroid Build Coastguard Worker--------------
221*61c4878aSAndroid Build Coastguard WorkerPython decoder
222*61c4878aSAndroid Build Coastguard Worker--------------
223*61c4878aSAndroid Build Coastguard WorkerThe python decoder can be used to convert the binary trace data into json data
224*61c4878aSAndroid Build Coastguard Workerwhich can be viewed in chrome://tracing.
225*61c4878aSAndroid Build Coastguard Worker
226*61c4878aSAndroid Build Coastguard Worker``get_trace.py`` can be used for retrieveing trace data from devices which are
227*61c4878aSAndroid Build Coastguard Workerusing the trace_rpc_server.
228*61c4878aSAndroid Build Coastguard Worker
229*61c4878aSAndroid Build Coastguard Worker``trace_tokenized.py`` can be used to decode a binary file of trace data.
230*61c4878aSAndroid Build Coastguard Worker
231*61c4878aSAndroid Build Coastguard Worker--------
232*61c4878aSAndroid Build Coastguard WorkerExamples
233*61c4878aSAndroid Build Coastguard Worker--------
234*61c4878aSAndroid Build Coastguard WorkerThe examples all use `pw_trace` sample app to provide the trace data. Details
235*61c4878aSAndroid Build Coastguard Workerfor how to build, run, and decode the traces are included at the top of each
236*61c4878aSAndroid Build Coastguard Workerexample. This is early work, and is provided as an example of how different
237*61c4878aSAndroid Build Coastguard Workertracing concepts can look.
238*61c4878aSAndroid Build Coastguard Worker
239*61c4878aSAndroid Build Coastguard WorkerBasic
240*61c4878aSAndroid Build Coastguard Worker-----
241*61c4878aSAndroid Build Coastguard WorkerThe basic example turns on tracing and dumps all trace output to a file provided
242*61c4878aSAndroid Build Coastguard Workeron the command line.
243*61c4878aSAndroid Build Coastguard Worker
244*61c4878aSAndroid Build Coastguard WorkerTrigger
245*61c4878aSAndroid Build Coastguard Worker-------
246*61c4878aSAndroid Build Coastguard WorkerThe trigger example demonstrates how a trace event can be used as a trigger to
247*61c4878aSAndroid Build Coastguard Workerstart and stop capturing a trace. The examples makes use of ``PW_TRACE_REF``
248*61c4878aSAndroid Build Coastguard Workerand ``PW_TRACE_REF_DATA`` to specify a start and stop event for the capture.
249*61c4878aSAndroid Build Coastguard WorkerThis can be useful if the trace buffer is small and you wish to capture a
250*61c4878aSAndroid Build Coastguard Workerspecific series of events.
251*61c4878aSAndroid Build Coastguard Worker
252*61c4878aSAndroid Build Coastguard WorkerFilter
253*61c4878aSAndroid Build Coastguard Worker------
254*61c4878aSAndroid Build Coastguard WorkerThe filter example demonstrates how a callback can be used to filter which trace
255*61c4878aSAndroid Build Coastguard Workerevents get processed and saved. In this example all events from the processing
256*61c4878aSAndroid Build Coastguard Workertask which don't have traceId equal to 3 are removed. Both the other task traces
257*61c4878aSAndroid Build Coastguard Workerare not removed. This can be a useful feature while debugging as it limits the
258*61c4878aSAndroid Build Coastguard Workeramount of events which get stored to the buffer, and only saves the events of
259*61c4878aSAndroid Build Coastguard Workerinterest.
260*61c4878aSAndroid Build Coastguard Worker
261*61c4878aSAndroid Build Coastguard Worker--------------------
262*61c4878aSAndroid Build Coastguard WorkerSnapshot integration
263*61c4878aSAndroid Build Coastguard Worker--------------------
264*61c4878aSAndroid Build Coastguard WorkerTokenized trace buffers can be captured to a ``pw.snapshot.Snapshot`` or
265*61c4878aSAndroid Build Coastguard Worker``pw.trace.SnapshotTraceInfo`` proto in the ``trace_data`` field. The expected
266*61c4878aSAndroid Build Coastguard Workerformat is a de-ringed raw tokenized trace buffer, which can be retrieved via
267*61c4878aSAndroid Build Coastguard Worker``pw::trace::DeringAndViewRawBuffer()``.
268*61c4878aSAndroid Build Coastguard Worker
269*61c4878aSAndroid Build Coastguard Worker``pw_trace_tokenized`` does not yet have Python tooling integration for
270*61c4878aSAndroid Build Coastguard Workerinterpretation of serialized snapshots with a populated ``trace_data`` field.
271