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