| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chromeos/memory/oom_priority_manager.h" | 5 #include "chrome/browser/chromeos/memory/oom_priority_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "ash/multi_profile_uma.h" | 11 #include "ash/multi_profile_uma.h" |
| 12 #include "ash/session/session_state_delegate.h" | 12 #include "ash/session/session_state_delegate.h" |
| 13 #include "ash/shell.h" | 13 #include "ash/shell.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" |
| 16 #include "base/chromeos/memory_pressure_observer_chromeos.h" | |
| 17 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 17 #include "base/memory/memory_pressure_monitor.h" |
| 18 #include "base/metrics/field_trial.h" | 18 #include "base/metrics/field_trial.h" |
| 19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 20 #include "base/process/process.h" | 20 #include "base/process/process.h" |
| 21 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
| 22 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 24 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 25 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
| 26 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
| 27 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 28 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 29 #include "chrome/browser/browser_process.h" | 29 #include "chrome/browser/browser_process.h" |
| 30 #include "chrome/browser/browser_process_platform_part_chromeos.h" | 30 #include "chrome/browser/browser_process_platform_part_chromeos.h" |
| 31 #include "chrome/browser/chromeos/memory/low_memory_observer.h" | 31 #include "chrome/browser/chromeos/memory/low_memory_observer.h" |
| 32 #include "chrome/browser/memory_details.h" | 32 #include "chrome/browser/memory_details.h" |
| 33 #include "chrome/browser/ui/browser.h" | 33 #include "chrome/browser/ui/browser.h" |
| 34 #include "chrome/browser/ui/browser_iterator.h" | 34 #include "chrome/browser/ui/browser_iterator.h" |
| 35 #include "chrome/browser/ui/browser_list.h" | 35 #include "chrome/browser/ui/browser_list.h" |
| 36 #include "chrome/browser/ui/host_desktop.h" | 36 #include "chrome/browser/ui/host_desktop.h" |
| 37 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 37 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 38 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 38 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 39 #include "chrome/browser/ui/tabs/tab_utils.h" | 39 #include "chrome/browser/ui/tabs/tab_utils.h" |
| 40 #include "chrome/common/chrome_constants.h" | 40 #include "chrome/common/chrome_constants.h" |
| 41 #include "chrome/common/url_constants.h" | 41 #include "chrome/common/url_constants.h" |
| 42 #include "chromeos/chromeos_switches.h" | 42 #include "chromeos/chromeos_switches.h" |
| 43 #include "content/public/browser/browser_thread.h" | 43 #include "content/public/browser/browser_thread.h" |
| 44 #include "content/public/browser/memory_pressure_observer.h" | |
| 45 #include "content/public/browser/notification_service.h" | 44 #include "content/public/browser/notification_service.h" |
| 46 #include "content/public/browser/notification_types.h" | 45 #include "content/public/browser/notification_types.h" |
| 47 #include "content/public/browser/render_process_host.h" | 46 #include "content/public/browser/render_process_host.h" |
| 48 #include "content/public/browser/render_widget_host.h" | 47 #include "content/public/browser/render_widget_host.h" |
| 49 #include "content/public/browser/web_contents.h" | 48 #include "content/public/browser/web_contents.h" |
| 50 #include "content/public/browser/zygote_host_linux.h" | 49 #include "content/public/browser/zygote_host_linux.h" |
| 51 #include "ui/base/text/bytes_formatting.h" | 50 #include "ui/base/text/bytes_formatting.h" |
| 52 | 51 |
| 53 using base::TimeDelta; | 52 using base::TimeDelta; |
| 54 using base::TimeTicks; | 53 using base::TimeTicks; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 // instance and thus only work if |name| is constant. | 97 // instance and thus only work if |name| is constant. |
| 99 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( | 98 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( |
| 100 name, | 99 name, |
| 101 1, // Minimum. The 0 bin for underflow is automatically added. | 100 1, // Minimum. The 0 bin for underflow is automatically added. |
| 102 maximum + 1, // Ensure bucket size of |maximum| / |bucket_count|. | 101 maximum + 1, // Ensure bucket size of |maximum| / |bucket_count|. |
| 103 bucket_count + 2, // Account for the underflow and overflow bins. | 102 bucket_count + 2, // Account for the underflow and overflow bins. |
| 104 base::Histogram::kUmaTargetedHistogramFlag); | 103 base::Histogram::kUmaTargetedHistogramFlag); |
| 105 counter->Add(sample); | 104 counter->Add(sample); |
| 106 } | 105 } |
| 107 | 106 |
| 108 // Gets the MemoryPressureObserver - if it exists. | |
| 109 base::MemoryPressureObserverChromeOS* GetMemoryPressureObserver() { | |
| 110 return content::GetMemoryPressureObserver(); | |
| 111 } | |
| 112 | |
| 113 } // namespace | 107 } // namespace |
| 114 | 108 |
| 115 //////////////////////////////////////////////////////////////////////////////// | 109 //////////////////////////////////////////////////////////////////////////////// |
| 116 // OomMemoryDetails logs details about all Chrome processes during an out-of- | 110 // OomMemoryDetails logs details about all Chrome processes during an out-of- |
| 117 // memory event in an attempt to identify the culprit, then discards a tab and | 111 // memory event in an attempt to identify the culprit, then discards a tab and |
| 118 // deletes itself. | 112 // deletes itself. |
| 119 class OomMemoryDetails : public MemoryDetails { | 113 class OomMemoryDetails : public MemoryDetails { |
| 120 public: | 114 public: |
| 121 OomMemoryDetails(); | 115 OomMemoryDetails(); |
| 122 | 116 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 tab_contents_id(0) { | 168 tab_contents_id(0) { |
| 175 } | 169 } |
| 176 | 170 |
| 177 OomPriorityManager::TabStats::~TabStats() { | 171 OomPriorityManager::TabStats::~TabStats() { |
| 178 } | 172 } |
| 179 | 173 |
| 180 OomPriorityManager::OomPriorityManager() | 174 OomPriorityManager::OomPriorityManager() |
| 181 : focused_tab_process_info_(std::make_pair(0, 0)), | 175 : focused_tab_process_info_(std::make_pair(0, 0)), |
| 182 discard_count_(0), | 176 discard_count_(0), |
| 183 recent_tab_discard_(false) { | 177 recent_tab_discard_(false) { |
| 184 // Use the old |LowMemoryObserver| when there is no | 178 // Use the old |LowMemoryObserver| when there is no |MemoryPressureMonitor|. |
| 185 // |MemoryPressureObserverChromeOS|. | 179 if (!base::MemoryPressureMonitor::Get()) |
| 186 if (!GetMemoryPressureObserver()) | |
| 187 low_memory_observer_.reset(new LowMemoryObserver); | 180 low_memory_observer_.reset(new LowMemoryObserver); |
| 188 | 181 |
| 189 registrar_.Add(this, | 182 registrar_.Add(this, |
| 190 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 183 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 191 content::NotificationService::AllBrowserContextsAndSources()); | 184 content::NotificationService::AllBrowserContextsAndSources()); |
| 192 registrar_.Add(this, | 185 registrar_.Add(this, |
| 193 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 186 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 194 content::NotificationService::AllBrowserContextsAndSources()); | 187 content::NotificationService::AllBrowserContextsAndSources()); |
| 195 registrar_.Add(this, | 188 registrar_.Add(this, |
| 196 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 189 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 211 if (!recent_tab_discard_timer_.IsRunning()) { | 204 if (!recent_tab_discard_timer_.IsRunning()) { |
| 212 recent_tab_discard_timer_.Start( | 205 recent_tab_discard_timer_.Start( |
| 213 FROM_HERE, | 206 FROM_HERE, |
| 214 TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds), | 207 TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds), |
| 215 this, | 208 this, |
| 216 &OomPriorityManager::RecordRecentTabDiscard); | 209 &OomPriorityManager::RecordRecentTabDiscard); |
| 217 } | 210 } |
| 218 start_time_ = TimeTicks::Now(); | 211 start_time_ = TimeTicks::Now(); |
| 219 // If a |LowMemoryObserver| exists we use the old system, otherwise we create | 212 // If a |LowMemoryObserver| exists we use the old system, otherwise we create |
| 220 // a |MemoryPressureListener| to listen for memory events. | 213 // a |MemoryPressureListener| to listen for memory events. |
| 221 if (low_memory_observer_.get()) { | 214 if (low_memory_observer_) { |
| 222 low_memory_observer_->Start(); | 215 low_memory_observer_->Start(); |
| 223 } else { | 216 } else { |
| 224 base::MemoryPressureObserverChromeOS* observer = | 217 base::MemoryPressureMonitor* monitor = base::MemoryPressureMonitor::Get(); |
| 225 GetMemoryPressureObserver(); | 218 if (monitor) { |
| 226 if (observer) { | |
| 227 memory_pressure_listener_.reset(new base::MemoryPressureListener( | 219 memory_pressure_listener_.reset(new base::MemoryPressureListener( |
| 228 base::Bind(&OomPriorityManager::OnMemoryPressure, | 220 base::Bind(&OomPriorityManager::OnMemoryPressure, |
| 229 base::Unretained(this)))); | 221 base::Unretained(this)))); |
| 230 base::MemoryPressureListener::MemoryPressureLevel level = | 222 base::MemoryPressureListener::MemoryPressureLevel level = |
| 231 observer->GetCurrentPressureLevel(); | 223 monitor->GetCurrentPressureLevel(); |
| 232 if (level == | 224 if (level == |
| 233 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { | 225 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| 234 OnMemoryPressure(level); | 226 OnMemoryPressure(level); |
| 235 } | 227 } |
| 236 } | 228 } |
| 237 } | 229 } |
| 238 } | 230 } |
| 239 | 231 |
| 240 void OomPriorityManager::Stop() { | 232 void OomPriorityManager::Stop() { |
| 241 timer_.Stop(); | 233 timer_.Stop(); |
| 242 recent_tab_discard_timer_.Stop(); | 234 recent_tab_discard_timer_.Stop(); |
| 243 if (low_memory_observer_.get()) | 235 if (low_memory_observer_) |
| 244 low_memory_observer_->Stop(); | 236 low_memory_observer_->Stop(); |
| 245 else | 237 else |
| 246 memory_pressure_listener_.reset(); | 238 memory_pressure_listener_.reset(); |
| 247 } | 239 } |
| 248 | 240 |
| 249 std::vector<base::string16> OomPriorityManager::GetTabTitles() { | 241 std::vector<base::string16> OomPriorityManager::GetTabTitles() { |
| 250 TabStatsList stats = GetTabStatsOnUIThread(); | 242 TabStatsList stats = GetTabStatsOnUIThread(); |
| 251 base::AutoLock oom_score_autolock(oom_score_lock_); | 243 base::AutoLock oom_score_autolock(oom_score_lock_); |
| 252 std::vector<base::string16> titles; | 244 std::vector<base::string16> titles; |
| 253 titles.reserve(stats.size()); | 245 titles.reserve(stats.size()); |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 void OomPriorityManager::Observe(int type, | 495 void OomPriorityManager::Observe(int type, |
| 504 const content::NotificationSource& source, | 496 const content::NotificationSource& source, |
| 505 const content::NotificationDetails& details) { | 497 const content::NotificationDetails& details) { |
| 506 base::AutoLock oom_score_autolock(oom_score_lock_); | 498 base::AutoLock oom_score_autolock(oom_score_lock_); |
| 507 switch (type) { | 499 switch (type) { |
| 508 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: | 500 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: |
| 509 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 501 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
| 510 content::RenderProcessHost* host = | 502 content::RenderProcessHost* host = |
| 511 content::Source<content::RenderProcessHost>(source).ptr(); | 503 content::Source<content::RenderProcessHost>(source).ptr(); |
| 512 oom_score_map_.erase(host->GetID()); | 504 oom_score_map_.erase(host->GetID()); |
| 513 if (!low_memory_observer_.get()) { | 505 if (!low_memory_observer_) { |
| 514 // Coming here we know that a renderer was just killed and memory should | 506 // Coming here we know that a renderer was just killed and memory should |
| 515 // come back into the pool. However - the memory pressure observer did | 507 // come back into the pool. However - the memory pressure observer did |
| 516 // not yet update its status and therefore we ask it to redo the | 508 // not yet update its status and therefore we ask it to redo the |
| 517 // measurement, calling us again if we have to release more. | 509 // measurement, calling us again if we have to release more. |
| 518 // Note: We do not only accelerate the discarding speed by doing another | 510 // Note: We do not only accelerate the discarding speed by doing another |
| 519 // check in short succession - we also accelerate it because the timer | 511 // check in short succession - we also accelerate it because the timer |
| 520 // driven MemoryPressureObserver will continue to produce timed events | 512 // driven MemoryPressureMonitor will continue to produce timed events |
| 521 // on top. So as longer as the cleanup phase takes, as more tabs will | 513 // on top. So the longer the cleanup phase takes, the more tabs will |
| 522 // get discarded in parallel. | 514 // get discarded in parallel. |
| 523 base::MemoryPressureObserverChromeOS* observer = | 515 base::MemoryPressureMonitorChromeOS* monitor = |
| 524 GetMemoryPressureObserver(); | 516 static_cast<base::MemoryPressureMonitorChromeOS*>( |
| 525 if (observer) | 517 base::MemoryPressureMonitor::Get()); |
| 526 observer->ScheduleEarlyCheck(); | 518 if (monitor) |
| 519 monitor->ScheduleEarlyCheck(); |
| 527 } | 520 } |
| 528 break; | 521 break; |
| 529 } | 522 } |
| 530 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: { | 523 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: { |
| 531 bool visible = *content::Details<bool>(details).ptr(); | 524 bool visible = *content::Details<bool>(details).ptr(); |
| 532 if (visible) { | 525 if (visible) { |
| 533 content::RenderProcessHost* render_host = | 526 content::RenderProcessHost* render_host = |
| 534 content::Source<content::RenderWidgetHost>(source).ptr()-> | 527 content::Source<content::RenderWidgetHost>(source).ptr()-> |
| 535 GetProcess(); | 528 GetProcess(); |
| 536 focused_tab_process_info_ = std::make_pair(render_host->GetID(), | 529 focused_tab_process_info_ = std::make_pair(render_host->GetID(), |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 // For the moment we only do something when we reach a critical state. | 700 // For the moment we only do something when we reach a critical state. |
| 708 if (memory_pressure_level == | 701 if (memory_pressure_level == |
| 709 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { | 702 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| 710 LogMemoryAndDiscardTab(); | 703 LogMemoryAndDiscardTab(); |
| 711 } | 704 } |
| 712 // TODO(skuhne): If more memory pressure levels are introduced, we might | 705 // TODO(skuhne): If more memory pressure levels are introduced, we might |
| 713 // consider to call PurgeBrowserMemory() before CRITICAL is reached. | 706 // consider to call PurgeBrowserMemory() before CRITICAL is reached. |
| 714 } | 707 } |
| 715 | 708 |
| 716 } // namespace chromeos | 709 } // namespace chromeos |
| OLD | NEW |