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

Side by Side Diff: chrome/browser/oom_priority_manager.cc

Issue 7930022: chromeos: Set a least score to the currently focused tab so that it is not selected (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 9 years, 3 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/oom_priority_manager.h" 5 #include "chrome/browser/oom_priority_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/process.h" 10 #include "base/process.h"
11 #include "base/process_util.h" 11 #include "base/process_util.h"
12 #include "base/string16.h" 12 #include "base/string16.h"
13 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
14 #include "base/threading/thread.h" 14 #include "base/threading/thread.h"
15 #include "base/timer.h" 15 #include "base/timer.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "chrome/browser/tabs/tab_strip_model.h" 17 #include "chrome/browser/tabs/tab_strip_model.h"
18 #include "chrome/browser/ui/browser_list.h" 18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
20 #include "chrome/common/chrome_constants.h" 20 #include "chrome/common/chrome_constants.h"
21 #include "content/browser/browser_thread.h" 21 #include "content/browser/browser_thread.h"
22 #include "content/browser/renderer_host/render_process_host.h" 22 #include "content/browser/renderer_host/render_process_host.h"
23 #include "content/browser/renderer_host/render_widget_host.h"
23 #include "content/browser/tab_contents/tab_contents.h" 24 #include "content/browser/tab_contents/tab_contents.h"
24 #include "content/browser/zygote_host_linux.h" 25 #include "content/browser/zygote_host_linux.h"
25 #include "content/common/notification_service.h" 26 #include "content/common/notification_service.h"
26 27
27 #if !defined(OS_CHROMEOS) 28 #if !defined(OS_CHROMEOS)
28 #error This file only meant to be compiled on ChromeOS 29 #error This file only meant to be compiled on ChromeOS
29 #endif 30 #endif
30 31
31 using base::TimeDelta; 32 using base::TimeDelta;
32 using base::TimeTicks; 33 using base::TimeTicks;
(...skipping 18 matching lines...) Expand all
51 } 52 }
52 53
53 OomPriorityManager::RendererStats::~RendererStats() { 54 OomPriorityManager::RendererStats::~RendererStats() {
54 } 55 }
55 56
56 // static 57 // static
57 OomPriorityManager* OomPriorityManager::GetInstance() { 58 OomPriorityManager* OomPriorityManager::GetInstance() {
58 return Singleton<OomPriorityManager>::get(); 59 return Singleton<OomPriorityManager>::get();
59 } 60 }
60 61
61 OomPriorityManager::OomPriorityManager() { 62 OomPriorityManager::OomPriorityManager()
63 : last_used_score_(chrome::kLowestRendererOomScore) {
62 renderer_stats_.reserve(32); // 99% of users have < 30 tabs open 64 renderer_stats_.reserve(32); // 99% of users have < 30 tabs open
63 registrar_.Add(this, 65 registrar_.Add(this,
64 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 66 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
65 NotificationService::AllSources()); 67 NotificationService::AllSources());
66 registrar_.Add(this, 68 registrar_.Add(this,
67 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 69 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
68 NotificationService::AllSources()); 70 NotificationService::AllSources());
71 registrar_.Add(this,
72 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
73 NotificationService::AllSources());
69 } 74 }
70 75
71 OomPriorityManager::~OomPriorityManager() { 76 OomPriorityManager::~OomPriorityManager() {
72 Stop(); 77 Stop();
73 } 78 }
74 79
75 void OomPriorityManager::Start() { 80 void OomPriorityManager::Start() {
76 if (!timer_.IsRunning()) { 81 if (!timer_.IsRunning()) {
77 timer_.Start(FROM_HERE, 82 timer_.Start(FROM_HERE,
78 TimeDelta::FromSeconds(ADJUSTMENT_INTERVAL_SECONDS), 83 TimeDelta::FromSeconds(ADJUSTMENT_INTERVAL_SECONDS),
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // it's not really possible for the times to be identical, but if 122 // it's not really possible for the times to be identical, but if
118 // the user selected two tabs at about the same time, we still want 123 // the user selected two tabs at about the same time, we still want
119 // to take the one that uses more memory. 124 // to take the one that uses more memory.
120 if (abs((first.last_selected - second.last_selected).ToInternalValue()) < 125 if (abs((first.last_selected - second.last_selected).ToInternalValue()) <
121 kTimeBucketInterval) 126 kTimeBucketInterval)
122 return first.last_selected > second.last_selected; 127 return first.last_selected > second.last_selected;
123 128
124 return first.memory_used < second.memory_used; 129 return first.memory_used < second.memory_used;
125 } 130 }
126 131
132 void OomPriorityManager::AdjustTabScore(base::ProcessHandle pid) {
133 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_);
134 // Use a score 1 less than the last used score. A lesser value ensures
James Cook 2011/09/19 17:08:55 I don't think this does what you want. oom_score_
amruthraj 2011/09/19 17:17:05 The problem with this approach is that when a user
Greg Spencer (Chromium) 2011/09/19 19:45:51 I agree with James that this isn't really what you
amruthraj 2011/09/20 10:07:54 Done.
135 // that the currently focused tab doesn't get killed.
136 int score = --last_used_score_;
137 ZygoteHost::GetInstance()->AdjustRendererOOMScore(
138 pid, score);
139 pid_to_oom_score_[pid] = score;
140 }
141
127 void OomPriorityManager::Observe(int type, const NotificationSource& source, 142 void OomPriorityManager::Observe(int type, const NotificationSource& source,
128 const NotificationDetails& details) { 143 const NotificationDetails& details) {
129 base::ProcessHandle handle = 0; 144 base::ProcessHandle handle = 0;
130 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_); 145 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_);
131 switch (type) { 146 switch (type) {
132 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: 147 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED:
133 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { 148 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
134 handle = Source<RenderProcessHost>(source)->GetHandle(); 149 handle = Source<RenderProcessHost>(source)->GetHandle();
135 pid_to_oom_score_.erase(handle); 150 pid_to_oom_score_.erase(handle);
136 break; 151 break;
137 } 152 }
153 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: {
James Cook 2011/09/19 17:08:55 Does this fire just when the widget becomes visibl
amruthraj 2011/09/19 17:17:05 It gets fired in invisible case too. But the if co
amruthraj 2011/09/20 10:07:54 Added a variable visible here.
154 if (*Details<bool>(details).ptr()) {
155 base::ProcessHandle pid =
156 Source<RenderWidgetHost>(source).ptr()->process()->GetHandle();
157 // Post a delayed task so that the tab switch time doesn't get effected.
Greg Spencer (Chromium) 2011/09/19 19:45:51 effected -> affected
amruthraj 2011/09/20 10:07:54 Done.
158 BrowserThread::PostDelayedTask(
Greg Spencer (Chromium) 2011/09/19 19:45:51 This doesn't replace previous tasks in the queue,
amruthraj 2011/09/20 10:07:54 Done.
159 BrowserThread::FILE, FROM_HERE,
160 NewRunnableMethod(this, &OomPriorityManager::AdjustTabScore, pid),
161 200);
James Cook 2011/09/19 17:08:55 200 should be a named constant
amruthraj 2011/09/20 10:07:54 Done.
162 }
163 break;
164 }
138 default: 165 default:
139 NOTREACHED() << L"Received unexpected notification"; 166 NOTREACHED() << L"Received unexpected notification";
140 break; 167 break;
141 } 168 }
142 } 169 }
143 170
144 // Here we collect most of the information we need to sort the 171 // Here we collect most of the information we need to sort the
145 // existing renderers in priority order, and hand out oom_score_adj 172 // existing renderers in priority order, and hand out oom_score_adj
146 // scores based on that sort order. 173 // scores based on that sort order.
147 // 174 //
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 // below the range we're using to allow for things that want to be 251 // below the range we're using to allow for things that want to be
225 // above the renderers in priority, so the defined range gives us 252 // above the renderers in priority, so the defined range gives us
226 // some variation in priority without taking up the whole range. In 253 // some variation in priority without taking up the whole range. In
227 // the end, however, it's a pretty arbitrary range to use. Higher 254 // the end, however, it's a pretty arbitrary range to use. Higher
228 // values are more likely to be killed by the OOM killer. 255 // values are more likely to be killed by the OOM killer.
229 // 256 //
230 // We also remove any duplicate PIDs, leaving the most important 257 // We also remove any duplicate PIDs, leaving the most important
231 // (least likely to be killed) of the duplicates, so that a 258 // (least likely to be killed) of the duplicates, so that a
232 // particular renderer process takes on the oom_score_adj of the 259 // particular renderer process takes on the oom_score_adj of the
233 // least likely tab to be killed. 260 // least likely tab to be killed.
234 const int kPriorityRange = chrome::kHighestRendererOomScore - 261 float priority_increment = -1;
James Cook 2011/09/19 17:08:55 I think the increment here needs to be much larger
amruthraj 2011/09/20 10:07:54 I reverted all the changes here.
235 chrome::kLowestRendererOomScore; 262 int priority = chrome::kHighestRendererOomScore;
236 float priority_increment =
237 static_cast<float>(kPriorityRange) / renderer_stats_.size();
238 float priority = chrome::kLowestRendererOomScore;
239 std::set<base::ProcessHandle> already_seen; 263 std::set<base::ProcessHandle> already_seen;
240 int score = 0; 264 int score = 0;
241 ProcessScoreMap::iterator it; 265 ProcessScoreMap::iterator it;
242 for (StatsList::iterator iterator = renderer_stats_.begin(); 266 for (StatsList::reverse_iterator iterator = renderer_stats_.rbegin();
243 iterator != renderer_stats_.end(); ++iterator) { 267 iterator != renderer_stats_.rend(); ++iterator) {
244 if (already_seen.find(iterator->renderer_handle) == already_seen.end()) { 268 if (already_seen.find(iterator->renderer_handle) == already_seen.end()) {
245 already_seen.insert(iterator->renderer_handle); 269 already_seen.insert(iterator->renderer_handle);
270 score = std::max(chrome::kLowestRendererOomScore, priority);
271 it = pid_to_oom_score_.find(iterator->renderer_handle);
246 // If a process has the same score as the newly calculated value, 272 // If a process has the same score as the newly calculated value,
247 // do not set it. 273 // do not set it.
248 score = static_cast<int>(priority + 0.5f);
249 it = pid_to_oom_score_.find(iterator->renderer_handle);
250 if (it == pid_to_oom_score_.end() || it->second != score) { 274 if (it == pid_to_oom_score_.end() || it->second != score) {
251 ZygoteHost::GetInstance()->AdjustRendererOOMScore( 275 ZygoteHost::GetInstance()->AdjustRendererOOMScore(
252 iterator->renderer_handle, score); 276 iterator->renderer_handle, score);
253 pid_to_oom_score_[iterator->renderer_handle] = score; 277 pid_to_oom_score_[iterator->renderer_handle] = score;
254 } 278 }
255 priority += priority_increment; 279 priority += priority_increment;
256 } 280 }
257 } 281 }
282 last_used_score_ = score;
258 } 283 }
259 284
260 } // namespace browser 285 } // namespace browser
OLDNEW
« chrome/browser/oom_priority_manager.h ('K') | « chrome/browser/oom_priority_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698