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