xref: /aosp_15_r20/external/libchrome/base/metrics/histogram_delta_serialization.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <memory>
9*635a8641SAndroid Build Coastguard Worker #include <string>
10*635a8641SAndroid Build Coastguard Worker #include <vector>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_flattener.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_snapshot_manager.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_checker.h"
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker namespace base {
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker class HistogramBase;
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker // Serializes and restores histograms deltas.
23*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT HistogramDeltaSerialization : public HistogramFlattener {
24*635a8641SAndroid Build Coastguard Worker  public:
25*635a8641SAndroid Build Coastguard Worker   // |caller_name| is string used in histograms for counting inconsistencies.
26*635a8641SAndroid Build Coastguard Worker   explicit HistogramDeltaSerialization(const std::string& caller_name);
27*635a8641SAndroid Build Coastguard Worker   ~HistogramDeltaSerialization() override;
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker   // Computes deltas in histogram bucket counts relative to the previous call to
30*635a8641SAndroid Build Coastguard Worker   // this method. Stores the deltas in serialized form into |serialized_deltas|.
31*635a8641SAndroid Build Coastguard Worker   // If |serialized_deltas| is null, no data is serialized, though the next call
32*635a8641SAndroid Build Coastguard Worker   // will compute the deltas relative to this one. Setting |include_persistent|
33*635a8641SAndroid Build Coastguard Worker   // will include histograms held in persistent memory (and thus may be reported
34*635a8641SAndroid Build Coastguard Worker   // elsewhere); otherwise only histograms local to this process are serialized.
35*635a8641SAndroid Build Coastguard Worker   void PrepareAndSerializeDeltas(std::vector<std::string>* serialized_deltas,
36*635a8641SAndroid Build Coastguard Worker                                  bool include_persistent);
37*635a8641SAndroid Build Coastguard Worker 
38*635a8641SAndroid Build Coastguard Worker   // Deserialize deltas and add samples to corresponding histograms, creating
39*635a8641SAndroid Build Coastguard Worker   // them if necessary. Silently ignores errors in |serialized_deltas|.
40*635a8641SAndroid Build Coastguard Worker   static void DeserializeAndAddSamples(
41*635a8641SAndroid Build Coastguard Worker       const std::vector<std::string>& serialized_deltas);
42*635a8641SAndroid Build Coastguard Worker 
43*635a8641SAndroid Build Coastguard Worker  private:
44*635a8641SAndroid Build Coastguard Worker   // HistogramFlattener implementation.
45*635a8641SAndroid Build Coastguard Worker   void RecordDelta(const HistogramBase& histogram,
46*635a8641SAndroid Build Coastguard Worker                    const HistogramSamples& snapshot) override;
47*635a8641SAndroid Build Coastguard Worker 
48*635a8641SAndroid Build Coastguard Worker   ThreadChecker thread_checker_;
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker   // Calculates deltas in histogram counters.
51*635a8641SAndroid Build Coastguard Worker   HistogramSnapshotManager histogram_snapshot_manager_;
52*635a8641SAndroid Build Coastguard Worker 
53*635a8641SAndroid Build Coastguard Worker   // Output buffer for serialized deltas.
54*635a8641SAndroid Build Coastguard Worker   std::vector<std::string>* serialized_deltas_;
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(HistogramDeltaSerialization);
57*635a8641SAndroid Build Coastguard Worker };
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker }  // namespace base
60*635a8641SAndroid Build Coastguard Worker 
61*635a8641SAndroid Build Coastguard Worker #endif  // BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_
62