Chromium Code Reviews| 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/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "components/tracing/tracing_messages.h" | |
| 13 #include "content/browser/tracing/trace_message_filter.h" | |
| 12 #include "content/public/browser/background_tracing_preemptive_config.h" | 14 #include "content/public/browser/background_tracing_preemptive_config.h" |
| 13 #include "content/public/browser/background_tracing_reactive_config.h" | 15 #include "content/public/browser/background_tracing_reactive_config.h" |
| 14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/content_browser_client.h" | 17 #include "content/public/browser/content_browser_client.h" |
| 16 #include "content/public/browser/tracing_delegate.h" | 18 #include "content/public/browser/tracing_delegate.h" |
| 17 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
| 18 | 20 |
| 19 namespace content { | 21 namespace content { |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() { | 82 BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() { |
| 81 return g_controller.Pointer(); | 83 return g_controller.Pointer(); |
| 82 } | 84 } |
| 83 | 85 |
| 84 BackgroundTracingManagerImpl::BackgroundTracingManagerImpl() | 86 BackgroundTracingManagerImpl::BackgroundTracingManagerImpl() |
| 85 : delegate_(GetContentClient()->browser()->GetTracingDelegate()), | 87 : delegate_(GetContentClient()->browser()->GetTracingDelegate()), |
| 86 is_gathering_(false), | 88 is_gathering_(false), |
| 87 is_tracing_(false), | 89 is_tracing_(false), |
| 88 requires_anonymized_data_(true), | 90 requires_anonymized_data_(true), |
| 89 trigger_handle_ids_(0) { | 91 trigger_handle_ids_(0) { |
| 92 TracingControllerImpl::GetInstance()->SetTraceMessageFilterAddedCallback( | |
| 93 base::Bind(&BackgroundTracingManagerImpl::OnTraceMessageFilterAdded, | |
| 94 base::Unretained(this))); | |
|
dcheng
2015/07/23 21:11:33
Why is it safe to use Unretained here? This callba
shatch
2015/07/23 23:06:51
This is a leaky instance, we shouldn't hit the dto
| |
| 90 } | 95 } |
| 91 | 96 |
| 92 BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() { | 97 BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() { |
| 93 NOTREACHED(); | 98 NOTREACHED(); |
| 94 } | 99 } |
| 95 | 100 |
| 96 void BackgroundTracingManagerImpl::WhenIdle( | 101 void BackgroundTracingManagerImpl::WhenIdle( |
| 97 base::Callback<void()> idle_callback) { | 102 base::Callback<void()> idle_callback) { |
| 98 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 103 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 99 idle_callback_ = idle_callback; | 104 idle_callback_ = idle_callback; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) { | 158 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) { |
| 154 continue; | 159 continue; |
| 155 } | 160 } |
| 156 | 161 |
| 157 if (mode == CLEAR_CALLBACKS) { | 162 if (mode == CLEAR_CALLBACKS) { |
| 158 base::StatisticsRecorder::ClearCallback( | 163 base::StatisticsRecorder::ClearCallback( |
| 159 configs[i].histogram_trigger_info.histogram_name); | 164 configs[i].histogram_trigger_info.histogram_name); |
| 160 } else { | 165 } else { |
| 161 base::StatisticsRecorder::SetCallback( | 166 base::StatisticsRecorder::SetCallback( |
| 162 configs[i].histogram_trigger_info.histogram_name, | 167 configs[i].histogram_trigger_info.histogram_name, |
| 163 base::Bind(&BackgroundTracingManagerImpl::OnHistogramChanged, | 168 base::Bind(&BackgroundTracingManagerImpl::OnHistogramChangedCallback, |
| 164 base::Unretained(this), | 169 base::Unretained(this), |
| 165 configs[i].histogram_trigger_info.histogram_name, | 170 configs[i].histogram_trigger_info.histogram_name, |
| 166 configs[i].histogram_trigger_info.histogram_value)); | 171 configs[i].histogram_trigger_info.histogram_value)); |
| 167 } | 172 } |
| 168 } | 173 } |
| 174 | |
| 175 SetupFiltersFromConfig(mode); | |
| 169 } | 176 } |
| 170 | 177 |
| 171 void BackgroundTracingManagerImpl::OnHistogramChanged( | 178 void BackgroundTracingManagerImpl::OnHistogramTrigger( |
| 172 const std::string& histogram_name, | 179 const std::string& histogram_name) { |
| 173 base::Histogram::Sample reference_value, | |
| 174 base::Histogram::Sample actual_value) { | |
| 175 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 180 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 176 content::BrowserThread::PostTask( | 181 content::BrowserThread::PostTask( |
| 177 content::BrowserThread::UI, FROM_HERE, | 182 content::BrowserThread::UI, FROM_HERE, |
| 178 base::Bind(&BackgroundTracingManagerImpl::OnHistogramChanged, | 183 base::Bind(&BackgroundTracingManagerImpl::OnHistogramTrigger, |
| 179 base::Unretained(this), histogram_name, reference_value, | 184 base::Unretained(this), histogram_name)); |
| 180 actual_value)); | |
| 181 return; | 185 return; |
| 182 } | 186 } |
| 183 | 187 |
| 184 CHECK(config_ && | 188 CHECK(config_ && |
| 185 config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE); | 189 config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE); |
| 186 | 190 |
| 191 if (!is_tracing_ || is_gathering_) | |
| 192 return; | |
| 193 | |
| 194 BackgroundTracingPreemptiveConfig* preemptive_config = | |
| 195 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()); | |
| 196 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>& | |
| 197 configs = preemptive_config->configs; | |
| 198 for (size_t i = 0; i < configs.size(); ++i) { | |
|
dcheng
2015/07/23 21:11:33
for (const auto& config : preemptive_config->confi
shatch
2015/07/23 23:06:51
Oh neat, didn't realize this was recommended now,
| |
| 199 if (configs[i].type != | |
| 200 BackgroundTracingPreemptiveConfig:: | |
| 201 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) { | |
| 202 continue; | |
| 203 } | |
| 204 | |
| 205 if (configs[i].histogram_trigger_info.histogram_name == histogram_name) { | |
| 206 RecordBackgroundTracingMetric(PREEMPTIVE_TRIGGERED); | |
| 207 BeginFinalizing(StartedFinalizingCallback()); | |
| 208 } | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 void BackgroundTracingManagerImpl::OnHistogramChangedCallback( | |
| 213 const std::string& histogram_name, | |
| 214 base::Histogram::Sample reference_value, | |
| 215 base::Histogram::Sample actual_value) { | |
| 187 if (reference_value > actual_value) | 216 if (reference_value > actual_value) |
| 188 return; | 217 return; |
| 189 | 218 |
| 190 if (!is_tracing_ || is_gathering_) | 219 OnHistogramTrigger(histogram_name); |
| 191 return; | |
| 192 | |
| 193 RecordBackgroundTracingMetric(PREEMPTIVE_TRIGGERED); | |
| 194 BeginFinalizing(StartedFinalizingCallback()); | |
| 195 } | 220 } |
| 196 | 221 |
| 197 bool BackgroundTracingManagerImpl::SetActiveScenario( | 222 bool BackgroundTracingManagerImpl::SetActiveScenario( |
| 198 scoped_ptr<BackgroundTracingConfig> config, | 223 scoped_ptr<BackgroundTracingConfig> config, |
| 199 const BackgroundTracingManager::ReceiveCallback& receive_callback, | 224 const BackgroundTracingManager::ReceiveCallback& receive_callback, |
| 200 DataFiltering data_filtering) { | 225 DataFiltering data_filtering) { |
| 201 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 226 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 202 RecordBackgroundTracingMetric(SCENARIO_ACTIVATION_REQUESTED); | 227 RecordBackgroundTracingMetric(SCENARIO_ACTIVATION_REQUESTED); |
| 203 | 228 |
| 204 if (is_tracing_) | 229 if (is_tracing_) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 241 EnableRecordingIfConfigNeedsIt(); | 266 EnableRecordingIfConfigNeedsIt(); |
| 242 | 267 |
| 243 RecordBackgroundTracingMetric(SCENARIO_ACTIVATED_SUCCESSFULLY); | 268 RecordBackgroundTracingMetric(SCENARIO_ACTIVATED_SUCCESSFULLY); |
| 244 return true; | 269 return true; |
| 245 } | 270 } |
| 246 | 271 |
| 247 bool BackgroundTracingManagerImpl::HasActiveScenarioForTesting() { | 272 bool BackgroundTracingManagerImpl::HasActiveScenarioForTesting() { |
| 248 return config_; | 273 return config_; |
| 249 } | 274 } |
| 250 | 275 |
| 276 void BackgroundTracingManagerImpl::OnTraceMessageFilterAdded( | |
| 277 TraceMessageFilter* filter) { | |
| 278 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 279 SetupFilterFromConfig(filter, BIND_CALLBACKS); | |
| 280 } | |
| 281 | |
| 282 void BackgroundTracingManagerImpl::SetupFiltersFromConfig( | |
| 283 BackgroundTracingManagerImpl::SetupUMACallMode mode) { | |
| 284 TracingControllerImpl::TraceMessageFilterSet filters; | |
| 285 TracingControllerImpl::GetInstance()->GetTraceMessageFilters(&filters); | |
| 286 | |
| 287 for (auto it = filters.begin(); it != filters.end(); ++it) | |
|
dcheng
2015/07/23 21:11:33
for (auto& filter : filters)
shatch
2015/07/23 23:06:51
Done.
| |
| 288 SetupFilterFromConfig(*it, mode); | |
| 289 } | |
| 290 | |
| 291 void BackgroundTracingManagerImpl::SetupFilterFromConfig( | |
| 292 scoped_refptr<TraceMessageFilter> filter, | |
| 293 BackgroundTracingManagerImpl::SetupUMACallMode mode) { | |
| 294 if (!config_ || | |
| 295 config_->mode != BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) | |
| 296 return; | |
| 297 | |
| 298 BackgroundTracingPreemptiveConfig* preemptive_config = | |
| 299 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()); | |
| 300 | |
| 301 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>& | |
| 302 configs = preemptive_config->configs; | |
| 303 | |
| 304 for (size_t i = 0; i < configs.size(); ++i) { | |
|
dcheng
2015/07/23 21:11:33
Ditto: use a for each.
shatch
2015/07/23 23:06:51
Done.
| |
| 305 if (configs[i].type != | |
| 306 BackgroundTracingPreemptiveConfig:: | |
| 307 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) { | |
| 308 continue; | |
| 309 } | |
| 310 | |
| 311 if (mode == CLEAR_CALLBACKS) { | |
| 312 filter->Send(new TracingMsg_ClearUMACallback( | |
| 313 configs[i].histogram_trigger_info.histogram_name)); | |
| 314 } else { | |
| 315 filter->Send(new TracingMsg_SetUMACallback( | |
| 316 configs[i].histogram_trigger_info.histogram_name, | |
| 317 configs[i].histogram_trigger_info.histogram_value)); | |
| 318 } | |
| 319 } | |
| 320 } | |
| 321 | |
| 251 void BackgroundTracingManagerImpl::ValidateStartupScenario() { | 322 void BackgroundTracingManagerImpl::ValidateStartupScenario() { |
| 252 if (!config_ || !delegate_) | 323 if (!config_ || !delegate_) |
| 253 return; | 324 return; |
| 254 | 325 |
| 255 if (!delegate_->IsAllowedToBeginBackgroundScenario( | 326 if (!delegate_->IsAllowedToBeginBackgroundScenario( |
| 256 *config_.get(), requires_anonymized_data_)) { | 327 *config_.get(), requires_anonymized_data_)) { |
| 257 AbortScenario(); | 328 AbortScenario(); |
| 258 } | 329 } |
| 259 } | 330 } |
| 260 | 331 |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 511 RecordBackgroundTracingMetric(FINALIZATION_DISALLOWED); | 582 RecordBackgroundTracingMetric(FINALIZATION_DISALLOWED); |
| 512 } | 583 } |
| 513 | 584 |
| 514 content::TracingController::GetInstance()->DisableRecording(trace_data_sink); | 585 content::TracingController::GetInstance()->DisableRecording(trace_data_sink); |
| 515 | 586 |
| 516 if (!callback.is_null()) | 587 if (!callback.is_null()) |
| 517 callback.Run(is_allowed_finalization); | 588 callback.Run(is_allowed_finalization); |
| 518 } | 589 } |
| 519 | 590 |
| 520 void BackgroundTracingManagerImpl::AbortScenario() { | 591 void BackgroundTracingManagerImpl::AbortScenario() { |
| 592 SetupUMACallbacks(CLEAR_CALLBACKS); | |
| 593 | |
| 521 is_tracing_ = false; | 594 is_tracing_ = false; |
| 522 config_.reset(); | 595 config_.reset(); |
| 523 | 596 |
| 524 content::TracingController::GetInstance()->DisableRecording(nullptr); | 597 content::TracingController::GetInstance()->DisableRecording(nullptr); |
| 525 } | 598 } |
| 526 | 599 |
| 527 std::string | 600 std::string |
| 528 BackgroundTracingManagerImpl::GetCategoryFilterStringForCategoryPreset( | 601 BackgroundTracingManagerImpl::GetCategoryFilterStringForCategoryPreset( |
| 529 BackgroundTracingConfig::CategoryPreset preset) const { | 602 BackgroundTracingConfig::CategoryPreset preset) const { |
| 530 switch (preset) { | 603 switch (preset) { |
| 531 case BackgroundTracingConfig::CategoryPreset::BENCHMARK: | 604 case BackgroundTracingConfig::CategoryPreset::BENCHMARK: |
| 532 return "benchmark,toplevel"; | 605 return "benchmark,toplevel"; |
| 533 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP: | 606 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP: |
| 534 return "*,disabled-by-default-benchmark.detailed"; | 607 return "*,disabled-by-default-benchmark.detailed"; |
| 535 } | 608 } |
| 536 NOTREACHED(); | 609 NOTREACHED(); |
| 537 return ""; | 610 return ""; |
| 538 } | 611 } |
| 539 | 612 |
| 540 } // namspace content | 613 } // namspace content |
| OLD | NEW |