OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_MEMORY_MEMORY_PRESSURE_MONITOR_H_ | 5 #ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_ |
6 #define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_ | 6 #define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/memory/memory_pressure_listener.h" | 9 #include "base/memory/memory_pressure_listener.h" |
| 10 #include "base/memory/memory_pressure_monitor.h" |
| 11 |
| 12 #if defined(OS_CHROMEOS) || defined(OS_LINUX) || defined(OS_WIN) |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "base/timer/timer.h" |
| 16 #endif |
| 17 |
| 18 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 19 #include "base/gtest_prod_util.h" |
| 20 #include "base/macros.h" |
| 21 #endif |
| 22 |
| 23 #if defined(OS_CHROMEOS) |
| 24 #include "base/files/scoped_file.h" |
| 25 #endif |
| 26 |
| 27 #if defined(OS_WIN) |
| 28 // To not pull in windows.h. |
| 29 typedef struct _MEMORYSTATUSEX MEMORYSTATUSEX; |
| 30 #endif |
| 31 |
| 32 #if defined(OS_MACOSX) |
| 33 #include <dispatch/dispatch.h> |
| 34 |
| 35 // The following was added to <dispatch/source.h> after 10.8. |
| 36 // TODO(shrike): Remove the DISPATCH_MEMORYPRESSURE_NORMAL ifndef once builders |
| 37 // reach 10.9 or higher. |
| 38 #ifndef DISPATCH_MEMORYPRESSURE_NORMAL |
| 39 |
| 40 #define DISPATCH_MEMORYPRESSURE_NORMAL 0x01 |
| 41 #define DISPATCH_MEMORYPRESSURE_WARN 0x02 |
| 42 #define DISPATCH_MEMORYPRESSURE_CRITICAL 0x04 |
| 43 |
| 44 #endif // DISPATCH_MEMORYPRESSURE_NORMAL |
| 45 #endif |
10 | 46 |
11 namespace base { | 47 namespace base { |
12 | 48 |
13 // TODO(chrisha): Make this a concrete class with per-OS implementations rather | 49 class TestMemoryPressureMonitor; |
14 // than an abstract base class. | 50 |
| 51 #if defined(OS_MACOSX) |
| 52 struct DispatchSourceSDeleter { |
| 53 void operator()(dispatch_source_s* ptr) { |
| 54 dispatch_source_cancel(ptr); |
| 55 dispatch_release(ptr); |
| 56 } |
| 57 }; |
| 58 #endif |
15 | 59 |
16 // Declares the interface for a MemoryPressureMonitor. There are multiple | 60 // Declares the interface for a MemoryPressureMonitor. There are multiple |
17 // OS specific implementations of this class. An instance of the memory | 61 // OS specific implementations of this class. An instance of the memory |
18 // pressure observer is created at the process level, tracks memory usage, and | 62 // pressure observer is created at the process level, tracks memory usage, and |
19 // pushes memory state change notifications to the static function | 63 // pushes memory state change notifications to the static function |
20 // base::MemoryPressureListener::NotifyMemoryPressure. This is turn notifies | 64 // base::MemoryPressureListener::NotifyMemoryPressure. This is turn notifies |
21 // all MemoryPressureListener instances via a callback. | 65 // all MemoryPressureListener instances via a callback. |
22 class BASE_EXPORT MemoryPressureMonitor { | 66 class BASE_EXPORT MemoryPressureMonitor { |
23 public: | 67 public: |
24 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; | 68 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; |
25 | 69 |
26 virtual ~MemoryPressureMonitor(); | 70 virtual ~MemoryPressureMonitor(); |
27 | 71 |
28 // Return the singleton MemoryPressureMonitor. | 72 // Return the singleton MemoryPressureMonitor. |
29 static MemoryPressureMonitor* Get(); | 73 static MemoryPressureMonitor* Get(); |
30 | 74 |
31 // Returns the currently observed memory pressure. | 75 // Get the current memory pressure level. |
32 virtual MemoryPressureLevel GetCurrentPressureLevel() const = 0; | 76 virtual MemoryPressureLevel GetCurrentPressureLevel() const; |
| 77 |
| 78 #if defined(OS_CHROMEOS) |
| 79 // There are two memory pressure events: |
| 80 // MODERATE - which will mainly release caches. |
| 81 // CRITICAL - which will discard tabs. |
| 82 // The |MemoryPressureThresholds| enum selects the strategy of firing these |
| 83 // events: A conservative strategy will keep as much content in memory as |
| 84 // possible (causing the system to swap to zram) and an aggressive strategy |
| 85 // will release memory earlier to avoid swapping. |
| 86 enum MemoryPressureThresholds { |
| 87 // Use the system default. |
| 88 THRESHOLD_DEFAULT = 0, |
| 89 // Try to keep as much content in memory as possible. |
| 90 THRESHOLD_CONSERVATIVE = 1, |
| 91 // Discard caches earlier, allowing to keep more tabs in memory. |
| 92 THRESHOLD_AGGRESSIVE_CACHE_DISCARD = 2, |
| 93 // Discard tabs earlier, allowing the system to get faster. |
| 94 THRESHOLD_AGGRESSIVE_TAB_DISCARD = 3, |
| 95 // Discard caches and tabs earlier to allow the system to be faster. |
| 96 THRESHOLD_AGGRESSIVE = 4 |
| 97 }; |
| 98 MemoryPressureMonitor(MemoryPressureThresholds thresholds); |
| 99 #endif |
| 100 |
| 101 #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_WIN) |
| 102 MemoryPressureMonitor(); |
| 103 #endif |
| 104 |
| 105 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 106 using GetUsedMemoryInPercentCallback = int (*)(); |
| 107 // Redo the memory pressure calculation soon and call again if a critical |
| 108 // memory pressure prevails. Note that this call will trigger an asynchronous |
| 109 // action which gives the system time to release memory back into the pool. |
| 110 void ScheduleEarlyCheck(); |
| 111 #endif |
| 112 |
| 113 #if defined(OS_WIN) |
| 114 // Constants governing the polling and hysteresis behaviour of the observer. |
| 115 |
| 116 // The polling interval, in milliseconds. While under critical pressure, this |
| 117 // is also the timer to repeat cleanup attempts. |
| 118 static const int kPollingIntervalMs; |
| 119 // The time which should pass between 2 successive moderate memory pressure |
| 120 // signals, in milliseconds. |
| 121 static const int kModeratePressureCooldownMs; |
| 122 // The number of cycles that should pass between 2 successive moderate memory |
| 123 // pressure signals. |
| 124 static const int kModeratePressureCooldownCycles; |
| 125 |
| 126 // Constants governing the memory pressure level detection. |
| 127 |
| 128 // The amount of total system memory beyond which a system is considered to be |
| 129 // a large-memory system. |
| 130 static const int kLargeMemoryThresholdMb; |
| 131 // Default minimum free memory thresholds for small-memory systems, in MB. |
| 132 static const int kSmallMemoryDefaultModerateThresholdMb; |
| 133 static const int kSmallMemoryDefaultCriticalThresholdMb; |
| 134 // Default minimum free memory thresholds for large-memory systems, in MB. |
| 135 static const int kLargeMemoryDefaultModerateThresholdMb; |
| 136 static const int kLargeMemoryDefaultCriticalThresholdMb; |
| 137 |
| 138 // Constructor with explicit memory thresholds. These represent the amount of |
| 139 // free memory below which the applicable memory pressure state engages. |
| 140 MemoryPressureMonitor(int moderate_threshold_mb, int critical_threshold_mb); |
| 141 |
| 142 // Schedules a memory pressure check to run soon. This must be called on the |
| 143 // same thread where the monitor was instantiated. |
| 144 void CheckMemoryPressureSoon(); |
| 145 |
| 146 // Returns the moderate pressure level free memory threshold, in MB. |
| 147 int moderate_threshold_mb() const { return moderate_threshold_mb_; } |
| 148 |
| 149 // Returns the critical pressure level free memory threshold, in MB. |
| 150 int critical_threshold_mb() const { return critical_threshold_mb_; } |
| 151 #endif |
33 | 152 |
34 protected: | 153 protected: |
35 MemoryPressureMonitor(); | 154 #if defined(OS_WIN) |
| 155 // Internals are exposed for unittests. |
| 156 |
| 157 // Automatically infers threshold values based on system memory. This invokes |
| 158 // GetMemoryStatus so it can be mocked in unittests. |
| 159 void InferThresholds(); |
| 160 |
| 161 // Starts observing the memory fill level. Calls to StartObserving should |
| 162 // always be matched with calls to StopObserving. |
| 163 void StartObserving(); |
| 164 |
| 165 // Stop observing the memory fill level. May be safely called if |
| 166 // StartObserving has not been called. Must be called from the same thread on |
| 167 // which the monitor was instantiated. |
| 168 void StopObserving(); |
| 169 |
| 170 // Checks memory pressure, storing the current level, applying any hysteresis |
| 171 // and emitting memory pressure level change signals as necessary. This |
| 172 // function is called periodically while the monitor is observing memory |
| 173 // pressure. This is split out from CheckMemoryPressureAndRecordStatistics so |
| 174 // that it may be called by CheckMemoryPressureSoon and not invoke UMA |
| 175 // logging. Must be called from the same thread on which the monitor was |
| 176 // instantiated. |
| 177 void CheckMemoryPressure(); |
| 178 |
| 179 // Wrapper to CheckMemoryPressure that also records the observed memory |
| 180 // pressure level via an UMA enumeration. This is the function that is called |
| 181 // periodically by the timer. Must be called from the same thread on which the |
| 182 // monitor was instantiated. |
| 183 void CheckMemoryPressureAndRecordStatistics(); |
| 184 |
| 185 // Calculates the current instantaneous memory pressure level. This does not |
| 186 // use any hysteresis and simply returns the result at the current moment. Can |
| 187 // be called on any thread. |
| 188 MemoryPressureLevel CalculateCurrentPressureLevel(); |
| 189 |
| 190 // Gets system memory status. This is virtual as a unittesting hook. Returns |
| 191 // true if the system call succeeds, false otherwise. Can be called on any |
| 192 // thread. |
| 193 virtual bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status); |
| 194 #endif |
36 | 195 |
37 private: | 196 private: |
| 197 friend TestMemoryPressureMonitor; |
38 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); | 198 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); |
| 199 |
| 200 static MemoryPressureMonitor* g_monitor; |
| 201 |
| 202 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 203 friend TestMemoryPressureMonitor; |
| 204 // Starts observing the memory fill level. |
| 205 // Calls to StartObserving should always be matched with calls to |
| 206 // StopObserving. |
| 207 void StartObserving(); |
| 208 |
| 209 // Stop observing the memory fill level. |
| 210 // May be safely called if StartObserving has not been called. |
| 211 void StopObserving(); |
| 212 |
| 213 // The function which gets periodically called to check any changes in the |
| 214 // memory pressure. It will report pressure changes as well as continuous |
| 215 // critical pressure levels. |
| 216 void CheckMemoryPressure(); |
| 217 |
| 218 // The function periodically checks the memory pressure changes and records |
| 219 // the UMA histogram statistics for the current memory pressure level. |
| 220 void CheckMemoryPressureAndRecordStatistics(); |
| 221 |
| 222 // Get the memory pressure in percent (virtual for testing). |
| 223 virtual int GetUsedMemoryInPercent(); |
| 224 |
| 225 // The current memory pressure. |
| 226 base::MemoryPressureListener::MemoryPressureLevel |
| 227 current_memory_pressure_level_; |
| 228 |
| 229 // A periodic timer to check for resource pressure changes. This will get |
| 230 // replaced by a kernel triggered event system (see crbug.com/381196). |
| 231 base::RepeatingTimer<MemoryPressureMonitor> timer_; |
| 232 |
| 233 // To slow down the amount of moderate pressure event calls, this counter |
| 234 // gets used to count the number of events since the last event occured. |
| 235 int moderate_pressure_repeat_count_; |
| 236 #endif |
| 237 |
| 238 #if defined(OS_CHROMEOS) |
| 239 // The thresholds for moderate and critical pressure. |
| 240 const int moderate_pressure_threshold_percent_; |
| 241 const int critical_pressure_threshold_percent_; |
| 242 |
| 243 // File descriptor used to detect low memory condition. |
| 244 ScopedFD low_mem_file_; |
| 245 #endif |
| 246 |
| 247 #if defined(OS_WIN) |
| 248 // Threshold amounts of available memory that trigger pressure levels. See |
| 249 // memory_pressure_monitor.cc for a discussion of reasonable values for these. |
| 250 int moderate_threshold_mb_; |
| 251 int critical_threshold_mb_; |
| 252 |
| 253 // A periodic timer to check for memory pressure changes. |
| 254 base::RepeatingTimer<MemoryPressureMonitor> timer_; |
| 255 |
| 256 // The current memory pressure. |
| 257 MemoryPressureLevel current_memory_pressure_level_; |
| 258 |
| 259 // To slow down the amount of moderate pressure event calls, this gets used to |
| 260 // count the number of events since the last event occured. This is used by |
| 261 // |CheckMemoryPressure| to apply hysteresis on the raw results of |
| 262 // |CalculateCurrentPressureLevel|. |
| 263 int moderate_pressure_repeat_count_; |
| 264 |
| 265 // Ensures that this object is used from a single thread. |
| 266 base::ThreadChecker thread_checker_; |
| 267 #endif |
| 268 |
| 269 #if defined(OS_MACOSX) |
| 270 static MemoryPressureLevel MemoryPressureLevelForMacMemoryPressure( |
| 271 int mac_memory_pressure); |
| 272 static void NotifyMemoryPressureChanged(dispatch_source_s* event_source); |
| 273 |
| 274 scoped_ptr<dispatch_source_s, DispatchSourceSDeleter> |
| 275 memory_level_event_source_; |
| 276 #endif |
| 277 |
| 278 #if defined(OS_CHROMEOS) || defined(OS_LINUX) || defined(OS_WIN) |
| 279 // Weak pointer factory to ourself used for scheduling calls to |
| 280 // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|. |
| 281 base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_; |
| 282 #endif |
39 }; | 283 }; |
40 | 284 |
41 } // namespace base | 285 } // namespace base |
42 | 286 |
43 #endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_ | 287 #endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_H_ |
OLD | NEW |