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