File: enterprise_memory_limit_evaluator.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download | duplicates (6)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEMORY_ENTERPRISE_MEMORY_LIMIT_EVALUATOR_H_
#define CHROME_BROWSER_MEMORY_ENTERPRISE_MEMORY_LIMIT_EVALUATOR_H_

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "components/memory_pressure/memory_pressure_voter.h"
#include "components/performance_manager/public/decorators/process_metrics_decorator.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/system_node.h"

namespace memory {

// Periodically receives updates on Chrome's current total resident set,
// compares that usage to the limit set via an Enterprise policy, and uses a
// MemoryLimitVoter to send its vote on the MemoryPressureLevel to the
// MultiSourceMemoryPressureMonitor.
class EnterpriseMemoryLimitEvaluator {
 protected:
  class GraphObserver;

 public:
  explicit EnterpriseMemoryLimitEvaluator(
      std::unique_ptr<memory_pressure::MemoryPressureVoter> voter);

  EnterpriseMemoryLimitEvaluator(const EnterpriseMemoryLimitEvaluator&) =
      delete;
  EnterpriseMemoryLimitEvaluator& operator=(
      const EnterpriseMemoryLimitEvaluator&) = delete;

  ~EnterpriseMemoryLimitEvaluator();

  // Starts/stops observing the resident set of Chrome processes and notifying
  // its MemoryPressureVoter when it is above the limit.
  void Start();
  void Stop();

  std::unique_ptr<GraphObserver> StartForTesting();
  void StopForTesting();

  // Sets the limit against which the total resident set will be compared.
  void SetResidentSetLimitMb(uint64_t resident_set_limit_mb);

  // Indicates if this evaluator is currently running.
  bool IsRunning() const;

 private:
  // Called on the sequence that owns this object every time a new total RSS
  // sample is available.
  void OnTotalResidentSetKbSample(uint64_t resident_set_sample_kb);

  // Raw pointer to the GraphObserver used by this object to monitor the total
  // RSS. This is only meant to be used as a key to remove the observer once
  // it's not necessary anymore, do not call functions directly from this
  // pointer.
  raw_ptr<GraphObserver> observer_ = nullptr;

  uint64_t resident_set_limit_mb_ = 0;

  const std::unique_ptr<memory_pressure::MemoryPressureVoter> voter_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<EnterpriseMemoryLimitEvaluator> weak_ptr_factory_;
};

// Instances of this class are constructed and destructed on the main thread.
// They are then passed to the Performance Manager's Graph and a task gets
// posted to the proper task runner when new data is available.
class EnterpriseMemoryLimitEvaluator::GraphObserver
    : public performance_manager::SystemNodeObserver,
      public performance_manager::GraphOwned {
 public:
  // The constructor of this class takes 2 parameters: the callback to run each
  // time a new sample is available and the task runner on which this callback
  // should run.
  GraphObserver(base::RepeatingCallback<void(uint64_t)> on_sample_callback,
                scoped_refptr<base::SequencedTaskRunner> task_runner);

  ~GraphObserver() override;

  // GraphOwned, called on the PM sequence:
  void OnPassedToGraph(performance_manager::Graph* graph) override;
  void OnTakenFromGraph(performance_manager::Graph* graph) override;

  // SystemNodeObserver, called on the PM sequence:
  void OnProcessMemoryMetricsAvailable(
      const performance_manager::SystemNode* system_node) override;

  void OnProcessMemoryMetricsAvailableForTesting(uint64_t total_rss_kb);

 private:
  const base::RepeatingCallback<void(uint64_t)> on_sample_callback_;

  std::unique_ptr<
      performance_manager::ProcessMetricsDecorator::ScopedMetricsInterestToken>
      metrics_interest_token_;

  // The task runner on which |on_sample_callback_| should be invoked.
  const scoped_refptr<base::SequencedTaskRunner> task_runner_;
};

}  // namespace memory

#endif  // CHROME_BROWSER_MEMORY_ENTERPRISE_MEMORY_LIMIT_EVALUATOR_H_