Chromium Code Reviews| 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 // static | 61 // static |
| 57 OomPriorityManager* OomPriorityManager::GetInstance() { | 62 OomPriorityManager* OomPriorityManager::GetInstance() { |
| 58 return Singleton<OomPriorityManager>::get(); | 63 return Singleton<OomPriorityManager>::get(); |
| 59 } | 64 } |
| 60 | 65 |
| 61 OomPriorityManager::OomPriorityManager() { | 66 OomPriorityManager::OomPriorityManager() |
| 67 : focused_tab_pid_(0) { | |
| 62 renderer_stats_.reserve(32); // 99% of users have < 30 tabs open | 68 renderer_stats_.reserve(32); // 99% of users have < 30 tabs open |
| 63 registrar_.Add(this, | 69 registrar_.Add(this, |
| 64 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 70 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 65 NotificationService::AllSources()); | 71 NotificationService::AllSources()); |
| 66 registrar_.Add(this, | 72 registrar_.Add(this, |
| 67 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 73 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 68 NotificationService::AllSources()); | 74 NotificationService::AllSources()); |
| 75 registrar_.Add(this, | |
| 76 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | |
| 77 NotificationService::AllSources()); | |
| 69 } | 78 } |
| 70 | 79 |
| 71 OomPriorityManager::~OomPriorityManager() { | 80 OomPriorityManager::~OomPriorityManager() { |
| 72 Stop(); | 81 Stop(); |
| 73 } | 82 } |
| 74 | 83 |
| 75 void OomPriorityManager::Start() { | 84 void OomPriorityManager::Start() { |
| 76 if (!timer_.IsRunning()) { | 85 if (!timer_.IsRunning()) { |
| 77 timer_.Start(FROM_HERE, | 86 timer_.Start(FROM_HERE, |
| 78 TimeDelta::FromSeconds(ADJUSTMENT_INTERVAL_SECONDS), | 87 TimeDelta::FromSeconds(ADJUSTMENT_INTERVAL_SECONDS), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 // it's not really possible for the times to be identical, but if | 126 // 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 | 127 // the user selected two tabs at about the same time, we still want |
| 119 // to take the one that uses more memory. | 128 // to take the one that uses more memory. |
| 120 if (abs((first.last_selected - second.last_selected).ToInternalValue()) < | 129 if (abs((first.last_selected - second.last_selected).ToInternalValue()) < |
| 121 kTimeBucketInterval) | 130 kTimeBucketInterval) |
| 122 return first.last_selected > second.last_selected; | 131 return first.last_selected > second.last_selected; |
| 123 | 132 |
| 124 return first.memory_used < second.memory_used; | 133 return first.memory_used < second.memory_used; |
| 125 } | 134 } |
| 126 | 135 |
| 136 void OomPriorityManager::AdjustFocusedTabScore() { | |
| 137 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_); | |
| 138 ZygoteHost::GetInstance()->AdjustRendererOOMScore( | |
| 139 focused_tab_pid_, chrome::kLowestRendererOomScore); | |
| 140 pid_to_oom_score_[focused_tab_pid_] = chrome::kLowestRendererOomScore; | |
| 141 } | |
| 142 | |
| 143 void OomPriorityManager::FocusTabScoreAdjustmentTimeout() { | |
| 144 BrowserThread::PostTask( | |
| 145 BrowserThread::FILE, FROM_HERE, | |
| 146 NewRunnableMethod(this, &OomPriorityManager::AdjustFocusedTabScore)); | |
| 147 } | |
| 148 | |
| 127 void OomPriorityManager::Observe(int type, const NotificationSource& source, | 149 void OomPriorityManager::Observe(int type, const NotificationSource& source, |
| 128 const NotificationDetails& details) { | 150 const NotificationDetails& details) { |
| 129 base::ProcessHandle handle = 0; | 151 base::ProcessHandle handle = 0; |
| 130 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_); | 152 base::AutoLock pid_to_oom_score_autolock(pid_to_oom_score_lock_); |
| 131 switch (type) { | 153 switch (type) { |
| 132 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: | 154 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: |
| 133 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 155 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
| 134 handle = Source<RenderProcessHost>(source)->GetHandle(); | 156 handle = Source<RenderProcessHost>(source)->GetHandle(); |
| 135 pid_to_oom_score_.erase(handle); | 157 pid_to_oom_score_.erase(handle); |
| 136 break; | 158 break; |
| 137 } | 159 } |
| 160 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: { | |
| 161 bool visible = *Details<bool>(details).ptr(); | |
| 162 if (visible) { | |
| 163 focused_tab_pid_ = | |
| 164 Source<RenderWidgetHost>(source).ptr()->process()->GetHandle(); | |
| 165 | |
| 166 // If the currently focused tab already has a lower score, do not | |
| 167 // set it. This can happen in case the newly focused tab is script | |
| 168 // connected to the previous tab. | |
| 169 ProcessScoreMap::iterator it; | |
| 170 it = pid_to_oom_score_.find(focused_tab_pid_); | |
| 171 if (it != pid_to_oom_score_.end() | |
| 172 && it->second == chrome::kLowestRendererOomScore) | |
| 173 break; | |
|
James Cook
2011/09/20 17:08:03
break needs to be indented, or reverse the if() co
amruthraj
2011/09/21 04:25:01
Done.
| |
| 174 | |
| 175 // By starting a timer we guarantee that the tab is focused for certain | |
| 176 // amount of time. Secondly, it also does not add overhead to the tab | |
| 177 // switching time. | |
| 178 if (focus_tab_score_adjust_timer_.IsRunning()) | |
| 179 focus_tab_score_adjust_timer_.Reset(); | |
|
James Cook
2011/09/20 17:08:03
Good idea - it won't adjust scores while tab focus
| |
| 180 else | |
| 181 focus_tab_score_adjust_timer_.Start(FROM_HERE, | |
| 182 TimeDelta::FromMilliseconds(FOCUSED_TAB_SCORE_ADJUST_INTERVAL_MS), | |
| 183 this, &OomPriorityManager::FocusTabScoreAdjustmentTimeout); | |
| 184 } | |
| 185 break; | |
| 186 } | |
| 138 default: | 187 default: |
| 139 NOTREACHED() << L"Received unexpected notification"; | 188 NOTREACHED() << L"Received unexpected notification"; |
| 140 break; | 189 break; |
| 141 } | 190 } |
| 142 } | 191 } |
| 143 | 192 |
| 144 // Here we collect most of the information we need to sort the | 193 // Here we collect most of the information we need to sort the |
| 145 // existing renderers in priority order, and hand out oom_score_adj | 194 // existing renderers in priority order, and hand out oom_score_adj |
| 146 // scores based on that sort order. | 195 // scores based on that sort order. |
| 147 // | 196 // |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 ZygoteHost::GetInstance()->AdjustRendererOOMScore( | 300 ZygoteHost::GetInstance()->AdjustRendererOOMScore( |
| 252 iterator->renderer_handle, score); | 301 iterator->renderer_handle, score); |
| 253 pid_to_oom_score_[iterator->renderer_handle] = score; | 302 pid_to_oom_score_[iterator->renderer_handle] = score; |
| 254 } | 303 } |
| 255 priority += priority_increment; | 304 priority += priority_increment; |
| 256 } | 305 } |
| 257 } | 306 } |
| 258 } | 307 } |
| 259 | 308 |
| 260 } // namespace browser | 309 } // namespace browser |
| OLD | NEW |