Enable clang warning for ignored attributes.
[chromium-blink-merge.git] / base / metrics / histogram_base.h
blob9979d470ff52d9ccf257fbc8fa59c1185f834a6d
1 // 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.
5 #ifndef BASE_METRICS_HISTOGRAM_BASE_H_
6 #define BASE_METRICS_HISTOGRAM_BASE_H_
8 #include <stdint.h>
10 #include <string>
11 #include <vector>
13 #include "base/atomicops.h"
14 #include "base/base_export.h"
15 #include "base/basictypes.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string_piece.h"
18 #include "base/time/time.h"
20 namespace base {
22 class DictionaryValue;
23 class HistogramBase;
24 class HistogramSamples;
25 class ListValue;
26 class Pickle;
27 class PickleIterator;
29 ////////////////////////////////////////////////////////////////////////////////
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.
34 enum HistogramType {
35 HISTOGRAM,
36 LINEAR_HISTOGRAM,
37 BOOLEAN_HISTOGRAM,
38 CUSTOM_HISTOGRAM,
39 SPARSE_HISTOGRAM,
42 std::string HistogramTypeToString(HistogramType type);
44 // Create or find existing histogram that matches the pickled info.
45 // Returns NULL if the pickled data has problems.
46 BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
47 base::PickleIterator* iter);
49 ////////////////////////////////////////////////////////////////////////////////
51 class BASE_EXPORT HistogramBase {
52 public:
53 typedef int32_t Sample; // Used for samples.
54 typedef subtle::Atomic32 AtomicCount; // Used to count samples.
55 typedef int32_t Count; // Used to manipulate counts in temporaries.
57 static const Sample kSampleType_MAX; // INT_MAX
59 enum Flags {
60 kNoFlags = 0,
62 // Histogram should be UMA uploaded.
63 kUmaTargetedHistogramFlag = 0x1,
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,
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!).
75 kIPCSerializationSourceFlag = 0x10,
77 // Only for Histogram and its sub classes: fancy bucket-naming support.
78 kHexRangePrintingFlag = 0x8000,
81 // 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,
89 NEVER_EXCEEDED_VALUE = 0x10
92 explicit HistogramBase(const std::string& name);
93 virtual ~HistogramBase();
95 const std::string& histogram_name() const { return histogram_name_; }
97 // 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;
102 // Operations with Flags enum.
103 int32_t flags() const { return flags_; }
104 void SetFlags(int32_t flags);
105 void ClearFlags(int32_t flags);
107 virtual HistogramType GetHistogramType() const = 0;
109 // Whether the histogram has construction arguments as parameters specified.
110 // 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;
116 virtual void Add(Sample value) = 0;
118 // 2 convenient functions that call Add(Sample).
119 void AddTime(const TimeDelta& time);
120 void AddBoolean(bool value);
122 virtual void AddSamples(const HistogramSamples& samples) = 0;
123 virtual bool AddSamplesFromPickle(base::PickleIterator* iter) = 0;
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.
128 bool SerializeInfo(base::Pickle* pickle) const;
130 // 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;
134 // Snapshot the current complete set of sample data.
135 // Override with atomic/locked snapshot if needed.
136 virtual scoped_ptr<HistogramSamples> SnapshotSamples() const = 0;
138 // 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;
142 // 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;
147 protected:
148 // Subclasses should implement this function to make SerializeInfo work.
149 virtual bool SerializeInfoImpl(base::Pickle* pickle) const = 0;
151 // Writes information about the construction parameters in |params|.
152 virtual void GetParameters(DictionaryValue* params) const = 0;
154 // Writes information about the current (non-empty) buckets and their sample
155 // counts to |buckets|, the total sample count to |count| and the total sum
156 // to |sum|.
157 virtual void GetCountAndBucketData(Count* count,
158 int64* sum,
159 ListValue* buckets) const = 0;
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;
166 // Return a string description of what goes in a given bucket.
167 const std::string GetSimpleAsciiBucketRange(Sample sample) const;
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;
175 private:
176 const std::string histogram_name_;
177 int32_t flags_;
179 DISALLOW_COPY_AND_ASSIGN(HistogramBase);
182 } // namespace base
184 #endif // BASE_METRICS_HISTOGRAM_BASE_H_