1 // Copyright 2013 The Chromium Authors. All rights reserved.
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/logging.h"
8 #include "base/metrics/histogram_base.h"
9 #include "base/metrics/histogram_snapshot_manager.h"
10 #include "base/metrics/statistics_recorder.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/pickle.h"
13 #include "base/values.h"
14
15 namespace base {
16
17 namespace {
18
19 // Create or find existing histogram and add the samples from pickle.
20 // Silently returns when seeing any data problem in the pickle.
DeserializeHistogramAndAddSamples(PickleIterator * iter)21 void DeserializeHistogramAndAddSamples(PickleIterator* iter) {
22 HistogramBase* histogram = DeserializeHistogramInfo(iter);
23 if (!histogram)
24 return;
25
26 if (histogram->flags() & HistogramBase::kIPCSerializationSourceFlag) {
27 DVLOG(1) << "Single process mode, histogram observed and not copied: "
28 << histogram->histogram_name();
29 return;
30 }
31 histogram->AddSamplesFromPickle(iter);
32 }
33
34 } // namespace
35
HistogramDeltaSerialization(const std::string & caller_name)36 HistogramDeltaSerialization::HistogramDeltaSerialization(
37 const std::string& caller_name)
38 : histogram_snapshot_manager_(this), serialized_deltas_(nullptr) {}
39
40 HistogramDeltaSerialization::~HistogramDeltaSerialization() = default;
41
PrepareAndSerializeDeltas(std::vector<std::string> * serialized_deltas,bool include_persistent)42 void HistogramDeltaSerialization::PrepareAndSerializeDeltas(
43 std::vector<std::string>* serialized_deltas,
44 bool include_persistent) {
45 DCHECK(thread_checker_.CalledOnValidThread());
46
47 serialized_deltas_ = serialized_deltas;
48 // Note: Before serializing, we set the kIPCSerializationSourceFlag for all
49 // the histograms, so that the receiving process can distinguish them from the
50 // local histograms.
51 StatisticsRecorder::PrepareDeltas(
52 include_persistent, Histogram::kIPCSerializationSourceFlag,
53 Histogram::kNoFlags, &histogram_snapshot_manager_);
54 serialized_deltas_ = nullptr;
55 }
56
57 // static
DeserializeAndAddSamples(const std::vector<std::string> & serialized_deltas)58 void HistogramDeltaSerialization::DeserializeAndAddSamples(
59 const std::vector<std::string>& serialized_deltas) {
60 for (std::vector<std::string>::const_iterator it = serialized_deltas.begin();
61 it != serialized_deltas.end(); ++it) {
62 Pickle pickle(it->data(), checked_cast<int>(it->size()));
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_->push_back(
78 std::string(static_cast<const char*>(pickle.data()), pickle.size()));
79 }
80
81 } // namespace base
82