Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / base / memory / memory_pressure_monitor_win.h
blob07f04eb893ebdb87a736b1daf31f1f93fcb4af05
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.
5 #ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_
6 #define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_
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"
15 // To not pull in windows.h.
16 typedef struct _MEMORYSTATUSEX MEMORYSTATUSEX;
18 namespace base {
19 namespace win {
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.
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;
37 // Constants governing the memory pressure level detection.
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;
49 // Default constructor. Will choose thresholds automatically basd on the
50 // actual amount of system memory.
51 MemoryPressureMonitor();
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);
57 ~MemoryPressureMonitor() override;
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();
63 // Get the current memory pressure level. This can be called from any thread.
64 MemoryPressureLevel GetCurrentPressureLevel() const override;
66 // Returns the moderate pressure level free memory threshold, in MB.
67 int moderate_threshold_mb() const { return moderate_threshold_mb_; }
69 // Returns the critical pressure level free memory threshold, in MB.
70 int critical_threshold_mb() const { return critical_threshold_mb_; }
72 protected:
73 // Internals are exposed for unittests.
75 // Automatically infers threshold values based on system memory. This invokes
76 // GetMemoryStatus so it can be mocked in unittests.
77 void InferThresholds();
79 // Starts observing the memory fill level. Calls to StartObserving should
80 // always be matched with calls to StopObserving.
81 void StartObserving();
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();
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();
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();
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();
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);
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_;
119 // A periodic timer to check for memory pressure changes.
120 base::RepeatingTimer<MemoryPressureMonitor> timer_;
122 // The current memory pressure.
123 MemoryPressureLevel current_memory_pressure_level_;
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_;
131 // Ensures that this object is used from a single thread.
132 base::ThreadChecker thread_checker_;
134 // Weak pointer factory to ourself used for scheduling calls to
135 // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|.
136 base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_;
138 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor);
141 } // namespace win
142 } // namespace base
144 #endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_