Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Trace events are for tracking application performance and resource usage. | 5 // Trace events are for tracking application performance and resource usage. |
| 6 // Macros are provided to track: | 6 // Macros are provided to track: |
| 7 // Begin and end of function calls | 7 // Begin and end of function calls |
| 8 // Counters | 8 // Counters |
| 9 // | 9 // |
| 10 // Events are issued against categories. Whereas LOG's | 10 // Events are issued against categories. Whereas LOG's |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 // A thread safe singleton and mutex are used for thread safety. Category | 119 // A thread safe singleton and mutex are used for thread safety. Category |
| 120 // enabled flags are used to limit the performance impact when the system | 120 // enabled flags are used to limit the performance impact when the system |
| 121 // is not enabled. | 121 // is not enabled. |
| 122 // | 122 // |
| 123 // TRACE_EVENT macros first cache a pointer to a category. The categories are | 123 // TRACE_EVENT macros first cache a pointer to a category. The categories are |
| 124 // statically allocated and safe at all times, even after exit. Fetching a | 124 // statically allocated and safe at all times, even after exit. Fetching a |
| 125 // category is protected by the TraceLog::lock_. Multiple threads initializing | 125 // category is protected by the TraceLog::lock_. Multiple threads initializing |
| 126 // the static variable is safe, as they will be serialized by the lock and | 126 // the static variable is safe, as they will be serialized by the lock and |
| 127 // multiple calls will return the same pointer to the category. | 127 // multiple calls will return the same pointer to the category. |
| 128 // | 128 // |
| 129 // Then the category.enabled flag is checked. This is a volatile bool, and | 129 // Then the category_enabled flag is checked. This is a unsigned char, and |
| 130 // not intended to be multithread safe. It optimizes access to AddTraceEvent | 130 // not intended to be multithread safe. It optimizes access to AddTraceEvent |
| 131 // which is threadsafe internally via TraceLog::lock_. The enabled flag may | 131 // which is threadsafe internally via TraceLog::lock_. The enabled flag may |
| 132 // cause some threads to incorrectly call or skip calling AddTraceEvent near | 132 // cause some threads to incorrectly call or skip calling AddTraceEvent near |
| 133 // the time of the system being enabled or disabled. This is acceptable as | 133 // the time of the system being enabled or disabled. This is acceptable as |
| 134 // we tolerate some data loss while the system is being enabled/disabled and | 134 // we tolerate some data loss while the system is being enabled/disabled and |
| 135 // because AddTraceEvent is threadsafe internally and checks the enabled state | 135 // because AddTraceEvent is threadsafe internally and checks the enabled state |
| 136 // again under lock. | 136 // again under lock. |
| 137 // | 137 // |
| 138 // Without the use of these static category pointers and enabled flags all | 138 // Without the use of these static category pointers and enabled flags all |
| 139 // trace points would carry a significant performance cost of aquiring a lock | 139 // trace points would carry a significant performance cost of aquiring a lock |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 153 #include <vector> | 153 #include <vector> |
| 154 | 154 |
| 155 #include "base/callback.h" | 155 #include "base/callback.h" |
| 156 #include "base/hash_tables.h" | 156 #include "base/hash_tables.h" |
| 157 #include "base/memory/ref_counted_memory.h" | 157 #include "base/memory/ref_counted_memory.h" |
| 158 #include "base/string_util.h" | 158 #include "base/string_util.h" |
| 159 #include "base/synchronization/lock.h" | 159 #include "base/synchronization/lock.h" |
| 160 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 160 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 161 #include "base/timer.h" | 161 #include "base/timer.h" |
| 162 | 162 |
| 163 //////////////////////////////////////////////////////////////////////////////// | |
| 164 // TODO(jbates): split this into a separate header. | |
| 165 // This header is designed to be give you trace_event macros without specifying | |
| 166 // how the events actually get collected and stored. If you need to expose trace | |
| 167 // event to some other universe, you can copy-and-paste this file and just | |
| 168 // implement these API macros. | |
| 169 | |
| 170 // unsigned char* | |
| 171 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) | |
| 172 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ | |
| 173 base::debug::TraceLog::GetCategoryEnabled | |
| 174 | |
| 175 // Returns the threshold_begin_id used by TRACE_IF_LONGER_THAN macros. | |
| 176 // int TRACE_EVENT_API_ADD_TRACE_EVENT( | |
| 177 // char phase, | |
| 178 // const unsigned char* category_enabled, | |
|
jar (doing other things)
2012/01/12 21:48:43
nit: IMO, bool is a better type. It adds to readab
jbates
2012/01/12 22:22:11
The main reasoning for not using bool in this API
| |
| 179 // const char* name, | |
| 180 // unsigned long long id, | |
| 181 // int num_args, | |
| 182 // const char** arg_names, | |
| 183 // const unsigned char* arg_types, | |
| 184 // const unsigned long long* arg_values, | |
| 185 // int threshold_begin_id, | |
| 186 // long long threshold, | |
| 187 // unsigned char flags) | |
| 188 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ | |
| 189 base::debug::TraceLog::GetInstance()->AddTraceEvent | |
| 190 | |
| 191 // void TRACE_EVENT_API_ADD_COUNTER_EVENT( | |
| 192 // const unsigned char* category_enabled, | |
| 193 // const char* name, | |
| 194 // unsigned long long id, | |
| 195 // const char* arg1_name, int arg1_val, | |
| 196 // const char* arg2_name, int arg2_val, | |
| 197 // unsigned char flags) | |
| 198 #define TRACE_EVENT_API_ADD_COUNTER_EVENT \ | |
| 199 base::debug::TraceLog::GetInstance()->AddCounterEvent | |
| 200 | |
| 201 // Mangle |pointer| with a process ID hash so that if |pointer| occurs on more | |
| 202 // than one process, it will not collide in the trace data. | |
| 203 // unsigned long long TRACE_EVENT_API_GET_ID_FROM_POINTER(void* pointer) | |
| 204 #define TRACE_EVENT_API_GET_ID_FROM_POINTER \ | |
| 205 base::debug::TraceLog::GetInstance()->GetInterProcessID | |
| 206 | |
| 207 //////////////////////////////////////////////////////////////////////////////// | |
| 208 | |
| 163 // By default, const char* argument values are assumed to have long-lived scope | 209 // By default, const char* argument values are assumed to have long-lived scope |
| 164 // and will not be copied. Use this macro to force a const char* to be copied. | 210 // and will not be copied. Use this macro to force a const char* to be copied. |
| 165 #define TRACE_STR_COPY(str) base::debug::TraceValue::StringWithCopy(str) | 211 #define TRACE_STR_COPY(str) \ |
| 212 trace_event_internal::TraceStringWithCopy(str) | |
| 166 | 213 |
| 167 // Older style trace macros with explicit id and extra data | 214 // Older style trace macros with explicit id and extra data |
| 168 // Only these macros result in publishing data to ETW as currently implemented. | 215 // Only these macros result in publishing data to ETW as currently implemented. |
| 169 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | 216 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ |
| 170 base::debug::TraceLog::AddTraceEventEtw( \ | 217 base::debug::TraceLog::AddTraceEventEtw( \ |
| 171 TRACE_EVENT_PHASE_BEGIN, \ | 218 TRACE_EVENT_PHASE_BEGIN, \ |
| 172 name, reinterpret_cast<const void*>(id), extra) | 219 name, reinterpret_cast<const void*>(id), extra) |
| 173 | 220 |
| 174 #define TRACE_EVENT_END_ETW(name, id, extra) \ | 221 #define TRACE_EVENT_END_ETW(name, id, extra) \ |
| 175 base::debug::TraceLog::AddTraceEventEtw( \ | 222 base::debug::TraceLog::AddTraceEventEtw( \ |
| 176 TRACE_EVENT_PHASE_END, \ | 223 TRACE_EVENT_PHASE_END, \ |
| 177 name, reinterpret_cast<const void*>(id), extra) | 224 name, reinterpret_cast<const void*>(id), extra) |
| 178 | 225 |
| 179 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \ | 226 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \ |
| 180 base::debug::TraceLog::AddTraceEventEtw( \ | 227 base::debug::TraceLog::AddTraceEventEtw( \ |
| 181 TRACE_EVENT_PHASE_INSTANT, \ | 228 TRACE_EVENT_PHASE_INSTANT, \ |
| 182 name, reinterpret_cast<const void*>(id), extra) | 229 name, reinterpret_cast<const void*>(id), extra) |
| 183 | 230 |
| 184 // Records a pair of begin and end events called "name" for the current | 231 // Records a pair of begin and end events called "name" for the current |
| 185 // scope, with 0, 1 or 2 associated arguments. If the category is not | 232 // scope, with 0, 1 or 2 associated arguments. If the category is not |
| 186 // enabled, then this does nothing. | 233 // enabled, then this does nothing. |
| 187 // - category and name strings must have application lifetime (statics or | 234 // - category and name strings must have application lifetime (statics or |
| 188 // literals). They may not include " chars. | 235 // literals). They may not include " chars. |
| 189 #define TRACE_EVENT0(category, name) \ | 236 #define TRACE_EVENT0(category, name) \ |
| 190 TRACE_EVENT1(category, name, NULL, 0) | 237 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) |
| 191 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | 238 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ |
| 192 TRACE_EVENT2(category, name, arg1_name, arg1_val, NULL, 0) | 239 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) |
| 193 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 240 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
| 194 INTERNAL_TRACE_EVENT_ADD_SCOPED( \ | 241 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \ |
| 195 category, name, arg1_name, arg1_val, arg2_name, arg2_val) | 242 arg2_name, arg2_val) |
| 196 | 243 |
| 197 // Same as TRACE_EVENT except that they are not included in official builds. | 244 // Same as TRACE_EVENT except that they are not included in official builds. |
| 198 #ifdef OFFICIAL_BUILD | 245 #ifdef OFFICIAL_BUILD |
| 199 #define UNSHIPPED_TRACE_EVENT0(category, name) (void)0 | 246 #define UNSHIPPED_TRACE_EVENT0(category, name) (void)0 |
| 200 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) (void)0 | 247 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) (void)0 |
| 201 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \ | 248 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \ |
| 202 arg2_name, arg2_val) (void)0 | 249 arg2_name, arg2_val) (void)0 |
| 203 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) (void)0 | 250 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) (void)0 |
| 204 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | 251 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ |
| 205 (void)0 | 252 (void)0 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 222 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | 269 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ |
| 223 arg2_name, arg2_val) | 270 arg2_name, arg2_val) |
| 224 #endif | 271 #endif |
| 225 | 272 |
| 226 // Records a single event called "name" immediately, with 0, 1 or 2 | 273 // Records a single event called "name" immediately, with 0, 1 or 2 |
| 227 // associated arguments. If the category is not enabled, then this | 274 // associated arguments. If the category is not enabled, then this |
| 228 // does nothing. | 275 // does nothing. |
| 229 // - category and name strings must have application lifetime (statics or | 276 // - category and name strings must have application lifetime (statics or |
| 230 // literals). They may not include " chars. | 277 // literals). They may not include " chars. |
| 231 #define TRACE_EVENT_INSTANT0(category, name) \ | 278 #define TRACE_EVENT_INSTANT0(category, name) \ |
| 232 TRACE_EVENT_INSTANT1(category, name, NULL, 0) | 279 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 280 category, name, TRACE_EVENT_FLAG_NONE) | |
| 233 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | 281 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ |
| 234 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, NULL, 0) | 282 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 283 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
| 235 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | 284 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ |
| 236 arg2_name, arg2_val) \ | 285 arg2_name, arg2_val) \ |
| 237 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | 286 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 238 category, name, arg1_name, arg1_val, arg2_name, arg2_val, \ | 287 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ |
| 239 TRACE_EVENT_FLAG_NONE) | 288 arg2_name, arg2_val) |
| 240 #define TRACE_EVENT_COPY_INSTANT0(category, name) \ | 289 #define TRACE_EVENT_COPY_INSTANT0(category, name) \ |
| 241 TRACE_EVENT_COPY_INSTANT1(category, name, NULL, 0) | 290 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 291 category, name, TRACE_EVENT_FLAG_COPY) | |
| 242 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \ | 292 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \ |
| 243 TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, NULL, 0) | 293 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 294 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
| 244 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \ | 295 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \ |
| 245 arg2_name, arg2_val) \ | 296 arg2_name, arg2_val) \ |
| 246 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | 297 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ |
| 247 category, name, \ | 298 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ |
| 248 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 299 arg2_name, arg2_val) |
| 249 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
| 250 TRACE_EVENT_FLAG_COPY) | |
| 251 | 300 |
| 252 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2 | 301 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2 |
| 253 // associated arguments. If the category is not enabled, then this | 302 // associated arguments. If the category is not enabled, then this |
| 254 // does nothing. | 303 // does nothing. |
| 255 // - category and name strings must have application lifetime (statics or | 304 // - category and name strings must have application lifetime (statics or |
| 256 // literals). They may not include " chars. | 305 // literals). They may not include " chars. |
| 257 #define TRACE_EVENT_BEGIN0(category, name) \ | 306 #define TRACE_EVENT_BEGIN0(category, name) \ |
| 258 TRACE_EVENT_BEGIN1(category, name, NULL, 0) | 307 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 308 category, name, TRACE_EVENT_FLAG_NONE) | |
| 259 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \ | 309 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \ |
| 260 TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, NULL, 0) | 310 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 311 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
| 261 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \ | 312 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \ |
| 262 arg2_name, arg2_val) \ | 313 arg2_name, arg2_val) \ |
| 263 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | 314 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 264 category, name, arg1_name, arg1_val, arg2_name, arg2_val, \ | 315 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ |
| 265 TRACE_EVENT_FLAG_NONE) | 316 arg2_name, arg2_val) |
| 266 #define TRACE_EVENT_COPY_BEGIN0(category, name) \ | 317 #define TRACE_EVENT_COPY_BEGIN0(category, name) \ |
| 267 TRACE_EVENT_COPY_BEGIN1(category, name, NULL, 0) | 318 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 319 category, name, TRACE_EVENT_FLAG_COPY) | |
| 268 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \ | 320 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \ |
| 269 TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, NULL, 0) | 321 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 322 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
| 270 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \ | 323 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \ |
| 271 arg2_name, arg2_val) \ | 324 arg2_name, arg2_val) \ |
| 272 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | 325 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ |
| 273 category, name, \ | 326 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ |
| 274 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 327 arg2_name, arg2_val) |
| 275 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
| 276 TRACE_EVENT_FLAG_COPY) | |
| 277 | 328 |
| 278 // Records a single END event for "name" immediately. If the category | 329 // Records a single END event for "name" immediately. If the category |
| 279 // is not enabled, then this does nothing. | 330 // is not enabled, then this does nothing. |
| 280 // - category and name strings must have application lifetime (statics or | 331 // - category and name strings must have application lifetime (statics or |
| 281 // literals). They may not include " chars. | 332 // literals). They may not include " chars. |
| 282 #define TRACE_EVENT_END0(category, name) \ | 333 #define TRACE_EVENT_END0(category, name) \ |
| 283 TRACE_EVENT_END1(category, name, NULL, 0) | 334 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 335 category, name, TRACE_EVENT_FLAG_NONE) | |
| 284 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \ | 336 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \ |
| 285 TRACE_EVENT_END2(category, name, arg1_name, arg1_val, NULL, 0) | 337 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 338 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
| 286 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \ | 339 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \ |
| 287 arg2_name, arg2_val) \ | 340 arg2_name, arg2_val) \ |
| 288 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | 341 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 289 category, name, arg1_name, arg1_val, arg2_name, arg2_val, \ | 342 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ |
| 290 TRACE_EVENT_FLAG_NONE) | 343 arg2_name, arg2_val) |
| 291 #define TRACE_EVENT_COPY_END0(category, name) \ | 344 #define TRACE_EVENT_COPY_END0(category, name) \ |
| 292 TRACE_EVENT_COPY_END1(category, name, NULL, 0) | 345 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 346 category, name, TRACE_EVENT_FLAG_COPY) | |
| 293 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ | 347 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ |
| 294 TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, NULL, 0) | 348 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 349 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
| 295 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ | 350 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ |
| 296 arg2_name, arg2_val) \ | 351 arg2_name, arg2_val) \ |
| 297 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | 352 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ |
| 298 category, name, \ | 353 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ |
| 299 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 354 arg2_name, arg2_val) |
| 300 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
| 301 TRACE_EVENT_FLAG_COPY) | |
| 302 | 355 |
| 303 // Time threshold event: | 356 // Time threshold event: |
| 304 // Only record the event if the duration is greater than the specified | 357 // Only record the event if the duration is greater than the specified |
| 305 // threshold_us (time in microseconds). | 358 // threshold_us (time in microseconds). |
| 306 // Records a pair of begin and end events called "name" for the current | 359 // Records a pair of begin and end events called "name" for the current |
| 307 // scope, with 0, 1 or 2 associated arguments. If the category is not | 360 // scope, with 0, 1 or 2 associated arguments. If the category is not |
| 308 // enabled, then this does nothing. | 361 // enabled, then this does nothing. |
| 309 // - category and name strings must have application lifetime (statics or | 362 // - category and name strings must have application lifetime (statics or |
| 310 // literals). They may not include " chars. | 363 // literals). They may not include " chars. |
| 311 #define TRACE_EVENT_IF_LONGER_THAN0(threshold_us, category, name) \ | 364 #define TRACE_EVENT_IF_LONGER_THAN0(threshold_us, category, name) \ |
| 312 TRACE_EVENT_IF_LONGER_THAN1(threshold_us, category, name, NULL, 0) | 365 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold_us, category, name) |
| 313 #define TRACE_EVENT_IF_LONGER_THAN1( \ | 366 #define TRACE_EVENT_IF_LONGER_THAN1( \ |
| 314 threshold_us, category, name, arg1_name, arg1_val) \ | 367 threshold_us, category, name, arg1_name, arg1_val) \ |
| 315 TRACE_EVENT_IF_LONGER_THAN2(threshold_us, category, name, \ | 368 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ |
| 316 arg1_name, arg1_val, NULL, 0) | 369 threshold_us, category, name, arg1_name, arg1_val) |
| 317 #define TRACE_EVENT_IF_LONGER_THAN2( \ | 370 #define TRACE_EVENT_IF_LONGER_THAN2( \ |
| 318 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 371 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
| 319 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold_us, \ | 372 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ |
| 320 category, name, arg1_name, arg1_val, arg2_name, arg2_val) | 373 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) |
| 321 | 374 |
| 322 // Records the value of a counter called "name" immediately. Value | 375 // Records the value of a counter called "name" immediately. Value |
| 323 // must be representable as a 32 bit integer. | 376 // must be representable as a 32 bit integer. |
| 324 // - category and name strings must have application lifetime (statics or | 377 // - category and name strings must have application lifetime (statics or |
| 325 // literals). They may not include " chars. | 378 // literals). They may not include " chars. |
| 326 #define TRACE_COUNTER1(category, name, value) \ | 379 #define TRACE_COUNTER1(category, name, value) \ |
| 327 TRACE_COUNTER2(category, name, "value", value, NULL, 0) | 380 TRACE_COUNTER2(category, name, "value", value, NULL, 0) |
| 328 #define TRACE_COPY_COUNTER1(category, name, value) \ | 381 #define TRACE_COPY_COUNTER1(category, name, value) \ |
| 329 TRACE_COPY_COUNTER2(category, name, "value", value, NULL, 0) | 382 TRACE_COPY_COUNTER2(category, name, "value", value, NULL, 0) |
| 330 | 383 |
| 331 // Records the values of a multi-parted counter called "name" immediately. | 384 // Records the values of a multi-parted counter called "name" immediately. |
| 332 // The UI will treat value1 and value2 as parts of a whole, displaying their | 385 // The UI will treat value1 and value2 as parts of a whole, displaying their |
| 333 // values as a stacked-bar chart. | 386 // values as a stacked-bar chart. |
| 334 // - category and name strings must have application lifetime (statics or | 387 // - category and name strings must have application lifetime (statics or |
| 335 // literals). They may not include " chars. | 388 // literals). They may not include " chars. |
| 336 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ | 389 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ |
| 337 value2_name, value2_val) \ | 390 value2_name, value2_val) \ |
| 338 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 391 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ |
| 339 category, name, 0, value1_name, value1_val, value2_name, value2_val, \ | 392 category, name, trace_event_internal::kNoEventId, \ |
| 393 value1_name, value1_val, value2_name, value2_val, \ | |
| 340 TRACE_EVENT_FLAG_NONE) | 394 TRACE_EVENT_FLAG_NONE) |
| 341 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ | 395 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ |
| 342 value2_name, value2_val) \ | 396 value2_name, value2_val) \ |
| 343 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 397 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ |
| 344 category, name, 0, \ | 398 category, name, trace_event_internal::kNoEventId, \ |
| 345 value1_name, value1_val, \ | 399 value1_name, value1_val, value2_name, value2_val, \ |
| 346 value2_name, value2_val, \ | |
| 347 TRACE_EVENT_FLAG_COPY) | 400 TRACE_EVENT_FLAG_COPY) |
| 348 | 401 |
| 349 // Records the value of a counter called "name" immediately. Value | 402 // Records the value of a counter called "name" immediately. Value |
| 350 // must be representable as a 32 bit integer. | 403 // must be representable as a 32 bit integer. |
| 351 // - category and name strings must have application lifetime (statics or | 404 // - category and name strings must have application lifetime (statics or |
| 352 // literals). They may not include " chars. | 405 // literals). They may not include " chars. |
| 353 // - |id| is used to disambiguate counters with the same name. It must either | 406 // - |id| is used to disambiguate counters with the same name. It must either |
| 354 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | 407 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits |
| 355 // will be xored with a hash of the process ID so that the same pointer on | 408 // will be xored with a hash of the process ID so that the same pointer on |
| 356 // two different processes will not collide. | 409 // two different processes will not collide. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 385 // Records a single START event called "name" immediately, with 0, 1 or 2 | 438 // Records a single START event called "name" immediately, with 0, 1 or 2 |
| 386 // associated arguments. If the category is not enabled, then this | 439 // associated arguments. If the category is not enabled, then this |
| 387 // does nothing. | 440 // does nothing. |
| 388 // - category and name strings must have application lifetime (statics or | 441 // - category and name strings must have application lifetime (statics or |
| 389 // literals). They may not include " chars. | 442 // literals). They may not include " chars. |
| 390 // - |id| is used to match the START event with the FINISH event. It must either | 443 // - |id| is used to match the START event with the FINISH event. It must either |
| 391 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | 444 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits |
| 392 // will be xored with a hash of the process ID so that the same pointer on | 445 // will be xored with a hash of the process ID so that the same pointer on |
| 393 // two different processes will not collide. | 446 // two different processes will not collide. |
| 394 #define TRACE_EVENT_START0(category, name, id) \ | 447 #define TRACE_EVENT_START0(category, name, id) \ |
| 395 TRACE_EVENT_START1(category, name, id, NULL, 0) | 448 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 449 category, name, id, TRACE_EVENT_FLAG_HAS_ID) | |
| 396 #define TRACE_EVENT_START1(category, name, id, arg1_name, arg1_val) \ | 450 #define TRACE_EVENT_START1(category, name, id, arg1_name, arg1_val) \ |
| 397 TRACE_EVENT_START2(category, name, id, arg1_name, arg1_val, NULL, 0) | 451 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 452 category, name, id, TRACE_EVENT_FLAG_HAS_ID, arg1_name, arg1_val) | |
| 398 #define TRACE_EVENT_START2(category, name, id, arg1_name, arg1_val, \ | 453 #define TRACE_EVENT_START2(category, name, id, arg1_name, arg1_val, \ |
| 399 arg2_name, arg2_val) \ | 454 arg2_name, arg2_val) \ |
| 400 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ | 455 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 401 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, \ | 456 category, name, id, TRACE_EVENT_FLAG_HAS_ID, \ |
| 402 TRACE_EVENT_FLAG_HAS_ID) | 457 arg1_name, arg1_val, arg2_name, arg2_val) |
| 403 #define TRACE_EVENT_COPY_START0(category, name, id) \ | 458 #define TRACE_EVENT_COPY_START0(category, name, id) \ |
| 404 TRACE_EVENT_COPY_START1(category, name, id, NULL, 0) | 459 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 460 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY) | |
| 405 #define TRACE_EVENT_COPY_START1(category, name, id, arg1_name, arg1_val) \ | 461 #define TRACE_EVENT_COPY_START1(category, name, id, arg1_name, arg1_val) \ |
| 406 TRACE_EVENT_COPY_START2(category, name, id, arg1_name, arg1_val, NULL, 0) | 462 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 463 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ | |
| 464 arg1_name, arg1_val) | |
| 407 #define TRACE_EVENT_COPY_START2(category, name, id, arg1_name, arg1_val, \ | 465 #define TRACE_EVENT_COPY_START2(category, name, id, arg1_name, arg1_val, \ |
| 408 arg2_name, arg2_val) \ | 466 arg2_name, arg2_val) \ |
| 409 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ | 467 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_START, \ |
| 410 category, name, id, \ | 468 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
| 411 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 469 arg1_name, arg1_val, arg2_name, arg2_val) |
| 412 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
| 413 TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY) | |
| 414 | 470 |
| 415 // Records a single FINISH event for "name" immediately. If the category | 471 // Records a single FINISH event for "name" immediately. If the category |
| 416 // is not enabled, then this does nothing. | 472 // is not enabled, then this does nothing. |
| 417 #define TRACE_EVENT_FINISH0(category, name, id) \ | 473 #define TRACE_EVENT_FINISH0(category, name, id) \ |
| 418 TRACE_EVENT_FINISH1(category, name, id, NULL, 0) | 474 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 475 category, name, id, TRACE_EVENT_FLAG_HAS_ID) | |
| 419 #define TRACE_EVENT_FINISH1(category, name, id, arg1_name, arg1_val) \ | 476 #define TRACE_EVENT_FINISH1(category, name, id, arg1_name, arg1_val) \ |
| 420 TRACE_EVENT_FINISH2(category, name, id, arg1_name, arg1_val, NULL, 0) | 477 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 478 category, name, id, TRACE_EVENT_FLAG_HAS_ID, arg1_name, arg1_val) | |
| 421 #define TRACE_EVENT_FINISH2(category, name, id, arg1_name, arg1_val, \ | 479 #define TRACE_EVENT_FINISH2(category, name, id, arg1_name, arg1_val, \ |
| 422 arg2_name, arg2_val) \ | 480 arg2_name, arg2_val) \ |
| 423 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ | 481 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 424 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, \ | 482 category, name, id, TRACE_EVENT_FLAG_HAS_ID, \ |
| 425 TRACE_EVENT_FLAG_HAS_ID) | 483 arg1_name, arg1_val, arg2_name, arg2_val) |
| 426 #define TRACE_EVENT_COPY_FINISH0(category, name, id) \ | 484 #define TRACE_EVENT_COPY_FINISH0(category, name, id) \ |
| 427 TRACE_EVENT_COPY_FINISH1(category, name, id, NULL, 0) | 485 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 486 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY) | |
| 428 #define TRACE_EVENT_COPY_FINISH1(category, name, id, arg1_name, arg1_val) \ | 487 #define TRACE_EVENT_COPY_FINISH1(category, name, id, arg1_name, arg1_val) \ |
| 429 TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, NULL, 0) | 488 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 489 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ | |
| 490 arg1_name, arg1_val) | |
| 430 #define TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, \ | 491 #define TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, \ |
| 431 arg2_name, arg2_val) \ | 492 arg2_name, arg2_val) \ |
| 432 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ | 493 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 433 category, name, id, \ | 494 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
| 434 arg1_name, base::debug::TraceValue::ForceCopy(arg1_val), \ | 495 arg1_name, arg1_val, arg2_name, arg2_val) |
| 435 arg2_name, base::debug::TraceValue::ForceCopy(arg2_val), \ | |
| 436 TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY) | |
| 437 | 496 |
| 438 | 497 |
| 439 // Implementation detail: trace event macros create temporary variables | 498 // Implementation detail: trace event macros create temporary variables |
| 440 // to keep instrumentation overhead low. These macros give each temporary | 499 // to keep instrumentation overhead low. These macros give each temporary |
| 441 // variable a unique name based on the line number to prevent name collissions. | 500 // variable a unique name based on the line number to prevent name collissions. |
| 442 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ | 501 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ |
| 443 trace_event_unique_##a##b | 502 trace_event_unique_##a##b |
| 444 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ | 503 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ |
| 445 INTERNAL_TRACE_EVENT_UID3(a,b) | 504 INTERNAL_TRACE_EVENT_UID3(a,b) |
| 446 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ | 505 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ |
| 447 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) | 506 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) |
| 448 | 507 |
| 449 // Implementation detail: internal macro to create static category. | 508 // Implementation detail: internal macro to create static category. |
| 450 // - ANNOTATE_BENIGN_RACE, see Thread Safety above. | 509 // - ANNOTATE_BENIGN_RACE, see Thread Safety above. |
| 451 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ | 510 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ |
| 452 static const base::debug::TraceCategory* \ | 511 static const unsigned char* INTERNAL_TRACE_EVENT_UID(catstatic) = NULL; \ |
| 453 INTERNAL_TRACE_EVENT_UID(catstatic) = NULL; \ | |
| 454 ANNOTATE_BENIGN_RACE(&INTERNAL_TRACE_EVENT_UID(catstatic), \ | 512 ANNOTATE_BENIGN_RACE(&INTERNAL_TRACE_EVENT_UID(catstatic), \ |
| 455 "trace_event category"); \ | 513 "trace_event category"); \ |
| 456 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) \ | 514 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) \ |
| 457 INTERNAL_TRACE_EVENT_UID(catstatic) = \ | 515 INTERNAL_TRACE_EVENT_UID(catstatic) = \ |
| 458 base::debug::TraceLog::GetCategory(category); | 516 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); |
| 459 | 517 |
| 460 // Implementation detail: internal macro to create static category and add | 518 // Implementation detail: internal macro to create static category and add |
| 461 // event if the category is enabled. | 519 // event if the category is enabled. |
| 462 #define INTERNAL_TRACE_EVENT_ADD( \ | 520 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ |
| 463 phase, category, name, arg1_name, arg1_val, arg2_name, arg2_val, flags) \ | |
| 464 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 521 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
| 465 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 522 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
| 466 base::debug::TraceLog::GetInstance()->AddTraceEvent( \ | 523 trace_event_internal::AddTraceEvent( \ |
| 467 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ | 524 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \ |
| 468 name, 0, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, flags); \ | 525 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \ |
| 469 } | 526 } |
| 470 | 527 |
| 471 // Implementation detail: internal macro to create static category and | 528 // Implementation detail: internal macro to create static category and |
| 472 // add the counter event if it is enabled. | 529 // add the counter event if it is enabled. |
| 473 #define INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 530 #define INTERNAL_TRACE_EVENT_ADD_COUNTER( \ |
| 474 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, flags) \ | 531 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, flags) \ |
| 475 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 532 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
| 476 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 533 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
| 477 base::debug::TraceLog::GetInstance()->AddCounterEvent( \ | 534 TRACE_EVENT_API_ADD_COUNTER_EVENT( \ |
| 478 INTERNAL_TRACE_EVENT_UID(catstatic), \ | 535 INTERNAL_TRACE_EVENT_UID(catstatic), \ |
| 479 name, id, arg1_name, arg1_val, arg2_name, arg2_val, flags); \ | 536 name, trace_event_internal::TraceID(id).data(), \ |
| 537 arg1_name, arg1_val, arg2_name, arg2_val, flags); \ | |
| 480 } | 538 } |
| 481 | 539 |
| 482 // Implementation detail: internal macro to create static category and add begin | 540 // Implementation detail: internal macro to create static category and add begin |
| 483 // event if the category is enabled. Also adds the end event when the scope | 541 // event if the category is enabled. Also adds the end event when the scope |
| 484 // ends. | 542 // ends. |
| 485 #define INTERNAL_TRACE_EVENT_ADD_SCOPED( \ | 543 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \ |
| 486 category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | |
| 487 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 544 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
| 488 base::debug::internal::TraceEndOnScopeClose \ | 545 trace_event_internal::TraceEndOnScopeClose \ |
| 489 INTERNAL_TRACE_EVENT_UID(profileScope); \ | 546 INTERNAL_TRACE_EVENT_UID(profileScope); \ |
| 490 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 547 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
| 491 base::debug::TraceLog::GetInstance()->AddTraceEvent( \ | 548 trace_event_internal::AddTraceEvent( \ |
| 492 TRACE_EVENT_PHASE_BEGIN, \ | 549 TRACE_EVENT_PHASE_BEGIN, \ |
| 493 INTERNAL_TRACE_EVENT_UID(catstatic), \ | 550 INTERNAL_TRACE_EVENT_UID(catstatic), \ |
| 494 name, 0, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, \ | 551 name, trace_event_internal::kNoEventId, \ |
| 495 TRACE_EVENT_FLAG_NONE); \ | 552 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ |
| 496 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | 553 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ |
| 497 INTERNAL_TRACE_EVENT_UID(catstatic), name); \ | 554 INTERNAL_TRACE_EVENT_UID(catstatic), name); \ |
| 498 } | 555 } |
| 499 | 556 |
| 500 // Implementation detail: internal macro to create static category and add begin | 557 // Implementation detail: internal macro to create static category and add begin |
| 501 // event if the category is enabled. Also adds the end event when the scope | 558 // event if the category is enabled. Also adds the end event when the scope |
| 502 // ends. If the elapsed time is < threshold time, the begin/end pair is erased. | 559 // ends. If the elapsed time is < threshold time, the begin/end pair is erased. |
| 503 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold, \ | 560 #define INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN(threshold, \ |
| 504 category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 561 category, name, ...) \ |
| 505 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 562 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
| 506 base::debug::internal::TraceEndOnScopeCloseThreshold \ | 563 trace_event_internal::TraceEndOnScopeCloseThreshold \ |
| 507 INTERNAL_TRACE_EVENT_UID(profileScope); \ | 564 INTERNAL_TRACE_EVENT_UID(profileScope); \ |
| 508 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 565 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
| 509 int INTERNAL_TRACE_EVENT_UID(begin_event_id) = \ | 566 int INTERNAL_TRACE_EVENT_UID(begin_event_id) = \ |
| 510 base::debug::TraceLog::GetInstance()->AddTraceEvent( \ | 567 trace_event_internal::AddTraceEvent( \ |
| 511 TRACE_EVENT_PHASE_BEGIN, \ | 568 TRACE_EVENT_PHASE_BEGIN, \ |
| 512 INTERNAL_TRACE_EVENT_UID(catstatic), \ | 569 INTERNAL_TRACE_EVENT_UID(catstatic), \ |
| 513 name, 0, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, \ | 570 name, trace_event_internal::kNoEventId, \ |
| 514 TRACE_EVENT_FLAG_NONE); \ | 571 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ |
| 515 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | 572 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ |
| 516 INTERNAL_TRACE_EVENT_UID(catstatic), name, \ | 573 INTERNAL_TRACE_EVENT_UID(catstatic), name, \ |
| 517 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \ | 574 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \ |
| 518 } | 575 } |
| 519 | 576 |
| 520 // Implementation detail: internal macro to create static category and add | 577 // Implementation detail: internal macro to create static category and add |
| 521 // event if the category is enabled. | 578 // event if the category is enabled. |
| 522 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, \ | 579 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ |
| 523 arg1_name, arg1_val, arg2_name, arg2_val, flags) \ | 580 ...) \ |
| 524 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 581 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
| 525 if (INTERNAL_TRACE_EVENT_UID(catstatic)->enabled) { \ | 582 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
| 526 base::debug::TraceLog::GetInstance()->AddTraceEvent( \ | 583 trace_event_internal::AddTraceEvent( \ |
| 527 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ | 584 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ |
| 528 name, id, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, flags); \ | 585 name, trace_event_internal::TraceID(id).data(), flags, \ |
| 586 ##__VA_ARGS__); \ | |
| 529 } | 587 } |
| 530 | 588 |
| 531 template <typename Type> | 589 template <typename Type> |
| 532 struct StaticMemorySingletonTraits; | 590 struct StaticMemorySingletonTraits; |
| 533 | 591 |
| 534 namespace base { | 592 namespace base { |
| 535 | 593 |
| 536 class RefCountedString; | 594 class RefCountedString; |
| 537 | 595 |
| 538 namespace debug { | 596 namespace debug { |
| 539 | 597 |
| 540 // Categories allow enabling/disabling of streams of trace events | 598 const int kTraceMaxNumArgs = 2; |
| 541 struct TraceCategory { | |
| 542 const char* name; | |
| 543 volatile bool enabled; | |
| 544 }; | |
| 545 | 599 |
| 546 const size_t kTraceMaxNumArgs = 2; | 600 // Notes regarding the following definitions: |
| 601 // New values can be added and propagated to third party libraries, but existing | |
| 602 // definitions must never be changed, because third party libraries may use old | |
| 603 // definitions. | |
| 547 | 604 |
| 548 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. | 605 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. |
| 549 typedef char TraceEventPhase; | 606 #define TRACE_EVENT_PHASE_BEGIN ('B') |
| 550 #define TRACE_EVENT_PHASE_BEGIN (base::debug::TraceEventPhase('B')) | 607 #define TRACE_EVENT_PHASE_END ('E') |
| 551 #define TRACE_EVENT_PHASE_END (base::debug::TraceEventPhase('E')) | 608 #define TRACE_EVENT_PHASE_INSTANT ('I') |
| 552 #define TRACE_EVENT_PHASE_INSTANT (base::debug::TraceEventPhase('I')) | 609 #define TRACE_EVENT_PHASE_START ('S') |
| 553 #define TRACE_EVENT_PHASE_START (base::debug::TraceEventPhase('S')) | 610 #define TRACE_EVENT_PHASE_FINISH ('F') |
| 554 #define TRACE_EVENT_PHASE_FINISH (base::debug::TraceEventPhase('F')) | 611 #define TRACE_EVENT_PHASE_METADATA ('M') |
| 555 #define TRACE_EVENT_PHASE_METADATA (base::debug::TraceEventPhase('M')) | 612 #define TRACE_EVENT_PHASE_COUNTER ('C') |
| 556 #define TRACE_EVENT_PHASE_COUNTER (base::debug::TraceEventPhase('C')) | |
| 557 | 613 |
| 558 typedef uint8 TraceEventFlags; | 614 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. |
| 559 #define TRACE_EVENT_FLAG_NONE (base::debug::TraceEventFlags(0)) | 615 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) |
| 560 #define TRACE_EVENT_FLAG_COPY (base::debug::TraceEventFlags(1<<0)) | 616 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1<<0)) |
| 561 #define TRACE_EVENT_FLAG_HAS_ID (base::debug::TraceEventFlags(1<<1)) | 617 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1<<1)) |
| 562 | 618 |
| 563 // Simple union of values. This is much lighter weight than base::Value, which | 619 // Type values for identifying types in the TraceValue union. |
| 564 // requires dynamic allocation and a vtable. To keep the trace runtime overhead | 620 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) |
| 565 // low, we want constant size storage here. | 621 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) |
| 566 class BASE_EXPORT TraceValue { | 622 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) |
| 567 public: | 623 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) |
| 568 enum Type { | 624 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) |
| 569 TRACE_TYPE_UNDEFINED, | 625 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) |
| 570 TRACE_TYPE_BOOL, | 626 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) |
| 571 TRACE_TYPE_UINT, | |
| 572 TRACE_TYPE_INT, | |
| 573 TRACE_TYPE_DOUBLE, | |
| 574 TRACE_TYPE_POINTER, | |
| 575 TRACE_TYPE_STRING, | |
| 576 TRACE_TYPE_STATIC_STRING | |
| 577 }; | |
| 578 | |
| 579 TraceValue() : type_(TRACE_TYPE_UNDEFINED) { | |
| 580 value_.as_uint = 0ull; | |
| 581 } | |
| 582 TraceValue(bool rhs) : type_(TRACE_TYPE_BOOL) { | |
| 583 value_.as_bool = rhs; | |
| 584 } | |
| 585 TraceValue(uint64 rhs) : type_(TRACE_TYPE_UINT) { | |
| 586 value_.as_uint = rhs; | |
| 587 } | |
| 588 TraceValue(uint32 rhs) : type_(TRACE_TYPE_UINT) { | |
| 589 value_.as_uint = rhs; | |
| 590 } | |
| 591 TraceValue(uint16 rhs) : type_(TRACE_TYPE_UINT) { | |
| 592 value_.as_uint = rhs; | |
| 593 } | |
| 594 TraceValue(uint8 rhs) : type_(TRACE_TYPE_UINT) { | |
| 595 value_.as_uint = rhs; | |
| 596 } | |
| 597 TraceValue(int64 rhs) : type_(TRACE_TYPE_INT) { | |
| 598 value_.as_int = rhs; | |
| 599 } | |
| 600 TraceValue(int32 rhs) : type_(TRACE_TYPE_INT) { | |
| 601 value_.as_int = rhs; | |
| 602 } | |
| 603 TraceValue(int16 rhs) : type_(TRACE_TYPE_INT) { | |
| 604 value_.as_int = rhs; | |
| 605 } | |
| 606 TraceValue(int8 rhs) : type_(TRACE_TYPE_INT) { | |
| 607 value_.as_int = rhs; | |
| 608 } | |
| 609 TraceValue(double rhs) : type_(TRACE_TYPE_DOUBLE) { | |
| 610 value_.as_double = rhs; | |
| 611 } | |
| 612 TraceValue(const void* rhs) : type_(TRACE_TYPE_POINTER) { | |
| 613 value_.as_pointer = rhs; | |
| 614 } | |
| 615 TraceValue(const std::string& rhs) : type_(TRACE_TYPE_STRING) { | |
| 616 value_.as_string = rhs.c_str(); | |
| 617 } | |
| 618 TraceValue(const char* rhs) : type_(TRACE_TYPE_STATIC_STRING) { | |
| 619 value_.as_string = rhs; | |
| 620 } | |
| 621 | |
| 622 static TraceValue StringWithCopy(const char* rhs) { | |
| 623 TraceValue value(rhs); | |
| 624 if (rhs) | |
| 625 value.type_ = TRACE_TYPE_STRING; | |
| 626 return value; | |
| 627 } | |
| 628 | |
| 629 static TraceValue ForceCopy(const TraceValue& rhs) { | |
| 630 TraceValue value(rhs); | |
| 631 if (value.type_ == TRACE_TYPE_STATIC_STRING && value.as_string()) | |
| 632 value.type_ = TRACE_TYPE_STRING; | |
| 633 return value; | |
| 634 } | |
| 635 | |
| 636 bool is_string() const { | |
| 637 return type_ == TRACE_TYPE_STRING || type_ == TRACE_TYPE_STATIC_STRING; | |
| 638 } | |
| 639 | |
| 640 void AppendAsJSON(std::string* out) const; | |
| 641 | |
| 642 Type type() const { | |
| 643 return type_; | |
| 644 } | |
| 645 uint64 as_uint() const { | |
| 646 DCHECK_EQ(TRACE_TYPE_UINT, type_); | |
| 647 return value_.as_uint; | |
| 648 } | |
| 649 bool as_bool() const { | |
| 650 DCHECK_EQ(TRACE_TYPE_BOOL, type_); | |
| 651 return value_.as_bool; | |
| 652 } | |
| 653 int64 as_int() const { | |
| 654 DCHECK_EQ(TRACE_TYPE_INT, type_); | |
| 655 return value_.as_int; | |
| 656 } | |
| 657 double as_double() const { | |
| 658 DCHECK_EQ(TRACE_TYPE_DOUBLE, type_); | |
| 659 return value_.as_double; | |
| 660 } | |
| 661 const void* as_pointer() const { | |
| 662 DCHECK_EQ(TRACE_TYPE_POINTER, type_); | |
| 663 return value_.as_pointer; | |
| 664 } | |
| 665 const char* as_string() const { | |
| 666 DCHECK(is_string()); | |
| 667 return value_.as_string; | |
| 668 } | |
| 669 const char** as_assignable_string() { | |
| 670 DCHECK_EQ(TRACE_TYPE_STRING, type_); | |
| 671 return &value_.as_string; | |
| 672 } | |
| 673 | |
| 674 private: | |
| 675 union Value { | |
| 676 bool as_bool; | |
| 677 uint64 as_uint; | |
| 678 int64 as_int; | |
| 679 double as_double; | |
| 680 const void* as_pointer; | |
| 681 const char* as_string; | |
| 682 }; | |
| 683 | |
| 684 Type type_; | |
| 685 Value value_; | |
| 686 }; | |
| 687 | |
| 688 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | |
| 689 // are mangled with the Process ID so that they are unlikely to collide when the | |
| 690 // same pointer is used on different processes. | |
| 691 class BASE_EXPORT TraceID { | |
| 692 public: | |
| 693 TraceID() : data_(0u) {} | |
| 694 TraceID(void* rhs); | |
| 695 TraceID(unsigned long long rhs) : data_(static_cast<uint64>(rhs)) {} | |
| 696 TraceID(unsigned long rhs) : data_(static_cast<uint64>(rhs)) {} | |
| 697 TraceID(unsigned int rhs) : data_(static_cast<uint64>(rhs)) {} | |
| 698 TraceID(long long rhs) : data_(static_cast<uint64>(rhs)) {} | |
| 699 TraceID(long rhs) : data_(static_cast<uint64>(rhs)) {} | |
| 700 TraceID(int rhs) : data_(static_cast<uint64>(rhs)) {} | |
| 701 | |
| 702 uint64 data() const { return data_; } | |
| 703 | |
| 704 private: | |
| 705 uint64 data_; | |
| 706 }; | |
| 707 | 627 |
| 708 // Output records are "Events" and can be obtained via the | 628 // Output records are "Events" and can be obtained via the |
| 709 // OutputCallback whenever the tracing system decides to flush. This | 629 // OutputCallback whenever the tracing system decides to flush. This |
| 710 // can happen at any time, on any thread, or you can programatically | 630 // can happen at any time, on any thread, or you can programatically |
| 711 // force it to happen. | 631 // force it to happen. |
| 712 class BASE_EXPORT TraceEvent { | 632 class BASE_EXPORT TraceEvent { |
| 713 public: | 633 public: |
| 634 union TraceValue { | |
| 635 bool as_bool; | |
| 636 unsigned long long as_uint; | |
| 637 long long as_int; | |
| 638 double as_double; | |
| 639 const void* as_pointer; | |
| 640 const char* as_string; | |
| 641 }; | |
| 642 | |
| 714 TraceEvent(); | 643 TraceEvent(); |
| 715 TraceEvent(int thread_id, | 644 TraceEvent(int thread_id, |
| 716 TimeTicks timestamp, | 645 TimeTicks timestamp, |
| 717 TraceEventPhase phase, | 646 char phase, |
| 718 const TraceCategory* category, | 647 const unsigned char* category_enabled, |
| 719 const char* name, | 648 const char* name, |
| 720 TraceID id, | 649 unsigned long long id, |
| 721 const char* arg1_name, const TraceValue& arg1_val, | 650 int num_args, |
| 722 const char* arg2_name, const TraceValue& arg2_val, | 651 const char** arg_names, |
| 723 TraceEventFlags flags); | 652 const unsigned char* arg_types, |
| 653 const unsigned long long* arg_values, | |
| 654 unsigned char flags); | |
| 724 ~TraceEvent(); | 655 ~TraceEvent(); |
| 725 | 656 |
| 726 static char GetPhaseChar(TraceEventPhase phase) { | |
| 727 return static_cast<char>(phase); | |
| 728 } | |
| 729 | |
| 730 static TraceEventPhase GetPhase(const char* phase) { | |
| 731 return static_cast<TraceEventPhase>(*phase); | |
| 732 } | |
| 733 | |
| 734 // Serialize event data to JSON | 657 // Serialize event data to JSON |
| 735 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 658 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
| 736 size_t start, | 659 size_t start, |
| 737 size_t count, | 660 size_t count, |
| 738 std::string* out); | 661 std::string* out); |
| 739 void AppendAsJSON(std::string* out) const; | 662 void AppendAsJSON(std::string* out) const; |
| 740 | 663 |
| 741 TimeTicks timestamp() const { return timestamp_; } | 664 TimeTicks timestamp() const { return timestamp_; } |
| 742 | 665 |
| 743 // Exposed for unittesting: | 666 // Exposed for unittesting: |
| 744 | 667 |
| 745 const base::RefCountedString* parameter_copy_storage() const { | 668 const base::RefCountedString* parameter_copy_storage() const { |
| 746 return parameter_copy_storage_.get(); | 669 return parameter_copy_storage_.get(); |
| 747 } | 670 } |
| 748 | 671 |
| 749 const char* name() const { return name_; } | 672 const char* name() const { return name_; } |
| 750 | 673 |
| 751 private: | 674 private: |
| 752 // Note: these are ordered by size (largest first) for optimal packing. | 675 // Note: these are ordered by size (largest first) for optimal packing. |
| 753 TimeTicks timestamp_; | 676 TimeTicks timestamp_; |
| 754 // id_ can be used to store phase-specific data. | 677 // id_ can be used to store phase-specific data. |
| 755 TraceID id_; | 678 unsigned long long id_; |
| 756 TraceValue arg_values_[kTraceMaxNumArgs]; | 679 TraceValue arg_values_[kTraceMaxNumArgs]; |
| 757 const char* arg_names_[kTraceMaxNumArgs]; | 680 const char* arg_names_[kTraceMaxNumArgs]; |
| 758 const TraceCategory* category_; | 681 const unsigned char* category_enabled_; |
| 759 const char* name_; | 682 const char* name_; |
| 760 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | 683 scoped_refptr<base::RefCountedString> parameter_copy_storage_; |
| 761 int thread_id_; | 684 int thread_id_; |
| 762 TraceEventPhase phase_; | 685 char phase_; |
| 763 TraceEventFlags flags_; | 686 unsigned char flags_; |
| 687 unsigned char arg_types_[kTraceMaxNumArgs]; | |
| 764 }; | 688 }; |
| 765 | 689 |
| 766 | 690 |
| 767 // TraceResultBuffer collects and converts trace fragments returned by TraceLog | 691 // TraceResultBuffer collects and converts trace fragments returned by TraceLog |
| 768 // to JSON output. | 692 // to JSON output. |
| 769 class BASE_EXPORT TraceResultBuffer { | 693 class BASE_EXPORT TraceResultBuffer { |
| 770 public: | 694 public: |
| 771 typedef base::Callback<void(const std::string&)> OutputCallback; | 695 typedef base::Callback<void(const std::string&)> OutputCallback; |
| 772 | 696 |
| 773 // If you don't need to stream JSON chunks out efficiently, and just want to | 697 // If you don't need to stream JSON chunks out efficiently, and just want to |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 863 // The trace buffer does not flush dynamically, so when it fills up, | 787 // The trace buffer does not flush dynamically, so when it fills up, |
| 864 // subsequent trace events will be dropped. This callback is generated when | 788 // subsequent trace events will be dropped. This callback is generated when |
| 865 // the trace buffer is full. The callback must be thread safe. | 789 // the trace buffer is full. The callback must be thread safe. |
| 866 typedef base::Callback<void(void)> BufferFullCallback; | 790 typedef base::Callback<void(void)> BufferFullCallback; |
| 867 void SetBufferFullCallback(const BufferFullCallback& cb); | 791 void SetBufferFullCallback(const BufferFullCallback& cb); |
| 868 | 792 |
| 869 // Flushes all logged data to the callback. | 793 // Flushes all logged data to the callback. |
| 870 void Flush(); | 794 void Flush(); |
| 871 | 795 |
| 872 // Called by TRACE_EVENT* macros, don't call this directly. | 796 // Called by TRACE_EVENT* macros, don't call this directly. |
| 873 static const TraceCategory* GetCategory(const char* name); | 797 static const unsigned char* GetCategoryEnabled(const char* name); |
| 798 static const char* GetCategoryName(const unsigned char* category_enabled); | |
| 874 | 799 |
| 875 // Called by TRACE_EVENT* macros, don't call this directly. | 800 // Called by TRACE_EVENT* macros, don't call this directly. |
| 876 // Returns the index in the internal vector of the event if it was added, or | 801 // Returns the index in the internal vector of the event if it was added, or |
| 877 // -1 if the event was not added. | 802 // -1 if the event was not added. |
| 878 // On end events, the return value of the begin event can be specified along | 803 // On end events, the return value of the begin event can be specified along |
| 879 // with a threshold in microseconds. If the elapsed time between begin and end | 804 // with a threshold in microseconds. If the elapsed time between begin and end |
| 880 // is less than the threshold, the begin/end event pair is dropped. | 805 // is less than the threshold, the begin/end event pair is dropped. |
| 881 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied | 806 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied |
| 882 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. | 807 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. |
| 883 int AddTraceEvent(TraceEventPhase phase, | 808 int AddTraceEvent(char phase, |
| 884 const TraceCategory* category, | 809 const unsigned char* category_enabled, |
| 885 const char* name, | 810 const char* name, |
| 886 TraceID id, | 811 unsigned long long id, |
| 887 const char* arg1_name, TraceValue arg1_val, | 812 int num_args, |
| 888 const char* arg2_name, TraceValue arg2_val, | 813 const char** arg_names, |
| 814 const unsigned char* arg_types, | |
| 815 const unsigned long long* arg_values, | |
| 889 int threshold_begin_id, | 816 int threshold_begin_id, |
| 890 int64 threshold, | 817 long long threshold, |
|
jar (doing other things)
2012/01/12 21:48:43
nit: Style guide argues for types like int64. I'm
jbates
2012/01/12 22:22:11
In order to make this API available to third party
| |
| 891 TraceEventFlags flags); | 818 unsigned char flags); |
| 892 static void AddTraceEventEtw(TraceEventPhase phase, | 819 static void AddTraceEventEtw(char phase, |
| 893 const char* name, | 820 const char* name, |
| 894 const void* id, | 821 const void* id, |
| 895 const char* extra); | 822 const char* extra); |
| 896 static void AddTraceEventEtw(TraceEventPhase phase, | 823 static void AddTraceEventEtw(char phase, |
| 897 const char* name, | 824 const char* name, |
| 898 const void* id, | 825 const void* id, |
| 899 const std::string& extra); | 826 const std::string& extra); |
| 900 | 827 |
| 901 // A wrapper around AddTraceEvent used by TRACE_COUNTERx macros | 828 // A wrapper around AddTraceEvent used by TRACE_COUNTERx macros |
| 902 // that allows only integer values for the counters. | 829 // that allows only integer values for the counters. |
| 903 int AddCounterEvent(const TraceCategory* category, | 830 void AddCounterEvent(const unsigned char* category_enabled, |
| 904 const char* name, | 831 const char* name, |
| 905 TraceID id, | 832 unsigned long long id, |
| 906 const char* arg1_name, int32 arg1_val, | 833 const char* arg1_name, int arg1_val, |
| 907 const char* arg2_name, int32 arg2_val, | 834 const char* arg2_name, int arg2_val, |
| 908 TraceEventFlags flags); | 835 unsigned char flags); |
| 909 | 836 |
| 910 // Mangle |id| with a hash based on the process ID so that if |id| occurs on | 837 // Mangle |ptr| with a hash based on the process ID so that if |ptr| occurs on |
| 911 // more than one process, it will not collide. | 838 // more than one process, it will not collide. |
| 912 uint64 GetIntraProcessID(uint64 id) const { return id ^ process_id_hash_; } | 839 unsigned long long GetInterProcessID(void* ptr) const { |
| 840 return static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(ptr)) ^ | |
| 841 process_id_hash_; | |
| 842 } | |
| 913 | 843 |
| 914 int process_id() const { return process_id_; } | 844 int process_id() const { return process_id_; } |
| 915 | 845 |
| 916 // Exposed for unittesting: | 846 // Exposed for unittesting: |
| 917 | 847 |
| 918 // Allows deleting our singleton instance. | 848 // Allows deleting our singleton instance. |
| 919 static void DeleteForTesting(); | 849 static void DeleteForTesting(); |
| 920 | 850 |
| 921 // Allows resurrecting our singleton instance post-AtExit processing. | 851 // Allows resurrecting our singleton instance post-AtExit processing. |
| 922 static void Resurrect(); | 852 static void Resurrect(); |
| 923 | 853 |
| 924 // Allow tests to inspect TraceEvents. | 854 // Allow tests to inspect TraceEvents. |
| 925 size_t GetEventsSize() const { return logged_events_.size(); } | 855 size_t GetEventsSize() const { return logged_events_.size(); } |
| 926 const TraceEvent& GetEventAt(size_t index) const { | 856 const TraceEvent& GetEventAt(size_t index) const { |
| 927 DCHECK(index < logged_events_.size()); | 857 DCHECK(index < logged_events_.size()); |
| 928 return logged_events_[index]; | 858 return logged_events_[index]; |
| 929 } | 859 } |
| 930 | 860 |
| 931 void SetProcessID(int process_id); | 861 void SetProcessID(int process_id); |
| 932 | 862 |
| 933 private: | 863 private: |
| 934 // This allows constructor and destructor to be private and usable only | 864 // This allows constructor and destructor to be private and usable only |
| 935 // by the Singleton class. | 865 // by the Singleton class. |
| 936 friend struct StaticMemorySingletonTraits<TraceLog>; | 866 friend struct StaticMemorySingletonTraits<TraceLog>; |
| 937 | 867 |
| 938 TraceLog(); | 868 TraceLog(); |
| 939 ~TraceLog(); | 869 ~TraceLog(); |
| 940 const TraceCategory* GetCategoryInternal(const char* name); | 870 const unsigned char* GetCategoryEnabledInternal(const char* name); |
| 941 void AddThreadNameMetadataEvents(); | 871 void AddThreadNameMetadataEvents(); |
| 942 void AddClockSyncMetadataEvents(); | 872 void AddClockSyncMetadataEvents(); |
| 943 | 873 |
| 944 // TODO(nduca): switch to per-thread trace buffers to reduce thread | 874 // TODO(nduca): switch to per-thread trace buffers to reduce thread |
| 945 // synchronization. | 875 // synchronization. |
| 946 Lock lock_; | 876 Lock lock_; |
| 947 bool enabled_; | 877 bool enabled_; |
| 948 OutputCallback output_callback_; | 878 OutputCallback output_callback_; |
| 949 BufferFullCallback buffer_full_callback_; | 879 BufferFullCallback buffer_full_callback_; |
| 950 std::vector<TraceEvent> logged_events_; | 880 std::vector<TraceEvent> logged_events_; |
| 951 std::vector<std::string> included_categories_; | 881 std::vector<std::string> included_categories_; |
| 952 std::vector<std::string> excluded_categories_; | 882 std::vector<std::string> excluded_categories_; |
| 953 | 883 |
| 954 base::hash_map<int, std::string> thread_names_; | 884 base::hash_map<int, std::string> thread_names_; |
| 955 | 885 |
| 956 // XORed with TraceID to make it unlikely to collide with other processes. | 886 // XORed with TraceID to make it unlikely to collide with other processes. |
| 957 uint64 process_id_hash_; | 887 unsigned long long process_id_hash_; |
| 958 | 888 |
| 959 int process_id_; | 889 int process_id_; |
| 960 | 890 |
| 961 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 891 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
| 962 }; | 892 }; |
| 963 | 893 |
| 964 namespace internal { | 894 } // namespace debug |
| 895 } // namespace base | |
| 896 | |
| 897 namespace trace_event_internal { | |
| 898 | |
| 899 // Specify these values when the corresponding argument of AddTraceEvent is not | |
| 900 // used. | |
| 901 const int kZeroNumArgs = 0; | |
| 902 const int kNoThreshholdBeginId = -1; | |
| 903 const long long kNoThresholdValue = 0; | |
| 904 const unsigned long long kNoEventId = 0; | |
| 905 | |
| 906 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | |
| 907 // are mangled with the Process ID so that they are unlikely to collide when the | |
| 908 // same pointer is used on different processes. | |
| 909 class BASE_EXPORT TraceID { | |
| 910 public: | |
| 911 explicit TraceID(void* rhs) : | |
| 912 data_(TRACE_EVENT_API_GET_ID_FROM_POINTER(rhs)) {} | |
| 913 explicit TraceID(unsigned long long rhs) : data_(rhs) {} | |
| 914 explicit TraceID(unsigned long rhs) : data_(rhs) {} | |
| 915 explicit TraceID(unsigned int rhs) : data_(rhs) {} | |
| 916 explicit TraceID(unsigned short rhs) : data_(rhs) {} | |
| 917 explicit TraceID(unsigned char rhs) : data_(rhs) {} | |
| 918 explicit TraceID(long long rhs) : | |
| 919 data_(static_cast<unsigned long long>(rhs)) {} | |
| 920 explicit TraceID(long rhs) : data_(static_cast<unsigned long long>(rhs)) {} | |
| 921 explicit TraceID(int rhs) : data_(static_cast<unsigned long long>(rhs)) {} | |
| 922 explicit TraceID(short rhs) : data_(static_cast<unsigned long long>(rhs)) {} | |
| 923 explicit TraceID(signed char rhs) : | |
| 924 data_(static_cast<unsigned long long>(rhs)) {} | |
| 925 | |
| 926 unsigned long long data() const { return data_; } | |
| 927 | |
| 928 private: | |
| 929 unsigned long long data_; | |
| 930 }; | |
| 931 | |
| 932 // Simple union to store various types as unsigned long long. | |
| 933 union TraceValueUnion { | |
| 934 bool as_bool; | |
| 935 unsigned long long as_uint; | |
| 936 long long as_int; | |
| 937 double as_double; | |
| 938 const void* as_pointer; | |
| 939 const char* as_string; | |
| 940 }; | |
| 941 | |
| 942 // Simple container for const char* that should be copied instead of retained. | |
| 943 class TraceStringWithCopy { | |
| 944 public: | |
| 945 explicit TraceStringWithCopy(const char* str) : str_(str) {} | |
| 946 operator const char* () const { return str_; } | |
| 947 private: | |
| 948 const char* str_; | |
| 949 }; | |
| 950 | |
| 951 // Define SetTraceValue for each allowed type. It stores the type and | |
| 952 // value in the return arguments. This allows this API to avoid declaring any | |
| 953 // structures so that it is portable to third_party libraries. | |
| 954 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \ | |
| 955 union_member, \ | |
| 956 value_type_id) \ | |
| 957 static inline void SetTraceValue(actual_type arg, \ | |
| 958 unsigned char* type, \ | |
| 959 unsigned long long* value) { \ | |
| 960 TraceValueUnion type_value; \ | |
| 961 type_value.union_member = arg; \ | |
| 962 *type = value_type_id; \ | |
| 963 *value = type_value.as_uint; \ | |
| 964 } | |
| 965 // Simpler form for int types that can be safely casted. | |
| 966 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \ | |
| 967 value_type_id) \ | |
| 968 static inline void SetTraceValue(actual_type arg, \ | |
| 969 unsigned char* type, \ | |
| 970 unsigned long long* value) { \ | |
| 971 *type = value_type_id; \ | |
| 972 *value = static_cast<unsigned long long>(arg); \ | |
| 973 } | |
| 974 | |
| 975 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT) | |
| 976 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT) | |
| 977 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT) | |
| 978 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT) | |
| 979 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT) | |
| 980 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT) | |
| 981 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT) | |
| 982 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT) | |
| 983 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL) | |
| 984 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE) | |
| 985 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer, | |
| 986 TRACE_VALUE_TYPE_POINTER) | |
| 987 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string, | |
| 988 TRACE_VALUE_TYPE_STRING) | |
| 989 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string, | |
| 990 TRACE_VALUE_TYPE_COPY_STRING) | |
| 991 | |
| 992 #undef INTERNAL_DECLARE_SET_TRACE_VALUE | |
| 993 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT | |
| 994 | |
| 995 // std::string version of SetTraceValue so that trace arguments can be strings. | |
| 996 static inline void SetTraceValue(const std::string& arg, | |
| 997 unsigned char* type, | |
| 998 unsigned long long* value) { | |
| 999 TraceValueUnion type_value; | |
| 1000 type_value.as_string = arg.c_str(); | |
| 1001 *type = TRACE_VALUE_TYPE_COPY_STRING; | |
| 1002 *value = type_value.as_uint; | |
| 1003 } | |
| 1004 | |
| 1005 // These AddTraceEvent template functions are defined here instead of in the | |
| 1006 // macro, because the arg_values could be temporary objects, such as | |
| 1007 // std::string. In order to store pointers to the internal c_str and pass | |
| 1008 // through to the tracing API, the arg_values must live throughout | |
| 1009 // these procedures. | |
| 1010 | |
| 1011 static inline int AddTraceEvent(char phase, | |
| 1012 const unsigned char* category_enabled, | |
| 1013 const char* name, | |
| 1014 unsigned long long id, | |
| 1015 unsigned char flags) { | |
| 1016 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
| 1017 phase, category_enabled, name, id, | |
| 1018 kZeroNumArgs, NULL, NULL, NULL, | |
| 1019 kNoThreshholdBeginId, kNoThresholdValue, flags); | |
| 1020 } | |
| 1021 | |
| 1022 template<class ARG1_TYPE> | |
| 1023 static inline int AddTraceEvent(char phase, | |
| 1024 const unsigned char* category_enabled, | |
| 1025 const char* name, | |
| 1026 unsigned long long id, | |
| 1027 unsigned char flags, | |
| 1028 const char* arg1_name, | |
| 1029 const ARG1_TYPE& arg1_val) { | |
| 1030 const int num_args = 1; | |
| 1031 unsigned char arg_types[1]; | |
| 1032 unsigned long long arg_values[1]; | |
| 1033 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | |
| 1034 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
| 1035 phase, category_enabled, name, id, | |
| 1036 num_args, &arg1_name, arg_types, arg_values, | |
| 1037 kNoThreshholdBeginId, kNoThresholdValue, flags); | |
| 1038 } | |
| 1039 | |
| 1040 template<class ARG1_TYPE, class ARG2_TYPE> | |
| 1041 static inline int AddTraceEvent(char phase, | |
| 1042 const unsigned char* category_enabled, | |
| 1043 const char* name, | |
| 1044 unsigned long long id, | |
| 1045 unsigned char flags, | |
| 1046 const char* arg1_name, | |
| 1047 const ARG1_TYPE& arg1_val, | |
| 1048 const char* arg2_name, | |
| 1049 const ARG2_TYPE& arg2_val) { | |
| 1050 const int num_args = 2; | |
| 1051 const char* arg_names[2] = { arg1_name, arg2_name }; | |
| 1052 unsigned char arg_types[2]; | |
| 1053 unsigned long long arg_values[2]; | |
| 1054 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | |
| 1055 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); | |
| 1056 return TRACE_EVENT_API_ADD_TRACE_EVENT( | |
| 1057 phase, category_enabled, name, id, | |
| 1058 num_args, arg_names, arg_types, arg_values, | |
| 1059 kNoThreshholdBeginId, kNoThresholdValue, flags); | |
| 1060 } | |
| 965 | 1061 |
| 966 // Used by TRACE_EVENTx macro. Do not use directly. | 1062 // Used by TRACE_EVENTx macro. Do not use directly. |
| 967 class BASE_EXPORT TraceEndOnScopeClose { | 1063 class BASE_EXPORT TraceEndOnScopeClose { |
| 968 public: | 1064 public: |
| 969 // Note: members of data_ intentionally left uninitialized. See Initialize. | 1065 // Note: members of data_ intentionally left uninitialized. See Initialize. |
| 970 TraceEndOnScopeClose() : p_data_(NULL) {} | 1066 TraceEndOnScopeClose() : p_data_(NULL) {} |
| 971 ~TraceEndOnScopeClose() { | 1067 ~TraceEndOnScopeClose() { |
| 972 if (p_data_) | 1068 if (p_data_) |
| 973 AddEventIfEnabled(); | 1069 AddEventIfEnabled(); |
| 974 } | 1070 } |
| 975 | 1071 |
| 976 void Initialize(const TraceCategory* category, | 1072 void Initialize(const unsigned char* category_enabled, |
| 977 const char* name); | 1073 const char* name); |
| 978 | 1074 |
| 979 private: | 1075 private: |
| 980 // Add the end event if the category is still enabled. | 1076 // Add the end event if the category is still enabled. |
| 981 void AddEventIfEnabled(); | 1077 void AddEventIfEnabled(); |
| 982 | 1078 |
| 983 // This Data struct workaround is to avoid initializing all the members | 1079 // This Data struct workaround is to avoid initializing all the members |
| 984 // in Data during construction of this object, since this object is always | 1080 // in Data during construction of this object, since this object is always |
| 985 // constructed, even when tracing is disabled. If the members of Data were | 1081 // constructed, even when tracing is disabled. If the members of Data were |
| 986 // members of this class instead, compiler warnings occur about potential | 1082 // members of this class instead, compiler warnings occur about potential |
| 987 // uninitialized accesses. | 1083 // uninitialized accesses. |
| 988 struct Data { | 1084 struct Data { |
| 989 const TraceCategory* category; | 1085 const unsigned char* category_enabled; |
| 990 const char* name; | 1086 const char* name; |
| 991 }; | 1087 }; |
| 992 Data* p_data_; | 1088 Data* p_data_; |
| 993 Data data_; | 1089 Data data_; |
| 994 }; | 1090 }; |
| 995 | 1091 |
| 996 // Used by TRACE_EVENTx macro. Do not use directly. | 1092 // Used by TRACE_EVENTx macro. Do not use directly. |
| 997 class BASE_EXPORT TraceEndOnScopeCloseThreshold { | 1093 class BASE_EXPORT TraceEndOnScopeCloseThreshold { |
| 998 public: | 1094 public: |
| 999 // Note: members of data_ intentionally left uninitialized. See Initialize. | 1095 // Note: members of data_ intentionally left uninitialized. See Initialize. |
| 1000 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {} | 1096 TraceEndOnScopeCloseThreshold() : p_data_(NULL) {} |
| 1001 ~TraceEndOnScopeCloseThreshold() { | 1097 ~TraceEndOnScopeCloseThreshold() { |
| 1002 if (p_data_) | 1098 if (p_data_) |
| 1003 AddEventIfEnabled(); | 1099 AddEventIfEnabled(); |
| 1004 } | 1100 } |
| 1005 | 1101 |
| 1006 // Called by macros only when tracing is enabled at the point when the begin | 1102 // Called by macros only when tracing is enabled at the point when the begin |
| 1007 // event is added. | 1103 // event is added. |
| 1008 void Initialize(const TraceCategory* category, | 1104 void Initialize(const unsigned char* category_enabled, |
| 1009 const char* name, | 1105 const char* name, |
| 1010 int threshold_begin_id, | 1106 int threshold_begin_id, |
| 1011 int64 threshold); | 1107 long long threshold); |
| 1012 | 1108 |
| 1013 private: | 1109 private: |
| 1014 // Add the end event if the category is still enabled. | 1110 // Add the end event if the category is still enabled. |
| 1015 void AddEventIfEnabled(); | 1111 void AddEventIfEnabled(); |
| 1016 | 1112 |
| 1017 // This Data struct workaround is to avoid initializing all the members | 1113 // This Data struct workaround is to avoid initializing all the members |
| 1018 // in Data during construction of this object, since this object is always | 1114 // in Data during construction of this object, since this object is always |
| 1019 // constructed, even when tracing is disabled. If the members of Data were | 1115 // constructed, even when tracing is disabled. If the members of Data were |
| 1020 // members of this class instead, compiler warnings occur about potential | 1116 // members of this class instead, compiler warnings occur about potential |
| 1021 // uninitialized accesses. | 1117 // uninitialized accesses. |
| 1022 struct Data { | 1118 struct Data { |
| 1023 int64 threshold; | 1119 long long threshold; |
| 1024 const TraceCategory* category; | 1120 const unsigned char* category_enabled; |
| 1025 const char* name; | 1121 const char* name; |
| 1026 int threshold_begin_id; | 1122 int threshold_begin_id; |
| 1027 }; | 1123 }; |
| 1028 Data* p_data_; | 1124 Data* p_data_; |
| 1029 Data data_; | 1125 Data data_; |
| 1030 }; | 1126 }; |
| 1031 | 1127 |
| 1032 } // namespace internal | 1128 } // namespace trace_event_internal |
| 1033 | |
| 1034 } // namespace debug | |
| 1035 } // namespace base | |
| 1036 | 1129 |
| 1037 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 1130 #endif // BASE_DEBUG_TRACE_EVENT_H_ |
| OLD | NEW |