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#ifndef BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_
6#define BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_
7
8#include <string>
9#include <vector>
10
11#include "base/base_export.h"
12#include "base/basictypes.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/metrics/histogram_flattener.h"
15#include "base/metrics/histogram_snapshot_manager.h"
16
17namespace base {
18
19class HistogramBase;
20
21// Serializes and restores histograms deltas.
22class BASE_EXPORT HistogramDeltaSerialization : public HistogramFlattener {
23 public:
24  // |caller_name| is string used in histograms for counting inconsistencies.
25  explicit HistogramDeltaSerialization(const std::string& caller_name);
26  virtual ~HistogramDeltaSerialization();
27
28  // Computes deltas in histogram bucket counts relative to the previous call to
29  // this method. Stores the deltas in serialized form into |serialized_deltas|.
30  // If |serialized_deltas| is NULL, no data is serialized, though the next call
31  // will compute the deltas relative to this one.
32  void PrepareAndSerializeDeltas(std::vector<std::string>* serialized_deltas);
33
34  // Deserialize deltas and add samples to corresponding histograms, creating
35  // them if necessary. Silently ignores errors in |serialized_deltas|.
36  static void DeserializeAndAddSamples(
37      const std::vector<std::string>& serialized_deltas);
38
39 private:
40  // HistogramFlattener implementation.
41  virtual void RecordDelta(const HistogramBase& histogram,
42                           const HistogramSamples& snapshot) OVERRIDE;
43  virtual void InconsistencyDetected(
44      HistogramBase::Inconsistency problem) OVERRIDE;
45  virtual void UniqueInconsistencyDetected(
46      HistogramBase::Inconsistency problem) OVERRIDE;
47  virtual void InconsistencyDetectedInLoggedCount(int amount) OVERRIDE;
48
49  // Calculates deltas in histogram counters.
50  HistogramSnapshotManager histogram_snapshot_manager_;
51
52  // Output buffer for serialized deltas.
53  std::vector<std::string>* serialized_deltas_;
54
55  // Histograms to count inconsistencies in snapshots.
56  HistogramBase* inconsistencies_histogram_;
57  HistogramBase* inconsistencies_unique_histogram_;
58  HistogramBase* inconsistent_snapshot_histogram_;
59
60  DISALLOW_COPY_AND_ASSIGN(HistogramDeltaSerialization);
61};
62
63}  // namespace base
64
65#endif  // BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_
66