Index: base/win/memory_pressure_monitor.cc |
diff --git a/base/win/memory_pressure_monitor.cc b/base/win/memory_pressure_monitor.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f5601503d805096fe021ed46f33a81091df6ba57 |
--- /dev/null |
+++ b/base/win/memory_pressure_monitor.cc |
@@ -0,0 +1,229 @@ |
+// Copyright 2015 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/win/memory_pressure_monitor.h" |
+ |
+#include <windows.h> |
+ |
+#include "base/metrics/histogram_macros.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "base/time/time.h" |
+ |
+namespace base { |
+namespace win { |
+ |
+namespace { |
+ |
+// TODO(chrisha): Explore the following constants further with an experiment. |
+ |
+// Moderate and critical memory load percentages. These percentages are applied |
+// to both system memory and virtual memory. |
+const int kModerateThresholdPercent = 60; |
+const int kCriticalThresholdPercent = 95; |
+ |
+// Absolute system memory requirements. These are modeled after typical renderer |
+// sizes on Windows sytems (rounded to the neared 10 MB). These are applied to |
+// system memory only. Intended as a lower bound to the above percentages on |
+// low-end systems. Taken from the Memory.Renderer UMA statistic. |
+const int kModerateThresholdMb = 300; // 95th percentile renderer size. |
+const int kCriticalThresholdMb = 60; // 50th percentile renderer size. |
+ |
+// Enumeration of UMA memory pressure levels. This needs to be kept in sync with |
+// histograms.xml and the memory pressure levels defined in |
+// MemoryPressureListener. |
+enum MemoryPressureLevelUMA { |
+ UMA_MEMORY_PRESSURE_LEVEL_NONE = 0, |
+ UMA_MEMORY_PRESSURE_LEVEL_MODERATE = 1, |
+ UMA_MEMORY_PRESSURE_LEVEL_CRITICAL = 2, |
+ // This must be the last value in the enum. |
+ UMA_MEMORY_PRESSURE_LEVEL_COUNT, |
+}; |
+ |
+// Converts a memory pressure level to an UMA enumeration value. |
+MemoryPressureLevelUMA MemoryPressureLevelToUmaEnumValue( |
+ MemoryPressureListener::MemoryPressureLevel level) { |
+ switch (level) { |
+ case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
+ return UMA_MEMORY_PRESSURE_LEVEL_NONE; |
+ case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: |
+ return UMA_MEMORY_PRESSURE_LEVEL_MODERATE; |
+ case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: |
+ return UMA_MEMORY_PRESSURE_LEVEL_CRITICAL; |
+ } |
+ NOTREACHED(); |
+ return UMA_MEMORY_PRESSURE_LEVEL_NONE; |
+} |
+ |
+} // namespace |
+ |
+// The following constants have been lifted from similar values in the ChromeOS |
+// memory pressure monitor. The values were determined experimentally to ensure |
+// sufficient responsiveness of the memory pressure subsystem, and minimal |
+// overhead. |
+const int MemoryPressureMonitor::kPollingIntervalMs = 1000; |
+const int MemoryPressureMonitor::kModeratePressureCooldownMs = 10000; |
+const int MemoryPressureMonitor::kModeratePressureCooldownCycles = |
+ kModeratePressureCooldownMs / kPollingIntervalMs; |
+ |
+MemoryPressureMonitor::MemoryPressureMonitor() |
+ : current_memory_pressure_level_( |
+ MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
+ moderate_pressure_repeat_count_(0), |
+ weak_ptr_factory_(this) { |
+ StartObserving(); |
+} |
+ |
+MemoryPressureMonitor::~MemoryPressureMonitor() { |
+ StopObserving(); |
+} |
+ |
+void MemoryPressureMonitor::CheckMemoryPressureSoon() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, Bind(&MemoryPressureMonitor::CheckMemoryPressure, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+MemoryPressureListener::MemoryPressureLevel |
+MemoryPressureMonitor::GetCurrentPressureLevel() const { |
+ return current_memory_pressure_level_; |
+} |
+ |
+void MemoryPressureMonitor::StartObserving() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ timer_.Start(FROM_HERE, |
+ TimeDelta::FromMilliseconds(kPollingIntervalMs), |
+ Bind(&MemoryPressureMonitor:: |
+ CheckMemoryPressureAndRecordStatistics, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void MemoryPressureMonitor::StopObserving() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ // If StartObserving failed, StopObserving will still get called. |
+ timer_.Stop(); |
+ weak_ptr_factory_.InvalidateWeakPtrs(); |
+} |
+ |
+void MemoryPressureMonitor::CheckMemoryPressure() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ // Get the previous pressure level and update the current one. |
+ MemoryPressureLevel old_pressure = current_memory_pressure_level_; |
+ current_memory_pressure_level_ = CalculateCurrentPressureLevel(); |
+ |
+ // |notify| will be set to true if MemoryPressureListeners need to be |
+ // notified of a memory pressure level state change. |
+ bool notify = false; |
+ switch (current_memory_pressure_level_) { |
+ case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
+ break; |
+ |
+ case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: { |
grt (UTC plus 2)
2015/05/06 16:31:09
nit: remove unneeded braces here and on line 143
|
+ if (old_pressure != current_memory_pressure_level_) { |
+ // This is a new transition to moderate pressure so notify. |
+ moderate_pressure_repeat_count_ = 0; |
grt (UTC plus 2)
2015/05/06 16:31:09
nit: it looks like you can move this assignment do
chrisha
2015/05/06 16:59:04
I personally like the expanded statement as is bec
grt (UTC plus 2)
2015/05/06 18:14:57
nod.
|
+ notify = true; |
+ } else { |
+ // Already in moderate pressure, only notify if sustained over the |
+ // cooldown period. |
+ if (++moderate_pressure_repeat_count_ == |
+ kModeratePressureCooldownCycles) { |
+ moderate_pressure_repeat_count_ = 0; |
+ notify = true; |
+ } |
+ } |
+ } break; |
+ |
+ case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: { |
+ // Always notify of critical pressure levels. |
+ notify = true; |
+ } break; |
+ } |
+ |
+ if (!notify) |
+ return; |
+ |
+ // Emit a notification of the current memory pressure level. This can only |
+ // happen for moderate and critical pressure levels. |
+ DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
+ current_memory_pressure_level_); |
+ MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); |
+} |
+ |
+void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ CheckMemoryPressure(); |
+ |
+ UMA_HISTOGRAM_ENUMERATION( |
+ "Memory.PressureLevel", |
+ MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_), |
+ UMA_MEMORY_PRESSURE_LEVEL_COUNT); |
+} |
+ |
+MemoryPressureListener::MemoryPressureLevel |
+MemoryPressureMonitor::CalculateCurrentPressureLevel() { |
+ MEMORYSTATUSEX mem_status = {}; |
+ if (!GetSystemMemoryStatus(&mem_status)) |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
+ |
+ // How much of the system's physical memory is actively being used by |
+ // pages that are in the working sets of running programs. |
+ int phys_load = mem_status.dwMemoryLoad; |
+ |
+ // How much system memory is actively available for use right now, in MBs. |
+ static const DWORDLONG kMBBytes = 1024 * 1024; |
+ int phys_free = static_cast<int>(mem_status.ullAvailPhys / kMBBytes); |
+ |
+ // Determine if the physical memory is under critical memory pressure. |
+ if (phys_load > kCriticalThresholdPercent || |
+ phys_free < kCriticalThresholdMb) { |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
+ } |
+ |
+ // On 64-bit platforms there is little value in calculating virtual memory |
+ // pressure as it is at least 3 orders of magnitude larger than system memory. |
+#if !defined(ARCH_CPU_64_BITS) |
+ // The virtual load of the current process. This is the percentage of virtual |
+ // memory that is reserved or committed. |
+ int virt_load = 100 - static_cast<int>( |
+ 100 * mem_status.ullAvailVirtual / mem_status.ullTotalVirtual); |
+ |
+ // Determine if the virtual memory is under critical memory pressure. |
+ if (virt_load > kCriticalThresholdPercent) |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
+ |
+ // Determine if the virtual memory is under moderate memory pressure. |
+ if (virt_load > kModerateThresholdPercent) |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
+#endif // !defined(ARCH_CPU_64_BITS) |
+ |
+ // Determine if the physical memory is under moderate memory pressure. This |
+ // is done after the virtual memory checking to give that a chance to emit a |
+ // critical pressure signal. |
+ if (phys_load > kModerateThresholdPercent || |
+ phys_free < kModerateThresholdMb) { |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
+ } |
+ |
+ // No memory pressure was detected. |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
+} |
+ |
+bool MemoryPressureMonitor::GetSystemMemoryStatus( |
+ MEMORYSTATUSEX* mem_status) { |
+ DCHECK(mem_status != nullptr); |
+ mem_status->dwLength = sizeof(MEMORYSTATUSEX); |
+ if (!::GlobalMemoryStatusEx(mem_status)) |
+ return false; |
+ return true; |
+} |
+ |
+} // namespace win |
+} // namespace base |