Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bc8816413ad0296efb0f5ff5829608a586dc68ec |
| --- /dev/null |
| +++ b/content/browser/memory/memory_coordinator_impl.cc |
| @@ -0,0 +1,153 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/memory/memory_coordinator_impl.h" |
| + |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "content/browser/memory/memory_monitor.h" |
| +#include "content/public/common/content_features.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +#if defined(OS_ANDROID) |
| +static const int kDefaultPredictedRendererSizeMB = 40; |
| +#else |
| +static const int kDefaultPredictedRendererSizeMB = 80; |
| +#endif |
| + |
| +mojom::MemoryState ToMojomMemoryState(base::MemoryState state) { |
| + switch (state) { |
| + case base::MemoryState::UNKNOWN: |
| + return mojom::MemoryState::UNKNOWN; |
| + case base::MemoryState::NORMAL: |
| + return mojom::MemoryState::NORMAL; |
| + case base::MemoryState::THROTTLED: |
| + return mojom::MemoryState::THROTTLED; |
| + case base::MemoryState::SUSPENDED: |
| + return mojom::MemoryState::SUSPENDED; |
| + default: |
| + NOTREACHED(); |
| + return mojom::MemoryState::UNKNOWN; |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +// SingletonTraits for MemoryCoordinator. Returns MemoryCoordinatorImpl |
| +// as an actual instance. |
| +struct MemoryCoordinatorSingletonTraits |
| + : public base::LeakySingletonTraits<MemoryCoordinator> { |
| + static MemoryCoordinator* New() { |
| + return new MemoryCoordinatorImpl(base::ThreadTaskRunnerHandle::Get(), |
| + CreateMemoryMonitor()); |
| + } |
| +}; |
| + |
| +// static |
| +MemoryCoordinator* MemoryCoordinator::GetInstance() { |
| + if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) |
| + return nullptr; |
| + return base::Singleton<MemoryCoordinator, |
| + MemoryCoordinatorSingletonTraits>::get(); |
| +} |
| + |
| +MemoryCoordinatorImpl::MemoryCoordinatorImpl( |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| + std::unique_ptr<MemoryMonitor> memory_monitor) |
| + : task_runner_(task_runner), memory_monitor_(std::move(memory_monitor)) { |
| + DCHECK(memory_monitor_.get()); |
| + timer_.SetTaskRunner(task_runner_); |
| + // Set default configuration. |
| + // TODO(bashi): Provide a way to change these configuration. |
| + config_.predicted_renderer_size = kDefaultPredictedRendererSizeMB; |
| + config_.num_children_until_throttled = 4; |
| + config_.num_children_until_suspended = 2; |
| + config_.minimum_transition_period = base::TimeDelta::FromSeconds(30); |
| + config_.monitoring_iterval = base::TimeDelta::FromSeconds(5); |
| +} |
| + |
| +MemoryCoordinatorImpl::~MemoryCoordinatorImpl() { |
| + timer_.Stop(); |
| +} |
| + |
| +void MemoryCoordinatorImpl::Start() { |
| + timer_.Start(FROM_HERE, config_.monitoring_iterval, |
|
chrisha
2016/10/04 20:00:53
Don't use a repeating timer? Or a timer at all?
I
bashi
2016/10/06 03:55:45
Removed timer.
|
| + this, &MemoryCoordinatorImpl::UpdateState); |
| +} |
| + |
| +void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) { |
| + // Populate the global state as an initial state of a newly created process. |
| + SetMemoryState(render_process_id, ToMojomMemoryState(current_state_)); |
| +} |
| + |
| +base::MemoryState MemoryCoordinatorImpl::CalculateNextState() { |
| + using MemoryState = base::MemoryState; |
| + |
| + int available = memory_monitor_->GetFreeMemoryUntilCriticalMB(); |
| + if (available <= 0) |
| + return MemoryState::SUSPENDED; |
| + |
| + int num_children_until_critical = available / config_.predicted_renderer_size; |
| + switch (current_state_) { |
| + case MemoryState::NORMAL: |
| + if (num_children_until_critical < config_.num_children_until_suspended) |
|
chrisha
2016/10/04 20:00:53
I'd want some hysteresis here. For example, shift
bashi
2016/10/06 03:55:45
I see. Added variables for deadbands.
|
| + return MemoryState::SUSPENDED; |
| + if (num_children_until_critical < config_.num_children_until_throttled) |
| + return MemoryState::THROTTLED; |
| + return MemoryState::NORMAL; |
| + case MemoryState::THROTTLED: |
| + if (num_children_until_critical < config_.num_children_until_suspended) |
| + return MemoryState::SUSPENDED; |
| + if (num_children_until_critical >= config_.num_children_until_throttled) |
| + return MemoryState::NORMAL; |
| + return MemoryState::THROTTLED; |
| + case MemoryState::SUSPENDED: |
| + if (num_children_until_critical >= config_.num_children_until_throttled) |
| + return MemoryState::NORMAL; |
| + if (num_children_until_critical >= config_.num_children_until_suspended) |
| + return MemoryState::THROTTLED; |
| + return MemoryState::SUSPENDED; |
| + case MemoryState::UNKNOWN: |
| + // Fall through |
| + default: |
| + NOTREACHED(); |
| + return MemoryState::UNKNOWN; |
| + } |
| +} |
| + |
| +void MemoryCoordinatorImpl::UpdateState() { |
| + base::TimeTicks now = base::TimeTicks::Now(); |
| + if (!last_state_change_.is_null() && |
| + now - last_state_change_ < config_.minimum_transition_period) { |
| + return; |
| + } |
| + |
| + MemoryState next_state = CalculateNextState(); |
| + if (current_state_ != next_state) { |
| + current_state_ = next_state; |
| + last_state_change_ = now; |
| + NotifyStateToClients(); |
| + NotifyStateToChildren(); |
| + } |
| +} |
| + |
| +void MemoryCoordinatorImpl::NotifyStateToClients() { |
| + // SUSPENDED state may not make sense to the browser process. Use THROTTLED |
| + // instead when the global state is SUSPENDED. |
| + // TODO(bashi): Maybe worth considering another state for the browser. |
|
chrisha
2016/10/04 20:00:53
We also need to consider tabs that can't be suspen
bashi
2016/10/06 03:55:45
I missed that. Maybe we need to add logic both bro
|
| + auto state = current_state_ == MemoryState::SUSPENDED ? MemoryState::THROTTLED |
| + : current_state_; |
| + base::MemoryCoordinatorClientRegistry::GetInstance()->Notify(state); |
| +} |
| + |
| +void MemoryCoordinatorImpl::NotifyStateToChildren() { |
| + // TODO(bashi): Don't send SUSPENDED state to the foreground renderer. |
| + auto mojo_state = ToMojomMemoryState(current_state_); |
| + for (auto& iter : children()) |
| + SetMemoryState(iter.first, mojo_state); |
| +} |
| + |
| +} // namespace content |