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