OLD | NEW |
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; |
33 using base::ProcessHandle; | 34 using base::ProcessHandle; |
34 using base::ProcessMetrics; | 35 using base::ProcessMetrics; |
35 | 36 |
36 namespace browser { | 37 namespace browser { |
37 | 38 |
38 // The default interval in seconds after which to adjust the oom_score_adj | 39 // The default interval in seconds after which to adjust the oom_score_adj |
39 // value. | 40 // value. |
40 #define ADJUSTMENT_INTERVAL_SECONDS 10 | 41 #define ADJUSTMENT_INTERVAL_SECONDS 10 |
41 | 42 |
42 // The default interval in minutes for considering activation times | 43 // The default interval in minutes for considering activation times |
43 // "equal". | 44 // "equal". |
44 #define BUCKET_INTERVAL_MINUTES 10 | 45 #define BUCKET_INTERVAL_MINUTES 10 |
45 | 46 |
| 47 // The default interval in milliseconds to wait before setting the score of |
| 48 // currently focused tab. |
| 49 #define FOCUSED_TAB_SCORE_ADJUST_INTERVAL_MS 500 |
| 50 |
46 OomPriorityManager::RendererStats::RendererStats() | 51 OomPriorityManager::RendererStats::RendererStats() |
47 : is_pinned(false), | 52 : is_pinned(false), |
48 is_selected(false), | 53 is_selected(false), |
49 memory_used(0), | 54 memory_used(0), |
50 renderer_handle(0) { | 55 renderer_handle(0) { |
51 } | 56 } |
52 | 57 |
53 OomPriorityManager::RendererStats::~RendererStats() { | 58 OomPriorityManager::RendererStats::~RendererStats() { |
54 } | 59 } |
55 | 60 |
56 OomPriorityManager::OomPriorityManager() { | 61 OomPriorityManager::OomPriorityManager() |
| 62 : focused_tab_pid_(0) { |
57 renderer_stats_.reserve(32); // 99% of users have < 30 tabs open | 63 renderer_stats_.reserve(32); // 99% of users have < 30 tabs open |
58 registrar_.Add(this, | 64 registrar_.Add(this, |
59 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 65 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
60 NotificationService::AllSources()); | 66 NotificationService::AllSources()); |
61 registrar_.Add(this, | 67 registrar_.Add(this, |
62 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 68 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
63 NotificationService::AllSources()); | 69 NotificationService::AllSources()); |
| 70 registrar_.Add(this, |
| 71 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 72 NotificationService::AllSources()); |
64 } | 73 } |
65 | 74 |
66 OomPriorityManager::~OomPriorityManager() { | 75 OomPriorityManager::~OomPriorityManager() { |
67 Stop(); | 76 Stop(); |
68 } | 77 } |
69 | 78 |
70 void OomPriorityManager::Start() { | 79 void OomPriorityManager::Start() { |
71 if (!timer_.IsRunning()) { | 80 if (!timer_.IsRunning()) { |
72 timer_.Start(FROM_HERE, | 81 timer_.Start(FROM_HERE, |
73 TimeDelta::FromSeconds(ADJUSTMENT_INTERVAL_SECONDS), | 82 TimeDelta::FromSeconds(ADJUSTMENT_INTERVAL_SECONDS), |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // it's not really possible for the times to be identical, but if | 121 // it's not really possible for the times to be identical, but if |
113 // the user selected two tabs at about the same time, we still want | 122 // the user selected two tabs at about the same time, we still want |
114 // to take the one that uses more memory. | 123 // to take the one that uses more memory. |
115 if (abs((first.last_selected - second.last_selected).ToInternalValue()) < | 124 if (abs((first.last_selected - second.last_selected).ToInternalValue()) < |
116 kTimeBucketInterval) | 125 kTimeBucketInterval) |
117 return first.last_selected > second.last_selected; | 126 return first.last_selected > second.last_selected; |
118 | 127 |
119 return first.memory_used < second.memory_used; | 128 return first.memory_used < second.memory_used; |
120 } | 129 } |
121 | 130 |
| 131 void OomPriorityManager::AdjustFocusedTabScore() { |
| 132 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_); |
| 133 ZygoteHost::GetInstance()->AdjustRendererOOMScore( |
| 134 focused_tab_pid_, chrome::kLowestRendererOomScore); |
| 135 pid_to_oom_score_[focused_tab_pid_] = chrome::kLowestRendererOomScore; |
| 136 } |
| 137 |
| 138 void OomPriorityManager::OnFocusTabScoreAdjustmentTimeout() { |
| 139 BrowserThread::PostTask( |
| 140 BrowserThread::FILE, FROM_HERE, |
| 141 NewRunnableMethod(this, &OomPriorityManager::AdjustFocusedTabScore)); |
| 142 } |
| 143 |
122 void OomPriorityManager::Observe(int type, const NotificationSource& source, | 144 void OomPriorityManager::Observe(int type, const NotificationSource& source, |
123 const NotificationDetails& details) { | 145 const NotificationDetails& details) { |
124 base::ProcessHandle handle = 0; | 146 base::ProcessHandle handle = 0; |
125 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_); | 147 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_); |
126 switch (type) { | 148 switch (type) { |
127 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: | 149 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: |
128 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 150 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
129 handle = Source<RenderProcessHost>(source)->GetHandle(); | 151 handle = Source<RenderProcessHost>(source)->GetHandle(); |
130 pid_to_oom_score_.erase(handle); | 152 pid_to_oom_score_.erase(handle); |
131 break; | 153 break; |
132 } | 154 } |
| 155 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: { |
| 156 bool visible = *Details<bool>(details).ptr(); |
| 157 if (visible) { |
| 158 focused_tab_pid_ = |
| 159 Source<RenderWidgetHost>(source).ptr()->process()->GetHandle(); |
| 160 |
| 161 // If the currently focused tab already has a lower score, do not |
| 162 // set it. This can happen in case the newly focused tab is script |
| 163 // connected to the previous tab. |
| 164 ProcessScoreMap::iterator it; |
| 165 it = pid_to_oom_score_.find(focused_tab_pid_); |
| 166 if (it == pid_to_oom_score_.end() |
| 167 || it->second != chrome::kLowestRendererOomScore) { |
| 168 // By starting a timer we guarantee that the tab is focused for |
| 169 // certain amount of time. Secondly, it also does not add overhead |
| 170 // to the tab switching time. |
| 171 if (focus_tab_score_adjust_timer_.IsRunning()) |
| 172 focus_tab_score_adjust_timer_.Reset(); |
| 173 else |
| 174 focus_tab_score_adjust_timer_.Start(FROM_HERE, |
| 175 TimeDelta::FromMilliseconds(FOCUSED_TAB_SCORE_ADJUST_INTERVAL_MS), |
| 176 this, &OomPriorityManager::OnFocusTabScoreAdjustmentTimeout); |
| 177 } |
| 178 } |
| 179 break; |
| 180 } |
133 default: | 181 default: |
134 NOTREACHED() << L"Received unexpected notification"; | 182 NOTREACHED() << L"Received unexpected notification"; |
135 break; | 183 break; |
136 } | 184 } |
137 } | 185 } |
138 | 186 |
139 // Here we collect most of the information we need to sort the | 187 // Here we collect most of the information we need to sort the |
140 // existing renderers in priority order, and hand out oom_score_adj | 188 // existing renderers in priority order, and hand out oom_score_adj |
141 // scores based on that sort order. | 189 // scores based on that sort order. |
142 // | 190 // |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 ZygoteHost::GetInstance()->AdjustRendererOOMScore( | 294 ZygoteHost::GetInstance()->AdjustRendererOOMScore( |
247 iterator->renderer_handle, score); | 295 iterator->renderer_handle, score); |
248 pid_to_oom_score_[iterator->renderer_handle] = score; | 296 pid_to_oom_score_[iterator->renderer_handle] = score; |
249 } | 297 } |
250 priority += priority_increment; | 298 priority += priority_increment; |
251 } | 299 } |
252 } | 300 } |
253 } | 301 } |
254 | 302 |
255 } // namespace browser | 303 } // namespace browser |
OLD | NEW |