Use a less compiler-specific way for preventing inlining / tail-call optimization.
[chromium-blink-merge.git] / base / trace_event / trace_event.h
blob1bf9429a180f16d88bdf7d234359f086825bf8db
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 // virtual void AppendAsTraceFormat(std::string* out) const override {
148 // out->append("{\"foo\":1}");
149 // }
150 // private:
151 // virtual ~MyData() {}
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)
404 // Records a single END event for "name" immediately. If the category
405 // is not enabled, then this does nothing.
406 // - category and name strings must have application lifetime (statics or
407 // literals). They may not include " chars.
408 #define TRACE_EVENT_END0(category_group, name) \
409 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
410 category_group, name, TRACE_EVENT_FLAG_NONE)
411 #define TRACE_EVENT_END1(category_group, name, arg1_name, arg1_val) \
412 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
413 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
414 #define TRACE_EVENT_END2(category_group, name, arg1_name, arg1_val, \
415 arg2_name, arg2_val) \
416 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
417 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
418 arg2_name, arg2_val)
419 #define TRACE_EVENT_COPY_END0(category_group, name) \
420 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
421 category_group, name, TRACE_EVENT_FLAG_COPY)
422 #define TRACE_EVENT_COPY_END1(category_group, name, arg1_name, arg1_val) \
423 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
424 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
425 #define TRACE_EVENT_COPY_END2(category_group, name, arg1_name, arg1_val, \
426 arg2_name, arg2_val) \
427 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
428 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
429 arg2_name, arg2_val)
431 // Similar to TRACE_EVENT_ENDx but with a custom |at| timestamp provided.
432 // - |id| is used to match the _BEGIN event with the _END event.
433 // Events are considered to match if their category_group, name and id values
434 // all match. |id| must either be a pointer or an integer value up to 64 bits.
435 // If it's a pointer, the bits will be xored with a hash of the process ID so
436 // that the same pointer on two different processes will not collide.
437 #define TRACE_EVENT_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
438 name, id, thread_id, timestamp) \
439 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
440 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
441 timestamp, TRACE_EVENT_FLAG_NONE)
442 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0( \
443 category_group, name, id, thread_id, timestamp) \
444 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
445 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
446 timestamp, TRACE_EVENT_FLAG_COPY)
447 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP1( \
448 category_group, name, id, thread_id, timestamp, arg1_name, arg1_val) \
449 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
450 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
451 timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
453 // Records the value of a counter called "name" immediately. Value
454 // must be representable as a 32 bit integer.
455 // - category and name strings must have application lifetime (statics or
456 // literals). They may not include " chars.
457 #define TRACE_COUNTER1(category_group, name, value) \
458 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
459 category_group, name, TRACE_EVENT_FLAG_NONE, \
460 "value", static_cast<int>(value))
461 #define TRACE_COPY_COUNTER1(category_group, name, value) \
462 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
463 category_group, name, TRACE_EVENT_FLAG_COPY, \
464 "value", static_cast<int>(value))
466 // Records the values of a multi-parted counter called "name" immediately.
467 // The UI will treat value1 and value2 as parts of a whole, displaying their
468 // values as a stacked-bar chart.
469 // - category and name strings must have application lifetime (statics or
470 // literals). They may not include " chars.
471 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \
472 value2_name, value2_val) \
473 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
474 category_group, name, TRACE_EVENT_FLAG_NONE, \
475 value1_name, static_cast<int>(value1_val), \
476 value2_name, static_cast<int>(value2_val))
477 #define TRACE_COPY_COUNTER2(category_group, name, value1_name, value1_val, \
478 value2_name, value2_val) \
479 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
480 category_group, name, TRACE_EVENT_FLAG_COPY, \
481 value1_name, static_cast<int>(value1_val), \
482 value2_name, static_cast<int>(value2_val))
484 // Records the value of a counter called "name" immediately. Value
485 // must be representable as a 32 bit integer.
486 // - category and name strings must have application lifetime (statics or
487 // literals). They may not include " chars.
488 // - |id| is used to disambiguate counters with the same name. It must either
489 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
490 // will be xored with a hash of the process ID so that the same pointer on
491 // two different processes will not collide.
492 #define TRACE_COUNTER_ID1(category_group, name, id, value) \
493 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
494 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
495 "value", static_cast<int>(value))
496 #define TRACE_COPY_COUNTER_ID1(category_group, name, id, value) \
497 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
498 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
499 "value", static_cast<int>(value))
501 // Records the values of a multi-parted counter called "name" immediately.
502 // The UI will treat value1 and value2 as parts of a whole, displaying their
503 // values as a stacked-bar chart.
504 // - category and name strings must have application lifetime (statics or
505 // literals). They may not include " chars.
506 // - |id| is used to disambiguate counters with the same name. It must either
507 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
508 // will be xored with a hash of the process ID so that the same pointer on
509 // two different processes will not collide.
510 #define TRACE_COUNTER_ID2(category_group, name, id, value1_name, value1_val, \
511 value2_name, value2_val) \
512 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
513 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
514 value1_name, static_cast<int>(value1_val), \
515 value2_name, static_cast<int>(value2_val))
516 #define TRACE_COPY_COUNTER_ID2(category_group, name, id, value1_name, \
517 value1_val, value2_name, value2_val) \
518 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
519 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
520 value1_name, static_cast<int>(value1_val), \
521 value2_name, static_cast<int>(value2_val))
523 // TRACE_EVENT_SAMPLE_* events are injected by the sampling profiler.
524 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP0(category_group, name, \
525 thread_id, timestamp) \
526 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
527 TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
528 TRACE_EVENT_FLAG_NONE)
530 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP1( \
531 category_group, name, thread_id, timestamp, arg1_name, arg1_val) \
532 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
533 TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
534 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
536 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP2(category_group, name, \
537 thread_id, timestamp, \
538 arg1_name, arg1_val, \
539 arg2_name, arg2_val) \
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, arg1_name, arg1_val, arg2_name, arg2_val)
544 // ASYNC_STEP_* APIs should be only used by legacy code. New code should
545 // consider using NESTABLE_ASYNC_* APIs to describe substeps within an async
546 // event.
547 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
548 // associated arguments. If the category is not enabled, then this
549 // does nothing.
550 // - category and name strings must have application lifetime (statics or
551 // literals). They may not include " chars.
552 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
553 // events are considered to match if their category_group, name and id values
554 // all match. |id| must either be a pointer or an integer value up to 64 bits.
555 // If it's a pointer, the bits will be xored with a hash of the process ID so
556 // that the same pointer on two different processes will not collide.
558 // An asynchronous operation can consist of multiple phases. The first phase is
559 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
560 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will
561 // annotate the block following the call. The ASYNC_STEP_PAST macro will
562 // annotate the block prior to the call. Note that any particular event must use
563 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the
564 // operation completes, call ASYNC_END.
566 // An ASYNC trace typically occurs on a single thread (if not, they will only be
567 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
568 // operation must use the same |name| and |id|. Each step can have its own
569 // args.
570 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id) \
571 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
572 category_group, name, id, TRACE_EVENT_FLAG_NONE)
573 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
574 arg1_val) \
575 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
576 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
577 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
578 arg1_val, arg2_name, arg2_val) \
579 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
580 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
581 arg1_name, arg1_val, arg2_name, arg2_val)
582 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id) \
583 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
584 category_group, name, id, TRACE_EVENT_FLAG_COPY)
585 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
586 arg1_val) \
587 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
588 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
589 arg1_name, arg1_val)
590 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
591 arg1_val, arg2_name, arg2_val) \
592 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
593 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
594 arg1_name, arg1_val, arg2_name, arg2_val)
596 // Similar to TRACE_EVENT_ASYNC_BEGINx but with a custom |at| timestamp
597 // provided.
598 #define TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, \
599 name, id, timestamp) \
600 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
601 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, \
602 static_cast<int>(base::PlatformThread::CurrentId()), \
603 timestamp, TRACE_EVENT_FLAG_NONE)
605 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the
606 // category is not enabled, then this does nothing. The |name| and |id| must
607 // match the ASYNC_BEGIN event above. The |step| param identifies this step
608 // within the async event. This should be called at the beginning of the next
609 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
610 // ASYNC_STEP_PAST events.
611 #define TRACE_EVENT_ASYNC_STEP_INTO0(category_group, name, id, step) \
612 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
613 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
614 #define TRACE_EVENT_ASYNC_STEP_INTO1(category_group, name, id, step, \
615 arg1_name, arg1_val) \
616 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
617 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
618 arg1_name, arg1_val)
620 // Similar to TRACE_EVENT_ASYNC_STEP_INTOx but with a custom |at| timestamp
621 // provided.
622 #define TRACE_EVENT_ASYNC_STEP_INTO_WITH_TIMESTAMP0(category_group, name, \
623 id, step, timestamp) \
624 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
625 TRACE_EVENT_PHASE_ASYNC_STEP_INTO, category_group, name, id, \
626 static_cast<int>(base::PlatformThread::CurrentId()), \
627 timestamp, TRACE_EVENT_FLAG_NONE, "step", step)
629 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the
630 // category is not enabled, then this does nothing. The |name| and |id| must
631 // match the ASYNC_BEGIN event above. The |step| param identifies this step
632 // within the async event. This should be called at the beginning of the next
633 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
634 // ASYNC_STEP_INTO events.
635 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step) \
636 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
637 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
638 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, \
639 arg1_name, arg1_val) \
640 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
641 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
642 arg1_name, arg1_val)
644 // Records a single ASYNC_END event for "name" immediately. If the category
645 // is not enabled, then this does nothing.
646 #define TRACE_EVENT_ASYNC_END0(category_group, name, id) \
647 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
648 category_group, name, id, TRACE_EVENT_FLAG_NONE)
649 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
650 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
651 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
652 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
653 arg2_name, arg2_val) \
654 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
655 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
656 arg1_name, arg1_val, arg2_name, arg2_val)
657 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id) \
658 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
659 category_group, name, id, TRACE_EVENT_FLAG_COPY)
660 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
661 arg1_val) \
662 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
663 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
664 arg1_name, arg1_val)
665 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
666 arg1_val, arg2_name, arg2_val) \
667 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
668 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
669 arg1_name, arg1_val, arg2_name, arg2_val)
671 // Similar to TRACE_EVENT_ASYNC_ENDx but with a custom |at| timestamp provided.
672 #define TRACE_EVENT_ASYNC_END_WITH_TIMESTAMP0(category_group, \
673 name, id, timestamp) \
674 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
675 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, \
676 static_cast<int>(base::PlatformThread::CurrentId()), \
677 timestamp, TRACE_EVENT_FLAG_NONE)
679 // NESTABLE_ASYNC_* APIs are used to describe an async operation, which can
680 // be nested within a NESTABLE_ASYNC event and/or have inner NESTABLE_ASYNC
681 // events.
682 // - category and name strings must have application lifetime (statics or
683 // literals). They may not include " chars.
684 // - A pair of NESTABLE_ASYNC_BEGIN event and NESTABLE_ASYNC_END event is
685 // considered as a match if their category_group, name and id all match.
686 // - |id| must either be a pointer or an integer value up to 64 bits.
687 // If it's a pointer, the bits will be xored with a hash of the process ID so
688 // that the same pointer on two different processes will not collide.
689 // - |id| is used to match a child NESTABLE_ASYNC event with its parent
690 // NESTABLE_ASYNC event. Therefore, events in the same nested event tree must
691 // be logged using the same id and category_group.
693 // Unmatched NESTABLE_ASYNC_END event will be parsed as an event that starts
694 // at the first NESTABLE_ASYNC event of that id, and unmatched
695 // NESTABLE_ASYNC_BEGIN event will be parsed as an event that ends at the last
696 // NESTABLE_ASYNC event of that id. Corresponding warning messages for
697 // unmatched events will be shown in the analysis view.
699 // Records a single NESTABLE_ASYNC_BEGIN event called "name" immediately, with 2
700 // associated arguments. If the category is not enabled, then this does nothing.
701 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
702 arg1_val, arg2_name, arg2_val) \
703 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
704 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
705 arg2_name, arg2_val)
706 // Records a single NESTABLE_ASYNC_END event called "name" immediately, with 2
707 // associated arguments. If the category is not enabled, then this does nothing.
708 #define TRACE_EVENT_NESTABLE_ASYNC_END2(category_group, name, id, arg1_name, \
709 arg1_val, arg2_name, arg2_val) \
710 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
711 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
712 arg2_name, arg2_val)
713 // Records a single NESTABLE_ASYNC_INSTANT event called "name" immediately,
714 // with 2 associated arguments. If the category is not enabled, then this
715 // does nothing.
716 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT2(category_group, name, id, \
717 arg1_name, arg1_val, arg2_name, arg2_val) \
718 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, \
719 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
720 arg2_name, arg2_val)
722 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
723 // associated arguments. If the category is not enabled, then this
724 // does nothing.
725 // - category and name strings must have application lifetime (statics or
726 // literals). They may not include " chars.
727 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
728 // events are considered to match if their category_group, name and id values
729 // all match. |id| must either be a pointer or an integer value up to 64 bits.
730 // If it's a pointer, the bits will be xored with a hash of the process ID so
731 // that the same pointer on two different processes will not collide.
732 // FLOW events are different from ASYNC events in how they are drawn by the
733 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
734 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
735 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
736 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
737 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
738 // macros. When the operation completes, call FLOW_END. An async operation can
739 // span threads and processes, but all events in that operation must use the
740 // same |name| and |id|. Each event can have its own args.
741 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
742 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
743 category_group, name, id, TRACE_EVENT_FLAG_NONE)
744 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
745 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
746 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
747 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
748 arg2_name, arg2_val) \
749 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
750 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
751 arg1_name, arg1_val, arg2_name, arg2_val)
752 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
753 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
754 category_group, name, id, TRACE_EVENT_FLAG_COPY)
755 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
756 arg1_val) \
757 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
758 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
759 arg1_name, arg1_val)
760 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
761 arg1_val, arg2_name, arg2_val) \
762 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
763 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
764 arg1_name, arg1_val, arg2_name, arg2_val)
766 // Records a single FLOW_STEP event for |step| immediately. If the category
767 // is not enabled, then this does nothing. The |name| and |id| must match the
768 // FLOW_BEGIN event above. The |step| param identifies this step within the
769 // async event. This should be called at the beginning of the next phase of an
770 // asynchronous operation.
771 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
772 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
773 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
774 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \
775 arg1_name, arg1_val) \
776 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
777 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
778 arg1_name, arg1_val)
779 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
780 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
781 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
782 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \
783 arg1_name, arg1_val) \
784 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
785 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
786 arg1_name, arg1_val)
788 // Records a single FLOW_END event for "name" immediately. If the category
789 // is not enabled, then this does nothing.
790 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
791 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
792 category_group, name, id, TRACE_EVENT_FLAG_NONE)
793 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
794 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
795 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
796 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
797 arg2_name, arg2_val) \
798 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
799 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
800 arg1_name, arg1_val, arg2_name, arg2_val)
801 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
802 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
803 category_group, name, id, TRACE_EVENT_FLAG_COPY)
804 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
805 arg1_val) \
806 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
807 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
808 arg1_name, arg1_val)
809 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
810 arg1_val, arg2_name, arg2_val) \
811 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
812 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
813 arg1_name, arg1_val, arg2_name, arg2_val)
815 // Macros to track the life time and value of arbitrary client objects.
816 // See also TraceTrackableObject.
817 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
818 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \
819 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
821 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
822 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \
823 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE,\
824 "snapshot", snapshot)
826 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
827 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \
828 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
830 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
831 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
832 (base::trace_event::TraceLog::ENABLED_FOR_RECORDING | \
833 base::trace_event::TraceLog::ENABLED_FOR_EVENT_CALLBACK))
835 // Macro to efficiently determine if a given category group is enabled.
836 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
837 do { \
838 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
839 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
840 *ret = true; \
841 } else { \
842 *ret = false; \
844 } while (0)
846 // Macro to efficiently determine, through polling, if a new trace has begun.
847 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
848 do { \
849 static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
850 int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
851 if (num_traces_recorded != -1 && \
852 num_traces_recorded != \
853 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
854 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = \
855 num_traces_recorded; \
856 *ret = true; \
857 } else { \
858 *ret = false; \
860 } while (0)
862 ////////////////////////////////////////////////////////////////////////////////
863 // Implementation specific tracing API definitions.
865 // Get a pointer to the enabled state of the given trace category. Only
866 // long-lived literal strings should be given as the category group. The
867 // returned pointer can be held permanently in a local static for example. If
868 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
869 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
870 // between the load of the tracing state and the call to
871 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
872 // for best performance when tracing is disabled.
873 // const unsigned char*
874 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
875 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
876 base::trace_event::TraceLog::GetCategoryGroupEnabled
878 // Get the number of times traces have been recorded. This is used to implement
879 // the TRACE_EVENT_IS_NEW_TRACE facility.
880 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
881 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
882 base::trace_event::TraceLog::GetInstance()->GetNumTracesRecorded
884 // Add a trace event to the platform tracing system.
885 // base::trace_event::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT(
886 // char phase,
887 // const unsigned char* category_group_enabled,
888 // const char* name,
889 // unsigned long long id,
890 // int num_args,
891 // const char** arg_names,
892 // const unsigned char* arg_types,
893 // const unsigned long long* arg_values,
894 // unsigned char flags)
895 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
896 base::trace_event::TraceLog::GetInstance()->AddTraceEvent
898 // Add a trace event to the platform tracing system.
899 // base::trace_event::TraceEventHandle
900 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
901 // char phase,
902 // const unsigned char* category_group_enabled,
903 // const char* name,
904 // unsigned long long id,
905 // int thread_id,
906 // const TimeTicks& timestamp,
907 // int num_args,
908 // const char** arg_names,
909 // const unsigned char* arg_types,
910 // const unsigned long long* arg_values,
911 // unsigned char flags)
912 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
913 base::trace_event::TraceLog::GetInstance() \
914 ->AddTraceEventWithThreadIdAndTimestamp
916 // Set the duration field of a COMPLETE trace event.
917 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
918 // const unsigned char* category_group_enabled,
919 // const char* name,
920 // base::trace_event::TraceEventHandle id)
921 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
922 base::trace_event::TraceLog::GetInstance()->UpdateTraceEventDuration
924 // Defines atomic operations used internally by the tracing system.
925 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
926 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
927 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
928 base::subtle::NoBarrier_Store(&(var), (value))
930 // Defines visibility for classes in trace_event.h
931 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
933 // The thread buckets for the sampling profiler.
934 TRACE_EVENT_API_CLASS_EXPORT extern \
935 TRACE_EVENT_API_ATOMIC_WORD g_trace_state[3];
937 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
938 g_trace_state[thread_bucket]
940 ////////////////////////////////////////////////////////////////////////////////
942 // Implementation detail: trace event macros create temporary variables
943 // to keep instrumentation overhead low. These macros give each temporary
944 // variable a unique name based on the line number to prevent name collisions.
945 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
946 trace_event_unique_##a##b
947 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
948 INTERNAL_TRACE_EVENT_UID3(a,b)
949 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
950 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
952 // Implementation detail: internal macro to create static category.
953 // No barriers are needed, because this code is designed to operate safely
954 // even when the unsigned char* points to garbage data (which may be the case
955 // on processors without cache coherency).
956 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
957 category_group, atomic, category_group_enabled) \
958 category_group_enabled = \
959 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
960 atomic)); \
961 if (UNLIKELY(!category_group_enabled)) { \
962 category_group_enabled = \
963 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
964 TRACE_EVENT_API_ATOMIC_STORE(atomic, \
965 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
966 category_group_enabled)); \
969 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
970 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
971 const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
972 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES(category_group, \
973 INTERNAL_TRACE_EVENT_UID(atomic), \
974 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
976 // Implementation detail: internal macro to create static category and add
977 // event if the category is enabled.
978 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
979 do { \
980 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
981 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
982 trace_event_internal::AddTraceEvent( \
983 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
984 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
986 } while (0)
988 // Implementation detail: internal macro to create static category and add begin
989 // event if the category is enabled. Also adds the end event when the scope
990 // ends.
991 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
992 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
993 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
994 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
995 base::trace_event::TraceEventHandle h = \
996 trace_event_internal::AddTraceEvent( \
997 TRACE_EVENT_PHASE_COMPLETE, \
998 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
999 trace_event_internal::kNoEventId, TRACE_EVENT_FLAG_NONE, \
1000 ##__VA_ARGS__); \
1001 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
1002 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
1005 // Implementation detail: internal macro to create static category and add
1006 // event if the category is enabled.
1007 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
1008 flags, ...) \
1009 do { \
1010 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1011 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1012 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1013 trace_event_internal::TraceID trace_event_trace_id( \
1014 id, &trace_event_flags); \
1015 trace_event_internal::AddTraceEvent( \
1016 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1017 name, trace_event_trace_id.data(), trace_event_flags, \
1018 ##__VA_ARGS__); \
1020 } while (0)
1022 // Implementation detail: internal macro to create static category and add
1023 // event if the category is enabled.
1024 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
1025 category_group, name, id, thread_id, timestamp, flags, ...) \
1026 do { \
1027 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1028 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1029 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1030 trace_event_internal::TraceID trace_event_trace_id( \
1031 id, &trace_event_flags); \
1032 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
1033 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1034 name, trace_event_trace_id.data(), \
1035 thread_id, base::TimeTicks::FromInternalValue(timestamp), \
1036 trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
1037 ##__VA_ARGS__); \
1039 } while (0)
1041 // Notes regarding the following definitions:
1042 // New values can be added and propagated to third party libraries, but existing
1043 // definitions must never be changed, because third party libraries may use old
1044 // definitions.
1046 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
1047 #define TRACE_EVENT_PHASE_BEGIN ('B')
1048 #define TRACE_EVENT_PHASE_END ('E')
1049 #define TRACE_EVENT_PHASE_COMPLETE ('X')
1050 #define TRACE_EVENT_PHASE_INSTANT ('I')
1051 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
1052 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T')
1053 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p')
1054 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
1055 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN ('b')
1056 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_END ('e')
1057 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT ('n')
1058 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
1059 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
1060 #define TRACE_EVENT_PHASE_FLOW_END ('f')
1061 #define TRACE_EVENT_PHASE_METADATA ('M')
1062 #define TRACE_EVENT_PHASE_COUNTER ('C')
1063 #define TRACE_EVENT_PHASE_SAMPLE ('P')
1064 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
1065 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
1066 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
1067 #define TRACE_EVENT_PHASE_MEMORY_DUMP ('v')
1069 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
1070 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
1071 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
1072 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
1073 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
1074 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned char>(1 << 3))
1075 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned char>(1 << 4))
1076 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned char>(1 << 5))
1078 #define TRACE_EVENT_FLAG_SCOPE_MASK (static_cast<unsigned char>( \
1079 TRACE_EVENT_FLAG_SCOPE_OFFSET | TRACE_EVENT_FLAG_SCOPE_EXTRA))
1081 // Type values for identifying types in the TraceValue union.
1082 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
1083 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
1084 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
1085 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
1086 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
1087 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
1088 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
1089 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
1091 // Enum reflecting the scope of an INSTANT event. Must fit within
1092 // TRACE_EVENT_FLAG_SCOPE_MASK.
1093 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
1094 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
1095 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
1097 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
1098 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
1099 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
1101 namespace trace_event_internal {
1103 // Specify these values when the corresponding argument of AddTraceEvent is not
1104 // used.
1105 const int kZeroNumArgs = 0;
1106 const unsigned long long kNoEventId = 0;
1108 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
1109 // are by default mangled with the Process ID so that they are unlikely to
1110 // collide when the same pointer is used on different processes.
1111 class TraceID {
1112 public:
1113 class DontMangle {
1114 public:
1115 explicit DontMangle(const void* id)
1116 : data_(static_cast<unsigned long long>(
1117 reinterpret_cast<uintptr_t>(id))) {}
1118 explicit DontMangle(unsigned long long id) : data_(id) {}
1119 explicit DontMangle(unsigned long id) : data_(id) {}
1120 explicit DontMangle(unsigned int id) : data_(id) {}
1121 explicit DontMangle(unsigned short id) : data_(id) {}
1122 explicit DontMangle(unsigned char id) : data_(id) {}
1123 explicit DontMangle(long long id)
1124 : data_(static_cast<unsigned long long>(id)) {}
1125 explicit DontMangle(long id)
1126 : data_(static_cast<unsigned long long>(id)) {}
1127 explicit DontMangle(int id)
1128 : data_(static_cast<unsigned long long>(id)) {}
1129 explicit DontMangle(short id)
1130 : data_(static_cast<unsigned long long>(id)) {}
1131 explicit DontMangle(signed char id)
1132 : data_(static_cast<unsigned long long>(id)) {}
1133 unsigned long long data() const { return data_; }
1134 private:
1135 unsigned long long data_;
1138 class ForceMangle {
1139 public:
1140 explicit ForceMangle(unsigned long long id) : data_(id) {}
1141 explicit ForceMangle(unsigned long id) : data_(id) {}
1142 explicit ForceMangle(unsigned int id) : data_(id) {}
1143 explicit ForceMangle(unsigned short id) : data_(id) {}
1144 explicit ForceMangle(unsigned char id) : data_(id) {}
1145 explicit ForceMangle(long long id)
1146 : data_(static_cast<unsigned long long>(id)) {}
1147 explicit ForceMangle(long id)
1148 : data_(static_cast<unsigned long long>(id)) {}
1149 explicit ForceMangle(int id)
1150 : data_(static_cast<unsigned long long>(id)) {}
1151 explicit ForceMangle(short id)
1152 : data_(static_cast<unsigned long long>(id)) {}
1153 explicit ForceMangle(signed char id)
1154 : data_(static_cast<unsigned long long>(id)) {}
1155 unsigned long long data() const { return data_; }
1156 private:
1157 unsigned long long data_;
1159 TraceID(const void* id, unsigned char* flags)
1160 : data_(static_cast<unsigned long long>(
1161 reinterpret_cast<uintptr_t>(id))) {
1162 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1164 TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
1165 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1167 TraceID(DontMangle id, unsigned char* flags) : data_(id.data()) {
1169 TraceID(unsigned long long id, unsigned char* flags)
1170 : data_(id) { (void)flags; }
1171 TraceID(unsigned long id, unsigned char* flags)
1172 : data_(id) { (void)flags; }
1173 TraceID(unsigned int id, unsigned char* flags)
1174 : data_(id) { (void)flags; }
1175 TraceID(unsigned short id, unsigned char* flags)
1176 : data_(id) { (void)flags; }
1177 TraceID(unsigned char id, unsigned char* flags)
1178 : data_(id) { (void)flags; }
1179 TraceID(long long id, unsigned char* flags)
1180 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1181 TraceID(long id, unsigned char* flags)
1182 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1183 TraceID(int id, unsigned char* flags)
1184 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1185 TraceID(short id, unsigned char* flags)
1186 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1187 TraceID(signed char id, unsigned char* flags)
1188 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1190 unsigned long long data() const { return data_; }
1192 private:
1193 unsigned long long data_;
1196 // Simple union to store various types as unsigned long long.
1197 union TraceValueUnion {
1198 bool as_bool;
1199 unsigned long long as_uint;
1200 long long as_int;
1201 double as_double;
1202 const void* as_pointer;
1203 const char* as_string;
1206 // Simple container for const char* that should be copied instead of retained.
1207 class TraceStringWithCopy {
1208 public:
1209 explicit TraceStringWithCopy(const char* str) : str_(str) {}
1210 const char* str() const { return str_; }
1211 private:
1212 const char* str_;
1215 // Define SetTraceValue for each allowed type. It stores the type and
1216 // value in the return arguments. This allows this API to avoid declaring any
1217 // structures so that it is portable to third_party libraries.
1218 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
1219 arg_expression, \
1220 union_member, \
1221 value_type_id) \
1222 static inline void SetTraceValue( \
1223 actual_type arg, \
1224 unsigned char* type, \
1225 unsigned long long* value) { \
1226 TraceValueUnion type_value; \
1227 type_value.union_member = arg_expression; \
1228 *type = value_type_id; \
1229 *value = type_value.as_uint; \
1231 // Simpler form for int types that can be safely casted.
1232 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
1233 value_type_id) \
1234 static inline void SetTraceValue( \
1235 actual_type arg, \
1236 unsigned char* type, \
1237 unsigned long long* value) { \
1238 *type = value_type_id; \
1239 *value = static_cast<unsigned long long>(arg); \
1242 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
1243 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
1244 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
1245 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
1246 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
1247 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
1248 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
1249 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
1250 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
1251 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
1252 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg, as_bool, TRACE_VALUE_TYPE_BOOL)
1253 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg, as_double,
1254 TRACE_VALUE_TYPE_DOUBLE)
1255 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, arg, as_pointer,
1256 TRACE_VALUE_TYPE_POINTER)
1257 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, arg, as_string,
1258 TRACE_VALUE_TYPE_STRING)
1259 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, arg.str(),
1260 as_string, TRACE_VALUE_TYPE_COPY_STRING)
1262 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
1263 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
1265 // std::string version of SetTraceValue so that trace arguments can be strings.
1266 static inline void SetTraceValue(const std::string& arg,
1267 unsigned char* type,
1268 unsigned long long* value) {
1269 TraceValueUnion type_value;
1270 type_value.as_string = arg.c_str();
1271 *type = TRACE_VALUE_TYPE_COPY_STRING;
1272 *value = type_value.as_uint;
1275 // base::Time and base::TimeTicks version of SetTraceValue to make it easier to
1276 // trace these types.
1277 static inline void SetTraceValue(const base::Time arg,
1278 unsigned char* type,
1279 unsigned long long* value) {
1280 *type = TRACE_VALUE_TYPE_INT;
1281 *value = arg.ToInternalValue();
1284 static inline void SetTraceValue(const base::TimeTicks arg,
1285 unsigned char* type,
1286 unsigned long long* value) {
1287 *type = TRACE_VALUE_TYPE_INT;
1288 *value = arg.ToInternalValue();
1291 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
1292 // functions are defined here instead of in the macro, because the arg_values
1293 // could be temporary objects, such as std::string. In order to store
1294 // pointers to the internal c_str and pass through to the tracing API,
1295 // the arg_values must live throughout these procedures.
1297 static inline base::trace_event::TraceEventHandle
1298 AddTraceEventWithThreadIdAndTimestamp(
1299 char phase,
1300 const unsigned char* category_group_enabled,
1301 const char* name,
1302 unsigned long long id,
1303 int thread_id,
1304 const base::TimeTicks& timestamp,
1305 unsigned char flags,
1306 const char* arg1_name,
1307 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1308 arg1_val) {
1309 const int num_args = 1;
1310 unsigned char arg_types[1] = { TRACE_VALUE_TYPE_CONVERTABLE };
1311 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1312 phase, category_group_enabled, name, id, thread_id, timestamp,
1313 num_args, &arg1_name, arg_types, NULL, &arg1_val, flags);
1316 template<class ARG1_TYPE>
1317 static inline base::trace_event::TraceEventHandle
1318 AddTraceEventWithThreadIdAndTimestamp(
1319 char phase,
1320 const unsigned char* category_group_enabled,
1321 const char* name,
1322 unsigned long long id,
1323 int thread_id,
1324 const base::TimeTicks& timestamp,
1325 unsigned char flags,
1326 const char* arg1_name,
1327 const ARG1_TYPE& arg1_val,
1328 const char* arg2_name,
1329 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1330 arg2_val) {
1331 const int num_args = 2;
1332 const char* arg_names[2] = { arg1_name, arg2_name };
1334 unsigned char arg_types[2];
1335 unsigned long long arg_values[2];
1336 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1337 arg_types[1] = TRACE_VALUE_TYPE_CONVERTABLE;
1339 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1340 convertable_values[2];
1341 convertable_values[1] = arg2_val;
1343 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1344 phase, category_group_enabled, name, id, thread_id, timestamp,
1345 num_args, arg_names, arg_types, arg_values, convertable_values, flags);
1348 template<class ARG2_TYPE>
1349 static inline base::trace_event::TraceEventHandle
1350 AddTraceEventWithThreadIdAndTimestamp(
1351 char phase,
1352 const unsigned char* category_group_enabled,
1353 const char* name,
1354 unsigned long long id,
1355 int thread_id,
1356 const base::TimeTicks& timestamp,
1357 unsigned char flags,
1358 const char* arg1_name,
1359 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
1360 const char* arg2_name,
1361 const ARG2_TYPE& arg2_val) {
1362 const int num_args = 2;
1363 const char* arg_names[2] = { arg1_name, arg2_name };
1365 unsigned char arg_types[2];
1366 unsigned long long arg_values[2];
1367 arg_types[0] = TRACE_VALUE_TYPE_CONVERTABLE;
1368 arg_values[0] = 0;
1369 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1371 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1372 convertable_values[2];
1373 convertable_values[0] = arg1_val;
1375 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1376 phase, category_group_enabled, name, id, thread_id, timestamp,
1377 num_args, arg_names, arg_types, arg_values, convertable_values, flags);
1380 static inline base::trace_event::TraceEventHandle
1381 AddTraceEventWithThreadIdAndTimestamp(
1382 char phase,
1383 const unsigned char* category_group_enabled,
1384 const char* name,
1385 unsigned long long id,
1386 int thread_id,
1387 const base::TimeTicks& timestamp,
1388 unsigned char flags,
1389 const char* arg1_name,
1390 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
1391 const char* arg2_name,
1392 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1393 arg2_val) {
1394 const int num_args = 2;
1395 const char* arg_names[2] = { arg1_name, arg2_name };
1396 unsigned char arg_types[2] =
1397 { TRACE_VALUE_TYPE_CONVERTABLE, TRACE_VALUE_TYPE_CONVERTABLE };
1398 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1399 convertable_values[2] = {arg1_val, arg2_val};
1401 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1402 phase, category_group_enabled, name, id, thread_id, timestamp,
1403 num_args, arg_names, arg_types, NULL, convertable_values, flags);
1406 static inline base::trace_event::TraceEventHandle
1407 AddTraceEventWithThreadIdAndTimestamp(
1408 char phase,
1409 const unsigned char* category_group_enabled,
1410 const char* name,
1411 unsigned long long id,
1412 int thread_id,
1413 const base::TimeTicks& timestamp,
1414 unsigned char flags) {
1415 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1416 phase, category_group_enabled, name, id, thread_id, timestamp,
1417 kZeroNumArgs, NULL, NULL, NULL, NULL, flags);
1420 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1421 char phase,
1422 const unsigned char* category_group_enabled,
1423 const char* name,
1424 unsigned long long id,
1425 unsigned char flags) {
1426 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1427 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1428 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1429 name, id, thread_id, now, flags);
1432 template<class ARG1_TYPE>
1433 static inline base::trace_event::TraceEventHandle
1434 AddTraceEventWithThreadIdAndTimestamp(
1435 char phase,
1436 const unsigned char* category_group_enabled,
1437 const char* name,
1438 unsigned long long id,
1439 int thread_id,
1440 const base::TimeTicks& timestamp,
1441 unsigned char flags,
1442 const char* arg1_name,
1443 const ARG1_TYPE& arg1_val) {
1444 const int num_args = 1;
1445 unsigned char arg_types[1];
1446 unsigned long long arg_values[1];
1447 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1448 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1449 phase, category_group_enabled, name, id, thread_id, timestamp,
1450 num_args, &arg1_name, arg_types, arg_values, NULL, flags);
1453 template<class ARG1_TYPE>
1454 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1455 char phase,
1456 const unsigned char* category_group_enabled,
1457 const char* name,
1458 unsigned long long id,
1459 unsigned char flags,
1460 const char* arg1_name,
1461 const ARG1_TYPE& arg1_val) {
1462 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1463 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1464 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1465 name, id, thread_id, now, flags,
1466 arg1_name, arg1_val);
1469 template<class ARG1_TYPE, class ARG2_TYPE>
1470 static inline base::trace_event::TraceEventHandle
1471 AddTraceEventWithThreadIdAndTimestamp(
1472 char phase,
1473 const unsigned char* category_group_enabled,
1474 const char* name,
1475 unsigned long long id,
1476 int thread_id,
1477 const base::TimeTicks& timestamp,
1478 unsigned char flags,
1479 const char* arg1_name,
1480 const ARG1_TYPE& 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 };
1485 unsigned char arg_types[2];
1486 unsigned long long arg_values[2];
1487 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1488 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1489 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1490 phase, category_group_enabled, name, id, thread_id, timestamp,
1491 num_args, arg_names, arg_types, arg_values, NULL, flags);
1494 template<class ARG1_TYPE, class ARG2_TYPE>
1495 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1496 char phase,
1497 const unsigned char* category_group_enabled,
1498 const char* name,
1499 unsigned long long id,
1500 unsigned char flags,
1501 const char* arg1_name,
1502 const ARG1_TYPE& arg1_val,
1503 const char* arg2_name,
1504 const ARG2_TYPE& arg2_val) {
1505 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1506 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1507 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1508 name, id, thread_id, now, flags,
1509 arg1_name, arg1_val,
1510 arg2_name, arg2_val);
1513 // Used by TRACE_EVENTx macros. Do not use directly.
1514 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
1515 public:
1516 // Note: members of data_ intentionally left uninitialized. See Initialize.
1517 ScopedTracer() : p_data_(NULL) {}
1519 ~ScopedTracer() {
1520 if (p_data_ && *data_.category_group_enabled)
1521 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1522 data_.category_group_enabled, data_.name, data_.event_handle);
1525 void Initialize(const unsigned char* category_group_enabled,
1526 const char* name,
1527 base::trace_event::TraceEventHandle event_handle) {
1528 data_.category_group_enabled = category_group_enabled;
1529 data_.name = name;
1530 data_.event_handle = event_handle;
1531 p_data_ = &data_;
1534 private:
1535 // This Data struct workaround is to avoid initializing all the members
1536 // in Data during construction of this object, since this object is always
1537 // constructed, even when tracing is disabled. If the members of Data were
1538 // members of this class instead, compiler warnings occur about potential
1539 // uninitialized accesses.
1540 struct Data {
1541 const unsigned char* category_group_enabled;
1542 const char* name;
1543 base::trace_event::TraceEventHandle event_handle;
1545 Data* p_data_;
1546 Data data_;
1549 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
1550 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
1551 public:
1552 ScopedTraceBinaryEfficient(const char* category_group, const char* name);
1553 ~ScopedTraceBinaryEfficient();
1555 private:
1556 const unsigned char* category_group_enabled_;
1557 const char* name_;
1558 base::trace_event::TraceEventHandle event_handle_;
1561 // This macro generates less code then TRACE_EVENT0 but is also
1562 // slower to execute when tracing is off. It should generally only be
1563 // used with code that is seldom executed or conditionally executed
1564 // when debugging.
1565 // For now the category_group must be "gpu".
1566 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
1567 trace_event_internal::ScopedTraceBinaryEfficient \
1568 INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
1570 // TraceEventSamplingStateScope records the current sampling state
1571 // and sets a new sampling state. When the scope exists, it restores
1572 // the sampling state having recorded.
1573 template<size_t BucketNumber>
1574 class TraceEventSamplingStateScope {
1575 public:
1576 TraceEventSamplingStateScope(const char* category_and_name) {
1577 previous_state_ = TraceEventSamplingStateScope<BucketNumber>::Current();
1578 TraceEventSamplingStateScope<BucketNumber>::Set(category_and_name);
1581 ~TraceEventSamplingStateScope() {
1582 TraceEventSamplingStateScope<BucketNumber>::Set(previous_state_);
1585 static inline const char* Current() {
1586 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
1587 g_trace_state[BucketNumber]));
1590 static inline void Set(const char* category_and_name) {
1591 TRACE_EVENT_API_ATOMIC_STORE(
1592 g_trace_state[BucketNumber],
1593 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
1594 const_cast<char*>(category_and_name)));
1597 private:
1598 const char* previous_state_;
1601 } // namespace trace_event_internal
1603 namespace base {
1604 namespace trace_event {
1606 template<typename IDType> class TraceScopedTrackableObject {
1607 public:
1608 TraceScopedTrackableObject(const char* category_group, const char* name,
1609 IDType id)
1610 : category_group_(category_group),
1611 name_(name),
1612 id_(id) {
1613 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_, name_, id_);
1616 template <typename ArgType> void snapshot(ArgType snapshot) {
1617 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_, name_, id_, snapshot);
1620 ~TraceScopedTrackableObject() {
1621 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_, name_, id_);
1624 private:
1625 const char* category_group_;
1626 const char* name_;
1627 IDType id_;
1629 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
1632 } // namespace trace_event
1633 } // namespace base
1635 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_