[go: nahoru, domu]

Create classes for Memory Pressure Voting system.

This CL creates the MultiSourceMemoryPressureMonitor,
MemoryPressureVoteAggregator, and MemoryPressureVoter classes described
here: https://docs.google.com/document/d/1W3FPDyjIAKBcFGNYsHA3EKR1FHrJlbBaqT4_RUnxzq0/edit#.
The aggregator class collects votes from voter instances via the OnVote
method. A MemoryPressureVoteAggregator object will be owned by the
MultiSourceMemoryPressureMonitor class, which will be merged with the
MemoryPressureMonitor class after subsequent CL's which will migrate
the OS-specific MemoryPressureMonitors to own a Voter and use that
Voter to inform the Monitor of their votes.

Bug: 980965
Change-Id: Ib84aadca9a4cf74b3c1f8a42787d63a96f5dfd17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1719109
Commit-Queue: Ryan Powell <ryanpow@google.com>
Reviewed-by: François Doray <fdoray@chromium.org>
Reviewed-by: Sébastien Marchand <sebmarchand@chromium.org>
Cr-Commit-Position: refs/heads/master@{#687282}
diff --git a/base/util/memory_pressure/multi_source_memory_pressure_monitor.cc b/base/util/memory_pressure/multi_source_memory_pressure_monitor.cc
new file mode 100644
index 0000000..098ed72e
--- /dev/null
+++ b/base/util/memory_pressure/multi_source_memory_pressure_monitor.cc
@@ -0,0 +1,66 @@
+// 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/multi_source_memory_pressure_monitor.h"
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/metrics/histogram_macros.h"
+
+namespace util {
+
+MultiSourceMemoryPressureMonitor::MultiSourceMemoryPressureMonitor()
+    : current_pressure_level_(
+          base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
+      dispatch_callback_(base::BindRepeating(
+          &base::MemoryPressureListener::NotifyMemoryPressure)),
+      aggregator_(this) {
+  StartMetricsTimer();
+}
+
+MultiSourceMemoryPressureMonitor::~MultiSourceMemoryPressureMonitor() = default;
+
+void MultiSourceMemoryPressureMonitor::StartMetricsTimer() {
+  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  metric_timer_.Start(
+      FROM_HERE, MemoryPressureMonitor::kUMAMemoryPressureLevelPeriod,
+      BindRepeating(&MemoryPressureMonitor::RecordMemoryPressure,
+                    GetCurrentPressureLevel(), /* ticks = */ 1));
+}
+
+void MultiSourceMemoryPressureMonitor::StopMetricsTimer() {
+  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  metric_timer_.Stop();
+}
+
+base::MemoryPressureListener::MemoryPressureLevel
+MultiSourceMemoryPressureMonitor::GetCurrentPressureLevel() const {
+  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  return current_pressure_level_;
+}
+
+std::unique_ptr<MemoryPressureVoter>
+MultiSourceMemoryPressureMonitor::CreateVoter() {
+  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  return std::make_unique<MemoryPressureVoter>(&aggregator_);
+}
+
+void MultiSourceMemoryPressureMonitor::SetDispatchCallback(
+    const DispatchCallback& callback) {
+  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  dispatch_callback_ = callback;
+}
+
+void MultiSourceMemoryPressureMonitor::OnMemoryPressureLevelChanged(
+    base::MemoryPressureListener::MemoryPressureLevel level) {
+  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  current_pressure_level_ = level;
+}
+
+void MultiSourceMemoryPressureMonitor::OnNotifyListenersRequested() {
+  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  dispatch_callback_.Run(current_pressure_level_);
+}
+
+}  // namespace util