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/memory/ref_counted.h" |
11 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 | 15 |
| 16 class MemoryPressureObserverChromeOSTest_CheckMemoryPressure_Test; |
| 17 |
15 //////////////////////////////////////////////////////////////////////////////// | 18 //////////////////////////////////////////////////////////////////////////////// |
16 // MemoryPressureObserverChromeOS | 19 // MemoryPressureObserverChromeOS |
17 // | 20 // |
18 // A class to handle the observation of our free memory. It notifies the | 21 // 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 | 22 // MemoryPressureListener of memory fill level changes, so that it can take |
20 // action to reduce memory resources accordingly. | 23 // action to reduce memory resources accordingly. |
21 // | 24 // |
22 class BASE_EXPORT MemoryPressureObserverChromeOS { | 25 class BASE_EXPORT MemoryPressureObserverChromeOS { |
23 public: | 26 public: |
| 27 typedef int (*GetUsedMemoryInPercentCallback)(void); |
| 28 |
24 MemoryPressureObserverChromeOS(); | 29 MemoryPressureObserverChromeOS(); |
25 ~MemoryPressureObserverChromeOS(); | 30 ~MemoryPressureObserverChromeOS(); |
26 | 31 |
| 32 // Redo the memory pressure calculation soon and call again if a critical |
| 33 // memory pressure prevails. Note that this call will trigger an asynchronous |
| 34 // action which gives the system time to release memory back into the pool. |
| 35 void ScheduleEarlyCheck(); |
| 36 |
27 // Get the current memory pressure level. | 37 // Get the current memory pressure level. |
28 base::MemoryPressureListener::MemoryPressureLevel GetCurrentPressureLevel() { | 38 base::MemoryPressureListener::MemoryPressureLevel GetCurrentPressureLevel() { |
29 return current_memory_pressure_level_; | 39 return current_memory_pressure_level_; |
30 } | 40 } |
31 | 41 |
| 42 // Overrides the function which calculates the used memory size in percent. |
| 43 void SetGetUsedMemoryInPercentCallbackForUnittest( |
| 44 GetUsedMemoryInPercentCallback callback); |
| 45 |
32 private: | 46 private: |
| 47 friend MemoryPressureObserverChromeOSTest_CheckMemoryPressure_Test; |
33 // Starts observing the memory fill level. | 48 // Starts observing the memory fill level. |
34 // Calls to StartObserving should always be matched with calls to | 49 // Calls to StartObserving should always be matched with calls to |
35 // StopObserving. | 50 // StopObserving. |
36 void StartObserving(); | 51 void StartObserving(); |
37 | 52 |
38 // Stop observing the memory fill level. | 53 // Stop observing the memory fill level. |
39 // May be safely called if StartObserving has not been called. | 54 // May be safely called if StartObserving has not been called. |
40 void StopObserving(); | 55 void StopObserving(); |
41 | 56 |
42 // The function which gets periodically be called to check any changes in the | 57 // The function which gets periodically called to check any changes in the |
43 // memory pressure. | 58 // memory pressure. It will report pressure changes as well as continuous |
| 59 // critical pressure levels. |
44 void CheckMemoryPressure(); | 60 void CheckMemoryPressure(); |
45 | 61 |
46 // The current memory pressure. | 62 // The current memory pressure. |
47 base::MemoryPressureListener::MemoryPressureLevel | 63 base::MemoryPressureListener::MemoryPressureLevel |
48 current_memory_pressure_level_; | 64 current_memory_pressure_level_; |
49 | 65 |
| 66 // The function which determines the used memory in percent. |
| 67 GetUsedMemoryInPercentCallback used_memory_in_percent_; |
| 68 |
50 // A periodic timer to check for resource pressure changes. This will get | 69 // A periodic timer to check for resource pressure changes. This will get |
51 // replaced by a kernel triggered event system (see crbug.com/381196). | 70 // replaced by a kernel triggered event system (see crbug.com/381196). |
52 base::RepeatingTimer<MemoryPressureObserverChromeOS> timer_; | 71 base::RepeatingTimer<MemoryPressureObserverChromeOS> timer_; |
53 | 72 |
| 73 // To slow down the amount of moderate pressure event calls, this counter |
| 74 // gets used to count the number of events since the last event occured. |
| 75 int moderate_pressure_repeat_count_; |
| 76 |
| 77 base::WeakPtrFactory<MemoryPressureObserverChromeOS> weak_ptr_factory_; |
| 78 |
54 DISALLOW_COPY_AND_ASSIGN(MemoryPressureObserverChromeOS); | 79 DISALLOW_COPY_AND_ASSIGN(MemoryPressureObserverChromeOS); |
55 }; | 80 }; |
56 | 81 |
57 } // namespace base | 82 } // namespace base |
58 | 83 |
59 #endif // BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ | 84 #endif // BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ |
OLD | NEW |