| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "content/browser/tracing/background_memory_tracing_observer.h" |
| 18 #include "content/browser/tracing/background_tracing_rule.h" | 19 #include "content/browser/tracing/background_tracing_rule.h" |
| 19 #include "content/browser/tracing/tracing_controller_impl.h" | 20 #include "content/browser/tracing/tracing_controller_impl.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/content_browser_client.h" | 22 #include "content/public/browser/content_browser_client.h" |
| 22 #include "content/public/browser/tracing_delegate.h" | 23 #include "content/public/browser/tracing_delegate.h" |
| 23 #include "content/public/common/content_client.h" | 24 #include "content/public/common/content_client.h" |
| 24 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
| 25 | 26 |
| 26 namespace content { | 27 namespace content { |
| 27 | 28 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 43 FINALIZATION_COMPLETE = 8, | 44 FINALIZATION_COMPLETE = 8, |
| 44 SCENARIO_ACTION_FAILED_LOWRES_CLOCK = 9, | 45 SCENARIO_ACTION_FAILED_LOWRES_CLOCK = 9, |
| 45 NUMBER_OF_BACKGROUND_TRACING_METRICS, | 46 NUMBER_OF_BACKGROUND_TRACING_METRICS, |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 void RecordBackgroundTracingMetric(BackgroundTracingMetrics metric) { | 49 void RecordBackgroundTracingMetric(BackgroundTracingMetrics metric) { |
| 49 UMA_HISTOGRAM_ENUMERATION("Tracing.Background.ScenarioState", metric, | 50 UMA_HISTOGRAM_ENUMERATION("Tracing.Background.ScenarioState", metric, |
| 50 NUMBER_OF_BACKGROUND_TRACING_METRICS); | 51 NUMBER_OF_BACKGROUND_TRACING_METRICS); |
| 51 } | 52 } |
| 52 | 53 |
| 53 // Tracing enabled callback for BENCHMARK_MEMORY_LIGHT category preset. | |
| 54 void BenchmarkMemoryLight_TracingEnabledCallback() { | |
| 55 auto* dump_manager = base::trace_event::MemoryDumpManager::GetInstance(); | |
| 56 dump_manager->RequestGlobalDump( | |
| 57 base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED, | |
| 58 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND); | |
| 59 } | |
| 60 | |
| 61 } // namespace | 54 } // namespace |
| 62 | 55 |
| 63 BackgroundTracingManagerImpl::TracingTimer::TracingTimer( | 56 BackgroundTracingManagerImpl::TracingTimer::TracingTimer( |
| 64 StartedFinalizingCallback callback) : callback_(callback) { | 57 StartedFinalizingCallback callback) : callback_(callback) { |
| 65 } | 58 } |
| 66 | 59 |
| 67 BackgroundTracingManagerImpl::TracingTimer::~TracingTimer() { | 60 BackgroundTracingManagerImpl::TracingTimer::~TracingTimer() { |
| 68 } | 61 } |
| 69 | 62 |
| 70 void BackgroundTracingManagerImpl::TracingTimer::StartTimer(int seconds) { | 63 void BackgroundTracingManagerImpl::TracingTimer::StartTimer(int seconds) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 93 BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() { | 86 BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() { |
| 94 return g_controller.Pointer(); | 87 return g_controller.Pointer(); |
| 95 } | 88 } |
| 96 | 89 |
| 97 BackgroundTracingManagerImpl::BackgroundTracingManagerImpl() | 90 BackgroundTracingManagerImpl::BackgroundTracingManagerImpl() |
| 98 : delegate_(GetContentClient()->browser()->GetTracingDelegate()), | 91 : delegate_(GetContentClient()->browser()->GetTracingDelegate()), |
| 99 is_gathering_(false), | 92 is_gathering_(false), |
| 100 is_tracing_(false), | 93 is_tracing_(false), |
| 101 requires_anonymized_data_(true), | 94 requires_anonymized_data_(true), |
| 102 trigger_handle_ids_(0), | 95 trigger_handle_ids_(0), |
| 103 triggered_named_event_handle_(-1) {} | 96 triggered_named_event_handle_(-1) { |
| 97 AddEnabledStateObserver(BackgroundMemoryTracingObserver::GetInstance()); |
| 98 } |
| 104 | 99 |
| 105 BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() { | 100 BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() { |
| 106 NOTREACHED(); | 101 NOTREACHED(); |
| 107 } | 102 } |
| 108 | 103 |
| 109 void BackgroundTracingManagerImpl::WhenIdle( | 104 void BackgroundTracingManagerImpl::WhenIdle( |
| 110 base::Callback<void()> idle_callback) { | 105 base::Callback<void()> idle_callback) { |
| 111 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 106 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 112 idle_callback_ = idle_callback; | 107 idle_callback_ = idle_callback; |
| 113 } | 108 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 183 } |
| 189 | 184 |
| 190 if (!config_->disable_blink_features().empty()) { | 185 if (!config_->disable_blink_features().empty()) { |
| 191 command_line->AppendSwitchASCII(switches::kDisableBlinkFeatures, | 186 command_line->AppendSwitchASCII(switches::kDisableBlinkFeatures, |
| 192 config_->disable_blink_features()); | 187 config_->disable_blink_features()); |
| 193 } | 188 } |
| 194 } | 189 } |
| 195 | 190 |
| 196 StartTracingIfConfigNeedsIt(); | 191 StartTracingIfConfigNeedsIt(); |
| 197 | 192 |
| 193 for (auto* observer : background_tracing_observer_list_) |
| 194 observer->OnScenarioActivated(*config_); |
| 198 RecordBackgroundTracingMetric(SCENARIO_ACTIVATED_SUCCESSFULLY); | 195 RecordBackgroundTracingMetric(SCENARIO_ACTIVATED_SUCCESSFULLY); |
| 199 return true; | 196 return true; |
| 200 } | 197 } |
| 201 | 198 |
| 202 bool BackgroundTracingManagerImpl::HasActiveScenario() { | 199 bool BackgroundTracingManagerImpl::HasActiveScenario() { |
| 203 return !!config_; | 200 return !!config_; |
| 204 } | 201 } |
| 205 | 202 |
| 203 void BackgroundTracingManagerImpl::OnStartTracingDone( |
| 204 BackgroundTracingConfigImpl::CategoryPreset preset) { |
| 205 DCHECK(BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 206 for (auto* observer : background_tracing_observer_list_) |
| 207 observer->OnTracingEnabled(preset); |
| 208 } |
| 209 |
| 210 void BackgroundTracingManagerImpl::AddEnabledStateObserver( |
| 211 EnabledStateObserver* observer) { |
| 212 DCHECK(BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 213 background_tracing_observer_list_.push_back(observer); |
| 214 } |
| 215 |
| 216 void BackgroundTracingManagerImpl::RemoveEnabledStateObserver( |
| 217 EnabledStateObserver* observer) { |
| 218 DCHECK(BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 219 std::vector<EnabledStateObserver*>::iterator it = |
| 220 std::find(background_tracing_observer_list_.begin(), |
| 221 background_tracing_observer_list_.end(), observer); |
| 222 if (it != background_tracing_observer_list_.end()) |
| 223 background_tracing_observer_list_.erase(it); |
| 224 } |
| 225 |
| 206 bool BackgroundTracingManagerImpl::IsTracingForTesting() { | 226 bool BackgroundTracingManagerImpl::IsTracingForTesting() { |
| 207 return is_tracing_; | 227 return is_tracing_; |
| 208 } | 228 } |
| 209 | 229 |
| 210 void BackgroundTracingManagerImpl::ValidateStartupScenario() { | 230 void BackgroundTracingManagerImpl::ValidateStartupScenario() { |
| 211 if (!config_ || !delegate_) | 231 if (!config_ || !delegate_) |
| 212 return; | 232 return; |
| 213 | 233 |
| 214 if (!delegate_->IsAllowedToBeginBackgroundScenario( | 234 if (!delegate_->IsAllowedToBeginBackgroundScenario( |
| 215 *config_.get(), requires_anonymized_data_)) { | 235 *config_.get(), requires_anonymized_data_)) { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 std::string BackgroundTracingManagerImpl::GetTriggerNameFromHandle( | 403 std::string BackgroundTracingManagerImpl::GetTriggerNameFromHandle( |
| 384 BackgroundTracingManager::TriggerHandle handle) const { | 404 BackgroundTracingManager::TriggerHandle handle) const { |
| 385 CHECK(IsTriggerHandleValid(handle)); | 405 CHECK(IsTriggerHandleValid(handle)); |
| 386 return trigger_handles_.find(handle)->second; | 406 return trigger_handles_.find(handle)->second; |
| 387 } | 407 } |
| 388 | 408 |
| 389 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() { | 409 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() { |
| 390 trigger_handles_.clear(); | 410 trigger_handles_.clear(); |
| 391 } | 411 } |
| 392 | 412 |
| 393 void BackgroundTracingManagerImpl::SetTracingEnabledCallbackForTesting( | |
| 394 const base::Closure& callback) { | |
| 395 tracing_enabled_callback_for_testing_ = callback; | |
| 396 }; | |
| 397 | |
| 398 void BackgroundTracingManagerImpl::SetRuleTriggeredCallbackForTesting( | 413 void BackgroundTracingManagerImpl::SetRuleTriggeredCallbackForTesting( |
| 399 const base::Closure& callback) { | 414 const base::Closure& callback) { |
| 400 rule_triggered_callback_for_testing_ = callback; | 415 rule_triggered_callback_for_testing_ = callback; |
| 401 }; | 416 }; |
| 402 | 417 |
| 403 void BackgroundTracingManagerImpl::FireTimerForTesting() { | 418 void BackgroundTracingManagerImpl::FireTimerForTesting() { |
| 404 DCHECK(tracing_timer_); | 419 DCHECK(tracing_timer_); |
| 405 tracing_timer_->FireTimerForTesting(); | 420 tracing_timer_->FireTimerForTesting(); |
| 406 } | 421 } |
| 407 | 422 |
| 408 void BackgroundTracingManagerImpl::StartTracing( | 423 void BackgroundTracingManagerImpl::StartTracing( |
| 409 BackgroundTracingConfigImpl::CategoryPreset preset, | 424 BackgroundTracingConfigImpl::CategoryPreset preset, |
| 410 base::trace_event::TraceRecordMode record_mode) { | 425 base::trace_event::TraceRecordMode record_mode) { |
| 411 base::trace_event::TraceConfig trace_config( | 426 base::trace_event::TraceConfig trace_config( |
| 412 GetCategoryFilterStringForCategoryPreset(preset), record_mode); | 427 GetCategoryFilterStringForCategoryPreset(preset), record_mode); |
| 413 if (requires_anonymized_data_) | 428 if (requires_anonymized_data_) |
| 414 trace_config.EnableArgumentFilter(); | 429 trace_config.EnableArgumentFilter(); |
| 415 | 430 |
| 416 base::Closure tracing_enabled_callback; | 431 if (preset == |
| 417 if (!tracing_enabled_callback_for_testing_.is_null()) { | 432 BackgroundTracingConfigImpl::CategoryPreset::BENCHMARK_MEMORY_LIGHT) { |
| 418 tracing_enabled_callback = tracing_enabled_callback_for_testing_; | 433 // On memory light mode, the periodic memory dumps are disabled. |
| 419 } else if (preset == BackgroundTracingConfigImpl::CategoryPreset:: | 434 // TODO(ssid): Remove this when memory-infra supports peak detection |
| 420 BENCHMARK_MEMORY_LIGHT) { | 435 // crbug.com/609935. |
| 421 // On memory light mode, the periodic memory dumps are disabled and a single | |
| 422 // memory dump is requested after tracing is enabled in all the processes. | |
| 423 // TODO(ssid): Remove this when background tracing supports trace config | |
| 424 // strings and memory-infra supports peak detection crbug.com/609935. | |
| 425 base::trace_event::TraceConfig::MemoryDumpConfig memory_config; | 436 base::trace_event::TraceConfig::MemoryDumpConfig memory_config; |
| 426 memory_config.allowed_dump_modes = | 437 memory_config.allowed_dump_modes = |
| 427 std::set<base::trace_event::MemoryDumpLevelOfDetail>( | 438 std::set<base::trace_event::MemoryDumpLevelOfDetail>( |
| 428 {base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND}); | 439 {base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND}); |
| 429 trace_config.ResetMemoryDumpConfig(memory_config); | 440 trace_config.ResetMemoryDumpConfig(memory_config); |
| 430 tracing_enabled_callback = | |
| 431 base::Bind(&BenchmarkMemoryLight_TracingEnabledCallback); | |
| 432 } | 441 } |
| 433 | 442 |
| 434 is_tracing_ = TracingController::GetInstance()->StartTracing( | 443 is_tracing_ = TracingController::GetInstance()->StartTracing( |
| 435 trace_config, tracing_enabled_callback); | 444 trace_config, |
| 445 base::Bind(&BackgroundTracingManagerImpl::OnStartTracingDone, |
| 446 base::Unretained(this), preset)); |
| 436 RecordBackgroundTracingMetric(RECORDING_ENABLED); | 447 RecordBackgroundTracingMetric(RECORDING_ENABLED); |
| 437 } | 448 } |
| 438 | 449 |
| 439 void BackgroundTracingManagerImpl::OnFinalizeStarted( | 450 void BackgroundTracingManagerImpl::OnFinalizeStarted( |
| 440 std::unique_ptr<const base::DictionaryValue> metadata, | 451 std::unique_ptr<const base::DictionaryValue> metadata, |
| 441 base::RefCountedString* file_contents) { | 452 base::RefCountedString* file_contents) { |
| 442 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 453 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 443 | 454 |
| 444 RecordBackgroundTracingMetric(FINALIZATION_STARTED); | 455 RecordBackgroundTracingMetric(FINALIZATION_STARTED); |
| 445 UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB", | 456 UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB", |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 case BackgroundTracingConfigImpl::CategoryPreset::BLINK_STYLE: | 577 case BackgroundTracingConfigImpl::CategoryPreset::BLINK_STYLE: |
| 567 return "blink_style"; | 578 return "blink_style"; |
| 568 case BackgroundTracingConfigImpl::CategoryPreset::CATEGORY_PRESET_UNSET: | 579 case BackgroundTracingConfigImpl::CategoryPreset::CATEGORY_PRESET_UNSET: |
| 569 NOTREACHED(); | 580 NOTREACHED(); |
| 570 } | 581 } |
| 571 NOTREACHED(); | 582 NOTREACHED(); |
| 572 return ""; | 583 return ""; |
| 573 } | 584 } |
| 574 | 585 |
| 575 } // namspace content | 586 } // namspace content |
| OLD | NEW |