| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // available memory, paging until that is the case. | 75 // available memory, paging until that is the case. |
| 76 const int MemoryPressureMonitor::kLargeMemoryDefaultModerateThresholdMb = 1000; | 76 const int MemoryPressureMonitor::kLargeMemoryDefaultModerateThresholdMb = 1000; |
| 77 const int MemoryPressureMonitor::kLargeMemoryDefaultCriticalThresholdMb = 400; | 77 const int MemoryPressureMonitor::kLargeMemoryDefaultCriticalThresholdMb = 400; |
| 78 | 78 |
| 79 MemoryPressureMonitor::MemoryPressureMonitor() | 79 MemoryPressureMonitor::MemoryPressureMonitor() |
| 80 : moderate_threshold_mb_(0), | 80 : moderate_threshold_mb_(0), |
| 81 critical_threshold_mb_(0), | 81 critical_threshold_mb_(0), |
| 82 current_memory_pressure_level_( | 82 current_memory_pressure_level_( |
| 83 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 83 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 84 moderate_pressure_repeat_count_(0), | 84 moderate_pressure_repeat_count_(0), |
| 85 dispatch_callback_( |
| 86 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)), |
| 85 weak_ptr_factory_(this) { | 87 weak_ptr_factory_(this) { |
| 86 InferThresholds(); | 88 InferThresholds(); |
| 87 StartObserving(); | 89 StartObserving(); |
| 88 } | 90 } |
| 89 | 91 |
| 90 MemoryPressureMonitor::MemoryPressureMonitor(int moderate_threshold_mb, | 92 MemoryPressureMonitor::MemoryPressureMonitor(int moderate_threshold_mb, |
| 91 int critical_threshold_mb) | 93 int critical_threshold_mb) |
| 92 : moderate_threshold_mb_(moderate_threshold_mb), | 94 : moderate_threshold_mb_(moderate_threshold_mb), |
| 93 critical_threshold_mb_(critical_threshold_mb), | 95 critical_threshold_mb_(critical_threshold_mb), |
| 94 current_memory_pressure_level_( | 96 current_memory_pressure_level_( |
| 95 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 97 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 96 moderate_pressure_repeat_count_(0), | 98 moderate_pressure_repeat_count_(0), |
| 99 dispatch_callback_( |
| 100 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)), |
| 97 weak_ptr_factory_(this) { | 101 weak_ptr_factory_(this) { |
| 98 DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_); | 102 DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_); |
| 99 DCHECK_LE(0, critical_threshold_mb_); | 103 DCHECK_LE(0, critical_threshold_mb_); |
| 100 StartObserving(); | 104 StartObserving(); |
| 101 } | 105 } |
| 102 | 106 |
| 103 MemoryPressureMonitor::~MemoryPressureMonitor() { | 107 MemoryPressureMonitor::~MemoryPressureMonitor() { |
| 104 StopObserving(); | 108 StopObserving(); |
| 105 } | 109 } |
| 106 | 110 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 break; | 195 break; |
| 192 } | 196 } |
| 193 | 197 |
| 194 if (!notify) | 198 if (!notify) |
| 195 return; | 199 return; |
| 196 | 200 |
| 197 // Emit a notification of the current memory pressure level. This can only | 201 // Emit a notification of the current memory pressure level. This can only |
| 198 // happen for moderate and critical pressure levels. | 202 // happen for moderate and critical pressure levels. |
| 199 DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | 203 DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
| 200 current_memory_pressure_level_); | 204 current_memory_pressure_level_); |
| 201 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); | 205 dispatch_callback_.Run(current_memory_pressure_level_); |
| 202 } | 206 } |
| 203 | 207 |
| 204 void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() { | 208 void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() { |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); | 209 DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 | 210 |
| 207 CheckMemoryPressure(); | 211 CheckMemoryPressure(); |
| 208 | 212 |
| 209 UMA_HISTOGRAM_ENUMERATION( | 213 UMA_HISTOGRAM_ENUMERATION( |
| 210 "Memory.PressureLevel", | 214 "Memory.PressureLevel", |
| 211 MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_), | 215 MemoryPressureLevelToUmaEnumValue(current_memory_pressure_level_), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 247 |
| 244 bool MemoryPressureMonitor::GetSystemMemoryStatus( | 248 bool MemoryPressureMonitor::GetSystemMemoryStatus( |
| 245 MEMORYSTATUSEX* mem_status) { | 249 MEMORYSTATUSEX* mem_status) { |
| 246 DCHECK(mem_status != nullptr); | 250 DCHECK(mem_status != nullptr); |
| 247 mem_status->dwLength = sizeof(*mem_status); | 251 mem_status->dwLength = sizeof(*mem_status); |
| 248 if (!::GlobalMemoryStatusEx(mem_status)) | 252 if (!::GlobalMemoryStatusEx(mem_status)) |
| 249 return false; | 253 return false; |
| 250 return true; | 254 return true; |
| 251 } | 255 } |
| 252 | 256 |
| 257 void MemoryPressureMonitor::SetDispatchCallback( |
| 258 const DispatchCallback& callback) { |
| 259 dispatch_callback_ = callback; |
| 260 } |
| 261 |
| 253 } // namespace win | 262 } // namespace win |
| 254 } // namespace base | 263 } // namespace base |
| OLD | NEW |