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

Side by Side Diff: base/win/memory_pressure_monitor.cc

Issue 1122863005: Create base::win::MemoryPressureMonitor class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed thakis@ and brucedawson@ comments. Created 5 years, 7 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 unified diff | Download patch
OLDNEW
(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;
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.
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 ThreadTaskRunnerHandle::Get()->PostTask(
84 FROM_HERE, Bind(&MemoryPressureMonitor::CheckMemoryPressure,
85 weak_ptr_factory_.GetWeakPtr()));
86 }
87
88 MemoryPressureListener::MemoryPressureLevel
89 MemoryPressureMonitor::GetCurrentPressureLevel() const {
90 return current_memory_pressure_level_;
91 }
92
93 void MemoryPressureMonitor::StartObserving() {
94 timer_.Start(FROM_HERE,
95 TimeDelta::FromMilliseconds(kPollingIntervalMs),
96 Bind(&MemoryPressureMonitor::
97 CheckMemoryPressureAndRecordStatistics,
98 weak_ptr_factory_.GetWeakPtr()));
99 }
100
101 void MemoryPressureMonitor::StopObserving() {
102 // If StartObserving failed, StopObserving will still get called.
103 timer_.Stop();
104 weak_ptr_factory_.InvalidateWeakPtrs();
105 }
106
107 void MemoryPressureMonitor::CheckMemoryPressure() {
108 // |notify| will be set to true if MemoryPressureListeners need to be
109 // notified of a memory pressure level state change.
110 bool notify = false;
111
112 base::AutoLock scoped_lock(lock_);
113 MemoryPressureLevel old_pressure = current_memory_pressure_level_;
114 current_memory_pressure_level_ = CalculateCurrentPressureLevel();
115
116 switch (current_memory_pressure_level_) {
117 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
118 break;
119
120 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: {
121 if (old_pressure != current_memory_pressure_level_) {
122 // This is a new transition to moderate pressure so notify.
123 moderate_pressure_repeat_count_ = 0;
124 notify = true;
125 } else {
126 // Already in moderate pressure, only notify if sustained over the
127 // cooldown period.
128 if (++moderate_pressure_repeat_count_ ==
129 kModeratePressureCooldownCycles) {
130 moderate_pressure_repeat_count_ = 0;
131 notify = true;
132 }
133 }
134 } break;
135
136 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: {
137 // Always notify of critical pressure levels.
138 notify = true;
139 } break;
140 }
141
142 if (!notify)
143 return;
144
145 // Emit a notification of the current memory pressure level. This can only
146 // happen for moderate and critical pressure levels.
147 DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
148 current_memory_pressure_level_);
149 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_);
grt (UTC plus 2) 2015/05/06 13:26:36 this is called while the lock is held. is this nee
chrisha 2015/05/06 15:09:57 No longer applicable, lock removed.
150 }
151
152 void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() {
153 CheckMemoryPressure();
154
155 UMA_HISTOGRAM_ENUMERATION(
156 "Memory.PressureLevel",
157 MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_),
158 UMA_MEMORY_PRESSURE_LEVEL_COUNT);
159 }
160
161 MemoryPressureListener::MemoryPressureLevel
162 MemoryPressureMonitor::CalculateCurrentPressureLevel() {
163 MEMORYSTATUSEX mem_status = {};
164 if (!GetSystemMemoryStatus(&mem_status))
165 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
166
167 // How much of the system's physical memory is actively being used by
168 // pages that are in the working sets of running programs.
169 int phys_load = mem_status.dwMemoryLoad;
170
171 // How much system memory is actively available for use right now, in MBs.
172 static DWORDLONG kMBBytes = 1024 * 1024;
grt (UTC plus 2) 2015/05/06 13:26:36 static const
chrisha 2015/05/06 15:09:57 Done.
173 int phys_free = static_cast<int>(mem_status.ullAvailPhys / kMBBytes);
174
175 // Determine if the physical memory is under critical memory pressure.
176 if (phys_load > kCriticalThresholdPercent ||
177 phys_free < kCriticalThresholdMb) {
178 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
179 }
180
181 // On 64-bit platforms there is little value in calculating virtual memory
182 // pressure as it is at least 3 orders of magnitude larger than system
grt (UTC plus 2) 2015/05/06 13:26:36 is "system" the end of the sentence? please add a
chrisha 2015/05/06 15:09:57 Done.
183 #if !defined(ARCH_CPU_64_BITS)
184 // The virtual load of the current process. This is the percentage of virtual
185 // memory that is reserved or committed.
186 int virt_load = 100 - static_cast<int>(
187 100 * mem_status.ullAvailVirtual / mem_status.ullTotalVirtual);
188
189 // Determine if the virtual memory is under critical memory pressure.
190 if (virt_load > kCriticalThresholdPercent)
191 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
192
193 // Determine if the virtual memory is under moderate memory pressure.
194 if (virt_load > kModerateThresholdPercent)
195 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
196 #endif // !defined(ARCH_CPU_64_BITS)
197
198 // Determine if the physical memory is under moderate memory pressure. This
199 // is done after the virtual memory checking to give that a chance to emit a
200 // critical pressure signal.
201 if (phys_load > kModerateThresholdPercent ||
202 phys_free < kModerateThresholdMb) {
203 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
204 }
205
206 // No memory pressure was detected.
207 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
208 }
209
210 bool MemoryPressureMonitor::GetSystemMemoryStatus(
211 MEMORYSTATUSEX* mem_status) {
212 DCHECK(mem_status != nullptr);
213 mem_status->dwLength = sizeof(MEMORYSTATUSEX);
214 if (!::GlobalMemoryStatusEx(mem_status))
215 return false;
216 return true;
217 }
218
219 } // namespace win
220 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698