| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/memory/low_memory_observer.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/chromeos/chromeos_version.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/message_loop.h" | |
| 13 #include "base/time.h" | |
| 14 #include "base/timer.h" | |
| 15 #include "chrome/browser/browser_process.h" | |
| 16 #include "chrome/browser/browser_process_platform_part_chromeos.h" | |
| 17 #include "chrome/browser/chromeos/memory/oom_priority_manager.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/browser/zygote_host_linux.h" | |
| 20 | |
| 21 using content::BrowserThread; | |
| 22 | |
| 23 namespace chromeos { | |
| 24 | |
| 25 namespace { | |
| 26 // This is the file that will exist if low memory notification is available | |
| 27 // on the device. Whenever it becomes readable, it signals a low memory | |
| 28 // condition. | |
| 29 const char kLowMemFile[] = "/dev/chromeos-low-mem"; | |
| 30 | |
| 31 // This is the minimum amount of time in milliseconds between checks for | |
| 32 // low memory. | |
| 33 const int kLowMemoryCheckTimeoutMs = 750; | |
| 34 } // namespace | |
| 35 | |
| 36 //////////////////////////////////////////////////////////////////////////////// | |
| 37 // LowMemoryObserverImpl | |
| 38 // | |
| 39 // Does the actual work of observing. The observation work happens on the FILE | |
| 40 // thread, and the discarding of tabs happens on the UI thread. | |
| 41 // If low memory is detected, then we discard a tab, wait | |
| 42 // kLowMemoryCheckTimeoutMs milliseconds and then start watching again to see | |
| 43 // if we're still in a low memory state. This is to keep from discarding all | |
| 44 // tabs the first time we enter the state, because it takes time for the | |
| 45 // tabs to deallocate their memory. A timer isn't the perfect solution, but | |
| 46 // without any reliable indicator that a tab has had all its parts deallocated, | |
| 47 // it's the next best thing. | |
| 48 class LowMemoryObserverImpl | |
| 49 : public base::RefCountedThreadSafe<LowMemoryObserverImpl> { | |
| 50 public: | |
| 51 LowMemoryObserverImpl() : watcher_delegate_(this), file_descriptor_(-1) {} | |
| 52 | |
| 53 // Start watching the low memory file for readability. | |
| 54 // Calls to StartObserving should always be matched with calls to | |
| 55 // StopObserving. This method should only be called from the FILE thread. | |
| 56 void StartObservingOnFileThread(); | |
| 57 | |
| 58 // Stop watching the low memory file for readability. | |
| 59 // May be safely called if StartObserving has not been called. | |
| 60 // This method should only be called from the FILE thread. | |
| 61 void StopObservingOnFileThread(); | |
| 62 | |
| 63 private: | |
| 64 friend class base::RefCountedThreadSafe<LowMemoryObserverImpl>; | |
| 65 | |
| 66 ~LowMemoryObserverImpl() { | |
| 67 StopObservingOnFileThread(); | |
| 68 } | |
| 69 | |
| 70 // Start a timer to resume watching the low memory file descriptor. | |
| 71 void ScheduleNextObservation(); | |
| 72 | |
| 73 // Actually start watching the file descriptor. | |
| 74 void StartWatchingDescriptor(); | |
| 75 | |
| 76 // Delegate to receive events from WatchFileDescriptor. | |
| 77 class FileWatcherDelegate : public MessageLoopForIO::Watcher { | |
| 78 public: | |
| 79 explicit FileWatcherDelegate(LowMemoryObserverImpl* owner) | |
| 80 : owner_(owner) {} | |
| 81 virtual ~FileWatcherDelegate() {} | |
| 82 | |
| 83 // Overrides for MessageLoopForIO::Watcher | |
| 84 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} | |
| 85 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE { | |
| 86 LOG(WARNING) << "Low memory condition detected. Discarding a tab."; | |
| 87 // We can only discard tabs on the UI thread. | |
| 88 base::Callback<void(void)> callback = base::Bind(&DiscardTab); | |
| 89 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | |
| 90 owner_->ScheduleNextObservation(); | |
| 91 } | |
| 92 | |
| 93 // Sends off a discard request to the OomPriorityManager. Must be run on | |
| 94 // the UI thread. | |
| 95 static void DiscardTab() { | |
| 96 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 97 if (g_browser_process && | |
| 98 g_browser_process->platform_part()->oom_priority_manager()) { | |
| 99 g_browser_process->platform_part()-> | |
| 100 oom_priority_manager()->LogMemoryAndDiscardTab(); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 LowMemoryObserverImpl* owner_; | |
| 106 DISALLOW_COPY_AND_ASSIGN(FileWatcherDelegate); | |
| 107 }; | |
| 108 | |
| 109 scoped_ptr<MessageLoopForIO::FileDescriptorWatcher> watcher_; | |
| 110 FileWatcherDelegate watcher_delegate_; | |
| 111 int file_descriptor_; | |
| 112 base::OneShotTimer<LowMemoryObserverImpl> timer_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(LowMemoryObserverImpl); | |
| 115 }; | |
| 116 | |
| 117 void LowMemoryObserverImpl::StartObservingOnFileThread() { | |
| 118 DCHECK_LE(file_descriptor_, 0) | |
| 119 << "Attempted to start observation when it was already started."; | |
| 120 DCHECK(watcher_.get() == NULL); | |
| 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 122 DCHECK(MessageLoopForIO::current()); | |
| 123 | |
| 124 file_descriptor_ = ::open(kLowMemFile, O_RDONLY); | |
| 125 // Don't report this error unless we're really running on ChromeOS | |
| 126 // to avoid testing spam. | |
| 127 if (file_descriptor_ < 0 && base::chromeos::IsRunningOnChromeOS()) { | |
| 128 PLOG(ERROR) << "Unable to open " << kLowMemFile; | |
| 129 return; | |
| 130 } | |
| 131 watcher_.reset(new MessageLoopForIO::FileDescriptorWatcher); | |
| 132 StartWatchingDescriptor(); | |
| 133 } | |
| 134 | |
| 135 void LowMemoryObserverImpl::StopObservingOnFileThread() { | |
| 136 // If StartObserving failed, StopObserving will still get called. | |
| 137 timer_.Stop(); | |
| 138 if (file_descriptor_ >= 0) { | |
| 139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 140 watcher_.reset(NULL); | |
| 141 ::close(file_descriptor_); | |
| 142 file_descriptor_ = -1; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 void LowMemoryObserverImpl::ScheduleNextObservation() { | |
| 147 timer_.Start(FROM_HERE, | |
| 148 base::TimeDelta::FromMilliseconds(kLowMemoryCheckTimeoutMs), | |
| 149 this, | |
| 150 &LowMemoryObserverImpl::StartWatchingDescriptor); | |
| 151 } | |
| 152 | |
| 153 void LowMemoryObserverImpl::StartWatchingDescriptor() { | |
| 154 DCHECK(watcher_.get()); | |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 156 DCHECK(MessageLoopForIO::current()); | |
| 157 if (file_descriptor_ < 0) | |
| 158 return; | |
| 159 if (!MessageLoopForIO::current()->WatchFileDescriptor( | |
| 160 file_descriptor_, | |
| 161 false, // persistent=false: We want it to fire once and reschedule. | |
| 162 MessageLoopForIO::WATCH_READ, | |
| 163 watcher_.get(), | |
| 164 &watcher_delegate_)) { | |
| 165 LOG(ERROR) << "Unable to watch " << kLowMemFile; | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 //////////////////////////////////////////////////////////////////////////////// | |
| 170 // LowMemoryObserver | |
| 171 | |
| 172 LowMemoryObserver::LowMemoryObserver() : observer_(new LowMemoryObserverImpl) {} | |
| 173 | |
| 174 LowMemoryObserver::~LowMemoryObserver() { Stop(); } | |
| 175 | |
| 176 void LowMemoryObserver::Start() { | |
| 177 BrowserThread::PostTask( | |
| 178 BrowserThread::FILE, | |
| 179 FROM_HERE, | |
| 180 base::Bind(&LowMemoryObserverImpl::StartObservingOnFileThread, | |
| 181 observer_.get())); | |
| 182 } | |
| 183 | |
| 184 void LowMemoryObserver::Stop() { | |
| 185 BrowserThread::PostTask( | |
| 186 BrowserThread::FILE, | |
| 187 FROM_HERE, | |
| 188 base::Bind(&LowMemoryObserverImpl::StopObservingOnFileThread, | |
| 189 observer_.get())); | |
| 190 } | |
| 191 | |
| 192 // static | |
| 193 void LowMemoryObserver::SetLowMemoryMargin(int64 margin_mb) { | |
| 194 content::ZygoteHost::GetInstance()->AdjustLowMemoryMargin(margin_mb); | |
| 195 } | |
| 196 | |
| 197 } // namespace chromeos | |
| OLD | NEW |