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/metrics/histogram_macros.h" |
8 #include "base/process/process_metrics.h" | 9 #include "base/process/process_metrics.h" |
9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
10 | 11 |
11 namespace base { | 12 namespace base { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // The time between memory pressure checks. While under critical pressure, this | 16 // The time between memory pressure checks. While under critical pressure, this |
16 // is also the timer to repeat cleanup attempts. | 17 // is also the timer to repeat cleanup attempts. |
17 const int kMemoryPressureIntervalMs = 1000; | 18 const int kMemoryPressureIntervalMs = 1000; |
18 | 19 |
19 // The time which should pass between two moderate memory pressure calls. | 20 // The time which should pass between two moderate memory pressure calls. |
20 const int kModerateMemoryPressureCooldownMs = 10000; | 21 const int kModerateMemoryPressureCooldownMs = 10000; |
21 | 22 |
22 // Number of event polls before the next moderate pressure event can be sent. | 23 // Number of event polls before the next moderate pressure event can be sent. |
23 const int kModerateMemoryPressureCooldown = | 24 const int kModerateMemoryPressureCooldown = |
24 kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs; | 25 kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs; |
25 | 26 |
26 // Threshold constants to emit pressure events. | 27 // Threshold constants to emit pressure events. |
27 const int kNormalMemoryPressureModerateThresholdPercent = 60; | 28 const int kNormalMemoryPressureModerateThresholdPercent = 60; |
28 const int kNormalMemoryPressureCriticalThresholdPercent = 90; | 29 const int kNormalMemoryPressureCriticalThresholdPercent = 90; |
29 const int kAggressiveMemoryPressureModerateThresholdPercent = 35; | 30 const int kAggressiveMemoryPressureModerateThresholdPercent = 35; |
30 const int kAggressiveMemoryPressureCriticalThresholdPercent = 70; | 31 const int kAggressiveMemoryPressureCriticalThresholdPercent = 70; |
31 | 32 |
| 33 // The possible state for memory pressure level. The values should be in line |
| 34 // with values in MemoryPressureListener::MemoryPressureLevel and should be |
| 35 // updated if more memory pressure levels are introduced. |
| 36 enum MemoryPressureLevelUMA { |
| 37 MEMORY_PRESSURE_LEVEL_NONE = 0, |
| 38 MEMORY_PRESSURE_LEVEL_MODERATE, |
| 39 MEMORY_PRESSURE_LEVEL_CRITICAL, |
| 40 NUM_MEMORY_PRESSURE_LEVELS |
| 41 }; |
| 42 |
32 // Converts a |MemoryPressureThreshold| value into a used memory percentage for | 43 // Converts a |MemoryPressureThreshold| value into a used memory percentage for |
33 // the moderate pressure event. | 44 // the moderate pressure event. |
34 int GetModerateMemoryThresholdInPercent( | 45 int GetModerateMemoryThresholdInPercent( |
35 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { | 46 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { |
36 return thresholds == MemoryPressureObserverChromeOS:: | 47 return thresholds == MemoryPressureObserverChromeOS:: |
37 THRESHOLD_AGGRESSIVE_CACHE_DISCARD || | 48 THRESHOLD_AGGRESSIVE_CACHE_DISCARD || |
38 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE | 49 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE |
39 ? kAggressiveMemoryPressureModerateThresholdPercent | 50 ? kAggressiveMemoryPressureModerateThresholdPercent |
40 : kNormalMemoryPressureModerateThresholdPercent; | 51 : kNormalMemoryPressureModerateThresholdPercent; |
41 } | 52 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { | 96 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { |
86 MessageLoop::current()->PostTask( | 97 MessageLoop::current()->PostTask( |
87 FROM_HERE, | 98 FROM_HERE, |
88 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | 99 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, |
89 weak_ptr_factory_.GetWeakPtr())); | 100 weak_ptr_factory_.GetWeakPtr())); |
90 } | 101 } |
91 | 102 |
92 void MemoryPressureObserverChromeOS::StartObserving() { | 103 void MemoryPressureObserverChromeOS::StartObserving() { |
93 timer_.Start(FROM_HERE, | 104 timer_.Start(FROM_HERE, |
94 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), | 105 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), |
95 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | 106 Bind(&MemoryPressureObserverChromeOS:: |
| 107 CheckMemoryPressureAndRecordStatistics, |
96 weak_ptr_factory_.GetWeakPtr())); | 108 weak_ptr_factory_.GetWeakPtr())); |
97 } | 109 } |
98 | 110 |
99 void MemoryPressureObserverChromeOS::StopObserving() { | 111 void MemoryPressureObserverChromeOS::StopObserving() { |
100 // If StartObserving failed, StopObserving will still get called. | 112 // If StartObserving failed, StopObserving will still get called. |
101 timer_.Stop(); | 113 timer_.Stop(); |
102 } | 114 } |
103 | 115 |
| 116 void MemoryPressureObserverChromeOS::CheckMemoryPressureAndRecordStatistics() { |
| 117 CheckMemoryPressure(); |
| 118 |
| 119 // Record UMA histogram statistics for the current memory pressure level. |
| 120 MemoryPressureLevelUMA memory_pressure_level_uma(MEMORY_PRESSURE_LEVEL_NONE); |
| 121 switch (current_memory_pressure_level_) { |
| 122 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: |
| 123 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_NONE; |
| 124 break; |
| 125 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: |
| 126 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_MODERATE; |
| 127 break; |
| 128 case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: |
| 129 memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_CRITICAL; |
| 130 break; |
| 131 } |
| 132 |
| 133 UMA_HISTOGRAM_ENUMERATION("ChromeOS.MemoryPressureLevel", |
| 134 memory_pressure_level_uma, |
| 135 NUM_MEMORY_PRESSURE_LEVELS); |
| 136 } |
| 137 |
104 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { | 138 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { |
105 MemoryPressureListener::MemoryPressureLevel old_pressure = | 139 MemoryPressureListener::MemoryPressureLevel old_pressure = |
106 current_memory_pressure_level_; | 140 current_memory_pressure_level_; |
107 current_memory_pressure_level_ = | 141 current_memory_pressure_level_ = |
108 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), | 142 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), |
109 moderate_pressure_threshold_percent_, | 143 moderate_pressure_threshold_percent_, |
110 critical_pressure_threshold_percent_); | 144 critical_pressure_threshold_percent_); |
111 // In case there is no memory pressure we do not notify. | 145 // In case there is no memory pressure we do not notify. |
112 if (current_memory_pressure_level_ == | 146 if (current_memory_pressure_level_ == |
113 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { | 147 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 // Available memory is the sum of free, swap and easy reclaimable memory. | 202 // Available memory is the sum of free, swap and easy reclaimable memory. |
169 int available_memory = | 203 int available_memory = |
170 info.free + info.swap_free / kSwapWeight + file_memory; | 204 info.free + info.swap_free / kSwapWeight + file_memory; |
171 | 205 |
172 DCHECK(available_memory < total_memory); | 206 DCHECK(available_memory < total_memory); |
173 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 207 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
174 return percentage; | 208 return percentage; |
175 } | 209 } |
176 | 210 |
177 } // namespace base | 211 } // namespace base |
OLD | NEW |