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.
5 #include "base/metrics/histogram_delta_serialization.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram_base.h"
9 #include "base/metrics/histogram_snapshot_manager.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/pickle.h"
12 #include "base/values.h"
18 // Create or find existing histogram and add the samples from pickle.
19 // Silently returns when seeing any data problem in the pickle.
20 void DeserializeHistogramAndAddSamples(PickleIterator
* iter
) {
21 HistogramBase
* histogram
= DeserializeHistogramInfo(iter
);
25 if (histogram
->flags() & HistogramBase::kIPCSerializationSourceFlag
) {
26 DVLOG(1) << "Single process mode, histogram observed and not copied: "
27 << histogram
->histogram_name();
30 histogram
->AddSamplesFromPickle(iter
);
35 HistogramDeltaSerialization::HistogramDeltaSerialization(
36 const std::string
& caller_name
)
37 : histogram_snapshot_manager_(this),
38 serialized_deltas_(NULL
) {
39 inconsistencies_histogram_
=
40 LinearHistogram::FactoryGet(
41 "Histogram.Inconsistencies" + caller_name
, 1,
42 HistogramBase::NEVER_EXCEEDED_VALUE
,
43 HistogramBase::NEVER_EXCEEDED_VALUE
+ 1,
44 HistogramBase::kUmaTargetedHistogramFlag
);
46 inconsistencies_unique_histogram_
=
47 LinearHistogram::FactoryGet(
48 "Histogram.Inconsistencies" + caller_name
+ "Unique", 1,
49 HistogramBase::NEVER_EXCEEDED_VALUE
,
50 HistogramBase::NEVER_EXCEEDED_VALUE
+ 1,
51 HistogramBase::kUmaTargetedHistogramFlag
);
53 inconsistent_snapshot_histogram_
=
54 Histogram::FactoryGet(
55 "Histogram.InconsistentSnapshot" + caller_name
, 1, 1000000, 50,
56 HistogramBase::kUmaTargetedHistogramFlag
);
59 HistogramDeltaSerialization::~HistogramDeltaSerialization() {
62 void HistogramDeltaSerialization::PrepareAndSerializeDeltas(
63 std::vector
<std::string
>* serialized_deltas
) {
64 serialized_deltas_
= serialized_deltas
;
65 // Note: Before serializing, we set the kIPCSerializationSourceFlag for all
66 // the histograms, so that the receiving process can distinguish them from the
68 histogram_snapshot_manager_
.PrepareDeltas(
69 Histogram::kIPCSerializationSourceFlag
, Histogram::kNoFlags
);
70 serialized_deltas_
= NULL
;
74 void HistogramDeltaSerialization::DeserializeAndAddSamples(
75 const std::vector
<std::string
>& serialized_deltas
) {
76 for (std::vector
<std::string
>::const_iterator it
= serialized_deltas
.begin();
77 it
!= serialized_deltas
.end(); ++it
) {
78 Pickle
pickle(it
->data(), checked_cast
<int>(it
->size()));
79 PickleIterator
iter(pickle
);
80 DeserializeHistogramAndAddSamples(&iter
);
84 void HistogramDeltaSerialization::RecordDelta(
85 const HistogramBase
& histogram
,
86 const HistogramSamples
& snapshot
) {
87 DCHECK_NE(0, snapshot
.TotalCount());
90 histogram
.SerializeInfo(&pickle
);
91 snapshot
.Serialize(&pickle
);
92 serialized_deltas_
->push_back(
93 std::string(static_cast<const char*>(pickle
.data()), pickle
.size()));
96 void HistogramDeltaSerialization::InconsistencyDetected(
97 HistogramBase::Inconsistency problem
) {
98 inconsistencies_histogram_
->Add(problem
);
101 void HistogramDeltaSerialization::UniqueInconsistencyDetected(
102 HistogramBase::Inconsistency problem
) {
103 inconsistencies_unique_histogram_
->Add(problem
);
106 void HistogramDeltaSerialization::InconsistencyDetectedInLoggedCount(
108 inconsistent_snapshot_histogram_
->Add(std::abs(amount
));