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/process/process_metrics.h" | 8 #include "base/process/process_metrics.h" |
8 #include "base/time/time.h" | 9 #include "base/time/time.h" |
9 | 10 |
10 namespace base { | 11 namespace base { |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 // The time between memory pressure checks. | 15 // The time between memory pressure checks. While under critical pressure, this |
15 const int kMemoryPressureIntervalInMS = 1000; | 16 // is also the timer to repeat cleanup attempts. |
| 17 const int kMemoryPressureIntervalMs = 1000; |
| 18 |
| 19 // The time which should pass between two moderate memory pressure calls. |
| 20 const int kModerateMemoryPressureCooldownMs = 10000; |
| 21 |
| 22 // Number of event polls before the next moderate pressure event can be sent. |
| 23 const int kModerateMemoryPressureCooldown = |
| 24 kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs; |
| 25 |
| 26 // Threshold constants to emit pressure events. |
| 27 const int kMemoryPressureModerateThresholdPercent = 70; |
| 28 const int kMemoryPressureCriticalThresholdPercent = 90; |
16 | 29 |
17 // Converts free percent of memory into a memory pressure value. | 30 // Converts free percent of memory into a memory pressure value. |
18 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( | 31 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( |
19 int memory_fill_level) { | 32 int memory_fill_level) { |
20 if (memory_fill_level < 70) | 33 if (memory_fill_level < kMemoryPressureModerateThresholdPercent) |
21 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 34 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
22 return memory_fill_level < 90 ? | 35 return memory_fill_level < kMemoryPressureCriticalThresholdPercent ? |
23 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE : | 36 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE : |
24 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | 37 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
25 } | 38 } |
26 | 39 |
| 40 } // namespace |
| 41 |
| 42 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS() |
| 43 : current_memory_pressure_level_( |
| 44 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), |
| 45 moderate_pressure_repeat_count_(0), |
| 46 weak_ptr_factory_(this) { |
| 47 StartObserving(); |
| 48 } |
| 49 |
| 50 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { |
| 51 StopObserving(); |
| 52 } |
| 53 |
| 54 void MemoryPressureObserverChromeOS::ScheduleEarlyCheck() { |
| 55 MessageLoop::current()->PostTask( |
| 56 FROM_HERE, |
| 57 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, |
| 58 weak_ptr_factory_.GetWeakPtr())); |
| 59 } |
| 60 |
| 61 void MemoryPressureObserverChromeOS::StartObserving() { |
| 62 timer_.Start(FROM_HERE, |
| 63 TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs), |
| 64 Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, |
| 65 weak_ptr_factory_.GetWeakPtr())); |
| 66 } |
| 67 |
| 68 void MemoryPressureObserverChromeOS::StopObserving() { |
| 69 // If StartObserving failed, StopObserving will still get called. |
| 70 timer_.Stop(); |
| 71 } |
| 72 |
| 73 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { |
| 74 MemoryPressureListener::MemoryPressureLevel old_pressure = |
| 75 current_memory_pressure_level_; |
| 76 current_memory_pressure_level_ = |
| 77 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent()); |
| 78 // In case there is no memory pressure we do not notify. |
| 79 if (current_memory_pressure_level_ == |
| 80 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { |
| 81 return; |
| 82 } |
| 83 if (old_pressure == current_memory_pressure_level_) { |
| 84 // 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 |
| 86 // a notification after a certain time has passed. |
| 87 if (current_memory_pressure_level_ == |
| 88 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE && |
| 89 ++moderate_pressure_repeat_count_ < |
| 90 kModerateMemoryPressureCooldown) { |
| 91 return; |
| 92 } |
| 93 } else if (current_memory_pressure_level_ == |
| 94 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE && |
| 95 old_pressure == |
| 96 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| 97 // When we reducing the pressure level from critical to moderate, we |
| 98 // restart the timeout and do not send another notification. |
| 99 moderate_pressure_repeat_count_ = 0; |
| 100 return; |
| 101 } |
| 102 moderate_pressure_repeat_count_ = 0; |
| 103 MemoryPressureListener::NotifyMemoryPressure(current_memory_pressure_level_); |
| 104 } |
| 105 |
27 // Gets the used ChromeOS memory in percent. | 106 // Gets the used ChromeOS memory in percent. |
28 int GetUsedMemoryInPercent() { | 107 int MemoryPressureObserverChromeOS::GetUsedMemoryInPercent() { |
29 base::SystemMemoryInfoKB info; | 108 base::SystemMemoryInfoKB info; |
30 if (!base::GetSystemMemoryInfo(&info)) { | 109 if (!base::GetSystemMemoryInfo(&info)) { |
31 VLOG(1) << "Cannot determine the free memory of the system."; | 110 VLOG(1) << "Cannot determine the free memory of the system."; |
32 return 0; | 111 return 0; |
33 } | 112 } |
34 // TODO(skuhne): Instead of adding the kernel memory pressure calculation | 113 // TODO(skuhne): Instead of adding the kernel memory pressure calculation |
35 // logic here, we should have a kernel mechanism similar to the low memory | 114 // logic here, we should have a kernel mechanism similar to the low memory |
36 // notifier in ChromeOS which offers multiple pressure states. | 115 // notifier in ChromeOS which offers multiple pressure states. |
37 // To track this, we have crbug.com/381196. | 116 // To track this, we have crbug.com/381196. |
38 | 117 |
(...skipping 16 matching lines...) Expand all Loading... |
55 | 134 |
56 // Available memory is the sum of free, swap and easy reclaimable memory. | 135 // Available memory is the sum of free, swap and easy reclaimable memory. |
57 int available_memory = | 136 int available_memory = |
58 info.free + info.swap_free / kSwapWeight + file_memory; | 137 info.free + info.swap_free / kSwapWeight + file_memory; |
59 | 138 |
60 DCHECK(available_memory < total_memory); | 139 DCHECK(available_memory < total_memory); |
61 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 140 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
62 return percentage; | 141 return percentage; |
63 } | 142 } |
64 | 143 |
65 } // namespace | |
66 | |
67 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS() | |
68 : current_memory_pressure_level_( | |
69 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { | |
70 StartObserving(); | |
71 } | |
72 | |
73 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { | |
74 StopObserving(); | |
75 } | |
76 | |
77 void MemoryPressureObserverChromeOS::StartObserving() { | |
78 timer_.Start(FROM_HERE, | |
79 base::TimeDelta::FromMilliseconds(kMemoryPressureIntervalInMS), | |
80 base::Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | |
81 base::Unretained(this))); | |
82 } | |
83 | |
84 void MemoryPressureObserverChromeOS::StopObserving() { | |
85 // If StartObserving failed, StopObserving will still get called. | |
86 timer_.Stop(); | |
87 } | |
88 | |
89 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { | |
90 MemoryPressureListener::MemoryPressureLevel old_pressure = | |
91 current_memory_pressure_level_; | |
92 MemoryPressureListener::MemoryPressureLevel new_pressure = | |
93 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent()); | |
94 if (old_pressure != new_pressure) { | |
95 current_memory_pressure_level_ = new_pressure; | |
96 // Everything but NONE will be sent to the listener. | |
97 if (new_pressure != MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) | |
98 MemoryPressureListener::NotifyMemoryPressure(new_pressure); | |
99 } | |
100 } | |
101 | |
102 } // namespace base | 144 } // namespace base |
OLD | NEW |