Index: content/browser/memory/memory_coordinator_impl.cc |
diff --git a/content/browser/memory/memory_coordinator_impl.cc b/content/browser/memory/memory_coordinator_impl.cc |
index ca18cbacdfd3ae581493db0f7d91263e27d95099..5a24676c9386baf3b7a518f6f7c9240bfa6082e6 100644 |
--- a/content/browser/memory/memory_coordinator_impl.cc |
+++ b/content/browser/memory/memory_coordinator_impl.cc |
@@ -9,8 +9,8 @@ |
#include "base/strings/string_number_conversions.h" |
#include "base/threading/thread_task_runner_handle.h" |
#include "base/trace_event/trace_event.h" |
-#include "components/variations/variations_associated_data.h" |
#include "content/browser/memory/memory_monitor.h" |
+#include "content/browser/memory/memory_state_updater.h" |
#include "content/public/browser/notification_service.h" |
#include "content/public/browser/notification_types.h" |
#include "content/public/browser/render_process_host.h" |
@@ -21,24 +21,6 @@ namespace content { |
namespace { |
-// A expected renderer size. These values come from the median of appropriate |
-// UMA stats. |
-#if defined(OS_ANDROID) || defined(OS_IOS) |
-const int kDefaultExpectedRendererSizeMB = 40; |
-#elif defined(OS_WIN) |
-const int kDefaultExpectedRendererSizeMB = 70; |
-#else // Mac, Linux, and ChromeOS |
-const int kDefaultExpectedRendererSizeMB = 120; |
-#endif |
- |
-// Default values for parameters to determine the global state. |
-const int kDefaultNewRenderersUntilThrottled = 4; |
-const int kDefaultNewRenderersUntilSuspended = 2; |
-const int kDefaultNewRenderersBackToNormal = 5; |
-const int kDefaultNewRenderersBackToThrottled = 3; |
-const int kDefaultMinimumTransitionPeriodSeconds = 30; |
-const int kDefaultMonitoringIntervalSeconds = 5; |
- |
mojom::MemoryState ToMojomMemoryState(base::MemoryState state) { |
switch (state) { |
case base::MemoryState::UNKNOWN: |
@@ -111,33 +93,6 @@ void RecordMetricsOnStateChange(base::MemoryState prev_state, |
#undef RECORD_METRICS |
} |
-void SetIntVariationParameter(const std::map<std::string, std::string> params, |
- const char* name, |
- int* target) { |
- const auto& iter = params.find(name); |
- if (iter == params.end()) |
- return; |
- int value; |
- if (!iter->second.empty() && base::StringToInt(iter->second, &value)) { |
- DCHECK(value > 0); |
- *target = value; |
- } |
-} |
- |
-void SetSecondsVariationParameter( |
- const std::map<std::string, std::string> params, |
- const char* name, |
- base::TimeDelta* target) { |
- const auto& iter = params.find(name); |
- if (iter == params.end()) |
- return; |
- int value; |
- if (!iter->second.empty() && base::StringToInt(iter->second, &value)) { |
- DCHECK(value > 0); |
- *target = base::TimeDelta::FromSeconds(value); |
- } |
-} |
- |
} // namespace |
// SingletonTraits for MemoryCoordinator. Returns MemoryCoordinatorImpl |
@@ -161,11 +116,9 @@ MemoryCoordinator* MemoryCoordinator::GetInstance() { |
MemoryCoordinatorImpl::MemoryCoordinatorImpl( |
scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
std::unique_ptr<MemoryMonitor> memory_monitor) |
- : task_runner_(task_runner), |
- memory_monitor_(std::move(memory_monitor)), |
- weak_ptr_factory_(this) { |
+ : memory_monitor_(std::move(memory_monitor)), |
+ state_updater_(base::MakeUnique<MemoryStateUpdater>(this, task_runner)) { |
DCHECK(memory_monitor_.get()); |
- InitializeParameters(); |
} |
MemoryCoordinatorImpl::~MemoryCoordinatorImpl() {} |
@@ -173,13 +126,12 @@ MemoryCoordinatorImpl::~MemoryCoordinatorImpl() {} |
void MemoryCoordinatorImpl::Start() { |
DCHECK(CalledOnValidThread()); |
DCHECK(last_state_change_.is_null()); |
- DCHECK(ValidateParameters()); |
notification_registrar_.Add( |
this, NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
NotificationService::AllBrowserContextsAndSources()); |
last_state_change_ = base::TimeTicks::Now(); |
- ScheduleUpdateState(base::TimeDelta()); |
+ state_updater_->ScheduleUpdateState(base::TimeDelta()); |
} |
void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) { |
@@ -211,7 +163,7 @@ void MemoryCoordinatorImpl::ForceSetGlobalState(base::MemoryState new_state, |
base::TimeDelta duration) { |
DCHECK(new_state != MemoryState::UNKNOWN); |
ChangeStateIfNeeded(current_state_, new_state); |
- ScheduleUpdateState(duration); |
+ state_updater_->ScheduleUpdateState(duration); |
} |
void MemoryCoordinatorImpl::Observe(int type, |
@@ -232,6 +184,7 @@ void MemoryCoordinatorImpl::Observe(int type, |
bool MemoryCoordinatorImpl::ChangeStateIfNeeded(base::MemoryState prev_state, |
base::MemoryState next_state) { |
+ DCHECK(CalledOnValidThread()); |
if (prev_state == next_state) |
return false; |
@@ -249,57 +202,6 @@ bool MemoryCoordinatorImpl::ChangeStateIfNeeded(base::MemoryState prev_state, |
return true; |
} |
-base::MemoryState MemoryCoordinatorImpl::CalculateNextState() { |
- using MemoryState = base::MemoryState; |
- |
- int available = memory_monitor_->GetFreeMemoryUntilCriticalMB(); |
- |
- // TODO(chrisha): Move this histogram recording to a better place when |
- // https://codereview.chromium.org/2479673002/ is landed. |
- UMA_HISTOGRAM_MEMORY_LARGE_MB("Memory.Coordinator.FreeMemoryUntilCritical", |
- available); |
- |
- if (available <= 0) |
- return MemoryState::SUSPENDED; |
- |
- int expected_renderer_count = available / expected_renderer_size_; |
- |
- switch (current_state_) { |
- case MemoryState::NORMAL: |
- if (expected_renderer_count <= new_renderers_until_suspended_) |
- return MemoryState::SUSPENDED; |
- if (expected_renderer_count <= new_renderers_until_throttled_) |
- return MemoryState::THROTTLED; |
- return MemoryState::NORMAL; |
- case MemoryState::THROTTLED: |
- if (expected_renderer_count <= new_renderers_until_suspended_) |
- return MemoryState::SUSPENDED; |
- if (expected_renderer_count >= new_renderers_back_to_normal_) |
- return MemoryState::NORMAL; |
- return MemoryState::THROTTLED; |
- case MemoryState::SUSPENDED: |
- if (expected_renderer_count >= new_renderers_back_to_normal_) |
- return MemoryState::NORMAL; |
- if (expected_renderer_count >= new_renderers_back_to_throttled_) |
- return MemoryState::THROTTLED; |
- return MemoryState::SUSPENDED; |
- case MemoryState::UNKNOWN: |
- // Fall through |
- default: |
- NOTREACHED(); |
- return MemoryState::UNKNOWN; |
- } |
-} |
- |
-void MemoryCoordinatorImpl::UpdateState() { |
- MemoryState next_state = CalculateNextState(); |
- if (ChangeStateIfNeeded(current_state_, next_state)) { |
- ScheduleUpdateState(minimum_transition_period_); |
- } else { |
- ScheduleUpdateState(monitoring_interval_); |
- } |
-} |
- |
void MemoryCoordinatorImpl::NotifyStateToClients() { |
auto state = GetCurrentMemoryState(); |
base::MemoryCoordinatorClientRegistry::GetInstance()->Notify(state); |
@@ -342,49 +244,4 @@ void MemoryCoordinatorImpl::RecordStateChange(MemoryState prev_state, |
total_private_kb / 1024); |
} |
-void MemoryCoordinatorImpl::ScheduleUpdateState(base::TimeDelta delta) { |
- update_state_closure_.Reset(base::Bind(&MemoryCoordinatorImpl::UpdateState, |
- weak_ptr_factory_.GetWeakPtr())); |
- task_runner_->PostDelayedTask(FROM_HERE, update_state_closure_.callback(), |
- delta); |
-} |
- |
-void MemoryCoordinatorImpl::InitializeParameters() { |
- expected_renderer_size_ = kDefaultExpectedRendererSizeMB; |
- new_renderers_until_throttled_ = kDefaultNewRenderersUntilThrottled; |
- new_renderers_until_suspended_ = kDefaultNewRenderersUntilSuspended; |
- new_renderers_back_to_normal_ = kDefaultNewRenderersBackToNormal; |
- new_renderers_back_to_throttled_ = kDefaultNewRenderersBackToThrottled; |
- minimum_transition_period_ = |
- base::TimeDelta::FromSeconds(kDefaultMinimumTransitionPeriodSeconds); |
- monitoring_interval_ = |
- base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); |
- |
- // Override default parameters with variations. |
- static constexpr char kMemoryCoordinatorV0Trial[] = "MemoryCoordinatorV0"; |
- std::map<std::string, std::string> params; |
- variations::GetVariationParams(kMemoryCoordinatorV0Trial, ¶ms); |
- SetIntVariationParameter(params, "expected_renderer_size", |
- &expected_renderer_size_); |
- SetIntVariationParameter(params, "new_renderers_until_throttled", |
- &new_renderers_until_throttled_); |
- SetIntVariationParameter(params, "new_renderers_until_suspended", |
- &new_renderers_until_suspended_); |
- SetIntVariationParameter(params, "new_renderers_back_to_normal", |
- &new_renderers_back_to_normal_); |
- SetIntVariationParameter(params, "new_renderers_back_to_throttled", |
- &new_renderers_back_to_throttled_); |
- SetSecondsVariationParameter(params, "minimum_transition_period", |
- &minimum_transition_period_); |
- SetSecondsVariationParameter(params, "monitoring_interval", |
- &monitoring_interval_); |
-} |
- |
-bool MemoryCoordinatorImpl::ValidateParameters() { |
- return (new_renderers_until_throttled_ > new_renderers_until_suspended_) && |
- (new_renderers_back_to_normal_ > new_renderers_back_to_throttled_) && |
- (new_renderers_back_to_normal_ > new_renderers_until_throttled_) && |
- (new_renderers_back_to_throttled_ > new_renderers_until_suspended_); |
-} |
- |
} // namespace content |