Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: content/browser/tracing/background_tracing_manager_impl.cc

Issue 2782933002: Reland of Add EnabledStateObserver to BackgroundTracingManager (Closed)
Patch Set: Fix Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures, 181 command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures,
187 config_->enable_blink_features()); 182 config_->enable_blink_features());
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
191 // Notify observers before starting tracing.
192 for (auto* observer : background_tracing_observer_list_)
193 observer->OnScenarioActivated(config_.get());
194
196 StartTracingIfConfigNeedsIt(); 195 StartTracingIfConfigNeedsIt();
197
198 RecordBackgroundTracingMetric(SCENARIO_ACTIVATED_SUCCESSFULLY); 196 RecordBackgroundTracingMetric(SCENARIO_ACTIVATED_SUCCESSFULLY);
199 return true; 197 return true;
200 } 198 }
201 199
202 bool BackgroundTracingManagerImpl::HasActiveScenario() { 200 bool BackgroundTracingManagerImpl::HasActiveScenario() {
203 return !!config_; 201 return !!config_;
204 } 202 }
205 203
204 void BackgroundTracingManagerImpl::OnStartTracingDone(
205 BackgroundTracingConfigImpl::CategoryPreset preset) {
206 DCHECK(BrowserThread::CurrentlyOn(content::BrowserThread::UI));
207 for (auto* observer : background_tracing_observer_list_)
208 observer->OnTracingEnabled(preset);
209 }
210
211 void BackgroundTracingManagerImpl::AddEnabledStateObserver(
212 EnabledStateObserver* observer) {
213 DCHECK(BrowserThread::CurrentlyOn(content::BrowserThread::UI));
214 background_tracing_observer_list_.push_back(observer);
215 }
216
217 void BackgroundTracingManagerImpl::RemoveEnabledStateObserver(
218 EnabledStateObserver* observer) {
219 DCHECK(BrowserThread::CurrentlyOn(content::BrowserThread::UI));
220 std::vector<EnabledStateObserver*>::iterator it =
221 std::find(background_tracing_observer_list_.begin(),
222 background_tracing_observer_list_.end(), observer);
223 if (it != background_tracing_observer_list_.end())
224 background_tracing_observer_list_.erase(it);
225 }
226
206 bool BackgroundTracingManagerImpl::IsTracingForTesting() { 227 bool BackgroundTracingManagerImpl::IsTracingForTesting() {
207 return is_tracing_; 228 return is_tracing_;
208 } 229 }
209 230
210 void BackgroundTracingManagerImpl::ValidateStartupScenario() { 231 void BackgroundTracingManagerImpl::ValidateStartupScenario() {
211 if (!config_ || !delegate_) 232 if (!config_ || !delegate_)
212 return; 233 return;
213 234
214 if (!delegate_->IsAllowedToBeginBackgroundScenario( 235 if (!delegate_->IsAllowedToBeginBackgroundScenario(
215 *config_.get(), requires_anonymized_data_)) { 236 *config_.get(), requires_anonymized_data_)) {
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 std::string BackgroundTracingManagerImpl::GetTriggerNameFromHandle( 404 std::string BackgroundTracingManagerImpl::GetTriggerNameFromHandle(
384 BackgroundTracingManager::TriggerHandle handle) const { 405 BackgroundTracingManager::TriggerHandle handle) const {
385 CHECK(IsTriggerHandleValid(handle)); 406 CHECK(IsTriggerHandleValid(handle));
386 return trigger_handles_.find(handle)->second; 407 return trigger_handles_.find(handle)->second;
387 } 408 }
388 409
389 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() { 410 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() {
390 trigger_handles_.clear(); 411 trigger_handles_.clear();
391 } 412 }
392 413
393 void BackgroundTracingManagerImpl::SetTracingEnabledCallbackForTesting(
394 const base::Closure& callback) {
395 tracing_enabled_callback_for_testing_ = callback;
396 };
397
398 void BackgroundTracingManagerImpl::SetRuleTriggeredCallbackForTesting( 414 void BackgroundTracingManagerImpl::SetRuleTriggeredCallbackForTesting(
399 const base::Closure& callback) { 415 const base::Closure& callback) {
400 rule_triggered_callback_for_testing_ = callback; 416 rule_triggered_callback_for_testing_ = callback;
401 }; 417 };
402 418
403 void BackgroundTracingManagerImpl::FireTimerForTesting() { 419 void BackgroundTracingManagerImpl::FireTimerForTesting() {
404 DCHECK(tracing_timer_); 420 DCHECK(tracing_timer_);
405 tracing_timer_->FireTimerForTesting(); 421 tracing_timer_->FireTimerForTesting();
406 } 422 }
407 423
408 void BackgroundTracingManagerImpl::StartTracing( 424 void BackgroundTracingManagerImpl::StartTracing(
409 BackgroundTracingConfigImpl::CategoryPreset preset, 425 BackgroundTracingConfigImpl::CategoryPreset preset,
410 base::trace_event::TraceRecordMode record_mode) { 426 base::trace_event::TraceRecordMode record_mode) {
411 base::trace_event::TraceConfig trace_config( 427 base::trace_event::TraceConfig trace_config(
412 GetCategoryFilterStringForCategoryPreset(preset), record_mode); 428 GetCategoryFilterStringForCategoryPreset(preset), record_mode);
413 if (requires_anonymized_data_) 429 if (requires_anonymized_data_)
414 trace_config.EnableArgumentFilter(); 430 trace_config.EnableArgumentFilter();
415 431
416 base::Closure tracing_enabled_callback; 432 if (preset ==
417 if (!tracing_enabled_callback_for_testing_.is_null()) { 433 BackgroundTracingConfigImpl::CategoryPreset::BENCHMARK_MEMORY_LIGHT) {
418 tracing_enabled_callback = tracing_enabled_callback_for_testing_; 434 // On memory light mode, the periodic memory dumps are disabled.
419 } else if (preset == BackgroundTracingConfigImpl::CategoryPreset:: 435 // TODO(ssid): Remove this when memory-infra supports peak detection
420 BENCHMARK_MEMORY_LIGHT) { 436 // 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; 437 base::trace_event::TraceConfig::MemoryDumpConfig memory_config;
426 memory_config.allowed_dump_modes = 438 memory_config.allowed_dump_modes =
427 std::set<base::trace_event::MemoryDumpLevelOfDetail>( 439 std::set<base::trace_event::MemoryDumpLevelOfDetail>(
428 {base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND}); 440 {base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND});
429 trace_config.ResetMemoryDumpConfig(memory_config); 441 trace_config.ResetMemoryDumpConfig(memory_config);
430 tracing_enabled_callback =
431 base::Bind(&BenchmarkMemoryLight_TracingEnabledCallback);
432 } 442 }
433 443
434 is_tracing_ = TracingController::GetInstance()->StartTracing( 444 is_tracing_ = TracingController::GetInstance()->StartTracing(
435 trace_config, tracing_enabled_callback); 445 trace_config,
446 base::Bind(&BackgroundTracingManagerImpl::OnStartTracingDone,
447 base::Unretained(this), preset));
436 RecordBackgroundTracingMetric(RECORDING_ENABLED); 448 RecordBackgroundTracingMetric(RECORDING_ENABLED);
437 } 449 }
438 450
439 void BackgroundTracingManagerImpl::OnFinalizeStarted( 451 void BackgroundTracingManagerImpl::OnFinalizeStarted(
440 std::unique_ptr<const base::DictionaryValue> metadata, 452 std::unique_ptr<const base::DictionaryValue> metadata,
441 base::RefCountedString* file_contents) { 453 base::RefCountedString* file_contents) {
442 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 454 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
443 455
444 RecordBackgroundTracingMetric(FINALIZATION_STARTED); 456 RecordBackgroundTracingMetric(FINALIZATION_STARTED);
445 UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB", 457 UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB",
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 case BackgroundTracingConfigImpl::CategoryPreset::BLINK_STYLE: 578 case BackgroundTracingConfigImpl::CategoryPreset::BLINK_STYLE:
567 return "blink_style"; 579 return "blink_style";
568 case BackgroundTracingConfigImpl::CategoryPreset::CATEGORY_PRESET_UNSET: 580 case BackgroundTracingConfigImpl::CategoryPreset::CATEGORY_PRESET_UNSET:
569 NOTREACHED(); 581 NOTREACHED();
570 } 582 }
571 NOTREACHED(); 583 NOTREACHED();
572 return ""; 584 return "";
573 } 585 }
574 586
575 } // namspace content 587 } // namspace content
OLDNEW
« no previous file with comments | « content/browser/tracing/background_tracing_manager_impl.h ('k') | content/public/browser/background_tracing_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698