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