blob: fee9a7f7665873467c696091be3cebf7e91a8f45 [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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200171// Records a single event called "name" immediately, with 0, 1 or 2
172// associated arguments. If the category is not enabled, then this
173// does nothing.
174// - category and name strings must have application lifetime (statics or
175// literals). They may not include " chars.
176#define TRACE_EVENT_INSTANT0(category, name) \
177 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
178 category, name, TRACE_EVENT_FLAG_NONE)
179#define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
180 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
181 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
182#define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
183 arg2_name, arg2_val) \
184 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
185 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
186 arg2_name, arg2_val)
187#define TRACE_EVENT_COPY_INSTANT0(category, name) \
188 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
189 category, name, TRACE_EVENT_FLAG_COPY)
190#define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \
191 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
192 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
193#define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \
194 arg2_name, arg2_val) \
195 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
196 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
197 arg2_name, arg2_val)
198
199// Records a single BEGIN event called "name" immediately, with 0, 1 or 2
200// associated arguments. If the category is not enabled, then this
201// does nothing.
202// - category and name strings must have application lifetime (statics or
203// literals). They may not include " chars.
204#define TRACE_EVENT_BEGIN0(category, name) \
205 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
206 category, name, TRACE_EVENT_FLAG_NONE)
207#define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \
208 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
209 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
210#define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \
211 arg2_name, arg2_val) \
212 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
213 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
214 arg2_name, arg2_val)
215#define TRACE_EVENT_COPY_BEGIN0(category, name) \
216 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
217 category, name, TRACE_EVENT_FLAG_COPY)
218#define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \
219 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
220 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
221#define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \
222 arg2_name, arg2_val) \
223 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
224 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
225 arg2_name, arg2_val)
226
227// Records a single END event for "name" immediately. If the category
228// is not enabled, then this does nothing.
229// - category and name strings must have application lifetime (statics or
230// literals). They may not include " chars.
231#define TRACE_EVENT_END0(category, name) \
232 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
233 category, name, TRACE_EVENT_FLAG_NONE)
234#define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \
235 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
236 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
237#define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \
238 arg2_name, arg2_val) \
239 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
240 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
241 arg2_name, arg2_val)
242#define TRACE_EVENT_COPY_END0(category, name) \
243 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
244 category, name, TRACE_EVENT_FLAG_COPY)
245#define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \
246 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
247 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
248#define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \
249 arg2_name, arg2_val) \
250 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
251 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
252 arg2_name, arg2_val)
253
254// Records the value of a counter called "name" immediately. Value
255// must be representable as a 32 bit integer.
256// - category and name strings must have application lifetime (statics or
257// literals). They may not include " chars.
258#define TRACE_COUNTER1(category, name, value) \
259 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
260 category, name, TRACE_EVENT_FLAG_NONE, \
261 "value", static_cast<int>(value))
262#define TRACE_COPY_COUNTER1(category, name, value) \
263 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
264 category, name, TRACE_EVENT_FLAG_COPY, \
265 "value", static_cast<int>(value))
266
267// Records the values of a multi-parted counter called "name" immediately.
268// The UI will treat value1 and value2 as parts of a whole, displaying their
269// values as a stacked-bar chart.
270// - category and name strings must have application lifetime (statics or
271// literals). They may not include " chars.
272#define TRACE_COUNTER2(category, name, value1_name, value1_val, \
273 value2_name, value2_val) \
274 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
275 category, name, TRACE_EVENT_FLAG_NONE, \
276 value1_name, static_cast<int>(value1_val), \
277 value2_name, static_cast<int>(value2_val))
278#define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \
279 value2_name, value2_val) \
280 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
281 category, name, TRACE_EVENT_FLAG_COPY, \
282 value1_name, static_cast<int>(value1_val), \
283 value2_name, static_cast<int>(value2_val))
284
285// Records the value of a counter called "name" immediately. Value
286// must be representable as a 32 bit integer.
287// - category and name strings must have application lifetime (statics or
288// literals). They may not include " chars.
289// - |id| is used to disambiguate counters with the same name. It must either
290// be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
291// will be xored with a hash of the process ID so that the same pointer on
292// two different processes will not collide.
293#define TRACE_COUNTER_ID1(category, name, id, value) \
294 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
295 category, name, id, TRACE_EVENT_FLAG_NONE, \
296 "value", static_cast<int>(value))
297#define TRACE_COPY_COUNTER_ID1(category, name, id, value) \
298 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
299 category, name, id, TRACE_EVENT_FLAG_COPY, \
300 "value", static_cast<int>(value))
301
302// Records the values of a multi-parted counter called "name" immediately.
303// The UI will treat value1 and value2 as parts of a whole, displaying their
304// values as a stacked-bar chart.
305// - category and name strings must have application lifetime (statics or
306// literals). They may not include " chars.
307// - |id| is used to disambiguate counters with the same name. It must either
308// be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
309// will be xored with a hash of the process ID so that the same pointer on
310// two different processes will not collide.
311#define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \
312 value2_name, value2_val) \
313 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
314 category, name, id, TRACE_EVENT_FLAG_NONE, \
315 value1_name, static_cast<int>(value1_val), \
316 value2_name, static_cast<int>(value2_val))
317#define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \
318 value2_name, value2_val) \
319 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
320 category, name, id, TRACE_EVENT_FLAG_COPY, \
321 value1_name, static_cast<int>(value1_val), \
322 value2_name, static_cast<int>(value2_val))
323
324
325// Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
326// associated arguments. If the category is not enabled, then this
327// does nothing.
328// - category and name strings must have application lifetime (statics or
329// literals). They may not include " chars.
330// - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
331// events are considered to match if their category, name and id values all
332// match. |id| must either be a pointer or an integer value up to 64 bits. If
333// it's a pointer, the bits will be xored with a hash of the process ID so
334// that the same pointer on two different processes will not collide.
335// An asynchronous operation can consist of multiple phases. The first phase is
336// defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
337// ASYNC_STEP macros. When the operation completes, call ASYNC_END.
338// An ASYNC trace typically occur on a single thread (if not, they will only be
339// drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
340// operation must use the same |name| and |id|. Each event can have its own
341// args.
342#define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \
343 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
344 category, name, id, TRACE_EVENT_FLAG_NONE)
345#define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
346 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
347 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
348#define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
349 arg2_name, arg2_val) \
350 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
351 category, name, id, TRACE_EVENT_FLAG_NONE, \
352 arg1_name, arg1_val, arg2_name, arg2_val)
353#define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \
354 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
355 category, name, id, TRACE_EVENT_FLAG_COPY)
356#define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
357 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
358 category, name, id, TRACE_EVENT_FLAG_COPY, \
359 arg1_name, arg1_val)
360#define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
361 arg2_name, arg2_val) \
362 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
363 category, name, id, TRACE_EVENT_FLAG_COPY, \
364 arg1_name, arg1_val, arg2_name, arg2_val)
365
366// Records a single ASYNC_STEP event for |step| immediately. If the category
367// is not enabled, then this does nothing. The |name| and |id| must match the
368// ASYNC_BEGIN event above. The |step| param identifies this step within the
369// async event. This should be called at the beginning of the next phase of an
370// asynchronous operation.
371#define TRACE_EVENT_ASYNC_STEP0(category, name, id, step) \
372 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
373 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
374#define TRACE_EVENT_ASYNC_STEP1(category, name, id, step, \
375 arg1_name, arg1_val) \
376 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
377 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
378 arg1_name, arg1_val)
379#define TRACE_EVENT_COPY_ASYNC_STEP0(category, name, id, step) \
380 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
381 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
382#define TRACE_EVENT_COPY_ASYNC_STEP1(category, name, id, step, \
383 arg1_name, arg1_val) \
384 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
385 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
386 arg1_name, arg1_val)
387
388// Records a single ASYNC_END event for "name" immediately. If the category
389// is not enabled, then this does nothing.
390#define TRACE_EVENT_ASYNC_END0(category, name, id) \
391 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
392 category, name, id, TRACE_EVENT_FLAG_NONE)
393#define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
394 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
395 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
396#define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
397 arg2_name, arg2_val) \
398 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
399 category, name, id, TRACE_EVENT_FLAG_NONE, \
400 arg1_name, arg1_val, arg2_name, arg2_val)
401#define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \
402 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
403 category, name, id, TRACE_EVENT_FLAG_COPY)
404#define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
405 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
406 category, name, id, TRACE_EVENT_FLAG_COPY, \
407 arg1_name, arg1_val)
408#define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
409 arg2_name, arg2_val) \
410 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
411 category, name, id, TRACE_EVENT_FLAG_COPY, \
412 arg1_name, arg1_val, arg2_name, arg2_val)
413
414
415// Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
416// associated arguments. If the category is not enabled, then this
417// does nothing.
418// - category and name strings must have application lifetime (statics or
419// literals). They may not include " chars.
420// - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
421// events are considered to match if their category, name and id values all
422// match. |id| must either be a pointer or an integer value up to 64 bits. If
423// it's a pointer, the bits will be xored with a hash of the process ID so
424// that the same pointer on two different processes will not collide.
425// FLOW events are different from ASYNC events in how they are drawn by the
426// tracing UI. A FLOW defines asynchronous data flow, such as posting a task
427// (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
428// drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
429// to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
430// by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
431// macros. When the operation completes, call FLOW_END. An async operation can
432// span threads and processes, but all events in that operation must use the
433// same |name| and |id|. Each event can have its own args.
434#define TRACE_EVENT_FLOW_BEGIN0(category, name, id) \
435 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
436 category, name, id, TRACE_EVENT_FLAG_NONE)
437#define TRACE_EVENT_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
438 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
439 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
440#define TRACE_EVENT_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
441 arg2_name, arg2_val) \
442 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
443 category, name, id, TRACE_EVENT_FLAG_NONE, \
444 arg1_name, arg1_val, arg2_name, arg2_val)
445#define TRACE_EVENT_COPY_FLOW_BEGIN0(category, name, id) \
446 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
447 category, name, id, TRACE_EVENT_FLAG_COPY)
448#define TRACE_EVENT_COPY_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
449 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
450 category, name, id, TRACE_EVENT_FLAG_COPY, \
451 arg1_name, arg1_val)
452#define TRACE_EVENT_COPY_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
453 arg2_name, arg2_val) \
454 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
455 category, name, id, TRACE_EVENT_FLAG_COPY, \
456 arg1_name, arg1_val, arg2_name, arg2_val)
457
458// Records a single FLOW_STEP event for |step| immediately. If the category
459// is not enabled, then this does nothing. The |name| and |id| must match the
460// FLOW_BEGIN event above. The |step| param identifies this step within the
461// async event. This should be called at the beginning of the next phase of an
462// asynchronous operation.
463#define TRACE_EVENT_FLOW_STEP0(category, name, id, step) \
464 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
465 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
466#define TRACE_EVENT_FLOW_STEP1(category, name, id, step, \
467 arg1_name, arg1_val) \
468 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
469 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
470 arg1_name, arg1_val)
471#define TRACE_EVENT_COPY_FLOW_STEP0(category, name, id, step) \
472 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
473 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
474#define TRACE_EVENT_COPY_FLOW_STEP1(category, name, id, step, \
475 arg1_name, arg1_val) \
476 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
477 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
478 arg1_name, arg1_val)
479
480// Records a single FLOW_END event for "name" immediately. If the category
481// is not enabled, then this does nothing.
482#define TRACE_EVENT_FLOW_END0(category, name, id) \
483 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
484 category, name, id, TRACE_EVENT_FLAG_NONE)
485#define TRACE_EVENT_FLOW_END1(category, name, id, arg1_name, arg1_val) \
486 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
487 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
488#define TRACE_EVENT_FLOW_END2(category, name, id, arg1_name, arg1_val, \
489 arg2_name, arg2_val) \
490 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
491 category, name, id, TRACE_EVENT_FLAG_NONE, \
492 arg1_name, arg1_val, arg2_name, arg2_val)
493#define TRACE_EVENT_COPY_FLOW_END0(category, name, id) \
494 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
495 category, name, id, TRACE_EVENT_FLAG_COPY)
496#define TRACE_EVENT_COPY_FLOW_END1(category, name, id, arg1_name, arg1_val) \
497 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
498 category, name, id, TRACE_EVENT_FLAG_COPY, \
499 arg1_name, arg1_val)
500#define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \
501 arg2_name, arg2_val) \
502 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
503 category, name, id, TRACE_EVENT_FLAG_COPY, \
504 arg1_name, arg1_val, arg2_name, arg2_val)
505
506
507////////////////////////////////////////////////////////////////////////////////
508// Implementation specific tracing API definitions.
509
510// Get a pointer to the enabled state of the given trace category. Only
511// long-lived literal strings should be given as the category name. The returned
512// pointer can be held permanently in a local static for example. If the
513// unsigned char is non-zero, tracing is enabled. If tracing is enabled,
514// TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
515// between the load of the tracing state and the call to
516// TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
517// for best performance when tracing is disabled.
518// const unsigned char*
519// TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name)
520#define TRACE_EVENT_API_GET_CATEGORY_ENABLED \
521 webrtc::EventTracer::GetCategoryEnabled
522
523// Add a trace event to the platform tracing system.
524// void TRACE_EVENT_API_ADD_TRACE_EVENT(
525// char phase,
526// const unsigned char* category_enabled,
527// const char* name,
528// unsigned long long id,
529// int num_args,
530// const char** arg_names,
531// const unsigned char* arg_types,
532// const unsigned long long* arg_values,
533// unsigned char flags)
534#define TRACE_EVENT_API_ADD_TRACE_EVENT webrtc::EventTracer::AddTraceEvent
535
536////////////////////////////////////////////////////////////////////////////////
537
538// Implementation detail: trace event macros create temporary variables
539// to keep instrumentation overhead low. These macros give each temporary
540// variable a unique name based on the line number to prevent name collissions.
541#define INTERNAL_TRACE_EVENT_UID3(a,b) \
542 trace_event_unique_##a##b
543#define INTERNAL_TRACE_EVENT_UID2(a,b) \
544 INTERNAL_TRACE_EVENT_UID3(a,b)
545#define INTERNAL_TRACE_EVENT_UID(name_prefix) \
546 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
547
ehmaldonado481c5492017-08-17 07:59:54 -0700548#if WEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS
549#define INTERNAL_TRACE_EVENT_INFO_TYPE const unsigned char*
550#else
551#define INTERNAL_TRACE_EVENT_INFO_TYPE static const unsigned char*
552#endif // WEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS
553
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200554// Implementation detail: internal macro to create static category.
555#define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \
ehmaldonado481c5492017-08-17 07:59:54 -0700556 INTERNAL_TRACE_EVENT_INFO_TYPE INTERNAL_TRACE_EVENT_UID(catstatic) = \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200557 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category);
558
559// Implementation detail: internal macro to create static category and add
560// event if the category is enabled.
561#define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \
562 do { \
563 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
564 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
565 webrtc::trace_event_internal::AddTraceEvent( \
566 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \
567 webrtc::trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
568 } \
569 } while (0)
570
571// Implementation detail: internal macro to create static category and add begin
572// event if the category is enabled. Also adds the end event when the scope
573// ends.
574#define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \
575 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
576 webrtc::trace_event_internal::TraceEndOnScopeClose \
577 INTERNAL_TRACE_EVENT_UID(profileScope); \
578 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
579 webrtc::trace_event_internal::AddTraceEvent( \
580 TRACE_EVENT_PHASE_BEGIN, \
581 INTERNAL_TRACE_EVENT_UID(catstatic), \
582 name, webrtc::trace_event_internal::kNoEventId, \
583 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
584 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \
585 INTERNAL_TRACE_EVENT_UID(catstatic), name); \
586 }
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_WITH_ID(phase, category, name, id, flags, \
591 ...) \
592 do { \
593 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
594 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
595 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
596 webrtc::trace_event_internal::TraceID trace_event_trace_id( \
597 id, &trace_event_flags); \
598 webrtc::trace_event_internal::AddTraceEvent( \
599 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \
600 name, trace_event_trace_id.data(), trace_event_flags, \
601 ##__VA_ARGS__); \
602 } \
603 } while (0)
604
605// Notes regarding the following definitions:
606// New values can be added and propagated to third party libraries, but existing
607// definitions must never be changed, because third party libraries may use old
608// definitions.
609
610// Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
611#define TRACE_EVENT_PHASE_BEGIN ('B')
612#define TRACE_EVENT_PHASE_END ('E')
613#define TRACE_EVENT_PHASE_INSTANT ('I')
614#define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
615#define TRACE_EVENT_PHASE_ASYNC_STEP ('T')
616#define TRACE_EVENT_PHASE_ASYNC_END ('F')
617#define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
618#define TRACE_EVENT_PHASE_FLOW_STEP ('t')
619#define TRACE_EVENT_PHASE_FLOW_END ('f')
620#define TRACE_EVENT_PHASE_METADATA ('M')
621#define TRACE_EVENT_PHASE_COUNTER ('C')
622
623// Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
624#define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
625#define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
626#define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
627#define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
628
629// Type values for identifying types in the TraceValue union.
630#define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
631#define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
632#define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
633#define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
634#define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
635#define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
636#define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
637
638namespace webrtc {
639namespace trace_event_internal {
640
641// Specify these values when the corresponding argument of AddTraceEvent is not
642// used.
643const int kZeroNumArgs = 0;
644const unsigned long long kNoEventId = 0;
645
646// TraceID encapsulates an ID that can either be an integer or pointer. Pointers
647// are mangled with the Process ID so that they are unlikely to collide when the
648// same pointer is used on different processes.
649class TraceID {
650 public:
651 class ForceMangle {
652 public:
653 explicit ForceMangle(unsigned long long id) : data_(id) {}
654 explicit ForceMangle(unsigned long id) : data_(id) {}
655 explicit ForceMangle(unsigned int id) : data_(id) {}
656 explicit ForceMangle(unsigned short id) : data_(id) {}
657 explicit ForceMangle(unsigned char id) : data_(id) {}
658 explicit ForceMangle(long long id)
659 : data_(static_cast<unsigned long long>(id)) {}
660 explicit ForceMangle(long id)
661 : data_(static_cast<unsigned long long>(id)) {}
662 explicit ForceMangle(int id)
663 : data_(static_cast<unsigned long long>(id)) {}
664 explicit ForceMangle(short id)
665 : data_(static_cast<unsigned long long>(id)) {}
666 explicit ForceMangle(signed char id)
667 : data_(static_cast<unsigned long long>(id)) {}
668
669 unsigned long long data() const { return data_; }
670
671 private:
672 unsigned long long data_;
673 };
674
675 explicit TraceID(const void* id, unsigned char* flags)
676 : data_(static_cast<unsigned long long>(
677 reinterpret_cast<uintptr_t>(id))) {
678 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
679 }
680 explicit TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
681 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
682 }
683 explicit TraceID(unsigned long long id, unsigned char* flags)
684 : data_(id) { (void)flags; }
685 explicit TraceID(unsigned long id, unsigned char* flags)
686 : data_(id) { (void)flags; }
687 explicit TraceID(unsigned int id, unsigned char* flags)
688 : data_(id) { (void)flags; }
689 explicit TraceID(unsigned short id, unsigned char* flags)
690 : data_(id) { (void)flags; }
691 explicit TraceID(unsigned char id, unsigned char* flags)
692 : data_(id) { (void)flags; }
693 explicit TraceID(long long id, unsigned char* flags)
694 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
695 explicit TraceID(long id, unsigned char* flags)
696 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
697 explicit TraceID(int id, unsigned char* flags)
698 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
699 explicit TraceID(short id, unsigned char* flags)
700 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
701 explicit TraceID(signed char id, unsigned char* flags)
702 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
703
704 unsigned long long data() const { return data_; }
705
706 private:
707 unsigned long long data_;
708};
709
710// Simple union to store various types as unsigned long long.
711union TraceValueUnion {
712 bool as_bool;
713 unsigned long long as_uint;
714 long long as_int;
715 double as_double;
716 const void* as_pointer;
717 const char* as_string;
718};
719
720// Simple container for const char* that should be copied instead of retained.
721class TraceStringWithCopy {
722 public:
723 explicit TraceStringWithCopy(const char* str) : str_(str) {}
724 operator const char* () const { return str_; }
725 private:
726 const char* str_;
727};
728
729// Define SetTraceValue for each allowed type. It stores the type and
730// value in the return arguments. This allows this API to avoid declaring any
731// structures so that it is portable to third_party libraries.
732#define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
733 union_member, \
734 value_type_id) \
735 static inline void SetTraceValue(actual_type arg, \
736 unsigned char* type, \
737 unsigned long long* value) { \
738 TraceValueUnion type_value; \
739 type_value.union_member = arg; \
740 *type = value_type_id; \
741 *value = type_value.as_uint; \
742 }
743// Simpler form for int types that can be safely casted.
744#define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
745 value_type_id) \
746 static inline void SetTraceValue(actual_type arg, \
747 unsigned char* type, \
748 unsigned long long* value) { \
749 *type = value_type_id; \
750 *value = static_cast<unsigned long long>(arg); \
751 }
752
753INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
754INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
755INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
756INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
757INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
758INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
759INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
760INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
761INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
762INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
763INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
764INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
765INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer,
766 TRACE_VALUE_TYPE_POINTER)
767INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string,
768 TRACE_VALUE_TYPE_STRING)
769INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
770 TRACE_VALUE_TYPE_COPY_STRING)
771
772#undef INTERNAL_DECLARE_SET_TRACE_VALUE
773#undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
774
775// std::string version of SetTraceValue so that trace arguments can be strings.
776static inline void SetTraceValue(const std::string& arg,
777 unsigned char* type,
778 unsigned long long* value) {
779 TraceValueUnion type_value;
780 type_value.as_string = arg.c_str();
781 *type = TRACE_VALUE_TYPE_COPY_STRING;
782 *value = type_value.as_uint;
783}
784
785// These AddTraceEvent template functions are defined here instead of in the
786// macro, because the arg_values could be temporary objects, such as
787// std::string. In order to store pointers to the internal c_str and pass
788// through to the tracing API, the arg_values must live throughout
789// these procedures.
790
791static inline void AddTraceEvent(char phase,
792 const unsigned char* category_enabled,
793 const char* name,
794 unsigned long long id,
795 unsigned char flags) {
796 TRACE_EVENT_API_ADD_TRACE_EVENT(phase, category_enabled, name, id,
797 kZeroNumArgs, nullptr, nullptr, nullptr,
798 flags);
799}
800
801template<class ARG1_TYPE>
802static inline void AddTraceEvent(char phase,
803 const unsigned char* category_enabled,
804 const char* name,
805 unsigned long long id,
806 unsigned char flags,
807 const char* arg1_name,
808 const ARG1_TYPE& arg1_val) {
809 const int num_args = 1;
810 unsigned char arg_types[1];
811 unsigned long long arg_values[1];
812 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
813 TRACE_EVENT_API_ADD_TRACE_EVENT(
814 phase, category_enabled, name, id,
815 num_args, &arg1_name, arg_types, arg_values,
816 flags);
817}
818
819template<class ARG1_TYPE, class ARG2_TYPE>
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 const char* arg1_name,
826 const ARG1_TYPE& arg1_val,
827 const char* arg2_name,
828 const ARG2_TYPE& arg2_val) {
829 const int num_args = 2;
830 const char* arg_names[2] = { arg1_name, arg2_name };
831 unsigned char arg_types[2];
832 unsigned long long arg_values[2];
833 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
834 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
835 TRACE_EVENT_API_ADD_TRACE_EVENT(
836 phase, category_enabled, name, id,
837 num_args, arg_names, arg_types, arg_values,
838 flags);
839}
840
841// Used by TRACE_EVENTx macro. Do not use directly.
842class TraceEndOnScopeClose {
843 public:
844 // Note: members of data_ intentionally left uninitialized. See Initialize.
845 TraceEndOnScopeClose() : p_data_(nullptr) {}
846 ~TraceEndOnScopeClose() {
847 if (p_data_)
848 AddEventIfEnabled();
849 }
850
851 void Initialize(const unsigned char* category_enabled,
852 const char* name) {
853 data_.category_enabled = category_enabled;
854 data_.name = name;
855 p_data_ = &data_;
856 }
857
858 private:
859 // Add the end event if the category is still enabled.
860 void AddEventIfEnabled() {
861 // Only called when p_data_ is non-null.
862 if (*p_data_->category_enabled) {
863 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_END,
864 p_data_->category_enabled, p_data_->name,
865 kNoEventId, kZeroNumArgs, nullptr,
866 nullptr, nullptr, TRACE_EVENT_FLAG_NONE);
867 }
868 }
869
870 // This Data struct workaround is to avoid initializing all the members
871 // in Data during construction of this object, since this object is always
872 // constructed, even when tracing is disabled. If the members of Data were
873 // members of this class instead, compiler warnings occur about potential
874 // uninitialized accesses.
875 struct Data {
876 const unsigned char* category_enabled;
877 const char* name;
878 };
879 Data* p_data_;
880 Data data_;
881};
882
883} // namespace trace_event_internal
884} // namespace webrtc
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +0000885
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200886#endif // RTC_BASE_TRACE_EVENT_H_