File: query_results.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 (119 lines) | stat: -rw-r--r-- 4,686 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
114
115
116
117
118
119
// 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.

#ifndef COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_RESOURCE_ATTRIBUTION_QUERY_RESULTS_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_RESOURCE_ATTRIBUTION_QUERY_RESULTS_H_

#include <compare>
#include <map>
#include <optional>

#include "base/time/time.h"
#include "components/performance_manager/public/resource_attribution/resource_contexts.h"
#include "components/performance_manager/public/resource_attribution/resource_types.h"

namespace resource_attribution {

// The Resource Attribution result and metadata structs described in
// https://bit.ly/resource-attribution-api#heading=h.k8fjwkwxxdj6.

// The methods used to produce a result.
enum class MeasurementAlgorithm {
  // The values in this result were measured directly, such as with an
  // OS system call.
  kDirectMeasurement,

  // The values in this result are estimates derived by subdividing direct
  // measurements of a context between several related contexts, such as with
  // SplitResourceAmongFramesAndWorkers().
  kSplit,

  // The values in this result are estimates derived by summing kSplit estimates
  // over a collection of contexts.
  kSum,
};

// Metadata about the measurement that produced a result.
struct ResultMetadata {
  // The time this measurement was taken.
  base::TimeTicks measurement_time;

  // Method used to assign measurement results to the resource context.
  MeasurementAlgorithm algorithm;

  // Constructor ensures both `measurement_time` and `algorithm` are set.
  //
  // Since there's no default constructor, any ResultType class containing
  // metadata also can't be default-constructed. This ensures none of them have
  // an invalid or uninitialized state. Use std::optional<ResultType> when
  // default-construction is needed.
  ResultMetadata(base::TimeTicks measurement_time,
                 MeasurementAlgorithm algorithm)
      : measurement_time(measurement_time), algorithm(algorithm) {}

  friend constexpr auto operator<=>(const ResultMetadata&,
                                    const ResultMetadata&) = default;
  friend constexpr bool operator==(const ResultMetadata&,
                                   const ResultMetadata&) = default;
};

// The result of a kCPUTime query.
struct CPUTimeResult {
  ResultMetadata metadata;

  // The time that Resource Attribution started monitoring the CPU usage of this
  // context.
  base::TimeTicks start_time;

  // Total time the context spent on CPU between `start_time` and
  // `metadata.measurement_time`.
  //
  // `cumulative_cpu` / (`metadata.measurement_time` - `start_time`)
  // gives percentage of CPU used as a fraction in the range 0% to 100% *
  // SysInfo::NumberOfProcessors(), the same as
  // ProcessMetrics::GetPlatformIndependentCPUUsage().
  base::TimeDelta cumulative_cpu;

  // Total time the context spent on CPU in a background process between
  // `start_time` and `metadata.measurement_time`. Time spent on CPU in a
  // foreground process doesn't affect this, even if the context itself was
  // backgrounded.
  base::TimeDelta cumulative_background_cpu;

  friend constexpr auto operator<=>(const CPUTimeResult&,
                                    const CPUTimeResult&) = default;
  friend constexpr bool operator==(const CPUTimeResult&,
                                   const CPUTimeResult&) = default;
};

// Results of a kMemorySummary query.
struct MemorySummaryResult {
  ResultMetadata metadata;
  uint64_t resident_set_size_kb = 0;
  uint64_t private_footprint_kb = 0;

  friend constexpr auto operator<=>(const MemorySummaryResult&,
                                    const MemorySummaryResult&) = default;
  friend constexpr bool operator==(const MemorySummaryResult&,
                                   const MemorySummaryResult&) = default;
};

// A container for at most one of each query result type. This is not a variant
// because it can contain more than one result.
struct QueryResults {
  std::optional<CPUTimeResult> cpu_time_result;
  std::optional<MemorySummaryResult> memory_summary_result;

  friend constexpr auto operator<=>(const QueryResults&,
                                    const QueryResults&) = default;
  friend constexpr bool operator==(const QueryResults&,
                                   const QueryResults&) = default;
};

// A map from a ResourceContext to all query results received for that context.
using QueryResultMap = std::map<ResourceContext, QueryResults>;

}  // namespace resource_attribution

#endif  // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_RESOURCE_ATTRIBUTION_QUERY_RESULTS_H_