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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // ANNOTATE_BENIGN_RACE is used to suppress the warning on the static category | 90 // ANNOTATE_BENIGN_RACE is used to suppress the warning on the static category |
91 // pointers. | 91 // pointers. |
92 | 92 |
93 | 93 |
94 #ifndef BASE_DEBUG_TRACE_EVENT_H_ | 94 #ifndef BASE_DEBUG_TRACE_EVENT_H_ |
95 #define BASE_DEBUG_TRACE_EVENT_H_ | 95 #define BASE_DEBUG_TRACE_EVENT_H_ |
96 #pragma once | 96 #pragma once |
97 | 97 |
98 #include "build/build_config.h" | 98 #include "build/build_config.h" |
99 | 99 |
| 100 #include <map> |
100 #include <string> | 101 #include <string> |
101 #include <vector> | 102 #include <vector> |
102 | 103 |
103 #include "base/callback.h" | 104 #include "base/callback.h" |
104 #include "base/hash_tables.h" | 105 #include "base/hash_tables.h" |
105 #include "base/memory/singleton.h" | 106 #include "base/memory/singleton.h" |
106 #include "base/string_util.h" | 107 #include "base/string_util.h" |
107 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 108 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
108 #include "base/timer.h" | 109 #include "base/timer.h" |
109 | 110 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 name, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, \ | 305 name, arg1_name, arg1_val, arg2_name, arg2_val, -1, 0, \ |
305 base::debug::TraceLog::EVENT_FLAG_NONE); \ | 306 base::debug::TraceLog::EVENT_FLAG_NONE); \ |
306 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | 307 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ |
307 INTERNAL_TRACE_EVENT_UID(catstatic), name, \ | 308 INTERNAL_TRACE_EVENT_UID(catstatic), name, \ |
308 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \ | 309 INTERNAL_TRACE_EVENT_UID(begin_event_id), threshold); \ |
309 } | 310 } |
310 | 311 |
311 namespace base { | 312 namespace base { |
312 | 313 |
313 class RefCountedString; | 314 class RefCountedString; |
| 315 class Value; |
314 | 316 |
315 namespace debug { | 317 namespace debug { |
316 | 318 |
317 // Categories allow enabling/disabling of streams of trace events | 319 // Categories allow enabling/disabling of streams of trace events |
318 struct TraceCategory { | 320 struct TraceCategory { |
319 const char* name; | 321 const char* name; |
320 volatile bool enabled; | 322 volatile bool enabled; |
321 }; | 323 }; |
322 | 324 |
323 const size_t kTraceMaxNumArgs = 2; | 325 const size_t kTraceMaxNumArgs = 2; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 return value; | 398 return value; |
397 } | 399 } |
398 | 400 |
399 static TraceValue ForceCopy(const TraceValue& rhs) { | 401 static TraceValue ForceCopy(const TraceValue& rhs) { |
400 TraceValue value(rhs); | 402 TraceValue value(rhs); |
401 if (value.type_ == TRACE_TYPE_STATIC_STRING && value.as_string()) | 403 if (value.type_ == TRACE_TYPE_STATIC_STRING && value.as_string()) |
402 value.type_ = TRACE_TYPE_STRING; | 404 value.type_ = TRACE_TYPE_STRING; |
403 return value; | 405 return value; |
404 } | 406 } |
405 | 407 |
| 408 bool is_string() const { |
| 409 return type_ == TRACE_TYPE_STRING || type_ == TRACE_TYPE_STATIC_STRING; |
| 410 } |
| 411 |
406 void AppendAsJSON(std::string* out) const; | 412 void AppendAsJSON(std::string* out) const; |
407 | 413 |
408 Type type() const { | 414 Type type() const { |
409 return type_; | 415 return type_; |
410 } | 416 } |
411 uint64 as_uint() const { | 417 uint64 as_uint() const { |
412 DCHECK_EQ(TRACE_TYPE_UINT, type_); | 418 DCHECK_EQ(TRACE_TYPE_UINT, type_); |
413 return value_.as_uint; | 419 return value_.as_uint; |
414 } | 420 } |
415 bool as_bool() const { | 421 bool as_bool() const { |
416 DCHECK_EQ(TRACE_TYPE_BOOL, type_); | 422 DCHECK_EQ(TRACE_TYPE_BOOL, type_); |
417 return value_.as_bool; | 423 return value_.as_bool; |
418 } | 424 } |
419 int64 as_int() const { | 425 int64 as_int() const { |
420 DCHECK_EQ(TRACE_TYPE_INT, type_); | 426 DCHECK_EQ(TRACE_TYPE_INT, type_); |
421 return value_.as_int; | 427 return value_.as_int; |
422 } | 428 } |
423 double as_double() const { | 429 double as_double() const { |
424 DCHECK_EQ(TRACE_TYPE_DOUBLE, type_); | 430 DCHECK_EQ(TRACE_TYPE_DOUBLE, type_); |
425 return value_.as_double; | 431 return value_.as_double; |
426 } | 432 } |
427 const void* as_pointer() const { | 433 const void* as_pointer() const { |
428 DCHECK_EQ(TRACE_TYPE_POINTER, type_); | 434 DCHECK_EQ(TRACE_TYPE_POINTER, type_); |
429 return value_.as_pointer; | 435 return value_.as_pointer; |
430 } | 436 } |
431 const char* as_string() const { | 437 const char* as_string() const { |
432 DCHECK(type_ == TRACE_TYPE_STRING || type_ == TRACE_TYPE_STATIC_STRING); | 438 DCHECK(is_string()); |
433 return value_.as_string; | 439 return value_.as_string; |
434 } | 440 } |
435 const char** as_assignable_string() { | 441 const char** as_assignable_string() { |
436 DCHECK_EQ(TRACE_TYPE_STRING, type_); | 442 DCHECK_EQ(TRACE_TYPE_STRING, type_); |
437 return &value_.as_string; | 443 return &value_.as_string; |
438 } | 444 } |
439 | 445 |
440 private: | 446 private: |
441 union Value { | 447 union Value { |
442 bool as_bool; | 448 bool as_bool; |
443 uint64 as_uint; | 449 uint64 as_uint; |
444 int64 as_int; | 450 int64 as_int; |
445 double as_double; | 451 double as_double; |
446 const void* as_pointer; | 452 const void* as_pointer; |
447 const char* as_string; | 453 const char* as_string; |
448 }; | 454 }; |
449 | 455 |
450 Type type_; | 456 Type type_; |
451 Value value_; | 457 Value value_; |
452 }; | 458 }; |
453 | 459 |
| 460 // TestTraceEvent is a more convenient form of the TraceEvent class to make |
| 461 // tracing-based tests easier to write. |
| 462 struct TestTraceEvent { |
| 463 // PidTid contains a Process ID and Thread ID. |
| 464 struct PidTid { |
| 465 PidTid() : pid(0), tid(0) {} |
| 466 PidTid(int pid, int tid) : pid(pid), tid(tid) {} |
| 467 bool operator< (PidTid rhs) const { |
| 468 if (pid != rhs.pid) |
| 469 return pid < rhs.pid; |
| 470 return tid < rhs.tid; |
| 471 } |
| 472 int pid; |
| 473 int tid; |
| 474 }; |
| 475 |
| 476 TestTraceEvent(); |
| 477 TestTraceEvent(const base::Value* event_value); |
| 478 ~TestTraceEvent(); |
| 479 |
| 480 // Convert JSON string to array of TestTraceEvent. |
| 481 // |output| is appended with the parsed events. |
| 482 static bool ParseEventsFromJson(const std::string& json, |
| 483 std::vector<TestTraceEvent>* output); |
| 484 |
| 485 bool operator< (const TestTraceEvent& rhs) const { |
| 486 return timestamp < rhs.timestamp; |
| 487 } |
| 488 |
| 489 // Returns duration if it's available. |
| 490 bool GetDuration(double* duration) const; |
| 491 |
| 492 // Return the argument value if it exists. |
| 493 bool IsArg(const std::string& name) const; |
| 494 // Return the argument value if it exists and it is a string. |
| 495 bool GetArgAsString(const std::string& name, std::string* arg) const; |
| 496 // Return the argument value if it exists and it is a number. |
| 497 bool GetArgAsNumber(const std::string& name, double* arg) const; |
| 498 |
| 499 // Called by TraceAnalyzer to associate two events. |
| 500 void SetAssociatedEvent(TestTraceEvent* event); |
| 501 |
| 502 // Process ID and Thread ID. |
| 503 PidTid pid_tid; |
| 504 // Time since epoch in microseconds. |
| 505 // Stored as double to match its JSON representation. |
| 506 double timestamp; |
| 507 TraceEventPhase phase; |
| 508 std::string category; |
| 509 std::string name; |
| 510 // All numbers and bool values from TraceEvent args are cast to double. |
| 511 // bool becomes 1.0 (true) or 0.0 (false). |
| 512 std::map<std::string, double> arg_numbers; |
| 513 std::map<std::string, std::string> arg_strings; |
| 514 // Set by TraceAnalyzer to point between matching BEGIN and END events. |
| 515 const TestTraceEvent* associated_event; |
| 516 }; |
| 517 |
454 // Output records are "Events" and can be obtained via the | 518 // Output records are "Events" and can be obtained via the |
455 // OutputCallback whenever the tracing system decides to flush. This | 519 // OutputCallback whenever the tracing system decides to flush. This |
456 // can happen at any time, on any thread, or you can programatically | 520 // can happen at any time, on any thread, or you can programatically |
457 // force it to happen. | 521 // force it to happen. |
458 class TraceEvent { | 522 class TraceEvent { |
459 public: | 523 public: |
460 TraceEvent(); | 524 TraceEvent(); |
461 TraceEvent(unsigned long process_id, | 525 TraceEvent(unsigned long process_id, |
462 unsigned long thread_id, | 526 unsigned long thread_id, |
463 TimeTicks timestamp, | 527 TimeTicks timestamp, |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 Data* p_data_; | 743 Data* p_data_; |
680 Data data_; | 744 Data data_; |
681 }; | 745 }; |
682 | 746 |
683 } // namespace internal | 747 } // namespace internal |
684 | 748 |
685 } // namespace debug | 749 } // namespace debug |
686 } // namespace base | 750 } // namespace base |
687 | 751 |
688 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 752 #endif // BASE_DEBUG_TRACE_EVENT_H_ |
OLD | NEW |