File: cpu_probe.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 (100 lines) | stat: -rw-r--r-- 3,890 bytes parent folder | download | duplicates (9)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SYSTEM_CPU_CPU_PROBE_H_
#define COMPONENTS_SYSTEM_CPU_CPU_PROBE_H_

#include <memory>
#include <optional>

#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/thread_annotations.h"
#include "components/system_cpu/cpu_sample.h"

namespace system_cpu {

// Interface for retrieving the CPU load from the underlying OS on request.
//
// Operating systems differ in how they summarize the info needed to derive the
// compute pressure state. For example, the Linux kernel exposes CPU utilization
// as a summary over the device's entire uptime, while the Windows WMI exposes
// CPU utilization over the last second.
//
// This interface abstracts over the differences with a unified model where the
// implementation is responsible for integrating over the time between two
// Update() calls.
//
// Callers must call StartSampling() to establish a baseline. After this they
// can call RequestUpdate(), generally on a fixed schedule. Each call returns
// the total CPU usage of processors since the baseline or the last
// RequestUpdate() call.
//
// Instances are not thread-safe and should be used on the same sequence.
class CpuProbe {
 public:
  using SampleCallback = base::OnceCallback<void(std::optional<CpuSample>)>;

  // Instantiates the CpuProbe subclass most suitable for the current platform.
  //
  // Returns nullptr if no suitable implementation exists.
  static std::unique_ptr<CpuProbe> Create();

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

  virtual ~CpuProbe();

  // Samples the CPU load to get a baseline for calls to RequestSample(). May be
  // called again to refresh the baseline.
  // `started_callback` will be invoked once the baseline is available, so tests
  // can verify the timing.
  void StartSampling(base::OnceClosure started_callback = base::DoNothing());

  // Invokes `callback` with the CPU load since the last call to
  // RequestSample(). It's an error to call this without calling StartSampling()
  // first.
  void RequestSample(SampleCallback callback);

  // Called to return a WeakPtr to the CpuProbe. Subclasses must own a
  // WeakPtrFactory to implement this.
  virtual base::WeakPtr<CpuProbe> GetWeakPtr() = 0;

 protected:
  // The constructor is intentionally only exposed to subclasses. Production
  // code must use the Create() factory method.
  CpuProbe();

  // Implemented by subclasses to retrieve the CPU usage for different operating
  // systems. It must call `callback` with the result.
  virtual void Update(SampleCallback callback) = 0;

  SEQUENCE_CHECKER(sequence_checker_);

 private:
  // Called with the result of an Update() triggered by StartSampling(). Will do
  // some bookkeeping and then call `started_callback`, ignoring the
  // CpuSample.
  void OnSamplingStarted(base::OnceClosure started_callback,
                         std::optional<CpuSample>);

  // Called with the result of an Update() triggered by RequestSample(). Will do
  // some bookkeeping and then pass `sample` to `callback`.
  void OnSampleAvailable(SampleCallback callback,
                         std::optional<CpuSample> sample);

  // True if the CpuProbe state will be reported after the next update.
  //
  // The CpuSample reported by many CpuProbe implementations relies
  // on the differences observed between two Update() calls. For this reason,
  // the CpuSample reported after a first Update() call is not
  // reported via the SampleCallback.
  bool got_probe_baseline_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
};

}  // namespace system_cpu

#endif  // COMPONENTS_SYSTEM_CPU_CPU_PROBE_H_