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