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

Side by Side Diff: chrome/browser/memory/low_memory_observer_chromeos.cc

Issue 1188823002: [MemoryPressure] Move chrome/browser/chromeos/* to chrome/browser/memory/*. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed sky@ comments. Created 5 years, 6 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/low_memory_observer.h" 5 #include "chrome/browser/memory/low_memory_observer_chromeos.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/sys_info.h" 12 #include "base/sys_info.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "base/timer/timer.h" 14 #include "base/timer/timer.h"
15 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/browser_process_platform_part_chromeos.h" 16 #include "chrome/browser/browser_process_platform_part_chromeos.h"
17 #include "chrome/browser/chromeos/memory/oom_priority_manager.h" 17 #include "chrome/browser/memory/oom_priority_manager.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 19
20 using content::BrowserThread; 20 using content::BrowserThread;
21 21
22 namespace chromeos {
23
24 namespace { 22 namespace {
25 // This is the file that will exist if low memory notification is available 23 // This is the file that will exist if low memory notification is available
26 // on the device. Whenever it becomes readable, it signals a low memory 24 // on the device. Whenever it becomes readable, it signals a low memory
27 // condition. 25 // condition.
28 const char kLowMemFile[] = "/dev/chromeos-low-mem"; 26 const char kLowMemFile[] = "/dev/chromeos-low-mem";
29 27
30 // This is the minimum amount of time in milliseconds between checks for 28 // This is the minimum amount of time in milliseconds between checks for
31 // low memory. 29 // low memory.
32 const int kLowMemoryCheckTimeoutMs = 750; 30 const int kLowMemoryCheckTimeoutMs = 750;
33 } // namespace 31 } // namespace
34 32
33 namespace memory {
34
35 //////////////////////////////////////////////////////////////////////////////// 35 ////////////////////////////////////////////////////////////////////////////////
36 // LowMemoryObserverImpl 36 // LowMemoryObserverImpl
37 // 37 //
38 // Does the actual work of observing. The observation work happens on the FILE 38 // Does the actual work of observing. The observation work happens on the FILE
39 // thread, and the discarding of tabs happens on the UI thread. 39 // thread, and the discarding of tabs happens on the UI thread.
40 // If low memory is detected, then we discard a tab, wait 40 // If low memory is detected, then we discard a tab, wait
41 // kLowMemoryCheckTimeoutMs milliseconds and then start watching again to see 41 // kLowMemoryCheckTimeoutMs milliseconds and then start watching again to see
42 // if we're still in a low memory state. This is to keep from discarding all 42 // if we're still in a low memory state. This is to keep from discarding all
43 // tabs the first time we enter the state, because it takes time for the 43 // tabs the first time we enter the state, because it takes time for the
44 // tabs to deallocate their memory. A timer isn't the perfect solution, but 44 // tabs to deallocate their memory. A timer isn't the perfect solution, but
(...skipping 10 matching lines...) Expand all
55 void StartObservingOnFileThread(); 55 void StartObservingOnFileThread();
56 56
57 // Stop watching the low memory file for readability. 57 // Stop watching the low memory file for readability.
58 // May be safely called if StartObserving has not been called. 58 // May be safely called if StartObserving has not been called.
59 // This method should only be called from the FILE thread. 59 // This method should only be called from the FILE thread.
60 void StopObservingOnFileThread(); 60 void StopObservingOnFileThread();
61 61
62 private: 62 private:
63 friend class base::RefCountedThreadSafe<LowMemoryObserverImpl>; 63 friend class base::RefCountedThreadSafe<LowMemoryObserverImpl>;
64 64
65 ~LowMemoryObserverImpl() { 65 ~LowMemoryObserverImpl() { StopObservingOnFileThread(); }
66 StopObservingOnFileThread();
67 }
68 66
69 // Start a timer to resume watching the low memory file descriptor. 67 // Start a timer to resume watching the low memory file descriptor.
70 void ScheduleNextObservation(); 68 void ScheduleNextObservation();
71 69
72 // Actually start watching the file descriptor. 70 // Actually start watching the file descriptor.
73 void StartWatchingDescriptor(); 71 void StartWatchingDescriptor();
74 72
75 // Delegate to receive events from WatchFileDescriptor. 73 // Delegate to receive events from WatchFileDescriptor.
76 class FileWatcherDelegate : public base::MessageLoopForIO::Watcher { 74 class FileWatcherDelegate : public base::MessageLoopForIO::Watcher {
77 public: 75 public:
(...skipping 10 matching lines...) Expand all
88 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); 86 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
89 owner_->ScheduleNextObservation(); 87 owner_->ScheduleNextObservation();
90 } 88 }
91 89
92 // Sends off a discard request to the OomPriorityManager. Must be run on 90 // Sends off a discard request to the OomPriorityManager. Must be run on
93 // the UI thread. 91 // the UI thread.
94 static void DiscardTab() { 92 static void DiscardTab() {
95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 93 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
96 if (g_browser_process && 94 if (g_browser_process &&
97 g_browser_process->platform_part()->oom_priority_manager()) { 95 g_browser_process->platform_part()->oom_priority_manager()) {
98 g_browser_process->platform_part()-> 96 g_browser_process->platform_part()
99 oom_priority_manager()->LogMemoryAndDiscardTab(); 97 ->oom_priority_manager()
98 ->LogMemoryAndDiscardTab();
100 } 99 }
101 } 100 }
102 101
103 private: 102 private:
104 LowMemoryObserverImpl* owner_; 103 LowMemoryObserverImpl* owner_;
105 DISALLOW_COPY_AND_ASSIGN(FileWatcherDelegate); 104 DISALLOW_COPY_AND_ASSIGN(FileWatcherDelegate);
106 }; 105 };
107 106
108 scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_; 107 scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_;
109 FileWatcherDelegate watcher_delegate_; 108 FileWatcherDelegate watcher_delegate_;
(...skipping 28 matching lines...) Expand all
138 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 137 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
139 watcher_.reset(NULL); 138 watcher_.reset(NULL);
140 ::close(file_descriptor_); 139 ::close(file_descriptor_);
141 file_descriptor_ = -1; 140 file_descriptor_ = -1;
142 } 141 }
143 } 142 }
144 143
145 void LowMemoryObserverImpl::ScheduleNextObservation() { 144 void LowMemoryObserverImpl::ScheduleNextObservation() {
146 timer_.Start(FROM_HERE, 145 timer_.Start(FROM_HERE,
147 base::TimeDelta::FromMilliseconds(kLowMemoryCheckTimeoutMs), 146 base::TimeDelta::FromMilliseconds(kLowMemoryCheckTimeoutMs),
148 this, 147 this, &LowMemoryObserverImpl::StartWatchingDescriptor);
149 &LowMemoryObserverImpl::StartWatchingDescriptor);
150 } 148 }
151 149
152 void LowMemoryObserverImpl::StartWatchingDescriptor() { 150 void LowMemoryObserverImpl::StartWatchingDescriptor() {
153 DCHECK(watcher_.get()); 151 DCHECK(watcher_.get());
154 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 152 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
155 DCHECK(base::MessageLoopForIO::current()); 153 DCHECK(base::MessageLoopForIO::current());
156 if (file_descriptor_ < 0) 154 if (file_descriptor_ < 0)
157 return; 155 return;
158 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( 156 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
159 file_descriptor_, 157 file_descriptor_,
160 false, // persistent=false: We want it to fire once and reschedule. 158 false, // persistent=false: We want it to fire once and reschedule.
161 base::MessageLoopForIO::WATCH_READ, 159 base::MessageLoopForIO::WATCH_READ, watcher_.get(),
162 watcher_.get(),
163 &watcher_delegate_)) { 160 &watcher_delegate_)) {
164 LOG(ERROR) << "Unable to watch " << kLowMemFile; 161 LOG(ERROR) << "Unable to watch " << kLowMemFile;
165 } 162 }
166 } 163 }
167 164
168 //////////////////////////////////////////////////////////////////////////////// 165 ////////////////////////////////////////////////////////////////////////////////
169 // LowMemoryObserver 166 // LowMemoryObserver
170 167
171 LowMemoryObserver::LowMemoryObserver() : observer_(new LowMemoryObserverImpl) {} 168 LowMemoryObserver::LowMemoryObserver() : observer_(new LowMemoryObserverImpl) {
169 }
172 170
173 LowMemoryObserver::~LowMemoryObserver() { Stop(); } 171 LowMemoryObserver::~LowMemoryObserver() {
172 Stop();
173 }
174 174
175 void LowMemoryObserver::Start() { 175 void LowMemoryObserver::Start() {
176 BrowserThread::PostTask( 176 BrowserThread::PostTask(
177 BrowserThread::FILE, 177 BrowserThread::FILE, FROM_HERE,
178 FROM_HERE,
179 base::Bind(&LowMemoryObserverImpl::StartObservingOnFileThread, 178 base::Bind(&LowMemoryObserverImpl::StartObservingOnFileThread,
180 observer_.get())); 179 observer_.get()));
181 } 180 }
182 181
183 void LowMemoryObserver::Stop() { 182 void LowMemoryObserver::Stop() {
184 BrowserThread::PostTask( 183 BrowserThread::PostTask(
185 BrowserThread::FILE, 184 BrowserThread::FILE, FROM_HERE,
186 FROM_HERE,
187 base::Bind(&LowMemoryObserverImpl::StopObservingOnFileThread, 185 base::Bind(&LowMemoryObserverImpl::StopObservingOnFileThread,
188 observer_.get())); 186 observer_.get()));
189 } 187 }
190 188
191 } // namespace chromeos 189 } // namespace memory
OLDNEW
« no previous file with comments | « chrome/browser/memory/low_memory_observer_chromeos.h ('k') | chrome/browser/memory/oom_memory_details.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698