OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_WIN_MEMORY_PRESSURE_MONITOR_H_ |
| 6 #define BASE_WIN_MEMORY_PRESSURE_MONITOR_H_ |
| 7 |
| 8 #include "base/base_export.h" |
| 9 #include "base/memory/memory_pressure_listener.h" |
| 10 #include "base/memory/memory_pressure_monitor.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/threading/thread_checker.h" |
| 13 #include "base/timer/timer.h" |
| 14 |
| 15 // To not pull in windows.h. |
| 16 typedef struct _MEMORYSTATUSEX MEMORYSTATUSEX; |
| 17 |
| 18 namespace base { |
| 19 namespace win { |
| 20 |
| 21 // Windows memory pressure monitor. Because there is no OS provided signal this |
| 22 // polls at a low frequency (once per second), and applies internal hysteresis. |
| 23 class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor { |
| 24 public: |
| 25 // Constants governing the polling and hysteresis behaviour of the observer. |
| 26 |
| 27 // The polling interval, in milliseconds. While under critical pressure, this |
| 28 // is also the timer to repeat cleanup attempts. |
| 29 static const int kPollingIntervalMs; |
| 30 // The time which should pass between 2 successive moderate memory pressure |
| 31 // signals, in milliseconds. |
| 32 static const int kModeratePressureCooldownMs; |
| 33 // The number of cycles that should pass between 2 successive moderate memory |
| 34 // pressure signals. |
| 35 static const int kModeratePressureCooldownCycles; |
| 36 |
| 37 // Constants governing the memory pressure level detection. |
| 38 |
| 39 // The amount of total system memory beyond which a system is considered to be |
| 40 // a large-memory system. |
| 41 static const int kLargeMemoryThresholdMb; |
| 42 // Default minimum free memory thresholds for small-memory systems, in MB. |
| 43 static const int kSmallMemoryDefaultModerateThresholdMb; |
| 44 static const int kSmallMemoryDefaultCriticalThresholdMb; |
| 45 // Default minimum free memory thresholds for large-memory systems, in MB. |
| 46 static const int kLargeMemoryDefaultModerateThresholdMb; |
| 47 static const int kLargeMemoryDefaultCriticalThresholdMb; |
| 48 |
| 49 // Default constructor. Will choose thresholds automatically basd on the |
| 50 // actual amount of system memory. |
| 51 MemoryPressureMonitor(); |
| 52 |
| 53 // Constructor with explicit memory thresholds. These represent the amount of |
| 54 // free memory below which the applicable memory pressure state engages. |
| 55 MemoryPressureMonitor(int moderate_threshold_mb, int critical_threshold_mb); |
| 56 |
| 57 ~MemoryPressureMonitor() override; |
| 58 |
| 59 // Schedules a memory pressure check to run soon. This must be called on the |
| 60 // same thread where the monitor was instantiated. |
| 61 void CheckMemoryPressureSoon(); |
| 62 |
| 63 // Get the current memory pressure level. This can be called from any thread. |
| 64 MemoryPressureLevel GetCurrentPressureLevel() const override; |
| 65 |
| 66 // Returns the moderate pressure level free memory threshold, in MB. |
| 67 int moderate_threshold_mb() const { return moderate_threshold_mb_; } |
| 68 |
| 69 // Returns the critical pressure level free memory threshold, in MB. |
| 70 int critical_threshold_mb() const { return critical_threshold_mb_; } |
| 71 |
| 72 protected: |
| 73 // Internals are exposed for unittests. |
| 74 |
| 75 // Automatically infers threshold values based on system memory. This invokes |
| 76 // GetMemoryStatus so it can be mocked in unittests. |
| 77 void InferThresholds(); |
| 78 |
| 79 // Starts observing the memory fill level. Calls to StartObserving should |
| 80 // always be matched with calls to StopObserving. |
| 81 void StartObserving(); |
| 82 |
| 83 // Stop observing the memory fill level. May be safely called if |
| 84 // StartObserving has not been called. Must be called from the same thread on |
| 85 // which the monitor was instantiated. |
| 86 void StopObserving(); |
| 87 |
| 88 // Checks memory pressure, storing the current level, applying any hysteresis |
| 89 // and emitting memory pressure level change signals as necessary. This |
| 90 // function is called periodically while the monitor is observing memory |
| 91 // pressure. This is split out from CheckMemoryPressureAndRecordStatistics so |
| 92 // that it may be called by CheckMemoryPressureSoon and not invoke UMA |
| 93 // logging. Must be called from the same thread on which the monitor was |
| 94 // instantiated. |
| 95 void CheckMemoryPressure(); |
| 96 |
| 97 // Wrapper to CheckMemoryPressure that also records the observed memory |
| 98 // pressure level via an UMA enumeration. This is the function that is called |
| 99 // periodically by the timer. Must be called from the same thread on which the |
| 100 // monitor was instantiated. |
| 101 void CheckMemoryPressureAndRecordStatistics(); |
| 102 |
| 103 // Calculates the current instantaneous memory pressure level. This does not |
| 104 // use any hysteresis and simply returns the result at the current moment. Can |
| 105 // be called on any thread. |
| 106 MemoryPressureLevel CalculateCurrentPressureLevel(); |
| 107 |
| 108 // Gets system memory status. This is virtual as a unittesting hook. Returns |
| 109 // true if the system call succeeds, false otherwise. Can be called on any |
| 110 // thread. |
| 111 virtual bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status); |
| 112 |
| 113 private: |
| 114 // Threshold amounts of available memory that trigger pressure levels. See |
| 115 // memory_pressure_monitor.cc for a discussion of reasonable values for these. |
| 116 int moderate_threshold_mb_; |
| 117 int critical_threshold_mb_; |
| 118 |
| 119 // A periodic timer to check for memory pressure changes. |
| 120 base::RepeatingTimer<MemoryPressureMonitor> timer_; |
| 121 |
| 122 // The current memory pressure. |
| 123 MemoryPressureLevel current_memory_pressure_level_; |
| 124 |
| 125 // To slow down the amount of moderate pressure event calls, this gets used to |
| 126 // count the number of events since the last event occured. This is used by |
| 127 // |CheckMemoryPressure| to apply hysteresis on the raw results of |
| 128 // |CalculateCurrentPressureLevel|. |
| 129 int moderate_pressure_repeat_count_; |
| 130 |
| 131 // Ensures that this object is used from a single thread. |
| 132 base::ThreadChecker thread_checker_; |
| 133 |
| 134 // Weak pointer factory to ourself used for scheduling calls to |
| 135 // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|. |
| 136 base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); |
| 139 }; |
| 140 |
| 141 } // namespace win |
| 142 } // namespace base |
| 143 |
| 144 #endif // BASE_WIN_MEMORY_PRESSURE_MONITOR_H_ |
OLD | NEW |