1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file under third_party_mods/chromium or at:
4 // http://src.chromium.org/svn/trunk/src/LICENSE
5
6 #ifndef RTC_BASE_TRACE_EVENT_H_
7 #define RTC_BASE_TRACE_EVENT_H_
8
9 #include <string>
10
11 #include "rtc_base/event_tracer.h"
12
13 #if defined(TRACE_EVENT0)
14 #error "Another copy of trace_event.h has already been included."
15 #endif
16
17 #if defined(RTC_DISABLE_TRACE_EVENTS)
18 #define RTC_TRACE_EVENTS_ENABLED 0
19 #else
20 #define RTC_TRACE_EVENTS_ENABLED 1
21 #endif
22
23 // Type values for identifying types in the TraceValue union.
24 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
25 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
26 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
27 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
28 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
29 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
30 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
31
32 #if RTC_TRACE_EVENTS_ENABLED
33
34 // Extracted from Chromium's src/base/debug/trace_event.h.
35
36 // This header is designed to give you trace_event macros without specifying
37 // how the events actually get collected and stored. If you need to expose trace
38 // event to some other universe, you can copy-and-paste this file,
39 // implement the TRACE_EVENT_API macros, and do any other necessary fixup for
40 // the target platform. The end result is that multiple libraries can funnel
41 // events through to a shared trace event collector.
42
43 // Trace events are for tracking application performance and resource usage.
44 // Macros are provided to track:
45 // Begin and end of function calls
46 // Counters
47 //
48 // Events are issued against categories. Whereas RTC_LOG's
49 // categories are statically defined, TRACE categories are created
50 // implicitly with a string. For example:
51 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent")
52 //
53 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope:
54 // TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly")
55 // doSomethingCostly()
56 // TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly")
57 // Note: our tools can't always determine the correct BEGIN/END pairs unless
58 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you
59 // need them to be in separate scopes.
60 //
61 // A common use case is to trace entire function scopes. This
62 // issues a trace BEGIN and END automatically:
63 // void doSomethingCostly() {
64 // TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly");
65 // ...
66 // }
67 //
68 // Additional parameters can be associated with an event:
69 // void doSomethingCostly2(int howMuch) {
70 // TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly",
71 // "howMuch", howMuch);
72 // ...
73 // }
74 //
75 // The trace system will automatically add to this information the
76 // current process id, thread id, and a timestamp in microseconds.
77 //
78 // To trace an asynchronous procedure such as an IPC send/receive, use
79 // ASYNC_BEGIN and ASYNC_END:
80 // [single threaded sender code]
81 // static int send_count = 0;
82 // ++send_count;
83 // TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count);
84 // Send(new MyMessage(send_count));
85 // [receive code]
86 // void OnMyMessage(send_count) {
87 // TRACE_EVENT_ASYNC_END0("ipc", "message", send_count);
88 // }
89 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs.
90 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process.
91 // Pointers can be used for the ID parameter, and they will be mangled
92 // internally so that the same pointer on two different processes will not
93 // match. For example:
94 // class MyTracedClass {
95 // public:
96 // MyTracedClass() {
97 // TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this);
98 // }
99 // ~MyTracedClass() {
100 // TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this);
101 // }
102 // }
103 //
104 // Trace event also supports counters, which is a way to track a quantity
105 // as it varies over time. Counters are created with the following macro:
106 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue);
107 //
108 // Counters are process-specific. The macro itself can be issued from any
109 // thread, however.
110 //
111 // Sometimes, you want to track two counters at once. You can do this with two
112 // counter macros:
113 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]);
114 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]);
115 // Or you can do it with a combined macro:
116 // TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter",
117 // "bytesPinned", g_myCounterValue[0],
118 // "bytesAllocated", g_myCounterValue[1]);
119 // This indicates to the tracing UI that these counters should be displayed
120 // in a single graph, as a summed area chart.
121 //
122 // Since counters are in a global namespace, you may want to disembiguate with a
123 // unique ID, by using the TRACE_COUNTER_ID* variations.
124 //
125 // By default, trace collection is compiled in, but turned off at runtime.
126 // Collecting trace data is the responsibility of the embedding
127 // application. In Chrome's case, navigating to about:tracing will turn on
128 // tracing and display data collected across all active processes.
129 //
130 //
131 // Memory scoping note:
132 // Tracing copies the pointers, not the string content, of the strings passed
133 // in for category, name, and arg_names. Thus, the following code will
134 // cause problems:
135 // char* str = strdup("impprtantName");
136 // TRACE_EVENT_INSTANT0("SUBSYSTEM", str); // BAD!
137 // free(str); // Trace system now has dangling pointer
138 //
139 // To avoid this issue with the |name| and |arg_name| parameters, use the
140 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead.
141 // Notes: The category must always be in a long-lived char* (i.e. static const).
142 // The |arg_values|, when used, are always deep copied with the _COPY
143 // macros.
144 //
145 // When are string argument values copied:
146 // const char* arg_values are only referenced by default:
147 // TRACE_EVENT1("category", "name",
148 // "arg1", "literal string is only referenced");
149 // Use TRACE_STR_COPY to force copying of a const char*:
150 // TRACE_EVENT1("category", "name",
151 // "arg1", TRACE_STR_COPY("string will be copied"));
152 // std::string arg_values are always copied:
153 // TRACE_EVENT1("category", "name",
154 // "arg1", std::string("string will be copied"));
155 //
156 //
157 // Thread Safety:
158 // Thread safety is provided by methods defined in event_tracer.h. See the file
159 // for details.
160
161 // By default, const char* argument values are assumed to have long-lived scope
162 // and will not be copied. Use this macro to force a const char* to be copied.
163 #define TRACE_STR_COPY(str) \
164 webrtc::trace_event_internal::TraceStringWithCopy(str)
165
166 // This will mark the trace event as disabled by default. The user will need
167 // to explicitly enable the event.
168 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name
169
170 // By default, uint64 ID argument values are not mangled with the Process ID in
171 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
172 #define TRACE_ID_MANGLE(id) \
173 webrtc::trace_event_internal::TraceID::ForceMangle(id)
174
175 // Records a pair of begin and end events called "name" for the current
176 // scope, with 0, 1 or 2 associated arguments. If the category is not
177 // enabled, then this does nothing.
178 // - category and name strings must have application lifetime (statics or
179 // literals). They may not include " chars.
180 #define TRACE_EVENT0(category, name) \
181 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name)
182 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \
183 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val)
184 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \
185 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \
186 arg2_name, arg2_val)
187
188 // Records a single event called "name" immediately, with 0, 1 or 2
189 // associated arguments. If the category is not enabled, then this
190 // does nothing.
191 // - category and name strings must have application lifetime (statics or
192 // literals). They may not include " chars.
193 #define TRACE_EVENT_INSTANT0(category, name) \
194 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
195 category, name, TRACE_EVENT_FLAG_NONE)
196 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
197 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
198 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
199 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
200 arg2_name, arg2_val) \
201 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
202 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
203 arg2_name, arg2_val)
204 #define TRACE_EVENT_COPY_INSTANT0(category, name) \
205 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
206 category, name, TRACE_EVENT_FLAG_COPY)
207 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \
208 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
209 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
210 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \
211 arg2_name, arg2_val) \
212 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
213 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
214 arg2_name, arg2_val)
215
216 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
217 // associated arguments. If the category is not enabled, then this
218 // does nothing.
219 // - category and name strings must have application lifetime (statics or
220 // literals). They may not include " chars.
221 #define TRACE_EVENT_BEGIN0(category, name) \
222 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
223 category, name, TRACE_EVENT_FLAG_NONE)
224 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \
225 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
226 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
227 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \
228 arg2_name, arg2_val) \
229 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
230 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
231 arg2_name, arg2_val)
232 #define TRACE_EVENT_COPY_BEGIN0(category, name) \
233 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
234 category, name, TRACE_EVENT_FLAG_COPY)
235 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \
236 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
237 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
238 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \
239 arg2_name, arg2_val) \
240 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
241 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
242 arg2_name, arg2_val)
243
244 // Records a single END event for "name" immediately. If the category
245 // is not enabled, then this does nothing.
246 // - category and name strings must have application lifetime (statics or
247 // literals). They may not include " chars.
248 #define TRACE_EVENT_END0(category, name) \
249 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
250 category, name, TRACE_EVENT_FLAG_NONE)
251 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \
252 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
253 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
254 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \
255 arg2_name, arg2_val) \
256 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
257 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
258 arg2_name, arg2_val)
259 #define TRACE_EVENT_COPY_END0(category, name) \
260 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
261 category, name, TRACE_EVENT_FLAG_COPY)
262 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \
263 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
264 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
265 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \
266 arg2_name, arg2_val) \
267 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
268 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
269 arg2_name, arg2_val)
270
271 // Records the value of a counter called "name" immediately. Value
272 // must be representable as a 32 bit integer.
273 // - category and name strings must have application lifetime (statics or
274 // literals). They may not include " chars.
275 #define TRACE_COUNTER1(category, name, value) \
276 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
277 category, name, TRACE_EVENT_FLAG_NONE, \
278 "value", static_cast<int>(value))
279 #define TRACE_COPY_COUNTER1(category, name, value) \
280 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
281 category, name, TRACE_EVENT_FLAG_COPY, \
282 "value", static_cast<int>(value))
283
284 // Records the values of a multi-parted counter called "name" immediately.
285 // The UI will treat value1 and value2 as parts of a whole, displaying their
286 // values as a stacked-bar chart.
287 // - category and name strings must have application lifetime (statics or
288 // literals). They may not include " chars.
289 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \
290 value2_name, value2_val) \
291 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
292 category, name, TRACE_EVENT_FLAG_NONE, \
293 value1_name, static_cast<int>(value1_val), \
294 value2_name, static_cast<int>(value2_val))
295 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \
296 value2_name, value2_val) \
297 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
298 category, name, TRACE_EVENT_FLAG_COPY, \
299 value1_name, static_cast<int>(value1_val), \
300 value2_name, static_cast<int>(value2_val))
301
302 // Records the value of a counter called "name" immediately. Value
303 // must be representable as a 32 bit integer.
304 // - category and name strings must have application lifetime (statics or
305 // literals). They may not include " chars.
306 // - |id| is used to disambiguate counters with the same name. It must either
307 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
308 // will be xored with a hash of the process ID so that the same pointer on
309 // two different processes will not collide.
310 #define TRACE_COUNTER_ID1(category, name, id, value) \
311 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
312 category, name, id, TRACE_EVENT_FLAG_NONE, \
313 "value", static_cast<int>(value))
314 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \
315 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
316 category, name, id, TRACE_EVENT_FLAG_COPY, \
317 "value", static_cast<int>(value))
318
319 // Records the values of a multi-parted counter called "name" immediately.
320 // The UI will treat value1 and value2 as parts of a whole, displaying their
321 // values as a stacked-bar chart.
322 // - category and name strings must have application lifetime (statics or
323 // literals). They may not include " chars.
324 // - |id| is used to disambiguate counters with the same name. It must either
325 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
326 // will be xored with a hash of the process ID so that the same pointer on
327 // two different processes will not collide.
328 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \
329 value2_name, value2_val) \
330 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
331 category, name, id, TRACE_EVENT_FLAG_NONE, \
332 value1_name, static_cast<int>(value1_val), \
333 value2_name, static_cast<int>(value2_val))
334 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \
335 value2_name, value2_val) \
336 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
337 category, name, id, TRACE_EVENT_FLAG_COPY, \
338 value1_name, static_cast<int>(value1_val), \
339 value2_name, static_cast<int>(value2_val))
340
341
342 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
343 // associated arguments. If the category is not enabled, then this
344 // does nothing.
345 // - category and name strings must have application lifetime (statics or
346 // literals). They may not include " chars.
347 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
348 // events are considered to match if their category, name and id values all
349 // match. |id| must either be a pointer or an integer value up to 64 bits. If
350 // it's a pointer, the bits will be xored with a hash of the process ID so
351 // that the same pointer on two different processes will not collide.
352 // An asynchronous operation can consist of multiple phases. The first phase is
353 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
354 // ASYNC_STEP macros. When the operation completes, call ASYNC_END.
355 // An ASYNC trace typically occur on a single thread (if not, they will only be
356 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
357 // operation must use the same |name| and |id|. Each event can have its own
358 // args.
359 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \
360 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
361 category, name, id, TRACE_EVENT_FLAG_NONE)
362 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
363 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
364 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
365 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
366 arg2_name, arg2_val) \
367 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
368 category, name, id, TRACE_EVENT_FLAG_NONE, \
369 arg1_name, arg1_val, arg2_name, arg2_val)
370 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \
371 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
372 category, name, id, TRACE_EVENT_FLAG_COPY)
373 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
374 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
375 category, name, id, TRACE_EVENT_FLAG_COPY, \
376 arg1_name, arg1_val)
377 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
378 arg2_name, arg2_val) \
379 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
380 category, name, id, TRACE_EVENT_FLAG_COPY, \
381 arg1_name, arg1_val, arg2_name, arg2_val)
382
383 // Records a single ASYNC_STEP event for |step| immediately. If the category
384 // is not enabled, then this does nothing. The |name| and |id| must match the
385 // ASYNC_BEGIN event above. The |step| param identifies this step within the
386 // async event. This should be called at the beginning of the next phase of an
387 // asynchronous operation.
388 #define TRACE_EVENT_ASYNC_STEP0(category, name, id, step) \
389 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
390 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
391 #define TRACE_EVENT_ASYNC_STEP1(category, name, id, step, \
392 arg1_name, arg1_val) \
393 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
394 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
395 arg1_name, arg1_val)
396 #define TRACE_EVENT_COPY_ASYNC_STEP0(category, name, id, step) \
397 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
398 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
399 #define TRACE_EVENT_COPY_ASYNC_STEP1(category, name, id, step, \
400 arg1_name, arg1_val) \
401 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
402 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
403 arg1_name, arg1_val)
404
405 // Records a single ASYNC_END event for "name" immediately. If the category
406 // is not enabled, then this does nothing.
407 #define TRACE_EVENT_ASYNC_END0(category, name, id) \
408 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
409 category, name, id, TRACE_EVENT_FLAG_NONE)
410 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
411 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
412 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
413 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
414 arg2_name, arg2_val) \
415 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
416 category, name, id, TRACE_EVENT_FLAG_NONE, \
417 arg1_name, arg1_val, arg2_name, arg2_val)
418 #define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \
419 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
420 category, name, id, TRACE_EVENT_FLAG_COPY)
421 #define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
422 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
423 category, name, id, TRACE_EVENT_FLAG_COPY, \
424 arg1_name, arg1_val)
425 #define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
426 arg2_name, arg2_val) \
427 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
428 category, name, id, TRACE_EVENT_FLAG_COPY, \
429 arg1_name, arg1_val, arg2_name, arg2_val)
430
431
432 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
433 // associated arguments. If the category is not enabled, then this
434 // does nothing.
435 // - category and name strings must have application lifetime (statics or
436 // literals). They may not include " chars.
437 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
438 // events are considered to match if their category, name and id values all
439 // match. |id| must either be a pointer or an integer value up to 64 bits. If
440 // it's a pointer, the bits will be xored with a hash of the process ID so
441 // that the same pointer on two different processes will not collide.
442 // FLOW events are different from ASYNC events in how they are drawn by the
443 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
444 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
445 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
446 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
447 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
448 // macros. When the operation completes, call FLOW_END. An async operation can
449 // span threads and processes, but all events in that operation must use the
450 // same |name| and |id|. Each event can have its own args.
451 #define TRACE_EVENT_FLOW_BEGIN0(category, name, id) \
452 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
453 category, name, id, TRACE_EVENT_FLAG_NONE)
454 #define TRACE_EVENT_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
455 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
456 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
457 #define TRACE_EVENT_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
458 arg2_name, arg2_val) \
459 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
460 category, name, id, TRACE_EVENT_FLAG_NONE, \
461 arg1_name, arg1_val, arg2_name, arg2_val)
462 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category, name, id) \
463 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
464 category, name, id, TRACE_EVENT_FLAG_COPY)
465 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
466 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
467 category, name, id, TRACE_EVENT_FLAG_COPY, \
468 arg1_name, arg1_val)
469 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
470 arg2_name, arg2_val) \
471 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
472 category, name, id, TRACE_EVENT_FLAG_COPY, \
473 arg1_name, arg1_val, arg2_name, arg2_val)
474
475 // Records a single FLOW_STEP event for |step| immediately. If the category
476 // is not enabled, then this does nothing. The |name| and |id| must match the
477 // FLOW_BEGIN event above. The |step| param identifies this step within the
478 // async event. This should be called at the beginning of the next phase of an
479 // asynchronous operation.
480 #define TRACE_EVENT_FLOW_STEP0(category, name, id, step) \
481 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
482 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
483 #define TRACE_EVENT_FLOW_STEP1(category, name, id, step, \
484 arg1_name, arg1_val) \
485 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
486 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
487 arg1_name, arg1_val)
488 #define TRACE_EVENT_COPY_FLOW_STEP0(category, name, id, step) \
489 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
490 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
491 #define TRACE_EVENT_COPY_FLOW_STEP1(category, name, id, step, \
492 arg1_name, arg1_val) \
493 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
494 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
495 arg1_name, arg1_val)
496
497 // Records a single FLOW_END event for "name" immediately. If the category
498 // is not enabled, then this does nothing.
499 #define TRACE_EVENT_FLOW_END0(category, name, id) \
500 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
501 category, name, id, TRACE_EVENT_FLAG_NONE)
502 #define TRACE_EVENT_FLOW_END1(category, name, id, arg1_name, arg1_val) \
503 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
504 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
505 #define TRACE_EVENT_FLOW_END2(category, name, id, arg1_name, arg1_val, \
506 arg2_name, arg2_val) \
507 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
508 category, name, id, TRACE_EVENT_FLAG_NONE, \
509 arg1_name, arg1_val, arg2_name, arg2_val)
510 #define TRACE_EVENT_COPY_FLOW_END0(category, name, id) \
511 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
512 category, name, id, TRACE_EVENT_FLAG_COPY)
513 #define TRACE_EVENT_COPY_FLOW_END1(category, name, id, arg1_name, arg1_val) \
514 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
515 category, name, id, TRACE_EVENT_FLAG_COPY, \
516 arg1_name, arg1_val)
517 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \
518 arg2_name, arg2_val) \
519 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
520 category, name, id, TRACE_EVENT_FLAG_COPY, \
521 arg1_name, arg1_val, arg2_name, arg2_val)
522
523
524 ////////////////////////////////////////////////////////////////////////////////
525 // Implementation specific tracing API definitions.
526
527 // Get a pointer to the enabled state of the given trace category. Only
528 // long-lived literal strings should be given as the category name. The returned
529 // pointer can be held permanently in a local static for example. If the
530 // unsigned char is non-zero, tracing is enabled. If tracing is enabled,
531 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
532 // between the load of the tracing state and the call to
533 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
534 // for best performance when tracing is disabled.
535 // const unsigned char*
536 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name)
537 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \
538 webrtc::EventTracer::GetCategoryEnabled
539
540 // Add a trace event to the platform tracing system.
541 // void TRACE_EVENT_API_ADD_TRACE_EVENT(
542 // char phase,
543 // const unsigned char* category_enabled,
544 // const char* name,
545 // unsigned long long id,
546 // int num_args,
547 // const char** arg_names,
548 // const unsigned char* arg_types,
549 // const unsigned long long* arg_values,
550 // unsigned char flags)
551 #define TRACE_EVENT_API_ADD_TRACE_EVENT webrtc::EventTracer::AddTraceEvent
552
553 ////////////////////////////////////////////////////////////////////////////////
554
555 // Implementation detail: trace event macros create temporary variables
556 // to keep instrumentation overhead low. These macros give each temporary
557 // variable a unique name based on the line number to prevent name collissions.
558 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
559 trace_event_unique_##a##b
560 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
561 INTERNAL_TRACE_EVENT_UID3(a,b)
562 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
563 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
564
565 #if WEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS
566 #define INTERNAL_TRACE_EVENT_INFO_TYPE const unsigned char*
567 #else
568 #define INTERNAL_TRACE_EVENT_INFO_TYPE static const unsigned char*
569 #endif // WEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS
570
571 // Implementation detail: internal macro to create static category.
572 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \
573 INTERNAL_TRACE_EVENT_INFO_TYPE INTERNAL_TRACE_EVENT_UID(catstatic) = \
574 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category);
575
576 // Implementation detail: internal macro to create static category and add
577 // event if the category is enabled.
578 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \
579 do { \
580 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
581 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
582 webrtc::trace_event_internal::AddTraceEvent( \
583 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \
584 webrtc::trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
585 } \
586 } while (0)
587
588 // Implementation detail: internal macro to create static category and add begin
589 // event if the category is enabled. Also adds the end event when the scope
590 // ends.
591 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \
592 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
593 webrtc::trace_event_internal::TraceEndOnScopeClose \
594 INTERNAL_TRACE_EVENT_UID(profileScope); \
595 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
596 webrtc::trace_event_internal::AddTraceEvent( \
597 TRACE_EVENT_PHASE_BEGIN, \
598 INTERNAL_TRACE_EVENT_UID(catstatic), \
599 name, webrtc::trace_event_internal::kNoEventId, \
600 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
601 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \
602 INTERNAL_TRACE_EVENT_UID(catstatic), name); \
603 }
604
605 // Implementation detail: internal macro to create static category and add
606 // event if the category is enabled.
607 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \
608 ...) \
609 do { \
610 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
611 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
612 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
613 webrtc::trace_event_internal::TraceID trace_event_trace_id( \
614 id, &trace_event_flags); \
615 webrtc::trace_event_internal::AddTraceEvent( \
616 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \
617 name, trace_event_trace_id.data(), trace_event_flags, \
618 ##__VA_ARGS__); \
619 } \
620 } while (0)
621
622 // Notes regarding the following definitions:
623 // New values can be added and propagated to third party libraries, but existing
624 // definitions must never be changed, because third party libraries may use old
625 // definitions.
626
627 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
628 #define TRACE_EVENT_PHASE_BEGIN ('B')
629 #define TRACE_EVENT_PHASE_END ('E')
630 #define TRACE_EVENT_PHASE_INSTANT ('I')
631 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
632 #define TRACE_EVENT_PHASE_ASYNC_STEP ('T')
633 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
634 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
635 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
636 #define TRACE_EVENT_PHASE_FLOW_END ('f')
637 #define TRACE_EVENT_PHASE_METADATA ('M')
638 #define TRACE_EVENT_PHASE_COUNTER ('C')
639
640 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
641 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
642 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
643 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
644 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
645
646 namespace webrtc {
647 namespace trace_event_internal {
648
649 // Specify these values when the corresponding argument of AddTraceEvent is not
650 // used.
651 const int kZeroNumArgs = 0;
652 const unsigned long long kNoEventId = 0;
653
654 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
655 // are mangled with the Process ID so that they are unlikely to collide when the
656 // same pointer is used on different processes.
657 class TraceID {
658 public:
659 class ForceMangle {
660 public:
ForceMangle(unsigned long long id)661 explicit ForceMangle(unsigned long long id) : data_(id) {}
ForceMangle(unsigned long id)662 explicit ForceMangle(unsigned long id) : data_(id) {}
ForceMangle(unsigned int id)663 explicit ForceMangle(unsigned int id) : data_(id) {}
ForceMangle(unsigned short id)664 explicit ForceMangle(unsigned short id) : data_(id) {}
ForceMangle(unsigned char id)665 explicit ForceMangle(unsigned char id) : data_(id) {}
ForceMangle(long long id)666 explicit ForceMangle(long long id)
667 : data_(static_cast<unsigned long long>(id)) {}
ForceMangle(long id)668 explicit ForceMangle(long id)
669 : data_(static_cast<unsigned long long>(id)) {}
ForceMangle(int id)670 explicit ForceMangle(int id)
671 : data_(static_cast<unsigned long long>(id)) {}
ForceMangle(short id)672 explicit ForceMangle(short id)
673 : data_(static_cast<unsigned long long>(id)) {}
ForceMangle(signed char id)674 explicit ForceMangle(signed char id)
675 : data_(static_cast<unsigned long long>(id)) {}
676
data()677 unsigned long long data() const { return data_; }
678
679 private:
680 unsigned long long data_;
681 };
682
TraceID(const void * id,unsigned char * flags)683 explicit TraceID(const void* id, unsigned char* flags)
684 : data_(static_cast<unsigned long long>(
685 reinterpret_cast<uintptr_t>(id))) {
686 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
687 }
TraceID(ForceMangle id,unsigned char * flags)688 explicit TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
689 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
690 }
TraceID(unsigned long long id,unsigned char * flags)691 explicit TraceID(unsigned long long id, unsigned char* flags)
692 : data_(id) { (void)flags; }
TraceID(unsigned long id,unsigned char * flags)693 explicit TraceID(unsigned long id, unsigned char* flags)
694 : data_(id) { (void)flags; }
TraceID(unsigned int id,unsigned char * flags)695 explicit TraceID(unsigned int id, unsigned char* flags)
696 : data_(id) { (void)flags; }
TraceID(unsigned short id,unsigned char * flags)697 explicit TraceID(unsigned short id, unsigned char* flags)
698 : data_(id) { (void)flags; }
TraceID(unsigned char id,unsigned char * flags)699 explicit TraceID(unsigned char id, unsigned char* flags)
700 : data_(id) { (void)flags; }
TraceID(long long id,unsigned char * flags)701 explicit TraceID(long long id, unsigned char* flags)
702 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(long id,unsigned char * flags)703 explicit TraceID(long id, unsigned char* flags)
704 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(int id,unsigned char * flags)705 explicit TraceID(int id, unsigned char* flags)
706 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(short id,unsigned char * flags)707 explicit TraceID(short id, unsigned char* flags)
708 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(signed char id,unsigned char * flags)709 explicit TraceID(signed char id, unsigned char* flags)
710 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
711
data()712 unsigned long long data() const { return data_; }
713
714 private:
715 unsigned long long data_;
716 };
717
718 // Simple union to store various types as unsigned long long.
719 union TraceValueUnion {
720 bool as_bool;
721 unsigned long long as_uint;
722 long long as_int;
723 double as_double;
724 const void* as_pointer;
725 const char* as_string;
726 };
727
728 // Simple container for const char* that should be copied instead of retained.
729 class TraceStringWithCopy {
730 public:
TraceStringWithCopy(const char * str)731 explicit TraceStringWithCopy(const char* str) : str_(str) {}
732 operator const char* () const { return str_; }
733 private:
734 const char* str_;
735 };
736
737 // Define SetTraceValue for each allowed type. It stores the type and
738 // value in the return arguments. This allows this API to avoid declaring any
739 // structures so that it is portable to third_party libraries.
740 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
741 union_member, \
742 value_type_id) \
743 static inline void SetTraceValue(actual_type arg, \
744 unsigned char* type, \
745 unsigned long long* value) { \
746 TraceValueUnion type_value; \
747 type_value.union_member = arg; \
748 *type = value_type_id; \
749 *value = type_value.as_uint; \
750 }
751 // Simpler form for int types that can be safely casted.
752 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
753 value_type_id) \
754 static inline void SetTraceValue(actual_type arg, \
755 unsigned char* type, \
756 unsigned long long* value) { \
757 *type = value_type_id; \
758 *value = static_cast<unsigned long long>(arg); \
759 }
760
INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long,TRACE_VALUE_TYPE_UINT)761 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
762 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
763 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
764 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
765 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
766 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
767 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
768 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
769 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
770 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
771 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
772 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
773 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer,
774 TRACE_VALUE_TYPE_POINTER)
775 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string,
776 TRACE_VALUE_TYPE_STRING)
777 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
778 TRACE_VALUE_TYPE_COPY_STRING)
779
780 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
781 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
782
783 // std::string version of SetTraceValue so that trace arguments can be strings.
784 static inline void SetTraceValue(const std::string& arg,
785 unsigned char* type,
786 unsigned long long* value) {
787 TraceValueUnion type_value;
788 type_value.as_string = arg.c_str();
789 *type = TRACE_VALUE_TYPE_COPY_STRING;
790 *value = type_value.as_uint;
791 }
792
793 // These AddTraceEvent template functions are defined here instead of in the
794 // macro, because the arg_values could be temporary objects, such as
795 // std::string. In order to store pointers to the internal c_str and pass
796 // through to the tracing API, the arg_values must live throughout
797 // these procedures.
798
AddTraceEvent(char phase,const unsigned char * category_enabled,const char * name,unsigned long long id,unsigned char flags)799 static inline void AddTraceEvent(char phase,
800 const unsigned char* category_enabled,
801 const char* name,
802 unsigned long long id,
803 unsigned char flags) {
804 TRACE_EVENT_API_ADD_TRACE_EVENT(phase, category_enabled, name, id,
805 kZeroNumArgs, nullptr, nullptr, nullptr,
806 flags);
807 }
808
809 template<class ARG1_TYPE>
AddTraceEvent(char phase,const unsigned char * category_enabled,const char * name,unsigned long long id,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val)810 static inline void AddTraceEvent(char phase,
811 const unsigned char* category_enabled,
812 const char* name,
813 unsigned long long id,
814 unsigned char flags,
815 const char* arg1_name,
816 const ARG1_TYPE& arg1_val) {
817 const int num_args = 1;
818 unsigned char arg_types[1];
819 unsigned long long arg_values[1];
820 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
821 TRACE_EVENT_API_ADD_TRACE_EVENT(
822 phase, category_enabled, name, id,
823 num_args, &arg1_name, arg_types, arg_values,
824 flags);
825 }
826
827 template<class ARG1_TYPE, class ARG2_TYPE>
AddTraceEvent(char phase,const unsigned char * category_enabled,const char * name,unsigned long long id,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val,const char * arg2_name,const ARG2_TYPE & arg2_val)828 static inline void AddTraceEvent(char phase,
829 const unsigned char* category_enabled,
830 const char* name,
831 unsigned long long id,
832 unsigned char flags,
833 const char* arg1_name,
834 const ARG1_TYPE& arg1_val,
835 const char* arg2_name,
836 const ARG2_TYPE& arg2_val) {
837 const int num_args = 2;
838 const char* arg_names[2] = { arg1_name, arg2_name };
839 unsigned char arg_types[2];
840 unsigned long long arg_values[2];
841 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
842 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
843 TRACE_EVENT_API_ADD_TRACE_EVENT(
844 phase, category_enabled, name, id,
845 num_args, arg_names, arg_types, arg_values,
846 flags);
847 }
848
849 // Used by TRACE_EVENTx macro. Do not use directly.
850 class TraceEndOnScopeClose {
851 public:
852 // Note: members of data_ intentionally left uninitialized. See Initialize.
TraceEndOnScopeClose()853 TraceEndOnScopeClose() : p_data_(nullptr) {}
~TraceEndOnScopeClose()854 ~TraceEndOnScopeClose() {
855 if (p_data_)
856 AddEventIfEnabled();
857 }
858
Initialize(const unsigned char * category_enabled,const char * name)859 void Initialize(const unsigned char* category_enabled,
860 const char* name) {
861 data_.category_enabled = category_enabled;
862 data_.name = name;
863 p_data_ = &data_;
864 }
865
866 private:
867 // Add the end event if the category is still enabled.
AddEventIfEnabled()868 void AddEventIfEnabled() {
869 // Only called when p_data_ is non-null.
870 if (*p_data_->category_enabled) {
871 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_END,
872 p_data_->category_enabled, p_data_->name,
873 kNoEventId, kZeroNumArgs, nullptr,
874 nullptr, nullptr, TRACE_EVENT_FLAG_NONE);
875 }
876 }
877
878 // This Data struct workaround is to avoid initializing all the members
879 // in Data during construction of this object, since this object is always
880 // constructed, even when tracing is disabled. If the members of Data were
881 // members of this class instead, compiler warnings occur about potential
882 // uninitialized accesses.
883 struct Data {
884 const unsigned char* category_enabled;
885 const char* name;
886 };
887 Data* p_data_;
888 Data data_;
889 };
890
891 } // namespace trace_event_internal
892 } // namespace webrtc
893 #else
894
895 ////////////////////////////////////////////////////////////////////////////////
896 // This section defines no-op alternatives to the tracing macros when
897 // RTC_DISABLE_TRACE_EVENTS is defined.
898
899 #define RTC_NOOP() do {} while (0)
900
901 #define TRACE_STR_COPY(str) RTC_NOOP()
902
903 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name
904
905 #define TRACE_ID_MANGLE(id) 0
906
907 #define TRACE_EVENT0(category, name) RTC_NOOP()
908 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) RTC_NOOP()
909 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \
910 RTC_NOOP()
911
912 #define TRACE_EVENT_INSTANT0(category, name) RTC_NOOP()
913 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) RTC_NOOP()
914
915 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
916 arg2_name, arg2_val) RTC_NOOP()
917
918 #define TRACE_EVENT_COPY_INSTANT0(category, name) RTC_NOOP()
919 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \
920 RTC_NOOP()
921 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \
922 arg2_name, arg2_val) RTC_NOOP()
923
924 #define TRACE_EVENT_BEGIN0(category, name) RTC_NOOP()
925 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) RTC_NOOP()
926 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \
927 arg2_name, arg2_val) RTC_NOOP()
928 #define TRACE_EVENT_COPY_BEGIN0(category, name) RTC_NOOP()
929 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) RTC_NOOP()
930 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \
931 arg2_name, arg2_val) RTC_NOOP()
932
933 #define TRACE_EVENT_END0(category, name) RTC_NOOP()
934 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) RTC_NOOP()
935 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \
936 arg2_name, arg2_val) RTC_NOOP()
937 #define TRACE_EVENT_COPY_END0(category, name) RTC_NOOP()
938 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) RTC_NOOP()
939 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \
940 arg2_name, arg2_val) RTC_NOOP()
941
942 #define TRACE_COUNTER1(category, name, value) RTC_NOOP()
943 #define TRACE_COPY_COUNTER1(category, name, value) RTC_NOOP()
944
945 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \
946 value2_name, value2_val) RTC_NOOP()
947 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \
948 value2_name, value2_val) RTC_NOOP()
949
950 #define TRACE_COUNTER_ID1(category, name, id, value) RTC_NOOP()
951 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) RTC_NOOP()
952
953 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \
954 value2_name, value2_val) RTC_NOOP()
955 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \
956 value2_name, value2_val) RTC_NOOP()
957
958 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) RTC_NOOP()
959 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
960 RTC_NOOP()
961 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
962 arg2_name, arg2_val) RTC_NOOP()
963 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) RTC_NOOP()
964 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
965 RTC_NOOP()
966 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
967 arg2_name, arg2_val) RTC_NOOP()
968
969 #define TRACE_EVENT_ASYNC_STEP0(category, name, id, step) RTC_NOOP()
970 #define TRACE_EVENT_ASYNC_STEP1(category, name, id, step, \
971 arg1_name, arg1_val) RTC_NOOP()
972 #define TRACE_EVENT_COPY_ASYNC_STEP0(category, name, id, step) RTC_NOOP()
973 #define TRACE_EVENT_COPY_ASYNC_STEP1(category, name, id, step, \
974 arg1_name, arg1_val) RTC_NOOP()
975
976 #define TRACE_EVENT_ASYNC_END0(category, name, id) RTC_NOOP()
977 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
978 RTC_NOOP()
979 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
980 arg2_name, arg2_val) RTC_NOOP()
981 #define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) RTC_NOOP()
982 #define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
983 RTC_NOOP()
984 #define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
985 arg2_name, arg2_val) RTC_NOOP()
986
987 #define TRACE_EVENT_FLOW_BEGIN0(category, name, id) RTC_NOOP()
988 #define TRACE_EVENT_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
989 RTC_NOOP()
990 #define TRACE_EVENT_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
991 arg2_name, arg2_val) RTC_NOOP()
992 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category, name, id) RTC_NOOP()
993 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
994 RTC_NOOP()
995 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
996 arg2_name, arg2_val) RTC_NOOP()
997
998 #define TRACE_EVENT_FLOW_STEP0(category, name, id, step) RTC_NOOP()
999 #define TRACE_EVENT_FLOW_STEP1(category, name, id, step, \
1000 arg1_name, arg1_val) RTC_NOOP()
1001 #define TRACE_EVENT_COPY_FLOW_STEP0(category, name, id, step) RTC_NOOP()
1002 #define TRACE_EVENT_COPY_FLOW_STEP1(category, name, id, step, \
1003 arg1_name, arg1_val) RTC_NOOP()
1004
1005 #define TRACE_EVENT_FLOW_END0(category, name, id) RTC_NOOP()
1006 #define TRACE_EVENT_FLOW_END1(category, name, id, arg1_name, arg1_val) \
1007 RTC_NOOP()
1008 #define TRACE_EVENT_FLOW_END2(category, name, id, arg1_name, arg1_val, \
1009 arg2_name, arg2_val) RTC_NOOP()
1010 #define TRACE_EVENT_COPY_FLOW_END0(category, name, id) RTC_NOOP()
1011 #define TRACE_EVENT_COPY_FLOW_END1(category, name, id, arg1_name, arg1_val) \
1012 RTC_NOOP()
1013 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \
1014 arg2_name, arg2_val) RTC_NOOP()
1015
1016 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED ""
1017
1018 #define TRACE_EVENT_API_ADD_TRACE_EVENT RTC_NOOP()
1019
1020 #endif // RTC_TRACE_EVENTS_ENABLED
1021
1022 #endif // RTC_BASE_TRACE_EVENT_H_