| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_chromeos.h" | 5 #include "base/memory/memory_pressure_monitor_chromeos.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/select.h> | 8 #include <sys/select.h> |
| 9 | 9 |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 MemoryPressureMonitor::MemoryPressureMonitor( | 106 MemoryPressureMonitor::MemoryPressureMonitor( |
| 107 MemoryPressureThresholds thresholds) | 107 MemoryPressureThresholds thresholds) |
| 108 : current_memory_pressure_level_( | 108 : current_memory_pressure_level_( |
| 109 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 109 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 110 moderate_pressure_repeat_count_(0), | 110 moderate_pressure_repeat_count_(0), |
| 111 moderate_pressure_threshold_percent_( | 111 moderate_pressure_threshold_percent_( |
| 112 GetModerateMemoryThresholdInPercent(thresholds)), | 112 GetModerateMemoryThresholdInPercent(thresholds)), |
| 113 critical_pressure_threshold_percent_( | 113 critical_pressure_threshold_percent_( |
| 114 GetCriticalMemoryThresholdInPercent(thresholds)), | 114 GetCriticalMemoryThresholdInPercent(thresholds)), |
| 115 low_mem_file_(HANDLE_EINTR(::open(kLowMemFile, O_RDONLY))), | 115 low_mem_file_(HANDLE_EINTR(::open(kLowMemFile, O_RDONLY))), |
| 116 dispatch_callback_( |
| 117 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)), |
| 116 weak_ptr_factory_(this) { | 118 weak_ptr_factory_(this) { |
| 117 StartObserving(); | 119 StartObserving(); |
| 118 LOG_IF(ERROR, | 120 LOG_IF(ERROR, |
| 119 base::SysInfo::IsRunningOnChromeOS() && !low_mem_file_.is_valid()) | 121 base::SysInfo::IsRunningOnChromeOS() && !low_mem_file_.is_valid()) |
| 120 << "Cannot open kernel listener"; | 122 << "Cannot open kernel listener"; |
| 121 } | 123 } |
| 122 | 124 |
| 123 MemoryPressureMonitor::~MemoryPressureMonitor() { | 125 MemoryPressureMonitor::~MemoryPressureMonitor() { |
| 124 StopObserving(); | 126 StopObserving(); |
| 125 } | 127 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 } else if (current_memory_pressure_level_ == | 225 } else if (current_memory_pressure_level_ == |
| 224 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE && | 226 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE && |
| 225 old_pressure == | 227 old_pressure == |
| 226 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { | 228 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| 227 // When we reducing the pressure level from critical to moderate, we | 229 // When we reducing the pressure level from critical to moderate, we |
| 228 // restart the timeout and do not send another notification. | 230 // restart the timeout and do not send another notification. |
| 229 moderate_pressure_repeat_count_ = 0; | 231 moderate_pressure_repeat_count_ = 0; |
| 230 return; | 232 return; |
| 231 } | 233 } |
| 232 moderate_pressure_repeat_count_ = 0; | 234 moderate_pressure_repeat_count_ = 0; |
| 233 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); | 235 dispatch_callback_.Run(current_memory_pressure_level_); |
| 234 } | 236 } |
| 235 | 237 |
| 236 // Gets the used ChromeOS memory in percent. | 238 // Gets the used ChromeOS memory in percent. |
| 237 int MemoryPressureMonitor::GetUsedMemoryInPercent() { | 239 int MemoryPressureMonitor::GetUsedMemoryInPercent() { |
| 238 base::SystemMemoryInfoKB info; | 240 base::SystemMemoryInfoKB info; |
| 239 if (!base::GetSystemMemoryInfo(&info)) { | 241 if (!base::GetSystemMemoryInfo(&info)) { |
| 240 VLOG(1) << "Cannot determine the free memory of the system."; | 242 VLOG(1) << "Cannot determine the free memory of the system."; |
| 241 return 0; | 243 return 0; |
| 242 } | 244 } |
| 243 // TODO(skuhne): Instead of adding the kernel memory pressure calculation | 245 // TODO(skuhne): Instead of adding the kernel memory pressure calculation |
| (...skipping 20 matching lines...) Expand all Loading... |
| 264 | 266 |
| 265 // Available memory is the sum of free, swap and easy reclaimable memory. | 267 // Available memory is the sum of free, swap and easy reclaimable memory. |
| 266 int available_memory = | 268 int available_memory = |
| 267 info.free + info.swap_free / kSwapWeight + file_memory; | 269 info.free + info.swap_free / kSwapWeight + file_memory; |
| 268 | 270 |
| 269 DCHECK(available_memory < total_memory); | 271 DCHECK(available_memory < total_memory); |
| 270 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 272 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
| 271 return percentage; | 273 return percentage; |
| 272 } | 274 } |
| 273 | 275 |
| 276 void MemoryPressureMonitor::SetDispatchCallback( |
| 277 const DispatchCallback& callback) { |
| 278 dispatch_callback_ = callback; |
| 279 } |
| 280 |
| 274 } // namespace chromeos | 281 } // namespace chromeos |
| 275 } // namespace base | 282 } // namespace base |
| OLD | NEW |