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..232bdf7f3a1f90cb84ff5fc0d76e24540cc687a4 |
--- /dev/null |
+++ b/base/win/memory_pressure_monitor.cc |
@@ -0,0 +1,221 @@ |
+// 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 { |
+ |
+// 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. |
+ |
+// The time between memory pressure checks. While under critical pressure, this |
+// is also the timer to repeat cleanup attempts. |
+const int kMemoryPressureIntervalMs = 1000; |
brucedawson
2015/05/06 00:58:27
All of the functions being called should, I believ
chrisha
2015/05/06 02:40:24
Yeah, I profiled GlobalMemoryStatusEx on a handful
|
+ |
+// The time which should pass between two successive moderate memory pressure |
+// calls. |
+const int kModerateMemoryPressureCooldownMs = 10000; |
+ |
+// Number of event polls before the next moderate pressure event can be sent. |
+const int kModerateMemoryPressureCooldown = |
+ kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs; |
+ |
+// 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 memory requirements. These are modeled after typical renderer sizes |
+// on Windows sytems (rounded to the neared 10 MB). |
Nico
2015/05/05 21:25:11
maybe mention which uma stats one has to look at t
chrisha
2015/05/06 02:40:24
Done.
|
+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 |
+ |
+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() { |
+ 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() { |
+ timer_.Start(FROM_HERE, |
+ TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), |
+ Bind(&MemoryPressureMonitor:: |
+ CheckMemoryPressureAndRecordStatistics, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void MemoryPressureMonitor::StopObserving() { |
+ // If StartObserving failed, StopObserving will still get called. |
+ timer_.Stop(); |
+ weak_ptr_factory_.InvalidateWeakPtrs(); |
+} |
+ |
+void MemoryPressureMonitor::CheckMemoryPressure() { |
+ // |notify| will be set to true if MemoryPressureListeners need to be |
+ // notified of a memory pressure level state change. |
+ bool notify = false; |
+ |
+ base::AutoLock scoped_lock(lock_); |
+ MemoryPressureLevel old_pressure = current_memory_pressure_level_; |
+ current_memory_pressure_level_ = CalculateCurrentPressureLevel(); |
+ |
+ switch (current_memory_pressure_level_) { |
+ case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
+ break; |
+ |
+ case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: { |
+ if (old_pressure != current_memory_pressure_level_) { |
+ // This is a new transition to moderate pressure so notify. |
+ moderate_pressure_repeat_count_ = 0; |
+ notify = true; |
+ } else { |
+ // Already in moderate pressure, only notify if sustained over the |
+ // cooldown period. |
+ if (++moderate_pressure_repeat_count_ == |
+ kModerateMemoryPressureCooldown) { |
+ 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() { |
+ 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. |
+ int phys_free = static_cast<int>(mem_status.ullAvailPhys / 1024 / 1024); |
+ |
+ // The virtual load of this process. This is the percentage of virtual memory |
+ // that is reserved or committed. This is calculated via a double to avoid |
+ // overflowing on 64-bit Windows. |
+ int virt_load = 100 - static_cast<int>( |
+ 100.0 * static_cast<double>(mem_status.ullAvailVirtual) / |
+ static_cast<double>(mem_status.ullTotalVirtual)); |
+ |
+ // The amount of virtual memory that is actively available for use right |
+ // now, in MBs. |
brucedawson
2015/05/06 00:58:27
This comment should clarify that ullAvailVirtual/v
chrisha
2015/05/06 02:40:24
Done.
|
+ int virt_free = static_cast<int>(mem_status.ullAvailVirtual / 1024 / 1024); |
Nico
2015/05/05 21:25:11
Did you check that this isn't some huge number in
brucedawson
2015/05/06 00:58:27
I agree with Nico. In 64-bit Windows ullAvailVirtu
chrisha
2015/05/06 02:40:24
I agree that there's little value in calculating t
|
+ |
+ // Determine if the system is under critical memory pressure. |
+ if (phys_load > kCriticalThresholdPercent || |
+ virt_load > kCriticalThresholdPercent || |
+ phys_free < kCriticalThresholdMb || |
+ virt_free < kCriticalThresholdMb) { |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
+ } |
+ |
+ // Determine if the system is under moderate memory pressure. |
+ if (phys_load > kModerateThresholdPercent || |
+ virt_load > kModerateThresholdPercent || |
+ phys_free < kModerateThresholdMb || |
brucedawson
2015/05/06 00:58:28
I'm not sure this test really works. My understand
chrisha
2015/05/06 02:40:24
The absolute thresholds are only meant for low-end
|
+ virt_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 |