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 // Number of memory pressure levels. Should update this value if more memory | |
34 // pressure level is introduced in MemoryPressureListener::MemoryPressureLevel. | |
Mr4D (OOO till 08-26)
2015/02/04 00:11:19
... if more memory pressure levels are introduced
| |
35 const int kNumMemoryPressureLevels = 3; | |
36 | |
32 // Converts a |MemoryPressureThreshold| value into a used memory percentage for | 37 // Converts a |MemoryPressureThreshold| value into a used memory percentage for |
33 // the moderate pressure event. | 38 // the moderate pressure event. |
34 int GetModerateMemoryThresholdInPercent( | 39 int GetModerateMemoryThresholdInPercent( |
35 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { | 40 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { |
36 return thresholds == MemoryPressureObserverChromeOS:: | 41 return thresholds == MemoryPressureObserverChromeOS:: |
37 THRESHOLD_AGGRESSIVE_CACHE_DISCARD || | 42 THRESHOLD_AGGRESSIVE_CACHE_DISCARD || |
38 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE | 43 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE |
39 ? kAggressiveMemoryPressureModerateThresholdPercent | 44 ? kAggressiveMemoryPressureModerateThresholdPercent |
40 : kNormalMemoryPressureModerateThresholdPercent; | 45 : kNormalMemoryPressureModerateThresholdPercent; |
41 } | 46 } |
42 | 47 |
43 // Converts a |MemoryPressureThreshold| value into a used memory percentage for | 48 // Converts a |MemoryPressureThreshold| value into a used memory percentage for |
44 // the critical pressure event. | 49 // the critical pressure event. |
45 int GetCriticalMemoryThresholdInPercent( | 50 int GetCriticalMemoryThresholdInPercent( |
46 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { | 51 MemoryPressureObserverChromeOS::MemoryPressureThresholds thresholds) { |
47 return thresholds == MemoryPressureObserverChromeOS:: | 52 return thresholds == MemoryPressureObserverChromeOS:: |
48 THRESHOLD_AGGRESSIVE_TAB_DISCARD || | 53 THRESHOLD_AGGRESSIVE_TAB_DISCARD || |
49 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE | 54 thresholds == MemoryPressureObserverChromeOS::THRESHOLD_AGGRESSIVE |
50 ? kAggressiveMemoryPressureCriticalThresholdPercent | 55 ? kAggressiveMemoryPressureCriticalThresholdPercent |
51 : kNormalMemoryPressureCriticalThresholdPercent; | 56 : kNormalMemoryPressureCriticalThresholdPercent; |
52 } | 57 } |
53 | 58 |
59 // Record UMA histogram statistics for the current memory pressure level. | |
60 void RecordMemoryStatistics( | |
61 MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | |
62 UMA_HISTOGRAM_ENUMERATION("ChromeOS.MemoryPressureLevel", | |
63 memory_pressure_level, kNumMemoryPressureLevels); | |
64 } | |
65 | |
54 // Converts free percent of memory into a memory pressure value. | 66 // Converts free percent of memory into a memory pressure value. |
55 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( | 67 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( |
56 int actual_fill_level, | 68 int actual_fill_level, |
57 int moderate_threshold, | 69 int moderate_threshold, |
58 int critical_threshold) { | 70 int critical_threshold) { |
59 if (actual_fill_level < moderate_threshold) | 71 if (actual_fill_level < moderate_threshold) |
60 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 72 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
61 return actual_fill_level < critical_threshold | 73 return actual_fill_level < critical_threshold |
62 ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE | 74 ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE |
63 : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | 75 : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
(...skipping 21 matching lines...) Expand all Loading... | |
85 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { | 97 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { |
86 MessageLoop::current()->PostTask( | 98 MessageLoop::current()->PostTask( |
87 FROM_HERE, | 99 FROM_HERE, |
88 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | 100 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, |
89 weak_ptr_factory_.GetWeakPtr())); | 101 weak_ptr_factory_.GetWeakPtr())); |
90 } | 102 } |
91 | 103 |
92 void MemoryPressureObserverChromeOS::StartObserving() { | 104 void MemoryPressureObserverChromeOS::StartObserving() { |
93 timer_.Start(FROM_HERE, | 105 timer_.Start(FROM_HERE, |
94 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), | 106 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), |
95 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | 107 Bind(&MemoryPressureObserverChromeOS:: |
108 CheckMemoryPressureAndRecordStatistics, | |
96 weak_ptr_factory_.GetWeakPtr())); | 109 weak_ptr_factory_.GetWeakPtr())); |
97 } | 110 } |
98 | 111 |
99 void MemoryPressureObserverChromeOS::StopObserving() { | 112 void MemoryPressureObserverChromeOS::StopObserving() { |
100 // If StartObserving failed, StopObserving will still get called. | 113 // If StartObserving failed, StopObserving will still get called. |
101 timer_.Stop(); | 114 timer_.Stop(); |
102 } | 115 } |
103 | 116 |
117 void MemoryPressureObserverChromeOS::CheckMemoryPressureAndRecordStatistics() { | |
118 CheckMemoryPressure(); | |
119 RecordMemoryStatistics(current_memory_pressure_level_); | |
Mr4D (OOO till 08-26)
2015/02/04 00:11:19
In this case you can pull the content of the funct
| |
120 } | |
121 | |
104 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { | 122 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { |
105 MemoryPressureListener::MemoryPressureLevel old_pressure = | 123 MemoryPressureListener::MemoryPressureLevel old_pressure = |
106 current_memory_pressure_level_; | 124 current_memory_pressure_level_; |
107 current_memory_pressure_level_ = | 125 current_memory_pressure_level_ = |
108 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), | 126 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), |
109 moderate_pressure_threshold_percent_, | 127 moderate_pressure_threshold_percent_, |
110 critical_pressure_threshold_percent_); | 128 critical_pressure_threshold_percent_); |
111 // In case there is no memory pressure we do not notify. | 129 // In case there is no memory pressure we do not notify. |
112 if (current_memory_pressure_level_ == | 130 if (current_memory_pressure_level_ == |
113 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { | 131 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. | 186 // Available memory is the sum of free, swap and easy reclaimable memory. |
169 int available_memory = | 187 int available_memory = |
170 info.free + info.swap_free / kSwapWeight + file_memory; | 188 info.free + info.swap_free / kSwapWeight + file_memory; |
171 | 189 |
172 DCHECK(available_memory < total_memory); | 190 DCHECK(available_memory < total_memory); |
173 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 191 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
174 return percentage; | 192 return percentage; |
175 } | 193 } |
176 | 194 |
177 } // namespace base | 195 } // namespace base |
OLD | NEW |