OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/tracing/background_tracing_manager_impl.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #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_upload_sink.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 namespace { |
| 16 |
| 17 base::LazyInstance<BackgroundTracingManagerImpl>::Leaky g_controller = |
| 18 LAZY_INSTANCE_INITIALIZER; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 BackgroundTracingManagerImpl::TraceDataEndpointWrapper:: |
| 23 TraceDataEndpointWrapper(base::Callback<void()> done_callback) |
| 24 : done_callback_(done_callback) { |
| 25 } |
| 26 |
| 27 BackgroundTracingManagerImpl::TraceDataEndpointWrapper:: |
| 28 ~TraceDataEndpointWrapper() { |
| 29 } |
| 30 |
| 31 void BackgroundTracingManagerImpl::TraceDataEndpointWrapper:: |
| 32 ReceiveTraceFinalContents(const std::string& file_contents) { |
| 33 upload_sink_->Upload(file_contents, done_callback_); |
| 34 } |
| 35 |
| 36 void BackgroundTracingManagerImpl::TraceDataEndpointWrapper::SetUploadSink( |
| 37 scoped_refptr<content::BackgroundTracingUploadSink> upload_sink) { |
| 38 upload_sink_ = upload_sink; |
| 39 } |
| 40 |
| 41 BackgroundTracingManager* BackgroundTracingManager::GetInstance() { |
| 42 return BackgroundTracingManagerImpl::GetInstance(); |
| 43 } |
| 44 |
| 45 BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() { |
| 46 return g_controller.Pointer(); |
| 47 } |
| 48 |
| 49 BackgroundTracingManagerImpl::BackgroundTracingManagerImpl() |
| 50 : is_gathering_(false), is_tracing_(false), trigger_handle_ids_(0) { |
| 51 data_endpoint_wrapper_ = new TraceDataEndpointWrapper( |
| 52 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete, |
| 53 base::Unretained(this))); |
| 54 } |
| 55 |
| 56 BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() { |
| 57 NOTREACHED(); |
| 58 } |
| 59 |
| 60 void BackgroundTracingManagerImpl::WhenIdle( |
| 61 base::Callback<void()> idle_callback) { |
| 62 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 63 idle_callback_ = idle_callback; |
| 64 } |
| 65 |
| 66 bool BackgroundTracingManagerImpl::IsSupportedConfig( |
| 67 BackgroundTracingConfig* config) { |
| 68 // No config is just fine, we just don't do anything. |
| 69 if (!config) |
| 70 return true; |
| 71 |
| 72 // TODO(simonhatch): Implement reactive tracing path. |
| 73 if (config->mode != BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) |
| 74 return false; |
| 75 |
| 76 // TODO(fmeawad): Implement uma triggers. |
| 77 BackgroundTracingPreemptiveConfig* preemptive_config = |
| 78 static_cast<BackgroundTracingPreemptiveConfig*>(config); |
| 79 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>& |
| 80 configs = preemptive_config->configs; |
| 81 for (size_t i = 0; i < configs.size(); ++i) { |
| 82 if (configs[i].type != |
| 83 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) |
| 84 return false; |
| 85 ; |
| 86 } |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 bool BackgroundTracingManagerImpl::SetActiveScenario( |
| 92 scoped_ptr<BackgroundTracingConfig> config, |
| 93 scoped_refptr<BackgroundTracingUploadSink> upload_sink) { |
| 94 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 95 if (is_tracing_) |
| 96 return false; |
| 97 |
| 98 if (!IsSupportedConfig(config.get())) |
| 99 return false; |
| 100 |
| 101 // No point in tracing if there's nowhere to send it. |
| 102 if (config && !upload_sink) |
| 103 return false; |
| 104 |
| 105 config_ = config.Pass(); |
| 106 |
| 107 data_endpoint_wrapper_->SetUploadSink(upload_sink); |
| 108 |
| 109 EnableRecordingIfConfigNeedsIt(); |
| 110 |
| 111 return true; |
| 112 } |
| 113 |
| 114 void BackgroundTracingManagerImpl::EnableRecordingIfConfigNeedsIt() { |
| 115 if (!config_) |
| 116 return; |
| 117 |
| 118 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
| 119 EnableRecording(GetCategoryFilterForCategoryPreset( |
| 120 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()) |
| 121 ->category_preset)); |
| 122 } else { |
| 123 // TODO(simonhatch): Implement reactive tracing path. |
| 124 NOTREACHED(); |
| 125 } |
| 126 } |
| 127 |
| 128 bool BackgroundTracingManagerImpl::IsAbleToTriggerTracing( |
| 129 TriggerHandle handle) const { |
| 130 if (!config_) |
| 131 return false; |
| 132 |
| 133 // If the last trace is still uploading, we don't allow a new one to trigger. |
| 134 if (is_gathering_) |
| 135 return false; |
| 136 |
| 137 if (!IsTriggerHandleValid(handle)) { |
| 138 return false; |
| 139 } |
| 140 |
| 141 std::string trigger_name = GetTriggerNameFromHandle(handle); |
| 142 |
| 143 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
| 144 BackgroundTracingPreemptiveConfig* preemptive_config = |
| 145 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()); |
| 146 |
| 147 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>& |
| 148 configs = preemptive_config->configs; |
| 149 |
| 150 for (size_t i = 0; i < configs.size(); ++i) { |
| 151 if (configs[i].type != BackgroundTracingPreemptiveConfig:: |
| 152 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) |
| 153 continue; |
| 154 |
| 155 if (trigger_name == configs[i].named_trigger_info.trigger_name) { |
| 156 return true; |
| 157 } |
| 158 } |
| 159 } else { |
| 160 // TODO(simonhatch): Implement reactive path. |
| 161 NOTREACHED(); |
| 162 } |
| 163 |
| 164 return false; |
| 165 } |
| 166 |
| 167 void BackgroundTracingManagerImpl::TriggerNamedEvent( |
| 168 BackgroundTracingManagerImpl::TriggerHandle handle, |
| 169 StartedFinalizingCallback callback) { |
| 170 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 171 content::BrowserThread::PostTask( |
| 172 content::BrowserThread::UI, FROM_HERE, |
| 173 base::Bind(&BackgroundTracingManagerImpl::TriggerNamedEvent, |
| 174 base::Unretained(this), handle, callback)); |
| 175 return; |
| 176 } |
| 177 |
| 178 if (!IsAbleToTriggerTracing(handle)) { |
| 179 if (!callback.is_null()) |
| 180 callback.Run(false); |
| 181 return; |
| 182 } |
| 183 |
| 184 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
| 185 BeginFinalizing(callback); |
| 186 } else { |
| 187 // TODO(simonhatch): Implement reactive tracing path. |
| 188 NOTREACHED(); |
| 189 } |
| 190 } |
| 191 |
| 192 BackgroundTracingManagerImpl::TriggerHandle |
| 193 BackgroundTracingManagerImpl::RegisterTriggerType(const char* trigger_name) { |
| 194 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 195 |
| 196 trigger_handle_ids_ += 1; |
| 197 |
| 198 trigger_handles_.insert( |
| 199 std::pair<TriggerHandle, std::string>(trigger_handle_ids_, trigger_name)); |
| 200 |
| 201 return static_cast<TriggerHandle>(trigger_handle_ids_); |
| 202 } |
| 203 |
| 204 bool BackgroundTracingManagerImpl::IsTriggerHandleValid( |
| 205 BackgroundTracingManager::TriggerHandle handle) const { |
| 206 return trigger_handles_.find(handle) != trigger_handles_.end(); |
| 207 } |
| 208 |
| 209 std::string BackgroundTracingManagerImpl::GetTriggerNameFromHandle( |
| 210 BackgroundTracingManager::TriggerHandle handle) const { |
| 211 CHECK(IsTriggerHandleValid(handle)); |
| 212 return trigger_handles_.find(handle)->second; |
| 213 } |
| 214 |
| 215 void BackgroundTracingManagerImpl::GetTriggerNameList( |
| 216 std::vector<std::string>& trigger_names) { |
| 217 for (std::map<TriggerHandle, std::string>::iterator it = |
| 218 trigger_handles_.begin(); |
| 219 it != trigger_handles_.end(); ++it) |
| 220 trigger_names.push_back(it->second); |
| 221 } |
| 222 |
| 223 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() { |
| 224 trigger_handles_.clear(); |
| 225 } |
| 226 |
| 227 void BackgroundTracingManagerImpl::EnableRecording( |
| 228 base::trace_event::CategoryFilter category_filter) { |
| 229 is_tracing_ = TracingController::GetInstance()->EnableRecording( |
| 230 category_filter, |
| 231 base::trace_event::TraceOptions(base::trace_event::RECORD_CONTINUOUSLY), |
| 232 TracingController::EnableRecordingDoneCallback()); |
| 233 } |
| 234 |
| 235 void BackgroundTracingManagerImpl::OnFinalizeComplete() { |
| 236 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 237 BrowserThread::PostTask( |
| 238 BrowserThread::UI, FROM_HERE, |
| 239 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete, |
| 240 base::Unretained(this))); |
| 241 return; |
| 242 } |
| 243 |
| 244 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 245 |
| 246 is_gathering_ = false; |
| 247 |
| 248 if (!idle_callback_.is_null()) |
| 249 idle_callback_.Run(); |
| 250 |
| 251 // Now that a trace has completed, we may need to enable recording again. |
| 252 EnableRecordingIfConfigNeedsIt(); |
| 253 } |
| 254 |
| 255 void BackgroundTracingManagerImpl::BeginFinalizing( |
| 256 StartedFinalizingCallback callback) { |
| 257 is_gathering_ = true; |
| 258 is_tracing_ = false; |
| 259 |
| 260 content::TracingController::GetInstance()->DisableRecording( |
| 261 content::TracingController::CreateCompressedStringSink( |
| 262 data_endpoint_wrapper_)); |
| 263 |
| 264 if (!callback.is_null()) |
| 265 callback.Run(true); |
| 266 } |
| 267 |
| 268 base::trace_event::CategoryFilter |
| 269 BackgroundTracingManagerImpl::GetCategoryFilterForCategoryPreset( |
| 270 BackgroundTracingConfig::CategoryPreset preset) const { |
| 271 switch (preset) { |
| 272 case BackgroundTracingConfig::CategoryPreset::BENCHMARK: |
| 273 return base::trace_event::CategoryFilter( |
| 274 "benchmark," |
| 275 "disabled-by-default-toplevel.flow," |
| 276 "disabled-by-default-ipc.flow"); |
| 277 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP: |
| 278 return base::trace_event::CategoryFilter( |
| 279 "*,disabled-by-default-blink.debug.layout"); |
| 280 } |
| 281 NOTREACHED(); |
| 282 return base::trace_event::CategoryFilter(); |
| 283 } |
| 284 |
| 285 BackgroundTracingConfig* BackgroundTracingConfig::FromDict( |
| 286 const base::DictionaryValue* dict) { |
| 287 // TODO(simonhatch): Implement this. |
| 288 CHECK(false); |
| 289 return NULL; |
| 290 } |
| 291 |
| 292 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config, |
| 293 base::DictionaryValue* dict) { |
| 294 // TODO(simonhatch): Implement this. |
| 295 CHECK(false); |
| 296 } |
| 297 |
| 298 } // namspace content |
OLD | NEW |