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