File: background_tracing_metrics_provider.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 (135 lines) | stat: -rw-r--r-- 5,995 bytes parent folder | download | duplicates (2)
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
// 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.

#include "components/tracing/common/background_tracing_metrics_provider.h"

#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/bind_post_task.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "components/metrics/content/gpu_metrics_provider.h"
#include "components/metrics/cpu_metrics_provider.h"
#include "components/metrics/metrics_features.h"
#include "components/metrics/metrics_log.h"
#include "content/public/browser/background_tracing_manager.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#include "third_party/metrics_proto/trace_log.pb.h"

namespace tracing {

BackgroundTracingMetricsProvider::BackgroundTracingMetricsProvider() = default;
BackgroundTracingMetricsProvider::~BackgroundTracingMetricsProvider() = default;

void BackgroundTracingMetricsProvider::Init() {
  system_profile_providers_.emplace_back(
      std::make_unique<metrics::CPUMetricsProvider>());
  system_profile_providers_.emplace_back(
      std::make_unique<metrics::GPUMetricsProvider>());

  content::BackgroundTracingManager::GetInstance().SetSystemProfileRecorder(
      base::BindRepeating(
          [](base::WeakPtr<BackgroundTracingMetricsProvider> self) {
            if (self) {
              return self->RecordSystemProfileMetrics();
            }
            return std::string();
          },
          weak_factory_.GetWeakPtr()));

  DoInit();
}

std::string BackgroundTracingMetricsProvider::RecordSystemProfileMetrics() {
  metrics::SystemProfileProto system_profile_proto;
  RecordCoreSystemProfileMetrics(&system_profile_proto);
  // RecordCoreSystemProfileMetrics is overridden by subclasses in
  // Chrome/WebView to provide core system profile metrics.
  // BackgroundTracingManager stores the returned system profile together with
  // the trace in the trace database at trace recording time.
  // ProvideIndependentMetrics() later overrides the system_profile in the log
  // proto with these stored metrics, to ensure that the uploaded system profile
  // matches the system profile at trace recording time.
  for (auto& provider : system_profile_providers_) {
    provider->ProvideSystemProfileMetricsWithLogCreationTime(
        base::TimeTicks::Now(), &system_profile_proto);
  }
  std::string serialized_system_profile;
  system_profile_proto.SerializeToString(&serialized_system_profile);
  return serialized_system_profile;
}

bool BackgroundTracingMetricsProvider::HasIndependentMetrics() {
  return content::BackgroundTracingManager::GetInstance().HasTraceToUpload();
}

void BackgroundTracingMetricsProvider::ProvideIndependentMetrics(
    base::OnceClosure serialize_log_callback,
    base::OnceCallback<void(bool)> done_callback,
    metrics::ChromeUserMetricsExtension* uma_proto,
    base::HistogramSnapshotManager* snapshot_manager) {
  auto task_runner = base::SequencedTaskRunner::GetCurrentDefault();
  auto provide_embedder_metrics = GetEmbedderMetricsProvider();
  content::BackgroundTracingManager::GetInstance().GetTraceToUpload(
      base::BindOnce(
          [](base::OnceCallback<bool(metrics::ChromeUserMetricsExtension*,
                                     std::string&&)> provide_embedder_metrics,
             base::OnceClosure serialize_log_callback,
             base::OnceCallback<void(bool)> done_callback,
             metrics::ChromeUserMetricsExtension* uma_proto,
             scoped_refptr<base::SequencedTaskRunner> task_runner,
             std::optional<std::string> compressed_trace_content,
             std::optional<std::string> serialized_system_profile,
             base::OnceClosure upload_complete) {
            if (!compressed_trace_content ||
                !std::move(provide_embedder_metrics)
                     .Run(uma_proto, std::move(*compressed_trace_content))) {
              task_runner->PostTask(
                  FROM_HERE, base::BindOnce(std::move(done_callback), false));
              return;
            }
            if (serialized_system_profile) {
              metrics::SystemProfileProto system_profile;
              system_profile.ParsePartialFromString(*serialized_system_profile);
              uma_proto->mutable_system_profile()->MergeFrom(system_profile);
            }
            // Serialize the log on a worker thread instead of on the main
            // thread.
            base::ThreadPool::PostTask(
                FROM_HERE,
                {base::TaskPriority::USER_VISIBLE,
                 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
                std::move(serialize_log_callback)
                    .Then(base::BindPostTask(
                        task_runner,
                        base::BindOnce(std::move(done_callback)
                                           .Then(std::move(upload_complete)),
                                       true))));
          },
          std::move(provide_embedder_metrics),
          std::move(serialize_log_callback), std::move(done_callback),
          uma_proto, task_runner));
}

base::OnceCallback<bool(metrics::ChromeUserMetricsExtension*, std::string&&)>
BackgroundTracingMetricsProvider::GetEmbedderMetricsProvider() {
  return base::BindOnce([](metrics::ChromeUserMetricsExtension* uma_proto,
                           std::string&& compressed_trace) {
    SetTrace(uma_proto->add_trace_log(), std::move(compressed_trace));
    return true;
  });
}

// static
void BackgroundTracingMetricsProvider::SetTrace(
    metrics::TraceLog* log,
    std::string&& compressed_trace) {
  base::UmaHistogramCounts100000("Tracing.Background.UploadingTraceSizeInKB",
                                 compressed_trace.size() / 1024);

  log->set_raw_data(std::move(compressed_trace));
  log->set_compression_type(metrics::TraceLog::COMPRESSION_TYPE_ZLIB);
}

}  // namespace tracing