system_memory_pressure_evaluator_linux.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2020 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_SYSTEM_MEMORY_PRESSURE_EVALUATOR_LINUX_H_
  5. #define COMPONENTS_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_LINUX_H_
  6. #include "base/memory/memory_pressure_listener.h"
  7. #include "base/process/process_metrics.h"
  8. #include "base/sequence_checker.h"
  9. #include "base/time/time.h"
  10. #include "base/timer/timer.h"
  11. #include "components/memory_pressure/memory_pressure_voter.h"
  12. #include "components/memory_pressure/system_memory_pressure_evaluator.h"
  13. namespace memory_pressure {
  14. namespace os_linux {
  15. // Linux memory pressure voter. Because there is no OS provided signal this
  16. // polls at a low frequency, and applies internal hysteresis.
  17. // TODO(https://crbug.com/1119396): use Pressure Stall Information (PSI) on
  18. // kernels >4.20.
  19. class SystemMemoryPressureEvaluator
  20. : public memory_pressure::SystemMemoryPressureEvaluator {
  21. public:
  22. using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
  23. // The memory sampling period, currently 5s.
  24. static const base::TimeDelta kMemorySamplingPeriod;
  25. // Constants governing the polling and hysteresis behaviour of the observer.
  26. // The time which should pass between 2 successive moderate memory pressure
  27. // signals.
  28. static const base::TimeDelta kModeratePressureCooldown;
  29. // Default minimum free memory thresholds, in percents.
  30. static const int kDefaultModerateThresholdPc;
  31. static const int kDefaultCriticalThresholdPc;
  32. // Default constructor. Will choose thresholds automatically based on the
  33. // actual amount of system memory.
  34. explicit SystemMemoryPressureEvaluator(
  35. std::unique_ptr<MemoryPressureVoter> voter);
  36. // Constructor with explicit memory thresholds. These represent the amount of
  37. // free memory below which the applicable memory pressure state engages.
  38. SystemMemoryPressureEvaluator(int moderate_threshold_mb,
  39. int critical_threshold_mb,
  40. std::unique_ptr<MemoryPressureVoter> voter);
  41. ~SystemMemoryPressureEvaluator() override = default;
  42. SystemMemoryPressureEvaluator(const SystemMemoryPressureEvaluator&) = delete;
  43. SystemMemoryPressureEvaluator& operator=(
  44. const SystemMemoryPressureEvaluator&) = delete;
  45. // Returns the moderate pressure level free memory threshold, in MB.
  46. int moderate_threshold_mb() const { return moderate_threshold_mb_; }
  47. // Returns the critical pressure level free memory threshold, in MB.
  48. int critical_threshold_mb() const { return critical_threshold_mb_; }
  49. protected:
  50. // Internals are exposed for unittests.
  51. // Starts observing the memory fill level. Calls to StartObserving should
  52. // always be matched with calls to StopObserving.
  53. void StartObserving();
  54. // Stop observing the memory fill level. May be safely called if
  55. // StartObserving has not been called. Must be called from the same thread on
  56. // which the monitor was instantiated.
  57. void StopObserving();
  58. // Checks memory pressure, storing the current level, applying any hysteresis
  59. // and emitting memory pressure level change signals as necessary. This
  60. // function is called periodically while the monitor is observing memory
  61. // pressure. Must be called from the same thread on which the monitor was
  62. // instantiated.
  63. void CheckMemoryPressure();
  64. // Automatically infers threshold values based on system memory.
  65. // Returns 'true' if succeeded.
  66. bool InferThresholds();
  67. // Calculates the current instantaneous memory pressure level. This does not
  68. // use any hysteresis and simply returns the result at the current moment. Can
  69. // be called on any thread.
  70. MemoryPressureLevel CalculateCurrentPressureLevel();
  71. // This is just a wrapper for base:: function;
  72. // declared as virtual for unit testing
  73. virtual bool GetSystemMemoryInfo(base::SystemMemoryInfoKB* mem_info);
  74. private:
  75. // Threshold amounts of available memory that trigger pressure levels
  76. int moderate_threshold_mb_;
  77. int critical_threshold_mb_;
  78. // A periodic timer to check for memory pressure changes.
  79. base::RepeatingTimer timer_;
  80. // To slow down the amount of moderate pressure event calls, this gets used to
  81. // count the number of events since the last event occurred. This is used by
  82. // |CheckMemoryPressure| to apply hysteresis on the raw results of
  83. // |CalculateCurrentPressureLevel|.
  84. int moderate_pressure_repeat_count_;
  85. // Ensures that this object is used from a single sequence.
  86. SEQUENCE_CHECKER(sequence_checker_);
  87. };
  88. } // namespace os_linux
  89. } // namespace memory_pressure
  90. #endif // COMPONENTS_MEMORY_PRESSURE_SYSTEM_MEMORY_PRESSURE_EVALUATOR_LINUX_H_