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

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: 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 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"
26 #include "base/time/time.h" 27 #include "base/time/time.h"
27 #include "build/build_config.h" 28 #include "build/build_config.h"
28 #include "chrome/browser/browser_process.h" 29 #include "chrome/browser/browser_process.h"
29 #include "chrome/browser/browser_process_platform_part_chromeos.h" 30 #include "chrome/browser/browser_process_platform_part_chromeos.h"
30 #include "chrome/browser/chromeos/memory/low_memory_observer.h" 31 #include "chrome/browser/chromeos/memory/low_memory_observer.h"
31 #include "chrome/browser/memory_details.h" 32 #include "chrome/browser/memory_details.h"
32 #include "chrome/browser/ui/browser.h" 33 #include "chrome/browser/ui/browser.h"
33 #include "chrome/browser/ui/browser_iterator.h" 34 #include "chrome/browser/ui/browser_iterator.h"
34 #include "chrome/browser/ui/browser_list.h" 35 #include "chrome/browser/ui/browser_list.h"
35 #include "chrome/browser/ui/host_desktop.h" 36 #include "chrome/browser/ui/host_desktop.h"
36 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" 37 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
37 #include "chrome/browser/ui/tabs/tab_strip_model.h" 38 #include "chrome/browser/ui/tabs/tab_strip_model.h"
38 #include "chrome/browser/ui/tabs/tab_utils.h" 39 #include "chrome/browser/ui/tabs/tab_utils.h"
39 #include "chrome/common/chrome_constants.h" 40 #include "chrome/common/chrome_constants.h"
40 #include "chrome/common/url_constants.h" 41 #include "chrome/common/url_constants.h"
41 #include "chromeos/chromeos_switches.h" 42 #include "chromeos/chromeos_switches.h"
43 #include "content/browser/browser_main_loop.h"
42 #include "content/public/browser/browser_thread.h" 44 #include "content/public/browser/browser_thread.h"
43 #include "content/public/browser/notification_service.h" 45 #include "content/public/browser/notification_service.h"
44 #include "content/public/browser/notification_types.h" 46 #include "content/public/browser/notification_types.h"
45 #include "content/public/browser/render_process_host.h" 47 #include "content/public/browser/render_process_host.h"
46 #include "content/public/browser/render_widget_host.h" 48 #include "content/public/browser/render_widget_host.h"
47 #include "content/public/browser/web_contents.h" 49 #include "content/public/browser/web_contents.h"
48 #include "content/public/browser/zygote_host_linux.h" 50 #include "content/public/browser/zygote_host_linux.h"
49 #include "ui/base/text/bytes_formatting.h" 51 #include "ui/base/text/bytes_formatting.h"
50 52
51 using base::TimeDelta; 53 using base::TimeDelta;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 // instance and thus only work if |name| is constant. 98 // instance and thus only work if |name| is constant.
97 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( 99 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
98 name, 100 name,
99 1, // Minimum. The 0 bin for underflow is automatically added. 101 1, // Minimum. The 0 bin for underflow is automatically added.
100 maximum + 1, // Ensure bucket size of |maximum| / |bucket_count|. 102 maximum + 1, // Ensure bucket size of |maximum| / |bucket_count|.
101 bucket_count + 2, // Account for the underflow and overflow bins. 103 bucket_count + 2, // Account for the underflow and overflow bins.
102 base::Histogram::kUmaTargetedHistogramFlag); 104 base::Histogram::kUmaTargetedHistogramFlag);
103 counter->Add(sample); 105 counter->Add(sample);
104 } 106 }
105 107
108 // Gets the MemoryPressureObserver - if it exists.
109 base::MemoryPressureObserverChromeOS* memory_pressure_observer() {
James Cook 2014/12/19 19:38:08 GetMemoryPressureObserver or similar
110 // TODO(skuhne): Before submitting this needs to move.
James Cook 2014/12/19 19:38:08 Acknowledged.
111 return content::BrowserMainLoop::GetInstance()->memory_pressure_observer();
112 }
113
106 } // namespace 114 } // namespace
107 115
108 //////////////////////////////////////////////////////////////////////////////// 116 ////////////////////////////////////////////////////////////////////////////////
109 // OomMemoryDetails logs details about all Chrome processes during an out-of- 117 // 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 118 // memory event in an attempt to identify the culprit, then discards a tab and
111 // deletes itself. 119 // deletes itself.
112 class OomMemoryDetails : public MemoryDetails { 120 class OomMemoryDetails : public MemoryDetails {
113 public: 121 public:
114 OomMemoryDetails(); 122 OomMemoryDetails();
115 123
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 is_discarded(false), 173 is_discarded(false),
166 renderer_handle(0), 174 renderer_handle(0),
167 tab_contents_id(0) { 175 tab_contents_id(0) {
168 } 176 }
169 177
170 OomPriorityManager::TabStats::~TabStats() { 178 OomPriorityManager::TabStats::~TabStats() {
171 } 179 }
172 180
173 OomPriorityManager::OomPriorityManager() 181 OomPriorityManager::OomPriorityManager()
174 : focused_tab_process_info_(std::make_pair(0, 0)), 182 : focused_tab_process_info_(std::make_pair(0, 0)),
175 low_memory_observer_(new LowMemoryObserver),
176 discard_count_(0), 183 discard_count_(0),
177 recent_tab_discard_(false) { 184 recent_tab_discard_(false) {
185 // Use the old |LowMemoryObserver| when there is no
186 // |MemoryPressureObserverChromeOS|.
187 if (!memory_pressure_observer())
188 low_memory_observer_.reset(new LowMemoryObserver);
189
178 registrar_.Add(this, 190 registrar_.Add(this,
179 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 191 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
180 content::NotificationService::AllBrowserContextsAndSources()); 192 content::NotificationService::AllBrowserContextsAndSources());
181 registrar_.Add(this, 193 registrar_.Add(this,
182 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 194 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
183 content::NotificationService::AllBrowserContextsAndSources()); 195 content::NotificationService::AllBrowserContextsAndSources());
184 registrar_.Add(this, 196 registrar_.Add(this,
185 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, 197 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
186 content::NotificationService::AllBrowserContextsAndSources()); 198 content::NotificationService::AllBrowserContextsAndSources());
187 } 199 }
188 200
189 OomPriorityManager::~OomPriorityManager() { 201 OomPriorityManager::~OomPriorityManager() {
190 Stop(); 202 Stop();
191 } 203 }
192 204
193 void OomPriorityManager::Start() { 205 void OomPriorityManager::Start() {
194 if (!timer_.IsRunning()) { 206 if (!timer_.IsRunning()) {
195 timer_.Start(FROM_HERE, 207 timer_.Start(FROM_HERE,
196 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds), 208 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds),
197 this, 209 this,
198 &OomPriorityManager::AdjustOomPriorities); 210 &OomPriorityManager::AdjustOomPriorities);
199 } 211 }
200 if (!recent_tab_discard_timer_.IsRunning()) { 212 if (!recent_tab_discard_timer_.IsRunning()) {
201 recent_tab_discard_timer_.Start( 213 recent_tab_discard_timer_.Start(
202 FROM_HERE, 214 FROM_HERE,
203 TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds), 215 TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds),
204 this, 216 this,
205 &OomPriorityManager::RecordRecentTabDiscard); 217 &OomPriorityManager::RecordRecentTabDiscard);
206 } 218 }
219 start_time_ = TimeTicks::Now();
220 // If a |LowMemoryObserver| exists we use the old system, otherwise we create
221 // a |MemoryPressureListener| to listen for memory events.
207 if (low_memory_observer_.get()) 222 if (low_memory_observer_.get())
James Cook 2014/12/19 19:38:08 I might write a little function like "bool UseOldM
208 low_memory_observer_->Start(); 223 low_memory_observer_->Start();
James Cook 2014/12/19 19:38:08 just return after this line
209 start_time_ = TimeTicks::Now(); 224 else {
225 base::MemoryPressureObserverChromeOS* observer = memory_pressure_observer();
226 if (observer) {
227 memory_pressure_listener_.reset(new base::MemoryPressureListener(
James Cook 2014/12/19 19:38:07 optional: "using base::MemoryPressureListener;" mi
228 base::Bind(&OomPriorityManager::OnMemoryPressure,
229 base::Unretained(this))));
230 base::MemoryPressureListener::MemoryPressureLevel level =
231 observer->GetCurrentPressureLevel();
232 if (level == base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL)
James Cook 2014/12/19 19:38:08 Do you really need this? I think the manager is s
233 OnMemoryPressure(level);
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?
James Cook 2014/12/19 19:38:07 Hooray for working on this!
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)
James Cook 2014/12/19 19:38:08 Do you need this if()?
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