Chromium Code Reviews| Index: base/metrics/histogram_delta_serializer.cc |
| diff --git a/base/metrics/histogram_delta_serializer.cc b/base/metrics/histogram_delta_serializer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8be0291b5c970393be781cb6e8bb2fcdc8312a33 |
| --- /dev/null |
| +++ b/base/metrics/histogram_delta_serializer.cc |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/metrics/histogram_delta_serializer.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/metrics/histogram_base.h" |
| +#include "base/metrics/histogram_snapshot_manager.h" |
| +#include "base/pickle.h" |
| +#include "base/safe_numerics.h" |
| +#include "base/values.h" |
| + |
| +namespace base { |
| + |
| +namespace { |
| + |
| +// Create or find existing histogram and add the samples from pickle. |
| +// Silently returns when seeing any data problem in the pickle. |
| +void DeserializeHistogramAndAddSamples(PickleIterator* iter) { |
| + HistogramBase* histogram = DeserializeHistogramInfo(iter); |
| + if (!histogram) |
| + return; |
| + |
| + if (histogram->flags() & HistogramBase::kIPCSerializationSourceFlag) { |
| + DVLOG(1) << "Single process mode, histogram observed and not copied: " |
| + << histogram->histogram_name(); |
| + return; |
| + } |
| + histogram->AddSamplesFromPickle(iter); |
| +} |
| + |
| +} |
| + |
| +HistogramDeltasSerializer::HistogramDeltasSerializer( |
| + const std::string& caller_name) |
| + : histogram_snapshot_manager_(this), |
| + serialized_deltas_(NULL) { |
| + inconsistencies_histogram_ = |
| + LinearHistogram::FactoryGet( |
| + "Histogram.Inconsistencies" + caller_name, 1, |
| + HistogramBase::NEVER_EXCEEDED_VALUE, |
| + HistogramBase::NEVER_EXCEEDED_VALUE + 1, |
| + HistogramBase::kUmaTargetedHistogramFlag); |
| + |
| + inconsistencies_unique_histogram_ = |
| + LinearHistogram::FactoryGet( |
| + "Histogram.Inconsistencies" + caller_name + "Unique", 1, |
| + HistogramBase::NEVER_EXCEEDED_VALUE, |
| + HistogramBase::NEVER_EXCEEDED_VALUE + 1, |
| + HistogramBase::kUmaTargetedHistogramFlag); |
| + |
| + inconsistent_snapshot_histogram_ = |
| + Histogram::FactoryGet( |
| + "Histogram.InconsistentSnapshot" + caller_name, 1, 1000000, 50, |
| + HistogramBase::kUmaTargetedHistogramFlag); |
| +} |
| + |
| +HistogramDeltasSerializer::~HistogramDeltasSerializer() { |
| +} |
| + |
| +void HistogramDeltasSerializer::PrepareAndSerializeDeltas( |
| + std::vector<std::string>* serialized_deltas) { |
| + serialized_deltas_ = serialized_deltas; |
| + // Note: Before serializing, we set the kIPCSerializationSourceFlag for all |
| + // the histograms, so that the receiving process can distinguish them from the |
| + // local histograms. |
| + histogram_snapshot_manager_.PrepareDeltas( |
| + Histogram::kIPCSerializationSourceFlag, false); |
| + serialized_deltas_ = NULL; |
| +} |
| + |
| +// static |
| +void HistogramDeltasSerializer::DeserializeAndAddSamples( |
| + const std::vector<std::string>& deltas) { |
| + for (std::vector<std::string>::const_iterator it = deltas.begin(); |
| + it != deltas.end(); ++it) { |
| + Pickle pickle(it->data(), checked_numeric_cast<int>(it->size())); |
| + PickleIterator iter(pickle); |
| + DeserializeHistogramAndAddSamples(&iter); |
| + } |
| +} |
| + |
| +void HistogramDeltasSerializer::RecordDelta(const HistogramBase& histogram, |
| + const HistogramSamples& snapshot) { |
| + DCHECK_NE(0, snapshot.TotalCount()); |
| + |
| + Pickle pickle; |
| + histogram.SerializeInfo(&pickle); |
| + snapshot.Serialize(&pickle); |
| + serialized_deltas_->push_back( |
| + std::string(static_cast<const char*>(pickle.data()), pickle.size())); |
| + } |
|
Ilya Sherman
2013/10/19 00:27:44
nit: De-indent this line.
Vitaly Buka (NO REVIEWS)
2013/10/21 19:13:54
Done.
|
| + |
| +void HistogramDeltasSerializer::InconsistencyDetected( |
| + HistogramBase::Inconsistency problem) { |
| + inconsistencies_histogram_->Add(problem); |
| +} |
| + |
| +void HistogramDeltasSerializer::UniqueInconsistencyDetected( |
| + HistogramBase::Inconsistency problem) { |
| + inconsistencies_unique_histogram_->Add(problem); |
| +} |
| + |
| +void HistogramDeltasSerializer::InconsistencyDetectedInLoggedCount(int amount) { |
| + inconsistent_snapshot_histogram_->Add(std::abs(amount)); |
| +} |
| + |
| +} // namespace base |