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