Chromium Code Reviews| Index: base/win/memory_pressure_monitor.h |
| diff --git a/base/win/memory_pressure_monitor.h b/base/win/memory_pressure_monitor.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2bf3345bf0f0aa6be8c3fe04161c21b6b8ff6b7c |
| --- /dev/null |
| +++ b/base/win/memory_pressure_monitor.h |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#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.
|
| +#define BASE_WIN_MEMORY_PRESSURE_MONITOR_WIN_H_ |
| + |
| +#include "base/base_export.h" |
| +#include "base/memory/memory_pressure_listener.h" |
| +#include "base/memory/memory_pressure_monitor.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/synchronization/lock.h" |
| +#include "base/timer/timer.h" |
| + |
| +// 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
|
| +typedef struct _MEMORYSTATUSEX MEMORYSTATUSEX; |
| + |
| +namespace base { |
| +namespace win { |
| + |
| +class TestMemoryPressureMonitor; |
| + |
| +// Windows memory pressure monitor. Because there is no OS provided signal this |
| +// polls at a low frequency (once per second), and applies internal hysteresis. |
| +class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor { |
| + public: |
| + using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; |
| + |
| + MemoryPressureMonitor(); |
| + 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
|
| + |
| + // Schedules a memory pressure check to run soon on the current thread. |
| + void CheckMemoryPressureSoon(); |
| + |
| + // Get the current memory pressure level. |
| + MemoryPressureLevel GetCurrentPressureLevel() const override; |
| + |
| + // Constants governing the polling and hysteresis behaviour of the observer. |
| + |
| + // The polling interval, in milliseconds. While under critical pressure, this |
| + // is also the timer to repeat cleanup attempts. |
| + 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.
|
| + // The time which should pass between 2 successive moderate memory pressure |
| + // signals, in milliseconds. |
| + static const int kModeratePressureCooldownMs; |
| + // The number of cycles that should pass between 2 successive moderate memory |
| + // pressure signals. |
| + static const int kModeratePressureCooldownCycles; |
| + |
| + private: |
| + friend TestMemoryPressureMonitor; |
| + |
| + // Starts observing the memory fill level. |
| + // Calls to StartObserving should always be matched with calls to |
| + // StopObserving. |
| + void StartObserving(); |
| + |
| + // Stop observing the memory fill level. |
| + // May be safely called if StartObserving has not been called. |
| + void StopObserving(); |
| + |
| + // Checks memory pressure, storing the current level, applying any hysteresis |
| + // and emitting memory pressure level change signals as necessary. This |
| + // function is called periodically while the monitor is observing memory |
| + // pressure. This is split out from CheckMemoryPressureAndRecordStatistics so |
| + // that it may be called by CheckMemoryPressureSoon and not invoke UMA |
| + // logging. Acquires and runs under lock_. |
| + void CheckMemoryPressure(); |
| + |
| + // Wrapper to CheckMemoryPressure that also records the observed memory |
| + // pressure level via an UMA enumeration. This is the function that is called |
| + // periodically by the timer. |
| + void CheckMemoryPressureAndRecordStatistics(); |
| + |
| + // Calculates the current instantaneous memory pressure level. This does not |
| + // use any hysteresis and simply returns the result at the current moment. |
| + MemoryPressureLevel CalculateCurrentPressureLevel(); |
| + |
| + // Gets system memory status. This is virtual as a unittesting hook. |
| + // Returns true if the system call succeeds, false otherwise. |
| + virtual bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status); |
| + |
| + // A periodic timer to check for memory pressure changes. |
| + base::RepeatingTimer<MemoryPressureMonitor> timer_; |
| + |
| + // A lock for synchronizing access to this class. |
| + base::Lock lock_; |
| + |
| + // The current memory pressure. |
| + MemoryPressureLevel current_memory_pressure_level_; // Under |lock_|. |
| + |
| + // To slow down the amount of moderate pressure event calls, this gets used to |
| + // count the number of events since the last event occured. This is used by |
| + // |CheckMemoryPressure| to apply hysteresis on the raw results of |
| + // |CalculateCurrentPressureLevel|. |
| + int moderate_pressure_repeat_count_; // Under |lock_|. |
| + |
| + // Weak pointer factory to ourself used for scheduling calls to |
| + // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|. |
| + base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); |
| +}; |
| + |
| +} // namespace win |
| +} // namespace base |
| + |
| +#endif // BASE_WIN_MEMORY_PRESSURE_MONITOR_WIN_H_ |