| 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 // BackgroundTracingManagerImpl is leaky, so there's no danger of this being |
| 93 // called after being destroyed and we can use base::Unretained(). |
| 94 TracingControllerImpl::GetInstance()->SetTraceMessageFilterAddedCallback( |
| 95 base::Bind(&BackgroundTracingManagerImpl::OnTraceMessageFilterAdded, |
| 96 base::Unretained(this))); |
| 90 } | 97 } |
| 91 | 98 |
| 92 BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() { | 99 BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() { |
| 93 NOTREACHED(); | 100 NOTREACHED(); |
| 94 } | 101 } |
| 95 | 102 |
| 96 void BackgroundTracingManagerImpl::WhenIdle( | 103 void BackgroundTracingManagerImpl::WhenIdle( |
| 97 base::Callback<void()> idle_callback) { | 104 base::Callback<void()> idle_callback) { |
| 98 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 105 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 99 idle_callback_ = idle_callback; | 106 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) { | 160 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) { |
| 154 continue; | 161 continue; |
| 155 } | 162 } |
| 156 | 163 |
| 157 if (mode == CLEAR_CALLBACKS) { | 164 if (mode == CLEAR_CALLBACKS) { |
| 158 base::StatisticsRecorder::ClearCallback( | 165 base::StatisticsRecorder::ClearCallback( |
| 159 configs[i].histogram_trigger_info.histogram_name); | 166 configs[i].histogram_trigger_info.histogram_name); |
| 160 } else { | 167 } else { |
| 161 base::StatisticsRecorder::SetCallback( | 168 base::StatisticsRecorder::SetCallback( |
| 162 configs[i].histogram_trigger_info.histogram_name, | 169 configs[i].histogram_trigger_info.histogram_name, |
| 163 base::Bind(&BackgroundTracingManagerImpl::OnHistogramChanged, | 170 base::Bind(&BackgroundTracingManagerImpl::OnHistogramChangedCallback, |
| 164 base::Unretained(this), | 171 base::Unretained(this), |
| 165 configs[i].histogram_trigger_info.histogram_name, | 172 configs[i].histogram_trigger_info.histogram_name, |
| 166 configs[i].histogram_trigger_info.histogram_value)); | 173 configs[i].histogram_trigger_info.histogram_value)); |
| 167 } | 174 } |
| 168 } | 175 } |
| 176 |
| 177 SetupFiltersFromConfig(mode); |
| 169 } | 178 } |
| 170 | 179 |
| 171 void BackgroundTracingManagerImpl::OnHistogramChanged( | 180 void BackgroundTracingManagerImpl::OnHistogramTrigger( |
| 172 const std::string& histogram_name, | 181 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)) { | 182 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 176 content::BrowserThread::PostTask( | 183 content::BrowserThread::PostTask( |
| 177 content::BrowserThread::UI, FROM_HERE, | 184 content::BrowserThread::UI, FROM_HERE, |
| 178 base::Bind(&BackgroundTracingManagerImpl::OnHistogramChanged, | 185 base::Bind(&BackgroundTracingManagerImpl::OnHistogramTrigger, |
| 179 base::Unretained(this), histogram_name, reference_value, | 186 base::Unretained(this), histogram_name)); |
| 180 actual_value)); | |
| 181 return; | 187 return; |
| 182 } | 188 } |
| 183 | 189 |
| 184 CHECK(config_ && | 190 CHECK(config_ && |
| 185 config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE); | 191 config_->mode == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE); |
| 186 | 192 |
| 193 if (!is_tracing_ || is_gathering_) |
| 194 return; |
| 195 |
| 196 BackgroundTracingPreemptiveConfig* preemptive_config = |
| 197 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()); |
| 198 for (const auto& config : preemptive_config->configs) { |
| 199 if (config.type != BackgroundTracingPreemptiveConfig:: |
| 200 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) { |
| 201 continue; |
| 202 } |
| 203 |
| 204 if (config.histogram_trigger_info.histogram_name == histogram_name) { |
| 205 RecordBackgroundTracingMetric(PREEMPTIVE_TRIGGERED); |
| 206 BeginFinalizing(StartedFinalizingCallback()); |
| 207 } |
| 208 } |
| 209 } |
| 210 |
| 211 void BackgroundTracingManagerImpl::OnHistogramChangedCallback( |
| 212 const std::string& histogram_name, |
| 213 base::Histogram::Sample reference_value, |
| 214 base::Histogram::Sample actual_value) { |
| 187 if (reference_value > actual_value) | 215 if (reference_value > actual_value) |
| 188 return; | 216 return; |
| 189 | 217 |
| 190 if (!is_tracing_ || is_gathering_) | 218 OnHistogramTrigger(histogram_name); |
| 191 return; | |
| 192 | |
| 193 RecordBackgroundTracingMetric(PREEMPTIVE_TRIGGERED); | |
| 194 BeginFinalizing(StartedFinalizingCallback()); | |
| 195 } | 219 } |
| 196 | 220 |
| 197 bool BackgroundTracingManagerImpl::SetActiveScenario( | 221 bool BackgroundTracingManagerImpl::SetActiveScenario( |
| 198 scoped_ptr<BackgroundTracingConfig> config, | 222 scoped_ptr<BackgroundTracingConfig> config, |
| 199 const BackgroundTracingManager::ReceiveCallback& receive_callback, | 223 const BackgroundTracingManager::ReceiveCallback& receive_callback, |
| 200 DataFiltering data_filtering) { | 224 DataFiltering data_filtering) { |
| 201 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 225 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 202 RecordBackgroundTracingMetric(SCENARIO_ACTIVATION_REQUESTED); | 226 RecordBackgroundTracingMetric(SCENARIO_ACTIVATION_REQUESTED); |
| 203 | 227 |
| 204 if (is_tracing_) | 228 if (is_tracing_) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 EnableRecordingIfConfigNeedsIt(); | 265 EnableRecordingIfConfigNeedsIt(); |
| 242 | 266 |
| 243 RecordBackgroundTracingMetric(SCENARIO_ACTIVATED_SUCCESSFULLY); | 267 RecordBackgroundTracingMetric(SCENARIO_ACTIVATED_SUCCESSFULLY); |
| 244 return true; | 268 return true; |
| 245 } | 269 } |
| 246 | 270 |
| 247 bool BackgroundTracingManagerImpl::HasActiveScenarioForTesting() { | 271 bool BackgroundTracingManagerImpl::HasActiveScenarioForTesting() { |
| 248 return config_; | 272 return config_; |
| 249 } | 273 } |
| 250 | 274 |
| 275 void BackgroundTracingManagerImpl::OnTraceMessageFilterAdded( |
| 276 TraceMessageFilter* filter) { |
| 277 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 278 SetupFilterFromConfig(filter, BIND_CALLBACKS); |
| 279 } |
| 280 |
| 281 void BackgroundTracingManagerImpl::SetupFiltersFromConfig( |
| 282 BackgroundTracingManagerImpl::SetupUMACallMode mode) { |
| 283 TracingControllerImpl::TraceMessageFilterSet filters; |
| 284 TracingControllerImpl::GetInstance()->GetTraceMessageFilters(&filters); |
| 285 |
| 286 for (auto& filter : filters) |
| 287 SetupFilterFromConfig(filter, mode); |
| 288 } |
| 289 |
| 290 void BackgroundTracingManagerImpl::SetupFilterFromConfig( |
| 291 scoped_refptr<TraceMessageFilter> filter, |
| 292 BackgroundTracingManagerImpl::SetupUMACallMode mode) { |
| 293 if (!config_ || |
| 294 config_->mode != BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) |
| 295 return; |
| 296 |
| 297 BackgroundTracingPreemptiveConfig* preemptive_config = |
| 298 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get()); |
| 299 |
| 300 for (const auto& config : preemptive_config->configs) { |
| 301 if (config.type != BackgroundTracingPreemptiveConfig:: |
| 302 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) { |
| 303 continue; |
| 304 } |
| 305 |
| 306 if (mode == CLEAR_CALLBACKS) { |
| 307 filter->Send(new TracingMsg_ClearUMACallback( |
| 308 config.histogram_trigger_info.histogram_name)); |
| 309 } else { |
| 310 filter->Send(new TracingMsg_SetUMACallback( |
| 311 config.histogram_trigger_info.histogram_name, |
| 312 config.histogram_trigger_info.histogram_value)); |
| 313 } |
| 314 } |
| 315 } |
| 316 |
| 251 void BackgroundTracingManagerImpl::ValidateStartupScenario() { | 317 void BackgroundTracingManagerImpl::ValidateStartupScenario() { |
| 252 if (!config_ || !delegate_) | 318 if (!config_ || !delegate_) |
| 253 return; | 319 return; |
| 254 | 320 |
| 255 if (!delegate_->IsAllowedToBeginBackgroundScenario( | 321 if (!delegate_->IsAllowedToBeginBackgroundScenario( |
| 256 *config_.get(), requires_anonymized_data_)) { | 322 *config_.get(), requires_anonymized_data_)) { |
| 257 AbortScenario(); | 323 AbortScenario(); |
| 258 } | 324 } |
| 259 } | 325 } |
| 260 | 326 |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 RecordBackgroundTracingMetric(FINALIZATION_DISALLOWED); | 577 RecordBackgroundTracingMetric(FINALIZATION_DISALLOWED); |
| 512 } | 578 } |
| 513 | 579 |
| 514 content::TracingController::GetInstance()->DisableRecording(trace_data_sink); | 580 content::TracingController::GetInstance()->DisableRecording(trace_data_sink); |
| 515 | 581 |
| 516 if (!callback.is_null()) | 582 if (!callback.is_null()) |
| 517 callback.Run(is_allowed_finalization); | 583 callback.Run(is_allowed_finalization); |
| 518 } | 584 } |
| 519 | 585 |
| 520 void BackgroundTracingManagerImpl::AbortScenario() { | 586 void BackgroundTracingManagerImpl::AbortScenario() { |
| 587 SetupUMACallbacks(CLEAR_CALLBACKS); |
| 588 |
| 521 is_tracing_ = false; | 589 is_tracing_ = false; |
| 522 config_.reset(); | 590 config_.reset(); |
| 523 | 591 |
| 524 content::TracingController::GetInstance()->DisableRecording(nullptr); | 592 content::TracingController::GetInstance()->DisableRecording(nullptr); |
| 525 } | 593 } |
| 526 | 594 |
| 527 std::string | 595 std::string |
| 528 BackgroundTracingManagerImpl::GetCategoryFilterStringForCategoryPreset( | 596 BackgroundTracingManagerImpl::GetCategoryFilterStringForCategoryPreset( |
| 529 BackgroundTracingConfig::CategoryPreset preset) const { | 597 BackgroundTracingConfig::CategoryPreset preset) const { |
| 530 switch (preset) { | 598 switch (preset) { |
| 531 case BackgroundTracingConfig::CategoryPreset::BENCHMARK: | 599 case BackgroundTracingConfig::CategoryPreset::BENCHMARK: |
| 532 return "benchmark,toplevel"; | 600 return "benchmark,toplevel"; |
| 533 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP: | 601 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP: |
| 534 return "*,disabled-by-default-benchmark.detailed"; | 602 return "*,disabled-by-default-benchmark.detailed"; |
| 535 } | 603 } |
| 536 NOTREACHED(); | 604 NOTREACHED(); |
| 537 return ""; | 605 return ""; |
| 538 } | 606 } |
| 539 | 607 |
| 540 } // namspace content | 608 } // namspace content |
| OLD | NEW |