Chromium Code Reviews| 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_WIN_H_ | |
|
grt (UTC plus 2)
2015/05/06 13:26:36
remove _WIN unless you and Nico override my opinio
chrisha
2015/05/06 15:09:57
Done.
| |
| 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 // To not pull in windows.h. | |
|
grt (UTC plus 2)
2015/05/06 13:26:36
why? is this a thing that we do elsewhere in the c
chrisha
2015/05/06 15:09:57
Yes, this is common practice. A forward declaratio
grt (UTC plus 2)
2015/05/06 16:31:08
I've seen it for our own types, but I don't recall
| |
| 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 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; | |
| 28 | |
| 29 MemoryPressureMonitor(); | |
| 30 virtual ~MemoryPressureMonitor() override; | |
|
grt (UTC plus 2)
2015/05/06 13:26:36
no virtual when override is present
chrisha
2015/05/06 15:09:57
Still getting used to new C++ features... :/
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 // Constants governing the polling and hysteresis behaviour of the observer. | |
| 39 | |
| 40 // The polling interval, in milliseconds. While under critical pressure, this | |
| 41 // is also the timer to repeat cleanup attempts. | |
| 42 static const int kPollingIntervalMs; | |
|
grt (UTC plus 2)
2015/05/06 13:26:36
constants belong before the ctor as per http://goo
chrisha
2015/05/06 15:09:57
Done.
| |
| 43 // The time which should pass between 2 successive moderate memory pressure | |
| 44 // signals, in milliseconds. | |
| 45 static const int kModeratePressureCooldownMs; | |
| 46 // The number of cycles that should pass between 2 successive moderate memory | |
| 47 // pressure signals. | |
| 48 static const int kModeratePressureCooldownCycles; | |
| 49 | |
| 50 private: | |
| 51 friend TestMemoryPressureMonitor; | |
| 52 | |
| 53 // Starts observing the memory fill level. | |
| 54 // Calls to StartObserving should always be matched with calls to | |
| 55 // StopObserving. | |
| 56 void StartObserving(); | |
| 57 | |
| 58 // Stop observing the memory fill level. | |
| 59 // May be safely called if StartObserving has not been called. | |
| 60 void StopObserving(); | |
| 61 | |
| 62 // Checks memory pressure, storing the current level, applying any hysteresis | |
| 63 // and emitting memory pressure level change signals as necessary. This | |
| 64 // function is called periodically while the monitor is observing memory | |
| 65 // pressure. This is split out from CheckMemoryPressureAndRecordStatistics so | |
| 66 // that it may be called by CheckMemoryPressureSoon and not invoke UMA | |
| 67 // logging. Acquires and runs under lock_. | |
| 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. | |
| 73 void CheckMemoryPressureAndRecordStatistics(); | |
| 74 | |
| 75 // Calculates the current instantaneous memory pressure level. This does not | |
| 76 // use any hysteresis and simply returns the result at the current moment. | |
| 77 MemoryPressureLevel CalculateCurrentPressureLevel(); | |
| 78 | |
| 79 // Gets system memory status. This is virtual as a unittesting hook. | |
| 80 // Returns true if the system call succeeds, false otherwise. | |
| 81 virtual bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status); | |
| 82 | |
| 83 // A periodic timer to check for memory pressure changes. | |
| 84 base::RepeatingTimer<MemoryPressureMonitor> timer_; | |
| 85 | |
| 86 // A lock for synchronizing access to this class. | |
| 87 base::Lock lock_; | |
| 88 | |
| 89 // The current memory pressure. | |
| 90 MemoryPressureLevel current_memory_pressure_level_; // Under |lock_|. | |
| 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_; // Under |lock_|. | |
| 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 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); | |
| 103 }; | |
| 104 | |
| 105 } // namespace win | |
| 106 } // namespace base | |
| 107 | |
| 108 #endif // BASE_WIN_MEMORY_PRESSURE_MONITOR_WIN_H_ | |
| OLD | NEW |