Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / trace_event / trace_event.h
blob962493774e8275209288807a7f856bb3277eb5c2
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
15 // Counters
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
30 // category.
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");
44 // ...
45 // }
47 // Additional parameters can be associated with an event:
48 // void doSomethingCostly2(int howMuch) {
49 // TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly",
50 // "howMuch", howMuch);
51 // ...
52 // }
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;
61 // ++send_count;
62 // TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count);
63 // Send(new MyMessage(send_count));
64 // [receive code]
65 // void OnMyMessage(send_count) {
66 // TRACE_EVENT_ASYNC_END0("ipc", "message", send_count);
67 // }
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 {
74 // public:
75 // MyTracedClass() {
76 // TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this);
77 // }
78 // ~MyTracedClass() {
79 // TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this);
80 // }
81 // }
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
88 // thread, however.
90 // Sometimes, you want to track two counters at once. You can do this with two
91 // counter macros:
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
113 // cause problems:
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
122 // macros.
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
142 // happen.
144 // class MyData : public base::trace_event::ConvertableToTraceFormat {
145 // public:
146 // MyData() {}
147 // void AppendAsTraceFormat(std::string* out) const override {
148 // out->append("{\"foo\":1}");
149 // }
150 // private:
151 // ~MyData() override {}
152 // DISALLOW_COPY_AND_ASSIGN(MyData);
153 // };
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.
165 // Thread Safety:
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
168 // is not enabled.
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
183 // again under lock.
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_
192 #include <string>
194 #include "base/atomicops.h"
195 #include "base/time/time.h"
196 #include "base/trace_event/trace_event_impl.h"
197 #include "base/trace_event/trace_event_memory.h"
198 #include "base/trace_event/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.
248 #if OFFICIAL_BUILD
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
253 #endif
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) \
258 (void)0
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
267 #else
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, \
284 arg2_name, arg2_val)
285 #endif
287 // Records a single event called "name" immediately, with 0, 1 or 2
288 // associated arguments. If the category is not enabled, then this
289 // does nothing.
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, \
298 arg1_name, arg1_val)
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, \
311 arg1_val)
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");
338 // ...;
339 // }
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
356 // does nothing.
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, \
369 arg2_name, arg2_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, \
380 arg2_name, arg2_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)
398 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP1( \
399 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val) \
400 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
401 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
402 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
403 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP2( \
404 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val, \
405 arg2_name, arg2_val) \
406 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
407 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
408 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, \
409 arg2_val)
411 // Records a single END event for "name" immediately. If the category
412 // is not enabled, then this does nothing.
413 // - category and name strings must have application lifetime (statics or
414 // literals). They may not include " chars.
415 #define TRACE_EVENT_END0(category_group, name) \
416 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
417 category_group, name, TRACE_EVENT_FLAG_NONE)
418 #define TRACE_EVENT_END1(category_group, name, arg1_name, arg1_val) \
419 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
420 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
421 #define TRACE_EVENT_END2(category_group, name, arg1_name, arg1_val, \
422 arg2_name, arg2_val) \
423 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
424 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
425 arg2_name, arg2_val)
426 #define TRACE_EVENT_COPY_END0(category_group, name) \
427 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
428 category_group, name, TRACE_EVENT_FLAG_COPY)
429 #define TRACE_EVENT_COPY_END1(category_group, name, arg1_name, arg1_val) \
430 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
431 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
432 #define TRACE_EVENT_COPY_END2(category_group, name, arg1_name, arg1_val, \
433 arg2_name, arg2_val) \
434 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
435 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
436 arg2_name, arg2_val)
438 // Similar to TRACE_EVENT_ENDx but with a custom |at| timestamp provided.
439 // - |id| is used to match the _BEGIN event with the _END event.
440 // Events are considered to match if their category_group, name and id values
441 // all match. |id| must either be a pointer or an integer value up to 64 bits.
442 // If it's a pointer, the bits will be xored with a hash of the process ID so
443 // that the same pointer on two different processes will not collide.
444 #define TRACE_EVENT_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
445 name, id, thread_id, timestamp) \
446 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
447 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
448 timestamp, TRACE_EVENT_FLAG_NONE)
449 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0( \
450 category_group, name, id, thread_id, timestamp) \
451 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
452 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
453 timestamp, TRACE_EVENT_FLAG_COPY)
454 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP1( \
455 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val) \
456 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
457 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
458 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
459 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP2( \
460 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val, \
461 arg2_name, arg2_val) \
462 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
463 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
464 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, \
465 arg2_val)
467 // Records the value of a counter called "name" immediately. Value
468 // must be representable as a 32 bit integer.
469 // - category and name strings must have application lifetime (statics or
470 // literals). They may not include " chars.
471 #define TRACE_COUNTER1(category_group, name, value) \
472 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
473 category_group, name, TRACE_EVENT_FLAG_NONE, \
474 "value", static_cast<int>(value))
475 #define TRACE_COPY_COUNTER1(category_group, name, value) \
476 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
477 category_group, name, TRACE_EVENT_FLAG_COPY, \
478 "value", static_cast<int>(value))
480 // Records the values of a multi-parted counter called "name" immediately.
481 // The UI will treat value1 and value2 as parts of a whole, displaying their
482 // values as a stacked-bar chart.
483 // - category and name strings must have application lifetime (statics or
484 // literals). They may not include " chars.
485 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \
486 value2_name, value2_val) \
487 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
488 category_group, name, TRACE_EVENT_FLAG_NONE, \
489 value1_name, static_cast<int>(value1_val), \
490 value2_name, static_cast<int>(value2_val))
491 #define TRACE_COPY_COUNTER2(category_group, name, value1_name, value1_val, \
492 value2_name, value2_val) \
493 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
494 category_group, name, TRACE_EVENT_FLAG_COPY, \
495 value1_name, static_cast<int>(value1_val), \
496 value2_name, static_cast<int>(value2_val))
498 // Records the value of a counter called "name" immediately. Value
499 // must be representable as a 32 bit integer.
500 // - category and name strings must have application lifetime (statics or
501 // literals). They may not include " chars.
502 // - |id| is used to disambiguate counters with the same name. It must either
503 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
504 // will be xored with a hash of the process ID so that the same pointer on
505 // two different processes will not collide.
506 #define TRACE_COUNTER_ID1(category_group, name, id, value) \
507 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
508 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
509 "value", static_cast<int>(value))
510 #define TRACE_COPY_COUNTER_ID1(category_group, name, id, value) \
511 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
512 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
513 "value", static_cast<int>(value))
515 // Records the values of a multi-parted counter called "name" immediately.
516 // The UI will treat value1 and value2 as parts of a whole, displaying their
517 // values as a stacked-bar chart.
518 // - category and name strings must have application lifetime (statics or
519 // literals). They may not include " chars.
520 // - |id| is used to disambiguate counters with the same name. It must either
521 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
522 // will be xored with a hash of the process ID so that the same pointer on
523 // two different processes will not collide.
524 #define TRACE_COUNTER_ID2(category_group, name, id, value1_name, value1_val, \
525 value2_name, value2_val) \
526 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
527 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
528 value1_name, static_cast<int>(value1_val), \
529 value2_name, static_cast<int>(value2_val))
530 #define TRACE_COPY_COUNTER_ID2(category_group, name, id, value1_name, \
531 value1_val, value2_name, value2_val) \
532 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
533 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
534 value1_name, static_cast<int>(value1_val), \
535 value2_name, static_cast<int>(value2_val))
537 // TRACE_EVENT_SAMPLE_* events are injected by the sampling profiler.
538 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP0(category_group, name, \
539 thread_id, timestamp) \
540 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
541 TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
542 TRACE_EVENT_FLAG_NONE)
544 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP1( \
545 category_group, name, thread_id, timestamp, arg1_name, arg1_val) \
546 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
547 TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
548 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
550 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP2(category_group, name, \
551 thread_id, timestamp, \
552 arg1_name, arg1_val, \
553 arg2_name, arg2_val) \
554 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
555 TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
556 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
558 // ASYNC_STEP_* APIs should be only used by legacy code. New code should
559 // consider using NESTABLE_ASYNC_* APIs to describe substeps within an async
560 // event.
561 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
562 // associated arguments. If the category is not enabled, then this
563 // does nothing.
564 // - category and name strings must have application lifetime (statics or
565 // literals). They may not include " chars.
566 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
567 // events are considered to match if their category_group, name and id values
568 // all match. |id| must either be a pointer or an integer value up to 64 bits.
569 // If it's a pointer, the bits will be xored with a hash of the process ID so
570 // that the same pointer on two different processes will not collide.
572 // An asynchronous operation can consist of multiple phases. The first phase is
573 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
574 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will
575 // annotate the block following the call. The ASYNC_STEP_PAST macro will
576 // annotate the block prior to the call. Note that any particular event must use
577 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the
578 // operation completes, call ASYNC_END.
580 // An ASYNC trace typically occurs on a single thread (if not, they will only be
581 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
582 // operation must use the same |name| and |id|. Each step can have its own
583 // args.
584 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id) \
585 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
586 category_group, name, id, TRACE_EVENT_FLAG_NONE)
587 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
588 arg1_val) \
589 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
590 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
591 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
592 arg1_val, arg2_name, arg2_val) \
593 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
594 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
595 arg1_name, arg1_val, arg2_name, arg2_val)
596 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id) \
597 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
598 category_group, name, id, TRACE_EVENT_FLAG_COPY)
599 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
600 arg1_val) \
601 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
602 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
603 arg1_name, arg1_val)
604 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
605 arg1_val, arg2_name, arg2_val) \
606 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
607 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
608 arg1_name, arg1_val, arg2_name, arg2_val)
610 // Similar to TRACE_EVENT_ASYNC_BEGINx but with a custom |at| timestamp
611 // provided.
612 #define TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, \
613 name, id, timestamp) \
614 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
615 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, \
616 static_cast<int>(base::PlatformThread::CurrentId()), \
617 timestamp, TRACE_EVENT_FLAG_NONE)
618 #define TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, \
619 name, id, timestamp) \
620 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
621 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, \
622 static_cast<int>(base::PlatformThread::CurrentId()), \
623 timestamp, TRACE_EVENT_FLAG_COPY)
625 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the
626 // category is not enabled, then this does nothing. The |name| and |id| must
627 // match the ASYNC_BEGIN event above. The |step| param identifies this step
628 // within the async event. This should be called at the beginning of the next
629 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
630 // ASYNC_STEP_PAST events.
631 #define TRACE_EVENT_ASYNC_STEP_INTO0(category_group, name, id, step) \
632 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
633 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
634 #define TRACE_EVENT_ASYNC_STEP_INTO1(category_group, name, id, step, \
635 arg1_name, arg1_val) \
636 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
637 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
638 arg1_name, arg1_val)
640 // Similar to TRACE_EVENT_ASYNC_STEP_INTOx but with a custom |at| timestamp
641 // provided.
642 #define TRACE_EVENT_ASYNC_STEP_INTO_WITH_TIMESTAMP0(category_group, name, \
643 id, step, timestamp) \
644 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
645 TRACE_EVENT_PHASE_ASYNC_STEP_INTO, category_group, name, id, \
646 static_cast<int>(base::PlatformThread::CurrentId()), \
647 timestamp, TRACE_EVENT_FLAG_NONE, "step", step)
649 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the
650 // category is not enabled, then this does nothing. The |name| and |id| must
651 // match the ASYNC_BEGIN event above. The |step| param identifies this step
652 // within the async event. This should be called at the beginning of the next
653 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
654 // ASYNC_STEP_INTO events.
655 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step) \
656 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
657 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
658 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, \
659 arg1_name, arg1_val) \
660 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
661 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
662 arg1_name, arg1_val)
664 // Records a single ASYNC_END event for "name" immediately. If the category
665 // is not enabled, then this does nothing.
666 #define TRACE_EVENT_ASYNC_END0(category_group, name, id) \
667 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
668 category_group, name, id, TRACE_EVENT_FLAG_NONE)
669 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
670 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
671 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
672 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
673 arg2_name, arg2_val) \
674 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
675 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
676 arg1_name, arg1_val, arg2_name, arg2_val)
677 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id) \
678 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
679 category_group, name, id, TRACE_EVENT_FLAG_COPY)
680 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
681 arg1_val) \
682 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
683 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
684 arg1_name, arg1_val)
685 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
686 arg1_val, arg2_name, arg2_val) \
687 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
688 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
689 arg1_name, arg1_val, arg2_name, arg2_val)
691 // Similar to TRACE_EVENT_ASYNC_ENDx but with a custom |at| timestamp provided.
692 #define TRACE_EVENT_ASYNC_END_WITH_TIMESTAMP0(category_group, \
693 name, id, timestamp) \
694 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
695 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, \
696 static_cast<int>(base::PlatformThread::CurrentId()), \
697 timestamp, TRACE_EVENT_FLAG_NONE)
699 // NESTABLE_ASYNC_* APIs are used to describe an async operation, which can
700 // be nested within a NESTABLE_ASYNC event and/or have inner NESTABLE_ASYNC
701 // events.
702 // - category and name strings must have application lifetime (statics or
703 // literals). They may not include " chars.
704 // - A pair of NESTABLE_ASYNC_BEGIN event and NESTABLE_ASYNC_END event is
705 // considered as a match if their category_group, name and id all match.
706 // - |id| must either be a pointer or an integer value up to 64 bits.
707 // If it's a pointer, the bits will be xored with a hash of the process ID so
708 // that the same pointer on two different processes will not collide.
709 // - |id| is used to match a child NESTABLE_ASYNC event with its parent
710 // NESTABLE_ASYNC event. Therefore, events in the same nested event tree must
711 // be logged using the same id and category_group.
713 // Unmatched NESTABLE_ASYNC_END event will be parsed as an event that starts
714 // at the first NESTABLE_ASYNC event of that id, and unmatched
715 // NESTABLE_ASYNC_BEGIN event will be parsed as an event that ends at the last
716 // NESTABLE_ASYNC event of that id. Corresponding warning messages for
717 // unmatched events will be shown in the analysis view.
719 // Records a single NESTABLE_ASYNC_BEGIN event called "name" immediately, with
720 // 0, 1 or 2 associated arguments. If the category is not enabled, then this
721 // does nothing.
722 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_group, name, id) \
723 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
724 category_group, name, id, TRACE_EVENT_FLAG_NONE)
725 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
726 arg1_val) \
727 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
728 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
729 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
730 arg1_val, arg2_name, arg2_val) \
731 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
732 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
733 arg2_name, arg2_val)
734 // Records a single NESTABLE_ASYNC_END event called "name" immediately, with 0
735 // or 2 associated arguments. If the category is not enabled, then this does
736 // nothing.
737 #define TRACE_EVENT_NESTABLE_ASYNC_END0(category_group, name, id) \
738 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
739 category_group, name, id, TRACE_EVENT_FLAG_NONE)
740 #define TRACE_EVENT_NESTABLE_ASYNC_END2(category_group, name, id, arg1_name, \
741 arg1_val, arg2_name, arg2_val) \
742 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
743 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
744 arg2_name, arg2_val)
746 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_BEGIN_WITH_TTS2(category_group, name, \
747 id, arg1_name, arg1_val, arg2_name, arg2_val) \
748 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
749 category_group, name, id, \
750 TRACE_EVENT_FLAG_ASYNC_TTS | TRACE_EVENT_FLAG_COPY, \
751 arg1_name, arg1_val, arg2_name, arg2_val)
752 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_END_WITH_TTS2(category_group, name, \
753 id, arg1_name, arg1_val, arg2_name, arg2_val) \
754 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
755 category_group, name, id, \
756 TRACE_EVENT_FLAG_ASYNC_TTS | TRACE_EVENT_FLAG_COPY, \
757 arg1_name, arg1_val, arg2_name, arg2_val)
759 // Similar to TRACE_EVENT_NESTABLE_ASYNC_{BEGIN,END}x but with a custom
760 // |timestamp| provided.
761 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, name, \
762 id, timestamp) \
763 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
764 TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, category_group, name, id, \
765 static_cast<int>(base::PlatformThread::CurrentId()), timestamp, \
766 TRACE_EVENT_FLAG_NONE)
768 #define TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(category_group, name, \
769 id, timestamp) \
770 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
771 TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, category_group, name, id, \
772 static_cast<int>(base::PlatformThread::CurrentId()), timestamp, \
773 TRACE_EVENT_FLAG_NONE)
775 // Records a single NESTABLE_ASYNC_INSTANT event called "name" immediately,
776 // with 2 associated arguments. If the category is not enabled, then this
777 // does nothing.
778 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT2(category_group, name, id, \
779 arg1_name, arg1_val, arg2_name, arg2_val) \
780 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, \
781 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
782 arg2_name, arg2_val)
784 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
785 // associated arguments. If the category is not enabled, then this
786 // does nothing.
787 // - category and name strings must have application lifetime (statics or
788 // literals). They may not include " chars.
789 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
790 // events are considered to match if their category_group, name and id values
791 // all match. |id| must either be a pointer or an integer value up to 64 bits.
792 // If it's a pointer, the bits will be xored with a hash of the process ID so
793 // that the same pointer on two different processes will not collide.
794 // FLOW events are different from ASYNC events in how they are drawn by the
795 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
796 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
797 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
798 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
799 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
800 // macros. When the operation completes, call FLOW_END. An async operation can
801 // span threads and processes, but all events in that operation must use the
802 // same |name| and |id|. Each event can have its own args.
803 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
804 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
805 category_group, name, id, TRACE_EVENT_FLAG_NONE)
806 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
807 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
808 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
809 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
810 arg2_name, arg2_val) \
811 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
812 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
813 arg1_name, arg1_val, arg2_name, arg2_val)
814 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
815 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
816 category_group, name, id, TRACE_EVENT_FLAG_COPY)
817 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
818 arg1_val) \
819 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
820 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
821 arg1_name, arg1_val)
822 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
823 arg1_val, arg2_name, arg2_val) \
824 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
825 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
826 arg1_name, arg1_val, arg2_name, arg2_val)
828 // Records a single FLOW_STEP event for |step| immediately. If the category
829 // is not enabled, then this does nothing. The |name| and |id| must match the
830 // FLOW_BEGIN event above. The |step| param identifies this step within the
831 // async event. This should be called at the beginning of the next phase of an
832 // asynchronous operation.
833 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
834 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
835 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
836 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \
837 arg1_name, arg1_val) \
838 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
839 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
840 arg1_name, arg1_val)
841 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
842 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
843 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
844 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \
845 arg1_name, arg1_val) \
846 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
847 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
848 arg1_name, arg1_val)
850 // Records a single FLOW_END event for "name" immediately. If the category
851 // is not enabled, then this does nothing.
852 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
853 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
854 category_group, name, id, TRACE_EVENT_FLAG_NONE)
855 #define TRACE_EVENT_FLOW_END_BIND_TO_ENCLOSING0(category_group, name, id) \
856 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
857 category_group, name, id, TRACE_EVENT_FLAG_BIND_TO_ENCLOSING)
858 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
859 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
860 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
861 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
862 arg2_name, arg2_val) \
863 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
864 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
865 arg1_name, arg1_val, arg2_name, arg2_val)
866 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
867 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
868 category_group, name, id, TRACE_EVENT_FLAG_COPY)
869 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
870 arg1_val) \
871 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
872 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
873 arg1_name, arg1_val)
874 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
875 arg1_val, arg2_name, arg2_val) \
876 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
877 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
878 arg1_name, arg1_val, arg2_name, arg2_val)
880 // Macros to track the life time and value of arbitrary client objects.
881 // See also TraceTrackableObject.
882 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
883 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \
884 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
886 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
887 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \
888 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE,\
889 "snapshot", snapshot)
891 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID_AND_TIMESTAMP( \
892 category_group, name, id, timestamp, snapshot) \
893 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
894 TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name, \
895 TRACE_ID_DONT_MANGLE(id), \
896 static_cast<int>(base::PlatformThread::CurrentId()), timestamp, \
897 TRACE_EVENT_FLAG_NONE, "snapshot", snapshot)
899 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
900 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \
901 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
903 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
904 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
905 (base::trace_event::TraceLog::ENABLED_FOR_RECORDING | \
906 base::trace_event::TraceLog::ENABLED_FOR_EVENT_CALLBACK | \
907 base::trace_event::TraceLog::ENABLED_FOR_ETW_EXPORT))
909 // Macro to efficiently determine if a given category group is enabled.
910 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
911 do { \
912 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
913 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
914 *ret = true; \
915 } else { \
916 *ret = false; \
918 } while (0)
920 // Macro to explicitly warm up a given category group. This could be useful in
921 // cases where we want to initialize a category group before any trace events
922 // for that category group is reported. For example, to have a category group
923 // always show up in the "record categories" list for manually selecting
924 // settings in about://tracing.
925 #define TRACE_EVENT_WARMUP_CATEGORY(category_group) \
926 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group)
928 // Macro to efficiently determine, through polling, if a new trace has begun.
929 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
930 do { \
931 static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
932 int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
933 if (num_traces_recorded != -1 && \
934 num_traces_recorded != \
935 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
936 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = \
937 num_traces_recorded; \
938 *ret = true; \
939 } else { \
940 *ret = false; \
942 } while (0)
944 ////////////////////////////////////////////////////////////////////////////////
945 // Implementation specific tracing API definitions.
947 // Get a pointer to the enabled state of the given trace category. Only
948 // long-lived literal strings should be given as the category group. The
949 // returned pointer can be held permanently in a local static for example. If
950 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
951 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
952 // between the load of the tracing state and the call to
953 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
954 // for best performance when tracing is disabled.
955 // const unsigned char*
956 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
957 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
958 base::trace_event::TraceLog::GetCategoryGroupEnabled
960 // Get the number of times traces have been recorded. This is used to implement
961 // the TRACE_EVENT_IS_NEW_TRACE facility.
962 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
963 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
964 base::trace_event::TraceLog::GetInstance()->GetNumTracesRecorded
966 // Add a trace event to the platform tracing system.
967 // base::trace_event::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT(
968 // char phase,
969 // const unsigned char* category_group_enabled,
970 // const char* name,
971 // unsigned long long id,
972 // unsigned long long context_id,
973 // int num_args,
974 // const char** arg_names,
975 // const unsigned char* arg_types,
976 // const unsigned long long* arg_values,
977 // unsigned int flags)
978 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
979 base::trace_event::TraceLog::GetInstance()->AddTraceEvent
981 // Add a trace event to the platform tracing system.
982 // base::trace_event::TraceEventHandle
983 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID(
984 // char phase,
985 // const unsigned char* category_group_enabled,
986 // const char* name,
987 // unsigned long long id,
988 // unsigned long long context_id,
989 // int num_args,
990 // const char** arg_names,
991 // const unsigned char* arg_types,
992 // const unsigned long long* arg_values,
993 // unsigned int flags)
994 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID \
995 base::trace_event::TraceLog::GetInstance()->AddTraceEventWithContextId
997 // Add a trace event to the platform tracing system.
998 // base::trace_event::TraceEventHandle
999 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
1000 // char phase,
1001 // const unsigned char* category_group_enabled,
1002 // const char* name,
1003 // unsigned long long id,
1004 // unsigned long long context_id,
1005 // int thread_id,
1006 // const TraceTicks& timestamp,
1007 // int num_args,
1008 // const char** arg_names,
1009 // const unsigned char* arg_types,
1010 // const unsigned long long* arg_values,
1011 // unsigned int flags)
1012 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
1013 base::trace_event::TraceLog::GetInstance() \
1014 ->AddTraceEventWithThreadIdAndTimestamp
1016 // Set the duration field of a COMPLETE trace event.
1017 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1018 // const unsigned char* category_group_enabled,
1019 // const char* name,
1020 // base::trace_event::TraceEventHandle id)
1021 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
1022 base::trace_event::TraceLog::GetInstance()->UpdateTraceEventDuration
1024 // Defines atomic operations used internally by the tracing system.
1025 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
1026 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
1027 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
1028 base::subtle::NoBarrier_Store(&(var), (value))
1030 // Defines visibility for classes in trace_event.h
1031 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
1033 // The thread buckets for the sampling profiler.
1034 TRACE_EVENT_API_CLASS_EXPORT extern \
1035 TRACE_EVENT_API_ATOMIC_WORD g_trace_state[3];
1037 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
1038 g_trace_state[thread_bucket]
1040 ////////////////////////////////////////////////////////////////////////////////
1042 // Implementation detail: trace event macros create temporary variables
1043 // to keep instrumentation overhead low. These macros give each temporary
1044 // variable a unique name based on the line number to prevent name collisions.
1045 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
1046 trace_event_unique_##a##b
1047 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
1048 INTERNAL_TRACE_EVENT_UID3(a,b)
1049 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
1050 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
1052 // Implementation detail: internal macro to create static category.
1053 // No barriers are needed, because this code is designed to operate safely
1054 // even when the unsigned char* points to garbage data (which may be the case
1055 // on processors without cache coherency).
1056 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
1057 category_group, atomic, category_group_enabled) \
1058 category_group_enabled = \
1059 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
1060 atomic)); \
1061 if (UNLIKELY(!category_group_enabled)) { \
1062 category_group_enabled = \
1063 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
1064 TRACE_EVENT_API_ATOMIC_STORE(atomic, \
1065 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
1066 category_group_enabled)); \
1069 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
1070 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
1071 const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
1072 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES(category_group, \
1073 INTERNAL_TRACE_EVENT_UID(atomic), \
1074 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
1076 // Implementation detail: internal macro to create static category and add
1077 // event if the category is enabled.
1078 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
1079 do { \
1080 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1081 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1082 trace_event_internal::AddTraceEvent( \
1083 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
1084 trace_event_internal::kNoId, flags, ##__VA_ARGS__); \
1086 } while (0)
1088 // Implementation detail: internal macro to create static category and add begin
1089 // event if the category is enabled. Also adds the end event when the scope
1090 // ends.
1091 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
1092 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1093 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
1094 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1095 base::trace_event::TraceEventHandle h = \
1096 trace_event_internal::AddTraceEvent( \
1097 TRACE_EVENT_PHASE_COMPLETE, \
1098 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
1099 trace_event_internal::kNoId, TRACE_EVENT_FLAG_NONE, \
1100 ##__VA_ARGS__); \
1101 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
1102 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
1105 // Implementation detail: internal macro to create static category and add
1106 // event if the category is enabled.
1107 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
1108 flags, ...) \
1109 do { \
1110 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1111 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1112 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1113 trace_event_internal::TraceID trace_event_trace_id( \
1114 id, &trace_event_flags); \
1115 trace_event_internal::AddTraceEvent( \
1116 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1117 name, trace_event_trace_id.data(), trace_event_flags, \
1118 ##__VA_ARGS__); \
1120 } while (0)
1122 // Implementation detail: internal macro to create static category and add
1123 // event if the category is enabled.
1124 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
1125 category_group, name, id, thread_id, timestamp, flags, ...) \
1126 do { \
1127 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1128 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1129 unsigned int trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1130 trace_event_internal::TraceID trace_event_trace_id( \
1131 id, &trace_event_flags); \
1132 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
1133 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1134 name, trace_event_trace_id.data(), trace_event_internal::kNoId, \
1135 thread_id, base::TraceTicks::FromInternalValue(timestamp), \
1136 trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
1137 ##__VA_ARGS__); \
1139 } while (0)
1141 // Notes regarding the following definitions:
1142 // New values can be added and propagated to third party libraries, but existing
1143 // definitions must never be changed, because third party libraries may use old
1144 // definitions.
1146 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
1147 #define TRACE_EVENT_PHASE_BEGIN ('B')
1148 #define TRACE_EVENT_PHASE_END ('E')
1149 #define TRACE_EVENT_PHASE_COMPLETE ('X')
1150 #define TRACE_EVENT_PHASE_INSTANT ('I')
1151 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
1152 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T')
1153 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p')
1154 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
1155 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN ('b')
1156 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_END ('e')
1157 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT ('n')
1158 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
1159 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
1160 #define TRACE_EVENT_PHASE_FLOW_END ('f')
1161 #define TRACE_EVENT_PHASE_METADATA ('M')
1162 #define TRACE_EVENT_PHASE_COUNTER ('C')
1163 #define TRACE_EVENT_PHASE_SAMPLE ('P')
1164 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
1165 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
1166 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
1167 #define TRACE_EVENT_PHASE_MEMORY_DUMP ('v')
1169 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
1170 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned int>(0))
1171 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned int>(1 << 0))
1172 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned int>(1 << 1))
1173 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned int>(1 << 2))
1174 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned int>(1 << 3))
1175 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned int>(1 << 4))
1176 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned int>(1 << 5))
1177 #define TRACE_EVENT_FLAG_ASYNC_TTS (static_cast<unsigned int>(1 << 6))
1178 #define TRACE_EVENT_FLAG_BIND_TO_ENCLOSING (static_cast<unsigned int>(1 << 7))
1179 #define TRACE_EVENT_FLAG_HAS_CONTEXT_ID (static_cast<unsigned int>(1 << 10))
1181 #define TRACE_EVENT_FLAG_SCOPE_MASK (static_cast<unsigned int>( \
1182 TRACE_EVENT_FLAG_SCOPE_OFFSET | TRACE_EVENT_FLAG_SCOPE_EXTRA))
1184 // Type values for identifying types in the TraceValue union.
1185 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
1186 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
1187 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
1188 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
1189 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
1190 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
1191 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
1192 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
1194 // Enum reflecting the scope of an INSTANT event. Must fit within
1195 // TRACE_EVENT_FLAG_SCOPE_MASK.
1196 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
1197 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
1198 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
1200 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
1201 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
1202 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
1204 namespace trace_event_internal {
1206 // Specify these values when the corresponding argument of AddTraceEvent is not
1207 // used.
1208 const int kZeroNumArgs = 0;
1209 const unsigned long long kNoId = 0;
1211 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
1212 // are by default mangled with the Process ID so that they are unlikely to
1213 // collide when the same pointer is used on different processes.
1214 class TraceID {
1215 public:
1216 class DontMangle {
1217 public:
1218 explicit DontMangle(const void* id)
1219 : data_(static_cast<unsigned long long>(
1220 reinterpret_cast<uintptr_t>(id))) {}
1221 explicit DontMangle(unsigned long long id) : data_(id) {}
1222 explicit DontMangle(unsigned long id) : data_(id) {}
1223 explicit DontMangle(unsigned int id) : data_(id) {}
1224 explicit DontMangle(unsigned short id) : data_(id) {}
1225 explicit DontMangle(unsigned char id) : data_(id) {}
1226 explicit DontMangle(long long id)
1227 : data_(static_cast<unsigned long long>(id)) {}
1228 explicit DontMangle(long id)
1229 : data_(static_cast<unsigned long long>(id)) {}
1230 explicit DontMangle(int id)
1231 : data_(static_cast<unsigned long long>(id)) {}
1232 explicit DontMangle(short id)
1233 : data_(static_cast<unsigned long long>(id)) {}
1234 explicit DontMangle(signed char id)
1235 : data_(static_cast<unsigned long long>(id)) {}
1236 unsigned long long data() const { return data_; }
1237 private:
1238 unsigned long long data_;
1241 class ForceMangle {
1242 public:
1243 explicit ForceMangle(unsigned long long id) : data_(id) {}
1244 explicit ForceMangle(unsigned long id) : data_(id) {}
1245 explicit ForceMangle(unsigned int id) : data_(id) {}
1246 explicit ForceMangle(unsigned short id) : data_(id) {}
1247 explicit ForceMangle(unsigned char id) : data_(id) {}
1248 explicit ForceMangle(long long id)
1249 : data_(static_cast<unsigned long long>(id)) {}
1250 explicit ForceMangle(long id)
1251 : data_(static_cast<unsigned long long>(id)) {}
1252 explicit ForceMangle(int id)
1253 : data_(static_cast<unsigned long long>(id)) {}
1254 explicit ForceMangle(short id)
1255 : data_(static_cast<unsigned long long>(id)) {}
1256 explicit ForceMangle(signed char id)
1257 : data_(static_cast<unsigned long long>(id)) {}
1258 unsigned long long data() const { return data_; }
1259 private:
1260 unsigned long long data_;
1262 TraceID(const void* id, unsigned int* flags)
1263 : data_(static_cast<unsigned long long>(
1264 reinterpret_cast<uintptr_t>(id))) {
1265 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1267 TraceID(ForceMangle id, unsigned int* flags) : data_(id.data()) {
1268 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1270 TraceID(DontMangle id, unsigned int* flags) : data_(id.data()) {
1272 TraceID(unsigned long long id, unsigned int* flags)
1273 : data_(id) { (void)flags; }
1274 TraceID(unsigned long id, unsigned int* flags)
1275 : data_(id) { (void)flags; }
1276 TraceID(unsigned int id, unsigned int* flags)
1277 : data_(id) { (void)flags; }
1278 TraceID(unsigned short id, unsigned int* flags)
1279 : data_(id) { (void)flags; }
1280 TraceID(unsigned char id, unsigned int* flags)
1281 : data_(id) { (void)flags; }
1282 TraceID(long long id, unsigned int* flags)
1283 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1284 TraceID(long id, unsigned int* flags)
1285 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1286 TraceID(int id, unsigned int* flags)
1287 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1288 TraceID(short id, unsigned int* flags)
1289 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1290 TraceID(signed char id, unsigned int* flags)
1291 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1293 unsigned long long data() const { return data_; }
1295 private:
1296 unsigned long long data_;
1299 // Simple union to store various types as unsigned long long.
1300 union TraceValueUnion {
1301 bool as_bool;
1302 unsigned long long as_uint;
1303 long long as_int;
1304 double as_double;
1305 const void* as_pointer;
1306 const char* as_string;
1309 // Simple container for const char* that should be copied instead of retained.
1310 class TraceStringWithCopy {
1311 public:
1312 explicit TraceStringWithCopy(const char* str) : str_(str) {}
1313 const char* str() const { return str_; }
1314 private:
1315 const char* str_;
1318 // Define SetTraceValue for each allowed type. It stores the type and
1319 // value in the return arguments. This allows this API to avoid declaring any
1320 // structures so that it is portable to third_party libraries.
1321 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
1322 arg_expression, \
1323 union_member, \
1324 value_type_id) \
1325 static inline void SetTraceValue( \
1326 actual_type arg, \
1327 unsigned char* type, \
1328 unsigned long long* value) { \
1329 TraceValueUnion type_value; \
1330 type_value.union_member = arg_expression; \
1331 *type = value_type_id; \
1332 *value = type_value.as_uint; \
1334 // Simpler form for int types that can be safely casted.
1335 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
1336 value_type_id) \
1337 static inline void SetTraceValue( \
1338 actual_type arg, \
1339 unsigned char* type, \
1340 unsigned long long* value) { \
1341 *type = value_type_id; \
1342 *value = static_cast<unsigned long long>(arg); \
1345 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
1346 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
1347 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
1348 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
1349 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
1350 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
1351 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
1352 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
1353 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
1354 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
1355 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg, as_bool, TRACE_VALUE_TYPE_BOOL)
1356 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg, as_double,
1357 TRACE_VALUE_TYPE_DOUBLE)
1358 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, arg, as_pointer,
1359 TRACE_VALUE_TYPE_POINTER)
1360 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, arg, as_string,
1361 TRACE_VALUE_TYPE_STRING)
1362 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, arg.str(),
1363 as_string, TRACE_VALUE_TYPE_COPY_STRING)
1365 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
1366 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
1368 // std::string version of SetTraceValue so that trace arguments can be strings.
1369 static inline void SetTraceValue(const std::string& arg,
1370 unsigned char* type,
1371 unsigned long long* value) {
1372 TraceValueUnion type_value;
1373 type_value.as_string = arg.c_str();
1374 *type = TRACE_VALUE_TYPE_COPY_STRING;
1375 *value = type_value.as_uint;
1378 // base::Time, base::TimeTicks, etc. versions of SetTraceValue to make it easier
1379 // to trace these types.
1380 static inline void SetTraceValue(const base::Time arg,
1381 unsigned char* type,
1382 unsigned long long* value) {
1383 *type = TRACE_VALUE_TYPE_INT;
1384 *value = arg.ToInternalValue();
1387 static inline void SetTraceValue(const base::TimeTicks arg,
1388 unsigned char* type,
1389 unsigned long long* value) {
1390 *type = TRACE_VALUE_TYPE_INT;
1391 *value = arg.ToInternalValue();
1394 static inline void SetTraceValue(const base::ThreadTicks arg,
1395 unsigned char* type,
1396 unsigned long long* value) {
1397 *type = TRACE_VALUE_TYPE_INT;
1398 *value = arg.ToInternalValue();
1401 static inline void SetTraceValue(const base::TraceTicks arg,
1402 unsigned char* type,
1403 unsigned long long* value) {
1404 *type = TRACE_VALUE_TYPE_INT;
1405 *value = arg.ToInternalValue();
1408 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
1409 // functions are defined here instead of in the macro, because the arg_values
1410 // could be temporary objects, such as std::string. In order to store
1411 // pointers to the internal c_str and pass through to the tracing API,
1412 // the arg_values must live throughout these procedures.
1414 static inline base::trace_event::TraceEventHandle
1415 AddTraceEventWithThreadIdAndTimestamp(
1416 char phase,
1417 const unsigned char* category_group_enabled,
1418 const char* name,
1419 unsigned long long id,
1420 unsigned long long context_id,
1421 int thread_id,
1422 const base::TraceTicks& timestamp,
1423 unsigned int flags,
1424 const char* arg1_name,
1425 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1426 arg1_val) {
1427 const int num_args = 1;
1428 unsigned char arg_types[1] = { TRACE_VALUE_TYPE_CONVERTABLE };
1429 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1430 phase, category_group_enabled, name, id, context_id, thread_id,
1431 timestamp, num_args, &arg1_name, arg_types, NULL, &arg1_val, flags);
1434 template<class ARG1_TYPE>
1435 static inline base::trace_event::TraceEventHandle
1436 AddTraceEventWithThreadIdAndTimestamp(
1437 char phase,
1438 const unsigned char* category_group_enabled,
1439 const char* name,
1440 unsigned long long id,
1441 unsigned long long context_id,
1442 int thread_id,
1443 const base::TraceTicks& timestamp,
1444 unsigned int flags,
1445 const char* arg1_name,
1446 const ARG1_TYPE& arg1_val,
1447 const char* arg2_name,
1448 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1449 arg2_val) {
1450 const int num_args = 2;
1451 const char* arg_names[2] = { arg1_name, arg2_name };
1453 unsigned char arg_types[2];
1454 unsigned long long arg_values[2];
1455 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1456 arg_types[1] = TRACE_VALUE_TYPE_CONVERTABLE;
1458 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1459 convertable_values[2];
1460 convertable_values[1] = arg2_val;
1462 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1463 phase, category_group_enabled, name, id, context_id, thread_id,
1464 timestamp, num_args, arg_names, arg_types, arg_values,
1465 convertable_values, flags);
1468 template<class ARG2_TYPE>
1469 static inline base::trace_event::TraceEventHandle
1470 AddTraceEventWithThreadIdAndTimestamp(
1471 char phase,
1472 const unsigned char* category_group_enabled,
1473 const char* name,
1474 unsigned long long id,
1475 unsigned long long context_id,
1476 int thread_id,
1477 const base::TraceTicks& timestamp,
1478 unsigned int flags,
1479 const char* arg1_name,
1480 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
1481 const char* arg2_name,
1482 const ARG2_TYPE& arg2_val) {
1483 const int num_args = 2;
1484 const char* arg_names[2] = { arg1_name, arg2_name };
1486 unsigned char arg_types[2];
1487 unsigned long long arg_values[2];
1488 arg_types[0] = TRACE_VALUE_TYPE_CONVERTABLE;
1489 arg_values[0] = 0;
1490 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1492 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1493 convertable_values[2];
1494 convertable_values[0] = arg1_val;
1496 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1497 phase, category_group_enabled, name, id, context_id, thread_id,
1498 timestamp, num_args, arg_names, arg_types, arg_values,
1499 convertable_values, flags);
1502 static inline base::trace_event::TraceEventHandle
1503 AddTraceEventWithThreadIdAndTimestamp(
1504 char phase,
1505 const unsigned char* category_group_enabled,
1506 const char* name,
1507 unsigned long long id,
1508 unsigned long long context_id,
1509 int thread_id,
1510 const base::TraceTicks& timestamp,
1511 unsigned int flags,
1512 const char* arg1_name,
1513 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
1514 const char* arg2_name,
1515 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1516 arg2_val) {
1517 const int num_args = 2;
1518 const char* arg_names[2] = { arg1_name, arg2_name };
1519 unsigned char arg_types[2] =
1520 { TRACE_VALUE_TYPE_CONVERTABLE, TRACE_VALUE_TYPE_CONVERTABLE };
1521 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1522 convertable_values[2] = {arg1_val, arg2_val};
1524 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1525 phase, category_group_enabled, name, id, context_id, thread_id,
1526 timestamp, num_args, arg_names, arg_types, NULL, convertable_values,
1527 flags);
1530 static inline base::trace_event::TraceEventHandle
1531 AddTraceEventWithThreadIdAndTimestamp(
1532 char phase,
1533 const unsigned char* category_group_enabled,
1534 const char* name,
1535 unsigned long long id,
1536 unsigned long long context_id,
1537 int thread_id,
1538 const base::TraceTicks& timestamp,
1539 unsigned int flags) {
1540 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1541 phase, category_group_enabled, name, id, context_id, thread_id,
1542 timestamp, kZeroNumArgs, NULL, NULL, NULL, NULL, flags);
1545 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1546 char phase,
1547 const unsigned char* category_group_enabled,
1548 const char* name,
1549 unsigned long long id,
1550 unsigned int flags) {
1551 const int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1552 const base::TraceTicks now = base::TraceTicks::Now();
1553 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1554 name, id, kNoId, thread_id, now,
1555 flags);
1558 template<class ARG1_TYPE>
1559 static inline base::trace_event::TraceEventHandle
1560 AddTraceEventWithThreadIdAndTimestamp(
1561 char phase,
1562 const unsigned char* category_group_enabled,
1563 const char* name,
1564 unsigned long long id,
1565 unsigned long long context_id,
1566 int thread_id,
1567 const base::TraceTicks& timestamp,
1568 unsigned int flags,
1569 const char* arg1_name,
1570 const ARG1_TYPE& arg1_val) {
1571 const int num_args = 1;
1572 unsigned char arg_types[1];
1573 unsigned long long arg_values[1];
1574 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1575 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1576 phase, category_group_enabled, name, id, context_id, thread_id,
1577 timestamp, num_args, &arg1_name, arg_types, arg_values, NULL, flags);
1580 template<class ARG1_TYPE>
1581 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1582 char phase,
1583 const unsigned char* category_group_enabled,
1584 const char* name,
1585 unsigned long long id,
1586 unsigned int flags,
1587 const char* arg1_name,
1588 const ARG1_TYPE& arg1_val) {
1589 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1590 base::TraceTicks now = base::TraceTicks::Now();
1591 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1592 name, id, kNoId, thread_id, now,
1593 flags, arg1_name, arg1_val);
1596 template<class ARG1_TYPE, class ARG2_TYPE>
1597 static inline base::trace_event::TraceEventHandle
1598 AddTraceEventWithThreadIdAndTimestamp(
1599 char phase,
1600 const unsigned char* category_group_enabled,
1601 const char* name,
1602 unsigned long long id,
1603 unsigned long long context_id,
1604 int thread_id,
1605 const base::TraceTicks& timestamp,
1606 unsigned int flags,
1607 const char* arg1_name,
1608 const ARG1_TYPE& arg1_val,
1609 const char* arg2_name,
1610 const ARG2_TYPE& arg2_val) {
1611 const int num_args = 2;
1612 const char* arg_names[2] = { arg1_name, arg2_name };
1613 unsigned char arg_types[2];
1614 unsigned long long arg_values[2];
1615 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1616 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1617 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1618 phase, category_group_enabled, name, id, context_id, thread_id,
1619 timestamp, num_args, arg_names, arg_types, arg_values, NULL, flags);
1622 template<class ARG1_TYPE, class ARG2_TYPE>
1623 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1624 char phase,
1625 const unsigned char* category_group_enabled,
1626 const char* name,
1627 unsigned long long id,
1628 unsigned int flags,
1629 const char* arg1_name,
1630 const ARG1_TYPE& arg1_val,
1631 const char* arg2_name,
1632 const ARG2_TYPE& arg2_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,
1637 flags, arg1_name, arg1_val,
1638 arg2_name, arg2_val);
1641 // Used by TRACE_EVENTx macros. Do not use directly.
1642 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
1643 public:
1644 // Note: members of data_ intentionally left uninitialized. See Initialize.
1645 ScopedTracer() : p_data_(NULL) {}
1647 ~ScopedTracer() {
1648 if (p_data_ && *data_.category_group_enabled)
1649 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1650 data_.category_group_enabled, data_.name, data_.event_handle);
1653 void Initialize(const unsigned char* category_group_enabled,
1654 const char* name,
1655 base::trace_event::TraceEventHandle event_handle) {
1656 data_.category_group_enabled = category_group_enabled;
1657 data_.name = name;
1658 data_.event_handle = event_handle;
1659 p_data_ = &data_;
1662 private:
1663 // This Data struct workaround is to avoid initializing all the members
1664 // in Data during construction of this object, since this object is always
1665 // constructed, even when tracing is disabled. If the members of Data were
1666 // members of this class instead, compiler warnings occur about potential
1667 // uninitialized accesses.
1668 struct Data {
1669 const unsigned char* category_group_enabled;
1670 const char* name;
1671 base::trace_event::TraceEventHandle event_handle;
1673 Data* p_data_;
1674 Data data_;
1677 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
1678 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
1679 public:
1680 ScopedTraceBinaryEfficient(const char* category_group, const char* name);
1681 ~ScopedTraceBinaryEfficient();
1683 private:
1684 const unsigned char* category_group_enabled_;
1685 const char* name_;
1686 base::trace_event::TraceEventHandle event_handle_;
1689 // This macro generates less code then TRACE_EVENT0 but is also
1690 // slower to execute when tracing is off. It should generally only be
1691 // used with code that is seldom executed or conditionally executed
1692 // when debugging.
1693 // For now the category_group must be "gpu".
1694 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
1695 trace_event_internal::ScopedTraceBinaryEfficient \
1696 INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
1698 // TraceEventSamplingStateScope records the current sampling state
1699 // and sets a new sampling state. When the scope exists, it restores
1700 // the sampling state having recorded.
1701 template<size_t BucketNumber>
1702 class TraceEventSamplingStateScope {
1703 public:
1704 TraceEventSamplingStateScope(const char* category_and_name) {
1705 previous_state_ = TraceEventSamplingStateScope<BucketNumber>::Current();
1706 TraceEventSamplingStateScope<BucketNumber>::Set(category_and_name);
1709 ~TraceEventSamplingStateScope() {
1710 TraceEventSamplingStateScope<BucketNumber>::Set(previous_state_);
1713 static inline const char* Current() {
1714 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
1715 g_trace_state[BucketNumber]));
1718 static inline void Set(const char* category_and_name) {
1719 TRACE_EVENT_API_ATOMIC_STORE(
1720 g_trace_state[BucketNumber],
1721 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
1722 const_cast<char*>(category_and_name)));
1725 private:
1726 const char* previous_state_;
1729 } // namespace trace_event_internal
1731 namespace base {
1732 namespace trace_event {
1734 template<typename IDType> class TraceScopedTrackableObject {
1735 public:
1736 TraceScopedTrackableObject(const char* category_group, const char* name,
1737 IDType id)
1738 : category_group_(category_group),
1739 name_(name),
1740 id_(id) {
1741 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_, name_, id_);
1744 template <typename ArgType> void snapshot(ArgType snapshot) {
1745 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_, name_, id_, snapshot);
1748 ~TraceScopedTrackableObject() {
1749 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_, name_, id_);
1752 private:
1753 const char* category_group_;
1754 const char* name_;
1755 IDType id_;
1757 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
1760 } // namespace trace_event
1761 } // namespace base
1763 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_