blob: 2496afa2ca6cd7ee5207d72f26cf980cf74a8e8c [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2019 The Chromium Authors
Ryan Powell30287d42019-08-15 15:48:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Albert J. Wong23183a12021-08-13 00:23:355#ifndef COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_
6#define COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_
Ryan Powell30287d42019-08-15 15:48:327
8#include <array>
Arthur Sonzognic571efb2024-01-26 20:26:189#include <optional>
Ryan Powell30287d42019-08-15 15:48:3210
Avi Drissman12be0312023-01-11 09:16:0911#include "base/functional/callback.h"
Ryan Powell30287d42019-08-15 15:48:3212#include "base/memory/memory_pressure_listener.h"
Keishi Hattori0e45c022021-11-27 09:25:5213#include "base/memory/raw_ptr.h"
Ryan Powell30287d42019-08-15 15:48:3214#include "base/sequence_checker.h"
15
Albert J. Wong23183a12021-08-13 00:23:3516namespace memory_pressure {
Ryan Powell30287d42019-08-15 15:48:3217
Wez861e2922020-03-01 21:14:0018// Interface used by code which actually monitors memory pressure, to inform
19// a MemoryPressureAggregator when the pressure they observe changes, or they
20// want to trigger a (re-)notification of clients of the current level.
21// Voters must be used only from the same sequence as the Aggregator to which
22// they are attached.
23class MemoryPressureVoter {
24 public:
Sorin Jianu6bc58072024-10-09 01:26:0225 virtual ~MemoryPressureVoter() = default;
Wez861e2922020-03-01 21:14:0026
27 // Called to set a vote / change a vote.
28 virtual void SetVote(base::MemoryPressureListener::MemoryPressureLevel level,
29 bool notify_listeners) = 0;
30};
31
Ryan Powell30287d42019-08-15 15:48:3232// Collects votes from MemoryPressureVoters and evaluates them to determine the
33// pressure level for the MultiSourceMemoryPressureMonitor, which will own
34// and outlive the aggregator. The pressure level is calculated as the most
35// critical of all votes collected. This class is not thread safe and should be
36// used from a single sequence.
37class MemoryPressureVoteAggregator {
38 public:
39 class Delegate;
40
41 using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
42
43 explicit MemoryPressureVoteAggregator(Delegate* delegate);
44 ~MemoryPressureVoteAggregator();
45
Albert J. Wong23183a12021-08-13 00:23:3546 MemoryPressureVoteAggregator(const MemoryPressureVoteAggregator&) = delete;
47 MemoryPressureVoteAggregator& operator=(const MemoryPressureVoteAggregator&) =
48 delete;
49
Wez861e2922020-03-01 21:14:0050 // Creates a MemoryPressureVoter attached to this Aggregator. The returned
51 // Voter must not out-live the Aggregator.
52 std::unique_ptr<MemoryPressureVoter> CreateVoter();
53
Arthur Sonzognic571efb2024-01-26 20:26:1854 void OnVoteForTesting(std::optional<MemoryPressureLevel> old_vote,
55 std::optional<MemoryPressureLevel> new_vote);
Ryan Powell30287d42019-08-15 15:48:3256
57 void NotifyListenersForTesting();
58
59 base::MemoryPressureListener::MemoryPressureLevel EvaluateVotesForTesting();
60 void SetVotesForTesting(size_t none_votes,
61 size_t moderate_votes,
62 size_t critical_votes);
63
64 private:
Wez861e2922020-03-01 21:14:0065 friend class MemoryPressureVoterImpl;
Ryan Powell30287d42019-08-15 15:48:3266
67 // Invoked by MemoryPressureVoter as it calculates its vote. Optional is
68 // used so a voter can pass null as |old_vote| if this is their first vote, or
69 // null as |new_vote| if they are removing their vote (e.g. when the voter is
70 // being destroyed). |old_vote| and |new_vote| should never both be null.
Arthur Sonzognic571efb2024-01-26 20:26:1871 void OnVote(std::optional<MemoryPressureLevel> old_vote,
72 std::optional<MemoryPressureLevel> new_vote);
Ryan Powell30287d42019-08-15 15:48:3273
74 // Triggers a notification of the MemoryPressureMonitor's current pressure
75 // level, allowing each of the various sources of input on MemoryPressureLevel
76 // to maintain their own signalling behavior.
Alison Galeb8be9522024-04-16 00:00:3177 // TODO(crbug.com/40639224): Remove this behavior and standardize across
78 // platforms.
Ryan Powell30287d42019-08-15 15:48:3279 void NotifyListeners();
80
81 // Returns the highest index of |votes_| with a non-zero value, as a
82 // MemoryPressureLevel.
83 MemoryPressureLevel EvaluateVotes() const;
84
Ryan Powell4b44d1082019-08-29 15:21:1285 MemoryPressureLevel current_pressure_level_ =
86 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
Ryan Powell30287d42019-08-15 15:48:3287
Keishi Hattori0e45c022021-11-27 09:25:5288 const raw_ptr<Delegate> delegate_;
Ryan Powell30287d42019-08-15 15:48:3289
90 // Array with one bucket for each potential MemoryPressureLevel. The overall
91 // MemoryPressureLevel is calculated as the highest index of a non-zero
92 // bucket.
93 // MEMORY_PRESSURE_LEVEL_CRITICAL + 1 is used in place of adding a kCount
94 // value to the MemoryPressureLevel enum as adding another value would require
95 // changing every instance of switch(MemoryPressureLevel) in Chromium, and the
96 // MemoryPressureLevel system will be changing soon regardless.
97 std::array<size_t,
98 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL + 1>
Ryan Powell4b44d1082019-08-29 15:21:1299 votes_ = {};
Ryan Powell30287d42019-08-15 15:48:32100
101 SEQUENCE_CHECKER(sequence_checker_);
Ryan Powell30287d42019-08-15 15:48:32102};
103
104// Interface used to notify MemoryPressureVoteAggregator's owner of changes to
105// vote aggregation.
106class MemoryPressureVoteAggregator::Delegate {
107 public:
108 Delegate() = default;
109 virtual ~Delegate() = default;
110
111 // Invoked when the aggregate vote has changed.
112 virtual void OnMemoryPressureLevelChanged(
113 base::MemoryPressureListener::MemoryPressureLevel level) = 0;
114
115 // Invoked when a voter has determined that a notification of the current
116 // pressure level is necessary.
117 virtual void OnNotifyListenersRequested() = 0;
118};
119
Albert J. Wong23183a12021-08-13 00:23:35120} // namespace memory_pressure
Ryan Powell30287d42019-08-15 15:48:32121
Albert J. Wong23183a12021-08-13 00:23:35122#endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_