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 // Convertable 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::trace_event::ConvertableToTraceFormat {
147 // void AppendAsTraceFormat(std::string* out) const override {
148 // out->append("{\"foo\":1}");
151 // ~MyData() override {}
152 // DISALLOW_COPY_AND_ASSIGN(MyData);
155 // TRACE_EVENT1("foo", "bar", "data",
156 // scoped_refptr<ConvertableToTraceFormat>(new MyData()));
158 // The trace framework will take ownership if the passed pointer and it will
159 // be free'd when the trace buffer is flushed.
161 // Note, we only do the conversion when the buffer is flushed, so the provided
162 // data object should not be modified after it's passed to the trace framework.
166 // A thread safe singleton and mutex are used for thread safety. Category
167 // enabled flags are used to limit the performance impact when the system
170 // TRACE_EVENT macros first cache a pointer to a category. The categories are
171 // statically allocated and safe at all times, even after exit. Fetching a
172 // category is protected by the TraceLog::lock_. Multiple threads initializing
173 // the static variable is safe, as they will be serialized by the lock and
174 // multiple calls will return the same pointer to the category.
176 // Then the category_group_enabled flag is checked. This is a unsigned char, and
177 // not intended to be multithread safe. It optimizes access to AddTraceEvent
178 // which is threadsafe internally via TraceLog::lock_. The enabled flag may
179 // cause some threads to incorrectly call or skip calling AddTraceEvent near
180 // the time of the system being enabled or disabled. This is acceptable as
181 // we tolerate some data loss while the system is being enabled/disabled and
182 // because AddTraceEvent is threadsafe internally and checks the enabled state
185 // Without the use of these static category pointers and enabled flags all
186 // trace points would carry a significant performance cost of acquiring a lock
187 // and resolving the category.
189 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_H_
190 #define BASE_TRACE_EVENT_TRACE_EVENT_H_
194 #include "base/atomicops.h"
195 #include "base/time/time.h"
196 #include "base/trace_event/trace_event_memory.h"
197 #include "base/trace_event/trace_event_system_stats_monitor.h"
198 #include "base/trace_event/trace_log.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_EVENT_WITH_FLOW0(category_group, name, bind_id, flow_flags) \
229 INTERNAL_TRACE_MEMORY(category_group, name) \
230 INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW(category_group, name, bind_id, \
232 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
233 INTERNAL_TRACE_MEMORY(category_group, name) \
234 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
235 #define TRACE_EVENT_WITH_FLOW1(category_group, name, bind_id, flow_flags, \
236 arg1_name, arg1_val) \
237 INTERNAL_TRACE_MEMORY(category_group, name) \
238 INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW( \
239 category_group, name, bind_id, flow_flags, arg1_name, arg1_val)
240 #define TRACE_EVENT2( \
241 category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
242 INTERNAL_TRACE_MEMORY(category_group, name) \
243 INTERNAL_TRACE_EVENT_ADD_SCOPED( \
244 category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
245 #define TRACE_EVENT_WITH_FLOW2(category_group, name, bind_id, flow_flags, \
246 arg1_name, arg1_val, arg2_name, arg2_val) \
247 INTERNAL_TRACE_MEMORY(category_group, name) \
248 INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW(category_group, name, bind_id, \
249 flow_flags, arg1_name, \
250 arg1_val, arg2_name, arg2_val)
252 // Records events like TRACE_EVENT2 but uses |memory_tag| for memory tracing.
253 // Use this where |name| is too generic to accurately aggregate allocations.
254 #define TRACE_EVENT_WITH_MEMORY_TAG2( \
255 category, name, memory_tag, arg1_name, arg1_val, arg2_name, arg2_val) \
256 INTERNAL_TRACE_MEMORY(category, memory_tag) \
257 INTERNAL_TRACE_EVENT_ADD_SCOPED( \
258 category, name, arg1_name, arg1_val, arg2_name, arg2_val)
260 // UNSHIPPED_TRACE_EVENT* are like TRACE_EVENT* except that they are not
261 // included in official builds.
264 #undef TRACING_IS_OFFICIAL_BUILD
265 #define TRACING_IS_OFFICIAL_BUILD 1
266 #elif !defined(TRACING_IS_OFFICIAL_BUILD)
267 #define TRACING_IS_OFFICIAL_BUILD 0
270 #if TRACING_IS_OFFICIAL_BUILD
271 #define UNSHIPPED_TRACE_EVENT0(category_group, name) (void)0
272 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
274 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
275 arg2_name, arg2_val) (void)0
276 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) (void)0
277 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, \
278 arg1_name, arg1_val) (void)0
279 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, \
280 arg1_name, arg1_val, \
281 arg2_name, arg2_val) (void)0
283 #define UNSHIPPED_TRACE_EVENT0(category_group, name) \
284 TRACE_EVENT0(category_group, name)
285 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
286 TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
287 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
288 arg2_name, arg2_val) \
289 TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
290 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) \
291 TRACE_EVENT_INSTANT0(category_group, name, scope)
292 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, \
293 arg1_name, arg1_val) \
294 TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val)
295 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, \
296 arg1_name, arg1_val, \
297 arg2_name, arg2_val) \
298 TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
302 // Records a single event called "name" immediately, with 0, 1 or 2
303 // associated arguments. If the category is not enabled, then this
305 // - category and name strings must have application lifetime (statics or
306 // literals). They may not include " chars.
307 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \
308 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
309 category_group, name, TRACE_EVENT_FLAG_NONE | scope)
310 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
311 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
312 category_group, name, TRACE_EVENT_FLAG_NONE | scope, \
314 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
315 arg2_name, arg2_val) \
316 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
317 category_group, name, TRACE_EVENT_FLAG_NONE | scope, \
318 arg1_name, arg1_val, arg2_name, arg2_val)
319 #define TRACE_EVENT_COPY_INSTANT0(category_group, name, scope) \
320 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
321 category_group, name, TRACE_EVENT_FLAG_COPY | scope)
322 #define TRACE_EVENT_COPY_INSTANT1(category_group, name, scope, \
323 arg1_name, arg1_val) \
324 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
325 category_group, name, TRACE_EVENT_FLAG_COPY | scope, arg1_name, \
327 #define TRACE_EVENT_COPY_INSTANT2(category_group, name, scope, \
328 arg1_name, arg1_val, \
329 arg2_name, arg2_val) \
330 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
331 category_group, name, TRACE_EVENT_FLAG_COPY | scope, \
332 arg1_name, arg1_val, arg2_name, arg2_val)
334 // Sets the current sample state to the given category and name (both must be
335 // constant strings). These states are intended for a sampling profiler.
336 // Implementation note: we store category and name together because we don't
337 // want the inconsistency/expense of storing two pointers.
338 // |thread_bucket| is [0..2] and is used to statically isolate samples in one
339 // thread from others.
340 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET( \
341 bucket_number, category, name) \
342 trace_event_internal:: \
343 TraceEventSamplingStateScope<bucket_number>::Set(category "\0" name)
345 // Returns a current sampling state of the given bucket.
346 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \
347 trace_event_internal::TraceEventSamplingStateScope<bucket_number>::Current()
349 // Creates a scope of a sampling state of the given bucket.
351 // { // The sampling state is set within this scope.
352 // TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name");
355 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET( \
356 bucket_number, category, name) \
357 trace_event_internal::TraceEventSamplingStateScope<bucket_number> \
358 traceEventSamplingScope(category "\0" name);
360 // Syntactic sugars for the sampling tracing in the main thread.
361 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \
362 TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name)
363 #define TRACE_EVENT_GET_SAMPLING_STATE() \
364 TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0)
365 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \
366 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name)
369 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
370 // associated arguments. If the category is not enabled, then this
372 // - category and name strings must have application lifetime (statics or
373 // literals). They may not include " chars.
374 #define TRACE_EVENT_BEGIN0(category_group, name) \
375 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
376 category_group, name, TRACE_EVENT_FLAG_NONE)
377 #define TRACE_EVENT_BEGIN1(category_group, name, arg1_name, arg1_val) \
378 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
379 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
380 #define TRACE_EVENT_BEGIN2(category_group, name, arg1_name, arg1_val, \
381 arg2_name, arg2_val) \
382 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
383 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
385 #define TRACE_EVENT_COPY_BEGIN0(category_group, name) \
386 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
387 category_group, name, TRACE_EVENT_FLAG_COPY)
388 #define TRACE_EVENT_COPY_BEGIN1(category_group, name, arg1_name, arg1_val) \
389 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
390 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
391 #define TRACE_EVENT_COPY_BEGIN2(category_group, name, arg1_name, arg1_val, \
392 arg2_name, arg2_val) \
393 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
394 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
397 // Similar to TRACE_EVENT_BEGINx but with a custom |at| timestamp provided.
398 // - |id| is used to match the _BEGIN event with the _END event.
399 // Events are considered to match if their category_group, name and id values
400 // all match. |id| must either be a pointer or an integer value up to 64 bits.
401 // If it's a pointer, the bits will be xored with a hash of the process ID so
402 // that the same pointer on two different processes will not collide.
403 #define TRACE_EVENT_BEGIN_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
404 name, id, thread_id, timestamp) \
405 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
406 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
407 timestamp, TRACE_EVENT_FLAG_NONE)
408 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP0( \
409 category_group, name, id, thread_id, timestamp) \
410 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
411 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
412 timestamp, TRACE_EVENT_FLAG_COPY)
413 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP1( \
414 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val) \
415 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
416 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
417 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
418 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP2( \
419 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val, \
420 arg2_name, arg2_val) \
421 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
422 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
423 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, \
426 // Records a single END event for "name" immediately. If the category
427 // is not enabled, then this does nothing.
428 // - category and name strings must have application lifetime (statics or
429 // literals). They may not include " chars.
430 #define TRACE_EVENT_END0(category_group, name) \
431 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
432 category_group, name, TRACE_EVENT_FLAG_NONE)
433 #define TRACE_EVENT_END1(category_group, name, arg1_name, arg1_val) \
434 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
435 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
436 #define TRACE_EVENT_END2(category_group, name, arg1_name, arg1_val, \
437 arg2_name, arg2_val) \
438 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
439 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
441 #define TRACE_EVENT_COPY_END0(category_group, name) \
442 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
443 category_group, name, TRACE_EVENT_FLAG_COPY)
444 #define TRACE_EVENT_COPY_END1(category_group, name, arg1_name, arg1_val) \
445 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
446 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
447 #define TRACE_EVENT_COPY_END2(category_group, name, arg1_name, arg1_val, \
448 arg2_name, arg2_val) \
449 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
450 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
453 // Similar to TRACE_EVENT_ENDx but with a custom |at| timestamp provided.
454 // - |id| is used to match the _BEGIN event with the _END event.
455 // Events are considered to match if their category_group, name and id values
456 // all match. |id| must either be a pointer or an integer value up to 64 bits.
457 // If it's a pointer, the bits will be xored with a hash of the process ID so
458 // that the same pointer on two different processes will not collide.
459 #define TRACE_EVENT_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
460 name, id, thread_id, timestamp) \
461 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
462 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
463 timestamp, TRACE_EVENT_FLAG_NONE)
464 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0( \
465 category_group, name, id, thread_id, timestamp) \
466 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
467 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
468 timestamp, TRACE_EVENT_FLAG_COPY)
469 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP1( \
470 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val) \
471 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
472 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
473 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
474 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP2( \
475 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val, \
476 arg2_name, arg2_val) \
477 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
478 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
479 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, \
482 // Records the value of a counter called "name" immediately. Value
483 // must be representable as a 32 bit integer.
484 // - category and name strings must have application lifetime (statics or
485 // literals). They may not include " chars.
486 #define TRACE_COUNTER1(category_group, name, value) \
487 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
488 category_group, name, TRACE_EVENT_FLAG_NONE, \
489 "value", static_cast<int>(value))
490 #define TRACE_COPY_COUNTER1(category_group, name, value) \
491 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
492 category_group, name, TRACE_EVENT_FLAG_COPY, \
493 "value", static_cast<int>(value))
495 // Records the values of a multi-parted counter called "name" immediately.
496 // The UI will treat value1 and value2 as parts of a whole, displaying their
497 // values as a stacked-bar chart.
498 // - category and name strings must have application lifetime (statics or
499 // literals). They may not include " chars.
500 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \
501 value2_name, value2_val) \
502 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
503 category_group, name, TRACE_EVENT_FLAG_NONE, \
504 value1_name, static_cast<int>(value1_val), \
505 value2_name, static_cast<int>(value2_val))
506 #define TRACE_COPY_COUNTER2(category_group, name, value1_name, value1_val, \
507 value2_name, value2_val) \
508 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
509 category_group, name, TRACE_EVENT_FLAG_COPY, \
510 value1_name, static_cast<int>(value1_val), \
511 value2_name, static_cast<int>(value2_val))
513 // Records the value of a counter called "name" immediately. Value
514 // must be representable as a 32 bit integer.
515 // - category and name strings must have application lifetime (statics or
516 // literals). They may not include " chars.
517 // - |id| is used to disambiguate counters with the same name. It must either
518 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
519 // will be xored with a hash of the process ID so that the same pointer on
520 // two different processes will not collide.
521 #define TRACE_COUNTER_ID1(category_group, name, id, value) \
522 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
523 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
524 "value", static_cast<int>(value))
525 #define TRACE_COPY_COUNTER_ID1(category_group, name, id, value) \
526 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
527 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
528 "value", static_cast<int>(value))
530 // Records the values of a multi-parted counter called "name" immediately.
531 // The UI will treat value1 and value2 as parts of a whole, displaying their
532 // values as a stacked-bar chart.
533 // - category and name strings must have application lifetime (statics or
534 // literals). They may not include " chars.
535 // - |id| is used to disambiguate counters with the same name. It must either
536 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
537 // will be xored with a hash of the process ID so that the same pointer on
538 // two different processes will not collide.
539 #define TRACE_COUNTER_ID2(category_group, name, id, value1_name, value1_val, \
540 value2_name, value2_val) \
541 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
542 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
543 value1_name, static_cast<int>(value1_val), \
544 value2_name, static_cast<int>(value2_val))
545 #define TRACE_COPY_COUNTER_ID2(category_group, name, id, value1_name, \
546 value1_val, value2_name, value2_val) \
547 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
548 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
549 value1_name, static_cast<int>(value1_val), \
550 value2_name, static_cast<int>(value2_val))
552 // TRACE_EVENT_SAMPLE_* events are injected by the sampling profiler.
553 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP0(category_group, name, \
554 thread_id, timestamp) \
555 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
556 TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
557 TRACE_EVENT_FLAG_NONE)
559 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP1( \
560 category_group, name, thread_id, timestamp, arg1_name, arg1_val) \
561 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
562 TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
563 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
565 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP2(category_group, name, \
566 thread_id, timestamp, \
567 arg1_name, arg1_val, \
568 arg2_name, arg2_val) \
569 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
570 TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
571 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
573 // ASYNC_STEP_* APIs should be only used by legacy code. New code should
574 // consider using NESTABLE_ASYNC_* APIs to describe substeps within an async
576 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
577 // associated arguments. If the category is not enabled, then this
579 // - category and name strings must have application lifetime (statics or
580 // literals). They may not include " chars.
581 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
582 // events are considered to match if their category_group, name and id values
583 // all match. |id| must either be a pointer or an integer value up to 64 bits.
584 // If it's a pointer, the bits will be xored with a hash of the process ID so
585 // that the same pointer on two different processes will not collide.
587 // An asynchronous operation can consist of multiple phases. The first phase is
588 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
589 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will
590 // annotate the block following the call. The ASYNC_STEP_PAST macro will
591 // annotate the block prior to the call. Note that any particular event must use
592 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the
593 // operation completes, call ASYNC_END.
595 // An ASYNC trace typically occurs on a single thread (if not, they will only be
596 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
597 // operation must use the same |name| and |id|. Each step can have its own
599 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id) \
600 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
601 category_group, name, id, TRACE_EVENT_FLAG_NONE)
602 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
604 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
605 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
606 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
607 arg1_val, arg2_name, arg2_val) \
608 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
609 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
610 arg1_name, arg1_val, arg2_name, arg2_val)
611 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id) \
612 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
613 category_group, name, id, TRACE_EVENT_FLAG_COPY)
614 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
616 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
617 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
619 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
620 arg1_val, arg2_name, arg2_val) \
621 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
622 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
623 arg1_name, arg1_val, arg2_name, arg2_val)
625 // Similar to TRACE_EVENT_ASYNC_BEGINx but with a custom |at| timestamp
627 #define TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, \
628 name, id, timestamp) \
629 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
630 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, \
631 static_cast<int>(base::PlatformThread::CurrentId()), \
632 timestamp, TRACE_EVENT_FLAG_NONE)
633 #define TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, \
634 name, id, timestamp) \
635 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
636 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, \
637 static_cast<int>(base::PlatformThread::CurrentId()), \
638 timestamp, TRACE_EVENT_FLAG_COPY)
640 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the
641 // category is not enabled, then this does nothing. The |name| and |id| must
642 // match the ASYNC_BEGIN event above. The |step| param identifies this step
643 // within the async event. This should be called at the beginning of the next
644 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
645 // ASYNC_STEP_PAST events.
646 #define TRACE_EVENT_ASYNC_STEP_INTO0(category_group, name, id, step) \
647 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
648 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
649 #define TRACE_EVENT_ASYNC_STEP_INTO1(category_group, name, id, step, \
650 arg1_name, arg1_val) \
651 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
652 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
655 // Similar to TRACE_EVENT_ASYNC_STEP_INTOx but with a custom |at| timestamp
657 #define TRACE_EVENT_ASYNC_STEP_INTO_WITH_TIMESTAMP0(category_group, name, \
658 id, step, timestamp) \
659 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
660 TRACE_EVENT_PHASE_ASYNC_STEP_INTO, category_group, name, id, \
661 static_cast<int>(base::PlatformThread::CurrentId()), \
662 timestamp, TRACE_EVENT_FLAG_NONE, "step", step)
664 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the
665 // category is not enabled, then this does nothing. The |name| and |id| must
666 // match the ASYNC_BEGIN event above. The |step| param identifies this step
667 // within the async event. This should be called at the beginning of the next
668 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
669 // ASYNC_STEP_INTO events.
670 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step) \
671 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
672 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
673 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, \
674 arg1_name, arg1_val) \
675 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
676 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
679 // Records a single ASYNC_END event for "name" immediately. If the category
680 // is not enabled, then this does nothing.
681 #define TRACE_EVENT_ASYNC_END0(category_group, name, id) \
682 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
683 category_group, name, id, TRACE_EVENT_FLAG_NONE)
684 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
685 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
686 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
687 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
688 arg2_name, arg2_val) \
689 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
690 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
691 arg1_name, arg1_val, arg2_name, arg2_val)
692 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id) \
693 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
694 category_group, name, id, TRACE_EVENT_FLAG_COPY)
695 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
697 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
698 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
700 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
701 arg1_val, arg2_name, arg2_val) \
702 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
703 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
704 arg1_name, arg1_val, arg2_name, arg2_val)
706 // Similar to TRACE_EVENT_ASYNC_ENDx but with a custom |at| timestamp provided.
707 #define TRACE_EVENT_ASYNC_END_WITH_TIMESTAMP0(category_group, \
708 name, id, timestamp) \
709 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
710 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, \
711 static_cast<int>(base::PlatformThread::CurrentId()), \
712 timestamp, TRACE_EVENT_FLAG_NONE)
714 // NESTABLE_ASYNC_* APIs are used to describe an async operation, which can
715 // be nested within a NESTABLE_ASYNC event and/or have inner NESTABLE_ASYNC
717 // - category and name strings must have application lifetime (statics or
718 // literals). They may not include " chars.
719 // - A pair of NESTABLE_ASYNC_BEGIN event and NESTABLE_ASYNC_END event is
720 // considered as a match if their category_group, name and id all match.
721 // - |id| must either be a pointer or an integer value up to 64 bits.
722 // If it's a pointer, the bits will be xored with a hash of the process ID so
723 // that the same pointer on two different processes will not collide.
724 // - |id| is used to match a child NESTABLE_ASYNC event with its parent
725 // NESTABLE_ASYNC event. Therefore, events in the same nested event tree must
726 // be logged using the same id and category_group.
728 // Unmatched NESTABLE_ASYNC_END event will be parsed as an event that starts
729 // at the first NESTABLE_ASYNC event of that id, and unmatched
730 // NESTABLE_ASYNC_BEGIN event will be parsed as an event that ends at the last
731 // NESTABLE_ASYNC event of that id. Corresponding warning messages for
732 // unmatched events will be shown in the analysis view.
734 // Records a single NESTABLE_ASYNC_BEGIN event called "name" immediately, with
735 // 0, 1 or 2 associated arguments. If the category is not enabled, then this
737 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_group, name, id) \
738 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
739 category_group, name, id, TRACE_EVENT_FLAG_NONE)
740 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
742 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
743 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
744 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
745 arg1_val, arg2_name, arg2_val) \
746 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
747 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
749 // Records a single NESTABLE_ASYNC_END event called "name" immediately, with 0
750 // or 2 associated arguments. If the category is not enabled, then this does
752 #define TRACE_EVENT_NESTABLE_ASYNC_END0(category_group, name, id) \
753 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
754 category_group, name, id, TRACE_EVENT_FLAG_NONE)
755 #define TRACE_EVENT_NESTABLE_ASYNC_END2(category_group, name, id, arg1_name, \
756 arg1_val, arg2_name, arg2_val) \
757 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
758 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
761 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_BEGIN_WITH_TTS2(category_group, name, \
762 id, arg1_name, arg1_val, arg2_name, arg2_val) \
763 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
764 category_group, name, id, \
765 TRACE_EVENT_FLAG_ASYNC_TTS | TRACE_EVENT_FLAG_COPY, \
766 arg1_name, arg1_val, arg2_name, arg2_val)
767 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_END_WITH_TTS2(category_group, name, \
768 id, arg1_name, arg1_val, arg2_name, arg2_val) \
769 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
770 category_group, name, id, \
771 TRACE_EVENT_FLAG_ASYNC_TTS | TRACE_EVENT_FLAG_COPY, \
772 arg1_name, arg1_val, arg2_name, arg2_val)
774 // Similar to TRACE_EVENT_NESTABLE_ASYNC_{BEGIN,END}x but with a custom
775 // |timestamp| provided.
776 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, name, \
778 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
779 TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, category_group, name, id, \
780 static_cast<int>(base::PlatformThread::CurrentId()), timestamp, \
781 TRACE_EVENT_FLAG_NONE)
783 #define TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(category_group, name, \
785 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
786 TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, category_group, name, id, \
787 static_cast<int>(base::PlatformThread::CurrentId()), timestamp, \
788 TRACE_EVENT_FLAG_NONE)
790 // Records a single NESTABLE_ASYNC_INSTANT event called "name" immediately,
791 // with 2 associated arguments. If the category is not enabled, then this
793 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT2(category_group, name, id, \
794 arg1_name, arg1_val, arg2_name, arg2_val) \
795 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, \
796 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
799 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
800 // associated arguments. If the category is not enabled, then this
802 // - category and name strings must have application lifetime (statics or
803 // literals). They may not include " chars.
804 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
805 // events are considered to match if their category_group, name and id values
806 // all match. |id| must either be a pointer or an integer value up to 64 bits.
807 // If it's a pointer, the bits will be xored with a hash of the process ID so
808 // that the same pointer on two different processes will not collide.
809 // FLOW events are different from ASYNC events in how they are drawn by the
810 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
811 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
812 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
813 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
814 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
815 // macros. When the operation completes, call FLOW_END. An async operation can
816 // span threads and processes, but all events in that operation must use the
817 // same |name| and |id|. Each event can have its own args.
818 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
819 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
820 category_group, name, id, TRACE_EVENT_FLAG_NONE)
821 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
822 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
823 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
824 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
825 arg2_name, arg2_val) \
826 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
827 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
828 arg1_name, arg1_val, arg2_name, arg2_val)
829 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
830 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
831 category_group, name, id, TRACE_EVENT_FLAG_COPY)
832 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
834 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
835 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
837 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
838 arg1_val, arg2_name, arg2_val) \
839 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
840 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
841 arg1_name, arg1_val, arg2_name, arg2_val)
843 // Records a single FLOW_STEP event for |step| immediately. If the category
844 // is not enabled, then this does nothing. The |name| and |id| must match the
845 // FLOW_BEGIN event above. The |step| param identifies this step within the
846 // async event. This should be called at the beginning of the next phase of an
847 // asynchronous operation.
848 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
849 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
850 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
851 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \
852 arg1_name, arg1_val) \
853 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
854 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
856 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
857 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
858 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
859 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \
860 arg1_name, arg1_val) \
861 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
862 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
865 // Records a single FLOW_END event for "name" immediately. If the category
866 // is not enabled, then this does nothing.
867 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
868 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
869 category_group, name, id, TRACE_EVENT_FLAG_NONE)
870 #define TRACE_EVENT_FLOW_END_BIND_TO_ENCLOSING0(category_group, name, id) \
871 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
872 category_group, name, id, TRACE_EVENT_FLAG_BIND_TO_ENCLOSING)
873 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
874 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
875 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
876 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
877 arg2_name, arg2_val) \
878 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
879 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
880 arg1_name, arg1_val, arg2_name, arg2_val)
881 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
882 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
883 category_group, name, id, TRACE_EVENT_FLAG_COPY)
884 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
886 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
887 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
889 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
890 arg1_val, arg2_name, arg2_val) \
891 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
892 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
893 arg1_name, arg1_val, arg2_name, arg2_val)
895 // Macros to track the life time and value of arbitrary client objects.
896 // See also TraceTrackableObject.
897 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
898 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \
899 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
901 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
902 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \
903 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE,\
904 "snapshot", snapshot)
906 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID_AND_TIMESTAMP( \
907 category_group, name, id, timestamp, snapshot) \
908 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
909 TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name, \
910 TRACE_ID_DONT_MANGLE(id), \
911 static_cast<int>(base::PlatformThread::CurrentId()), timestamp, \
912 TRACE_EVENT_FLAG_NONE, "snapshot", snapshot)
914 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
915 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \
916 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
918 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
919 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
920 (base::trace_event::TraceLog::ENABLED_FOR_RECORDING | \
921 base::trace_event::TraceLog::ENABLED_FOR_EVENT_CALLBACK | \
922 base::trace_event::TraceLog::ENABLED_FOR_ETW_EXPORT))
924 // Macro to efficiently determine if a given category group is enabled.
925 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
927 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
928 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
935 // Macro to explicitly warm up a given category group. This could be useful in
936 // cases where we want to initialize a category group before any trace events
937 // for that category group is reported. For example, to have a category group
938 // always show up in the "record categories" list for manually selecting
939 // settings in about://tracing.
940 #define TRACE_EVENT_WARMUP_CATEGORY(category_group) \
941 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group)
943 // Macro to efficiently determine, through polling, if a new trace has begun.
944 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
946 static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
947 int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
948 if (num_traces_recorded != -1 && \
949 num_traces_recorded != \
950 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
951 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = \
952 num_traces_recorded; \
959 ////////////////////////////////////////////////////////////////////////////////
960 // Implementation specific tracing API definitions.
962 // Get a pointer to the enabled state of the given trace category. Only
963 // long-lived literal strings should be given as the category group. The
964 // returned pointer can be held permanently in a local static for example. If
965 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
966 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
967 // between the load of the tracing state and the call to
968 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
969 // for best performance when tracing is disabled.
970 // const unsigned char*
971 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
972 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
973 base::trace_event::TraceLog::GetCategoryGroupEnabled
975 // Get the number of times traces have been recorded. This is used to implement
976 // the TRACE_EVENT_IS_NEW_TRACE facility.
977 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
978 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
979 base::trace_event::TraceLog::GetInstance()->GetNumTracesRecorded
981 // Add a trace event to the platform tracing system.
982 // base::trace_event::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT(
984 // const unsigned char* category_group_enabled,
986 // unsigned long long id,
987 // unsigned long long context_id,
989 // const char** arg_names,
990 // const unsigned char* arg_types,
991 // const unsigned long long* arg_values,
992 // unsigned int flags)
993 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
994 base::trace_event::TraceLog::GetInstance()->AddTraceEvent
996 // Add a trace event to the platform tracing system.
997 // base::trace_event::TraceEventHandle
998 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID(
1000 // const unsigned char* category_group_enabled,
1001 // const char* name,
1002 // unsigned long long id,
1003 // unsigned long long context_id,
1005 // const char** arg_names,
1006 // const unsigned char* arg_types,
1007 // const unsigned long long* arg_values,
1008 // unsigned int flags)
1009 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID \
1010 base::trace_event::TraceLog::GetInstance()->AddTraceEventWithContextId
1012 // Add a trace event to the platform tracing system.
1013 // base::trace_event::TraceEventHandle
1014 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
1016 // const unsigned char* category_group_enabled,
1017 // const char* name,
1018 // unsigned long long id,
1019 // unsigned long long context_id,
1021 // const TraceTicks& timestamp,
1023 // const char** arg_names,
1024 // const unsigned char* arg_types,
1025 // const unsigned long long* arg_values,
1026 // unsigned int flags)
1027 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
1028 base::trace_event::TraceLog::GetInstance() \
1029 ->AddTraceEventWithThreadIdAndTimestamp
1031 // Set the duration field of a COMPLETE trace event.
1032 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1033 // const unsigned char* category_group_enabled,
1034 // const char* name,
1035 // base::trace_event::TraceEventHandle id)
1036 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
1037 base::trace_event::TraceLog::GetInstance()->UpdateTraceEventDuration
1039 // Defines atomic operations used internally by the tracing system.
1040 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
1041 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
1042 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
1043 base::subtle::NoBarrier_Store(&(var), (value))
1045 // Defines visibility for classes in trace_event.h
1046 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
1048 // The thread buckets for the sampling profiler.
1049 TRACE_EVENT_API_CLASS_EXPORT
extern \
1050 TRACE_EVENT_API_ATOMIC_WORD g_trace_state
[3];
1052 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
1053 g_trace_state[thread_bucket]
1055 ////////////////////////////////////////////////////////////////////////////////
1057 // Implementation detail: trace event macros create temporary variables
1058 // to keep instrumentation overhead low. These macros give each temporary
1059 // variable a unique name based on the line number to prevent name collisions.
1060 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
1061 trace_event_unique_##a##b
1062 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
1063 INTERNAL_TRACE_EVENT_UID3(a,b)
1064 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
1065 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
1067 // Implementation detail: internal macro to create static category.
1068 // No barriers are needed, because this code is designed to operate safely
1069 // even when the unsigned char* points to garbage data (which may be the case
1070 // on processors without cache coherency).
1071 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
1072 category_group, atomic, category_group_enabled) \
1073 category_group_enabled = \
1074 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
1076 if (UNLIKELY(!category_group_enabled)) { \
1077 category_group_enabled = \
1078 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
1079 TRACE_EVENT_API_ATOMIC_STORE(atomic, \
1080 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
1081 category_group_enabled)); \
1084 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
1085 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
1086 const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
1087 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES(category_group, \
1088 INTERNAL_TRACE_EVENT_UID(atomic), \
1089 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
1091 // Implementation detail: internal macro to create static category and add
1092 // event if the category is enabled.
1093 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
1095 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1096 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1097 trace_event_internal::AddTraceEvent( \
1098 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
1099 trace_event_internal::kNoId, flags, \
1100 trace_event_internal::kNoId, ##__VA_ARGS__); \
1104 // Implementation detail: internal macro to create static category and add begin
1105 // event if the category is enabled. Also adds the end event when the scope
1107 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
1108 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1109 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
1110 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1111 base::trace_event::TraceEventHandle h = \
1112 trace_event_internal::AddTraceEvent( \
1113 TRACE_EVENT_PHASE_COMPLETE, \
1114 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
1115 trace_event_internal::kNoId, TRACE_EVENT_FLAG_NONE, \
1116 trace_event_internal::kNoId, ##__VA_ARGS__); \
1117 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
1118 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
1121 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW( \
1122 category_group, name, bind_id, flow_flags, ...) \
1123 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1124 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
1125 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1126 unsigned int trace_event_flags = flow_flags; \
1127 trace_event_internal::TraceID trace_event_bind_id(bind_id, \
1128 &trace_event_flags); \
1129 base::trace_event::TraceEventHandle h = \
1130 trace_event_internal::AddTraceEvent( \
1131 TRACE_EVENT_PHASE_COMPLETE, \
1132 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
1133 trace_event_internal::kNoId, trace_event_flags, \
1134 trace_event_bind_id.data(), ##__VA_ARGS__); \
1135 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
1136 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
1139 // Implementation detail: internal macro to create static category and add
1140 // event if the category is enabled.
1141 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
1144 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1145 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1146 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1147 trace_event_internal::TraceID trace_event_trace_id( \
1148 id, &trace_event_flags); \
1149 trace_event_internal::AddTraceEvent( \
1150 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1151 name, trace_event_trace_id.data(), trace_event_flags, \
1152 trace_event_internal::kNoId, ##__VA_ARGS__); \
1156 // Implementation detail: internal macro to create static category and add
1157 // event if the category is enabled.
1158 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
1159 category_group, name, id, thread_id, timestamp, flags, ...) \
1161 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1162 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1163 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1164 trace_event_internal::TraceID trace_event_trace_id( \
1165 id, &trace_event_flags); \
1166 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
1167 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1168 name, trace_event_trace_id.data(), trace_event_internal::kNoId, \
1169 thread_id, base::TraceTicks::FromInternalValue(timestamp), \
1170 trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
1171 trace_event_internal::kNoId, ##__VA_ARGS__); \
1175 // Notes regarding the following definitions:
1176 // New values can be added and propagated to third party libraries, but existing
1177 // definitions must never be changed, because third party libraries may use old
1180 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
1181 #define TRACE_EVENT_PHASE_BEGIN ('B')
1182 #define TRACE_EVENT_PHASE_END ('E')
1183 #define TRACE_EVENT_PHASE_COMPLETE ('X')
1184 #define TRACE_EVENT_PHASE_INSTANT ('I')
1185 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
1186 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T')
1187 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p')
1188 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
1189 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN ('b')
1190 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_END ('e')
1191 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT ('n')
1192 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
1193 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
1194 #define TRACE_EVENT_PHASE_FLOW_END ('f')
1195 #define TRACE_EVENT_PHASE_METADATA ('M')
1196 #define TRACE_EVENT_PHASE_COUNTER ('C')
1197 #define TRACE_EVENT_PHASE_SAMPLE ('P')
1198 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
1199 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
1200 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
1201 #define TRACE_EVENT_PHASE_MEMORY_DUMP ('v')
1203 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
1204 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned int>(0))
1205 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned int>(1 << 0))
1206 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned int>(1 << 1))
1207 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned int>(1 << 2))
1208 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned int>(1 << 3))
1209 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned int>(1 << 4))
1210 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned int>(1 << 5))
1211 #define TRACE_EVENT_FLAG_ASYNC_TTS (static_cast<unsigned int>(1 << 6))
1212 #define TRACE_EVENT_FLAG_BIND_TO_ENCLOSING (static_cast<unsigned int>(1 << 7))
1213 #define TRACE_EVENT_FLAG_FLOW_IN (static_cast<unsigned int>(1 << 8))
1214 #define TRACE_EVENT_FLAG_FLOW_OUT (static_cast<unsigned int>(1 << 9))
1215 #define TRACE_EVENT_FLAG_HAS_CONTEXT_ID (static_cast<unsigned int>(1 << 10))
1217 #define TRACE_EVENT_FLAG_SCOPE_MASK (static_cast<unsigned int>( \
1218 TRACE_EVENT_FLAG_SCOPE_OFFSET | TRACE_EVENT_FLAG_SCOPE_EXTRA))
1220 // Type values for identifying types in the TraceValue union.
1221 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
1222 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
1223 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
1224 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
1225 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
1226 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
1227 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
1228 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
1230 // Enum reflecting the scope of an INSTANT event. Must fit within
1231 // TRACE_EVENT_FLAG_SCOPE_MASK.
1232 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
1233 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
1234 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
1236 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
1237 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
1238 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
1240 namespace trace_event_internal
{
1242 // Specify these values when the corresponding argument of AddTraceEvent is not
1244 const int kZeroNumArgs
= 0;
1245 const unsigned long long kNoId
= 0;
1247 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
1248 // are by default mangled with the Process ID so that they are unlikely to
1249 // collide when the same pointer is used on different processes.
1254 explicit DontMangle(const void* id
)
1255 : data_(static_cast<unsigned long long>(
1256 reinterpret_cast<uintptr_t>(id
))) {}
1257 explicit DontMangle(unsigned long long id
) : data_(id
) {}
1258 explicit DontMangle(unsigned long id
) : data_(id
) {}
1259 explicit DontMangle(unsigned int id
) : data_(id
) {}
1260 explicit DontMangle(unsigned short id
) : data_(id
) {}
1261 explicit DontMangle(unsigned char id
) : data_(id
) {}
1262 explicit DontMangle(long long id
)
1263 : data_(static_cast<unsigned long long>(id
)) {}
1264 explicit DontMangle(long id
)
1265 : data_(static_cast<unsigned long long>(id
)) {}
1266 explicit DontMangle(int id
)
1267 : data_(static_cast<unsigned long long>(id
)) {}
1268 explicit DontMangle(short id
)
1269 : data_(static_cast<unsigned long long>(id
)) {}
1270 explicit DontMangle(signed char id
)
1271 : data_(static_cast<unsigned long long>(id
)) {}
1272 unsigned long long data() const { return data_
; }
1274 unsigned long long data_
;
1279 explicit ForceMangle(unsigned long long id
) : data_(id
) {}
1280 explicit ForceMangle(unsigned long id
) : data_(id
) {}
1281 explicit ForceMangle(unsigned int id
) : data_(id
) {}
1282 explicit ForceMangle(unsigned short id
) : data_(id
) {}
1283 explicit ForceMangle(unsigned char id
) : data_(id
) {}
1284 explicit ForceMangle(long long id
)
1285 : data_(static_cast<unsigned long long>(id
)) {}
1286 explicit ForceMangle(long id
)
1287 : data_(static_cast<unsigned long long>(id
)) {}
1288 explicit ForceMangle(int id
)
1289 : data_(static_cast<unsigned long long>(id
)) {}
1290 explicit ForceMangle(short id
)
1291 : data_(static_cast<unsigned long long>(id
)) {}
1292 explicit ForceMangle(signed char id
)
1293 : data_(static_cast<unsigned long long>(id
)) {}
1294 unsigned long long data() const { return data_
; }
1296 unsigned long long data_
;
1298 TraceID(const void* id
, unsigned int* flags
)
1299 : data_(static_cast<unsigned long long>(
1300 reinterpret_cast<uintptr_t>(id
))) {
1301 *flags
|= TRACE_EVENT_FLAG_MANGLE_ID
;
1303 TraceID(ForceMangle id
, unsigned int* flags
) : data_(id
.data()) {
1304 *flags
|= TRACE_EVENT_FLAG_MANGLE_ID
;
1306 TraceID(DontMangle id
, unsigned int* flags
) : data_(id
.data()) {
1308 TraceID(unsigned long long id
, unsigned int* flags
)
1309 : data_(id
) { (void)flags
; }
1310 TraceID(unsigned long id
, unsigned int* flags
)
1311 : data_(id
) { (void)flags
; }
1312 TraceID(unsigned int id
, unsigned int* flags
)
1313 : data_(id
) { (void)flags
; }
1314 TraceID(unsigned short id
, unsigned int* flags
)
1315 : data_(id
) { (void)flags
; }
1316 TraceID(unsigned char id
, unsigned int* flags
)
1317 : data_(id
) { (void)flags
; }
1318 TraceID(long long id
, unsigned int* flags
)
1319 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1320 TraceID(long id
, unsigned int* flags
)
1321 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1322 TraceID(int id
, unsigned int* flags
)
1323 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1324 TraceID(short id
, unsigned int* flags
)
1325 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1326 TraceID(signed char id
, unsigned int* flags
)
1327 : data_(static_cast<unsigned long long>(id
)) { (void)flags
; }
1329 unsigned long long data() const { return data_
; }
1332 unsigned long long data_
;
1335 // Simple union to store various types as unsigned long long.
1336 union TraceValueUnion
{
1338 unsigned long long as_uint
;
1341 const void* as_pointer
;
1342 const char* as_string
;
1345 // Simple container for const char* that should be copied instead of retained.
1346 class TraceStringWithCopy
{
1348 explicit TraceStringWithCopy(const char* str
) : str_(str
) {}
1349 const char* str() const { return str_
; }
1354 // Define SetTraceValue for each allowed type. It stores the type and
1355 // value in the return arguments. This allows this API to avoid declaring any
1356 // structures so that it is portable to third_party libraries.
1357 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
1361 static inline void SetTraceValue( \
1363 unsigned char* type, \
1364 unsigned long long* value) { \
1365 TraceValueUnion type_value; \
1366 type_value.union_member = arg_expression; \
1367 *type = value_type_id; \
1368 *value = type_value.as_uint; \
1370 // Simpler form for int types that can be safely casted.
1371 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
1373 static inline void SetTraceValue( \
1375 unsigned char* type, \
1376 unsigned long long* value) { \
1377 *type = value_type_id; \
1378 *value = static_cast<unsigned long long>(arg); \
1381 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT
)
1382 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT
)
1383 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT
)
1384 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT
)
1385 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT
)
1386 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT
)
1387 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT
)
1388 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT
)
1389 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT
)
1390 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT
)
1391 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg
, as_bool
, TRACE_VALUE_TYPE_BOOL
)
1392 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg
, as_double
,
1393 TRACE_VALUE_TYPE_DOUBLE
)
1394 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, arg
, as_pointer
,
1395 TRACE_VALUE_TYPE_POINTER
)
1396 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, arg
, as_string
,
1397 TRACE_VALUE_TYPE_STRING
)
1398 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy
&, arg
.str(),
1399 as_string
, TRACE_VALUE_TYPE_COPY_STRING
)
1401 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
1402 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
1404 // std::string version of SetTraceValue so that trace arguments can be strings.
1405 static inline void SetTraceValue(const std::string
& arg
,
1406 unsigned char* type
,
1407 unsigned long long* value
) {
1408 TraceValueUnion type_value
;
1409 type_value
.as_string
= arg
.c_str();
1410 *type
= TRACE_VALUE_TYPE_COPY_STRING
;
1411 *value
= type_value
.as_uint
;
1414 // base::Time, base::TimeTicks, etc. versions of SetTraceValue to make it easier
1415 // to trace these types.
1416 static inline void SetTraceValue(const base::Time arg
,
1417 unsigned char* type
,
1418 unsigned long long* value
) {
1419 *type
= TRACE_VALUE_TYPE_INT
;
1420 *value
= arg
.ToInternalValue();
1423 static inline void SetTraceValue(const base::TimeTicks arg
,
1424 unsigned char* type
,
1425 unsigned long long* value
) {
1426 *type
= TRACE_VALUE_TYPE_INT
;
1427 *value
= arg
.ToInternalValue();
1430 static inline void SetTraceValue(const base::ThreadTicks arg
,
1431 unsigned char* type
,
1432 unsigned long long* value
) {
1433 *type
= TRACE_VALUE_TYPE_INT
;
1434 *value
= arg
.ToInternalValue();
1437 static inline void SetTraceValue(const base::TraceTicks arg
,
1438 unsigned char* type
,
1439 unsigned long long* value
) {
1440 *type
= TRACE_VALUE_TYPE_INT
;
1441 *value
= arg
.ToInternalValue();
1444 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
1445 // functions are defined here instead of in the macro, because the arg_values
1446 // could be temporary objects, such as std::string. In order to store
1447 // pointers to the internal c_str and pass through to the tracing API,
1448 // the arg_values must live throughout these procedures.
1450 static inline base::trace_event::TraceEventHandle
1451 AddTraceEventWithThreadIdAndTimestamp(
1453 const unsigned char* category_group_enabled
,
1455 unsigned long long id
,
1456 unsigned long long context_id
,
1458 const base::TraceTicks
& timestamp
,
1460 unsigned long long bind_id
,
1461 const char* arg1_name
,
1462 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>&
1464 const int num_args
= 1;
1465 unsigned char arg_types
[1] = { TRACE_VALUE_TYPE_CONVERTABLE
};
1466 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1467 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
1468 timestamp
, num_args
, &arg1_name
, arg_types
, NULL
, &arg1_val
, flags
);
1471 template<class ARG1_TYPE
>
1472 static inline base::trace_event::TraceEventHandle
1473 AddTraceEventWithThreadIdAndTimestamp(
1475 const unsigned char* category_group_enabled
,
1477 unsigned long long id
,
1478 unsigned long long context_id
,
1480 const base::TraceTicks
& timestamp
,
1482 unsigned long long bind_id
,
1483 const char* arg1_name
,
1484 const ARG1_TYPE
& arg1_val
,
1485 const char* arg2_name
,
1486 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>&
1488 const int num_args
= 2;
1489 const char* arg_names
[2] = { arg1_name
, arg2_name
};
1491 unsigned char arg_types
[2];
1492 unsigned long long arg_values
[2];
1493 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
1494 arg_types
[1] = TRACE_VALUE_TYPE_CONVERTABLE
;
1496 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
1497 convertable_values
[2];
1498 convertable_values
[1] = arg2_val
;
1500 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1501 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
1502 timestamp
, num_args
, arg_names
, arg_types
, arg_values
,
1503 convertable_values
, flags
);
1506 template<class ARG2_TYPE
>
1507 static inline base::trace_event::TraceEventHandle
1508 AddTraceEventWithThreadIdAndTimestamp(
1510 const unsigned char* category_group_enabled
,
1512 unsigned long long id
,
1513 unsigned long long context_id
,
1515 const base::TraceTicks
& timestamp
,
1517 unsigned long long bind_id
,
1518 const char* arg1_name
,
1519 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>& arg1_val
,
1520 const char* arg2_name
,
1521 const ARG2_TYPE
& arg2_val
) {
1522 const int num_args
= 2;
1523 const char* arg_names
[2] = { arg1_name
, arg2_name
};
1525 unsigned char arg_types
[2];
1526 unsigned long long arg_values
[2];
1527 arg_types
[0] = TRACE_VALUE_TYPE_CONVERTABLE
;
1529 SetTraceValue(arg2_val
, &arg_types
[1], &arg_values
[1]);
1531 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
1532 convertable_values
[2];
1533 convertable_values
[0] = arg1_val
;
1535 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1536 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
1537 timestamp
, num_args
, arg_names
, arg_types
, arg_values
,
1538 convertable_values
, flags
);
1541 static inline base::trace_event::TraceEventHandle
1542 AddTraceEventWithThreadIdAndTimestamp(
1544 const unsigned char* category_group_enabled
,
1546 unsigned long long id
,
1547 unsigned long long context_id
,
1549 const base::TraceTicks
& timestamp
,
1551 unsigned long long bind_id
,
1552 const char* arg1_name
,
1553 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>& arg1_val
,
1554 const char* arg2_name
,
1555 const scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>&
1557 const int num_args
= 2;
1558 const char* arg_names
[2] = { arg1_name
, arg2_name
};
1559 unsigned char arg_types
[2] =
1560 { TRACE_VALUE_TYPE_CONVERTABLE
, TRACE_VALUE_TYPE_CONVERTABLE
};
1561 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
1562 convertable_values
[2] = {arg1_val
, arg2_val
};
1564 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1565 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
1566 timestamp
, num_args
, arg_names
, arg_types
, NULL
, convertable_values
,
1570 static inline base::trace_event::TraceEventHandle
1571 AddTraceEventWithThreadIdAndTimestamp(
1573 const unsigned char* category_group_enabled
,
1575 unsigned long long id
,
1576 unsigned long long context_id
,
1578 const base::TraceTicks
& timestamp
,
1580 unsigned long long bind_id
) {
1581 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1582 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
1583 timestamp
, kZeroNumArgs
, NULL
, NULL
, NULL
, NULL
, flags
);
1586 static inline base::trace_event::TraceEventHandle
AddTraceEvent(
1588 const unsigned char* category_group_enabled
,
1590 unsigned long long id
,
1592 unsigned long long bind_id
) {
1593 const int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1594 const base::TraceTicks now
= base::TraceTicks::Now();
1595 return AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
,
1596 name
, id
, kNoId
, thread_id
, now
,
1600 template<class ARG1_TYPE
>
1601 static inline base::trace_event::TraceEventHandle
1602 AddTraceEventWithThreadIdAndTimestamp(
1604 const unsigned char* category_group_enabled
,
1606 unsigned long long id
,
1607 unsigned long long context_id
,
1609 const base::TraceTicks
& timestamp
,
1611 unsigned long long bind_id
,
1612 const char* arg1_name
,
1613 const ARG1_TYPE
& arg1_val
) {
1614 const int num_args
= 1;
1615 unsigned char arg_types
[1];
1616 unsigned long long arg_values
[1];
1617 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
1618 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1619 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
1620 timestamp
, num_args
, &arg1_name
, arg_types
, arg_values
, NULL
, flags
);
1623 template<class ARG1_TYPE
>
1624 static inline base::trace_event::TraceEventHandle
AddTraceEvent(
1626 const unsigned char* category_group_enabled
,
1628 unsigned long long id
,
1630 unsigned long long bind_id
,
1631 const char* arg1_name
,
1632 const ARG1_TYPE
& arg1_val
) {
1633 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1634 base::TraceTicks now
= base::TraceTicks::Now();
1635 return AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
,
1636 name
, id
, kNoId
, thread_id
, now
,
1638 arg1_name
, arg1_val
);
1641 template<class ARG1_TYPE
, class ARG2_TYPE
>
1642 static inline base::trace_event::TraceEventHandle
1643 AddTraceEventWithThreadIdAndTimestamp(
1645 const unsigned char* category_group_enabled
,
1647 unsigned long long id
,
1648 unsigned long long context_id
,
1650 const base::TraceTicks
& timestamp
,
1652 unsigned long long bind_id
,
1653 const char* arg1_name
,
1654 const ARG1_TYPE
& arg1_val
,
1655 const char* arg2_name
,
1656 const ARG2_TYPE
& arg2_val
) {
1657 const int num_args
= 2;
1658 const char* arg_names
[2] = { arg1_name
, arg2_name
};
1659 unsigned char arg_types
[2];
1660 unsigned long long arg_values
[2];
1661 SetTraceValue(arg1_val
, &arg_types
[0], &arg_values
[0]);
1662 SetTraceValue(arg2_val
, &arg_types
[1], &arg_values
[1]);
1663 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1664 phase
, category_group_enabled
, name
, id
, context_id
, bind_id
, thread_id
,
1665 timestamp
, num_args
, arg_names
, arg_types
, arg_values
, NULL
, flags
);
1668 template<class ARG1_TYPE
, class ARG2_TYPE
>
1669 static inline base::trace_event::TraceEventHandle
AddTraceEvent(
1671 const unsigned char* category_group_enabled
,
1673 unsigned long long id
,
1675 unsigned long long bind_id
,
1676 const char* arg1_name
,
1677 const ARG1_TYPE
& arg1_val
,
1678 const char* arg2_name
,
1679 const ARG2_TYPE
& arg2_val
) {
1680 int thread_id
= static_cast<int>(base::PlatformThread::CurrentId());
1681 base::TraceTicks now
= base::TraceTicks::Now();
1682 return AddTraceEventWithThreadIdAndTimestamp(phase
, category_group_enabled
,
1683 name
, id
, kNoId
, thread_id
, now
,
1685 arg1_name
, arg1_val
,
1686 arg2_name
, arg2_val
);
1689 // Used by TRACE_EVENTx macros. Do not use directly.
1690 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer
{
1692 // Note: members of data_ intentionally left uninitialized. See Initialize.
1693 ScopedTracer() : p_data_(NULL
) {}
1696 if (p_data_
&& *data_
.category_group_enabled
)
1697 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1698 data_
.category_group_enabled
, data_
.name
, data_
.event_handle
);
1701 void Initialize(const unsigned char* category_group_enabled
,
1703 base::trace_event::TraceEventHandle event_handle
) {
1704 data_
.category_group_enabled
= category_group_enabled
;
1706 data_
.event_handle
= event_handle
;
1711 // This Data struct workaround is to avoid initializing all the members
1712 // in Data during construction of this object, since this object is always
1713 // constructed, even when tracing is disabled. If the members of Data were
1714 // members of this class instead, compiler warnings occur about potential
1715 // uninitialized accesses.
1717 const unsigned char* category_group_enabled
;
1719 base::trace_event::TraceEventHandle event_handle
;
1725 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
1726 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient
{
1728 ScopedTraceBinaryEfficient(const char* category_group
, const char* name
);
1729 ~ScopedTraceBinaryEfficient();
1732 const unsigned char* category_group_enabled_
;
1734 base::trace_event::TraceEventHandle event_handle_
;
1737 // This macro generates less code then TRACE_EVENT0 but is also
1738 // slower to execute when tracing is off. It should generally only be
1739 // used with code that is seldom executed or conditionally executed
1741 // For now the category_group must be "gpu".
1742 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
1743 trace_event_internal::ScopedTraceBinaryEfficient \
1744 INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
1746 // TraceEventSamplingStateScope records the current sampling state
1747 // and sets a new sampling state. When the scope exists, it restores
1748 // the sampling state having recorded.
1749 template<size_t BucketNumber
>
1750 class TraceEventSamplingStateScope
{
1752 TraceEventSamplingStateScope(const char* category_and_name
) {
1753 previous_state_
= TraceEventSamplingStateScope
<BucketNumber
>::Current();
1754 TraceEventSamplingStateScope
<BucketNumber
>::Set(category_and_name
);
1757 ~TraceEventSamplingStateScope() {
1758 TraceEventSamplingStateScope
<BucketNumber
>::Set(previous_state_
);
1761 static inline const char* Current() {
1762 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
1763 g_trace_state
[BucketNumber
]));
1766 static inline void Set(const char* category_and_name
) {
1767 TRACE_EVENT_API_ATOMIC_STORE(
1768 g_trace_state
[BucketNumber
],
1769 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD
>(
1770 const_cast<char*>(category_and_name
)));
1774 const char* previous_state_
;
1777 } // namespace trace_event_internal
1780 namespace trace_event
{
1782 template<typename IDType
> class TraceScopedTrackableObject
{
1784 TraceScopedTrackableObject(const char* category_group
, const char* name
,
1786 : category_group_(category_group
),
1789 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_
, name_
, id_
);
1792 template <typename ArgType
> void snapshot(ArgType snapshot
) {
1793 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_
, name_
, id_
, snapshot
);
1796 ~TraceScopedTrackableObject() {
1797 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_
, name_
, id_
);
1801 const char* category_group_
;
1805 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject
);
1808 } // namespace trace_event
1811 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_