File: child_histogram_fetcher_impl.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (120 lines) | stat: -rw-r--r-- 4,695 bytes parent folder | download | duplicates (4)
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
// Copyright 2012 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/metrics/child_histogram_fetcher_impl.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/metrics/histogram_delta_serialization.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/histogram_macros_local.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "components/metrics/public/mojom/histogram_fetcher.mojom.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/system/platform_handle.h"

namespace metrics {

ChildHistogramFetcherFactoryImpl::ChildHistogramFetcherFactoryImpl() = default;

ChildHistogramFetcherFactoryImpl::~ChildHistogramFetcherFactoryImpl() = default;

void ChildHistogramFetcherFactoryImpl::Create(
    mojo::PendingReceiver<mojom::ChildHistogramFetcherFactory> receiver) {
  mojo::MakeSelfOwnedReceiver(
      std::make_unique<ChildHistogramFetcherFactoryImpl>(),
      std::move(receiver));
}

void ChildHistogramFetcherFactoryImpl::CreateFetcher(
    base::UnsafeSharedMemoryRegion shared_memory,
    mojo::PendingReceiver<mojom::ChildHistogramFetcher> receiver) {
  // This message must be received only once.
  static bool already_called = false;
  CHECK(!already_called);
  already_called = true;

  // If the shared memory region was passed via the command line, then the
  // global histogram allocator should already by setup. Otherwise, the region
  // is being passed via this IPC. We need to initialize the global histogram
  // allocator and the tracking histograms here.
  if (shared_memory.IsValid()) {
    CHECK(!base::GlobalHistogramAllocator::Get());
    base::GlobalHistogramAllocator::CreateWithSharedMemoryRegion(shared_memory);
    // Emit a local histogram, which should not be reported to servers. This is
    // monitored from the serverside.
    LOCAL_HISTOGRAM_BOOLEAN("UMA.LocalHistogram", true);

    base::PersistentHistogramAllocator* global_allocator =
        base::GlobalHistogramAllocator::Get();
    if (global_allocator) {
      global_allocator->CreateTrackingHistograms(global_allocator->Name());
    }
  }

  // Setup the Mojo receiver.
  mojo::MakeSelfOwnedReceiver(std::make_unique<ChildHistogramFetcherImpl>(),
                              std::move(receiver));
}

ChildHistogramFetcherImpl::ChildHistogramFetcherImpl() = default;

ChildHistogramFetcherImpl::~ChildHistogramFetcherImpl() = default;

// Extract snapshot data and then send it off to the Browser process.
// Send only a delta to what we have already sent.
void ChildHistogramFetcherImpl::GetChildNonPersistentHistogramData(
    GetChildNonPersistentHistogramDataCallback callback) {
  // If a persistent allocator is in use, it needs to occasionally update
  // some internal histograms. An upload is happening so this is a good time.
  base::PersistentHistogramAllocator* global_allocator =
      base::GlobalHistogramAllocator::Get();
  if (global_allocator) {
    global_allocator->UpdateTrackingHistograms();
  }

  if (!histogram_delta_serialization_) {
    histogram_delta_serialization_ =
        std::make_unique<base::HistogramDeltaSerialization>("ChildProcess");
  }

  std::vector<std::string> deltas;
  // "false" to PrepareAndSerializeDeltas() indicates to *not* include
  // histograms held in persistent storage on the assumption that they will be
  // visible to the recipient through other means.
  histogram_delta_serialization_->PrepareAndSerializeDeltas(&deltas, false);

  std::move(callback).Run(deltas);

#ifndef NDEBUG
  static int count = 0;
  count++;
  LOCAL_HISTOGRAM_COUNTS("Histogram.ChildProcessHistogramSentCount", count);
#endif
}

void ChildHistogramFetcherImpl::Ping(mojom::UmaPingCallSource call_source,
                                     PingCallback callback) {
  // Since the ChildHistogramFetcherImpl instance was created after setting up
  // the shared memory (if there was one -- see CreateFetcher()), this histogram
  // will live in it (i.e., it should have the |kIsPersistent| flag).
  const char* histogram_name = nullptr;
  switch (call_source) {
    case mojom::UmaPingCallSource::PERIODIC:
      histogram_name = "UMA.ChildProcess.Ping.Periodic";
      break;
    case mojom::UmaPingCallSource::SHARED_MEMORY_SET_UP:
      histogram_name = "UMA.ChildProcess.Ping.SharedMemorySetUp";
      break;
  }
  base::UmaHistogramEnumeration(histogram_name,
                                mojom::UmaChildPingStatus::CHILD_RECEIVED_IPC);

  std::move(callback).Run();
}

}  // namespace metrics