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