| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/memory_pressure_monitor_win.h" | 5 #include "base/memory/memory_pressure_monitor_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 StartObserving(); | 87 StartObserving(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 MemoryPressureMonitor::MemoryPressureMonitor(int moderate_threshold_mb, | 90 MemoryPressureMonitor::MemoryPressureMonitor(int moderate_threshold_mb, |
| 91 int critical_threshold_mb) | 91 int critical_threshold_mb) |
| 92 : moderate_threshold_mb_(moderate_threshold_mb), | 92 : moderate_threshold_mb_(moderate_threshold_mb), |
| 93 critical_threshold_mb_(critical_threshold_mb), | 93 critical_threshold_mb_(critical_threshold_mb), |
| 94 current_memory_pressure_level_( | 94 current_memory_pressure_level_( |
| 95 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 95 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 96 moderate_pressure_repeat_count_(0), | 96 moderate_pressure_repeat_count_(0), |
| 97 dispatch_callback_( |
| 98 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)), |
| 97 weak_ptr_factory_(this) { | 99 weak_ptr_factory_(this) { |
| 98 DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_); | 100 DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_); |
| 99 DCHECK_LE(0, critical_threshold_mb_); | 101 DCHECK_LE(0, critical_threshold_mb_); |
| 100 StartObserving(); | 102 StartObserving(); |
| 101 } | 103 } |
| 102 | 104 |
| 103 MemoryPressureMonitor::~MemoryPressureMonitor() { | 105 MemoryPressureMonitor::~MemoryPressureMonitor() { |
| 104 StopObserving(); | 106 StopObserving(); |
| 105 } | 107 } |
| 106 | 108 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 break; | 193 break; |
| 192 } | 194 } |
| 193 | 195 |
| 194 if (!notify) | 196 if (!notify) |
| 195 return; | 197 return; |
| 196 | 198 |
| 197 // Emit a notification of the current memory pressure level. This can only | 199 // Emit a notification of the current memory pressure level. This can only |
| 198 // happen for moderate and critical pressure levels. | 200 // happen for moderate and critical pressure levels. |
| 199 DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | 201 DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
| 200 current_memory_pressure_level_); | 202 current_memory_pressure_level_); |
| 201 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); | 203 dispatch_callback_.Run(current_memory_pressure_level_); |
| 202 } | 204 } |
| 203 | 205 |
| 204 void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() { | 206 void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() { |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); | 207 DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 | 208 |
| 207 CheckMemoryPressure(); | 209 CheckMemoryPressure(); |
| 208 | 210 |
| 209 UMA_HISTOGRAM_ENUMERATION( | 211 UMA_HISTOGRAM_ENUMERATION( |
| 210 "Memory.PressureLevel", | 212 "Memory.PressureLevel", |
| 211 MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_), | 213 MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 245 |
| 244 bool MemoryPressureMonitor::GetSystemMemoryStatus( | 246 bool MemoryPressureMonitor::GetSystemMemoryStatus( |
| 245 MEMORYSTATUSEX* mem_status) { | 247 MEMORYSTATUSEX* mem_status) { |
| 246 DCHECK(mem_status != nullptr); | 248 DCHECK(mem_status != nullptr); |
| 247 mem_status->dwLength = sizeof(*mem_status); | 249 mem_status->dwLength = sizeof(*mem_status); |
| 248 if (!::GlobalMemoryStatusEx(mem_status)) | 250 if (!::GlobalMemoryStatusEx(mem_status)) |
| 249 return false; | 251 return false; |
| 250 return true; | 252 return true; |
| 251 } | 253 } |
| 252 | 254 |
| 255 void MemoryPressureMonitor::SetDispatchCallback( |
| 256 const DispatchCallback& callback) { |
| 257 dispatch_callback_ = callback; |
| 258 } |
| 259 |
| 253 } // namespace win | 260 } // namespace win |
| 254 } // namespace base | 261 } // namespace base |
| OLD | NEW |