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..b65da8b3f3191f54ad109bce76207c530ecc2766 |
| --- /dev/null |
| +++ b/content/browser/memory/memory_coordinator_impl.cc |
| @@ -0,0 +1,126 @@ |
| +// 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 "content/browser/memory/memory_monitor.h" |
| +#include "content/public/common/content_features.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +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(CreateMemoryMonitor()); |
| + } |
| +}; |
| + |
| +// static |
| +MemoryCoordinator* MemoryCoordinator::GetInstance() { |
| + if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) |
| + return nullptr; |
| + return base::Singleton<MemoryCoordinator, |
| + MemoryCoordinatorSingletonTraits>::get(); |
| +} |
| + |
| +MemoryCoordinatorImpl::MemoryCoordinatorImpl( |
| + std::unique_ptr<MemoryMonitor> memory_monitor) |
| + : memory_monitor_(std::move(memory_monitor)) { |
| + DCHECK(memory_monitor_.get()); |
| + // Set default configuration. |
| + // TODO(bashi): Provide a way to change these configuration. |
| + config_.pressure_threshold_mb = 0; |
| + config_.num_children_until_suspended = 2; |
| + config_.minimum_transition_period = base::TimeDelta::FromSeconds(30); |
| +} |
| + |
| +MemoryCoordinatorImpl::~MemoryCoordinatorImpl() {} |
| + |
| +void MemoryCoordinatorImpl::OnChildAdded() { |
| + UpdateState(); |
| +} |
| + |
| +void MemoryCoordinatorImpl::OnChildRemoved() { |
| + UpdateState(); |
| +} |
| + |
| +void MemoryCoordinatorImpl::UpdateState() { |
| + base::TimeTicks now = base::TimeTicks::Now(); |
| + if (!last_state_change_.is_null() && |
| + now - last_state_change_ < config_.minimum_transition_period) |
| + return; |
| + |
| + // TODO(bashi): Come up with a good strategy. |
| + // The current strategy is that: |
| + // * There is only one global memory state. |
| + // * When avaiable free memory is below a certain threshold (default to |
| + // zero), enter either THROTTLED or SUSPENDED state based on the number |
| + // of child processes. |
|
haraken
2016/09/30 02:29:42
I'm curious what Chris has in mind, but I'd define
|
| + base::MemoryState previous_state = current_state_; |
| + int available = memory_monitor_->GetFreeMemoryUntilCriticalMB(); |
| + if (available > config_.pressure_threshold_mb) { |
| + if (current_state_ != MemoryState::NORMAL) { |
| + current_state_ = MemoryState::NORMAL; |
| + } |
| + } else { |
| + if (children().size() <= config_.num_children_until_suspended) { |
| + current_state_ = MemoryState::SUSPENDED; |
| + } else { |
| + current_state_ = MemoryState::THROTTLED; |
| + } |
| + } |
| + |
| + if (current_state_ != previous_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. |
| + 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. |
| + for (auto& iter : children()) { |
| + auto& child_info = iter.second; |
| + mojom::MemoryState state = ToMojomMemoryState(current_state_); |
| + child_info.memory_state = state; |
| + // TODO(bashi): We shouldn't check these conditions but checking them |
| + // otherwise crashes. Find & fix the root cause. |
| + if (child_info.handle->child().get() && |
| + child_info.handle->child().is_bound()) |
| + child_info.handle->child()->OnStateChange(state); |
| + } |
| +} |
| + |
| +} // namespace content |