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 need 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_BEGIN 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. Pointers 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/PassRefPtr.h" 170 #include "wtf/text/CString.h" 171 172 // By default, const char* argument values are assumed to have long-lived scope 173 // and will not be copied. Use this macro to force a const char* to be copied. 174 #define TRACE_STR_COPY(str) \ 175 blink::TraceEvent::TraceStringWithCopy(str) 176 177 // By default, uint64 ID argument values are not mangled with the Process ID in 178 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling. 179 #define TRACE_ID_MANGLE(id) \ 180 blink::TraceEvent::TraceID::ForceMangle(id) 181 182 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC 183 // macros. Use this macro to prevent Process ID mangling. 184 #define TRACE_ID_DONT_MANGLE(id) \ 185 blink::TraceEvent::TraceID::DontMangle(id) 186 187 // Records a pair of begin and end events called "name" for the current 188 // scope, with 0, 1 or 2 associated arguments. If the category is not 189 // enabled, then this does nothing. 190 // - category and name strings must have application lifetime (statics or 191 // literals). They may not include " chars. 192 #define TRACE_EVENT0(category, name) \ 193 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) 194 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ 195 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) 196 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ 197 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \ 198 arg2_name, arg2_val) 199 200 // Records a single event called "name" immediately, with 0, 1 or 2 201 // associated arguments. If the category is not enabled, then this 202 // does nothing. 203 // - category and name strings must have application lifetime (statics or 204 // literals). They may not include " chars. 205 #define TRACE_EVENT_INSTANT0(category, name) \ 206 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ 207 category, name, TRACE_EVENT_FLAG_NONE) 208 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ 209 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ 210 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 211 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ 212 arg2_name, arg2_val) \ 213 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ 214 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ 215 arg2_name, arg2_val) 216 #define TRACE_EVENT_COPY_INSTANT0(category, name) \ 217 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ 218 category, name, TRACE_EVENT_FLAG_COPY) 219 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \ 220 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ 221 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) 222 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \ 223 arg2_name, arg2_val) \ 224 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ 225 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ 226 arg2_name, arg2_val) 227 228 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2 229 // associated arguments. If the category is not enabled, then this 230 // does nothing. 231 // - category and name strings must have application lifetime (statics or 232 // literals). They may not include " chars. 233 #define TRACE_EVENT_BEGIN0(category, name) \ 234 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ 235 category, name, TRACE_EVENT_FLAG_NONE) 236 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \ 237 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ 238 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 239 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \ 240 arg2_name, arg2_val) \ 241 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ 242 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ 243 arg2_name, arg2_val) 244 #define TRACE_EVENT_COPY_BEGIN0(category, name) \ 245 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ 246 category, name, TRACE_EVENT_FLAG_COPY) 247 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \ 248 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ 249 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) 250 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \ 251 arg2_name, arg2_val) \ 252 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ 253 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ 254 arg2_name, arg2_val) 255 256 // Records a single END event for "name" immediately. If the category 257 // is not enabled, then this does nothing. 258 // - category and name strings must have application lifetime (statics or 259 // literals). They may not include " chars. 260 #define TRACE_EVENT_END0(category, name) \ 261 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ 262 category, name, TRACE_EVENT_FLAG_NONE) 263 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \ 264 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ 265 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 266 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \ 267 arg2_name, arg2_val) \ 268 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ 269 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ 270 arg2_name, arg2_val) 271 #define TRACE_EVENT_COPY_END0(category, name) \ 272 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ 273 category, name, TRACE_EVENT_FLAG_COPY) 274 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ 275 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ 276 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) 277 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ 278 arg2_name, arg2_val) \ 279 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ 280 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ 281 arg2_name, arg2_val) 282 283 // Records the value of a counter called "name" immediately. Value 284 // must be representable as a 32 bit integer. 285 // - category and name strings must have application lifetime (statics or 286 // literals). They may not include " chars. 287 #define TRACE_COUNTER1(category, name, value) \ 288 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ 289 category, name, TRACE_EVENT_FLAG_NONE, \ 290 "value", static_cast<int>(value)) 291 #define TRACE_COPY_COUNTER1(category, name, value) \ 292 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ 293 category, name, TRACE_EVENT_FLAG_COPY, \ 294 "value", static_cast<int>(value)) 295 296 // Records the values of a multi-parted counter called "name" immediately. 297 // The UI will treat value1 and value2 as parts of a whole, displaying their 298 // values as a stacked-bar chart. 299 // - category and name strings must have application lifetime (statics or 300 // literals). They may not include " chars. 301 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ 302 value2_name, value2_val) \ 303 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ 304 category, name, TRACE_EVENT_FLAG_NONE, \ 305 value1_name, static_cast<int>(value1_val), \ 306 value2_name, static_cast<int>(value2_val)) 307 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ 308 value2_name, value2_val) \ 309 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ 310 category, name, TRACE_EVENT_FLAG_COPY, \ 311 value1_name, static_cast<int>(value1_val), \ 312 value2_name, static_cast<int>(value2_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 // - |id| is used to disambiguate counters with the same name. It must either 319 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits 320 // will be xored with a hash of the process ID so that the same pointer on 321 // two different processes will not collide. 322 #define TRACE_COUNTER_ID1(category, name, id, value) \ 323 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ 324 category, name, id, TRACE_EVENT_FLAG_NONE, \ 325 "value", static_cast<int>(value)) 326 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \ 327 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ 328 category, name, id, TRACE_EVENT_FLAG_COPY, \ 329 "value", static_cast<int>(value)) 330 331 // Records the values of a multi-parted counter called "name" immediately. 332 // The UI will treat value1 and value2 as parts of a whole, displaying their 333 // values as a stacked-bar chart. 334 // - category and name strings must have application lifetime (statics or 335 // literals). They may not include " chars. 336 // - |id| is used to disambiguate counters with the same name. It must either 337 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits 338 // will be xored with a hash of the process ID so that the same pointer on 339 // two different processes will not collide. 340 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \ 341 value2_name, value2_val) \ 342 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ 343 category, name, id, TRACE_EVENT_FLAG_NONE, \ 344 value1_name, static_cast<int>(value1_val), \ 345 value2_name, static_cast<int>(value2_val)) 346 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \ 347 value2_name, value2_val) \ 348 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ 349 category, name, id, TRACE_EVENT_FLAG_COPY, \ 350 value1_name, static_cast<int>(value1_val), \ 351 value2_name, static_cast<int>(value2_val)) 352 353 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2 354 // associated arguments. If the category is not enabled, then this 355 // does nothing. 356 // - category and name strings must have application lifetime (statics or 357 // literals). They may not include " chars. 358 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC 359 // events are considered to match if their category, name and id values all 360 // match. |id| must either be a pointer or an integer value up to 64 bits. If 361 // it's a pointer, the bits will be xored with a hash of the process ID so 362 // that the same pointer on two different processes will not collide. 363 // 364 // An asynchronous operation can consist of multiple phases. The first phase is 365 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the 366 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will 367 // annotate the block following the call. The ASYNC_STEP_PAST macro will 368 // annotate the block prior to the call. Note that any particular event must use 369 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the 370 // operation completes, call ASYNC_END. 371 // 372 // An ASYNC trace typically occurs on a single thread (if not, they will only be 373 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that 374 // operation must use the same |name| and |id|. Each step can have its own 375 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \ 376 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 377 category, name, id, TRACE_EVENT_FLAG_NONE) 378 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ 379 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 380 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 381 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \ 382 arg2_name, arg2_val) \ 383 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 384 category, name, id, TRACE_EVENT_FLAG_NONE, \ 385 arg1_name, arg1_val, arg2_name, arg2_val) 386 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \ 387 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 388 category, name, id, TRACE_EVENT_FLAG_COPY) 389 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ 390 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 391 category, name, id, TRACE_EVENT_FLAG_COPY, \ 392 arg1_name, arg1_val) 393 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \ 394 arg2_name, arg2_val) \ 395 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 396 category, name, id, TRACE_EVENT_FLAG_COPY, \ 397 arg1_name, arg1_val, arg2_name, arg2_val) 398 399 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the 400 // category is not enabled, then this does nothing. The |name| and |id| must 401 // match the ASYNC_BEGIN event above. The |step| param identifies this step 402 // within the async event. This should be called at the beginning of the next 403 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any 404 // ASYNC_STEP_PAST events. 405 #define TRACE_EVENT_ASYNC_STEP_INTO0(category, name, id, step) \ 406 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \ 407 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step) 408 #define TRACE_EVENT_ASYNC_STEP_INTO1(category, name, id, step, \ 409 arg1_name, arg1_val) \ 410 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \ 411 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ 412 arg1_name, arg1_val) 413 414 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the 415 // category is not enabled, then this does nothing. The |name| and |id| must 416 // match the ASYNC_BEGIN event above. The |step| param identifies this step 417 // within the async event. This should be called at the beginning of the next 418 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any 419 // ASYNC_STEP_INTO events. 420 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step) \ 421 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \ 422 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step) 423 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, arg1_name, arg1_val) \ 424 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \ 425 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ 426 arg1_name, arg1_val) 427 428 // Records a single ASYNC_END event for "name" immediately. If the category 429 // is not enabled, then this does nothing. 430 #define TRACE_EVENT_ASYNC_END0(category, name, id) \ 431 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 432 category, name, id, TRACE_EVENT_FLAG_NONE) 433 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ 434 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 435 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 436 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \ 437 arg2_name, arg2_val) \ 438 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 439 category, name, id, TRACE_EVENT_FLAG_NONE, \ 440 arg1_name, arg1_val, arg2_name, arg2_val) 441 #define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \ 442 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 443 category, name, id, TRACE_EVENT_FLAG_COPY) 444 #define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ 445 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 446 category, name, id, TRACE_EVENT_FLAG_COPY, \ 447 arg1_name, arg1_val) 448 #define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \ 449 arg2_name, arg2_val) \ 450 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 451 category, name, id, TRACE_EVENT_FLAG_COPY, \ 452 arg1_name, arg1_val, arg2_name, arg2_val) 453 454 455 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2 456 // associated arguments. If the category is not enabled, then this 457 // does nothing. 458 // - category and name strings must have application lifetime (statics or 459 // literals). They may not include " chars. 460 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW 461 // events are considered to match if their category_group, name and id values 462 // all match. |id| must either be a pointer or an integer value up to 64 bits. 463 // If it's a pointer, the bits will be xored with a hash of the process ID so 464 // that the same pointer on two different processes will not collide. 465 // FLOW events are different from ASYNC events in how they are drawn by the 466 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task 467 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be 468 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar 469 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined 470 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP 471 // macros. When the operation completes, call FLOW_END. An async operation can 472 // span threads and processes, but all events in that operation must use the 473 // same |name| and |id|. Each event can have its own args. 474 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \ 475 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ 476 category_group, name, id, TRACE_EVENT_FLAG_NONE) 477 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \ 478 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ 479 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 480 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \ 481 arg2_name, arg2_val) \ 482 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ 483 category_group, name, id, TRACE_EVENT_FLAG_NONE, \ 484 arg1_name, arg1_val, arg2_name, arg2_val) 485 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \ 486 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ 487 category_group, name, id, TRACE_EVENT_FLAG_COPY) 488 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \ 489 arg1_val) \ 490 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ 491 category_group, name, id, TRACE_EVENT_FLAG_COPY, \ 492 arg1_name, arg1_val) 493 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \ 494 arg1_val, arg2_name, arg2_val) \ 495 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ 496 category_group, name, id, TRACE_EVENT_FLAG_COPY, \ 497 arg1_name, arg1_val, arg2_name, arg2_val) 498 499 // Records a single FLOW_STEP event for |step| immediately. If the category 500 // is not enabled, then this does nothing. The |name| and |id| must match the 501 // FLOW_BEGIN event above. The |step| param identifies this step within the 502 // async event. This should be called at the beginning of the next phase of an 503 // asynchronous operation. 504 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \ 505 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ 506 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step) 507 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \ 508 arg1_name, arg1_val) \ 509 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ 510 category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ 511 arg1_name, arg1_val) 512 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \ 513 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ 514 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step) 515 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \ 516 arg1_name, arg1_val) \ 517 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ 518 category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \ 519 arg1_name, arg1_val) 520 521 // Records a single FLOW_END event for "name" immediately. If the category 522 // is not enabled, then this does nothing. 523 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \ 524 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ 525 category_group, name, id, TRACE_EVENT_FLAG_NONE) 526 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \ 527 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ 528 category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 529 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \ 530 arg2_name, arg2_val) \ 531 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ 532 category_group, name, id, TRACE_EVENT_FLAG_NONE, \ 533 arg1_name, arg1_val, arg2_name, arg2_val) 534 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \ 535 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ 536 category_group, name, id, TRACE_EVENT_FLAG_COPY) 537 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \ 538 arg1_val) \ 539 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ 540 category_group, name, id, TRACE_EVENT_FLAG_COPY, \ 541 arg1_name, arg1_val) 542 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \ 543 arg1_val, arg2_name, arg2_val) \ 544 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ 545 category_group, name, id, TRACE_EVENT_FLAG_COPY, \ 546 arg1_name, arg1_val, arg2_name, arg2_val) 547 548 549 // Creates a scope of a sampling state with the given category and name (both must 550 // be constant strings). These states are intended for a sampling profiler. 551 // Implementation note: we store category and name together because we don't 552 // want the inconsistency/expense of storing two pointers. 553 // |thread_bucket| is [0..2] and is used to statically isolate samples in one 554 // thread from others. 555 // 556 // { // The sampling state is set within this scope. 557 // TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name"); 558 // ...; 559 // } 560 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(bucket_number, category, name) \ 561 TraceEvent::SamplingStateScope<bucket_number> traceEventSamplingScope(category "\0" name); 562 563 // Returns a current sampling state of the given bucket. 564 // The format of the returned string is "category\0name". 565 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \ 566 TraceEvent::SamplingStateScope<bucket_number>::current() 567 568 // Sets a current sampling state of the given bucket. 569 // |category| and |name| have to be constant strings. 570 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(bucket_number, category, name) \ 571 TraceEvent::SamplingStateScope<bucket_number>::set(category "\0" name) 572 573 // Sets a current sampling state of the given bucket. 574 // |categoryAndName| doesn't need to be a constant string. 575 // The format of the string is "category\0name". 576 #define TRACE_EVENT_SET_NONCONST_SAMPLING_STATE_FOR_BUCKET(bucket_number, categoryAndName) \ 577 TraceEvent::SamplingStateScope<bucket_number>::set(categoryAndName) 578 579 // Syntactic sugars for the sampling tracing in the main thread. 580 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \ 581 TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name) 582 #define TRACE_EVENT_GET_SAMPLING_STATE() \ 583 TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0) 584 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \ 585 TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name) 586 #define TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(categoryAndName) \ 587 TRACE_EVENT_SET_NONCONST_SAMPLING_STATE_FOR_BUCKET(0, categoryAndName) 588 589 // Macros to track the life time and value of arbitrary client objects. 590 // See also TraceTrackableObject. 591 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(categoryGroup, name, id) \ 592 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \ 593 categoryGroup, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE) 594 595 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(categoryGroup, name, id, snapshot) \ 596 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \ 597 categoryGroup, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE, \ 598 "snapshot", snapshot) 599 600 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(categoryGroup, name, id) \ 601 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \ 602 categoryGroup, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE) 603 604 // Macro to efficiently determine if a given category group is enabled. 605 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(categoryGroup, ret) \ 606 do { \ 607 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(categoryGroup); \ 608 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 609 *ret = true; \ 610 } else { \ 611 *ret = false; \ 612 } \ 613 } while (0) 614 615 // This will mark the trace event as disabled by default. The user will need 616 // to explicitly enable the event. 617 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name 618 619 //////////////////////////////////////////////////////////////////////////////// 620 // Implementation specific tracing API definitions. 621 622 // Get a pointer to the enabled state of the given trace category. Only 623 // long-lived literal strings should be given as the category name. The returned 624 // pointer can be held permanently in a local static for example. If the 625 // unsigned char is non-zero, tracing is enabled. If tracing is enabled, 626 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled 627 // between the load of the tracing state and the call to 628 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out 629 // for best performance when tracing is disabled. 630 // const unsigned char* 631 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) 632 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ 633 blink::EventTracer::getTraceCategoryEnabledFlag 634 635 // Add a trace event to the platform tracing system. 636 // blink::TraceEvent::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT( 637 // char phase, 638 // const unsigned char* category_enabled, 639 // const char* name, 640 // unsigned long long id, 641 // int num_args, 642 // const char** arg_names, 643 // const unsigned char* arg_types, 644 // const unsigned long long* arg_values, 645 // const RefPtr<ConvertableToTraceFormat>* convertableValues 646 // unsigned char flags) 647 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ 648 blink::EventTracer::addTraceEvent 649 650 // Set the duration field of a COMPLETE trace event. 651 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION( 652 // blink::TraceEvent::TraceEventHandle handle) 653 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \ 654 blink::EventTracer::updateTraceEventDuration 655 656 //////////////////////////////////////////////////////////////////////////////// 657 658 // Implementation detail: trace event macros create temporary variables 659 // to keep instrumentation overhead low. These macros give each temporary 660 // variable a unique name based on the line number to prevent name collissions. 661 #define INTERNAL_TRACE_EVENT_UID3(a, b) \ 662 trace_event_unique_##a##b 663 #define INTERNAL_TRACE_EVENT_UID2(a, b) \ 664 INTERNAL_TRACE_EVENT_UID3(a, b) 665 #define INTERNALTRACEEVENTUID(name_prefix) \ 666 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) 667 668 // Implementation detail: internal macro to create static category. 669 // - WTF_ANNOTATE_BENIGN_RACE, see Thread Safety above. 670 671 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ 672 static const unsigned char* INTERNALTRACEEVENTUID(categoryGroupEnabled) = 0; \ 673 WTF_ANNOTATE_BENIGN_RACE(&INTERNALTRACEEVENTUID(categoryGroupEnabled), \ 674 "trace_event category"); \ 675 if (!INTERNALTRACEEVENTUID(categoryGroupEnabled)) { \ 676 INTERNALTRACEEVENTUID(categoryGroupEnabled) = \ 677 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \ 678 } 679 680 // Implementation detail: internal macro to create static category and add 681 // event if the category is enabled. 682 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ 683 do { \ 684 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ 685 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 686 blink::TraceEvent::addTraceEvent( \ 687 phase, INTERNALTRACEEVENTUID(categoryGroupEnabled), name, \ 688 blink::TraceEvent::noEventId, flags, ##__VA_ARGS__); \ 689 } \ 690 } while (0) 691 692 // Implementation detail: internal macro to create static category and add begin 693 // event if the category is enabled. Also adds the end event when the scope 694 // ends. 695 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \ 696 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ 697 blink::TraceEvent::ScopedTracer INTERNALTRACEEVENTUID(scopedTracer); \ 698 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 699 blink::TraceEvent::TraceEventHandle h = \ 700 blink::TraceEvent::addTraceEvent( \ 701 TRACE_EVENT_PHASE_COMPLETE, \ 702 INTERNALTRACEEVENTUID(categoryGroupEnabled), \ 703 name, blink::TraceEvent::noEventId, \ 704 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ 705 INTERNALTRACEEVENTUID(scopedTracer).initialize( \ 706 INTERNALTRACEEVENTUID(categoryGroupEnabled), name, h); \ 707 } 708 709 // Implementation detail: internal macro to create static category and add 710 // event if the category is enabled. 711 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ 712 ...) \ 713 do { \ 714 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ 715 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 716 unsigned char traceEventFlags = flags | TRACE_EVENT_FLAG_HAS_ID; \ 717 blink::TraceEvent::TraceID traceEventTraceID( \ 718 id, &traceEventFlags); \ 719 blink::TraceEvent::addTraceEvent( \ 720 phase, INTERNALTRACEEVENTUID(categoryGroupEnabled), \ 721 name, traceEventTraceID.data(), traceEventFlags, \ 722 ##__VA_ARGS__); \ 723 } \ 724 } while (0) 725 726 // Notes regarding the following definitions: 727 // New values can be added and propagated to third party libraries, but existing 728 // definitions must never be changed, because third party libraries may use old 729 // definitions. 730 731 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. 732 #define TRACE_EVENT_PHASE_BEGIN ('B') 733 #define TRACE_EVENT_PHASE_END ('E') 734 #define TRACE_EVENT_PHASE_COMPLETE ('X') 735 #define TRACE_EVENT_PHASE_INSTANT ('I') 736 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S') 737 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T') 738 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p') 739 #define TRACE_EVENT_PHASE_ASYNC_END ('F') 740 #define TRACE_EVENT_PHASE_METADATA ('M') 741 #define TRACE_EVENT_PHASE_COUNTER ('C') 742 #define TRACE_EVENT_PHASE_SAMPLE ('P') 743 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N') 744 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O') 745 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D') 746 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s') 747 #define TRACE_EVENT_PHASE_FLOW_STEP ('t') 748 #define TRACE_EVENT_PHASE_FLOW_END ('f') 749 750 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. 751 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) 752 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0)) 753 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1)) 754 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2)) 755 756 // Type values for identifying types in the TraceValue union. 757 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) 758 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) 759 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) 760 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) 761 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) 762 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) 763 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) 764 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8)) 765 766 // These values must be in sync with base::debug::TraceLog::CategoryGroupEnabledFlags. 767 #define ENABLED_FOR_RECORDING (1 << 0) 768 #define ENABLED_FOR_EVENT_CALLBACK (1 << 2) 769 770 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \ 771 (*INTERNALTRACEEVENTUID(categoryGroupEnabled) & (ENABLED_FOR_RECORDING | ENABLED_FOR_EVENT_CALLBACK)) 772 773 namespace blink { 774 775 namespace TraceEvent { 776 777 // Specify these values when the corresponding argument of addTraceEvent is not 778 // used. 779 const int zeroNumArgs = 0; 780 const unsigned long long noEventId = 0; 781 782 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers 783 // are mangled with the Process ID so that they are unlikely to collide when the 784 // same pointer is used on different processes. 785 class TraceID { 786 public: 787 template<bool dummyMangle> class MangleBehavior { 788 public: 789 template<typename T> explicit MangleBehavior(T id) : m_data(reinterpret_cast<unsigned long long>(id)) { } 790 unsigned long long data() const { return m_data; } 791 private: 792 unsigned long long m_data; 793 }; 794 typedef MangleBehavior<false> DontMangle; 795 typedef MangleBehavior<true> ForceMangle; 796 797 TraceID(const void* id, unsigned char* flags) : 798 m_data(static_cast<unsigned long long>(reinterpret_cast<unsigned long>(id))) 799 { 800 *flags |= TRACE_EVENT_FLAG_MANGLE_ID; 801 } 802 TraceID(ForceMangle id, unsigned char* flags) : m_data(id.data()) 803 { 804 *flags |= TRACE_EVENT_FLAG_MANGLE_ID; 805 } 806 TraceID(DontMangle id, unsigned char*) : m_data(id.data()) { } 807 TraceID(unsigned long long id, unsigned char*) : m_data(id) { } 808 TraceID(unsigned long id, unsigned char*) : m_data(id) { } 809 TraceID(unsigned id, unsigned char*) : m_data(id) { } 810 TraceID(unsigned short id, unsigned char*) : m_data(id) { } 811 TraceID(unsigned char id, unsigned char*) : m_data(id) { } 812 TraceID(long long id, unsigned char*) : 813 m_data(static_cast<unsigned long long>(id)) { } 814 TraceID(long id, unsigned char*) : 815 m_data(static_cast<unsigned long long>(id)) { } 816 TraceID(int id, unsigned char*) : 817 m_data(static_cast<unsigned long long>(id)) { } 818 TraceID(short id, unsigned char*) : 819 m_data(static_cast<unsigned long long>(id)) { } 820 TraceID(signed char id, unsigned char*) : 821 m_data(static_cast<unsigned long long>(id)) { } 822 823 unsigned long long data() const { return m_data; } 824 825 private: 826 unsigned long long m_data; 827 }; 828 829 // Simple union to store various types as unsigned long long. 830 union TraceValueUnion { 831 bool m_bool; 832 unsigned long long m_uint; 833 long long m_int; 834 double m_double; 835 const void* m_pointer; 836 const char* m_string; 837 }; 838 839 // Simple container for const char* that should be copied instead of retained. 840 class TraceStringWithCopy { 841 public: 842 explicit TraceStringWithCopy(const char* str) : m_str(str) { } 843 const char* str() const { return m_str; } 844 private: 845 const char* m_str; 846 }; 847 848 // Define setTraceValue for each allowed type. It stores the type and 849 // value in the return arguments. This allows this API to avoid declaring any 850 // structures so that it is portable to third_party libraries. 851 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actualType, argExpression, unionMember, valueTypeId) \ 852 static inline void setTraceValue(actualType arg, unsigned char* type, unsigned long long* value) { \ 853 TraceValueUnion typeValue; \ 854 typeValue.unionMember = argExpression; \ 855 *type = valueTypeId; \ 856 *value = typeValue.m_uint; \ 857 } 858 // Simpler form for int types that can be safely casted. 859 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actualType, valueTypeId) \ 860 static inline void setTraceValue(actualType arg, \ 861 unsigned char* type, \ 862 unsigned long long* value) { \ 863 *type = valueTypeId; \ 864 *value = static_cast<unsigned long long>(arg); \ 865 } 866 867 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT) 868 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned, TRACE_VALUE_TYPE_UINT) 869 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT) 870 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT) 871 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT) 872 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT) 873 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT) 874 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT) 875 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, arg, m_bool, TRACE_VALUE_TYPE_BOOL) 876 INTERNAL_DECLARE_SET_TRACE_VALUE(double, arg, m_double, TRACE_VALUE_TYPE_DOUBLE) 877 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, arg, m_pointer, TRACE_VALUE_TYPE_POINTER) 878 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, arg, m_string, TRACE_VALUE_TYPE_STRING) 879 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, arg.str(), m_string, TRACE_VALUE_TYPE_COPY_STRING) 880 881 #undef INTERNAL_DECLARE_SET_TRACE_VALUE 882 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT 883 884 // WTF::String version of setTraceValue so that trace arguments can be strings. 885 static inline void setTraceValue(const WTF::CString& arg, unsigned char* type, unsigned long long* value) 886 { 887 TraceValueUnion typeValue; 888 typeValue.m_string = arg.data(); 889 *type = TRACE_VALUE_TYPE_COPY_STRING; 890 *value = typeValue.m_uint; 891 } 892 893 static inline void setTraceValue(ConvertableToTraceFormat*, unsigned char* type, unsigned long long*) 894 { 895 *type = TRACE_VALUE_TYPE_CONVERTABLE; 896 } 897 898 template<typename T> static inline void setTraceValue(const PassRefPtr<T>& ptr, unsigned char* type, unsigned long long* value) 899 { 900 setTraceValue(ptr.get(), type, value); 901 } 902 903 template<typename T> struct ConvertableToTraceFormatTraits { 904 static const bool isConvertable = false; 905 static void assignIfConvertable(ConvertableToTraceFormat*& left, const T&) 906 { 907 left = 0; 908 } 909 }; 910 911 template<typename T> struct ConvertableToTraceFormatTraits<T*> { 912 static const bool isConvertable = WTF::IsSubclass<T, TraceEvent::ConvertableToTraceFormat>::value; 913 static void assignIfConvertable(ConvertableToTraceFormat*& left, ...) 914 { 915 left = 0; 916 } 917 static void assignIfConvertable(ConvertableToTraceFormat*& left, ConvertableToTraceFormat* const& right) 918 { 919 left = right; 920 } 921 }; 922 923 template<typename T> struct ConvertableToTraceFormatTraits<PassRefPtr<T> > { 924 static const bool isConvertable = WTF::IsSubclass<T, TraceEvent::ConvertableToTraceFormat>::value; 925 static void assignIfConvertable(ConvertableToTraceFormat*& left, const PassRefPtr<T>& right) 926 { 927 ConvertableToTraceFormatTraits<T*>::assignIfConvertable(left, right.get()); 928 } 929 }; 930 931 template<typename T> bool isConvertableToTraceFormat(const T&) 932 { 933 return ConvertableToTraceFormatTraits<T>::isConvertable; 934 } 935 936 template<typename T> void assignIfConvertableToTraceFormat(ConvertableToTraceFormat*& left, const T& right) 937 { 938 ConvertableToTraceFormatTraits<T>::assignIfConvertable(left, right); 939 } 940 941 // These addTraceEvent template functions are defined here instead of in the 942 // macro, because the arg values could be temporary string objects. In order to 943 // store pointers to the internal c_str and pass through to the tracing API, the 944 // arg values must live throughout these procedures. 945 946 static inline TraceEventHandle addTraceEvent( 947 char phase, 948 const unsigned char* categoryEnabled, 949 const char* name, 950 unsigned long long id, 951 unsigned char flags) 952 { 953 return TRACE_EVENT_API_ADD_TRACE_EVENT( 954 phase, categoryEnabled, name, id, 955 zeroNumArgs, 0, 0, 0, 956 flags); 957 } 958 959 template<typename ARG1_TYPE> 960 static inline TraceEventHandle addTraceEvent( 961 char phase, 962 const unsigned char* categoryEnabled, 963 const char* name, 964 unsigned long long id, 965 unsigned char flags, 966 const char* arg1Name, 967 const ARG1_TYPE& arg1Val) 968 { 969 const int numArgs = 1; 970 unsigned char argTypes[1]; 971 unsigned long long argValues[1]; 972 setTraceValue(arg1Val, &argTypes[0], &argValues[0]); 973 if (isConvertableToTraceFormat(arg1Val)) { 974 ConvertableToTraceFormat* convertableValues[1]; 975 assignIfConvertableToTraceFormat(convertableValues[0], arg1Val); 976 return TRACE_EVENT_API_ADD_TRACE_EVENT( 977 phase, categoryEnabled, name, id, 978 numArgs, &arg1Name, argTypes, argValues, 979 convertableValues, 980 flags); 981 } 982 return TRACE_EVENT_API_ADD_TRACE_EVENT( 983 phase, categoryEnabled, name, id, 984 numArgs, &arg1Name, argTypes, argValues, 985 flags); 986 } 987 988 template<typename ARG1_TYPE, typename ARG2_TYPE> 989 static inline TraceEventHandle addTraceEvent( 990 char phase, 991 const unsigned char* categoryEnabled, 992 const char* name, 993 unsigned long long id, 994 unsigned char flags, 995 const char* arg1Name, 996 const ARG1_TYPE& arg1Val, 997 const char* arg2Name, 998 const ARG2_TYPE& arg2Val) 999 { 1000 const int numArgs = 2; 1001 const char* argNames[2] = { arg1Name, arg2Name }; 1002 unsigned char argTypes[2]; 1003 unsigned long long argValues[2]; 1004 setTraceValue(arg1Val, &argTypes[0], &argValues[0]); 1005 setTraceValue(arg2Val, &argTypes[1], &argValues[1]); 1006 if (isConvertableToTraceFormat(arg1Val) || isConvertableToTraceFormat(arg2Val)) { 1007 ConvertableToTraceFormat* convertableValues[2]; 1008 assignIfConvertableToTraceFormat(convertableValues[0], arg1Val); 1009 assignIfConvertableToTraceFormat(convertableValues[1], arg2Val); 1010 return TRACE_EVENT_API_ADD_TRACE_EVENT( 1011 phase, categoryEnabled, name, id, 1012 numArgs, argNames, argTypes, argValues, 1013 convertableValues, 1014 flags); 1015 } 1016 return TRACE_EVENT_API_ADD_TRACE_EVENT( 1017 phase, categoryEnabled, name, id, 1018 numArgs, argNames, argTypes, argValues, 1019 flags); 1020 } 1021 1022 // Used by TRACE_EVENTx macro. Do not use directly. 1023 class ScopedTracer { 1024 public: 1025 // Note: members of m_data intentionally left uninitialized. See initialize. 1026 ScopedTracer() : m_pdata(0) { } 1027 ~ScopedTracer() 1028 { 1029 if (m_pdata && *m_pdata->categoryGroupEnabled) 1030 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(m_data.categoryGroupEnabled, m_data.name, m_data.eventHandle); 1031 } 1032 1033 void initialize(const unsigned char* categoryGroupEnabled, const char* name, TraceEventHandle eventHandle) 1034 { 1035 m_data.categoryGroupEnabled = categoryGroupEnabled; 1036 m_data.name = name; 1037 m_data.eventHandle = eventHandle; 1038 m_pdata = &m_data; 1039 } 1040 1041 private: 1042 // This Data struct workaround is to avoid initializing all the members 1043 // in Data during construction of this object, since this object is always 1044 // constructed, even when tracing is disabled. If the members of Data were 1045 // members of this class instead, compiler warnings occur about potential 1046 // uninitialized accesses. 1047 struct Data { 1048 const unsigned char* categoryGroupEnabled; 1049 const char* name; 1050 TraceEventHandle eventHandle; 1051 }; 1052 Data* m_pdata; 1053 Data m_data; 1054 }; 1055 1056 // TraceEventSamplingStateScope records the current sampling state 1057 // and sets a new sampling state. When the scope exists, it restores 1058 // the sampling state having recorded. 1059 template<size_t BucketNumber> 1060 class SamplingStateScope { 1061 WTF_MAKE_FAST_ALLOCATED; 1062 public: 1063 SamplingStateScope(const char* categoryAndName) 1064 { 1065 m_previousState = SamplingStateScope<BucketNumber>::current(); 1066 SamplingStateScope<BucketNumber>::set(categoryAndName); 1067 } 1068 1069 ~SamplingStateScope() 1070 { 1071 SamplingStateScope<BucketNumber>::set(m_previousState); 1072 } 1073 1074 // FIXME: Make load/store to traceSamplingState[] thread-safe and atomic. 1075 static inline const char* current() 1076 { 1077 return reinterpret_cast<const char*>(*blink::traceSamplingState[BucketNumber]); 1078 } 1079 static inline void set(const char* categoryAndName) 1080 { 1081 *blink::traceSamplingState[BucketNumber] = reinterpret_cast<long>(const_cast<char*>(categoryAndName)); 1082 } 1083 1084 private: 1085 const char* m_previousState; 1086 }; 1087 1088 template<typename IDType> class TraceScopedTrackableObject { 1089 WTF_MAKE_NONCOPYABLE(TraceScopedTrackableObject); 1090 public: 1091 TraceScopedTrackableObject(const char* categoryGroup, const char* name, IDType id) 1092 : m_categoryGroup(categoryGroup), m_name(name), m_id(id) 1093 { 1094 TRACE_EVENT_OBJECT_CREATED_WITH_ID(m_categoryGroup, m_name, m_id); 1095 } 1096 1097 ~TraceScopedTrackableObject() 1098 { 1099 TRACE_EVENT_OBJECT_DELETED_WITH_ID(m_categoryGroup, m_name, m_id); 1100 } 1101 1102 private: 1103 const char* m_categoryGroup; 1104 const char* m_name; 1105 IDType m_id; 1106 }; 1107 1108 } // namespace TraceEvent 1109 1110 } // namespace blink 1111 1112 #endif 1113