File: page_resource_monitor.h

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 (113 lines) | stat: -rw-r--r-- 4,674 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
112
113
// 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 COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_METRICS_PAGE_RESOURCE_MONITOR_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_METRICS_PAGE_RESOURCE_MONITOR_H_

#include <map>
#include <memory>
#include <optional>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/resource_attribution/queries.h"
#include "components/performance_manager/public/resource_attribution/query_results.h"
#include "components/performance_manager/public/resource_attribution/resource_contexts.h"
#include "components/system_cpu/cpu_sample.h"

namespace performance_manager::metrics {

// Periodically reports tab resource usage via UKM.
class PageResourceMonitor : public resource_attribution::QueryResultObserver,
                            public GraphOwnedDefaultImpl {
 public:
  // These values are logged to UKM. Entries should not be renumbered and
  // numeric values should never be reused. Please keep in sync with
  // PageMeasurementAlgorithm in enums.xml.
  enum class PageMeasurementAlgorithm {
    kLegacy = 0,
    kEvenSplitAndAggregate = 1,
    kMaxValue = kEvenSplitAndAggregate,
  };

  // These values are logged to UKM. Entries should not be renumbered and
  // numeric values should never be reused. Please keep in sync with
  // PageMeasurementBackgroundState in enums.xml.
  enum class PageMeasurementBackgroundState {
    kForeground = 0,
    kBackground = 1,
    kAudibleInBackground = 2,
    kBackgroundMixedAudible = 3,
    kMixedForegroundBackground = 4,
    kMaxValue = kMixedForegroundBackground,
  };

  // If `enable_system_cpu_probe` is false, `system_cpu_probe_` will be left
  // null. This is mainly intended for tests.
  explicit PageResourceMonitor(bool enable_system_cpu_probe = true);

  ~PageResourceMonitor() override;
  PageResourceMonitor(const PageResourceMonitor& other) = delete;
  PageResourceMonitor& operator=(const PageResourceMonitor&) = delete;

  // QueryResultObserver:
  void OnResourceUsageUpdated(
      const resource_attribution::QueryResultMap& results) override;

  // Returns the time between calls to OnResourceUsageUpdated(). Tests can
  // advance the mock clock by this amount to trigger metrics collection.
  base::TimeDelta GetCollectionDelayForTesting() const;

 private:
  // The percent CPU usage for each PageNode that was measured. This stores a
  // ResourceContext instead of a node pointer in case the PageNode is deleted
  // while taking asynchronous system CPU measurements.
  using PageCPUUsageMap =
      std::map<resource_attribution::ResourceContext, double>;

  // Helper class that converts CPUTimeResult to proportion of CPU used over a
  // fixed interval, and adds system CPU to the result.
  class CPUResultConverter;

  // Invoked asynchronously from OnResourceUsageUpdate() when both page and
  // system CPU measurements are ready. `results` contains the original query
  // results, with both CPU and memory measurements. `page_cpu_usage` is the
  // proportion of CPU usage calculated for each page from the original results,
  // and `system_cpu` is the overall system CPU usage.
  void OnPageResourceUsageResult(
      const resource_attribution::QueryResultMap& results,
      const PageCPUUsageMap& page_cpu_usage,
      std::optional<system_cpu::CpuSample> system_cpu);

  SEQUENCE_CHECKER(sequence_checker_);

  // Repeating query that triggers OnResourceUsageUpdated on a timer.
  resource_attribution::ScopedResourceUsageQuery resource_query_
      GUARDED_BY_CONTEXT(sequence_checker_);

  // Manages notificatoin subscriptions to `resource_query_`.
  resource_attribution::ScopedQueryObservation query_observation_{this};

  // Time of last PageResourceUsage collection.
  base::TimeTicks time_of_last_resource_usage_
      GUARDED_BY_CONTEXT(sequence_checker_) = base::TimeTicks::Now();

  // Helpers to convert CPU measurements for UMA.
  std::unique_ptr<CPUResultConverter> cpu_result_converter_
      GUARDED_BY_CONTEXT(sequence_checker_);
  std::unique_ptr<CPUResultConverter> delayed_cpu_result_converter_
      GUARDED_BY_CONTEXT(sequence_checker_);

  // Graph being monitored.
  raw_ptr<Graph> graph_ GUARDED_BY_CONTEXT(sequence_checker_) = nullptr;

  base::WeakPtrFactory<PageResourceMonitor> weak_factory_{this};
};

}  // namespace performance_manager::metrics

#endif  // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_METRICS_PAGE_RESOURCE_MONITOR_H_