Index: components/memory_coordinator/browser/memory_monitor_win.cc |
diff --git a/components/memory_coordinator/browser/memory_monitor_win.cc b/components/memory_coordinator/browser/memory_monitor_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c3102769494d1fa3abb836b5fd5b49750edd4298 |
--- /dev/null |
+++ b/components/memory_coordinator/browser/memory_monitor_win.cc |
@@ -0,0 +1,96 @@ |
+// 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_win.h" |
+ |
+#include "base/process/process_metrics.h" |
+ |
+// TODO(chrisha): Implement a mechanism for observing swapping, and updating the |
+// memory threshold on a per machine basis. |
+ |
+namespace memory_coordinator { |
+ |
+namespace { |
+ |
+const int kKBperMB = 1024; |
+ |
+// A default implementation of MemoryMonitorWinDelegate. Used by default by |
+// MemoryMonitorWin. |
+class MemoryMonitorWinDelegateImpl : public MemoryMonitorWinDelegate { |
bashi
2016/08/15 23:29:40
(Just comment)
Maybe this delegate can be platform
chrisha
2016/08/17 18:52:36
Yeah, it might want to be. I'll leave it Win only
|
+ public: |
+ MemoryMonitorWinDelegateImpl() {} |
+ ~MemoryMonitorWinDelegateImpl() override {} |
+ |
+ // GetSystemMemoryInfoDelegate: |
+ void GetSystemMemoryInfo(base::SystemMemoryInfoKB* mem_info) override { |
+ base::GetSystemMemoryInfo(mem_info); |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(MemoryMonitorWinDelegateImpl); |
+}; |
+ |
+// A global static instance of the default delegate. Used by default by |
+// MemoryMonitorWin. |
+MemoryMonitorWinDelegateImpl g_memory_monitor_win_delegate; |
+ |
+} // namespace |
+ |
+// A system is considered 'large memory' if it has more than 1.5GB of system |
+// memory available for use by the memory manager (not reserved for hardware |
+// and drivers). This is a fuzzy version of the ~2GB discussed below. |
+const int MemoryMonitorWin::kLargeMemoryThresholdMB = 1536; |
+ |
+// This is the target free memory used for systems with < ~2GB of physical |
+// memory. Such systems have been observed to always maintain ~100MB of |
+// available memory, paging until that is the case. To try to avoid paging a |
+// threshold slightly above this is chosen. |
+const int MemoryMonitorWin::kSmallMemoryTargetFreeMB = 200; |
+ |
+// This is the target free memory used for systems with >= ~2GB of physical |
+// memory. Such systems have been observed to always maintain ~300MB of |
+// available memory, paging until that is the case. |
+const int MemoryMonitorWin::kLargeMemoryTargetFreeMB = 400; |
+ |
+MemoryMonitorWin::MemoryMonitorWin(MemoryMonitorWinDelegate* delegate, |
+ int target_free_mb) |
+ : delegate_(delegate), target_free_mb_(target_free_mb) { |
+} |
+ |
+int MemoryMonitorWin::GetFreeMemoryUntilCriticalMB() { |
+ base::SystemMemoryInfoKB mem_info = {}; |
+ delegate_->GetSystemMemoryInfo(&mem_info); |
+ int free_mb = mem_info.free / kKBperMB; |
+ free_mb -= target_free_mb_; |
+ return free_mb; |
+} |
+ |
+// static |
+std::unique_ptr<MemoryMonitorWin> MemoryMonitorWin::Create( |
+ MemoryMonitorWinDelegate* delegate) { |
+ return std::unique_ptr<MemoryMonitorWin>(new MemoryMonitorWin( |
+ delegate, GetTargetFreeMB(delegate))); |
+} |
+ |
+// static |
+bool MemoryMonitorWin::IsLargeMemory(MemoryMonitorWinDelegate* delegate) { |
+ base::SystemMemoryInfoKB mem_info = {}; |
+ delegate->GetSystemMemoryInfo(&mem_info); |
+ return (mem_info.total / kKBperMB) >= |
+ MemoryMonitorWin::kLargeMemoryThresholdMB; |
+} |
+ |
+// static |
+int MemoryMonitorWin::GetTargetFreeMB(MemoryMonitorWinDelegate* delegate) { |
+ if (IsLargeMemory(delegate)) |
+ return MemoryMonitorWin::kLargeMemoryTargetFreeMB; |
+ return MemoryMonitorWin::kSmallMemoryTargetFreeMB; |
+} |
+ |
+// Implementation of factory function defined in memory_monitor.h. |
+std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() { |
+ return MemoryMonitorWin::Create(&g_memory_monitor_win_delegate); |
+} |
+ |
+} // namespace memory_coordinator |