Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Albert J. Wong | 23183a1 | 2021-08-13 00:23:35 | [diff] [blame] | 5 | #ifndef COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_ |
| 6 | #define COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_ |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 7 | |
| 8 | #include <array> |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 9 | #include <optional> |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 10 | |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 11 | #include "base/functional/callback.h" |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 12 | #include "base/memory/memory_pressure_listener.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
| 15 | |
Albert J. Wong | 23183a1 | 2021-08-13 00:23:35 | [diff] [blame] | 16 | namespace memory_pressure { |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 17 | |
Wez | 861e292 | 2020-03-01 21:14:00 | [diff] [blame] | 18 | // 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. |
| 23 | class MemoryPressureVoter { |
| 24 | public: |
Sorin Jianu | 6bc5807 | 2024-10-09 01:26:02 | [diff] [blame] | 25 | virtual ~MemoryPressureVoter() = default; |
Wez | 861e292 | 2020-03-01 21:14:00 | [diff] [blame] | 26 | |
| 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 Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 32 | // 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. |
| 37 | class MemoryPressureVoteAggregator { |
| 38 | public: |
| 39 | class Delegate; |
| 40 | |
| 41 | using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel; |
| 42 | |
| 43 | explicit MemoryPressureVoteAggregator(Delegate* delegate); |
| 44 | ~MemoryPressureVoteAggregator(); |
| 45 | |
Albert J. Wong | 23183a1 | 2021-08-13 00:23:35 | [diff] [blame] | 46 | MemoryPressureVoteAggregator(const MemoryPressureVoteAggregator&) = delete; |
| 47 | MemoryPressureVoteAggregator& operator=(const MemoryPressureVoteAggregator&) = |
| 48 | delete; |
| 49 | |
Wez | 861e292 | 2020-03-01 21:14:00 | [diff] [blame] | 50 | // 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 Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 54 | void OnVoteForTesting(std::optional<MemoryPressureLevel> old_vote, |
| 55 | std::optional<MemoryPressureLevel> new_vote); |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 56 | |
| 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: |
Wez | 861e292 | 2020-03-01 21:14:00 | [diff] [blame] | 65 | friend class MemoryPressureVoterImpl; |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 66 | |
| 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 Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 71 | void OnVote(std::optional<MemoryPressureLevel> old_vote, |
| 72 | std::optional<MemoryPressureLevel> new_vote); |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 73 | |
| 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 Gale | b8be952 | 2024-04-16 00:00:31 | [diff] [blame] | 77 | // TODO(crbug.com/40639224): Remove this behavior and standardize across |
| 78 | // platforms. |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 79 | 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 Powell | 4b44d108 | 2019-08-29 15:21:12 | [diff] [blame] | 85 | MemoryPressureLevel current_pressure_level_ = |
| 86 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 87 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 88 | const raw_ptr<Delegate> delegate_; |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 89 | |
| 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 Powell | 4b44d108 | 2019-08-29 15:21:12 | [diff] [blame] | 99 | votes_ = {}; |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 100 | |
| 101 | SEQUENCE_CHECKER(sequence_checker_); |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | // Interface used to notify MemoryPressureVoteAggregator's owner of changes to |
| 105 | // vote aggregation. |
| 106 | class 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. Wong | 23183a1 | 2021-08-13 00:23:35 | [diff] [blame] | 120 | } // namespace memory_pressure |
Ryan Powell | 30287d4 | 2019-08-15 15:48:32 | [diff] [blame] | 121 | |
Albert J. Wong | 23183a1 | 2021-08-13 00:23:35 | [diff] [blame] | 122 | #endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_ |