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 #ifndef BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ | 5 #ifndef BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ |
6 #define BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ | 6 #define BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/memory_pressure_listener.h" | 10 #include "base/memory/memory_pressure_listener.h" |
11 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 //////////////////////////////////////////////////////////////////////////////// | 15 //////////////////////////////////////////////////////////////////////////////// |
16 // MemoryPressureObserverChromeOS | 16 // MemoryPressureObserverChromeOS |
17 // | 17 // |
18 // A class to handle the observation of our free memory. It notifies the | 18 // A class to handle the observation of our free memory. It notifies the |
19 // MemoryPressureListener of memory fill level changes, so that it can take | 19 // MemoryPressureListener of memory fill level changes, so that it can take |
20 // action to reduce memory resources accordingly. | 20 // action to reduce memory resources accordingly. |
21 // | 21 // |
22 class BASE_EXPORT MemoryPressureObserverChromeOS { | 22 class BASE_EXPORT MemoryPressureObserverChromeOS { |
23 public: | 23 public: |
24 // This is like the |MemoryPressureListener::MemoryPressureLevel| but it has | |
25 // more states to allow for a finer grained control as well as a request for | |
26 // the current status. | |
27 // TODO(skuhne): If the |MemoryPressureListener| will get extended to support | |
28 // all these levels, this can be removed. | |
29 enum MemoryPressureLevel { | |
30 // There is enough memory available to use. | |
31 MEMORY_PRESSURE_LEVEL_LOW = 0, | |
32 | |
33 // Modules are advised to free buffers that are cheap to re-allocate and not | |
34 // immediately needed. | |
35 MEMORY_PRESSURE_LEVEL_MODERATE = 1, | |
36 | |
37 // Modules are advised that they might get unloaded dependent on the OS. | |
38 // As such they should start to release more memory if possible. | |
39 MEMORY_PRESSURE_LEVEL_HIGH = 2, | |
40 | |
41 // At this level, modules are advised to free all possible memory. The | |
42 // alternative is to be killed by the system, which means all memory will | |
43 // have to be re-created, plus the cost of a cold start. | |
44 MEMORY_PRESSURE_LEVEL_CRITICAL = 3, | |
45 }; | |
46 | |
47 MemoryPressureObserverChromeOS(); | 24 MemoryPressureObserverChromeOS(); |
48 ~MemoryPressureObserverChromeOS(); | 25 ~MemoryPressureObserverChromeOS(); |
49 | 26 |
50 // Get the current memory pressure level. | 27 // Get the current memory pressure level. |
51 MemoryPressureLevel GetCurrentPressureLevel() { | 28 base::MemoryPressureListener::MemoryPressureLevel GetCurrentPressureLevel() { |
52 return current_memory_pressure_level_; | 29 return current_memory_pressure_level_; |
53 } | 30 } |
54 | 31 |
55 private: | 32 private: |
56 // Starts observing the memory fill level. | 33 // Starts observing the memory fill level. |
57 // Calls to StartObserving should always be matched with calls to | 34 // Calls to StartObserving should always be matched with calls to |
58 // StopObserving. | 35 // StopObserving. |
59 void StartObserving(); | 36 void StartObserving(); |
60 | 37 |
61 // Stop observing the memory fill level. | 38 // Stop observing the memory fill level. |
62 // May be safely called if StartObserving has not been called. | 39 // May be safely called if StartObserving has not been called. |
63 void StopObserving(); | 40 void StopObserving(); |
64 | 41 |
65 // The function which gets periodically be called to check any changes in the | 42 // The function which gets periodically be called to check any changes in the |
66 // memory pressure. | 43 // memory pressure. |
67 void CheckMemoryPressure(); | 44 void CheckMemoryPressure(); |
68 | 45 |
69 // The current memory pressure. | 46 // The current memory pressure. |
70 MemoryPressureLevel current_memory_pressure_level_; | 47 base::MemoryPressureListener::MemoryPressureLevel |
| 48 current_memory_pressure_level_; |
71 | 49 |
72 // A periodic timer to check for resource pressure changes. This will get | 50 // A periodic timer to check for resource pressure changes. This will get |
73 // replaced by a kernel triggered event system (see crbug.com/381196). | 51 // replaced by a kernel triggered event system (see crbug.com/381196). |
74 base::RepeatingTimer<MemoryPressureObserverChromeOS> timer_; | 52 base::RepeatingTimer<MemoryPressureObserverChromeOS> timer_; |
75 | 53 |
76 DISALLOW_COPY_AND_ASSIGN(MemoryPressureObserverChromeOS); | 54 DISALLOW_COPY_AND_ASSIGN(MemoryPressureObserverChromeOS); |
77 }; | 55 }; |
78 | 56 |
79 } // namespace base | 57 } // namespace base |
80 | 58 |
81 #endif // BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ | 59 #endif // BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ |
OLD | NEW |