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 "base/threading/thread_task_runner_handle.h" | |
8 #include "content/browser/memory/memory_monitor.h" | |
9 #include "content/public/common/content_features.h" | |
10 | |
11 namespace content { | |
12 | |
13 namespace { | |
14 | |
15 #if defined(OS_ANDROID) | |
16 static const int kDefaultPredictedRendererSizeMB = 40; | |
17 #else | |
18 static const int kDefaultPredictedRendererSizeMB = 80; | |
19 #endif | |
20 | |
21 mojom::MemoryState ToMojomMemoryState(base::MemoryState state) { | |
22 switch (state) { | |
23 case base::MemoryState::UNKNOWN: | |
24 return mojom::MemoryState::UNKNOWN; | |
25 case base::MemoryState::NORMAL: | |
26 return mojom::MemoryState::NORMAL; | |
27 case base::MemoryState::THROTTLED: | |
28 return mojom::MemoryState::THROTTLED; | |
29 case base::MemoryState::SUSPENDED: | |
30 return mojom::MemoryState::SUSPENDED; | |
31 default: | |
32 NOTREACHED(); | |
33 return mojom::MemoryState::UNKNOWN; | |
34 } | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 // SingletonTraits for MemoryCoordinator. Returns MemoryCoordinatorImpl | |
40 // as an actual instance. | |
41 struct MemoryCoordinatorSingletonTraits | |
42 : public base::LeakySingletonTraits<MemoryCoordinator> { | |
43 static MemoryCoordinator* New() { | |
44 return new MemoryCoordinatorImpl(base::ThreadTaskRunnerHandle::Get(), | |
45 CreateMemoryMonitor()); | |
46 } | |
47 }; | |
48 | |
49 // static | |
50 MemoryCoordinator* MemoryCoordinator::GetInstance() { | |
51 if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) | |
52 return nullptr; | |
53 return base::Singleton<MemoryCoordinator, | |
54 MemoryCoordinatorSingletonTraits>::get(); | |
55 } | |
56 | |
57 MemoryCoordinatorImpl::MemoryCoordinatorImpl( | |
58 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
59 std::unique_ptr<MemoryMonitor> memory_monitor) | |
60 : task_runner_(task_runner), memory_monitor_(std::move(memory_monitor)) { | |
61 DCHECK(memory_monitor_.get()); | |
62 timer_.SetTaskRunner(task_runner_); | |
63 // Set default configuration. | |
64 // TODO(bashi): Provide a way to change these configuration. | |
65 config_.predicted_renderer_size = kDefaultPredictedRendererSizeMB; | |
66 config_.num_children_until_throttled = 4; | |
67 config_.num_children_until_suspended = 2; | |
68 config_.minimum_transition_period = base::TimeDelta::FromSeconds(30); | |
69 config_.monitoring_iterval = base::TimeDelta::FromSeconds(5); | |
70 } | |
71 | |
72 MemoryCoordinatorImpl::~MemoryCoordinatorImpl() { | |
73 timer_.Stop(); | |
74 } | |
75 | |
76 void MemoryCoordinatorImpl::Start() { | |
77 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.
| |
78 this, &MemoryCoordinatorImpl::UpdateState); | |
79 } | |
80 | |
81 void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) { | |
82 // Populate the global state as an initial state of a newly created process. | |
83 SetMemoryState(render_process_id, ToMojomMemoryState(current_state_)); | |
84 } | |
85 | |
86 base::MemoryState MemoryCoordinatorImpl::CalculateNextState() { | |
87 using MemoryState = base::MemoryState; | |
88 | |
89 int available = memory_monitor_->GetFreeMemoryUntilCriticalMB(); | |
90 if (available <= 0) | |
91 return MemoryState::SUSPENDED; | |
92 | |
93 int num_children_until_critical = available / config_.predicted_renderer_size; | |
94 switch (current_state_) { | |
95 case MemoryState::NORMAL: | |
96 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.
| |
97 return MemoryState::SUSPENDED; | |
98 if (num_children_until_critical < config_.num_children_until_throttled) | |
99 return MemoryState::THROTTLED; | |
100 return MemoryState::NORMAL; | |
101 case MemoryState::THROTTLED: | |
102 if (num_children_until_critical < config_.num_children_until_suspended) | |
103 return MemoryState::SUSPENDED; | |
104 if (num_children_until_critical >= config_.num_children_until_throttled) | |
105 return MemoryState::NORMAL; | |
106 return MemoryState::THROTTLED; | |
107 case MemoryState::SUSPENDED: | |
108 if (num_children_until_critical >= config_.num_children_until_throttled) | |
109 return MemoryState::NORMAL; | |
110 if (num_children_until_critical >= config_.num_children_until_suspended) | |
111 return MemoryState::THROTTLED; | |
112 return MemoryState::SUSPENDED; | |
113 case MemoryState::UNKNOWN: | |
114 // Fall through | |
115 default: | |
116 NOTREACHED(); | |
117 return MemoryState::UNKNOWN; | |
118 } | |
119 } | |
120 | |
121 void MemoryCoordinatorImpl::UpdateState() { | |
122 base::TimeTicks now = base::TimeTicks::Now(); | |
123 if (!last_state_change_.is_null() && | |
124 now - last_state_change_ < config_.minimum_transition_period) { | |
125 return; | |
126 } | |
127 | |
128 MemoryState next_state = CalculateNextState(); | |
129 if (current_state_ != next_state) { | |
130 current_state_ = next_state; | |
131 last_state_change_ = now; | |
132 NotifyStateToClients(); | |
133 NotifyStateToChildren(); | |
134 } | |
135 } | |
136 | |
137 void MemoryCoordinatorImpl::NotifyStateToClients() { | |
138 // SUSPENDED state may not make sense to the browser process. Use THROTTLED | |
139 // instead when the global state is SUSPENDED. | |
140 // 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
| |
141 auto state = current_state_ == MemoryState::SUSPENDED ? MemoryState::THROTTLED | |
142 : current_state_; | |
143 base::MemoryCoordinatorClientRegistry::GetInstance()->Notify(state); | |
144 } | |
145 | |
146 void MemoryCoordinatorImpl::NotifyStateToChildren() { | |
147 // TODO(bashi): Don't send SUSPENDED state to the foreground renderer. | |
148 auto mojo_state = ToMojomMemoryState(current_state_); | |
149 for (auto& iter : children()) | |
150 SetMemoryState(iter.first, mojo_state); | |
151 } | |
152 | |
153 } // namespace content | |
OLD | NEW |