Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(440)

Side by Side Diff: src/tracing/trace-event.h

Issue 988893003: Implement tracing interface for v8 (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add a Unittest for trace-event Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 the V8 project 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 // Thread Safety:
137 // A thread safe singleton and mutex are used for thread safety. Category
138 // enabled flags are used to limit the performance impact when the system
139 // is not enabled.
140 //
141 // TRACE_EVENT macros first cache a pointer to a category. The categories are
142 // statically allocated and safe at all times, even after exit. Fetching a
143 // category is protected by the TraceLog::lock_. Multiple threads initializing
144 // the static variable is safe, as they will be serialized by the lock and
145 // multiple calls will return the same pointer to the category.
146 //
147 // Then the category_group_enabled flag is checked. This is a unsigned char, and
148 // not intended to be multithread safe. It optimizes access to AddTraceEvent
149 // which is threadsafe internally via TraceLog::lock_. The enabled flag may
150 // cause some threads to incorrectly call or ip calling AddTraceEvent near
151 // the time of the system being enabled or disabled. This is acceptable as
152 // we tolerate some data loss while the system is being enabled/disabled and
153 // because AddTraceEvent is threadsafe internally and checks the enabled state
154 // again under lock.
155 //
156 // Without the use of these static category pointers and enabled flags all
157 // trace points would carry a significant performance cost of acquiring a lock
158 // and resolving the category.
159
160 #ifndef SRC_TRACING_TRACE_EVENT_H_
161 #define SRC_TRACING_TRACE_EVENT_H_
162
163 #include "include/v8-tracing.h"
164
165 // By default, const char* argument values are assumed to have long-lived scope
166 // and will not be copied. Use this macro to force a const char* to be copied.
167 #define TRACE_STR_COPY(str) v8::tracing_internals::TraceStringWithCopy(str)
168
169 // By default, uint64 ID argument values are not mangled with the Process ID in
170 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
171 #define TRACE_ID_MANGLE(id) v8::tracing_internals::TraceID::ForceMangle(id)
172
173 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC
174 // macros. Use this macro to prevent Process ID mangling.
175 #define TRACE_ID_DONT_MANGLE(id) v8::tracing_internals::TraceID::DontMangle(id)
176
177 // Records a pair of begin and end events called "name" for the current
178 // scope, with 0, 1 or 2 associated arguments. If the category is not
179 // enabled, then this does nothing.
180 // - category and name strings must have application lifetime (statics or
181 // literals). They may not include " chars.
182 #define TRACE_EVENT0(category_group, name) \
183 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
184 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
185 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
186 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, \
187 arg2_val) \
188 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val, \
189 arg2_name, arg2_val)
190
191 // Records events like TRACE_EVENT2 but uses |memory_tag| for memory tracing.
192 // Use this where |name| is too generic to accurately aggregate allocations.
193 #define TRACE_EVENT_WITH_MEMORY_TAG2(category, name, memory_tag, arg1_name, \
194 arg1_val, arg2_name, arg2_val) \
195 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \
196 arg2_name, arg2_val)
197
198 // UNSHIPPED_TRACE_EVENT* are like TRACE_EVENT* except that they are not
199 // included in official builds.
200
201 #if OFFICIAL_BUILD
202 #undef TRACING_IS_OFFICIAL_BUILD
203 #define TRACING_IS_OFFICIAL_BUILD 1
204 #elif !defined(TRACING_IS_OFFICIAL_BUILD)
205 #define TRACING_IS_OFFICIAL_BUILD 0
206 #endif
207
208 #if TRACING_IS_OFFICIAL_BUILD
209 #define UNSHIPPED_TRACE_EVENT0(category_group, name) (void)0
210 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
211 (void)0
212 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
213 arg2_name, arg2_val) \
214 (void)0
215 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) (void)0
216 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, \
217 arg1_val) \
218 (void)0
219 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, \
220 arg1_val, arg2_name, arg2_val) \
221 (void)0
222 #else
223 #define UNSHIPPED_TRACE_EVENT0(category_group, name) \
224 TRACE_EVENT0(category_group, name)
225 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
226 TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
227 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
228 arg2_name, arg2_val) \
229 TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
230 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) \
231 TRACE_EVENT_INSTANT0(category_group, name, scope)
232 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, \
233 arg1_val) \
234 TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val)
235 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, \
236 arg1_val, arg2_name, arg2_val) \
237 TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
238 arg2_name, arg2_val)
239 #endif
240
241 // Records a single event called "name" immediately, with 0, 1 or 2
242 // associated arguments. If the category is not enabled, then this
243 // does nothing.
244 // - category and name strings must have application lifetime (statics or
245 // literals). They may not include " chars.
246 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \
247 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
248 TRACE_EVENT_FLAG_NONE | scope)
249 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
250 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
251 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val)
252 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
253 arg2_name, arg2_val) \
254 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
255 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val, \
256 arg2_name, arg2_val)
257 #define TRACE_EVENT_COPY_INSTANT0(category_group, name, scope) \
258 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
259 TRACE_EVENT_FLAG_COPY | scope)
260 #define TRACE_EVENT_COPY_INSTANT1(category_group, name, scope, arg1_name, \
261 arg1_val) \
262 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
263 TRACE_EVENT_FLAG_COPY | scope, arg1_name, arg1_val)
264 #define TRACE_EVENT_COPY_INSTANT2(category_group, name, scope, arg1_name, \
265 arg1_val, arg2_name, arg2_val) \
266 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
267 TRACE_EVENT_FLAG_COPY | scope, arg1_name, arg1_val, \
268 arg2_name, arg2_val)
269
270 // Sets the current sample state to the given category and name (both must be
271 // constant strings). These states are intended for a sampling profiler.
272 // Implementation note: we store category and name together because we don't
273 // want the inconsistency/expense of storing two pointers.
274 // |thread_bucket| is [0..2] and is used to statically isolate samples in one
275 // thread from others.
276 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(bucket_number, category, \
277 name) \
278 v8::tracing_internals::TraceEventSamplingStateScope<bucket_number>::Set( \
279 category "\0" name)
280
281 // Returns a current sampling state of the given bucket.
282 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \
283 v8::tracing_internals::TraceEventSamplingStateScope<bucket_number>::Current()
284
285 // Creates a scope of a sampling state of the given bucket.
286 //
287 // { // The sampling state is set within this scope.
288 // TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name");
289 // ...;
290 // }
291 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(bucket_number, category, \
292 name) \
293 v8::tracing_internals::TraceEventSamplingStateScope<bucket_number> \
294 traceEventSamplingScope(category "\0" name);
295
296 // Syntactic sugars for the sampling tracing in the main thread.
297 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \
298 TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name)
299 #define TRACE_EVENT_GET_SAMPLING_STATE() \
300 TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0)
301 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \
302 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name)
303
304
305 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
306 // associated arguments. If the category is not enabled, then this
307 // does nothing.
308 // - category and name strings must have application lifetime (statics or
309 // literals). They may not include " chars.
310 #define TRACE_EVENT_BEGIN0(category_group, name) \
311 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
312 TRACE_EVENT_FLAG_NONE)
313 #define TRACE_EVENT_BEGIN1(category_group, name, arg1_name, arg1_val) \
314 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
315 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
316 #define TRACE_EVENT_BEGIN2(category_group, name, arg1_name, arg1_val, \
317 arg2_name, arg2_val) \
318 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
319 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
320 arg2_name, arg2_val)
321 #define TRACE_EVENT_COPY_BEGIN0(category_group, name) \
322 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
323 TRACE_EVENT_FLAG_COPY)
324 #define TRACE_EVENT_COPY_BEGIN1(category_group, name, arg1_name, arg1_val) \
325 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
326 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
327 #define TRACE_EVENT_COPY_BEGIN2(category_group, name, arg1_name, arg1_val, \
328 arg2_name, arg2_val) \
329 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
330 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
331 arg2_name, arg2_val)
332
333 // Similar to TRACE_EVENT_BEGINx but with a custom |at| timestamp provided.
334 // - |id| is used to match the _BEGIN event with the _END event.
335 // Events are considered to match if their category_group, name and id values
336 // all match. |id| must either be a pointer or an integer value up to 64 bits.
337 // If it's a pointer, the bits will be xored with a hash of the process ID so
338 // that the same pointer on two different processes will not collide.
339 #define TRACE_EVENT_BEGIN_WITH_ID_TID_AND_TIMESTAMP0(category_group, name, id, \
340 thread_id) \
341 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
342 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
343 timestamp, TRACE_EVENT_FLAG_NONE)
344 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
345 name, id, thread_id) \
346 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
347 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
348 timestamp, TRACE_EVENT_FLAG_COPY)
349
350 // Records a single END event for "name" immediately. If the category
351 // is not enabled, then this does nothing.
352 // - category and name strings must have application lifetime (statics or
353 // literals). They may not include " chars.
354 #define TRACE_EVENT_END0(category_group, name) \
355 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
356 TRACE_EVENT_FLAG_NONE)
357 #define TRACE_EVENT_END1(category_group, name, arg1_name, arg1_val) \
358 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
359 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
360 #define TRACE_EVENT_END2(category_group, name, arg1_name, arg1_val, arg2_name, \
361 arg2_val) \
362 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
363 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
364 arg2_name, arg2_val)
365 #define TRACE_EVENT_COPY_END0(category_group, name) \
366 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
367 TRACE_EVENT_FLAG_COPY)
368 #define TRACE_EVENT_COPY_END1(category_group, name, arg1_name, arg1_val) \
369 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
370 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
371 #define TRACE_EVENT_COPY_END2(category_group, name, arg1_name, arg1_val, \
372 arg2_name, arg2_val) \
373 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
374 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
375 arg2_name, arg2_val)
376
377 // Similar to TRACE_EVENT_ENDx but with a custom |at| timestamp provided.
378 // - |id| is used to match the _BEGIN event with the _END event.
379 // Events are considered to match if their category_group, name and id values
380 // all match. |id| must either be a pointer or an integer value up to 64 bits.
381 // If it's a pointer, the bits will be xored with a hash of the process ID so
382 // that the same pointer on two different processes will not collide.
383 #define TRACE_EVENT_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, name, id, \
384 thread_id) \
385 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
386 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
387 timestamp, TRACE_EVENT_FLAG_NONE)
388 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, name, \
389 id, thread_id) \
390 INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
391 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
392 timestamp, TRACE_EVENT_FLAG_COPY)
393
394 // Records the value of a counter called "name" immediately. Value
395 // must be representable as a 32 bit integer.
396 // - category and name strings must have application lifetime (statics or
397 // literals). They may not include " chars.
398 #define TRACE_COUNTER1(category_group, name, value) \
399 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
400 TRACE_EVENT_FLAG_NONE, "value", \
401 static_cast<int>(value))
402 #define TRACE_COPY_COUNTER1(category_group, name, value) \
403 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
404 TRACE_EVENT_FLAG_COPY, "value", \
405 static_cast<int>(value))
406
407 // Records the values of a multi-parted counter called "name" immediately.
408 // The UI will treat value1 and value2 as parts of a whole, displaying their
409 // values as a stacked-bar chart.
410 // - category and name strings must have application lifetime (statics or
411 // literals). They may not include " chars.
412 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \
413 value2_name, value2_val) \
414 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
415 TRACE_EVENT_FLAG_NONE, value1_name, \
416 static_cast<int>(value1_val), value2_name, \
417 static_cast<int>(value2_val))
418 #define TRACE_COPY_COUNTER2(category_group, name, value1_name, value1_val, \
419 value2_name, value2_val) \
420 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
421 TRACE_EVENT_FLAG_COPY, value1_name, \
422 static_cast<int>(value1_val), value2_name, \
423 static_cast<int>(value2_val))
424
425 // Records the value of a counter called "name" immediately. Value
426 // must be representable as a 32 bit integer.
427 // - category and name strings must have application lifetime (statics or
428 // literals). They may not include " chars.
429 // - |id| is used to disambiguate counters with the same name. It must either
430 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
431 // will be xored with a hash of the process ID so that the same pointer on
432 // two different processes will not collide.
433 #define TRACE_COUNTER_ID1(category_group, name, id, value) \
434 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, category_group, \
435 name, id, TRACE_EVENT_FLAG_NONE, "value", \
436 static_cast<int>(value))
437 #define TRACE_COPY_COUNTER_ID1(category_group, name, id, value) \
438 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, category_group, \
439 name, id, TRACE_EVENT_FLAG_COPY, "value", \
440 static_cast<int>(value))
441
442 // Records the values of a multi-parted counter called "name" immediately.
443 // The UI will treat value1 and value2 as parts of a whole, displaying their
444 // values as a stacked-bar chart.
445 // - category and name strings must have application lifetime (statics or
446 // literals). They may not include " chars.
447 // - |id| is used to disambiguate counters with the same name. It must either
448 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
449 // will be xored with a hash of the process ID so that the same pointer on
450 // two different processes will not collide.
451 #define TRACE_COUNTER_ID2(category_group, name, id, value1_name, value1_val, \
452 value2_name, value2_val) \
453 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, category_group, \
454 name, id, TRACE_EVENT_FLAG_NONE, \
455 value1_name, static_cast<int>(value1_val), \
456 value2_name, static_cast<int>(value2_val))
457 #define TRACE_COPY_COUNTER_ID2(category_group, name, id, value1_name, \
458 value1_val, value2_name, value2_val) \
459 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, category_group, \
460 name, id, TRACE_EVENT_FLAG_COPY, \
461 value1_name, static_cast<int>(value1_val), \
462 value2_name, static_cast<int>(value2_val))
463
464
465 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
466 // associated arguments. If the category is not enabled, then this
467 // does nothing.
468 // - category and name strings must have application lifetime (statics or
469 // literals). They may not include " chars.
470 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
471 // events are considered to match if their category_group, name and id values
472 // all match. |id| must either be a pointer or an integer value up to 64 bits.
473 // If it's a pointer, the bits will be xored with a hash of the process ID so
474 // that the same pointer on two different processes will not collide.
475 //
476 // An asynchronous operation can consist of multiple phases. The first phase is
477 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
478 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will
479 // annotate the block following the call. The ASYNC_STEP_PAST macro will
480 // annotate the block prior to the call. Note that any particular event must use
481 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the
482 // operation completes, call ASYNC_END.
483 //
484 // An ASYNC trace typically occurs on a single thread (if not, they will only be
485 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
486 // operation must use the same |name| and |id|. Each step can have its own
487 // args.
488 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id) \
489 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
490 category_group, name, id, \
491 TRACE_EVENT_FLAG_NONE)
492 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
493 arg1_val) \
494 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
495 category_group, name, id, \
496 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
497 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
498 arg1_val, arg2_name, arg2_val) \
499 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
500 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, \
501 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
502 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id) \
503 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
504 category_group, name, id, \
505 TRACE_EVENT_FLAG_COPY)
506 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
507 arg1_val) \
508 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
509 category_group, name, id, \
510 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
511 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
512 arg1_val, arg2_name, arg2_val) \
513 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
514 TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, \
515 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, arg2_val)
516
517 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the
518 // category is not enabled, then this does nothing. The |name| and |id| must
519 // match the ASYNC_BEGIN event above. The |step| param identifies this step
520 // within the async event. This should be called at the beginning of the next
521 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
522 // ASYNC_STEP_PAST events.
523 #define TRACE_EVENT_ASYNC_STEP_INTO0(category_group, name, id, step) \
524 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
525 category_group, name, id, \
526 TRACE_EVENT_FLAG_NONE, "step", step)
527 #define TRACE_EVENT_ASYNC_STEP_INTO1(category_group, name, id, step, \
528 arg1_name, arg1_val) \
529 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
530 TRACE_EVENT_PHASE_ASYNC_STEP_INTO, category_group, name, id, \
531 TRACE_EVENT_FLAG_NONE, "step", step, arg1_name, arg1_val)
532
533 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the
534 // category is not enabled, then this does nothing. The |name| and |id| must
535 // match the ASYNC_BEGIN event above. The |step| param identifies this step
536 // within the async event. This should be called at the beginning of the next
537 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
538 // ASYNC_STEP_INTO events.
539 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step) \
540 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
541 category_group, name, id, \
542 TRACE_EVENT_FLAG_NONE, "step", step)
543 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, \
544 arg1_name, arg1_val) \
545 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
546 TRACE_EVENT_PHASE_ASYNC_STEP_PAST, category_group, name, id, \
547 TRACE_EVENT_FLAG_NONE, "step", step, arg1_name, arg1_val)
548
549 // Records a single ASYNC_END event for "name" immediately. If the category
550 // is not enabled, then this does nothing.
551 #define TRACE_EVENT_ASYNC_END0(category_group, name, id) \
552 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
553 category_group, name, id, \
554 TRACE_EVENT_FLAG_NONE)
555 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
556 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
557 category_group, name, id, \
558 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
559 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
560 arg2_name, arg2_val) \
561 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
562 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, \
563 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
564 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id) \
565 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
566 category_group, name, id, \
567 TRACE_EVENT_FLAG_COPY)
568 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
569 arg1_val) \
570 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
571 category_group, name, id, \
572 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
573 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
574 arg1_val, arg2_name, arg2_val) \
575 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
576 TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, \
577 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, arg2_val)
578
579
580 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
581 // associated arguments. If the category is not enabled, then this
582 // does nothing.
583 // - category and name strings must have application lifetime (statics or
584 // literals). They may not include " chars.
585 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
586 // events are considered to match if their category_group, name and id values
587 // all match. |id| must either be a pointer or an integer value up to 64 bits.
588 // If it's a pointer, the bits will be xored with a hash of the process ID so
589 // that the same pointer on two different processes will not collide.
590 // FLOW events are different from ASYNC events in how they are drawn by the
591 // tracing UI. A FLOW defines asynchronous data flow, such as posting a ta
592 // (FLOW_BEGIN) and later executing that ta (FLOW_END). Expect FLOWs to be
593 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
594 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
595 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
596 // macros. When the operation completes, call FLOW_END. An async operation can
597 // span threads and processes, but all events in that operation must use the
598 // same |name| and |id|. Each event can have its own args.
599 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
600 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
601 category_group, name, id, \
602 TRACE_EVENT_FLAG_NONE)
603 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
604 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
605 category_group, name, id, \
606 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
607 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
608 arg2_name, arg2_val) \
609 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
610 TRACE_EVENT_PHASE_FLOW_BEGIN, category_group, name, id, \
611 TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
612 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
613 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
614 category_group, name, id, \
615 TRACE_EVENT_FLAG_COPY)
616 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
617 arg1_val) \
618 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
619 category_group, name, id, \
620 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
621 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
622 arg1_val, arg2_name, arg2_val) \
623 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
624 TRACE_EVENT_PHASE_FLOW_BEGIN, category_group, name, id, \
625 TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, arg2_val)
626
627 // Records a single FLOW_STEP event for |step| immediately. If the category
628 // is not enabled, then this does nothing. The |name| and |id| must match the
629 // FLOW_BEGIN event above. The |step| param identifies this step within the
630 // async event. This should be called at the beginning of the next phase of an
631 // asynchronous operation.
632 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
633 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
634 category_group, name, id, \
635 TRACE_EVENT_FLAG_NONE, "step", step)
636 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, arg1_name, \
637 arg1_val) \
638 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
639 TRACE_EVENT_PHASE_FLOW_STEP, category_group, name, id, \
640 TRACE_EVENT_FLAG_NONE, "step", step, arg1_name, arg1_val)
641 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
642 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
643 category_group, name, id, \
644 TRACE_EVENT_FLAG_COPY, "step", step)
645 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, arg1_name, \
646 arg1_val) \
647 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
648 TRACE_EVENT_PHASE_FLOW_STEP, category_group, name, id, \
649 TRACE_EVENT_FLAG_COPY, "step", step, arg1_name, arg1_val)
650
651 // Records a single FLOW_END event for "name" immediately. If the category
652 // is not enabled, then this does nothing.
653 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
654 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
655 name, id, TRACE_EVENT_FLAG_NONE)
656 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
657 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
658 name, id, TRACE_EVENT_FLAG_NONE, arg1_name, \
659 arg1_val)
660 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
661 arg2_name, arg2_val) \
662 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
663 name, id, TRACE_EVENT_FLAG_NONE, arg1_name, \
664 arg1_val, arg2_name, arg2_val)
665 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
666 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
667 name, id, TRACE_EVENT_FLAG_COPY)
668 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
669 arg1_val) \
670 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
671 name, id, TRACE_EVENT_FLAG_COPY, arg1_name, \
672 arg1_val)
673 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
674 arg1_val, arg2_name, arg2_val) \
675 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
676 name, id, TRACE_EVENT_FLAG_COPY, arg1_name, \
677 arg1_val, arg2_name, arg2_val)
678
679 // Macros to track the life time and value of arbitrary client objects.
680 // See also TraceTrackableObject.
681 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
682 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
683 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group, name, \
684 TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
685
686 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, \
687 snapshot) \
688 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
689 TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name, \
690 TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE, "snapshot", snapshot)
691
692 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
693 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
694 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group, name, \
695 TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
696
697 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
698 *INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
699 (EventTracer::kEnabledForRecording_CategoryGroupEnabledFlags | \
700 EventTracer::kEnabledForEventCallback_CategoryGroupEnabledFlags)
701
702 // Macro to efficiently determine if a given category group is enabled.
703 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
704 do { \
705 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
706 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
707 *ret = true; \
708 } else { \
709 *ret = false; \
710 } \
711 } while (0)
712
713 // Macro to efficiently determine, through polling, if a new trace has begun.
714 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
715 do { \
716 static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
717 int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
718 if (num_traces_recorded != -1 && \
719 num_traces_recorded != \
720 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
721 INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = num_traces_recorded; \
722 *ret = true; \
723 } else { \
724 *ret = false; \
725 } \
726 } while (0)
727
728 ////////////////////////////////////////////////////////////////////////////////
729 // Implementation specific tracing API definitions.
730
731 // Get a pointer to the enabled state of the given trace category. Only
732 // long-lived literal strings should be given as the category group. The
733 // returned pointer can be held permanently in a local static for example. If
734 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
735 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
736 // between the load of the tracing state and the call to
737 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
738 // for best performance when tracing is disabled.
739 // const uint8_t*
740 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
741 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
742 EventTracer::GetInstance()->GetCategoryGroupEnabled
743
744 // Get the number of times traces have been recorded. This is used to implement
745 // the TRACE_EVENT_IS_NEW_TRACE facility.
746 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
747 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
748 EventTracer::GetInstance()->getNumTracesRecorded
749
750 // Add a trace event to the platform tracing system.
751 // EventTracer::Handle TRACE_EVENT_API_ADD_TRACE_EVENT(
752 // char phase,
753 // const uint8_t* category_group_enabled,
754 // const char* name,
755 // uint64_t id,
756 // int num_args,
757 // const char** arg_names,
758 // const uint8_t* arg_types,
759 // const uint64_t* arg_values,
760 // unsigned char flags)
761 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
762 EventTracer::GetInstance()->AddTraceEvent
763
764 // Set the duration field of a COMPLETE trace event.
765 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
766 // const uint8_t* category_group_enabled,
767 // const char* name,
768 // EventTracer::Handle id)
769 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
770 EventTracer::GetInstance()->UpdateTraceEventDuration
771
772 // These operations are atomic in the Chrome tracing implementation
773 // to cater to ARM's weak memory consistency; we're just doing read/
774 // write here because it's not strictly needed for correctness.
775 // So says Nat.
776 // FIXME
777
778 #define TRACE_EVENT_API_ATOMIC_WORD intptr_t
779 #define TRACE_EVENT_API_ATOMIC_LOAD(var) (*(&var))
780 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) (var = value)
781
782 // Defines visibility for classes in trace_event.h
783 // #define TRACE_EVENT_API_CLASS_EXPORT _API
784
785 // The thread buckets for the sampling profiler.
786 extern TRACE_EVENT_API_ATOMIC_WORD g_trace_state[3];
787
788 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket) \
789 g_trace_state[thread_bucket]
790
791 ////////////////////////////////////////////////////////////////////////////////
792
793 // Implementation detail: trace event macros create temporary variables
794 // to keep instrumentation overhead low. These macros give each temporary
795 // variable a unique name based on the line number to prevent name collisions.
796 #define INTERNAL_TRACE_EVENT_UID3(a, b) trace_event_unique_##a##b
797 #define INTERNAL_TRACE_EVENT_UID2(a, b) INTERNAL_TRACE_EVENT_UID3(a, b)
798 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
799 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
800
801 // Implementation detail: internal macro to create static category.
802 // No barriers are needed, because this code is designed to operate safely
803 // even when the unsigned char* points to garbage data (which may be the case
804 // on processors without cache coherency).
805 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
806 category_group, atomic, category_group_enabled) \
807 category_group_enabled = \
808 reinterpret_cast<const uint8_t*>(TRACE_EVENT_API_ATOMIC_LOAD(atomic)); \
809 if (!category_group_enabled) { \
810 category_group_enabled = \
811 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
812 TRACE_EVENT_API_ATOMIC_STORE( \
813 atomic, reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
814 category_group_enabled)); \
815 }
816
817 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
818 static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
819 const uint8_t* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
820 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
821 category_group, INTERNAL_TRACE_EVENT_UID(atomic), \
822 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
823
824 // Implementation detail: internal macro to create static category and add
825 // event if the category is enabled.
826 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, ...) \
827 do { \
828 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
829 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
830 v8::tracing_internals::AddTraceEvent( \
831 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
832 v8::tracing_internals::kNoEventId, __VA_ARGS__); \
833 } \
834 } while (0)
835
836 // Implementation detail: internal macro to create static category and add begin
837 // event if the category is enabled. Also adds the end event when the scope
838 // ends.
839 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
840 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
841 v8::tracing_internals::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
842 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
843 EventTracer::Handle h = v8::tracing_internals::AddTraceEvent( \
844 TRACE_EVENT_PHASE_COMPLETE, \
845 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
846 v8::tracing_internals::kNoEventId, TRACE_EVENT_FLAG_NONE, \
847 __VA_ARGS__); \
848 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
849 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
850 }
851
852 // Implementation detail: internal macro to create static category and add
853 // event if the category is enabled.
854 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
855 flags, ...) \
856 do { \
857 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
858 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
859 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
860 v8::tracing_internals::TraceID trace_event_trace_id(id, \
861 &trace_event_flags); \
862 v8::tracing_internals::AddTraceEvent( \
863 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
864 trace_event_trace_id.data(), trace_event_flags, __VA_ARGS__); \
865 } \
866 } while (0)
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_WITH_ID_TID_AND_TIMESTAMP( \
871 phase, category_group, name, id, thread_id, flags, ...) \
872 do { \
873 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
874 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
875 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
876 v8::tracing_internals::TraceID trace_event_trace_id(id, \
877 &trace_event_flags); \
878 v8::tracing_internals::AddTraceEventWithThreadIdAndTimestamp( \
879 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
880 trace_event_trace_id.data(), thread_id, \
881 base::TimeTicks::FromInternalValue(timestamp), trace_event_flags, \
882 __VA_ARGS__); \
883 } \
884 } while (0)
885
886 // Notes regarding the following definitions:
887 // New values can be added and propagated to third party libraries, but existing
888 // definitions must never be changed, because third party libraries may use old
889 // definitions.
890
891 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
892 #define TRACE_EVENT_PHASE_BEGIN ('B')
893 #define TRACE_EVENT_PHASE_END ('E')
894 #define TRACE_EVENT_PHASE_COMPLETE ('X')
895 #define TRACE_EVENT_PHASE_INSTANT ('i')
dsinclair 2015/06/24 19:05:09 This doesn't match the base/trace_event/trace_even
896 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
897 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T')
898 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p')
899 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
900 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
dsinclair 2015/06/24 19:05:09 The NESTABLE_ASYNC_ stuff is missing from here (an
901 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
902 #define TRACE_EVENT_PHASE_FLOW_END ('f')
903 #define TRACE_EVENT_PHASE_METADATA ('M')
904 #define TRACE_EVENT_PHASE_COUNTER ('C')
905 #define TRACE_EVENT_PHASE_SAMPLE ('P')
906 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
907 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
908 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
909
dsinclair 2015/06/24 19:05:09 PHASE_MEMORY_DUMP 'v' is missing
910 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
911 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0))
912 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0))
913 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1))
914 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2))
915 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned char>(1 << 3))
916
dsinclair 2015/06/24 19:05:09 There are a few flags missing from here _SCOPE_EXT
917 #define TRACE_EVENT_FLAG_SCOPE_MA \
dsinclair 2015/06/24 19:05:09 Seem to have lost the SK on the end? SCOPE_MASK
918 (static_cast<unsigned char>(TRACE_EVENT_FLAG_SCOPE_OFFSET | \
919 (TRACE_EVENT_FLAG_SCOPE_OFFSET << 1)))
dsinclair 2015/06/24 19:05:09 This is SCOPE_EXTRA in the base version.
920
921 // Type values for identifying types in the TraceValue union.
922 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
923 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
924 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
925 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
926 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
927 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
928 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
929 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
930
931 // Enum reflecting the scope of an INSTANT event. Must fit within
932 // TRACE_EVENT_FLAG_SCOPE_MA.
dsinclair 2015/06/24 19:05:09 SK
933 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
934 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
935 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
936
937 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
938 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
939 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
940
941 namespace v8 {
942 namespace tracing_internals {
943
944 // Specify these values when the corresponding argument of AddTraceEvent is not
945 // used.
946 const int kZeroNumArgs = 0;
947 const uint64_t kNoEventId = 0;
948
949 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
950 // are by default mangled with the Process ID so that they are unlikely to
951 // collide when the same pointer is used on different processes.
952 class TraceID {
953 public:
954 class DontMangle {
955 public:
956 explicit DontMangle(const void* id)
957 : data_(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(id))) {}
958 explicit DontMangle(uint64_t id) : data_(id) {}
959 explicit DontMangle(unsigned int id) : data_(id) {}
960 explicit DontMangle(uint16_t id) : data_(id) {}
961 explicit DontMangle(unsigned char id) : data_(id) {}
962 explicit DontMangle(int64_t id) : data_(static_cast<uint64_t>(id)) {}
963 explicit DontMangle(int id) : data_(static_cast<uint64_t>(id)) {}
964 explicit DontMangle(int16_t id) : data_(static_cast<uint64_t>(id)) {}
965 explicit DontMangle(signed char id) : data_(static_cast<uint64_t>(id)) {}
966 uint64_t data() const { return data_; }
967
968 private:
969 uint64_t data_;
970 };
971
972 class ForceMangle {
973 public:
974 explicit ForceMangle(uint64_t id) : data_(id) {}
975 explicit ForceMangle(unsigned int id) : data_(id) {}
976 explicit ForceMangle(uint16_t id) : data_(id) {}
977 explicit ForceMangle(unsigned char id) : data_(id) {}
978 explicit ForceMangle(int64_t id) : data_(static_cast<uint64_t>(id)) {}
979 explicit ForceMangle(int id) : data_(static_cast<uint64_t>(id)) {}
980 explicit ForceMangle(int16_t id) : data_(static_cast<uint64_t>(id)) {}
981 explicit ForceMangle(signed char id) : data_(static_cast<uint64_t>(id)) {}
982 uint64_t data() const { return data_; }
983
984 private:
985 uint64_t data_;
986 };
987
988 TraceID(const void* id, unsigned char* flags)
989 : data_(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(id))) {
990 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
991 }
992 TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
993 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
994 }
995 TraceID(DontMangle id, unsigned char* flags) : data_(id.data()) {}
996 TraceID(uint64_t id, unsigned char* flags) : data_(id) { (void)flags; }
997 TraceID(unsigned int id, unsigned char* flags) : data_(id) { (void)flags; }
998 TraceID(uint16_t id, unsigned char* flags) : data_(id) { (void)flags; }
999 TraceID(unsigned char id, unsigned char* flags) : data_(id) { (void)flags; }
1000 TraceID(int64_t id, unsigned char* flags) : data_(static_cast<uint64_t>(id)) {
1001 (void)flags;
1002 }
1003 TraceID(int id, unsigned char* flags) : data_(static_cast<uint64_t>(id)) {
1004 (void)flags;
1005 }
1006 TraceID(int16_t id, unsigned char* flags) : data_(static_cast<uint64_t>(id)) {
1007 (void)flags;
1008 }
1009 TraceID(signed char id, unsigned char* flags)
1010 : data_(static_cast<uint64_t>(id)) {
1011 (void)flags;
1012 }
1013
1014 uint64_t data() const { return data_; }
1015
1016 private:
1017 uint64_t data_;
1018 };
1019
1020 // Simple union to store various types as uint64_t.
1021 union TraceValueUnion {
1022 bool as_bool;
1023 uint64_t as_uint;
1024 int64_t as_int;
1025 double as_double;
1026 const void* as_pointer;
1027 const char* as_string;
1028 };
1029
1030 // Simple container for const char* that should be copied instead of retained.
1031 class TraceStringWithCopy {
1032 public:
1033 explicit TraceStringWithCopy(const char* str) : str_(str) {}
1034 operator const char*() const { return str_; }
1035
1036 private:
1037 const char* str_;
1038 };
1039
1040 // Define SetTraceValue for each allowed type. It stores the type and
1041 // value in the return arguments. This allows this API to avoid declaring any
1042 // structures so that it is portable to third_party libraries.
1043 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, union_member, \
1044 value_type_id) \
1045 static inline void SetTraceValue(actual_type arg, unsigned char* type, \
1046 uint64_t* value) { \
1047 TraceValueUnion type_value; \
1048 type_value.union_member = arg; \
1049 *type = value_type_id; \
1050 *value = type_value.as_uint; \
1051 }
1052 // Simpler form for int types that can be safely casted.
1053 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, value_type_id) \
1054 static inline void SetTraceValue(actual_type arg, unsigned char* type, \
1055 uint64_t* value) { \
1056 *type = value_type_id; \
1057 *value = static_cast<uint64_t>(arg); \
1058 }
1059
1060 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(uint64_t, TRACE_VALUE_TYPE_UINT)
1061 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
1062 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(uint16_t, TRACE_VALUE_TYPE_UINT)
1063 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
1064 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int64_t, TRACE_VALUE_TYPE_INT)
1065 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
1066 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int16_t, TRACE_VALUE_TYPE_INT)
1067 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
1068 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
1069 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
1070 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer,
1071 TRACE_VALUE_TYPE_POINTER)
1072 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string,
1073 TRACE_VALUE_TYPE_STRING)
1074 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
1075 TRACE_VALUE_TYPE_COPY_STRING)
1076
1077 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
1078 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
1079
1080 // These AddTraceEvent and AddTraceEvent template
1081 // functions are defined here instead of in the macro, because the arg_values
1082 // could be temporary objects, such as std::string. In order to store
1083 // pointers to the internal c_str and pass through to the tracing API,
1084 // the arg_values must live throughout these procedures.
1085
1086 static inline EventTracer::Handle AddTraceEvent(
1087 char phase, const uint8_t* category_group_enabled, const char* name,
1088 uint64_t id, unsigned char flags) {
1089 return TRACE_EVENT_API_ADD_TRACE_EVENT(phase, category_group_enabled, name,
1090 id, kZeroNumArgs, NULL, NULL, NULL,
1091 flags);
1092 }
1093
1094 template <class ARG1_TYPE>
1095 static inline EventTracer::Handle AddTraceEvent(
1096 char phase, const uint8_t* category_group_enabled, const char* name,
1097 uint64_t id, unsigned char flags, const char* arg1_name,
1098 const ARG1_TYPE& arg1_val) {
1099 const int num_args = 1;
1100 uint8_t arg_types[1];
1101 uint64_t arg_values[1];
1102 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1103 return TRACE_EVENT_API_ADD_TRACE_EVENT(phase, category_group_enabled, name,
1104 id, num_args, &arg1_name, arg_types,
1105 arg_values, flags);
1106 }
1107
1108 template <class ARG1_TYPE, class ARG2_TYPE>
1109 static inline EventTracer::Handle AddTraceEvent(
1110 char phase, const uint8_t* category_group_enabled, const char* name,
1111 uint64_t id, unsigned char flags, const char* arg1_name,
1112 const ARG1_TYPE& arg1_val, const char* arg2_name,
1113 const ARG2_TYPE& arg2_val) {
1114 const int num_args = 2;
1115 const char* arg_names[2] = {arg1_name, arg2_name};
1116 unsigned char arg_types[2];
1117 uint64_t arg_values[2];
1118 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
1119 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
1120 return TRACE_EVENT_API_ADD_TRACE_EVENT(phase, category_group_enabled, name,
1121 id, num_args, arg_names, arg_types,
1122 arg_values, flags);
1123 }
1124
1125 // Used by TRACE_EVENTx macros. Do not use directly.
1126 class ScopedTracer {
1127 public:
1128 // Note: members of data_ intentionally left uninitialized. See Initialize.
1129 ScopedTracer() : p_data_(NULL) {}
1130
1131 ~ScopedTracer() {
1132 if (p_data_ && *data_.category_group_enabled)
1133 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
1134 data_.category_group_enabled, data_.name, data_.event_handle);
1135 }
1136
1137 void Initialize(const uint8_t* category_group_enabled, const char* name,
1138 EventTracer::Handle event_handle) {
1139 data_.category_group_enabled = category_group_enabled;
1140 data_.name = name;
1141 data_.event_handle = event_handle;
1142 p_data_ = &data_;
1143 }
1144
1145 private:
1146 // This Data struct workaround is to avoid initializing all the members
1147 // in Data during construction of this object, since this object is always
1148 // constructed, even when tracing is disabled. If the members of Data were
1149 // members of this class instead, compiler warnings occur about potential
1150 // uninitialized accesses.
1151 struct Data {
1152 const uint8_t* category_group_enabled;
1153 const char* name;
1154 EventTracer::Handle event_handle;
1155 };
1156 Data* p_data_;
1157 Data data_;
1158 };
1159
1160 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
1161 class ScopedTraceBinaryEfficient {
1162 public:
1163 ScopedTraceBinaryEfficient(const char* category_group, const char* name);
1164 ~ScopedTraceBinaryEfficient();
1165
1166 private:
1167 const uint8_t* category_group_enabled_;
1168 const char* name_;
1169 EventTracer::Handle event_handle_;
1170 };
1171
1172 // This macro generates less code then TRACE_EVENT0 but is also
1173 // slower to execute when tracing is off. It should generally only be
1174 // used with code that is seldom executed or conditionally executed
1175 // when debugging.
1176 // For now the category_group must be "gpu".
1177 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
1178 v8::tracing_internals::ScopedTraceBinaryEfficient INTERNAL_TRACE_EVENT_UID( \
1179 scoped_trace)(category_group, name);
1180
1181 // TraceEventSamplingStateScope records the current sampling state
1182 // and sets a new sampling state. When the scope exists, it restores
1183 // the sampling state having recorded.
1184 template <size_t BucketNumber>
1185 class TraceEventSamplingStateScope {
1186 public:
1187 explicit TraceEventSamplingStateScope(const char* category_and_name) {
1188 previous_state_ = TraceEventSamplingStateScope<BucketNumber>::Current();
1189 TraceEventSamplingStateScope<BucketNumber>::Set(category_and_name);
1190 }
1191
1192 ~TraceEventSamplingStateScope() {
1193 TraceEventSamplingStateScope<BucketNumber>::Set(previous_state_);
1194 }
1195
1196 static inline const char* Current() {
1197 return reinterpret_cast<const char*>(
1198 TRACE_EVENT_API_ATOMIC_LOAD(g_trace_state[BucketNumber]));
1199 }
1200
1201 static inline void Set(const char* category_and_name) {
1202 TRACE_EVENT_API_ATOMIC_STORE(g_trace_state[BucketNumber],
1203 reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
1204 const_cast<char*>(category_and_name)));
1205 }
1206
1207 private:
1208 const char* previous_state_;
1209 };
1210
1211 } // namespace tracing_internals
1212 } // namespace v8
1213
1214 #endif // SRC_TRACING_TRACE_EVENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698