Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: base/debug/trace_event_impl.h

Issue 22962004: Thread-local trace-event buffers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/debug/trace_event_impl.cc » ('j') | base/debug/trace_event_impl.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
533 #if defined(OS_ANDROID) 553 #if defined(OS_ANDROID)
534 void SendToATrace(char phase, 554 void SendToATrace(char phase,
535 const char* category_group, 555 const char* category_group,
536 const char* name, 556 const char* name,
537 unsigned long long id, 557 unsigned long long id,
538 int num_args, 558 int num_args,
539 const char** arg_names, 559 const char** arg_names,
540 const unsigned char* arg_types, 560 const unsigned char* arg_types,
541 const unsigned long long* arg_values, 561 const unsigned long long* arg_values,
542 scoped_ptr<ConvertableToTraceFormat> convertable_values[], 562 scoped_ptr<ConvertableToTraceFormat> convertable_values[],
543 unsigned char flags); 563 unsigned char flags);
544 static void ApplyATraceEnabledFlag(unsigned char* category_group_enabled); 564 static void ApplyATraceEnabledFlag(unsigned char* category_group_enabled);
545 #endif 565 #endif
546 566
547 TraceBuffer* GetTraceBuffer(); 567 TraceBuffer* GetTraceBuffer();
548 568
549 // TODO(nduca): switch to per-thread trace buffers to reduce thread 569 bool AddEventToMainBufferWhileLocked(const TraceEvent& trace_event,
550 // synchronization. 570 NotificationHelper* notifier);
571 void FlushNextThreadOrFinish();
572 void FlushCurrentThreadAndContinue();
573
551 // This lock protects TraceLog member accesses from arbitrary threads. 574 // This lock protects TraceLog member accesses from arbitrary threads.
552 Lock lock_; 575 Lock lock_;
553 int enable_count_; 576 int enable_count_;
554 int num_traces_recorded_; 577 int num_traces_recorded_;
578 subtle::AtomicWord /* bool */ buffer_is_full_;
555 NotificationCallback notification_callback_; 579 NotificationCallback notification_callback_;
556 scoped_ptr<TraceBuffer> logged_events_; 580 scoped_ptr<TraceBuffer> logged_events_;
557 EventCallback event_callback_; 581 subtle::AtomicWord /* EventCallback */ event_callback_;
558 bool dispatching_to_observer_list_; 582 bool dispatching_to_observer_list_;
559 std::vector<EnabledStateObserver*> enabled_state_observer_list_; 583 std::vector<EnabledStateObserver*> enabled_state_observer_list_;
560 584
561 std::string process_name_; 585 std::string process_name_;
562 base::hash_map<int, std::string> process_labels_; 586 base::hash_map<int, std::string> process_labels_;
563 int process_sort_index_; 587 int process_sort_index_;
564 base::hash_map<int, int> thread_sort_indices_; 588 base::hash_map<int, int> thread_sort_indices_;
565 589
566 base::hash_map<int, std::string> thread_names_; 590 base::hash_map<int, std::string> thread_names_;
567 base::hash_map<int, std::stack<TimeTicks> > thread_event_start_times_; 591
568 base::hash_map<std::string, int> thread_colors_; 592 subtle::AtomicWord /* EchoToConsoleContext* */ echo_to_console_context_;
569 593
570 // XORed with TraceID to make it unlikely to collide with other processes. 594 // XORed with TraceID to make it unlikely to collide with other processes.
571 unsigned long long process_id_hash_; 595 unsigned long long process_id_hash_;
572 596
573 int process_id_; 597 int process_id_;
574 598
575 TimeDelta time_offset_; 599 TimeDelta time_offset_;
576 600
577 // Allow tests to wake up when certain events occur. 601 // Allow tests to wake up when certain events occur.
578 const unsigned char* watch_category_; 602 subtle::AtomicWord /* const unsigned char* */ watch_category_;
579 std::string watch_event_name_; 603 std::string watch_event_name_;
580 604
581 Options trace_options_; 605 Options trace_options_;
582 606
583 // Sampling thread handles. 607 // Sampling thread handles.
584 scoped_ptr<TraceSamplingThread> sampling_thread_; 608 scoped_ptr<TraceSamplingThread> sampling_thread_;
585 PlatformThreadHandle sampling_thread_handle_; 609 PlatformThreadHandle sampling_thread_handle_;
586 610
587 CategoryFilter category_filter_; 611 CategoryFilter category_filter_;
588 612
613 ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_;
614
615 // Contains the message loops of threads that have had at least one event
616 // is added into the local event buffer.
dsinclair 2013/08/16 14:38:32 s/is//
Xianzhu 2013/08/16 19:41:03 Done.
617 base::hash_set<MessageLoop*> thread_message_loops_;
618
619 // Set when asynchronous Flush is in progress.
620 OutputCallback flush_output_callback_;
621 MessageLoop* flush_message_loop_;
622
589 DISALLOW_COPY_AND_ASSIGN(TraceLog); 623 DISALLOW_COPY_AND_ASSIGN(TraceLog);
590 }; 624 };
591 625
592 } // namespace debug 626 } // namespace debug
593 } // namespace base 627 } // namespace base
594 628
595 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_ 629 #endif // BASE_DEBUG_TRACE_EVENT_IMPL_H_
OLDNEW
« no previous file with comments | « no previous file | base/debug/trace_event_impl.cc » ('j') | base/debug/trace_event_impl.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698