xref: /aosp_15_r20/external/libchrome/base/metrics/sparse_histogram.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 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_SPARSE_HISTOGRAM_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_METRICS_SPARSE_HISTOGRAM_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <map>
12*635a8641SAndroid Build Coastguard Worker #include <memory>
13*635a8641SAndroid Build Coastguard Worker #include <string>
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_base.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_samples.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/lock.h"
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker namespace base {
22*635a8641SAndroid Build Coastguard Worker 
23*635a8641SAndroid Build Coastguard Worker class HistogramSamples;
24*635a8641SAndroid Build Coastguard Worker class PersistentHistogramAllocator;
25*635a8641SAndroid Build Coastguard Worker class Pickle;
26*635a8641SAndroid Build Coastguard Worker class PickleIterator;
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT SparseHistogram : public HistogramBase {
29*635a8641SAndroid Build Coastguard Worker  public:
30*635a8641SAndroid Build Coastguard Worker   // If there's one with same name, return the existing one. If not, create a
31*635a8641SAndroid Build Coastguard Worker   // new one.
32*635a8641SAndroid Build Coastguard Worker   static HistogramBase* FactoryGet(const std::string& name, int32_t flags);
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker   // Create a histogram using data in persistent storage. The allocator must
35*635a8641SAndroid Build Coastguard Worker   // live longer than the created sparse histogram.
36*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<HistogramBase> PersistentCreate(
37*635a8641SAndroid Build Coastguard Worker       PersistentHistogramAllocator* allocator,
38*635a8641SAndroid Build Coastguard Worker       const char* name,
39*635a8641SAndroid Build Coastguard Worker       HistogramSamples::Metadata* meta,
40*635a8641SAndroid Build Coastguard Worker       HistogramSamples::Metadata* logged_meta);
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker   ~SparseHistogram() override;
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker   // HistogramBase implementation:
45*635a8641SAndroid Build Coastguard Worker   uint64_t name_hash() const override;
46*635a8641SAndroid Build Coastguard Worker   HistogramType GetHistogramType() const override;
47*635a8641SAndroid Build Coastguard Worker   bool HasConstructionArguments(Sample expected_minimum,
48*635a8641SAndroid Build Coastguard Worker                                 Sample expected_maximum,
49*635a8641SAndroid Build Coastguard Worker                                 uint32_t expected_bucket_count) const override;
50*635a8641SAndroid Build Coastguard Worker   void Add(Sample value) override;
51*635a8641SAndroid Build Coastguard Worker   void AddCount(Sample value, int count) override;
52*635a8641SAndroid Build Coastguard Worker   void AddSamples(const HistogramSamples& samples) override;
53*635a8641SAndroid Build Coastguard Worker   bool AddSamplesFromPickle(base::PickleIterator* iter) override;
54*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<HistogramSamples> SnapshotSamples() const override;
55*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<HistogramSamples> SnapshotDelta() override;
56*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override;
57*635a8641SAndroid Build Coastguard Worker   void WriteHTMLGraph(std::string* output) const override;
58*635a8641SAndroid Build Coastguard Worker   void WriteAscii(std::string* output) const override;
59*635a8641SAndroid Build Coastguard Worker 
60*635a8641SAndroid Build Coastguard Worker  protected:
61*635a8641SAndroid Build Coastguard Worker   // HistogramBase implementation:
62*635a8641SAndroid Build Coastguard Worker   void SerializeInfoImpl(base::Pickle* pickle) const override;
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker  private:
65*635a8641SAndroid Build Coastguard Worker   // Clients should always use FactoryGet to create SparseHistogram.
66*635a8641SAndroid Build Coastguard Worker   explicit SparseHistogram(const char* name);
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker   SparseHistogram(PersistentHistogramAllocator* allocator,
69*635a8641SAndroid Build Coastguard Worker                   const char* name,
70*635a8641SAndroid Build Coastguard Worker                   HistogramSamples::Metadata* meta,
71*635a8641SAndroid Build Coastguard Worker                   HistogramSamples::Metadata* logged_meta);
72*635a8641SAndroid Build Coastguard Worker 
73*635a8641SAndroid Build Coastguard Worker   friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
74*635a8641SAndroid Build Coastguard Worker       base::PickleIterator* iter);
75*635a8641SAndroid Build Coastguard Worker   static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker   void GetParameters(DictionaryValue* params) const override;
78*635a8641SAndroid Build Coastguard Worker   void GetCountAndBucketData(Count* count,
79*635a8641SAndroid Build Coastguard Worker                              int64_t* sum,
80*635a8641SAndroid Build Coastguard Worker                              ListValue* buckets) const override;
81*635a8641SAndroid Build Coastguard Worker 
82*635a8641SAndroid Build Coastguard Worker   // Helpers for emitting Ascii graphic.  Each method appends data to output.
83*635a8641SAndroid Build Coastguard Worker   void WriteAsciiImpl(bool graph_it,
84*635a8641SAndroid Build Coastguard Worker                       const std::string& newline,
85*635a8641SAndroid Build Coastguard Worker                       std::string* output) const;
86*635a8641SAndroid Build Coastguard Worker 
87*635a8641SAndroid Build Coastguard Worker   // Write a common header message describing this histogram.
88*635a8641SAndroid Build Coastguard Worker   void WriteAsciiHeader(const Count total_count,
89*635a8641SAndroid Build Coastguard Worker                         std::string* output) const;
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker   // For constuctor calling.
92*635a8641SAndroid Build Coastguard Worker   friend class SparseHistogramTest;
93*635a8641SAndroid Build Coastguard Worker 
94*635a8641SAndroid Build Coastguard Worker   // Protects access to |samples_|.
95*635a8641SAndroid Build Coastguard Worker   mutable base::Lock lock_;
96*635a8641SAndroid Build Coastguard Worker 
97*635a8641SAndroid Build Coastguard Worker   // Flag to indicate if PrepareFinalDelta has been previously called.
98*635a8641SAndroid Build Coastguard Worker   mutable bool final_delta_created_ = false;
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<HistogramSamples> unlogged_samples_;
101*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<HistogramSamples> logged_samples_;
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(SparseHistogram);
104*635a8641SAndroid Build Coastguard Worker };
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker }  // namespace base
107*635a8641SAndroid Build Coastguard Worker 
108*635a8641SAndroid Build Coastguard Worker #endif  // BASE_METRICS_SPARSE_HISTOGRAM_H_
109