Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/memory/memory_coordinator_impl.h" | |
| 6 | |
| 7 #include "content/browser/memory/memory_monitor.h" | |
| 8 #include "content/public/common/content_features.h" | |
| 9 | |
| 10 namespace content { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 mojom::MemoryState ToMojomMemoryState(base::MemoryState state) { | |
| 15 switch (state) { | |
| 16 case base::MemoryState::UNKNOWN: | |
| 17 return mojom::MemoryState::UNKNOWN; | |
| 18 case base::MemoryState::NORMAL: | |
| 19 return mojom::MemoryState::NORMAL; | |
| 20 case base::MemoryState::THROTTLED: | |
| 21 return mojom::MemoryState::THROTTLED; | |
| 22 case base::MemoryState::SUSPENDED: | |
| 23 return mojom::MemoryState::SUSPENDED; | |
| 24 default: | |
| 25 NOTREACHED(); | |
| 26 return mojom::MemoryState::UNKNOWN; | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 // SingletonTraits for MemoryCoordinator. Returns MemoryCoordinatorImpl | |
| 33 // as an actual instance. | |
| 34 struct MemoryCoordinatorSingletonTraits | |
| 35 : public base::LeakySingletonTraits<MemoryCoordinator> { | |
| 36 static MemoryCoordinator* New() { | |
| 37 return new MemoryCoordinatorImpl(CreateMemoryMonitor()); | |
| 38 } | |
| 39 }; | |
| 40 | |
| 41 // static | |
| 42 MemoryCoordinator* MemoryCoordinator::GetInstance() { | |
| 43 if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) | |
| 44 return nullptr; | |
| 45 return base::Singleton<MemoryCoordinator, | |
| 46 MemoryCoordinatorSingletonTraits>::get(); | |
| 47 } | |
| 48 | |
| 49 MemoryCoordinatorImpl::MemoryCoordinatorImpl( | |
| 50 std::unique_ptr<MemoryMonitor> memory_monitor) | |
| 51 : memory_monitor_(std::move(memory_monitor)) { | |
| 52 DCHECK(memory_monitor_.get()); | |
| 53 // Set default configuration. | |
| 54 // TODO(bashi): Provide a way to change these configuration. | |
| 55 config_.pressure_threshold_mb = 0; | |
| 56 config_.num_children_until_suspended = 2; | |
| 57 config_.minimum_transition_period = base::TimeDelta::FromSeconds(30); | |
| 58 } | |
| 59 | |
| 60 MemoryCoordinatorImpl::~MemoryCoordinatorImpl() {} | |
| 61 | |
| 62 void MemoryCoordinatorImpl::OnChildAdded() { | |
| 63 UpdateState(); | |
| 64 } | |
| 65 | |
| 66 void MemoryCoordinatorImpl::OnChildRemoved() { | |
| 67 UpdateState(); | |
| 68 } | |
| 69 | |
| 70 void MemoryCoordinatorImpl::UpdateState() { | |
| 71 base::TimeTicks now = base::TimeTicks::Now(); | |
| 72 if (!last_state_change_.is_null() && | |
| 73 now - last_state_change_ < config_.minimum_transition_period) | |
| 74 return; | |
| 75 | |
| 76 // TODO(bashi): Come up with a good strategy. | |
| 77 // The current strategy is that: | |
| 78 // * There is only one global memory state. | |
| 79 // * When avaiable free memory is below a certain threshold (default to | |
| 80 // zero), enter either THROTTLED or SUSPENDED state based on the number | |
| 81 // of child processes. | |
|
haraken
2016/09/30 02:29:42
I'm curious what Chris has in mind, but I'd define
| |
| 82 base::MemoryState previous_state = current_state_; | |
| 83 int available = memory_monitor_->GetFreeMemoryUntilCriticalMB(); | |
| 84 if (available > config_.pressure_threshold_mb) { | |
| 85 if (current_state_ != MemoryState::NORMAL) { | |
| 86 current_state_ = MemoryState::NORMAL; | |
| 87 } | |
| 88 } else { | |
| 89 if (children().size() <= config_.num_children_until_suspended) { | |
| 90 current_state_ = MemoryState::SUSPENDED; | |
| 91 } else { | |
| 92 current_state_ = MemoryState::THROTTLED; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 if (current_state_ != previous_state) { | |
| 97 last_state_change_ = now; | |
| 98 NotifyStateToClients(); | |
| 99 NotifyStateToChildren(); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void MemoryCoordinatorImpl::NotifyStateToClients() { | |
| 104 // SUSPENDED state may not make sense to the browser process. Use THROTTLED | |
| 105 // instead when the global state is SUSPENDED. | |
| 106 // TODO(bashi): Maybe worth considering another state for the browser. | |
| 107 auto state = current_state_ == MemoryState::SUSPENDED ? MemoryState::THROTTLED | |
| 108 : current_state_; | |
| 109 base::MemoryCoordinatorClientRegistry::GetInstance()->Notify(state); | |
| 110 } | |
| 111 | |
| 112 void MemoryCoordinatorImpl::NotifyStateToChildren() { | |
| 113 // TODO(bashi): Don't send SUSPENDED state to the foreground renderer. | |
| 114 for (auto& iter : children()) { | |
| 115 auto& child_info = iter.second; | |
| 116 mojom::MemoryState state = ToMojomMemoryState(current_state_); | |
| 117 child_info.memory_state = state; | |
| 118 // TODO(bashi): We shouldn't check these conditions but checking them | |
| 119 // otherwise crashes. Find & fix the root cause. | |
| 120 if (child_info.handle->child().get() && | |
| 121 child_info.handle->child().is_bound()) | |
| 122 child_info.handle->child()->OnStateChange(state); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 } // namespace content | |
| OLD | NEW |