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

Unified Diff: base/memory/low_memory_observer_chromeos.cc

Issue 2783613002: WIP: memory coordinator: Check /dev/chromeos-low-mem to determine memory condition
Patch Set: Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/memory/low_memory_observer_chromeos.h ('k') | base/memory/memory_pressure_monitor_chromeos.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/low_memory_observer_chromeos.cc
diff --git a/base/memory/low_memory_observer_chromeos.cc b/base/memory/low_memory_observer_chromeos.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b580972e649bd89f9bc689980de98b8db3b77170
--- /dev/null
+++ b/base/memory/low_memory_observer_chromeos.cc
@@ -0,0 +1,53 @@
+// Copyright 2017 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 "base/memory/low_memory_observer_chromeos.h"
+
+#include <fcntl.h>
+#include <sys/select.h>
+#include <sys/time.h>
+
+#include "base/posix/eintr_wrapper.h"
+#include "base/sys_info.h"
+
+namespace base {
+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";
+
+} // namespace
+
+LowMemoryObserver::LowMemoryObserver()
+ : low_mem_file_(HANDLE_EINTR(::open(kLowMemFile, O_RDONLY))) {
+ LOG_IF(ERROR,
+ base::SysInfo::IsRunningOnChromeOS() && !low_mem_file_.is_valid())
+ << "Cannot open kernel listener";
+}
+
+LowMemoryObserver::~LowMemoryObserver() {}
+
+bool LowMemoryObserver::IsLowMemoryCondition() const {
+ if (!is_valid())
+ return false;
+
+ int fd = low_mem_file_.get();
+ fd_set fds;
+ struct timeval tv;
+
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+
+ return HANDLE_EINTR(select(fd + 1, &fds, NULL, NULL, &tv)) > 0;
+}
+
+} // namespace chromeos
+} // namespace base
« no previous file with comments | « base/memory/low_memory_observer_chromeos.h ('k') | base/memory/memory_pressure_monitor_chromeos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698