Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(812)

Side by Side Diff: content/browser/memory/memory_coordinator_impl.cc

Issue 2443093002: memory coordinator: Observe renderer visibility to update state (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/memory/memory_coordinator_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/memory/memory_coordinator_impl.h" 5 #include "content/browser/memory/memory_coordinator_impl.h"
6 6
7 #include "base/threading/thread_task_runner_handle.h" 7 #include "base/threading/thread_task_runner_handle.h"
8 #include "content/browser/memory/memory_monitor.h" 8 #include "content/browser/memory/memory_monitor.h"
9 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/notification_types.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/render_widget_host.h"
9 #include "content/public/common/content_features.h" 13 #include "content/public/common/content_features.h"
10 14
11 namespace content { 15 namespace content {
12 16
13 namespace { 17 namespace {
14 18
15 // A expected renderer size. These values come from the median of appropriate 19 // A expected renderer size. These values come from the median of appropriate
16 // UMA stats. 20 // UMA stats.
17 #if defined(OS_ANDROID) || defined(OS_IOS) 21 #if defined(OS_ANDROID) || defined(OS_IOS)
18 const int kDefaultExpectedRendererSizeMB = 40; 22 const int kDefaultExpectedRendererSizeMB = 40;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 monitoring_interval_ = 91 monitoring_interval_ =
88 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); 92 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds);
89 } 93 }
90 94
91 MemoryCoordinatorImpl::~MemoryCoordinatorImpl() {} 95 MemoryCoordinatorImpl::~MemoryCoordinatorImpl() {}
92 96
93 void MemoryCoordinatorImpl::Start() { 97 void MemoryCoordinatorImpl::Start() {
94 DCHECK(CalledOnValidThread()); 98 DCHECK(CalledOnValidThread());
95 DCHECK(last_state_change_.is_null()); 99 DCHECK(last_state_change_.is_null());
96 DCHECK(ValidateParameters()); 100 DCHECK(ValidateParameters());
101
102 notification_registrar_.Add(
103 this, NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
104 NotificationService::AllBrowserContextsAndSources());
97 ScheduleUpdateState(base::TimeDelta()); 105 ScheduleUpdateState(base::TimeDelta());
98 } 106 }
99 107
100 void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) { 108 void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) {
101 // Populate the global state as an initial state of a newly created process. 109 // Populate the global state as an initial state of a newly created process.
102 SetMemoryState(render_process_id, ToMojomMemoryState(current_state_)); 110 SetMemoryState(render_process_id, ToMojomMemoryState(current_state_));
103 } 111 }
104 112
105 base::MemoryState MemoryCoordinatorImpl::GetCurrentMemoryState() const { 113 base::MemoryState MemoryCoordinatorImpl::GetCurrentMemoryState() const {
106 return current_state_; 114 return current_state_;
107 } 115 }
108 116
117 void MemoryCoordinatorImpl::Observe(int type,
118 const NotificationSource& source,
119 const NotificationDetails& details) {
120 DCHECK(type == NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED);
121 RenderWidgetHost* render_widget_host = Source<RenderWidgetHost>(source).ptr();
122 RenderProcessHost* process = render_widget_host->GetProcess();
123 if (!process)
124 return;
125 auto iter = children().find(process->GetID());
126 if (iter == children().end())
127 return;
128 bool is_visible = *Details<bool>(details).ptr();
129 // We don't throttle/suspend a visible renderer for now.
130 auto new_state = is_visible ? mojom::MemoryState::NORMAL
131 : ToMojomMemoryState(current_state_);
132 SetMemoryState(iter->first, new_state);
133 }
134
109 base::MemoryState MemoryCoordinatorImpl::CalculateNextState() { 135 base::MemoryState MemoryCoordinatorImpl::CalculateNextState() {
110 using MemoryState = base::MemoryState; 136 using MemoryState = base::MemoryState;
111 137
112 int available = memory_monitor_->GetFreeMemoryUntilCriticalMB(); 138 int available = memory_monitor_->GetFreeMemoryUntilCriticalMB();
113 if (available <= 0) 139 if (available <= 0)
114 return MemoryState::SUSPENDED; 140 return MemoryState::SUSPENDED;
115 141
116 int expected_renderer_count = available / expected_renderer_size_; 142 int expected_renderer_count = available / expected_renderer_size_;
117 143
118 switch (current_state_) { 144 switch (current_state_) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 // TODO(bashi): Maybe worth considering another state for the browser. 193 // TODO(bashi): Maybe worth considering another state for the browser.
168 auto state = current_state_ == MemoryState::SUSPENDED ? MemoryState::THROTTLED 194 auto state = current_state_ == MemoryState::SUSPENDED ? MemoryState::THROTTLED
169 : current_state_; 195 : current_state_;
170 base::MemoryCoordinatorClientRegistry::GetInstance()->Notify(state); 196 base::MemoryCoordinatorClientRegistry::GetInstance()->Notify(state);
171 } 197 }
172 198
173 void MemoryCoordinatorImpl::NotifyStateToChildren() { 199 void MemoryCoordinatorImpl::NotifyStateToChildren() {
174 auto mojo_state = ToMojomMemoryState(current_state_); 200 auto mojo_state = ToMojomMemoryState(current_state_);
175 // It's OK to call SetMemoryState() unconditionally because it checks whether 201 // It's OK to call SetMemoryState() unconditionally because it checks whether
176 // this state transition is valid. 202 // this state transition is valid.
177 // TODO(bashi): In SUSPENDED state, we should update children's state
178 // accordingly when the foreground tab is changed (e.g. resume a renderer
179 // which becomes foreground and suspend a renderer which goes to background).
180 for (auto& iter : children()) 203 for (auto& iter : children())
181 SetMemoryState(iter.first, mojo_state); 204 SetMemoryState(iter.first, mojo_state);
182 } 205 }
183 206
184 void MemoryCoordinatorImpl::ScheduleUpdateState(base::TimeDelta delta) { 207 void MemoryCoordinatorImpl::ScheduleUpdateState(base::TimeDelta delta) {
185 task_runner_->PostDelayedTask(FROM_HERE, update_state_callback_, delta); 208 task_runner_->PostDelayedTask(FROM_HERE, update_state_callback_, delta);
186 } 209 }
187 210
188 bool MemoryCoordinatorImpl::ValidateParameters() { 211 bool MemoryCoordinatorImpl::ValidateParameters() {
189 return (new_renderers_until_throttled_ > new_renderers_until_suspended_) && 212 return (new_renderers_until_throttled_ > new_renderers_until_suspended_) &&
190 (new_renderers_back_to_normal_ > new_renderers_back_to_throttled_) && 213 (new_renderers_back_to_normal_ > new_renderers_back_to_throttled_) &&
191 (new_renderers_back_to_normal_ > new_renderers_until_throttled_) && 214 (new_renderers_back_to_normal_ > new_renderers_until_throttled_) &&
192 (new_renderers_back_to_throttled_ > new_renderers_until_suspended_); 215 (new_renderers_back_to_throttled_ > new_renderers_until_suspended_);
193 } 216 }
194 217
195 } // namespace content 218 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698