| 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.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" |
| 11 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
| 12 #include "base/process/process_metrics.h" | 12 #include "base/process/process_metrics.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 namespace chromeos { | |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 // The time between memory pressure checks. While under critical pressure, this | 21 // The time between memory pressure checks. While under critical pressure, this |
| 23 // is also the timer to repeat cleanup attempts. | 22 // is also the timer to repeat cleanup attempts. |
| 24 const int kMemoryPressureIntervalMs = 1000; | 23 const int kMemoryPressureIntervalMs = 1000; |
| 25 | 24 |
| 26 // The time which should pass between two moderate memory pressure calls. | 25 // The time which should pass between two moderate memory pressure calls. |
| 27 const int kModerateMemoryPressureCooldownMs = 10000; | 26 const int kModerateMemoryPressureCooldownMs = 10000; |
| 28 | 27 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 MemoryPressureThresholds thresholds) | 105 MemoryPressureThresholds thresholds) |
| 107 : current_memory_pressure_level_( | 106 : current_memory_pressure_level_( |
| 108 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 107 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 109 moderate_pressure_repeat_count_(0), | 108 moderate_pressure_repeat_count_(0), |
| 110 moderate_pressure_threshold_percent_( | 109 moderate_pressure_threshold_percent_( |
| 111 GetModerateMemoryThresholdInPercent(thresholds)), | 110 GetModerateMemoryThresholdInPercent(thresholds)), |
| 112 critical_pressure_threshold_percent_( | 111 critical_pressure_threshold_percent_( |
| 113 GetCriticalMemoryThresholdInPercent(thresholds)), | 112 GetCriticalMemoryThresholdInPercent(thresholds)), |
| 114 low_mem_file_(HANDLE_EINTR(::open(kLowMemFile, O_RDONLY))), | 113 low_mem_file_(HANDLE_EINTR(::open(kLowMemFile, O_RDONLY))), |
| 115 weak_ptr_factory_(this) { | 114 weak_ptr_factory_(this) { |
| 115 DCHECK(!g_monitor); |
| 116 g_monitor = this; |
| 117 |
| 116 StartObserving(); | 118 StartObserving(); |
| 117 LOG_IF(ERROR, !low_mem_file_.is_valid()) << "Cannot open kernel listener"; | 119 LOG_IF(ERROR, !low_mem_file_.is_valid()) << "Cannot open kernel listener"; |
| 118 } | 120 } |
| 119 | 121 |
| 120 MemoryPressureMonitor::~MemoryPressureMonitor() { | 122 MemoryPressureMonitor::~MemoryPressureMonitor() { |
| 123 DCHECK(g_monitor); |
| 124 g_monitor = nullptr; |
| 125 |
| 121 StopObserving(); | 126 StopObserving(); |
| 122 } | 127 } |
| 123 | 128 |
| 124 void MemoryPressureMonitor::ScheduleEarlyCheck() { | 129 void MemoryPressureMonitor::ScheduleEarlyCheck() { |
| 125 ThreadTaskRunnerHandle::Get()->PostTask( | 130 ThreadTaskRunnerHandle::Get()->PostTask( |
| 126 FROM_HERE, Bind(&MemoryPressureMonitor::CheckMemoryPressure, | 131 FROM_HERE, Bind(&MemoryPressureMonitor::CheckMemoryPressure, |
| 127 weak_ptr_factory_.GetWeakPtr())); | 132 weak_ptr_factory_.GetWeakPtr())); |
| 128 } | 133 } |
| 129 | 134 |
| 130 MemoryPressureListener::MemoryPressureLevel | 135 MemoryPressureListener::MemoryPressureLevel |
| 131 MemoryPressureMonitor::GetCurrentPressureLevel() const { | 136 MemoryPressureMonitor::GetCurrentPressureLevel() const { |
| 132 return current_memory_pressure_level_; | 137 return current_memory_pressure_level_; |
| 133 } | 138 } |
| 134 | 139 |
| 135 // static | |
| 136 MemoryPressureMonitor* MemoryPressureMonitor::Get() { | |
| 137 return static_cast<MemoryPressureMonitor*>( | |
| 138 base::MemoryPressureMonitor::Get()); | |
| 139 } | |
| 140 | |
| 141 void MemoryPressureMonitor::StartObserving() { | 140 void MemoryPressureMonitor::StartObserving() { |
| 142 timer_.Start(FROM_HERE, | 141 timer_.Start(FROM_HERE, |
| 143 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), | 142 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), |
| 144 Bind(&MemoryPressureMonitor:: | 143 Bind(&MemoryPressureMonitor:: |
| 145 CheckMemoryPressureAndRecordStatistics, | 144 CheckMemoryPressureAndRecordStatistics, |
| 146 weak_ptr_factory_.GetWeakPtr())); | 145 weak_ptr_factory_.GetWeakPtr())); |
| 147 } | 146 } |
| 148 | 147 |
| 149 void MemoryPressureMonitor::StopObserving() { | 148 void MemoryPressureMonitor::StopObserving() { |
| 150 // If StartObserving failed, StopObserving will still get called. | 149 // If StartObserving failed, StopObserving will still get called. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 | 260 |
| 262 // Available memory is the sum of free, swap and easy reclaimable memory. | 261 // Available memory is the sum of free, swap and easy reclaimable memory. |
| 263 int available_memory = | 262 int available_memory = |
| 264 info.free + info.swap_free / kSwapWeight + file_memory; | 263 info.free + info.swap_free / kSwapWeight + file_memory; |
| 265 | 264 |
| 266 DCHECK(available_memory < total_memory); | 265 DCHECK(available_memory < total_memory); |
| 267 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 266 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
| 268 return percentage; | 267 return percentage; |
| 269 } | 268 } |
| 270 | 269 |
| 271 } // namespace chromeos | |
| 272 } // namespace base | 270 } // namespace base |
| OLD | NEW |