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.
4
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.
11
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
16 //
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)
22 //
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:
26 //
27 // TRACE_EVENT_INSTANT0("input,views", "OnMouseOver", TRACE_EVENT_SCOPE_THREAD)
28 //
29 // We can enable/disable tracing of OnMouseOver by enabling/disabling either
30 // category.
31 //
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.
39 //
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 // }
46 //
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 // }
53 //
54 // The trace system will automatically add to this information the
55 // current process id, thread id, and a timestamp in microseconds.
56 //
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 // }
82 //
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);
86 //
87 // Counters are process-specific. The macro itself can be issued from any
88 // thread, however.
89 //
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.
100 //
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.
103 //
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.
108 //
109 //
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
117 //
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.
123 //
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"));
134 //
135 //
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.
143 //
144 // class MyData : public base::debug::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 // };
154 //
155 // TRACE_EVENT1("foo", "bar", "data",
156 // scoped_refptr<ConvertableToTraceFormat>(new MyData()));
157 //
158 // The trace framework will take ownership if the passed pointer and it will
159 // be free'd when the trace buffer is flushed.
160 //
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.
163 //
164 //
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.
169 //
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.
175 //
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.
184 //
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.
188
189 #ifndef BASE_DEBUG_TRACE_EVENT_H_
190 #define BASE_DEBUG_TRACE_EVENT_H_
191
192 #include <string>
193
194 #include "base/atomicops.h"
195 #include "base/debug/trace_event_impl.h"
196 #include "base/debug/trace_event_memory.h"
197 #include "base/debug/trace_event_system_stats_monitor.h"
198 #include "build/build_config.h"
199
200 // By default, const char* argument values are assumed to have long-lived scope
201 // and will not be copied. Use this macro to force a const char* to be copied.
202 #define TRACE_STR_COPY(str) \
203 trace_event_internal::TraceStringWithCopy(str)
204
205 // This will mark the trace event as disabled by default. The user will need
206 // to explicitly enable the event.
207 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name
208
209 // By default, uint64 ID argument values are not mangled with the Process ID in
210 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
211 #define TRACE_ID_MANGLE(id) \
212 trace_event_internal::TraceID::ForceMangle(id)
213
214 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC
215 // macros. Use this macro to prevent Process ID mangling.
216 #define TRACE_ID_DONT_MANGLE(id) \
217 trace_event_internal::TraceID::DontMangle(id)
218
219 // Records a pair of begin and end events called "name" for the current
220 // scope, with 0, 1 or 2 associated arguments. If the category is not
221 // enabled, then this does nothing.
222 // - category and name strings must have application lifetime (statics or
223 // literals). They may not include " chars.
224 #define TRACE_EVENT0(category_group, name) \
225 INTERNAL_TRACE_MEMORY(category_group, name) \
226 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
227 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
228 INTERNAL_TRACE_MEMORY(category_group, name) \
229 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
230 #define TRACE_EVENT2( \
231 category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
232 INTERNAL_TRACE_MEMORY(category_group, name) \
233 INTERNAL_TRACE_EVENT_ADD_SCOPED( \
234 category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
235
236 // Records events like TRACE_EVENT2 but uses |memory_tag| for memory tracing.
237 // Use this where |name| is too generic to accurately aggregate allocations.
238 #define TRACE_EVENT_WITH_MEMORY_TAG2( \
239 category, name, memory_tag, arg1_name, arg1_val, arg2_name, arg2_val) \
240 INTERNAL_TRACE_MEMORY(category, memory_tag) \
241 INTERNAL_TRACE_EVENT_ADD_SCOPED( \
242 category, name, arg1_name, arg1_val, arg2_name, arg2_val)
243
244 // UNSHIPPED_TRACE_EVENT* are like TRACE_EVENT* except that they are not
245 // included in official builds.
246
247 #if OFFICIAL_BUILD
248 #undef TRACING_IS_OFFICIAL_BUILD
249 #define TRACING_IS_OFFICIAL_BUILD 1
250 #elif !defined(TRACING_IS_OFFICIAL_BUILD)
251 #define TRACING_IS_OFFICIAL_BUILD 0
252 #endif
253
254 #if TRACING_IS_OFFICIAL_BUILD
255 #define UNSHIPPED_TRACE_EVENT0(category_group, name) (void)0
256 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
257 (void)0
258 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
259 arg2_name, arg2_val) (void)0
260 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) (void)0
261 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, \
262 arg1_name, arg1_val) (void)0
263 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, \
264 arg1_name, arg1_val, \
265 arg2_name, arg2_val) (void)0
266 #else
267 #define UNSHIPPED_TRACE_EVENT0(category_group, name) \
268 TRACE_EVENT0(category_group, name)
269 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
270 TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
271 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
272 arg2_name, arg2_val) \
273 TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
274 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) \
275 TRACE_EVENT_INSTANT0(category_group, name, scope)
276 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, \
277 arg1_name, arg1_val) \
278 TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val)
279 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, \
280 arg1_name, arg1_val, \
281 arg2_name, arg2_val) \
282 TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
283 arg2_name, arg2_val)
284 #endif
285
286 // Records a single event called "name" immediately, with 0, 1 or 2
287 // associated arguments. If the category is not enabled, then this
288 // does nothing.
289 // - category and name strings must have application lifetime (statics or
290 // literals). They may not include " chars.
291 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \
292 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
293 category_group, name, TRACE_EVENT_FLAG_NONE | scope)
294 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
295 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
296 category_group, name, TRACE_EVENT_FLAG_NONE | scope, \
297 arg1_name, arg1_val)
298 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
299 arg2_name, arg2_val) \
300 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
301 category_group, name, TRACE_EVENT_FLAG_NONE | scope, \
302 arg1_name, arg1_val, arg2_name, arg2_val)
303 #define TRACE_EVENT_COPY_INSTANT0(category_group, name, scope) \
304 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
305 category_group, name, TRACE_EVENT_FLAG_COPY | scope)
306 #define TRACE_EVENT_COPY_INSTANT1(category_group, name, scope, \
307 arg1_name, arg1_val) \
308 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
309 category_group, name, TRACE_EVENT_FLAG_COPY | scope, arg1_name, \
310 arg1_val)
311 #define TRACE_EVENT_COPY_INSTANT2(category_group, name, scope, \
312 arg1_name, arg1_val, \
313 arg2_name, arg2_val) \
314 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
315 category_group, name, TRACE_EVENT_FLAG_COPY | scope, \
316 arg1_name, arg1_val, arg2_name, arg2_val)
317
318 // Sets the current sample state to the given category and name (both must be
319 // constant strings). These states are intended for a sampling profiler.
320 // Implementation note: we store category and name together because we don't
321 // want the inconsistency/expense of storing two pointers.
322 // |thread_bucket| is [0..2] and is used to statically isolate samples in one
323 // thread from others.
324 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET( \
325 bucket_number, category, name) \
326 trace_event_internal:: \
327 TraceEventSamplingStateScope<bucket_number>::Set(category "\0" name)
328
329 // Returns a current sampling state of the given bucket.
330 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \
331 trace_event_internal::TraceEventSamplingStateScope<bucket_number>::Current()
332
333 // Creates a scope of a sampling state of the given bucket.
334 //
335 // { // The sampling state is set within this scope.
336 // TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name");
337 // ...;
338 // }
339 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET( \
340 bucket_number, category, name) \
341 trace_event_internal::TraceEventSamplingStateScope<bucket_number> \
342 traceEventSamplingScope(category "\0" name);
343
344 // Syntactic sugars for the sampling tracing in the main thread.
345 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \
346 TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name)
347 #define TRACE_EVENT_GET_SAMPLING_STATE() \
348 TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0)
349 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \
350 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name)
351
352
353 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
354 // associated arguments. If the category is not enabled, then this
355 // does nothing.
356 // - category and name strings must have application lifetime (statics or
357 // literals). They may not include " chars.
358 #define TRACE_EVENT_BEGIN0(category_group, name) \
359 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
360 category_group, name, TRACE_EVENT_FLAG_NONE)
361 #define TRACE_EVENT_BEGIN1(category_group, name, arg1_name, arg1_val) \
362 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
363 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
364 #define TRACE_EVENT_BEGIN2(category_group, name, arg1_name, arg1_val, \
365 arg2_name, arg2_val) \
366 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
367 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
368 arg2_name, arg2_val)
369 #define TRACE_EVENT_COPY_BEGIN0(category_group, name) \
370 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
371 category_group, name, TRACE_EVENT_FLAG_COPY)
372 #define TRACE_EVENT_COPY_BEGIN1(category_group, name, arg1_name, arg1_val) \
373 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
374 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
375 #define TRACE_EVENT_COPY_BEGIN2(category_group, name, arg1_name, arg1_val, \
376 arg2_name, arg2_val) \
377 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
378 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
379 arg2_name, arg2_val)
380
381 // Similar to TRACE_EVENT_BEGINx but with a custom |at| timestamp provided.
382 // - |id| is used to match the _BEGIN event with the _END event.
383 // Events are considered to match if their category_group, name and id values
384 // all match. |id| must either be a pointer or an integer value up to 64 bits.
385 // If it's a pointer, the bits will be xored with a hash of the process ID so
386 // that the same pointer on two different processes will not collide.
387 #define TRACE_EVENT_BEGIN_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
388 name, id, thread_id, timestamp) \
389 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
390 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
391 timestamp, TRACE_EVENT_FLAG_NONE)
392 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP0( \
393 category_group, name, id, thread_id, timestamp) \
394 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
395 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
396 timestamp, TRACE_EVENT_FLAG_COPY)
397
398 // Records a single END event for "name" immediately. If the category
399 // is not enabled, then this does nothing.
400 // - category and name strings must have application lifetime (statics or
401 // literals). They may not include " chars.
402 #define TRACE_EVENT_END0(category_group, name) \
403 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
404 category_group, name, TRACE_EVENT_FLAG_NONE)
405 #define TRACE_EVENT_END1(category_group, name, arg1_name, arg1_val) \
406 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
407 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
408 #define TRACE_EVENT_END2(category_group, name, arg1_name, arg1_val, \
409 arg2_name, arg2_val) \
410 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
411 category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
412 arg2_name, arg2_val)
413 #define TRACE_EVENT_COPY_END0(category_group, name) \
414 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
415 category_group, name, TRACE_EVENT_FLAG_COPY)
416 #define TRACE_EVENT_COPY_END1(category_group, name, arg1_name, arg1_val) \
417 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
418 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
419 #define TRACE_EVENT_COPY_END2(category_group, name, arg1_name, arg1_val, \
420 arg2_name, arg2_val) \
421 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
422 category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
423 arg2_name, arg2_val)
424
425 // Similar to TRACE_EVENT_ENDx but with a custom |at| timestamp provided.
426 // - |id| is used to match the _BEGIN event with the _END event.
427 // Events are considered to match if their category_group, name and id values
428 // all match. |id| must either be a pointer or an integer value up to 64 bits.
429 // If it's a pointer, the bits will be xored with a hash of the process ID so
430 // that the same pointer on two different processes will not collide.
431 #define TRACE_EVENT_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
432 name, id, thread_id, timestamp) \
433 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
434 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
435 timestamp, TRACE_EVENT_FLAG_NONE)
436 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0( \
437 category_group, name, id, thread_id, timestamp) \
438 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
439 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
440 timestamp, TRACE_EVENT_FLAG_COPY)
441
442 // Records the value of a counter called "name" immediately. Value
443 // must be representable as a 32 bit integer.
444 // - category and name strings must have application lifetime (statics or
445 // literals). They may not include " chars.
446 #define TRACE_COUNTER1(category_group, name, value) \
447 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
448 category_group, name, TRACE_EVENT_FLAG_NONE, \
449 "value", static_cast<int>(value))
450 #define TRACE_COPY_COUNTER1(category_group, name, value) \
451 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
452 category_group, name, TRACE_EVENT_FLAG_COPY, \
453 "value", static_cast<int>(value))
454
455 // Records the values of a multi-parted counter called "name" immediately.
456 // The UI will treat value1 and value2 as parts of a whole, displaying their
457 // values as a stacked-bar chart.
458 // - category and name strings must have application lifetime (statics or
459 // literals). They may not include " chars.
460 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \
461 value2_name, value2_val) \
462 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
463 category_group, name, TRACE_EVENT_FLAG_NONE, \
464 value1_name, static_cast<int>(value1_val), \
465 value2_name, static_cast<int>(value2_val))
466 #define TRACE_COPY_COUNTER2(category_group, name, value1_name, value1_val, \
467 value2_name, value2_val) \
468 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
469 category_group, name, TRACE_EVENT_FLAG_COPY, \
470 value1_name, static_cast<int>(value1_val), \
471 value2_name, static_cast<int>(value2_val))
472
473 // Records the value of a counter called "name" immediately. Value
474 // must be representable as a 32 bit integer.
475 // - category and name strings must have application lifetime (statics or
476 // literals). They may not include " chars.
477 // - |id| is used to disambiguate counters with the same name. It must either
478 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
479 // will be xored with a hash of the process ID so that the same pointer on
480 // two different processes will not collide.
481 #define TRACE_COUNTER_ID1(category_group, name, id, value) \
482 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
483 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
484 "value", static_cast<int>(value))
485 #define TRACE_COPY_COUNTER_ID1(category_group, name, id, value) \
486 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
487 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
488 "value", static_cast<int>(value))
489
490 // Records the values of a multi-parted counter called "name" immediately.
491 // The UI will treat value1 and value2 as parts of a whole, displaying their
492 // values as a stacked-bar chart.
493 // - category and name strings must have application lifetime (statics or
494 // literals). They may not include " chars.
495 // - |id| is used to disambiguate counters with the same name. It must either
496 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
497 // will be xored with a hash of the process ID so that the same pointer on
498 // two different processes will not collide.
499 #define TRACE_COUNTER_ID2(category_group, name, id, value1_name, value1_val, \
500 value2_name, value2_val) \
501 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
502 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
503 value1_name, static_cast<int>(value1_val), \
504 value2_name, static_cast<int>(value2_val))
505 #define TRACE_COPY_COUNTER_ID2(category_group, name, id, value1_name, \
506 value1_val, value2_name, value2_val) \
507 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
508 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
509 value1_name, static_cast<int>(value1_val), \
510 value2_name, static_cast<int>(value2_val))
511
512
513 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
514 // associated arguments. If the category is not enabled, then this
515 // does nothing.
516 // - category and name strings must have application lifetime (statics or
517 // literals). They may not include " chars.
518 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
519 // events are considered to match if their category_group, name and id values
520 // all match. |id| must either be a pointer or an integer value up to 64 bits.
521 // If it's a pointer, the bits will be xored with a hash of the process ID so
522 // that the same pointer on two different processes will not collide.
523 //
524 // An asynchronous operation can consist of multiple phases. The first phase is
525 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
526 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will
527 // annotate the block following the call. The ASYNC_STEP_PAST macro will
528 // annotate the block prior to the call. Note that any particular event must use
529 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the
530 // operation completes, call ASYNC_END.
531 //
532 // An ASYNC trace typically occurs on a single thread (if not, they will only be
533 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
534 // operation must use the same |name| and |id|. Each step can have its own
535 // args.
536 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id) \
537 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
538 category_group, name, id, TRACE_EVENT_FLAG_NONE)
539 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
540 arg1_val) \
541 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
542 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
543 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
544 arg1_val, arg2_name, arg2_val) \
545 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
546 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
547 arg1_name, arg1_val, arg2_name, arg2_val)
548 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id) \
549 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
550 category_group, name, id, TRACE_EVENT_FLAG_COPY)
551 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
552 arg1_val) \
553 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
554 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
555 arg1_name, arg1_val)
556 #define TRACE_EVENT_COPY_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_COPY, \
560 arg1_name, arg1_val, arg2_name, arg2_val)
561
562 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the
563 // category is not enabled, then this does nothing. The |name| and |id| must
564 // match the ASYNC_BEGIN event above. The |step| param identifies this step
565 // within the async event. This should be called at the beginning of the next
566 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
567 // ASYNC_STEP_PAST events.
568 #define TRACE_EVENT_ASYNC_STEP_INTO0(category_group, name, id, step) \
569 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
570 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
571 #define TRACE_EVENT_ASYNC_STEP_INTO1(category_group, name, id, step, \
572 arg1_name, arg1_val) \
573 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
574 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
575 arg1_name, arg1_val)
576
577 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the
578 // category is not enabled, then this does nothing. The |name| and |id| must
579 // match the ASYNC_BEGIN event above. The |step| param identifies this step
580 // within the async event. This should be called at the beginning of the next
581 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
582 // ASYNC_STEP_INTO events.
583 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step) \
584 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
585 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
586 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, \
587 arg1_name, arg1_val) \
588 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
589 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
590 arg1_name, arg1_val)
591
592 // Records a single ASYNC_END event for "name" immediately. If the category
593 // is not enabled, then this does nothing.
594 #define TRACE_EVENT_ASYNC_END0(category_group, name, id) \
595 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
596 category_group, name, id, TRACE_EVENT_FLAG_NONE)
597 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
598 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
599 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
600 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
601 arg2_name, arg2_val) \
602 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
603 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
604 arg1_name, arg1_val, arg2_name, arg2_val)
605 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id) \
606 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
607 category_group, name, id, TRACE_EVENT_FLAG_COPY)
608 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
609 arg1_val) \
610 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
611 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
612 arg1_name, arg1_val)
613 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
614 arg1_val, arg2_name, arg2_val) \
615 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
616 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
617 arg1_name, arg1_val, arg2_name, arg2_val)
618
619
620 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
621 // associated arguments. If the category is not enabled, then this
622 // does nothing.
623 // - category and name strings must have application lifetime (statics or
624 // literals). They may not include " chars.
625 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
626 // events are considered to match if their category_group, name and id values
627 // all match. |id| must either be a pointer or an integer value up to 64 bits.
628 // If it's a pointer, the bits will be xored with a hash of the process ID so
629 // that the same pointer on two different processes will not collide.
630 // FLOW events are different from ASYNC events in how they are drawn by the
631 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
632 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
633 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
634 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
635 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
636 // macros. When the operation completes, call FLOW_END. An async operation can
637 // span threads and processes, but all events in that operation must use the
638 // same |name| and |id|. Each event can have its own args.
639 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
640 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
641 category_group, name, id, TRACE_EVENT_FLAG_NONE)
642 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
643 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
644 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
645 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
646 arg2_name, arg2_val) \
647 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
648 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
649 arg1_name, arg1_val, arg2_name, arg2_val)
650 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
651 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
652 category_group, name, id, TRACE_EVENT_FLAG_COPY)
653 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
654 arg1_val) \
655 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
656 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
657 arg1_name, arg1_val)
658 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
659 arg1_val, arg2_name, arg2_val) \
660 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
661 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
662 arg1_name, arg1_val, arg2_name, arg2_val)
663
664 // Records a single FLOW_STEP event for |step| immediately. If the category
665 // is not enabled, then this does nothing. The |name| and |id| must match the
666 // FLOW_BEGIN event above. The |step| param identifies this step within the
667 // async event. This should be called at the beginning of the next phase of an
668 // asynchronous operation.
669 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
670 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
671 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
672 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \
673 arg1_name, arg1_val) \
674 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
675 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
676 arg1_name, arg1_val)
677 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
678 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
679 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
680 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \
681 arg1_name, arg1_val) \
682 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
683 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
684 arg1_name, arg1_val)
685
686 // Records a single FLOW_END event for "name" immediately. If the category
687 // is not enabled, then this does nothing.
688 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
689 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
690 category_group, name, id, TRACE_EVENT_FLAG_NONE)
691 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
692 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
693 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
694 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
695 arg2_name, arg2_val) \
696 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
697 category_group, name, id, TRACE_EVENT_FLAG_NONE, \
698 arg1_name, arg1_val, arg2_name, arg2_val)
699 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
700 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
701 category_group, name, id, TRACE_EVENT_FLAG_COPY)
702 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
703 arg1_val) \
704 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
705 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
706 arg1_name, arg1_val)
707 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
708 arg1_val, arg2_name, arg2_val) \
709 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
710 category_group, name, id, TRACE_EVENT_FLAG_COPY, \
711 arg1_name, arg1_val, arg2_name, arg2_val)
712
713 // Macros to track the life time and value of arbitrary client objects.
714 // See also TraceTrackableObject.
715 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
716 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \
717 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
718
719 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
720 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \
721 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE,\
722 "snapshot", snapshot)
723
724 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
725 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \
726 category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
727
728
729 // Macro to efficiently determine if a given category group is enabled.
730 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
731 do { \
732 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
733 if (*INTERNAL_TRACE_EVENT_UID(category_group_enabled)) { \
734 *ret = true; \
735 } else { \
736 *ret = false; \
737 } \
738 } while (0)
739
740 // Macro to efficiently determine, through polling, if a new trace has begun.
741 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
742 do { \
743 static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
744 int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
745 if (num_traces_recorded != -1 && \
746 num_traces_recorded != \
747 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
748 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = \
749 num_traces_recorded; \
750 *ret = true; \
751 } else { \
752 *ret = false; \
753 } \
754 } while (0)
755
756 ////////////////////////////////////////////////////////////////////////////////
757 // Implementation specific tracing API definitions.
758
759 // Get a pointer to the enabled state of the given trace category. Only
760 // long-lived literal strings should be given as the category group. The
761 // returned pointer can be held permanently in a local static for example. If
762 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
763 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
764 // between the load of the tracing state and the call to
765 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
766 // for best performance when tracing is disabled.
767 // const unsigned char*
768 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
769 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
770 base::debug::TraceLog::GetCategoryGroupEnabled
771
772 // Get the number of times traces have been recorded. This is used to implement
773 // the TRACE_EVENT_IS_NEW_TRACE facility.
774 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
775 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
776 base::debug::TraceLog::GetInstance()->GetNumTracesRecorded
777
778 // Add a trace event to the platform tracing system.
779 // base::debug::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT(
780 // char phase,
781 // const unsigned char* category_group_enabled,
782 // const char* name,
783 // unsigned long long id,
784 // int num_args,
785 // const char** arg_names,
786 // const unsigned char* arg_types,
787 // const unsigned long long* arg_values,
788 // unsigned char flags)
789 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
790 base::debug::TraceLog::GetInstance()->AddTraceEvent
791
792 // Add a trace event to the platform tracing system.
793 // base::debug::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
794 // char phase,
795 // const unsigned char* category_group_enabled,
796 // const char* name,
797 // unsigned long long id,
798 // int thread_id,
799 // const TimeTicks& timestamp,
800 // int num_args,
801 // const char** arg_names,
802 // const unsigned char* arg_types,
803 // const unsigned long long* arg_values,
804 // unsigned char flags)
805 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
806 base::debug::TraceLog::GetInstance()->AddTraceEventWithThreadIdAndTimestamp
807
808 // Set the duration field of a COMPLETE trace event.
809 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
810 // const unsigned char* category_group_enabled,
811 // const char* name,
812 // base::debug::TraceEventHandle id)
813 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
814 base::debug::TraceLog::GetInstance()->UpdateTraceEventDuration
815
816 // Defines atomic operations used internally by the tracing system.
817 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
818 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
819 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
820 base::subtle::NoBarrier_Store(&(var), (value))
821
822 // Defines visibility for classes in trace_event.h
823 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
824
825 // The thread buckets for the sampling profiler.
826 TRACE_EVENT_API_CLASS_EXPORT extern \
827 TRACE_EVENT_API_ATOMIC_WORD g_trace_state[3];
828
829 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
830 g_trace_state[thread_bucket]
831
832 ////////////////////////////////////////////////////////////////////////////////
833
834 // Implementation detail: trace event macros create temporary variables
835 // to keep instrumentation overhead low. These macros give each temporary
836 // variable a unique name based on the line number to prevent name collisions.
837 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
838 trace_event_unique_##a##b
839 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
840 INTERNAL_TRACE_EVENT_UID3(a,b)
841 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
842 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
843
844 // Implementation detail: internal macro to create static category.
845 // No barriers are needed, because this code is designed to operate safely
846 // even when the unsigned char* points to garbage data (which may be the case
847 // on processors without cache coherency).
848 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
849 category_group, atomic, category_group_enabled) \
850 category_group_enabled = \
851 reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
852 atomic)); \
853 if (!category_group_enabled) { \
854 category_group_enabled = \
855 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
856 TRACE_EVENT_API_ATOMIC_STORE(atomic, \
857 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
858 category_group_enabled)); \
859 }
860
861 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
862 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
863 const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
864 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES(category_group, \
865 INTERNAL_TRACE_EVENT_UID(atomic), \
866 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
867
868 // Implementation detail: internal macro to create static category and add
869 // event if the category is enabled.
870 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
871 do { \
872 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
873 if (*INTERNAL_TRACE_EVENT_UID(category_group_enabled)) { \
874 trace_event_internal::AddTraceEvent( \
875 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
876 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
877 } \
878 } while (0)
879
880 // Implementation detail: internal macro to create static category and add begin
881 // event if the category is enabled. Also adds the end event when the scope
882 // ends.
883 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
884 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
885 trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
886 if (*INTERNAL_TRACE_EVENT_UID(category_group_enabled)) { \
887 base::debug::TraceEventHandle h = trace_event_internal::AddTraceEvent( \
888 TRACE_EVENT_PHASE_COMPLETE, \
889 INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
890 name, trace_event_internal::kNoEventId, \
891 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
892 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
893 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
894 }
895
896 // Implementation detail: internal macro to create static category and add
897 // event if the category is enabled.
898 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
899 flags, ...) \
900 do { \
901 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
902 if (*INTERNAL_TRACE_EVENT_UID(category_group_enabled)) { \
903 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
904 trace_event_internal::TraceID trace_event_trace_id( \
905 id, &trace_event_flags); \
906 trace_event_internal::AddTraceEvent( \
907 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
908 name, trace_event_trace_id.data(), trace_event_flags, \
909 ##__VA_ARGS__); \
910 } \
911 } while (0)
912
913 // Implementation detail: internal macro to create static category and add
914 // event if the category is enabled.
915 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
916 category_group, name, id, thread_id, timestamp, flags, ...) \
917 do { \
918 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
919 if (*INTERNAL_TRACE_EVENT_UID(category_group_enabled)) { \
920 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
921 trace_event_internal::TraceID trace_event_trace_id( \
922 id, &trace_event_flags); \
923 trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
924 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
925 name, trace_event_trace_id.data(), \
926 thread_id, base::TimeTicks::FromInternalValue(timestamp), \
927 trace_event_flags, ##__VA_ARGS__); \
928 } \
929 } while (0)
930
931 // Notes regarding the following definitions:
932 // New values can be added and propagated to third party libraries, but existing
933 // definitions must never be changed, because third party libraries may use old
934 // definitions.
935
936 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
937 #define TRACE_EVENT_PHASE_BEGIN ('B')
938 #define TRACE_EVENT_PHASE_END ('E')
939 #define TRACE_EVENT_PHASE_COMPLETE ('X')
940 #define TRACE_EVENT_PHASE_INSTANT ('i')
941 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
942 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T')
943 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p')
944 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
945 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
946 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
947 #define TRACE_EVENT_PHASE_FLOW_END ('f')
948 #define TRACE_EVENT_PHASE_METADATA ('M')
949 #define TRACE_EVENT_PHASE_COUNTER ('C')
950 #define TRACE_EVENT_PHASE_SAMPLE ('P')
951 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
952 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
953 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
954
955 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
956 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
957 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
958 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
959 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
960 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned char>(1 << 3))
961
962 #define TRACE_EVENT_FLAG_SCOPE_MASK (static_cast<unsigned char>( \
963 TRACE_EVENT_FLAG_SCOPE_OFFSET | (TRACE_EVENT_FLAG_SCOPE_OFFSET << 1)))
964
965 // Type values for identifying types in the TraceValue union.
966 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
967 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
968 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
969 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
970 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
971 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
972 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
973 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
974
975 // Enum reflecting the scope of an INSTANT event. Must fit within
976 // TRACE_EVENT_FLAG_SCOPE_MASK.
977 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
978 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
979 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
980
981 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
982 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
983 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
984
985 namespace trace_event_internal {
986
987 // Specify these values when the corresponding argument of AddTraceEvent is not
988 // used.
989 const int kZeroNumArgs = 0;
990 const unsigned long long kNoEventId = 0;
991
992 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
993 // are by default mangled with the Process ID so that they are unlikely to
994 // collide when the same pointer is used on different processes.
995 class TraceID {
996 public:
997 class DontMangle {
998 public:
DontMangle(const void * id)999 explicit DontMangle(const void* id)
1000 : data_(static_cast<unsigned long long>(
1001 reinterpret_cast<unsigned long>(id))) {}
DontMangle(unsigned long long id)1002 explicit DontMangle(unsigned long long id) : data_(id) {}
DontMangle(unsigned long id)1003 explicit DontMangle(unsigned long id) : data_(id) {}
DontMangle(unsigned int id)1004 explicit DontMangle(unsigned int id) : data_(id) {}
DontMangle(unsigned short id)1005 explicit DontMangle(unsigned short id) : data_(id) {}
DontMangle(unsigned char id)1006 explicit DontMangle(unsigned char id) : data_(id) {}
DontMangle(long long id)1007 explicit DontMangle(long long id)
1008 : data_(static_cast<unsigned long long>(id)) {}
DontMangle(long id)1009 explicit DontMangle(long id)
1010 : data_(static_cast<unsigned long long>(id)) {}
DontMangle(int id)1011 explicit DontMangle(int id)
1012 : data_(static_cast<unsigned long long>(id)) {}
DontMangle(short id)1013 explicit DontMangle(short id)
1014 : data_(static_cast<unsigned long long>(id)) {}
DontMangle(signed char id)1015 explicit DontMangle(signed char id)
1016 : data_(static_cast<unsigned long long>(id)) {}
data()1017 unsigned long long data() const { return data_; }
1018 private:
1019 unsigned long long data_;
1020 };
1021
1022 class ForceMangle {
1023 public:
ForceMangle(unsigned long long id)1024 explicit ForceMangle(unsigned long long id) : data_(id) {}
ForceMangle(unsigned long id)1025 explicit ForceMangle(unsigned long id) : data_(id) {}
ForceMangle(unsigned int id)1026 explicit ForceMangle(unsigned int id) : data_(id) {}
ForceMangle(unsigned short id)1027 explicit ForceMangle(unsigned short id) : data_(id) {}
ForceMangle(unsigned char id)1028 explicit ForceMangle(unsigned char id) : data_(id) {}
ForceMangle(long long id)1029 explicit ForceMangle(long long id)
1030 : data_(static_cast<unsigned long long>(id)) {}
ForceMangle(long id)1031 explicit ForceMangle(long id)
1032 : data_(static_cast<unsigned long long>(id)) {}
ForceMangle(int id)1033 explicit ForceMangle(int id)
1034 : data_(static_cast<unsigned long long>(id)) {}
ForceMangle(short id)1035 explicit ForceMangle(short id)
1036 : data_(static_cast<unsigned long long>(id)) {}
ForceMangle(signed char id)1037 explicit ForceMangle(signed char id)
1038 : data_(static_cast<unsigned long long>(id)) {}
data()1039 unsigned long long data() const { return data_; }
1040 private:
1041 unsigned long long data_;
1042 };
1043
TraceID(const void * id,unsigned char * flags)1044 TraceID(const void* id, unsigned char* flags)
1045 : data_(static_cast<unsigned long long>(
1046 reinterpret_cast<unsigned long>(id))) {
1047 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1048 }
TraceID(ForceMangle id,unsigned char * flags)1049 TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
1050 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
1051 }
TraceID(DontMangle id,unsigned char * flags)1052 TraceID(DontMangle id, unsigned char* flags) : data_(id.data()) {
1053 }
TraceID(unsigned long long id,unsigned char * flags)1054 TraceID(unsigned long long id, unsigned char* flags)
1055 : data_(id) { (void)flags; }
TraceID(unsigned long id,unsigned char * flags)1056 TraceID(unsigned long id, unsigned char* flags)
1057 : data_(id) { (void)flags; }
TraceID(unsigned int id,unsigned char * flags)1058 TraceID(unsigned int id, unsigned char* flags)
1059 : data_(id) { (void)flags; }
TraceID(unsigned short id,unsigned char * flags)1060 TraceID(unsigned short id, unsigned char* flags)
1061 : data_(id) { (void)flags; }
TraceID(unsigned char id,unsigned char * flags)1062 TraceID(unsigned char id, unsigned char* flags)
1063 : data_(id) { (void)flags; }
TraceID(long long id,unsigned char * flags)1064 TraceID(long long id, unsigned char* flags)
1065 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(long id,unsigned char * flags)1066 TraceID(long id, unsigned char* flags)
1067 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(int id,unsigned char * flags)1068 TraceID(int id, unsigned char* flags)
1069 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(short id,unsigned char * flags)1070 TraceID(short id, unsigned char* flags)
1071 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(signed char id,unsigned char * flags)1072 TraceID(signed char id, unsigned char* flags)
1073 : data_(static_cast<unsigned long long>(id)) { (void)flags; }
1074
data()1075 unsigned long long data() const { return data_; }
1076
1077 private:
1078 unsigned long long data_;
1079 };
1080
1081 // Simple union to store various types as unsigned long long.
1082 union TraceValueUnion {
1083 bool as_bool;
1084 unsigned long long as_uint;
1085 long long as_int;
1086 double as_double;
1087 const void* as_pointer;
1088 const char* as_string;
1089 };
1090
1091 // Simple container for const char* that should be copied instead of retained.
1092 class TraceStringWithCopy {
1093 public:
TraceStringWithCopy(const char * str)1094 explicit TraceStringWithCopy(const char* str) : str_(str) {}
1095 operator const char* () const { return str_; }
1096 private:
1097 const char* str_;
1098 };
1099
1100 // Define SetTraceValue for each allowed type. It stores the type and
1101 // value in the return arguments. This allows this API to avoid declaring any
1102 // structures so that it is portable to third_party libraries.
1103 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
1104 union_member, \
1105 value_type_id) \
1106 static inline void SetTraceValue( \
1107 actual_type arg, \
1108 unsigned char* type, \
1109 unsigned long long* value) { \
1110 TraceValueUnion type_value; \
1111 type_value.union_member = arg; \
1112 *type = value_type_id; \
1113 *value = type_value.as_uint; \
1114 }
1115 // Simpler form for int types that can be safely casted.
1116 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
1117 value_type_id) \
1118 static inline void SetTraceValue( \
1119 actual_type arg, \
1120 unsigned char* type, \
1121 unsigned long long* value) { \
1122 *type = value_type_id; \
1123 *value = static_cast<unsigned long long>(arg); \
1124 }
1125
INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long,TRACE_VALUE_TYPE_UINT)1126 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
1127 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
1128 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
1129 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
1130 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
1131 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
1132 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
1133 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
1134 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
1135 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
1136 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
1137 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
1138 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer,
1139 TRACE_VALUE_TYPE_POINTER)
1140 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string,
1141 TRACE_VALUE_TYPE_STRING)
1142 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
1143 TRACE_VALUE_TYPE_COPY_STRING)
1144
1145 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
1146 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
1147
1148 // std::string version of SetTraceValue so that trace arguments can be strings.
1149 static inline void SetTraceValue(const std::string& arg,
1150 unsigned char* type,
1151 unsigned long long* value) {
1152 TraceValueUnion type_value;
1153 type_value.as_string = arg.c_str();
1154 *type = TRACE_VALUE_TYPE_COPY_STRING;
1155 *value = type_value.as_uint;
1156 }
1157
1158 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
1159 // functions are defined here instead of in the macro, because the arg_values
1160 // could be temporary objects, such as std::string. In order to store
1161 // pointers to the internal c_str and pass through to the tracing API,
1162 // the arg_values must live throughout these procedures.
1163
1164 static inline base::debug::TraceEventHandle
AddTraceEventWithThreadIdAndTimestamp(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,int thread_id,const base::TimeTicks & timestamp,unsigned char flags,const char * arg1_name,const scoped_refptr<base::debug::ConvertableToTraceFormat> & arg1_val)1165 AddTraceEventWithThreadIdAndTimestamp(
1166 char phase,
1167 const unsigned char* category_group_enabled,
1168 const char* name,
1169 unsigned long long id,
1170 int thread_id,
1171 const base::TimeTicks& timestamp,
1172 unsigned char flags,
1173 const char* arg1_name,
1174 const scoped_refptr<base::debug::ConvertableToTraceFormat>& arg1_val) {
1175 const int num_args = 1;
1176 unsigned char arg_types[1] = { TRACE_VALUE_TYPE_CONVERTABLE };
1177 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1178 phase, category_group_enabled, name, id, thread_id, timestamp,
1179 num_args, &arg1_name, arg_types, NULL, &arg1_val, flags);
1180 }
1181
1182 template<class ARG1_TYPE>
1183 static inline base::debug::TraceEventHandle
AddTraceEventWithThreadIdAndTimestamp(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,int thread_id,const base::TimeTicks & timestamp,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val,const char * arg2_name,const scoped_refptr<base::debug::ConvertableToTraceFormat> & arg2_val)1184 AddTraceEventWithThreadIdAndTimestamp(
1185 char phase,
1186 const unsigned char* category_group_enabled,
1187 const char* name,
1188 unsigned long long id,
1189 int thread_id,
1190 const base::TimeTicks& timestamp,
1191 unsigned char flags,
1192 const char* arg1_name,
1193 const ARG1_TYPE& arg1_val,
1194 const char* arg2_name,
1195 const scoped_refptr<base::debug::ConvertableToTraceFormat>& arg2_val) {
1196 const int num_args = 2;
1197 const char* arg_names[2] = { arg1_name, arg2_name };
1198
1199 unsigned char arg_types[2];
1200 unsigned long long arg_values[2];
1201 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1202 arg_types[1] = TRACE_VALUE_TYPE_CONVERTABLE;
1203
1204 scoped_refptr<base::debug::ConvertableToTraceFormat> convertable_values[2];
1205 convertable_values[1] = arg2_val;
1206
1207 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1208 phase, category_group_enabled, name, id, thread_id, timestamp,
1209 num_args, arg_names, arg_types, arg_values, convertable_values, flags);
1210 }
1211
1212 template<class ARG2_TYPE>
1213 static inline base::debug::TraceEventHandle
AddTraceEventWithThreadIdAndTimestamp(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,int thread_id,const base::TimeTicks & timestamp,unsigned char flags,const char * arg1_name,const scoped_refptr<base::debug::ConvertableToTraceFormat> & arg1_val,const char * arg2_name,const ARG2_TYPE & arg2_val)1214 AddTraceEventWithThreadIdAndTimestamp(
1215 char phase,
1216 const unsigned char* category_group_enabled,
1217 const char* name,
1218 unsigned long long id,
1219 int thread_id,
1220 const base::TimeTicks& timestamp,
1221 unsigned char flags,
1222 const char* arg1_name,
1223 const scoped_refptr<base::debug::ConvertableToTraceFormat>& arg1_val,
1224 const char* arg2_name,
1225 const ARG2_TYPE& arg2_val) {
1226 const int num_args = 2;
1227 const char* arg_names[2] = { arg1_name, arg2_name };
1228
1229 unsigned char arg_types[2];
1230 unsigned long long arg_values[2];
1231 arg_types[0] = TRACE_VALUE_TYPE_CONVERTABLE;
1232 arg_values[0] = 0;
1233 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1234
1235 scoped_refptr<base::debug::ConvertableToTraceFormat> convertable_values[2];
1236 convertable_values[0] = arg1_val;
1237
1238 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1239 phase, category_group_enabled, name, id, thread_id, timestamp,
1240 num_args, arg_names, arg_types, arg_values, convertable_values, flags);
1241 }
1242
1243 static inline base::debug::TraceEventHandle
AddTraceEventWithThreadIdAndTimestamp(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,int thread_id,const base::TimeTicks & timestamp,unsigned char flags,const char * arg1_name,const scoped_refptr<base::debug::ConvertableToTraceFormat> & arg1_val,const char * arg2_name,const scoped_refptr<base::debug::ConvertableToTraceFormat> & arg2_val)1244 AddTraceEventWithThreadIdAndTimestamp(
1245 char phase,
1246 const unsigned char* category_group_enabled,
1247 const char* name,
1248 unsigned long long id,
1249 int thread_id,
1250 const base::TimeTicks& timestamp,
1251 unsigned char flags,
1252 const char* arg1_name,
1253 const scoped_refptr<base::debug::ConvertableToTraceFormat>& arg1_val,
1254 const char* arg2_name,
1255 const scoped_refptr<base::debug::ConvertableToTraceFormat>& arg2_val) {
1256 const int num_args = 2;
1257 const char* arg_names[2] = { arg1_name, arg2_name };
1258 unsigned char arg_types[2] =
1259 { TRACE_VALUE_TYPE_CONVERTABLE, TRACE_VALUE_TYPE_CONVERTABLE };
1260 scoped_refptr<base::debug::ConvertableToTraceFormat> convertable_values[2] =
1261 { arg1_val, arg2_val };
1262
1263 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1264 phase, category_group_enabled, name, id, thread_id, timestamp,
1265 num_args, arg_names, arg_types, NULL, convertable_values, flags);
1266 }
1267
1268 static inline base::debug::TraceEventHandle
AddTraceEventWithThreadIdAndTimestamp(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,int thread_id,const base::TimeTicks & timestamp,unsigned char flags)1269 AddTraceEventWithThreadIdAndTimestamp(
1270 char phase,
1271 const unsigned char* category_group_enabled,
1272 const char* name,
1273 unsigned long long id,
1274 int thread_id,
1275 const base::TimeTicks& timestamp,
1276 unsigned char flags) {
1277 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1278 phase, category_group_enabled, name, id, thread_id, timestamp,
1279 kZeroNumArgs, NULL, NULL, NULL, NULL, flags);
1280 }
1281
AddTraceEvent(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,unsigned char flags)1282 static inline base::debug::TraceEventHandle AddTraceEvent(
1283 char phase,
1284 const unsigned char* category_group_enabled,
1285 const char* name,
1286 unsigned long long id,
1287 unsigned char flags) {
1288 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1289 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1290 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1291 name, id, thread_id, now, flags);
1292 }
1293
1294 template<class ARG1_TYPE>
1295 static inline base::debug::TraceEventHandle
AddTraceEventWithThreadIdAndTimestamp(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,int thread_id,const base::TimeTicks & timestamp,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val)1296 AddTraceEventWithThreadIdAndTimestamp(
1297 char phase,
1298 const unsigned char* category_group_enabled,
1299 const char* name,
1300 unsigned long long id,
1301 int thread_id,
1302 const base::TimeTicks& timestamp,
1303 unsigned char flags,
1304 const char* arg1_name,
1305 const ARG1_TYPE& arg1_val) {
1306 const int num_args = 1;
1307 unsigned char arg_types[1];
1308 unsigned long long arg_values[1];
1309 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1310 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1311 phase, category_group_enabled, name, id, thread_id, timestamp,
1312 num_args, &arg1_name, arg_types, arg_values, NULL, flags);
1313 }
1314
1315 template<class ARG1_TYPE>
AddTraceEvent(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val)1316 static inline base::debug::TraceEventHandle AddTraceEvent(
1317 char phase,
1318 const unsigned char* category_group_enabled,
1319 const char* name,
1320 unsigned long long id,
1321 unsigned char flags,
1322 const char* arg1_name,
1323 const ARG1_TYPE& arg1_val) {
1324 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1325 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1326 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1327 name, id, thread_id, now, flags,
1328 arg1_name, arg1_val);
1329 }
1330
1331 template<class ARG1_TYPE, class ARG2_TYPE>
1332 static inline base::debug::TraceEventHandle
AddTraceEventWithThreadIdAndTimestamp(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,int thread_id,const base::TimeTicks & timestamp,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val,const char * arg2_name,const ARG2_TYPE & arg2_val)1333 AddTraceEventWithThreadIdAndTimestamp(
1334 char phase,
1335 const unsigned char* category_group_enabled,
1336 const char* name,
1337 unsigned long long id,
1338 int thread_id,
1339 const base::TimeTicks& timestamp,
1340 unsigned char flags,
1341 const char* arg1_name,
1342 const ARG1_TYPE& arg1_val,
1343 const char* arg2_name,
1344 const ARG2_TYPE& arg2_val) {
1345 const int num_args = 2;
1346 const char* arg_names[2] = { arg1_name, arg2_name };
1347 unsigned char arg_types[2];
1348 unsigned long long arg_values[2];
1349 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1350 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1351 return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
1352 phase, category_group_enabled, name, id, thread_id, timestamp,
1353 num_args, arg_names, arg_types, arg_values, NULL, flags);
1354 }
1355
1356 template<class ARG1_TYPE, class ARG2_TYPE>
AddTraceEvent(char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val,const char * arg2_name,const ARG2_TYPE & arg2_val)1357 static inline base::debug::TraceEventHandle AddTraceEvent(
1358 char phase,
1359 const unsigned char* category_group_enabled,
1360 const char* name,
1361 unsigned long long id,
1362 unsigned char flags,
1363 const char* arg1_name,
1364 const ARG1_TYPE& arg1_val,
1365 const char* arg2_name,
1366 const ARG2_TYPE& arg2_val) {
1367 int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
1368 base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
1369 return AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled,
1370 name, id, thread_id, now, flags,
1371 arg1_name, arg1_val,
1372 arg2_name, arg2_val);
1373 }
1374
1375 // Used by TRACE_EVENTx macros. Do not use directly.
1376 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
1377 public:
1378 // Note: members of data_ intentionally left uninitialized. See Initialize.
ScopedTracer()1379 ScopedTracer() : p_data_(NULL) {}
1380
~ScopedTracer()1381 ~ScopedTracer() {
1382 if (p_data_ && *data_.category_group_enabled)
1383 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1384 data_.category_group_enabled, data_.name, data_.event_handle);
1385 }
1386
Initialize(const unsigned char * category_group_enabled,const char * name,base::debug::TraceEventHandle event_handle)1387 void Initialize(const unsigned char* category_group_enabled,
1388 const char* name,
1389 base::debug::TraceEventHandle event_handle) {
1390 data_.category_group_enabled = category_group_enabled;
1391 data_.name = name;
1392 data_.event_handle = event_handle;
1393 p_data_ = &data_;
1394 }
1395
1396 private:
1397 // This Data struct workaround is to avoid initializing all the members
1398 // in Data during construction of this object, since this object is always
1399 // constructed, even when tracing is disabled. If the members of Data were
1400 // members of this class instead, compiler warnings occur about potential
1401 // uninitialized accesses.
1402 struct Data {
1403 const unsigned char* category_group_enabled;
1404 const char* name;
1405 base::debug::TraceEventHandle event_handle;
1406 };
1407 Data* p_data_;
1408 Data data_;
1409 };
1410
1411 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
1412 class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
1413 public:
1414 ScopedTraceBinaryEfficient(const char* category_group, const char* name);
1415 ~ScopedTraceBinaryEfficient();
1416
1417 private:
1418 const unsigned char* category_group_enabled_;
1419 const char* name_;
1420 base::debug::TraceEventHandle event_handle_;
1421 };
1422
1423 // This macro generates less code then TRACE_EVENT0 but is also
1424 // slower to execute when tracing is off. It should generally only be
1425 // used with code that is seldom executed or conditionally executed
1426 // when debugging.
1427 // For now the category_group must be "gpu".
1428 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
1429 trace_event_internal::ScopedTraceBinaryEfficient \
1430 INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
1431
1432 // TraceEventSamplingStateScope records the current sampling state
1433 // and sets a new sampling state. When the scope exists, it restores
1434 // the sampling state having recorded.
1435 template<size_t BucketNumber>
1436 class TraceEventSamplingStateScope {
1437 public:
TraceEventSamplingStateScope(const char * category_and_name)1438 TraceEventSamplingStateScope(const char* category_and_name) {
1439 previous_state_ = TraceEventSamplingStateScope<BucketNumber>::Current();
1440 TraceEventSamplingStateScope<BucketNumber>::Set(category_and_name);
1441 }
1442
~TraceEventSamplingStateScope()1443 ~TraceEventSamplingStateScope() {
1444 TraceEventSamplingStateScope<BucketNumber>::Set(previous_state_);
1445 }
1446
Current()1447 static inline const char* Current() {
1448 return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
1449 g_trace_state[BucketNumber]));
1450 }
1451
Set(const char * category_and_name)1452 static inline void Set(const char* category_and_name) {
1453 TRACE_EVENT_API_ATOMIC_STORE(
1454 g_trace_state[BucketNumber],
1455 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
1456 const_cast<char*>(category_and_name)));
1457 }
1458
1459 private:
1460 const char* previous_state_;
1461 };
1462
1463 } // namespace trace_event_internal
1464
1465 namespace base {
1466 namespace debug {
1467
1468 template<typename IDType> class TraceScopedTrackableObject {
1469 public:
TraceScopedTrackableObject(const char * category_group,const char * name,IDType id)1470 TraceScopedTrackableObject(const char* category_group, const char* name,
1471 IDType id)
1472 : category_group_(category_group),
1473 name_(name),
1474 id_(id) {
1475 TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_, name_, id_);
1476 }
1477
snapshot(ArgType snapshot)1478 template <typename ArgType> void snapshot(ArgType snapshot) {
1479 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_, name_, id_, snapshot);
1480 }
1481
~TraceScopedTrackableObject()1482 ~TraceScopedTrackableObject() {
1483 TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_, name_, id_);
1484 }
1485
1486 private:
1487 const char* category_group_;
1488 const char* name_;
1489 IDType id_;
1490
1491 DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
1492 };
1493
1494 } // namespace debug
1495 } // namespace base
1496
1497 #endif /* BASE_DEBUG_TRACE_EVENT_H_ */
1498