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