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