File: memory_measurement_delegate.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 (83 lines) | stat: -rw-r--r-- 3,046 bytes parent folder | download | duplicates (5)
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
// 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_MEMORY_MEASUREMENT_DELEGATE_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_RESOURCE_ATTRIBUTION_MEMORY_MEASUREMENT_DELEGATE_H_

#include <stddef.h>
#include <stdint.h>

#include <compare>
#include <map>
#include <memory>

#include "base/functional/callback_forward.h"

namespace performance_manager {
class Graph;
}

namespace resource_attribution {

class ProcessContext;

// A shim that Resource Attribution queries use to request memory measurements.
// Public so that users of the API can inject a test override by passing a
// factory object to SetDelegateFactoryForTesting().
class MemoryMeasurementDelegate {
 public:
  class Factory;

  // The minimal results returned for a process memory measurement.
  // MemoryMeasurementProvider will wrap this in a full MemorySummaryResult.
  struct MemorySummaryMeasurement {
    uint64_t resident_set_size_kb = 0;
    uint64_t private_footprint_kb = 0;

    // Division operator required by SplitResourceAmongFramesAndWorkers().
    constexpr MemorySummaryMeasurement operator/(size_t divisor) {
      return MemorySummaryMeasurement{
          .resident_set_size_kb = resident_set_size_kb / divisor,
          .private_footprint_kb = private_footprint_kb / divisor};
    }

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

  using MemorySummaryMap = std::map<ProcessContext, MemorySummaryMeasurement>;

  // The given `factory` will be used to create a MemoryMeasurementDelegate to
  // measure ProcessNodes in `graph`. The factory object must outlive the graph.
  // Usually it's owned by the test harness. nullptr will cause the factory
  // returned by GetDefaultFactory() to be used.
  static void SetDelegateFactoryForTesting(performance_manager::Graph* graph,
                                           Factory* factory);

  // Returns the default factory to use in production.
  static Factory* GetDefaultFactory();

  virtual ~MemoryMeasurementDelegate() = default;

  // Requests a memory summary for all processes. `callback` will be invoked
  // with the results, or an empty map on error.
  virtual void RequestMemorySummary(
      base::OnceCallback<void(MemorySummaryMap)>) = 0;
};

class MemoryMeasurementDelegate::Factory {
 public:
  virtual ~Factory() = default;

  // Creates a MemoryMeasurementDelegate for all ProcessNodes in `graph`.
  virtual std::unique_ptr<MemoryMeasurementDelegate> CreateDelegate(
      performance_manager::Graph* graph) = 0;
};

}  // namespace resource_attribution

#endif  // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_RESOURCE_ATTRIBUTION_MEMORY_MEASUREMENT_DELEGATE_H_