Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: base/win/memory_pressure_monitor.h

Issue 1122863005: Create base::win::MemoryPressureMonitor class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 class TestMemoryPressureMonitor;
grt (UTC plus 2) 2015/05/07 02:12:11 in previous reads, i thought that this was the nam
chrisha 2015/05/07 21:17:36 Done.
22
23 // Windows memory pressure monitor. Because there is no OS provided signal this
24 // polls at a low frequency (once per second), and applies internal hysteresis.
25 class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
26 public:
27 // Constants governing the polling and hysteresis behaviour of the observer.
28
29 // The polling interval, in milliseconds. While under critical pressure, this
30 // is also the timer to repeat cleanup attempts.
31 static const int kPollingIntervalMs;
32 // The time which should pass between 2 successive moderate memory pressure
33 // signals, in milliseconds.
34 static const int kModeratePressureCooldownMs;
35 // The number of cycles that should pass between 2 successive moderate memory
36 // pressure signals.
37 static const int kModeratePressureCooldownCycles;
38
39 // Constants governing the memory pressure level detection.
40
41 // The amount of total system memory beyond which a system is considered to be
42 // a high-memory system.
43 static const int kHighMemoryThresholdMb;
44 // Default minimum free memory thresholds for low-memory systems, in MB.
45 static const int kLowMemoryDefsaultModerateThresholdMb;
46 static const int kLowMemoryDefaultCriticalThresholdMb;
47 // Default minimum free memory thresholds for high-memory systems, in MB.
48 static const int kHighMemoryDefaultModerateThresholdMb;
49 static const int kHighMemoryDefaultCriticalThresholdMb;
50
51 // Default constructor. Will choose thresholds automatically basd on the
52 // actual amount of system memory.
53 MemoryPressureMonitor();
54
55 // Constructor with explicit memory thresholds. These represent the amount of
56 // free memory below which the applicable memory pressure state engages.
57 MemoryPressureMonitor(int moderate_threshold_mb, int critical_threshold_mb);
58
59 ~MemoryPressureMonitor() override;
60
61 // Schedules a memory pressure check to run soon. This must be called on the
62 // same thread where the monitor was instantiated.
63 void CheckMemoryPressureSoon();
64
65 // Get the current memory pressure level. This can be called from any thread.
66 MemoryPressureLevel GetCurrentPressureLevel() const override;
67
68 // Returns the moderate pressure level free memory threshold, in MB.
69 int moderate_threshold_mb() const { return moderate_threshold_mb_; }
70
71 // Returns the critical pressure level free memory threshold, in MB.
72 int critical_threshold_mb() const { return critical_threshold_mb_; }
73
74 private:
75 friend TestMemoryPressureMonitor;
76
77 // Automatically infers threshold values based on system memory. This invokes
78 // GetMemoryStatus so it can be mocked in unittests.
79 void InferThresholds();
80
81 // Starts observing the memory fill level. Calls to StartObserving should
82 // always be matched with calls to StopObserving.
83 void StartObserving();
84
85 // Stop observing the memory fill level. May be safely called if
86 // StartObserving has not been called. Must be called from the same thread on
87 // which the monitor was instantiated.
88 void StopObserving();
89
90 // Checks memory pressure, storing the current level, applying any hysteresis
91 // and emitting memory pressure level change signals as necessary. This
92 // function is called periodically while the monitor is observing memory
93 // pressure. This is split out from CheckMemoryPressureAndRecordStatistics so
94 // that it may be called by CheckMemoryPressureSoon and not invoke UMA
95 // logging. Must be called from the same thread on which the monitor was
96 // instantiated.
97 void CheckMemoryPressure();
98
99 // Wrapper to CheckMemoryPressure that also records the observed memory
100 // pressure level via an UMA enumeration. This is the function that is called
101 // periodically by the timer. Must be called from the same thread on which the
102 // monitor was instantiated.
103 void CheckMemoryPressureAndRecordStatistics();
104
105 // Calculates the current instantaneous memory pressure level. This does not
106 // use any hysteresis and simply returns the result at the current moment. Can
107 // be called on any thread.
108 MemoryPressureLevel CalculateCurrentPressureLevel();
109
110 // Gets system memory status. This is virtual as a unittesting hook. Returns
111 // true if the system call succeeds, false otherwise. Can be called on any
112 // thread.
113 virtual bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status);
114
115 // Threshold amounts of available memory that trigger pressure levels. See
116 // memory_pressure_monitor.cc for a discussion of reasonable values for these.
117 int moderate_threshold_mb_;
118 int critical_threshold_mb_;
119
120 // A periodic timer to check for memory pressure changes.
121 base::RepeatingTimer<MemoryPressureMonitor> timer_;
122
123 // The current memory pressure.
124 MemoryPressureLevel current_memory_pressure_level_;
125
126 // To slow down the amount of moderate pressure event calls, this gets used to
127 // count the number of events since the last event occured. This is used by
128 // |CheckMemoryPressure| to apply hysteresis on the raw results of
129 // |CalculateCurrentPressureLevel|.
130 int moderate_pressure_repeat_count_;
131
132 // Weak pointer factory to ourself used for scheduling calls to
133 // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|.
134 base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_;
135
136 // Ensures that this object is used from a single thread.
137 base::ThreadChecker thread_checker_;
138
139 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor);
140 };
141
142 } // namespace win
143 } // namespace base
144
145 #endif // BASE_WIN_MEMORY_PRESSURE_MONITOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698