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

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

Powered by Google App Engine
This is Rietveld 408576698