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. | 5 // Trace events are for tracking application performance. |
6 // | 6 // |
7 // Events are issued against categories. Whereas LOG's | 7 // Events are issued against categories. Whereas LOG's |
8 // categories are statically defined, TRACE categories are created | 8 // categories are statically defined, TRACE categories are created |
9 // implicitly with a string. For example: | 9 // implicitly with a string. For example: |
10 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") | 10 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 #include <string> | 89 #include <string> |
90 #include <vector> | 90 #include <vector> |
91 | 91 |
92 #include "base/callback.h" | 92 #include "base/callback.h" |
93 #include "base/hash_tables.h" | 93 #include "base/hash_tables.h" |
94 #include "base/memory/singleton.h" | 94 #include "base/memory/singleton.h" |
95 #include "base/string_util.h" | 95 #include "base/string_util.h" |
96 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 96 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
97 #include "base/timer.h" | 97 #include "base/timer.h" |
98 | 98 |
| 99 // Short hand convenience macro for specifying unscoped strings as argument |
| 100 // values to avoid allocating/copying. The string pointer must remain valid |
| 101 // until tracing is disabled. |
| 102 #define TRACE_STATIC_STR(str) base::debug::TraceValue::StaticString(str) |
| 103 |
99 // Older style trace macros with explicit id and extra data | 104 // Older style trace macros with explicit id and extra data |
100 // Only these macros result in publishing data to ETW as currently implemented. | 105 // Only these macros result in publishing data to ETW as currently implemented. |
101 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | 106 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ |
102 base::debug::TraceLog::AddTraceEventEtw( \ | 107 base::debug::TraceLog::AddTraceEventEtw( \ |
103 base::debug::TRACE_EVENT_PHASE_BEGIN, \ | 108 base::debug::TRACE_EVENT_PHASE_BEGIN, \ |
104 name, reinterpret_cast<const void*>(id), extra); | 109 name, reinterpret_cast<const void*>(id), extra); |
105 | 110 |
106 #define TRACE_EVENT_END_ETW(name, id, extra) \ | 111 #define TRACE_EVENT_END_ETW(name, id, extra) \ |
107 base::debug::TraceLog::AddTraceEventEtw( \ | 112 base::debug::TraceLog::AddTraceEventEtw( \ |
108 base::debug::TRACE_EVENT_PHASE_END, \ | 113 base::debug::TRACE_EVENT_PHASE_END, \ |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 // low, we want constant size storage here. | 311 // low, we want constant size storage here. |
307 class BASE_EXPORT TraceValue { | 312 class BASE_EXPORT TraceValue { |
308 public: | 313 public: |
309 enum Type { | 314 enum Type { |
310 TRACE_TYPE_UNDEFINED, | 315 TRACE_TYPE_UNDEFINED, |
311 TRACE_TYPE_BOOL, | 316 TRACE_TYPE_BOOL, |
312 TRACE_TYPE_UINT, | 317 TRACE_TYPE_UINT, |
313 TRACE_TYPE_INT, | 318 TRACE_TYPE_INT, |
314 TRACE_TYPE_DOUBLE, | 319 TRACE_TYPE_DOUBLE, |
315 TRACE_TYPE_POINTER, | 320 TRACE_TYPE_POINTER, |
316 TRACE_TYPE_STRING | 321 TRACE_TYPE_STRING, |
| 322 TRACE_TYPE_STATIC_STRING |
317 }; | 323 }; |
318 | 324 |
319 TraceValue() : type_(TRACE_TYPE_UNDEFINED) { | 325 TraceValue() : type_(TRACE_TYPE_UNDEFINED) { |
320 value_.as_uint = 0ull; | 326 value_.as_uint = 0ull; |
321 } | 327 } |
322 TraceValue(bool rhs) : type_(TRACE_TYPE_BOOL) { | 328 TraceValue(bool rhs) : type_(TRACE_TYPE_BOOL) { |
323 value_.as_bool = rhs; | 329 value_.as_bool = rhs; |
324 } | 330 } |
325 TraceValue(uint64 rhs) : type_(TRACE_TYPE_UINT) { | 331 TraceValue(uint64 rhs) : type_(TRACE_TYPE_UINT) { |
326 value_.as_uint = rhs; | 332 value_.as_uint = rhs; |
(...skipping 21 matching lines...) Expand all Loading... |
348 } | 354 } |
349 TraceValue(double rhs) : type_(TRACE_TYPE_DOUBLE) { | 355 TraceValue(double rhs) : type_(TRACE_TYPE_DOUBLE) { |
350 value_.as_double = rhs; | 356 value_.as_double = rhs; |
351 } | 357 } |
352 TraceValue(const void* rhs) : type_(TRACE_TYPE_POINTER) { | 358 TraceValue(const void* rhs) : type_(TRACE_TYPE_POINTER) { |
353 value_.as_pointer = rhs; | 359 value_.as_pointer = rhs; |
354 } | 360 } |
355 TraceValue(const char* rhs) : type_(TRACE_TYPE_STRING) { | 361 TraceValue(const char* rhs) : type_(TRACE_TYPE_STRING) { |
356 value_.as_string = rhs; | 362 value_.as_string = rhs; |
357 } | 363 } |
| 364 // Use this if your string is already global and does not need to be copied. |
| 365 static TraceValue StaticString(const char* rhs) { |
| 366 TraceValue tv(rhs); |
| 367 tv.type_ = TRACE_TYPE_STATIC_STRING; |
| 368 return tv; |
| 369 } |
358 | 370 |
359 void AppendAsJSON(std::string* out) const; | 371 void AppendAsJSON(std::string* out) const; |
360 | 372 |
361 Type type() const { | 373 Type type() const { |
362 return type_; | 374 return type_; |
363 } | 375 } |
364 uint64 as_uint() const { | 376 uint64 as_uint() const { |
365 DCHECK_EQ(TRACE_TYPE_UINT, type_); | 377 DCHECK_EQ(TRACE_TYPE_UINT, type_); |
366 return value_.as_uint; | 378 return value_.as_uint; |
367 } | 379 } |
368 bool as_bool() const { | 380 bool as_bool() const { |
369 DCHECK_EQ(TRACE_TYPE_BOOL, type_); | 381 DCHECK_EQ(TRACE_TYPE_BOOL, type_); |
370 return value_.as_bool; | 382 return value_.as_bool; |
371 } | 383 } |
372 int64 as_int() const { | 384 int64 as_int() const { |
373 DCHECK_EQ(TRACE_TYPE_INT, type_); | 385 DCHECK_EQ(TRACE_TYPE_INT, type_); |
374 return value_.as_int; | 386 return value_.as_int; |
375 } | 387 } |
376 double as_double() const { | 388 double as_double() const { |
377 DCHECK_EQ(TRACE_TYPE_DOUBLE, type_); | 389 DCHECK_EQ(TRACE_TYPE_DOUBLE, type_); |
378 return value_.as_double; | 390 return value_.as_double; |
379 } | 391 } |
380 const void* as_pointer() const { | 392 const void* as_pointer() const { |
381 DCHECK_EQ(TRACE_TYPE_POINTER, type_); | 393 DCHECK_EQ(TRACE_TYPE_POINTER, type_); |
382 return value_.as_pointer; | 394 return value_.as_pointer; |
383 } | 395 } |
384 const char* as_string() const { | 396 const char* as_string() const { |
385 DCHECK_EQ(TRACE_TYPE_STRING, type_); | 397 DCHECK(type_ == TRACE_TYPE_STRING || type_ == TRACE_TYPE_STATIC_STRING); |
386 return value_.as_string; | 398 return value_.as_string; |
387 } | 399 } |
388 const char** as_assignable_string() { | 400 const char** as_assignable_string() { |
389 DCHECK_EQ(TRACE_TYPE_STRING, type_); | 401 DCHECK_EQ(TRACE_TYPE_STRING, type_); |
390 return &value_.as_string; | 402 return &value_.as_string; |
391 } | 403 } |
392 | 404 |
393 private: | 405 private: |
394 union Value { | 406 union Value { |
395 bool as_bool; | 407 bool as_bool; |
(...skipping 28 matching lines...) Expand all Loading... |
424 | 436 |
425 // Serialize event data to JSON | 437 // Serialize event data to JSON |
426 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 438 static void AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
427 size_t start, | 439 size_t start, |
428 size_t count, | 440 size_t count, |
429 std::string* out); | 441 std::string* out); |
430 void AppendAsJSON(std::string* out) const; | 442 void AppendAsJSON(std::string* out) const; |
431 | 443 |
432 TimeTicks timestamp() const { return timestamp_; } | 444 TimeTicks timestamp() const { return timestamp_; } |
433 | 445 |
| 446 // Exposed for unittesting: |
| 447 |
| 448 const base::RefCountedString* parameter_copy_storage() const { |
| 449 return parameter_copy_storage_.get(); |
| 450 } |
| 451 |
| 452 const char* name() const { return name_; } |
| 453 |
434 private: | 454 private: |
435 unsigned long process_id_; | 455 unsigned long process_id_; |
436 unsigned long thread_id_; | 456 unsigned long thread_id_; |
437 TimeTicks timestamp_; | 457 TimeTicks timestamp_; |
438 TraceEventPhase phase_; | 458 TraceEventPhase phase_; |
439 const TraceCategory* category_; | 459 const TraceCategory* category_; |
440 const char* name_; | 460 const char* name_; |
441 const char* arg_names_[kTraceMaxNumArgs]; | 461 const char* arg_names_[kTraceMaxNumArgs]; |
442 TraceValue arg_values_[kTraceMaxNumArgs]; | 462 TraceValue arg_values_[kTraceMaxNumArgs]; |
443 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | 463 scoped_refptr<base::RefCountedString> parameter_copy_storage_; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 const char* name, | 515 const char* name, |
496 const void* id, | 516 const void* id, |
497 const char* extra); | 517 const char* extra); |
498 static void AddTraceEventEtw(TraceEventPhase phase, | 518 static void AddTraceEventEtw(TraceEventPhase phase, |
499 const char* name, | 519 const char* name, |
500 const void* id, | 520 const void* id, |
501 const std::string& extra) { | 521 const std::string& extra) { |
502 AddTraceEventEtw(phase, name, id, extra.c_str()); | 522 AddTraceEventEtw(phase, name, id, extra.c_str()); |
503 } | 523 } |
504 | 524 |
505 // Exposed for unittesting only, allows resurrecting our | 525 // Exposed for unittesting: |
506 // singleton instance post-AtExit processing. | 526 |
| 527 // Allows resurrecting our singleton instance post-AtExit processing. |
507 static void Resurrect(); | 528 static void Resurrect(); |
508 | 529 |
| 530 // Allow tests to inspect TraceEvents. |
| 531 size_t GetEventsSize() const { return logged_events_.size(); } |
| 532 const TraceEvent& GetEventAt(size_t index) const { |
| 533 DCHECK(index < logged_events_.size()); |
| 534 return logged_events_[index]; |
| 535 } |
| 536 |
509 private: | 537 private: |
510 // This allows constructor and destructor to be private and usable only | 538 // This allows constructor and destructor to be private and usable only |
511 // by the Singleton class. | 539 // by the Singleton class. |
512 friend struct StaticMemorySingletonTraits<TraceLog>; | 540 friend struct StaticMemorySingletonTraits<TraceLog>; |
513 | 541 |
514 TraceLog(); | 542 TraceLog(); |
515 ~TraceLog(); | 543 ~TraceLog(); |
516 const TraceCategory* GetCategoryInternal(const char* name); | 544 const TraceCategory* GetCategoryInternal(const char* name); |
517 void AddCurrentMetadataEvents(); | 545 void AddCurrentMetadataEvents(); |
518 | 546 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 Data* p_data_; | 622 Data* p_data_; |
595 Data data_; | 623 Data data_; |
596 }; | 624 }; |
597 | 625 |
598 } // namespace internal | 626 } // namespace internal |
599 | 627 |
600 } // namespace debug | 628 } // namespace debug |
601 } // namespace base | 629 } // namespace base |
602 | 630 |
603 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 631 #endif // BASE_DEBUG_TRACE_EVENT_H_ |
OLD | NEW |