Commit a68d66fc authored by Ryan Powell's avatar Ryan Powell Committed by Commit Bot

Create SystemMemoryPressureEvaluator from Win MemoryPressureMonitor.

This CL creates the SystemMemoryPressureEvaluator for Windows, as part
of a refactoring of the MemoryPressureMonitor to use the
Voter-Aggregator design described here:
https://docs.google.com/document/d/1W3FPDyjIAKBcFGNYsHA3EKR1FHrJlbBaqT4_RUnxzq0/edit?ts=5d3f5714#heading=h.7nki9mck5t64
This Evaluator calculates memory pressure and emits notifications
identically to the previous Monitor, but does so by casting its votes
for memory pressure through a MemoryPressureVoter.

Bug: 980965
Change-Id: Ib2fe6e7699762c209411a08ea1c938998bffe641
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1721376Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarSébastien Marchand <sebmarchand@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Ryan Powell <ryanpow@google.com>
Cr-Commit-Position: refs/heads/master@{#688273}
parent c30e72d1
...@@ -427,8 +427,6 @@ jumbo_component("base") { ...@@ -427,8 +427,6 @@ jumbo_component("base") {
"memory/memory_pressure_monitor_chromeos.h", "memory/memory_pressure_monitor_chromeos.h",
"memory/memory_pressure_monitor_mac.cc", "memory/memory_pressure_monitor_mac.cc",
"memory/memory_pressure_monitor_mac.h", "memory/memory_pressure_monitor_mac.h",
"memory/memory_pressure_monitor_win.cc",
"memory/memory_pressure_monitor_win.h",
"memory/platform_shared_memory_region.cc", "memory/platform_shared_memory_region.cc",
"memory/platform_shared_memory_region.h", "memory/platform_shared_memory_region.h",
"memory/protected_memory.cc", "memory/protected_memory.cc",
...@@ -2597,7 +2595,6 @@ test("base_unittests") { ...@@ -2597,7 +2595,6 @@ test("base_unittests") {
"memory/memory_pressure_monitor_chromeos_unittest.cc", "memory/memory_pressure_monitor_chromeos_unittest.cc",
"memory/memory_pressure_monitor_mac_unittest.cc", "memory/memory_pressure_monitor_mac_unittest.cc",
"memory/memory_pressure_monitor_unittest.cc", "memory/memory_pressure_monitor_unittest.cc",
"memory/memory_pressure_monitor_win_unittest.cc",
"memory/platform_shared_memory_region_unittest.cc", "memory/platform_shared_memory_region_unittest.cc",
"memory/protected_memory_unittest.cc", "memory/protected_memory_unittest.cc",
"memory/ptr_util_unittest.cc", "memory/ptr_util_unittest.cc",
......
...@@ -8,6 +8,10 @@ source_set("memory_pressure") { ...@@ -8,6 +8,10 @@ source_set("memory_pressure") {
"memory_pressure_voter.h", "memory_pressure_voter.h",
"multi_source_memory_pressure_monitor.cc", "multi_source_memory_pressure_monitor.cc",
"multi_source_memory_pressure_monitor.h", "multi_source_memory_pressure_monitor.h",
"system_memory_pressure_evaluator.cc",
"system_memory_pressure_evaluator.h",
"system_memory_pressure_evaluator_win.cc",
"system_memory_pressure_evaluator_win.h",
] ]
deps = [ deps = [
...@@ -20,12 +24,14 @@ source_set("unittests") { ...@@ -20,12 +24,14 @@ source_set("unittests") {
sources = [ sources = [
"memory_pressure_voter_unittest.cc", "memory_pressure_voter_unittest.cc",
"multi_source_memory_pressure_monitor_unittest.cc", "multi_source_memory_pressure_monitor_unittest.cc",
"system_memory_pressure_evaluator_win_unittest.cc",
] ]
deps = [ deps = [
":memory_pressure", ":memory_pressure",
"//base", "//base",
"//base/test:test_support", "//base/test:test_support",
"//testing/gmock",
"//testing/gtest", "//testing/gtest",
] ]
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/util/memory_pressure/system_memory_pressure_evaluator.h"
namespace util { namespace util {
...@@ -16,6 +17,11 @@ MultiSourceMemoryPressureMonitor::MultiSourceMemoryPressureMonitor() ...@@ -16,6 +17,11 @@ MultiSourceMemoryPressureMonitor::MultiSourceMemoryPressureMonitor()
dispatch_callback_(base::BindRepeating( dispatch_callback_(base::BindRepeating(
&base::MemoryPressureListener::NotifyMemoryPressure)), &base::MemoryPressureListener::NotifyMemoryPressure)),
aggregator_(this) { aggregator_(this) {
// This can't be in the parameter list because |sequence_checker_| wouldn't be
// available, which would be needed by the |system_evaluator_|'s constructor's
// call to CreateVoter().
system_evaluator_ =
SystemMemoryPressureEvaluator::CreateDefaultSystemEvaluator(this);
StartMetricsTimer(); StartMetricsTimer();
} }
...@@ -63,4 +69,8 @@ void MultiSourceMemoryPressureMonitor::OnNotifyListenersRequested() { ...@@ -63,4 +69,8 @@ void MultiSourceMemoryPressureMonitor::OnNotifyListenersRequested() {
dispatch_callback_.Run(current_pressure_level_); dispatch_callback_.Run(current_pressure_level_);
} }
void MultiSourceMemoryPressureMonitor::ResetSystemEvaluatorForTesting() {
system_evaluator_.reset();
}
} // namespace util } // namespace util
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
namespace util { namespace util {
class SystemMemoryPressureEvaluator;
// This is a specialization of a MemoryPressureMonitor that relies on a set of // This is a specialization of a MemoryPressureMonitor that relies on a set of
// MemoryPressureVoters to determine the memory pressure state. The // MemoryPressureVoters to determine the memory pressure state. The
// MemoryPressureVoteAggregator is in charge of receiving votes from these // MemoryPressureVoteAggregator is in charge of receiving votes from these
...@@ -40,6 +42,8 @@ class MultiSourceMemoryPressureMonitor ...@@ -40,6 +42,8 @@ class MultiSourceMemoryPressureMonitor
return &aggregator_; return &aggregator_;
} }
void ResetSystemEvaluatorForTesting();
protected: protected:
void StartMetricsTimer(); void StartMetricsTimer();
void StopMetricsTimer(); void StopMetricsTimer();
...@@ -58,6 +62,8 @@ class MultiSourceMemoryPressureMonitor ...@@ -58,6 +62,8 @@ class MultiSourceMemoryPressureMonitor
MemoryPressureVoteAggregator aggregator_; MemoryPressureVoteAggregator aggregator_;
std::unique_ptr<SystemMemoryPressureEvaluator> system_evaluator_;
// A periodic timer to record UMA metrics. // A periodic timer to record UMA metrics.
base::RepeatingTimer metric_timer_; base::RepeatingTimer metric_timer_;
......
// Copyright 2019 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.
#include "base/util/memory_pressure/system_memory_pressure_evaluator.h"
#include "build/build_config.h"
#if defined(OS_WIN)
#include "base/util/memory_pressure/system_memory_pressure_evaluator_win.h"
#endif
namespace util {
// static
std::unique_ptr<SystemMemoryPressureEvaluator>
SystemMemoryPressureEvaluator::CreateDefaultSystemEvaluator(
MultiSourceMemoryPressureMonitor* monitor) {
#if defined(OS_WIN)
return std::make_unique<util::win::SystemMemoryPressureEvaluator>(
monitor->CreateVoter());
#endif
return nullptr;
}
SystemMemoryPressureEvaluator::SystemMemoryPressureEvaluator() = default;
} // namespace util
// Copyright 2019 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_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_H_
#define BASE_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_H_
#include "base/util/memory_pressure/memory_pressure_voter.h"
#include "base/util/memory_pressure/multi_source_memory_pressure_monitor.h"
namespace util {
// Base class for the platform SystemMemoryPressureEvaluators, which use
// MemoryPressureVoters to cast their vote on the overall MemoryPressureLevel.
class SystemMemoryPressureEvaluator {
public:
// Used by the MemoryPressureMonitor to create the correct Evaluator for the
// platform in use.
static std::unique_ptr<SystemMemoryPressureEvaluator>
CreateDefaultSystemEvaluator(MultiSourceMemoryPressureMonitor* monitor);
virtual ~SystemMemoryPressureEvaluator() = default;
protected:
SystemMemoryPressureEvaluator();
};
} // namespace util
#endif // BASE_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_H_
// Copyright 2015 The Chromium Authors. All rights reserved. // Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/memory/memory_pressure_monitor_win.h" #include "base/util/memory_pressure/system_memory_pressure_evaluator_win.h"
#include <windows.h> #include <windows.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/util/memory_pressure/multi_source_memory_pressure_monitor.h"
namespace base { namespace util {
namespace win { namespace win {
namespace { namespace {
...@@ -24,75 +25,74 @@ static const DWORDLONG kMBBytes = 1024 * 1024; ...@@ -24,75 +25,74 @@ static const DWORDLONG kMBBytes = 1024 * 1024;
// memory pressure monitor. The values were determined experimentally to ensure // memory pressure monitor. The values were determined experimentally to ensure
// sufficient responsiveness of the memory pressure subsystem, and minimal // sufficient responsiveness of the memory pressure subsystem, and minimal
// overhead. // overhead.
const int MemoryPressureMonitor::kModeratePressureCooldownMs = 10000; const int SystemMemoryPressureEvaluator::kModeratePressureCooldownMs = 10000;
// TODO(chrisha): Explore the following constants further with an experiment. // TODO(chrisha): Explore the following constants further with an experiment.
// A system is considered 'high memory' if it has more than 1.5GB of system // A system is considered 'high memory' if it has more than 1.5GB of system
// memory available for use by the memory manager (not reserved for hardware // memory available for use by the memory manager (not reserved for hardware
// and drivers). This is a fuzzy version of the ~2GB discussed below. // and drivers). This is a fuzzy version of the ~2GB discussed below.
const int MemoryPressureMonitor::kLargeMemoryThresholdMb = 1536; const int SystemMemoryPressureEvaluator::kLargeMemoryThresholdMb = 1536;
// These are the default thresholds used for systems with < ~2GB of physical // These are the default thresholds used for systems with < ~2GB of physical
// memory. Such systems have been observed to always maintain ~100MB of // memory. Such systems have been observed to always maintain ~100MB of
// available memory, paging until that is the case. To try to avoid paging a // available memory, paging until that is the case. To try to avoid paging a
// threshold slightly above this is chosen. The moderate threshold is slightly // threshold slightly above this is chosen. The moderate threshold is slightly
// less grounded in reality and chosen as 2.5x critical. // less grounded in reality and chosen as 2.5x critical.
const int MemoryPressureMonitor::kSmallMemoryDefaultModerateThresholdMb = 500; const int
const int MemoryPressureMonitor::kSmallMemoryDefaultCriticalThresholdMb = 200; SystemMemoryPressureEvaluator::kSmallMemoryDefaultModerateThresholdMb = 500;
const int
SystemMemoryPressureEvaluator::kSmallMemoryDefaultCriticalThresholdMb = 200;
// These are the default thresholds used for systems with >= ~2GB of physical // These are the default thresholds used for systems with >= ~2GB of physical
// memory. Such systems have been observed to always maintain ~300MB of // memory. Such systems have been observed to always maintain ~300MB of
// available memory, paging until that is the case. // available memory, paging until that is the case.
const int MemoryPressureMonitor::kLargeMemoryDefaultModerateThresholdMb = 1000; const int
const int MemoryPressureMonitor::kLargeMemoryDefaultCriticalThresholdMb = 400; SystemMemoryPressureEvaluator::kLargeMemoryDefaultModerateThresholdMb =
1000;
MemoryPressureMonitor::MemoryPressureMonitor() const int
SystemMemoryPressureEvaluator::kLargeMemoryDefaultCriticalThresholdMb = 400;
SystemMemoryPressureEvaluator::SystemMemoryPressureEvaluator(
std::unique_ptr<MemoryPressureVoter> voter)
: moderate_threshold_mb_(0), : moderate_threshold_mb_(0),
critical_threshold_mb_(0), critical_threshold_mb_(0),
current_memory_pressure_level_(
MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
moderate_pressure_repeat_count_(0), moderate_pressure_repeat_count_(0),
dispatch_callback_( voter_(std::move(voter)),
base::BindRepeating(&MemoryPressureListener::NotifyMemoryPressure)), current_vote_(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
InferThresholds(); InferThresholds();
StartObserving(); StartObserving();
} }
MemoryPressureMonitor::MemoryPressureMonitor(int moderate_threshold_mb, SystemMemoryPressureEvaluator::SystemMemoryPressureEvaluator(
int critical_threshold_mb) int moderate_threshold_mb,
int critical_threshold_mb,
std::unique_ptr<MemoryPressureVoter> voter)
: moderate_threshold_mb_(moderate_threshold_mb), : moderate_threshold_mb_(moderate_threshold_mb),
critical_threshold_mb_(critical_threshold_mb), critical_threshold_mb_(critical_threshold_mb),
current_memory_pressure_level_(
MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
moderate_pressure_repeat_count_(0), moderate_pressure_repeat_count_(0),
dispatch_callback_( voter_(std::move(voter)),
base::BindRepeating(&MemoryPressureListener::NotifyMemoryPressure)), current_vote_(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_); DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_);
DCHECK_LE(0, critical_threshold_mb_); DCHECK_LE(0, critical_threshold_mb_);
StartObserving(); StartObserving();
} }
MemoryPressureMonitor::~MemoryPressureMonitor() { SystemMemoryPressureEvaluator::~SystemMemoryPressureEvaluator() {
StopObserving(); StopObserving();
} }
void MemoryPressureMonitor::CheckMemoryPressureSoon() { void SystemMemoryPressureEvaluator::CheckMemoryPressureSoon() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ThreadTaskRunnerHandle::Get()->PostTask( base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&MemoryPressureMonitor::CheckMemoryPressure, FROM_HERE, BindOnce(&SystemMemoryPressureEvaluator::CheckMemoryPressure,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
MemoryPressureListener::MemoryPressureLevel void SystemMemoryPressureEvaluator::InferThresholds() {
MemoryPressureMonitor::GetCurrentPressureLevel() const {
return current_memory_pressure_level_;
}
void MemoryPressureMonitor::InferThresholds() {
// Default to a 'high' memory situation, which uses more conservative // Default to a 'high' memory situation, which uses more conservative
// thresholds. // thresholds.
bool high_memory = true; bool high_memory = true;
...@@ -112,40 +112,39 @@ void MemoryPressureMonitor::InferThresholds() { ...@@ -112,40 +112,39 @@ void MemoryPressureMonitor::InferThresholds() {
} }
} }
void MemoryPressureMonitor::StartObserving() { void SystemMemoryPressureEvaluator::StartObserving() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
timer_.Start( timer_.Start(
FROM_HERE, base::MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod, FROM_HERE, base::MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod,
BindRepeating( BindRepeating(&SystemMemoryPressureEvaluator::CheckMemoryPressure,
&MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics, weak_ptr_factory_.GetWeakPtr()));
weak_ptr_factory_.GetWeakPtr()));
} }
void MemoryPressureMonitor::StopObserving() { void SystemMemoryPressureEvaluator::StopObserving() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// If StartObserving failed, StopObserving will still get called. // If StartObserving failed, StopObserving will still get called.
timer_.Stop(); timer_.Stop();
weak_ptr_factory_.InvalidateWeakPtrs(); weak_ptr_factory_.InvalidateWeakPtrs();
} }
void MemoryPressureMonitor::CheckMemoryPressure() { void SystemMemoryPressureEvaluator::CheckMemoryPressure() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Get the previous pressure level and update the current one. // Get the previous pressure level and update the current one.
MemoryPressureLevel old_pressure = current_memory_pressure_level_; MemoryPressureLevel old_vote = current_vote_;
current_memory_pressure_level_ = CalculateCurrentPressureLevel(); current_vote_ = CalculateCurrentPressureLevel();
// |notify| will be set to true if MemoryPressureListeners need to be // |notify| will be set to true if MemoryPressureListeners need to be
// notified of a memory pressure level state change. // notified of a memory pressure level state change.
bool notify = false; bool notify = false;
switch (current_memory_pressure_level_) { switch (current_vote_) {
case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
break; break;
case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
if (old_pressure != current_memory_pressure_level_) { if (old_vote != current_vote_) {
// This is a new transition to moderate pressure so notify. // This is a new transition to moderate pressure so notify.
moderate_pressure_repeat_count_ = 0; moderate_pressure_repeat_count_ = 0;
notify = true; notify = true;
...@@ -157,42 +156,27 @@ void MemoryPressureMonitor::CheckMemoryPressure() { ...@@ -157,42 +156,27 @@ void MemoryPressureMonitor::CheckMemoryPressure() {
base::MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod base::MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod
.InMilliseconds(); .InMilliseconds();
if (++moderate_pressure_repeat_count_ == if (++moderate_pressure_repeat_count_ ==
kModeratePressureCooldownCycles) { kModeratePressureCooldownCycles) {
moderate_pressure_repeat_count_ = 0; moderate_pressure_repeat_count_ = 0;
notify = true; notify = true;
} }
} }
break; break;
case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
// Always notify of critical pressure levels. // Always notify of critical pressure levels.
notify = true; notify = true;
break; break;
} }
if (!notify) voter_->SetVote(current_vote_, notify);
return;
// Emit a notification of the current memory pressure level. This can only
// happen for moderate and critical pressure levels.
DCHECK_NE(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
current_memory_pressure_level_);
dispatch_callback_.Run(current_memory_pressure_level_);
}
void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() {
DCHECK(thread_checker_.CalledOnValidThread());
CheckMemoryPressure();
RecordMemoryPressure(current_memory_pressure_level_, 1);
} }
MemoryPressureListener::MemoryPressureLevel base::MemoryPressureListener::MemoryPressureLevel
MemoryPressureMonitor::CalculateCurrentPressureLevel() { SystemMemoryPressureEvaluator::CalculateCurrentPressureLevel() {
MEMORYSTATUSEX mem_status = {}; MEMORYSTATUSEX mem_status = {};
if (!GetSystemMemoryStatus(&mem_status)) if (!GetSystemMemoryStatus(&mem_status))
return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; return base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
// How much system memory is actively available for use right now, in MBs. // How much system memory is actively available for use right now, in MBs.
int phys_free = static_cast<int>(mem_status.ullAvailPhys / kMBBytes); int phys_free = static_cast<int>(mem_status.ullAvailPhys / kMBBytes);
...@@ -207,17 +191,17 @@ MemoryPressureMonitor::CalculateCurrentPressureLevel() { ...@@ -207,17 +191,17 @@ MemoryPressureMonitor::CalculateCurrentPressureLevel() {
// Determine if the physical memory is under critical memory pressure. // Determine if the physical memory is under critical memory pressure.
if (phys_free <= critical_threshold_mb_) if (phys_free <= critical_threshold_mb_)
return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; return base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
// Determine if the physical memory is under moderate memory pressure. // Determine if the physical memory is under moderate memory pressure.
if (phys_free <= moderate_threshold_mb_) if (phys_free <= moderate_threshold_mb_)
return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; return base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE;
// No memory pressure was detected. // No memory pressure was detected.
return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; return base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
} }
bool MemoryPressureMonitor::GetSystemMemoryStatus( bool SystemMemoryPressureEvaluator::GetSystemMemoryStatus(
MEMORYSTATUSEX* mem_status) { MEMORYSTATUSEX* mem_status) {
DCHECK(mem_status != nullptr); DCHECK(mem_status != nullptr);
mem_status->dwLength = sizeof(*mem_status); mem_status->dwLength = sizeof(*mem_status);
...@@ -226,10 +210,5 @@ bool MemoryPressureMonitor::GetSystemMemoryStatus( ...@@ -226,10 +210,5 @@ bool MemoryPressureMonitor::GetSystemMemoryStatus(
return true; return true;
} }
void MemoryPressureMonitor::SetDispatchCallback(
const DispatchCallback& callback) {
dispatch_callback_ = callback;
}
} // namespace win } // namespace win
} // namespace base } // namespace util
// Copyright 2015 The Chromium Authors. All rights reserved. // Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_ #ifndef BASE_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_WIN_H_
#define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_ #define BASE_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_WIN_H_
#include "base/base_export.h" #include "base/base_export.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/memory_pressure_listener.h" #include "base/memory/memory_pressure_listener.h"
#include "base/memory/memory_pressure_monitor.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h" #include "base/sequence_checker.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "base/util/memory_pressure/memory_pressure_voter.h"
#include "base/util/memory_pressure/system_memory_pressure_evaluator.h"
// To not pull in windows.h. // To not pull in windows.h.
typedef struct _MEMORYSTATUSEX MEMORYSTATUSEX; typedef struct _MEMORYSTATUSEX MEMORYSTATUSEX;
namespace base { namespace util {
namespace win { namespace win {
// Windows memory pressure monitor. Because there is no OS provided signal this // Windows memory pressure voter. Because there is no OS provided signal this
// polls at a low frequency (once per second), and applies internal hysteresis. // polls at a low frequency, and applies internal hysteresis.
class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor { class SystemMemoryPressureEvaluator
: public util::SystemMemoryPressureEvaluator {
public: public:
using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
// Constants governing the polling and hysteresis behaviour of the observer. // Constants governing the polling and hysteresis behaviour of the observer.
// The time which should pass between 2 successive moderate memory pressure // The time which should pass between 2 successive moderate memory pressure
// signals, in milliseconds. // signals, in milliseconds.
...@@ -40,24 +44,24 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor { ...@@ -40,24 +44,24 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
static const int kLargeMemoryDefaultModerateThresholdMb; static const int kLargeMemoryDefaultModerateThresholdMb;
static const int kLargeMemoryDefaultCriticalThresholdMb; static const int kLargeMemoryDefaultCriticalThresholdMb;
// Default constructor. Will choose thresholds automatically basd on the // Default constructor. Will choose thresholds automatically based on the
// actual amount of system memory. // actual amount of system memory.
MemoryPressureMonitor(); explicit SystemMemoryPressureEvaluator(
std::unique_ptr<MemoryPressureVoter> voter);
// Constructor with explicit memory thresholds. These represent the amount of // Constructor with explicit memory thresholds. These represent the amount of
// free memory below which the applicable memory pressure state engages. // free memory below which the applicable memory pressure state engages.
MemoryPressureMonitor(int moderate_threshold_mb, int critical_threshold_mb); // For testing purposes.
SystemMemoryPressureEvaluator(int moderate_threshold_mb,
int critical_threshold_mb,
std::unique_ptr<MemoryPressureVoter> voter);
~MemoryPressureMonitor() override; ~SystemMemoryPressureEvaluator() override;
// Schedules a memory pressure check to run soon. This must be called on the // Schedules a memory pressure check to run soon. This must be called on the
// same thread where the monitor was instantiated. // same sequence where the monitor was instantiated.
void CheckMemoryPressureSoon(); void CheckMemoryPressureSoon();
// Get the current memory pressure level. This can be called from any thread.
MemoryPressureLevel GetCurrentPressureLevel() const override;
void SetDispatchCallback(const DispatchCallback& callback) override;
// Returns the moderate pressure level free memory threshold, in MB. // Returns the moderate pressure level free memory threshold, in MB.
int moderate_threshold_mb() const { return moderate_threshold_mb_; } int moderate_threshold_mb() const { return moderate_threshold_mb_; }
...@@ -83,23 +87,18 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor { ...@@ -83,23 +87,18 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
// Checks memory pressure, storing the current level, applying any hysteresis // Checks memory pressure, storing the current level, applying any hysteresis
// and emitting memory pressure level change signals as necessary. This // and emitting memory pressure level change signals as necessary. This
// function is called periodically while the monitor is observing memory // function is called periodically while the monitor is observing memory
// pressure. This is split out from CheckMemoryPressureAndRecordStatistics so // pressure. Must be called from the same thread on which the monitor was
// that it may be called by CheckMemoryPressureSoon and not invoke UMA
// logging. Must be called from the same thread on which the monitor was
// instantiated. // instantiated.
void CheckMemoryPressure(); 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. Must be called from the same thread on which the
// monitor was instantiated.
void CheckMemoryPressureAndRecordStatistics();
// Calculates the current instantaneous memory pressure level. This does not // Calculates the current instantaneous memory pressure level. This does not
// use any hysteresis and simply returns the result at the current moment. Can // use any hysteresis and simply returns the result at the current moment. Can
// be called on any thread. // be called on any thread.
MemoryPressureLevel CalculateCurrentPressureLevel(); MemoryPressureLevel CalculateCurrentPressureLevel();
// Gets the most recently cast vote.
MemoryPressureLevel current_vote_for_testing() const { return current_vote_; }
// Gets system memory status. This is virtual as a unittesting hook. Returns // Gets system memory status. This is virtual as a unittesting hook. Returns
// true if the system call succeeds, false otherwise. Can be called on any // true if the system call succeeds, false otherwise. Can be called on any
// thread. // thread.
...@@ -114,28 +113,29 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor { ...@@ -114,28 +113,29 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
// A periodic timer to check for memory pressure changes. // A periodic timer to check for memory pressure changes.
base::RepeatingTimer timer_; base::RepeatingTimer timer_;
// The current memory pressure.
MemoryPressureLevel current_memory_pressure_level_;
// To slow down the amount of moderate pressure event calls, this gets used to // 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 // count the number of events since the last event occurred. This is used by
// |CheckMemoryPressure| to apply hysteresis on the raw results of // |CheckMemoryPressure| to apply hysteresis on the raw results of
// |CalculateCurrentPressureLevel|. // |CalculateCurrentPressureLevel|.
int moderate_pressure_repeat_count_; int moderate_pressure_repeat_count_;
// Ensures that this object is used from a single thread. // In charge of forwarding votes from here to the
base::ThreadChecker thread_checker_; // MemoryPressureVoteAggregator.
std::unique_ptr<MemoryPressureVoter> voter_;
MemoryPressureLevel current_vote_;
DispatchCallback dispatch_callback_; // Ensures that this object is used from a single sequence.
SEQUENCE_CHECKER(sequence_checker_);
// Weak pointer factory to ourself used for scheduling calls to // Weak pointer factory to ourself used for scheduling calls to
// CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|. // CheckMemoryPressure/CheckMemoryPressureAndRecordStatistics via |timer_|.
base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_; base::WeakPtrFactory<SystemMemoryPressureEvaluator> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor); DISALLOW_COPY_AND_ASSIGN(SystemMemoryPressureEvaluator);
}; };
} // namespace win } // namespace win
} // namespace base } // namespace util
#endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_ #endif // BASE_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_WIN_H_
// Copyright 2015 The Chromium Authors. All rights reserved. // Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/memory/memory_pressure_monitor_win.h" #include "base/util/memory_pressure/system_memory_pressure_evaluator_win.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/scoped_task_environment.h" #include "base/test/scoped_task_environment.h"
#include "base/util/memory_pressure/multi_source_memory_pressure_monitor.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -17,29 +17,32 @@ ...@@ -17,29 +17,32 @@
#include <windows.h> #include <windows.h>
#endif #endif
namespace base { namespace util {
namespace win { namespace win {
namespace { namespace {
struct PressureSettings { struct PressureSettings {
int phys_left_mb; int phys_left_mb;
MemoryPressureListener::MemoryPressureLevel level; base::MemoryPressureListener::MemoryPressureLevel level;
}; };
} // namespace } // namespace
// This is outside of the anonymous namespace so that it can be seen as a friend // This is outside of the anonymous namespace so that it can be seen as a friend
// to the monitor class. // to the evaluator class.
class TestMemoryPressureMonitor : public MemoryPressureMonitor { class TestSystemMemoryPressureEvaluator : public SystemMemoryPressureEvaluator {
public: public:
using MemoryPressureMonitor::CalculateCurrentPressureLevel; using SystemMemoryPressureEvaluator::CalculateCurrentPressureLevel;
using MemoryPressureMonitor::CheckMemoryPressure; using SystemMemoryPressureEvaluator::CheckMemoryPressure;
using SystemMemoryPressureEvaluator::current_vote_for_testing;
static const DWORDLONG kMBBytes = 1024 * 1024; static const DWORDLONG kMBBytes = 1024 * 1024;
explicit TestMemoryPressureMonitor(bool large_memory) explicit TestSystemMemoryPressureEvaluator(
: mem_status_() { bool large_memory,
std::unique_ptr<MemoryPressureVoter> voter)
: SystemMemoryPressureEvaluator(std::move(voter)), mem_status_() {
// Generate a plausible amount of memory. // Generate a plausible amount of memory.
mem_status_.ullTotalPhys = mem_status_.ullTotalPhys =
static_cast<DWORDLONG>(GenerateTotalMemoryMb(large_memory)) * kMBBytes; static_cast<DWORDLONG>(GenerateTotalMemoryMb(large_memory)) * kMBBytes;
...@@ -50,29 +53,29 @@ class TestMemoryPressureMonitor : public MemoryPressureMonitor { ...@@ -50,29 +53,29 @@ class TestMemoryPressureMonitor : public MemoryPressureMonitor {
StopObserving(); StopObserving();
} }
TestMemoryPressureMonitor(int system_memory_mb, TestSystemMemoryPressureEvaluator(int system_memory_mb,
int moderate_threshold_mb, int moderate_threshold_mb,
int critical_threshold_mb) int critical_threshold_mb)
: MemoryPressureMonitor(moderate_threshold_mb, critical_threshold_mb), : SystemMemoryPressureEvaluator(moderate_threshold_mb,
critical_threshold_mb,
nullptr),
mem_status_() { mem_status_() {
// Set the amount of system memory. // Set the amount of system memory.
mem_status_.ullTotalPhys = static_cast<DWORDLONG>( mem_status_.ullTotalPhys =
system_memory_mb * kMBBytes); static_cast<DWORDLONG>(system_memory_mb * kMBBytes);
// Stop the timer. // Stop the timer.
StopObserving(); StopObserving();
} }
virtual ~TestMemoryPressureMonitor() {}
MOCK_METHOD1(OnMemoryPressure, MOCK_METHOD1(OnMemoryPressure,
void(MemoryPressureListener::MemoryPressureLevel level)); void(base::MemoryPressureListener::MemoryPressureLevel level));
// Generates an amount of total memory that is consistent with the requested // Generates an amount of total memory that is consistent with the requested
// memory model. // memory model.
int GenerateTotalMemoryMb(bool large_memory) { int GenerateTotalMemoryMb(bool large_memory) {
int total_mb = 64; int total_mb = 64;
while (total_mb < MemoryPressureMonitor::kLargeMemoryThresholdMb) while (total_mb < SystemMemoryPressureEvaluator::kLargeMemoryThresholdMb)
total_mb *= 2; total_mb *= 2;
if (large_memory) if (large_memory)
return total_mb * 2; return total_mb * 2;
...@@ -84,8 +87,7 @@ class TestMemoryPressureMonitor : public MemoryPressureMonitor { ...@@ -84,8 +87,7 @@ class TestMemoryPressureMonitor : public MemoryPressureMonitor {
// ullTotalPhys is set in the constructor and not modified. // ullTotalPhys is set in the constructor and not modified.
// Set the amount of available memory. // Set the amount of available memory.
mem_status_.ullAvailPhys = mem_status_.ullAvailPhys = static_cast<DWORDLONG>(phys_left_mb) * kMBBytes;
static_cast<DWORDLONG>(phys_left_mb) * kMBBytes;
DCHECK_LT(mem_status_.ullAvailPhys, mem_status_.ullTotalPhys); DCHECK_LT(mem_status_.ullAvailPhys, mem_status_.ullTotalPhys);
// These fields are unused. // These fields are unused.
...@@ -96,17 +98,11 @@ class TestMemoryPressureMonitor : public MemoryPressureMonitor { ...@@ -96,17 +98,11 @@ class TestMemoryPressureMonitor : public MemoryPressureMonitor {
mem_status_.ullAvailVirtual = 0; mem_status_.ullAvailVirtual = 0;
} }
void SetNone() { void SetNone() { SetMemoryFree(moderate_threshold_mb() + 1); }
SetMemoryFree(moderate_threshold_mb() + 1);
}
void SetModerate() { void SetModerate() { SetMemoryFree(moderate_threshold_mb() - 1); }
SetMemoryFree(moderate_threshold_mb() - 1);
}
void SetCritical() { void SetCritical() { SetMemoryFree(critical_threshold_mb() - 1); }
SetMemoryFree(critical_threshold_mb() - 1);
}
private: private:
bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status) override { bool GetSystemMemoryStatus(MEMORYSTATUSEX* mem_status) override {
...@@ -117,195 +113,209 @@ class TestMemoryPressureMonitor : public MemoryPressureMonitor { ...@@ -117,195 +113,209 @@ class TestMemoryPressureMonitor : public MemoryPressureMonitor {
MEMORYSTATUSEX mem_status_; MEMORYSTATUSEX mem_status_;
DISALLOW_COPY_AND_ASSIGN(TestMemoryPressureMonitor); DISALLOW_COPY_AND_ASSIGN(TestSystemMemoryPressureEvaluator);
}; };
class WinMemoryPressureMonitorTest : public testing::Test { class WinSystemMemoryPressureEvaluatorTest : public testing::Test {
protected: protected:
void CalculateCurrentMemoryPressureLevelTest( void CalculateCurrentMemoryPressureLevelTest(
TestMemoryPressureMonitor* monitor) { TestSystemMemoryPressureEvaluator* evaluator) {
int mod = evaluator->moderate_threshold_mb();
int mod = monitor->moderate_threshold_mb(); evaluator->SetMemoryFree(mod + 1);
monitor->SetMemoryFree(mod + 1); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, evaluator->CalculateCurrentPressureLevel());
monitor->CalculateCurrentPressureLevel());
evaluator->SetMemoryFree(mod);
monitor->SetMemoryFree(mod); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, evaluator->CalculateCurrentPressureLevel());
monitor->CalculateCurrentPressureLevel());
evaluator->SetMemoryFree(mod - 1);
monitor->SetMemoryFree(mod - 1); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, evaluator->CalculateCurrentPressureLevel());
monitor->CalculateCurrentPressureLevel());
int crit = evaluator->critical_threshold_mb();
int crit = monitor->critical_threshold_mb(); evaluator->SetMemoryFree(crit + 1);
monitor->SetMemoryFree(crit + 1); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, evaluator->CalculateCurrentPressureLevel());
monitor->CalculateCurrentPressureLevel());
evaluator->SetMemoryFree(crit);
monitor->SetMemoryFree(crit); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, evaluator->CalculateCurrentPressureLevel());
monitor->CalculateCurrentPressureLevel());
evaluator->SetMemoryFree(crit - 1);
monitor->SetMemoryFree(crit - 1); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, evaluator->CalculateCurrentPressureLevel());
monitor->CalculateCurrentPressureLevel());
} }
base::test::TaskEnvironment task_environment_{ base::MessageLoopForUI message_loop_;
base::test::TaskEnvironment::MainThreadType::UI};
}; };
// Tests the fundamental direct calculation of memory pressure with automatic // Tests the fundamental direct calculation of memory pressure with automatic
// small-memory thresholds. // small-memory thresholds.
TEST_F(WinMemoryPressureMonitorTest, CalculateCurrentMemoryPressureLevelSmall) { TEST_F(WinSystemMemoryPressureEvaluatorTest,
CalculateCurrentMemoryPressureLevelSmall) {
static const int kModerateMb = static const int kModerateMb =
MemoryPressureMonitor::kSmallMemoryDefaultModerateThresholdMb; SystemMemoryPressureEvaluator::kSmallMemoryDefaultModerateThresholdMb;
static const int kCriticalMb = static const int kCriticalMb =
MemoryPressureMonitor::kSmallMemoryDefaultCriticalThresholdMb; SystemMemoryPressureEvaluator::kSmallMemoryDefaultCriticalThresholdMb;
TestMemoryPressureMonitor monitor(false); // Small-memory model. // Small-memory model.
TestSystemMemoryPressureEvaluator evaluator(false, nullptr);
EXPECT_EQ(kModerateMb, monitor.moderate_threshold_mb()); EXPECT_EQ(kModerateMb, evaluator.moderate_threshold_mb());
EXPECT_EQ(kCriticalMb, monitor.critical_threshold_mb()); EXPECT_EQ(kCriticalMb, evaluator.critical_threshold_mb());
ASSERT_NO_FATAL_FAILURE(CalculateCurrentMemoryPressureLevelTest(&monitor)); ASSERT_NO_FATAL_FAILURE(CalculateCurrentMemoryPressureLevelTest(&evaluator));
} }
// Tests the fundamental direct calculation of memory pressure with automatic // Tests the fundamental direct calculation of memory pressure with automatic
// large-memory thresholds. // large-memory thresholds.
TEST_F(WinMemoryPressureMonitorTest, CalculateCurrentMemoryPressureLevelLarge) { TEST_F(WinSystemMemoryPressureEvaluatorTest,
CalculateCurrentMemoryPressureLevelLarge) {
static const int kModerateMb = static const int kModerateMb =
MemoryPressureMonitor::kLargeMemoryDefaultModerateThresholdMb; SystemMemoryPressureEvaluator::kLargeMemoryDefaultModerateThresholdMb;
static const int kCriticalMb = static const int kCriticalMb =
MemoryPressureMonitor::kLargeMemoryDefaultCriticalThresholdMb; SystemMemoryPressureEvaluator::kLargeMemoryDefaultCriticalThresholdMb;
TestMemoryPressureMonitor monitor(true); // Large-memory model. // Large-memory model.
TestSystemMemoryPressureEvaluator evaluator(true, nullptr);
EXPECT_EQ(kModerateMb, monitor.moderate_threshold_mb()); EXPECT_EQ(kModerateMb, evaluator.moderate_threshold_mb());
EXPECT_EQ(kCriticalMb, monitor.critical_threshold_mb()); EXPECT_EQ(kCriticalMb, evaluator.critical_threshold_mb());
ASSERT_NO_FATAL_FAILURE(CalculateCurrentMemoryPressureLevelTest(&monitor)); ASSERT_NO_FATAL_FAILURE(CalculateCurrentMemoryPressureLevelTest(&evaluator));
} }
// Tests the fundamental direct calculation of memory pressure with manually // Tests the fundamental direct calculation of memory pressure with manually
// specified threshold levels. // specified threshold levels.
TEST_F(WinMemoryPressureMonitorTest, TEST_F(WinSystemMemoryPressureEvaluatorTest,
CalculateCurrentMemoryPressureLevelCustom) { CalculateCurrentMemoryPressureLevelCustom) {
static const int kSystemMb = 512; static const int kSystemMb = 512;
static const int kModerateMb = 256; static const int kModerateMb = 256;
static const int kCriticalMb = 128; static const int kCriticalMb = 128;
TestMemoryPressureMonitor monitor(kSystemMb, kModerateMb, kCriticalMb); TestSystemMemoryPressureEvaluator evaluator(kSystemMb, kModerateMb,
kCriticalMb);
EXPECT_EQ(kModerateMb, monitor.moderate_threshold_mb()); EXPECT_EQ(kModerateMb, evaluator.moderate_threshold_mb());
EXPECT_EQ(kCriticalMb, monitor.critical_threshold_mb()); EXPECT_EQ(kCriticalMb, evaluator.critical_threshold_mb());
ASSERT_NO_FATAL_FAILURE(CalculateCurrentMemoryPressureLevelTest(&monitor)); ASSERT_NO_FATAL_FAILURE(CalculateCurrentMemoryPressureLevelTest(&evaluator));
} }
// This test tests the various transition states from memory pressure, looking // This test tests the various transition states from memory pressure, looking
// for the correct behavior on event reposting as well as state updates. // for the correct behavior on event reposting as well as state updates.
TEST_F(WinMemoryPressureMonitorTest, CheckMemoryPressure) { TEST_F(WinSystemMemoryPressureEvaluatorTest, CheckMemoryPressure) {
MultiSourceMemoryPressureMonitor monitor;
monitor.ResetSystemEvaluatorForTesting();
// Large-memory. // Large-memory.
testing::StrictMock<TestMemoryPressureMonitor> monitor(true); testing::StrictMock<TestSystemMemoryPressureEvaluator> evaluator(
MemoryPressureListener listener( true, monitor.CreateVoter());
base::BindRepeating(&TestMemoryPressureMonitor::OnMemoryPressure,
base::Unretained(&monitor))); base::MemoryPressureListener listener(
base::BindRepeating(&TestSystemMemoryPressureEvaluator::OnMemoryPressure,
base::Unretained(&evaluator)));
// Checking the memory pressure at 0% load should not produce any // Checking the memory pressure at 0% load should not produce any
// events. // events.
monitor.SetNone(); evaluator.SetNone();
monitor.CheckMemoryPressure(); evaluator.CheckMemoryPressure();
RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
monitor.GetCurrentPressureLevel()); evaluator.current_vote_for_testing());
// Setting the memory level to 80% should produce a moderate pressure level. // Setting the memory level to 80% should produce a moderate pressure level.
EXPECT_CALL(monitor, EXPECT_CALL(
OnMemoryPressure(MemoryPressureListener:: evaluator,
MEMORY_PRESSURE_LEVEL_MODERATE)); OnMemoryPressure(
monitor.SetModerate(); base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
monitor.CheckMemoryPressure(); evaluator.SetModerate();
RunLoop().RunUntilIdle(); evaluator.CheckMemoryPressure();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, base::RunLoop().RunUntilIdle();
monitor.GetCurrentPressureLevel()); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
testing::Mock::VerifyAndClearExpectations(&monitor); evaluator.current_vote_for_testing());
testing::Mock::VerifyAndClearExpectations(&evaluator);
// Check that the event gets reposted after a while. // Check that the event gets reposted after a while.
const int kModeratePressureCooldownCycles = const int kModeratePressureCooldownCycles =
monitor.kModeratePressureCooldownMs / evaluator.kModeratePressureCooldownMs /
base::MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod base::MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod
.InMilliseconds(); .InMilliseconds();
for (int i = 0; i < kModeratePressureCooldownCycles; ++i) { for (int i = 0; i < kModeratePressureCooldownCycles; ++i) {
if (i + 1 == kModeratePressureCooldownCycles) { if (i + 1 == kModeratePressureCooldownCycles) {
EXPECT_CALL(monitor, EXPECT_CALL(
OnMemoryPressure(MemoryPressureListener:: evaluator,
MEMORY_PRESSURE_LEVEL_MODERATE)); OnMemoryPressure(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
} }
monitor.CheckMemoryPressure(); evaluator.CheckMemoryPressure();
RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
monitor.GetCurrentPressureLevel()); evaluator.current_vote_for_testing());
testing::Mock::VerifyAndClearExpectations(&monitor); testing::Mock::VerifyAndClearExpectations(&evaluator);
} }
// Setting the memory usage to 99% should produce critical levels. // Setting the memory usage to 99% should produce critical levels.
EXPECT_CALL(monitor, EXPECT_CALL(
OnMemoryPressure(MemoryPressureListener:: evaluator,
MEMORY_PRESSURE_LEVEL_CRITICAL)); OnMemoryPressure(
monitor.SetCritical(); base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL));
monitor.CheckMemoryPressure(); evaluator.SetCritical();
RunLoop().RunUntilIdle(); evaluator.CheckMemoryPressure();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, base::RunLoop().RunUntilIdle();
monitor.GetCurrentPressureLevel()); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
testing::Mock::VerifyAndClearExpectations(&monitor); evaluator.current_vote_for_testing());
testing::Mock::VerifyAndClearExpectations(&evaluator);
// Calling it again should immediately produce a second call. // Calling it again should immediately produce a second call.
EXPECT_CALL(monitor, EXPECT_CALL(
OnMemoryPressure(MemoryPressureListener:: evaluator,
MEMORY_PRESSURE_LEVEL_CRITICAL)); OnMemoryPressure(
monitor.CheckMemoryPressure(); base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL));
RunLoop().RunUntilIdle(); evaluator.CheckMemoryPressure();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, base::RunLoop().RunUntilIdle();
monitor.GetCurrentPressureLevel()); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
testing::Mock::VerifyAndClearExpectations(&monitor); evaluator.current_vote_for_testing());
testing::Mock::VerifyAndClearExpectations(&evaluator);
// When lowering the pressure again there should be a notification and the // When lowering the pressure again there should be a notification and the
// pressure should go back to moderate. // pressure should go back to moderate.
EXPECT_CALL(monitor, EXPECT_CALL(
OnMemoryPressure(MemoryPressureListener:: evaluator,
MEMORY_PRESSURE_LEVEL_MODERATE)); OnMemoryPressure(
monitor.SetModerate(); base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
monitor.CheckMemoryPressure(); evaluator.SetModerate();
RunLoop().RunUntilIdle(); evaluator.CheckMemoryPressure();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, base::RunLoop().RunUntilIdle();
monitor.GetCurrentPressureLevel()); EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
testing::Mock::VerifyAndClearExpectations(&monitor); evaluator.current_vote_for_testing());
testing::Mock::VerifyAndClearExpectations(&evaluator);
// Check that the event gets reposted after a while. // Check that the event gets reposted after a while.
for (int i = 0; i < kModeratePressureCooldownCycles; ++i) { for (int i = 0; i < kModeratePressureCooldownCycles; ++i) {
if (i + 1 == kModeratePressureCooldownCycles) { if (i + 1 == kModeratePressureCooldownCycles) {
EXPECT_CALL(monitor, EXPECT_CALL(
OnMemoryPressure(MemoryPressureListener:: evaluator,
MEMORY_PRESSURE_LEVEL_MODERATE)); OnMemoryPressure(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
} }
monitor.CheckMemoryPressure(); evaluator.CheckMemoryPressure();
RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
monitor.GetCurrentPressureLevel()); evaluator.current_vote_for_testing());
testing::Mock::VerifyAndClearExpectations(&monitor); testing::Mock::VerifyAndClearExpectations(&evaluator);
} }
// Going down to no pressure should not produce an notification. // Going down to no pressure should not produce an notification.
monitor.SetNone(); evaluator.SetNone();
monitor.CheckMemoryPressure(); evaluator.CheckMemoryPressure();
RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
monitor.GetCurrentPressureLevel()); evaluator.current_vote_for_testing());
testing::Mock::VerifyAndClearExpectations(&monitor); testing::Mock::VerifyAndClearExpectations(&evaluator);
} }
} // namespace win } // namespace win
} // namespace base } // namespace util
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
#include "base/timer/hi_res_timer_manager.h" #include "base/timer/hi_res_timer_manager.h"
#include "base/trace_event/memory_dump_manager.h" #include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "base/util/memory_pressure/multi_source_memory_pressure_monitor.h"
#include "build/branding_buildflags.h" #include "build/branding_buildflags.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "cc/base/histograms.h" #include "cc/base/histograms.h"
...@@ -184,7 +185,6 @@ ...@@ -184,7 +185,6 @@
#include <commctrl.h> #include <commctrl.h>
#include <shellapi.h> #include <shellapi.h>
#include "base/memory/memory_pressure_monitor_win.h"
#include "content/browser/renderer_host/dwrite_font_lookup_table_builder_win.h" #include "content/browser/renderer_host/dwrite_font_lookup_table_builder_win.h"
#include "net/base/winsock_init.h" #include "net/base/winsock_init.h"
#include "services/service_manager/sandbox/win/sandbox_win.h" #include "services/service_manager/sandbox/win/sandbox_win.h"
...@@ -384,7 +384,7 @@ std::unique_ptr<base::MemoryPressureMonitor> CreateMemoryPressureMonitor( ...@@ -384,7 +384,7 @@ std::unique_ptr<base::MemoryPressureMonitor> CreateMemoryPressureMonitor(
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
return std::make_unique<base::mac::MemoryPressureMonitor>(); return std::make_unique<base::mac::MemoryPressureMonitor>();
#elif defined(OS_WIN) #elif defined(OS_WIN)
return std::make_unique<base::win::MemoryPressureMonitor>(); return std::make_unique<util::MultiSourceMemoryPressureMonitor>();
#else #else
// No memory monitor on other platforms... // No memory monitor on other platforms...
return nullptr; return nullptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment