| 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" |
| 16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 17 #include "base/metrics/field_trial.h" | 18 #include "base/metrics/field_trial.h" |
| 18 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 19 #include "base/process/process.h" | 20 #include "base/process/process.h" |
| 20 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
| 21 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
| 25 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // instance and thus only work if |name| is constant. | 97 // instance and thus only work if |name| is constant. |
| 97 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( | 98 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( |
| 98 name, | 99 name, |
| 99 1, // Minimum. The 0 bin for underflow is automatically added. | 100 1, // Minimum. The 0 bin for underflow is automatically added. |
| 100 maximum + 1, // Ensure bucket size of |maximum| / |bucket_count|. | 101 maximum + 1, // Ensure bucket size of |maximum| / |bucket_count|. |
| 101 bucket_count + 2, // Account for the underflow and overflow bins. | 102 bucket_count + 2, // Account for the underflow and overflow bins. |
| 102 base::Histogram::kUmaTargetedHistogramFlag); | 103 base::Histogram::kUmaTargetedHistogramFlag); |
| 103 counter->Add(sample); | 104 counter->Add(sample); |
| 104 } | 105 } |
| 105 | 106 |
| 107 // Gets the MemoryPressureObserver - if it exists. |
| 108 base::MemoryPressureObserverChromeOS* memory_pressure_observer() { |
| 109 return content::BrowserThread::GetMemoryPressureObserver(); |
| 110 } |
| 111 |
| 106 } // namespace | 112 } // namespace |
| 107 | 113 |
| 108 //////////////////////////////////////////////////////////////////////////////// | 114 //////////////////////////////////////////////////////////////////////////////// |
| 109 // OomMemoryDetails logs details about all Chrome processes during an out-of- | 115 // OomMemoryDetails logs details about all Chrome processes during an out-of- |
| 110 // memory event in an attempt to identify the culprit, then discards a tab and | 116 // memory event in an attempt to identify the culprit, then discards a tab and |
| 111 // deletes itself. | 117 // deletes itself. |
| 112 class OomMemoryDetails : public MemoryDetails { | 118 class OomMemoryDetails : public MemoryDetails { |
| 113 public: | 119 public: |
| 114 OomMemoryDetails(); | 120 OomMemoryDetails(); |
| 115 | 121 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 is_discarded(false), | 171 is_discarded(false), |
| 166 renderer_handle(0), | 172 renderer_handle(0), |
| 167 tab_contents_id(0) { | 173 tab_contents_id(0) { |
| 168 } | 174 } |
| 169 | 175 |
| 170 OomPriorityManager::TabStats::~TabStats() { | 176 OomPriorityManager::TabStats::~TabStats() { |
| 171 } | 177 } |
| 172 | 178 |
| 173 OomPriorityManager::OomPriorityManager() | 179 OomPriorityManager::OomPriorityManager() |
| 174 : focused_tab_process_info_(std::make_pair(0, 0)), | 180 : focused_tab_process_info_(std::make_pair(0, 0)), |
| 175 low_memory_observer_(new LowMemoryObserver), | |
| 176 discard_count_(0), | 181 discard_count_(0), |
| 177 recent_tab_discard_(false) { | 182 recent_tab_discard_(false) { |
| 183 // Use the old |LowMemoryObserver| when there is no |
| 184 // |MemoryPressureObserverChromeOS|. |
| 185 if (!memory_pressure_observer()) |
| 186 low_memory_observer_.reset(new LowMemoryObserver); |
| 187 |
| 178 registrar_.Add(this, | 188 registrar_.Add(this, |
| 179 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 189 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 180 content::NotificationService::AllBrowserContextsAndSources()); | 190 content::NotificationService::AllBrowserContextsAndSources()); |
| 181 registrar_.Add(this, | 191 registrar_.Add(this, |
| 182 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 192 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 183 content::NotificationService::AllBrowserContextsAndSources()); | 193 content::NotificationService::AllBrowserContextsAndSources()); |
| 184 registrar_.Add(this, | 194 registrar_.Add(this, |
| 185 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | 195 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, |
| 186 content::NotificationService::AllBrowserContextsAndSources()); | 196 content::NotificationService::AllBrowserContextsAndSources()); |
| 187 } | 197 } |
| 188 | 198 |
| 189 OomPriorityManager::~OomPriorityManager() { | 199 OomPriorityManager::~OomPriorityManager() { |
| 190 Stop(); | 200 Stop(); |
| 191 } | 201 } |
| 192 | 202 |
| 193 void OomPriorityManager::Start() { | 203 void OomPriorityManager::Start() { |
| 194 if (!timer_.IsRunning()) { | 204 if (!timer_.IsRunning()) { |
| 195 timer_.Start(FROM_HERE, | 205 timer_.Start(FROM_HERE, |
| 196 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds), | 206 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds), |
| 197 this, | 207 this, |
| 198 &OomPriorityManager::AdjustOomPriorities); | 208 &OomPriorityManager::AdjustOomPriorities); |
| 199 } | 209 } |
| 200 if (!recent_tab_discard_timer_.IsRunning()) { | 210 if (!recent_tab_discard_timer_.IsRunning()) { |
| 201 recent_tab_discard_timer_.Start( | 211 recent_tab_discard_timer_.Start( |
| 202 FROM_HERE, | 212 FROM_HERE, |
| 203 TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds), | 213 TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds), |
| 204 this, | 214 this, |
| 205 &OomPriorityManager::RecordRecentTabDiscard); | 215 &OomPriorityManager::RecordRecentTabDiscard); |
| 206 } | 216 } |
| 207 if (low_memory_observer_.get()) | 217 start_time_ = TimeTicks::Now(); |
| 218 // If a |LowMemoryObserver| exists we use the old system, otherwise we create |
| 219 // a |MemoryPressureListener| to listen for memory events. |
| 220 if (low_memory_observer_.get()) { |
| 208 low_memory_observer_->Start(); | 221 low_memory_observer_->Start(); |
| 209 start_time_ = TimeTicks::Now(); | 222 } else { |
| 223 base::MemoryPressureObserverChromeOS* observer = memory_pressure_observer(); |
| 224 if (observer) { |
| 225 memory_pressure_listener_.reset(new base::MemoryPressureListener( |
| 226 base::Bind(&OomPriorityManager::OnMemoryPressure, |
| 227 base::Unretained(this)))); |
| 228 base::MemoryPressureListener::MemoryPressureLevel level = |
| 229 observer->GetCurrentPressureLevel(); |
| 230 if (level == base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) |
| 231 OnMemoryPressure(level); |
| 232 } |
| 233 } |
| 210 } | 234 } |
| 211 | 235 |
| 212 void OomPriorityManager::Stop() { | 236 void OomPriorityManager::Stop() { |
| 213 timer_.Stop(); | 237 timer_.Stop(); |
| 214 recent_tab_discard_timer_.Stop(); | 238 recent_tab_discard_timer_.Stop(); |
| 215 if (low_memory_observer_.get()) | 239 if (low_memory_observer_.get()) |
| 216 low_memory_observer_->Stop(); | 240 low_memory_observer_->Stop(); |
| 241 else |
| 242 memory_pressure_listener_.reset(); |
| 217 } | 243 } |
| 218 | 244 |
| 219 std::vector<base::string16> OomPriorityManager::GetTabTitles() { | 245 std::vector<base::string16> OomPriorityManager::GetTabTitles() { |
| 220 TabStatsList stats = GetTabStatsOnUIThread(); | 246 TabStatsList stats = GetTabStatsOnUIThread(); |
| 221 base::AutoLock oom_score_autolock(oom_score_lock_); | 247 base::AutoLock oom_score_autolock(oom_score_lock_); |
| 222 std::vector<base::string16> titles; | 248 std::vector<base::string16> titles; |
| 223 titles.reserve(stats.size()); | 249 titles.reserve(stats.size()); |
| 224 TabStatsList::iterator it = stats.begin(); | 250 TabStatsList::iterator it = stats.begin(); |
| 225 for ( ; it != stats.end(); ++it) { | 251 for ( ; it != stats.end(); ++it) { |
| 226 base::string16 str; | 252 base::string16 str; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 recent_tab_discard_); | 420 recent_tab_discard_); |
| 395 // Reset for the next interval. | 421 // Reset for the next interval. |
| 396 recent_tab_discard_ = false; | 422 recent_tab_discard_ = false; |
| 397 } | 423 } |
| 398 | 424 |
| 399 void OomPriorityManager::PurgeBrowserMemory() { | 425 void OomPriorityManager::PurgeBrowserMemory() { |
| 400 // Based on experimental evidence, attempts to free memory from renderers | 426 // Based on experimental evidence, attempts to free memory from renderers |
| 401 // have been too slow to use in OOM situations (V8 garbage collection) or | 427 // have been too slow to use in OOM situations (V8 garbage collection) or |
| 402 // do not lead to persistent decreased usage (image/bitmap caches). This | 428 // do not lead to persistent decreased usage (image/bitmap caches). This |
| 403 // function therefore only targets large blocks of memory in the browser. | 429 // function therefore only targets large blocks of memory in the browser. |
| 430 // Note that other objects will listen to MemoryPressureListener events |
| 431 // to release memory. |
| 404 for (TabContentsIterator it; !it.done(); it.Next()) { | 432 for (TabContentsIterator it; !it.done(); it.Next()) { |
| 405 WebContents* web_contents = *it; | 433 WebContents* web_contents = *it; |
| 406 // Screenshots can consume ~5 MB per web contents for platforms that do | 434 // Screenshots can consume ~5 MB per web contents for platforms that do |
| 407 // touch back/forward. | 435 // touch back/forward. |
| 408 web_contents->GetController().ClearAllScreenshots(); | 436 web_contents->GetController().ClearAllScreenshots(); |
| 409 } | 437 } |
| 410 // TODO(jamescook): Are there other things we could flush? Drive metadata? | |
| 411 } | 438 } |
| 412 | 439 |
| 413 int OomPriorityManager::GetTabCount() const { | 440 int OomPriorityManager::GetTabCount() const { |
| 414 int tab_count = 0; | 441 int tab_count = 0; |
| 415 for (chrome::BrowserIterator it; !it.done(); it.Next()) | 442 for (chrome::BrowserIterator it; !it.done(); it.Next()) |
| 416 tab_count += it->tab_strip_model()->count(); | 443 tab_count += it->tab_strip_model()->count(); |
| 417 return tab_count; | 444 return tab_count; |
| 418 } | 445 } |
| 419 | 446 |
| 420 // Returns true if |first| is considered less desirable to be killed | 447 // Returns true if |first| is considered less desirable to be killed |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 void OomPriorityManager::Observe(int type, | 501 void OomPriorityManager::Observe(int type, |
| 475 const content::NotificationSource& source, | 502 const content::NotificationSource& source, |
| 476 const content::NotificationDetails& details) { | 503 const content::NotificationDetails& details) { |
| 477 base::AutoLock oom_score_autolock(oom_score_lock_); | 504 base::AutoLock oom_score_autolock(oom_score_lock_); |
| 478 switch (type) { | 505 switch (type) { |
| 479 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: | 506 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: |
| 480 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 507 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
| 481 content::RenderProcessHost* host = | 508 content::RenderProcessHost* host = |
| 482 content::Source<content::RenderProcessHost>(source).ptr(); | 509 content::Source<content::RenderProcessHost>(source).ptr(); |
| 483 oom_score_map_.erase(host->GetID()); | 510 oom_score_map_.erase(host->GetID()); |
| 511 if (!low_memory_observer_.get()) { |
| 512 // Coming here we know that a renderer was just killed and memory should |
| 513 // come back into the pool. However - the memory pressure observer did |
| 514 // not yet update its status and therefore we ask it to redo the |
| 515 // measurement, calling us again if we have to release more. |
| 516 // Note: We do not only accelerate the discarding speed by doing another |
| 517 // check in short succession - we also accelerate it because the timer |
| 518 // driven MemoryPressureObserver will continue to produce timed events |
| 519 // on top. So as longer as the cleanup phase takes, as more tabs will |
| 520 // get discarded in parallel. |
| 521 base::MemoryPressureObserverChromeOS* observer = |
| 522 memory_pressure_observer(); |
| 523 if (observer) |
| 524 observer->ScheduleEarlyCheck(); |
| 525 } |
| 484 break; | 526 break; |
| 485 } | 527 } |
| 486 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: { | 528 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: { |
| 487 bool visible = *content::Details<bool>(details).ptr(); | 529 bool visible = *content::Details<bool>(details).ptr(); |
| 488 if (visible) { | 530 if (visible) { |
| 489 content::RenderProcessHost* render_host = | 531 content::RenderProcessHost* render_host = |
| 490 content::Source<content::RenderWidgetHost>(source).ptr()-> | 532 content::Source<content::RenderWidgetHost>(source).ptr()-> |
| 491 GetProcess(); | 533 GetProcess(); |
| 492 focused_tab_process_info_ = std::make_pair(render_host->GetID(), | 534 focused_tab_process_info_ = std::make_pair(render_host->GetID(), |
| 493 render_host->GetHandle()); | 535 render_host->GetHandle()); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 // do not set it. | 693 // do not set it. |
| 652 if (it == oom_score_map_.end() || it->second != score) { | 694 if (it == oom_score_map_.end() || it->second != score) { |
| 653 content::ZygoteHost::GetInstance()->AdjustRendererOOMScore( | 695 content::ZygoteHost::GetInstance()->AdjustRendererOOMScore( |
| 654 process_info.second, score); | 696 process_info.second, score); |
| 655 oom_score_map_[process_info.first] = score; | 697 oom_score_map_[process_info.first] = score; |
| 656 } | 698 } |
| 657 priority += priority_increment; | 699 priority += priority_increment; |
| 658 } | 700 } |
| 659 } | 701 } |
| 660 | 702 |
| 703 void OomPriorityManager::OnMemoryPressure( |
| 704 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 705 // For the moment we only do something when we reach a critical state. |
| 706 if (memory_pressure_level == |
| 707 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| 708 LogMemoryAndDiscardTab(); |
| 709 } |
| 710 // TODO(skuhne): If more memory pressure levels are introduced, we might |
| 711 // consider to call PurgeBrowserMemory() before CRITICAL is reached. |
| 712 } |
| 713 |
| 661 } // namespace chromeos | 714 } // namespace chromeos |
| OLD | NEW |