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; | |
29 | 31 |
30 // Converts free percent of memory into a memory pressure value. | 32 // Converts free percent of memory into a memory pressure value. |
31 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( | 33 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( |
32 int memory_fill_level) { | 34 int actual_fill_level, int moderate_threshold, int critical_threshold) { |
33 if (memory_fill_level < kMemoryPressureModerateThresholdPercent) | 35 if (actual_fill_level < moderate_threshold) |
34 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 36 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
35 return memory_fill_level < kMemoryPressureCriticalThresholdPercent ? | 37 return actual_fill_level < critical_threshold ? |
36 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE : | 38 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE : |
37 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | 39 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
38 } | 40 } |
39 | 41 |
40 } // namespace | 42 } // namespace |
41 | 43 |
42 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS() | 44 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS( |
45 MemoryPressureThresholds thresholds) | |
43 : current_memory_pressure_level_( | 46 : current_memory_pressure_level_( |
44 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), | 47 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
45 moderate_pressure_repeat_count_(0), | 48 moderate_pressure_repeat_count_(0), |
49 moderate_pressure_threshold_( | |
50 (thresholds == AGGRESSIVE_MODERATE_THRESHOLD || | |
Charlie Reis
2015/01/21 17:42:05
Might this be easier to read with helper functions
Mr4D (OOO till 08-26)
2015/01/21 18:52:14
Done.
| |
51 thresholds == AGGRESSIVE_THRESHOLDS)? | |
52 kAggressiveMemoryPressureModerateThresholdPercent : | |
53 kNormalMemoryPressureModerateThresholdPercent), | |
54 critical_pressure_threshold_( | |
55 (thresholds == AGGRESSIVE_CRITICAL_THRESHOLD || | |
56 thresholds == AGGRESSIVE_THRESHOLDS)? | |
57 kAggressiveMemoryPressureCriticalThresholdPercent : | |
58 kNormalMemoryPressureCriticalThresholdPercent), | |
46 weak_ptr_factory_(this) { | 59 weak_ptr_factory_(this) { |
47 StartObserving(); | 60 StartObserving(); |
48 } | 61 } |
49 | 62 |
50 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { | 63 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { |
51 StopObserving(); | 64 StopObserving(); |
52 } | 65 } |
53 | 66 |
54 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { | 67 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { |
55 MessageLoop::current()->PostTask( | 68 MessageLoop::current()->PostTask( |
(...skipping 11 matching lines...) Expand all Loading... | |
67 | 80 |
68 void MemoryPressureObserverChromeOS::StopObserving() { | 81 void MemoryPressureObserverChromeOS::StopObserving() { |
69 // If StartObserving failed, StopObserving will still get called. | 82 // If StartObserving failed, StopObserving will still get called. |
70 timer_.Stop(); | 83 timer_.Stop(); |
71 } | 84 } |
72 | 85 |
73 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { | 86 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { |
74 MemoryPressureListener::MemoryPressureLevel old_pressure = | 87 MemoryPressureListener::MemoryPressureLevel old_pressure = |
75 current_memory_pressure_level_; | 88 current_memory_pressure_level_; |
76 current_memory_pressure_level_ = | 89 current_memory_pressure_level_ = |
77 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent()); | 90 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent(), |
91 moderate_pressure_threshold_, | |
92 critical_pressure_threshold_); | |
78 // In case there is no memory pressure we do not notify. | 93 // In case there is no memory pressure we do not notify. |
79 if (current_memory_pressure_level_ == | 94 if (current_memory_pressure_level_ == |
80 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { | 95 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { |
81 return; | 96 return; |
82 } | 97 } |
83 if (old_pressure == current_memory_pressure_level_) { | 98 if (old_pressure == current_memory_pressure_level_) { |
84 // If the memory pressure is still at the same level, we notify again for a | 99 // 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 | 100 // critical level. In case of a moderate level repeat however, we only send |
86 // a notification after a certain time has passed. | 101 // a notification after a certain time has passed. |
87 if (current_memory_pressure_level_ == | 102 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. | 150 // Available memory is the sum of free, swap and easy reclaimable memory. |
136 int available_memory = | 151 int available_memory = |
137 info.free + info.swap_free / kSwapWeight + file_memory; | 152 info.free + info.swap_free / kSwapWeight + file_memory; |
138 | 153 |
139 DCHECK(available_memory < total_memory); | 154 DCHECK(available_memory < total_memory); |
140 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 155 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
141 return percentage; | 156 return percentage; |
142 } | 157 } |
143 | 158 |
144 } // namespace base | 159 } // namespace base |
OLD | NEW |