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