File: cpu_measurement_delegate.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 4,196 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/public/resource_attribution/cpu_measurement_delegate.h"

#include "base/check.h"
#include "base/no_destructor.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/process/process_metrics.h"
#include "base/types/expected.h"
#include "build/build_config.h"
#include "components/performance_manager/public/graph/process_node.h"
#include "components/performance_manager/resource_attribution/cpu_measurement_monitor.h"
#include "components/performance_manager/resource_attribution/performance_manager_aliases.h"
#include "components/performance_manager/resource_attribution/query_scheduler.h"
#include "content/public/browser/browser_child_process_host.h"
#include "content/public/common/process_type.h"

namespace resource_attribution {

namespace {

// A production CPUMeasurementDelegate that measures processes with
// base::ProcessMetrics.
class CPUMeasurementDelegateImpl final : public CPUMeasurementDelegate {
 public:
  explicit CPUMeasurementDelegateImpl(const ProcessNode* process_node);
  ~CPUMeasurementDelegateImpl() final = default;

  base::expected<base::TimeDelta, ProcessCPUUsageError> GetCumulativeCPUUsage()
      final;

 private:
  std::unique_ptr<base::ProcessMetrics> process_metrics_;
};

CPUMeasurementDelegateImpl::CPUMeasurementDelegateImpl(
    const ProcessNode* process_node) {
  const base::ProcessHandle handle = process_node->GetProcess().Handle();
#if BUILDFLAG(IS_MAC)
  process_metrics_ = base::ProcessMetrics::CreateProcessMetrics(
      handle, content::BrowserChildProcessHost::GetPortProvider());
#else
  process_metrics_ = base::ProcessMetrics::CreateProcessMetrics(handle);
#endif
}

base::expected<base::TimeDelta, CPUMeasurementDelegate::ProcessCPUUsageError>
CPUMeasurementDelegateImpl::GetCumulativeCPUUsage() {
  return process_metrics_->GetCumulativeCPUUsage();
}

// The default production factory for CPUMeasurementDelegateImpl objects.
class CPUMeasurementDelegateFactoryImpl final
    : public CPUMeasurementDelegate::Factory {
 public:
  CPUMeasurementDelegateFactoryImpl() = default;
  ~CPUMeasurementDelegateFactoryImpl() final = default;

  bool ShouldMeasureProcess(const ProcessNode* process_node) final;
  std::unique_ptr<CPUMeasurementDelegate> CreateDelegateForProcess(
      const ProcessNode* process_node) final;
};

bool CPUMeasurementDelegateFactoryImpl::ShouldMeasureProcess(
    const ProcessNode* process_node) {
  // The process start time is not available until the ProcessId is assigned.
  if (process_node->GetProcessId() == base::kNullProcessId) {
    return false;
  }
  // This can be called from OnProcessLifetimeChange after a process exits.
  // Only handle process start notifications (which is when the pid is
  // assigned), not exit notifications. Note the pid can be reassigned if a
  // process dies and a new one is started for the same ProcessNode - in that
  // case MonitorCPUUsage will reset the measurements and start monitoring the
  // new process from scratch.
  if (!process_node->GetProcess().IsValid()) {
    return false;
  }
  return true;
}

std::unique_ptr<CPUMeasurementDelegate>
CPUMeasurementDelegateFactoryImpl::CreateDelegateForProcess(
    const ProcessNode* process_node) {
  return std::make_unique<CPUMeasurementDelegateImpl>(process_node);
}

}  // namespace

// static
void CPUMeasurementDelegate::SetDelegateFactoryForTesting(Graph* graph,
                                                          Factory* factory) {
  auto* scheduler = internal::QueryScheduler::GetFromGraph(graph);
  CHECK(scheduler);
  scheduler
      ->GetCPUMonitorForTesting()                      // IN-TEST
      .SetDelegateFactoryForTesting(factory ? factory  // IN-TEST
                                            : GetDefaultFactory());
}

// static
CPUMeasurementDelegate::Factory* CPUMeasurementDelegate::GetDefaultFactory() {
  static base::NoDestructor<CPUMeasurementDelegateFactoryImpl> default_factory;
  return default_factory.get();
}

}  // namespace resource_attribution