| 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/chromeos/memory_pressure_observer_chromeos.h" | 5 #include "chrome/common/memory_pressure_monitor_chromeos.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/process/process_metrics.h" | 9 #include "base/process/process_metrics.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 | 11 |
| 12 namespace base { | 12 using base::MemoryPressureListener; |
| 13 using chromeos::switches::MemoryPressureThresholds; |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // The time between memory pressure checks. While under critical pressure, this | 17 // The time between memory pressure checks. While under critical pressure, this |
| 17 // is also the timer to repeat cleanup attempts. | 18 // is also the timer to repeat cleanup attempts. |
| 18 const int kMemoryPressureIntervalMs = 1000; | 19 const int kMemoryPressureIntervalMs = 1000; |
| 19 | 20 |
| 20 // The time which should pass between two moderate memory pressure calls. | 21 // The time which should pass between two moderate memory pressure calls. |
| 21 const int kModerateMemoryPressureCooldownMs = 10000; | 22 const int kModerateMemoryPressureCooldownMs = 10000; |
| 22 | 23 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 35 // updated if more memory pressure levels are introduced. | 36 // updated if more memory pressure levels are introduced. |
| 36 enum MemoryPressureLevelUMA { | 37 enum MemoryPressureLevelUMA { |
| 37 MEMORY_PRESSURE_LEVEL_NONE = 0, | 38 MEMORY_PRESSURE_LEVEL_NONE = 0, |
| 38 MEMORY_PRESSURE_LEVEL_MODERATE, | 39 MEMORY_PRESSURE_LEVEL_MODERATE, |
| 39 MEMORY_PRESSURE_LEVEL_CRITICAL, | 40 MEMORY_PRESSURE_LEVEL_CRITICAL, |
| 40 NUM_MEMORY_PRESSURE_LEVELS | 41 NUM_MEMORY_PRESSURE_LEVELS |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 // Converts a |MemoryPressureThreshold| value into a used memory percentage for | 44 // Converts a |MemoryPressureThreshold| value into a used memory percentage for |
| 44 // the moderate pressure event. | 45 // the moderate pressure event. |
| 45 int GetModerateMemoryThresholdInPercent( | 46 int GetModerateMemoryThresholdInPercent(MemoryPressureThresholds thresholds) { |
| 46 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { | 47 return thresholds == chromeos::switches::THRESHOLD_AGGRESSIVE_CACHE_DISCARD || |
| 47 return thresholds == MemoryPressureObserverChromeOS:: | 48 thresholds == chromeos::switches::THRESHOLD_AGGRESSIVE |
| 48 THRESHOLD_AGGRESSIVE_CACHE_DISCARD || | |
| 49 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE | |
| 50 ? kAggressiveMemoryPressureModerateThresholdPercent | 49 ? kAggressiveMemoryPressureModerateThresholdPercent |
| 51 : kNormalMemoryPressureModerateThresholdPercent; | 50 : kNormalMemoryPressureModerateThresholdPercent; |
| 52 } | 51 } |
| 53 | 52 |
| 54 // Converts a |MemoryPressureThreshold| value into a used memory percentage for | 53 // Converts a |MemoryPressureThreshold| value into a used memory percentage for |
| 55 // the critical pressure event. | 54 // the critical pressure event. |
| 56 int GetCriticalMemoryThresholdInPercent( | 55 int GetCriticalMemoryThresholdInPercent(MemoryPressureThresholds thresholds) { |
| 57 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { | 56 return thresholds == chromeos::switches::THRESHOLD_AGGRESSIVE_TAB_DISCARD || |
| 58 return thresholds == MemoryPressureObserverChromeOS:: | 57 thresholds == chromeos::switches::THRESHOLD_AGGRESSIVE |
| 59 THRESHOLD_AGGRESSIVE_TAB_DISCARD || | |
| 60 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE | |
| 61 ? kAggressiveMemoryPressureCriticalThresholdPercent | 58 ? kAggressiveMemoryPressureCriticalThresholdPercent |
| 62 : kNormalMemoryPressureCriticalThresholdPercent; | 59 : kNormalMemoryPressureCriticalThresholdPercent; |
| 63 } | 60 } |
| 64 | 61 |
| 65 // Converts free percent of memory into a memory pressure value. | 62 // Converts free percent of memory into a memory pressure value. |
| 66 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( | 63 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( |
| 67 int actual_fill_level, | 64 int actual_fill_level, |
| 68 int moderate_threshold, | 65 int moderate_threshold, |
| 69 int critical_threshold) { | 66 int critical_threshold) { |
| 70 if (actual_fill_level < moderate_threshold) | 67 if (actual_fill_level < moderate_threshold) |
| 71 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 68 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
| 72 return actual_fill_level < critical_threshold | 69 return actual_fill_level < critical_threshold |
| 73 ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE | 70 ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE |
| 74 : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | 71 : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
| 75 } | 72 } |
| 76 | 73 |
| 77 } // namespace | 74 } // namespace |
| 78 | 75 |
| 79 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS( | 76 MemoryPressureMonitorChromeOS::MemoryPressureMonitorChromeOS( |
| 80 MemoryPressureThresholds thresholds) | 77 MemoryPressureThresholds thresholds) |
| 81 : current_memory_pressure_level_( | 78 : current_memory_pressure_level_( |
| 82 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 79 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 83 moderate_pressure_repeat_count_(0), | 80 moderate_pressure_repeat_count_(0), |
| 84 moderate_pressure_threshold_percent_( | 81 moderate_pressure_threshold_percent_( |
| 85 GetModerateMemoryThresholdInPercent(thresholds)), | 82 GetModerateMemoryThresholdInPercent(thresholds)), |
| 86 critical_pressure_threshold_percent_( | 83 critical_pressure_threshold_percent_( |
| 87 GetCriticalMemoryThresholdInPercent(thresholds)), | 84 GetCriticalMemoryThresholdInPercent(thresholds)), |
| 88 weak_ptr_factory_(this) { | 85 weak_ptr_factory_(this) { |
| 89 StartObserving(); | 86 StartObserving(); |
| 90 } | 87 } |
| 91 | 88 |
| 92 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { | 89 MemoryPressureMonitorChromeOS::~MemoryPressureMonitorChromeOS() { |
| 93 StopObserving(); | 90 StopObserving(); |
| 94 } | 91 } |
| 95 | 92 |
| 96 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { | 93 MemoryPressureListener::MemoryPressureLevel |
| 97 MessageLoop::current()->PostTask( | 94 MemoryPressureMonitorChromeOS::GetCurrentPressureLevel() const { |
| 95 return current_memory_pressure_level_; |
| 96 } |
| 97 |
| 98 void MemoryPressureMonitorChromeOS::ScheduleEarlyCheck() { |
| 99 base::MessageLoop::current()->PostTask( |
| 98 FROM_HERE, | 100 FROM_HERE, |
| 99 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | 101 Bind(&MemoryPressureMonitorChromeOS::CheckMemoryPressure, |
| 100 weak_ptr_factory_.GetWeakPtr())); | 102 weak_ptr_factory_.GetWeakPtr())); |
| 101 } | 103 } |
| 102 | 104 |
| 103 void MemoryPressureObserverChromeOS::StartObserving() { | 105 void MemoryPressureMonitorChromeOS::StartObserving() { |
| 104 timer_.Start(FROM_HERE, | 106 timer_.Start(FROM_HERE, |
| 105 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), | 107 base::TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), |
| 106 Bind(&MemoryPressureObserverChromeOS:: | 108 Bind(&MemoryPressureMonitorChromeOS:: |
| 107 CheckMemoryPressureAndRecordStatistics, | 109 CheckMemoryPressureAndRecordStatistics, |
| 108 weak_ptr_factory_.GetWeakPtr())); | 110 weak_ptr_factory_.GetWeakPtr())); |
| 109 } | 111 } |
| 110 | 112 |
| 111 void MemoryPressureObserverChromeOS::StopObserving() { | 113 void MemoryPressureMonitorChromeOS::StopObserving() { |
| 112 // If StartObserving failed, StopObserving will still get called. | 114 // If StartObserving failed, StopObserving will still get called. |
| 113 timer_.Stop(); | 115 timer_.Stop(); |
| 114 } | 116 } |
| 115 | 117 |
| 116 void MemoryPressureObserverChromeOS::CheckMemoryPressureAndRecordStatistics() { | 118 void MemoryPressureMonitorChromeOS::CheckMemoryPressureAndRecordStatistics() { |
| 117 CheckMemoryPressure(); | 119 CheckMemoryPressure(); |
| 118 | 120 |
| 119 // Record UMA histogram statistics for the current memory pressure level. | 121 // Record UMA histogram statistics for the current memory pressure level. |
| 120 MemoryPressureLevelUMA memory_pressure_level_uma(MEMORY_PRESSURE_LEVEL_NONE); | 122 MemoryPressureLevelUMA memory_pressure_level_uma(MEMORY_PRESSURE_LEVEL_NONE); |
| 121 switch (current_memory_pressure_level_) { | 123 switch (current_memory_pressure_level_) { |
| 122 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | 124 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
| 123 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_NONE; | 125 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_NONE; |
| 124 break; | 126 break; |
| 125 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | 127 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: |
| 126 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_MODERATE; | 128 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_MODERATE; |
| 127 break; | 129 break; |
| 128 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | 130 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: |
| 129 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_CRITICAL; | 131 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_CRITICAL; |
| 130 break; | 132 break; |
| 131 } | 133 } |
| 132 | 134 |
| 133 UMA_HISTOGRAM_ENUMERATION("ChromeOS.MemoryPressureLevel", | 135 UMA_HISTOGRAM_ENUMERATION("ChromeOS.MemoryPressureLevel", |
| 134 memory_pressure_level_uma, | 136 memory_pressure_level_uma, |
| 135 NUM_MEMORY_PRESSURE_LEVELS); | 137 NUM_MEMORY_PRESSURE_LEVELS); |
| 136 } | 138 } |
| 137 | 139 |
| 138 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { | 140 void MemoryPressureMonitorChromeOS::CheckMemoryPressure() { |
| 139 MemoryPressureListener::MemoryPressureLevel old_pressure = | 141 MemoryPressureListener::MemoryPressureLevel old_pressure = |
| 140 current_memory_pressure_level_; | 142 current_memory_pressure_level_; |
| 141 current_memory_pressure_level_ = | 143 current_memory_pressure_level_ = |
| 142 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), | 144 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), |
| 143 moderate_pressure_threshold_percent_, | 145 moderate_pressure_threshold_percent_, |
| 144 critical_pressure_threshold_percent_); | 146 critical_pressure_threshold_percent_); |
| 145 // In case there is no memory pressure we do not notify. | 147 // In case there is no memory pressure we do not notify. |
| 146 if (current_memory_pressure_level_ == | 148 if (current_memory_pressure_level_ == |
| 147 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { | 149 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { |
| 148 return; | 150 return; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 164 // When we reducing the pressure level from critical to moderate, we | 166 // When we reducing the pressure level from critical to moderate, we |
| 165 // restart the timeout and do not send another notification. | 167 // restart the timeout and do not send another notification. |
| 166 moderate_pressure_repeat_count_ = 0; | 168 moderate_pressure_repeat_count_ = 0; |
| 167 return; | 169 return; |
| 168 } | 170 } |
| 169 moderate_pressure_repeat_count_ = 0; | 171 moderate_pressure_repeat_count_ = 0; |
| 170 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); | 172 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); |
| 171 } | 173 } |
| 172 | 174 |
| 173 // Gets the used ChromeOS memory in percent. | 175 // Gets the used ChromeOS memory in percent. |
| 174 int MemoryPressureObserverChromeOS::GetUsedMemoryInPercent() { | 176 int MemoryPressureMonitorChromeOS::GetUsedMemoryInPercent() { |
| 175 base::SystemMemoryInfoKB info; | 177 base::SystemMemoryInfoKB info; |
| 176 if (!base::GetSystemMemoryInfo(&info)) { | 178 if (!base::GetSystemMemoryInfo(&info)) { |
| 177 VLOG(1) << "Cannot determine the free memory of the system."; | 179 VLOG(1) << "Cannot determine the free memory of the system."; |
| 178 return 0; | 180 return 0; |
| 179 } | 181 } |
| 180 // TODO(skuhne): Instead of adding the kernel memory pressure calculation | 182 // TODO(skuhne): Instead of adding the kernel memory pressure calculation |
| 181 // logic here, we should have a kernel mechanism similar to the low memory | 183 // logic here, we should have a kernel mechanism similar to the low memory |
| 182 // notifier in ChromeOS which offers multiple pressure states. | 184 // notifier in ChromeOS which offers multiple pressure states. |
| 183 // To track this, we have crbug.com/381196. | 185 // To track this, we have crbug.com/381196. |
| 184 | 186 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 201 | 203 |
| 202 // Available memory is the sum of free, swap and easy reclaimable memory. | 204 // Available memory is the sum of free, swap and easy reclaimable memory. |
| 203 int available_memory = | 205 int available_memory = |
| 204 info.free + info.swap_free / kSwapWeight + file_memory; | 206 info.free + info.swap_free / kSwapWeight + file_memory; |
| 205 | 207 |
| 206 DCHECK(available_memory < total_memory); | 208 DCHECK(available_memory < total_memory); |
| 207 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 209 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
| 208 return percentage; | 210 return percentage; |
| 209 } | 211 } |
| 210 | 212 |
| 211 } // namespace base | |
| OLD | NEW |