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