| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This header is designed to give you trace_event macros without specifying |
| 6 // how the events actually get collected and stored. If you need to expose trace |
| 7 // event to some other universe, you can copy-and-paste this file, |
| 8 // implement the TRACE_EVENT_API macros, and do any other necessary fixup for |
| 9 // the target platform. The end result is that multiple libraries can funnel |
| 10 // events through to a shared trace event collector. |
| 11 |
| 5 // Trace events are for tracking application performance and resource usage. | 12 // Trace events are for tracking application performance and resource usage. |
| 6 // Macros are provided to track: | 13 // Macros are provided to track: |
| 7 // Begin and end of function calls | 14 // Begin and end of function calls |
| 8 // Counters | 15 // Counters |
| 9 // | 16 // |
| 10 // Events are issued against categories. Whereas LOG's | 17 // Events are issued against categories. Whereas LOG's |
| 11 // categories are statically defined, TRACE categories are created | 18 // categories are statically defined, TRACE categories are created |
| 12 // implicitly with a string. For example: | 19 // implicitly with a string. For example: |
| 13 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") | 20 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") |
| 14 // | 21 // |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 // and resolving the category. | 147 // and resolving the category. |
| 141 // | 148 // |
| 142 // ANNOTATE_BENIGN_RACE is used to suppress the warning on the static category | 149 // ANNOTATE_BENIGN_RACE is used to suppress the warning on the static category |
| 143 // pointers. | 150 // pointers. |
| 144 | 151 |
| 145 | 152 |
| 146 #ifndef BASE_DEBUG_TRACE_EVENT_H_ | 153 #ifndef BASE_DEBUG_TRACE_EVENT_H_ |
| 147 #define BASE_DEBUG_TRACE_EVENT_H_ | 154 #define BASE_DEBUG_TRACE_EVENT_H_ |
| 148 #pragma once | 155 #pragma once |
| 149 | 156 |
| 157 #include <string> |
| 158 |
| 150 #include "build/build_config.h" | 159 #include "build/build_config.h" |
| 151 | 160 #include "base/debug/trace_event_impl.h" |
| 152 #include <string> | |
| 153 #include <vector> | |
| 154 | |
| 155 #include "base/callback.h" | |
| 156 #include "base/hash_tables.h" | |
| 157 #include "base/memory/ref_counted_memory.h" | |
| 158 #include "base/string_util.h" | |
| 159 #include "base/synchronization/lock.h" | |
| 160 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
| 161 #include "base/timer.h" | |
| 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, | |
| 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 | 161 |
| 209 // By default, const char* argument values are assumed to have long-lived scope | 162 // By default, const char* argument values are assumed to have long-lived scope |
| 210 // and will not be copied. Use this macro to force a const char* to be copied. | 163 // and will not be copied. Use this macro to force a const char* to be copied. |
| 211 #define TRACE_STR_COPY(str) \ | 164 #define TRACE_STR_COPY(str) \ |
| 212 trace_event_internal::TraceStringWithCopy(str) | 165 trace_event_internal::TraceStringWithCopy(str) |
| 213 | 166 |
| 214 // Older style trace macros with explicit id and extra data | |
| 215 // Only these macros result in publishing data to ETW as currently implemented. | |
| 216 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | |
| 217 base::debug::TraceLog::AddTraceEventEtw( \ | |
| 218 TRACE_EVENT_PHASE_BEGIN, \ | |
| 219 name, reinterpret_cast<const void*>(id), extra) | |
| 220 | |
| 221 #define TRACE_EVENT_END_ETW(name, id, extra) \ | |
| 222 base::debug::TraceLog::AddTraceEventEtw( \ | |
| 223 TRACE_EVENT_PHASE_END, \ | |
| 224 name, reinterpret_cast<const void*>(id), extra) | |
| 225 | |
| 226 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \ | |
| 227 base::debug::TraceLog::AddTraceEventEtw( \ | |
| 228 TRACE_EVENT_PHASE_INSTANT, \ | |
| 229 name, reinterpret_cast<const void*>(id), extra) | |
| 230 | |
| 231 // Records a pair of begin and end events called "name" for the current | 167 // Records a pair of begin and end events called "name" for the current |
| 232 // scope, with 0, 1 or 2 associated arguments. If the category is not | 168 // scope, with 0, 1 or 2 associated arguments. If the category is not |
| 233 // enabled, then this does nothing. | 169 // enabled, then this does nothing. |
| 234 // - category and name strings must have application lifetime (statics or | 170 // - category and name strings must have application lifetime (statics or |
| 235 // literals). They may not include " chars. | 171 // literals). They may not include " chars. |
| 236 #define TRACE_EVENT0(category, name) \ | 172 #define TRACE_EVENT0(category, name) \ |
| 237 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) | 173 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) |
| 238 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | 174 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ |
| 239 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) | 175 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) |
| 240 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 176 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 #define TRACE_EVENT_IF_LONGER_THAN2( \ | 306 #define TRACE_EVENT_IF_LONGER_THAN2( \ |
| 371 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 307 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
| 372 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ | 308 INTERNAL_TRACE_EVENT_ADD_SCOPED_IF_LONGER_THAN( \ |
| 373 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) | 309 threshold_us, category, name, arg1_name, arg1_val, arg2_name, arg2_val) |
| 374 | 310 |
| 375 // Records the value of a counter called "name" immediately. Value | 311 // Records the value of a counter called "name" immediately. Value |
| 376 // must be representable as a 32 bit integer. | 312 // must be representable as a 32 bit integer. |
| 377 // - category and name strings must have application lifetime (statics or | 313 // - category and name strings must have application lifetime (statics or |
| 378 // literals). They may not include " chars. | 314 // literals). They may not include " chars. |
| 379 #define TRACE_COUNTER1(category, name, value) \ | 315 #define TRACE_COUNTER1(category, name, value) \ |
| 380 TRACE_COUNTER2(category, name, "value", value, NULL, 0) | 316 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ |
| 317 category, name, TRACE_EVENT_FLAG_NONE, \ |
| 318 "value", static_cast<int>(value)) |
| 381 #define TRACE_COPY_COUNTER1(category, name, value) \ | 319 #define TRACE_COPY_COUNTER1(category, name, value) \ |
| 382 TRACE_COPY_COUNTER2(category, name, "value", value, NULL, 0) | 320 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ |
| 321 category, name, TRACE_EVENT_FLAG_COPY, \ |
| 322 "value", static_cast<int>(value)) |
| 383 | 323 |
| 384 // Records the values of a multi-parted counter called "name" immediately. | 324 // Records the values of a multi-parted counter called "name" immediately. |
| 385 // The UI will treat value1 and value2 as parts of a whole, displaying their | 325 // The UI will treat value1 and value2 as parts of a whole, displaying their |
| 386 // values as a stacked-bar chart. | 326 // values as a stacked-bar chart. |
| 387 // - category and name strings must have application lifetime (statics or | 327 // - category and name strings must have application lifetime (statics or |
| 388 // literals). They may not include " chars. | 328 // literals). They may not include " chars. |
| 389 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ | 329 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ |
| 390 value2_name, value2_val) \ | 330 value2_name, value2_val) \ |
| 391 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 331 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ |
| 392 category, name, trace_event_internal::kNoEventId, \ | 332 category, name, TRACE_EVENT_FLAG_NONE, \ |
| 393 value1_name, value1_val, value2_name, value2_val, \ | 333 value1_name, static_cast<int>(value1_val), \ |
| 394 TRACE_EVENT_FLAG_NONE) | 334 value2_name, static_cast<int>(value2_val)) |
| 395 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ | 335 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ |
| 396 value2_name, value2_val) \ | 336 value2_name, value2_val) \ |
| 397 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 337 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ |
| 398 category, name, trace_event_internal::kNoEventId, \ | 338 category, name, TRACE_EVENT_FLAG_COPY, \ |
| 399 value1_name, value1_val, value2_name, value2_val, \ | 339 value1_name, static_cast<int>(value1_val), \ |
| 400 TRACE_EVENT_FLAG_COPY) | 340 value2_name, static_cast<int>(value2_val)) |
| 401 | 341 |
| 402 // Records the value of a counter called "name" immediately. Value | 342 // Records the value of a counter called "name" immediately. Value |
| 403 // must be representable as a 32 bit integer. | 343 // must be representable as a 32 bit integer. |
| 404 // - category and name strings must have application lifetime (statics or | 344 // - category and name strings must have application lifetime (statics or |
| 405 // literals). They may not include " chars. | 345 // literals). They may not include " chars. |
| 406 // - |id| is used to disambiguate counters with the same name. It must either | 346 // - |id| is used to disambiguate counters with the same name. It must either |
| 407 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | 347 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits |
| 408 // will be xored with a hash of the process ID so that the same pointer on | 348 // will be xored with a hash of the process ID so that the same pointer on |
| 409 // two different processes will not collide. | 349 // two different processes will not collide. |
| 410 #define TRACE_COUNTER_ID1(category, name, id, value) \ | 350 #define TRACE_COUNTER_ID1(category, name, id, value) \ |
| 411 TRACE_COUNTER_ID2(category, name, id, "value", value, NULL, 0) | 351 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ |
| 352 category, name, id, TRACE_EVENT_FLAG_HAS_ID, \ |
| 353 "value", static_cast<int>(value)) |
| 412 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \ | 354 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \ |
| 413 TRACE_COPY_COUNTER_ID2(category, name, id, "value", value, NULL, 0) | 355 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ |
| 356 category, name, id, TRACE_EVENT_FLAG_COPY | TRACE_EVENT_FLAG_HAS_ID, \ |
| 357 "value", static_cast<int>(value)) |
| 414 | 358 |
| 415 // Records the values of a multi-parted counter called "name" immediately. | 359 // Records the values of a multi-parted counter called "name" immediately. |
| 416 // The UI will treat value1 and value2 as parts of a whole, displaying their | 360 // The UI will treat value1 and value2 as parts of a whole, displaying their |
| 417 // values as a stacked-bar chart. | 361 // values as a stacked-bar chart. |
| 418 // - category and name strings must have application lifetime (statics or | 362 // - category and name strings must have application lifetime (statics or |
| 419 // literals). They may not include " chars. | 363 // literals). They may not include " chars. |
| 420 // - |id| is used to disambiguate counters with the same name. It must either | 364 // - |id| is used to disambiguate counters with the same name. It must either |
| 421 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | 365 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits |
| 422 // will be xored with a hash of the process ID so that the same pointer on | 366 // will be xored with a hash of the process ID so that the same pointer on |
| 423 // two different processes will not collide. | 367 // two different processes will not collide. |
| 424 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \ | 368 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \ |
| 425 value2_name, value2_val) \ | 369 value2_name, value2_val) \ |
| 426 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 370 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ |
| 427 category, name, id, value1_name, value1_val, value2_name, value2_val, \ | 371 category, name, id, TRACE_EVENT_FLAG_HAS_ID, \ |
| 428 TRACE_EVENT_FLAG_HAS_ID) | 372 value1_name, static_cast<int>(value1_val), \ |
| 373 value2_name, static_cast<int>(value2_val)) |
| 429 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \ | 374 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \ |
| 430 value2_name, value2_val) \ | 375 value2_name, value2_val) \ |
| 431 INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | 376 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ |
| 432 category, name, id, \ | 377 category, name, id, TRACE_EVENT_FLAG_COPY | TRACE_EVENT_FLAG_HAS_ID, \ |
| 433 value1_name, value1_val, \ | 378 value1_name, static_cast<int>(value1_val), \ |
| 434 value2_name, value2_val, \ | 379 value2_name, static_cast<int>(value2_val)) |
| 435 TRACE_EVENT_FLAG_COPY|TRACE_EVENT_FLAG_HAS_ID) | |
| 436 | 380 |
| 437 | 381 |
| 438 // Records a single START event called "name" immediately, with 0, 1 or 2 | 382 // Records a single START event called "name" immediately, with 0, 1 or 2 |
| 439 // associated arguments. If the category is not enabled, then this | 383 // associated arguments. If the category is not enabled, then this |
| 440 // does nothing. | 384 // does nothing. |
| 441 // - category and name strings must have application lifetime (statics or | 385 // - category and name strings must have application lifetime (statics or |
| 442 // literals). They may not include " chars. | 386 // literals). They may not include " chars. |
| 443 // - |id| is used to match the START event with the FINISH event. It must either | 387 // - |id| is used to match the START event with the FINISH event. It must either |
| 444 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | 388 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits |
| 445 // will be xored with a hash of the process ID so that the same pointer on | 389 // will be xored with a hash of the process ID so that the same pointer on |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ | 432 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 489 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ | 433 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
| 490 arg1_name, arg1_val) | 434 arg1_name, arg1_val) |
| 491 #define TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, \ | 435 #define TRACE_EVENT_COPY_FINISH2(category, name, id, arg1_name, arg1_val, \ |
| 492 arg2_name, arg2_val) \ | 436 arg2_name, arg2_val) \ |
| 493 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ | 437 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FINISH, \ |
| 494 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ | 438 category, name, id, TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_COPY, \ |
| 495 arg1_name, arg1_val, arg2_name, arg2_val) | 439 arg1_name, arg1_val, arg2_name, arg2_val) |
| 496 | 440 |
| 497 | 441 |
| 442 //////////////////////////////////////////////////////////////////////////////// |
| 443 // Implementation specific tracing API definitions. |
| 444 |
| 445 // const unsigned char* |
| 446 // TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name) |
| 447 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \ |
| 448 base::debug::TraceLog::GetCategoryEnabled |
| 449 |
| 450 // Returns the threshold_begin_id used by TRACE_IF_LONGER_THAN macros. |
| 451 // int TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 452 // char phase, |
| 453 // const unsigned char* category_enabled, |
| 454 // const char* name, |
| 455 // unsigned long long id, |
| 456 // int num_args, |
| 457 // const char** arg_names, |
| 458 // const unsigned char* arg_types, |
| 459 // const unsigned long long* arg_values, |
| 460 // int threshold_begin_id, |
| 461 // long long threshold, |
| 462 // unsigned char flags) |
| 463 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ |
| 464 base::debug::TraceLog::GetInstance()->AddTraceEvent |
| 465 |
| 466 // void TRACE_EVENT_API_ADD_COUNTER_EVENT( |
| 467 // const unsigned char* category_enabled, |
| 468 // const char* name, |
| 469 // unsigned long long id, |
| 470 // const char* arg1_name, int arg1_val, |
| 471 // const char* arg2_name, int arg2_val, |
| 472 // unsigned char flags) |
| 473 #define TRACE_EVENT_API_ADD_COUNTER_EVENT \ |
| 474 base::debug::TraceLog::GetInstance()->AddCounterEvent |
| 475 |
| 476 // Mangle |pointer| with a process ID hash so that if |pointer| occurs on more |
| 477 // than one process, it will not collide in the trace data. |
| 478 // unsigned long long TRACE_EVENT_API_GET_ID_FROM_POINTER(void* pointer) |
| 479 #define TRACE_EVENT_API_GET_ID_FROM_POINTER \ |
| 480 base::debug::TraceLog::GetInstance()->GetInterProcessID |
| 481 |
| 482 //////////////////////////////////////////////////////////////////////////////// |
| 483 |
| 498 // Implementation detail: trace event macros create temporary variables | 484 // Implementation detail: trace event macros create temporary variables |
| 499 // to keep instrumentation overhead low. These macros give each temporary | 485 // to keep instrumentation overhead low. These macros give each temporary |
| 500 // variable a unique name based on the line number to prevent name collissions. | 486 // variable a unique name based on the line number to prevent name collissions. |
| 501 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ | 487 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ |
| 502 trace_event_unique_##a##b | 488 trace_event_unique_##a##b |
| 503 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ | 489 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ |
| 504 INTERNAL_TRACE_EVENT_UID3(a,b) | 490 INTERNAL_TRACE_EVENT_UID3(a,b) |
| 505 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ | 491 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ |
| 506 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) | 492 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) |
| 507 | 493 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 518 // Implementation detail: internal macro to create static category and add | 504 // Implementation detail: internal macro to create static category and add |
| 519 // event if the category is enabled. | 505 // event if the category is enabled. |
| 520 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ | 506 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ |
| 521 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 507 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
| 522 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | 508 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
| 523 trace_event_internal::AddTraceEvent( \ | 509 trace_event_internal::AddTraceEvent( \ |
| 524 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \ | 510 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \ |
| 525 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \ | 511 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \ |
| 526 } | 512 } |
| 527 | 513 |
| 528 // Implementation detail: internal macro to create static category and | |
| 529 // add the counter event if it is enabled. | |
| 530 #define INTERNAL_TRACE_EVENT_ADD_COUNTER( \ | |
| 531 category, name, id, arg1_name, arg1_val, arg2_name, arg2_val, flags) \ | |
| 532 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
| 533 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
| 534 TRACE_EVENT_API_ADD_COUNTER_EVENT( \ | |
| 535 INTERNAL_TRACE_EVENT_UID(catstatic), \ | |
| 536 name, trace_event_internal::TraceID(id).data(), \ | |
| 537 arg1_name, arg1_val, arg2_name, arg2_val, flags); \ | |
| 538 } | |
| 539 | |
| 540 // Implementation detail: internal macro to create static category and add begin | 514 // Implementation detail: internal macro to create static category and add begin |
| 541 // event if the category is enabled. Also adds the end event when the scope | 515 // event if the category is enabled. Also adds the end event when the scope |
| 542 // ends. | 516 // ends. |
| 543 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \ | 517 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \ |
| 544 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 518 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
| 545 trace_event_internal::TraceEndOnScopeClose \ | 519 trace_event_internal::TraceEndOnScopeClose \ |
| 546 INTERNAL_TRACE_EVENT_UID(profileScope); \ | 520 INTERNAL_TRACE_EVENT_UID(profileScope); \ |
| 547 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | 521 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
| 548 trace_event_internal::AddTraceEvent( \ | 522 trace_event_internal::AddTraceEvent( \ |
| 549 TRACE_EVENT_PHASE_BEGIN, \ | 523 TRACE_EVENT_PHASE_BEGIN, \ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 579 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ | 553 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ |
| 580 ...) \ | 554 ...) \ |
| 581 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | 555 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ |
| 582 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | 556 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ |
| 583 trace_event_internal::AddTraceEvent( \ | 557 trace_event_internal::AddTraceEvent( \ |
| 584 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ | 558 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ |
| 585 name, trace_event_internal::TraceID(id).data(), flags, \ | 559 name, trace_event_internal::TraceID(id).data(), flags, \ |
| 586 ##__VA_ARGS__); \ | 560 ##__VA_ARGS__); \ |
| 587 } | 561 } |
| 588 | 562 |
| 589 template <typename Type> | |
| 590 struct StaticMemorySingletonTraits; | |
| 591 | |
| 592 namespace base { | |
| 593 | |
| 594 class RefCountedString; | |
| 595 | |
| 596 namespace debug { | |
| 597 | |
| 598 const int kTraceMaxNumArgs = 2; | |
| 599 | |
| 600 // Notes regarding the following definitions: | 563 // Notes regarding the following definitions: |
| 601 // New values can be added and propagated to third party libraries, but existing | 564 // 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 | 565 // definitions must never be changed, because third party libraries may use old |
| 603 // definitions. | 566 // definitions. |
| 604 | 567 |
| 605 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. | 568 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. |
| 606 #define TRACE_EVENT_PHASE_BEGIN ('B') | 569 #define TRACE_EVENT_PHASE_BEGIN ('B') |
| 607 #define TRACE_EVENT_PHASE_END ('E') | 570 #define TRACE_EVENT_PHASE_END ('E') |
| 608 #define TRACE_EVENT_PHASE_INSTANT ('I') | 571 #define TRACE_EVENT_PHASE_INSTANT ('I') |
| 609 #define TRACE_EVENT_PHASE_START ('S') | 572 #define TRACE_EVENT_PHASE_START ('S') |
| 610 #define TRACE_EVENT_PHASE_FINISH ('F') | 573 #define TRACE_EVENT_PHASE_FINISH ('F') |
| 611 #define TRACE_EVENT_PHASE_METADATA ('M') | 574 #define TRACE_EVENT_PHASE_METADATA ('M') |
| 612 #define TRACE_EVENT_PHASE_COUNTER ('C') | 575 #define TRACE_EVENT_PHASE_COUNTER ('C') |
| 613 | 576 |
| 614 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. | 577 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. |
| 615 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) | 578 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) |
| 616 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1<<0)) | 579 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1<<0)) |
| 617 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1<<1)) | 580 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1<<1)) |
| 618 | 581 |
| 619 // Type values for identifying types in the TraceValue union. | 582 // Type values for identifying types in the TraceValue union. |
| 620 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) | 583 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) |
| 621 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) | 584 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) |
| 622 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) | 585 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) |
| 623 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) | 586 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) |
| 624 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) | 587 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) |
| 625 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) | 588 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) |
| 626 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) | 589 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) |
| 627 | 590 |
| 628 // Output records are "Events" and can be obtained via the | |
| 629 // OutputCallback whenever the tracing system decides to flush. This | |
| 630 // can happen at any time, on any thread, or you can programatically | |
| 631 // force it to happen. | |
| 632 class BASE_EXPORT TraceEvent { | |
| 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 | |
| 643 TraceEvent(); | |
| 644 TraceEvent(int thread_id, | |
| 645 TimeTicks timestamp, | |
| 646 char phase, | |
| 647 const unsigned char* category_enabled, | |
| 648 const char* name, | |
| 649 unsigned long long id, | |
| 650 int num_args, | |
| 651 const char** arg_names, | |
| 652 const unsigned char* arg_types, | |
| 653 const unsigned long long* arg_values, | |
| 654 unsigned char flags); | |
| 655 ~TraceEvent(); | |
| 656 | |
| 657 // Serialize event data to JSON | |
| 658 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, | |
| 659 size_t start, | |
| 660 size_t count, | |
| 661 std::string* out); | |
| 662 void AppendAsJSON(std::string* out) const; | |
| 663 | |
| 664 TimeTicks timestamp() const { return timestamp_; } | |
| 665 | |
| 666 // Exposed for unittesting: | |
| 667 | |
| 668 const base::RefCountedString* parameter_copy_storage() const { | |
| 669 return parameter_copy_storage_.get(); | |
| 670 } | |
| 671 | |
| 672 const char* name() const { return name_; } | |
| 673 | |
| 674 private: | |
| 675 // Note: these are ordered by size (largest first) for optimal packing. | |
| 676 TimeTicks timestamp_; | |
| 677 // id_ can be used to store phase-specific data. | |
| 678 unsigned long long id_; | |
| 679 TraceValue arg_values_[kTraceMaxNumArgs]; | |
| 680 const char* arg_names_[kTraceMaxNumArgs]; | |
| 681 const unsigned char* category_enabled_; | |
| 682 const char* name_; | |
| 683 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | |
| 684 int thread_id_; | |
| 685 char phase_; | |
| 686 unsigned char flags_; | |
| 687 unsigned char arg_types_[kTraceMaxNumArgs]; | |
| 688 }; | |
| 689 | |
| 690 | |
| 691 // TraceResultBuffer collects and converts trace fragments returned by TraceLog | |
| 692 // to JSON output. | |
| 693 class BASE_EXPORT TraceResultBuffer { | |
| 694 public: | |
| 695 typedef base::Callback<void(const std::string&)> OutputCallback; | |
| 696 | |
| 697 // If you don't need to stream JSON chunks out efficiently, and just want to | |
| 698 // get a complete JSON string after calling Finish, use this struct to collect | |
| 699 // JSON trace output. | |
| 700 struct BASE_EXPORT SimpleOutput { | |
| 701 OutputCallback GetCallback(); | |
| 702 void Append(const std::string& json_string); | |
| 703 | |
| 704 // Do what you want with the json_output_ string after calling | |
| 705 // TraceResultBuffer::Finish. | |
| 706 std::string json_output; | |
| 707 }; | |
| 708 | |
| 709 TraceResultBuffer(); | |
| 710 ~TraceResultBuffer(); | |
| 711 | |
| 712 // Set callback. The callback will be called during Start with the initial | |
| 713 // JSON output and during AddFragment and Finish with following JSON output | |
| 714 // chunks. The callback target must live past the last calls to | |
| 715 // TraceResultBuffer::Start/AddFragment/Finish. | |
| 716 void SetOutputCallback(const OutputCallback& json_chunk_callback); | |
| 717 | |
| 718 // Start JSON output. This resets all internal state, so you can reuse | |
| 719 // the TraceResultBuffer by calling Start. | |
| 720 void Start(); | |
| 721 | |
| 722 // Call AddFragment 0 or more times to add trace fragments from TraceLog. | |
| 723 void AddFragment(const std::string& trace_fragment); | |
| 724 | |
| 725 // When all fragments have been added, call Finish to complete the JSON | |
| 726 // formatted output. | |
| 727 void Finish(); | |
| 728 | |
| 729 private: | |
| 730 OutputCallback output_callback_; | |
| 731 bool append_comma_; | |
| 732 }; | |
| 733 | |
| 734 | |
| 735 class BASE_EXPORT TraceLog { | |
| 736 public: | |
| 737 static TraceLog* GetInstance(); | |
| 738 | |
| 739 // Get set of known categories. This can change as new code paths are reached. | |
| 740 // The known categories are inserted into |categories|. | |
| 741 void GetKnownCategories(std::vector<std::string>* categories); | |
| 742 | |
| 743 // Enable tracing for provided list of categories. If tracing is already | |
| 744 // enabled, this method does nothing -- changing categories during trace is | |
| 745 // not supported. | |
| 746 // If both included_categories and excluded_categories are empty, | |
| 747 // all categories are traced. | |
| 748 // Else if included_categories is non-empty, only those are traced. | |
| 749 // Else if excluded_categories is non-empty, everything but those are traced. | |
| 750 // Wildcards * and ? are supported (see MatchPattern in string_util.h). | |
| 751 void SetEnabled(const std::vector<std::string>& included_categories, | |
| 752 const std::vector<std::string>& excluded_categories); | |
| 753 | |
| 754 // |categories| is a comma-delimited list of category wildcards. | |
| 755 // A category can have an optional '-' prefix to make it an excluded category. | |
| 756 // All the same rules apply above, so for example, having both included and | |
| 757 // excluded categories in the same list would not be supported. | |
| 758 // | |
| 759 // Example: SetEnabled("test_MyTest*"); | |
| 760 // Example: SetEnabled("test_MyTest*,test_OtherStuff"); | |
| 761 // Example: SetEnabled("-excluded_category1,-excluded_category2"); | |
| 762 void SetEnabled(const std::string& categories); | |
| 763 | |
| 764 // Retieves the categories set via a prior call to SetEnabled(). Only | |
| 765 // meaningful if |IsEnabled()| is true. | |
| 766 void GetEnabledTraceCategories(std::vector<std::string>* included_out, | |
| 767 std::vector<std::string>* excluded_out); | |
| 768 | |
| 769 // Disable tracing for all categories. | |
| 770 void SetDisabled(); | |
| 771 // Helper method to enable/disable tracing for all categories. | |
| 772 void SetEnabled(bool enabled); | |
| 773 bool IsEnabled() { return enabled_; } | |
| 774 | |
| 775 float GetBufferPercentFull() const; | |
| 776 | |
| 777 // When enough events are collected, they are handed (in bulk) to | |
| 778 // the output callback. If no callback is set, the output will be | |
| 779 // silently dropped. The callback must be thread safe. The string format is | |
| 780 // undefined. Use TraceResultBuffer to convert one or more trace strings to | |
| 781 // JSON. | |
| 782 typedef RefCountedData<std::string> RefCountedString; | |
| 783 typedef base::Callback<void(const scoped_refptr<RefCountedString>&)> | |
| 784 OutputCallback; | |
| 785 void SetOutputCallback(const OutputCallback& cb); | |
| 786 | |
| 787 // The trace buffer does not flush dynamically, so when it fills up, | |
| 788 // subsequent trace events will be dropped. This callback is generated when | |
| 789 // the trace buffer is full. The callback must be thread safe. | |
| 790 typedef base::Callback<void(void)> BufferFullCallback; | |
| 791 void SetBufferFullCallback(const BufferFullCallback& cb); | |
| 792 | |
| 793 // Flushes all logged data to the callback. | |
| 794 void Flush(); | |
| 795 | |
| 796 // Called by TRACE_EVENT* macros, don't call this directly. | |
| 797 static const unsigned char* GetCategoryEnabled(const char* name); | |
| 798 static const char* GetCategoryName(const unsigned char* category_enabled); | |
| 799 | |
| 800 // Called by TRACE_EVENT* macros, don't call this directly. | |
| 801 // Returns the index in the internal vector of the event if it was added, or | |
| 802 // -1 if the event was not added. | |
| 803 // On end events, the return value of the begin event can be specified along | |
| 804 // with a threshold in microseconds. If the elapsed time between begin and end | |
| 805 // is less than the threshold, the begin/end event pair is dropped. | |
| 806 // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied | |
| 807 // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above. | |
| 808 int AddTraceEvent(char phase, | |
| 809 const unsigned char* category_enabled, | |
| 810 const char* name, | |
| 811 unsigned long long id, | |
| 812 int num_args, | |
| 813 const char** arg_names, | |
| 814 const unsigned char* arg_types, | |
| 815 const unsigned long long* arg_values, | |
| 816 int threshold_begin_id, | |
| 817 long long threshold, | |
| 818 unsigned char flags); | |
| 819 static void AddTraceEventEtw(char phase, | |
| 820 const char* name, | |
| 821 const void* id, | |
| 822 const char* extra); | |
| 823 static void AddTraceEventEtw(char phase, | |
| 824 const char* name, | |
| 825 const void* id, | |
| 826 const std::string& extra); | |
| 827 | |
| 828 // A wrapper around AddTraceEvent used by TRACE_COUNTERx macros | |
| 829 // that allows only integer values for the counters. | |
| 830 void AddCounterEvent(const unsigned char* category_enabled, | |
| 831 const char* name, | |
| 832 unsigned long long id, | |
| 833 const char* arg1_name, int arg1_val, | |
| 834 const char* arg2_name, int arg2_val, | |
| 835 unsigned char flags); | |
| 836 | |
| 837 // Mangle |ptr| with a hash based on the process ID so that if |ptr| occurs on | |
| 838 // more than one process, it will not collide. | |
| 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 } | |
| 843 | |
| 844 int process_id() const { return process_id_; } | |
| 845 | |
| 846 // Exposed for unittesting: | |
| 847 | |
| 848 // Allows deleting our singleton instance. | |
| 849 static void DeleteForTesting(); | |
| 850 | |
| 851 // Allows resurrecting our singleton instance post-AtExit processing. | |
| 852 static void Resurrect(); | |
| 853 | |
| 854 // Allow tests to inspect TraceEvents. | |
| 855 size_t GetEventsSize() const { return logged_events_.size(); } | |
| 856 const TraceEvent& GetEventAt(size_t index) const { | |
| 857 DCHECK(index < logged_events_.size()); | |
| 858 return logged_events_[index]; | |
| 859 } | |
| 860 | |
| 861 void SetProcessID(int process_id); | |
| 862 | |
| 863 private: | |
| 864 // This allows constructor and destructor to be private and usable only | |
| 865 // by the Singleton class. | |
| 866 friend struct StaticMemorySingletonTraits<TraceLog>; | |
| 867 | |
| 868 TraceLog(); | |
| 869 ~TraceLog(); | |
| 870 const unsigned char* GetCategoryEnabledInternal(const char* name); | |
| 871 void AddThreadNameMetadataEvents(); | |
| 872 void AddClockSyncMetadataEvents(); | |
| 873 | |
| 874 // TODO(nduca): switch to per-thread trace buffers to reduce thread | |
| 875 // synchronization. | |
| 876 Lock lock_; | |
| 877 bool enabled_; | |
| 878 OutputCallback output_callback_; | |
| 879 BufferFullCallback buffer_full_callback_; | |
| 880 std::vector<TraceEvent> logged_events_; | |
| 881 std::vector<std::string> included_categories_; | |
| 882 std::vector<std::string> excluded_categories_; | |
| 883 | |
| 884 base::hash_map<int, std::string> thread_names_; | |
| 885 | |
| 886 // XORed with TraceID to make it unlikely to collide with other processes. | |
| 887 unsigned long long process_id_hash_; | |
| 888 | |
| 889 int process_id_; | |
| 890 | |
| 891 DISALLOW_COPY_AND_ASSIGN(TraceLog); | |
| 892 }; | |
| 893 | |
| 894 } // namespace debug | |
| 895 } // namespace base | |
| 896 | |
| 897 namespace trace_event_internal { | 591 namespace trace_event_internal { |
| 898 | 592 |
| 899 // Specify these values when the corresponding argument of AddTraceEvent is not | 593 // Specify these values when the corresponding argument of AddTraceEvent is not |
| 900 // used. | 594 // used. |
| 901 const int kZeroNumArgs = 0; | 595 const int kZeroNumArgs = 0; |
| 902 const int kNoThreshholdBeginId = -1; | 596 const int kNoThreshholdBeginId = -1; |
| 903 const long long kNoThresholdValue = 0; | 597 const long long kNoThresholdValue = 0; |
| 904 const unsigned long long kNoEventId = 0; | 598 const unsigned long long kNoEventId = 0; |
| 905 | 599 |
| 906 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | 600 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1121 const char* name; | 815 const char* name; |
| 1122 int threshold_begin_id; | 816 int threshold_begin_id; |
| 1123 }; | 817 }; |
| 1124 Data* p_data_; | 818 Data* p_data_; |
| 1125 Data data_; | 819 Data data_; |
| 1126 }; | 820 }; |
| 1127 | 821 |
| 1128 } // namespace trace_event_internal | 822 } // namespace trace_event_internal |
| 1129 | 823 |
| 1130 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 824 #endif // BASE_DEBUG_TRACE_EVENT_H_ |
| OLD | NEW |