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

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: Addressed grt's comments. 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_WIN_H_
6 #define BASE_WIN_MEMORY_PRESSURE_MONITOR_WIN_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/synchronization/lock.h"
13 #include "base/timer/timer.h"
14
15 // Forward declaration. This is defined by windows.h.
16 struct _MEMORYSTATUSEX;
Nico 2015/05/05 21:25:12 If you want, you can also say typedef struct _MEM
chrisha 2015/05/06 02:40:24 Done.
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.
Nico 2015/05/05 21:25:12 :-( Is once per minute enough? Can we make it ob
chrisha 2015/05/06 02:40:24 Once per minute is maybe too low, but we could lik
25 class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
26 public:
27 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
28
29 MemoryPressureMonitor();
30 virtual ~MemoryPressureMonitor() override;
Nico 2015/05/05 21:25:11 no virtual
chrisha 2015/05/06 02:40:24 Done.
31
32 // Schedules a memory pressure check to run soon on the current thread.
33 void CheckMemoryPressureSoon();
34
35 // Get the current memory pressure level.
36 MemoryPressureLevel GetCurrentPressureLevel() const override;
37
38 private:
39 friend TestMemoryPressureMonitor;
40
41 // Starts observing the memory fill level.
42 // Calls to StartObserving should always be matched with calls to
43 // StopObserving.
44 void StartObserving();
45
46 // Stop observing the memory fill level.
47 // May be safely called if StartObserving has not been called.
48 void StopObserving();
49
50 // Checks memory pressure, storing the current level, applying any hysteresis
51 // and emitting memory pressure level change signals as necessary. This
52 // function is called periodically while the monitor is observing memory
53 // pressure. This is split out from CheckMemoryPressureAndRecordStatistics so
54 // that it may be called by CheckMemoryPressureSoon and not invoke UMA
55 // logging. Acquires and runs under lock_.
56 void CheckMemoryPressure();
57
58 // Wrapper to CheckMemoryPressure that also records the observed memory
59 // pressure level via an UMA enumeration. This is the function that is called
60 // periodically by the timer.
61 void CheckMemoryPressureAndRecordStatistics();
62
63 // Calculates the current instantaneous memory pressure level. This does not
64 // use any hysteresis and simply returns the result at the current moment.
65 MemoryPressureLevel CalculateCurrentPressureLevel();
66
67 // Gets system memory status. This is virtual as a unittesting hook.
68 // Returns true if the system call succeeds, false otherwise.
69 virtual bool GetSystemMemoryStatus(_MEMORYSTATUSEX* mem_status);
70
71 // A periodic timer to check for memory pressure changes.
72 base::RepeatingTimer<MemoryPressureMonitor> timer_;
73
74 // A lock for synchronizing access to this class.
75 base::Lock lock_;
76
77 // The current memory pressure.
78 MemoryPressureLevel current_memory_pressure_level_; // Under |lock_|.
79
80 // To slow down the amount of moderate pressure event calls, this gets used to
81 // count the number of events since the last event occured. This is used by
82 // |CheckMemoryPressure| to apply hysteresis on the raw results of
83 // |CalculateCurrentPressureLevel|.
84 int moderate_pressure_repeat_count_; // Under |lock_|.
85
86 // Weak pointer factory to ourself used for scheduling calls to
87 // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|.
88 base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_;
89
90 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor);
91 };
92
93 } // namespace win
94 } // namespace base
95
96 #endif // BASE_WIN_MEMORY_PRESSURE_MONITOR_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698