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

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

Issue 815183002: Using the new MemoryPressureListener instead of the LowMemoryObserver when the enhanced memory mana… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Created 6 years 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
OLDNEW
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
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() {
James Cook 2015/01/05 18:41:19 I would call this GetMemoryPressureObserver(). I d
Mr4D (OOO till 08-26) 2015/01/06 22:22:06 Done.
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
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 ==
231 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
232 OnMemoryPressure(level);
233 }
234 }
235 }
210 } 236 }
211 237
212 void OomPriorityManager::Stop() { 238 void OomPriorityManager::Stop() {
213 timer_.Stop(); 239 timer_.Stop();
214 recent_tab_discard_timer_.Stop(); 240 recent_tab_discard_timer_.Stop();
215 if (low_memory_observer_.get()) 241 if (low_memory_observer_.get())
216 low_memory_observer_->Stop(); 242 low_memory_observer_->Stop();
243 else
244 memory_pressure_listener_.reset();
217 } 245 }
218 246
219 std::vector<base::string16> OomPriorityManager::GetTabTitles() { 247 std::vector<base::string16> OomPriorityManager::GetTabTitles() {
220 TabStatsList stats = GetTabStatsOnUIThread(); 248 TabStatsList stats = GetTabStatsOnUIThread();
221 base::AutoLock oom_score_autolock(oom_score_lock_); 249 base::AutoLock oom_score_autolock(oom_score_lock_);
222 std::vector<base::string16> titles; 250 std::vector<base::string16> titles;
223 titles.reserve(stats.size()); 251 titles.reserve(stats.size());
224 TabStatsList::iterator it = stats.begin(); 252 TabStatsList::iterator it = stats.begin();
225 for ( ; it != stats.end(); ++it) { 253 for ( ; it != stats.end(); ++it) {
226 base::string16 str; 254 base::string16 str;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 recent_tab_discard_); 422 recent_tab_discard_);
395 // Reset for the next interval. 423 // Reset for the next interval.
396 recent_tab_discard_ = false; 424 recent_tab_discard_ = false;
397 } 425 }
398 426
399 void OomPriorityManager::PurgeBrowserMemory() { 427 void OomPriorityManager::PurgeBrowserMemory() {
400 // Based on experimental evidence, attempts to free memory from renderers 428 // Based on experimental evidence, attempts to free memory from renderers
401 // have been too slow to use in OOM situations (V8 garbage collection) or 429 // 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 430 // do not lead to persistent decreased usage (image/bitmap caches). This
403 // function therefore only targets large blocks of memory in the browser. 431 // function therefore only targets large blocks of memory in the browser.
432 // Note that other objects will listen to MemoryPressureListener events
433 // to release memory.
404 for (TabContentsIterator it; !it.done(); it.Next()) { 434 for (TabContentsIterator it; !it.done(); it.Next()) {
405 WebContents* web_contents = *it; 435 WebContents* web_contents = *it;
406 // Screenshots can consume ~5 MB per web contents for platforms that do 436 // Screenshots can consume ~5 MB per web contents for platforms that do
407 // touch back/forward. 437 // touch back/forward.
408 web_contents->GetController().ClearAllScreenshots(); 438 web_contents->GetController().ClearAllScreenshots();
409 } 439 }
410 // TODO(jamescook): Are there other things we could flush? Drive metadata?
411 } 440 }
412 441
413 int OomPriorityManager::GetTabCount() const { 442 int OomPriorityManager::GetTabCount() const {
414 int tab_count = 0; 443 int tab_count = 0;
415 for (chrome::BrowserIterator it; !it.done(); it.Next()) 444 for (chrome::BrowserIterator it; !it.done(); it.Next())
416 tab_count += it->tab_strip_model()->count(); 445 tab_count += it->tab_strip_model()->count();
417 return tab_count; 446 return tab_count;
418 } 447 }
419 448
420 // Returns true if |first| is considered less desirable to be killed 449 // Returns true if |first| is considered less desirable to be killed
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 void OomPriorityManager::Observe(int type, 503 void OomPriorityManager::Observe(int type,
475 const content::NotificationSource& source, 504 const content::NotificationSource& source,
476 const content::NotificationDetails& details) { 505 const content::NotificationDetails& details) {
477 base::AutoLock oom_score_autolock(oom_score_lock_); 506 base::AutoLock oom_score_autolock(oom_score_lock_);
478 switch (type) { 507 switch (type) {
479 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: 508 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED:
480 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { 509 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
481 content::RenderProcessHost* host = 510 content::RenderProcessHost* host =
482 content::Source<content::RenderProcessHost>(source).ptr(); 511 content::Source<content::RenderProcessHost>(source).ptr();
483 oom_score_map_.erase(host->GetID()); 512 oom_score_map_.erase(host->GetID());
513 if (!low_memory_observer_.get()) {
514 // 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
516 // not yet update its status and therefore we ask it to redo the
517 // measurement, calling us again if we have to release more.
518 // Note: We do not only accelerate the discarding speed by doing another
519 // check in short succession - we also accelerate it because the timer
520 // driven MemoryPressureObserver will continue to produce timed events
521 // on top. So as longer as the cleanup phase takes, as more tabs will
522 // get discarded in parallel.
523 base::MemoryPressureObserverChromeOS* observer =
524 memory_pressure_observer();
525 if (observer)
526 observer->ScheduleEarlyCheck();
527 }
484 break; 528 break;
485 } 529 }
486 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: { 530 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: {
487 bool visible = *content::Details<bool>(details).ptr(); 531 bool visible = *content::Details<bool>(details).ptr();
488 if (visible) { 532 if (visible) {
489 content::RenderProcessHost* render_host = 533 content::RenderProcessHost* render_host =
490 content::Source<content::RenderWidgetHost>(source).ptr()-> 534 content::Source<content::RenderWidgetHost>(source).ptr()->
491 GetProcess(); 535 GetProcess();
492 focused_tab_process_info_ = std::make_pair(render_host->GetID(), 536 focused_tab_process_info_ = std::make_pair(render_host->GetID(),
493 render_host->GetHandle()); 537 render_host->GetHandle());
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 // do not set it. 695 // do not set it.
652 if (it == oom_score_map_.end() || it->second != score) { 696 if (it == oom_score_map_.end() || it->second != score) {
653 content::ZygoteHost::GetInstance()->AdjustRendererOOMScore( 697 content::ZygoteHost::GetInstance()->AdjustRendererOOMScore(
654 process_info.second, score); 698 process_info.second, score);
655 oom_score_map_[process_info.first] = score; 699 oom_score_map_[process_info.first] = score;
656 } 700 }
657 priority += priority_increment; 701 priority += priority_increment;
658 } 702 }
659 } 703 }
660 704
705 void OomPriorityManager::OnMemoryPressure(
706 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
707 // For the moment we only do something when we reach a critical state.
708 if (memory_pressure_level ==
709 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
710 LogMemoryAndDiscardTab();
711 }
712 // TODO(skuhne): If more memory pressure levels are introduced, we might
713 // consider to call PurgeBrowserMemory() before CRITICAL is reached.
714 }
715
661 } // namespace chromeos 716 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698