memory_pressure_level_reporter.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2021 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_LEVEL_REPORTER_H_
  5. #define COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_LEVEL_REPORTER_H_
  6. #include <array>
  7. #include "base/memory/memory_pressure_listener.h"
  8. #include "base/time/time.h"
  9. #include "base/timer/timer.h"
  10. namespace memory_pressure {
  11. // Report metrics related to memory pressure.
  12. class MemoryPressureLevelReporter {
  13. public:
  14. using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
  15. explicit MemoryPressureLevelReporter(
  16. MemoryPressureLevel initial_pressure_level);
  17. ~MemoryPressureLevelReporter();
  18. // Should be called whenever the current memory pressure level changes.
  19. void OnMemoryPressureLevelChanged(MemoryPressureLevel new_level);
  20. private:
  21. void ReportHistogram(base::TimeTicks now);
  22. void StartPeriodicTimer();
  23. MemoryPressureLevel current_pressure_level_;
  24. base::TimeTicks current_pressure_level_begin_ = base::TimeTicks::Now();
  25. // The reporting of the pressure level histogram is done in seconds, the
  26. // duration in a given pressure state will be floored. This means that some
  27. // time will be truncated each time we send a report. This array is used to
  28. // accumulate the truncated time and add it to the reported value when it
  29. // exceeds one second.
  30. std::array<base::TimeDelta, MemoryPressureLevel::kMaxValue + 1>
  31. accumulator_buckets_;
  32. // Timer used to ensure a periodic reporting of the pressure level metric.
  33. // Without this there's a risk that a browser crash will cause some data to
  34. // be lost.
  35. base::OneShotTimer periodic_reporting_timer_;
  36. };
  37. } // namespace memory_pressure
  38. #endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_LEVEL_REPORTER_H_