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

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

Issue 1045433002: Migrate ChromeOS to base::MemoryPressureMonitor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix chromeos compile Created 5 years, 8 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"
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
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. 107 // Gets the MemoryPressureMonitor - if it exists.
109 base::MemoryPressureObserverChromeOS* GetMemoryPressureObserver() { 108 base::MemoryPressureMonitorChromeOS* GetMemoryPressureMonitor() {
Mr4D (OOO till 08-26) 2015/04/13 15:10:51 Why not using MemoryPressureMonitor as a return va
dmichael (off chromium) 2015/04/13 21:02:15 Done.
110 return content::GetMemoryPressureObserver(); 109 return static_cast<base::MemoryPressureMonitorChromeOS*>(
110 base::MemoryPressureMonitor::Get());
111 } 111 }
112 112
113 } // namespace 113 } // namespace
114 114
115 //////////////////////////////////////////////////////////////////////////////// 115 ////////////////////////////////////////////////////////////////////////////////
116 // OomMemoryDetails logs details about all Chrome processes during an out-of- 116 // 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 117 // memory event in an attempt to identify the culprit, then discards a tab and
118 // deletes itself. 118 // deletes itself.
119 class OomMemoryDetails : public MemoryDetails { 119 class OomMemoryDetails : public MemoryDetails {
120 public: 120 public:
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 tab_contents_id(0) { 174 tab_contents_id(0) {
175 } 175 }
176 176
177 OomPriorityManager::TabStats::~TabStats() { 177 OomPriorityManager::TabStats::~TabStats() {
178 } 178 }
179 179
180 OomPriorityManager::OomPriorityManager() 180 OomPriorityManager::OomPriorityManager()
181 : focused_tab_process_info_(std::make_pair(0, 0)), 181 : focused_tab_process_info_(std::make_pair(0, 0)),
182 discard_count_(0), 182 discard_count_(0),
183 recent_tab_discard_(false) { 183 recent_tab_discard_(false) {
184 // Use the old |LowMemoryObserver| when there is no 184 // Use the old |LowMemoryObserver| when there is no |MemoryPressureMonitor|.
185 // |MemoryPressureObserverChromeOS|. 185 if (!GetMemoryPressureMonitor())
186 if (!GetMemoryPressureObserver())
187 low_memory_observer_.reset(new LowMemoryObserver); 186 low_memory_observer_.reset(new LowMemoryObserver);
188 187
189 registrar_.Add(this, 188 registrar_.Add(this,
190 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 189 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
191 content::NotificationService::AllBrowserContextsAndSources()); 190 content::NotificationService::AllBrowserContextsAndSources());
192 registrar_.Add(this, 191 registrar_.Add(this,
193 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 192 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
194 content::NotificationService::AllBrowserContextsAndSources()); 193 content::NotificationService::AllBrowserContextsAndSources());
195 registrar_.Add(this, 194 registrar_.Add(this,
196 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, 195 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
(...skipping 14 matching lines...) Expand all
211 if (!recent_tab_discard_timer_.IsRunning()) { 210 if (!recent_tab_discard_timer_.IsRunning()) {
212 recent_tab_discard_timer_.Start( 211 recent_tab_discard_timer_.Start(
213 FROM_HERE, 212 FROM_HERE,
214 TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds), 213 TimeDelta::FromSeconds(kRecentTabDiscardIntervalSeconds),
215 this, 214 this,
216 &OomPriorityManager::RecordRecentTabDiscard); 215 &OomPriorityManager::RecordRecentTabDiscard);
217 } 216 }
218 start_time_ = TimeTicks::Now(); 217 start_time_ = TimeTicks::Now();
219 // If a |LowMemoryObserver| exists we use the old system, otherwise we create 218 // If a |LowMemoryObserver| exists we use the old system, otherwise we create
220 // a |MemoryPressureListener| to listen for memory events. 219 // a |MemoryPressureListener| to listen for memory events.
221 if (low_memory_observer_.get()) { 220 if (low_memory_observer_) {
222 low_memory_observer_->Start(); 221 low_memory_observer_->Start();
223 } else { 222 } else {
224 base::MemoryPressureObserverChromeOS* observer = 223 base::MemoryPressureMonitorChromeOS* monitor = GetMemoryPressureMonitor();
225 GetMemoryPressureObserver(); 224 if (monitor) {
226 if (observer) {
227 memory_pressure_listener_.reset(new base::MemoryPressureListener( 225 memory_pressure_listener_.reset(new base::MemoryPressureListener(
228 base::Bind(&OomPriorityManager::OnMemoryPressure, 226 base::Bind(&OomPriorityManager::OnMemoryPressure,
229 base::Unretained(this)))); 227 base::Unretained(this))));
230 base::MemoryPressureListener::MemoryPressureLevel level = 228 base::MemoryPressureListener::MemoryPressureLevel level =
231 observer->GetCurrentPressureLevel(); 229 monitor->GetCurrentPressureLevel();
232 if (level == 230 if (level ==
233 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { 231 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
234 OnMemoryPressure(level); 232 OnMemoryPressure(level);
235 } 233 }
236 } 234 }
237 } 235 }
238 } 236 }
239 237
240 void OomPriorityManager::Stop() { 238 void OomPriorityManager::Stop() {
241 timer_.Stop(); 239 timer_.Stop();
242 recent_tab_discard_timer_.Stop(); 240 recent_tab_discard_timer_.Stop();
243 if (low_memory_observer_.get()) 241 if (low_memory_observer_)
244 low_memory_observer_->Stop(); 242 low_memory_observer_->Stop();
245 else 243 else
246 memory_pressure_listener_.reset(); 244 memory_pressure_listener_.reset();
247 } 245 }
248 246
249 std::vector<base::string16> OomPriorityManager::GetTabTitles() { 247 std::vector<base::string16> OomPriorityManager::GetTabTitles() {
250 TabStatsList stats = GetTabStatsOnUIThread(); 248 TabStatsList stats = GetTabStatsOnUIThread();
251 base::AutoLock oom_score_autolock(oom_score_lock_); 249 base::AutoLock oom_score_autolock(oom_score_lock_);
252 std::vector<base::string16> titles; 250 std::vector<base::string16> titles;
253 titles.reserve(stats.size()); 251 titles.reserve(stats.size());
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 void OomPriorityManager::Observe(int type, 501 void OomPriorityManager::Observe(int type,
504 const content::NotificationSource& source, 502 const content::NotificationSource& source,
505 const content::NotificationDetails& details) { 503 const content::NotificationDetails& details) {
506 base::AutoLock oom_score_autolock(oom_score_lock_); 504 base::AutoLock oom_score_autolock(oom_score_lock_);
507 switch (type) { 505 switch (type) {
508 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: 506 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED:
509 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { 507 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
510 content::RenderProcessHost* host = 508 content::RenderProcessHost* host =
511 content::Source<content::RenderProcessHost>(source).ptr(); 509 content::Source<content::RenderProcessHost>(source).ptr();
512 oom_score_map_.erase(host->GetID()); 510 oom_score_map_.erase(host->GetID());
513 if (!low_memory_observer_.get()) { 511 if (!low_memory_observer_) {
514 // Coming here we know that a renderer was just killed and memory should 512 // 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 513 // 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 514 // not yet update its status and therefore we ask it to redo the
517 // measurement, calling us again if we have to release more. 515 // measurement, calling us again if we have to release more.
518 // Note: We do not only accelerate the discarding speed by doing another 516 // Note: We do not only accelerate the discarding speed by doing another
519 // check in short succession - we also accelerate it because the timer 517 // check in short succession - we also accelerate it because the timer
520 // driven MemoryPressureObserver will continue to produce timed events 518 // driven MemoryPressureMonitor will continue to produce timed events
521 // on top. So as longer as the cleanup phase takes, as more tabs will 519 // on top. So the longer the cleanup phase takes, the more tabs will
522 // get discarded in parallel. 520 // get discarded in parallel.
523 base::MemoryPressureObserverChromeOS* observer = 521 base::MemoryPressureMonitorChromeOS* monitor =
524 GetMemoryPressureObserver(); 522 GetMemoryPressureMonitor();
525 if (observer) 523 if (monitor)
526 observer->ScheduleEarlyCheck(); 524 monitor->ScheduleEarlyCheck();
527 } 525 }
528 break; 526 break;
529 } 527 }
530 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: { 528 case content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: {
531 bool visible = *content::Details<bool>(details).ptr(); 529 bool visible = *content::Details<bool>(details).ptr();
532 if (visible) { 530 if (visible) {
533 content::RenderProcessHost* render_host = 531 content::RenderProcessHost* render_host =
534 content::Source<content::RenderWidgetHost>(source).ptr()-> 532 content::Source<content::RenderWidgetHost>(source).ptr()->
535 GetProcess(); 533 GetProcess();
536 focused_tab_process_info_ = std::make_pair(render_host->GetID(), 534 focused_tab_process_info_ = std::make_pair(render_host->GetID(),
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 // For the moment we only do something when we reach a critical state. 705 // For the moment we only do something when we reach a critical state.
708 if (memory_pressure_level == 706 if (memory_pressure_level ==
709 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { 707 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
710 LogMemoryAndDiscardTab(); 708 LogMemoryAndDiscardTab();
711 } 709 }
712 // TODO(skuhne): If more memory pressure levels are introduced, we might 710 // TODO(skuhne): If more memory pressure levels are introduced, we might
713 // consider to call PurgeBrowserMemory() before CRITICAL is reached. 711 // consider to call PurgeBrowserMemory() before CRITICAL is reached.
714 } 712 }
715 713
716 } // namespace chromeos 714 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698