Index: components/memory_coordinator/browser/memory_monitor_linux.cc |
diff --git a/components/memory_coordinator/browser/memory_monitor_linux.cc b/components/memory_coordinator/browser/memory_monitor_linux.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bd1cf500dae38fe9b9b3c490cdaef5bdff130379 |
--- /dev/null |
+++ b/components/memory_coordinator/browser/memory_monitor_linux.cc |
@@ -0,0 +1,54 @@ |
+// Copyright (c) 2016 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 "components/memory_coordinator/browser/memory_monitor_linux.h" |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/process/process_metrics.h" |
+ |
+namespace memory_coordinator { |
+ |
+namespace { |
+ |
+// A global static instance of the default delegate. Used by default by |
+// MemoryMonitorLinux. |
+MemoryMonitorDelegate g_memory_monitor_delegate; |
+ |
+// The number of bits to shift to convert KiB to MiB. |
+const int kShiftKiBtoMiB = 10; |
+ |
+} // namespace |
+ |
+MemoryMonitorLinux::MemoryMonitorLinux(MemoryMonitorDelegate* delegate) |
+ : delegate_(delegate) {} |
+ |
+int MemoryMonitorLinux::GetFreeMemoryUntilCriticalMB() { |
+ base::SystemMemoryInfoKB mem_info = {}; |
+ delegate_->GetSystemMemoryInfo(&mem_info); |
+ |
+ // According to kernel commit 34e431b0ae398fc54ea69ff85ec700722c9da773, |
+ // "available" is the "amount of memory that is available for a new workload |
+ // without pushing the system into swap"; return that value if it is valid. |
+ // Old linux kernels (before 3.14) don't support "available" and show zero |
+ // instead. |
+ if (mem_info.available > 0) |
+ return mem_info.available >> kShiftKiBtoMiB; |
+ |
+ // If there is no "available" value, guess at it based on free memory and |
+ // what the OS can easily discard. |
+ return (mem_info.free + mem_info.buffers + mem_info.cached) >> kShiftKiBtoMiB; |
+} |
+ |
+// static |
+std::unique_ptr<MemoryMonitorLinux> MemoryMonitorLinux::Create( |
+ MemoryMonitorDelegate* delegate) { |
+ return base::MakeUnique<MemoryMonitorLinux>(delegate); |
+} |
+ |
+// Implementation of factory function defined in memory_monitor.h. |
+std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
+ return MemoryMonitorLinux::Create(&g_memory_monitor_delegate); |
+} |
+ |
+} // namespace memory_coordinator |