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 class TestMemoryPressureMonitor; | |
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 MemoryPressureMonitor(); | |
40 ~MemoryPressureMonitor() override; | |
41 | |
42 // Schedules a memory pressure check to run soon. This should be called on the | |
grt (UTC plus 2)
2015/05/06 16:31:09
nit: should -> must
| |
43 // same thread where the monitor was instantiated. | |
44 void CheckMemoryPressureSoon(); | |
45 | |
46 // Get the current memory pressure level. This can be called from any thread. | |
47 MemoryPressureLevel GetCurrentPressureLevel() const override; | |
48 | |
49 private: | |
50 friend TestMemoryPressureMonitor; | |
51 | |
52 // Starts observing the memory fill level. Calls to StartObserving should | |
53 // always be matched with calls to StopObserving. | |
54 void StartObserving(); | |
55 | |
56 // Stop observing the memory fill level. May be safely called if | |
57 // StartObserving has not been called. Must be called from the same thread on | |
58 // which the monitor was instantiated. | |
59 void StopObserving(); | |
60 | |
61 // Checks memory pressure, storing the current level, applying any hysteresis | |
62 // and emitting memory pressure level change signals as necessary. This | |
63 // function is called periodically while the monitor is observing memory | |
64 // pressure. This is split out from CheckMemoryPressureAndRecordStatistics so | |
65 // that it may be called by CheckMemoryPressureSoon and not invoke UMA | |
66 // logging. Must be called from the same thread on which the monitor was | |
67 // instantiated. | |
68 void CheckMemoryPressure(); | |
69 | |
70 // Wrapper to CheckMemoryPressure that also records the observed memory | |
71 // pressure level via an UMA enumeration. This is the function that is called | |
72 // periodically by the timer. Must be called from the same thread on which the | |
73 // monitor was instantiated. | |
74 void CheckMemoryPressureAndRecordStatistics(); | |
75 | |
76 // Calculates the current instantaneous memory pressure level. This does not | |
77 // use any hysteresis and simply returns the result at the current moment. Can | |
78 // be called on any thread. | |
79 MemoryPressureLevel CalculateCurrentPressureLevel(); | |
80 | |
81 // Gets system memory status. This is virtual as a unittesting hook. Returns | |
82 // true if the system call succeeds, false otherwise. Can be called on any | |
83 // thread. | |
84 virtual bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status); | |
85 | |
86 // A periodic timer to check for memory pressure changes. | |
87 base::RepeatingTimer<MemoryPressureMonitor> timer_; | |
88 | |
89 // The current memory pressure. | |
90 MemoryPressureLevel current_memory_pressure_level_; | |
91 | |
92 // To slow down the amount of moderate pressure event calls, this gets used to | |
93 // count the number of events since the last event occured. This is used by | |
94 // |CheckMemoryPressure| to apply hysteresis on the raw results of | |
95 // |CalculateCurrentPressureLevel|. | |
96 int moderate_pressure_repeat_count_; | |
97 | |
98 // Weak pointer factory to ourself used for scheduling calls to | |
99 // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|. | |
100 base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_; | |
101 | |
102 // Ensures that this object is used from a single thread. | |
103 base::ThreadChecker thread_checker_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); | |
106 }; | |
107 | |
108 } // namespace win | |
109 } // namespace base | |
110 | |
111 #endif // BASE_WIN_MEMORY_PRESSURE_MONITOR_H_ | |
OLD | NEW |