[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef BASE_METRICS_HISTOGRAM_BASE_H_ |
| 6 | #define BASE_METRICS_HISTOGRAM_BASE_H_ |
| 7 | |
isherman | bed49a6 | 2014-08-28 07:21:52 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 10 | #include <string> |
[email protected] | 6afa90f | 2013-10-23 01:16:04 | [diff] [blame] | 11 | #include <vector> |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 12 | |
[email protected] | 9905345 | 2013-09-04 13:37:41 | [diff] [blame] | 13 | #include "base/atomicops.h" |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 14 | #include "base/base_export.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 15 | #include "base/basictypes.h" |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5bfd0a4 | 2014-02-15 03:11:45 | [diff] [blame] | 17 | #include "base/strings/string_piece.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 18 | #include "base/time/time.h" |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 19 | |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 20 | namespace base { |
| 21 | |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 22 | class DictionaryValue; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 23 | class HistogramBase; |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 24 | class HistogramSamples; |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 25 | class ListValue; |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame^] | 26 | class Pickle; |
| 27 | class PickleIterator; |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 28 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 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. |
| 33 | |
| 34 | enum BASE_EXPORT HistogramType { |
| 35 | HISTOGRAM, |
| 36 | LINEAR_HISTOGRAM, |
| 37 | BOOLEAN_HISTOGRAM, |
| 38 | CUSTOM_HISTOGRAM, |
| 39 | SPARSE_HISTOGRAM, |
| 40 | }; |
| 41 | |
| 42 | std::string HistogramTypeToString(HistogramType type); |
| 43 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 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( |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame^] | 47 | base::PickleIterator* iter); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 48 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 49 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 50 | |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 51 | class BASE_EXPORT HistogramBase { |
| 52 | public: |
isherman | bed49a6 | 2014-08-28 07:21:52 | [diff] [blame] | 53 | typedef int32_t Sample; // Used for samples. |
[email protected] | 5bfd0a4 | 2014-02-15 03:11:45 | [diff] [blame] | 54 | typedef subtle::Atomic32 AtomicCount; // Used to count samples. |
isherman | bed49a6 | 2014-08-28 07:21:52 | [diff] [blame] | 55 | typedef int32_t Count; // Used to manipulate counts in temporaries. |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 56 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 57 | static const Sample kSampleType_MAX; // INT_MAX |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 58 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 59 | enum Flags { |
| 60 | kNoFlags = 0, |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 61 | |
[email protected] | c778687a | 2014-02-11 14:46:45 | [diff] [blame] | 62 | // 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 75 | kIPCSerializationSourceFlag = 0x10, |
| 76 | |
| 77 | // Only for Histogram and its sub classes: fancy bucket-naming support. |
| 78 | kHexRangePrintingFlag = 0x8000, |
| 79 | }; |
| 80 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 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, |
| 88 | |
| 89 | NEVER_EXCEEDED_VALUE = 0x10 |
| 90 | }; |
| 91 | |
[email protected] | f3c697c5 | 2013-01-15 10:52:11 | [diff] [blame] | 92 | explicit HistogramBase(const std::string& name); |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 93 | virtual ~HistogramBase(); |
| 94 | |
| 95 | std::string histogram_name() const { return histogram_name_; } |
| 96 | |
[email protected] | 5bfd0a4 | 2014-02-15 03:11:45 | [diff] [blame] | 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; |
| 101 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 102 | // Operations with Flags enum. |
isherman | bed49a6 | 2014-08-28 07:21:52 | [diff] [blame] | 103 | int32_t flags() const { return flags_; } |
| 104 | void SetFlags(int32_t flags); |
| 105 | void ClearFlags(int32_t flags); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 106 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 107 | virtual HistogramType GetHistogramType() const = 0; |
| 108 | |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 109 | // Whether the histogram has construction arguments as parameters specified. |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 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; |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 115 | |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 116 | virtual void Add(Sample value) = 0; |
| 117 | |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 118 | // 2 convenient functions that call Add(Sample). |
| 119 | void AddTime(const TimeDelta& time); |
| 120 | void AddBoolean(bool value); |
| 121 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 122 | virtual void AddSamples(const HistogramSamples& samples) = 0; |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame^] | 123 | virtual bool AddSamplesFromPickle(base::PickleIterator* iter) = 0; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 124 | |
| 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. |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame^] | 128 | bool SerializeInfo(base::Pickle* pickle) const; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 129 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 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; |
| 133 | |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 134 | // Snapshot the current complete set of sample data. |
| 135 | // Override with atomic/locked snapshot if needed. |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 136 | virtual scoped_ptr<HistogramSamples> SnapshotSamples() const = 0; |
| 137 | |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 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; |
| 141 | |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 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; |
| 146 | |
[email protected] | 5bfd0a4 | 2014-02-15 03:11:45 | [diff] [blame] | 147 | protected: |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 148 | // Subclasses should implement this function to make SerializeInfo work. |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame^] | 149 | virtual bool SerializeInfoImpl(base::Pickle* pickle) const = 0; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 150 | |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 151 | // 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] | cdd98fc | 2013-05-10 09:32:58 | [diff] [blame] | 155 | // counts to |buckets|, the total sample count to |count| and the total sum |
| 156 | // to |sum|. |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 157 | virtual void GetCountAndBucketData(Count* count, |
[email protected] | cdd98fc | 2013-05-10 09:32:58 | [diff] [blame] | 158 | int64* sum, |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 159 | ListValue* buckets) const = 0; |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 160 | |
| 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] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 175 | private: |
| 176 | const std::string histogram_name_; |
isherman | bed49a6 | 2014-08-28 07:21:52 | [diff] [blame] | 177 | int32_t flags_; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 178 | |
| 179 | DISALLOW_COPY_AND_ASSIGN(HistogramBase); |
[email protected] | c884117b | 2012-07-19 05:31:49 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | } // namespace base |
| 183 | |
| 184 | #endif // BASE_METRICS_HISTOGRAM_BASE_H_ |