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