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