OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This header is designed to give you trace_event macros without specifying | 5 // This header defines the set of functions necessary for trace_event_internal.h |
6 // how the events actually get collected and stored. If you need to expose trace | 6 // to remain platform and context neutral. To use tracing in another context, |
7 // event to some other universe, you can copy-and-paste this file, | 7 // copy this header (and trace_event_internal.h) and implement the |
8 // implement the TRACE_EVENT_API macros, and do any other necessary fixup for | 8 // TRACE_EVENT_API_* macros below for the new environment. |
9 // the target platform. The end result is that multiple libraries can funnel | |
10 // events 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 // | |
22 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope: | |
23 // TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly") | |
24 // doSomethingCostly() | |
25 // TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly") | |
26 // Note: our tools can't always determine the correct BEGIN/END pairs unless | |
27 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you | |
28 // need them to be in separate scopes. | |
29 // | |
30 // A common use case is to trace entire function scopes. This | |
31 // issues a trace BEGIN and END automatically: | |
32 // void doSomethingCostly() { | |
33 // TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly"); | |
34 // ... | |
35 // } | |
36 // | |
37 // Additional parameters can be associated with an event: | |
38 // void doSomethingCostly2(int howMuch) { | |
39 // TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly", | |
40 // "howMuch", howMuch); | |
41 // ... | |
42 // } | |
43 // | |
44 // The trace system will automatically add to this information the | |
45 // current process id, thread id, and a timestamp in microseconds. | |
46 // | |
47 // To trace an asynchronous procedure such as an IPC send/receive, use | |
48 // ASYNC_BEGIN and ASYNC_END: | |
49 // [single threaded sender code] | |
50 // static int send_count = 0; | |
51 // ++send_count; | |
52 // TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count); | |
53 // Send(new MyMessage(send_count)); | |
54 // [receive code] | |
55 // void OnMyMessage(send_count) { | |
56 // TRACE_EVENT_ASYNC_END0("ipc", "message", send_count); | |
57 // } | |
58 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs. | |
59 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process. | |
60 // Pointers can be used for the ID parameter, and they will be mangled | |
61 // internally so that the same pointer on two different processes will not | |
62 // match. For example: | |
63 // class MyTracedClass { | |
64 // public: | |
65 // MyTracedClass() { | |
66 // TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this); | |
67 // } | |
68 // ~MyTracedClass() { | |
69 // TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this); | |
70 // } | |
71 // } | |
72 // | |
73 // Trace event also supports counters, which is a way to track a quantity | |
74 // as it varies over time. Counters are created with the following macro: | |
75 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue); | |
76 // | |
77 // Counters are process-specific. The macro itself can be issued from any | |
78 // thread, however. | |
79 // | |
80 // Sometimes, you want to track two counters at once. You can do this with two | |
81 // counter macros: | |
82 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]); | |
83 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]); | |
84 // Or you can do it with a combined macro: | |
85 // TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter", | |
86 // "bytesPinned", g_myCounterValue[0], | |
87 // "bytesAllocated", g_myCounterValue[1]); | |
88 // This indicates to the tracing UI that these counters should be displayed | |
89 // in a single graph, as a summed area chart. | |
90 // | |
91 // Since counters are in a global namespace, you may want to disembiguate with a | |
92 // unique ID, by using the TRACE_COUNTER_ID* variations. | |
93 // | |
94 // By default, trace collection is compiled in, but turned off at runtime. | |
95 // Collecting trace data is the responsibility of the embedding | |
96 // application. In Chrome's case, navigating to about:tracing will turn on | |
97 // tracing and display data collected across all active processes. | |
98 // | |
99 // | |
100 // Memory scoping note: | |
101 // Tracing copies the pointers, not the string content, of the strings passed | |
102 // in for category, name, and arg_names. Thus, the following code will | |
103 // cause problems: | |
104 // char* str = strdup("impprtantName"); | |
105 // TRACE_EVENT_INSTANT0("SUBSYSTEM", str); // BAD! | |
106 // free(str); // Trace system now has dangling pointer | |
107 // | |
108 // To avoid this issue with the |name| and |arg_name| parameters, use the | |
109 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead. | |
110 // Notes: The category must always be in a long-lived char* (i.e. static const). | |
111 // The |arg_values|, when used, are always deep copied with the _COPY | |
112 // macros. | |
113 // | |
114 // When are string argument values copied: | |
115 // const char* arg_values are only referenced by default: | |
116 // TRACE_EVENT1("category", "name", | |
117 // "arg1", "literal string is only referenced"); | |
118 // Use TRACE_STR_COPY to force copying of a const char*: | |
119 // TRACE_EVENT1("category", "name", | |
120 // "arg1", TRACE_STR_COPY("string will be copied")); | |
121 // std::string arg_values are always copied: | |
122 // TRACE_EVENT1("category", "name", | |
123 // "arg1", std::string("string will be copied")); | |
124 // | |
125 // | |
126 // Thread Safety: | |
127 // A thread safe singleton and mutex are used for thread safety. Category | |
128 // enabled flags are used to limit the performance impact when the system | |
129 // is not enabled. | |
130 // | |
131 // TRACE_EVENT macros first cache a pointer to a category. The categories are | |
132 // statically allocated and safe at all times, even after exit. Fetching a | |
133 // category is protected by the TraceLog::lock_. Multiple threads initializing | |
134 // the static variable is safe, as they will be serialized by the lock and | |
135 // multiple calls will return the same pointer to the category. | |
136 // | |
137 // Then the category_enabled flag is checked. This is a unsigned char, and | |
138 // not intended to be multithread safe. It optimizes access to AddTraceEvent | |
139 // which is threadsafe internally via TraceLog::lock_. The enabled flag may | |
140 // cause some threads to incorrectly call or skip calling AddTraceEvent near | |
141 // the time of the system being enabled or disabled. This is acceptable as | |
142 // we tolerate some data loss while the system is being enabled/disabled and | |
143 // because AddTraceEvent is threadsafe internally and checks the enabled state | |
144 // again under lock. | |
145 // | |
146 // Without the use of these static category pointers and enabled flags all | |
147 // trace points would carry a significant performance cost of aquiring a lock | |
148 // and resolving the category. | |
149 | |
150 | 9 |
151 #ifndef BASE_DEBUG_TRACE_EVENT_H_ | 10 #ifndef BASE_DEBUG_TRACE_EVENT_H_ |
152 #define BASE_DEBUG_TRACE_EVENT_H_ | 11 #define BASE_DEBUG_TRACE_EVENT_H_ |
153 | 12 |
154 #include <string> | |
155 | |
156 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
157 #include "base/debug/trace_event_impl.h" | 14 #include "base/debug/trace_event_impl.h" |
158 #include "build/build_config.h" | 15 #include "build/build_config.h" |
159 | 16 |
160 // By default, const char* argument values are assumed to have long-lived scope | |
161 // and will not be copied. Use this macro to force a const char* to be copied. | |
162 #define TRACE_STR_COPY(str) \ | |
163 trace_event_internal::TraceStringWithCopy(str) | |
164 | |
165 // By default, uint64 ID argument values are not mangled with the Process ID in | |
166 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling. | |
167 #define TRACE_ID_MANGLE(id) \ | |
168 trace_event_internal::TraceID::ForceMangle(id) | |
169 | |
170 // Records a pair of begin and end events called "name" for the current | |
171 // scope, with 0, 1 or 2 associated arguments. If the category is not | |
172 // enabled, then this does nothing. | |
173 // - category and name strings must have application lifetime (statics or | |
174 // literals). They may not include " chars. | |
175 #define TRACE_EVENT0(category, name) \ | |
176 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) | |
177 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | |
178 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) | |
179 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | |
180 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \ | |
181 arg2_name, arg2_val) | |
182 | |
183 // Same as TRACE_EVENT except that they are not included in official builds. | |
184 #ifdef OFFICIAL_BUILD | |
185 #define UNSHIPPED_TRACE_EVENT0(category, name) (void)0 | |
186 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) (void)0 | |
187 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \ | |
188 arg2_name, arg2_val) (void)0 | |
189 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) (void)0 | |
190 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | |
191 (void)0 | |
192 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
193 arg2_name, arg2_val) (void)0 | |
194 #else | |
195 #define UNSHIPPED_TRACE_EVENT0(category, name) \ | |
196 TRACE_EVENT0(category, name) | |
197 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | |
198 TRACE_EVENT1(category, name, arg1_name, arg1_val) | |
199 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \ | |
200 arg2_name, arg2_val) \ | |
201 TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) | |
202 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) \ | |
203 TRACE_EVENT_INSTANT0(category, name) | |
204 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | |
205 TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) | |
206 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
207 arg2_name, arg2_val) \ | |
208 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
209 arg2_name, arg2_val) | |
210 #endif | |
211 | |
212 // Records a single event called "name" immediately, with 0, 1 or 2 | |
213 // associated arguments. If the category is not enabled, then this | |
214 // does nothing. | |
215 // - category and name strings must have application lifetime (statics or | |
216 // literals). They may not include " chars. | |
217 #define TRACE_EVENT_INSTANT0(category, name) \ | |
218 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
219 category, name, TRACE_EVENT_FLAG_NONE) | |
220 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | |
221 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
222 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
223 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
224 arg2_name, arg2_val) \ | |
225 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
226 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
227 arg2_name, arg2_val) | |
228 #define TRACE_EVENT_COPY_INSTANT0(category, name) \ | |
229 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
230 category, name, TRACE_EVENT_FLAG_COPY) | |
231 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \ | |
232 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
233 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
234 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \ | |
235 arg2_name, arg2_val) \ | |
236 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
237 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
238 arg2_name, arg2_val) | |
239 | |
240 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2 | |
241 // associated arguments. If the category is not enabled, then this | |
242 // does nothing. | |
243 // - category and name strings must have application lifetime (statics or | |
244 // literals). They may not include " chars. | |
245 #define TRACE_EVENT_BEGIN0(category, name) \ | |
246 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
247 category, name, TRACE_EVENT_FLAG_NONE) | |
248 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \ | |
249 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
250 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
251 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \ | |
252 arg2_name, arg2_val) \ | |
253 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
254 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
255 arg2_name, arg2_val) | |
256 #define TRACE_EVENT_COPY_BEGIN0(category, name) \ | |
257 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
258 category, name, TRACE_EVENT_FLAG_COPY) | |
259 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \ | |
260 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
261 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
262 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \ | |
263 arg2_name, arg2_val) \ | |
264 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
265 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
266 arg2_name, arg2_val) | |
267 | |
268 // Records a single END event for "name" immediately. If the category | |
269 // is not enabled, then this does nothing. | |
270 // - category and name strings must have application lifetime (statics or | |
271 // literals). They may not include " chars. | |
272 #define TRACE_EVENT_END0(category, name) \ | |
273 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
274 category, name, TRACE_EVENT_FLAG_NONE) | |
275 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \ | |
276 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
277 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
278 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \ | |
279 arg2_name, arg2_val) \ | |
280 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
281 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
282 arg2_name, arg2_val) | |
283 #define TRACE_EVENT_COPY_END0(category, name) \ | |
284 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
285 category, name, TRACE_EVENT_FLAG_COPY) | |
286 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ | |
287 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
288 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
289 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ | |
290 arg2_name, arg2_val) \ | |
291 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
292 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
293 arg2_name, arg2_val) | |
294 | |
295 // Time threshold event: | |
296 // Only record the event if the duration is greater than the specified | |
297 // threshold_us (time in microseconds). | |
298 // Records a pair of begin and end events called "name" for the current | |
299 // scope, with 0, 1 or 2 associated arguments. If the category is not | |
300 // enabled, then this does nothing. | |
301 // - category and name strings must have application lifetime (statics or | |
302 // literals). They may not include " chars. | |
303 #define TRACE_EVENT_IF_LONGER_THAN0(threshold_us, category, name) \ | |
304 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold_us, category, name) | |
305 #define TRACE_EVENT_IF_LONGER_THAN1( \ | |
306 threshold_us, category, name, arg1_name, arg1_val) \ | |
307 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ | |
308 threshold_us, category, name, arg1_name, arg1_val) | |
309 #define TRACE_EVENT_IF_LONGER_THAN2( \ | |
310 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | |
311 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ | |
312 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) | |
313 | |
314 // Records the value of a counter called "name" immediately. Value | |
315 // must be representable as a 32 bit integer. | |
316 // - category and name strings must have application lifetime (statics or | |
317 // literals). They may not include " chars. | |
318 #define TRACE_COUNTER1(category, name, value) \ | |
319 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
320 category, name, TRACE_EVENT_FLAG_NONE, \ | |
321 "value", static_cast<int>(value)) | |
322 #define TRACE_COPY_COUNTER1(category, name, value) \ | |
323 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
324 category, name, TRACE_EVENT_FLAG_COPY, \ | |
325 "value", static_cast<int>(value)) | |
326 | |
327 // Records the values of a multi-parted counter called "name" immediately. | |
328 // The UI will treat value1 and value2 as parts of a whole, displaying their | |
329 // values as a stacked-bar chart. | |
330 // - category and name strings must have application lifetime (statics or | |
331 // literals). They may not include " chars. | |
332 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ | |
333 value2_name, value2_val) \ | |
334 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
335 category, name, TRACE_EVENT_FLAG_NONE, \ | |
336 value1_name, static_cast<int>(value1_val), \ | |
337 value2_name, static_cast<int>(value2_val)) | |
338 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ | |
339 value2_name, value2_val) \ | |
340 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
341 category, name, TRACE_EVENT_FLAG_COPY, \ | |
342 value1_name, static_cast<int>(value1_val), \ | |
343 value2_name, static_cast<int>(value2_val)) | |
344 | |
345 // Records the value of a counter called "name" immediately. Value | |
346 // must be representable as a 32 bit integer. | |
347 // - category and name strings must have application lifetime (statics or | |
348 // literals). They may not include " chars. | |
349 // - |id| is used to disambiguate counters with the same name. It must either | |
350 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | |
351 // will be xored with a hash of the process ID so that the same pointer on | |
352 // two different processes will not collide. | |
353 #define TRACE_COUNTER_ID1(category, name, id, value) \ | |
354 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
355 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
356 "value", static_cast<int>(value)) | |
357 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \ | |
358 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
359 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
360 "value", static_cast<int>(value)) | |
361 | |
362 // Records the values of a multi-parted counter called "name" immediately. | |
363 // The UI will treat value1 and value2 as parts of a whole, displaying their | |
364 // values as a stacked-bar chart. | |
365 // - category and name strings must have application lifetime (statics or | |
366 // literals). They may not include " chars. | |
367 // - |id| is used to disambiguate counters with the same name. It must either | |
368 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | |
369 // will be xored with a hash of the process ID so that the same pointer on | |
370 // two different processes will not collide. | |
371 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \ | |
372 value2_name, value2_val) \ | |
373 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
374 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
375 value1_name, static_cast<int>(value1_val), \ | |
376 value2_name, static_cast<int>(value2_val)) | |
377 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \ | |
378 value2_name, value2_val) \ | |
379 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
380 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
381 value1_name, static_cast<int>(value1_val), \ | |
382 value2_name, static_cast<int>(value2_val)) | |
383 | |
384 | |
385 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2 | |
386 // associated arguments. If the category is not enabled, then this | |
387 // does nothing. | |
388 // - category and name strings must have application lifetime (statics or | |
389 // literals). They may not include " chars. | |
390 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC | |
391 // events are considered to match if their category, name and id values all | |
392 // match. |id| must either be a pointer or an integer value up to 64 bits. If | |
393 // it's a pointer, the bits will be xored with a hash of the process ID so | |
394 // that the same pointer on two different processes will not collide. | |
395 // An asynchronous operation can consist of multiple phases. The first phase is | |
396 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the | |
397 // ASYNC_STEP macros. When the operation completes, call ASYNC_END. | |
398 // An ASYNC trace typically occur on a single thread (if not, they will only be | |
399 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that | |
400 // operation must use the same |name| and |id|. Each event can have its own | |
401 // args. | |
402 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \ | |
403 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
404 category, name, id, TRACE_EVENT_FLAG_NONE) | |
405 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
406 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
407 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
408 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
409 arg2_name, arg2_val) \ | |
410 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
411 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
412 arg1_name, arg1_val, arg2_name, arg2_val) | |
413 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \ | |
414 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
415 category, name, id, TRACE_EVENT_FLAG_COPY) | |
416 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
417 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
418 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
419 arg1_name, arg1_val) | |
420 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
421 arg2_name, arg2_val) \ | |
422 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
423 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
424 arg1_name, arg1_val, arg2_name, arg2_val) | |
425 | |
426 // Records a single ASYNC_STEP event for |step| immediately. If the category | |
427 // is not enabled, then this does nothing. The |name| and |id| must match the | |
428 // ASYNC_BEGIN event above. The |step| param identifies this step within the | |
429 // async event. This should be called at the beginning of the next phase of an | |
430 // asynchronous operation. | |
431 #define TRACE_EVENT_ASYNC_STEP0(category, name, id, step) \ | |
432 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \ | |
433 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step) | |
434 #define TRACE_EVENT_ASYNC_STEP1(category, name, id, step, \ | |
435 arg1_name, arg1_val) \ | |
436 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \ | |
437 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ | |
438 arg1_name, arg1_val) | |
439 #define TRACE_EVENT_COPY_ASYNC_STEP0(category, name, id, step) \ | |
440 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \ | |
441 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step) | |
442 #define TRACE_EVENT_COPY_ASYNC_STEP1(category, name, id, step, \ | |
443 arg1_name, arg1_val) \ | |
444 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \ | |
445 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \ | |
446 arg1_name, arg1_val) | |
447 | |
448 // Records a single ASYNC_END event for "name" immediately. If the category | |
449 // is not enabled, then this does nothing. | |
450 #define TRACE_EVENT_ASYNC_END0(category, name, id) \ | |
451 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
452 category, name, id, TRACE_EVENT_FLAG_NONE) | |
453 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ | |
454 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
455 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
456 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \ | |
457 arg2_name, arg2_val) \ | |
458 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
459 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
460 arg1_name, arg1_val, arg2_name, arg2_val) | |
461 #define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \ | |
462 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
463 category, name, id, TRACE_EVENT_FLAG_COPY) | |
464 #define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ | |
465 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
466 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
467 arg1_name, arg1_val) | |
468 #define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \ | |
469 arg2_name, arg2_val) \ | |
470 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
471 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
472 arg1_name, arg1_val, arg2_name, arg2_val) | |
473 | |
474 | |
475 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2 | |
476 // associated arguments. If the category is not enabled, then this | |
477 // does nothing. | |
478 // - category and name strings must have application lifetime (statics or | |
479 // literals). They may not include " chars. | |
480 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW | |
481 // events are considered to match if their category, name and id values all | |
482 // match. |id| must either be a pointer or an integer value up to 64 bits. If | |
483 // it's a pointer, the bits will be xored with a hash of the process ID so | |
484 // that the same pointer on two different processes will not collide. | |
485 // FLOW events are different from ASYNC events in how they are drawn by the | |
486 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task | |
487 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be | |
488 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar | |
489 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined | |
490 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP | |
491 // macros. When the operation completes, call FLOW_END. An async operation can | |
492 // span threads and processes, but all events in that operation must use the | |
493 // same |name| and |id|. Each event can have its own args. | |
494 #define TRACE_EVENT_FLOW_BEGIN0(category, name, id) \ | |
495 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
496 category, name, id, TRACE_EVENT_FLAG_NONE) | |
497 #define TRACE_EVENT_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
498 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
499 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
500 #define TRACE_EVENT_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
501 arg2_name, arg2_val) \ | |
502 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
503 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
504 arg1_name, arg1_val, arg2_name, arg2_val) | |
505 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category, name, id) \ | |
506 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
507 category, name, id, TRACE_EVENT_FLAG_COPY) | |
508 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
509 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
510 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
511 arg1_name, arg1_val) | |
512 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
513 arg2_name, arg2_val) \ | |
514 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
515 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
516 arg1_name, arg1_val, arg2_name, arg2_val) | |
517 | |
518 // Records a single FLOW_STEP event for |step| immediately. If the category | |
519 // is not enabled, then this does nothing. The |name| and |id| must match the | |
520 // FLOW_BEGIN event above. The |step| param identifies this step within the | |
521 // async event. This should be called at the beginning of the next phase of an | |
522 // asynchronous operation. | |
523 #define TRACE_EVENT_FLOW_STEP0(category, name, id, step) \ | |
524 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ | |
525 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step) | |
526 #define TRACE_EVENT_FLOW_STEP1(category, name, id, step, \ | |
527 arg1_name, arg1_val) \ | |
528 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ | |
529 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ | |
530 arg1_name, arg1_val) | |
531 #define TRACE_EVENT_COPY_FLOW_STEP0(category, name, id, step) \ | |
532 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ | |
533 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step) | |
534 #define TRACE_EVENT_COPY_FLOW_STEP1(category, name, id, step, \ | |
535 arg1_name, arg1_val) \ | |
536 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ | |
537 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \ | |
538 arg1_name, arg1_val) | |
539 | |
540 // Records a single FLOW_END event for "name" immediately. If the category | |
541 // is not enabled, then this does nothing. | |
542 #define TRACE_EVENT_FLOW_END0(category, name, id) \ | |
543 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
544 category, name, id, TRACE_EVENT_FLAG_NONE) | |
545 #define TRACE_EVENT_FLOW_END1(category, name, id, arg1_name, arg1_val) \ | |
546 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
547 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
548 #define TRACE_EVENT_FLOW_END2(category, name, id, arg1_name, arg1_val, \ | |
549 arg2_name, arg2_val) \ | |
550 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
551 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
552 arg1_name, arg1_val, arg2_name, arg2_val) | |
553 #define TRACE_EVENT_COPY_FLOW_END0(category, name, id) \ | |
554 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
555 category, name, id, TRACE_EVENT_FLAG_COPY) | |
556 #define TRACE_EVENT_COPY_FLOW_END1(category, name, id, arg1_name, arg1_val) \ | |
557 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
558 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
559 arg1_name, arg1_val) | |
560 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \ | |
561 arg2_name, arg2_val) \ | |
562 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
563 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
564 arg1_name, arg1_val, arg2_name, arg2_val) | |
565 | |
566 | |
567 //////////////////////////////////////////////////////////////////////////////// | 17 //////////////////////////////////////////////////////////////////////////////// |
568 // Implementation specific tracing API definitions. | 18 // Implementation specific tracing API definitions. |
569 | 19 |
570 // Get a pointer to the enabled state of the given trace category. Only | 20 // Get a pointer to the enabled state of the given trace category. Only |
571 // long-lived literal strings should be given as the category name. The returned | 21 // long-lived literal strings should be given as the category name. The returned |
572 // pointer can be held permanently in a local static for example. If the | 22 // pointer can be held permanently in a local static for example. If the |
573 // unsigned char is non-zero, tracing is enabled. If tracing is enabled, | 23 // unsigned char is non-zero, tracing is enabled. If tracing is enabled, |
574 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled | 24 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled |
575 // between the load of the tracing state and the call to | 25 // between the load of the tracing state and the call to |
576 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out | 26 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out |
(...skipping 13 matching lines...) Expand all Loading... | |
590 // int num_args, | 40 // int num_args, |
591 // const char** arg_names, | 41 // const char** arg_names, |
592 // const unsigned char* arg_types, | 42 // const unsigned char* arg_types, |
593 // const unsigned long long* arg_values, | 43 // const unsigned long long* arg_values, |
594 // int threshold_begin_id, | 44 // int threshold_begin_id, |
595 // long long threshold, | 45 // long long threshold, |
596 // unsigned char flags) | 46 // unsigned char flags) |
597 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ | 47 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ |
598 base::debug::TraceLog::GetInstance()->AddTraceEvent | 48 base::debug::TraceLog::GetInstance()->AddTraceEvent |
599 | 49 |
50 // Defines atomic operations used internally by the tracing system. | |
51 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord | |
52 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var)) | |
53 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \ | |
54 base::subtle::NoBarrier_Store(&(var), (value)) | |
55 | |
56 // Defines visibility for classes in trace_event_internal.h | |
57 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT | |
brettw
2013/01/02 22:44:25
Is it possible to #undef this stuff after includin
elijahtaylor1
2013/01/03 22:11:35
Done.
elijahtaylor1
2013/01/03 22:22:32
Clicked Done before and forgot to edit. This was
| |
58 | |
600 //////////////////////////////////////////////////////////////////////////////// | 59 //////////////////////////////////////////////////////////////////////////////// |
601 | 60 |
602 // Implementation detail: trace event macros create temporary variables | 61 #include "base/debug/trace_event_internal.h" |
603 // to keep instrumentation overhead low. These macros give each temporary | |
604 // variable a unique name based on the line number to prevent name collissions. | |
605 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ | |
606 trace_event_unique_##a##b | |
607 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ | |
608 INTERNAL_TRACE_EVENT_UID3(a,b) | |
609 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ | |
610 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) | |
611 | 62 |
612 // Implementation detail: internal macro to create static category. | 63 #endif /* BASE_DEBUG_TRACE_EVENT_H_ */ |
613 // No barriers are needed, because this code is designed to operate safely | |
614 // even when the unsigned char* points to garbage data (which may be the case | |
615 // on processors without cache coherency). | |
616 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ | |
617 static base::subtle::AtomicWord INTERNAL_TRACE_EVENT_UID(atomic) = 0; \ | |
618 const uint8* INTERNAL_TRACE_EVENT_UID(catstatic) = \ | |
619 reinterpret_cast<const uint8*>( \ | |
620 base::subtle::NoBarrier_Load(&INTERNAL_TRACE_EVENT_UID(atomic))); \ | |
621 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
622 INTERNAL_TRACE_EVENT_UID(catstatic) = \ | |
623 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \ | |
624 base::subtle::NoBarrier_Store(&INTERNAL_TRACE_EVENT_UID(atomic), \ | |
625 reinterpret_cast<base::subtle::AtomicWord>( \ | |
626 INTERNAL_TRACE_EVENT_UID(catstatic))); \ | |
627 } | |
628 | |
629 // Implementation detail: internal macro to create static category and add | |
630 // event if the category is enabled. | |
631 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ | |
632 do { \ | |
633 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
634 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
635 trace_event_internal::AddTraceEvent( \ | |
636 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \ | |
637 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \ | |
638 } \ | |
639 } while (0) | |
640 | |
641 // Implementation detail: internal macro to create static category and add begin | |
642 // event if the category is enabled. Also adds the end event when the scope | |
643 // ends. | |
644 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \ | |
645 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
646 trace_event_internal::TraceEndOnScopeClose \ | |
647 INTERNAL_TRACE_EVENT_UID(profileScope); \ | |
648 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
649 trace_event_internal::AddTraceEvent( \ | |
650 TRACE_EVENT_PHASE_BEGIN, \ | |
651 INTERNAL_TRACE_EVENT_UID(catstatic), \ | |
652 name, trace_event_internal::kNoEventId, \ | |
653 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ | |
654 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | |
655 INTERNAL_TRACE_EVENT_UID(catstatic), name); \ | |
656 } | |
657 | |
658 // Implementation detail: internal macro to create static category and add begin | |
659 // event if the category is enabled. Also adds the end event when the scope | |
660 // ends. If the elapsed time is < threshold time, the begin/end pair is erased. | |
661 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold, \ | |
662 category, name, ...) \ | |
663 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
664 trace_event_internal::TraceEndOnScopeCloseThreshold \ | |
665 INTERNAL_TRACE_EVENT_UID(profileScope); \ | |
666 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
667 int INTERNAL_TRACE_EVENT_UID(begin_event_id) = \ | |
668 trace_event_internal::AddTraceEvent( \ | |
669 TRACE_EVENT_PHASE_BEGIN, \ | |
670 INTERNAL_TRACE_EVENT_UID(catstatic), \ | |
671 name, trace_event_internal::kNoEventId, \ | |
672 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ | |
673 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | |
674 INTERNAL_TRACE_EVENT_UID(catstatic), name, \ | |
675 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \ | |
676 } | |
677 | |
678 // Implementation detail: internal macro to create static category and add | |
679 // event if the category is enabled. | |
680 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ | |
681 ...) \ | |
682 do { \ | |
683 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
684 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
685 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \ | |
686 trace_event_internal::TraceID trace_event_trace_id( \ | |
687 id, &trace_event_flags); \ | |
688 trace_event_internal::AddTraceEvent( \ | |
689 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ | |
690 name, trace_event_trace_id.data(), trace_event_flags, \ | |
691 ##__VA_ARGS__); \ | |
692 } \ | |
693 } while (0) | |
694 | |
695 // Notes regarding the following definitions: | |
696 // New values can be added and propagated to third party libraries, but existing | |
697 // definitions must never be changed, because third party libraries may use old | |
698 // definitions. | |
699 | |
700 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. | |
701 #define TRACE_EVENT_PHASE_BEGIN ('B') | |
702 #define TRACE_EVENT_PHASE_END ('E') | |
703 #define TRACE_EVENT_PHASE_INSTANT ('I') | |
704 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S') | |
705 #define TRACE_EVENT_PHASE_ASYNC_STEP ('T') | |
706 #define TRACE_EVENT_PHASE_ASYNC_END ('F') | |
707 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s') | |
708 #define TRACE_EVENT_PHASE_FLOW_STEP ('t') | |
709 #define TRACE_EVENT_PHASE_FLOW_END ('f') | |
710 #define TRACE_EVENT_PHASE_METADATA ('M') | |
711 #define TRACE_EVENT_PHASE_COUNTER ('C') | |
712 | |
713 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. | |
714 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) | |
715 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0)) | |
716 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1)) | |
717 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2)) | |
718 | |
719 // Type values for identifying types in the TraceValue union. | |
720 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) | |
721 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) | |
722 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) | |
723 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) | |
724 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) | |
725 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) | |
726 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) | |
727 | |
728 namespace trace_event_internal { | |
729 | |
730 // Specify these values when the corresponding argument of AddTraceEvent is not | |
731 // used. | |
732 const int kZeroNumArgs = 0; | |
733 const int kNoThreshholdBeginId = -1; | |
734 const long long kNoThresholdValue = 0; | |
735 const unsigned long long kNoEventId = 0; | |
736 | |
737 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | |
738 // are mangled with the Process ID so that they are unlikely to collide when the | |
739 // same pointer is used on different processes. | |
740 class TraceID { | |
741 public: | |
742 class ForceMangle { | |
743 public: | |
744 explicit ForceMangle(unsigned long long id) : data_(id) {} | |
745 explicit ForceMangle(unsigned long id) : data_(id) {} | |
746 explicit ForceMangle(unsigned int id) : data_(id) {} | |
747 explicit ForceMangle(unsigned short id) : data_(id) {} | |
748 explicit ForceMangle(unsigned char id) : data_(id) {} | |
749 explicit ForceMangle(long long id) | |
750 : data_(static_cast<unsigned long long>(id)) {} | |
751 explicit ForceMangle(long id) | |
752 : data_(static_cast<unsigned long long>(id)) {} | |
753 explicit ForceMangle(int id) | |
754 : data_(static_cast<unsigned long long>(id)) {} | |
755 explicit ForceMangle(short id) | |
756 : data_(static_cast<unsigned long long>(id)) {} | |
757 explicit ForceMangle(signed char id) | |
758 : data_(static_cast<unsigned long long>(id)) {} | |
759 | |
760 unsigned long long data() const { return data_; } | |
761 | |
762 private: | |
763 unsigned long long data_; | |
764 }; | |
765 | |
766 explicit TraceID(const void* id, unsigned char* flags) | |
767 : data_(static_cast<unsigned long long>( | |
768 reinterpret_cast<unsigned long>(id))) { | |
769 *flags |= TRACE_EVENT_FLAG_MANGLE_ID; | |
770 } | |
771 explicit TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) { | |
772 *flags |= TRACE_EVENT_FLAG_MANGLE_ID; | |
773 } | |
774 explicit TraceID(unsigned long long id, unsigned char* flags) | |
775 : data_(id) { (void)flags; } | |
776 explicit TraceID(unsigned long id, unsigned char* flags) | |
777 : data_(id) { (void)flags; } | |
778 explicit TraceID(unsigned int id, unsigned char* flags) | |
779 : data_(id) { (void)flags; } | |
780 explicit TraceID(unsigned short id, unsigned char* flags) | |
781 : data_(id) { (void)flags; } | |
782 explicit TraceID(unsigned char id, unsigned char* flags) | |
783 : data_(id) { (void)flags; } | |
784 explicit TraceID(long long id, unsigned char* flags) | |
785 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
786 explicit TraceID(long id, unsigned char* flags) | |
787 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
788 explicit TraceID(int id, unsigned char* flags) | |
789 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
790 explicit TraceID(short id, unsigned char* flags) | |
791 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
792 explicit TraceID(signed char id, unsigned char* flags) | |
793 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
794 | |
795 unsigned long long data() const { return data_; } | |
796 | |
797 private: | |
798 unsigned long long data_; | |
799 }; | |
800 | |
801 // Simple union to store various types as unsigned long long. | |
802 union TraceValueUnion { | |
803 bool as_bool; | |
804 unsigned long long as_uint; | |
805 long long as_int; | |
806 double as_double; | |
807 const void* as_pointer; | |
808 const char* as_string; | |
809 }; | |
810 | |
811 // Simple container for const char* that should be copied instead of retained. | |
812 class TraceStringWithCopy { | |
813 public: | |
814 explicit TraceStringWithCopy(const char* str) : str_(str) {} | |
815 operator const char* () const { return str_; } | |
816 private: | |
817 const char* str_; | |
818 }; | |
819 | |
820 // Define SetTraceValue for each allowed type. It stores the type and | |
821 // value in the return arguments. This allows this API to avoid declaring any | |
822 // structures so that it is portable to third_party libraries. | |
823 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \ | |
824 union_member, \ | |
825 value_type_id) \ | |
826 static inline void SetTraceValue(actual_type arg, \ | |
827 unsigned char* type, \ | |
828 unsigned long long* value) { \ | |
829 TraceValueUnion type_value; \ | |
830 type_value.union_member = arg; \ | |
831 *type = value_type_id; \ | |
832 *value = type_value.as_uint; \ | |
833 } | |
834 // Simpler form for int types that can be safely casted. | |
835 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \ | |
836 value_type_id) \ | |
837 static inline void SetTraceValue(actual_type arg, \ | |
838 unsigned char* type, \ | |
839 unsigned long long* value) { \ | |
840 *type = value_type_id; \ | |
841 *value = static_cast<unsigned long long>(arg); \ | |
842 } | |
843 | |
844 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT) | |
845 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT) | |
846 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT) | |
847 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT) | |
848 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT) | |
849 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT) | |
850 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT) | |
851 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT) | |
852 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT) | |
853 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT) | |
854 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL) | |
855 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE) | |
856 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer, | |
857 TRACE_VALUE_TYPE_POINTER) | |
858 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string, | |
859 TRACE_VALUE_TYPE_STRING) | |
860 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string, | |
861 TRACE_VALUE_TYPE_COPY_STRING) | |
862 | |
863 #undef INTERNAL_DECLARE_SET_TRACE_VALUE | |
864 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT | |
865 | |
866 // std::string version of SetTraceValue so that trace arguments can be strings. | |
867 static inline void SetTraceValue(const std::string& arg, | |
868 unsigned char* type, | |
869 unsigned long long* value) { | |
870 TraceValueUnion type_value; | |
871 type_value.as_string = arg.c_str(); | |
872 *type = TRACE_VALUE_TYPE_COPY_STRING; | |
873 *value = type_value.as_uint; | |
874 } | |
875 | |
876 // These AddTraceEvent template functions are defined here instead of in the | |
877 // macro, because the arg_values could be temporary objects, such as | |
878 // std::string. In order to store pointers to the internal c_str and pass | |
879 // through to the tracing API, the arg_values must live throughout | |
880 // these procedures. | |
881 | |
882 static inline int AddTraceEvent(char phase, | |
883 const unsigned char* category_enabled, | |
884 const char* name, | |
885 unsigned long long id, | |
886 unsigned char flags) { | |
887 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
888 phase, category_enabled, name, id, | |
889 kZeroNumArgs, NULL, NULL, NULL, | |
890 kNoThreshholdBeginId, kNoThresholdValue, flags); | |
891 } | |
892 | |
893 template<class ARG1_TYPE> | |
894 static inline int AddTraceEvent(char phase, | |
895 const unsigned char* category_enabled, | |
896 const char* name, | |
897 unsigned long long id, | |
898 unsigned char flags, | |
899 const char* arg1_name, | |
900 const ARG1_TYPE& arg1_val) { | |
901 const int num_args = 1; | |
902 unsigned char arg_types[1]; | |
903 unsigned long long arg_values[1]; | |
904 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | |
905 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
906 phase, category_enabled, name, id, | |
907 num_args, &arg1_name, arg_types, arg_values, | |
908 kNoThreshholdBeginId, kNoThresholdValue, flags); | |
909 } | |
910 | |
911 template<class ARG1_TYPE, class ARG2_TYPE> | |
912 static inline int AddTraceEvent(char phase, | |
913 const unsigned char* category_enabled, | |
914 const char* name, | |
915 unsigned long long id, | |
916 unsigned char flags, | |
917 const char* arg1_name, | |
918 const ARG1_TYPE& arg1_val, | |
919 const char* arg2_name, | |
920 const ARG2_TYPE& arg2_val) { | |
921 const int num_args = 2; | |
922 const char* arg_names[2] = { arg1_name, arg2_name }; | |
923 unsigned char arg_types[2]; | |
924 unsigned long long arg_values[2]; | |
925 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | |
926 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); | |
927 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
928 phase, category_enabled, name, id, | |
929 num_args, arg_names, arg_types, arg_values, | |
930 kNoThreshholdBeginId, kNoThresholdValue, flags); | |
931 } | |
932 | |
933 // Used by TRACE_EVENTx macro. Do not use directly. | |
934 class BASE_EXPORT TraceEndOnScopeClose { | |
935 public: | |
936 // Note: members of data_ intentionally left uninitialized. See Initialize. | |
937 TraceEndOnScopeClose() : p_data_(NULL) {} | |
938 ~TraceEndOnScopeClose() { | |
939 if (p_data_) | |
940 AddEventIfEnabled(); | |
941 } | |
942 | |
943 void Initialize(const unsigned char* category_enabled, | |
944 const char* name); | |
945 | |
946 private: | |
947 // Add the end event if the category is still enabled. | |
948 void AddEventIfEnabled(); | |
949 | |
950 // This Data struct workaround is to avoid initializing all the members | |
951 // in Data during construction of this object, since this object is always | |
952 // constructed, even when tracing is disabled. If the members of Data were | |
953 // members of this class instead, compiler warnings occur about potential | |
954 // uninitialized accesses. | |
955 struct Data { | |
956 const unsigned char* category_enabled; | |
957 const char* name; | |
958 }; | |
959 Data* p_data_; | |
960 Data data_; | |
961 }; | |
962 | |
963 // Used by TRACE_EVENTx macro. Do not use directly. | |
964 class BASE_EXPORT TraceEndOnScopeCloseThreshold { | |
965 public: | |
966 // Note: members of data_ intentionally left uninitialized. See Initialize. | |
967 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {} | |
968 ~TraceEndOnScopeCloseThreshold() { | |
969 if (p_data_) | |
970 AddEventIfEnabled(); | |
971 } | |
972 | |
973 // Called by macros only when tracing is enabled at the point when the begin | |
974 // event is added. | |
975 void Initialize(const unsigned char* category_enabled, | |
976 const char* name, | |
977 int threshold_begin_id, | |
978 long long threshold); | |
979 | |
980 private: | |
981 // Add the end event if the category is still enabled. | |
982 void AddEventIfEnabled(); | |
983 | |
984 // This Data struct workaround is to avoid initializing all the members | |
985 // in Data during construction of this object, since this object is always | |
986 // constructed, even when tracing is disabled. If the members of Data were | |
987 // members of this class instead, compiler warnings occur about potential | |
988 // uninitialized accesses. | |
989 struct Data { | |
990 long long threshold; | |
991 const unsigned char* category_enabled; | |
992 const char* name; | |
993 int threshold_begin_id; | |
994 }; | |
995 Data* p_data_; | |
996 Data data_; | |
997 }; | |
998 | |
999 } // namespace trace_event_internal | |
1000 | |
1001 #endif // BASE_DEBUG_TRACE_EVENT_H_ | |
OLD | NEW |