1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72

content / browser / memory_pressure / user_level_memory_pressure_signal_generator.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_MEMORY_PRESSURE_USER_LEVEL_MEMORY_PRESSURE_SIGNAL_GENERATOR_H_
#define CONTENT_BROWSER_MEMORY_PRESSURE_USER_LEVEL_MEMORY_PRESSURE_SIGNAL_GENERATOR_H_

#include "build/build_config.h"

#if BUILDFLAG(IS_ANDROID)
#include <optional>
#include <utility>

#include "base/no_destructor.h"
#include "base/timer/timer.h"

namespace base {
class Process;
class TimeDelta;
}  // namespace base

namespace memory_pressure {

// Generates extra memory pressure signals (on top of the OS generated ones)
// when the memory usage exceeds a threshold.
class UserLevelMemoryPressureSignalGenerator {
 public:
  static void Initialize();

 private:
  friend class base::NoDestructor<UserLevelMemoryPressureSignalGenerator>;

  // Singleton
  static UserLevelMemoryPressureSignalGenerator& Get();

  UserLevelMemoryPressureSignalGenerator();
  ~UserLevelMemoryPressureSignalGenerator();

  void Start(uint64_t memory_threshold,
             base::TimeDelta measure_interval,
             base::TimeDelta minimum_interval);
  void OnTimerFired();
  void OnReportingTimerFired();

  void StartPeriodicTimer(base::TimeDelta interval);
  void StartReportingTimer();

  static std::pair<uint64_t, uint64_t>
  GetTotalPrivateFootprintVisibleOrHigherPriorityRenderers();

  static void NotifyMemoryPressure();

  static void ReportBeforeAfterMetrics(
      uint64_t total_pmf_visible_or_higher_priority_renderers,
      uint64_t total_pmf,
      const char* suffix_name);

  static std::optional<uint64_t> GetPrivateFootprint(
      const base::Process& process);

  uint64_t memory_threshold_;
  base::TimeDelta measure_interval_;
  base::TimeDelta minimum_interval_;
  base::OneShotTimer periodic_measuring_timer_;
  base::OneShotTimer delayed_report_timer_;
};

}  // namespace memory_pressure

#endif  // BUILDFLAG(IS_ANDROID)

#endif  // CONTENT_BROWSER_MEMORY_PRESSURE_USER_LEVEL_MEMORY_PRESSURE_SIGNAL_GENERATOR_H_