blob: eecf4f6e92c2cfba55f4c264f9f559785449effc [file] [log] [blame]
hclam@chromium.orgad7efa62012-12-11 21:19:08 +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
6#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_EVENT_H_
7#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_EVENT_H_
8
9#include <string>
10
hclam@chromium.orgad7efa62012-12-11 21:19:08 +000011#include "webrtc/system_wrappers/interface/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// 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//
31// Events are issued against categories. Whereas LOG's
32// 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
144
145// By default, const char* argument values are assumed to have long-lived scope
146// and will not be copied. Use this macro to force a const char* to be copied.
147#define TRACE_STR_COPY(str) \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000148 webrtc::trace_event_internal::TraceStringWithCopy(str)
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000149
150// By default, uint64 ID argument values are not mangled with the Process ID in
151// TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
152#define TRACE_ID_MANGLE(id) \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000153 webrtc::trace_event_internal::TraceID::ForceMangle(id)
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000154
155// Records a pair of begin and end events called "name" for the current
156// scope, with 0, 1 or 2 associated arguments. If the category is not
157// enabled, then this does nothing.
158// - category and name strings must have application lifetime (statics or
159// literals). They may not include " chars.
160#define TRACE_EVENT0(category, name) \
161 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name)
162#define TRACE_EVENT1(category, name, arg1_name, arg1_val) \
163 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val)
164#define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \
165 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \
166 arg2_name, arg2_val)
167
168// Same as TRACE_EVENT except that they are not included in official builds.
169#ifdef OFFICIAL_BUILD
170#define UNSHIPPED_TRACE_EVENT0(category, name) (void)0
171#define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) (void)0
172#define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \
173 arg2_name, arg2_val) (void)0
174#define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) (void)0
175#define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
176 (void)0
177#define UNSHIPPED_TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
178 arg2_name, arg2_val) (void)0
179#else
180#define UNSHIPPED_TRACE_EVENT0(category, name) \
181 TRACE_EVENT0(category, name)
182#define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) \
183 TRACE_EVENT1(category, name, arg1_name, arg1_val)
184#define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \
185 arg2_name, arg2_val) \
186 TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val)
187#define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) \
188 TRACE_EVENT_INSTANT0(category, name)
189#define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
190 TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val)
191#define UNSHIPPED_TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
192 arg2_name, arg2_val) \
193 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
194 arg2_name, arg2_val)
195#endif
196
197// Records a single event called "name" immediately, with 0, 1 or 2
198// associated arguments. If the category is not enabled, then this
199// does nothing.
200// - category and name strings must have application lifetime (statics or
201// literals). They may not include " chars.
202#define TRACE_EVENT_INSTANT0(category, name) \
203 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
204 category, name, TRACE_EVENT_FLAG_NONE)
205#define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
206 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
207 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
208#define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
209 arg2_name, arg2_val) \
210 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
211 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
212 arg2_name, arg2_val)
213#define TRACE_EVENT_COPY_INSTANT0(category, name) \
214 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
215 category, name, TRACE_EVENT_FLAG_COPY)
216#define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \
217 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
218 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
219#define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \
220 arg2_name, arg2_val) \
221 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
222 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
223 arg2_name, arg2_val)
224
225// Records a single BEGIN event called "name" immediately, with 0, 1 or 2
226// associated arguments. If the category is not enabled, then this
227// does nothing.
228// - category and name strings must have application lifetime (statics or
229// literals). They may not include " chars.
230#define TRACE_EVENT_BEGIN0(category, name) \
231 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
232 category, name, TRACE_EVENT_FLAG_NONE)
233#define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \
234 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
235 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
236#define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \
237 arg2_name, arg2_val) \
238 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
239 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
240 arg2_name, arg2_val)
241#define TRACE_EVENT_COPY_BEGIN0(category, name) \
242 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
243 category, name, TRACE_EVENT_FLAG_COPY)
244#define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \
245 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
246 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
247#define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \
248 arg2_name, arg2_val) \
249 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
250 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
251 arg2_name, arg2_val)
252
253// Records a single END event for "name" immediately. If the category
254// is not enabled, then this does nothing.
255// - category and name strings must have application lifetime (statics or
256// literals). They may not include " chars.
257#define TRACE_EVENT_END0(category, name) \
258 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
259 category, name, TRACE_EVENT_FLAG_NONE)
260#define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \
261 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
262 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
263#define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \
264 arg2_name, arg2_val) \
265 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
266 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
267 arg2_name, arg2_val)
268#define TRACE_EVENT_COPY_END0(category, name) \
269 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
270 category, name, TRACE_EVENT_FLAG_COPY)
271#define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \
272 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
273 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
274#define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \
275 arg2_name, arg2_val) \
276 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
277 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
278 arg2_name, arg2_val)
279
280// Time threshold event:
281// Only record the event if the duration is greater than the specified
282// threshold_us (time in microseconds).
283// Records a pair of begin and end events called "name" for the current
284// scope, with 0, 1 or 2 associated arguments. If the category is not
285// enabled, then this does nothing.
286// - category and name strings must have application lifetime (statics or
287// literals). They may not include " chars.
288#define TRACE_EVENT_IF_LONGER_THAN0(threshold_us, category, name) \
289 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold_us, category, name)
290#define TRACE_EVENT_IF_LONGER_THAN1( \
291 threshold_us, category, name, arg1_name, arg1_val) \
292 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \
293 threshold_us, category, name, arg1_name, arg1_val)
294#define TRACE_EVENT_IF_LONGER_THAN2( \
295 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \
296 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \
297 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val)
298
299// Records the value of a counter called "name" immediately. Value
300// must be representable as a 32 bit integer.
301// - category and name strings must have application lifetime (statics or
302// literals). They may not include " chars.
303#define TRACE_COUNTER1(category, name, value) \
304 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
305 category, name, TRACE_EVENT_FLAG_NONE, \
306 "value", static_cast<int>(value))
307#define TRACE_COPY_COUNTER1(category, name, value) \
308 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
309 category, name, TRACE_EVENT_FLAG_COPY, \
310 "value", static_cast<int>(value))
311
312// Records the values of a multi-parted counter called "name" immediately.
313// The UI will treat value1 and value2 as parts of a whole, displaying their
314// values as a stacked-bar chart.
315// - category and name strings must have application lifetime (statics or
316// literals). They may not include " chars.
317#define TRACE_COUNTER2(category, name, value1_name, value1_val, \
318 value2_name, value2_val) \
319 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
320 category, name, TRACE_EVENT_FLAG_NONE, \
321 value1_name, static_cast<int>(value1_val), \
322 value2_name, static_cast<int>(value2_val))
323#define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \
324 value2_name, value2_val) \
325 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
326 category, name, TRACE_EVENT_FLAG_COPY, \
327 value1_name, static_cast<int>(value1_val), \
328 value2_name, static_cast<int>(value2_val))
329
330// Records the value of a counter called "name" immediately. Value
331// must be representable as a 32 bit integer.
332// - category and name strings must have application lifetime (statics or
333// literals). They may not include " chars.
334// - |id| is used to disambiguate counters with the same name. It must either
335// be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
336// will be xored with a hash of the process ID so that the same pointer on
337// two different processes will not collide.
338#define TRACE_COUNTER_ID1(category, name, id, value) \
339 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
340 category, name, id, TRACE_EVENT_FLAG_NONE, \
341 "value", static_cast<int>(value))
342#define TRACE_COPY_COUNTER_ID1(category, name, id, value) \
343 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
344 category, name, id, TRACE_EVENT_FLAG_COPY, \
345 "value", static_cast<int>(value))
346
347// Records the values of a multi-parted counter called "name" immediately.
348// The UI will treat value1 and value2 as parts of a whole, displaying their
349// values as a stacked-bar chart.
350// - category and name strings must have application lifetime (statics or
351// literals). They may not include " chars.
352// - |id| is used to disambiguate counters with the same name. It must either
353// be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
354// will be xored with a hash of the process ID so that the same pointer on
355// two different processes will not collide.
356#define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \
357 value2_name, value2_val) \
358 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
359 category, name, id, TRACE_EVENT_FLAG_NONE, \
360 value1_name, static_cast<int>(value1_val), \
361 value2_name, static_cast<int>(value2_val))
362#define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \
363 value2_name, value2_val) \
364 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
365 category, name, id, TRACE_EVENT_FLAG_COPY, \
366 value1_name, static_cast<int>(value1_val), \
367 value2_name, static_cast<int>(value2_val))
368
369
370// Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
371// associated arguments. If the category is not enabled, then this
372// does nothing.
373// - category and name strings must have application lifetime (statics or
374// literals). They may not include " chars.
375// - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
376// events are considered to match if their category, name and id values all
377// match. |id| must either be a pointer or an integer value up to 64 bits. If
378// it's a pointer, the bits will be xored with a hash of the process ID so
379// that the same pointer on two different processes will not collide.
380// An asynchronous operation can consist of multiple phases. The first phase is
381// defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
382// ASYNC_STEP macros. When the operation completes, call ASYNC_END.
383// An ASYNC trace typically occur on a single thread (if not, they will only be
384// drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
385// operation must use the same |name| and |id|. Each event can have its own
386// args.
387#define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \
388 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
389 category, name, id, TRACE_EVENT_FLAG_NONE)
390#define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
391 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
392 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
393#define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
394 arg2_name, arg2_val) \
395 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
396 category, name, id, TRACE_EVENT_FLAG_NONE, \
397 arg1_name, arg1_val, arg2_name, arg2_val)
398#define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \
399 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
400 category, name, id, TRACE_EVENT_FLAG_COPY)
401#define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
402 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
403 category, name, id, TRACE_EVENT_FLAG_COPY, \
404 arg1_name, arg1_val)
405#define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
406 arg2_name, arg2_val) \
407 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
408 category, name, id, TRACE_EVENT_FLAG_COPY, \
409 arg1_name, arg1_val, arg2_name, arg2_val)
410
411// Records a single ASYNC_STEP event for |step| immediately. If the category
412// is not enabled, then this does nothing. The |name| and |id| must match the
413// ASYNC_BEGIN event above. The |step| param identifies this step within the
414// async event. This should be called at the beginning of the next phase of an
415// asynchronous operation.
416#define TRACE_EVENT_ASYNC_STEP0(category, name, id, step) \
417 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
418 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
419#define TRACE_EVENT_ASYNC_STEP1(category, name, id, step, \
420 arg1_name, arg1_val) \
421 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
422 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
423 arg1_name, arg1_val)
424#define TRACE_EVENT_COPY_ASYNC_STEP0(category, name, id, step) \
425 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
426 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
427#define TRACE_EVENT_COPY_ASYNC_STEP1(category, name, id, step, \
428 arg1_name, arg1_val) \
429 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
430 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
431 arg1_name, arg1_val)
432
433// Records a single ASYNC_END event for "name" immediately. If the category
434// is not enabled, then this does nothing.
435#define TRACE_EVENT_ASYNC_END0(category, name, id) \
436 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
437 category, name, id, TRACE_EVENT_FLAG_NONE)
438#define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
439 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
440 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
441#define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
442 arg2_name, arg2_val) \
443 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
444 category, name, id, TRACE_EVENT_FLAG_NONE, \
445 arg1_name, arg1_val, arg2_name, arg2_val)
446#define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \
447 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
448 category, name, id, TRACE_EVENT_FLAG_COPY)
449#define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
450 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
451 category, name, id, TRACE_EVENT_FLAG_COPY, \
452 arg1_name, arg1_val)
453#define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
454 arg2_name, arg2_val) \
455 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
456 category, name, id, TRACE_EVENT_FLAG_COPY, \
457 arg1_name, arg1_val, arg2_name, arg2_val)
458
459
460// Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
461// associated arguments. If the category is not enabled, then this
462// does nothing.
463// - category and name strings must have application lifetime (statics or
464// literals). They may not include " chars.
465// - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
466// events are considered to match if their category, name and id values all
467// match. |id| must either be a pointer or an integer value up to 64 bits. If
468// it's a pointer, the bits will be xored with a hash of the process ID so
469// that the same pointer on two different processes will not collide.
470// FLOW events are different from ASYNC events in how they are drawn by the
471// tracing UI. A FLOW defines asynchronous data flow, such as posting a task
472// (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
473// drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
474// to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
475// by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
476// macros. When the operation completes, call FLOW_END. An async operation can
477// span threads and processes, but all events in that operation must use the
478// same |name| and |id|. Each event can have its own args.
479#define TRACE_EVENT_FLOW_BEGIN0(category, name, id) \
480 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
481 category, name, id, TRACE_EVENT_FLAG_NONE)
482#define TRACE_EVENT_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
483 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
484 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
485#define TRACE_EVENT_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
486 arg2_name, arg2_val) \
487 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
488 category, name, id, TRACE_EVENT_FLAG_NONE, \
489 arg1_name, arg1_val, arg2_name, arg2_val)
490#define TRACE_EVENT_COPY_FLOW_BEGIN0(category, name, id) \
491 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
492 category, name, id, TRACE_EVENT_FLAG_COPY)
493#define TRACE_EVENT_COPY_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
494 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
495 category, name, id, TRACE_EVENT_FLAG_COPY, \
496 arg1_name, arg1_val)
497#define TRACE_EVENT_COPY_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
498 arg2_name, arg2_val) \
499 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
500 category, name, id, TRACE_EVENT_FLAG_COPY, \
501 arg1_name, arg1_val, arg2_name, arg2_val)
502
503// Records a single FLOW_STEP event for |step| immediately. If the category
504// is not enabled, then this does nothing. The |name| and |id| must match the
505// FLOW_BEGIN event above. The |step| param identifies this step within the
506// async event. This should be called at the beginning of the next phase of an
507// asynchronous operation.
508#define TRACE_EVENT_FLOW_STEP0(category, name, id, step) \
509 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
510 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
511#define TRACE_EVENT_FLOW_STEP1(category, name, id, step, \
512 arg1_name, arg1_val) \
513 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
514 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
515 arg1_name, arg1_val)
516#define TRACE_EVENT_COPY_FLOW_STEP0(category, name, id, step) \
517 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
518 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
519#define TRACE_EVENT_COPY_FLOW_STEP1(category, name, id, step, \
520 arg1_name, arg1_val) \
521 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
522 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
523 arg1_name, arg1_val)
524
525// Records a single FLOW_END event for "name" immediately. If the category
526// is not enabled, then this does nothing.
527#define TRACE_EVENT_FLOW_END0(category, name, id) \
528 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
529 category, name, id, TRACE_EVENT_FLAG_NONE)
530#define TRACE_EVENT_FLOW_END1(category, name, id, arg1_name, arg1_val) \
531 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
532 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
533#define TRACE_EVENT_FLOW_END2(category, name, id, arg1_name, arg1_val, \
534 arg2_name, arg2_val) \
535 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
536 category, name, id, TRACE_EVENT_FLAG_NONE, \
537 arg1_name, arg1_val, arg2_name, arg2_val)
538#define TRACE_EVENT_COPY_FLOW_END0(category, name, id) \
539 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
540 category, name, id, TRACE_EVENT_FLAG_COPY)
541#define TRACE_EVENT_COPY_FLOW_END1(category, name, id, arg1_name, arg1_val) \
542 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
543 category, name, id, TRACE_EVENT_FLAG_COPY, \
544 arg1_name, arg1_val)
545#define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \
546 arg2_name, arg2_val) \
547 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
548 category, name, id, TRACE_EVENT_FLAG_COPY, \
549 arg1_name, arg1_val, arg2_name, arg2_val)
550
551
552////////////////////////////////////////////////////////////////////////////////
553// Implementation specific tracing API definitions.
554
555// Get a pointer to the enabled state of the given trace category. Only
556// long-lived literal strings should be given as the category name. The returned
557// pointer can be held permanently in a local static for example. If the
558// unsigned char is non-zero, tracing is enabled. If tracing is enabled,
559// TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
560// between the load of the tracing state and the call to
561// TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
562// for best performance when tracing is disabled.
563// const unsigned char*
564// TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name)
565#define TRACE_EVENT_API_GET_CATEGORY_ENABLED \
566 webrtc::EventTracer::GetCategoryEnabled
567
568// Add a trace event to the platform tracing system. Returns thresholdBeginId
569// for use in a corresponding end TRACE_EVENT_API_ADD_TRACE_EVENT call.
570// int TRACE_EVENT_API_ADD_TRACE_EVENT(
571// char phase,
572// const unsigned char* category_enabled,
573// const char* name,
574// unsigned long long id,
575// int num_args,
576// const char** arg_names,
577// const unsigned char* arg_types,
578// const unsigned long long* arg_values,
579// int threshold_begin_id,
580// long long threshold,
581// unsigned char flags)
582#define TRACE_EVENT_API_ADD_TRACE_EVENT webrtc::EventTracer::AddTraceEvent
583
584////////////////////////////////////////////////////////////////////////////////
585
586// Implementation detail: trace event macros create temporary variables
587// to keep instrumentation overhead low. These macros give each temporary
588// variable a unique name based on the line number to prevent name collissions.
589#define INTERNAL_TRACE_EVENT_UID3(a,b) \
590 trace_event_unique_##a##b
591#define INTERNAL_TRACE_EVENT_UID2(a,b) \
592 INTERNAL_TRACE_EVENT_UID3(a,b)
593#define INTERNAL_TRACE_EVENT_UID(name_prefix) \
594 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
595
596// Implementation detail: internal macro to create static category.
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000597#define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \
hclam@chromium.org4e16f252012-12-12 01:13:19 +0000598 static const unsigned char* INTERNAL_TRACE_EVENT_UID(catstatic) = 0; \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000599 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \
600 INTERNAL_TRACE_EVENT_UID(catstatic) = \
601 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000602 }
603
604// Implementation detail: internal macro to create static category and add
605// event if the category is enabled.
606#define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \
607 do { \
608 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
609 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000610 webrtc::trace_event_internal::AddTraceEvent( \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000611 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000612 webrtc::trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000613 } \
614 } while (0)
615
616// Implementation detail: internal macro to create static category and add begin
617// event if the category is enabled. Also adds the end event when the scope
618// ends.
619#define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \
620 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000621 webrtc::trace_event_internal::TraceEndOnScopeClose \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000622 INTERNAL_TRACE_EVENT_UID(profileScope); \
623 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000624 webrtc::trace_event_internal::AddTraceEvent( \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000625 TRACE_EVENT_PHASE_BEGIN, \
626 INTERNAL_TRACE_EVENT_UID(catstatic), \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000627 name, webrtc::trace_event_internal::kNoEventId, \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000628 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
629 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \
630 INTERNAL_TRACE_EVENT_UID(catstatic), name); \
631 }
632
633// Implementation detail: internal macro to create static category and add begin
634// event if the category is enabled. Also adds the end event when the scope
635// ends. If the elapsed time is < threshold time, the begin/end pair is erased.
636#define INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold, \
637 category, name, ...) \
638 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000639 webrtc::trace_event_internal::TraceEndOnScopeCloseThreshold \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000640 INTERNAL_TRACE_EVENT_UID(profileScope); \
641 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
642 int INTERNAL_TRACE_EVENT_UID(begin_event_id) = \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000643 webrtc::trace_event_internal::AddTraceEvent( \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000644 TRACE_EVENT_PHASE_BEGIN, \
645 INTERNAL_TRACE_EVENT_UID(catstatic), \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000646 name, webrtc::trace_event_internal::kNoEventId, \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000647 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
648 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \
649 INTERNAL_TRACE_EVENT_UID(catstatic), name, \
650 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \
651 }
652
653// Implementation detail: internal macro to create static category and add
654// event if the category is enabled.
655#define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \
656 ...) \
657 do { \
658 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
659 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
660 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000661 webrtc::trace_event_internal::TraceID trace_event_trace_id( \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000662 id, &trace_event_flags); \
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000663 webrtc::trace_event_internal::AddTraceEvent( \
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000664 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \
665 name, trace_event_trace_id.data(), trace_event_flags, \
666 ##__VA_ARGS__); \
667 } \
668 } while (0)
669
670// Notes regarding the following definitions:
671// New values can be added and propagated to third party libraries, but existing
672// definitions must never be changed, because third party libraries may use old
673// definitions.
674
675// Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
676#define TRACE_EVENT_PHASE_BEGIN ('B')
677#define TRACE_EVENT_PHASE_END ('E')
678#define TRACE_EVENT_PHASE_INSTANT ('I')
679#define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
680#define TRACE_EVENT_PHASE_ASYNC_STEP ('T')
681#define TRACE_EVENT_PHASE_ASYNC_END ('F')
682#define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
683#define TRACE_EVENT_PHASE_FLOW_STEP ('t')
684#define TRACE_EVENT_PHASE_FLOW_END ('f')
685#define TRACE_EVENT_PHASE_METADATA ('M')
686#define TRACE_EVENT_PHASE_COUNTER ('C')
687
688// Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
689#define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
690#define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
691#define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
692#define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
693
694// Type values for identifying types in the TraceValue union.
695#define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
696#define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
697#define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
698#define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
699#define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
700#define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
701#define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
702
hclam@chromium.org770a01e2012-12-11 22:51:38 +0000703namespace webrtc {
hclam@chromium.orgad7efa62012-12-11 21:19:08 +0000704namespace trace_event_internal {
705
706// Specify these values when the corresponding argument of AddTraceEvent is not
707// used.
708const int kZeroNumArgs = 0;
709const int kNoThreshholdBeginId = -1;
710const long long kNoThresholdValue = 0;
711const unsigned long long kNoEventId = 0;
712
713// TraceID encapsulates an ID that can either be an integer or pointer. Pointers
714// are mangled with the Process ID so that they are unlikely to collide when the
715// same pointer is used on different processes.
716class TraceID {
717 public:
718 class ForceMangle {
719 public:
720 explicit ForceMangle(unsigned long long id) : data_(id) {}
721 explicit ForceMangle(unsigned long id) : data_(id) {}
722 explicit ForceMangle(unsigned int id) : data_(id) {}
723 explicit ForceMangle(unsigned short id) : data_(id) {}
724 explicit ForceMangle(unsigned char id) : data_(id) {}
725 explicit ForceMangle(long long id)
726 : data_(static_cast<unsigned long long>(id)) {}
727 explicit ForceMangle(long id)
728 : data_(static_cast<unsigned long long>(id)) {}
729 explicit ForceMangle(int id)
730 : data_(static_cast<unsigned long long>(id)) {}
731 explicit ForceMangle(short id)
732 : data_(static_cast<unsigned long long>(id)) {}
733 explicit ForceMangle(signed char id)
734 : data_(static_cast<unsigned long long>(id)) {}
735
736 unsigned long long data() const { return data_; }
737
738 private:
739 unsigned long long data_;
740 };
741
742 explicit TraceID(const void* id, unsigned char* flags)
743 : data_(static_cast<unsigned long long>(
744 reinterpret_cast<unsigned long>(id))) {
745 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
746 }
747 explicit TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
748 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
749 }
750 explicit TraceID(unsigned long long id, unsigned char* flags)
751 : data_(id) { (void)flags; }
752 explicit TraceID(unsigned long id, unsigned char* flags)
753 : data_(id) { (void)flags; }
754 explicit TraceID(unsigned int id, unsigned char* flags)
755 : data_(id) { (void)flags; }
756 explicit TraceID(unsigned short id, unsigned char* flags)
757 : data_(id) { (void)flags; }
758 explicit TraceID(unsigned char id, unsigned char* flags)
759 : data_(id) { (void)flags; }
760 explicit TraceID(long long id, unsigned char* flags)
761 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
762 explicit TraceID(long id, unsigned char* flags)
763 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
764 explicit TraceID(int id, unsigned char* flags)
765 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
766 explicit TraceID(short id, unsigned char* flags)
767 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
768 explicit TraceID(signed char id, unsigned char* flags)
769 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
770
771 unsigned long long data() const { return data_; }
772
773 private:
774 unsigned long long data_;
775};
776
777// Simple union to store various types as unsigned long long.
778union TraceValueUnion {
779 bool as_bool;
780 unsigned long long as_uint;
781 long long as_int;
782 double as_double;
783 const void* as_pointer;
784 const char* as_string;
785};
786
787// Simple container for const char* that should be copied instead of retained.
788class TraceStringWithCopy {
789 public:
790 explicit TraceStringWithCopy(const char* str) : str_(str) {}
791 operator const char* () const { return str_; }
792 private:
793 const char* str_;
794};
795
796// Define SetTraceValue for each allowed type. It stores the type and
797// value in the return arguments. This allows this API to avoid declaring any
798// structures so that it is portable to third_party libraries.
799#define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
800 union_member, \
801 value_type_id) \
802 static inline void SetTraceValue(actual_type arg, \
803 unsigned char* type, \
804 unsigned long long* value) { \
805 TraceValueUnion type_value; \
806 type_value.union_member = arg; \
807 *type = value_type_id; \
808 *value = type_value.as_uint; \
809 }
810// Simpler form for int types that can be safely casted.
811#define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
812 value_type_id) \
813 static inline void SetTraceValue(actual_type arg, \
814 unsigned char* type, \
815 unsigned long long* value) { \
816 *type = value_type_id; \
817 *value = static_cast<unsigned long long>(arg); \
818 }
819
820INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
821INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
822INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
823INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
824INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
825INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
826INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
827INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
828INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
829INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
830INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
831INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
832INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer,
833 TRACE_VALUE_TYPE_POINTER)
834INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string,
835 TRACE_VALUE_TYPE_STRING)
836INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
837 TRACE_VALUE_TYPE_COPY_STRING)
838
839#undef INTERNAL_DECLARE_SET_TRACE_VALUE
840#undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
841
842// std::string version of SetTraceValue so that trace arguments can be strings.
843static inline void SetTraceValue(const std::string& arg,
844 unsigned char* type,
845 unsigned long long* value) {
846 TraceValueUnion type_value;
847 type_value.as_string = arg.c_str();
848 *type = TRACE_VALUE_TYPE_COPY_STRING;
849 *value = type_value.as_uint;
850}
851
852// These AddTraceEvent template functions are defined here instead of in the
853// macro, because the arg_values could be temporary objects, such as
854// std::string. In order to store pointers to the internal c_str and pass
855// through to the tracing API, the arg_values must live throughout
856// these procedures.
857
858static inline int AddTraceEvent(char phase,
859 const unsigned char* category_enabled,
860 const char* name,
861 unsigned long long id,
862 unsigned char flags) {
863 return TRACE_EVENT_API_ADD_TRACE_EVENT(
864 phase, category_enabled, name, id,
865 kZeroNumArgs, NULL, NULL, NULL,
866 kNoThreshholdBeginId, kNoThresholdValue, flags);
867}
868
869template<class ARG1_TYPE>
870static inline int AddTraceEvent(char phase,
871 const unsigned char* category_enabled,
872 const char* name,
873 unsigned long long id,
874 unsigned char flags,
875 const char* arg1_name,
876 const ARG1_TYPE& arg1_val) {
877 const int num_args = 1;
878 unsigned char arg_types[1];
879 unsigned long long arg_values[1];
880 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
881 return TRACE_EVENT_API_ADD_TRACE_EVENT(
882 phase, category_enabled, name, id,
883 num_args, &arg1_name, arg_types, arg_values,
884 kNoThreshholdBeginId, kNoThresholdValue, flags);
885}
886
887template<class ARG1_TYPE, class ARG2_TYPE>
888static inline int AddTraceEvent(char phase,
889 const unsigned char* category_enabled,
890 const char* name,
891 unsigned long long id,
892 unsigned char flags,
893 const char* arg1_name,
894 const ARG1_TYPE& arg1_val,
895 const char* arg2_name,
896 const ARG2_TYPE& arg2_val) {
897 const int num_args = 2;
898 const char* arg_names[2] = { arg1_name, arg2_name };
899 unsigned char arg_types[2];
900 unsigned long long arg_values[2];
901 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
902 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
903 return TRACE_EVENT_API_ADD_TRACE_EVENT(
904 phase, category_enabled, name, id,
905 num_args, arg_names, arg_types, arg_values,
906 kNoThreshholdBeginId, kNoThresholdValue, flags);
907}
908
909// Used by TRACE_EVENTx macro. Do not use directly.
910class TraceEndOnScopeClose {
911 public:
912 // Note: members of data_ intentionally left uninitialized. See Initialize.
913 TraceEndOnScopeClose() : p_data_(NULL) {}
914 ~TraceEndOnScopeClose() {
915 if (p_data_)
916 AddEventIfEnabled();
917 }
918
919 void Initialize(const unsigned char* category_enabled,
920 const char* name) {
921 data_.category_enabled = category_enabled;
922 data_.name = name;
923 p_data_ = &data_;
924 }
925
926 private:
927 // Add the end event if the category is still enabled.
928 void AddEventIfEnabled() {
929 // Only called when p_data_ is non-null.
930 if (*p_data_->category_enabled) {
931 TRACE_EVENT_API_ADD_TRACE_EVENT(
932 TRACE_EVENT_PHASE_END,
933 p_data_->category_enabled,
934 p_data_->name, kNoEventId,
935 kZeroNumArgs, NULL, NULL, NULL,
936 kNoThreshholdBeginId, kNoThresholdValue, TRACE_EVENT_FLAG_NONE);
937 }
938 }
939
940 // This Data struct workaround is to avoid initializing all the members
941 // in Data during construction of this object, since this object is always
942 // constructed, even when tracing is disabled. If the members of Data were
943 // members of this class instead, compiler warnings occur about potential
944 // uninitialized accesses.
945 struct Data {
946 const unsigned char* category_enabled;
947 const char* name;
948 };
949 Data* p_data_;
950 Data data_;
951};
952
953// Used by TRACE_EVENTx macro. Do not use directly.
954class TraceEndOnScopeCloseThreshold {
955 public:
956 // Note: members of data_ intentionally left uninitialized. See Initialize.
957 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {}
958 ~TraceEndOnScopeCloseThreshold() {
959 if (p_data_)
960 AddEventIfEnabled();
961 }
962
963 // Called by macros only when tracing is enabled at the point when the begin
964 // event is added.
965 void Initialize(const unsigned char* category_enabled,
966 const char* name,
967 int threshold_begin_id,
968 long long threshold) {
969 data_.category_enabled = category_enabled;
970 data_.name = name;
971 data_.threshold_begin_id = threshold_begin_id;
972 data_.threshold = threshold;
973 p_data_ = &data_;
974 }
975
976 private:
977 // Add the end event if the category is still enabled.
978 void AddEventIfEnabled() {
979 // Only called when p_data_ is non-null.
980 if (*p_data_->category_enabled) {
981 TRACE_EVENT_API_ADD_TRACE_EVENT(
982 TRACE_EVENT_PHASE_END,
983 p_data_->category_enabled,
984 p_data_->name, kNoEventId,
985 kZeroNumArgs, NULL, NULL, NULL,
986 p_data_->threshold_begin_id, p_data_->threshold,
987 TRACE_EVENT_FLAG_NONE);
988 }
989 }
990
991 // This Data struct workaround is to avoid initializing all the members
992 // in Data during construction of this object, since this object is always
993 // constructed, even when tracing is disabled. If the members of Data were
994 // members of this class instead, compiler warnings occur about potential
995 // uninitialized accesses.
996 struct Data {
997 long long threshold;
998 const unsigned char* category_enabled;
999 const char* name;
1000 int threshold_begin_id;
1001 };
1002 Data* p_data_;
1003 Data data_;
1004};
1005
1006} // namespace trace_event_internal
hclam@chromium.org770a01e2012-12-11 22:51:38 +00001007} // namespace webrtc
hclam@chromium.orgad7efa62012-12-11 21:19:08 +00001008
1009#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_EVENT_H_