OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "content/browser/tracing/background_tracing_manager_impl.h" | 5 #include "content/browser/tracing/background_tracing_manager_impl.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/time/time.h" | |
8 #include "content/public/browser/background_tracing_preemptive_config.h" | 9 #include "content/public/browser/background_tracing_preemptive_config.h" |
9 #include "content/public/browser/background_tracing_reactive_config.h" | 10 #include "content/public/browser/background_tracing_reactive_config.h" |
10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
11 | 12 |
12 namespace content { | 13 namespace content { |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 base::LazyInstance<BackgroundTracingManagerImpl>::Leaky g_controller = | 17 base::LazyInstance<BackgroundTracingManagerImpl>::Leaky g_controller = |
17 LAZY_INSTANCE_INITIALIZER; | 18 LAZY_INSTANCE_INITIALIZER; |
(...skipping 13 matching lines...) Expand all Loading... | |
31 void BackgroundTracingManagerImpl::TraceDataEndpointWrapper:: | 32 void BackgroundTracingManagerImpl::TraceDataEndpointWrapper:: |
32 ReceiveTraceFinalContents(const std::string& file_contents) { | 33 ReceiveTraceFinalContents(const std::string& file_contents) { |
33 std::string tmp = file_contents; | 34 std::string tmp = file_contents; |
34 scoped_refptr<base::RefCountedString> contents_ptr = | 35 scoped_refptr<base::RefCountedString> contents_ptr = |
35 base::RefCountedString::TakeString(&tmp); | 36 base::RefCountedString::TakeString(&tmp); |
36 | 37 |
37 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 38 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
38 base::Bind(done_callback_, contents_ptr)); | 39 base::Bind(done_callback_, contents_ptr)); |
39 } | 40 } |
40 | 41 |
42 BackgroundTracingManagerImpl::TracingTimer::TracingTimer( | |
43 StartedFinalizingCallback callback) : callback_(callback) { | |
44 } | |
45 | |
46 BackgroundTracingManagerImpl::TracingTimer::~TracingTimer() { | |
47 } | |
48 | |
49 void BackgroundTracingManagerImpl::TracingTimer::StartTimer() { | |
50 tracing_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(10), this, | |
dsinclair
2015/06/03 14:15:30
nit: Let's put the 10 into a constant.
fmeawad
2015/06/03 18:00:18
Done.
| |
51 &BackgroundTracingManagerImpl::TracingTimer::TracingTimerFired); | |
52 } | |
53 | |
54 void BackgroundTracingManagerImpl::TracingTimer::CancelTimer() { | |
55 tracing_timer_.Stop(); | |
56 } | |
57 | |
58 void BackgroundTracingManagerImpl::TracingTimer::TracingTimerFired() { | |
59 BackgroundTracingManagerImpl::GetInstance()->BeginFinalizing(callback_); | |
60 } | |
61 | |
62 void BackgroundTracingManagerImpl::TracingTimer::FireTimerForTesting() { | |
63 CancelTimer(); | |
64 TracingTimerFired(); | |
65 } | |
66 | |
41 BackgroundTracingManager* BackgroundTracingManager::GetInstance() { | 67 BackgroundTracingManager* BackgroundTracingManager::GetInstance() { |
42 return BackgroundTracingManagerImpl::GetInstance(); | 68 return BackgroundTracingManagerImpl::GetInstance(); |
43 } | 69 } |
44 | 70 |
45 BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() { | 71 BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() { |
46 return g_controller.Pointer(); | 72 return g_controller.Pointer(); |
47 } | 73 } |
48 | 74 |
49 BackgroundTracingManagerImpl::BackgroundTracingManagerImpl() | 75 BackgroundTracingManagerImpl::BackgroundTracingManagerImpl() |
50 : is_gathering_(false), | 76 : is_gathering_(false), |
(...skipping 14 matching lines...) Expand all Loading... | |
65 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 91 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
66 idle_callback_ = idle_callback; | 92 idle_callback_ = idle_callback; |
67 } | 93 } |
68 | 94 |
69 bool BackgroundTracingManagerImpl::IsSupportedConfig( | 95 bool BackgroundTracingManagerImpl::IsSupportedConfig( |
70 BackgroundTracingConfig* config) { | 96 BackgroundTracingConfig* config) { |
71 // No config is just fine, we just don't do anything. | 97 // No config is just fine, we just don't do anything. |
72 if (!config) | 98 if (!config) |
73 return true; | 99 return true; |
74 | 100 |
75 // TODO(simonhatch): Implement reactive tracing path. | 101 if (config->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
76 if (config->mode != BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) | 102 BackgroundTracingPreemptiveConfig* preemptive_config = |
77 return false; | 103 static_cast<BackgroundTracingPreemptiveConfig*>(config); |
104 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>& | |
105 configs = preemptive_config->configs; | |
106 for (size_t i = 0; i < configs.size(); ++i) { | |
107 if (configs[i].type != | |
108 BackgroundTracingPreemptiveConfig:: | |
109 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) | |
110 return false; | |
111 } | |
112 } | |
78 | 113 |
79 // TODO(fmeawad): Implement uma triggers. | 114 if (config->mode == BackgroundTracingConfig::REACTIVE_TRACING_MODE) { |
80 BackgroundTracingPreemptiveConfig* preemptive_config = | 115 BackgroundTracingReactiveConfig* reactive_config = |
81 static_cast<BackgroundTracingPreemptiveConfig*>(config); | 116 static_cast<BackgroundTracingReactiveConfig*>(config); |
82 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>& | 117 const std::vector<BackgroundTracingReactiveConfig::TracingRule>& |
83 configs = preemptive_config->configs; | 118 configs = reactive_config->configs; |
84 for (size_t i = 0; i < configs.size(); ++i) { | 119 for (size_t i = 0; i < configs.size(); ++i) { |
85 if (configs[i].type != | 120 if (configs[i].type != |
86 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) | 121 BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL) |
87 return false; | 122 return false; |
123 } | |
88 } | 124 } |
89 | 125 |
90 return true; | 126 return true; |
91 } | 127 } |
92 | 128 |
93 bool BackgroundTracingManagerImpl::SetActiveScenario( | 129 bool BackgroundTracingManagerImpl::SetActiveScenario( |
94 scoped_ptr<BackgroundTracingConfig> config, | 130 scoped_ptr<BackgroundTracingConfig> config, |
95 const BackgroundTracingManager::ReceiveCallback& receive_callback, | 131 const BackgroundTracingManager::ReceiveCallback& receive_callback, |
96 bool requires_anonymized_data) { | 132 bool requires_anonymized_data) { |
97 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 133 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
(...skipping 16 matching lines...) Expand all Loading... | |
114 return true; | 150 return true; |
115 } | 151 } |
116 | 152 |
117 void BackgroundTracingManagerImpl::EnableRecordingIfConfigNeedsIt() { | 153 void BackgroundTracingManagerImpl::EnableRecordingIfConfigNeedsIt() { |
118 if (!config_) | 154 if (!config_) |
119 return; | 155 return; |
120 | 156 |
121 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { | 157 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
122 EnableRecording(GetCategoryFilterStringForCategoryPreset( | 158 EnableRecording(GetCategoryFilterStringForCategoryPreset( |
123 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()) | 159 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()) |
124 ->category_preset)); | 160 ->category_preset), |
125 } else { | 161 base::trace_event::RECORD_CONTINUOUSLY); |
126 // TODO(simonhatch): Implement reactive tracing path. | |
127 NOTREACHED(); | |
128 } | 162 } |
163 // There is nothing to do in case of reactive tracing. | |
129 } | 164 } |
130 | 165 |
131 bool BackgroundTracingManagerImpl::IsAbleToTriggerTracing( | 166 bool BackgroundTracingManagerImpl::IsAbleToTriggerTracing( |
132 TriggerHandle handle) const { | 167 TriggerHandle handle) const { |
133 if (!config_) | 168 if (!config_) |
134 return false; | 169 return false; |
135 | 170 |
136 // If the last trace is still uploading, we don't allow a new one to trigger. | 171 // If the last trace is still uploading, we don't allow a new one to trigger. |
137 if (is_gathering_) | 172 if (is_gathering_) |
138 return false; | 173 return false; |
(...skipping 14 matching lines...) Expand all Loading... | |
153 for (size_t i = 0; i < configs.size(); ++i) { | 188 for (size_t i = 0; i < configs.size(); ++i) { |
154 if (configs[i].type != BackgroundTracingPreemptiveConfig:: | 189 if (configs[i].type != BackgroundTracingPreemptiveConfig:: |
155 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) | 190 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) |
156 continue; | 191 continue; |
157 | 192 |
158 if (trigger_name == configs[i].named_trigger_info.trigger_name) { | 193 if (trigger_name == configs[i].named_trigger_info.trigger_name) { |
159 return true; | 194 return true; |
160 } | 195 } |
161 } | 196 } |
162 } else { | 197 } else { |
163 // TODO(simonhatch): Implement reactive path. | 198 BackgroundTracingReactiveConfig* reactive_config = |
164 NOTREACHED(); | 199 static_cast<BackgroundTracingReactiveConfig*>(config_.get()); |
200 | |
201 const std::vector<BackgroundTracingReactiveConfig::TracingRule>& | |
202 configs = reactive_config->configs; | |
203 | |
204 for (size_t i = 0; i < configs.size(); ++i) { | |
205 if (configs[i].type != | |
206 BackgroundTracingReactiveConfig:: | |
207 TRACE_FOR_10S_OR_TRIGGER_OR_FULL) | |
208 continue; | |
209 if (trigger_name == configs[i].trigger_name) { | |
210 return true; | |
211 } | |
212 } | |
165 } | 213 } |
166 | |
167 return false; | 214 return false; |
168 } | 215 } |
169 | 216 |
170 void BackgroundTracingManagerImpl::TriggerNamedEvent( | 217 void BackgroundTracingManagerImpl::TriggerNamedEvent( |
171 BackgroundTracingManagerImpl::TriggerHandle handle, | 218 BackgroundTracingManagerImpl::TriggerHandle handle, |
172 StartedFinalizingCallback callback) { | 219 StartedFinalizingCallback callback) { |
173 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 220 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
174 content::BrowserThread::PostTask( | 221 content::BrowserThread::PostTask( |
175 content::BrowserThread::UI, FROM_HERE, | 222 content::BrowserThread::UI, FROM_HERE, |
176 base::Bind(&BackgroundTracingManagerImpl::TriggerNamedEvent, | 223 base::Bind(&BackgroundTracingManagerImpl::TriggerNamedEvent, |
177 base::Unretained(this), handle, callback)); | 224 base::Unretained(this), handle, callback)); |
178 return; | 225 return; |
179 } | 226 } |
180 | 227 |
181 if (!IsAbleToTriggerTracing(handle)) { | 228 if (!IsAbleToTriggerTracing(handle)) { |
182 if (!callback.is_null()) | 229 if (!callback.is_null()) |
183 callback.Run(false); | 230 callback.Run(false); |
184 return; | 231 return; |
185 } | 232 } |
186 | 233 |
187 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { | 234 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
188 BeginFinalizing(callback); | 235 BeginFinalizing(callback); |
189 } else { | 236 } else { |
190 // TODO(simonhatch): Implement reactive tracing path. | 237 if (is_tracing_) { |
191 NOTREACHED(); | 238 tracing_timer_->CancelTimer(); |
239 BeginFinalizing(callback); | |
240 return; | |
241 } | |
242 | |
243 // It was not already tracing, start a new trace. | |
244 BackgroundTracingReactiveConfig* reactive_config = | |
245 static_cast<BackgroundTracingReactiveConfig*>(config_.get()); | |
246 const std::vector<BackgroundTracingReactiveConfig::TracingRule>& | |
247 configs = reactive_config->configs; | |
248 std::string trigger_name = GetTriggerNameFromHandle(handle); | |
249 for (size_t i = 0; i < configs.size(); ++i) { | |
250 if (configs[i].trigger_name == trigger_name) { | |
251 EnableRecording( | |
252 GetCategoryFilterStringForCategoryPreset( | |
253 configs[i].category_preset), | |
254 base::trace_event::RECORD_UNTIL_FULL); | |
255 tracing_timer_.reset(new TracingTimer(callback)); | |
256 tracing_timer_->StartTimer(); | |
257 break; | |
258 } | |
259 } | |
192 } | 260 } |
193 } | 261 } |
194 | 262 |
195 BackgroundTracingManagerImpl::TriggerHandle | 263 BackgroundTracingManagerImpl::TriggerHandle |
196 BackgroundTracingManagerImpl::RegisterTriggerType(const char* trigger_name) { | 264 BackgroundTracingManagerImpl::RegisterTriggerType(const char* trigger_name) { |
197 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 265 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
198 | 266 |
199 trigger_handle_ids_ += 1; | 267 trigger_handle_ids_ += 1; |
200 | 268 |
201 trigger_handles_.insert( | 269 trigger_handles_.insert( |
(...skipping 18 matching lines...) Expand all Loading... | |
220 for (std::map<TriggerHandle, std::string>::iterator it = | 288 for (std::map<TriggerHandle, std::string>::iterator it = |
221 trigger_handles_.begin(); | 289 trigger_handles_.begin(); |
222 it != trigger_handles_.end(); ++it) | 290 it != trigger_handles_.end(); ++it) |
223 trigger_names->push_back(it->second); | 291 trigger_names->push_back(it->second); |
224 } | 292 } |
225 | 293 |
226 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() { | 294 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() { |
227 trigger_handles_.clear(); | 295 trigger_handles_.clear(); |
228 } | 296 } |
229 | 297 |
298 void BackgroundTracingManagerImpl::FireTimerForTesting() { | |
299 tracing_timer_->FireTimerForTesting(); | |
300 } | |
301 | |
230 void BackgroundTracingManagerImpl::EnableRecording( | 302 void BackgroundTracingManagerImpl::EnableRecording( |
231 std::string category_filter_str) { | 303 std::string category_filter_str, |
304 base::trace_event::TraceRecordMode record_mode) { | |
232 is_tracing_ = TracingController::GetInstance()->EnableRecording( | 305 is_tracing_ = TracingController::GetInstance()->EnableRecording( |
233 base::trace_event::TraceConfig(category_filter_str, | 306 base::trace_event::TraceConfig(category_filter_str, record_mode), |
234 base::trace_event::RECORD_CONTINUOUSLY), | |
235 TracingController::EnableRecordingDoneCallback()); | 307 TracingController::EnableRecordingDoneCallback()); |
236 } | 308 } |
237 | 309 |
238 void BackgroundTracingManagerImpl::OnFinalizeStarted( | 310 void BackgroundTracingManagerImpl::OnFinalizeStarted( |
239 scoped_refptr<base::RefCountedString> file_contents) { | 311 scoped_refptr<base::RefCountedString> file_contents) { |
240 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 312 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
241 | 313 |
242 if (!receive_callback_.is_null()) | 314 if (!receive_callback_.is_null()) |
243 receive_callback_.Run( | 315 receive_callback_.Run( |
244 file_contents.get(), | 316 file_contents.get(), |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 return NULL; | 373 return NULL; |
302 } | 374 } |
303 | 375 |
304 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config, | 376 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config, |
305 base::DictionaryValue* dict) { | 377 base::DictionaryValue* dict) { |
306 // TODO(simonhatch): Implement this. | 378 // TODO(simonhatch): Implement this. |
307 CHECK(false); | 379 CHECK(false); |
308 } | 380 } |
309 | 381 |
310 } // namspace content | 382 } // namspace content |
OLD | NEW |