Fixit: Fork base::TimeTicks --> TimeTicks + ThreadTicks + TraceTicks
[chromium-blink-merge.git] / base / trace_event / trace_event.h
blob875d9055b3d5471ff7f6ea8f8ac1f4d18789ee71
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 // 1, 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_END1(category_group, name, id, arg1_name, \
741 arg1_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 #define TRACE_EVENT_NESTABLE_ASYNC_END2(category_group, name, id, arg1_name, \
745 arg1_val, arg2_name, arg2_val) \
746 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
747 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
748 arg2_name, arg2_val)
750 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_BEGIN_WITH_TTS2(category_group, name, \
751 id, arg1_name, arg1_val, arg2_name, arg2_val) \
752 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
753 category_group, name, id, \
754 TRACE_EVENT_FLAG_ASYNC_TTS | TRACE_EVENT_FLAG_COPY, \
755 arg1_name, arg1_val, arg2_name, arg2_val)
756 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_END_WITH_TTS2(category_group, name, \
757 id, arg1_name, arg1_val, arg2_name, arg2_val) \
758 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
759 category_group, name, id, \
760 TRACE_EVENT_FLAG_ASYNC_TTS | TRACE_EVENT_FLAG_COPY, \
761 arg1_name, arg1_val, arg2_name, arg2_val)
763 // Records a single NESTABLE_ASYNC_INSTANT event called "name" immediately,
764 // with 2 associated arguments. If the category is not enabled, then this
765 // does nothing.
766 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT2(category_group, name, id, \
767 arg1_name, arg1_val, arg2_name, arg2_val) \
768 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, \
769 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
770 arg2_name, arg2_val)
772 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
773 // associated arguments. If the category is not enabled, then this
774 // does nothing.
775 // - category and name strings must have application lifetime (statics or
776 // literals). They may not include " chars.
777 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
778 // events are considered to match if their category_group, name and id values
779 // all match. |id| must either be a pointer or an integer value up to 64 bits.
780 // If it's a pointer, the bits will be xored with a hash of the process ID so
781 // that the same pointer on two different processes will not collide.
782 // FLOW events are different from ASYNC events in how they are drawn by the
783 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
784 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
785 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
786 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
787 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
788 // macros. When the operation completes, call FLOW_END. An async operation can
789 // span threads and processes, but all events in that operation must use the
790 // same |name| and |id|. Each event can have its own args.
791 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
792 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
793 category_group, name, id, TRACE_EVENT_FLAG_NONE)
794 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
795 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
796 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
797 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
798 arg2_name, arg2_val) \
799 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
800 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
801 arg1_name, arg1_val, arg2_name, arg2_val)
802 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
803 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
804 category_group, name, id, TRACE_EVENT_FLAG_COPY)
805 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
806 arg1_val) \
807 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
808 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
809 arg1_name, arg1_val)
810 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
811 arg1_val, arg2_name, arg2_val) \
812 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
813 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
814 arg1_name, arg1_val, arg2_name, arg2_val)
816 // Records a single FLOW_STEP event for |step| immediately. If the category
817 // is not enabled, then this does nothing. The |name| and |id| must match the
818 // FLOW_BEGIN event above. The |step| param identifies this step within the
819 // async event. This should be called at the beginning of the next phase of an
820 // asynchronous operation.
821 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
822 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
823 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
824 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \
825 arg1_name, arg1_val) \
826 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
827 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
828 arg1_name, arg1_val)
829 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
830 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
831 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
832 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \
833 arg1_name, arg1_val) \
834 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
835 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
836 arg1_name, arg1_val)
838 // Records a single FLOW_END event for "name" immediately. If the category
839 // is not enabled, then this does nothing.
840 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
841 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
842 category_group, name, id, TRACE_EVENT_FLAG_NONE)
843 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
844 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
845 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
846 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
847 arg2_name, arg2_val) \
848 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
849 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
850 arg1_name, arg1_val, arg2_name, arg2_val)
851 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
852 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
853 category_group, name, id, TRACE_EVENT_FLAG_COPY)
854 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
855 arg1_val) \
856 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
857 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
858 arg1_name, arg1_val)
859 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
860 arg1_val, arg2_name, arg2_val) \
861 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
862 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
863 arg1_name, arg1_val, arg2_name, arg2_val)
865 // Macros to track the life time and value of arbitrary client objects.
866 // See also TraceTrackableObject.
867 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
868 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \
869 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
871 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
872 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \
873 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE,\
874 "snapshot", snapshot)
876 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
877 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \
878 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
880 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
881 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
882 (base::trace_event::TraceLog::ENABLED_FOR_RECORDING | \
883 base::trace_event::TraceLog::ENABLED_FOR_EVENT_CALLBACK | \
884 base::trace_event::TraceLog::ENABLED_FOR_ETW_EXPORT))
886 // Macro to efficiently determine if a given category group is enabled.
887 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
888 do { \
889 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
890 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
891 *ret = true; \
892 } else { \
893 *ret = false; \
895 } while (0)
897 // Macro to efficiently determine, through polling, if a new trace has begun.
898 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
899 do { \
900 static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
901 int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
902 if (num_traces_recorded != -1 && \
903 num_traces_recorded != \
904 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
905 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = \
906 num_traces_recorded; \
907 *ret = true; \
908 } else { \
909 *ret = false; \
911 } while (0)
913 ////////////////////////////////////////////////////////////////////////////////
914 // Implementation specific tracing API definitions.
916 // Get a pointer to the enabled state of the given trace category. Only
917 // long-lived literal strings should be given as the category group. The
918 // returned pointer can be held permanently in a local static for example. If
919 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
920 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
921 // between the load of the tracing state and the call to
922 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
923 // for best performance when tracing is disabled.
924 // const unsigned char*
925 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
926 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
927 base::trace_event::TraceLog::GetCategoryGroupEnabled
929 // Get the number of times traces have been recorded. This is used to implement
930 // the TRACE_EVENT_IS_NEW_TRACE facility.
931 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
932 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
933 base::trace_event::TraceLog::GetInstance()->GetNumTracesRecorded
935 // Add a trace event to the platform tracing system.
936 // base::trace_event::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT(
937 // char phase,
938 // const unsigned char* category_group_enabled,
939 // const char* name,
940 // unsigned long long id,
941 // int num_args,
942 // const char** arg_names,
943 // const unsigned char* arg_types,
944 // const unsigned long long* arg_values,
945 // unsigned char flags)
946 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
947 base::trace_event::TraceLog::GetInstance()->AddTraceEvent
949 // Add a trace event to the platform tracing system.
950 // base::trace_event::TraceEventHandle
951 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
952 // char phase,
953 // const unsigned char* category_group_enabled,
954 // const char* name,
955 // unsigned long long id,
956 // int thread_id,
957 // const TraceTicks& timestamp,
958 // int num_args,
959 // const char** arg_names,
960 // const unsigned char* arg_types,
961 // const unsigned long long* arg_values,
962 // unsigned char flags)
963 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
964 base::trace_event::TraceLog::GetInstance() \
965 ->AddTraceEventWithThreadIdAndTimestamp
967 // Set the duration field of a COMPLETE trace event.
968 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
969 // const unsigned char* category_group_enabled,
970 // const char* name,
971 // base::trace_event::TraceEventHandle id)
972 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
973 base::trace_event::TraceLog::GetInstance()->UpdateTraceEventDuration
975 // Defines atomic operations used internally by the tracing system.
976 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
977 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
978 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
979 base::subtle::NoBarrier_Store(&(var), (value))
981 // Defines visibility for classes in trace_event.h
982 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
984 // The thread buckets for the sampling profiler.
985 TRACE_EVENT_API_CLASS_EXPORT extern \
986 TRACE_EVENT_API_ATOMIC_WORD g_trace_state[3];
988 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
989 g_trace_state[thread_bucket]
991 ////////////////////////////////////////////////////////////////////////////////
993 // Implementation detail: trace event macros create temporary variables
994 // to keep instrumentation overhead low. These macros give each temporary
995 // variable a unique name based on the line number to prevent name collisions.
996 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
997 trace_event_unique_##a##b
998 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
999 INTERNAL_TRACE_EVENT_UID3(a,b)
1000 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
1001 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
1003 // Implementation detail: internal macro to create static category.
1004 // No barriers are needed, because this code is designed to operate safely
1005 // even when the unsigned char* points to garbage data (which may be the case
1006 // on processors without cache coherency).
1007 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
1008 category_group, atomic, category_group_enabled) \
1009 category_group_enabled = \
1010 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
1011 atomic)); \
1012 if (UNLIKELY(!category_group_enabled)) { \
1013 category_group_enabled = \
1014 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
1015 TRACE_EVENT_API_ATOMIC_STORE(atomic, \
1016 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
1017 category_group_enabled)); \
1020 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
1021 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
1022 const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
1023 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES(category_group, \
1024 INTERNAL_TRACE_EVENT_UID(atomic), \
1025 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
1027 // Implementation detail: internal macro to create static category and add
1028 // event if the category is enabled.
1029 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
1030 do { \
1031 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1032 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1033 trace_event_internal::AddTraceEvent( \
1034 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
1035 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
1037 } while (0)
1039 // Implementation detail: internal macro to create static category and add begin
1040 // event if the category is enabled. Also adds the end event when the scope
1041 // ends.
1042 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
1043 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1044 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
1045 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1046 base::trace_event::TraceEventHandle h = \
1047 trace_event_internal::AddTraceEvent( \
1048 TRACE_EVENT_PHASE_COMPLETE, \
1049 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
1050 trace_event_internal::kNoEventId, TRACE_EVENT_FLAG_NONE, \
1051 ##__VA_ARGS__); \
1052 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
1053 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
1056 // Implementation detail: internal macro to create static category and add
1057 // event if the category is enabled.
1058 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
1059 flags, ...) \
1060 do { \
1061 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1062 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1063 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1064 trace_event_internal::TraceID trace_event_trace_id( \
1065 id, &trace_event_flags); \
1066 trace_event_internal::AddTraceEvent( \
1067 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1068 name, trace_event_trace_id.data(), trace_event_flags, \
1069 ##__VA_ARGS__); \
1071 } while (0)
1073 // Implementation detail: internal macro to create static category and add
1074 // event if the category is enabled.
1075 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
1076 category_group, name, id, thread_id, timestamp, flags, ...) \
1077 do { \
1078 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1079 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1080 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1081 trace_event_internal::TraceID trace_event_trace_id( \
1082 id, &trace_event_flags); \
1083 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
1084 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1085 name, trace_event_trace_id.data(), \
1086 thread_id, base::TraceTicks::FromInternalValue(timestamp), \
1087 trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
1088 ##__VA_ARGS__); \
1090 } while (0)
1092 // Notes regarding the following definitions:
1093 // New values can be added and propagated to third party libraries, but existing
1094 // definitions must never be changed, because third party libraries may use old
1095 // definitions.
1097 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
1098 #define TRACE_EVENT_PHASE_BEGIN ('B')
1099 #define TRACE_EVENT_PHASE_END ('E')
1100 #define TRACE_EVENT_PHASE_COMPLETE ('X')
1101 #define TRACE_EVENT_PHASE_INSTANT ('I')
1102 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
1103 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T')
1104 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p')
1105 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
1106 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN ('b')
1107 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_END ('e')
1108 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT ('n')
1109 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
1110 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
1111 #define TRACE_EVENT_PHASE_FLOW_END ('f')
1112 #define TRACE_EVENT_PHASE_METADATA ('M')
1113 #define TRACE_EVENT_PHASE_COUNTER ('C')
1114 #define TRACE_EVENT_PHASE_SAMPLE ('P')
1115 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
1116 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
1117 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
1118 #define TRACE_EVENT_PHASE_MEMORY_DUMP ('v')
1120 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
1121 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
1122 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
1123 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
1124 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
1125 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned char>(1 << 3))
1126 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned char>(1 << 4))
1127 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned char>(1 << 5))
1128 #define TRACE_EVENT_FLAG_ASYNC_TTS (static_cast<unsigned char>(1 << 6))
1130 #define TRACE_EVENT_FLAG_SCOPE_MASK (static_cast<unsigned char>( \
1131 TRACE_EVENT_FLAG_SCOPE_OFFSET | TRACE_EVENT_FLAG_SCOPE_EXTRA))
1133 // Type values for identifying types in the TraceValue union.
1134 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
1135 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
1136 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
1137 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
1138 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
1139 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
1140 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
1141 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
1143 // Enum reflecting the scope of an INSTANT event. Must fit within
1144 // TRACE_EVENT_FLAG_SCOPE_MASK.
1145 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
1146 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
1147 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
1149 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
1150 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
1151 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
1153 namespace trace_event_internal {
1155 // Specify these values when the corresponding argument of AddTraceEvent is not
1156 // used.
1157 const int kZeroNumArgs = 0;
1158 const unsigned long long kNoEventId = 0;
1160 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
1161 // are by default mangled with the Process ID so that they are unlikely to
1162 // collide when the same pointer is used on different processes.
1163 class TraceID {
1164 public:
1165 class DontMangle {
1166 public:
1167 explicit DontMangle(const void* id)
1168 : data_(static_cast<unsigned long long>(
1169 reinterpret_cast<uintptr_t>(id))) {}
1170 explicit DontMangle(unsigned long long id) : data_(id) {}
1171 explicit DontMangle(unsigned long id) : data_(id) {}
1172 explicit DontMangle(unsigned int id) : data_(id) {}
1173 explicit DontMangle(unsigned short id) : data_(id) {}
1174 explicit DontMangle(unsigned char id) : data_(id) {}
1175 explicit DontMangle(long long id)
1176 : data_(static_cast<unsigned long long>(id)) {}
1177 explicit DontMangle(long id)
1178 : data_(static_cast<unsigned long long>(id)) {}
1179 explicit DontMangle(int id)
1180 : data_(static_cast<unsigned long long>(id)) {}
1181 explicit DontMangle(short id)
1182 : data_(static_cast<unsigned long long>(id)) {}
1183 explicit DontMangle(signed char id)
1184 : data_(static_cast<unsigned long long>(id)) {}
1185 unsigned long long data() const { return data_; }
1186 private:
1187 unsigned long long data_;
1190 class ForceMangle {
1191 public:
1192 explicit ForceMangle(unsigned long long id) : data_(id) {}
1193 explicit ForceMangle(unsigned long id) : data_(id) {}
1194 explicit ForceMangle(unsigned int id) : data_(id) {}
1195 explicit ForceMangle(unsigned short id) : data_(id) {}
1196 explicit ForceMangle(unsigned char id) : data_(id) {}
1197 explicit ForceMangle(long long id)
1198 : data_(static_cast<unsigned long long>(id)) {}
1199 explicit ForceMangle(long id)
1200 : data_(static_cast<unsigned long long>(id)) {}
1201 explicit ForceMangle(int id)
1202 : data_(static_cast<unsigned long long>(id)) {}
1203 explicit ForceMangle(short id)
1204 : data_(static_cast<unsigned long long>(id)) {}
1205 explicit ForceMangle(signed char id)
1206 : data_(static_cast<unsigned long long>(id)) {}
1207 unsigned long long data() const { return data_; }
1208 private:
1209 unsigned long long data_;
1211 TraceID(const void* id, unsigned char* flags)
1212 : data_(static_cast<unsigned long long>(
1213 reinterpret_cast<uintptr_t>(id))) {
1214 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1216 TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
1217 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1219 TraceID(DontMangle id, unsigned char* flags) : data_(id.data()) {
1221 TraceID(unsigned long long id, unsigned char* flags)
1222 : data_(id) { (void)flags; }
1223 TraceID(unsigned long id, unsigned char* flags)
1224 : data_(id) { (void)flags; }
1225 TraceID(unsigned int id, unsigned char* flags)
1226 : data_(id) { (void)flags; }
1227 TraceID(unsigned short id, unsigned char* flags)
1228 : data_(id) { (void)flags; }
1229 TraceID(unsigned char id, unsigned char* flags)
1230 : data_(id) { (void)flags; }
1231 TraceID(long long id, unsigned char* flags)
1232 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1233 TraceID(long id, unsigned char* flags)
1234 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1235 TraceID(int id, unsigned char* flags)
1236 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1237 TraceID(short id, unsigned char* flags)
1238 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1239 TraceID(signed char id, unsigned char* flags)
1240 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1242 unsigned long long data() const { return data_; }
1244 private:
1245 unsigned long long data_;
1248 // Simple union to store various types as unsigned long long.
1249 union TraceValueUnion {
1250 bool as_bool;
1251 unsigned long long as_uint;
1252 long long as_int;
1253 double as_double;
1254 const void* as_pointer;
1255 const char* as_string;
1258 // Simple container for const char* that should be copied instead of retained.
1259 class TraceStringWithCopy {
1260 public:
1261 explicit TraceStringWithCopy(const char* str) : str_(str) {}
1262 const char* str() const { return str_; }
1263 private:
1264 const char* str_;
1267 // Define SetTraceValue for each allowed type. It stores the type and
1268 // value in the return arguments. This allows this API to avoid declaring any
1269 // structures so that it is portable to third_party libraries.
1270 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
1271 arg_expression, \
1272 union_member, \
1273 value_type_id) \
1274 static inline void SetTraceValue( \
1275 actual_type arg, \
1276 unsigned char* type, \
1277 unsigned long long* value) { \
1278 TraceValueUnion type_value; \
1279 type_value.union_member = arg_expression; \
1280 *type = value_type_id; \
1281 *value = type_value.as_uint; \
1283 // Simpler form for int types that can be safely casted.
1284 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
1285 value_type_id) \
1286 static inline void SetTraceValue( \
1287 actual_type arg, \
1288 unsigned char* type, \
1289 unsigned long long* value) { \
1290 *type = value_type_id; \
1291 *value = static_cast<unsigned long long>(arg); \
1294 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
1295 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
1296 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
1297 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
1298 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
1299 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
1300 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
1301 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
1302 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
1303 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
1304 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg, as_bool, TRACE_VALUE_TYPE_BOOL)
1305 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg, as_double,
1306 TRACE_VALUE_TYPE_DOUBLE)
1307 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, arg, as_pointer,
1308 TRACE_VALUE_TYPE_POINTER)
1309 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, arg, as_string,
1310 TRACE_VALUE_TYPE_STRING)
1311 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, arg.str(),
1312 as_string, TRACE_VALUE_TYPE_COPY_STRING)
1314 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
1315 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
1317 // std::string version of SetTraceValue so that trace arguments can be strings.
1318 static inline void SetTraceValue(const std::string& arg,
1319 unsigned char* type,
1320 unsigned long long* value) {
1321 TraceValueUnion type_value;
1322 type_value.as_string = arg.c_str();
1323 *type = TRACE_VALUE_TYPE_COPY_STRING;
1324 *value = type_value.as_uint;
1327 // base::Time, base::TimeTicks, etc. versions of SetTraceValue to make it easier
1328 // to trace these types.
1329 static inline void SetTraceValue(const base::Time arg,
1330 unsigned char* type,
1331 unsigned long long* value) {
1332 *type = TRACE_VALUE_TYPE_INT;
1333 *value = arg.ToInternalValue();
1336 static inline void SetTraceValue(const base::TimeTicks arg,
1337 unsigned char* type,
1338 unsigned long long* value) {
1339 *type = TRACE_VALUE_TYPE_INT;
1340 *value = arg.ToInternalValue();
1343 static inline void SetTraceValue(const base::ThreadTicks arg,
1344 unsigned char* type,
1345 unsigned long long* value) {
1346 *type = TRACE_VALUE_TYPE_INT;
1347 *value = arg.ToInternalValue();
1350 static inline void SetTraceValue(const base::TraceTicks arg,
1351 unsigned char* type,
1352 unsigned long long* value) {
1353 *type = TRACE_VALUE_TYPE_INT;
1354 *value = arg.ToInternalValue();
1357 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
1358 // functions are defined here instead of in the macro, because the arg_values
1359 // could be temporary objects, such as std::string. In order to store
1360 // pointers to the internal c_str and pass through to the tracing API,
1361 // the arg_values must live throughout these procedures.
1363 static inline base::trace_event::TraceEventHandle
1364 AddTraceEventWithThreadIdAndTimestamp(
1365 char phase,
1366 const unsigned char* category_group_enabled,
1367 const char* name,
1368 unsigned long long id,
1369 int thread_id,
1370 const base::TraceTicks& timestamp,
1371 unsigned char flags,
1372 const char* arg1_name,
1373 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1374 arg1_val) {
1375 const int num_args = 1;
1376 unsigned char arg_types[1] = { TRACE_VALUE_TYPE_CONVERTABLE };
1377 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1378 phase, category_group_enabled, name, id, thread_id, timestamp,
1379 num_args, &arg1_name, arg_types, NULL, &arg1_val, flags);
1382 template<class ARG1_TYPE>
1383 static inline base::trace_event::TraceEventHandle
1384 AddTraceEventWithThreadIdAndTimestamp(
1385 char phase,
1386 const unsigned char* category_group_enabled,
1387 const char* name,
1388 unsigned long long id,
1389 int thread_id,
1390 const base::TraceTicks& timestamp,
1391 unsigned char flags,
1392 const char* arg1_name,
1393 const ARG1_TYPE& arg1_val,
1394 const char* arg2_name,
1395 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1396 arg2_val) {
1397 const int num_args = 2;
1398 const char* arg_names[2] = { arg1_name, arg2_name };
1400 unsigned char arg_types[2];
1401 unsigned long long arg_values[2];
1402 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1403 arg_types[1] = TRACE_VALUE_TYPE_CONVERTABLE;
1405 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1406 convertable_values[2];
1407 convertable_values[1] = arg2_val;
1409 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1410 phase, category_group_enabled, name, id, thread_id, timestamp,
1411 num_args, arg_names, arg_types, arg_values, convertable_values, flags);
1414 template<class ARG2_TYPE>
1415 static inline base::trace_event::TraceEventHandle
1416 AddTraceEventWithThreadIdAndTimestamp(
1417 char phase,
1418 const unsigned char* category_group_enabled,
1419 const char* name,
1420 unsigned long long id,
1421 int thread_id,
1422 const base::TraceTicks& timestamp,
1423 unsigned char flags,
1424 const char* arg1_name,
1425 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
1426 const char* arg2_name,
1427 const ARG2_TYPE& arg2_val) {
1428 const int num_args = 2;
1429 const char* arg_names[2] = { arg1_name, arg2_name };
1431 unsigned char arg_types[2];
1432 unsigned long long arg_values[2];
1433 arg_types[0] = TRACE_VALUE_TYPE_CONVERTABLE;
1434 arg_values[0] = 0;
1435 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1437 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1438 convertable_values[2];
1439 convertable_values[0] = arg1_val;
1441 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1442 phase, category_group_enabled, name, id, thread_id, timestamp,
1443 num_args, arg_names, arg_types, arg_values, convertable_values, flags);
1446 static inline base::trace_event::TraceEventHandle
1447 AddTraceEventWithThreadIdAndTimestamp(
1448 char phase,
1449 const unsigned char* category_group_enabled,
1450 const char* name,
1451 unsigned long long id,
1452 int thread_id,
1453 const base::TraceTicks& timestamp,
1454 unsigned char flags,
1455 const char* arg1_name,
1456 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
1457 const char* arg2_name,
1458 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1459 arg2_val) {
1460 const int num_args = 2;
1461 const char* arg_names[2] = { arg1_name, arg2_name };
1462 unsigned char arg_types[2] =
1463 { TRACE_VALUE_TYPE_CONVERTABLE, TRACE_VALUE_TYPE_CONVERTABLE };
1464 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1465 convertable_values[2] = {arg1_val, arg2_val};
1467 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1468 phase, category_group_enabled, name, id, thread_id, timestamp,
1469 num_args, arg_names, arg_types, NULL, convertable_values, flags);
1472 static inline base::trace_event::TraceEventHandle
1473 AddTraceEventWithThreadIdAndTimestamp(
1474 char phase,
1475 const unsigned char* category_group_enabled,
1476 const char* name,
1477 unsigned long long id,
1478 int thread_id,
1479 const base::TraceTicks& timestamp,
1480 unsigned char flags) {
1481 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1482 phase, category_group_enabled, name, id, thread_id, timestamp,
1483 kZeroNumArgs, NULL, NULL, NULL, NULL, flags);
1486 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1487 char phase,
1488 const unsigned char* category_group_enabled,
1489 const char* name,
1490 unsigned long long id,
1491 unsigned char flags) {
1492 const int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1493 const base::TraceTicks now = base::TraceTicks::Now();
1494 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1495 name, id, thread_id, now, flags);
1498 template<class ARG1_TYPE>
1499 static inline base::trace_event::TraceEventHandle
1500 AddTraceEventWithThreadIdAndTimestamp(
1501 char phase,
1502 const unsigned char* category_group_enabled,
1503 const char* name,
1504 unsigned long long id,
1505 int thread_id,
1506 const base::TraceTicks& timestamp,
1507 unsigned char flags,
1508 const char* arg1_name,
1509 const ARG1_TYPE& arg1_val) {
1510 const int num_args = 1;
1511 unsigned char arg_types[1];
1512 unsigned long long arg_values[1];
1513 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1514 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1515 phase, category_group_enabled, name, id, thread_id, timestamp,
1516 num_args, &arg1_name, arg_types, arg_values, NULL, flags);
1519 template<class ARG1_TYPE>
1520 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1521 char phase,
1522 const unsigned char* category_group_enabled,
1523 const char* name,
1524 unsigned long long id,
1525 unsigned char flags,
1526 const char* arg1_name,
1527 const ARG1_TYPE& arg1_val) {
1528 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1529 base::TraceTicks now = base::TraceTicks::Now();
1530 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1531 name, id, thread_id, now, flags,
1532 arg1_name, arg1_val);
1535 template<class ARG1_TYPE, class ARG2_TYPE>
1536 static inline base::trace_event::TraceEventHandle
1537 AddTraceEventWithThreadIdAndTimestamp(
1538 char phase,
1539 const unsigned char* category_group_enabled,
1540 const char* name,
1541 unsigned long long id,
1542 int thread_id,
1543 const base::TraceTicks& timestamp,
1544 unsigned char flags,
1545 const char* arg1_name,
1546 const ARG1_TYPE& arg1_val,
1547 const char* arg2_name,
1548 const ARG2_TYPE& arg2_val) {
1549 const int num_args = 2;
1550 const char* arg_names[2] = { arg1_name, arg2_name };
1551 unsigned char arg_types[2];
1552 unsigned long long arg_values[2];
1553 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1554 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1555 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1556 phase, category_group_enabled, name, id, thread_id, timestamp,
1557 num_args, arg_names, arg_types, arg_values, NULL, flags);
1560 template<class ARG1_TYPE, class ARG2_TYPE>
1561 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1562 char phase,
1563 const unsigned char* category_group_enabled,
1564 const char* name,
1565 unsigned long long id,
1566 unsigned char flags,
1567 const char* arg1_name,
1568 const ARG1_TYPE& arg1_val,
1569 const char* arg2_name,
1570 const ARG2_TYPE& arg2_val) {
1571 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1572 base::TraceTicks now = base::TraceTicks::Now();
1573 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1574 name, id, thread_id, now, flags,
1575 arg1_name, arg1_val,
1576 arg2_name, arg2_val);
1579 // Used by TRACE_EVENTx macros. Do not use directly.
1580 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
1581 public:
1582 // Note: members of data_ intentionally left uninitialized. See Initialize.
1583 ScopedTracer() : p_data_(NULL) {}
1585 ~ScopedTracer() {
1586 if (p_data_ && *data_.category_group_enabled)
1587 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1588 data_.category_group_enabled, data_.name, data_.event_handle);
1591 void Initialize(const unsigned char* category_group_enabled,
1592 const char* name,
1593 base::trace_event::TraceEventHandle event_handle) {
1594 data_.category_group_enabled = category_group_enabled;
1595 data_.name = name;
1596 data_.event_handle = event_handle;
1597 p_data_ = &data_;
1600 private:
1601 // This Data struct workaround is to avoid initializing all the members
1602 // in Data during construction of this object, since this object is always
1603 // constructed, even when tracing is disabled. If the members of Data were
1604 // members of this class instead, compiler warnings occur about potential
1605 // uninitialized accesses.
1606 struct Data {
1607 const unsigned char* category_group_enabled;
1608 const char* name;
1609 base::trace_event::TraceEventHandle event_handle;
1611 Data* p_data_;
1612 Data data_;
1615 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
1616 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
1617 public:
1618 ScopedTraceBinaryEfficient(const char* category_group, const char* name);
1619 ~ScopedTraceBinaryEfficient();
1621 private:
1622 const unsigned char* category_group_enabled_;
1623 const char* name_;
1624 base::trace_event::TraceEventHandle event_handle_;
1627 // This macro generates less code then TRACE_EVENT0 but is also
1628 // slower to execute when tracing is off. It should generally only be
1629 // used with code that is seldom executed or conditionally executed
1630 // when debugging.
1631 // For now the category_group must be "gpu".
1632 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
1633 trace_event_internal::ScopedTraceBinaryEfficient \
1634 INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
1636 // TraceEventSamplingStateScope records the current sampling state
1637 // and sets a new sampling state. When the scope exists, it restores
1638 // the sampling state having recorded.
1639 template<size_t BucketNumber>
1640 class TraceEventSamplingStateScope {
1641 public:
1642 TraceEventSamplingStateScope(const char* category_and_name) {
1643 previous_state_ = TraceEventSamplingStateScope<BucketNumber>::Current();
1644 TraceEventSamplingStateScope<BucketNumber>::Set(category_and_name);
1647 ~TraceEventSamplingStateScope() {
1648 TraceEventSamplingStateScope<BucketNumber>::Set(previous_state_);
1651 static inline const char* Current() {
1652 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
1653 g_trace_state[BucketNumber]));
1656 static inline void Set(const char* category_and_name) {
1657 TRACE_EVENT_API_ATOMIC_STORE(
1658 g_trace_state[BucketNumber],
1659 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
1660 const_cast<char*>(category_and_name)));
1663 private:
1664 const char* previous_state_;
1667 } // namespace trace_event_internal
1669 namespace base {
1670 namespace trace_event {
1672 template<typename IDType> class TraceScopedTrackableObject {
1673 public:
1674 TraceScopedTrackableObject(const char* category_group, const char* name,
1675 IDType id)
1676 : category_group_(category_group),
1677 name_(name),
1678 id_(id) {
1679 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_, name_, id_);
1682 template <typename ArgType> void snapshot(ArgType snapshot) {
1683 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_, name_, id_, snapshot);
1686 ~TraceScopedTrackableObject() {
1687 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_, name_, id_);
1690 private:
1691 const char* category_group_;
1692 const char* name_;
1693 IDType id_;
1695 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
1698 } // namespace trace_event
1699 } // namespace base
1701 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_