[email protected] | a93721e2 | 2012-01-06 02:13:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | // Histogram is an object that aggregates statistics, and can summarize them in |
| 6 | // various forms, including ASCII graphical, HTML, and numerically (as a |
| 7 | // vector of numbers corresponding to each of the aggregating buckets). |
| 8 | // See header file for details and examples. |
| 9 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | |
Alexei Svitkine | e73f80a0 | 2017-07-18 00:08:39 | [diff] [blame] | 12 | #include <inttypes.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 13 | #include <limits.h> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | #include <math.h> |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 15 | |
Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame^] | 16 | #include <memory> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | #include <string> |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 18 | #include <utility> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 20 | #include "base/compiler_specific.h" |
| 21 | #include "base/debug/alias.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | #include "base/logging.h" |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 23 | #include "base/memory/ptr_util.h" |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 24 | #include "base/metrics/dummy_histogram.h" |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 25 | #include "base/metrics/histogram_functions.h" |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 26 | #include "base/metrics/metrics_hashes.h" |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 27 | #include "base/metrics/persistent_histogram_allocator.h" |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 28 | #include "base/metrics/persistent_memory_allocator.h" |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 29 | #include "base/metrics/sample_vector.h" |
[email protected] | 567d30e | 2012-07-13 21:48:29 | [diff] [blame] | 30 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 3f38385 | 2009-04-03 18:18:55 | [diff] [blame] | 31 | #include "base/pickle.h" |
Anton Bikineev | a61fb57 | 2020-10-18 08:54:44 | [diff] [blame] | 32 | #include "base/ranges/algorithm.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 33 | #include "base/strings/string_util.h" |
| 34 | #include "base/strings/stringprintf.h" |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame] | 35 | #include "base/strings/utf_string_conversions.h" |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 36 | #include "base/synchronization/lock.h" |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 37 | #include "base/values.h" |
Alexei Svitkine | e73f80a0 | 2017-07-18 00:08:39 | [diff] [blame] | 38 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 39 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 40 | namespace base { |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 41 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | bool ReadHistogramArguments(PickleIterator* iter, |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 45 | std::string* histogram_name, |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 46 | int* flags, |
| 47 | int* declared_min, |
| 48 | int* declared_max, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 49 | uint32_t* bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 50 | uint32_t* range_checksum) { |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 51 | if (!iter->ReadString(histogram_name) || |
| 52 | !iter->ReadInt(flags) || |
| 53 | !iter->ReadInt(declared_min) || |
| 54 | !iter->ReadInt(declared_max) || |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 55 | !iter->ReadUInt32(bucket_count) || |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 56 | !iter->ReadUInt32(range_checksum)) { |
| 57 | DLOG(ERROR) << "Pickle error decoding Histogram: " << *histogram_name; |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Since these fields may have come from an untrusted renderer, do additional |
| 62 | // checks above and beyond those in Histogram::Initialize() |
| 63 | if (*declared_max <= 0 || |
| 64 | *declared_min <= 0 || |
| 65 | *declared_max < *declared_min || |
| 66 | INT_MAX / sizeof(HistogramBase::Count) <= *bucket_count || |
| 67 | *bucket_count < 2) { |
| 68 | DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name; |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // We use the arguments to find or create the local version of the histogram |
bcwhite | 05dc092 | 2016-06-03 04:59:44 | [diff] [blame] | 73 | // in this process, so we need to clear any IPC flag. |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 74 | *flags &= ~HistogramBase::kIPCSerializationSourceFlag; |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | bool ValidateRangeChecksum(const HistogramBase& histogram, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 80 | uint32_t range_checksum) { |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 81 | // Normally, |histogram| should have type HISTOGRAM or be inherited from it. |
| 82 | // However, if it's expired, it will actually be a DUMMY_HISTOGRAM. |
| 83 | // Skip the checks in that case. |
| 84 | if (histogram.GetHistogramType() == DUMMY_HISTOGRAM) |
| 85 | return true; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 86 | const Histogram& casted_histogram = |
| 87 | static_cast<const Histogram&>(histogram); |
| 88 | |
| 89 | return casted_histogram.bucket_ranges()->checksum() == range_checksum; |
| 90 | } |
| 91 | |
| 92 | } // namespace |
| 93 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 94 | typedef HistogramBase::Count Count; |
| 95 | typedef HistogramBase::Sample Sample; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 97 | // static |
Brian White | abfa270 | 2019-04-03 17:00:54 | [diff] [blame] | 98 | const uint32_t Histogram::kBucketCount_MAX = 1002u; |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 99 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 100 | class Histogram::Factory { |
| 101 | public: |
| 102 | Factory(const std::string& name, |
| 103 | HistogramBase::Sample minimum, |
| 104 | HistogramBase::Sample maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 105 | uint32_t bucket_count, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 106 | int32_t flags) |
| 107 | : Factory(name, HISTOGRAM, minimum, maximum, bucket_count, flags) {} |
| 108 | |
| 109 | // Create histogram based on construction parameters. Caller takes |
| 110 | // ownership of the returned object. |
| 111 | HistogramBase* Build(); |
| 112 | |
| 113 | protected: |
| 114 | Factory(const std::string& name, |
| 115 | HistogramType histogram_type, |
| 116 | HistogramBase::Sample minimum, |
| 117 | HistogramBase::Sample maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 118 | uint32_t bucket_count, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 119 | int32_t flags) |
| 120 | : name_(name), |
| 121 | histogram_type_(histogram_type), |
| 122 | minimum_(minimum), |
| 123 | maximum_(maximum), |
| 124 | bucket_count_(bucket_count), |
| 125 | flags_(flags) {} |
| 126 | |
| 127 | // Create a BucketRanges structure appropriate for this histogram. |
| 128 | virtual BucketRanges* CreateRanges() { |
| 129 | BucketRanges* ranges = new BucketRanges(bucket_count_ + 1); |
| 130 | Histogram::InitializeBucketRanges(minimum_, maximum_, ranges); |
| 131 | return ranges; |
| 132 | } |
| 133 | |
| 134 | // Allocate the correct Histogram object off the heap (in case persistent |
| 135 | // memory is not available). |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 136 | virtual std::unique_ptr<HistogramBase> HeapAlloc(const BucketRanges* ranges) { |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 137 | return WrapUnique( |
| 138 | new Histogram(GetPermanentName(name_), minimum_, maximum_, ranges)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Perform any required datafill on the just-created histogram. If |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 142 | // overridden, be sure to call the "super" version -- this method may not |
| 143 | // always remain empty. |
| 144 | virtual void FillHistogram(HistogramBase* histogram) {} |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 145 | |
| 146 | // These values are protected (instead of private) because they need to |
| 147 | // be accessible to methods of sub-classes in order to avoid passing |
| 148 | // unnecessary parameters everywhere. |
| 149 | const std::string& name_; |
| 150 | const HistogramType histogram_type_; |
| 151 | HistogramBase::Sample minimum_; |
| 152 | HistogramBase::Sample maximum_; |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 153 | uint32_t bucket_count_; |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 154 | int32_t flags_; |
| 155 | |
| 156 | private: |
| 157 | DISALLOW_COPY_AND_ASSIGN(Factory); |
| 158 | }; |
| 159 | |
| 160 | HistogramBase* Histogram::Factory::Build() { |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 161 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name_); |
| 162 | if (!histogram) { |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 163 | // TODO(gayane): |HashMetricName()| is called again in Histogram |
| 164 | // constructor. Refactor code to avoid the additional call. |
| 165 | bool should_record = |
| 166 | StatisticsRecorder::ShouldRecordHistogram(HashMetricName(name_)); |
| 167 | if (!should_record) |
| 168 | return DummyHistogram::GetInstance(); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 169 | // To avoid racy destruction at shutdown, the following will be leaked. |
bcwhite | 4ebd727 | 2016-03-22 19:14:38 | [diff] [blame] | 170 | const BucketRanges* created_ranges = CreateRanges(); |
Brian White | e716f6e | 2018-09-27 21:01:46 | [diff] [blame] | 171 | |
| 172 | const BucketRanges* registered_ranges = |
| 173 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(created_ranges); |
| 174 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 175 | // In most cases, the bucket-count, minimum, and maximum values are known |
| 176 | // when the code is written and so are passed in explicitly. In other |
| 177 | // cases (such as with a CustomHistogram), they are calculated dynamically |
| 178 | // at run-time. In the latter case, those ctor parameters are zero and |
| 179 | // the results extracted from the result of CreateRanges(). |
| 180 | if (bucket_count_ == 0) { |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 181 | bucket_count_ = static_cast<uint32_t>(registered_ranges->bucket_count()); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 182 | minimum_ = registered_ranges->range(1); |
| 183 | maximum_ = registered_ranges->range(bucket_count_ - 1); |
| 184 | } |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 185 | DCHECK_EQ(minimum_, registered_ranges->range(1)); |
| 186 | DCHECK_EQ(maximum_, registered_ranges->range(bucket_count_ - 1)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 187 | |
| 188 | // Try to create the histogram using a "persistent" allocator. As of |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 189 | // 2016-02-25, the availability of such is controlled by a base::Feature |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 190 | // that is off by default. If the allocator doesn't exist or if |
| 191 | // allocating from it fails, code below will allocate the histogram from |
| 192 | // the process heap. |
bcwhite | 4ebd727 | 2016-03-22 19:14:38 | [diff] [blame] | 193 | PersistentHistogramAllocator::Reference histogram_ref = 0; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 194 | std::unique_ptr<HistogramBase> tentative_histogram; |
bcwhite | 5e748c6 | 2016-04-06 02:03:53 | [diff] [blame] | 195 | PersistentHistogramAllocator* allocator = GlobalHistogramAllocator::Get(); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 196 | if (allocator) { |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 197 | tentative_histogram = allocator->AllocateHistogram( |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 198 | histogram_type_, |
| 199 | name_, |
| 200 | minimum_, |
| 201 | maximum_, |
| 202 | registered_ranges, |
| 203 | flags_, |
| 204 | &histogram_ref); |
| 205 | } |
| 206 | |
| 207 | // Handle the case where no persistent allocator is present or the |
| 208 | // persistent allocation fails (perhaps because it is full). |
| 209 | if (!tentative_histogram) { |
bcwhite | 4ebd727 | 2016-03-22 19:14:38 | [diff] [blame] | 210 | DCHECK(!histogram_ref); // Should never have been set. |
| 211 | DCHECK(!allocator); // Shouldn't have failed. |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 212 | flags_ &= ~HistogramBase::kIsPersistent; |
| 213 | tentative_histogram = HeapAlloc(registered_ranges); |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 214 | tentative_histogram->SetFlags(flags_); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 215 | } |
| 216 | |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 217 | FillHistogram(tentative_histogram.get()); |
| 218 | |
| 219 | // Register this histogram with the StatisticsRecorder. Keep a copy of |
| 220 | // the pointer value to tell later whether the locally created histogram |
| 221 | // was registered or deleted. The type is "void" because it could point |
| 222 | // to released memory after the following line. |
| 223 | const void* tentative_histogram_ptr = tentative_histogram.get(); |
| 224 | histogram = StatisticsRecorder::RegisterOrDeleteDuplicate( |
| 225 | tentative_histogram.release()); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 226 | |
| 227 | // Persistent histograms need some follow-up processing. |
bcwhite | 4ebd727 | 2016-03-22 19:14:38 | [diff] [blame] | 228 | if (histogram_ref) { |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 229 | allocator->FinalizeHistogram(histogram_ref, |
| 230 | histogram == tentative_histogram_ptr); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Brian White | a958cc7 | 2018-04-19 18:24:16 | [diff] [blame] | 234 | if (histogram_type_ != histogram->GetHistogramType() || |
| 235 | (bucket_count_ != 0 && !histogram->HasConstructionArguments( |
| 236 | minimum_, maximum_, bucket_count_))) { |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 237 | // The construction arguments do not match the existing histogram. This can |
| 238 | // come about if an extension updates in the middle of a chrome run and has |
Brian White | a958cc7 | 2018-04-19 18:24:16 | [diff] [blame] | 239 | // changed one of them, or simply by bad code within Chrome itself. A NULL |
| 240 | // return would cause Chrome to crash; better to just record it for later |
| 241 | // analysis. |
| 242 | UmaHistogramSparse("Histogram.MismatchedConstructionArguments", |
| 243 | static_cast<Sample>(HashMetricName(name_))); |
| 244 | DLOG(ERROR) << "Histogram " << name_ |
| 245 | << " has mismatched construction arguments"; |
| 246 | return DummyHistogram::GetInstance(); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 247 | } |
| 248 | return histogram; |
| 249 | } |
| 250 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 251 | HistogramBase* Histogram::FactoryGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 252 | Sample minimum, |
| 253 | Sample maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 254 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 255 | int32_t flags) { |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 256 | bool valid_arguments = |
| 257 | InspectConstructionArguments(name, &minimum, &maximum, &bucket_count); |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 258 | DCHECK(valid_arguments) << name; |
[email protected] | a93721e2 | 2012-01-06 02:13:28 | [diff] [blame] | 259 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 260 | return Factory(name, minimum, maximum, bucket_count, flags).Build(); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 261 | } |
| 262 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 263 | HistogramBase* Histogram::FactoryTimeGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 264 | TimeDelta minimum, |
| 265 | TimeDelta maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 266 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 267 | int32_t flags) { |
Jan Krcal | 4e553f42 | 2019-04-08 12:52:46 | [diff] [blame] | 268 | DCHECK_LT(minimum.InMilliseconds(), std::numeric_limits<Sample>::max()); |
| 269 | DCHECK_LT(maximum.InMilliseconds(), std::numeric_limits<Sample>::max()); |
pkasting | 9cf9b94a | 2014-10-01 22:18:43 | [diff] [blame] | 270 | return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()), |
| 271 | static_cast<Sample>(maximum.InMilliseconds()), bucket_count, |
| 272 | flags); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 273 | } |
| 274 | |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 275 | HistogramBase* Histogram::FactoryMicrosecondsTimeGet(const std::string& name, |
| 276 | TimeDelta minimum, |
| 277 | TimeDelta maximum, |
| 278 | uint32_t bucket_count, |
| 279 | int32_t flags) { |
Jan Krcal | 4e553f42 | 2019-04-08 12:52:46 | [diff] [blame] | 280 | DCHECK_LT(minimum.InMicroseconds(), std::numeric_limits<Sample>::max()); |
| 281 | DCHECK_LT(maximum.InMicroseconds(), std::numeric_limits<Sample>::max()); |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 282 | return FactoryGet(name, static_cast<Sample>(minimum.InMicroseconds()), |
| 283 | static_cast<Sample>(maximum.InMicroseconds()), bucket_count, |
| 284 | flags); |
| 285 | } |
| 286 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 287 | HistogramBase* Histogram::FactoryGet(const char* name, |
| 288 | Sample minimum, |
| 289 | Sample maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 290 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 291 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 292 | return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags); |
| 293 | } |
| 294 | |
| 295 | HistogramBase* Histogram::FactoryTimeGet(const char* name, |
| 296 | TimeDelta minimum, |
| 297 | TimeDelta maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 298 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 299 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 300 | return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count, |
| 301 | flags); |
| 302 | } |
| 303 | |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 304 | HistogramBase* Histogram::FactoryMicrosecondsTimeGet(const char* name, |
| 305 | TimeDelta minimum, |
| 306 | TimeDelta maximum, |
| 307 | uint32_t bucket_count, |
| 308 | int32_t flags) { |
| 309 | return FactoryMicrosecondsTimeGet(std::string(name), minimum, maximum, |
| 310 | bucket_count, flags); |
| 311 | } |
| 312 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 313 | std::unique_ptr<HistogramBase> Histogram::PersistentCreate( |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 314 | const char* name, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 315 | Sample minimum, |
| 316 | Sample maximum, |
| 317 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 318 | const DelayedPersistentAllocation& counts, |
| 319 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 320 | HistogramSamples::Metadata* meta, |
| 321 | HistogramSamples::Metadata* logged_meta) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 322 | return WrapUnique(new Histogram(name, minimum, maximum, ranges, counts, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 323 | logged_counts, meta, logged_meta)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 324 | } |
| 325 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 326 | // Calculate what range of values are held in each bucket. |
| 327 | // We have to be careful that we don't pick a ratio between starting points in |
| 328 | // consecutive buckets that is sooo small, that the integer bounds are the same |
| 329 | // (effectively making one bucket get no values). We need to avoid: |
| 330 | // ranges(i) == ranges(i + 1) |
| 331 | // To avoid that, we just do a fine-grained bucket width as far as we need to |
| 332 | // until we get a ratio that moves us along at least 2 units at a time. From |
| 333 | // that bucket onward we do use the exponential growth of buckets. |
| 334 | // |
| 335 | // static |
| 336 | void Histogram::InitializeBucketRanges(Sample minimum, |
| 337 | Sample maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 338 | BucketRanges* ranges) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 339 | double log_max = log(static_cast<double>(maximum)); |
| 340 | double log_ratio; |
| 341 | double log_next; |
| 342 | size_t bucket_index = 1; |
| 343 | Sample current = minimum; |
| 344 | ranges->set_range(bucket_index, current); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 345 | size_t bucket_count = ranges->bucket_count(); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 346 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 347 | while (bucket_count > ++bucket_index) { |
| 348 | double log_current; |
| 349 | log_current = log(static_cast<double>(current)); |
Brian White | 2aaa8b01a | 2018-11-13 23:24:56 | [diff] [blame] | 350 | debug::Alias(&log_current); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 351 | // Calculate the count'th root of the range. |
| 352 | log_ratio = (log_max - log_current) / (bucket_count - bucket_index); |
| 353 | // See where the next bucket would start. |
| 354 | log_next = log_current + log_ratio; |
| 355 | Sample next; |
Peter Kasting | 8c3adfb | 2017-09-13 08:07:39 | [diff] [blame] | 356 | next = static_cast<int>(std::round(exp(log_next))); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 357 | if (next > current) |
| 358 | current = next; |
| 359 | else |
| 360 | ++current; // Just do a narrow bucket, and keep trying. |
| 361 | ranges->set_range(bucket_index, current); |
| 362 | } |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 363 | ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 364 | ranges->ResetChecksum(); |
| 365 | } |
| 366 | |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 367 | // static |
| 368 | const int Histogram::kCommonRaceBasedCountMismatch = 5; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 369 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 370 | uint32_t Histogram::FindCorruption(const HistogramSamples& samples) const { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 371 | int inconsistencies = NO_INCONSISTENCIES; |
| 372 | Sample previous_range = -1; // Bottom range is always 0. |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 373 | for (uint32_t index = 0; index < bucket_count(); ++index) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 374 | int new_range = ranges(index); |
| 375 | if (previous_range >= new_range) |
| 376 | inconsistencies |= BUCKET_ORDER_ERROR; |
| 377 | previous_range = new_range; |
| 378 | } |
| 379 | |
| 380 | if (!bucket_ranges()->HasValidChecksum()) |
| 381 | inconsistencies |= RANGE_CHECKSUM_ERROR; |
| 382 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 383 | int64_t delta64 = samples.redundant_count() - samples.TotalCount(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 384 | if (delta64 != 0) { |
| 385 | int delta = static_cast<int>(delta64); |
| 386 | if (delta != delta64) |
| 387 | delta = INT_MAX; // Flag all giant errors as INT_MAX. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 388 | if (delta > 0) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 389 | if (delta > kCommonRaceBasedCountMismatch) |
| 390 | inconsistencies |= COUNT_HIGH_ERROR; |
| 391 | } else { |
| 392 | DCHECK_GT(0, delta); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 393 | if (-delta > kCommonRaceBasedCountMismatch) |
| 394 | inconsistencies |= COUNT_LOW_ERROR; |
| 395 | } |
| 396 | } |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 397 | return inconsistencies; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 398 | } |
| 399 | |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 400 | const BucketRanges* Histogram::bucket_ranges() const { |
| 401 | return unlogged_samples_->bucket_ranges(); |
| 402 | } |
| 403 | |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 404 | Sample Histogram::declared_min() const { |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 405 | const BucketRanges* ranges = bucket_ranges(); |
| 406 | if (ranges->bucket_count() < 2) |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 407 | return -1; |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 408 | return ranges->range(1); |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | Sample Histogram::declared_max() const { |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 412 | const BucketRanges* ranges = bucket_ranges(); |
| 413 | if (ranges->bucket_count() < 2) |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 414 | return -1; |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 415 | return ranges->range(ranges->bucket_count() - 1); |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 416 | } |
| 417 | |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 418 | Sample Histogram::ranges(uint32_t i) const { |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 419 | return bucket_ranges()->range(i); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 420 | } |
| 421 | |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 422 | uint32_t Histogram::bucket_count() const { |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 423 | return static_cast<uint32_t>(bucket_ranges()->bucket_count()); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 426 | // static |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 427 | bool Histogram::InspectConstructionArguments(StringPiece name, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 428 | Sample* minimum, |
| 429 | Sample* maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 430 | uint32_t* bucket_count) { |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 431 | bool check_okay = true; |
| 432 | |
| 433 | // Checks below must be done after any min/max swap. |
| 434 | if (*minimum > *maximum) { |
| 435 | check_okay = false; |
| 436 | std::swap(*minimum, *maximum); |
| 437 | } |
| 438 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 439 | // Defensive code for backward compatibility. |
| 440 | if (*minimum < 1) { |
[email protected] | a5c7bd79 | 2012-08-02 00:29:04 | [diff] [blame] | 441 | DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 442 | *minimum = 1; |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 443 | if (*maximum < 1) |
| 444 | *maximum = 1; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 445 | } |
| 446 | if (*maximum >= kSampleType_MAX) { |
[email protected] | a5c7bd79 | 2012-08-02 00:29:04 | [diff] [blame] | 447 | DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum; |
| 448 | *maximum = kSampleType_MAX - 1; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 449 | } |
Brian White | abfa270 | 2019-04-03 17:00:54 | [diff] [blame] | 450 | if (*bucket_count > kBucketCount_MAX) { |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 451 | UmaHistogramSparse("Histogram.TooManyBuckets.1000", |
| 452 | static_cast<Sample>(HashMetricName(name))); |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 453 | |
Alexei Svitkine | d67a443 | 2020-10-13 22:31:21 | [diff] [blame] | 454 | // TODO(bcwhite): Look at injecting allowlist (using hashes) from a higher |
| 455 | // layer rather than hardcoding them here. |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 456 | // Blink.UseCounter legitimately has more than 1000 entries in its enum. |
Alexei Svitkine | d67a443 | 2020-10-13 22:31:21 | [diff] [blame] | 457 | if (!StartsWith(name, "Blink.UseCounter")) { |
Brian White | abfa270 | 2019-04-03 17:00:54 | [diff] [blame] | 458 | DVLOG(1) << "Histogram: " << name |
| 459 | << " has bad bucket_count: " << *bucket_count << " (limit " |
| 460 | << kBucketCount_MAX << ")"; |
| 461 | |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 462 | // Assume it's a mistake and limit to 100 buckets, plus under and over. |
| 463 | // If the DCHECK doesn't alert the user then hopefully the small number |
| 464 | // will be obvious on the dashboard. If not, then it probably wasn't |
| 465 | // important. |
| 466 | *bucket_count = 102; |
| 467 | check_okay = false; |
| 468 | } |
bcwhite | aaf2d345 | 2017-04-26 19:17:47 | [diff] [blame] | 469 | } |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 470 | |
| 471 | // Ensure parameters are sane. |
bcwhite | aaf2d345 | 2017-04-26 19:17:47 | [diff] [blame] | 472 | if (*maximum == *minimum) { |
| 473 | check_okay = false; |
| 474 | *maximum = *minimum + 1; |
| 475 | } |
| 476 | if (*bucket_count < 3) { |
| 477 | check_okay = false; |
| 478 | *bucket_count = 3; |
| 479 | } |
| 480 | if (*bucket_count > static_cast<uint32_t>(*maximum - *minimum + 2)) { |
| 481 | check_okay = false; |
| 482 | *bucket_count = static_cast<uint32_t>(*maximum - *minimum + 2); |
| 483 | } |
| 484 | |
| 485 | if (!check_okay) { |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 486 | UmaHistogramSparse("Histogram.BadConstructionArguments", |
| 487 | static_cast<Sample>(HashMetricName(name))); |
bcwhite | aaf2d345 | 2017-04-26 19:17:47 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | return check_okay; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 491 | } |
| 492 | |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 493 | uint64_t Histogram::name_hash() const { |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 494 | return unlogged_samples_->id(); |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 495 | } |
| 496 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 497 | HistogramType Histogram::GetHistogramType() const { |
| 498 | return HISTOGRAM; |
| 499 | } |
| 500 | |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 501 | bool Histogram::HasConstructionArguments(Sample expected_minimum, |
| 502 | Sample expected_maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 503 | uint32_t expected_bucket_count) const { |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 504 | return (expected_bucket_count == bucket_count() && |
| 505 | expected_minimum == declared_min() && |
| 506 | expected_maximum == declared_max()); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void Histogram::Add(int value) { |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 510 | AddCount(value, 1); |
| 511 | } |
| 512 | |
| 513 | void Histogram::AddCount(int value, int count) { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 514 | DCHECK_EQ(0, ranges(0)); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 515 | DCHECK_EQ(kSampleType_MAX, ranges(bucket_count())); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 516 | |
| 517 | if (value > kSampleType_MAX - 1) |
| 518 | value = kSampleType_MAX - 1; |
| 519 | if (value < 0) |
| 520 | value = 0; |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 521 | if (count <= 0) { |
| 522 | NOTREACHED(); |
| 523 | return; |
| 524 | } |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 525 | unlogged_samples_->Accumulate(value, count); |
simonhatch | df5a814 | 2015-07-15 22:22:57 | [diff] [blame] | 526 | |
Oystein Eftevaag | 53412eb | 2019-12-09 20:53:56 | [diff] [blame] | 527 | if (UNLIKELY(StatisticsRecorder::have_active_callbacks())) |
| 528 | FindAndRunCallback(value); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 529 | } |
| 530 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 531 | std::unique_ptr<HistogramSamples> Histogram::SnapshotSamples() const { |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 532 | return SnapshotAllSamples(); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 533 | } |
| 534 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 535 | std::unique_ptr<HistogramSamples> Histogram::SnapshotDelta() { |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 536 | #if DCHECK_IS_ON() |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 537 | DCHECK(!final_delta_created_); |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 538 | #endif |
| 539 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 540 | // The code below has subtle thread-safety guarantees! All changes to |
| 541 | // the underlying SampleVectors use atomic integer operations, which guarantee |
| 542 | // eventual consistency, but do not guarantee full synchronization between |
| 543 | // different entries in the SampleVector. In particular, this means that |
| 544 | // concurrent updates to the histogram might result in the reported sum not |
| 545 | // matching the individual bucket counts; or there being some buckets that are |
| 546 | // logically updated "together", but end up being only partially updated when |
| 547 | // a snapshot is captured. Note that this is why it's important to subtract |
| 548 | // exactly the snapshotted unlogged samples, rather than simply resetting the |
| 549 | // vector: this way, the next snapshot will include any concurrent updates |
| 550 | // missed by the current snapshot. |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 551 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 552 | std::unique_ptr<HistogramSamples> snapshot = SnapshotUnloggedSamples(); |
| 553 | unlogged_samples_->Subtract(*snapshot); |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 554 | logged_samples_->Add(*snapshot); |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 555 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 556 | return snapshot; |
| 557 | } |
| 558 | |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 559 | std::unique_ptr<HistogramSamples> Histogram::SnapshotFinalDelta() const { |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 560 | #if DCHECK_IS_ON() |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 561 | DCHECK(!final_delta_created_); |
| 562 | final_delta_created_ = true; |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 563 | #endif |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 564 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 565 | return SnapshotUnloggedSamples(); |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 566 | } |
| 567 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 568 | void Histogram::AddSamples(const HistogramSamples& samples) { |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 569 | unlogged_samples_->Add(samples); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | bool Histogram::AddSamplesFromPickle(PickleIterator* iter) { |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 573 | return unlogged_samples_->AddFromPickle(iter); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 574 | } |
| 575 | |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame] | 576 | base::DictionaryValue Histogram::ToGraphDict() const { |
| 577 | std::unique_ptr<SampleVector> snapshot = SnapshotAllSamples(); |
Quang Minh Tuan Nguyen | 39b1fed | 2021-03-16 00:49:38 | [diff] [blame] | 578 | return snapshot->ToGraphDict(histogram_name(), flags()); |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame] | 579 | } |
| 580 | |
Brian White | 1a3a225c | 2018-04-27 00:40:07 | [diff] [blame] | 581 | void Histogram::ValidateHistogramContents() const { |
| 582 | CHECK(unlogged_samples_); |
| 583 | CHECK(unlogged_samples_->bucket_ranges()); |
| 584 | CHECK(logged_samples_); |
| 585 | CHECK(logged_samples_->bucket_ranges()); |
| 586 | CHECK_NE(0U, logged_samples_->id()); |
| 587 | } |
| 588 | |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 589 | void Histogram::SerializeInfoImpl(Pickle* pickle) const { |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 590 | DCHECK(bucket_ranges()->HasValidChecksum()); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 591 | pickle->WriteString(histogram_name()); |
| 592 | pickle->WriteInt(flags()); |
| 593 | pickle->WriteInt(declared_min()); |
| 594 | pickle->WriteInt(declared_max()); |
| 595 | pickle->WriteUInt32(bucket_count()); |
| 596 | pickle->WriteUInt32(bucket_ranges()->checksum()); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 597 | } |
| 598 | |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 599 | // TODO(bcwhite): Remove minimum/maximum parameters from here and call chain. |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 600 | Histogram::Histogram(const char* name, |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 601 | Sample minimum, |
| 602 | Sample maximum, |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 603 | const BucketRanges* ranges) |
Brian White | caef6e1 | 2018-03-12 22:06:11 | [diff] [blame] | 604 | : HistogramBase(name) { |
Brian White | 7463677b | 2018-04-09 21:51:40 | [diff] [blame] | 605 | DCHECK(ranges) << name << ": " << minimum << "-" << maximum; |
Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame^] | 606 | unlogged_samples_ = |
| 607 | std::make_unique<SampleVector>(HashMetricName(name), ranges); |
| 608 | logged_samples_ = |
| 609 | std::make_unique<SampleVector>(unlogged_samples_->id(), ranges); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 610 | } |
| 611 | |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 612 | Histogram::Histogram(const char* name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 613 | Sample minimum, |
| 614 | Sample maximum, |
| 615 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 616 | const DelayedPersistentAllocation& counts, |
| 617 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 618 | HistogramSamples::Metadata* meta, |
| 619 | HistogramSamples::Metadata* logged_meta) |
Brian White | caef6e1 | 2018-03-12 22:06:11 | [diff] [blame] | 620 | : HistogramBase(name) { |
Brian White | 7463677b | 2018-04-09 21:51:40 | [diff] [blame] | 621 | DCHECK(ranges) << name << ": " << minimum << "-" << maximum; |
Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame^] | 622 | unlogged_samples_ = std::make_unique<PersistentSampleVector>( |
| 623 | HashMetricName(name), ranges, meta, counts); |
| 624 | logged_samples_ = std::make_unique<PersistentSampleVector>( |
| 625 | unlogged_samples_->id(), ranges, logged_meta, logged_counts); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 626 | } |
| 627 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 628 | Histogram::~Histogram() = default; |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 629 | |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 630 | const std::string Histogram::GetAsciiBucketRange(uint32_t i) const { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 631 | return GetSimpleAsciiBucketRange(ranges(i)); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 632 | } |
| 633 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 634 | //------------------------------------------------------------------------------ |
| 635 | // Private methods |
| 636 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 637 | // static |
| 638 | HistogramBase* Histogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 639 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 640 | int flags; |
| 641 | int declared_min; |
| 642 | int declared_max; |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 643 | uint32_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 644 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 645 | |
| 646 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 647 | &declared_max, &bucket_count, &range_checksum)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 648 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | // Find or create the local version of the histogram in this process. |
| 652 | HistogramBase* histogram = Histogram::FactoryGet( |
| 653 | histogram_name, declared_min, declared_max, bucket_count, flags); |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 654 | if (!histogram) |
| 655 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 656 | |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 657 | // The serialized histogram might be corrupted. |
| 658 | if (!ValidateRangeChecksum(*histogram, range_checksum)) |
| 659 | return nullptr; |
| 660 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 661 | return histogram; |
| 662 | } |
| 663 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 664 | std::unique_ptr<SampleVector> Histogram::SnapshotAllSamples() const { |
| 665 | std::unique_ptr<SampleVector> samples = SnapshotUnloggedSamples(); |
| 666 | samples->Add(*logged_samples_); |
| 667 | return samples; |
| 668 | } |
| 669 | |
| 670 | std::unique_ptr<SampleVector> Histogram::SnapshotUnloggedSamples() const { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 671 | std::unique_ptr<SampleVector> samples( |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 672 | new SampleVector(unlogged_samples_->id(), bucket_ranges())); |
| 673 | samples->Add(*unlogged_samples_); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 674 | return samples; |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 675 | } |
| 676 | |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 677 | void Histogram::GetParameters(DictionaryValue* params) const { |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 678 | params->SetString("type", HistogramTypeToString(GetHistogramType())); |
David 'Digit' Turner | e169e6c | 2019-03-28 22:06:29 | [diff] [blame] | 679 | params->SetIntKey("min", declared_min()); |
| 680 | params->SetIntKey("max", declared_max()); |
| 681 | params->SetIntKey("bucket_count", static_cast<int>(bucket_count())); |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 682 | } |
| 683 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 684 | //------------------------------------------------------------------------------ |
| 685 | // LinearHistogram: This histogram uses a traditional set of evenly spaced |
| 686 | // buckets. |
| 687 | //------------------------------------------------------------------------------ |
| 688 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 689 | class LinearHistogram::Factory : public Histogram::Factory { |
| 690 | public: |
| 691 | Factory(const std::string& name, |
| 692 | HistogramBase::Sample minimum, |
| 693 | HistogramBase::Sample maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 694 | uint32_t bucket_count, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 695 | int32_t flags, |
| 696 | const DescriptionPair* descriptions) |
| 697 | : Histogram::Factory(name, LINEAR_HISTOGRAM, minimum, maximum, |
| 698 | bucket_count, flags) { |
| 699 | descriptions_ = descriptions; |
| 700 | } |
| 701 | |
| 702 | protected: |
| 703 | BucketRanges* CreateRanges() override { |
| 704 | BucketRanges* ranges = new BucketRanges(bucket_count_ + 1); |
| 705 | LinearHistogram::InitializeBucketRanges(minimum_, maximum_, ranges); |
| 706 | return ranges; |
| 707 | } |
| 708 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 709 | std::unique_ptr<HistogramBase> HeapAlloc( |
| 710 | const BucketRanges* ranges) override { |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 711 | return WrapUnique(new LinearHistogram(GetPermanentName(name_), minimum_, |
| 712 | maximum_, ranges)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void FillHistogram(HistogramBase* base_histogram) override { |
| 716 | Histogram::Factory::FillHistogram(base_histogram); |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 717 | // Normally, |base_histogram| should have type LINEAR_HISTOGRAM or be |
| 718 | // inherited from it. However, if it's expired, it will actually be a |
| 719 | // DUMMY_HISTOGRAM. Skip filling in that case. |
| 720 | if (base_histogram->GetHistogramType() == DUMMY_HISTOGRAM) |
| 721 | return; |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 722 | LinearHistogram* histogram = static_cast<LinearHistogram*>(base_histogram); |
| 723 | // Set range descriptions. |
| 724 | if (descriptions_) { |
| 725 | for (int i = 0; descriptions_[i].description; ++i) { |
| 726 | histogram->bucket_description_[descriptions_[i].sample] = |
| 727 | descriptions_[i].description; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | private: |
| 733 | const DescriptionPair* descriptions_; |
| 734 | |
| 735 | DISALLOW_COPY_AND_ASSIGN(Factory); |
| 736 | }; |
| 737 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 738 | LinearHistogram::~LinearHistogram() = default; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 739 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 740 | HistogramBase* LinearHistogram::FactoryGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 741 | Sample minimum, |
| 742 | Sample maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 743 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 744 | int32_t flags) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 745 | return FactoryGetWithRangeDescription(name, minimum, maximum, bucket_count, |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 746 | flags, NULL); |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 747 | } |
| 748 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 749 | HistogramBase* LinearHistogram::FactoryTimeGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 750 | TimeDelta minimum, |
| 751 | TimeDelta maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 752 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 753 | int32_t flags) { |
Jan Krcal | 4e553f42 | 2019-04-08 12:52:46 | [diff] [blame] | 754 | DCHECK_LT(minimum.InMilliseconds(), std::numeric_limits<Sample>::max()); |
| 755 | DCHECK_LT(maximum.InMilliseconds(), std::numeric_limits<Sample>::max()); |
pkasting | 9cf9b94a | 2014-10-01 22:18:43 | [diff] [blame] | 756 | return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()), |
| 757 | static_cast<Sample>(maximum.InMilliseconds()), bucket_count, |
| 758 | flags); |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 759 | } |
| 760 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 761 | HistogramBase* LinearHistogram::FactoryGet(const char* name, |
| 762 | Sample minimum, |
| 763 | Sample maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 764 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 765 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 766 | return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags); |
| 767 | } |
| 768 | |
| 769 | HistogramBase* LinearHistogram::FactoryTimeGet(const char* name, |
| 770 | TimeDelta minimum, |
| 771 | TimeDelta maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 772 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 773 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 774 | return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count, |
| 775 | flags); |
| 776 | } |
| 777 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 778 | std::unique_ptr<HistogramBase> LinearHistogram::PersistentCreate( |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 779 | const char* name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 780 | Sample minimum, |
| 781 | Sample maximum, |
| 782 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 783 | const DelayedPersistentAllocation& counts, |
| 784 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 785 | HistogramSamples::Metadata* meta, |
| 786 | HistogramSamples::Metadata* logged_meta) { |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 787 | return WrapUnique(new LinearHistogram(name, minimum, maximum, ranges, counts, |
| 788 | logged_counts, meta, logged_meta)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 789 | } |
| 790 | |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 791 | HistogramBase* LinearHistogram::FactoryGetWithRangeDescription( |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 792 | const std::string& name, |
| 793 | Sample minimum, |
| 794 | Sample maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 795 | uint32_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 796 | int32_t flags, |
| 797 | const DescriptionPair descriptions[]) { |
Brian White | 564cedf | 2018-11-15 20:26:00 | [diff] [blame] | 798 | // Originally, histograms were required to have at least one sample value |
| 799 | // plus underflow and overflow buckets. For single-entry enumerations, |
| 800 | // that one value is usually zero (which IS the underflow bucket) |
| 801 | // resulting in a |maximum| value of 1 (the exclusive upper-bound) and only |
| 802 | // the two outlier buckets. Handle this by making max==2 and buckets==3. |
| 803 | // This usually won't have any cost since the single-value-optimization |
| 804 | // will be used until the count exceeds 16 bits. |
| 805 | if (maximum == 1 && bucket_count == 2) { |
| 806 | maximum = 2; |
| 807 | bucket_count = 3; |
| 808 | } |
| 809 | |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 810 | bool valid_arguments = Histogram::InspectConstructionArguments( |
| 811 | name, &minimum, &maximum, &bucket_count); |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 812 | DCHECK(valid_arguments) << name; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 813 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 814 | return Factory(name, minimum, maximum, bucket_count, flags, descriptions) |
| 815 | .Build(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 816 | } |
| 817 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 818 | HistogramType LinearHistogram::GetHistogramType() const { |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 819 | return LINEAR_HISTOGRAM; |
| 820 | } |
| 821 | |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 822 | LinearHistogram::LinearHistogram(const char* name, |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 823 | Sample minimum, |
| 824 | Sample maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 825 | const BucketRanges* ranges) |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 826 | : Histogram(name, minimum, maximum, ranges) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 827 | |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 828 | LinearHistogram::LinearHistogram( |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 829 | const char* name, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 830 | Sample minimum, |
| 831 | Sample maximum, |
| 832 | const BucketRanges* ranges, |
| 833 | const DelayedPersistentAllocation& counts, |
| 834 | const DelayedPersistentAllocation& logged_counts, |
| 835 | HistogramSamples::Metadata* meta, |
| 836 | HistogramSamples::Metadata* logged_meta) |
| 837 | : Histogram(name, |
| 838 | minimum, |
| 839 | maximum, |
| 840 | ranges, |
| 841 | counts, |
| 842 | logged_counts, |
| 843 | meta, |
| 844 | logged_meta) {} |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 845 | |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 846 | const std::string LinearHistogram::GetAsciiBucketRange(uint32_t i) const { |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 847 | int range = ranges(i); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 848 | BucketDescriptionMap::const_iterator it = bucket_description_.find(range); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 849 | if (it == bucket_description_.end()) |
| 850 | return Histogram::GetAsciiBucketRange(i); |
| 851 | return it->second; |
| 852 | } |
| 853 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 854 | // static |
| 855 | void LinearHistogram::InitializeBucketRanges(Sample minimum, |
| 856 | Sample maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 857 | BucketRanges* ranges) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 858 | double min = minimum; |
| 859 | double max = maximum; |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 860 | size_t bucket_count = ranges->bucket_count(); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 861 | |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 862 | for (size_t i = 1; i < bucket_count; ++i) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 863 | double linear_range = |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 864 | (min * (bucket_count - 1 - i) + max * (i - 1)) / (bucket_count - 2); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 865 | uint32_t range = static_cast<Sample>(linear_range + 0.5); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 866 | ranges->set_range(i, range); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 867 | } |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 868 | ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 869 | ranges->ResetChecksum(); |
| 870 | } |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 871 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 872 | // static |
| 873 | HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 874 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 875 | int flags; |
| 876 | int declared_min; |
| 877 | int declared_max; |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 878 | uint32_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 879 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 880 | |
| 881 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 882 | &declared_max, &bucket_count, &range_checksum)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 883 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | HistogramBase* histogram = LinearHistogram::FactoryGet( |
| 887 | histogram_name, declared_min, declared_max, bucket_count, flags); |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 888 | if (!histogram) |
| 889 | return nullptr; |
| 890 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 891 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 892 | // The serialized histogram might be corrupted. |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 893 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 894 | } |
| 895 | return histogram; |
| 896 | } |
| 897 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 898 | //------------------------------------------------------------------------------ |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 899 | // ScaledLinearHistogram: This is a wrapper around a LinearHistogram that |
| 900 | // scales input counts. |
| 901 | //------------------------------------------------------------------------------ |
| 902 | |
| 903 | ScaledLinearHistogram::ScaledLinearHistogram(const char* name, |
| 904 | Sample minimum, |
| 905 | Sample maximum, |
| 906 | uint32_t bucket_count, |
| 907 | int32_t scale, |
| 908 | int32_t flags) |
Eric Seckler | 21a69f4 | 2020-07-10 14:18:17 | [diff] [blame] | 909 | : ScaledLinearHistogram(std::string(name), |
| 910 | minimum, |
| 911 | maximum, |
| 912 | bucket_count, |
| 913 | scale, |
| 914 | flags) {} |
| 915 | |
| 916 | ScaledLinearHistogram::ScaledLinearHistogram(const std::string& name, |
| 917 | Sample minimum, |
| 918 | Sample maximum, |
| 919 | uint32_t bucket_count, |
| 920 | int32_t scale, |
| 921 | int32_t flags) |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 922 | : histogram_(LinearHistogram::FactoryGet(name, |
| 923 | minimum, |
| 924 | maximum, |
| 925 | bucket_count, |
| 926 | flags)), |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 927 | scale_(scale) { |
| 928 | DCHECK(histogram_); |
| 929 | DCHECK_LT(1, scale); |
| 930 | DCHECK_EQ(1, minimum); |
| 931 | CHECK_EQ(static_cast<Sample>(bucket_count), maximum - minimum + 2) |
| 932 | << " ScaledLinearHistogram requires buckets of size 1"; |
| 933 | |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 934 | // Normally, |histogram_| should have type LINEAR_HISTOGRAM or be |
| 935 | // inherited from it. However, if it's expired, it will be DUMMY_HISTOGRAM. |
| 936 | if (histogram_->GetHistogramType() == DUMMY_HISTOGRAM) |
| 937 | return; |
| 938 | |
| 939 | DCHECK_EQ(histogram_->GetHistogramType(), LINEAR_HISTOGRAM); |
| 940 | LinearHistogram* histogram = static_cast<LinearHistogram*>(histogram_); |
| 941 | remainders_.resize(histogram->bucket_count(), 0); |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | ScaledLinearHistogram::~ScaledLinearHistogram() = default; |
| 945 | |
| 946 | void ScaledLinearHistogram::AddScaledCount(Sample value, int count) { |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 947 | if (histogram_->GetHistogramType() == DUMMY_HISTOGRAM) |
| 948 | return; |
Farah Charab | b114f86 | 2018-06-12 11:53:57 | [diff] [blame] | 949 | if (count == 0) |
| 950 | return; |
| 951 | if (count < 0) { |
| 952 | NOTREACHED(); |
| 953 | return; |
| 954 | } |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 955 | |
| 956 | DCHECK_EQ(histogram_->GetHistogramType(), LINEAR_HISTOGRAM); |
| 957 | LinearHistogram* histogram = static_cast<LinearHistogram*>(histogram_); |
| 958 | const int32_t max_value = static_cast<int32_t>(histogram->bucket_count() - 1); |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 959 | if (value > max_value) |
| 960 | value = max_value; |
| 961 | if (value < 0) |
| 962 | value = 0; |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 963 | |
| 964 | int scaled_count = count / scale_; |
| 965 | subtle::Atomic32 remainder = count - scaled_count * scale_; |
| 966 | |
| 967 | // ScaledLinearHistogram currently requires 1-to-1 mappings between value |
| 968 | // and bucket which alleviates the need to do a bucket lookup here (something |
| 969 | // that is internal to the HistogramSamples object). |
| 970 | if (remainder > 0) { |
| 971 | remainder = |
| 972 | subtle::NoBarrier_AtomicIncrement(&remainders_[value], remainder); |
| 973 | // If remainder passes 1/2 scale, increment main count (thus rounding up). |
| 974 | // The remainder is decremented by the full scale, though, which will |
| 975 | // cause it to go negative and thus requrire another increase by the full |
| 976 | // scale amount before another bump of the scaled count. |
| 977 | if (remainder >= scale_ / 2) { |
| 978 | scaled_count += 1; |
| 979 | subtle::NoBarrier_AtomicIncrement(&remainders_[value], -scale_); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | if (scaled_count > 0) |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 984 | histogram->AddCount(value, scaled_count); |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | //------------------------------------------------------------------------------ |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 988 | // This section provides implementation for BooleanHistogram. |
| 989 | //------------------------------------------------------------------------------ |
| 990 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 991 | class BooleanHistogram::Factory : public Histogram::Factory { |
| 992 | public: |
| 993 | Factory(const std::string& name, int32_t flags) |
| 994 | : Histogram::Factory(name, BOOLEAN_HISTOGRAM, 1, 2, 3, flags) {} |
| 995 | |
| 996 | protected: |
| 997 | BucketRanges* CreateRanges() override { |
| 998 | BucketRanges* ranges = new BucketRanges(3 + 1); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 999 | LinearHistogram::InitializeBucketRanges(1, 2, ranges); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1000 | return ranges; |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 1001 | } |
| 1002 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1003 | std::unique_ptr<HistogramBase> HeapAlloc( |
| 1004 | const BucketRanges* ranges) override { |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 1005 | return WrapUnique(new BooleanHistogram(GetPermanentName(name_), ranges)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | private: |
| 1009 | DISALLOW_COPY_AND_ASSIGN(Factory); |
| 1010 | }; |
| 1011 | |
| 1012 | HistogramBase* BooleanHistogram::FactoryGet(const std::string& name, |
| 1013 | int32_t flags) { |
| 1014 | return Factory(name, flags).Build(); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 1015 | } |
| 1016 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1017 | HistogramBase* BooleanHistogram::FactoryGet(const char* name, int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 1018 | return FactoryGet(std::string(name), flags); |
| 1019 | } |
| 1020 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1021 | std::unique_ptr<HistogramBase> BooleanHistogram::PersistentCreate( |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 1022 | const char* name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1023 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1024 | const DelayedPersistentAllocation& counts, |
| 1025 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 1026 | HistogramSamples::Metadata* meta, |
| 1027 | HistogramSamples::Metadata* logged_meta) { |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1028 | return WrapUnique(new BooleanHistogram(name, ranges, counts, logged_counts, |
| 1029 | meta, logged_meta)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1030 | } |
| 1031 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 1032 | HistogramType BooleanHistogram::GetHistogramType() const { |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 1033 | return BOOLEAN_HISTOGRAM; |
| 1034 | } |
| 1035 | |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 1036 | BooleanHistogram::BooleanHistogram(const char* name, const BucketRanges* ranges) |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 1037 | : LinearHistogram(name, 1, 2, ranges) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1038 | |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1039 | BooleanHistogram::BooleanHistogram( |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 1040 | const char* name, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1041 | const BucketRanges* ranges, |
| 1042 | const DelayedPersistentAllocation& counts, |
| 1043 | const DelayedPersistentAllocation& logged_counts, |
| 1044 | HistogramSamples::Metadata* meta, |
| 1045 | HistogramSamples::Metadata* logged_meta) |
| 1046 | : LinearHistogram(name, |
| 1047 | 1, |
| 1048 | 2, |
| 1049 | ranges, |
| 1050 | counts, |
| 1051 | logged_counts, |
| 1052 | meta, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 1053 | logged_meta) {} |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1054 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1055 | HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1056 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1057 | int flags; |
| 1058 | int declared_min; |
| 1059 | int declared_max; |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 1060 | uint32_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1061 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1062 | |
| 1063 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 1064 | &declared_max, &bucket_count, &range_checksum)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1065 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | HistogramBase* histogram = BooleanHistogram::FactoryGet( |
| 1069 | histogram_name, flags); |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 1070 | if (!histogram) |
| 1071 | return nullptr; |
| 1072 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1073 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 1074 | // The serialized histogram might be corrupted. |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1075 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1076 | } |
| 1077 | return histogram; |
| 1078 | } |
| 1079 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1080 | //------------------------------------------------------------------------------ |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1081 | // CustomHistogram: |
| 1082 | //------------------------------------------------------------------------------ |
| 1083 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1084 | class CustomHistogram::Factory : public Histogram::Factory { |
| 1085 | public: |
| 1086 | Factory(const std::string& name, |
| 1087 | const std::vector<Sample>* custom_ranges, |
| 1088 | int32_t flags) |
| 1089 | : Histogram::Factory(name, CUSTOM_HISTOGRAM, 0, 0, 0, flags) { |
| 1090 | custom_ranges_ = custom_ranges; |
| 1091 | } |
| 1092 | |
| 1093 | protected: |
| 1094 | BucketRanges* CreateRanges() override { |
| 1095 | // Remove the duplicates in the custom ranges array. |
| 1096 | std::vector<int> ranges = *custom_ranges_; |
| 1097 | ranges.push_back(0); // Ensure we have a zero value. |
| 1098 | ranges.push_back(HistogramBase::kSampleType_MAX); |
Anton Bikineev | a61fb57 | 2020-10-18 08:54:44 | [diff] [blame] | 1099 | ranges::sort(ranges); |
| 1100 | ranges.erase(ranges::unique(ranges), ranges.end()); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1101 | |
| 1102 | BucketRanges* bucket_ranges = new BucketRanges(ranges.size()); |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 1103 | for (uint32_t i = 0; i < ranges.size(); i++) { |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1104 | bucket_ranges->set_range(i, ranges[i]); |
| 1105 | } |
| 1106 | bucket_ranges->ResetChecksum(); |
| 1107 | return bucket_ranges; |
| 1108 | } |
| 1109 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1110 | std::unique_ptr<HistogramBase> HeapAlloc( |
| 1111 | const BucketRanges* ranges) override { |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 1112 | return WrapUnique(new CustomHistogram(GetPermanentName(name_), ranges)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | private: |
| 1116 | const std::vector<Sample>* custom_ranges_; |
| 1117 | |
| 1118 | DISALLOW_COPY_AND_ASSIGN(Factory); |
| 1119 | }; |
| 1120 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1121 | HistogramBase* CustomHistogram::FactoryGet( |
| 1122 | const std::string& name, |
| 1123 | const std::vector<Sample>& custom_ranges, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1124 | int32_t flags) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1125 | CHECK(ValidateCustomRanges(custom_ranges)); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1126 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1127 | return Factory(name, &custom_ranges, flags).Build(); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1128 | } |
| 1129 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 1130 | HistogramBase* CustomHistogram::FactoryGet( |
| 1131 | const char* name, |
| 1132 | const std::vector<Sample>& custom_ranges, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1133 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 1134 | return FactoryGet(std::string(name), custom_ranges, flags); |
| 1135 | } |
| 1136 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1137 | std::unique_ptr<HistogramBase> CustomHistogram::PersistentCreate( |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 1138 | const char* name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1139 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1140 | const DelayedPersistentAllocation& counts, |
| 1141 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 1142 | HistogramSamples::Metadata* meta, |
| 1143 | HistogramSamples::Metadata* logged_meta) { |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1144 | return WrapUnique(new CustomHistogram(name, ranges, counts, logged_counts, |
| 1145 | meta, logged_meta)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1146 | } |
| 1147 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 1148 | HistogramType CustomHistogram::GetHistogramType() const { |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 1149 | return CUSTOM_HISTOGRAM; |
| 1150 | } |
| 1151 | |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 1152 | // static |
Ryan Sleevi | 9c79c354 | 2018-05-12 01:38:09 | [diff] [blame] | 1153 | std::vector<Sample> CustomHistogram::ArrayToCustomEnumRanges( |
| 1154 | base::span<const Sample> values) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1155 | std::vector<Sample> all_values; |
Ryan Sleevi | 9c79c354 | 2018-05-12 01:38:09 | [diff] [blame] | 1156 | for (Sample value : values) { |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 1157 | all_values.push_back(value); |
| 1158 | |
| 1159 | // Ensure that a guard bucket is added. If we end up with duplicate |
| 1160 | // values, FactoryGet will take care of removing them. |
| 1161 | all_values.push_back(value + 1); |
| 1162 | } |
| 1163 | return all_values; |
| 1164 | } |
| 1165 | |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 1166 | CustomHistogram::CustomHistogram(const char* name, const BucketRanges* ranges) |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1167 | : Histogram(name, |
| 1168 | ranges->range(1), |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 1169 | ranges->range(ranges->bucket_count() - 1), |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1170 | ranges) {} |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1171 | |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1172 | CustomHistogram::CustomHistogram( |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 1173 | const char* name, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1174 | const BucketRanges* ranges, |
| 1175 | const DelayedPersistentAllocation& counts, |
| 1176 | const DelayedPersistentAllocation& logged_counts, |
| 1177 | HistogramSamples::Metadata* meta, |
| 1178 | HistogramSamples::Metadata* logged_meta) |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1179 | : Histogram(name, |
| 1180 | ranges->range(1), |
| 1181 | ranges->range(ranges->bucket_count() - 1), |
| 1182 | ranges, |
| 1183 | counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 1184 | logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 1185 | meta, |
| 1186 | logged_meta) {} |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1187 | |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 1188 | void CustomHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 1189 | Histogram::SerializeInfoImpl(pickle); |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 1190 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1191 | // Serialize ranges. First and last ranges are alwasy 0 and INT_MAX, so don't |
| 1192 | // write them. |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 1193 | for (uint32_t i = 1; i < bucket_ranges()->bucket_count(); ++i) |
| 1194 | pickle->WriteInt(bucket_ranges()->range(i)); |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 1195 | } |
| 1196 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1197 | // static |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1198 | HistogramBase* CustomHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1199 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1200 | int flags; |
| 1201 | int declared_min; |
| 1202 | int declared_max; |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 1203 | uint32_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1204 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1205 | |
| 1206 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 1207 | &declared_max, &bucket_count, &range_checksum)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1208 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | // First and last ranges are not serialized. |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1212 | std::vector<Sample> sample_ranges(bucket_count - 1); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1213 | |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 1214 | for (uint32_t i = 0; i < sample_ranges.size(); ++i) { |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1215 | if (!iter->ReadInt(&sample_ranges[i])) |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1216 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | HistogramBase* histogram = CustomHistogram::FactoryGet( |
| 1220 | histogram_name, sample_ranges, flags); |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 1221 | if (!histogram) |
| 1222 | return nullptr; |
| 1223 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1224 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 1225 | // The serialized histogram might be corrupted. |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1226 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1227 | } |
| 1228 | return histogram; |
| 1229 | } |
| 1230 | |
| 1231 | // static |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1232 | bool CustomHistogram::ValidateCustomRanges( |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1233 | const std::vector<Sample>& custom_ranges) { |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 1234 | bool has_valid_range = false; |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 1235 | for (uint32_t i = 0; i < custom_ranges.size(); i++) { |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 1236 | Sample sample = custom_ranges[i]; |
| 1237 | if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1238 | return false; |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 1239 | if (sample != 0) |
| 1240 | has_valid_range = true; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1241 | } |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 1242 | return has_valid_range; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1243 | } |
| 1244 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 1245 | } // namespace base |