memory_pressure_voter.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_
  5. #define COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_
  6. #include <array>
  7. #include "base/callback.h"
  8. #include "base/memory/memory_pressure_listener.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace memory_pressure {
  14. // Interface used by code which actually monitors memory pressure, to inform
  15. // a MemoryPressureAggregator when the pressure they observe changes, or they
  16. // want to trigger a (re-)notification of clients of the current level.
  17. // Voters must be used only from the same sequence as the Aggregator to which
  18. // they are attached.
  19. class MemoryPressureVoter {
  20. public:
  21. virtual ~MemoryPressureVoter() {}
  22. // Called to set a vote / change a vote.
  23. virtual void SetVote(base::MemoryPressureListener::MemoryPressureLevel level,
  24. bool notify_listeners) = 0;
  25. };
  26. // Collects votes from MemoryPressureVoters and evaluates them to determine the
  27. // pressure level for the MultiSourceMemoryPressureMonitor, which will own
  28. // and outlive the aggregator. The pressure level is calculated as the most
  29. // critical of all votes collected. This class is not thread safe and should be
  30. // used from a single sequence.
  31. class MemoryPressureVoteAggregator {
  32. public:
  33. class Delegate;
  34. using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
  35. explicit MemoryPressureVoteAggregator(Delegate* delegate);
  36. ~MemoryPressureVoteAggregator();
  37. MemoryPressureVoteAggregator(const MemoryPressureVoteAggregator&) = delete;
  38. MemoryPressureVoteAggregator& operator=(const MemoryPressureVoteAggregator&) =
  39. delete;
  40. // Creates a MemoryPressureVoter attached to this Aggregator. The returned
  41. // Voter must not out-live the Aggregator.
  42. std::unique_ptr<MemoryPressureVoter> CreateVoter();
  43. void OnVoteForTesting(absl::optional<MemoryPressureLevel> old_vote,
  44. absl::optional<MemoryPressureLevel> new_vote);
  45. void NotifyListenersForTesting();
  46. base::MemoryPressureListener::MemoryPressureLevel EvaluateVotesForTesting();
  47. void SetVotesForTesting(size_t none_votes,
  48. size_t moderate_votes,
  49. size_t critical_votes);
  50. private:
  51. friend class MemoryPressureVoterImpl;
  52. // Invoked by MemoryPressureVoter as it calculates its vote. Optional is
  53. // used so a voter can pass null as |old_vote| if this is their first vote, or
  54. // null as |new_vote| if they are removing their vote (e.g. when the voter is
  55. // being destroyed). |old_vote| and |new_vote| should never both be null.
  56. void OnVote(absl::optional<MemoryPressureLevel> old_vote,
  57. absl::optional<MemoryPressureLevel> new_vote);
  58. // Triggers a notification of the MemoryPressureMonitor's current pressure
  59. // level, allowing each of the various sources of input on MemoryPressureLevel
  60. // to maintain their own signalling behavior.
  61. // TODO(991361): Remove this behavior and standardize across platforms.
  62. void NotifyListeners();
  63. // Returns the highest index of |votes_| with a non-zero value, as a
  64. // MemoryPressureLevel.
  65. MemoryPressureLevel EvaluateVotes() const;
  66. MemoryPressureLevel current_pressure_level_ =
  67. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
  68. const raw_ptr<Delegate> delegate_;
  69. // Array with one bucket for each potential MemoryPressureLevel. The overall
  70. // MemoryPressureLevel is calculated as the highest index of a non-zero
  71. // bucket.
  72. // MEMORY_PRESSURE_LEVEL_CRITICAL + 1 is used in place of adding a kCount
  73. // value to the MemoryPressureLevel enum as adding another value would require
  74. // changing every instance of switch(MemoryPressureLevel) in Chromium, and the
  75. // MemoryPressureLevel system will be changing soon regardless.
  76. std::array<size_t,
  77. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL + 1>
  78. votes_ = {};
  79. SEQUENCE_CHECKER(sequence_checker_);
  80. };
  81. // Interface used to notify MemoryPressureVoteAggregator's owner of changes to
  82. // vote aggregation.
  83. class MemoryPressureVoteAggregator::Delegate {
  84. public:
  85. Delegate() = default;
  86. virtual ~Delegate() = default;
  87. // Invoked when the aggregate vote has changed.
  88. virtual void OnMemoryPressureLevelChanged(
  89. base::MemoryPressureListener::MemoryPressureLevel level) = 0;
  90. // Invoked when a voter has determined that a notification of the current
  91. // pressure level is necessary.
  92. virtual void OnNotifyListenersRequested() = 0;
  93. };
  94. } // namespace memory_pressure
  95. #endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_VOTER_H_