xref: /aosp_15_r20/external/libchrome/base/metrics/histogram_delta_serialization.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_delta_serialization.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_base.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_snapshot_manager.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/metrics/statistics_recorder.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/numerics/safe_conversions.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/pickle.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/values.h"
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker namespace base {
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker // Create or find existing histogram and add the samples from pickle.
20*635a8641SAndroid Build Coastguard Worker // Silently returns when seeing any data problem in the pickle.
DeserializeHistogramAndAddSamples(PickleIterator * iter)21*635a8641SAndroid Build Coastguard Worker void DeserializeHistogramAndAddSamples(PickleIterator* iter) {
22*635a8641SAndroid Build Coastguard Worker   HistogramBase* histogram = DeserializeHistogramInfo(iter);
23*635a8641SAndroid Build Coastguard Worker   if (!histogram)
24*635a8641SAndroid Build Coastguard Worker     return;
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker   if (histogram->flags() & HistogramBase::kIPCSerializationSourceFlag) {
27*635a8641SAndroid Build Coastguard Worker     DVLOG(1) << "Single process mode, histogram observed and not copied: "
28*635a8641SAndroid Build Coastguard Worker              << histogram->histogram_name();
29*635a8641SAndroid Build Coastguard Worker     return;
30*635a8641SAndroid Build Coastguard Worker   }
31*635a8641SAndroid Build Coastguard Worker   histogram->AddSamplesFromPickle(iter);
32*635a8641SAndroid Build Coastguard Worker }
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker }  // namespace
35*635a8641SAndroid Build Coastguard Worker 
HistogramDeltaSerialization(const std::string & caller_name)36*635a8641SAndroid Build Coastguard Worker HistogramDeltaSerialization::HistogramDeltaSerialization(
37*635a8641SAndroid Build Coastguard Worker     const std::string& caller_name)
38*635a8641SAndroid Build Coastguard Worker     : histogram_snapshot_manager_(this), serialized_deltas_(nullptr) {}
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker HistogramDeltaSerialization::~HistogramDeltaSerialization() = default;
41*635a8641SAndroid Build Coastguard Worker 
PrepareAndSerializeDeltas(std::vector<std::string> * serialized_deltas,bool include_persistent)42*635a8641SAndroid Build Coastguard Worker void HistogramDeltaSerialization::PrepareAndSerializeDeltas(
43*635a8641SAndroid Build Coastguard Worker     std::vector<std::string>* serialized_deltas,
44*635a8641SAndroid Build Coastguard Worker     bool include_persistent) {
45*635a8641SAndroid Build Coastguard Worker   DCHECK(thread_checker_.CalledOnValidThread());
46*635a8641SAndroid Build Coastguard Worker 
47*635a8641SAndroid Build Coastguard Worker   serialized_deltas_ = serialized_deltas;
48*635a8641SAndroid Build Coastguard Worker   // Note: Before serializing, we set the kIPCSerializationSourceFlag for all
49*635a8641SAndroid Build Coastguard Worker   // the histograms, so that the receiving process can distinguish them from the
50*635a8641SAndroid Build Coastguard Worker   // local histograms.
51*635a8641SAndroid Build Coastguard Worker   StatisticsRecorder::PrepareDeltas(
52*635a8641SAndroid Build Coastguard Worker       include_persistent, Histogram::kIPCSerializationSourceFlag,
53*635a8641SAndroid Build Coastguard Worker       Histogram::kNoFlags, &histogram_snapshot_manager_);
54*635a8641SAndroid Build Coastguard Worker   serialized_deltas_ = nullptr;
55*635a8641SAndroid Build Coastguard Worker }
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker // static
DeserializeAndAddSamples(const std::vector<std::string> & serialized_deltas)58*635a8641SAndroid Build Coastguard Worker void HistogramDeltaSerialization::DeserializeAndAddSamples(
59*635a8641SAndroid Build Coastguard Worker     const std::vector<std::string>& serialized_deltas) {
60*635a8641SAndroid Build Coastguard Worker   for (std::vector<std::string>::const_iterator it = serialized_deltas.begin();
61*635a8641SAndroid Build Coastguard Worker        it != serialized_deltas.end(); ++it) {
62*635a8641SAndroid Build Coastguard Worker     Pickle pickle(it->data(), checked_cast<int>(it->size()));
63*635a8641SAndroid Build Coastguard Worker     PickleIterator iter(pickle);
64*635a8641SAndroid Build Coastguard Worker     DeserializeHistogramAndAddSamples(&iter);
65*635a8641SAndroid Build Coastguard Worker   }
66*635a8641SAndroid Build Coastguard Worker }
67*635a8641SAndroid Build Coastguard Worker 
RecordDelta(const HistogramBase & histogram,const HistogramSamples & snapshot)68*635a8641SAndroid Build Coastguard Worker void HistogramDeltaSerialization::RecordDelta(
69*635a8641SAndroid Build Coastguard Worker     const HistogramBase& histogram,
70*635a8641SAndroid Build Coastguard Worker     const HistogramSamples& snapshot) {
71*635a8641SAndroid Build Coastguard Worker   DCHECK(thread_checker_.CalledOnValidThread());
72*635a8641SAndroid Build Coastguard Worker   DCHECK_NE(0, snapshot.TotalCount());
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker   Pickle pickle;
75*635a8641SAndroid Build Coastguard Worker   histogram.SerializeInfo(&pickle);
76*635a8641SAndroid Build Coastguard Worker   snapshot.Serialize(&pickle);
77*635a8641SAndroid Build Coastguard Worker   serialized_deltas_->push_back(
78*635a8641SAndroid Build Coastguard Worker       std::string(static_cast<const char*>(pickle.data()), pickle.size()));
79*635a8641SAndroid Build Coastguard Worker }
80*635a8641SAndroid Build Coastguard Worker 
81*635a8641SAndroid Build Coastguard Worker }  // namespace base
82