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), 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 scoped_refptr<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.get()); |
| 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_refptr<BackgroundTracingConfig> config, |
| 93 scoped_refptr<BackgroundTracingUploadSink> upload_sink) { |
| 94 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 95 if (!IsSupportedConfig(config)) |
| 96 return false; |
| 97 |
| 98 // No point in tracing if there's nowhere to send it. |
| 99 if (config && !upload_sink) |
| 100 return false; |
| 101 |
| 102 config_ = config; |
| 103 |
| 104 data_endpoint_wrapper_->SetUploadSink(upload_sink); |
| 105 |
| 106 EnableRecordingIfConfigNeedsIt(); |
| 107 |
| 108 return true; |
| 109 } |
| 110 |
| 111 void BackgroundTracingManagerImpl::EnableRecordingIfConfigNeedsIt() { |
| 112 if (!config_) |
| 113 return; |
| 114 |
| 115 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
| 116 EnableRecording(GetCategoryFilterForCategoryPreset( |
| 117 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()) |
| 118 ->category_preset)); |
| 119 } else { |
| 120 // TODO(simonhatch): Implement reactive tracing path. |
| 121 NOTREACHED(); |
| 122 } |
| 123 } |
| 124 |
| 125 bool BackgroundTracingManagerImpl::IsAbleToTriggerTracing( |
| 126 TriggerHandle handle) const { |
| 127 if (!config_) |
| 128 return false; |
| 129 |
| 130 // If the last trace is still uploading, we don't allow a new one to trigger. |
| 131 if (is_gathering_) |
| 132 return false; |
| 133 |
| 134 if (!IsTriggerHandleValid(handle)) { |
| 135 return false; |
| 136 } |
| 137 |
| 138 std::string trigger_name = GetTriggerNameFromHandle(handle); |
| 139 |
| 140 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
| 141 BackgroundTracingPreemptiveConfig* preemptive_config = |
| 142 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()); |
| 143 |
| 144 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>& |
| 145 configs = preemptive_config->configs; |
| 146 |
| 147 for (size_t i = 0; i < configs.size(); ++i) { |
| 148 if (configs[i].type != BackgroundTracingPreemptiveConfig:: |
| 149 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) |
| 150 continue; |
| 151 |
| 152 if (trigger_name == configs[i].named_trigger_info.trigger_name) { |
| 153 return true; |
| 154 } |
| 155 } |
| 156 } else { |
| 157 // TODO(simonhatch): Implement reactive path. |
| 158 NOTREACHED(); |
| 159 } |
| 160 |
| 161 return false; |
| 162 } |
| 163 |
| 164 void BackgroundTracingManagerImpl::DidTriggerHappen( |
| 165 BackgroundTracingManagerImpl::TriggerHandle handle, |
| 166 StartedFinalizingCallback callback) { |
| 167 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 168 content::BrowserThread::PostTask( |
| 169 content::BrowserThread::UI, FROM_HERE, |
| 170 base::Bind(&BackgroundTracingManagerImpl::DidTriggerHappen, |
| 171 base::Unretained(this), handle, callback)); |
| 172 return; |
| 173 } |
| 174 |
| 175 if (!IsAbleToTriggerTracing(handle)) { |
| 176 if (!callback.is_null()) |
| 177 callback.Run(false); |
| 178 return; |
| 179 } |
| 180 |
| 181 if (config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) { |
| 182 BeginFinalizing(callback); |
| 183 } else { |
| 184 // TODO(simonhatch): Implement reactive tracing path. |
| 185 NOTREACHED(); |
| 186 } |
| 187 } |
| 188 |
| 189 BackgroundTracingManagerImpl::TriggerHandle |
| 190 BackgroundTracingManagerImpl::RegisterTriggerType(const char* trigger_name) { |
| 191 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 192 |
| 193 trigger_handle_ids_ += 1; |
| 194 |
| 195 trigger_handles_.insert( |
| 196 std::pair<TriggerHandle, std::string>(trigger_handle_ids_, trigger_name)); |
| 197 |
| 198 return static_cast<TriggerHandle>(trigger_handle_ids_); |
| 199 } |
| 200 |
| 201 bool BackgroundTracingManagerImpl::IsTriggerHandleValid( |
| 202 BackgroundTracingManager::TriggerHandle handle) const { |
| 203 return trigger_handles_.find(handle) != trigger_handles_.end(); |
| 204 } |
| 205 |
| 206 std::string BackgroundTracingManagerImpl::GetTriggerNameFromHandle( |
| 207 BackgroundTracingManager::TriggerHandle handle) const { |
| 208 CHECK(IsTriggerHandleValid(handle)); |
| 209 return trigger_handles_.find(handle)->second; |
| 210 } |
| 211 |
| 212 void BackgroundTracingManagerImpl::GetTriggerNameList( |
| 213 std::vector<std::string>& trigger_names) { |
| 214 for (std::map<TriggerHandle, std::string>::iterator it = |
| 215 trigger_handles_.begin(); |
| 216 it != trigger_handles_.end(); ++it) |
| 217 trigger_names.push_back(it->second); |
| 218 } |
| 219 |
| 220 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() { |
| 221 trigger_handles_.clear(); |
| 222 } |
| 223 |
| 224 void BackgroundTracingManagerImpl::EnableRecording( |
| 225 base::trace_event::CategoryFilter category_filter) { |
| 226 TracingController::GetInstance()->EnableRecording( |
| 227 category_filter, |
| 228 base::trace_event::TraceOptions(base::trace_event::RECORD_CONTINUOUSLY), |
| 229 TracingController::EnableRecordingDoneCallback()); |
| 230 } |
| 231 |
| 232 void BackgroundTracingManagerImpl::OnFinalizeComplete() { |
| 233 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 234 BrowserThread::PostTask( |
| 235 BrowserThread::UI, FROM_HERE, |
| 236 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete, |
| 237 base::Unretained(this))); |
| 238 return; |
| 239 } |
| 240 |
| 241 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 242 |
| 243 is_gathering_ = false; |
| 244 |
| 245 if (!idle_callback_.is_null()) |
| 246 idle_callback_.Run(); |
| 247 |
| 248 // Now that a trace has completed, we may need to enable recording again. |
| 249 EnableRecordingIfConfigNeedsIt(); |
| 250 } |
| 251 |
| 252 void BackgroundTracingManagerImpl::BeginFinalizing( |
| 253 StartedFinalizingCallback callback) { |
| 254 is_gathering_ = true; |
| 255 |
| 256 content::TracingController::GetInstance()->DisableRecording( |
| 257 content::TracingController::CreateCompressedStringSink( |
| 258 data_endpoint_wrapper_)); |
| 259 |
| 260 if (!callback.is_null()) |
| 261 callback.Run(true); |
| 262 } |
| 263 |
| 264 base::trace_event::CategoryFilter |
| 265 BackgroundTracingManagerImpl::GetCategoryFilterForCategoryPreset( |
| 266 BackgroundTracingConfig::CategoryPreset preset) const { |
| 267 switch (preset) { |
| 268 case BackgroundTracingConfig::CategoryPreset::BENCHMARK: |
| 269 return base::trace_event::CategoryFilter( |
| 270 "benchmark," |
| 271 "disabled-by-default-toplevel.flow," |
| 272 "disabled-by-default-ipc.flow"); |
| 273 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP: |
| 274 return base::trace_event::CategoryFilter( |
| 275 "*,disabled-by-default-blink.debug.layout"); |
| 276 } |
| 277 NOTREACHED(); |
| 278 return base::trace_event::CategoryFilter(); |
| 279 } |
| 280 |
| 281 BackgroundTracingConfig* BackgroundTracingConfig::FromDict( |
| 282 const base::DictionaryValue* dict) { |
| 283 // TODO(simonhatch): Implement this. |
| 284 CHECK(false); |
| 285 return NULL; |
| 286 } |
| 287 |
| 288 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config, |
| 289 base::DictionaryValue* dict) { |
| 290 // TODO(simonhatch): Implement this. |
| 291 CHECK(false); |
| 292 } |
| 293 |
| 294 } // namspace content |
OLD | NEW |