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