1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This header file defines the set of trace_event macros without specifying
6 // how the events actually get collected and stored. If you need to expose trace
7 // events to some other universe, you can copy-and-paste this file as well as
8 // trace_event.h, modifying the macros contained there as necessary for the
9 // target platform. The end result is that multiple libraries can funnel events
10 // through to a shared trace event collector.
12 // Trace events are for tracking application performance and resource usage.
13 // Macros are provided to track:
14 // Begin and end of function calls
17 // Events are issued against categories. Whereas LOG's
18 // categories are statically defined, TRACE categories are created
19 // implicitly with a string. For example:
20 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent",
21 // TRACE_EVENT_SCOPE_THREAD)
23 // It is often the case that one trace may belong in multiple categories at the
24 // same time. The first argument to the trace can be a comma-separated list of
25 // categories, forming a category group, like:
27 // TRACE_EVENT_INSTANT0("input,views", "OnMouseOver", TRACE_EVENT_SCOPE_THREAD)
29 // We can enable/disable tracing of OnMouseOver by enabling/disabling either
32 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope:
33 // TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly")
34 // doSomethingCostly()
35 // TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly")
36 // Note: our tools can't always determine the correct BEGIN/END pairs unless
37 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you
38 // need them to be in separate scopes.
40 // A common use case is to trace entire function scopes. This
41 // issues a trace BEGIN and END automatically:
42 // void doSomethingCostly() {
43 // TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly");
47 // Additional parameters can be associated with an event:
48 // void doSomethingCostly2(int howMuch) {
49 // TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly",
50 // "howMuch", howMuch);
54 // The trace system will automatically add to this information the
55 // current process id, thread id, and a timestamp in microseconds.
57 // To trace an asynchronous procedure such as an IPC send/receive, use
58 // ASYNC_BEGIN and ASYNC_END:
59 // [single threaded sender code]
60 // static int send_count = 0;
62 // TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count);
63 // Send(new MyMessage(send_count));
65 // void OnMyMessage(send_count) {
66 // TRACE_EVENT_ASYNC_END0("ipc", "message", send_count);
68 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs.
69 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process.
70 // Pointers can be used for the ID parameter, and they will be mangled
71 // internally so that the same pointer on two different processes will not
72 // match. For example:
73 // class MyTracedClass {
76 // TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this);
79 // TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this);
83 // Trace event also supports counters, which is a way to track a quantity
84 // as it varies over time. Counters are created with the following macro:
85 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue);
87 // Counters are process-specific. The macro itself can be issued from any
90 // Sometimes, you want to track two counters at once. You can do this with two
92 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]);
93 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]);
94 // Or you can do it with a combined macro:
95 // TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter",
96 // "bytesPinned", g_myCounterValue[0],
97 // "bytesAllocated", g_myCounterValue[1]);
98 // This indicates to the tracing UI that these counters should be displayed
99 // in a single graph, as a summed area chart.
101 // Since counters are in a global namespace, you may want to disambiguate with a
102 // unique ID, by using the TRACE_COUNTER_ID* variations.
104 // By default, trace collection is compiled in, but turned off at runtime.
105 // Collecting trace data is the responsibility of the embedding
106 // application. In Chrome's case, navigating to about:tracing will turn on
107 // tracing and display data collected across all active processes.
110 // Memory scoping note:
111 // Tracing copies the pointers, not the string content, of the strings passed
112 // in for category_group, name, and arg_names. Thus, the following code will
114 // char* str = strdup("importantName");
115 // TRACE_EVENT_INSTANT0("SUBSYSTEM", str); // BAD!
116 // free(str); // Trace system now has dangling pointer
118 // To avoid this issue with the |name| and |arg_name| parameters, use the
119 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead.
120 // Notes: The category must always be in a long-lived char* (i.e. static const).
121 // The |arg_values|, when used, are always deep copied with the _COPY
124 // When are string argument values copied:
125 // const char* arg_values are only referenced by default:
126 // TRACE_EVENT1("category", "name",
127 // "arg1", "literal string is only referenced");
128 // Use TRACE_STR_COPY to force copying of a const char*:
129 // TRACE_EVENT1("category", "name",
130 // "arg1", TRACE_STR_COPY("string will be copied"));
131 // std::string arg_values are always copied:
132 // TRACE_EVENT1("category", "name",
133 // "arg1", std::string("string will be copied"));
136 // Convertible notes:
137 // Converting a large data type to a string can be costly. To help with this,
138 // the trace framework provides an interface ConvertableToTraceFormat. If you
139 // inherit from it and implement the AppendAsTraceFormat method the trace
140 // framework will call back to your object to convert a trace output time. This
141 // means, if the category for the event is disabled, the conversion will not
144 // class MyData : public base::debug::ConvertableToTraceFormat {
147 // virtual ~MyData() {}
148 // virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
149 // out->append("{\"foo\":1}");
152 // DISALLOW_COPY_AND_ASSIGN(MyData);
155 // scoped_ptr<MyData> data(new MyData());
156 // TRACE_EVENT1("foo", "bar", "data",
157 // data.PassAs<base::debug::ConvertableToTraceFormat>());
159 // The trace framework will take ownership if the passed pointer and it will
160 // be free'd when the trace buffer is flushed.
162 // Note, we only do the conversion when the buffer is flushed, so the provided
163 // data object should not be modified after it's passed to the trace framework.
167 // A thread safe singleton and mutex are used for thread safety. Category
168 // enabled flags are used to limit the performance impact when the system
171 // TRACE_EVENT macros first cache a pointer to a category. The categories are
172 // statically allocated and safe at all times, even after exit. Fetching a
173 // category is protected by the TraceLog::lock_. Multiple threads initializing
174 // the static variable is safe, as they will be serialized by the lock and
175 // multiple calls will return the same pointer to the category.
177 // Then the category_group_enabled flag is checked. This is a unsigned char, and
178 // not intended to be multithread safe. It optimizes access to AddTraceEvent
179 // which is threadsafe internally via TraceLog::lock_. The enabled flag may
180 // cause some threads to incorrectly call or skip calling AddTraceEvent near
181 // the time of the system being enabled or disabled. This is acceptable as
182 // we tolerate some data loss while the system is being enabled/disabled and
183 // because AddTraceEvent is threadsafe internally and checks the enabled state
186 // Without the use of these static category pointers and enabled flags all
187 // trace points would carry a significant performance cost of acquiring a lock
188 // and resolving the category.
190 #ifndef BASE_DEBUG_TRACE_EVENT_H_
191 #define BASE_DEBUG_TRACE_EVENT_H_
195 #include "base/atomicops.h"
196 #include "base/debug/trace_event_impl.h"
197 #include "base/debug/trace_event_memory.h"
198 #include "base/debug/trace_event_system_stats_monitor.h"
199 #include "build/build_config.h"
201 // By default, const char* argument values are assumed to have long-lived scope
202 // and will not be copied. Use this macro to force a const char* to be copied.
203 #define TRACE_STR_COPY(str) \
204 trace_event_internal::TraceStringWithCopy(str)
206 // This will mark the trace event as disabled by default. The user will need
207 // to explicitly enable the event.
208 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name
210 // By default, uint64 ID argument values are not mangled with the Process ID in
211 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
212 #define TRACE_ID_MANGLE(id) \
213 trace_event_internal::TraceID::ForceMangle(id)
215 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC
216 // macros. Use this macro to prevent Process ID mangling.
217 #define TRACE_ID_DONT_MANGLE(id) \
218 trace_event_internal::TraceID::DontMangle(id)
220 // Records a pair of begin and end events called "name" for the current
221 // scope, with 0, 1 or 2 associated arguments. If the category is not
222 // enabled, then this does nothing.
223 // - category and name strings must have application lifetime (statics or
224 // literals). They may not include " chars.
225 #define TRACE_EVENT0(category_group, name) \
226 INTERNAL_TRACE_MEMORY(category_group, name) \
227 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
228 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
229 INTERNAL_TRACE_MEMORY(category_group, name) \
230 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
231 #define TRACE_EVENT2( \
232 category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
233 INTERNAL_TRACE_MEMORY(category_group, name) \
234 INTERNAL_TRACE_EVENT_ADD_SCOPED( \
235 category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
237 // Records events like TRACE_EVENT2 but uses |memory_tag| for memory tracing.
238 // Use this where |name| is too generic to accurately aggregate allocations.
239 #define TRACE_EVENT_WITH_MEMORY_TAG2( \
240 category, name, memory_tag, arg1_name, arg1_val, arg2_name, arg2_val) \
241 INTERNAL_TRACE_MEMORY(category, memory_tag) \
242 INTERNAL_TRACE_EVENT_ADD_SCOPED( \
243 category, name, arg1_name, arg1_val, arg2_name, arg2_val)
245 // UNSHIPPED_TRACE_EVENT* are like TRACE_EVENT* except that they are not
246 // included in official builds.
249 #undef TRACING_IS_OFFICIAL_BUILD
250 #define TRACING_IS_OFFICIAL_BUILD 1
251 #elif !defined(TRACING_IS_OFFICIAL_BUILD)
252 #define TRACING_IS_OFFICIAL_BUILD 0
255 #if TRACING_IS_OFFICIAL_BUILD
256 #define UNSHIPPED_TRACE_EVENT0(category_group, name) (void)0
257 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
259 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
260 arg2_name, arg2_val) (void)0
261 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) (void)0
262 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, \
263 arg1_name, arg1_val) (void)0
264 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, \
265 arg1_name, arg1_val, \
266 arg2_name, arg2_val) (void)0
268 #define UNSHIPPED_TRACE_EVENT0(category_group, name) \
269 TRACE_EVENT0(category_group, name)
270 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
271 TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
272 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
273 arg2_name, arg2_val) \
274 TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
275 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) \
276 TRACE_EVENT_INSTANT0(category_group, name, scope)
277 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, \
278 arg1_name, arg1_val) \
279 TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val)
280 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, \
281 arg1_name, arg1_val, \
282 arg2_name, arg2_val) \
283 TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
287 // Records a single event called "name" immediately, with 0, 1 or 2
288 // associated arguments. If the category is not enabled, then this
290 // - category and name strings must have application lifetime (statics or
291 // literals). They may not include " chars.
292 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \
293 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
294 category_group, name, TRACE_EVENT_FLAG_NONE | scope)
295 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
296 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
297 category_group, name, TRACE_EVENT_FLAG_NONE | scope, \
299 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
300 arg2_name, arg2_val) \
301 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
302 category_group, name, TRACE_EVENT_FLAG_NONE | scope, \
303 arg1_name, arg1_val, arg2_name, arg2_val)
304 #define TRACE_EVENT_COPY_INSTANT0(category_group, name, scope) \
305 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
306 category_group, name, TRACE_EVENT_FLAG_COPY | scope)
307 #define TRACE_EVENT_COPY_INSTANT1(category_group, name, scope, \
308 arg1_name, arg1_val) \
309 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
310 category_group, name, TRACE_EVENT_FLAG_COPY | scope, arg1_name, \
312 #define TRACE_EVENT_COPY_INSTANT2(category_group, name, scope, \
313 arg1_name, arg1_val, \
314 arg2_name, arg2_val) \
315 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
316 category_group, name, TRACE_EVENT_FLAG_COPY | scope, \
317 arg1_name, arg1_val, arg2_name, arg2_val)
319 // Sets the current sample state to the given category and name (both must be
320 // constant strings). These states are intended for a sampling profiler.
321 // Implementation note: we store category and name together because we don't
322 // want the inconsistency/expense of storing two pointers.
323 // |thread_bucket| is [0..2] and is used to statically isolate samples in one
324 // thread from others.
325 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET( \
326 bucket_number, category, name) \
327 trace_event_internal:: \
328 TraceEventSamplingStateScope<bucket_number>::Set(category "\0" name)
330 // Returns a current sampling state of the given bucket.
331 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \
332 trace_event_internal::TraceEventSamplingStateScope<bucket_number>::Current()
334 // Creates a scope of a sampling state of the given bucket.
336 // { // The sampling state is set within this scope.
337 // TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name");
340 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET( \
341 bucket_number, category, name) \
342 trace_event_internal::TraceEventSamplingStateScope<bucket_number> \
343 traceEventSamplingScope(category "\0" name);
345 // Syntactic sugars for the sampling tracing in the main thread.
346 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \
347 TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name)
348 #define TRACE_EVENT_GET_SAMPLING_STATE() \
349 TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0)
350 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \
351 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name)
354 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
355 // associated arguments. If the category is not enabled, then this
357 // - category and name strings must have application lifetime (statics or
358 // literals). They may not include " chars.
359 #define TRACE_EVENT_BEGIN0(category_group, name) \
360 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
361 category_group, name, TRACE_EVENT_FLAG_NONE)
362 #define TRACE_EVENT_BEGIN1(category_group, name, arg1_name, arg1_val) \
363 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
364 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
365 #define TRACE_EVENT_BEGIN2(category_group, name, arg1_name, arg1_val, \
366 arg2_name, arg2_val) \
367 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
368 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
370 #define TRACE_EVENT_COPY_BEGIN0(category_group, name) \
371 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
372 category_group, name, TRACE_EVENT_FLAG_COPY)
373 #define TRACE_EVENT_COPY_BEGIN1(category_group, name, arg1_name, arg1_val) \
374 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
375 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
376 #define TRACE_EVENT_COPY_BEGIN2(category_group, name, arg1_name, arg1_val, \
377 arg2_name, arg2_val) \
378 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
379 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
382 // Similar to TRACE_EVENT_BEGINx but with a custom |at| timestamp provided.
383 // - |id| is used to match the _BEGIN event with the _END event.
384 // Events are considered to match if their category_group, name and id values
385 // all match. |id| must either be a pointer or an integer value up to 64 bits.
386 // If it's a pointer, the bits will be xored with a hash of the process ID so
387 // that the same pointer on two different processes will not collide.
388 #define TRACE_EVENT_BEGIN_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
389 name, id, thread_id, timestamp) \
390 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
391 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
392 timestamp, TRACE_EVENT_FLAG_NONE)
393 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP0( \
394 category_group, name, id, thread_id, timestamp) \
395 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
396 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
397 timestamp, TRACE_EVENT_FLAG_COPY)
399 // Records a single END event for "name" immediately. If the category
400 // is not enabled, then this does nothing.
401 // - category and name strings must have application lifetime (statics or
402 // literals). They may not include " chars.
403 #define TRACE_EVENT_END0(category_group, name) \
404 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
405 category_group, name, TRACE_EVENT_FLAG_NONE)
406 #define TRACE_EVENT_END1(category_group, name, arg1_name, arg1_val) \
407 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
408 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
409 #define TRACE_EVENT_END2(category_group, name, arg1_name, arg1_val, \
410 arg2_name, arg2_val) \
411 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
412 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
414 #define TRACE_EVENT_COPY_END0(category_group, name) \
415 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
416 category_group, name, TRACE_EVENT_FLAG_COPY)
417 #define TRACE_EVENT_COPY_END1(category_group, name, arg1_name, arg1_val) \
418 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
419 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
420 #define TRACE_EVENT_COPY_END2(category_group, name, arg1_name, arg1_val, \
421 arg2_name, arg2_val) \
422 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
423 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
426 // Similar to TRACE_EVENT_ENDx but with a custom |at| timestamp provided.
427 // - |id| is used to match the _BEGIN event with the _END event.
428 // Events are considered to match if their category_group, name and id values
429 // all match. |id| must either be a pointer or an integer value up to 64 bits.
430 // If it's a pointer, the bits will be xored with a hash of the process ID so
431 // that the same pointer on two different processes will not collide.
432 #define TRACE_EVENT_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
433 name, id, thread_id, timestamp) \
434 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
435 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
436 timestamp, TRACE_EVENT_FLAG_NONE)
437 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0( \
438 category_group, name, id, thread_id, timestamp) \
439 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
440 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
441 timestamp, TRACE_EVENT_FLAG_COPY)
443 // Records the value of a counter called "name" immediately. Value
444 // must be representable as a 32 bit integer.
445 // - category and name strings must have application lifetime (statics or
446 // literals). They may not include " chars.
447 #define TRACE_COUNTER1(category_group, name, value) \
448 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
449 category_group, name, TRACE_EVENT_FLAG_NONE, \
450 "value", static_cast<int>(value))
451 #define TRACE_COPY_COUNTER1(category_group, name, value) \
452 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
453 category_group, name, TRACE_EVENT_FLAG_COPY, \
454 "value", static_cast<int>(value))
456 // Records the values of a multi-parted counter called "name" immediately.
457 // The UI will treat value1 and value2 as parts of a whole, displaying their
458 // values as a stacked-bar chart.
459 // - category and name strings must have application lifetime (statics or
460 // literals). They may not include " chars.
461 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \
462 value2_name, value2_val) \
463 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
464 category_group, name, TRACE_EVENT_FLAG_NONE, \
465 value1_name, static_cast<int>(value1_val), \
466 value2_name, static_cast<int>(value2_val))
467 #define TRACE_COPY_COUNTER2(category_group, name, value1_name, value1_val, \
468 value2_name, value2_val) \
469 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
470 category_group, name, TRACE_EVENT_FLAG_COPY, \
471 value1_name, static_cast<int>(value1_val), \
472 value2_name, static_cast<int>(value2_val))
474 // Records the value of a counter called "name" immediately. Value
475 // must be representable as a 32 bit integer.
476 // - category and name strings must have application lifetime (statics or
477 // literals). They may not include " chars.
478 // - |id| is used to disambiguate counters with the same name. It must either
479 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
480 // will be xored with a hash of the process ID so that the same pointer on
481 // two different processes will not collide.
482 #define TRACE_COUNTER_ID1(category_group, name, id, value) \
483 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
484 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
485 "value", static_cast<int>(value))
486 #define TRACE_COPY_COUNTER_ID1(category_group, name, id, value) \
487 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
488 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
489 "value", static_cast<int>(value))
491 // Records the values of a multi-parted counter called "name" immediately.
492 // The UI will treat value1 and value2 as parts of a whole, displaying their
493 // values as a stacked-bar chart.
494 // - category and name strings must have application lifetime (statics or
495 // literals). They may not include " chars.
496 // - |id| is used to disambiguate counters with the same name. It must either
497 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
498 // will be xored with a hash of the process ID so that the same pointer on
499 // two different processes will not collide.
500 #define TRACE_COUNTER_ID2(category_group, name, id, value1_name, value1_val, \
501 value2_name, value2_val) \
502 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
503 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
504 value1_name, static_cast<int>(value1_val), \
505 value2_name, static_cast<int>(value2_val))
506 #define TRACE_COPY_COUNTER_ID2(category_group, name, id, value1_name, \
507 value1_val, value2_name, value2_val) \
508 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
509 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
510 value1_name, static_cast<int>(value1_val), \
511 value2_name, static_cast<int>(value2_val))
514 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
515 // associated arguments. If the category is not enabled, then this
517 // - category and name strings must have application lifetime (statics or
518 // literals). They may not include " chars.
519 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
520 // events are considered to match if their category_group, name and id values
521 // all match. |id| must either be a pointer or an integer value up to 64 bits.
522 // If it's a pointer, the bits will be xored with a hash of the process ID so
523 // that the same pointer on two different processes will not collide.
524 // An asynchronous operation can consist of multiple phases. The first phase is
525 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
526 // ASYNC_STEP macros. When the operation completes, call ASYNC_END.
527 // An ASYNC trace typically occur on a single thread (if not, they will only be
528 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
529 // operation must use the same |name| and |id|. Each event can have its own
531 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id) \
532 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
533 category_group, name, id, TRACE_EVENT_FLAG_NONE)
534 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
536 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
537 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
538 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
539 arg1_val, arg2_name, arg2_val) \
540 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
541 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
542 arg1_name, arg1_val, arg2_name, arg2_val)
543 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id) \
544 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
545 category_group, name, id, TRACE_EVENT_FLAG_COPY)
546 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
548 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
549 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
551 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
552 arg1_val, arg2_name, arg2_val) \
553 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
554 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
555 arg1_name, arg1_val, arg2_name, arg2_val)
557 // Records a single ASYNC_STEP event for |step| immediately. If the category
558 // is not enabled, then this does nothing. The |name| and |id| must match the
559 // ASYNC_BEGIN event above. The |step| param identifies this step within the
560 // async event. This should be called at the beginning of the next phase of an
561 // asynchronous operation.
562 #define TRACE_EVENT_ASYNC_STEP0(category_group, name, id, step) \
563 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
564 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
565 #define TRACE_EVENT_ASYNC_STEP1(category_group, name, id, step, \
566 arg1_name, arg1_val) \
567 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
568 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
571 #define TRACE_EVENT_COPY_ASYNC_STEP0(category_group, name, id, step) \
572 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
573 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
574 #define TRACE_EVENT_COPY_ASYNC_STEP1(category_group, name, id, step, \
575 arg1_name, arg1_val) \
576 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
577 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
580 // Records a single ASYNC_END event for "name" immediately. If the category
581 // is not enabled, then this does nothing.
582 #define TRACE_EVENT_ASYNC_END0(category_group, name, id) \
583 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
584 category_group, name, id, TRACE_EVENT_FLAG_NONE)
585 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
586 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
587 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
588 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
589 arg2_name, arg2_val) \
590 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
591 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
592 arg1_name, arg1_val, arg2_name, arg2_val)
593 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id) \
594 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
595 category_group, name, id, TRACE_EVENT_FLAG_COPY)
596 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
598 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
599 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
601 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
602 arg1_val, arg2_name, arg2_val) \
603 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
604 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
605 arg1_name, arg1_val, arg2_name, arg2_val)
608 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
609 // associated arguments. If the category is not enabled, then this
611 // - category and name strings must have application lifetime (statics or
612 // literals). They may not include " chars.
613 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
614 // events are considered to match if their category_group, name and id values
615 // all match. |id| must either be a pointer or an integer value up to 64 bits.
616 // If it's a pointer, the bits will be xored with a hash of the process ID so
617 // that the same pointer on two different processes will not collide.
618 // FLOW events are different from ASYNC events in how they are drawn by the
619 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
620 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
621 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
622 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
623 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
624 // macros. When the operation completes, call FLOW_END. An async operation can
625 // span threads and processes, but all events in that operation must use the
626 // same |name| and |id|. Each event can have its own args.
627 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
628 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
629 category_group, name, id, TRACE_EVENT_FLAG_NONE)
630 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
631 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
632 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
633 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
634 arg2_name, arg2_val) \
635 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
636 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
637 arg1_name, arg1_val, arg2_name, arg2_val)
638 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
639 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
640 category_group, name, id, TRACE_EVENT_FLAG_COPY)
641 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
643 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
644 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
646 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
647 arg1_val, arg2_name, arg2_val) \
648 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
649 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
650 arg1_name, arg1_val, arg2_name, arg2_val)
652 // Records a single FLOW_STEP event for |step| immediately. If the category
653 // is not enabled, then this does nothing. The |name| and |id| must match the
654 // FLOW_BEGIN event above. The |step| param identifies this step within the
655 // async event. This should be called at the beginning of the next phase of an
656 // asynchronous operation.
657 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
658 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
659 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
660 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \
661 arg1_name, arg1_val) \
662 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
663 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
665 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
666 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
667 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
668 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \
669 arg1_name, arg1_val) \
670 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
671 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
674 // Records a single FLOW_END event for "name" immediately. If the category
675 // is not enabled, then this does nothing.
676 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
677 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
678 category_group, name, id, TRACE_EVENT_FLAG_NONE)
679 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
680 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
681 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
682 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
683 arg2_name, arg2_val) \
684 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
685 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
686 arg1_name, arg1_val, arg2_name, arg2_val)
687 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
688 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
689 category_group, name, id, TRACE_EVENT_FLAG_COPY)
690 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
692 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
693 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
695 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
696 arg1_val, arg2_name, arg2_val) \
697 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
698 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
699 arg1_name, arg1_val, arg2_name, arg2_val)
701 // Macros to track the life time and value of arbitrary client objects.
702 // See also TraceTrackableObject.
703 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
704 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \
705 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
707 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
708 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \
709 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE,\
710 "snapshot", snapshot)
712 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
713 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \
714 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
717 // Macro to efficiently determine if a given category group is enabled.
718 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
720 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
721 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
728 // Macro to efficiently determine, through polling, if a new trace has begun.
729 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
731 static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
732 int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
733 if (num_traces_recorded != -1 && \
734 num_traces_recorded != \
735 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
736 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = \
737 num_traces_recorded; \
744 ////////////////////////////////////////////////////////////////////////////////
745 // Implementation specific tracing API definitions.
747 // Get a pointer to the enabled state of the given trace category. Only
748 // long-lived literal strings should be given as the category group. The
749 // returned pointer can be held permanently in a local static for example. If
750 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
751 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
752 // between the load of the tracing state and the call to
753 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
754 // for best performance when tracing is disabled.
755 // const unsigned char*
756 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
757 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
758 base::debug::TraceLog::GetCategoryGroupEnabled
760 // Get the number of times traces have been recorded. This is used to implement
761 // the TRACE_EVENT_IS_NEW_TRACE facility.
762 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
763 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
764 base::debug::TraceLog::GetInstance()->GetNumTracesRecorded
766 // Add a trace event to the platform tracing system.
767 // void TRACE_EVENT_API_ADD_TRACE_EVENT(
769 // const unsigned char* category_group_enabled,
771 // unsigned long long id,
773 // const char** arg_names,
774 // const unsigned char* arg_types,
775 // const unsigned long long* arg_values,
776 // unsigned char flags)
777 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
778 base::debug::TraceLog::GetInstance()->AddTraceEvent
780 // Add a trace event to the platform tracing system.
781 // void TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
783 // const unsigned char* category_group_enabled,
785 // unsigned long long id,
787 // const TimeTicks& timestamp,
789 // const char** arg_names,
790 // const unsigned char* arg_types,
791 // const unsigned long long* arg_values,
792 // unsigned char flags)
793 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
794 base::debug::TraceLog::GetInstance()->AddTraceEventWithThreadIdAndTimestamp
796 // Defines atomic operations used internally by the tracing system.
797 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
798 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
799 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
800 base::subtle::NoBarrier_Store(&(var), (value))
802 // Defines visibility for classes in trace_event.h
803 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
805 // The thread buckets for the sampling profiler.
806 TRACE_EVENT_API_CLASS_EXPORT
extern \
807 TRACE_EVENT_API_ATOMIC_WORD g_trace_state
[3];
809 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
810 g_trace_state[thread_bucket]
812 ////////////////////////////////////////////////////////////////////////////////
814 // Implementation detail: trace event macros create temporary variables
815 // to keep instrumentation overhead low. These macros give each temporary
816 // variable a unique name based on the line number to prevent name collisions.
817 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
818 trace_event_unique_##a##b
819 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
820 INTERNAL_TRACE_EVENT_UID3(a,b)
821 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
822 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
824 // Implementation detail: internal macro to create static category.
825 // No barriers are needed, because this code is designed to operate safely
826 // even when the unsigned char* points to garbage data (which may be the case
827 // on processors without cache coherency).
828 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
829 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
830 const unsigned char* INTERNAL_TRACE_EVENT_UID(catstatic) = \
831 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
832 INTERNAL_TRACE_EVENT_UID(atomic))); \
833 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \
834 INTERNAL_TRACE_EVENT_UID(catstatic) = \
835 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
836 TRACE_EVENT_API_ATOMIC_STORE(INTERNAL_TRACE_EVENT_UID(atomic), \
837 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
838 INTERNAL_TRACE_EVENT_UID(catstatic))); \
841 // Implementation detail: internal macro to create static category and add
842 // event if the category is enabled.
843 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
845 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
846 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
847 trace_event_internal::AddTraceEvent( \
848 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \
849 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
853 // Implementation detail: internal macro to create static category and add begin
854 // event if the category is enabled. Also adds the end event when the scope
856 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
857 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
858 trace_event_internal::TraceEndOnScopeClose \
859 INTERNAL_TRACE_EVENT_UID(profileScope); \
860 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
861 trace_event_internal::AddTraceEvent( \
862 TRACE_EVENT_PHASE_BEGIN, \
863 INTERNAL_TRACE_EVENT_UID(catstatic), \
864 name, trace_event_internal::kNoEventId, \
865 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
866 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \
867 INTERNAL_TRACE_EVENT_UID(catstatic), name); \
870 // Implementation detail: internal macro to create static category and add
871 // event if the category is enabled.
872 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
875 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
876 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
877 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
878 trace_event_internal::TraceID trace_event_trace_id( \
879 id, &trace_event_flags); \
880 trace_event_internal::AddTraceEvent( \
881 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \
882 name, trace_event_trace_id.data(), trace_event_flags, \
887 // Implementation detail: internal macro to create static category and add
888 // event if the category is enabled.
889 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
890 category_group, name, id, thread_id, timestamp, flags, ...) \
892 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
893 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
894 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
895 trace_event_internal::TraceID trace_event_trace_id( \
896 id, &trace_event_flags); \
897 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
898 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \
899 name, trace_event_trace_id.data(), \
900 thread_id, base::TimeTicks::FromInternalValue(timestamp), \
901 trace_event_flags, ##__VA_ARGS__); \
905 // Notes regarding the following definitions:
906 // New values can be added and propagated to third party libraries, but existing
907 // definitions must never be changed, because third party libraries may use old
910 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
911 #define TRACE_EVENT_PHASE_BEGIN ('B')
912 #define TRACE_EVENT_PHASE_END ('E')
913 #define TRACE_EVENT_PHASE_INSTANT ('i')
914 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
915 #define TRACE_EVENT_PHASE_ASYNC_STEP ('T')
916 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
917 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
918 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
919 #define TRACE_EVENT_PHASE_FLOW_END ('f')
920 #define TRACE_EVENT_PHASE_METADATA ('M')
921 #define TRACE_EVENT_PHASE_COUNTER ('C')
922 #define TRACE_EVENT_PHASE_SAMPLE ('P')
923 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
924 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
925 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
927 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
928 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
929 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
930 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
931 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
932 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned char>(1 << 3))
934 #define TRACE_EVENT_FLAG_SCOPE_MASK (static_cast<unsigned char>( \
935 TRACE_EVENT_FLAG_SCOPE_OFFSET | (TRACE_EVENT_FLAG_SCOPE_OFFSET << 1)))
937 // Type values for identifying types in the TraceValue union.
938 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
939 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
940 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
941 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
942 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
943 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
944 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
945 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
947 // Enum reflecting the scope of an INSTANT event. Must fit within
948 // TRACE_EVENT_FLAG_SCOPE_MASK.
949 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
950 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
951 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
953 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
954 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
955 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
957 namespace trace_event_internal
{
959 // Specify these values when the corresponding argument of AddTraceEvent is not
961 const int kZeroNumArgs
= 0;
962 const unsigned long long kNoEventId
= 0;
964 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
965 // are by default mangled with the Process ID so that they are unlikely to
966 // collide when the same pointer is used on different processes.
971 explicit DontMangle(const void* id
)
972 : data_(static_cast<unsigned long long>(
973 reinterpret_cast<unsigned long>(id
))) {}
974 explicit DontMangle(unsigned long long id
) : data_(id
) {}
975 explicit DontMangle(unsigned long id
) : data_(id
) {}
976 explicit DontMangle(unsigned int id
) : data_(id
) {}
977 explicit DontMangle(unsigned short id
) : data_(id
) {}
978 explicit DontMangle(unsigned char id
) : data_(id
) {}
979 explicit DontMangle(long long id
)
980 : data_(static_cast<unsigned long long>(id
)) {}
981 explicit DontMangle(long id
)
982 : data_(static_cast<unsigned long long>(id
)) {}
983 explicit DontMangle(int id
)
984 : data_(static_cast<unsigned long long>(id
)) {}
985 explicit DontMangle(short id
)
986 : data_(static_cast<unsigned long long>(id
)) {}
987 explicit DontMangle(signed char id
)
988 : data_(static_cast<unsigned long long>(id
)) {}
989 unsigned long long data() const { return data_
; }
991 unsigned long long data_
;
996 explicit ForceMangle(unsigned long long id
) : data_(id
) {}
997 explicit ForceMangle(unsigned long id
) : data_(id
) {}
998 explicit ForceMangle(unsigned int id
) : data_(id
) {}
999 explicit ForceMangle(unsigned short id
) : data_(id
) {}
1000 explicit ForceMangle(unsigned char id
) : data_(id
) {}
1001 explicit ForceMangle(long long id
)
1002 : data_(static_cast<unsigned long long>(id
)) {}
1003 explicit ForceMangle(long id
)
1004 : data_(static_cast<unsigned long long>(id
)) {}
1005 explicit ForceMangle(int id
)
1006 : data_(static_cast<unsigned long long>(id
)) {}
1007 explicit ForceMangle(short id
)
1008 : data_(static_cast<unsigned long long>(id
)) {}
1009 explicit ForceMangle(signed char id
)
1010 : data_(static_cast<unsigned long long>(id
)) {}
1011 unsigned long long data() const { return data_
; }
1013 unsigned long long data_
;
1016 TraceID(const void* id
, unsigned char* flags
)
1017 : data_(static_cast<unsigned long long>(
1018 reinterpret_cast<unsigned long>(id
))) {
1019 *flags
|= TRACE_EVENT_FLAG_MANGLE_ID
;
1021 TraceID(ForceMangle id
, unsigned char* flags
) : data_(id
.data()) {
1022 *flags
|= TRACE_EVENT_FLAG_MANGLE_ID
;
1024 TraceID(DontMangle id
, unsigned char* flags
) : data_(id
.data()) {
1026 TraceID(unsigned long long id
, unsigned char* flags
)
1027 : data_(id
) { (void)flags
; }
1028 TraceID(unsigned long id
, unsigned char* flags
)
1029 : data_(id
) { (void)flags
; }
1030 TraceID(unsigned int id
, unsigned char* flags
)
1031 : data_(id
) { (void)flags
; }
1032 TraceID(unsigned short id
, unsigned char* flags
)
1033 : data_(id
) { (void)flags
; }
1034 TraceID(unsigned char id
, unsigned char* flags
)
1035 : data_(id
) { (void)flags
; }
1036 TraceID(long long id
, unsigned char* flags
)
1037 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1038 TraceID(long id
, unsigned char* flags
)
1039 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1040 TraceID(int id
, unsigned char* flags
)
1041 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1042 TraceID(short id
, unsigned char* flags
)
1043 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1044 TraceID(signed char id
, unsigned char* flags
)
1045 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1047 unsigned long long data() const { return data_
; }
1050 unsigned long long data_
;
1053 // Simple union to store various types as unsigned long long.
1054 union TraceValueUnion
{
1056 unsigned long long as_uint
;
1059 const void* as_pointer
;
1060 const char* as_string
;
1063 // Simple container for const char* that should be copied instead of retained.
1064 class TraceStringWithCopy
{
1066 explicit TraceStringWithCopy(const char* str
) : str_(str
) {}
1067 operator const char* () const { return str_
; }
1072 // Define SetTraceValue for each allowed type. It stores the type and
1073 // value in the return arguments. This allows this API to avoid declaring any
1074 // structures so that it is portable to third_party libraries.
1075 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
1078 static inline void SetTraceValue( \
1080 unsigned char* type, \
1081 unsigned long long* value) { \
1082 TraceValueUnion type_value; \
1083 type_value.union_member = arg; \
1084 *type = value_type_id; \
1085 *value = type_value.as_uint; \
1087 // Simpler form for int types that can be safely casted.
1088 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
1090 static inline void SetTraceValue( \
1092 unsigned char* type, \
1093 unsigned long long* value) { \
1094 *type = value_type_id; \
1095 *value = static_cast<unsigned long long>(arg); \
1098 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT
)
1099 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT
)
1100 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT
)
1101 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT
)
1102 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT
)
1103 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT
)
1104 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT
)
1105 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT
)
1106 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT
)
1107 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT
)
1108 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool
, TRACE_VALUE_TYPE_BOOL
)
1109 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double
, TRACE_VALUE_TYPE_DOUBLE
)
1110 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer
,
1111 TRACE_VALUE_TYPE_POINTER
)
1112 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string
,
1113 TRACE_VALUE_TYPE_STRING
)
1114 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy
&, as_string
,
1115 TRACE_VALUE_TYPE_COPY_STRING
)
1117 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
1118 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
1120 // std::string version of SetTraceValue so that trace arguments can be strings.
1121 static inline void SetTraceValue(const std::string
& arg
,
1122 unsigned char* type
,
1123 unsigned long long* value
) {
1124 TraceValueUnion type_value
;
1125 type_value
.as_string
= arg
.c_str();
1126 *type
= TRACE_VALUE_TYPE_COPY_STRING
;
1127 *value
= type_value
.as_uint
;
1130 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
1131 // functions are defined here instead of in the macro, because the arg_values
1132 // could be temporary objects, such as std::string. In order to store
1133 // pointers to the internal c_str and pass through to the tracing API,
1134 // the arg_values must live throughout these procedures.
1136 static inline void AddTraceEventWithThreadIdAndTimestamp(
1138 const unsigned char* category_group_enabled
,
1140 unsigned long long id
,
1142 const base::TimeTicks
& timestamp
,
1143 unsigned char flags
,
1144 const char* arg1_name
,
1145 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg1_val
) {
1146 const int num_args
= 1;
1147 unsigned char arg_types
[1] = { TRACE_VALUE_TYPE_CONVERTABLE
};
1148 scoped_ptr
<base::debug::ConvertableToTraceFormat
> convertable_values
[1];
1149 convertable_values
[0].reset(arg1_val
.release());
1151 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1152 phase
, category_group_enabled
, name
, id
, thread_id
, timestamp
,
1153 num_args
, &arg1_name
, arg_types
, NULL
, convertable_values
, flags
);
1156 static inline void AddTraceEvent(
1158 const unsigned char* category_group_enabled
,
1160 unsigned long long id
,
1161 unsigned char flags
,
1162 const char* arg1_name
,
1163 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg1_val
) {
1164 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1165 base::TimeTicks now
= base::TimeTicks::NowFromSystemTraceTime();
1166 AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
, name
, id
,
1167 thread_id
, now
, flags
, arg1_name
,
1171 template<class ARG1_TYPE
>
1172 static inline void AddTraceEventWithThreadIdAndTimestamp(
1174 const unsigned char* category_group_enabled
,
1176 unsigned long long id
,
1178 const base::TimeTicks
& timestamp
,
1179 unsigned char flags
,
1180 const char* arg1_name
,
1182 const char* arg2_name
,
1183 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg2_val
) {
1184 const int num_args
= 2;
1185 const char* arg_names
[2] = { arg1_name
, arg2_name
};
1187 unsigned char arg_types
[2];
1188 unsigned long long arg_values
[2];
1189 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
1190 arg_types
[1] = TRACE_VALUE_TYPE_CONVERTABLE
;
1192 scoped_ptr
<base::debug::ConvertableToTraceFormat
> convertable_values
[2];
1193 convertable_values
[1].reset(arg2_val
.release());
1195 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1196 phase
, category_group_enabled
, name
, id
, thread_id
, timestamp
,
1197 num_args
, arg_names
, arg_types
, arg_values
, convertable_values
, flags
);
1200 template<class ARG1_TYPE
>
1201 static inline void AddTraceEvent(
1203 const unsigned char* category_group_enabled
,
1205 unsigned long long id
,
1206 unsigned char flags
,
1207 const char* arg1_name
,
1209 const char* arg2_name
,
1210 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg2_val
) {
1211 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1212 base::TimeTicks now
= base::TimeTicks::NowFromSystemTraceTime();
1213 AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
, name
, id
,
1214 thread_id
, now
, flags
,
1215 arg1_name
, arg1_val
,
1216 arg2_name
, arg2_val
.Pass());
1219 template<class ARG2_TYPE
>
1220 static inline void AddTraceEventWithThreadIdAndTimestamp(
1222 const unsigned char* category_group_enabled
,
1224 unsigned long long id
,
1226 const base::TimeTicks
& timestamp
,
1227 unsigned char flags
,
1228 const char* arg1_name
,
1229 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg1_val
,
1230 const char* arg2_name
,
1231 ARG2_TYPE arg2_val
) {
1232 const int num_args
= 2;
1233 const char* arg_names
[2] = { arg1_name
, arg2_name
};
1235 unsigned char arg_types
[2];
1236 unsigned long long arg_values
[2];
1237 arg_types
[0] = TRACE_VALUE_TYPE_CONVERTABLE
;
1239 SetTraceValue(arg2_val
, &arg_types
[1], &arg_values
[1]);
1241 scoped_ptr
<base::debug::ConvertableToTraceFormat
> convertable_values
[2];
1242 convertable_values
[0].reset(arg1_val
.release());
1244 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1245 phase
, category_group_enabled
, name
, id
, thread_id
, timestamp
,
1246 num_args
, arg_names
, arg_types
, arg_values
, convertable_values
, flags
);
1249 template<class ARG2_TYPE
>
1250 static inline void AddTraceEvent(
1252 const unsigned char* category_group_enabled
,
1254 unsigned long long id
,
1255 unsigned char flags
,
1256 const char* arg1_name
,
1257 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg1_val
,
1258 const char* arg2_name
,
1259 ARG2_TYPE arg2_val
) {
1260 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1261 base::TimeTicks now
= base::TimeTicks::NowFromSystemTraceTime();
1262 AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
, name
, id
,
1263 thread_id
, now
, flags
,
1264 arg1_name
, arg1_val
.Pass(),
1265 arg2_name
, arg2_val
);
1268 static inline void AddTraceEventWithThreadIdAndTimestamp(
1270 const unsigned char* category_group_enabled
,
1272 unsigned long long id
,
1274 const base::TimeTicks
& timestamp
,
1275 unsigned char flags
,
1276 const char* arg1_name
,
1277 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg1_val
,
1278 const char* arg2_name
,
1279 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg2_val
) {
1280 const int num_args
= 2;
1281 const char* arg_names
[2] = { arg1_name
, arg2_name
};
1282 unsigned char arg_types
[2] =
1283 { TRACE_VALUE_TYPE_CONVERTABLE
, TRACE_VALUE_TYPE_CONVERTABLE
};
1284 scoped_ptr
<base::debug::ConvertableToTraceFormat
> convertable_values
[2];
1285 convertable_values
[0].reset(arg1_val
.release());
1286 convertable_values
[1].reset(arg2_val
.release());
1288 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1289 phase
, category_group_enabled
, name
, id
, thread_id
, timestamp
,
1290 num_args
, arg_names
, arg_types
, NULL
, convertable_values
, flags
);
1293 static inline void AddTraceEvent(
1295 const unsigned char* category_group_enabled
,
1297 unsigned long long id
,
1298 unsigned char flags
,
1299 const char* arg1_name
,
1300 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg1_val
,
1301 const char* arg2_name
,
1302 scoped_ptr
<base::debug::ConvertableToTraceFormat
> arg2_val
) {
1303 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1304 base::TimeTicks now
= base::TimeTicks::NowFromSystemTraceTime();
1305 AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
, name
, id
,
1306 thread_id
, now
, flags
,
1307 arg1_name
, arg1_val
.Pass(),
1308 arg2_name
, arg2_val
.Pass());
1311 static inline void AddTraceEventWithThreadIdAndTimestamp(
1313 const unsigned char* category_group_enabled
,
1315 unsigned long long id
,
1317 const base::TimeTicks
& timestamp
,
1318 unsigned char flags
) {
1319 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1320 phase
, category_group_enabled
, name
, id
, thread_id
, timestamp
,
1321 kZeroNumArgs
, NULL
, NULL
, NULL
, NULL
, flags
);
1324 static inline void AddTraceEvent(char phase
,
1325 const unsigned char* category_group_enabled
,
1327 unsigned long long id
,
1328 unsigned char flags
) {
1329 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1330 base::TimeTicks now
= base::TimeTicks::NowFromSystemTraceTime();
1331 AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
, name
, id
,
1332 thread_id
, now
, flags
);
1335 template<class ARG1_TYPE
>
1336 static inline void AddTraceEventWithThreadIdAndTimestamp(
1338 const unsigned char* category_group_enabled
,
1340 unsigned long long id
,
1342 const base::TimeTicks
& timestamp
,
1343 unsigned char flags
,
1344 const char* arg1_name
,
1345 const ARG1_TYPE
& arg1_val
) {
1346 const int num_args
= 1;
1347 unsigned char arg_types
[1];
1348 unsigned long long arg_values
[1];
1349 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
1350 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1351 phase
, category_group_enabled
, name
, id
, thread_id
, timestamp
,
1352 num_args
, &arg1_name
, arg_types
, arg_values
, NULL
, flags
);
1355 template<class ARG1_TYPE
>
1356 static inline void AddTraceEvent(char phase
,
1357 const unsigned char* category_group_enabled
,
1359 unsigned long long id
,
1360 unsigned char flags
,
1361 const char* arg1_name
,
1362 const ARG1_TYPE
& arg1_val
) {
1363 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1364 base::TimeTicks now
= base::TimeTicks::NowFromSystemTraceTime();
1365 AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
, name
, id
,
1366 thread_id
, now
, flags
, arg1_name
,
1370 template<class ARG1_TYPE
, class ARG2_TYPE
>
1371 static inline void AddTraceEventWithThreadIdAndTimestamp(
1373 const unsigned char* category_group_enabled
,
1375 unsigned long long id
,
1377 const base::TimeTicks
& timestamp
,
1378 unsigned char flags
,
1379 const char* arg1_name
,
1380 const ARG1_TYPE
& arg1_val
,
1381 const char* arg2_name
,
1382 const ARG2_TYPE
& arg2_val
) {
1383 const int num_args
= 2;
1384 const char* arg_names
[2] = { arg1_name
, arg2_name
};
1385 unsigned char arg_types
[2];
1386 unsigned long long arg_values
[2];
1387 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
1388 SetTraceValue(arg2_val
, &arg_types
[1], &arg_values
[1]);
1389 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1390 phase
, category_group_enabled
, name
, id
, thread_id
, timestamp
,
1391 num_args
, arg_names
, arg_types
, arg_values
, NULL
, flags
);
1394 template<class ARG1_TYPE
, class ARG2_TYPE
>
1395 static inline void AddTraceEvent(char phase
,
1396 const unsigned char* category_group_enabled
,
1398 unsigned long long id
,
1399 unsigned char flags
,
1400 const char* arg1_name
,
1401 const ARG1_TYPE
& arg1_val
,
1402 const char* arg2_name
,
1403 const ARG2_TYPE
& arg2_val
) {
1404 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1405 base::TimeTicks now
= base::TimeTicks::NowFromSystemTraceTime();
1406 AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
, name
, id
,
1407 thread_id
, now
, flags
, arg1_name
,
1408 arg1_val
, arg2_name
, arg2_val
);
1411 // Used by TRACE_EVENTx macro. Do not use directly.
1412 class TRACE_EVENT_API_CLASS_EXPORT TraceEndOnScopeClose
{
1414 // Note: members of data_ intentionally left uninitialized. See Initialize.
1415 TraceEndOnScopeClose() : p_data_(NULL
) {}
1416 ~TraceEndOnScopeClose() {
1418 AddEventIfEnabled();
1421 void Initialize(const unsigned char* category_group_enabled
,
1423 data_
.category_group_enabled
= category_group_enabled
;
1429 // Add the end event if the category is still enabled.
1430 void AddEventIfEnabled() {
1431 // Only called when p_data_ is non-null.
1432 if (*p_data_
->category_group_enabled
) {
1433 TRACE_EVENT_API_ADD_TRACE_EVENT(
1434 TRACE_EVENT_PHASE_END
, // phase
1435 p_data_
->category_group_enabled
, // category enabled
1436 p_data_
->name
, // name
1438 kZeroNumArgs
, // num_args
1442 NULL
, // convertable_values
1443 TRACE_EVENT_FLAG_NONE
); // flags
1447 // This Data struct workaround is to avoid initializing all the members
1448 // in Data during construction of this object, since this object is always
1449 // constructed, even when tracing is disabled. If the members of Data were
1450 // members of this class instead, compiler warnings occur about potential
1451 // uninitialized accesses.
1453 const unsigned char* category_group_enabled
;
1460 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
1461 class TRACE_EVENT_API_CLASS_EXPORT ScopedTrace
{
1463 ScopedTrace(TRACE_EVENT_API_ATOMIC_WORD
* event_uid
, const char* name
);
1467 const unsigned char* category_group_enabled_
;
1471 // A support macro for TRACE_EVENT_BINARY_EFFICIENTx
1472 #define INTERNAL_TRACE_EVENT_BINARY_EFFICIENT_ADD_SCOPED( \
1473 category_group, name, ...) \
1474 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
1475 trace_event_internal::ScopedTrace \
1476 INTERNAL_TRACE_EVENT_UID(profileScope)( \
1477 &INTERNAL_TRACE_EVENT_UID(atomic), name); \
1479 // This macro generates less code then TRACE_EVENT0 but is also
1480 // slower to execute when tracing is off. It should generally only be
1481 // used with code that is seldom executed or conditionally executed
1483 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
1484 INTERNAL_TRACE_EVENT_BINARY_EFFICIENT_ADD_SCOPED(category_group, name)
1486 // TraceEventSamplingStateScope records the current sampling state
1487 // and sets a new sampling state. When the scope exists, it restores
1488 // the sampling state having recorded.
1489 template<size_t BucketNumber
>
1490 class TraceEventSamplingStateScope
{
1492 TraceEventSamplingStateScope(const char* category_and_name
) {
1493 previous_state_
= TraceEventSamplingStateScope
<BucketNumber
>::Current();
1494 TraceEventSamplingStateScope
<BucketNumber
>::Set(category_and_name
);
1497 ~TraceEventSamplingStateScope() {
1498 TraceEventSamplingStateScope
<BucketNumber
>::Set(previous_state_
);
1501 static inline const char* Current() {
1502 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
1503 g_trace_state
[BucketNumber
]));
1506 static inline void Set(const char* category_and_name
) {
1507 TRACE_EVENT_API_ATOMIC_STORE(
1508 g_trace_state
[BucketNumber
],
1509 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD
>(
1510 const_cast<char*>(category_and_name
)));
1514 const char* previous_state_
;
1517 } // namespace trace_event_internal
1522 template<typename IDType
> class TraceScopedTrackableObject
{
1524 TraceScopedTrackableObject(const char* category_group
, const char* name
,
1526 : category_group_(category_group
),
1529 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_
, name_
, id_
);
1532 template <typename ArgType
> void snapshot(ArgType snapshot
) {
1533 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_
, name_
, id_
, snapshot
);
1536 ~TraceScopedTrackableObject() {
1537 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_
, name_
, id_
);
1541 const char* category_group_
;
1545 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject
);
1548 } // namespace debug
1551 #endif /* BASE_DEBUG_TRACE_EVENT_H_ */