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

Unified Diff: components/memory_coordinator/browser/memory_monitor_win.cc

Issue 2236983002: Create MemoryMonitor and Windows implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix presubmit and clang errors. Created 4 years, 4 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
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..828cf06f00462fc4997e475fe49422b6aa90575d
--- /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 {
+ 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 the target free memory used for systems with < ~2GB of physical
haraken 2016/08/12 06:53:15 the the
chrisha 2016/08/12 20:14:00 Done.
+// 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.
bashi 2016/08/11 22:03:12 Put this in a separate file (memory_monitor.cc) ?
chrisha 2016/08/12 20:14:00 This is a platform dependent factory function. Mov
+std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() {
+ return MemoryMonitorWin::Create(&g_memory_monitor_win_delegate);
+}
+
+} // namespace memory_coordinator

Powered by Google App Engine
This is Rietveld 408576698