blob: 006395b1f2939707af6c52b8df935413cdf139b7 [file] [log] [blame]
[email protected]c884117b2012-07-19 05:31:491// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_METRICS_HISTOGRAM_BASE_H_
6#define BASE_METRICS_HISTOGRAM_BASE_H_
7
ishermanbed49a62014-08-28 07:21:528#include <stdint.h>
9
[email protected]c884117b2012-07-19 05:31:4910#include <string>
[email protected]6afa90f2013-10-23 01:16:0411#include <vector>
[email protected]c884117b2012-07-19 05:31:4912
[email protected]99053452013-09-04 13:37:4113#include "base/atomicops.h"
[email protected]c884117b2012-07-19 05:31:4914#include "base/base_export.h"
[email protected]7c7a42752012-08-09 05:14:1515#include "base/basictypes.h"
[email protected]877ef562012-10-20 02:56:1816#include "base/memory/scoped_ptr.h"
[email protected]5bfd0a42014-02-15 03:11:4517#include "base/strings/string_piece.h"
[email protected]99084f62013-06-28 00:49:0718#include "base/time/time.h"
[email protected]c884117b2012-07-19 05:31:4919
[email protected]c884117b2012-07-19 05:31:4920namespace base {
21
[email protected]24a7ec5e2012-10-08 10:31:5022class DictionaryValue;
[email protected]c50c21d2013-01-11 21:52:4423class HistogramBase;
[email protected]07c02402012-10-31 06:20:2524class HistogramSamples;
[email protected]24a7ec5e2012-10-08 10:31:5025class ListValue;
brettw05cfd8ddb2015-06-02 07:02:4726class Pickle;
27class PickleIterator;
[email protected]24a7ec5e2012-10-08 10:31:5028
[email protected]07c02402012-10-31 06:20:2529////////////////////////////////////////////////////////////////////////////////
30// These enums are used to facilitate deserialization of histograms from other
31// processes into the browser. If you create another class that inherits from
32// HistogramBase, add new histogram types and names below.
33
34enum BASE_EXPORT HistogramType {
35 HISTOGRAM,
36 LINEAR_HISTOGRAM,
37 BOOLEAN_HISTOGRAM,
38 CUSTOM_HISTOGRAM,
39 SPARSE_HISTOGRAM,
40};
41
42std::string HistogramTypeToString(HistogramType type);
43
[email protected]c50c21d2013-01-11 21:52:4444// Create or find existing histogram that matches the pickled info.
45// Returns NULL if the pickled data has problems.
46BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
brettw05cfd8ddb2015-06-02 07:02:4747 base::PickleIterator* iter);
[email protected]c50c21d2013-01-11 21:52:4448
[email protected]07c02402012-10-31 06:20:2549////////////////////////////////////////////////////////////////////////////////
[email protected]877ef562012-10-20 02:56:1850
[email protected]c884117b2012-07-19 05:31:4951class BASE_EXPORT HistogramBase {
52 public:
ishermanbed49a62014-08-28 07:21:5253 typedef int32_t Sample; // Used for samples.
[email protected]5bfd0a42014-02-15 03:11:4554 typedef subtle::Atomic32 AtomicCount; // Used to count samples.
ishermanbed49a62014-08-28 07:21:5255 typedef int32_t Count; // Used to manipulate counts in temporaries.
[email protected]c884117b2012-07-19 05:31:4956
[email protected]34d062322012-08-01 21:34:0857 static const Sample kSampleType_MAX; // INT_MAX
[email protected]c884117b2012-07-19 05:31:4958
[email protected]7c7a42752012-08-09 05:14:1559 enum Flags {
60 kNoFlags = 0,
[email protected]7c7a42752012-08-09 05:14:1561
[email protected]c778687a2014-02-11 14:46:4562 // Histogram should be UMA uploaded.
63 kUmaTargetedHistogramFlag = 0x1,
64
65 // Indicates that this is a stability histogram. This flag exists to specify
66 // which histograms should be included in the initial stability log. Please
67 // refer to |MetricsService::PrepareInitialStabilityLog|.
68 kUmaStabilityHistogramFlag = kUmaTargetedHistogramFlag | 0x2,
69
70 // Indicates that the histogram was pickled to be sent across an IPC
71 // Channel. If we observe this flag on a histogram being aggregated into
72 // after IPC, then we are running in a single process mode, and the
73 // aggregation should not take place (as we would be aggregating back into
74 // the source histogram!).
[email protected]7c7a42752012-08-09 05:14:1575 kIPCSerializationSourceFlag = 0x10,
76
77 // Only for Histogram and its sub classes: fancy bucket-naming support.
78 kHexRangePrintingFlag = 0x8000,
79 };
80
[email protected]cc7dec212013-03-01 03:53:2581 // Histogram data inconsistency types.
82 enum Inconsistency {
83 NO_INCONSISTENCIES = 0x0,
84 RANGE_CHECKSUM_ERROR = 0x1,
85 BUCKET_ORDER_ERROR = 0x2,
86 COUNT_HIGH_ERROR = 0x4,
87 COUNT_LOW_ERROR = 0x8,
88
89 NEVER_EXCEEDED_VALUE = 0x10
90 };
91
[email protected]f3c697c52013-01-15 10:52:1192 explicit HistogramBase(const std::string& name);
[email protected]c884117b2012-07-19 05:31:4993 virtual ~HistogramBase();
94
95 std::string histogram_name() const { return histogram_name_; }
96
[email protected]5bfd0a42014-02-15 03:11:4597 // Comapres |name| to the histogram name and triggers a DCHECK if they do not
98 // match. This is a helper function used by histogram macros, which results in
99 // in more compact machine code being generated by the macros.
100 void CheckName(const StringPiece& name) const;
101
[email protected]7c7a42752012-08-09 05:14:15102 // Operations with Flags enum.
ishermanbed49a62014-08-28 07:21:52103 int32_t flags() const { return flags_; }
104 void SetFlags(int32_t flags);
105 void ClearFlags(int32_t flags);
[email protected]7c7a42752012-08-09 05:14:15106
[email protected]07c02402012-10-31 06:20:25107 virtual HistogramType GetHistogramType() const = 0;
108
[email protected]abae9b022012-10-24 08:18:52109 // Whether the histogram has construction arguments as parameters specified.
[email protected]15ce3842013-06-27 14:38:45110 // For histograms that don't have the concept of minimum, maximum or
111 // bucket_count, this function always returns false.
112 virtual bool HasConstructionArguments(Sample expected_minimum,
113 Sample expected_maximum,
114 size_t expected_bucket_count) const = 0;
[email protected]abae9b022012-10-24 08:18:52115
[email protected]c884117b2012-07-19 05:31:49116 virtual void Add(Sample value) = 0;
117
[email protected]de415552013-01-23 04:12:17118 // 2 convenient functions that call Add(Sample).
119 void AddTime(const TimeDelta& time);
120 void AddBoolean(bool value);
121
[email protected]c50c21d2013-01-11 21:52:44122 virtual void AddSamples(const HistogramSamples& samples) = 0;
brettw05cfd8ddb2015-06-02 07:02:47123 virtual bool AddSamplesFromPickle(base::PickleIterator* iter) = 0;
[email protected]c50c21d2013-01-11 21:52:44124
125 // Serialize the histogram info into |pickle|.
126 // Note: This only serializes the construction arguments of the histogram, but
127 // does not serialize the samples.
brettw05cfd8ddb2015-06-02 07:02:47128 bool SerializeInfo(base::Pickle* pickle) const;
[email protected]c50c21d2013-01-11 21:52:44129
[email protected]cc7dec212013-03-01 03:53:25130 // Try to find out data corruption from histogram and the samples.
131 // The returned value is a combination of Inconsistency enum.
132 virtual int FindCorruption(const HistogramSamples& samples) const;
133
[email protected]abae9b022012-10-24 08:18:52134 // Snapshot the current complete set of sample data.
135 // Override with atomic/locked snapshot if needed.
[email protected]877ef562012-10-20 02:56:18136 virtual scoped_ptr<HistogramSamples> SnapshotSamples() const = 0;
137
[email protected]c884117b2012-07-19 05:31:49138 // The following methods provide graphical histogram displays.
139 virtual void WriteHTMLGraph(std::string* output) const = 0;
140 virtual void WriteAscii(std::string* output) const = 0;
141
[email protected]24a7ec5e2012-10-08 10:31:50142 // Produce a JSON representation of the histogram. This is implemented with
143 // the help of GetParameters and GetCountAndBucketData; overwrite them to
144 // customize the output.
145 void WriteJSON(std::string* output) const;
146
[email protected]5bfd0a42014-02-15 03:11:45147 protected:
[email protected]c50c21d2013-01-11 21:52:44148 // Subclasses should implement this function to make SerializeInfo work.
brettw05cfd8ddb2015-06-02 07:02:47149 virtual bool SerializeInfoImpl(base::Pickle* pickle) const = 0;
[email protected]c50c21d2013-01-11 21:52:44150
[email protected]24a7ec5e2012-10-08 10:31:50151 // Writes information about the construction parameters in |params|.
152 virtual void GetParameters(DictionaryValue* params) const = 0;
153
154 // Writes information about the current (non-empty) buckets and their sample
[email protected]cdd98fc2013-05-10 09:32:58155 // counts to |buckets|, the total sample count to |count| and the total sum
156 // to |sum|.
[email protected]24a7ec5e2012-10-08 10:31:50157 virtual void GetCountAndBucketData(Count* count,
[email protected]cdd98fc2013-05-10 09:32:58158 int64* sum,
[email protected]24a7ec5e2012-10-08 10:31:50159 ListValue* buckets) const = 0;
[email protected]f2bb3202013-04-05 21:21:54160
161 //// Produce actual graph (set of blank vs non blank char's) for a bucket.
162 void WriteAsciiBucketGraph(double current_size,
163 double max_size,
164 std::string* output) const;
165
166 // Return a string description of what goes in a given bucket.
167 const std::string GetSimpleAsciiBucketRange(Sample sample) const;
168
169 // Write textual description of the bucket contents (relative to histogram).
170 // Output is the count in the buckets, as well as the percentage.
171 void WriteAsciiBucketValue(Count current,
172 double scaled_sum,
173 std::string* output) const;
174
[email protected]c884117b2012-07-19 05:31:49175 private:
176 const std::string histogram_name_;
ishermanbed49a62014-08-28 07:21:52177 int32_t flags_;
[email protected]7c7a42752012-08-09 05:14:15178
179 DISALLOW_COPY_AND_ASSIGN(HistogramBase);
[email protected]c884117b2012-07-19 05:31:49180};
181
182} // namespace base
183
184#endif // BASE_METRICS_HISTOGRAM_BASE_H_