| 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 "base/chromeos/memory_pressure_observer_chromeos.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/process/process_metrics.h" | 8 #include "base/process/process_metrics.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // The time between memory pressure checks. While under critical pressure, this | 15 // The time between memory pressure checks. While under critical pressure, this |
| 16 // is also the timer to repeat cleanup attempts. | 16 // is also the timer to repeat cleanup attempts. |
| 17 const int kMemoryPressureIntervalMs = 1000; | 17 const int kMemoryPressureIntervalMs = 1000; |
| 18 | 18 |
| 19 // The time which should pass between two moderate memory pressure calls. | 19 // The time which should pass between two moderate memory pressure calls. |
| 20 const int kModerateMemoryPressureCooldownMs = 10000; | 20 const int kModerateMemoryPressureCooldownMs = 10000; |
| 21 | 21 |
| 22 // Number of event polls before the next moderate pressure event can be sent. | 22 // Number of event polls before the next moderate pressure event can be sent. |
| 23 const int kModerateMemoryPressureCooldown = | 23 const int kModerateMemoryPressureCooldown = |
| 24 kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs; | 24 kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs; |
| 25 | 25 |
| 26 // Threshold constants to emit pressure events. | 26 // Threshold constants to emit pressure events. |
| 27 const int kMemoryPressureModerateThresholdPercent = 70; | 27 const int kNormalMemoryPressureModerateThresholdPercent = 60; |
| 28 const int kMemoryPressureCriticalThresholdPercent = 90; | 28 const int kNormalMemoryPressureCriticalThresholdPercent = 90; |
| 29 const int kAggressiveMemoryPressureModerateThresholdPercent = 35; |
| 30 const int kAggressiveMemoryPressureCriticalThresholdPercent = 70; |
| 31 |
| 32 // Converts a |MemoryPressureThreshold| value into a used memory percentage for |
| 33 // the moderate pressure event. |
| 34 int GetModerateMemoryThresholdInPercent( |
| 35 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { |
| 36 return thresholds == MemoryPressureObserverChromeOS:: |
| 37 THRESHOLD_AGGRESSIVE_CACHE_DISCARD || |
| 38 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE |
| 39 ? kAggressiveMemoryPressureModerateThresholdPercent |
| 40 : kNormalMemoryPressureModerateThresholdPercent; |
| 41 } |
| 42 |
| 43 // Converts a |MemoryPressureThreshold| value into a used memory percentage for |
| 44 // the critical pressure event. |
| 45 int GetCriticalMemoryThresholdInPercent( |
| 46 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { |
| 47 return thresholds == MemoryPressureObserverChromeOS:: |
| 48 THRESHOLD_AGGRESSIVE_TAB_DISCARD || |
| 49 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE |
| 50 ? kAggressiveMemoryPressureCriticalThresholdPercent |
| 51 : kNormalMemoryPressureCriticalThresholdPercent; |
| 52 } |
| 29 | 53 |
| 30 // Converts free percent of memory into a memory pressure value. | 54 // Converts free percent of memory into a memory pressure value. |
| 31 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( | 55 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( |
| 32 int memory_fill_level) { | 56 int actual_fill_level, |
| 33 if (memory_fill_level < kMemoryPressureModerateThresholdPercent) | 57 int moderate_threshold, |
| 58 int critical_threshold) { |
| 59 if (actual_fill_level < moderate_threshold) |
| 34 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 60 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
| 35 return memory_fill_level < kMemoryPressureCriticalThresholdPercent ? | 61 return actual_fill_level < critical_threshold |
| 36 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE : | 62 ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE |
| 37 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | 63 : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
| 38 } | 64 } |
| 39 | 65 |
| 40 } // namespace | 66 } // namespace |
| 41 | 67 |
| 42 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS() | 68 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS( |
| 69 MemoryPressureThresholds thresholds) |
| 43 : current_memory_pressure_level_( | 70 : current_memory_pressure_level_( |
| 44 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 71 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 45 moderate_pressure_repeat_count_(0), | 72 moderate_pressure_repeat_count_(0), |
| 73 moderate_pressure_threshold_percent_( |
| 74 GetModerateMemoryThresholdInPercent(thresholds)), |
| 75 critical_pressure_threshold_percent_( |
| 76 GetCriticalMemoryThresholdInPercent(thresholds)), |
| 46 weak_ptr_factory_(this) { | 77 weak_ptr_factory_(this) { |
| 47 StartObserving(); | 78 StartObserving(); |
| 48 } | 79 } |
| 49 | 80 |
| 50 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { | 81 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { |
| 51 StopObserving(); | 82 StopObserving(); |
| 52 } | 83 } |
| 53 | 84 |
| 54 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { | 85 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { |
| 55 MessageLoop::current()->PostTask( | 86 MessageLoop::current()->PostTask( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 | 98 |
| 68 void MemoryPressureObserverChromeOS::StopObserving() { | 99 void MemoryPressureObserverChromeOS::StopObserving() { |
| 69 // If StartObserving failed, StopObserving will still get called. | 100 // If StartObserving failed, StopObserving will still get called. |
| 70 timer_.Stop(); | 101 timer_.Stop(); |
| 71 } | 102 } |
| 72 | 103 |
| 73 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { | 104 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { |
| 74 MemoryPressureListener::MemoryPressureLevel old_pressure = | 105 MemoryPressureListener::MemoryPressureLevel old_pressure = |
| 75 current_memory_pressure_level_; | 106 current_memory_pressure_level_; |
| 76 current_memory_pressure_level_ = | 107 current_memory_pressure_level_ = |
| 77 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent()); | 108 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), |
| 109 moderate_pressure_threshold_percent_, |
| 110 critical_pressure_threshold_percent_); |
| 78 // In case there is no memory pressure we do not notify. | 111 // In case there is no memory pressure we do not notify. |
| 79 if (current_memory_pressure_level_ == | 112 if (current_memory_pressure_level_ == |
| 80 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { | 113 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { |
| 81 return; | 114 return; |
| 82 } | 115 } |
| 83 if (old_pressure == current_memory_pressure_level_) { | 116 if (old_pressure == current_memory_pressure_level_) { |
| 84 // If the memory pressure is still at the same level, we notify again for a | 117 // If the memory pressure is still at the same level, we notify again for a |
| 85 // critical level. In case of a moderate level repeat however, we only send | 118 // critical level. In case of a moderate level repeat however, we only send |
| 86 // a notification after a certain time has passed. | 119 // a notification after a certain time has passed. |
| 87 if (current_memory_pressure_level_ == | 120 if (current_memory_pressure_level_ == |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 // Available memory is the sum of free, swap and easy reclaimable memory. | 168 // Available memory is the sum of free, swap and easy reclaimable memory. |
| 136 int available_memory = | 169 int available_memory = |
| 137 info.free + info.swap_free / kSwapWeight + file_memory; | 170 info.free + info.swap_free / kSwapWeight + file_memory; |
| 138 | 171 |
| 139 DCHECK(available_memory < total_memory); | 172 DCHECK(available_memory < total_memory); |
| 140 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 173 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
| 141 return percentage; | 174 return percentage; |
| 142 } | 175 } |
| 143 | 176 |
| 144 } // namespace base | 177 } // namespace base |
| OLD | NEW |