Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/win/memory_pressure_monitor.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 | |
| 9 #include "base/metrics/histogram_macros.h" | |
| 10 #include "base/single_thread_task_runner.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "base/time/time.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace win { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // TODO(chrisha): Explore the following constants further with an experiment. | |
| 20 | |
| 21 // Moderate and critical memory load percentages. These percentages are applied | |
| 22 // to both system memory and virtual memory. | |
| 23 const int kModerateThresholdPercent = 60; | |
|
brucedawson
2015/05/06 18:44:37
I'm not sure that the threshold percentages make s
| |
| 24 const int kCriticalThresholdPercent = 95; | |
| 25 | |
| 26 // Absolute system memory requirements. These are modeled after typical renderer | |
| 27 // sizes on Windows sytems (rounded to the neared 10 MB). These are applied to | |
| 28 // system memory only. Intended as a lower bound to the above percentages on | |
| 29 // low-end systems. Taken from the Memory.Renderer UMA statistic. | |
| 30 const int kModerateThresholdMb = 300; // 95th percentile renderer size. | |
| 31 const int kCriticalThresholdMb = 60; // 50th percentile renderer size. | |
|
brucedawson
2015/05/06 18:44:37
I'm concerned about these values. The description
| |
| 32 | |
| 33 // Enumeration of UMA memory pressure levels. This needs to be kept in sync with | |
| 34 // histograms.xml and the memory pressure levels defined in | |
| 35 // MemoryPressureListener. | |
| 36 enum MemoryPressureLevelUMA { | |
| 37 UMA_MEMORY_PRESSURE_LEVEL_NONE = 0, | |
| 38 UMA_MEMORY_PRESSURE_LEVEL_MODERATE = 1, | |
| 39 UMA_MEMORY_PRESSURE_LEVEL_CRITICAL = 2, | |
| 40 // This must be the last value in the enum. | |
| 41 UMA_MEMORY_PRESSURE_LEVEL_COUNT, | |
| 42 }; | |
| 43 | |
| 44 // Converts a memory pressure level to an UMA enumeration value. | |
| 45 MemoryPressureLevelUMA MemoryPressureLevelToUmaEnumValue( | |
| 46 MemoryPressureListener::MemoryPressureLevel level) { | |
| 47 switch (level) { | |
| 48 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | |
| 49 return UMA_MEMORY_PRESSURE_LEVEL_NONE; | |
| 50 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | |
| 51 return UMA_MEMORY_PRESSURE_LEVEL_MODERATE; | |
| 52 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | |
| 53 return UMA_MEMORY_PRESSURE_LEVEL_CRITICAL; | |
| 54 } | |
| 55 NOTREACHED(); | |
| 56 return UMA_MEMORY_PRESSURE_LEVEL_NONE; | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 // The following constants have been lifted from similar values in the ChromeOS | |
| 62 // memory pressure monitor. The values were determined experimentally to ensure | |
| 63 // sufficient responsiveness of the memory pressure subsystem, and minimal | |
| 64 // overhead. | |
| 65 const int MemoryPressureMonitor::kPollingIntervalMs = 1000; | |
| 66 const int MemoryPressureMonitor::kModeratePressureCooldownMs = 10000; | |
| 67 const int MemoryPressureMonitor::kModeratePressureCooldownCycles = | |
| 68 kModeratePressureCooldownMs / kPollingIntervalMs; | |
| 69 | |
| 70 MemoryPressureMonitor::MemoryPressureMonitor() | |
| 71 : current_memory_pressure_level_( | |
| 72 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | |
| 73 moderate_pressure_repeat_count_(0), | |
| 74 weak_ptr_factory_(this) { | |
| 75 StartObserving(); | |
| 76 } | |
| 77 | |
| 78 MemoryPressureMonitor::~MemoryPressureMonitor() { | |
| 79 StopObserving(); | |
| 80 } | |
| 81 | |
| 82 void MemoryPressureMonitor::CheckMemoryPressureSoon() { | |
| 83 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 84 | |
| 85 ThreadTaskRunnerHandle::Get()->PostTask( | |
| 86 FROM_HERE, Bind(&MemoryPressureMonitor::CheckMemoryPressure, | |
| 87 weak_ptr_factory_.GetWeakPtr())); | |
| 88 } | |
| 89 | |
| 90 MemoryPressureListener::MemoryPressureLevel | |
| 91 MemoryPressureMonitor::GetCurrentPressureLevel() const { | |
| 92 return current_memory_pressure_level_; | |
| 93 } | |
| 94 | |
| 95 void MemoryPressureMonitor::StartObserving() { | |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 97 | |
| 98 timer_.Start(FROM_HERE, | |
| 99 TimeDelta::FromMilliseconds(kPollingIntervalMs), | |
| 100 Bind(&MemoryPressureMonitor:: | |
| 101 CheckMemoryPressureAndRecordStatistics, | |
| 102 weak_ptr_factory_.GetWeakPtr())); | |
| 103 } | |
| 104 | |
| 105 void MemoryPressureMonitor::StopObserving() { | |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 107 | |
| 108 // If StartObserving failed, StopObserving will still get called. | |
| 109 timer_.Stop(); | |
| 110 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 111 } | |
| 112 | |
| 113 void MemoryPressureMonitor::CheckMemoryPressure() { | |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 115 | |
| 116 // Get the previous pressure level and update the current one. | |
| 117 MemoryPressureLevel old_pressure = current_memory_pressure_level_; | |
| 118 current_memory_pressure_level_ = CalculateCurrentPressureLevel(); | |
| 119 | |
| 120 // |notify| will be set to true if MemoryPressureListeners need to be | |
| 121 // notified of a memory pressure level state change. | |
| 122 bool notify = false; | |
| 123 switch (current_memory_pressure_level_) { | |
| 124 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | |
| 125 break; | |
| 126 | |
| 127 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | |
| 128 if (old_pressure != current_memory_pressure_level_) { | |
| 129 // This is a new transition to moderate pressure so notify. | |
| 130 moderate_pressure_repeat_count_ = 0; | |
| 131 notify = true; | |
| 132 } else { | |
| 133 // Already in moderate pressure, only notify if sustained over the | |
| 134 // cooldown period. | |
| 135 if (++moderate_pressure_repeat_count_ == | |
| 136 kModeratePressureCooldownCycles) { | |
| 137 moderate_pressure_repeat_count_ = 0; | |
| 138 notify = true; | |
| 139 } | |
| 140 } | |
| 141 break; | |
| 142 | |
| 143 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | |
| 144 // Always notify of critical pressure levels. | |
| 145 notify = true; | |
| 146 break; | |
| 147 } | |
| 148 | |
| 149 if (!notify) | |
| 150 return; | |
| 151 | |
| 152 // Emit a notification of the current memory pressure level. This can only | |
| 153 // happen for moderate and critical pressure levels. | |
| 154 DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | |
| 155 current_memory_pressure_level_); | |
| 156 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); | |
| 157 } | |
| 158 | |
| 159 void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() { | |
| 160 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 161 | |
| 162 CheckMemoryPressure(); | |
| 163 | |
| 164 UMA_HISTOGRAM_ENUMERATION( | |
| 165 "Memory.PressureLevel", | |
| 166 MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_), | |
| 167 UMA_MEMORY_PRESSURE_LEVEL_COUNT); | |
| 168 } | |
| 169 | |
| 170 MemoryPressureListener::MemoryPressureLevel | |
| 171 MemoryPressureMonitor::CalculateCurrentPressureLevel() { | |
| 172 MEMORYSTATUSEX mem_status = {}; | |
| 173 if (!GetSystemMemoryStatus(&mem_status)) | |
| 174 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
| 175 | |
| 176 // How much of the system's physical memory is actively being used by | |
| 177 // pages that are in the working sets of running programs. | |
| 178 int phys_load = mem_status.dwMemoryLoad; | |
| 179 | |
| 180 // How much system memory is actively available for use right now, in MBs. | |
| 181 static const DWORDLONG kMBBytes = 1024 * 1024; | |
| 182 int phys_free = static_cast<int>(mem_status.ullAvailPhys / kMBBytes); | |
| 183 | |
| 184 // Determine if the physical memory is under critical memory pressure. | |
| 185 if (phys_load > kCriticalThresholdPercent || | |
| 186 phys_free < kCriticalThresholdMb) { | |
| 187 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | |
| 188 } | |
| 189 | |
| 190 // On 64-bit platforms there is little value in calculating virtual memory | |
| 191 // pressure as it is at least 3 orders of magnitude larger than system memory. | |
| 192 #if !defined(ARCH_CPU_64_BITS) | |
| 193 // The virtual load of the current process. This is the percentage of virtual | |
| 194 // memory that is reserved or committed. | |
| 195 int virt_load = 100 - static_cast<int>( | |
| 196 100 * mem_status.ullAvailVirtual / mem_status.ullTotalVirtual); | |
| 197 | |
| 198 // Determine if the virtual memory is under critical memory pressure. | |
| 199 if (virt_load > kCriticalThresholdPercent) | |
| 200 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | |
| 201 | |
| 202 // Determine if the virtual memory is under moderate memory pressure. | |
| 203 if (virt_load > kModerateThresholdPercent) | |
| 204 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; | |
| 205 #endif // !defined(ARCH_CPU_64_BITS) | |
| 206 | |
| 207 // Determine if the physical memory is under moderate memory pressure. This | |
| 208 // is done after the virtual memory checking to give that a chance to emit a | |
| 209 // critical pressure signal. | |
| 210 if (phys_load > kModerateThresholdPercent || | |
| 211 phys_free < kModerateThresholdMb) { | |
| 212 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; | |
| 213 } | |
| 214 | |
| 215 // No memory pressure was detected. | |
| 216 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
| 217 } | |
| 218 | |
| 219 bool MemoryPressureMonitor::GetSystemMemoryStatus( | |
| 220 MEMORYSTATUSEX* mem_status) { | |
| 221 DCHECK(mem_status != nullptr); | |
| 222 mem_status->dwLength = sizeof(MEMORYSTATUSEX); | |
| 223 if (!::GlobalMemoryStatusEx(mem_status)) | |
| 224 return false; | |
| 225 return true; | |
| 226 } | |
| 227 | |
| 228 } // namespace win | |
| 229 } // namespace base | |
| OLD | NEW |