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") {
"memory/memory_pressure_monitor_chromeos.h",
"memory/memory_pressure_monitor_mac.cc",
"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.h",
"memory/protected_memory.cc",
......@@ -2597,7 +2595,6 @@ test("base_unittests") {
"memory/memory_pressure_monitor_chromeos_unittest.cc",
"memory/memory_pressure_monitor_mac_unittest.cc",
"memory/memory_pressure_monitor_unittest.cc",
"memory/memory_pressure_monitor_win_unittest.cc",
"memory/platform_shared_memory_region_unittest.cc",
"memory/protected_memory_unittest.cc",
"memory/ptr_util_unittest.cc",
......
......@@ -8,6 +8,10 @@ source_set("memory_pressure") {
"memory_pressure_voter.h",
"multi_source_memory_pressure_monitor.cc",
"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 = [
......@@ -20,12 +24,14 @@ source_set("unittests") {
sources = [
"memory_pressure_voter_unittest.cc",
"multi_source_memory_pressure_monitor_unittest.cc",
"system_memory_pressure_evaluator_win_unittest.cc",
]
deps = [
":memory_pressure",
"//base",
"//base/test:test_support",
"//testing/gmock",
"//testing/gtest",
]
}
......@@ -7,6 +7,7 @@
#include "base/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/util/memory_pressure/system_memory_pressure_evaluator.h"
namespace util {
......@@ -16,6 +17,11 @@ MultiSourceMemoryPressureMonitor::MultiSourceMemoryPressureMonitor()
dispatch_callback_(base::BindRepeating(
&base::MemoryPressureListener::NotifyMemoryPressure)),
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();
}
......@@ -63,4 +69,8 @@ void MultiSourceMemoryPressureMonitor::OnNotifyListenersRequested() {
dispatch_callback_.Run(current_pressure_level_);
}
void MultiSourceMemoryPressureMonitor::ResetSystemEvaluatorForTesting() {
system_evaluator_.reset();
}
} // namespace util
......@@ -11,6 +11,8 @@
namespace util {
class SystemMemoryPressureEvaluator;
// This is a specialization of a MemoryPressureMonitor that relies on a set of
// MemoryPressureVoters to determine the memory pressure state. The
// MemoryPressureVoteAggregator is in charge of receiving votes from these
......@@ -40,6 +42,8 @@ class MultiSourceMemoryPressureMonitor
return &aggregator_;
}
void ResetSystemEvaluatorForTesting();
protected:
void StartMetricsTimer();
void StopMetricsTimer();
......@@ -58,6 +62,8 @@ class MultiSourceMemoryPressureMonitor
MemoryPressureVoteAggregator aggregator_;
std::unique_ptr<SystemMemoryPressureEvaluator> system_evaluator_;
// A periodic timer to record UMA metrics.
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
// found in the LICENSE file.
#ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_
#define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_
#ifndef BASE_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_WIN_H_
#define BASE_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_WIN_H_
#include "base/base_export.h"
#include "base/macros.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/memory_pressure_monitor.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/util/memory_pressure/memory_pressure_voter.h"
#include "base/util/memory_pressure/system_memory_pressure_evaluator.h"
// To not pull in windows.h.
typedef struct _MEMORYSTATUSEX MEMORYSTATUSEX;
namespace base {
namespace util {
namespace win {
// 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 {
// Windows memory pressure voter. Because there is no OS provided signal this
// polls at a low frequency, and applies internal hysteresis.
class SystemMemoryPressureEvaluator
: public util::SystemMemoryPressureEvaluator {
public:
using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
// Constants governing the polling and hysteresis behaviour of the observer.
// The time which should pass between 2 successive moderate memory pressure
// signals, in milliseconds.
......@@ -40,24 +44,24 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
static const int kLargeMemoryDefaultModerateThresholdMb;
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.
MemoryPressureMonitor();
explicit SystemMemoryPressureEvaluator(
std::unique_ptr<MemoryPressureVoter> voter);
// Constructor with explicit memory thresholds. These represent the amount of
// 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
// same thread where the monitor was instantiated.
// same sequence where the monitor was instantiated.
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.
int moderate_threshold_mb() const { return moderate_threshold_mb_; }
......@@ -83,23 +87,18 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
// 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. Must be called from the same thread on which the monitor was
// pressure. Must be called from the same thread on which the monitor was
// instantiated.
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
// use any hysteresis and simply returns the result at the current moment. Can
// be called on any thread.
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
// true if the system call succeeds, false otherwise. Can be called on any
// thread.
......@@ -114,28 +113,29 @@ class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
// A periodic timer to check for memory pressure changes.
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
// 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
// |CalculateCurrentPressureLevel|.
int moderate_pressure_repeat_count_;
// Ensures that this object is used from a single thread.
base::ThreadChecker thread_checker_;
// In charge of forwarding votes from here to the
// 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
// 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 base
} // namespace util
#endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_WIN_H_
#endif // BASE_UTIL_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_WIN_H_
......@@ -47,6 +47,7 @@
#include "base/timer/hi_res_timer_manager.h"
#include "base/trace_event/memory_dump_manager.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/build_config.h"
#include "cc/base/histograms.h"
......@@ -184,7 +185,6 @@
#include <commctrl.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 "net/base/winsock_init.h"
#include "services/service_manager/sandbox/win/sandbox_win.h"
......@@ -384,7 +384,7 @@ std::unique_ptr<base::MemoryPressureMonitor> CreateMemoryPressureMonitor(
#elif defined(OS_MACOSX)
return std::make_unique<base::mac::MemoryPressureMonitor>();
#elif defined(OS_WIN)
return std::make_unique<base::win::MemoryPressureMonitor>();
return std::make_unique<util::MultiSourceMemoryPressureMonitor>();
#else
// No memory monitor on other platforms...
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