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 | 5 |
6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_ | 6 #ifndef BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_ | 7 #define BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
8 | 8 |
9 #include <stack> | 9 #include <stack> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/atomicops.h" | |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
16 #include "base/memory/ref_counted_memory.h" | 17 #include "base/memory/ref_counted_memory.h" |
17 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
18 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/synchronization/condition_variable.h" | 21 #include "base/synchronization/condition_variable.h" |
21 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
22 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
24 #include "base/threading/thread_local.h" | |
23 #include "base/timer/timer.h" | 25 #include "base/timer/timer.h" |
24 | 26 |
25 // Older style trace macros with explicit id and extra data | 27 // Older style trace macros with explicit id and extra data |
26 // Only these macros result in publishing data to ETW as currently implemented. | 28 // Only these macros result in publishing data to ETW as currently implemented. |
27 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ | 29 #define TRACE_EVENT_BEGIN_ETW(name, id, extra) \ |
28 base::debug::TraceLog::AddTraceEventEtw( \ | 30 base::debug::TraceLog::AddTraceEventEtw( \ |
29 TRACE_EVENT_PHASE_BEGIN, \ | 31 TRACE_EVENT_PHASE_BEGIN, \ |
30 name, reinterpret_cast<const void*>(id), extra) | 32 name, reinterpret_cast<const void*>(id), extra) |
31 | 33 |
32 #define TRACE_EVENT_END_ETW(name, id, extra) \ | 34 #define TRACE_EVENT_END_ETW(name, id, extra) \ |
33 base::debug::TraceLog::AddTraceEventEtw( \ | 35 base::debug::TraceLog::AddTraceEventEtw( \ |
34 TRACE_EVENT_PHASE_END, \ | 36 TRACE_EVENT_PHASE_END, \ |
35 name, reinterpret_cast<const void*>(id), extra) | 37 name, reinterpret_cast<const void*>(id), extra) |
36 | 38 |
37 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \ | 39 #define TRACE_EVENT_INSTANT_ETW(name, id, extra) \ |
38 base::debug::TraceLog::AddTraceEventEtw( \ | 40 base::debug::TraceLog::AddTraceEventEtw( \ |
39 TRACE_EVENT_PHASE_INSTANT, \ | 41 TRACE_EVENT_PHASE_INSTANT, \ |
40 name, reinterpret_cast<const void*>(id), extra) | 42 name, reinterpret_cast<const void*>(id), extra) |
41 | 43 |
42 template <typename Type> | 44 template <typename Type> |
43 struct DefaultSingletonTraits; | 45 struct DefaultSingletonTraits; |
44 | 46 |
47 #if defined(COMPILER_GCC) | |
48 namespace BASE_HASH_NAMESPACE { | |
49 template <> | |
50 struct hash<base::MessageLoop*> { | |
51 std::size_t operator()(base::MessageLoop* value) const { | |
52 return reinterpret_cast<std::size_t>(value); | |
53 } | |
54 }; | |
55 } | |
56 #endif | |
57 | |
45 namespace base { | 58 namespace base { |
46 | 59 |
47 class WaitableEvent; | 60 class WaitableEvent; |
61 class MessageLoop; | |
48 | 62 |
49 namespace debug { | 63 namespace debug { |
50 | 64 |
51 // For any argument of type TRACE_VALUE_TYPE_CONVERTABLE the provided | 65 // For any argument of type TRACE_VALUE_TYPE_CONVERTABLE the provided |
52 // class must implement this interface. | 66 // class must implement this interface. |
53 class ConvertableToTraceFormat { | 67 class ConvertableToTraceFormat { |
54 public: | 68 public: |
55 virtual ~ConvertableToTraceFormat() {} | 69 virtual ~ConvertableToTraceFormat() {} |
56 | 70 |
57 // Append the class info to the provided |out| string. The appended | 71 // Append the class info to the provided |out| string. The appended |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 size_t count, | 115 size_t count, |
102 std::string* out); | 116 std::string* out); |
103 void AppendAsJSON(std::string* out) const; | 117 void AppendAsJSON(std::string* out) const; |
104 void AppendPrettyPrinted(std::ostringstream* out) const; | 118 void AppendPrettyPrinted(std::ostringstream* out) const; |
105 | 119 |
106 static void AppendValueAsJSON(unsigned char type, | 120 static void AppendValueAsJSON(unsigned char type, |
107 TraceValue value, | 121 TraceValue value, |
108 std::string* out); | 122 std::string* out); |
109 | 123 |
110 TimeTicks timestamp() const { return timestamp_; } | 124 TimeTicks timestamp() const { return timestamp_; } |
125 char phase() const { return phase_; } | |
126 int thread_id() const { return thread_id_; } | |
111 | 127 |
112 // Exposed for unittesting: | 128 // Exposed for unittesting: |
113 | 129 |
114 const base::RefCountedString* parameter_copy_storage() const { | 130 const base::RefCountedString* parameter_copy_storage() const { |
115 return parameter_copy_storage_.get(); | 131 return parameter_copy_storage_.get(); |
116 } | 132 } |
117 | 133 |
118 const unsigned char* category_group_enabled() const { | 134 const unsigned char* category_group_enabled() const { |
119 return category_group_enabled_; | 135 return category_group_enabled_; |
120 } | 136 } |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
518 // notifications from previous calls to AddNotificationWhileLocked, this | 534 // notifications from previous calls to AddNotificationWhileLocked, this |
519 // will call the NotificationCallback. | 535 // will call the NotificationCallback. |
520 inline void SendNotificationIfAny(); | 536 inline void SendNotificationIfAny(); |
521 | 537 |
522 private: | 538 private: |
523 TraceLog* trace_log_; | 539 TraceLog* trace_log_; |
524 NotificationCallback callback_copy_; | 540 NotificationCallback callback_copy_; |
525 int notification_; | 541 int notification_; |
526 }; | 542 }; |
527 | 543 |
544 class ThreadLocalEventBuffer; | |
545 | |
528 TraceLog(); | 546 TraceLog(); |
529 ~TraceLog(); | 547 ~TraceLog(); |
530 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); | 548 const unsigned char* GetCategoryGroupEnabledInternal(const char* name); |
531 void AddMetadataEvents(); | 549 void AddMetadataEvents(); |
532 | 550 |
551 void EnableEchoToConsole(bool enable); | |
552 void EchoEventToConsoleIfEnabled(const TraceEvent& event); | |
553 | |
533 #if defined(OS_ANDROID) | 554 #if defined(OS_ANDROID) |
534 void SendToATrace(char phase, | 555 void SendToATrace(char phase, |
535 const char* category_group, | 556 const unsigned char* category_group_enabled, |
536 const char* name, | 557 const char* name, |
537 unsigned long long id, | 558 unsigned long long id, |
538 int num_args, | 559 int num_args, |
539 const char** arg_names, | 560 const char** arg_names, |
540 const unsigned char* arg_types, | 561 const unsigned char* arg_types, |
541 const unsigned long long* arg_values, | 562 const unsigned long long* arg_values, |
542 scoped_ptr<ConvertableToTraceFormat> convertable_values[], | 563 scoped_ptr<ConvertableToTraceFormat> convertable_values[], |
543 unsigned char flags); | 564 unsigned char flags); |
544 static void ApplyATraceEnabledFlag(unsigned char* category_group_enabled); | 565 static void ApplyATraceEnabledFlag(unsigned char* category_group_enabled); |
545 #endif | 566 #endif |
546 | 567 |
547 TraceBuffer* GetTraceBuffer(); | 568 TraceBuffer* GetTraceBuffer(); |
548 | 569 |
549 // TODO(nduca): switch to per-thread trace buffers to reduce thread | 570 bool AddEventToMainBufferWhileLocked(const TraceEvent& trace_event, |
550 // synchronization. | 571 NotificationHelper* notifier); |
572 void FlushNextThreadOrFinish(); | |
573 void DestroyThreadLocalEventBuffer(); | |
574 | |
551 // This lock protects TraceLog member accesses from arbitrary threads. | 575 // This lock protects TraceLog member accesses from arbitrary threads. |
552 Lock lock_; | 576 Lock lock_; |
553 int enable_count_; | 577 int enable_count_; |
554 int num_traces_recorded_; | 578 int num_traces_recorded_; |
579 subtle::AtomicWord /* bool */ buffer_is_full_; | |
555 NotificationCallback notification_callback_; | 580 NotificationCallback notification_callback_; |
556 scoped_ptr<TraceBuffer> logged_events_; | 581 scoped_ptr<TraceBuffer> logged_events_; |
557 EventCallback event_callback_; | 582 subtle::AtomicWord /* EventCallback */ event_callback_; |
558 bool dispatching_to_observer_list_; | 583 bool dispatching_to_observer_list_; |
559 std::vector<EnabledStateObserver*> enabled_state_observer_list_; | 584 std::vector<EnabledStateObserver*> enabled_state_observer_list_; |
560 | 585 |
561 std::string process_name_; | 586 std::string process_name_; |
562 base::hash_map<int, std::string> process_labels_; | 587 base::hash_map<int, std::string> process_labels_; |
563 int process_sort_index_; | 588 int process_sort_index_; |
564 base::hash_map<int, int> thread_sort_indices_; | 589 base::hash_map<int, int> thread_sort_indices_; |
565 | 590 |
566 base::hash_map<int, std::string> thread_names_; | 591 base::hash_map<int, std::string> thread_names_; |
567 base::hash_map<int, std::stack<TimeTicks> > thread_event_start_times_; | 592 |
568 base::hash_map<std::string, int> thread_colors_; | 593 subtle::AtomicWord echo_to_console_context_; |
dsinclair
2013/08/13 21:04:42
nit: the other AtomicWords have a comment with the
Xianzhu
2013/08/14 21:28:33
Done.
| |
569 | 594 |
570 // XORed with TraceID to make it unlikely to collide with other processes. | 595 // XORed with TraceID to make it unlikely to collide with other processes. |
571 unsigned long long process_id_hash_; | 596 unsigned long long process_id_hash_; |
572 | 597 |
573 int process_id_; | 598 int process_id_; |
574 | 599 |
575 TimeDelta time_offset_; | 600 TimeDelta time_offset_; |
576 | 601 |
577 // Allow tests to wake up when certain events occur. | 602 // Allow tests to wake up when certain events occur. |
578 const unsigned char* watch_category_; | 603 subtle::AtomicWord /* const unsigned char* */ watch_category_; |
579 std::string watch_event_name_; | 604 std::string watch_event_name_; |
580 | 605 |
581 Options trace_options_; | 606 Options trace_options_; |
582 | 607 |
583 // Sampling thread handles. | 608 // Sampling thread handles. |
584 scoped_ptr<TraceSamplingThread> sampling_thread_; | 609 scoped_ptr<TraceSamplingThread> sampling_thread_; |
585 PlatformThreadHandle sampling_thread_handle_; | 610 PlatformThreadHandle sampling_thread_handle_; |
586 | 611 |
587 CategoryFilter category_filter_; | 612 CategoryFilter category_filter_; |
588 | 613 |
614 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_; | |
615 base::hash_set<MessageLoop*> thread_message_loops_; | |
616 | |
617 // Set when asynchronous Flush is in progress. | |
618 OutputCallback flush_output_callback_; | |
619 MessageLoop* flush_message_loop_; | |
620 | |
589 DISALLOW_COPY_AND_ASSIGN(TraceLog); | 621 DISALLOW_COPY_AND_ASSIGN(TraceLog); |
590 }; | 622 }; |
591 | 623 |
592 } // namespace debug | 624 } // namespace debug |
593 } // namespace base | 625 } // namespace base |
594 | 626 |
595 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ | 627 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ |
OLD | NEW |