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/process/process_metrics.h" | 7 #include "base/process/process_metrics.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 // The time between memory pressure checks. | 14 // The time between memory pressure checks. |
15 const int kMemoryPressureIntervalInMS = 1000; | 15 const int kMemoryPressureIntervalInMS = 1000; |
16 | 16 |
17 // Converts free percent of memory into a memory pressure value. | 17 // Converts free percent of memory into a memory pressure value. |
18 MemoryPressureObserverChromeOS::MemoryPressureLevel | 18 MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel( |
19 GetMemoryPressureLevelFromFillLevel( | |
20 int memory_fill_level) { | 19 int memory_fill_level) { |
21 if (memory_fill_level < 50) | 20 if (memory_fill_level < 70) |
22 return MemoryPressureObserverChromeOS::MEMORY_PRESSURE_LEVEL_LOW; | 21 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
23 if (memory_fill_level < 75) | 22 return memory_fill_level < 90 ? |
24 return MemoryPressureObserverChromeOS::MEMORY_PRESSURE_LEVEL_MODERATE; | 23 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE : |
25 if (memory_fill_level < 90) | 24 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
26 return MemoryPressureObserverChromeOS::MEMORY_PRESSURE_LEVEL_HIGH; | |
27 return MemoryPressureObserverChromeOS::MEMORY_PRESSURE_LEVEL_CRITICAL; | |
28 } | 25 } |
29 | 26 |
30 // Gets the used ChromeOS memory in percent. | 27 // Gets the used ChromeOS memory in percent. |
31 int GetUsedMemoryInPercent() { | 28 int GetUsedMemoryInPercent() { |
32 base::SystemMemoryInfoKB info; | 29 base::SystemMemoryInfoKB info; |
33 if (!base::GetSystemMemoryInfo(&info)) { | 30 if (!base::GetSystemMemoryInfo(&info)) { |
34 VLOG(1) << "Cannot determine the free memory of the system."; | 31 VLOG(1) << "Cannot determine the free memory of the system."; |
35 return 0; | 32 return 0; |
36 } | 33 } |
37 // TODO(skuhne): Instead of adding the kernel memory pressure calculation | 34 // TODO(skuhne): Instead of adding the kernel memory pressure calculation |
(...skipping 23 matching lines...) Expand all Loading... |
61 info.free + info.swap_free / kSwapWeight + file_memory; | 58 info.free + info.swap_free / kSwapWeight + file_memory; |
62 | 59 |
63 DCHECK(available_memory < total_memory); | 60 DCHECK(available_memory < total_memory); |
64 int percentage = ((total_memory - available_memory) * 100) / total_memory; | 61 int percentage = ((total_memory - available_memory) * 100) / total_memory; |
65 return percentage; | 62 return percentage; |
66 } | 63 } |
67 | 64 |
68 } // namespace | 65 } // namespace |
69 | 66 |
70 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS() | 67 MemoryPressureObserverChromeOS::MemoryPressureObserverChromeOS() |
71 : current_memory_pressure_level_(MEMORY_PRESSURE_LEVEL_LOW) { | 68 : current_memory_pressure_level_( |
| 69 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) { |
72 StartObserving(); | 70 StartObserving(); |
73 } | 71 } |
74 | 72 |
75 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { | 73 MemoryPressureObserverChromeOS::~MemoryPressureObserverChromeOS() { |
76 StopObserving(); | 74 StopObserving(); |
77 } | 75 } |
78 | 76 |
79 void MemoryPressureObserverChromeOS::StartObserving() { | 77 void MemoryPressureObserverChromeOS::StartObserving() { |
80 timer_.Start(FROM_HERE, | 78 timer_.Start(FROM_HERE, |
81 base::TimeDelta::FromMilliseconds(kMemoryPressureIntervalInMS), | 79 base::TimeDelta::FromMilliseconds(kMemoryPressureIntervalInMS), |
82 base::Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, | 80 base::Bind(&MemoryPressureObserverChromeOS::CheckMemoryPressure, |
83 base::Unretained(this))); | 81 base::Unretained(this))); |
84 } | 82 } |
85 | 83 |
86 void MemoryPressureObserverChromeOS::StopObserving() { | 84 void MemoryPressureObserverChromeOS::StopObserving() { |
87 // If StartObserving failed, StopObserving will still get called. | 85 // If StartObserving failed, StopObserving will still get called. |
88 timer_.Stop(); | 86 timer_.Stop(); |
89 } | 87 } |
90 | 88 |
91 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { | 89 void MemoryPressureObserverChromeOS::CheckMemoryPressure() { |
92 MemoryPressureLevel old_pressure = current_memory_pressure_level_; | 90 MemoryPressureListener::MemoryPressureLevel old_pressure = |
93 MemoryPressureLevel new_pressure = | 91 current_memory_pressure_level_; |
| 92 MemoryPressureListener::MemoryPressureLevel new_pressure = |
94 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent()); | 93 GetMemoryPressureLevelFromFillLevel(GetUsedMemoryInPercent()); |
95 if (old_pressure != new_pressure) { | 94 if (old_pressure != new_pressure) { |
96 current_memory_pressure_level_ = new_pressure; | 95 current_memory_pressure_level_ = new_pressure; |
97 switch (new_pressure) { | 96 // Everything but NONE will be sent to the listener. |
98 case MEMORY_PRESSURE_LEVEL_LOW: | 97 if (new_pressure != MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) |
99 // The |MemoryPressureListener| does currently not support this. | 98 MemoryPressureListener::NotifyMemoryPressure(new_pressure); |
100 break; | |
101 case MEMORY_PRESSURE_LEVEL_MODERATE: | |
102 MemoryPressureListener::NotifyMemoryPressure( | |
103 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | |
104 break; | |
105 case MEMORY_PRESSURE_LEVEL_HIGH: | |
106 // The |MemoryPressureListener| does currently not support this. | |
107 break; | |
108 case MEMORY_PRESSURE_LEVEL_CRITICAL: | |
109 MemoryPressureListener::NotifyMemoryPressure( | |
110 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | |
111 break; | |
112 } | |
113 } | 99 } |
114 } | 100 } |
115 | 101 |
116 } // namespace base | 102 } // namespace base |
OLD | NEW |