File: cpu_histogram_logger.cc

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 (161 lines) | stat: -rw-r--r-- 5,877 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/passage_embeddings/cpu_histogram_logger.h"

#include <memory>
#include <utility>

#include "base/check_op.h"
#include "base/metrics/histogram_functions.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "components/performance_manager/public/resource_attribution/cpu_proportion_tracker.h"
#include "components/performance_manager/public/resource_attribution/process_context.h"
#include "components/performance_manager/public/resource_attribution/queries.h"
#include "components/performance_manager/public/resource_attribution/query_results.h"
#include "content/public/browser/browser_child_process_host.h"

namespace {

constexpr base::TimeDelta kSampleInterval = base::Seconds(30);
constexpr int kCpuUsageFactor = 100 * 100;
constexpr int kCpuUsageMin = 1;
constexpr int kCpuUsageMax = 400 * kCpuUsageFactor;
constexpr int kBucketCount = 100;

}  // namespace

namespace passage_embeddings {

class CpuHistogramLogger::CpuObserver
    : public resource_attribution::QueryResultObserver {
 public:
  CpuObserver(const resource_attribution::ProcessContext& context,
              base::RepeatingCallback<bool()> poll_embedder_running);
  ~CpuObserver() override;

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

  void MakeSelfOwned(std::unique_ptr<CpuObserver> self);

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

 private:
  resource_attribution::ScopedResourceUsageQuery scoped_query_;
  resource_attribution::ScopedQueryObservation query_observation_{this};
  resource_attribution::CPUProportionTracker proportion_tracker_;
  bool proportion_tracker_started_ = false;

  base::RepeatingCallback<bool()> poll_embedder_running_;
  bool embedder_was_running_ = false;

  std::unique_ptr<CpuObserver> self_;
};

CpuHistogramLogger::CpuObserver::CpuObserver(
    const resource_attribution::ProcessContext& context,
    base::RepeatingCallback<bool()> poll_embedder_running)
    : scoped_query_(
          resource_attribution::QueryBuilder()
              .AddResourceType(resource_attribution::ResourceType::kCPUTime)
              .AddResourceContext(context)
              .CreateScopedQuery()),
      poll_embedder_running_(poll_embedder_running) {
  query_observation_.Observe(&scoped_query_);
  scoped_query_.Start(kSampleInterval);
  // Take an immediate baseline measurement for proportion tracker.
  scoped_query_.QueryOnce();
}

CpuHistogramLogger::CpuObserver::~CpuObserver() = default;

void CpuHistogramLogger::CpuObserver::MakeSelfOwned(
    std::unique_ptr<CpuObserver> self) {
  CHECK_EQ(self.get(), this);
  CHECK(!self_);
  self_ = std::move(self);
}

void CpuHistogramLogger::CpuObserver::OnResourceUsageUpdated(
    const resource_attribution::QueryResultMap& results) {
  // If MakeSelfOwned() was called, delete `this` on function exit.
  std::unique_ptr<CpuObserver> to_delete = std::move(self_);

  if (results.size() == 0) {
    // Service process couldn't be measured.
    return;
  }

  CHECK_EQ(results.size(), 1ul);

  if (!proportion_tracker_started_) {
    proportion_tracker_.StartFirstInterval(base::TimeTicks::Now(), results);
    proportion_tracker_started_ = true;
  } else {
    std::map<resource_attribution::ResourceContext, double> cpu_proportion =
        proportion_tracker_.StartNextInterval(base::TimeTicks::Now(), results);
    if (cpu_proportion.size() == 0) {
      // When the service process exits close to the measurement time, the
      // proportion tracker might skip the final measurement due to a race.
      return;
    }
    CHECK_EQ(cpu_proportion.size(), 1ul);
    int sample = cpu_proportion.begin()->second * kCpuUsageFactor;
    base::UmaHistogramCustomCounts("History.Embeddings.Embedder.CpuUsage2",
                                   sample, kCpuUsageMin, kCpuUsageMax,
                                   kBucketCount);

    if (!to_delete) {
      const bool embedder_is_running = poll_embedder_running_.Run();
      if (!embedder_was_running_ && !embedder_is_running) {
        base::UmaHistogramCustomCounts(
            "History.Embeddings.Embedder.CpuUsage2.NotRunning", sample,
            kCpuUsageMin, kCpuUsageMax, kBucketCount);
      } else if (embedder_was_running_ && embedder_is_running) {
        base::UmaHistogramCustomCounts(
            "History.Embeddings.Embedder.CpuUsage2.Running", sample,
            kCpuUsageMin, kCpuUsageMax, kBucketCount);
      } else {
        base::UmaHistogramCustomCounts(
            "History.Embeddings.Embedder.CpuUsage2.RunningPartial", sample,
            kCpuUsageMin, kCpuUsageMax, kBucketCount);
      }
      embedder_was_running_ = embedder_is_running;
    }
  }
}

CpuHistogramLogger::CpuHistogramLogger() = default;

CpuHistogramLogger::~CpuHistogramLogger() {
  StopLoggingAfterNextUpdate();
  CHECK(!cpu_observer_);
}

void CpuHistogramLogger::StartLogging(
    content::BrowserChildProcessHost* utility_process_host,
    base::RepeatingCallback<bool()> poll_embedder_running) {
  CHECK(!cpu_observer_);
  auto process_context =
      resource_attribution::ProcessContext::FromBrowserChildProcessHost(
          utility_process_host);
  if (process_context.has_value()) {
    cpu_observer_ = std::make_unique<CpuObserver>(process_context.value(),
                                                  poll_embedder_running);
  }
}

void CpuHistogramLogger::StopLoggingAfterNextUpdate() {
  // The observer will finish the current interval and then delete itself.
  CpuObserver* observer = cpu_observer_.get();
  if (observer) {
    observer->MakeSelfOwned(std::move(cpu_observer_));
  }
}

}  // namespace passage_embeddings