xref: /aosp_15_r20/external/cronet/base/metrics/histogram_base.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 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_BASE_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_METRICS_HISTOGRAM_BASE_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <limits.h>
9*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
10*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
11*6777b538SAndroid Build Coastguard Worker 
12*6777b538SAndroid Build Coastguard Worker #include <atomic>
13*6777b538SAndroid Build Coastguard Worker #include <memory>
14*6777b538SAndroid Build Coastguard Worker #include <string>
15*6777b538SAndroid Build Coastguard Worker #include <string_view>
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker #include "base/atomicops.h"
18*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
19*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
20*6777b538SAndroid Build Coastguard Worker #include "base/values.h"
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker namespace base {
23*6777b538SAndroid Build Coastguard Worker 
24*6777b538SAndroid Build Coastguard Worker class Value;
25*6777b538SAndroid Build Coastguard Worker class HistogramBase;
26*6777b538SAndroid Build Coastguard Worker class HistogramSamples;
27*6777b538SAndroid Build Coastguard Worker class Pickle;
28*6777b538SAndroid Build Coastguard Worker class PickleIterator;
29*6777b538SAndroid Build Coastguard Worker 
30*6777b538SAndroid Build Coastguard Worker ////////////////////////////////////////////////////////////////////////////////
31*6777b538SAndroid Build Coastguard Worker // This enum is used to facilitate deserialization of histograms from other
32*6777b538SAndroid Build Coastguard Worker // processes into the browser. If you create another class that inherits from
33*6777b538SAndroid Build Coastguard Worker // HistogramBase, add new histogram types and names below.
34*6777b538SAndroid Build Coastguard Worker 
35*6777b538SAndroid Build Coastguard Worker enum HistogramType {
36*6777b538SAndroid Build Coastguard Worker   HISTOGRAM,
37*6777b538SAndroid Build Coastguard Worker   LINEAR_HISTOGRAM,
38*6777b538SAndroid Build Coastguard Worker   BOOLEAN_HISTOGRAM,
39*6777b538SAndroid Build Coastguard Worker   CUSTOM_HISTOGRAM,
40*6777b538SAndroid Build Coastguard Worker   SPARSE_HISTOGRAM,
41*6777b538SAndroid Build Coastguard Worker   DUMMY_HISTOGRAM,
42*6777b538SAndroid Build Coastguard Worker };
43*6777b538SAndroid Build Coastguard Worker 
44*6777b538SAndroid Build Coastguard Worker // Controls the verbosity of the information when the histogram is serialized to
45*6777b538SAndroid Build Coastguard Worker // a JSON.
46*6777b538SAndroid Build Coastguard Worker // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base.metrics
47*6777b538SAndroid Build Coastguard Worker enum JSONVerbosityLevel {
48*6777b538SAndroid Build Coastguard Worker   // The histogram is completely serialized.
49*6777b538SAndroid Build Coastguard Worker   JSON_VERBOSITY_LEVEL_FULL,
50*6777b538SAndroid Build Coastguard Worker   // The bucket information is not serialized.
51*6777b538SAndroid Build Coastguard Worker   JSON_VERBOSITY_LEVEL_OMIT_BUCKETS,
52*6777b538SAndroid Build Coastguard Worker };
53*6777b538SAndroid Build Coastguard Worker 
54*6777b538SAndroid Build Coastguard Worker std::string HistogramTypeToString(HistogramType type);
55*6777b538SAndroid Build Coastguard Worker 
56*6777b538SAndroid Build Coastguard Worker // This enum is used for reporting how many histograms and of what types and
57*6777b538SAndroid Build Coastguard Worker // variations are being created. It has to be in the main .h file so it is
58*6777b538SAndroid Build Coastguard Worker // visible to files that define the various histogram types.
59*6777b538SAndroid Build Coastguard Worker enum HistogramReport {
60*6777b538SAndroid Build Coastguard Worker   // Count the number of reports created. The other counts divided by this
61*6777b538SAndroid Build Coastguard Worker   // number will give the average per run of the program.
62*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_CREATED = 0,
63*6777b538SAndroid Build Coastguard Worker 
64*6777b538SAndroid Build Coastguard Worker   // Count the total number of histograms created. It is the limit against
65*6777b538SAndroid Build Coastguard Worker   // which all others are compared.
66*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_HISTOGRAM_CREATED = 1,
67*6777b538SAndroid Build Coastguard Worker 
68*6777b538SAndroid Build Coastguard Worker   // Count the total number of histograms looked-up. It's better to cache
69*6777b538SAndroid Build Coastguard Worker   // the result of a single lookup rather than do it repeatedly.
70*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_HISTOGRAM_LOOKUP = 2,
71*6777b538SAndroid Build Coastguard Worker 
72*6777b538SAndroid Build Coastguard Worker   // These count the individual histogram types. This must follow the order
73*6777b538SAndroid Build Coastguard Worker   // of HistogramType above.
74*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_TYPE_LOGARITHMIC = 3,
75*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_TYPE_LINEAR = 4,
76*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_TYPE_BOOLEAN = 5,
77*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_TYPE_CUSTOM = 6,
78*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_TYPE_SPARSE = 7,
79*6777b538SAndroid Build Coastguard Worker 
80*6777b538SAndroid Build Coastguard Worker   // These indicate the individual flags that were set.
81*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_FLAG_UMA_TARGETED = 8,
82*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_FLAG_UMA_STABILITY = 9,
83*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_FLAG_PERSISTENT = 10,
84*6777b538SAndroid Build Coastguard Worker 
85*6777b538SAndroid Build Coastguard Worker   // This must be last.
86*6777b538SAndroid Build Coastguard Worker   HISTOGRAM_REPORT_MAX = 11
87*6777b538SAndroid Build Coastguard Worker };
88*6777b538SAndroid Build Coastguard Worker 
89*6777b538SAndroid Build Coastguard Worker // Create or find existing histogram that matches the pickled info.
90*6777b538SAndroid Build Coastguard Worker // Returns NULL if the pickled data has problems.
91*6777b538SAndroid Build Coastguard Worker BASE_EXPORT HistogramBase* DeserializeHistogramInfo(base::PickleIterator* iter);
92*6777b538SAndroid Build Coastguard Worker 
93*6777b538SAndroid Build Coastguard Worker ////////////////////////////////////////////////////////////////////////////////
94*6777b538SAndroid Build Coastguard Worker 
95*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT HistogramBase {
96*6777b538SAndroid Build Coastguard Worker  public:
97*6777b538SAndroid Build Coastguard Worker   typedef int32_t Sample;                // Used for samples.
98*6777b538SAndroid Build Coastguard Worker   typedef subtle::Atomic32 AtomicCount;  // Used to count samples.
99*6777b538SAndroid Build Coastguard Worker   typedef int32_t Count;  // Used to manipulate counts in temporaries.
100*6777b538SAndroid Build Coastguard Worker 
101*6777b538SAndroid Build Coastguard Worker   static const Sample kSampleType_MAX;  // INT_MAX
102*6777b538SAndroid Build Coastguard Worker 
103*6777b538SAndroid Build Coastguard Worker   enum Flags {
104*6777b538SAndroid Build Coastguard Worker     kNoFlags = 0x0,
105*6777b538SAndroid Build Coastguard Worker 
106*6777b538SAndroid Build Coastguard Worker     // Histogram should be UMA uploaded.
107*6777b538SAndroid Build Coastguard Worker     kUmaTargetedHistogramFlag = 0x1,
108*6777b538SAndroid Build Coastguard Worker 
109*6777b538SAndroid Build Coastguard Worker     // Indicates that this is a stability histogram. This flag exists to specify
110*6777b538SAndroid Build Coastguard Worker     // which histograms should be included in the initial stability log. Please
111*6777b538SAndroid Build Coastguard Worker     // refer to |MetricsService::PrepareInitialStabilityLog|.
112*6777b538SAndroid Build Coastguard Worker     kUmaStabilityHistogramFlag = kUmaTargetedHistogramFlag | 0x2,
113*6777b538SAndroid Build Coastguard Worker 
114*6777b538SAndroid Build Coastguard Worker     // Indicates that the histogram was pickled to be sent across an IPC
115*6777b538SAndroid Build Coastguard Worker     // Channel. If we observe this flag on a histogram being aggregated into
116*6777b538SAndroid Build Coastguard Worker     // after IPC, then we are running in a single process mode, and the
117*6777b538SAndroid Build Coastguard Worker     // aggregation should not take place (as we would be aggregating back into
118*6777b538SAndroid Build Coastguard Worker     // the source histogram!).
119*6777b538SAndroid Build Coastguard Worker     kIPCSerializationSourceFlag = 0x10,
120*6777b538SAndroid Build Coastguard Worker 
121*6777b538SAndroid Build Coastguard Worker     // Indicates that a callback exists for when a new sample is recorded on
122*6777b538SAndroid Build Coastguard Worker     // this histogram. We store this as a flag with the histogram since
123*6777b538SAndroid Build Coastguard Worker     // histograms can be in performance critical code, and this allows us
124*6777b538SAndroid Build Coastguard Worker     // to shortcut looking up the callback if it doesn't exist.
125*6777b538SAndroid Build Coastguard Worker     kCallbackExists = 0x20,
126*6777b538SAndroid Build Coastguard Worker 
127*6777b538SAndroid Build Coastguard Worker     // Indicates that the histogram is held in "persistent" memory and may
128*6777b538SAndroid Build Coastguard Worker     // be accessible between processes. This is only possible if such a
129*6777b538SAndroid Build Coastguard Worker     // memory segment has been created/attached, used to create a Persistent-
130*6777b538SAndroid Build Coastguard Worker     // MemoryAllocator, and that loaded into the Histogram module before this
131*6777b538SAndroid Build Coastguard Worker     // histogram is created.
132*6777b538SAndroid Build Coastguard Worker     kIsPersistent = 0x40,
133*6777b538SAndroid Build Coastguard Worker   };
134*6777b538SAndroid Build Coastguard Worker 
135*6777b538SAndroid Build Coastguard Worker   // Histogram data inconsistency types.
136*6777b538SAndroid Build Coastguard Worker   enum Inconsistency : uint32_t {
137*6777b538SAndroid Build Coastguard Worker     NO_INCONSISTENCIES = 0x0,
138*6777b538SAndroid Build Coastguard Worker     RANGE_CHECKSUM_ERROR = 0x1,
139*6777b538SAndroid Build Coastguard Worker     BUCKET_ORDER_ERROR = 0x2,
140*6777b538SAndroid Build Coastguard Worker     COUNT_HIGH_ERROR = 0x4,
141*6777b538SAndroid Build Coastguard Worker     COUNT_LOW_ERROR = 0x8,
142*6777b538SAndroid Build Coastguard Worker 
143*6777b538SAndroid Build Coastguard Worker     NEVER_EXCEEDED_VALUE = 0x10,
144*6777b538SAndroid Build Coastguard Worker   };
145*6777b538SAndroid Build Coastguard Worker 
146*6777b538SAndroid Build Coastguard Worker   // Construct the base histogram. The name is not copied; it's up to the
147*6777b538SAndroid Build Coastguard Worker   // caller to ensure that it lives at least as long as this object.
148*6777b538SAndroid Build Coastguard Worker   explicit HistogramBase(const char* name);
149*6777b538SAndroid Build Coastguard Worker 
150*6777b538SAndroid Build Coastguard Worker   HistogramBase(const HistogramBase&) = delete;
151*6777b538SAndroid Build Coastguard Worker   HistogramBase& operator=(const HistogramBase&) = delete;
152*6777b538SAndroid Build Coastguard Worker 
153*6777b538SAndroid Build Coastguard Worker   virtual ~HistogramBase();
154*6777b538SAndroid Build Coastguard Worker 
histogram_name()155*6777b538SAndroid Build Coastguard Worker   const char* histogram_name() const { return histogram_name_; }
156*6777b538SAndroid Build Coastguard Worker 
157*6777b538SAndroid Build Coastguard Worker   // Compares |name| to the histogram name and triggers a DCHECK if they do not
158*6777b538SAndroid Build Coastguard Worker   // match. This is a helper function used by histogram macros, which results in
159*6777b538SAndroid Build Coastguard Worker   // in more compact machine code being generated by the macros.
160*6777b538SAndroid Build Coastguard Worker   virtual void CheckName(std::string_view name) const;
161*6777b538SAndroid Build Coastguard Worker 
162*6777b538SAndroid Build Coastguard Worker   // Get a unique ID for this histogram's samples.
163*6777b538SAndroid Build Coastguard Worker   virtual uint64_t name_hash() const = 0;
164*6777b538SAndroid Build Coastguard Worker 
165*6777b538SAndroid Build Coastguard Worker   // Operations with Flags enum.
flags()166*6777b538SAndroid Build Coastguard Worker   int32_t flags() const { return flags_.load(std::memory_order_relaxed); }
167*6777b538SAndroid Build Coastguard Worker   void SetFlags(int32_t flags);
168*6777b538SAndroid Build Coastguard Worker   void ClearFlags(int32_t flags);
169*6777b538SAndroid Build Coastguard Worker   bool HasFlags(int32_t flags) const;
170*6777b538SAndroid Build Coastguard Worker 
171*6777b538SAndroid Build Coastguard Worker   virtual HistogramType GetHistogramType() const = 0;
172*6777b538SAndroid Build Coastguard Worker 
173*6777b538SAndroid Build Coastguard Worker   // Whether the histogram has construction arguments as parameters specified.
174*6777b538SAndroid Build Coastguard Worker   // For histograms that don't have the concept of minimum, maximum or
175*6777b538SAndroid Build Coastguard Worker   // bucket_count, this function always returns false.
176*6777b538SAndroid Build Coastguard Worker   virtual bool HasConstructionArguments(Sample expected_minimum,
177*6777b538SAndroid Build Coastguard Worker                                         Sample expected_maximum,
178*6777b538SAndroid Build Coastguard Worker                                         size_t expected_bucket_count) const = 0;
179*6777b538SAndroid Build Coastguard Worker 
180*6777b538SAndroid Build Coastguard Worker   virtual void Add(Sample value) = 0;
181*6777b538SAndroid Build Coastguard Worker 
182*6777b538SAndroid Build Coastguard Worker   // In Add function the |value| bucket is increased by one, but in some use
183*6777b538SAndroid Build Coastguard Worker   // cases we need to increase this value by an arbitrary integer. AddCount
184*6777b538SAndroid Build Coastguard Worker   // function increases the |value| bucket by |count|. |count| should be greater
185*6777b538SAndroid Build Coastguard Worker   // than or equal to 1.
186*6777b538SAndroid Build Coastguard Worker   virtual void AddCount(Sample value, int count) = 0;
187*6777b538SAndroid Build Coastguard Worker 
188*6777b538SAndroid Build Coastguard Worker   // Similar to above but divides |count| by the |scale| amount. Probabilistic
189*6777b538SAndroid Build Coastguard Worker   // rounding is used to yield a reasonably accurate total when many samples
190*6777b538SAndroid Build Coastguard Worker   // are added. Methods for common cases of scales 1000 and 1024 are included.
191*6777b538SAndroid Build Coastguard Worker   // The ScaledLinearHistogram (which can also used be for enumerations) may be
192*6777b538SAndroid Build Coastguard Worker   // a better (and faster) solution.
193*6777b538SAndroid Build Coastguard Worker   void AddScaled(Sample value, int count, int scale);
194*6777b538SAndroid Build Coastguard Worker   void AddKilo(Sample value, int count);  // scale=1000
195*6777b538SAndroid Build Coastguard Worker   void AddKiB(Sample value, int count);   // scale=1024
196*6777b538SAndroid Build Coastguard Worker 
197*6777b538SAndroid Build Coastguard Worker   // Convenient functions that call Add(Sample).
AddTime(const TimeDelta & time)198*6777b538SAndroid Build Coastguard Worker   void AddTime(const TimeDelta& time) { AddTimeMillisecondsGranularity(time); }
199*6777b538SAndroid Build Coastguard Worker   void AddTimeMillisecondsGranularity(const TimeDelta& time);
200*6777b538SAndroid Build Coastguard Worker   // Note: AddTimeMicrosecondsGranularity() drops the report if this client
201*6777b538SAndroid Build Coastguard Worker   // doesn't have a high-resolution clock.
202*6777b538SAndroid Build Coastguard Worker   void AddTimeMicrosecondsGranularity(const TimeDelta& time);
203*6777b538SAndroid Build Coastguard Worker   void AddBoolean(bool value);
204*6777b538SAndroid Build Coastguard Worker 
205*6777b538SAndroid Build Coastguard Worker   virtual void AddSamples(const HistogramSamples& samples) = 0;
206*6777b538SAndroid Build Coastguard Worker   virtual bool AddSamplesFromPickle(base::PickleIterator* iter) = 0;
207*6777b538SAndroid Build Coastguard Worker 
208*6777b538SAndroid Build Coastguard Worker   // Serialize the histogram info into |pickle|.
209*6777b538SAndroid Build Coastguard Worker   // Note: This only serializes the construction arguments of the histogram, but
210*6777b538SAndroid Build Coastguard Worker   // does not serialize the samples.
211*6777b538SAndroid Build Coastguard Worker   void SerializeInfo(base::Pickle* pickle) const;
212*6777b538SAndroid Build Coastguard Worker 
213*6777b538SAndroid Build Coastguard Worker   // Try to find out data corruption from histogram and the samples.
214*6777b538SAndroid Build Coastguard Worker   // The returned value is a combination of Inconsistency enum.
215*6777b538SAndroid Build Coastguard Worker   virtual uint32_t FindCorruption(const HistogramSamples& samples) const;
216*6777b538SAndroid Build Coastguard Worker 
217*6777b538SAndroid Build Coastguard Worker   // Snapshot the current complete set of sample data.
218*6777b538SAndroid Build Coastguard Worker   // Note that histogram data is stored per-process. The browser process
219*6777b538SAndroid Build Coastguard Worker   // periodically ingests data from subprocesses. As such, the browser
220*6777b538SAndroid Build Coastguard Worker   // process can see histogram data from any process but other processes
221*6777b538SAndroid Build Coastguard Worker   // can only see histogram data recorded in the subprocess.
222*6777b538SAndroid Build Coastguard Worker   // Moreover, the data returned here may not be up to date:
223*6777b538SAndroid Build Coastguard Worker   // - this function does not use a lock so data might not be synced
224*6777b538SAndroid Build Coastguard Worker   //   (e.g., across cpu caches)
225*6777b538SAndroid Build Coastguard Worker   // - in the browser process, the data from subprocesses may not have
226*6777b538SAndroid Build Coastguard Worker   //   synced data from subprocesses via MergeHistogramDeltas() recently.
227*6777b538SAndroid Build Coastguard Worker   //
228*6777b538SAndroid Build Coastguard Worker   // Override with atomic/locked snapshot if needed.
229*6777b538SAndroid Build Coastguard Worker   // NOTE: this data can overflow for long-running sessions. It should be
230*6777b538SAndroid Build Coastguard Worker   // handled with care and this method is recommended to be used only
231*6777b538SAndroid Build Coastguard Worker   // in about:histograms and test code.
232*6777b538SAndroid Build Coastguard Worker   virtual std::unique_ptr<HistogramSamples> SnapshotSamples() const = 0;
233*6777b538SAndroid Build Coastguard Worker 
234*6777b538SAndroid Build Coastguard Worker   // Returns a copy of the samples that have not yet been logged. To mark the
235*6777b538SAndroid Build Coastguard Worker   // returned samples as logged, see MarkSamplesAsLogged().
236*6777b538SAndroid Build Coastguard Worker   //
237*6777b538SAndroid Build Coastguard Worker   // See additional caveats by SnapshotSamples().
238*6777b538SAndroid Build Coastguard Worker   //
239*6777b538SAndroid Build Coastguard Worker   // WARNING: This may be called from a background thread by the metrics
240*6777b538SAndroid Build Coastguard Worker   // collection system. Do not make a call to this unless it was properly vetted
241*6777b538SAndroid Build Coastguard Worker   // by someone familiar with the system.
242*6777b538SAndroid Build Coastguard Worker   // TODO(crbug/1052796): Consider gating this behind a PassKey, so that
243*6777b538SAndroid Build Coastguard Worker   // eventually, only StatisticsRecorder can use this.
244*6777b538SAndroid Build Coastguard Worker   virtual std::unique_ptr<HistogramSamples> SnapshotUnloggedSamples() const = 0;
245*6777b538SAndroid Build Coastguard Worker 
246*6777b538SAndroid Build Coastguard Worker   // Marks the passed |samples| as logged. More formally, the |samples| passed
247*6777b538SAndroid Build Coastguard Worker   // will not appear in the samples returned by a subsequent call to
248*6777b538SAndroid Build Coastguard Worker   // SnapshotDelta().
249*6777b538SAndroid Build Coastguard Worker   //
250*6777b538SAndroid Build Coastguard Worker   // See additional caveats by SnapshotSamples().
251*6777b538SAndroid Build Coastguard Worker   //
252*6777b538SAndroid Build Coastguard Worker   // WARNING: This may be called from a background thread by the metrics
253*6777b538SAndroid Build Coastguard Worker   // collection system. Do not make a call to this unless it was properly vetted
254*6777b538SAndroid Build Coastguard Worker   // by someone familiar with the system.
255*6777b538SAndroid Build Coastguard Worker   // TODO(crbug/1052796): Consider gating this behind a PassKey, so that
256*6777b538SAndroid Build Coastguard Worker   // eventually, only StatisticsRecorder can use this.
257*6777b538SAndroid Build Coastguard Worker   virtual void MarkSamplesAsLogged(const HistogramSamples& samples) = 0;
258*6777b538SAndroid Build Coastguard Worker 
259*6777b538SAndroid Build Coastguard Worker   // Calculate the change (delta) in histogram counts since the previous call
260*6777b538SAndroid Build Coastguard Worker   // to this method. Each successive call will return only those counts changed
261*6777b538SAndroid Build Coastguard Worker   // since the last call. Calls to MarkSamplesAsLogged() will also affect the
262*6777b538SAndroid Build Coastguard Worker   // samples returned. Logically, this function is equivalent to a call to
263*6777b538SAndroid Build Coastguard Worker   // SnapshotUnloggedSamples() followed by a call to MarkSamplesAsLogged().
264*6777b538SAndroid Build Coastguard Worker   //
265*6777b538SAndroid Build Coastguard Worker   // See additional caveats by SnapshotSamples().
266*6777b538SAndroid Build Coastguard Worker   //
267*6777b538SAndroid Build Coastguard Worker   // WARNING: This may be called from a background thread by the metrics
268*6777b538SAndroid Build Coastguard Worker   // collection system. Do not make a call to this unless it was properly vetted
269*6777b538SAndroid Build Coastguard Worker   // by someone familiar with the system.
270*6777b538SAndroid Build Coastguard Worker   virtual std::unique_ptr<HistogramSamples> SnapshotDelta() = 0;
271*6777b538SAndroid Build Coastguard Worker 
272*6777b538SAndroid Build Coastguard Worker   // Calculate the change (delta) in histogram counts since the previous call
273*6777b538SAndroid Build Coastguard Worker   // to SnapshotDelta() but do so without modifying any internal data as to
274*6777b538SAndroid Build Coastguard Worker   // what was previous logged. After such a call, no further calls to this
275*6777b538SAndroid Build Coastguard Worker   // method or to SnapshotDelta() should be done as the result would include
276*6777b538SAndroid Build Coastguard Worker   // data previously returned. Because no internal data is changed, this call
277*6777b538SAndroid Build Coastguard Worker   // can be made on "const" histograms such as those with data held in
278*6777b538SAndroid Build Coastguard Worker   // read-only memory.
279*6777b538SAndroid Build Coastguard Worker   //
280*6777b538SAndroid Build Coastguard Worker   // See additional caveats by SnapshotSamples().
281*6777b538SAndroid Build Coastguard Worker   virtual std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const = 0;
282*6777b538SAndroid Build Coastguard Worker 
283*6777b538SAndroid Build Coastguard Worker   // The following method provides graphical histogram displays.
284*6777b538SAndroid Build Coastguard Worker   virtual void WriteAscii(std::string* output) const;
285*6777b538SAndroid Build Coastguard Worker 
286*6777b538SAndroid Build Coastguard Worker   // Returns histograms data as a Dict (or an empty dict if not available),
287*6777b538SAndroid Build Coastguard Worker   // with the following format:
288*6777b538SAndroid Build Coastguard Worker   // {"header": "Name of the histogram with samples, mean, and/or flags",
289*6777b538SAndroid Build Coastguard Worker   // "body": "ASCII histogram representation"}
290*6777b538SAndroid Build Coastguard Worker   virtual base::Value::Dict ToGraphDict() const = 0;
291*6777b538SAndroid Build Coastguard Worker 
292*6777b538SAndroid Build Coastguard Worker   // Produce a JSON representation of the histogram with |verbosity_level| as
293*6777b538SAndroid Build Coastguard Worker   // the serialization verbosity. This is implemented with the help of
294*6777b538SAndroid Build Coastguard Worker   // GetParameters and GetCountAndBucketData; overwrite them to customize the
295*6777b538SAndroid Build Coastguard Worker   // output.
296*6777b538SAndroid Build Coastguard Worker   void WriteJSON(std::string* output, JSONVerbosityLevel verbosity_level) const;
297*6777b538SAndroid Build Coastguard Worker 
298*6777b538SAndroid Build Coastguard Worker  protected:
299*6777b538SAndroid Build Coastguard Worker   enum ReportActivity { HISTOGRAM_CREATED, HISTOGRAM_LOOKUP };
300*6777b538SAndroid Build Coastguard Worker 
301*6777b538SAndroid Build Coastguard Worker   struct BASE_EXPORT CountAndBucketData {
302*6777b538SAndroid Build Coastguard Worker     Count count;
303*6777b538SAndroid Build Coastguard Worker     int64_t sum;
304*6777b538SAndroid Build Coastguard Worker     Value::List buckets;
305*6777b538SAndroid Build Coastguard Worker 
306*6777b538SAndroid Build Coastguard Worker     CountAndBucketData(Count count, int64_t sum, Value::List buckets);
307*6777b538SAndroid Build Coastguard Worker     ~CountAndBucketData();
308*6777b538SAndroid Build Coastguard Worker 
309*6777b538SAndroid Build Coastguard Worker     CountAndBucketData(CountAndBucketData&& other);
310*6777b538SAndroid Build Coastguard Worker     CountAndBucketData& operator=(CountAndBucketData&& other);
311*6777b538SAndroid Build Coastguard Worker   };
312*6777b538SAndroid Build Coastguard Worker 
313*6777b538SAndroid Build Coastguard Worker   // Subclasses should implement this function to make SerializeInfo work.
314*6777b538SAndroid Build Coastguard Worker   virtual void SerializeInfoImpl(base::Pickle* pickle) const = 0;
315*6777b538SAndroid Build Coastguard Worker 
316*6777b538SAndroid Build Coastguard Worker   // Writes information about the construction parameters in |params|.
317*6777b538SAndroid Build Coastguard Worker   virtual Value::Dict GetParameters() const = 0;
318*6777b538SAndroid Build Coastguard Worker 
319*6777b538SAndroid Build Coastguard Worker   // Returns information about the current (non-empty) buckets and their sample
320*6777b538SAndroid Build Coastguard Worker   // counts to |buckets|, the total sample count to |count| and the total sum
321*6777b538SAndroid Build Coastguard Worker   // to |sum|.
322*6777b538SAndroid Build Coastguard Worker   CountAndBucketData GetCountAndBucketData() const;
323*6777b538SAndroid Build Coastguard Worker 
324*6777b538SAndroid Build Coastguard Worker   // Produces an actual graph (set of blank vs non blank char's) for a bucket.
325*6777b538SAndroid Build Coastguard Worker   void WriteAsciiBucketGraph(double x_count,
326*6777b538SAndroid Build Coastguard Worker                              int line_length,
327*6777b538SAndroid Build Coastguard Worker                              std::string* output) const;
328*6777b538SAndroid Build Coastguard Worker 
329*6777b538SAndroid Build Coastguard Worker   // Return a string description of what goes in a given bucket.
330*6777b538SAndroid Build Coastguard Worker   const std::string GetSimpleAsciiBucketRange(Sample sample) const;
331*6777b538SAndroid Build Coastguard Worker 
332*6777b538SAndroid Build Coastguard Worker   // Write textual description of the bucket contents (relative to histogram).
333*6777b538SAndroid Build Coastguard Worker   // Output is the count in the buckets, as well as the percentage.
334*6777b538SAndroid Build Coastguard Worker   void WriteAsciiBucketValue(Count current,
335*6777b538SAndroid Build Coastguard Worker                              double scaled_sum,
336*6777b538SAndroid Build Coastguard Worker                              std::string* output) const;
337*6777b538SAndroid Build Coastguard Worker 
338*6777b538SAndroid Build Coastguard Worker   // Retrieves the registered callbacks for this histogram, if any, and runs
339*6777b538SAndroid Build Coastguard Worker   // them passing |sample| as the parameter.
340*6777b538SAndroid Build Coastguard Worker   void FindAndRunCallbacks(Sample sample) const;
341*6777b538SAndroid Build Coastguard Worker 
342*6777b538SAndroid Build Coastguard Worker   // Gets a permanent string that can be used for histogram objects when the
343*6777b538SAndroid Build Coastguard Worker   // original is not a code constant or held in persistent memory.
344*6777b538SAndroid Build Coastguard Worker   static const char* GetPermanentName(std::string_view name);
345*6777b538SAndroid Build Coastguard Worker 
346*6777b538SAndroid Build Coastguard Worker  private:
347*6777b538SAndroid Build Coastguard Worker   friend class HistogramBaseTest;
348*6777b538SAndroid Build Coastguard Worker 
349*6777b538SAndroid Build Coastguard Worker   // A pointer to permanent storage where the histogram name is held. This can
350*6777b538SAndroid Build Coastguard Worker   // be code space or the output of GetPermanentName() or any other storage
351*6777b538SAndroid Build Coastguard Worker   // that is known to never change. This is not std::string_view because (a)
352*6777b538SAndroid Build Coastguard Worker   // char* is 1/2 the size and (b) std::string_view transparently casts from
353*6777b538SAndroid Build Coastguard Worker   // std::string which can easily lead to a pointer to non-permanent space. For
354*6777b538SAndroid Build Coastguard Worker   // persistent histograms, this will simply point into the persistent memory
355*6777b538SAndroid Build Coastguard Worker   // segment, thus avoiding duplication. For heap histograms, the
356*6777b538SAndroid Build Coastguard Worker   // GetPermanentName method will create the necessary copy.
357*6777b538SAndroid Build Coastguard Worker   const char* const histogram_name_;
358*6777b538SAndroid Build Coastguard Worker 
359*6777b538SAndroid Build Coastguard Worker   // Additional information about the histogram.
360*6777b538SAndroid Build Coastguard Worker   std::atomic<int32_t> flags_{0};
361*6777b538SAndroid Build Coastguard Worker };
362*6777b538SAndroid Build Coastguard Worker 
363*6777b538SAndroid Build Coastguard Worker }  // namespace base
364*6777b538SAndroid Build Coastguard Worker 
365*6777b538SAndroid Build Coastguard Worker #endif  // BASE_METRICS_HISTOGRAM_BASE_H_
366