blob: 4248bd84a4c3136f6f2a1ee60483fa54e14ce2e9 [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
[email protected]c884117b2012-07-19 05:31:498#include <string>
[email protected]6afa90f2013-10-23 01:16:049#include <vector>
[email protected]c884117b2012-07-19 05:31:4910
[email protected]99053452013-09-04 13:37:4111#include "base/atomicops.h"
[email protected]c884117b2012-07-19 05:31:4912#include "base/base_export.h"
[email protected]7c7a42752012-08-09 05:14:1513#include "base/basictypes.h"
[email protected]877ef562012-10-20 02:56:1814#include "base/memory/scoped_ptr.h"
[email protected]99084f62013-06-28 00:49:0715#include "base/time/time.h"
[email protected]c884117b2012-07-19 05:31:4916
[email protected]c50c21d2013-01-11 21:52:4417class Pickle;
18class PickleIterator;
19
[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;
26
[email protected]07c02402012-10-31 06:20:2527////////////////////////////////////////////////////////////////////////////////
28// These enums are used to facilitate deserialization of histograms from other
29// processes into the browser. If you create another class that inherits from
30// HistogramBase, add new histogram types and names below.
31
32enum BASE_EXPORT HistogramType {
33 HISTOGRAM,
34 LINEAR_HISTOGRAM,
35 BOOLEAN_HISTOGRAM,
36 CUSTOM_HISTOGRAM,
37 SPARSE_HISTOGRAM,
38};
39
40std::string HistogramTypeToString(HistogramType type);
41
[email protected]c50c21d2013-01-11 21:52:4442// Create or find existing histogram that matches the pickled info.
43// Returns NULL if the pickled data has problems.
44BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
45 PickleIterator* iter);
46
[email protected]07c02402012-10-31 06:20:2547////////////////////////////////////////////////////////////////////////////////
[email protected]877ef562012-10-20 02:56:1848
[email protected]c884117b2012-07-19 05:31:4949class BASE_EXPORT HistogramBase {
50 public:
[email protected]99053452013-09-04 13:37:4151 typedef int Sample; // Used for samples.
52 typedef subtle::Atomic32 Count; // Used to count samples.
[email protected]c884117b2012-07-19 05:31:4953
[email protected]34d062322012-08-01 21:34:0854 static const Sample kSampleType_MAX; // INT_MAX
[email protected]c884117b2012-07-19 05:31:4955
[email protected]7c7a42752012-08-09 05:14:1556 enum Flags {
57 kNoFlags = 0,
58 kUmaTargetedHistogramFlag = 0x1, // Histogram should be UMA uploaded.
59
60 // Indicate that the histogram was pickled to be sent across an IPC Channel.
61 // If we observe this flag on a histogram being aggregated into after IPC,
62 // then we are running in a single process mode, and the aggregation should
63 // not take place (as we would be aggregating back into the source
64 // histogram!).
65 kIPCSerializationSourceFlag = 0x10,
66
67 // Only for Histogram and its sub classes: fancy bucket-naming support.
68 kHexRangePrintingFlag = 0x8000,
69 };
70
[email protected]cc7dec212013-03-01 03:53:2571 // Histogram data inconsistency types.
72 enum Inconsistency {
73 NO_INCONSISTENCIES = 0x0,
74 RANGE_CHECKSUM_ERROR = 0x1,
75 BUCKET_ORDER_ERROR = 0x2,
76 COUNT_HIGH_ERROR = 0x4,
77 COUNT_LOW_ERROR = 0x8,
78
79 NEVER_EXCEEDED_VALUE = 0x10
80 };
81
[email protected]f3c697c52013-01-15 10:52:1182 explicit HistogramBase(const std::string& name);
[email protected]c884117b2012-07-19 05:31:4983 virtual ~HistogramBase();
84
85 std::string histogram_name() const { return histogram_name_; }
86
[email protected]7c7a42752012-08-09 05:14:1587 // Operations with Flags enum.
88 int32 flags() const { return flags_; }
89 void SetFlags(int32 flags);
90 void ClearFlags(int32 flags);
91
[email protected]07c02402012-10-31 06:20:2592 virtual HistogramType GetHistogramType() const = 0;
93
[email protected]abae9b022012-10-24 08:18:5294 // Whether the histogram has construction arguments as parameters specified.
[email protected]15ce3842013-06-27 14:38:4595 // For histograms that don't have the concept of minimum, maximum or
96 // bucket_count, this function always returns false.
97 virtual bool HasConstructionArguments(Sample expected_minimum,
98 Sample expected_maximum,
99 size_t expected_bucket_count) const = 0;
[email protected]abae9b022012-10-24 08:18:52100
[email protected]c884117b2012-07-19 05:31:49101 virtual void Add(Sample value) = 0;
102
[email protected]de415552013-01-23 04:12:17103 // 2 convenient functions that call Add(Sample).
104 void AddTime(const TimeDelta& time);
105 void AddBoolean(bool value);
106
[email protected]c50c21d2013-01-11 21:52:44107 virtual void AddSamples(const HistogramSamples& samples) = 0;
108 virtual bool AddSamplesFromPickle(PickleIterator* iter) = 0;
109
110 // Serialize the histogram info into |pickle|.
111 // Note: This only serializes the construction arguments of the histogram, but
112 // does not serialize the samples.
113 bool SerializeInfo(Pickle* pickle) const;
114
[email protected]cc7dec212013-03-01 03:53:25115 // Try to find out data corruption from histogram and the samples.
116 // The returned value is a combination of Inconsistency enum.
117 virtual int FindCorruption(const HistogramSamples& samples) const;
118
[email protected]abae9b022012-10-24 08:18:52119 // Snapshot the current complete set of sample data.
120 // Override with atomic/locked snapshot if needed.
[email protected]877ef562012-10-20 02:56:18121 virtual scoped_ptr<HistogramSamples> SnapshotSamples() const = 0;
122
[email protected]c884117b2012-07-19 05:31:49123 // The following methods provide graphical histogram displays.
124 virtual void WriteHTMLGraph(std::string* output) const = 0;
125 virtual void WriteAscii(std::string* output) const = 0;
126
[email protected]24a7ec5e2012-10-08 10:31:50127 // Produce a JSON representation of the histogram. This is implemented with
128 // the help of GetParameters and GetCountAndBucketData; overwrite them to
129 // customize the output.
130 void WriteJSON(std::string* output) const;
131
132protected:
[email protected]c50c21d2013-01-11 21:52:44133 // Subclasses should implement this function to make SerializeInfo work.
134 virtual bool SerializeInfoImpl(Pickle* pickle) const = 0;
135
[email protected]24a7ec5e2012-10-08 10:31:50136 // Writes information about the construction parameters in |params|.
137 virtual void GetParameters(DictionaryValue* params) const = 0;
138
139 // Writes information about the current (non-empty) buckets and their sample
[email protected]cdd98fc2013-05-10 09:32:58140 // counts to |buckets|, the total sample count to |count| and the total sum
141 // to |sum|.
[email protected]24a7ec5e2012-10-08 10:31:50142 virtual void GetCountAndBucketData(Count* count,
[email protected]cdd98fc2013-05-10 09:32:58143 int64* sum,
[email protected]24a7ec5e2012-10-08 10:31:50144 ListValue* buckets) const = 0;
[email protected]f2bb3202013-04-05 21:21:54145
146 //// Produce actual graph (set of blank vs non blank char's) for a bucket.
147 void WriteAsciiBucketGraph(double current_size,
148 double max_size,
149 std::string* output) const;
150
151 // Return a string description of what goes in a given bucket.
152 const std::string GetSimpleAsciiBucketRange(Sample sample) const;
153
154 // Write textual description of the bucket contents (relative to histogram).
155 // Output is the count in the buckets, as well as the percentage.
156 void WriteAsciiBucketValue(Count current,
157 double scaled_sum,
158 std::string* output) const;
159
[email protected]c884117b2012-07-19 05:31:49160 private:
161 const std::string histogram_name_;
[email protected]7c7a42752012-08-09 05:14:15162 int32 flags_;
163
164 DISALLOW_COPY_AND_ASSIGN(HistogramBase);
[email protected]c884117b2012-07-19 05:31:49165};
166
167} // namespace base
168
169#endif // BASE_METRICS_HISTOGRAM_BASE_H_