OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #ifndef CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_ |
6 #define CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_ | 6 #define CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <list> | 11 #include <list> |
12 #include <set> | 12 #include <set> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "base/trace_event/memory_dump_manager.h" | 19 #include "base/trace_event/memory_dump_manager.h" |
20 #include "base/trace_event/tracing_agent.h" | 20 #include "base/trace_event/tracing_agent.h" |
21 #include "content/public/browser/tracing_controller.h" | 21 #include "content/public/browser/tracing_controller.h" |
| 22 #include "mojo/common/data_pipe_drainer.h" |
| 23 #include "services/resource_coordinator/public/interfaces/tracing/tracing.mojom.
h" |
| 24 #include "services/service_manager/public/cpp/connector.h" |
22 | 25 |
23 namespace base { | 26 namespace base { |
24 class RefCountedString; | 27 class RefCountedString; |
25 } | 28 } |
26 | 29 |
27 namespace content { | 30 namespace content { |
28 | 31 |
29 class TraceMessageFilter; | 32 class TraceMessageFilter; |
30 class TracingUI; | 33 class TracingUI; |
31 | 34 |
32 // An implementation of this interface is passed when constructing a | 35 // An implementation of this interface is passed when constructing a |
33 // TraceDataSink, and receives chunks of the final trace data as it's being | 36 // TraceDataSink, and receives chunks of the final trace data as it's being |
34 // constructed. | 37 // constructed. |
35 // Methods may be called from any thread. | 38 // Methods may be called from any thread. |
36 class CONTENT_EXPORT TraceDataEndpoint | 39 class CONTENT_EXPORT TraceDataEndpoint |
37 : public base::RefCountedThreadSafe<TraceDataEndpoint> { | 40 : public base::RefCountedThreadSafe<TraceDataEndpoint> { |
38 public: | 41 public: |
39 virtual void ReceiveTraceChunk(std::unique_ptr<std::string> chunk) {} | 42 virtual void ReceiveTraceChunk(std::unique_ptr<std::string> chunk) {} |
40 virtual void ReceiveTraceFinalContents( | 43 virtual void ReceiveTraceFinalContents( |
41 std::unique_ptr<const base::DictionaryValue> metadata) {} | 44 std::unique_ptr<const base::DictionaryValue> metadata) {} |
42 | 45 |
43 protected: | 46 protected: |
44 friend class base::RefCountedThreadSafe<TraceDataEndpoint>; | 47 friend class base::RefCountedThreadSafe<TraceDataEndpoint>; |
45 virtual ~TraceDataEndpoint() {} | 48 virtual ~TraceDataEndpoint() {} |
46 }; | 49 }; |
47 | 50 |
48 class TracingControllerImpl | 51 class TracingControllerImpl : public TracingController, |
49 : public TracingController, | 52 public mojo::common::DataPipeDrainer::Client { |
50 public base::trace_event::TracingAgent { | |
51 public: | 53 public: |
52 // Create an endpoint that may be supplied to any TraceDataSink to | 54 // Create an endpoint that may be supplied to any TraceDataSink to |
53 // dump the trace data to a callback. | 55 // dump the trace data to a callback. |
54 CONTENT_EXPORT static scoped_refptr<TraceDataEndpoint> CreateCallbackEndpoint( | 56 CONTENT_EXPORT static scoped_refptr<TraceDataEndpoint> CreateCallbackEndpoint( |
55 const base::Callback<void(std::unique_ptr<const base::DictionaryValue>, | 57 const base::Callback<void(std::unique_ptr<const base::DictionaryValue>, |
56 base::RefCountedString*)>& callback); | 58 base::RefCountedString*)>& callback); |
57 | 59 |
58 CONTENT_EXPORT static scoped_refptr<TraceDataSink> CreateCompressedStringSink( | 60 CONTENT_EXPORT static scoped_refptr<TraceDataSink> CreateCompressedStringSink( |
59 scoped_refptr<TraceDataEndpoint> endpoint); | 61 scoped_refptr<TraceDataEndpoint> endpoint); |
60 static scoped_refptr<TraceDataSink> CreateJSONSink( | 62 static scoped_refptr<TraceDataSink> CreateJSONSink( |
61 scoped_refptr<TraceDataEndpoint> endpoint); | 63 scoped_refptr<TraceDataEndpoint> endpoint); |
62 | 64 |
63 static TracingControllerImpl* GetInstance(); | 65 static TracingControllerImpl* GetInstance(); |
64 | 66 |
| 67 void Initialize(service_manager::Connector* connector); |
| 68 |
65 // TracingController implementation. | 69 // TracingController implementation. |
66 bool GetCategories(const GetCategoriesDoneCallback& callback) override; | 70 bool GetCategories(const GetCategoriesDoneCallback& callback) override; |
67 bool StartTracing(const base::trace_event::TraceConfig& trace_config, | 71 bool StartTracing(const base::trace_event::TraceConfig& trace_config, |
68 const StartTracingDoneCallback& callback) override; | 72 const StartTracingDoneCallback& callback) override; |
69 bool StopTracing(const scoped_refptr<TraceDataSink>& sink) override; | 73 bool StopTracing(const scoped_refptr<TraceDataSink>& sink) override; |
70 bool GetTraceBufferUsage( | 74 bool GetTraceBufferUsage( |
71 const GetTraceBufferUsageCallback& callback) override; | 75 const GetTraceBufferUsageCallback& callback) override; |
72 void AddMetadata(const base::DictionaryValue& metadata) override; | 76 void AddMetadata(const base::DictionaryValue& metadata) override; |
73 | 77 |
74 bool IsTracing() const override; | 78 bool IsTracing() const override; |
75 | 79 |
76 void RegisterTracingUI(TracingUI* tracing_ui); | 80 void RegisterTracingUI(TracingUI* tracing_ui); |
77 void UnregisterTracingUI(TracingUI* tracing_ui); | 81 void UnregisterTracingUI(TracingUI* tracing_ui); |
78 | 82 |
79 // base::trace_event::TracingAgent implementation. | |
80 std::string GetTracingAgentName() override; | |
81 std::string GetTraceEventLabel() override; | |
82 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | |
83 const StartAgentTracingCallback& callback) override; | |
84 void StopAgentTracing(const StopAgentTracingCallback& callback) override; | |
85 bool SupportsExplicitClockSync() override; | |
86 void RecordClockSyncMarker( | |
87 const std::string& sync_id, | |
88 const RecordClockSyncMarkerCallback& callback) override; | |
89 | |
90 class TraceMessageFilterObserver { | 83 class TraceMessageFilterObserver { |
91 public: | 84 public: |
92 virtual void OnTraceMessageFilterAdded(TraceMessageFilter* filter) = 0; | 85 virtual void OnTraceMessageFilterAdded(TraceMessageFilter* filter) = 0; |
93 virtual void OnTraceMessageFilterRemoved(TraceMessageFilter* filter) = 0; | 86 virtual void OnTraceMessageFilterRemoved(TraceMessageFilter* filter) = 0; |
94 }; | 87 }; |
95 void AddTraceMessageFilterObserver(TraceMessageFilterObserver* observer); | 88 void AddTraceMessageFilterObserver(TraceMessageFilterObserver* observer); |
96 void RemoveTraceMessageFilterObserver(TraceMessageFilterObserver* observer); | 89 void RemoveTraceMessageFilterObserver(TraceMessageFilterObserver* observer); |
97 | 90 |
98 private: | 91 private: |
99 friend struct base::LazyInstanceTraitsBase<TracingControllerImpl>; | 92 friend struct base::LazyInstanceTraitsBase<TracingControllerImpl>; |
100 friend class TraceMessageFilter; | 93 friend class TraceMessageFilter; |
101 | 94 |
102 TracingControllerImpl(); | 95 TracingControllerImpl(); |
103 ~TracingControllerImpl() override; | 96 ~TracingControllerImpl() override; |
104 | 97 |
105 bool can_start_tracing() const { return !enabled_tracing_modes_; } | |
106 | |
107 bool can_stop_tracing() const { | |
108 return enabled_tracing_modes_ && !trace_data_sink_.get(); | |
109 } | |
110 | |
111 bool can_start_monitoring() const { | |
112 return !is_monitoring_; | |
113 } | |
114 | |
115 bool can_stop_monitoring() const { | |
116 return is_monitoring_ && !monitoring_data_sink_.get(); | |
117 } | |
118 | |
119 bool can_get_trace_buffer_usage() const { | |
120 return pending_trace_buffer_usage_callback_.is_null(); | |
121 } | |
122 | |
123 // Methods for use by TraceMessageFilter. | 98 // Methods for use by TraceMessageFilter. |
124 void AddTraceMessageFilter(TraceMessageFilter* trace_message_filter); | 99 void AddTraceMessageFilter(TraceMessageFilter* trace_message_filter); |
125 void RemoveTraceMessageFilter(TraceMessageFilter* trace_message_filter); | 100 void RemoveTraceMessageFilter(TraceMessageFilter* trace_message_filter); |
126 | 101 |
127 void OnTraceDataCollected( | |
128 const scoped_refptr<base::RefCountedString>& events_str_ptr); | |
129 | |
130 // Callback of TraceLog::Flush() for the local trace. | |
131 void OnLocalTraceDataCollected( | |
132 const scoped_refptr<base::RefCountedString>& events_str_ptr, | |
133 bool has_more_events); | |
134 | |
135 // Adds the tracing agent with the specified agent name to the list of | |
136 // additional tracing agents. | |
137 void AddTracingAgent(const std::string& agent_name); | |
138 | |
139 void OnStartAgentTracingAcked(const std::string& agent_name, bool success); | |
140 | |
141 void OnStopTracingAcked( | |
142 TraceMessageFilter* trace_message_filter, | |
143 const std::vector<std::string>& known_category_groups); | |
144 | |
145 void OnEndAgentTracingAcked( | |
146 const std::string& agent_name, | |
147 const std::string& events_label, | |
148 const scoped_refptr<base::RefCountedString>& events_str_ptr); | |
149 | |
150 void OnTraceLogStatusReply(TraceMessageFilter* trace_message_filter, | |
151 const base::trace_event::TraceLogStatus& status); | |
152 | |
153 void SetEnabledOnFileThread( | |
154 const base::trace_event::TraceConfig& trace_config, | |
155 const base::Closure& callback); | |
156 void SetDisabledOnFileThread(const base::Closure& callback); | |
157 void OnAllTracingAgentsStarted(); | |
158 void StopTracingAfterClockSync(); | |
159 void OnStopTracingDone(); | |
160 | |
161 // Issue clock sync markers to the tracing agents. | |
162 void IssueClockSyncMarker(); | |
163 void OnClockSyncMarkerRecordedByAgent( | |
164 const std::string& sync_id, | |
165 const base::TimeTicks& issue_ts, | |
166 const base::TimeTicks& issue_end_ts); | |
167 | |
168 void AddFilteredMetadata(TracingController::TraceDataSink* sink, | 102 void AddFilteredMetadata(TracingController::TraceDataSink* sink, |
169 std::unique_ptr<base::DictionaryValue> metadata, | 103 std::unique_ptr<base::DictionaryValue> metadata, |
170 const MetadataFilterPredicate& filter); | 104 const MetadataFilterPredicate& filter); |
171 | 105 |
172 std::unique_ptr<base::DictionaryValue> GenerateTracingMetadataDict() const; | 106 void GetCategoriesDone(const GetCategoriesDoneCallback& callback, |
| 107 const std::string& categories); |
| 108 void GetTraceBufferUsageDone(const GetTraceBufferUsageCallback& callback, |
| 109 bool success, |
| 110 float percent_full, |
| 111 uint32_t approximate_count); |
| 112 |
| 113 // mojo::common::DataPipeDrainer::Client |
| 114 void OnDataAvailable(const void* data, size_t num_bytes) override; |
| 115 void OnDataComplete() override; |
173 | 116 |
174 typedef std::set<scoped_refptr<TraceMessageFilter>> TraceMessageFilterSet; | 117 typedef std::set<scoped_refptr<TraceMessageFilter>> TraceMessageFilterSet; |
175 TraceMessageFilterSet trace_message_filters_; | 118 TraceMessageFilterSet trace_message_filters_; |
176 | 119 |
177 // Pending acks for StartTracing. | |
178 int pending_start_tracing_ack_count_; | |
179 base::OneShotTimer start_tracing_timer_; | |
180 StartTracingDoneCallback start_tracing_done_callback_; | |
181 std::unique_ptr<base::trace_event::TraceConfig> trace_config_; | |
182 | |
183 // Pending acks for StopTracing. | |
184 int pending_stop_tracing_ack_count_; | |
185 TraceMessageFilterSet pending_stop_tracing_filters_; | |
186 | |
187 // Pending acks for GetTraceLogStatus. | |
188 int pending_trace_log_status_ack_count_; | |
189 TraceMessageFilterSet pending_trace_log_status_filters_; | |
190 float maximum_trace_buffer_usage_; | |
191 size_t approximate_event_count_; | |
192 | |
193 std::vector<base::trace_event::TracingAgent*> additional_tracing_agents_; | |
194 int pending_clock_sync_ack_count_; | |
195 base::OneShotTimer clock_sync_timer_; | |
196 | |
197 uint8_t enabled_tracing_modes_; | |
198 bool is_monitoring_; | |
199 | |
200 GetCategoriesDoneCallback pending_get_categories_done_callback_; | |
201 GetTraceBufferUsageCallback pending_trace_buffer_usage_callback_; | |
202 | |
203 base::ObserverList<TraceMessageFilterObserver> | 120 base::ObserverList<TraceMessageFilterObserver> |
204 trace_message_filter_observers_; | 121 trace_message_filter_observers_; |
205 | 122 |
206 std::set<std::string> known_category_groups_; | |
207 std::set<TracingUI*> tracing_uis_; | 123 std::set<TracingUI*> tracing_uis_; |
208 scoped_refptr<TraceDataSink> trace_data_sink_; | 124 scoped_refptr<TraceDataSink> trace_data_sink_; |
209 scoped_refptr<TraceDataSink> monitoring_data_sink_; | 125 scoped_refptr<TraceDataSink> monitoring_data_sink_; |
210 std::unique_ptr<base::DictionaryValue> metadata_; | 126 std::unique_ptr<base::DictionaryValue> metadata_; |
| 127 bool is_tracing_ = false; |
| 128 |
| 129 resource_coordinator::tracing::mojom::CoordinatorPtr coordinator_; |
| 130 std::unique_ptr<mojo::common::DataPipeDrainer> drainer_; |
211 | 131 |
212 DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl); | 132 DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl); |
213 }; | 133 }; |
214 | 134 |
215 } // namespace content | 135 } // namespace content |
216 | 136 |
217 #endif // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_ | 137 #endif // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_ |
OLD | NEW |