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

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

Issue 1122863005: Create base::win::MemoryPressureMonitor class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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_win.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
16 namespace {
17
18 // The time between memory pressure checks. While under critical pressure, this
19 // is also the timer to repeat cleanup attempts.
20 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.
21
22 // The time which should pass between two successive moderate memory pressure
23 // calls.
24 const int kModerateMemoryPressureCooldownMs = 10000;
25
26 // Number of event polls before the next moderate pressure event can be sent.
27 const int kModerateMemoryPressureCooldown =
28 kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs;
29
30 // Moderate and critical memory load percentages. These percentages are applied
31 // to both system memory and virtual memory.
32 const int kModerateThresholdPercent = 60;
33 const int kCriticalThresholdPercent = 95;
34
35 // Absolute memory requirements. These are modeled after typical renderer sizes
36 // on Windows sytems (rounded to the neared 10 MB).
37 const int kModerateThresholdMb = 300; // 95th percentile renderer.
38 const int kCriticalThresholdMb = 60; // 50th percentile renderer size.
39
40 // Enumeration of UMA memory pressure levels. This needs to be kept in sync with
41 // histograms.xml and the memory pressure levels defined in
42 // MemoryPressureListener.
43 enum MemoryPressureLevelUMA {
44 UMA_MEMORY_PRESSURE_LEVEL_NONE = 0,
45 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.
46 UMA_MEMORY_PRESSURE_LEVEL_CRITICAL,
47 UMA_MEMORY_PRESSURE_LEVEL_COUNT,
48 };
49
50 // Converts a memory pressure level to an UMA enumeration value.
51 MemoryPressureLevelUMA MemoryPressureLevelToUmaEnumValue(
52 MemoryPressureListener::MemoryPressureLevel level) {
53 switch (level) {
54 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
55 return UMA_MEMORY_PRESSURE_LEVEL_NONE;
56 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
57 return UMA_MEMORY_PRESSURE_LEVEL_MODERATE;
58 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
59 return UMA_MEMORY_PRESSURE_LEVEL_CRITICAL;
60 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.
61 break;
62 }
63 NOTREACHED();
64 return UMA_MEMORY_PRESSURE_LEVEL_NONE;
65 }
66
67 } // namespace
68
69 MemoryPressureMonitorWin::MemoryPressureMonitorWin()
70 : 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.
71 weak_ptr_factory_(this) {
72 StartObserving();
73 }
74
75 MemoryPressureMonitorWin::~MemoryPressureMonitorWin() {
76 StopObserving();
77 }
78
79 void MemoryPressureMonitorWin::ScheduleEarlyCheck() {
80 ThreadTaskRunnerHandle::Get()->PostTask(
81 FROM_HERE, Bind(&MemoryPressureMonitorWin::CheckMemoryPressure,
82 weak_ptr_factory_.GetWeakPtr()));
83 }
84
85 MemoryPressureListener::MemoryPressureLevel
86 MemoryPressureMonitorWin::GetCurrentPressureLevel() const {
87 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
88 }
89
90 void MemoryPressureMonitorWin::StartObserving() {
91 timer_.Start(FROM_HERE,
92 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs),
93 Bind(&MemoryPressureMonitorWin::
94 CheckMemoryPressureAndRecordStatistics,
95 weak_ptr_factory_.GetWeakPtr()));
96 }
97
98 void MemoryPressureMonitorWin::StopObserving() {
99 // If StartObserving failed, StopObserving will still get called.
100 timer_.Stop();
grt (UTC plus 2) 2015/05/05 15:33:59 weak_ptr_factory_.InvalidateWeakPtrs();
chrisha 2015/05/05 19:46:12 Done.
101 }
102
103 void MemoryPressureMonitorWin::CheckMemoryPressure() {
104 MemoryPressureLevel old_pressure = current_memory_pressure_level_;
105 current_memory_pressure_level_ = CalculateCurrentPressureLevel();
106
107 // 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.
108 if (current_memory_pressure_level_ ==
109 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) {
110 moderate_pressure_repeat_count_ = 0;
111 return;
112 }
113
114 // If the pressure is critical we *always* notify. This keeps a repeated
115 // ping going when memory pressure is very tight.
116 if (current_memory_pressure_level_ ==
117 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
118 moderate_pressure_repeat_count_ = 0;
119 MemoryPressureListener::NotifyMemoryPressure(
120 current_memory_pressure_level_);
121 return;
122 }
123
124 // At this point the memory pressure is moderate.
125 DCHECK_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
126 current_memory_pressure_level_);
127
128 // If the pressure is moderate and the cool-down period hasn't elapsed, then
129 // swallow the memory pressure level.
130 if (old_pressure == current_memory_pressure_level_ &&
131 ++moderate_pressure_repeat_count_ < kModerateMemoryPressureCooldown) {
132 return;
133 }
134
135 // Reset the moderate pressure cooldown counter and emit a moderate pressure
136 // event.
137 moderate_pressure_repeat_count_ = 0;
138 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_);
139 }
140
141 void MemoryPressureMonitorWin::CheckMemoryPressureAndRecordStatistics() {
142 CheckMemoryPressure();
143
144 UMA_HISTOGRAM_ENUMERATION(
145 "Memory.PressureLevel",
146 MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_),
147 UMA_MEMORY_PRESSURE_LEVEL_COUNT);
148 }
149
150 MemoryPressureListener::MemoryPressureLevel
151 MemoryPressureMonitorWin::CalculateCurrentPressureLevel() {
152 _MEMORYSTATUSEX mem_status = {};
153 if (!GetSystemMemoryStatus(&mem_status))
154 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
155
156 // How much of the system's physical memory is actively being used by
157 // pages that are in the working sets of running programs.
158 int phys_load = mem_status.dwMemoryLoad;
159
160 // How much system memory is actively available for use right now, in MBs.
161 int phys_free = static_cast<int>(mem_status.ullAvailPhys / 1024 / 1024);
162
163 // The virtual load of this process. This is the percentage of virtual memory
164 // that is reserved or committed.
165 int virt_load =
166 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
167
168 // The amount of virtual memory that is actively available for use right
169 // now, in MBs.
170 int virt_free = static_cast<int>(mem_status.ullAvailVirtual / 1024 / 1024);
171
172 // Determine if the system is under critical memory pressure.
173 if (phys_load > kCriticalThresholdPercent ||
174 virt_load > kCriticalThresholdPercent ||
175 phys_free < kCriticalThresholdMb ||
176 virt_free < kCriticalThresholdMb) {
177 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
178 }
179
180 // Determine if the system is under moderate memory pressure.
181 if (phys_load > kModerateThresholdPercent ||
182 virt_load > kModerateThresholdPercent ||
183 phys_free < kModerateThresholdMb ||
184 virt_free < kModerateThresholdMb) {
185 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
186 }
187
188 // No memory pressure was detected.
189 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
190 }
191
192 bool MemoryPressureMonitorWin::GetSystemMemoryStatus(
193 _MEMORYSTATUSEX* mem_status) {
194 DCHECK(mem_status != nullptr);
195 mem_status->dwLength = sizeof(_MEMORYSTATUSEX);
196 if (!::GlobalMemoryStatusEx(mem_status))
197 return false;
198 return true;
199 }
200
201 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698