OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 // Trace events are for tracking application performance and resource usage. | |
27 // Macros are provided to track: | |
28 // Begin and end of function calls | |
29 // Counters | |
30 // | |
31 // Events are issued against categories. Whereas LOG's | |
32 // categories are statically defined, TRACE categories are created | |
33 // implicitly with a string. For example: | |
34 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") | |
35 // | |
36 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope: | |
37 // TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly") | |
38 // doSomethingCostly() | |
39 // TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly") | |
40 // Note: our tools can't always determine the correct BEGIN/END pairs unless | |
41 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you nee
d them | |
42 // to be in separate scopes. | |
43 // | |
44 // A common use case is to trace entire function scopes. This | |
45 // issues a trace BEGIN and END automatically: | |
46 // void doSomethingCostly() { | |
47 // TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly"); | |
48 // ... | |
49 // } | |
50 // | |
51 // Additional parameters can be associated with an event: | |
52 // void doSomethingCostly2(int howMuch) { | |
53 // TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly", | |
54 // "howMuch", howMuch); | |
55 // ... | |
56 // } | |
57 // | |
58 // The trace system will automatically add to this information the | |
59 // current process id, thread id, and a timestamp in microseconds. | |
60 // | |
61 // To trace an asynchronous procedure such as an IPC send/receive, use ASYNC_BEG
IN and | |
62 // ASYNC_END: | |
63 // [single threaded sender code] | |
64 // static int send_count = 0; | |
65 // ++send_count; | |
66 // TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count); | |
67 // Send(new MyMessage(send_count)); | |
68 // [receive code] | |
69 // void OnMyMessage(send_count) { | |
70 // TRACE_EVENT_ASYNC_END0("ipc", "message", send_count); | |
71 // } | |
72 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs. | |
73 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process. Poin
ters can | |
74 // be used for the ID parameter, and they will be mangled internally so that | |
75 // the same pointer on two different processes will not match. For example: | |
76 // class MyTracedClass { | |
77 // public: | |
78 // MyTracedClass() { | |
79 // TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this); | |
80 // } | |
81 // ~MyTracedClass() { | |
82 // TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this); | |
83 // } | |
84 // } | |
85 // | |
86 // Trace event also supports counters, which is a way to track a quantity | |
87 // as it varies over time. Counters are created with the following macro: | |
88 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue); | |
89 // | |
90 // Counters are process-specific. The macro itself can be issued from any | |
91 // thread, however. | |
92 // | |
93 // Sometimes, you want to track two counters at once. You can do this with two | |
94 // counter macros: | |
95 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]); | |
96 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]); | |
97 // Or you can do it with a combined macro: | |
98 // TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter", | |
99 // "bytesPinned", g_myCounterValue[0], | |
100 // "bytesAllocated", g_myCounterValue[1]); | |
101 // This indicates to the tracing UI that these counters should be displayed | |
102 // in a single graph, as a summed area chart. | |
103 // | |
104 // Since counters are in a global namespace, you may want to disembiguate with a | |
105 // unique ID, by using the TRACE_COUNTER_ID* variations. | |
106 // | |
107 // By default, trace collection is compiled in, but turned off at runtime. | |
108 // Collecting trace data is the responsibility of the embedding | |
109 // application. In Chrome's case, navigating to about:tracing will turn on | |
110 // tracing and display data collected across all active processes. | |
111 // | |
112 // | |
113 // Memory scoping note: | |
114 // Tracing copies the pointers, not the string content, of the strings passed | |
115 // in for category, name, and arg_names. Thus, the following code will | |
116 // cause problems: | |
117 // char* str = strdup("impprtantName"); | |
118 // TRACE_EVENT_INSTANT0("SUBSYSTEM", str); // BAD! | |
119 // free(str); // Trace system now has dangling pointer | |
120 // | |
121 // To avoid this issue with the |name| and |arg_name| parameters, use the | |
122 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead. | |
123 // Notes: The category must always be in a long-lived char* (i.e. static const). | |
124 // The |arg_values|, when used, are always deep copied with the _COPY | |
125 // macros. | |
126 // | |
127 // When are string argument values copied: | |
128 // const char* arg_values are only referenced by default: | |
129 // TRACE_EVENT1("category", "name", | |
130 // "arg1", "literal string is only referenced"); | |
131 // Use TRACE_STR_COPY to force copying of a const char*: | |
132 // TRACE_EVENT1("category", "name", | |
133 // "arg1", TRACE_STR_COPY("string will be copied")); | |
134 // std::string arg_values are always copied: | |
135 // TRACE_EVENT1("category", "name", | |
136 // "arg1", std::string("string will be copied")); | |
137 // | |
138 // | |
139 // Thread Safety: | |
140 // A thread safe singleton and mutex are used for thread safety. Category | |
141 // enabled flags are used to limit the performance impact when the system | |
142 // is not enabled. | |
143 // | |
144 // TRACE_EVENT macros first cache a pointer to a category. The categories are | |
145 // statically allocated and safe at all times, even after exit. Fetching a | |
146 // category is protected by the TraceLog::lock_. Multiple threads initializing | |
147 // the static variable is safe, as they will be serialized by the lock and | |
148 // multiple calls will return the same pointer to the category. | |
149 // | |
150 // Then the category_enabled flag is checked. This is a unsigned char, and | |
151 // not intended to be multithread safe. It optimizes access to addTraceEvent | |
152 // which is threadsafe internally via TraceLog::lock_. The enabled flag may | |
153 // cause some threads to incorrectly call or skip calling addTraceEvent near | |
154 // the time of the system being enabled or disabled. This is acceptable as | |
155 // we tolerate some data loss while the system is being enabled/disabled and | |
156 // because addTraceEvent is threadsafe internally and checks the enabled state | |
157 // again under lock. | |
158 // | |
159 // Without the use of these static category pointers and enabled flags all | |
160 // trace points would carry a significant performance cost of aquiring a lock | |
161 // and resolving the category. | |
162 | |
163 #ifndef TraceEvent_h | |
164 #define TraceEvent_h | |
165 | |
166 #include "platform/EventTracer.h" | |
167 | |
168 #include "wtf/DynamicAnnotations.h" | |
169 #include "wtf/text/CString.h" | |
170 | |
171 // By default, const char* argument values are assumed to have long-lived scope | |
172 // and will not be copied. Use this macro to force a const char* to be copied. | |
173 #define TRACE_STR_COPY(str) \ | |
174 WebCore::TraceEvent::TraceStringWithCopy(str) | |
175 | |
176 // By default, uint64 ID argument values are not mangled with the Process ID in | |
177 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling. | |
178 #define TRACE_ID_MANGLE(id) \ | |
179 TraceID::ForceMangle(id) | |
180 | |
181 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC | |
182 // macros. Use this macro to prevent Process ID mangling. | |
183 #define TRACE_ID_DONT_MANGLE(id) \ | |
184 TraceID::DontMangle(id) | |
185 | |
186 // Records a pair of begin and end events called "name" for the current | |
187 // scope, with 0, 1 or 2 associated arguments. If the category is not | |
188 // enabled, then this does nothing. | |
189 // - category and name strings must have application lifetime (statics or | |
190 // literals). They may not include " chars. | |
191 #define TRACE_EVENT0(category, name) \ | |
192 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) | |
193 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | |
194 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) | |
195 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | |
196 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \ | |
197 arg2_name, arg2_val) | |
198 | |
199 // Records a single event called "name" immediately, with 0, 1 or 2 | |
200 // associated arguments. If the category is not enabled, then this | |
201 // does nothing. | |
202 // - category and name strings must have application lifetime (statics or | |
203 // literals). They may not include " chars. | |
204 #define TRACE_EVENT_INSTANT0(category, name) \ | |
205 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
206 category, name, TRACE_EVENT_FLAG_NONE) | |
207 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | |
208 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
209 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
210 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
211 arg2_name, arg2_val) \ | |
212 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
213 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
214 arg2_name, arg2_val) | |
215 #define TRACE_EVENT_COPY_INSTANT0(category, name) \ | |
216 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
217 category, name, TRACE_EVENT_FLAG_COPY) | |
218 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \ | |
219 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
220 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
221 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \ | |
222 arg2_name, arg2_val) \ | |
223 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
224 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
225 arg2_name, arg2_val) | |
226 | |
227 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2 | |
228 // associated arguments. If the category is not enabled, then this | |
229 // does nothing. | |
230 // - category and name strings must have application lifetime (statics or | |
231 // literals). They may not include " chars. | |
232 #define TRACE_EVENT_BEGIN0(category, name) \ | |
233 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
234 category, name, TRACE_EVENT_FLAG_NONE) | |
235 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \ | |
236 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
237 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
238 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \ | |
239 arg2_name, arg2_val) \ | |
240 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
241 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
242 arg2_name, arg2_val) | |
243 #define TRACE_EVENT_COPY_BEGIN0(category, name) \ | |
244 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
245 category, name, TRACE_EVENT_FLAG_COPY) | |
246 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \ | |
247 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
248 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
249 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \ | |
250 arg2_name, arg2_val) \ | |
251 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
252 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
253 arg2_name, arg2_val) | |
254 | |
255 // Records a single END event for "name" immediately. If the category | |
256 // is not enabled, then this does nothing. | |
257 // - category and name strings must have application lifetime (statics or | |
258 // literals). They may not include " chars. | |
259 #define TRACE_EVENT_END0(category, name) \ | |
260 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
261 category, name, TRACE_EVENT_FLAG_NONE) | |
262 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \ | |
263 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
264 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
265 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \ | |
266 arg2_name, arg2_val) \ | |
267 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
268 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
269 arg2_name, arg2_val) | |
270 #define TRACE_EVENT_COPY_END0(category, name) \ | |
271 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
272 category, name, TRACE_EVENT_FLAG_COPY) | |
273 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ | |
274 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
275 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
276 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ | |
277 arg2_name, arg2_val) \ | |
278 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
279 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
280 arg2_name, arg2_val) | |
281 | |
282 // Records the value of a counter called "name" immediately. Value | |
283 // must be representable as a 32 bit integer. | |
284 // - category and name strings must have application lifetime (statics or | |
285 // literals). They may not include " chars. | |
286 #define TRACE_COUNTER1(category, name, value) \ | |
287 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
288 category, name, TRACE_EVENT_FLAG_NONE, \ | |
289 "value", static_cast<int>(value)) | |
290 #define TRACE_COPY_COUNTER1(category, name, value) \ | |
291 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
292 category, name, TRACE_EVENT_FLAG_COPY, \ | |
293 "value", static_cast<int>(value)) | |
294 | |
295 // Records the values of a multi-parted counter called "name" immediately. | |
296 // The UI will treat value1 and value2 as parts of a whole, displaying their | |
297 // values as a stacked-bar chart. | |
298 // - category and name strings must have application lifetime (statics or | |
299 // literals). They may not include " chars. | |
300 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ | |
301 value2_name, value2_val) \ | |
302 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
303 category, name, TRACE_EVENT_FLAG_NONE, \ | |
304 value1_name, static_cast<int>(value1_val), \ | |
305 value2_name, static_cast<int>(value2_val)) | |
306 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ | |
307 value2_name, value2_val) \ | |
308 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
309 category, name, TRACE_EVENT_FLAG_COPY, \ | |
310 value1_name, static_cast<int>(value1_val), \ | |
311 value2_name, static_cast<int>(value2_val)) | |
312 | |
313 // Records the value of a counter called "name" immediately. Value | |
314 // must be representable as a 32 bit integer. | |
315 // - category and name strings must have application lifetime (statics or | |
316 // literals). They may not include " chars. | |
317 // - |id| is used to disambiguate counters with the same name. It must either | |
318 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | |
319 // will be xored with a hash of the process ID so that the same pointer on | |
320 // two different processes will not collide. | |
321 #define TRACE_COUNTER_ID1(category, name, id, value) \ | |
322 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
323 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
324 "value", static_cast<int>(value)) | |
325 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \ | |
326 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
327 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
328 "value", static_cast<int>(value)) | |
329 | |
330 // Records the values of a multi-parted counter called "name" immediately. | |
331 // The UI will treat value1 and value2 as parts of a whole, displaying their | |
332 // values as a stacked-bar chart. | |
333 // - category and name strings must have application lifetime (statics or | |
334 // literals). They may not include " chars. | |
335 // - |id| is used to disambiguate counters with the same name. It must either | |
336 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | |
337 // will be xored with a hash of the process ID so that the same pointer on | |
338 // two different processes will not collide. | |
339 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \ | |
340 value2_name, value2_val) \ | |
341 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
342 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
343 value1_name, static_cast<int>(value1_val), \ | |
344 value2_name, static_cast<int>(value2_val)) | |
345 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \ | |
346 value2_name, value2_val) \ | |
347 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
348 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
349 value1_name, static_cast<int>(value1_val), \ | |
350 value2_name, static_cast<int>(value2_val)) | |
351 | |
352 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2 | |
353 // associated arguments. If the category is not enabled, then this | |
354 // does nothing. | |
355 // - category and name strings must have application lifetime (statics or | |
356 // literals). They may not include " chars. | |
357 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC | |
358 // events are considered to match if their category, name and id values all | |
359 // match. |id| must either be a pointer or an integer value up to 64 bits. If | |
360 // it's a pointer, the bits will be xored with a hash of the process ID so | |
361 // that the same pointer on two different processes will not collide. | |
362 // | |
363 // An asynchronous operation can consist of multiple phases. The first phase is | |
364 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the | |
365 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will | |
366 // annotate the block following the call. The ASYNC_STEP_PAST macro will | |
367 // annotate the block prior to the call. Note that any particular event must use | |
368 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the | |
369 // operation completes, call ASYNC_END. | |
370 // | |
371 // An ASYNC trace typically occurs on a single thread (if not, they will only be | |
372 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that | |
373 // operation must use the same |name| and |id|. Each step can have its own | |
374 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \ | |
375 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
376 category, name, id, TRACE_EVENT_FLAG_NONE) | |
377 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
378 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
379 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
380 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
381 arg2_name, arg2_val) \ | |
382 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
383 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
384 arg1_name, arg1_val, arg2_name, arg2_val) | |
385 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \ | |
386 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
387 category, name, id, TRACE_EVENT_FLAG_COPY) | |
388 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
389 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
390 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
391 arg1_name, arg1_val) | |
392 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
393 arg2_name, arg2_val) \ | |
394 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
395 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
396 arg1_name, arg1_val, arg2_name, arg2_val) | |
397 | |
398 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the | |
399 // category is not enabled, then this does nothing. The |name| and |id| must | |
400 // match the ASYNC_BEGIN event above. The |step| param identifies this step | |
401 // within the async event. This should be called at the beginning of the next | |
402 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any | |
403 // ASYNC_STEP_PAST events. | |
404 #define TRACE_EVENT_ASYNC_STEP_INTO0(category, name, id, step) \ | |
405 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \ | |
406 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step) | |
407 #define TRACE_EVENT_ASYNC_STEP_INTO1(category, name, id, step, \ | |
408 arg1_name, arg1_val) \ | |
409 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \ | |
410 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ | |
411 arg1_name, arg1_val) | |
412 | |
413 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the | |
414 // category is not enabled, then this does nothing. The |name| and |id| must | |
415 // match the ASYNC_BEGIN event above. The |step| param identifies this step | |
416 // within the async event. This should be called at the beginning of the next | |
417 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any | |
418 // ASYNC_STEP_INTO events. | |
419 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step) \ | |
420 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \ | |
421 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step) | |
422 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, arg1_name,
arg1_val) \ | |
423 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \ | |
424 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ | |
425 arg1_name, arg1_val) | |
426 | |
427 // Records a single ASYNC_END event for "name" immediately. If the category | |
428 // is not enabled, then this does nothing. | |
429 #define TRACE_EVENT_ASYNC_END0(category, name, id) \ | |
430 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
431 category, name, id, TRACE_EVENT_FLAG_NONE) | |
432 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ | |
433 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
434 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
435 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \ | |
436 arg2_name, arg2_val) \ | |
437 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
438 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
439 arg1_name, arg1_val, arg2_name, arg2_val) | |
440 #define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \ | |
441 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
442 category, name, id, TRACE_EVENT_FLAG_COPY) | |
443 #define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ | |
444 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
445 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
446 arg1_name, arg1_val) | |
447 #define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \ | |
448 arg2_name, arg2_val) \ | |
449 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
450 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
451 arg1_name, arg1_val, arg2_name, arg2_val) | |
452 | |
453 // Creates a scope of a sampling state with the given category and name (both mu
st | |
454 // be constant strings). These states are intended for a sampling profiler. | |
455 // Implementation note: we store category and name together because we don't | |
456 // want the inconsistency/expense of storing two pointers. | |
457 // |thread_bucket| is [0..2] and is used to statically isolate samples in one | |
458 // thread from others. | |
459 // | |
460 // { // The sampling state is set within this scope. | |
461 // TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name"); | |
462 // ...; | |
463 // } | |
464 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(bucket_number, category, na
me) \ | |
465 TraceEvent::SamplingStateScope<bucket_number> traceEventSamplingScope(catego
ry "\0" name); | |
466 | |
467 // Returns a current sampling state of the given bucket. | |
468 // The format of the returned string is "category\0name". | |
469 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \ | |
470 TraceEvent::SamplingStateScope<bucket_number>::current() | |
471 | |
472 // Sets a current sampling state of the given bucket. | |
473 // |category| and |name| have to be constant strings. | |
474 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(bucket_number, category, name)
\ | |
475 TraceEvent::SamplingStateScope<bucket_number>::set(category "\0" name) | |
476 | |
477 // Sets a current sampling state of the given bucket. | |
478 // |categoryAndName| doesn't need to be a constant string. | |
479 // The format of the string is "category\0name". | |
480 #define TRACE_EVENT_SET_NONCONST_SAMPLING_STATE_FOR_BUCKET(bucket_number, catego
ryAndName) \ | |
481 TraceEvent::SamplingStateScope<bucket_number>::set(categoryAndName) | |
482 | |
483 // Syntactic sugars for the sampling tracing in the main thread. | |
484 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \ | |
485 TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name) | |
486 #define TRACE_EVENT_GET_SAMPLING_STATE() \ | |
487 TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0) | |
488 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \ | |
489 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name) | |
490 #define TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(categoryAndName) \ | |
491 TRACE_EVENT_SET_NONCONST_SAMPLING_STATE_FOR_BUCKET(0, categoryAndName) | |
492 | |
493 // Macros to track the life time and value of arbitrary client objects. | |
494 // See also TraceTrackableObject. | |
495 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(categoryGroup, name, id) \ | |
496 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \ | |
497 categoryGroup, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE) | |
498 | |
499 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(categoryGroup, name, id) \ | |
500 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \ | |
501 categoryGroup, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE) | |
502 | |
503 // Macro to efficiently determine if a given category group is enabled. | |
504 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(categoryGroup, ret) \ | |
505 do { \ | |
506 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(categoryGroup); \ | |
507 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) {
\ | |
508 *ret = true; \ | |
509 } else { \ | |
510 *ret = false; \ | |
511 } \ | |
512 } while (0) | |
513 | |
514 // This will mark the trace event as disabled by default. The user will need | |
515 // to explicitly enable the event. | |
516 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name | |
517 | |
518 //////////////////////////////////////////////////////////////////////////////// | |
519 // Implementation specific tracing API definitions. | |
520 | |
521 // Get a pointer to the enabled state of the given trace category. Only | |
522 // long-lived literal strings should be given as the category name. The returned | |
523 // pointer can be held permanently in a local static for example. If the | |
524 // unsigned char is non-zero, tracing is enabled. If tracing is enabled, | |
525 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled | |
526 // between the load of the tracing state and the call to | |
527 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out | |
528 // for best performance when tracing is disabled. | |
529 // const unsigned char* | |
530 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) | |
531 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ | |
532 WebCore::EventTracer::getTraceCategoryEnabledFlag | |
533 | |
534 // Add a trace event to the platform tracing system. | |
535 // WebCore::TraceEvent::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT( | |
536 // char phase, | |
537 // const unsigned char* category_enabled, | |
538 // const char* name, | |
539 // unsigned long long id, | |
540 // int num_args, | |
541 // const char** arg_names, | |
542 // const unsigned char* arg_types, | |
543 // const unsigned long long* arg_values, | |
544 // unsigned char flags) | |
545 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ | |
546 WebCore::EventTracer::addTraceEvent | |
547 | |
548 // Set the duration field of a COMPLETE trace event. | |
549 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION( | |
550 // WebCore::TraceEvent::TraceEventHandle handle) | |
551 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \ | |
552 WebCore::EventTracer::updateTraceEventDuration | |
553 | |
554 //////////////////////////////////////////////////////////////////////////////// | |
555 | |
556 // Implementation detail: trace event macros create temporary variables | |
557 // to keep instrumentation overhead low. These macros give each temporary | |
558 // variable a unique name based on the line number to prevent name collissions. | |
559 #define INTERNAL_TRACE_EVENT_UID3(a, b) \ | |
560 trace_event_unique_##a##b | |
561 #define INTERNAL_TRACE_EVENT_UID2(a, b) \ | |
562 INTERNAL_TRACE_EVENT_UID3(a, b) | |
563 #define INTERNALTRACEEVENTUID(name_prefix) \ | |
564 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) | |
565 | |
566 // Implementation detail: internal macro to create static category. | |
567 // - WTF_ANNOTATE_BENIGN_RACE, see Thread Safety above. | |
568 | |
569 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ | |
570 static const unsigned char* INTERNALTRACEEVENTUID(categoryGroupEnabled) = 0;
\ | |
571 WTF_ANNOTATE_BENIGN_RACE(&INTERNALTRACEEVENTUID(categoryGroupEnabled), \ | |
572 "trace_event category"); \ | |
573 if (!INTERNALTRACEEVENTUID(categoryGroupEnabled)) { \ | |
574 INTERNALTRACEEVENTUID(categoryGroupEnabled) = \ | |
575 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \ | |
576 } | |
577 | |
578 // Implementation detail: internal macro to create static category and add | |
579 // event if the category is enabled. | |
580 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ | |
581 do { \ | |
582 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
583 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) {
\ | |
584 WebCore::TraceEvent::addTraceEvent( \ | |
585 phase, INTERNALTRACEEVENTUID(categoryGroupEnabled), name, \ | |
586 WebCore::TraceEvent::noEventId, flags, ##__VA_ARGS__); \ | |
587 } \ | |
588 } while (0) | |
589 | |
590 // Implementation detail: internal macro to create static category and add begin | |
591 // event if the category is enabled. Also adds the end event when the scope | |
592 // ends. | |
593 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \ | |
594 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
595 WebCore::TraceEvent::ScopedTracer INTERNALTRACEEVENTUID(scopedTracer); \ | |
596 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ | |
597 WebCore::TraceEvent::TraceEventHandle h = \ | |
598 WebCore::TraceEvent::addTraceEvent( \ | |
599 TRACE_EVENT_PHASE_COMPLETE, \ | |
600 INTERNALTRACEEVENTUID(categoryGroupEnabled), \ | |
601 name, WebCore::TraceEvent::noEventId, \ | |
602 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ | |
603 INTERNALTRACEEVENTUID(scopedTracer).initialize( \ | |
604 INTERNALTRACEEVENTUID(categoryGroupEnabled), name, h); \ | |
605 } | |
606 | |
607 // Implementation detail: internal macro to create static category and add | |
608 // event if the category is enabled. | |
609 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ | |
610 ...) \ | |
611 do { \ | |
612 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
613 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) {
\ | |
614 unsigned char traceEventFlags = flags | TRACE_EVENT_FLAG_HAS_ID; \ | |
615 WebCore::TraceEvent::TraceID traceEventTraceID( \ | |
616 id, &traceEventFlags); \ | |
617 WebCore::TraceEvent::addTraceEvent( \ | |
618 phase, INTERNALTRACEEVENTUID(categoryGroupEnabled), \ | |
619 name, traceEventTraceID.data(), traceEventFlags, \ | |
620 ##__VA_ARGS__); \ | |
621 } \ | |
622 } while (0) | |
623 | |
624 // Notes regarding the following definitions: | |
625 // New values can be added and propagated to third party libraries, but existing | |
626 // definitions must never be changed, because third party libraries may use old | |
627 // definitions. | |
628 | |
629 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. | |
630 #define TRACE_EVENT_PHASE_BEGIN ('B') | |
631 #define TRACE_EVENT_PHASE_END ('E') | |
632 #define TRACE_EVENT_PHASE_COMPLETE ('X') | |
633 // FIXME: unify instant events handling between blink and platform. | |
634 #define TRACE_EVENT_PHASE_INSTANT ('I') | |
635 #define TRACE_EVENT_PHASE_INSTANT_WITH_SCOPE ('i') | |
636 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S') | |
637 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T') | |
638 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p') | |
639 #define TRACE_EVENT_PHASE_ASYNC_END ('F') | |
640 #define TRACE_EVENT_PHASE_METADATA ('M') | |
641 #define TRACE_EVENT_PHASE_COUNTER ('C') | |
642 #define TRACE_EVENT_PHASE_SAMPLE ('P') | |
643 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N') | |
644 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D') | |
645 | |
646 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. | |
647 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) | |
648 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0)) | |
649 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1)) | |
650 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2)) | |
651 | |
652 // Type values for identifying types in the TraceValue union. | |
653 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) | |
654 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) | |
655 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) | |
656 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) | |
657 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) | |
658 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) | |
659 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) | |
660 | |
661 // These values must be in sync with base::debug::TraceLog::CategoryGroupEnabled
Flags. | |
662 #define ENABLED_FOR_RECORDING (1 << 0) | |
663 #define ENABLED_FOR_EVENT_CALLBACK (1 << 2) | |
664 | |
665 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \ | |
666 (*INTERNALTRACEEVENTUID(categoryGroupEnabled) & (ENABLED_FOR_RECORDING | ENA
BLED_FOR_EVENT_CALLBACK)) | |
667 | |
668 namespace WebCore { | |
669 | |
670 namespace TraceEvent { | |
671 | |
672 // Specify these values when the corresponding argument of addTraceEvent is not | |
673 // used. | |
674 const int zeroNumArgs = 0; | |
675 const unsigned long long noEventId = 0; | |
676 | |
677 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | |
678 // are mangled with the Process ID so that they are unlikely to collide when the | |
679 // same pointer is used on different processes. | |
680 class TraceID { | |
681 public: | |
682 template<bool dummyMangle> class MangleBehavior { | |
683 public: | |
684 template<typename T> explicit MangleBehavior(T id) : m_data(reinterpret_
cast<unsigned long long>(id)) { } | |
685 unsigned long long data() const { return m_data; } | |
686 private: | |
687 unsigned long long m_data; | |
688 }; | |
689 typedef MangleBehavior<false> DontMangle; | |
690 typedef MangleBehavior<true> ForceMangle; | |
691 | |
692 TraceID(const void* id, unsigned char* flags) : | |
693 m_data(static_cast<unsigned long long>(reinterpret_cast<unsigned long>(i
d))) | |
694 { | |
695 *flags |= TRACE_EVENT_FLAG_MANGLE_ID; | |
696 } | |
697 TraceID(ForceMangle id, unsigned char* flags) : m_data(id.data()) | |
698 { | |
699 *flags |= TRACE_EVENT_FLAG_MANGLE_ID; | |
700 } | |
701 TraceID(DontMangle id, unsigned char*) : m_data(id.data()) { } | |
702 TraceID(unsigned long long id, unsigned char*) : m_data(id) { } | |
703 TraceID(unsigned long id, unsigned char*) : m_data(id) { } | |
704 TraceID(unsigned id, unsigned char*) : m_data(id) { } | |
705 TraceID(unsigned short id, unsigned char*) : m_data(id) { } | |
706 TraceID(unsigned char id, unsigned char*) : m_data(id) { } | |
707 TraceID(long long id, unsigned char*) : | |
708 m_data(static_cast<unsigned long long>(id)) { } | |
709 TraceID(long id, unsigned char*) : | |
710 m_data(static_cast<unsigned long long>(id)) { } | |
711 TraceID(int id, unsigned char*) : | |
712 m_data(static_cast<unsigned long long>(id)) { } | |
713 TraceID(short id, unsigned char*) : | |
714 m_data(static_cast<unsigned long long>(id)) { } | |
715 TraceID(signed char id, unsigned char*) : | |
716 m_data(static_cast<unsigned long long>(id)) { } | |
717 | |
718 unsigned long long data() const { return m_data; } | |
719 | |
720 private: | |
721 unsigned long long m_data; | |
722 }; | |
723 | |
724 // Simple union to store various types as unsigned long long. | |
725 union TraceValueUnion { | |
726 bool m_bool; | |
727 unsigned long long m_uint; | |
728 long long m_int; | |
729 double m_double; | |
730 const void* m_pointer; | |
731 const char* m_string; | |
732 }; | |
733 | |
734 // Simple container for const char* that should be copied instead of retained. | |
735 class TraceStringWithCopy { | |
736 public: | |
737 explicit TraceStringWithCopy(const char* str) : m_str(str) { } | |
738 operator const char* () const { return m_str; } | |
739 private: | |
740 const char* m_str; | |
741 }; | |
742 | |
743 // Define setTraceValue for each allowed type. It stores the type and | |
744 // value in the return arguments. This allows this API to avoid declaring any | |
745 // structures so that it is portable to third_party libraries. | |
746 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \ | |
747 union_member, \ | |
748 value_type_id) \ | |
749 static inline void setTraceValue(actual_type arg, \ | |
750 unsigned char* type, \ | |
751 unsigned long long* value) { \ | |
752 TraceValueUnion typeValue; \ | |
753 typeValue.union_member = arg; \ | |
754 *type = value_type_id; \ | |
755 *value = typeValue.m_uint; \ | |
756 } | |
757 // Simpler form for int types that can be safely casted. | |
758 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \ | |
759 value_type_id) \ | |
760 static inline void setTraceValue(actual_type arg, \ | |
761 unsigned char* type, \ | |
762 unsigned long long* value) { \ | |
763 *type = value_type_id; \ | |
764 *value = static_cast<unsigned long long>(arg); \ | |
765 } | |
766 | |
767 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT) | |
768 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT) | |
769 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT) | |
770 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT) | |
771 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT) | |
772 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT) | |
773 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT) | |
774 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT) | |
775 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, m_bool, TRACE_VALUE_TYPE_BOOL) | |
776 INTERNAL_DECLARE_SET_TRACE_VALUE(double, m_double, TRACE_VALUE_TYPE_DOUBLE) | |
777 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, m_pointer, | |
778 TRACE_VALUE_TYPE_POINTER) | |
779 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, m_string, | |
780 TRACE_VALUE_TYPE_STRING) | |
781 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, m_string, | |
782 TRACE_VALUE_TYPE_COPY_STRING) | |
783 | |
784 #undef INTERNAL_DECLARE_SET_TRACE_VALUE | |
785 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT | |
786 | |
787 // WTF::String version of setTraceValue so that trace arguments can be strings. | |
788 static inline void setTraceValue(const WTF::CString& arg, unsigned char* type, u
nsigned long long* value) | |
789 { | |
790 TraceValueUnion typeValue; | |
791 typeValue.m_string = arg.data(); | |
792 *type = TRACE_VALUE_TYPE_COPY_STRING; | |
793 *value = typeValue.m_uint; | |
794 } | |
795 | |
796 // These addTraceEvent template functions are defined here instead of in the | |
797 // macro, because the arg values could be temporary string objects. In order to | |
798 // store pointers to the internal c_str and pass through to the tracing API, the | |
799 // arg values must live throughout these procedures. | |
800 | |
801 static inline TraceEventHandle addTraceEvent( | |
802 char phase, | |
803 const unsigned char* categoryEnabled, | |
804 const char* name, | |
805 unsigned long long id, | |
806 unsigned char flags) | |
807 { | |
808 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
809 phase, categoryEnabled, name, id, | |
810 zeroNumArgs, 0, 0, 0, | |
811 flags); | |
812 } | |
813 | |
814 template<class ARG1_TYPE> | |
815 static inline TraceEventHandle addTraceEvent( | |
816 char phase, | |
817 const unsigned char* categoryEnabled, | |
818 const char* name, | |
819 unsigned long long id, | |
820 unsigned char flags, | |
821 const char* arg1Name, | |
822 const ARG1_TYPE& arg1Val) | |
823 { | |
824 const int numArgs = 1; | |
825 unsigned char argTypes[1]; | |
826 unsigned long long argValues[1]; | |
827 setTraceValue(arg1Val, &argTypes[0], &argValues[0]); | |
828 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
829 phase, categoryEnabled, name, id, | |
830 numArgs, &arg1Name, argTypes, argValues, | |
831 flags); | |
832 } | |
833 | |
834 template<class ARG1_TYPE, class ARG2_TYPE> | |
835 static inline TraceEventHandle addTraceEvent( | |
836 char phase, | |
837 const unsigned char* categoryEnabled, | |
838 const char* name, | |
839 unsigned long long id, | |
840 unsigned char flags, | |
841 const char* arg1Name, | |
842 const ARG1_TYPE& arg1Val, | |
843 const char* arg2Name, | |
844 const ARG2_TYPE& arg2Val) | |
845 { | |
846 const int numArgs = 2; | |
847 const char* argNames[2] = { arg1Name, arg2Name }; | |
848 unsigned char argTypes[2]; | |
849 unsigned long long argValues[2]; | |
850 setTraceValue(arg1Val, &argTypes[0], &argValues[0]); | |
851 setTraceValue(arg2Val, &argTypes[1], &argValues[1]); | |
852 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
853 phase, categoryEnabled, name, id, | |
854 numArgs, argNames, argTypes, argValues, | |
855 flags); | |
856 } | |
857 | |
858 // Used by TRACE_EVENTx macro. Do not use directly. | |
859 class ScopedTracer { | |
860 public: | |
861 // Note: members of m_data intentionally left uninitialized. See initialize. | |
862 ScopedTracer() : m_pdata(0) { } | |
863 ~ScopedTracer() | |
864 { | |
865 if (m_pdata && *m_pdata->categoryGroupEnabled) | |
866 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(m_data.categoryGroupEnab
led, m_data.name, m_data.eventHandle); | |
867 } | |
868 | |
869 void initialize(const unsigned char* categoryGroupEnabled, const char* name,
TraceEventHandle eventHandle) | |
870 { | |
871 m_data.categoryGroupEnabled = categoryGroupEnabled; | |
872 m_data.name = name; | |
873 m_data.eventHandle = eventHandle; | |
874 m_pdata = &m_data; | |
875 } | |
876 | |
877 private: | |
878 // This Data struct workaround is to avoid initializing all the members | |
879 // in Data during construction of this object, since this object is always | |
880 // constructed, even when tracing is disabled. If the members of Data were | |
881 // members of this class instead, compiler warnings occur about potential | |
882 // uninitialized accesses. | |
883 struct Data { | |
884 const unsigned char* categoryGroupEnabled; | |
885 const char* name; | |
886 TraceEventHandle eventHandle; | |
887 }; | |
888 Data* m_pdata; | |
889 Data m_data; | |
890 }; | |
891 | |
892 // TraceEventSamplingStateScope records the current sampling state | |
893 // and sets a new sampling state. When the scope exists, it restores | |
894 // the sampling state having recorded. | |
895 template<size_t BucketNumber> | |
896 class SamplingStateScope { | |
897 WTF_MAKE_FAST_ALLOCATED; | |
898 public: | |
899 SamplingStateScope(const char* categoryAndName) | |
900 { | |
901 m_previousState = SamplingStateScope<BucketNumber>::current(); | |
902 SamplingStateScope<BucketNumber>::set(categoryAndName); | |
903 } | |
904 | |
905 ~SamplingStateScope() | |
906 { | |
907 SamplingStateScope<BucketNumber>::set(m_previousState); | |
908 } | |
909 | |
910 // FIXME: Make load/store to traceSamplingState[] thread-safe and atomic. | |
911 static inline const char* current() | |
912 { | |
913 return reinterpret_cast<const char*>(*WebCore::traceSamplingState[Bucket
Number]); | |
914 } | |
915 static inline void set(const char* categoryAndName) | |
916 { | |
917 *WebCore::traceSamplingState[BucketNumber] = reinterpret_cast<long>(cons
t_cast<char*>(categoryAndName)); | |
918 } | |
919 | |
920 private: | |
921 const char* m_previousState; | |
922 }; | |
923 | |
924 template<typename IDType> class TraceScopedTrackableObject { | |
925 WTF_MAKE_NONCOPYABLE(TraceScopedTrackableObject); | |
926 public: | |
927 TraceScopedTrackableObject(const char* categoryGroup, const char* name, IDTy
pe id) | |
928 : m_categoryGroup(categoryGroup), m_name(name), m_id(id) | |
929 { | |
930 TRACE_EVENT_OBJECT_CREATED_WITH_ID(m_categoryGroup, m_name, m_id); | |
931 } | |
932 | |
933 ~TraceScopedTrackableObject() | |
934 { | |
935 TRACE_EVENT_OBJECT_DELETED_WITH_ID(m_categoryGroup, m_name, m_id); | |
936 } | |
937 | |
938 private: | |
939 const char* m_categoryGroup; | |
940 const char* m_name; | |
941 IDType m_id; | |
942 }; | |
943 | |
944 } // namespace TraceEvent | |
945 | |
946 } // namespace WebCore | |
947 | |
948 #endif | |
OLD | NEW |