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> |
Gabriel Charette | 8becf3e | 2024-06-07 00:47:20 | [diff] [blame] | 17 | #include <atomic> |
Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame] | 18 | #include <memory> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | #include <string> |
Helmut Januschka | 274c380 | 2024-03-12 23:31:29 | [diff] [blame] | 20 | #include <string_view> |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 21 | #include <utility> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 23 | #include "base/compiler_specific.h" |
| 24 | #include "base/debug/alias.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | #include "base/logging.h" |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 26 | #include "base/memory/ptr_util.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 27 | #include "base/memory/raw_ptr.h" |
Ali Hijazi | a887789 | 2022-11-10 20:51:03 | [diff] [blame] | 28 | #include "base/memory/raw_ref.h" |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 29 | #include "base/metrics/dummy_histogram.h" |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 30 | #include "base/metrics/histogram_functions.h" |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 31 | #include "base/metrics/metrics_hashes.h" |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 32 | #include "base/metrics/persistent_histogram_allocator.h" |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 33 | #include "base/metrics/persistent_memory_allocator.h" |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 34 | #include "base/metrics/sample_vector.h" |
[email protected] | 567d30e | 2012-07-13 21:48:29 | [diff] [blame] | 35 | #include "base/metrics/statistics_recorder.h" |
David Sanders | 83f8ae4 | 2022-04-04 23:15:39 | [diff] [blame] | 36 | #include "base/notreached.h" |
[email protected] | 3f38385 | 2009-04-03 18:18:55 | [diff] [blame] | 37 | #include "base/pickle.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 38 | #include "base/strings/string_util.h" |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame] | 39 | #include "base/strings/utf_string_conversions.h" |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 40 | #include "base/synchronization/lock.h" |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 41 | #include "base/values.h" |
Alexei Svitkine | e73f80a0 | 2017-07-18 00:08:39 | [diff] [blame] | 42 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 44 | namespace base { |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 45 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | bool ReadHistogramArguments(PickleIterator* iter, |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 49 | std::string* histogram_name, |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 50 | int* flags, |
| 51 | int* declared_min, |
| 52 | int* declared_max, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 53 | size_t* bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 54 | uint32_t* range_checksum) { |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 55 | uint32_t bucket_count_u32; |
| 56 | if (!iter->ReadString(histogram_name) || !iter->ReadInt(flags) || |
| 57 | !iter->ReadInt(declared_min) || !iter->ReadInt(declared_max) || |
| 58 | !iter->ReadUInt32(&bucket_count_u32) || |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 59 | !iter->ReadUInt32(range_checksum)) { |
| 60 | DLOG(ERROR) << "Pickle error decoding Histogram: " << *histogram_name; |
| 61 | return false; |
| 62 | } |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 63 | *bucket_count = bucket_count_u32; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 64 | |
| 65 | // Since these fields may have come from an untrusted renderer, do additional |
| 66 | // checks above and beyond those in Histogram::Initialize() |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 67 | if (*declared_max <= 0 || *declared_min <= 0 || |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 68 | *declared_max < *declared_min || |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 69 | INT_MAX / sizeof(HistogramBase::Count32) <= *bucket_count || |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 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. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 87 | if (histogram.GetHistogramType() == DUMMY_HISTOGRAM) { |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 88 | return true; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 89 | } |
| 90 | const Histogram& casted_histogram = static_cast<const Histogram&>(histogram); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 91 | |
| 92 | return casted_histogram.bucket_ranges()->checksum() == range_checksum; |
| 93 | } |
| 94 | |
| 95 | } // namespace |
| 96 | |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 97 | typedef HistogramBase::Count32 Count32; |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 98 | typedef HistogramBase::Sample32 Sample32; |
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: |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 102 | Factory(std::string_view name, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 103 | Sample32 minimum, |
| 104 | Sample32 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: |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 117 | Factory(std::string_view name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 118 | HistogramType histogram_type, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 119 | Sample32 minimum, |
| 120 | Sample32 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) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [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. |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 151 | const std::string_view name_; |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 152 | const HistogramType histogram_type_; |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 153 | Sample32 minimum_; |
| 154 | Sample32 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() { |
Ramon Cano Aparicio | 901ec6c | 2025-04-22 14:30:45 | [diff] [blame] | 160 | uint64_t name_hash = HashMetricName(name_); |
| 161 | HistogramBase* histogram = |
| 162 | StatisticsRecorder::FindHistogram(name_hash, name_); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 163 | if (!histogram) { |
Toshiaki Tanaka | d917f46 | 2021-05-10 17:24:04 | [diff] [blame] | 164 | bool should_record = StatisticsRecorder::ShouldRecordHistogram( |
Ramon Cano Aparicio | 901ec6c | 2025-04-22 14:30:45 | [diff] [blame] | 165 | ParseMetricHashTo32Bits(name_hash)); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 166 | if (!should_record) { |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 167 | return DummyHistogram::GetInstance(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 168 | } |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 169 | // To avoid racy destruction at shutdown, the following will be leaked. |
bcwhite | 4ebd727 | 2016-03-22 19:14:38 | [diff] [blame] | 170 | const BucketRanges* created_ranges = CreateRanges(); |
Brian White | e716f6e | 2018-09-27 21:01:46 | [diff] [blame] | 171 | |
| 172 | const BucketRanges* registered_ranges = |
| 173 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(created_ranges); |
| 174 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 175 | // In most cases, the bucket-count, minimum, and maximum values are known |
| 176 | // when the code is written and so are passed in explicitly. In other |
| 177 | // cases (such as with a CustomHistogram), they are calculated dynamically |
| 178 | // at run-time. In the latter case, those ctor parameters are zero and |
| 179 | // the results extracted from the result of CreateRanges(). |
| 180 | if (bucket_count_ == 0) { |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 181 | bucket_count_ = registered_ranges->bucket_count(); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 182 | minimum_ = registered_ranges->range(1); |
| 183 | maximum_ = registered_ranges->range(bucket_count_ - 1); |
| 184 | } |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 185 | DCHECK_EQ(minimum_, registered_ranges->range(1)); |
| 186 | DCHECK_EQ(maximum_, registered_ranges->range(bucket_count_ - 1)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 187 | |
| 188 | // Try to create the histogram using a "persistent" allocator. As of |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 189 | // 2016-02-25, the availability of such is controlled by a base::Feature |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 190 | // that is off by default. If the allocator doesn't exist or if |
| 191 | // allocating from it fails, code below will allocate the histogram from |
| 192 | // the process heap. |
bcwhite | 4ebd727 | 2016-03-22 19:14:38 | [diff] [blame] | 193 | PersistentHistogramAllocator::Reference histogram_ref = 0; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 194 | std::unique_ptr<HistogramBase> tentative_histogram; |
bcwhite | 5e748c6 | 2016-04-06 02:03:53 | [diff] [blame] | 195 | PersistentHistogramAllocator* allocator = GlobalHistogramAllocator::Get(); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 196 | if (allocator) { |
Ramon Cano Aparicio | 901ec6c | 2025-04-22 14:30:45 | [diff] [blame] | 197 | // TODO(crbug.com/394149163): AllocateHistogram ends up calling |
| 198 | // CreateHistogram, which calls HashMetricName. We already have the hash, |
| 199 | // so we could pass it in. |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 200 | tentative_histogram = allocator->AllocateHistogram( |
Ramon Cano Aparicio | eb4e315 | 2025-05-09 14:26:57 | [diff] [blame] | 201 | histogram_type_, name_, name_hash, minimum_, maximum_, |
| 202 | registered_ranges, flags_, &histogram_ref); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 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. |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 209 | flags_ &= ~HistogramBase::kIsPersistent; |
Ramon Cano Aparicio | 901ec6c | 2025-04-22 14:30:45 | [diff] [blame] | 210 | // TODO(crbug.com/394149163): HeapAlloc creates a new Histogram object, |
| 211 | // which calls HashMetricName. We already have the hash, so we could pass |
| 212 | // it in. We could also store it so we can use it directly in every |
| 213 | // HeapAlloc instead of passing it as a parameter. |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 214 | tentative_histogram = HeapAlloc(registered_ranges); |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 215 | tentative_histogram->SetFlags(flags_); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 216 | } |
| 217 | |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 218 | FillHistogram(tentative_histogram.get()); |
| 219 | |
| 220 | // Register this histogram with the StatisticsRecorder. Keep a copy of |
| 221 | // the pointer value to tell later whether the locally created histogram |
| 222 | // was registered or deleted. The type is "void" because it could point |
| 223 | // to released memory after the following line. |
| 224 | const void* tentative_histogram_ptr = tentative_histogram.get(); |
| 225 | histogram = StatisticsRecorder::RegisterOrDeleteDuplicate( |
| 226 | tentative_histogram.release()); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 227 | |
| 228 | // Persistent histograms need some follow-up processing. |
bcwhite | 4ebd727 | 2016-03-22 19:14:38 | [diff] [blame] | 229 | if (histogram_ref) { |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 230 | allocator->FinalizeHistogram(histogram_ref, |
| 231 | histogram == tentative_histogram_ptr); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Brian White | a958cc7 | 2018-04-19 18:24:16 | [diff] [blame] | 235 | if (histogram_type_ != histogram->GetHistogramType() || |
| 236 | (bucket_count_ != 0 && !histogram->HasConstructionArguments( |
| 237 | minimum_, maximum_, bucket_count_))) { |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 238 | // The construction arguments do not match the existing histogram. This can |
| 239 | // 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] | 240 | // changed one of them, or simply by bad code within Chrome itself. A NULL |
| 241 | // return would cause Chrome to crash; better to just record it for later |
| 242 | // analysis. |
| 243 | UmaHistogramSparse("Histogram.MismatchedConstructionArguments", |
Ramon Cano Aparicio | 901ec6c | 2025-04-22 14:30:45 | [diff] [blame] | 244 | static_cast<Sample32>(name_hash)); |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 245 | DLOG(ERROR) << "Histogram " << name_ |
Brian White | a958cc7 | 2018-04-19 18:24:16 | [diff] [blame] | 246 | << " has mismatched construction arguments"; |
| 247 | return DummyHistogram::GetInstance(); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 248 | } |
| 249 | return histogram; |
| 250 | } |
| 251 | |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 252 | HistogramBase* Histogram::FactoryGet(std::string_view name, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 253 | Sample32 minimum, |
| 254 | Sample32 maximum, |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 255 | size_t bucket_count, |
| 256 | int32_t flags) { |
| 257 | return FactoryGetInternal(name, minimum, maximum, bucket_count, flags); |
| 258 | } |
| 259 | |
| 260 | HistogramBase* Histogram::FactoryTimeGet(std::string_view name, |
| 261 | TimeDelta minimum, |
| 262 | TimeDelta maximum, |
| 263 | size_t bucket_count, |
| 264 | int32_t flags) { |
| 265 | return FactoryTimeGetInternal(name, minimum, maximum, bucket_count, flags); |
| 266 | } |
| 267 | |
| 268 | HistogramBase* Histogram::FactoryMicrosecondsTimeGet(std::string_view name, |
| 269 | TimeDelta minimum, |
| 270 | TimeDelta maximum, |
| 271 | size_t bucket_count, |
| 272 | int32_t flags) { |
| 273 | return FactoryMicrosecondsTimeGetInternal(name, minimum, maximum, |
| 274 | bucket_count, flags); |
| 275 | } |
| 276 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 277 | HistogramBase* Histogram::FactoryGet(const std::string& name, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 278 | Sample32 minimum, |
| 279 | Sample32 maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 280 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 281 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 282 | return FactoryGetInternal(name, minimum, maximum, bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 283 | } |
| 284 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 285 | HistogramBase* Histogram::FactoryTimeGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 286 | TimeDelta minimum, |
| 287 | TimeDelta maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 288 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 289 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 290 | return FactoryTimeGetInternal(name, minimum, maximum, bucket_count, flags); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 291 | } |
| 292 | |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 293 | HistogramBase* Histogram::FactoryMicrosecondsTimeGet(const std::string& name, |
| 294 | TimeDelta minimum, |
| 295 | TimeDelta maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 296 | size_t bucket_count, |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 297 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 298 | return FactoryMicrosecondsTimeGetInternal(name, minimum, maximum, |
| 299 | bucket_count, flags); |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 300 | } |
| 301 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 302 | HistogramBase* Histogram::FactoryGet(const char* name, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 303 | Sample32 minimum, |
| 304 | Sample32 maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 305 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 306 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 307 | return FactoryGetInternal(name, minimum, maximum, bucket_count, flags); |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | HistogramBase* Histogram::FactoryTimeGet(const char* name, |
| 311 | TimeDelta minimum, |
| 312 | TimeDelta maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 313 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 314 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 315 | return FactoryTimeGetInternal(name, minimum, maximum, bucket_count, flags); |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 316 | } |
| 317 | |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 318 | HistogramBase* Histogram::FactoryMicrosecondsTimeGet(const char* name, |
| 319 | TimeDelta minimum, |
| 320 | TimeDelta maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 321 | size_t bucket_count, |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 322 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 323 | return FactoryMicrosecondsTimeGetInternal(name, minimum, maximum, |
| 324 | bucket_count, flags); |
Gabriel Charette | 5ec20588 | 2018-05-22 18:07:31 | [diff] [blame] | 325 | } |
| 326 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 327 | std::unique_ptr<HistogramBase> Histogram::PersistentCreate( |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 328 | DurableStringView durable_name, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 329 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 330 | const DelayedPersistentAllocation& counts, |
| 331 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 332 | HistogramSamples::Metadata* meta, |
| 333 | HistogramSamples::Metadata* logged_meta) { |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 334 | return WrapUnique(new Histogram(durable_name, ranges, counts, logged_counts, |
| 335 | meta, logged_meta)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 336 | } |
| 337 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 338 | // Calculate what range of values are held in each bucket. |
| 339 | // We have to be careful that we don't pick a ratio between starting points in |
| 340 | // consecutive buckets that is sooo small, that the integer bounds are the same |
| 341 | // (effectively making one bucket get no values). We need to avoid: |
| 342 | // ranges(i) == ranges(i + 1) |
| 343 | // To avoid that, we just do a fine-grained bucket width as far as we need to |
| 344 | // until we get a ratio that moves us along at least 2 units at a time. From |
| 345 | // that bucket onward we do use the exponential growth of buckets. |
| 346 | // |
| 347 | // static |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 348 | void Histogram::InitializeBucketRanges(Sample32 minimum, |
| 349 | Sample32 maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 350 | BucketRanges* ranges) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 351 | double log_max = log(static_cast<double>(maximum)); |
| 352 | double log_ratio; |
| 353 | double log_next; |
| 354 | size_t bucket_index = 1; |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 355 | Sample32 current = minimum; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 356 | ranges->set_range(bucket_index, current); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 357 | size_t bucket_count = ranges->bucket_count(); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 358 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 359 | while (bucket_count > ++bucket_index) { |
| 360 | double log_current; |
| 361 | log_current = log(static_cast<double>(current)); |
Brian White | 2aaa8b01a | 2018-11-13 23:24:56 | [diff] [blame] | 362 | debug::Alias(&log_current); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 363 | // Calculate the count'th root of the range. |
| 364 | log_ratio = (log_max - log_current) / (bucket_count - bucket_index); |
| 365 | // See where the next bucket would start. |
| 366 | log_next = log_current + log_ratio; |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 367 | Sample32 next; |
Peter Kasting | 8c3adfb | 2017-09-13 08:07:39 | [diff] [blame] | 368 | next = static_cast<int>(std::round(exp(log_next))); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 369 | if (next > current) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 370 | current = next; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 371 | } else { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 372 | ++current; // Just do a narrow bucket, and keep trying. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 373 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 374 | ranges->set_range(bucket_index, current); |
| 375 | } |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 376 | ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 377 | ranges->ResetChecksum(); |
| 378 | } |
| 379 | |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 380 | // static |
| 381 | const int Histogram::kCommonRaceBasedCountMismatch = 5; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 382 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 383 | uint32_t Histogram::FindCorruption(const HistogramSamples& samples) const { |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 384 | uint32_t inconsistencies = NO_INCONSISTENCIES; |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 385 | Sample32 previous_range = -1; // Bottom range is always 0. |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 386 | for (size_t index = 0; index < bucket_count(); ++index) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 387 | int new_range = ranges(index); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 388 | if (previous_range >= new_range) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 389 | inconsistencies |= BUCKET_ORDER_ERROR; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 390 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 391 | previous_range = new_range; |
| 392 | } |
| 393 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 394 | if (!bucket_ranges()->HasValidChecksum()) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 395 | inconsistencies |= RANGE_CHECKSUM_ERROR; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 396 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 397 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 398 | int64_t delta64 = samples.redundant_count() - samples.TotalCount(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 399 | if (delta64 != 0) { |
| 400 | int delta = static_cast<int>(delta64); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 401 | if (delta != delta64) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 402 | delta = INT_MAX; // Flag all giant errors as INT_MAX. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 403 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 404 | if (delta > 0) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 405 | if (delta > kCommonRaceBasedCountMismatch) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 406 | inconsistencies |= COUNT_HIGH_ERROR; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 407 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 408 | } else { |
| 409 | DCHECK_GT(0, delta); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 410 | if (-delta > kCommonRaceBasedCountMismatch) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 411 | inconsistencies |= COUNT_LOW_ERROR; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 412 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 413 | } |
| 414 | } |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 415 | return inconsistencies; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 416 | } |
| 417 | |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 418 | const BucketRanges* Histogram::bucket_ranges() const { |
| 419 | return unlogged_samples_->bucket_ranges(); |
| 420 | } |
| 421 | |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 422 | Sample32 Histogram::declared_min() const { |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 423 | const BucketRanges* ranges = bucket_ranges(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 424 | if (ranges->bucket_count() < 2) { |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 425 | return -1; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 426 | } |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 427 | return ranges->range(1); |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 428 | } |
| 429 | |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 430 | Sample32 Histogram::declared_max() const { |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 431 | const BucketRanges* ranges = bucket_ranges(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 432 | if (ranges->bucket_count() < 2) { |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 433 | return -1; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 434 | } |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 435 | return ranges->range(ranges->bucket_count() - 1); |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 436 | } |
| 437 | |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 438 | Sample32 Histogram::ranges(size_t i) const { |
Brian White | 32052d5b | 2017-08-07 16:46:57 | [diff] [blame] | 439 | return bucket_ranges()->range(i); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 440 | } |
| 441 | |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 442 | size_t Histogram::bucket_count() const { |
| 443 | return bucket_ranges()->bucket_count(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 444 | } |
| 445 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 446 | // static |
Helmut Januschka | 274c380 | 2024-03-12 23:31:29 | [diff] [blame] | 447 | bool Histogram::InspectConstructionArguments(std::string_view name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 448 | Sample32* minimum, |
| 449 | Sample32* maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 450 | size_t* bucket_count) { |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 451 | bool check_okay = true; |
| 452 | |
| 453 | // Checks below must be done after any min/max swap. |
| 454 | if (*minimum > *maximum) { |
Brian White | a254ab84 | 2022-02-16 16:34:53 | [diff] [blame] | 455 | DLOG(ERROR) << "Histogram: " << name << " has swapped minimum/maximum"; |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 456 | check_okay = false; |
| 457 | std::swap(*minimum, *maximum); |
| 458 | } |
| 459 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 460 | // Defensive code for backward compatibility. |
| 461 | if (*minimum < 1) { |
Alison Gale | 59c007a | 2024-04-20 03:05:40 | [diff] [blame] | 462 | // TODO(crbug.com/40211696): Temporarily disabled during cleanup. |
Brian White | 9c8bb64 | 2022-02-22 18:19:12 | [diff] [blame] | 463 | // DLOG(ERROR) << "Histogram: " << name << " has bad minimum: " << *minimum; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 464 | *minimum = 1; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 465 | if (*maximum < 1) { |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 466 | *maximum = 1; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 467 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 468 | } |
| 469 | if (*maximum >= kSampleType_MAX) { |
Brian White | a254ab84 | 2022-02-16 16:34:53 | [diff] [blame] | 470 | DLOG(ERROR) << "Histogram: " << name << " has bad maximum: " << *maximum; |
[email protected] | a5c7bd79 | 2012-08-02 00:29:04 | [diff] [blame] | 471 | *maximum = kSampleType_MAX - 1; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 472 | } |
Brian White | abfa270 | 2019-04-03 17:00:54 | [diff] [blame] | 473 | if (*bucket_count > kBucketCount_MAX) { |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 474 | UmaHistogramSparse("Histogram.TooManyBuckets.1000", |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 475 | static_cast<Sample32>(HashMetricName(name))); |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 476 | |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 477 | // Blink.UseCounter legitimately has more than 1000 entries in its enum. |
Alexei Svitkine | d67a443 | 2020-10-13 22:31:21 | [diff] [blame] | 478 | if (!StartsWith(name, "Blink.UseCounter")) { |
Brian White | a254ab84 | 2022-02-16 16:34:53 | [diff] [blame] | 479 | DLOG(ERROR) << "Histogram: " << name |
| 480 | << " has bad bucket_count: " << *bucket_count << " (limit " |
| 481 | << kBucketCount_MAX << ")"; |
Brian White | abfa270 | 2019-04-03 17:00:54 | [diff] [blame] | 482 | |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 483 | // Assume it's a mistake and limit to 100 buckets, plus under and over. |
| 484 | // If the DCHECK doesn't alert the user then hopefully the small number |
| 485 | // will be obvious on the dashboard. If not, then it probably wasn't |
| 486 | // important. |
| 487 | *bucket_count = 102; |
| 488 | check_okay = false; |
| 489 | } |
bcwhite | aaf2d345 | 2017-04-26 19:17:47 | [diff] [blame] | 490 | } |
Brian White | 7cfaffb1 | 2018-11-15 16:51:31 | [diff] [blame] | 491 | |
| 492 | // Ensure parameters are sane. |
bcwhite | aaf2d345 | 2017-04-26 19:17:47 | [diff] [blame] | 493 | if (*maximum == *minimum) { |
| 494 | check_okay = false; |
| 495 | *maximum = *minimum + 1; |
| 496 | } |
| 497 | if (*bucket_count < 3) { |
| 498 | check_okay = false; |
| 499 | *bucket_count = 3; |
| 500 | } |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 501 | // The swap at the top of the function guarantees this cast is safe. |
| 502 | const size_t max_buckets = static_cast<size_t>(*maximum - *minimum + 2); |
| 503 | if (*bucket_count > max_buckets) { |
bcwhite | aaf2d345 | 2017-04-26 19:17:47 | [diff] [blame] | 504 | check_okay = false; |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 505 | *bucket_count = max_buckets; |
bcwhite | aaf2d345 | 2017-04-26 19:17:47 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | if (!check_okay) { |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 509 | UmaHistogramSparse("Histogram.BadConstructionArguments", |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 510 | static_cast<Sample32>(HashMetricName(name))); |
bcwhite | aaf2d345 | 2017-04-26 19:17:47 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | return check_okay; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 514 | } |
| 515 | |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 516 | uint64_t Histogram::name_hash() const { |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 517 | return unlogged_samples_->id(); |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 518 | } |
| 519 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 520 | HistogramType Histogram::GetHistogramType() const { |
| 521 | return HISTOGRAM; |
| 522 | } |
| 523 | |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 524 | bool Histogram::HasConstructionArguments(Sample32 expected_minimum, |
| 525 | Sample32 expected_maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 526 | size_t expected_bucket_count) const { |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 527 | return (expected_bucket_count == bucket_count() && |
| 528 | expected_minimum == declared_min() && |
| 529 | expected_maximum == declared_max()); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void Histogram::Add(int value) { |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 533 | AddCount(value, 1); |
| 534 | } |
| 535 | |
| 536 | void Histogram::AddCount(int value, int count) { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 537 | DCHECK_EQ(0, ranges(0)); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 538 | DCHECK_EQ(kSampleType_MAX, ranges(bucket_count())); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 539 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 540 | if (value > kSampleType_MAX - 1) { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 541 | value = kSampleType_MAX - 1; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 542 | } |
| 543 | if (value < 0) { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 544 | value = 0; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 545 | } |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 546 | if (count <= 0) { |
Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 547 | NOTREACHED(); |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 548 | } |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 549 | unlogged_samples_->Accumulate(value, count); |
simonhatch | df5a814 | 2015-07-15 22:22:57 | [diff] [blame] | 550 | |
Peter Kasting | fa48899 | 2024-08-06 07:48:14 | [diff] [blame] | 551 | if (StatisticsRecorder::have_active_callbacks()) [[unlikely]] { |
Shakti Sahu | e0e07a0e | 2021-06-06 00:07:52 | [diff] [blame] | 552 | FindAndRunCallbacks(value); |
Peter Kasting | fa48899 | 2024-08-06 07:48:14 | [diff] [blame] | 553 | } |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 554 | } |
| 555 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 556 | std::unique_ptr<HistogramSamples> Histogram::SnapshotSamples() const { |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 557 | return SnapshotAllSamples(); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 558 | } |
| 559 | |
Luc Nguyen | e01c675 | 2022-12-01 18:40:15 | [diff] [blame] | 560 | std::unique_ptr<HistogramSamples> Histogram::SnapshotUnloggedSamples() const { |
| 561 | return SnapshotUnloggedSamplesImpl(); |
| 562 | } |
| 563 | |
| 564 | void Histogram::MarkSamplesAsLogged(const HistogramSamples& samples) { |
| 565 | // |final_delta_created_| only exists when DCHECK is on. |
| 566 | #if DCHECK_IS_ON() |
| 567 | DCHECK(!final_delta_created_); |
| 568 | #endif |
| 569 | |
| 570 | unlogged_samples_->Subtract(samples); |
| 571 | logged_samples_->Add(samples); |
| 572 | } |
| 573 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 574 | std::unique_ptr<HistogramSamples> Histogram::SnapshotDelta() { |
Luc Nguyen | e01c675 | 2022-12-01 18:40:15 | [diff] [blame] | 575 | // |final_delta_created_| only exists when DCHECK is on. |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 576 | #if DCHECK_IS_ON() |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 577 | DCHECK(!final_delta_created_); |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 578 | #endif |
| 579 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 580 | // The code below has subtle thread-safety guarantees! All changes to |
| 581 | // the underlying SampleVectors use atomic integer operations, which guarantee |
| 582 | // eventual consistency, but do not guarantee full synchronization between |
| 583 | // different entries in the SampleVector. In particular, this means that |
| 584 | // concurrent updates to the histogram might result in the reported sum not |
| 585 | // matching the individual bucket counts; or there being some buckets that are |
| 586 | // logically updated "together", but end up being only partially updated when |
| 587 | // a snapshot is captured. Note that this is why it's important to subtract |
| 588 | // exactly the snapshotted unlogged samples, rather than simply resetting the |
| 589 | // vector: this way, the next snapshot will include any concurrent updates |
| 590 | // missed by the current snapshot. |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 591 | |
Luc Nguyen | 257ecb0 | 2023-04-11 21:09:41 | [diff] [blame] | 592 | std::unique_ptr<HistogramSamples> snapshot = |
| 593 | std::make_unique<SampleVector>(unlogged_samples_->id(), bucket_ranges()); |
| 594 | snapshot->Extract(*unlogged_samples_); |
| 595 | logged_samples_->Add(*snapshot); |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 596 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 597 | return snapshot; |
| 598 | } |
| 599 | |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 600 | std::unique_ptr<HistogramSamples> Histogram::SnapshotFinalDelta() const { |
Luc Nguyen | e01c675 | 2022-12-01 18:40:15 | [diff] [blame] | 601 | // |final_delta_created_| only exists when DCHECK is on. |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 602 | #if DCHECK_IS_ON() |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 603 | DCHECK(!final_delta_created_); |
| 604 | final_delta_created_ = true; |
bcwhite | f1dec48 | 2017-07-06 14:39:11 | [diff] [blame] | 605 | #endif |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 606 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 607 | return SnapshotUnloggedSamples(); |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 608 | } |
| 609 | |
Alexei Svitkine | 1e86b0c89 | 2024-10-02 21:02:03 | [diff] [blame] | 610 | bool Histogram::AddSamples(const HistogramSamples& samples) { |
| 611 | return unlogged_samples_->Add(samples); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | bool Histogram::AddSamplesFromPickle(PickleIterator* iter) { |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 615 | return unlogged_samples_->AddFromPickle(iter); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 616 | } |
| 617 | |
Nathan Memmott | c034fa4e | 2022-07-15 23:59:43 | [diff] [blame] | 618 | base::Value::Dict Histogram::ToGraphDict() const { |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame] | 619 | std::unique_ptr<SampleVector> snapshot = SnapshotAllSamples(); |
Quang Minh Tuan Nguyen | 39b1fed | 2021-03-16 00:49:38 | [diff] [blame] | 620 | return snapshot->ToGraphDict(histogram_name(), flags()); |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame] | 621 | } |
| 622 | |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 623 | void Histogram::SerializeInfoImpl(Pickle* pickle) const { |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 624 | DCHECK(bucket_ranges()->HasValidChecksum()); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 625 | pickle->WriteString(histogram_name()); |
| 626 | pickle->WriteInt(flags()); |
| 627 | pickle->WriteInt(declared_min()); |
| 628 | pickle->WriteInt(declared_max()); |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 629 | // Limited to kBucketCount_MAX, which fits in a uint32_t. |
| 630 | pickle->WriteUInt32(static_cast<uint32_t>(bucket_count())); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 631 | pickle->WriteUInt32(bucket_ranges()->checksum()); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 632 | } |
| 633 | |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 634 | Histogram::Histogram(DurableStringView durable_name, const BucketRanges* ranges) |
| 635 | : HistogramBase(durable_name) { |
| 636 | DCHECK(ranges) << histogram_name(); |
Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame] | 637 | unlogged_samples_ = |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 638 | std::make_unique<SampleVector>(HashMetricName(histogram_name()), ranges); |
Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame] | 639 | logged_samples_ = |
| 640 | std::make_unique<SampleVector>(unlogged_samples_->id(), ranges); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 641 | } |
| 642 | |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 643 | Histogram::Histogram(DurableStringView durable_name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 644 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 645 | const DelayedPersistentAllocation& counts, |
| 646 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 647 | HistogramSamples::Metadata* meta, |
| 648 | HistogramSamples::Metadata* logged_meta) |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 649 | : HistogramBase(durable_name) { |
Roger McFarlane | d9048a0 | 2025-04-14 16:22:01 | [diff] [blame] | 650 | const auto name = histogram_name(); |
| 651 | const auto id = HashMetricName(name); |
| 652 | DCHECK(ranges) << name; |
| 653 | unlogged_samples_ = |
| 654 | std::make_unique<PersistentSampleVector>(name, id, ranges, meta, counts); |
Peter Boström | fd85123 | 2021-03-31 17:05:33 | [diff] [blame] | 655 | logged_samples_ = std::make_unique<PersistentSampleVector>( |
Roger McFarlane | d9048a0 | 2025-04-14 16:22:01 | [diff] [blame] | 656 | name, id, ranges, logged_meta, logged_counts); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 657 | } |
| 658 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 659 | Histogram::~Histogram() = default; |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 660 | |
Tom Sepez | e9a541c5 | 2024-10-23 22:47:51 | [diff] [blame] | 661 | std::string Histogram::GetAsciiBucketRange(size_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; |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 674 | size_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); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 685 | if (!histogram) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 686 | return nullptr; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 687 | } |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 688 | |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 689 | // The serialized histogram might be corrupted. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 690 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 691 | return nullptr; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 692 | } |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 693 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 694 | return histogram; |
| 695 | } |
| 696 | |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 697 | // static |
| 698 | HistogramBase* Histogram::FactoryGetInternal(std::string_view name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 699 | Sample32 minimum, |
| 700 | Sample32 maximum, |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 701 | size_t bucket_count, |
| 702 | int32_t flags) { |
| 703 | bool valid_arguments = |
| 704 | InspectConstructionArguments(name, &minimum, &maximum, &bucket_count); |
| 705 | DCHECK(valid_arguments) << name; |
| 706 | if (!valid_arguments) { |
| 707 | DLOG(ERROR) << "Histogram " << name << " dropped for invalid parameters."; |
| 708 | return DummyHistogram::GetInstance(); |
| 709 | } |
| 710 | |
| 711 | return Factory(name, minimum, maximum, bucket_count, flags).Build(); |
| 712 | } |
| 713 | |
| 714 | // static |
| 715 | HistogramBase* Histogram::FactoryTimeGetInternal(std::string_view name, |
| 716 | TimeDelta minimum, |
| 717 | TimeDelta maximum, |
| 718 | size_t bucket_count, |
| 719 | int32_t flags) { |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 720 | DCHECK_LT(minimum.InMilliseconds(), std::numeric_limits<Sample32>::max()); |
| 721 | DCHECK_LT(maximum.InMilliseconds(), std::numeric_limits<Sample32>::max()); |
| 722 | return FactoryGetInternal(name, static_cast<Sample32>(minimum.InMilliseconds()), |
| 723 | static_cast<Sample32>(maximum.InMilliseconds()), |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 724 | bucket_count, flags); |
| 725 | } |
| 726 | |
| 727 | // static |
| 728 | HistogramBase* Histogram::FactoryMicrosecondsTimeGetInternal( |
| 729 | std::string_view name, |
| 730 | TimeDelta minimum, |
| 731 | TimeDelta maximum, |
| 732 | size_t bucket_count, |
| 733 | int32_t flags) { |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 734 | DCHECK_LT(minimum.InMicroseconds(), std::numeric_limits<Sample32>::max()); |
| 735 | DCHECK_LT(maximum.InMicroseconds(), std::numeric_limits<Sample32>::max()); |
| 736 | return FactoryGetInternal(name, static_cast<Sample32>(minimum.InMicroseconds()), |
| 737 | static_cast<Sample32>(maximum.InMicroseconds()), |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 738 | bucket_count, flags); |
| 739 | } |
| 740 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 741 | std::unique_ptr<SampleVector> Histogram::SnapshotAllSamples() const { |
Luc Nguyen | e01c675 | 2022-12-01 18:40:15 | [diff] [blame] | 742 | std::unique_ptr<SampleVector> samples = SnapshotUnloggedSamplesImpl(); |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 743 | samples->Add(*logged_samples_); |
| 744 | return samples; |
| 745 | } |
| 746 | |
Luc Nguyen | e01c675 | 2022-12-01 18:40:15 | [diff] [blame] | 747 | std::unique_ptr<SampleVector> Histogram::SnapshotUnloggedSamplesImpl() const { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 748 | std::unique_ptr<SampleVector> samples( |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 749 | new SampleVector(unlogged_samples_->id(), bucket_ranges())); |
| 750 | samples->Add(*unlogged_samples_); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 751 | return samples; |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 752 | } |
| 753 | |
Nathan Memmott | c034fa4e | 2022-07-15 23:59:43 | [diff] [blame] | 754 | Value::Dict Histogram::GetParameters() const { |
| 755 | Value::Dict params; |
| 756 | params.Set("type", HistogramTypeToString(GetHistogramType())); |
| 757 | params.Set("min", declared_min()); |
| 758 | params.Set("max", declared_max()); |
| 759 | params.Set("bucket_count", static_cast<int>(bucket_count())); |
Sylvain Defresne | 0348ecfd | 2021-06-23 08:27:34 | [diff] [blame] | 760 | return params; |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 761 | } |
| 762 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 763 | //------------------------------------------------------------------------------ |
| 764 | // LinearHistogram: This histogram uses a traditional set of evenly spaced |
| 765 | // buckets. |
| 766 | //------------------------------------------------------------------------------ |
| 767 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 768 | class LinearHistogram::Factory : public Histogram::Factory { |
| 769 | public: |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 770 | Factory(std::string_view name, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 771 | Sample32 minimum, |
| 772 | Sample32 maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 773 | size_t bucket_count, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 774 | int32_t flags, |
| 775 | const DescriptionPair* descriptions) |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 776 | : Histogram::Factory(name, |
| 777 | LINEAR_HISTOGRAM, |
| 778 | minimum, |
| 779 | maximum, |
| 780 | bucket_count, |
| 781 | flags) { |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 782 | descriptions_ = descriptions; |
| 783 | } |
| 784 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 785 | Factory(const Factory&) = delete; |
| 786 | Factory& operator=(const Factory&) = delete; |
| 787 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 788 | protected: |
| 789 | BucketRanges* CreateRanges() override { |
| 790 | BucketRanges* ranges = new BucketRanges(bucket_count_ + 1); |
| 791 | LinearHistogram::InitializeBucketRanges(minimum_, maximum_, ranges); |
| 792 | return ranges; |
| 793 | } |
| 794 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 795 | std::unique_ptr<HistogramBase> HeapAlloc( |
| 796 | const BucketRanges* ranges) override { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 797 | return WrapUnique(new LinearHistogram(GetPermanentName(name_), ranges)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | void FillHistogram(HistogramBase* base_histogram) override { |
| 801 | Histogram::Factory::FillHistogram(base_histogram); |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 802 | // Normally, |base_histogram| should have type LINEAR_HISTOGRAM or be |
| 803 | // inherited from it. However, if it's expired, it will actually be a |
| 804 | // DUMMY_HISTOGRAM. Skip filling in that case. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 805 | if (base_histogram->GetHistogramType() == DUMMY_HISTOGRAM) { |
Gayane Petrosyan | 5745ac6 | 2018-03-23 01:45:24 | [diff] [blame] | 806 | return; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 807 | } |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 808 | LinearHistogram* histogram = static_cast<LinearHistogram*>(base_histogram); |
| 809 | // Set range descriptions. |
| 810 | if (descriptions_) { |
| 811 | for (int i = 0; descriptions_[i].description; ++i) { |
| 812 | histogram->bucket_description_[descriptions_[i].sample] = |
| 813 | descriptions_[i].description; |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | private: |
Tom Sepez | d83ed1b | 2023-09-19 00:10:00 | [diff] [blame] | 819 | raw_ptr<const DescriptionPair, AllowPtrArithmetic> descriptions_; |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 820 | }; |
| 821 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 822 | LinearHistogram::~LinearHistogram() = default; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 823 | |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 824 | HistogramBase* LinearHistogram::FactoryGet(std::string_view name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 825 | Sample32 minimum, |
| 826 | Sample32 maximum, |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 827 | size_t bucket_count, |
| 828 | int32_t flags) { |
| 829 | return FactoryGetInternal(name, minimum, maximum, bucket_count, flags); |
| 830 | } |
| 831 | |
| 832 | HistogramBase* LinearHistogram::FactoryTimeGet(std::string_view name, |
| 833 | TimeDelta minimum, |
| 834 | TimeDelta maximum, |
| 835 | size_t bucket_count, |
| 836 | int32_t flags) { |
| 837 | return FactoryTimeGetInternal(name, minimum, maximum, bucket_count, flags); |
| 838 | } |
| 839 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 840 | HistogramBase* LinearHistogram::FactoryGet(const std::string& name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 841 | Sample32 minimum, |
| 842 | Sample32 maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 843 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 844 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 845 | return FactoryGetInternal(name, minimum, maximum, bucket_count, flags); |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 846 | } |
| 847 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 848 | HistogramBase* LinearHistogram::FactoryTimeGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 849 | TimeDelta minimum, |
| 850 | TimeDelta maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 851 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 852 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 853 | return FactoryTimeGetInternal(name, minimum, maximum, bucket_count, flags); |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 854 | } |
| 855 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 856 | HistogramBase* LinearHistogram::FactoryGet(const char* name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 857 | Sample32 minimum, |
| 858 | Sample32 maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 859 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 860 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 861 | return FactoryGetInternal(name, minimum, maximum, bucket_count, flags); |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | HistogramBase* LinearHistogram::FactoryTimeGet(const char* name, |
| 865 | TimeDelta minimum, |
| 866 | TimeDelta maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 867 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 868 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 869 | return FactoryTimeGetInternal(name, minimum, maximum, bucket_count, flags); |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 870 | } |
| 871 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 872 | std::unique_ptr<HistogramBase> LinearHistogram::PersistentCreate( |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 873 | DurableStringView durable_name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 874 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 875 | const DelayedPersistentAllocation& counts, |
| 876 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 877 | HistogramSamples::Metadata* meta, |
| 878 | HistogramSamples::Metadata* logged_meta) { |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 879 | return WrapUnique(new LinearHistogram(durable_name, ranges, counts, |
| 880 | logged_counts, meta, logged_meta)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 881 | } |
| 882 | |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 883 | HistogramBase* LinearHistogram::FactoryGetWithRangeDescription( |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 884 | std::string_view name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 885 | Sample32 minimum, |
| 886 | Sample32 maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 887 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 888 | int32_t flags, |
| 889 | const DescriptionPair descriptions[]) { |
Brian White | 564cedf | 2018-11-15 20:26:00 | [diff] [blame] | 890 | // Originally, histograms were required to have at least one sample value |
| 891 | // plus underflow and overflow buckets. For single-entry enumerations, |
| 892 | // that one value is usually zero (which IS the underflow bucket) |
| 893 | // resulting in a |maximum| value of 1 (the exclusive upper-bound) and only |
| 894 | // the two outlier buckets. Handle this by making max==2 and buckets==3. |
| 895 | // This usually won't have any cost since the single-value-optimization |
| 896 | // will be used until the count exceeds 16 bits. |
| 897 | if (maximum == 1 && bucket_count == 2) { |
| 898 | maximum = 2; |
| 899 | bucket_count = 3; |
| 900 | } |
| 901 | |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 902 | bool valid_arguments = Histogram::InspectConstructionArguments( |
| 903 | name, &minimum, &maximum, &bucket_count); |
Brian White | 226244e | 2018-12-20 23:11:39 | [diff] [blame] | 904 | DCHECK(valid_arguments) << name; |
Brian White | a254ab84 | 2022-02-16 16:34:53 | [diff] [blame] | 905 | if (!valid_arguments) { |
| 906 | DLOG(ERROR) << "Histogram " << name << " dropped for invalid parameters."; |
| 907 | return DummyHistogram::GetInstance(); |
| 908 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 909 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 910 | return Factory(name, minimum, maximum, bucket_count, flags, descriptions) |
| 911 | .Build(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 912 | } |
| 913 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 914 | HistogramType LinearHistogram::GetHistogramType() const { |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 915 | return LINEAR_HISTOGRAM; |
| 916 | } |
| 917 | |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 918 | LinearHistogram::LinearHistogram(DurableStringView durable_name, |
| 919 | const BucketRanges* ranges) |
| 920 | : Histogram(durable_name, ranges) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 921 | |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 922 | LinearHistogram::LinearHistogram( |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 923 | DurableStringView durable_name, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 924 | const BucketRanges* ranges, |
| 925 | const DelayedPersistentAllocation& counts, |
| 926 | const DelayedPersistentAllocation& logged_counts, |
| 927 | HistogramSamples::Metadata* meta, |
| 928 | HistogramSamples::Metadata* logged_meta) |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 929 | : Histogram(durable_name, |
| 930 | ranges, |
| 931 | counts, |
| 932 | logged_counts, |
| 933 | meta, |
| 934 | logged_meta) {} |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 935 | |
Tom Sepez | e9a541c5 | 2024-10-23 22:47:51 | [diff] [blame] | 936 | std::string LinearHistogram::GetAsciiBucketRange(size_t i) const { |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 937 | int range = ranges(i); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 938 | BucketDescriptionMap::const_iterator it = bucket_description_.find(range); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 939 | if (it == bucket_description_.end()) { |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 940 | return Histogram::GetAsciiBucketRange(i); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 941 | } |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 942 | return it->second; |
| 943 | } |
| 944 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 945 | // static |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 946 | void LinearHistogram::InitializeBucketRanges(Sample32 minimum, |
| 947 | Sample32 maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 948 | BucketRanges* ranges) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 949 | double min = minimum; |
| 950 | double max = maximum; |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 951 | size_t bucket_count = ranges->bucket_count(); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 952 | |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 953 | for (size_t i = 1; i < bucket_count; ++i) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 954 | double linear_range = |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 955 | (min * (bucket_count - 1 - i) + max * (i - 1)) / (bucket_count - 2); |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 956 | auto range = static_cast<Sample32>(linear_range + 0.5); |
Brian White | 61b84b2 | 2018-10-05 18:03:18 | [diff] [blame] | 957 | ranges->set_range(i, range); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 958 | } |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 959 | ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 960 | ranges->ResetChecksum(); |
| 961 | } |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 962 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 963 | // static |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 964 | HistogramBase* LinearHistogram::FactoryGetInternal(std::string_view name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 965 | Sample32 minimum, |
| 966 | Sample32 maximum, |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 967 | size_t bucket_count, |
| 968 | int32_t flags) { |
| 969 | return FactoryGetWithRangeDescription(name, minimum, maximum, bucket_count, |
| 970 | flags, nullptr); |
| 971 | } |
| 972 | |
| 973 | // static |
| 974 | HistogramBase* LinearHistogram::FactoryTimeGetInternal(std::string_view name, |
| 975 | TimeDelta minimum, |
| 976 | TimeDelta maximum, |
| 977 | size_t bucket_count, |
| 978 | int32_t flags) { |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 979 | DCHECK_LT(minimum.InMilliseconds(), std::numeric_limits<Sample32>::max()); |
| 980 | DCHECK_LT(maximum.InMilliseconds(), std::numeric_limits<Sample32>::max()); |
| 981 | return FactoryGetInternal(name, static_cast<Sample32>(minimum.InMilliseconds()), |
| 982 | static_cast<Sample32>(maximum.InMilliseconds()), |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 983 | bucket_count, flags); |
| 984 | } |
| 985 | |
| 986 | // static |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 987 | HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 988 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 989 | int flags; |
| 990 | int declared_min; |
| 991 | int declared_max; |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 992 | size_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 993 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 994 | |
| 995 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 996 | &declared_max, &bucket_count, &range_checksum)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 997 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | HistogramBase* histogram = LinearHistogram::FactoryGet( |
| 1001 | histogram_name, declared_min, declared_max, bucket_count, flags); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1002 | if (!histogram) { |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 1003 | return nullptr; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1004 | } |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 1005 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1006 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 1007 | // The serialized histogram might be corrupted. |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1008 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1009 | } |
| 1010 | return histogram; |
| 1011 | } |
| 1012 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1013 | //------------------------------------------------------------------------------ |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1014 | // ScaledLinearHistogram: This is a wrapper around a LinearHistogram that |
| 1015 | // scales input counts. |
| 1016 | //------------------------------------------------------------------------------ |
| 1017 | |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 1018 | ScaledLinearHistogram::ScaledLinearHistogram(std::string_view name, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 1019 | Sample32 minimum, |
| 1020 | Sample32 maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 1021 | size_t bucket_count, |
Eric Seckler | 21a69f4 | 2020-07-10 14:18:17 | [diff] [blame] | 1022 | int32_t scale, |
| 1023 | int32_t flags) |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 1024 | : histogram_(LinearHistogram::FactoryGet(name, |
| 1025 | minimum, |
| 1026 | maximum, |
| 1027 | bucket_count, |
| 1028 | flags)), |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1029 | scale_(scale) { |
| 1030 | DCHECK(histogram_); |
| 1031 | DCHECK_LT(1, scale); |
| 1032 | DCHECK_EQ(1, minimum); |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 1033 | CHECK_EQ(static_cast<Sample32>(bucket_count), maximum - minimum + 2) |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1034 | << " ScaledLinearHistogram requires buckets of size 1"; |
| 1035 | |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 1036 | // Normally, |histogram_| should have type LINEAR_HISTOGRAM or be |
| 1037 | // inherited from it. However, if it's expired, it will be DUMMY_HISTOGRAM. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1038 | if (histogram_->GetHistogramType() == DUMMY_HISTOGRAM) { |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 1039 | return; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1040 | } |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 1041 | |
| 1042 | DCHECK_EQ(histogram_->GetHistogramType(), LINEAR_HISTOGRAM); |
| 1043 | LinearHistogram* histogram = static_cast<LinearHistogram*>(histogram_); |
| 1044 | remainders_.resize(histogram->bucket_count(), 0); |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1045 | } |
| 1046 | |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 1047 | ScaledLinearHistogram::ScaledLinearHistogram(const std::string& name, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 1048 | Sample32 minimum, |
| 1049 | Sample32 maximum, |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 1050 | size_t bucket_count, |
| 1051 | int32_t scale, |
| 1052 | int32_t flags) |
| 1053 | : ScaledLinearHistogram(std::string_view(name), |
| 1054 | minimum, |
| 1055 | maximum, |
| 1056 | bucket_count, |
| 1057 | scale, |
| 1058 | flags) {} |
| 1059 | |
| 1060 | ScaledLinearHistogram::ScaledLinearHistogram(const char* name, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 1061 | Sample32 minimum, |
| 1062 | Sample32 maximum, |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 1063 | size_t bucket_count, |
| 1064 | int32_t scale, |
| 1065 | int32_t flags) |
| 1066 | : ScaledLinearHistogram(std::string_view(name), |
| 1067 | minimum, |
| 1068 | maximum, |
| 1069 | bucket_count, |
| 1070 | scale, |
| 1071 | flags) {} |
| 1072 | |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1073 | ScaledLinearHistogram::~ScaledLinearHistogram() = default; |
| 1074 | |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 1075 | void ScaledLinearHistogram::AddScaledCount(Sample32 value, int64_t count) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1076 | if (histogram_->GetHistogramType() == DUMMY_HISTOGRAM) { |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 1077 | return; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1078 | } |
| 1079 | if (count == 0) { |
Farah Charab | b114f86 | 2018-06-12 11:53:57 | [diff] [blame] | 1080 | return; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1081 | } |
Farah Charab | b114f86 | 2018-06-12 11:53:57 | [diff] [blame] | 1082 | if (count < 0) { |
Peter Boström | cb0d5306 | 2024-06-04 18:45:31 | [diff] [blame] | 1083 | DUMP_WILL_BE_NOTREACHED(); |
Farah Charab | b114f86 | 2018-06-12 11:53:57 | [diff] [blame] | 1084 | return; |
| 1085 | } |
Joshua Berenhaus | 9c88c1a2 | 2020-07-16 16:37:34 | [diff] [blame] | 1086 | |
| 1087 | DCHECK_EQ(histogram_->GetHistogramType(), LINEAR_HISTOGRAM); |
| 1088 | LinearHistogram* histogram = static_cast<LinearHistogram*>(histogram_); |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 1089 | const auto max_value = static_cast<Sample32>(histogram->bucket_count() - 1); |
Ho Cheung | 3f166bc | 2023-05-08 16:07:47 | [diff] [blame] | 1090 | value = std::clamp(value, 0, max_value); |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1091 | |
Mikhail Khokhlov | aa89605 | 2023-01-25 11:41:48 | [diff] [blame] | 1092 | int64_t scaled_count = count / scale_; |
| 1093 | subtle::Atomic32 remainder = static_cast<int>(count - scaled_count * scale_); |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1094 | |
| 1095 | // ScaledLinearHistogram currently requires 1-to-1 mappings between value |
| 1096 | // and bucket which alleviates the need to do a bucket lookup here (something |
| 1097 | // that is internal to the HistogramSamples object). |
| 1098 | if (remainder > 0) { |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 1099 | remainder = subtle::NoBarrier_AtomicIncrement( |
| 1100 | &remainders_[static_cast<size_t>(value)], remainder); |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1101 | // If remainder passes 1/2 scale, increment main count (thus rounding up). |
| 1102 | // The remainder is decremented by the full scale, though, which will |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 1103 | // cause it to go negative and thus require another increase by the full |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1104 | // scale amount before another bump of the scaled count. |
| 1105 | if (remainder >= scale_ / 2) { |
| 1106 | scaled_count += 1; |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 1107 | subtle::NoBarrier_AtomicIncrement( |
| 1108 | &remainders_[static_cast<size_t>(value)], -scale_); |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Mikhail Khokhlov | aa89605 | 2023-01-25 11:41:48 | [diff] [blame] | 1112 | if (scaled_count > 0) { |
wuguobin | 0914ace | 2024-12-20 10:50:45 | [diff] [blame] | 1113 | int capped_scaled_count = scaled_count > std::numeric_limits<int>::max() |
| 1114 | ? std::numeric_limits<int>::max() |
| 1115 | : static_cast<int>(scaled_count); |
| 1116 | histogram->AddCount(value, capped_scaled_count); |
Mikhail Khokhlov | aa89605 | 2023-01-25 11:41:48 | [diff] [blame] | 1117 | } |
Brian White | fffb358 | 2018-05-24 21:17:01 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | //------------------------------------------------------------------------------ |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 1121 | // This section provides implementation for BooleanHistogram. |
| 1122 | //------------------------------------------------------------------------------ |
| 1123 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1124 | class BooleanHistogram::Factory : public Histogram::Factory { |
| 1125 | public: |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1126 | Factory(std::string_view name, int32_t flags) |
| 1127 | : Histogram::Factory(name, BOOLEAN_HISTOGRAM, 1, 2, 3, flags) {} |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1128 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 1129 | Factory(const Factory&) = delete; |
| 1130 | Factory& operator=(const Factory&) = delete; |
| 1131 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1132 | protected: |
| 1133 | BucketRanges* CreateRanges() override { |
| 1134 | BucketRanges* ranges = new BucketRanges(3 + 1); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 1135 | LinearHistogram::InitializeBucketRanges(1, 2, ranges); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1136 | return ranges; |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 1137 | } |
| 1138 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1139 | std::unique_ptr<HistogramBase> HeapAlloc( |
| 1140 | const BucketRanges* ranges) override { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1141 | return WrapUnique(new BooleanHistogram(GetPermanentName(name_), ranges)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1142 | } |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1143 | }; |
| 1144 | |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 1145 | HistogramBase* BooleanHistogram::FactoryGet(std::string_view name, |
| 1146 | int32_t flags) { |
| 1147 | return FactoryGetInternal(name, flags); |
| 1148 | } |
| 1149 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1150 | HistogramBase* BooleanHistogram::FactoryGet(const std::string& name, |
| 1151 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1152 | return FactoryGetInternal(name, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 1153 | } |
| 1154 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1155 | HistogramBase* BooleanHistogram::FactoryGet(const char* name, int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1156 | return FactoryGetInternal(name, flags); |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 1157 | } |
| 1158 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1159 | std::unique_ptr<HistogramBase> BooleanHistogram::PersistentCreate( |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1160 | DurableStringView durable_name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1161 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1162 | const DelayedPersistentAllocation& counts, |
| 1163 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 1164 | HistogramSamples::Metadata* meta, |
| 1165 | HistogramSamples::Metadata* logged_meta) { |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1166 | return WrapUnique(new BooleanHistogram(durable_name, ranges, counts, |
| 1167 | logged_counts, meta, logged_meta)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1168 | } |
| 1169 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 1170 | HistogramType BooleanHistogram::GetHistogramType() const { |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 1171 | return BOOLEAN_HISTOGRAM; |
| 1172 | } |
| 1173 | |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1174 | // static |
| 1175 | HistogramBase* BooleanHistogram::FactoryGetInternal(std::string_view name, |
| 1176 | int32_t flags) { |
| 1177 | return Factory(name, flags).Build(); |
| 1178 | } |
| 1179 | |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1180 | BooleanHistogram::BooleanHistogram(DurableStringView durable_name, |
| 1181 | const BucketRanges* ranges) |
| 1182 | : LinearHistogram(durable_name, ranges) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1183 | |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1184 | BooleanHistogram::BooleanHistogram( |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1185 | DurableStringView durable_name, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1186 | const BucketRanges* ranges, |
| 1187 | const DelayedPersistentAllocation& counts, |
| 1188 | const DelayedPersistentAllocation& logged_counts, |
| 1189 | HistogramSamples::Metadata* meta, |
| 1190 | HistogramSamples::Metadata* logged_meta) |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1191 | : LinearHistogram(durable_name, |
| 1192 | ranges, |
| 1193 | counts, |
| 1194 | logged_counts, |
| 1195 | meta, |
| 1196 | logged_meta) {} |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1197 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1198 | HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1199 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1200 | int flags; |
| 1201 | int declared_min; |
| 1202 | int declared_max; |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 1203 | size_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1204 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1205 | |
| 1206 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 1207 | &declared_max, &bucket_count, &range_checksum)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1208 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1209 | } |
| 1210 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1211 | HistogramBase* histogram = |
| 1212 | BooleanHistogram::FactoryGet(histogram_name, flags); |
| 1213 | if (!histogram) { |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 1214 | return nullptr; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1215 | } |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 1216 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1217 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 1218 | // The serialized histogram might be corrupted. |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1219 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1220 | } |
| 1221 | return histogram; |
| 1222 | } |
| 1223 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1224 | //------------------------------------------------------------------------------ |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1225 | // CustomHistogram: |
| 1226 | //------------------------------------------------------------------------------ |
| 1227 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1228 | class CustomHistogram::Factory : public Histogram::Factory { |
| 1229 | public: |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1230 | Factory(std::string_view name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1231 | const std::vector<Sample32>* custom_ranges, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1232 | int32_t flags) |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1233 | : Histogram::Factory(name, CUSTOM_HISTOGRAM, 0, 0, 0, flags) { |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1234 | custom_ranges_ = custom_ranges; |
| 1235 | } |
| 1236 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 1237 | Factory(const Factory&) = delete; |
| 1238 | Factory& operator=(const Factory&) = delete; |
| 1239 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1240 | protected: |
| 1241 | BucketRanges* CreateRanges() override { |
| 1242 | // Remove the duplicates in the custom ranges array. |
| 1243 | std::vector<int> ranges = *custom_ranges_; |
| 1244 | ranges.push_back(0); // Ensure we have a zero value. |
| 1245 | ranges.push_back(HistogramBase::kSampleType_MAX); |
Peter Kasting | 025a9425 | 2025-01-29 21:28:37 | [diff] [blame] | 1246 | std::ranges::sort(ranges); |
Peter Kasting | bd9dde5c | 2025-01-08 12:48:22 | [diff] [blame] | 1247 | auto removed = std::ranges::unique(ranges); |
| 1248 | ranges.erase(removed.begin(), removed.end()); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1249 | |
| 1250 | BucketRanges* bucket_ranges = new BucketRanges(ranges.size()); |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 1251 | for (size_t i = 0; i < ranges.size(); i++) { |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1252 | bucket_ranges->set_range(i, ranges[i]); |
| 1253 | } |
| 1254 | bucket_ranges->ResetChecksum(); |
| 1255 | return bucket_ranges; |
| 1256 | } |
| 1257 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1258 | std::unique_ptr<HistogramBase> HeapAlloc( |
| 1259 | const BucketRanges* ranges) override { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1260 | return WrapUnique(new CustomHistogram(GetPermanentName(name_), ranges)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | private: |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1264 | raw_ptr<const std::vector<Sample32>> custom_ranges_; |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1265 | }; |
| 1266 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1267 | HistogramBase* CustomHistogram::FactoryGet( |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 1268 | std::string_view name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1269 | const std::vector<Sample32>& custom_ranges, |
Alex Turner | bb5cb56e | 2024-08-12 17:12:42 | [diff] [blame] | 1270 | int32_t flags) { |
| 1271 | return FactoryGetInternal(name, custom_ranges, flags); |
| 1272 | } |
| 1273 | |
| 1274 | HistogramBase* CustomHistogram::FactoryGet( |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1275 | const std::string& name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1276 | const std::vector<Sample32>& custom_ranges, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1277 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1278 | return FactoryGetInternal(name, custom_ranges, flags); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1279 | } |
| 1280 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 1281 | HistogramBase* CustomHistogram::FactoryGet( |
| 1282 | const char* name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1283 | const std::vector<Sample32>& custom_ranges, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1284 | int32_t flags) { |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1285 | return FactoryGetInternal(name, custom_ranges, flags); |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 1286 | } |
| 1287 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1288 | std::unique_ptr<HistogramBase> CustomHistogram::PersistentCreate( |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1289 | DurableStringView durable_name, |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1290 | const BucketRanges* ranges, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1291 | const DelayedPersistentAllocation& counts, |
| 1292 | const DelayedPersistentAllocation& logged_counts, |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 1293 | HistogramSamples::Metadata* meta, |
| 1294 | HistogramSamples::Metadata* logged_meta) { |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1295 | return WrapUnique(new CustomHistogram(durable_name, ranges, counts, |
| 1296 | logged_counts, meta, logged_meta)); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1297 | } |
| 1298 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 1299 | HistogramType CustomHistogram::GetHistogramType() const { |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 1300 | return CUSTOM_HISTOGRAM; |
| 1301 | } |
| 1302 | |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 1303 | // static |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 1304 | std::vector<Sample32> CustomHistogram::ArrayToCustomEnumRanges( |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1305 | base::span<const Sample32> values) { |
| 1306 | std::vector<Sample32> all_values; |
| 1307 | for (Sample32 value : values) { |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 1308 | all_values.push_back(value); |
| 1309 | |
| 1310 | // Ensure that a guard bucket is added. If we end up with duplicate |
| 1311 | // values, FactoryGet will take care of removing them. |
| 1312 | all_values.push_back(value + 1); |
| 1313 | } |
| 1314 | return all_values; |
| 1315 | } |
| 1316 | |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1317 | CustomHistogram::CustomHistogram(DurableStringView durable_name, |
| 1318 | const BucketRanges* ranges) |
| 1319 | : Histogram(durable_name, ranges) {} |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1320 | |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1321 | CustomHistogram::CustomHistogram( |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1322 | DurableStringView durable_name, |
bcwhite | fa8485b | 2017-05-01 16:43:25 | [diff] [blame] | 1323 | const BucketRanges* ranges, |
| 1324 | const DelayedPersistentAllocation& counts, |
| 1325 | const DelayedPersistentAllocation& logged_counts, |
| 1326 | HistogramSamples::Metadata* meta, |
| 1327 | HistogramSamples::Metadata* logged_meta) |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 1328 | : Histogram(durable_name, |
| 1329 | ranges, |
| 1330 | counts, |
| 1331 | logged_counts, |
| 1332 | meta, |
| 1333 | logged_meta) {} |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1334 | |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 1335 | void CustomHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 1336 | Histogram::SerializeInfoImpl(pickle); |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 1337 | |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 1338 | // Serialize ranges. First and last ranges are always 0 and INT_MAX, so don't |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1339 | // write them. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1340 | for (size_t i = 1; i < bucket_ranges()->bucket_count(); ++i) { |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 1341 | pickle->WriteInt(bucket_ranges()->range(i)); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1342 | } |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 1343 | } |
| 1344 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1345 | // static |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1346 | HistogramBase* CustomHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 1347 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1348 | int flags; |
| 1349 | int declared_min; |
| 1350 | int declared_max; |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 1351 | size_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 1352 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1353 | |
| 1354 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 1355 | &declared_max, &bucket_count, &range_checksum)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1356 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | // First and last ranges are not serialized. |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1360 | std::vector<Sample32> sample_ranges(bucket_count - 1); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1361 | |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1362 | for (Sample32& sample : sample_ranges) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1363 | if (!iter->ReadInt(&sample)) { |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1364 | return nullptr; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1365 | } |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1366 | } |
| 1367 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1368 | HistogramBase* histogram = |
| 1369 | CustomHistogram::FactoryGet(histogram_name, sample_ranges, flags); |
| 1370 | if (!histogram) { |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 1371 | return nullptr; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1372 | } |
Brian White | 82027ff5d | 2017-08-21 19:50:22 | [diff] [blame] | 1373 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1374 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 1375 | // The serialized histogram might be corrupted. |
Brian White | 7eb9148 | 2017-08-09 19:54:45 | [diff] [blame] | 1376 | return nullptr; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 1377 | } |
| 1378 | return histogram; |
| 1379 | } |
| 1380 | |
| 1381 | // static |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1382 | HistogramBase* CustomHistogram::FactoryGetInternal( |
| 1383 | std::string_view name, |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1384 | const std::vector<Sample32>& custom_ranges, |
Alex Ilin | 84f845b | 2024-01-15 19:51:04 | [diff] [blame] | 1385 | int32_t flags) { |
| 1386 | CHECK(ValidateCustomRanges(custom_ranges)); |
| 1387 | |
| 1388 | return Factory(name, &custom_ranges, flags).Build(); |
| 1389 | } |
| 1390 | |
| 1391 | // static |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1392 | bool CustomHistogram::ValidateCustomRanges( |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1393 | const std::vector<Sample32>& custom_ranges) { |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 1394 | bool has_valid_range = false; |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 1395 | for (Sample32 sample : custom_ranges) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1396 | if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1397 | return false; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1398 | } |
| 1399 | if (sample != 0) { |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 1400 | has_valid_range = true; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 1401 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1402 | } |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 1403 | return has_valid_range; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 1404 | } |
| 1405 | |
Gabriel Charette | 8becf3e | 2024-06-07 00:47:20 | [diff] [blame] | 1406 | namespace internal { |
| 1407 | |
| 1408 | namespace { |
| 1409 | // The pointer to the atomic const-pointer also needs to be atomic as some |
| 1410 | // threads might already be alive when it's set. It requires acquire-release |
| 1411 | // semantics to ensure the memory it points to is seen in its initialized state. |
| 1412 | constinit std::atomic<const std::atomic<TimeTicks>*> g_last_foreground_time_ref; |
| 1413 | } // namespace |
| 1414 | |
| 1415 | void SetSharedLastForegroundTimeForMetrics( |
| 1416 | const std::atomic<TimeTicks>* last_foreground_time_ref) { |
| 1417 | g_last_foreground_time_ref.store(last_foreground_time_ref, |
| 1418 | std::memory_order_release); |
| 1419 | } |
| 1420 | |
Joe Mason | 0c9a90a | 2024-09-25 16:41:38 | [diff] [blame] | 1421 | const std::atomic<TimeTicks>* |
| 1422 | GetSharedLastForegroundTimeForMetricsForTesting() { |
| 1423 | return g_last_foreground_time_ref.load(std::memory_order_acquire); |
| 1424 | } |
| 1425 | |
Gabriel Charette | 8becf3e | 2024-06-07 00:47:20 | [diff] [blame] | 1426 | bool OverlapsBestEffortRange(TimeTicks sample_time, TimeDelta sample_interval) { |
| 1427 | // std::memory_order_acquire semantics required as documented above to make |
| 1428 | // sure the memory pointed to by the stored `const std::atomic<TimeTicks>*` |
| 1429 | // is initialized from this thread's POV. |
| 1430 | auto last_foreground_time_ref = |
| 1431 | g_last_foreground_time_ref.load(std::memory_order_acquire); |
| 1432 | if (!last_foreground_time_ref) { |
| 1433 | return false; |
| 1434 | } |
| 1435 | |
| 1436 | // std::memory_order_relaxed is sufficient here as we care about the stored |
| 1437 | // TimeTicks value but don't assume the state of any other shared memory based |
| 1438 | // on the result. |
| 1439 | auto last_foreground_time = |
| 1440 | last_foreground_time_ref->load(std::memory_order_relaxed); |
| 1441 | // `last_foreground_time.is_null()` indicates we're currently under |
| 1442 | // best-effort priority and thus assume overlap. Otherwise we compare whether |
| 1443 | // the range of interest is fully contained within the last time this process |
| 1444 | // was running at a foreground priority. |
| 1445 | return last_foreground_time.is_null() || |
| 1446 | (sample_time - sample_interval) < last_foreground_time; |
| 1447 | } |
| 1448 | |
| 1449 | } // namespace internal |
| 1450 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 1451 | } // namespace base |