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

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

Issue 2553553002: memory coordinator: Throttle foreground renderers on SUSPENDED state (Closed)
Patch Set: Fix unittests Created 4 years 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
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.h" 5 #include "content/browser/memory/memory_coordinator.h"
6 6
7 #include "base/memory/memory_coordinator_client_registry.h" 7 #include "base/memory/memory_coordinator_client_registry.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "content/public/browser/content_browser_client.h" 9 #include "content/public/browser/content_browser_client.h"
10 #include "content/public/browser/render_process_host.h" 10 #include "content/public/browser/render_process_host.h"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 81
82 // Can't send a message to a child that doesn't exist. 82 // Can't send a message to a child that doesn't exist.
83 auto iter = children_.find(render_process_id); 83 auto iter = children_.find(render_process_id);
84 if (iter == children_.end()) 84 if (iter == children_.end())
85 return false; 85 return false;
86 86
87 // Can't send a message to a child that isn't bound. 87 // Can't send a message to a child that isn't bound.
88 if (!iter->second.handle->child().is_bound()) 88 if (!iter->second.handle->child().is_bound())
89 return false; 89 return false;
90 90
91 // We don't suspend foreground renderers. Throttle them instead.
92 if (memory_state == mojom::MemoryState::SUSPENDED &&
93 iter->second.is_visible)
94 memory_state = mojom::MemoryState::THROTTLED;
95
91 // A nop doesn't need to be sent, but is considered successful. 96 // A nop doesn't need to be sent, but is considered successful.
92 if (iter->second.memory_state == memory_state) 97 if (iter->second.memory_state == memory_state)
93 return true; 98 return true;
94 99
95 // Can't throttle the given renderer.
96 if (memory_state == mojom::MemoryState::THROTTLED &&
97 !CanThrottleRenderer(render_process_id))
98 return false;
99
100 // Can't suspend the given renderer. 100 // Can't suspend the given renderer.
101 if (memory_state == mojom::MemoryState::SUSPENDED && 101 if (memory_state == mojom::MemoryState::SUSPENDED &&
102 !CanSuspendRenderer(render_process_id)) 102 !CanSuspendRenderer(render_process_id))
103 return false; 103 return false;
104 104
105 // Update the internal state and send the message. 105 // Update the internal state and send the message.
106 iter->second.memory_state = memory_state; 106 iter->second.memory_state = memory_state;
107 iter->second.handle->child()->OnStateChange(memory_state); 107 iter->second.handle->child()->OnStateChange(memory_state);
108 return true; 108 return true;
109 } 109 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 new MemoryCoordinatorHandleImpl(std::move(request), this, 156 new MemoryCoordinatorHandleImpl(std::move(request), this,
157 dummy_render_process_id)); 157 dummy_render_process_id));
158 handle->AddChild(std::move(child)); 158 handle->AddChild(std::move(child));
159 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle)); 159 CreateChildInfoMapEntry(dummy_render_process_id, std::move(handle));
160 } 160 }
161 161
162 void MemoryCoordinator::OnConnectionError(int render_process_id) { 162 void MemoryCoordinator::OnConnectionError(int render_process_id) {
163 children_.erase(render_process_id); 163 children_.erase(render_process_id);
164 } 164 }
165 165
166 bool MemoryCoordinator::CanThrottleRenderer(int render_process_id) {
167 // If there is no delegate (i.e. unittests), renderers are always
168 // throttleable.
169 // TODO(bashi): We check |delegate_| to avoid calling FromID() on a
170 // wrong thread in tests. Figure out a better way to handle tests.
171 if (!delegate_)
172 return true;
173 auto* render_process_host = RenderProcessHost::FromID(render_process_id);
174 return render_process_host && render_process_host->IsProcessBackgrounded();
175 }
176
177 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) { 166 bool MemoryCoordinator::CanSuspendRenderer(int render_process_id) {
178 // If there is no delegate (i.e. unittests), renderers are always suspendable. 167 // If there is no delegate (i.e. unittests), renderers are always suspendable.
179 if (!delegate_) 168 if (!delegate_)
180 return true; 169 return true;
181 auto* render_process_host = RenderProcessHost::FromID(render_process_id); 170 auto* render_process_host = RenderProcessHost::FromID(render_process_id);
182 if (!render_process_host || !render_process_host->IsProcessBackgrounded()) 171 if (!render_process_host || !render_process_host->IsProcessBackgrounded())
183 return false; 172 return false;
184 return delegate_->CanSuspendBackgroundedRenderer(render_process_id); 173 return delegate_->CanSuspendBackgroundedRenderer(render_process_id);
185 } 174 }
186 175
187 void MemoryCoordinator::SetDelegateForTesting( 176 void MemoryCoordinator::SetDelegateForTesting(
188 std::unique_ptr<MemoryCoordinatorDelegate> delegate) { 177 std::unique_ptr<MemoryCoordinatorDelegate> delegate) {
189 CHECK(!delegate_); 178 CHECK(!delegate_);
190 delegate_ = std::move(delegate); 179 delegate_ = std::move(delegate);
191 } 180 }
192 181
193 void MemoryCoordinator::CreateChildInfoMapEntry( 182 void MemoryCoordinator::CreateChildInfoMapEntry(
194 int render_process_id, 183 int render_process_id,
195 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { 184 std::unique_ptr<MemoryCoordinatorHandleImpl> handle) {
196 auto& child_info = children_[render_process_id]; 185 auto& child_info = children_[render_process_id];
197 // Process always start with normal memory state. 186 // Process always start with normal memory state.
198 // We'll set renderer's memory state to the current global state when the 187 // We'll set renderer's memory state to the current global state when the
199 // corresponding renderer process is ready to communicate. Renderer processes 188 // corresponding renderer process is ready to communicate. Renderer processes
200 // call AddChild() when they are ready. 189 // call AddChild() when they are ready.
201 child_info.memory_state = mojom::MemoryState::NORMAL; 190 child_info.memory_state = mojom::MemoryState::NORMAL;
191 child_info.is_visible = true;
202 child_info.handle = std::move(handle); 192 child_info.handle = std::move(handle);
203 } 193 }
204 194
205 MemoryCoordinator::ChildInfo::ChildInfo() {} 195 MemoryCoordinator::ChildInfo::ChildInfo() {}
206 196
207 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) { 197 MemoryCoordinator::ChildInfo::ChildInfo(const ChildInfo& rhs) {
208 // This is a nop, but exists for compatibility with STL containers. 198 // This is a nop, but exists for compatibility with STL containers.
209 } 199 }
210 200
211 MemoryCoordinator::ChildInfo::~ChildInfo() {} 201 MemoryCoordinator::ChildInfo::~ChildInfo() {}
212 202
213 } // namespace content 203 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator.h ('k') | content/browser/memory/memory_coordinator_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698