Index: base/win/memory_pressure_monitor_win.cc |
diff --git a/base/win/memory_pressure_monitor_win.cc b/base/win/memory_pressure_monitor_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cfed1ed78fc7fefdd05e94b6d2b67306c2a4bf95 |
--- /dev/null |
+++ b/base/win/memory_pressure_monitor_win.cc |
@@ -0,0 +1,201 @@ |
+// 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_win.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 { |
+ |
+// The time between memory pressure checks. While under critical pressure, this |
+// is also the timer to repeat cleanup attempts. |
+const int kMemoryPressureIntervalMs = 1000; |
grt (UTC plus 2)
2015/05/05 15:33:59
how were these constants chosen? consider document
chrisha
2015/05/05 19:46:12
Done.
|
+ |
+// 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; |
+ |
+// 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). |
+const int kModerateThresholdMb = 300; // 95th percentile renderer. |
+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, |
grt (UTC plus 2)
2015/05/05 15:33:59
nit: i like the practice of making the values expl
chrisha
2015/05/05 19:46:12
Done.
|
+ UMA_MEMORY_PRESSURE_LEVEL_CRITICAL, |
+ 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; |
+ default: |
grt (UTC plus 2)
2015/05/05 15:33:59
remove the default case so that the compiler will
chrisha
2015/05/05 19:46:12
Err... yeah, meant to do that.
Done.
|
+ break; |
+ } |
+ NOTREACHED(); |
+ return UMA_MEMORY_PRESSURE_LEVEL_NONE; |
+} |
+ |
+} // namespace |
+ |
+MemoryPressureMonitorWin::MemoryPressureMonitorWin() |
+ : moderate_pressure_repeat_count_(0), |
grt (UTC plus 2)
2015/05/05 15:33:59
current_memory_pressure_level_(MEMORY_PRESSURE_LEV
chrisha
2015/05/05 19:46:12
Done.
|
+ weak_ptr_factory_(this) { |
+ StartObserving(); |
+} |
+ |
+MemoryPressureMonitorWin::~MemoryPressureMonitorWin() { |
+ StopObserving(); |
+} |
+ |
+void MemoryPressureMonitorWin::ScheduleEarlyCheck() { |
+ ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, Bind(&MemoryPressureMonitorWin::CheckMemoryPressure, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+MemoryPressureListener::MemoryPressureLevel |
+MemoryPressureMonitorWin::GetCurrentPressureLevel() const { |
+ return current_memory_pressure_level_; |
grt (UTC plus 2)
2015/05/05 15:33:59
what do you think of adding a base::ThreadChecker
chrisha
2015/05/05 19:46:12
Actually, I don't see why the MemoryPressureMonito
grt (UTC plus 2)
2015/05/06 13:26:35
In general, Chromium code is expected to run on a
chrisha
2015/05/06 15:09:57
It makes sense for the actual CheckMemoryPressure
|
+} |
+ |
+void MemoryPressureMonitorWin::StartObserving() { |
+ timer_.Start(FROM_HERE, |
+ TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), |
+ Bind(&MemoryPressureMonitorWin:: |
+ CheckMemoryPressureAndRecordStatistics, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void MemoryPressureMonitorWin::StopObserving() { |
+ // If StartObserving failed, StopObserving will still get called. |
+ timer_.Stop(); |
grt (UTC plus 2)
2015/05/05 15:33:59
weak_ptr_factory_.InvalidateWeakPtrs();
chrisha
2015/05/05 19:46:12
Done.
|
+} |
+ |
+void MemoryPressureMonitorWin::CheckMemoryPressure() { |
+ MemoryPressureLevel old_pressure = current_memory_pressure_level_; |
+ current_memory_pressure_level_ = CalculateCurrentPressureLevel(); |
+ |
+ // In case there is no memory pressure we do not notify. |
grt (UTC plus 2)
2015/05/05 15:33:59
consider using a switch statement here w/o a defau
chrisha
2015/05/05 19:46:12
Done.
|
+ if (current_memory_pressure_level_ == |
+ MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { |
+ moderate_pressure_repeat_count_ = 0; |
+ return; |
+ } |
+ |
+ // If the pressure is critical we *always* notify. This keeps a repeated |
+ // ping going when memory pressure is very tight. |
+ if (current_memory_pressure_level_ == |
+ MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
+ moderate_pressure_repeat_count_ = 0; |
+ MemoryPressureListener::NotifyMemoryPressure( |
+ current_memory_pressure_level_); |
+ return; |
+ } |
+ |
+ // At this point the memory pressure is moderate. |
+ DCHECK_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
+ current_memory_pressure_level_); |
+ |
+ // If the pressure is moderate and the cool-down period hasn't elapsed, then |
+ // swallow the memory pressure level. |
+ if (old_pressure == current_memory_pressure_level_ && |
+ ++moderate_pressure_repeat_count_ < kModerateMemoryPressureCooldown) { |
+ return; |
+ } |
+ |
+ // Reset the moderate pressure cooldown counter and emit a moderate pressure |
+ // event. |
+ moderate_pressure_repeat_count_ = 0; |
+ MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); |
+} |
+ |
+void MemoryPressureMonitorWin::CheckMemoryPressureAndRecordStatistics() { |
+ CheckMemoryPressure(); |
+ |
+ UMA_HISTOGRAM_ENUMERATION( |
+ "Memory.PressureLevel", |
+ MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_), |
+ UMA_MEMORY_PRESSURE_LEVEL_COUNT); |
+} |
+ |
+MemoryPressureListener::MemoryPressureLevel |
+MemoryPressureMonitorWin::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. |
+ int virt_load = |
+ 100 - 100 * mem_status.ullAvailVirtual / mem_status.ullTotalVirtual; |
grt (UTC plus 2)
2015/05/05 15:33:59
do you want to explicitly compute the ratio using
chrisha
2015/05/05 19:46:12
Err, good catch, for 64-bit systems this can most
|
+ |
+ // The amount of virtual memory that is actively available for use right |
+ // now, in MBs. |
+ int virt_free = static_cast<int>(mem_status.ullAvailVirtual / 1024 / 1024); |
+ |
+ // 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 || |
+ virt_free < kModerateThresholdMb) { |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
+ } |
+ |
+ // No memory pressure was detected. |
+ return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
+} |
+ |
+bool MemoryPressureMonitorWin::GetSystemMemoryStatus( |
+ _MEMORYSTATUSEX* mem_status) { |
+ DCHECK(mem_status != nullptr); |
+ mem_status->dwLength = sizeof(_MEMORYSTATUSEX); |
+ if (!::GlobalMemoryStatusEx(mem_status)) |
+ return false; |
+ return true; |
+} |
+ |
+} // namespace base |