1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_trace: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker======== 4*61c4878aSAndroid Build Coastguard Workerpw_trace 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 (this module) which is only a macro interface layer 11*61c4878aSAndroid Build Coastguard Worker2. The backend, provided elsewhere, implements 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 add: 20*61c4878aSAndroid Build Coastguard Worker 21*61c4878aSAndroid Build Coastguard Worker1. A Python library to generate trace files which can be viewed. (pwbug/205) 22*61c4878aSAndroid Build Coastguard Worker2. Add more examples with sample output. (pwbug/207) 23*61c4878aSAndroid Build Coastguard Worker3. Implement a trace backend module. (pwbug/260) 24*61c4878aSAndroid Build Coastguard Worker 25*61c4878aSAndroid Build Coastguard Worker-------- 26*61c4878aSAndroid Build Coastguard WorkerOverview 27*61c4878aSAndroid Build Coastguard Worker-------- 28*61c4878aSAndroid Build Coastguard WorkerTraces provide a useful view which shows the flow of events through a system, 29*61c4878aSAndroid Build Coastguard Workerand can greatly assist in understanding complex software problems. These traces 30*61c4878aSAndroid Build Coastguard Workercan either show what tasks are running at any given time, or use added code 31*61c4878aSAndroid Build Coastguard Worker(similar to logging), to help annotate specific interesting flows. 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard WorkerFundamentally, tracing is similar to logging in that it provides the developer 34*61c4878aSAndroid Build Coastguard Workerwith a view into what the system was doing at a particular time. The fundamental 35*61c4878aSAndroid Build Coastguard Workerdifference between logs and tracing is that logs present information as ad-hoc 36*61c4878aSAndroid Build Coastguard Workerhuman-readable strings and are better suited to providing the current state of 37*61c4878aSAndroid Build Coastguard Workerthe system. Instead, traces capture sequences of events with precise timestamps, 38*61c4878aSAndroid Build Coastguard Workerand are therefore useful at understanding the flow of events in the system over 39*61c4878aSAndroid Build Coastguard Workertime. 40*61c4878aSAndroid Build Coastguard Worker 41*61c4878aSAndroid Build Coastguard WorkerThe default backend for pw_trace is pw_trace_null, which disables tracing. 42*61c4878aSAndroid Build Coastguard Worker 43*61c4878aSAndroid Build Coastguard WorkerCompatibility 44*61c4878aSAndroid Build Coastguard Worker------------- 45*61c4878aSAndroid Build Coastguard WorkerMost of the facade is compatible with C and C++, the only exception to this is 46*61c4878aSAndroid Build Coastguard Workerthe Scope and Function tracing macros which are convenience wrappers only 47*61c4878aSAndroid Build Coastguard Workeravailable in C++. 48*61c4878aSAndroid Build Coastguard Worker 49*61c4878aSAndroid Build Coastguard Workerpw_trace:null 50*61c4878aSAndroid Build Coastguard Worker------------- 51*61c4878aSAndroid Build Coastguard Worker``pw_trace_null`` is a ``pw_trace backend`` that ignores all ``pw_trace`` 52*61c4878aSAndroid Build Coastguard Workerstatements. The backend implements ``pw_trace`` with empty inline functions. 53*61c4878aSAndroid Build Coastguard WorkerUsing empty functions ensure that the arguments are evaluated and their types 54*61c4878aSAndroid Build Coastguard Workerare correct. Since the functions are inline in the header, the compiler will 55*61c4878aSAndroid Build Coastguard Workeroptimize out the function call. 56*61c4878aSAndroid Build Coastguard Worker 57*61c4878aSAndroid Build Coastguard WorkerThis backend can be used to completely disable ``pw_trace``. 58*61c4878aSAndroid Build Coastguard Worker 59*61c4878aSAndroid Build Coastguard WorkerDependencies 60*61c4878aSAndroid Build Coastguard Worker------------- 61*61c4878aSAndroid Build Coastguard Worker``pw_preprocessor`` 62*61c4878aSAndroid Build Coastguard Worker 63*61c4878aSAndroid Build Coastguard WorkerExample 64*61c4878aSAndroid Build Coastguard Worker------- 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard Worker #define PW_TRACE_MODULE_NAME "Input" 69*61c4878aSAndroid Build Coastguard Worker #include "pw_trace/trace.h" 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard Worker void SendButton() { 72*61c4878aSAndroid Build Coastguard Worker PW_TRACE_FUNCTION() 73*61c4878aSAndroid Build Coastguard Worker // do something 74*61c4878aSAndroid Build Coastguard Worker } 75*61c4878aSAndroid Build Coastguard Worker 76*61c4878aSAndroid Build Coastguard Worker void InputLoop() { 77*61c4878aSAndroid Build Coastguard Worker while(1) { 78*61c4878aSAndroid Build Coastguard Worker auto event = WaitNewInputEvent() 79*61c4878aSAndroid Build Coastguard Worker TRACE_SCOPE("Handle Event"); // measure until loop finished 80*61c4878aSAndroid Build Coastguard Worker if (event == kNewButton){ 81*61c4878aSAndroid Build Coastguard Worker SendButton(); 82*61c4878aSAndroid Build Coastguard Worker PW_TRACE_END("button"); // Trace event was started in ButtonIsr 83*61c4878aSAndroid Build Coastguard Worker } else { 84*61c4878aSAndroid Build Coastguard Worker PW_TRACE_INSTANT("Unknown event"); 85*61c4878aSAndroid Build Coastguard Worker } 86*61c4878aSAndroid Build Coastguard Worker } 87*61c4878aSAndroid Build Coastguard Worker } 88*61c4878aSAndroid Build Coastguard Worker 89*61c4878aSAndroid Build Coastguard Worker void ButtonIsr() { 90*61c4878aSAndroid Build Coastguard Worker PW_TRACE_START("button"); 91*61c4878aSAndroid Build Coastguard Worker SendNewInputEvent(kNewButton); 92*61c4878aSAndroid Build Coastguard Worker } 93*61c4878aSAndroid Build Coastguard Worker 94*61c4878aSAndroid Build Coastguard Worker 95*61c4878aSAndroid Build Coastguard Worker------------ 96*61c4878aSAndroid Build Coastguard WorkerTrace macros 97*61c4878aSAndroid Build Coastguard Worker------------ 98*61c4878aSAndroid Build Coastguard Worker 99*61c4878aSAndroid Build Coastguard WorkerThe ``pw_trace`` public API provides three basic trace events: 100*61c4878aSAndroid Build Coastguard Worker 101*61c4878aSAndroid Build Coastguard Worker- ``PW_TRACE_INSTANT`` - This is used to trace an instant event, which has no 102*61c4878aSAndroid Build Coastguard Worker duration. 103*61c4878aSAndroid Build Coastguard Worker- ``PW_TRACE_START`` & ``PW_TRACE_END`` - Trace 'start' and 'end' events are 104*61c4878aSAndroid Build Coastguard Worker paired together to measure the duration of an event. 105*61c4878aSAndroid Build Coastguard Worker 106*61c4878aSAndroid Build Coastguard WorkerThese trace event macros all have the same arguments: 107*61c4878aSAndroid Build Coastguard Worker 108*61c4878aSAndroid Build Coastguard Worker- *label* - Each of these trace events must have a label, which is a string 109*61c4878aSAndroid Build Coastguard Worker describing the event. In addition to the required label, each of these traces 110*61c4878aSAndroid Build Coastguard Worker can optionally provide a group label and trace id. 111*61c4878aSAndroid Build Coastguard Worker- *group_label* - The *optional* group label is used if many traces are all 112*61c4878aSAndroid Build Coastguard Worker measuring the same thing and should be grouped together. This information will 113*61c4878aSAndroid Build Coastguard Worker be used when visualizing the trace to ensure the information appears together. 114*61c4878aSAndroid Build Coastguard Worker- *trace_id* - The *optional* trace id is similar to the group_id, but instead 115*61c4878aSAndroid Build Coastguard Worker groups events using a runtime value. This can be used if multiple trace flow 116*61c4878aSAndroid Build Coastguard Worker might happen at the same time and need to be grouped together. 117*61c4878aSAndroid Build Coastguard Worker For example, this could be used to trace data packets flowing through the 118*61c4878aSAndroid Build Coastguard Worker system; when a new sample might arrive before the previous packet is finished 119*61c4878aSAndroid Build Coastguard Worker processing. This would result in two start events occurring before the end 120*61c4878aSAndroid Build Coastguard Worker event. By providing a trace id with a different value for each packet, these 121*61c4878aSAndroid Build Coastguard Worker can be separated when decoding. 122*61c4878aSAndroid Build Coastguard Worker 123*61c4878aSAndroid Build Coastguard Worker.. tip:: 124*61c4878aSAndroid Build Coastguard Worker 125*61c4878aSAndroid Build Coastguard Worker All of these arguments must be the same for a *start* and *end* pair. 126*61c4878aSAndroid Build Coastguard Worker 127*61c4878aSAndroid Build Coastguard WorkerThis results in 9 different trace calls: 128*61c4878aSAndroid Build Coastguard Worker 129*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT(label) 130*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT(label, group_label) 131*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT(label, group_label, trace_id) 132*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START(label) 133*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START(label, group_label) 134*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START(label, group_label, trace_id) 135*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END(label) 136*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END(label, group_label) 137*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END(label, group_label, trace_id) 138*61c4878aSAndroid Build Coastguard Worker 139*61c4878aSAndroid Build Coastguard WorkerModules 140*61c4878aSAndroid Build Coastguard Worker------- 141*61c4878aSAndroid Build Coastguard WorkerIn addition to these arguments, traces can be grouped into modules similar to 142*61c4878aSAndroid Build Coastguard Workerlogging. This is done by redefining the ``PW_TRACE_MODULE_NAME``. 143*61c4878aSAndroid Build Coastguard Worker 144*61c4878aSAndroid Build Coastguard WorkerFlags 145*61c4878aSAndroid Build Coastguard Worker----- 146*61c4878aSAndroid Build Coastguard WorkerEach trace event also has a flags field which can be used to provide additional 147*61c4878aSAndroid Build Coastguard Workercompile time trace information. Each trace macro has a matching macro which 148*61c4878aSAndroid Build Coastguard Workerallows specifying the flag: 149*61c4878aSAndroid Build Coastguard Worker 150*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT_FLAG(flag, label) 151*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT_FLAG(flag, label, group_label) 152*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT_FLAG(flag, label, group_label, trace_id) 153*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START_FLAG(flag, label) 154*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START_FLAG(flag, label, group_label) 155*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START_FLAG(flag, label, group_label, trace_id) 156*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END_FLAG(flag, label) 157*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END_FLAG(flag, label, group_label) 158*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END_FLAG(flag, label, group_label, trace_id) 159*61c4878aSAndroid Build Coastguard Worker 160*61c4878aSAndroid Build Coastguard WorkerWhen not specified the flag uses the value of the macro ``PW_TRACE_FLAGS``. 161*61c4878aSAndroid Build Coastguard Worker 162*61c4878aSAndroid Build Coastguard WorkerData 163*61c4878aSAndroid Build Coastguard Worker---- 164*61c4878aSAndroid Build Coastguard WorkerEach macro also has a variant which allows appending additional data: 165*61c4878aSAndroid Build Coastguard Worker 166*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT_DATA(label, data_format_string, data, size) 167*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT_DATA(\ 168*61c4878aSAndroid Build Coastguard Worker label, group_label, data_format_string, data, size) 169*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_INSTANT_DATA(\ 170*61c4878aSAndroid Build Coastguard Worker label, group_label, trace_id, data_format_string, data, size) 171*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START_DATA(label, data_format_string, data, size) 172*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START_DATA(\ 173*61c4878aSAndroid Build Coastguard Worker label, group_label, data_format_string, data, size) 174*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_START_DATA(\ 175*61c4878aSAndroid Build Coastguard Worker label, group_label, trace_id, data_format_string, data, size) 176*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END_DATA(label, data_format_string, data, size) 177*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END_DATA(\ 178*61c4878aSAndroid Build Coastguard Worker label, group_label, data_format_string, data, size) 179*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_END_DATA(\ 180*61c4878aSAndroid Build Coastguard Worker label, group_label, trace_id, data_format_string, data, size) 181*61c4878aSAndroid Build Coastguard Worker 182*61c4878aSAndroid Build Coastguard WorkerThese macros require 3 additional arguments: 183*61c4878aSAndroid Build Coastguard Worker 184*61c4878aSAndroid Build Coastguard Worker- *data_format_string* - A string which is used by the decoder to identify the 185*61c4878aSAndroid Build Coastguard Worker data. This could for example either be printf style tokens, python struct 186*61c4878aSAndroid Build Coastguard Worker packed fmt string or a custom label recognized by the decoder. 187*61c4878aSAndroid Build Coastguard Worker- *data* - A pointer to a buffer of arbitrary caller-provided data (void*). 188*61c4878aSAndroid Build Coastguard Worker- *size* - The size of the data (size_t). 189*61c4878aSAndroid Build Coastguard Worker 190*61c4878aSAndroid Build Coastguard WorkerCurrently the included python tool supports a few different options for 191*61c4878aSAndroid Build Coastguard Worker*data_format_string*: 192*61c4878aSAndroid Build Coastguard Worker 193*61c4878aSAndroid Build Coastguard Worker- *@pw_arg_label* - Uses the string in the data as the trace event label. 194*61c4878aSAndroid Build Coastguard Worker- *@pw_arg_group* - Uses the string in the data as the trace event group. 195*61c4878aSAndroid Build Coastguard Worker- *@pw_arg_counter* - Uses the data as a little endian integer value, and 196*61c4878aSAndroid Build Coastguard Worker visualizes it as a counter value in the trace (on a graph). 197*61c4878aSAndroid Build Coastguard Worker- *@pw_py_struct_fmt:* - Interprets the string after the ":" as a python struct 198*61c4878aSAndroid Build Coastguard Worker format string, and uses that format string to unpack the data elements. This 199*61c4878aSAndroid Build Coastguard Worker can be used to either provide a single value type, or provide multiple 200*61c4878aSAndroid Build Coastguard Worker different values with a variety of types. Options for format string types can 201*61c4878aSAndroid Build Coastguard Worker be found here: https://docs.python.org/3/library/struct.html#format-characters 202*61c4878aSAndroid Build Coastguard Worker . The data is always assumed to be packed with little-endian ordering if not 203*61c4878aSAndroid Build Coastguard Worker indicated otherwise:: 204*61c4878aSAndroid Build Coastguard Worker 205*61c4878aSAndroid Build Coastguard Worker // Example 206*61c4878aSAndroid Build Coastguard Worker data_format_string = "@pw_py_struct_fmt:ll" 207*61c4878aSAndroid Build Coastguard Worker data = 0x1400000014000000 208*61c4878aSAndroid Build Coastguard Worker args = {data_0: 20, data_1:20} 209*61c4878aSAndroid Build Coastguard Worker- *@pw_py_map_fmt:* - Interprets the string after the ":" as a dictionary 210*61c4878aSAndroid Build Coastguard Worker relating the data field name to the python struct format string. Once 211*61c4878aSAndroid Build Coastguard Worker collected, the format strings are concatenated and used to unpack the data 212*61c4878aSAndroid Build Coastguard Worker elements as above. The data is always assumed to be packed with little-endian 213*61c4878aSAndroid Build Coastguard Worker ordering if not indicated otherwise. To specify a different ordering, 214*61c4878aSAndroid Build Coastguard Worker construct the format string as ``@pw_py_map_fmt:[@=<>!]{k:v,...}``:: 215*61c4878aSAndroid Build Coastguard Worker 216*61c4878aSAndroid Build Coastguard Worker // Example 217*61c4878aSAndroid Build Coastguard Worker data_format_string = "@pw_py_map_fmt:{Field: l, Field2: l }" 218*61c4878aSAndroid Build Coastguard Worker data = 0x1400000014000000 219*61c4878aSAndroid Build Coastguard Worker args = {Field: 20, Field2:20} 220*61c4878aSAndroid Build Coastguard Worker 221*61c4878aSAndroid Build Coastguard Worker.. tip:: 222*61c4878aSAndroid Build Coastguard Worker 223*61c4878aSAndroid Build Coastguard Worker It is ok for only one event of a start/end pair to contain data, as long the 224*61c4878aSAndroid Build Coastguard Worker *label*, *group_label*, and *trace_id*, are all the same. 225*61c4878aSAndroid Build Coastguard Worker 226*61c4878aSAndroid Build Coastguard WorkerC++ Only Traces 227*61c4878aSAndroid Build Coastguard Worker--------------- 228*61c4878aSAndroid Build Coastguard WorkerScope API measures durations until the object loses scope. This can for 229*61c4878aSAndroid Build Coastguard Workerexample, provide a convenient method of tracing functions or loops. 230*61c4878aSAndroid Build Coastguard Worker 231*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_SCOPE(label) 232*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_SCOPE(label, group_label) 233*61c4878aSAndroid Build Coastguard Worker 234*61c4878aSAndroid Build Coastguard WorkerFunction API measures durations until the function returns. This is the only 235*61c4878aSAndroid Build Coastguard Workermacro which does not require a *label*, and instead uses the function name as the 236*61c4878aSAndroid Build Coastguard Workerlabel. It still can optionally be provided a *group_id*. 237*61c4878aSAndroid Build Coastguard Worker 238*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_FUNCTION() 239*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_FUNCTION(group_label) 240*61c4878aSAndroid Build Coastguard Worker 241*61c4878aSAndroid Build Coastguard WorkerCompile time enabling/disabling 242*61c4878aSAndroid Build Coastguard Worker------------------------------- 243*61c4878aSAndroid Build Coastguard WorkerTraces in a file can be enabled/disabled at compile time by defining through 244*61c4878aSAndroid Build Coastguard Workerthe ``PW_TRACE_ENABLE`` macro. A value of 0 causes traces to be disabled. 245*61c4878aSAndroid Build Coastguard WorkerA non-zero value will enable traces. While tracing defaults to enabled, 246*61c4878aSAndroid Build Coastguard Workerit is best practice to define ``PW_TRACE_ENABLE`` explicitly in files that 247*61c4878aSAndroid Build Coastguard Workeruse tracing as the default may change in the future. 248*61c4878aSAndroid Build Coastguard Worker 249*61c4878aSAndroid Build Coastguard WorkerA good pattern is to have a module level configuration parameter for enabling 250*61c4878aSAndroid Build Coastguard Workertracing and define ``PW_TRACE_ENABLE`` in terms of that at the top of each 251*61c4878aSAndroid Build Coastguard Workerof the module's files: 252*61c4878aSAndroid Build Coastguard Worker 253*61c4878aSAndroid Build Coastguard Worker 254*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 255*61c4878aSAndroid Build Coastguard Worker 256*61c4878aSAndroid Build Coastguard Worker // Enable tracing based on pw_example module config parameter. 257*61c4878aSAndroid Build Coastguard Worker #define PW_TRACE_ENABLE PW_EXAMPLE_TRACE_ENABLE 258*61c4878aSAndroid Build Coastguard Worker 259*61c4878aSAndroid Build Coastguard Worker 260*61c4878aSAndroid Build Coastguard WorkerAdditionally specific trace points (or sets of points) can be enabled/disabled 261*61c4878aSAndroid Build Coastguard Workerusing the following pattern: 262*61c4878aSAndroid Build Coastguard Worker 263*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 264*61c4878aSAndroid Build Coastguard Worker 265*61c4878aSAndroid Build Coastguard Worker // Assuming tracing is disabled at the top of the file. 266*61c4878aSAndroid Build Coastguard Worker 267*61c4878aSAndroid Build Coastguard Worker // Enable specific trace. 268*61c4878aSAndroid Build Coastguard Worker #undef PW_TRACE_ENABLE 269*61c4878aSAndroid Build Coastguard Worker #define PW_TRACE_ENABLE 1 270*61c4878aSAndroid Build Coastguard Worker PW_TRACE_INSTANT("important trace"); 271*61c4878aSAndroid Build Coastguard Worker 272*61c4878aSAndroid Build Coastguard Worker // Set traces back to disabled. PW_TRACE_ENABLE can not be left 273*61c4878aSAndroid Build Coastguard Worker // undefined. 274*61c4878aSAndroid Build Coastguard Worker #undef PW_TRACE_ENABLE 275*61c4878aSAndroid Build Coastguard Worker #define PW_TRACE_ENABLE 0 276*61c4878aSAndroid Build Coastguard Worker 277*61c4878aSAndroid Build Coastguard Worker----------- 278*61c4878aSAndroid Build Coastguard WorkerBackend API 279*61c4878aSAndroid Build Coastguard Worker----------- 280*61c4878aSAndroid Build Coastguard WorkerEach of the trace event macros get sent to one of two macros which are 281*61c4878aSAndroid Build Coastguard Workerimplemented by the backend: 282*61c4878aSAndroid Build Coastguard Worker 283*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE(event_type, flags, label, group_label, trace_id) 284*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_TRACE_DATA(event_type, flags, label, group_label, \ 285*61c4878aSAndroid Build Coastguard Worker trace_id, data_format_string, data, size) 286*61c4878aSAndroid Build Coastguard Worker 287*61c4878aSAndroid Build Coastguard WorkerThe ``event_type`` value will be whatever the backend defined for that specific 288*61c4878aSAndroid Build Coastguard Workertrace type using the macros defined below. 289*61c4878aSAndroid Build Coastguard Worker 290*61c4878aSAndroid Build Coastguard WorkerThe backend can optionally not define ``PW_TRACE_DATA`` to have those traces 291*61c4878aSAndroid Build Coastguard Workerdisabled. 292*61c4878aSAndroid Build Coastguard Worker 293*61c4878aSAndroid Build Coastguard WorkerTrace types 294*61c4878aSAndroid Build Coastguard Worker----------- 295*61c4878aSAndroid Build Coastguard WorkerAlthough there are only 3 basic trace types, each has 3 variants: 296*61c4878aSAndroid Build Coastguard Worker 297*61c4878aSAndroid Build Coastguard Worker- Label only 298*61c4878aSAndroid Build Coastguard Worker- Label and group 299*61c4878aSAndroid Build Coastguard Worker- Label, group, and trace_id 300*61c4878aSAndroid Build Coastguard Worker 301*61c4878aSAndroid Build Coastguard WorkerThis combination creates 9 different trace event types: 302*61c4878aSAndroid Build Coastguard Worker 303*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_INSTANT*: Instant trace, with only a label. 304*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_DURATION_START*: Start trace, with only a label. 305*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_DURATION_END*: End trace, with only a label. 306*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_INSTANT_GROUP*: Instant trace, with a label and a group. 307*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_DURATION_GROUP_START*: Start trace, with a label and a group. 308*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_DURATION_GROUP_END*: End trace, with a label and a group. 309*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_ASYNC_INSTANT*: Instant trace, with label, group, and trace_id 310*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_ASYNC_START*: Start trace, with label, group, and trace_id. 311*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TYPE_ASYNC_END*: End trace, with label, group, and trace_id. 312*61c4878aSAndroid Build Coastguard Worker 313*61c4878aSAndroid Build Coastguard WorkerThe backend must define these macros to have them enabled. If any are left 314*61c4878aSAndroid Build Coastguard Workerundefined, any traces of that type are removed. 315*61c4878aSAndroid Build Coastguard Worker 316*61c4878aSAndroid Build Coastguard WorkerDefaults 317*61c4878aSAndroid Build Coastguard Worker-------- 318*61c4878aSAndroid Build Coastguard WorkerThe backend can use these macros to change what the default value is if not 319*61c4878aSAndroid Build Coastguard Workerprovided. 320*61c4878aSAndroid Build Coastguard Worker 321*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_FLAGS_DEFAULT*: Default value if no flags are provided. 322*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_TRACE_ID_DEFAULT*: Default value if no trace_id provided. 323*61c4878aSAndroid Build Coastguard Worker- *PW_TRACE_GROUP_LABEL_DEFAULT*: Default value if no group_label provided. 324*61c4878aSAndroid Build Coastguard Worker 325*61c4878aSAndroid Build Coastguard Worker---------- 326*61c4878aSAndroid Build Coastguard WorkerSample App 327*61c4878aSAndroid Build Coastguard Worker---------- 328*61c4878aSAndroid Build Coastguard WorkerA sample application is provided in the examples folder. This code attempts to 329*61c4878aSAndroid Build Coastguard Workerprovide examples of the multiple ways tracing can be used. Furthermore, 330*61c4878aSAndroid Build Coastguard Workertrace backends can include the sample app in their own examples to show how to 331*61c4878aSAndroid Build Coastguard Workeruse other features. 332*61c4878aSAndroid Build Coastguard Worker 333*61c4878aSAndroid Build Coastguard WorkerThe sample app contains 3 "fake" tasks, which are each in their own 334*61c4878aSAndroid Build Coastguard Worker`PW_TRACE_MODULE`. 335*61c4878aSAndroid Build Coastguard Worker 336*61c4878aSAndroid Build Coastguard Worker- *Input*: Simulating a driver, which gets data periodically, and sends to 337*61c4878aSAndroid Build Coastguard Worker *Processing* task. 338*61c4878aSAndroid Build Coastguard Worker- *Processing*: Has a work queue, which handles processing the jobs. 339*61c4878aSAndroid Build Coastguard Worker- *Kernel*: A simple work loop which demonstrates a possible integration of 340*61c4878aSAndroid Build Coastguard Worker tracing with a RTOS/Kernel. 341*61c4878aSAndroid Build Coastguard Worker 342*61c4878aSAndroid Build Coastguard WorkerJobs are intentionally made to have multiple stages of processing (simulated by 343*61c4878aSAndroid Build Coastguard Workerbeing re-added to the work-queue). This results in multiple jobs being handled 344*61c4878aSAndroid Build Coastguard Workerat the same time, the trace_id is used to separate these traces. 345*61c4878aSAndroid Build Coastguard Worker 346*61c4878aSAndroid Build Coastguard Worker---------------------- 347*61c4878aSAndroid Build Coastguard WorkerPython Trace Generator 348*61c4878aSAndroid Build Coastguard Worker---------------------- 349*61c4878aSAndroid Build Coastguard WorkerThe Python tool is still in early development, but currently it supports 350*61c4878aSAndroid Build Coastguard Workergenerating a list of json lines from a list of trace events. 351*61c4878aSAndroid Build Coastguard Worker 352*61c4878aSAndroid Build Coastguard WorkerTo view the trace, these lines can be saved to a file and loaded into 353*61c4878aSAndroid Build Coastguard Workerchrome://tracing. 354*61c4878aSAndroid Build Coastguard Worker 355*61c4878aSAndroid Build Coastguard WorkerFuture work will look to add: 356*61c4878aSAndroid Build Coastguard Worker 357*61c4878aSAndroid Build Coastguard Worker- Config options to customize output. 358*61c4878aSAndroid Build Coastguard Worker- A method of providing custom data formatters. 359*61c4878aSAndroid Build Coastguard Worker- Perfetto support. 360*61c4878aSAndroid Build Coastguard Worker 361*61c4878aSAndroid Build Coastguard Worker 362*61c4878aSAndroid Build Coastguard Worker.. toctree:: 363*61c4878aSAndroid Build Coastguard Worker :hidden: 364*61c4878aSAndroid Build Coastguard Worker :maxdepth: 1 365*61c4878aSAndroid Build Coastguard Worker 366*61c4878aSAndroid Build Coastguard Worker Backends <backends> 367