| 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_MONITOR_CHROMEOS_H_ |
| 6 #define BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ | 6 #define BASE_CHROMEOS_MEMORY_PRESSURE_MONITOR_CHROMEOS_H_ |
| 7 | 7 |
| 8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/files/scoped_file.h" |
| 9 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/memory_pressure_listener.h" | 12 #include "base/memory/memory_pressure_listener.h" |
| 13 #include "base/memory/memory_pressure_monitor.h" |
| 12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 13 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 14 | 16 |
| 15 namespace base { | 17 namespace base { |
| 16 | 18 |
| 17 class TestMemoryPressureObserver; | 19 class TestMemoryPressureMonitor; |
| 18 | 20 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 21 //////////////////////////////////////////////////////////////////////////////// |
| 20 // MemoryPressureObserverChromeOS | 22 // MemoryPressureMonitorChromeOS |
| 21 // | 23 // |
| 22 // A class to handle the observation of our free memory. It notifies the | 24 // A class to handle the observation of our free memory. It notifies the |
| 23 // MemoryPressureListener of memory fill level changes, so that it can take | 25 // MemoryPressureListener of memory fill level changes, so that it can take |
| 24 // action to reduce memory resources accordingly. | 26 // action to reduce memory resources accordingly. |
| 25 // | 27 // |
| 26 class BASE_EXPORT MemoryPressureObserverChromeOS { | 28 class BASE_EXPORT MemoryPressureMonitorChromeOS : public MemoryPressureMonitor { |
| 27 public: | 29 public: |
| 28 using GetUsedMemoryInPercentCallback = int (*)(); | 30 using GetUsedMemoryInPercentCallback = int (*)(); |
| 29 | 31 |
| 30 // There are two memory pressure events: | 32 // There are two memory pressure events: |
| 31 // MODERATE - which will mainly release caches. | 33 // MODERATE - which will mainly release caches. |
| 32 // CRITICAL - which will discard tabs. | 34 // CRITICAL - which will discard tabs. |
| 33 // The |MemoryPressureThresholds| enum selects the strategy of firing these | 35 // The |MemoryPressureThresholds| enum selects the strategy of firing these |
| 34 // events: A conservative strategy will keep as much content in memory as | 36 // events: A conservative strategy will keep as much content in memory as |
| 35 // possible (causing the system to swap to zram) and an aggressive strategy | 37 // possible (causing the system to swap to zram) and an aggressive strategy |
| 36 // will release memory earlier to avoid swapping. | 38 // will release memory earlier to avoid swapping. |
| 37 enum MemoryPressureThresholds { | 39 enum MemoryPressureThresholds { |
| 38 // Use the system default. | 40 // Use the system default. |
| 39 THRESHOLD_DEFAULT = 0, | 41 THRESHOLD_DEFAULT = 0, |
| 40 // Try to keep as much content in memory as possible. | 42 // Try to keep as much content in memory as possible. |
| 41 THRESHOLD_CONSERVATIVE = 1, | 43 THRESHOLD_CONSERVATIVE = 1, |
| 42 // Discard caches earlier, allowing to keep more tabs in memory. | 44 // Discard caches earlier, allowing to keep more tabs in memory. |
| 43 THRESHOLD_AGGRESSIVE_CACHE_DISCARD = 2, | 45 THRESHOLD_AGGRESSIVE_CACHE_DISCARD = 2, |
| 44 // Discard tabs earlier, allowing the system to get faster. | 46 // Discard tabs earlier, allowing the system to get faster. |
| 45 THRESHOLD_AGGRESSIVE_TAB_DISCARD = 3, | 47 THRESHOLD_AGGRESSIVE_TAB_DISCARD = 3, |
| 46 // Discard caches and tabs earlier to allow the system to be faster. | 48 // Discard caches and tabs earlier to allow the system to be faster. |
| 47 THRESHOLD_AGGRESSIVE = 4 | 49 THRESHOLD_AGGRESSIVE = 4 |
| 48 }; | 50 }; |
| 49 | 51 |
| 50 explicit MemoryPressureObserverChromeOS(MemoryPressureThresholds thresholds); | 52 explicit MemoryPressureMonitorChromeOS(MemoryPressureThresholds thresholds); |
| 51 virtual ~MemoryPressureObserverChromeOS(); | 53 ~MemoryPressureMonitorChromeOS() override; |
| 52 | 54 |
| 53 // Redo the memory pressure calculation soon and call again if a critical | 55 // Redo the memory pressure calculation soon and call again if a critical |
| 54 // memory pressure prevails. Note that this call will trigger an asynchronous | 56 // memory pressure prevails. Note that this call will trigger an asynchronous |
| 55 // action which gives the system time to release memory back into the pool. | 57 // action which gives the system time to release memory back into the pool. |
| 56 void ScheduleEarlyCheck(); | 58 void ScheduleEarlyCheck(); |
| 57 | 59 |
| 58 // Get the current memory pressure level. | 60 // Get the current memory pressure level. |
| 59 base::MemoryPressureListener::MemoryPressureLevel GetCurrentPressureLevel() { | 61 MemoryPressureListener::MemoryPressureLevel GetCurrentPressureLevel() const |
| 60 return current_memory_pressure_level_; | 62 override; |
| 61 } | |
| 62 | 63 |
| 63 private: | 64 private: |
| 64 friend TestMemoryPressureObserver; | 65 friend TestMemoryPressureMonitor; |
| 65 // Starts observing the memory fill level. | 66 // Starts observing the memory fill level. |
| 66 // Calls to StartObserving should always be matched with calls to | 67 // Calls to StartObserving should always be matched with calls to |
| 67 // StopObserving. | 68 // StopObserving. |
| 68 void StartObserving(); | 69 void StartObserving(); |
| 69 | 70 |
| 70 // Stop observing the memory fill level. | 71 // Stop observing the memory fill level. |
| 71 // May be safely called if StartObserving has not been called. | 72 // May be safely called if StartObserving has not been called. |
| 72 void StopObserving(); | 73 void StopObserving(); |
| 73 | 74 |
| 74 // The function which gets periodically called to check any changes in the | 75 // The function which gets periodically called to check any changes in the |
| 75 // memory pressure. It will report pressure changes as well as continuous | 76 // memory pressure. It will report pressure changes as well as continuous |
| 76 // critical pressure levels. | 77 // critical pressure levels. |
| 77 void CheckMemoryPressure(); | 78 void CheckMemoryPressure(); |
| 78 | 79 |
| 79 // The function periodically checks the memory pressure changes and records | 80 // The function periodically checks the memory pressure changes and records |
| 80 // the UMA histogram statistics for the current memory pressure level. | 81 // the UMA histogram statistics for the current memory pressure level. |
| 81 void CheckMemoryPressureAndRecordStatistics(); | 82 void CheckMemoryPressureAndRecordStatistics(); |
| 82 | 83 |
| 83 // Get the memory pressure in percent (virtual for testing). | 84 // Get the memory pressure in percent (virtual for testing). |
| 84 virtual int GetUsedMemoryInPercent(); | 85 virtual int GetUsedMemoryInPercent(); |
| 85 | 86 |
| 86 // The current memory pressure. | 87 // The current memory pressure. |
| 87 base::MemoryPressureListener::MemoryPressureLevel | 88 base::MemoryPressureListener::MemoryPressureLevel |
| 88 current_memory_pressure_level_; | 89 current_memory_pressure_level_; |
| 89 | 90 |
| 90 // A periodic timer to check for resource pressure changes. This will get | 91 // A periodic timer to check for resource pressure changes. This will get |
| 91 // replaced by a kernel triggered event system (see crbug.com/381196). | 92 // replaced by a kernel triggered event system (see crbug.com/381196). |
| 92 base::RepeatingTimer<MemoryPressureObserverChromeOS> timer_; | 93 base::RepeatingTimer<MemoryPressureMonitorChromeOS> timer_; |
| 93 | 94 |
| 94 // To slow down the amount of moderate pressure event calls, this counter | 95 // To slow down the amount of moderate pressure event calls, this counter |
| 95 // gets used to count the number of events since the last event occured. | 96 // gets used to count the number of events since the last event occured. |
| 96 int moderate_pressure_repeat_count_; | 97 int moderate_pressure_repeat_count_; |
| 97 | 98 |
| 98 // The thresholds for moderate and critical pressure. | 99 // The thresholds for moderate and critical pressure. |
| 99 const int moderate_pressure_threshold_percent_; | 100 const int moderate_pressure_threshold_percent_; |
| 100 const int critical_pressure_threshold_percent_; | 101 const int critical_pressure_threshold_percent_; |
| 101 | 102 |
| 102 base::WeakPtrFactory<MemoryPressureObserverChromeOS> weak_ptr_factory_; | 103 // File descriptor used to detect low memory condition. |
| 104 ScopedFD low_mem_file_; |
| 103 | 105 |
| 104 DISALLOW_COPY_AND_ASSIGN(MemoryPressureObserverChromeOS); | 106 base::WeakPtrFactory<MemoryPressureMonitorChromeOS> weak_ptr_factory_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitorChromeOS); |
| 105 }; | 109 }; |
| 106 | 110 |
| 107 } // namespace base | 111 } // namespace base |
| 108 | 112 |
| 109 #endif // BASE_CHROMEOS_MEMORY_PRESSURE_OBSERVER_CHROMEOS_H_ | 113 #endif // BASE_CHROMEOS_MEMORY_PRESSURE_MONITOR_CHROMEOS_H_ |
| OLD | NEW |