base: Fix/add header #ifndef guards.
[chromium-blink-merge.git] / base / trace_event / trace_event.h
blobb90a3eac6c05ea575631145844e76d979b08d8c0
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 // ASYNC_STEP_* APIs should be only used by legacy code. New code should
524 // consider using NESTABLE_ASYNC_* APIs to describe substeps within an async
525 // event.
526 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
527 // associated arguments. If the category is not enabled, then this
528 // does nothing.
529 // - category and name strings must have application lifetime (statics or
530 // literals). They may not include " chars.
531 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
532 // events are considered to match if their category_group, name and id values
533 // all match. |id| must either be a pointer or an integer value up to 64 bits.
534 // If it's a pointer, the bits will be xored with a hash of the process ID so
535 // that the same pointer on two different processes will not collide.
537 // An asynchronous operation can consist of multiple phases. The first phase is
538 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
539 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will
540 // annotate the block following the call. The ASYNC_STEP_PAST macro will
541 // annotate the block prior to the call. Note that any particular event must use
542 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the
543 // operation completes, call ASYNC_END.
545 // An ASYNC trace typically occurs on a single thread (if not, they will only be
546 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
547 // operation must use the same |name| and |id|. Each step can have its own
548 // args.
549 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id) \
550 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
551 category_group, name, id, TRACE_EVENT_FLAG_NONE)
552 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
553 arg1_val) \
554 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
555 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
556 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
557 arg1_val, arg2_name, arg2_val) \
558 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
559 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
560 arg1_name, arg1_val, arg2_name, arg2_val)
561 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id) \
562 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
563 category_group, name, id, TRACE_EVENT_FLAG_COPY)
564 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
565 arg1_val) \
566 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
567 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
568 arg1_name, arg1_val)
569 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
570 arg1_val, arg2_name, arg2_val) \
571 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
572 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
573 arg1_name, arg1_val, arg2_name, arg2_val)
575 // Similar to TRACE_EVENT_ASYNC_BEGINx but with a custom |at| timestamp
576 // provided.
577 #define TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, \
578 name, id, timestamp) \
579 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
580 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, \
581 static_cast<int>(base::PlatformThread::CurrentId()), \
582 timestamp, TRACE_EVENT_FLAG_NONE)
584 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the
585 // category is not enabled, then this does nothing. The |name| and |id| must
586 // match the ASYNC_BEGIN event above. The |step| param identifies this step
587 // within the async event. This should be called at the beginning of the next
588 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
589 // ASYNC_STEP_PAST events.
590 #define TRACE_EVENT_ASYNC_STEP_INTO0(category_group, name, id, step) \
591 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
592 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
593 #define TRACE_EVENT_ASYNC_STEP_INTO1(category_group, name, id, step, \
594 arg1_name, arg1_val) \
595 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
596 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
597 arg1_name, arg1_val)
599 // Similar to TRACE_EVENT_ASYNC_STEP_INTOx but with a custom |at| timestamp
600 // provided.
601 #define TRACE_EVENT_ASYNC_STEP_INTO_WITH_TIMESTAMP0(category_group, name, \
602 id, step, timestamp) \
603 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
604 TRACE_EVENT_PHASE_ASYNC_STEP_INTO, category_group, name, id, \
605 static_cast<int>(base::PlatformThread::CurrentId()), \
606 timestamp, TRACE_EVENT_FLAG_NONE, "step", step)
608 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the
609 // category is not enabled, then this does nothing. The |name| and |id| must
610 // match the ASYNC_BEGIN event above. The |step| param identifies this step
611 // within the async event. This should be called at the beginning of the next
612 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
613 // ASYNC_STEP_INTO events.
614 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step) \
615 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
616 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
617 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, \
618 arg1_name, arg1_val) \
619 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
620 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
621 arg1_name, arg1_val)
623 // Records a single ASYNC_END event for "name" immediately. If the category
624 // is not enabled, then this does nothing.
625 #define TRACE_EVENT_ASYNC_END0(category_group, name, id) \
626 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
627 category_group, name, id, TRACE_EVENT_FLAG_NONE)
628 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
629 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
630 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
631 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
632 arg2_name, arg2_val) \
633 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
634 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
635 arg1_name, arg1_val, arg2_name, arg2_val)
636 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id) \
637 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
638 category_group, name, id, TRACE_EVENT_FLAG_COPY)
639 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
640 arg1_val) \
641 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
642 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
643 arg1_name, arg1_val)
644 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
645 arg1_val, arg2_name, arg2_val) \
646 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
647 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
648 arg1_name, arg1_val, arg2_name, arg2_val)
650 // Similar to TRACE_EVENT_ASYNC_ENDx but with a custom |at| timestamp provided.
651 #define TRACE_EVENT_ASYNC_END_WITH_TIMESTAMP0(category_group, \
652 name, id, timestamp) \
653 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
654 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, \
655 static_cast<int>(base::PlatformThread::CurrentId()), \
656 timestamp, TRACE_EVENT_FLAG_NONE)
658 // NESTABLE_ASYNC_* APIs are used to describe an async operation, which can
659 // be nested within a NESTABLE_ASYNC event and/or have inner NESTABLE_ASYNC
660 // events.
661 // - category and name strings must have application lifetime (statics or
662 // literals). They may not include " chars.
663 // - |id| is used to match the NESTABLE_ASYNC_BEGIN event with the
664 // NESTABLE_ASYNC_END event. Events are considered to match if their
665 // category_group, name and id values all match. |id| must either be a
666 // pointer or an integer value up to 64 bits. If it's a pointer, the bits
667 // will be xored with a hash of the process ID so that the same pointer on two
668 // different processes will not collide.
670 // Unmatched NESTABLE_ASYNC_END event will be parsed as an instant event,
671 // and unmatched NESTABLE_ASYNC_BEGIN event will be parsed as an event that
672 // ends at the last NESTABLE_ASYNC_END event of that |id|.
674 // Records a single NESTABLE_ASYNC_BEGIN event called "name" immediately, with 2
675 // associated arguments. If the category is not enabled, then this does nothing.
676 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
677 arg1_val, arg2_name, arg2_val) \
678 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
679 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
680 arg2_name, arg2_val)
681 // Records a single NESTABLE_ASYNC_END event called "name" immediately, with 2
682 // associated arguments. If the category is not enabled, then this does nothing.
683 #define TRACE_EVENT_NESTABLE_ASYNC_END2(category_group, name, id, arg1_name, \
684 arg1_val, arg2_name, arg2_val) \
685 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
686 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
687 arg2_name, arg2_val)
688 // Records a single NESTABLE_ASYNC_INSTANT event called "name" immediately,
689 // with 2 associated arguments. If the category is not enabled, then this
690 // does nothing.
691 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT2(category_group, name, id, \
692 arg1_name, arg1_val, arg2_name, arg2_val) \
693 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, \
694 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
695 arg2_name, arg2_val)
697 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
698 // associated arguments. If the category is not enabled, then this
699 // does nothing.
700 // - category and name strings must have application lifetime (statics or
701 // literals). They may not include " chars.
702 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
703 // events are considered to match if their category_group, name and id values
704 // all match. |id| must either be a pointer or an integer value up to 64 bits.
705 // If it's a pointer, the bits will be xored with a hash of the process ID so
706 // that the same pointer on two different processes will not collide.
707 // FLOW events are different from ASYNC events in how they are drawn by the
708 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
709 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
710 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
711 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
712 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
713 // macros. When the operation completes, call FLOW_END. An async operation can
714 // span threads and processes, but all events in that operation must use the
715 // same |name| and |id|. Each event can have its own args.
716 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
717 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
718 category_group, name, id, TRACE_EVENT_FLAG_NONE)
719 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
720 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
721 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
722 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
723 arg2_name, arg2_val) \
724 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
725 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
726 arg1_name, arg1_val, arg2_name, arg2_val)
727 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
728 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
729 category_group, name, id, TRACE_EVENT_FLAG_COPY)
730 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
731 arg1_val) \
732 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
733 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
734 arg1_name, arg1_val)
735 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
736 arg1_val, arg2_name, arg2_val) \
737 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
738 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
739 arg1_name, arg1_val, arg2_name, arg2_val)
741 // Records a single FLOW_STEP event for |step| immediately. If the category
742 // is not enabled, then this does nothing. The |name| and |id| must match the
743 // FLOW_BEGIN event above. The |step| param identifies this step within the
744 // async event. This should be called at the beginning of the next phase of an
745 // asynchronous operation.
746 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
747 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
748 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
749 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \
750 arg1_name, arg1_val) \
751 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
752 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
753 arg1_name, arg1_val)
754 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
755 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
756 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
757 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \
758 arg1_name, arg1_val) \
759 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
760 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
761 arg1_name, arg1_val)
763 // Records a single FLOW_END event for "name" immediately. If the category
764 // is not enabled, then this does nothing.
765 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
766 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
767 category_group, name, id, TRACE_EVENT_FLAG_NONE)
768 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
769 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
770 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
771 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
772 arg2_name, arg2_val) \
773 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
774 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
775 arg1_name, arg1_val, arg2_name, arg2_val)
776 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
777 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
778 category_group, name, id, TRACE_EVENT_FLAG_COPY)
779 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
780 arg1_val) \
781 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
782 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
783 arg1_name, arg1_val)
784 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
785 arg1_val, arg2_name, arg2_val) \
786 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
787 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
788 arg1_name, arg1_val, arg2_name, arg2_val)
790 // Macros to track the life time and value of arbitrary client objects.
791 // See also TraceTrackableObject.
792 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
793 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \
794 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
796 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
797 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \
798 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE,\
799 "snapshot", snapshot)
801 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
802 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \
803 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
805 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
806 UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
807 (base::trace_event::TraceLog::ENABLED_FOR_RECORDING | \
808 base::trace_event::TraceLog::ENABLED_FOR_EVENT_CALLBACK))
810 // Macro to efficiently determine if a given category group is enabled.
811 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
812 do { \
813 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
814 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
815 *ret = true; \
816 } else { \
817 *ret = false; \
819 } while (0)
821 // Macro to efficiently determine, through polling, if a new trace has begun.
822 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
823 do { \
824 static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
825 int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
826 if (num_traces_recorded != -1 && \
827 num_traces_recorded != \
828 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
829 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = \
830 num_traces_recorded; \
831 *ret = true; \
832 } else { \
833 *ret = false; \
835 } while (0)
837 ////////////////////////////////////////////////////////////////////////////////
838 // Implementation specific tracing API definitions.
840 // Get a pointer to the enabled state of the given trace category. Only
841 // long-lived literal strings should be given as the category group. The
842 // returned pointer can be held permanently in a local static for example. If
843 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
844 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
845 // between the load of the tracing state and the call to
846 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
847 // for best performance when tracing is disabled.
848 // const unsigned char*
849 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
850 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
851 base::trace_event::TraceLog::GetCategoryGroupEnabled
853 // Get the number of times traces have been recorded. This is used to implement
854 // the TRACE_EVENT_IS_NEW_TRACE facility.
855 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
856 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
857 base::trace_event::TraceLog::GetInstance()->GetNumTracesRecorded
859 // Add a trace event to the platform tracing system.
860 // base::trace_event::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT(
861 // char phase,
862 // const unsigned char* category_group_enabled,
863 // const char* name,
864 // unsigned long long id,
865 // int num_args,
866 // const char** arg_names,
867 // const unsigned char* arg_types,
868 // const unsigned long long* arg_values,
869 // unsigned char flags)
870 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
871 base::trace_event::TraceLog::GetInstance()->AddTraceEvent
873 // Add a trace event to the platform tracing system.
874 // base::trace_event::TraceEventHandle
875 // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
876 // char phase,
877 // const unsigned char* category_group_enabled,
878 // const char* name,
879 // unsigned long long id,
880 // int thread_id,
881 // const TimeTicks& timestamp,
882 // int num_args,
883 // const char** arg_names,
884 // const unsigned char* arg_types,
885 // const unsigned long long* arg_values,
886 // unsigned char flags)
887 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
888 base::trace_event::TraceLog::GetInstance() \
889 ->AddTraceEventWithThreadIdAndTimestamp
891 // Set the duration field of a COMPLETE trace event.
892 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
893 // const unsigned char* category_group_enabled,
894 // const char* name,
895 // base::trace_event::TraceEventHandle id)
896 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
897 base::trace_event::TraceLog::GetInstance()->UpdateTraceEventDuration
899 // Defines atomic operations used internally by the tracing system.
900 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
901 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
902 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
903 base::subtle::NoBarrier_Store(&(var), (value))
905 // Defines visibility for classes in trace_event.h
906 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
908 // The thread buckets for the sampling profiler.
909 TRACE_EVENT_API_CLASS_EXPORT extern \
910 TRACE_EVENT_API_ATOMIC_WORD g_trace_state[3];
912 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
913 g_trace_state[thread_bucket]
915 ////////////////////////////////////////////////////////////////////////////////
917 // Implementation detail: trace event macros create temporary variables
918 // to keep instrumentation overhead low. These macros give each temporary
919 // variable a unique name based on the line number to prevent name collisions.
920 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
921 trace_event_unique_##a##b
922 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
923 INTERNAL_TRACE_EVENT_UID3(a,b)
924 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
925 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
927 // Implementation detail: internal macro to create static category.
928 // No barriers are needed, because this code is designed to operate safely
929 // even when the unsigned char* points to garbage data (which may be the case
930 // on processors without cache coherency).
931 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
932 category_group, atomic, category_group_enabled) \
933 category_group_enabled = \
934 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
935 atomic)); \
936 if (UNLIKELY(!category_group_enabled)) { \
937 category_group_enabled = \
938 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
939 TRACE_EVENT_API_ATOMIC_STORE(atomic, \
940 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
941 category_group_enabled)); \
944 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
945 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
946 const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
947 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES(category_group, \
948 INTERNAL_TRACE_EVENT_UID(atomic), \
949 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
951 // Implementation detail: internal macro to create static category and add
952 // event if the category is enabled.
953 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
954 do { \
955 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
956 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
957 trace_event_internal::AddTraceEvent( \
958 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
959 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
961 } while (0)
963 // Implementation detail: internal macro to create static category and add begin
964 // event if the category is enabled. Also adds the end event when the scope
965 // ends.
966 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
967 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
968 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
969 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
970 base::trace_event::TraceEventHandle h = \
971 trace_event_internal::AddTraceEvent( \
972 TRACE_EVENT_PHASE_COMPLETE, \
973 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
974 trace_event_internal::kNoEventId, TRACE_EVENT_FLAG_NONE, \
975 ##__VA_ARGS__); \
976 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
977 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
980 // Implementation detail: internal macro to create static category and add
981 // event if the category is enabled.
982 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
983 flags, ...) \
984 do { \
985 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
986 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
987 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
988 trace_event_internal::TraceID trace_event_trace_id( \
989 id, &trace_event_flags); \
990 trace_event_internal::AddTraceEvent( \
991 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
992 name, trace_event_trace_id.data(), trace_event_flags, \
993 ##__VA_ARGS__); \
995 } while (0)
997 // Implementation detail: internal macro to create static category and add
998 // event if the category is enabled.
999 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
1000 category_group, name, id, thread_id, timestamp, flags, ...) \
1001 do { \
1002 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
1003 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
1004 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
1005 trace_event_internal::TraceID trace_event_trace_id( \
1006 id, &trace_event_flags); \
1007 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
1008 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
1009 name, trace_event_trace_id.data(), \
1010 thread_id, base::TimeTicks::FromInternalValue(timestamp), \
1011 trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
1012 ##__VA_ARGS__); \
1014 } while (0)
1016 // Notes regarding the following definitions:
1017 // New values can be added and propagated to third party libraries, but existing
1018 // definitions must never be changed, because third party libraries may use old
1019 // definitions.
1021 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
1022 #define TRACE_EVENT_PHASE_BEGIN ('B')
1023 #define TRACE_EVENT_PHASE_END ('E')
1024 #define TRACE_EVENT_PHASE_COMPLETE ('X')
1025 #define TRACE_EVENT_PHASE_INSTANT ('I')
1026 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
1027 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T')
1028 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p')
1029 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
1030 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN ('b')
1031 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_END ('e')
1032 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT ('n')
1033 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
1034 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
1035 #define TRACE_EVENT_PHASE_FLOW_END ('f')
1036 #define TRACE_EVENT_PHASE_METADATA ('M')
1037 #define TRACE_EVENT_PHASE_COUNTER ('C')
1038 #define TRACE_EVENT_PHASE_SAMPLE ('P')
1039 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
1040 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
1041 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
1042 #define TRACE_EVENT_PHASE_MEMORY_DUMP ('v')
1044 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
1045 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
1046 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
1047 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
1048 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
1049 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned char>(1 << 3))
1050 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned char>(1 << 4))
1051 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned char>(1 << 5))
1053 #define TRACE_EVENT_FLAG_SCOPE_MASK (static_cast<unsigned char>( \
1054 TRACE_EVENT_FLAG_SCOPE_OFFSET | TRACE_EVENT_FLAG_SCOPE_EXTRA))
1056 // Type values for identifying types in the TraceValue union.
1057 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
1058 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
1059 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
1060 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
1061 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
1062 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
1063 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
1064 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
1066 // Enum reflecting the scope of an INSTANT event. Must fit within
1067 // TRACE_EVENT_FLAG_SCOPE_MASK.
1068 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
1069 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
1070 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
1072 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
1073 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
1074 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
1076 namespace trace_event_internal {
1078 // Specify these values when the corresponding argument of AddTraceEvent is not
1079 // used.
1080 const int kZeroNumArgs = 0;
1081 const unsigned long long kNoEventId = 0;
1083 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
1084 // are by default mangled with the Process ID so that they are unlikely to
1085 // collide when the same pointer is used on different processes.
1086 class TraceID {
1087 public:
1088 class DontMangle {
1089 public:
1090 explicit DontMangle(const void* id)
1091 : data_(static_cast<unsigned long long>(
1092 reinterpret_cast<uintptr_t>(id))) {}
1093 explicit DontMangle(unsigned long long id) : data_(id) {}
1094 explicit DontMangle(unsigned long id) : data_(id) {}
1095 explicit DontMangle(unsigned int id) : data_(id) {}
1096 explicit DontMangle(unsigned short id) : data_(id) {}
1097 explicit DontMangle(unsigned char id) : data_(id) {}
1098 explicit DontMangle(long long id)
1099 : data_(static_cast<unsigned long long>(id)) {}
1100 explicit DontMangle(long id)
1101 : data_(static_cast<unsigned long long>(id)) {}
1102 explicit DontMangle(int id)
1103 : data_(static_cast<unsigned long long>(id)) {}
1104 explicit DontMangle(short id)
1105 : data_(static_cast<unsigned long long>(id)) {}
1106 explicit DontMangle(signed char id)
1107 : data_(static_cast<unsigned long long>(id)) {}
1108 unsigned long long data() const { return data_; }
1109 private:
1110 unsigned long long data_;
1113 class ForceMangle {
1114 public:
1115 explicit ForceMangle(unsigned long long id) : data_(id) {}
1116 explicit ForceMangle(unsigned long id) : data_(id) {}
1117 explicit ForceMangle(unsigned int id) : data_(id) {}
1118 explicit ForceMangle(unsigned short id) : data_(id) {}
1119 explicit ForceMangle(unsigned char id) : data_(id) {}
1120 explicit ForceMangle(long long id)
1121 : data_(static_cast<unsigned long long>(id)) {}
1122 explicit ForceMangle(long id)
1123 : data_(static_cast<unsigned long long>(id)) {}
1124 explicit ForceMangle(int id)
1125 : data_(static_cast<unsigned long long>(id)) {}
1126 explicit ForceMangle(short id)
1127 : data_(static_cast<unsigned long long>(id)) {}
1128 explicit ForceMangle(signed char id)
1129 : data_(static_cast<unsigned long long>(id)) {}
1130 unsigned long long data() const { return data_; }
1131 private:
1132 unsigned long long data_;
1134 TraceID(const void* id, unsigned char* flags)
1135 : data_(static_cast<unsigned long long>(
1136 reinterpret_cast<uintptr_t>(id))) {
1137 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1139 TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
1140 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1142 TraceID(DontMangle id, unsigned char* flags) : data_(id.data()) {
1144 TraceID(unsigned long long id, unsigned char* flags)
1145 : data_(id) { (void)flags; }
1146 TraceID(unsigned long id, unsigned char* flags)
1147 : data_(id) { (void)flags; }
1148 TraceID(unsigned int id, unsigned char* flags)
1149 : data_(id) { (void)flags; }
1150 TraceID(unsigned short id, unsigned char* flags)
1151 : data_(id) { (void)flags; }
1152 TraceID(unsigned char id, unsigned char* flags)
1153 : data_(id) { (void)flags; }
1154 TraceID(long long id, unsigned char* flags)
1155 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1156 TraceID(long id, unsigned char* flags)
1157 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1158 TraceID(int id, unsigned char* flags)
1159 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1160 TraceID(short id, unsigned char* flags)
1161 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1162 TraceID(signed char id, unsigned char* flags)
1163 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1165 unsigned long long data() const { return data_; }
1167 private:
1168 unsigned long long data_;
1171 // Simple union to store various types as unsigned long long.
1172 union TraceValueUnion {
1173 bool as_bool;
1174 unsigned long long as_uint;
1175 long long as_int;
1176 double as_double;
1177 const void* as_pointer;
1178 const char* as_string;
1181 // Simple container for const char* that should be copied instead of retained.
1182 class TraceStringWithCopy {
1183 public:
1184 explicit TraceStringWithCopy(const char* str) : str_(str) {}
1185 const char* str() const { return str_; }
1186 private:
1187 const char* str_;
1190 // Define SetTraceValue for each allowed type. It stores the type and
1191 // value in the return arguments. This allows this API to avoid declaring any
1192 // structures so that it is portable to third_party libraries.
1193 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
1194 arg_expression, \
1195 union_member, \
1196 value_type_id) \
1197 static inline void SetTraceValue( \
1198 actual_type arg, \
1199 unsigned char* type, \
1200 unsigned long long* value) { \
1201 TraceValueUnion type_value; \
1202 type_value.union_member = arg_expression; \
1203 *type = value_type_id; \
1204 *value = type_value.as_uint; \
1206 // Simpler form for int types that can be safely casted.
1207 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
1208 value_type_id) \
1209 static inline void SetTraceValue( \
1210 actual_type arg, \
1211 unsigned char* type, \
1212 unsigned long long* value) { \
1213 *type = value_type_id; \
1214 *value = static_cast<unsigned long long>(arg); \
1217 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
1218 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
1219 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
1220 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
1221 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
1222 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
1223 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
1224 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
1225 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
1226 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
1227 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg, as_bool, TRACE_VALUE_TYPE_BOOL)
1228 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg, as_double,
1229 TRACE_VALUE_TYPE_DOUBLE)
1230 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, arg, as_pointer,
1231 TRACE_VALUE_TYPE_POINTER)
1232 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, arg, as_string,
1233 TRACE_VALUE_TYPE_STRING)
1234 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, arg.str(),
1235 as_string, TRACE_VALUE_TYPE_COPY_STRING)
1237 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
1238 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
1240 // std::string version of SetTraceValue so that trace arguments can be strings.
1241 static inline void SetTraceValue(const std::string& arg,
1242 unsigned char* type,
1243 unsigned long long* value) {
1244 TraceValueUnion type_value;
1245 type_value.as_string = arg.c_str();
1246 *type = TRACE_VALUE_TYPE_COPY_STRING;
1247 *value = type_value.as_uint;
1250 // base::Time and base::TimeTicks version of SetTraceValue to make it easier to
1251 // trace these types.
1252 static inline void SetTraceValue(const base::Time arg,
1253 unsigned char* type,
1254 unsigned long long* value) {
1255 *type = TRACE_VALUE_TYPE_INT;
1256 *value = arg.ToInternalValue();
1259 static inline void SetTraceValue(const base::TimeTicks arg,
1260 unsigned char* type,
1261 unsigned long long* value) {
1262 *type = TRACE_VALUE_TYPE_INT;
1263 *value = arg.ToInternalValue();
1266 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
1267 // functions are defined here instead of in the macro, because the arg_values
1268 // could be temporary objects, such as std::string. In order to store
1269 // pointers to the internal c_str and pass through to the tracing API,
1270 // the arg_values must live throughout these procedures.
1272 static inline base::trace_event::TraceEventHandle
1273 AddTraceEventWithThreadIdAndTimestamp(
1274 char phase,
1275 const unsigned char* category_group_enabled,
1276 const char* name,
1277 unsigned long long id,
1278 int thread_id,
1279 const base::TimeTicks& timestamp,
1280 unsigned char flags,
1281 const char* arg1_name,
1282 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1283 arg1_val) {
1284 const int num_args = 1;
1285 unsigned char arg_types[1] = { TRACE_VALUE_TYPE_CONVERTABLE };
1286 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1287 phase, category_group_enabled, name, id, thread_id, timestamp,
1288 num_args, &arg1_name, arg_types, NULL, &arg1_val, flags);
1291 template<class ARG1_TYPE>
1292 static inline base::trace_event::TraceEventHandle
1293 AddTraceEventWithThreadIdAndTimestamp(
1294 char phase,
1295 const unsigned char* category_group_enabled,
1296 const char* name,
1297 unsigned long long id,
1298 int thread_id,
1299 const base::TimeTicks& timestamp,
1300 unsigned char flags,
1301 const char* arg1_name,
1302 const ARG1_TYPE& arg1_val,
1303 const char* arg2_name,
1304 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1305 arg2_val) {
1306 const int num_args = 2;
1307 const char* arg_names[2] = { arg1_name, arg2_name };
1309 unsigned char arg_types[2];
1310 unsigned long long arg_values[2];
1311 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1312 arg_types[1] = TRACE_VALUE_TYPE_CONVERTABLE;
1314 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1315 convertable_values[2];
1316 convertable_values[1] = arg2_val;
1318 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1319 phase, category_group_enabled, name, id, thread_id, timestamp,
1320 num_args, arg_names, arg_types, arg_values, convertable_values, flags);
1323 template<class ARG2_TYPE>
1324 static inline base::trace_event::TraceEventHandle
1325 AddTraceEventWithThreadIdAndTimestamp(
1326 char phase,
1327 const unsigned char* category_group_enabled,
1328 const char* name,
1329 unsigned long long id,
1330 int thread_id,
1331 const base::TimeTicks& timestamp,
1332 unsigned char flags,
1333 const char* arg1_name,
1334 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
1335 const char* arg2_name,
1336 const ARG2_TYPE& arg2_val) {
1337 const int num_args = 2;
1338 const char* arg_names[2] = { arg1_name, arg2_name };
1340 unsigned char arg_types[2];
1341 unsigned long long arg_values[2];
1342 arg_types[0] = TRACE_VALUE_TYPE_CONVERTABLE;
1343 arg_values[0] = 0;
1344 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1346 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1347 convertable_values[2];
1348 convertable_values[0] = arg1_val;
1350 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1351 phase, category_group_enabled, name, id, thread_id, timestamp,
1352 num_args, arg_names, arg_types, arg_values, convertable_values, flags);
1355 static inline base::trace_event::TraceEventHandle
1356 AddTraceEventWithThreadIdAndTimestamp(
1357 char phase,
1358 const unsigned char* category_group_enabled,
1359 const char* name,
1360 unsigned long long id,
1361 int thread_id,
1362 const base::TimeTicks& timestamp,
1363 unsigned char flags,
1364 const char* arg1_name,
1365 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>& arg1_val,
1366 const char* arg2_name,
1367 const scoped_refptr<base::trace_event::ConvertableToTraceFormat>&
1368 arg2_val) {
1369 const int num_args = 2;
1370 const char* arg_names[2] = { arg1_name, arg2_name };
1371 unsigned char arg_types[2] =
1372 { TRACE_VALUE_TYPE_CONVERTABLE, TRACE_VALUE_TYPE_CONVERTABLE };
1373 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1374 convertable_values[2] = {arg1_val, arg2_val};
1376 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1377 phase, category_group_enabled, name, id, thread_id, timestamp,
1378 num_args, arg_names, arg_types, NULL, convertable_values, flags);
1381 static inline base::trace_event::TraceEventHandle
1382 AddTraceEventWithThreadIdAndTimestamp(
1383 char phase,
1384 const unsigned char* category_group_enabled,
1385 const char* name,
1386 unsigned long long id,
1387 int thread_id,
1388 const base::TimeTicks& timestamp,
1389 unsigned char flags) {
1390 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1391 phase, category_group_enabled, name, id, thread_id, timestamp,
1392 kZeroNumArgs, NULL, NULL, NULL, NULL, flags);
1395 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1396 char phase,
1397 const unsigned char* category_group_enabled,
1398 const char* name,
1399 unsigned long long id,
1400 unsigned char flags) {
1401 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1402 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1403 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1404 name, id, thread_id, now, flags);
1407 template<class ARG1_TYPE>
1408 static inline base::trace_event::TraceEventHandle
1409 AddTraceEventWithThreadIdAndTimestamp(
1410 char phase,
1411 const unsigned char* category_group_enabled,
1412 const char* name,
1413 unsigned long long id,
1414 int thread_id,
1415 const base::TimeTicks& timestamp,
1416 unsigned char flags,
1417 const char* arg1_name,
1418 const ARG1_TYPE& arg1_val) {
1419 const int num_args = 1;
1420 unsigned char arg_types[1];
1421 unsigned long long arg_values[1];
1422 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1423 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1424 phase, category_group_enabled, name, id, thread_id, timestamp,
1425 num_args, &arg1_name, arg_types, arg_values, NULL, flags);
1428 template<class ARG1_TYPE>
1429 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1430 char phase,
1431 const unsigned char* category_group_enabled,
1432 const char* name,
1433 unsigned long long id,
1434 unsigned char flags,
1435 const char* arg1_name,
1436 const ARG1_TYPE& arg1_val) {
1437 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1438 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1439 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1440 name, id, thread_id, now, flags,
1441 arg1_name, arg1_val);
1444 template<class ARG1_TYPE, class ARG2_TYPE>
1445 static inline base::trace_event::TraceEventHandle
1446 AddTraceEventWithThreadIdAndTimestamp(
1447 char phase,
1448 const unsigned char* category_group_enabled,
1449 const char* name,
1450 unsigned long long id,
1451 int thread_id,
1452 const base::TimeTicks& timestamp,
1453 unsigned char flags,
1454 const char* arg1_name,
1455 const ARG1_TYPE& arg1_val,
1456 const char* arg2_name,
1457 const ARG2_TYPE& arg2_val) {
1458 const int num_args = 2;
1459 const char* arg_names[2] = { arg1_name, arg2_name };
1460 unsigned char arg_types[2];
1461 unsigned long long arg_values[2];
1462 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1463 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1464 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1465 phase, category_group_enabled, name, id, thread_id, timestamp,
1466 num_args, arg_names, arg_types, arg_values, NULL, flags);
1469 template<class ARG1_TYPE, class ARG2_TYPE>
1470 static inline base::trace_event::TraceEventHandle AddTraceEvent(
1471 char phase,
1472 const unsigned char* category_group_enabled,
1473 const char* name,
1474 unsigned long long id,
1475 unsigned char flags,
1476 const char* arg1_name,
1477 const ARG1_TYPE& arg1_val,
1478 const char* arg2_name,
1479 const ARG2_TYPE& arg2_val) {
1480 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1481 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1482 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1483 name, id, thread_id, now, flags,
1484 arg1_name, arg1_val,
1485 arg2_name, arg2_val);
1488 // Used by TRACE_EVENTx macros. Do not use directly.
1489 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
1490 public:
1491 // Note: members of data_ intentionally left uninitialized. See Initialize.
1492 ScopedTracer() : p_data_(NULL) {}
1494 ~ScopedTracer() {
1495 if (p_data_ && *data_.category_group_enabled)
1496 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1497 data_.category_group_enabled, data_.name, data_.event_handle);
1500 void Initialize(const unsigned char* category_group_enabled,
1501 const char* name,
1502 base::trace_event::TraceEventHandle event_handle) {
1503 data_.category_group_enabled = category_group_enabled;
1504 data_.name = name;
1505 data_.event_handle = event_handle;
1506 p_data_ = &data_;
1509 private:
1510 // This Data struct workaround is to avoid initializing all the members
1511 // in Data during construction of this object, since this object is always
1512 // constructed, even when tracing is disabled. If the members of Data were
1513 // members of this class instead, compiler warnings occur about potential
1514 // uninitialized accesses.
1515 struct Data {
1516 const unsigned char* category_group_enabled;
1517 const char* name;
1518 base::trace_event::TraceEventHandle event_handle;
1520 Data* p_data_;
1521 Data data_;
1524 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
1525 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
1526 public:
1527 ScopedTraceBinaryEfficient(const char* category_group, const char* name);
1528 ~ScopedTraceBinaryEfficient();
1530 private:
1531 const unsigned char* category_group_enabled_;
1532 const char* name_;
1533 base::trace_event::TraceEventHandle event_handle_;
1536 // This macro generates less code then TRACE_EVENT0 but is also
1537 // slower to execute when tracing is off. It should generally only be
1538 // used with code that is seldom executed or conditionally executed
1539 // when debugging.
1540 // For now the category_group must be "gpu".
1541 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
1542 trace_event_internal::ScopedTraceBinaryEfficient \
1543 INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
1545 // TraceEventSamplingStateScope records the current sampling state
1546 // and sets a new sampling state. When the scope exists, it restores
1547 // the sampling state having recorded.
1548 template<size_t BucketNumber>
1549 class TraceEventSamplingStateScope {
1550 public:
1551 TraceEventSamplingStateScope(const char* category_and_name) {
1552 previous_state_ = TraceEventSamplingStateScope<BucketNumber>::Current();
1553 TraceEventSamplingStateScope<BucketNumber>::Set(category_and_name);
1556 ~TraceEventSamplingStateScope() {
1557 TraceEventSamplingStateScope<BucketNumber>::Set(previous_state_);
1560 static inline const char* Current() {
1561 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
1562 g_trace_state[BucketNumber]));
1565 static inline void Set(const char* category_and_name) {
1566 TRACE_EVENT_API_ATOMIC_STORE(
1567 g_trace_state[BucketNumber],
1568 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
1569 const_cast<char*>(category_and_name)));
1572 private:
1573 const char* previous_state_;
1576 } // namespace trace_event_internal
1578 namespace base {
1579 namespace trace_event {
1581 template<typename IDType> class TraceScopedTrackableObject {
1582 public:
1583 TraceScopedTrackableObject(const char* category_group, const char* name,
1584 IDType id)
1585 : category_group_(category_group),
1586 name_(name),
1587 id_(id) {
1588 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_, name_, id_);
1591 template <typename ArgType> void snapshot(ArgType snapshot) {
1592 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_, name_, id_, snapshot);
1595 ~TraceScopedTrackableObject() {
1596 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_, name_, id_);
1599 private:
1600 const char* category_group_;
1601 const char* name_;
1602 IDType id_;
1604 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
1607 } // namespace trace_event
1608 } // namespace base
1610 #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_