blob: 9c3645d9f7743feba32615f610a01801c3beb704 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
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]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
initial.commitd7cae122008-07-26 21:49:3811
Alexei Svitkinee73f80a02017-07-18 00:08:3912#include <inttypes.h>
avi9b6f42932015-12-26 22:15:1413#include <limits.h>
initial.commitd7cae122008-07-26 21:49:3814#include <math.h>
[email protected]f1633932010-08-17 23:05:2815
Ho Cheung3f166bc2023-05-08 16:07:4716#include <algorithm>
Peter Boströmfd851232021-03-31 17:05:3317#include <memory>
initial.commitd7cae122008-07-26 21:49:3818#include <string>
jdoerrief1e72e32017-04-26 16:23:5519#include <utility>
initial.commitd7cae122008-07-26 21:49:3820
[email protected]ec0c0aa2012-08-14 02:02:0021#include "base/compiler_specific.h"
22#include "base/debug/alias.h"
initial.commitd7cae122008-07-26 21:49:3823#include "base/logging.h"
dcheng093de9b2016-04-04 21:25:5124#include "base/memory/ptr_util.h"
Keishi Hattori0e45c022021-11-27 09:25:5225#include "base/memory/raw_ptr.h"
Ali Hijazia8877892022-11-10 20:51:0326#include "base/memory/raw_ref.h"
Gayane Petrosyan5745ac62018-03-23 01:45:2427#include "base/metrics/dummy_histogram.h"
Ilya Sherman16d5d5f42017-12-08 00:32:4428#include "base/metrics/histogram_functions.h"
bcwhiteb036e4322015-12-10 18:36:3429#include "base/metrics/metrics_hashes.h"
bcwhite33d95806a2016-03-16 02:37:4530#include "base/metrics/persistent_histogram_allocator.h"
bcwhite5cb99eb2016-02-01 21:07:5631#include "base/metrics/persistent_memory_allocator.h"
[email protected]877ef562012-10-20 02:56:1832#include "base/metrics/sample_vector.h"
[email protected]567d30e2012-07-13 21:48:2933#include "base/metrics/statistics_recorder.h"
David Sanders83f8ae42022-04-04 23:15:3934#include "base/notreached.h"
[email protected]3f383852009-04-03 18:18:5535#include "base/pickle.h"
Anton Bikineeva61fb572020-10-18 08:54:4436#include "base/ranges/algorithm.h"
[email protected]d529cb02013-06-10 19:06:5737#include "base/strings/string_util.h"
Jun Kokatsu505af9f2020-05-05 11:59:4738#include "base/strings/utf_string_conversions.h"
[email protected]bc581a682011-01-01 23:16:2039#include "base/synchronization/lock.h"
[email protected]24a7ec52012-10-08 10:31:5040#include "base/values.h"
Alexei Svitkinee73f80a02017-07-18 00:08:3941#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3842
[email protected]835d7c82010-10-14 04:38:3843namespace base {
[email protected]e1acf6f2008-10-27 20:43:3344
[email protected]c50c21d2013-01-11 21:52:4445namespace {
46
47bool ReadHistogramArguments(PickleIterator* iter,
asvitkine24d3e9a2015-05-27 05:22:1448 std::string* histogram_name,
[email protected]c50c21d2013-01-11 21:52:4449 int* flags,
50 int* declared_min,
51 int* declared_max,
Peter Kastingfc94f5062022-06-08 16:41:4552 size_t* bucket_count,
avi9b6f42932015-12-26 22:15:1453 uint32_t* range_checksum) {
Peter Kastingfc94f5062022-06-08 16:41:4554 uint32_t bucket_count_u32;
55 if (!iter->ReadString(histogram_name) || !iter->ReadInt(flags) ||
56 !iter->ReadInt(declared_min) || !iter->ReadInt(declared_max) ||
57 !iter->ReadUInt32(&bucket_count_u32) ||
[email protected]c50c21d2013-01-11 21:52:4458 !iter->ReadUInt32(range_checksum)) {
59 DLOG(ERROR) << "Pickle error decoding Histogram: " << *histogram_name;
60 return false;
61 }
Peter Kastingfc94f5062022-06-08 16:41:4562 *bucket_count = bucket_count_u32;
[email protected]c50c21d2013-01-11 21:52:4463
64 // Since these fields may have come from an untrusted renderer, do additional
65 // checks above and beyond those in Histogram::Initialize()
66 if (*declared_max <= 0 ||
67 *declared_min <= 0 ||
68 *declared_max < *declared_min ||
69 INT_MAX / sizeof(HistogramBase::Count) <= *bucket_count ||
70 *bucket_count < 2) {
71 DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name;
72 return false;
73 }
74
75 // We use the arguments to find or create the local version of the histogram
bcwhite05dc0922016-06-03 04:59:4476 // in this process, so we need to clear any IPC flag.
[email protected]c50c21d2013-01-11 21:52:4477 *flags &= ~HistogramBase::kIPCSerializationSourceFlag;
78
79 return true;
80}
81
82bool ValidateRangeChecksum(const HistogramBase& histogram,
avi9b6f42932015-12-26 22:15:1483 uint32_t range_checksum) {
Gayane Petrosyan5745ac62018-03-23 01:45:2484 // Normally, |histogram| should have type HISTOGRAM or be inherited from it.
85 // However, if it's expired, it will actually be a DUMMY_HISTOGRAM.
86 // Skip the checks in that case.
87 if (histogram.GetHistogramType() == DUMMY_HISTOGRAM)
88 return true;
[email protected]c50c21d2013-01-11 21:52:4489 const Histogram& casted_histogram =
90 static_cast<const Histogram&>(histogram);
91
92 return casted_histogram.bucket_ranges()->checksum() == range_checksum;
93}
94
95} // namespace
96
[email protected]34d062322012-08-01 21:34:0897typedef HistogramBase::Count Count;
98typedef HistogramBase::Sample Sample;
initial.commitd7cae122008-07-26 21:49:3899
bcwhite5cb99eb2016-02-01 21:07:56100class Histogram::Factory {
101 public:
102 Factory(const std::string& name,
103 HistogramBase::Sample minimum,
104 HistogramBase::Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45105 size_t bucket_count,
bcwhite5cb99eb2016-02-01 21:07:56106 int32_t flags)
Peter Kastingfc94f5062022-06-08 16:41:45107 : Factory(name, HISTOGRAM, minimum, maximum, bucket_count, flags) {}
bcwhite5cb99eb2016-02-01 21:07:56108
Peter Boström75cd3c02021-09-28 15:23:18109 Factory(const Factory&) = delete;
110 Factory& operator=(const Factory&) = delete;
111
bcwhite5cb99eb2016-02-01 21:07:56112 // Create histogram based on construction parameters. Caller takes
113 // ownership of the returned object.
114 HistogramBase* Build();
115
116 protected:
117 Factory(const std::string& name,
118 HistogramType histogram_type,
119 HistogramBase::Sample minimum,
120 HistogramBase::Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45121 size_t bucket_count,
bcwhite5cb99eb2016-02-01 21:07:56122 int32_t flags)
Peter Kastingfc94f5062022-06-08 16:41:45123 : name_(name),
124 histogram_type_(histogram_type),
125 minimum_(minimum),
126 maximum_(maximum),
127 bucket_count_(bucket_count),
128 flags_(flags) {}
bcwhite5cb99eb2016-02-01 21:07:56129
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).
dcheng093de9b2016-04-04 21:25:51139 virtual std::unique_ptr<HistogramBase> HeapAlloc(const BucketRanges* ranges) {
Ali Hijazia8877892022-11-10 20:51:03140 return WrapUnique(new Histogram(GetPermanentName(*name_), ranges));
bcwhite5cb99eb2016-02-01 21:07:56141 }
142
143 // Perform any required datafill on the just-created histogram. If
bcwhite3dd85c4f2016-03-17 13:21:56144 // overridden, be sure to call the "super" version -- this method may not
145 // always remain empty.
146 virtual void FillHistogram(HistogramBase* histogram) {}
bcwhite5cb99eb2016-02-01 21:07:56147
148 // These values are protected (instead of private) because they need to
149 // be accessible to methods of sub-classes in order to avoid passing
150 // unnecessary parameters everywhere.
Ali Hijazia8877892022-11-10 20:51:03151 const raw_ref<const std::string> name_;
bcwhite5cb99eb2016-02-01 21:07:56152 const HistogramType histogram_type_;
153 HistogramBase::Sample minimum_;
154 HistogramBase::Sample maximum_;
Peter Kastingfc94f5062022-06-08 16:41:45155 size_t bucket_count_;
bcwhite5cb99eb2016-02-01 21:07:56156 int32_t flags_;
bcwhite5cb99eb2016-02-01 21:07:56157};
158
159HistogramBase* Histogram::Factory::Build() {
Ali Hijazia8877892022-11-10 20:51:03160 HistogramBase* histogram = StatisticsRecorder::FindHistogram(*name_);
bcwhite5cb99eb2016-02-01 21:07:56161 if (!histogram) {
Gayane Petrosyan5745ac62018-03-23 01:45:24162 // constructor. Refactor code to avoid the additional call.
Toshiaki Tanakad917f462021-05-10 17:24:04163 bool should_record = StatisticsRecorder::ShouldRecordHistogram(
Ali Hijazia8877892022-11-10 20:51:03164 HashMetricNameAs32Bits(*name_));
Gayane Petrosyan5745ac62018-03-23 01:45:24165 if (!should_record)
166 return DummyHistogram::GetInstance();
bcwhite5cb99eb2016-02-01 21:07:56167 // To avoid racy destruction at shutdown, the following will be leaked.
bcwhite4ebd7272016-03-22 19:14:38168 const BucketRanges* created_ranges = CreateRanges();
Brian Whitee716f6e2018-09-27 21:01:46169
170 const BucketRanges* registered_ranges =
171 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(created_ranges);
172
bcwhite5cb99eb2016-02-01 21:07:56173 // In most cases, the bucket-count, minimum, and maximum values are known
174 // when the code is written and so are passed in explicitly. In other
175 // cases (such as with a CustomHistogram), they are calculated dynamically
176 // at run-time. In the latter case, those ctor parameters are zero and
177 // the results extracted from the result of CreateRanges().
178 if (bucket_count_ == 0) {
Peter Kastingfc94f5062022-06-08 16:41:45179 bucket_count_ = registered_ranges->bucket_count();
bcwhite5cb99eb2016-02-01 21:07:56180 minimum_ = registered_ranges->range(1);
181 maximum_ = registered_ranges->range(bucket_count_ - 1);
182 }
bcwhitef1dec482017-07-06 14:39:11183 DCHECK_EQ(minimum_, registered_ranges->range(1));
184 DCHECK_EQ(maximum_, registered_ranges->range(bucket_count_ - 1));
bcwhite5cb99eb2016-02-01 21:07:56185
186 // Try to create the histogram using a "persistent" allocator. As of
bcwhite3dd85c4f2016-03-17 13:21:56187 // 2016-02-25, the availability of such is controlled by a base::Feature
bcwhite5cb99eb2016-02-01 21:07:56188 // that is off by default. If the allocator doesn't exist or if
189 // allocating from it fails, code below will allocate the histogram from
190 // the process heap.
bcwhite4ebd7272016-03-22 19:14:38191 PersistentHistogramAllocator::Reference histogram_ref = 0;
dcheng093de9b2016-04-04 21:25:51192 std::unique_ptr<HistogramBase> tentative_histogram;
bcwhite5e748c62016-04-06 02:03:53193 PersistentHistogramAllocator* allocator = GlobalHistogramAllocator::Get();
bcwhite5cb99eb2016-02-01 21:07:56194 if (allocator) {
bcwhite33d95806a2016-03-16 02:37:45195 tentative_histogram = allocator->AllocateHistogram(
Ali Hijazia8877892022-11-10 20:51:03196 histogram_type_, *name_, minimum_, maximum_, registered_ranges,
197 flags_, &histogram_ref);
bcwhite5cb99eb2016-02-01 21:07:56198 }
199
200 // Handle the case where no persistent allocator is present or the
201 // persistent allocation fails (perhaps because it is full).
202 if (!tentative_histogram) {
bcwhite4ebd7272016-03-22 19:14:38203 DCHECK(!histogram_ref); // Should never have been set.
bcwhite5cb99eb2016-02-01 21:07:56204 flags_ &= ~HistogramBase::kIsPersistent;
205 tentative_histogram = HeapAlloc(registered_ranges);
bcwhite3dd85c4f2016-03-17 13:21:56206 tentative_histogram->SetFlags(flags_);
bcwhite5cb99eb2016-02-01 21:07:56207 }
208
bcwhite33d95806a2016-03-16 02:37:45209 FillHistogram(tentative_histogram.get());
210
211 // Register this histogram with the StatisticsRecorder. Keep a copy of
212 // the pointer value to tell later whether the locally created histogram
213 // was registered or deleted. The type is "void" because it could point
214 // to released memory after the following line.
215 const void* tentative_histogram_ptr = tentative_histogram.get();
216 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate(
217 tentative_histogram.release());
bcwhite5cb99eb2016-02-01 21:07:56218
219 // Persistent histograms need some follow-up processing.
bcwhite4ebd7272016-03-22 19:14:38220 if (histogram_ref) {
bcwhite33d95806a2016-03-16 02:37:45221 allocator->FinalizeHistogram(histogram_ref,
222 histogram == tentative_histogram_ptr);
bcwhite5cb99eb2016-02-01 21:07:56223 }
224 }
225
Brian Whitea958cc72018-04-19 18:24:16226 if (histogram_type_ != histogram->GetHistogramType() ||
227 (bucket_count_ != 0 && !histogram->HasConstructionArguments(
228 minimum_, maximum_, bucket_count_))) {
bcwhite5cb99eb2016-02-01 21:07:56229 // The construction arguments do not match the existing histogram. This can
230 // come about if an extension updates in the middle of a chrome run and has
Brian Whitea958cc72018-04-19 18:24:16231 // changed one of them, or simply by bad code within Chrome itself. A NULL
232 // return would cause Chrome to crash; better to just record it for later
233 // analysis.
234 UmaHistogramSparse("Histogram.MismatchedConstructionArguments",
Ali Hijazia8877892022-11-10 20:51:03235 static_cast<Sample>(HashMetricName(*name_)));
236 DLOG(ERROR) << "Histogram " << *name_
Brian Whitea958cc72018-04-19 18:24:16237 << " has mismatched construction arguments";
238 return DummyHistogram::GetInstance();
bcwhite5cb99eb2016-02-01 21:07:56239 }
240 return histogram;
241}
242
asvitkine24d3e9a2015-05-27 05:22:14243HistogramBase* Histogram::FactoryGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17244 Sample minimum,
245 Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45246 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14247 int32_t flags) {
[email protected]e184be902012-08-07 04:49:24248 bool valid_arguments =
249 InspectConstructionArguments(name, &minimum, &maximum, &bucket_count);
Brian White226244e2018-12-20 23:11:39250 DCHECK(valid_arguments) << name;
Brian Whitea254ab842022-02-16 16:34:53251 if (!valid_arguments) {
252 DLOG(ERROR) << "Histogram " << name << " dropped for invalid parameters.";
253 return DummyHistogram::GetInstance();
254 }
[email protected]a93721e22012-01-06 02:13:28255
bcwhite5cb99eb2016-02-01 21:07:56256 return Factory(name, minimum, maximum, bucket_count, flags).Build();
[email protected]e8829a192009-12-06 00:09:37257}
258
asvitkine24d3e9a2015-05-27 05:22:14259HistogramBase* Histogram::FactoryTimeGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17260 TimeDelta minimum,
261 TimeDelta maximum,
Peter Kastingfc94f5062022-06-08 16:41:45262 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14263 int32_t flags) {
Jan Krcal4e553f422019-04-08 12:52:46264 DCHECK_LT(minimum.InMilliseconds(), std::numeric_limits<Sample>::max());
265 DCHECK_LT(maximum.InMilliseconds(), std::numeric_limits<Sample>::max());
pkasting9cf9b94a2014-10-01 22:18:43266 return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
267 static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
268 flags);
[email protected]34d062322012-08-01 21:34:08269}
270
Gabriel Charette5ec205882018-05-22 18:07:31271HistogramBase* Histogram::FactoryMicrosecondsTimeGet(const std::string& name,
272 TimeDelta minimum,
273 TimeDelta maximum,
Peter Kastingfc94f5062022-06-08 16:41:45274 size_t bucket_count,
Gabriel Charette5ec205882018-05-22 18:07:31275 int32_t flags) {
Jan Krcal4e553f422019-04-08 12:52:46276 DCHECK_LT(minimum.InMicroseconds(), std::numeric_limits<Sample>::max());
277 DCHECK_LT(maximum.InMicroseconds(), std::numeric_limits<Sample>::max());
Gabriel Charette5ec205882018-05-22 18:07:31278 return FactoryGet(name, static_cast<Sample>(minimum.InMicroseconds()),
279 static_cast<Sample>(maximum.InMicroseconds()), bucket_count,
280 flags);
281}
282
asvitkine5c2d5022015-06-19 00:37:50283HistogramBase* Histogram::FactoryGet(const char* name,
284 Sample minimum,
285 Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45286 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14287 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50288 return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags);
289}
290
291HistogramBase* Histogram::FactoryTimeGet(const char* name,
292 TimeDelta minimum,
293 TimeDelta maximum,
Peter Kastingfc94f5062022-06-08 16:41:45294 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14295 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50296 return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count,
297 flags);
298}
299
Gabriel Charette5ec205882018-05-22 18:07:31300HistogramBase* Histogram::FactoryMicrosecondsTimeGet(const char* name,
301 TimeDelta minimum,
302 TimeDelta maximum,
Peter Kastingfc94f5062022-06-08 16:41:45303 size_t bucket_count,
Gabriel Charette5ec205882018-05-22 18:07:31304 int32_t flags) {
305 return FactoryMicrosecondsTimeGet(std::string(name), minimum, maximum,
306 bucket_count, flags);
307}
308
dcheng093de9b2016-04-04 21:25:51309std::unique_ptr<HistogramBase> Histogram::PersistentCreate(
Brian Whited1c91082017-11-03 14:46:42310 const char* name,
bcwhitec85a1f822016-02-18 21:22:14311 const BucketRanges* ranges,
bcwhitefa8485b2017-05-01 16:43:25312 const DelayedPersistentAllocation& counts,
313 const DelayedPersistentAllocation& logged_counts,
bcwhitec85a1f822016-02-18 21:22:14314 HistogramSamples::Metadata* meta,
315 HistogramSamples::Metadata* logged_meta) {
Brian White16f612b42022-02-24 01:02:51316 return WrapUnique(
317 new Histogram(name, ranges, counts, logged_counts, meta, logged_meta));
bcwhite5cb99eb2016-02-01 21:07:56318}
319
[email protected]34d062322012-08-01 21:34:08320// Calculate what range of values are held in each bucket.
321// We have to be careful that we don't pick a ratio between starting points in
322// consecutive buckets that is sooo small, that the integer bounds are the same
323// (effectively making one bucket get no values). We need to avoid:
324// ranges(i) == ranges(i + 1)
325// To avoid that, we just do a fine-grained bucket width as far as we need to
326// until we get a ratio that moves us along at least 2 units at a time. From
327// that bucket onward we do use the exponential growth of buckets.
328//
329// static
330void Histogram::InitializeBucketRanges(Sample minimum,
331 Sample maximum,
[email protected]34d062322012-08-01 21:34:08332 BucketRanges* ranges) {
[email protected]34d062322012-08-01 21:34:08333 double log_max = log(static_cast<double>(maximum));
334 double log_ratio;
335 double log_next;
336 size_t bucket_index = 1;
337 Sample current = minimum;
338 ranges->set_range(bucket_index, current);
[email protected]15ce3842013-06-27 14:38:45339 size_t bucket_count = ranges->bucket_count();
Brian White61b84b22018-10-05 18:03:18340
[email protected]34d062322012-08-01 21:34:08341 while (bucket_count > ++bucket_index) {
342 double log_current;
343 log_current = log(static_cast<double>(current));
Brian White2aaa8b01a2018-11-13 23:24:56344 debug::Alias(&log_current);
[email protected]34d062322012-08-01 21:34:08345 // Calculate the count'th root of the range.
346 log_ratio = (log_max - log_current) / (bucket_count - bucket_index);
347 // See where the next bucket would start.
348 log_next = log_current + log_ratio;
349 Sample next;
Peter Kasting8c3adfb2017-09-13 08:07:39350 next = static_cast<int>(std::round(exp(log_next)));
[email protected]34d062322012-08-01 21:34:08351 if (next > current)
352 current = next;
353 else
354 ++current; // Just do a narrow bucket, and keep trying.
355 ranges->set_range(bucket_index, current);
356 }
[email protected]15ce3842013-06-27 14:38:45357 ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX);
[email protected]34d062322012-08-01 21:34:08358 ranges->ResetChecksum();
359}
360
[email protected]2f7d9cd2012-09-22 03:42:12361// static
362const int Histogram::kCommonRaceBasedCountMismatch = 5;
[email protected]34d062322012-08-01 21:34:08363
bcwhitec85a1f822016-02-18 21:22:14364uint32_t Histogram::FindCorruption(const HistogramSamples& samples) const {
Peter Kastingfc94f5062022-06-08 16:41:45365 uint32_t inconsistencies = NO_INCONSISTENCIES;
[email protected]34d062322012-08-01 21:34:08366 Sample previous_range = -1; // Bottom range is always 0.
Peter Kastingfc94f5062022-06-08 16:41:45367 for (size_t index = 0; index < bucket_count(); ++index) {
[email protected]34d062322012-08-01 21:34:08368 int new_range = ranges(index);
369 if (previous_range >= new_range)
370 inconsistencies |= BUCKET_ORDER_ERROR;
371 previous_range = new_range;
372 }
373
374 if (!bucket_ranges()->HasValidChecksum())
375 inconsistencies |= RANGE_CHECKSUM_ERROR;
376
avi9b6f42932015-12-26 22:15:14377 int64_t delta64 = samples.redundant_count() - samples.TotalCount();
[email protected]34d062322012-08-01 21:34:08378 if (delta64 != 0) {
379 int delta = static_cast<int>(delta64);
380 if (delta != delta64)
381 delta = INT_MAX; // Flag all giant errors as INT_MAX.
[email protected]34d062322012-08-01 21:34:08382 if (delta > 0) {
[email protected]34d062322012-08-01 21:34:08383 if (delta > kCommonRaceBasedCountMismatch)
384 inconsistencies |= COUNT_HIGH_ERROR;
385 } else {
386 DCHECK_GT(0, delta);
[email protected]34d062322012-08-01 21:34:08387 if (-delta > kCommonRaceBasedCountMismatch)
388 inconsistencies |= COUNT_LOW_ERROR;
389 }
390 }
[email protected]cc7dec212013-03-01 03:53:25391 return inconsistencies;
[email protected]34d062322012-08-01 21:34:08392}
393
Brian White32052d5b2017-08-07 16:46:57394const BucketRanges* Histogram::bucket_ranges() const {
395 return unlogged_samples_->bucket_ranges();
396}
397
bcwhitef1dec482017-07-06 14:39:11398Sample Histogram::declared_min() const {
Brian White32052d5b2017-08-07 16:46:57399 const BucketRanges* ranges = bucket_ranges();
400 if (ranges->bucket_count() < 2)
bcwhitef1dec482017-07-06 14:39:11401 return -1;
Brian White32052d5b2017-08-07 16:46:57402 return ranges->range(1);
bcwhitef1dec482017-07-06 14:39:11403}
404
405Sample Histogram::declared_max() const {
Brian White32052d5b2017-08-07 16:46:57406 const BucketRanges* ranges = bucket_ranges();
407 if (ranges->bucket_count() < 2)
bcwhitef1dec482017-07-06 14:39:11408 return -1;
Brian White32052d5b2017-08-07 16:46:57409 return ranges->range(ranges->bucket_count() - 1);
bcwhitef1dec482017-07-06 14:39:11410}
411
Peter Kastingfc94f5062022-06-08 16:41:45412Sample Histogram::ranges(size_t i) const {
Brian White32052d5b2017-08-07 16:46:57413 return bucket_ranges()->range(i);
[email protected]34d062322012-08-01 21:34:08414}
415
Peter Kastingfc94f5062022-06-08 16:41:45416size_t Histogram::bucket_count() const {
417 return bucket_ranges()->bucket_count();
[email protected]34d062322012-08-01 21:34:08418}
419
[email protected]34d062322012-08-01 21:34:08420// static
Brian Whited1c91082017-11-03 14:46:42421bool Histogram::InspectConstructionArguments(StringPiece name,
[email protected]34d062322012-08-01 21:34:08422 Sample* minimum,
423 Sample* maximum,
Peter Kastingfc94f5062022-06-08 16:41:45424 size_t* bucket_count) {
Brian White7cfaffb12018-11-15 16:51:31425 bool check_okay = true;
426
427 // Checks below must be done after any min/max swap.
428 if (*minimum > *maximum) {
Brian Whitea254ab842022-02-16 16:34:53429 DLOG(ERROR) << "Histogram: " << name << " has swapped minimum/maximum";
Brian White7cfaffb12018-11-15 16:51:31430 check_okay = false;
431 std::swap(*minimum, *maximum);
432 }
433
[email protected]34d062322012-08-01 21:34:08434 // Defensive code for backward compatibility.
435 if (*minimum < 1) {
Brian White9c8bb642022-02-22 18:19:12436 // TODO(crbug.com/1288842): Temporarily disabled during cleanup.
437 // DLOG(ERROR) << "Histogram: " << name << " has bad minimum: " << *minimum;
[email protected]34d062322012-08-01 21:34:08438 *minimum = 1;
Brian White7cfaffb12018-11-15 16:51:31439 if (*maximum < 1)
440 *maximum = 1;
[email protected]34d062322012-08-01 21:34:08441 }
442 if (*maximum >= kSampleType_MAX) {
Brian Whitea254ab842022-02-16 16:34:53443 DLOG(ERROR) << "Histogram: " << name << " has bad maximum: " << *maximum;
[email protected]a5c7bd792012-08-02 00:29:04444 *maximum = kSampleType_MAX - 1;
[email protected]34d062322012-08-01 21:34:08445 }
Brian Whiteabfa2702019-04-03 17:00:54446 if (*bucket_count > kBucketCount_MAX) {
Brian White7cfaffb12018-11-15 16:51:31447 UmaHistogramSparse("Histogram.TooManyBuckets.1000",
448 static_cast<Sample>(HashMetricName(name)));
Brian White226244e2018-12-20 23:11:39449
Brian White226244e2018-12-20 23:11:39450 // Blink.UseCounter legitimately has more than 1000 entries in its enum.
Alexei Svitkined67a4432020-10-13 22:31:21451 if (!StartsWith(name, "Blink.UseCounter")) {
Brian Whitea254ab842022-02-16 16:34:53452 DLOG(ERROR) << "Histogram: " << name
453 << " has bad bucket_count: " << *bucket_count << " (limit "
454 << kBucketCount_MAX << ")";
Brian Whiteabfa2702019-04-03 17:00:54455
Brian White226244e2018-12-20 23:11:39456 // Assume it's a mistake and limit to 100 buckets, plus under and over.
457 // If the DCHECK doesn't alert the user then hopefully the small number
458 // will be obvious on the dashboard. If not, then it probably wasn't
459 // important.
460 *bucket_count = 102;
461 check_okay = false;
462 }
bcwhiteaaf2d3452017-04-26 19:17:47463 }
Brian White7cfaffb12018-11-15 16:51:31464
465 // Ensure parameters are sane.
bcwhiteaaf2d3452017-04-26 19:17:47466 if (*maximum == *minimum) {
467 check_okay = false;
468 *maximum = *minimum + 1;
469 }
470 if (*bucket_count < 3) {
471 check_okay = false;
472 *bucket_count = 3;
473 }
Peter Kastingfc94f5062022-06-08 16:41:45474 // The swap at the top of the function guarantees this cast is safe.
475 const size_t max_buckets = static_cast<size_t>(*maximum - *minimum + 2);
476 if (*bucket_count > max_buckets) {
bcwhiteaaf2d3452017-04-26 19:17:47477 check_okay = false;
Peter Kastingfc94f5062022-06-08 16:41:45478 *bucket_count = max_buckets;
bcwhiteaaf2d3452017-04-26 19:17:47479 }
480
481 if (!check_okay) {
Ilya Sherman16d5d5f42017-12-08 00:32:44482 UmaHistogramSparse("Histogram.BadConstructionArguments",
483 static_cast<Sample>(HashMetricName(name)));
bcwhiteaaf2d3452017-04-26 19:17:47484 }
485
486 return check_okay;
[email protected]34d062322012-08-01 21:34:08487}
488
bcwhiteb036e4322015-12-10 18:36:34489uint64_t Histogram::name_hash() const {
altimin498c8382017-05-12 17:49:18490 return unlogged_samples_->id();
bcwhiteb036e4322015-12-10 18:36:34491}
492
[email protected]07c02402012-10-31 06:20:25493HistogramType Histogram::GetHistogramType() const {
494 return HISTOGRAM;
495}
496
[email protected]15ce3842013-06-27 14:38:45497bool Histogram::HasConstructionArguments(Sample expected_minimum,
498 Sample expected_maximum,
Peter Kastingfc94f5062022-06-08 16:41:45499 size_t expected_bucket_count) const {
bcwhitef1dec482017-07-06 14:39:11500 return (expected_bucket_count == bucket_count() &&
501 expected_minimum == declared_min() &&
502 expected_maximum == declared_max());
[email protected]abae9b022012-10-24 08:18:52503}
504
505void Histogram::Add(int value) {
amohammadkhan6779b5c32015-08-05 20:31:11506 AddCount(value, 1);
507}
508
509void Histogram::AddCount(int value, int count) {
[email protected]abae9b022012-10-24 08:18:52510 DCHECK_EQ(0, ranges(0));
[email protected]15ce3842013-06-27 14:38:45511 DCHECK_EQ(kSampleType_MAX, ranges(bucket_count()));
[email protected]abae9b022012-10-24 08:18:52512
513 if (value > kSampleType_MAX - 1)
514 value = kSampleType_MAX - 1;
515 if (value < 0)
516 value = 0;
amohammadkhan6779b5c32015-08-05 20:31:11517 if (count <= 0) {
518 NOTREACHED();
519 return;
520 }
altimin498c8382017-05-12 17:49:18521 unlogged_samples_->Accumulate(value, count);
simonhatchdf5a8142015-07-15 22:22:57522
Oystein Eftevaag53412eb2019-12-09 20:53:56523 if (UNLIKELY(StatisticsRecorder::have_active_callbacks()))
Shakti Sahue0e07a0e2021-06-06 00:07:52524 FindAndRunCallbacks(value);
[email protected]abae9b022012-10-24 08:18:52525}
526
dcheng093de9b2016-04-04 21:25:51527std::unique_ptr<HistogramSamples> Histogram::SnapshotSamples() const {
altimin498c8382017-05-12 17:49:18528 return SnapshotAllSamples();
[email protected]abae9b022012-10-24 08:18:52529}
530
Luc Nguyene01c6752022-12-01 18:40:15531std::unique_ptr<HistogramSamples> Histogram::SnapshotUnloggedSamples() const {
532 return SnapshotUnloggedSamplesImpl();
533}
534
535void Histogram::MarkSamplesAsLogged(const HistogramSamples& samples) {
536 // |final_delta_created_| only exists when DCHECK is on.
537#if DCHECK_IS_ON()
538 DCHECK(!final_delta_created_);
539#endif
540
541 unlogged_samples_->Subtract(samples);
542 logged_samples_->Add(samples);
543}
544
dcheng093de9b2016-04-04 21:25:51545std::unique_ptr<HistogramSamples> Histogram::SnapshotDelta() {
Luc Nguyene01c6752022-12-01 18:40:15546 // |final_delta_created_| only exists when DCHECK is on.
bcwhitef1dec482017-07-06 14:39:11547#if DCHECK_IS_ON()
bcwhite65e57d02016-05-13 14:39:40548 DCHECK(!final_delta_created_);
bcwhitef1dec482017-07-06 14:39:11549#endif
550
altimin498c8382017-05-12 17:49:18551 // The code below has subtle thread-safety guarantees! All changes to
552 // the underlying SampleVectors use atomic integer operations, which guarantee
553 // eventual consistency, but do not guarantee full synchronization between
554 // different entries in the SampleVector. In particular, this means that
555 // concurrent updates to the histogram might result in the reported sum not
556 // matching the individual bucket counts; or there being some buckets that are
557 // logically updated "together", but end up being only partially updated when
558 // a snapshot is captured. Note that this is why it's important to subtract
559 // exactly the snapshotted unlogged samples, rather than simply resetting the
560 // vector: this way, the next snapshot will include any concurrent updates
561 // missed by the current snapshot.
bcwhite65e57d02016-05-13 14:39:40562
Luc Nguyen257ecb02023-04-11 21:09:41563 std::unique_ptr<HistogramSamples> snapshot =
564 std::make_unique<SampleVector>(unlogged_samples_->id(), bucket_ranges());
565 snapshot->Extract(*unlogged_samples_);
566 logged_samples_->Add(*snapshot);
altimin498c8382017-05-12 17:49:18567
bcwhitec85a1f822016-02-18 21:22:14568 return snapshot;
569}
570
bcwhite65e57d02016-05-13 14:39:40571std::unique_ptr<HistogramSamples> Histogram::SnapshotFinalDelta() const {
Luc Nguyene01c6752022-12-01 18:40:15572 // |final_delta_created_| only exists when DCHECK is on.
bcwhitef1dec482017-07-06 14:39:11573#if DCHECK_IS_ON()
bcwhite65e57d02016-05-13 14:39:40574 DCHECK(!final_delta_created_);
575 final_delta_created_ = true;
bcwhitef1dec482017-07-06 14:39:11576#endif
bcwhite65e57d02016-05-13 14:39:40577
altimin498c8382017-05-12 17:49:18578 return SnapshotUnloggedSamples();
bcwhite65e57d02016-05-13 14:39:40579}
580
[email protected]c50c21d2013-01-11 21:52:44581void Histogram::AddSamples(const HistogramSamples& samples) {
altimin498c8382017-05-12 17:49:18582 unlogged_samples_->Add(samples);
[email protected]c50c21d2013-01-11 21:52:44583}
584
585bool Histogram::AddSamplesFromPickle(PickleIterator* iter) {
altimin498c8382017-05-12 17:49:18586 return unlogged_samples_->AddFromPickle(iter);
[email protected]c50c21d2013-01-11 21:52:44587}
588
Nathan Memmottc034fa4e2022-07-15 23:59:43589base::Value::Dict Histogram::ToGraphDict() const {
Jun Kokatsu505af9f2020-05-05 11:59:47590 std::unique_ptr<SampleVector> snapshot = SnapshotAllSamples();
Quang Minh Tuan Nguyen39b1fed2021-03-16 00:49:38591 return snapshot->ToGraphDict(histogram_name(), flags());
Jun Kokatsu505af9f2020-05-05 11:59:47592}
593
Daniel Cheng0d89f9222017-09-22 05:05:07594void Histogram::SerializeInfoImpl(Pickle* pickle) const {
[email protected]c50c21d2013-01-11 21:52:44595 DCHECK(bucket_ranges()->HasValidChecksum());
Daniel Cheng0d89f9222017-09-22 05:05:07596 pickle->WriteString(histogram_name());
597 pickle->WriteInt(flags());
598 pickle->WriteInt(declared_min());
599 pickle->WriteInt(declared_max());
Peter Kastingfc94f5062022-06-08 16:41:45600 // Limited to kBucketCount_MAX, which fits in a uint32_t.
601 pickle->WriteUInt32(static_cast<uint32_t>(bucket_count()));
Daniel Cheng0d89f9222017-09-22 05:05:07602 pickle->WriteUInt32(bucket_ranges()->checksum());
[email protected]c50c21d2013-01-11 21:52:44603}
604
Brian White16f612b42022-02-24 01:02:51605Histogram::Histogram(const char* name, const BucketRanges* ranges)
Brian Whitecaef6e12018-03-12 22:06:11606 : HistogramBase(name) {
Brian White16f612b42022-02-24 01:02:51607 DCHECK(ranges) << name;
Peter Boströmfd851232021-03-31 17:05:33608 unlogged_samples_ =
609 std::make_unique<SampleVector>(HashMetricName(name), ranges);
610 logged_samples_ =
611 std::make_unique<SampleVector>(unlogged_samples_->id(), ranges);
[email protected]abae9b022012-10-24 08:18:52612}
613
Brian Whited1c91082017-11-03 14:46:42614Histogram::Histogram(const char* name,
bcwhite5cb99eb2016-02-01 21:07:56615 const BucketRanges* ranges,
bcwhitefa8485b2017-05-01 16:43:25616 const DelayedPersistentAllocation& counts,
617 const DelayedPersistentAllocation& logged_counts,
bcwhitec85a1f822016-02-18 21:22:14618 HistogramSamples::Metadata* meta,
619 HistogramSamples::Metadata* logged_meta)
Brian Whitecaef6e12018-03-12 22:06:11620 : HistogramBase(name) {
Brian White16f612b42022-02-24 01:02:51621 DCHECK(ranges) << name;
Peter Boströmfd851232021-03-31 17:05:33622 unlogged_samples_ = std::make_unique<PersistentSampleVector>(
623 HashMetricName(name), ranges, meta, counts);
624 logged_samples_ = std::make_unique<PersistentSampleVector>(
625 unlogged_samples_->id(), ranges, logged_meta, logged_counts);
bcwhite5cb99eb2016-02-01 21:07:56626}
627
Chris Watkinsbb7211c2017-11-29 07:16:38628Histogram::~Histogram() = default;
[email protected]abae9b022012-10-24 08:18:52629
Peter Kastingfc94f5062022-06-08 16:41:45630const std::string Histogram::GetAsciiBucketRange(size_t i) const {
[email protected]f2bb3202013-04-05 21:21:54631 return GetSimpleAsciiBucketRange(ranges(i));
[email protected]34d062322012-08-01 21:34:08632}
633
[email protected]34d062322012-08-01 21:34:08634//------------------------------------------------------------------------------
635// Private methods
636
[email protected]c50c21d2013-01-11 21:52:44637// static
638HistogramBase* Histogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14639 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44640 int flags;
641 int declared_min;
642 int declared_max;
Peter Kastingfc94f5062022-06-08 16:41:45643 size_t bucket_count;
avi9b6f42932015-12-26 22:15:14644 uint32_t range_checksum;
[email protected]c50c21d2013-01-11 21:52:44645
646 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
647 &declared_max, &bucket_count, &range_checksum)) {
Brian White7eb91482017-08-09 19:54:45648 return nullptr;
[email protected]c50c21d2013-01-11 21:52:44649 }
650
651 // Find or create the local version of the histogram in this process.
652 HistogramBase* histogram = Histogram::FactoryGet(
653 histogram_name, declared_min, declared_max, bucket_count, flags);
Brian White7eb91482017-08-09 19:54:45654 if (!histogram)
655 return nullptr;
[email protected]c50c21d2013-01-11 21:52:44656
Brian White7eb91482017-08-09 19:54:45657 // The serialized histogram might be corrupted.
658 if (!ValidateRangeChecksum(*histogram, range_checksum))
659 return nullptr;
660
[email protected]c50c21d2013-01-11 21:52:44661 return histogram;
662}
663
altimin498c8382017-05-12 17:49:18664std::unique_ptr<SampleVector> Histogram::SnapshotAllSamples() const {
Luc Nguyene01c6752022-12-01 18:40:15665 std::unique_ptr<SampleVector> samples = SnapshotUnloggedSamplesImpl();
altimin498c8382017-05-12 17:49:18666 samples->Add(*logged_samples_);
667 return samples;
668}
669
Luc Nguyene01c6752022-12-01 18:40:15670std::unique_ptr<SampleVector> Histogram::SnapshotUnloggedSamplesImpl() const {
dcheng093de9b2016-04-04 21:25:51671 std::unique_ptr<SampleVector> samples(
altimin498c8382017-05-12 17:49:18672 new SampleVector(unlogged_samples_->id(), bucket_ranges()));
673 samples->Add(*unlogged_samples_);
danakj0c8d4aa2015-11-25 05:29:58674 return samples;
[email protected]877ef562012-10-20 02:56:18675}
676
Nathan Memmottc034fa4e2022-07-15 23:59:43677Value::Dict Histogram::GetParameters() const {
678 Value::Dict params;
679 params.Set("type", HistogramTypeToString(GetHistogramType()));
680 params.Set("min", declared_min());
681 params.Set("max", declared_max());
682 params.Set("bucket_count", static_cast<int>(bucket_count()));
Sylvain Defresne0348ecfd2021-06-23 08:27:34683 return params;
[email protected]24a7ec52012-10-08 10:31:50684}
685
[email protected]34d062322012-08-01 21:34:08686//------------------------------------------------------------------------------
687// LinearHistogram: This histogram uses a traditional set of evenly spaced
688// buckets.
689//------------------------------------------------------------------------------
690
bcwhite5cb99eb2016-02-01 21:07:56691class LinearHistogram::Factory : public Histogram::Factory {
692 public:
693 Factory(const std::string& name,
694 HistogramBase::Sample minimum,
695 HistogramBase::Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45696 size_t bucket_count,
bcwhite5cb99eb2016-02-01 21:07:56697 int32_t flags,
698 const DescriptionPair* descriptions)
Peter Kastingfc94f5062022-06-08 16:41:45699 : Histogram::Factory(name,
700 LINEAR_HISTOGRAM,
701 minimum,
702 maximum,
703 bucket_count,
704 flags) {
bcwhite5cb99eb2016-02-01 21:07:56705 descriptions_ = descriptions;
706 }
707
Peter Boström75cd3c02021-09-28 15:23:18708 Factory(const Factory&) = delete;
709 Factory& operator=(const Factory&) = delete;
710
bcwhite5cb99eb2016-02-01 21:07:56711 protected:
712 BucketRanges* CreateRanges() override {
713 BucketRanges* ranges = new BucketRanges(bucket_count_ + 1);
714 LinearHistogram::InitializeBucketRanges(minimum_, maximum_, ranges);
715 return ranges;
716 }
717
dcheng093de9b2016-04-04 21:25:51718 std::unique_ptr<HistogramBase> HeapAlloc(
719 const BucketRanges* ranges) override {
Ali Hijazia8877892022-11-10 20:51:03720 return WrapUnique(new LinearHistogram(GetPermanentName(*name_), ranges));
bcwhite5cb99eb2016-02-01 21:07:56721 }
722
723 void FillHistogram(HistogramBase* base_histogram) override {
724 Histogram::Factory::FillHistogram(base_histogram);
Gayane Petrosyan5745ac62018-03-23 01:45:24725 // Normally, |base_histogram| should have type LINEAR_HISTOGRAM or be
726 // inherited from it. However, if it's expired, it will actually be a
727 // DUMMY_HISTOGRAM. Skip filling in that case.
728 if (base_histogram->GetHistogramType() == DUMMY_HISTOGRAM)
729 return;
bcwhite5cb99eb2016-02-01 21:07:56730 LinearHistogram* histogram = static_cast<LinearHistogram*>(base_histogram);
731 // Set range descriptions.
732 if (descriptions_) {
733 for (int i = 0; descriptions_[i].description; ++i) {
734 histogram->bucket_description_[descriptions_[i].sample] =
735 descriptions_[i].description;
736 }
737 }
738 }
739
740 private:
Tom Sepezd83ed1b2023-09-19 00:10:00741 raw_ptr<const DescriptionPair, AllowPtrArithmetic> descriptions_;
bcwhite5cb99eb2016-02-01 21:07:56742};
743
Chris Watkinsbb7211c2017-11-29 07:16:38744LinearHistogram::~LinearHistogram() = default;
[email protected]34d062322012-08-01 21:34:08745
asvitkine24d3e9a2015-05-27 05:22:14746HistogramBase* LinearHistogram::FactoryGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17747 Sample minimum,
748 Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45749 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14750 int32_t flags) {
Brian White7eb91482017-08-09 19:54:45751 return FactoryGetWithRangeDescription(name, minimum, maximum, bucket_count,
Brian Whited1c91082017-11-03 14:46:42752 flags, NULL);
[email protected]07c02402012-10-31 06:20:25753}
754
asvitkine24d3e9a2015-05-27 05:22:14755HistogramBase* LinearHistogram::FactoryTimeGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17756 TimeDelta minimum,
757 TimeDelta maximum,
Peter Kastingfc94f5062022-06-08 16:41:45758 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14759 int32_t flags) {
Jan Krcal4e553f422019-04-08 12:52:46760 DCHECK_LT(minimum.InMilliseconds(), std::numeric_limits<Sample>::max());
761 DCHECK_LT(maximum.InMilliseconds(), std::numeric_limits<Sample>::max());
pkasting9cf9b94a2014-10-01 22:18:43762 return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
763 static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
764 flags);
[email protected]07c02402012-10-31 06:20:25765}
766
asvitkine5c2d5022015-06-19 00:37:50767HistogramBase* LinearHistogram::FactoryGet(const char* name,
768 Sample minimum,
769 Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45770 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14771 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50772 return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags);
773}
774
775HistogramBase* LinearHistogram::FactoryTimeGet(const char* name,
776 TimeDelta minimum,
777 TimeDelta maximum,
Peter Kastingfc94f5062022-06-08 16:41:45778 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14779 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50780 return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count,
781 flags);
782}
783
dcheng093de9b2016-04-04 21:25:51784std::unique_ptr<HistogramBase> LinearHistogram::PersistentCreate(
Brian Whited1c91082017-11-03 14:46:42785 const char* name,
bcwhite5cb99eb2016-02-01 21:07:56786 const BucketRanges* ranges,
bcwhitefa8485b2017-05-01 16:43:25787 const DelayedPersistentAllocation& counts,
788 const DelayedPersistentAllocation& logged_counts,
bcwhitec85a1f822016-02-18 21:22:14789 HistogramSamples::Metadata* meta,
790 HistogramSamples::Metadata* logged_meta) {
Brian White16f612b42022-02-24 01:02:51791 return WrapUnique(new LinearHistogram(name, ranges, counts, logged_counts,
792 meta, logged_meta));
bcwhite5cb99eb2016-02-01 21:07:56793}
794
[email protected]de415552013-01-23 04:12:17795HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
avi9b6f42932015-12-26 22:15:14796 const std::string& name,
797 Sample minimum,
798 Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45799 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14800 int32_t flags,
801 const DescriptionPair descriptions[]) {
Brian White564cedf2018-11-15 20:26:00802 // Originally, histograms were required to have at least one sample value
803 // plus underflow and overflow buckets. For single-entry enumerations,
804 // that one value is usually zero (which IS the underflow bucket)
805 // resulting in a |maximum| value of 1 (the exclusive upper-bound) and only
806 // the two outlier buckets. Handle this by making max==2 and buckets==3.
807 // This usually won't have any cost since the single-value-optimization
808 // will be used until the count exceeds 16 bits.
809 if (maximum == 1 && bucket_count == 2) {
810 maximum = 2;
811 bucket_count = 3;
812 }
813
[email protected]e184be902012-08-07 04:49:24814 bool valid_arguments = Histogram::InspectConstructionArguments(
815 name, &minimum, &maximum, &bucket_count);
Brian White226244e2018-12-20 23:11:39816 DCHECK(valid_arguments) << name;
Brian Whitea254ab842022-02-16 16:34:53817 if (!valid_arguments) {
818 DLOG(ERROR) << "Histogram " << name << " dropped for invalid parameters.";
819 return DummyHistogram::GetInstance();
820 }
[email protected]34d062322012-08-01 21:34:08821
bcwhite5cb99eb2016-02-01 21:07:56822 return Factory(name, minimum, maximum, bucket_count, flags, descriptions)
823 .Build();
[email protected]34d062322012-08-01 21:34:08824}
825
[email protected]07c02402012-10-31 06:20:25826HistogramType LinearHistogram::GetHistogramType() const {
[email protected]b7d08202011-01-25 17:29:39827 return LINEAR_HISTOGRAM;
828}
829
Brian White16f612b42022-02-24 01:02:51830LinearHistogram::LinearHistogram(const char* name, const BucketRanges* ranges)
831 : Histogram(name, ranges) {}
initial.commitd7cae122008-07-26 21:49:38832
bcwhitefa8485b2017-05-01 16:43:25833LinearHistogram::LinearHistogram(
Brian Whited1c91082017-11-03 14:46:42834 const char* name,
bcwhitefa8485b2017-05-01 16:43:25835 const BucketRanges* ranges,
836 const DelayedPersistentAllocation& counts,
837 const DelayedPersistentAllocation& logged_counts,
838 HistogramSamples::Metadata* meta,
839 HistogramSamples::Metadata* logged_meta)
Brian White16f612b42022-02-24 01:02:51840 : Histogram(name, ranges, counts, logged_counts, meta, logged_meta) {}
bcwhite5cb99eb2016-02-01 21:07:56841
Peter Kastingfc94f5062022-06-08 16:41:45842const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const {
[email protected]b7d08202011-01-25 17:29:39843 int range = ranges(i);
Brian White61b84b22018-10-05 18:03:18844 BucketDescriptionMap::const_iterator it = bucket_description_.find(range);
[email protected]b7d08202011-01-25 17:29:39845 if (it == bucket_description_.end())
846 return Histogram::GetAsciiBucketRange(i);
847 return it->second;
848}
849
[email protected]34d062322012-08-01 21:34:08850// static
851void LinearHistogram::InitializeBucketRanges(Sample minimum,
852 Sample maximum,
[email protected]34d062322012-08-01 21:34:08853 BucketRanges* ranges) {
[email protected]34d062322012-08-01 21:34:08854 double min = minimum;
855 double max = maximum;
[email protected]15ce3842013-06-27 14:38:45856 size_t bucket_count = ranges->bucket_count();
Brian White61b84b22018-10-05 18:03:18857
[email protected]15ce3842013-06-27 14:38:45858 for (size_t i = 1; i < bucket_count; ++i) {
[email protected]34d062322012-08-01 21:34:08859 double linear_range =
[email protected]15ce3842013-06-27 14:38:45860 (min * (bucket_count - 1 - i) + max * (i - 1)) / (bucket_count - 2);
Peter Kastingfc94f5062022-06-08 16:41:45861 auto range = static_cast<Sample>(linear_range + 0.5);
Brian White61b84b22018-10-05 18:03:18862 ranges->set_range(i, range);
[email protected]34d062322012-08-01 21:34:08863 }
[email protected]15ce3842013-06-27 14:38:45864 ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX);
[email protected]34d062322012-08-01 21:34:08865 ranges->ResetChecksum();
866}
[email protected]b7d08202011-01-25 17:29:39867
[email protected]c50c21d2013-01-11 21:52:44868// static
869HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14870 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44871 int flags;
872 int declared_min;
873 int declared_max;
Peter Kastingfc94f5062022-06-08 16:41:45874 size_t bucket_count;
avi9b6f42932015-12-26 22:15:14875 uint32_t range_checksum;
[email protected]c50c21d2013-01-11 21:52:44876
877 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
878 &declared_max, &bucket_count, &range_checksum)) {
Brian White7eb91482017-08-09 19:54:45879 return nullptr;
[email protected]c50c21d2013-01-11 21:52:44880 }
881
882 HistogramBase* histogram = LinearHistogram::FactoryGet(
883 histogram_name, declared_min, declared_max, bucket_count, flags);
Brian White82027ff5d2017-08-21 19:50:22884 if (!histogram)
885 return nullptr;
886
[email protected]c50c21d2013-01-11 21:52:44887 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
888 // The serialized histogram might be corrupted.
Brian White7eb91482017-08-09 19:54:45889 return nullptr;
[email protected]c50c21d2013-01-11 21:52:44890 }
891 return histogram;
892}
893
initial.commitd7cae122008-07-26 21:49:38894//------------------------------------------------------------------------------
Brian Whitefffb3582018-05-24 21:17:01895// ScaledLinearHistogram: This is a wrapper around a LinearHistogram that
896// scales input counts.
897//------------------------------------------------------------------------------
898
899ScaledLinearHistogram::ScaledLinearHistogram(const char* name,
900 Sample minimum,
901 Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45902 size_t bucket_count,
Brian Whitefffb3582018-05-24 21:17:01903 int32_t scale,
904 int32_t flags)
Eric Seckler21a69f42020-07-10 14:18:17905 : ScaledLinearHistogram(std::string(name),
906 minimum,
907 maximum,
908 bucket_count,
909 scale,
910 flags) {}
911
912ScaledLinearHistogram::ScaledLinearHistogram(const std::string& name,
913 Sample minimum,
914 Sample maximum,
Peter Kastingfc94f5062022-06-08 16:41:45915 size_t bucket_count,
Eric Seckler21a69f42020-07-10 14:18:17916 int32_t scale,
917 int32_t flags)
Joshua Berenhaus9c88c1a22020-07-16 16:37:34918 : histogram_(LinearHistogram::FactoryGet(name,
919 minimum,
920 maximum,
921 bucket_count,
922 flags)),
Brian Whitefffb3582018-05-24 21:17:01923 scale_(scale) {
924 DCHECK(histogram_);
925 DCHECK_LT(1, scale);
926 DCHECK_EQ(1, minimum);
927 CHECK_EQ(static_cast<Sample>(bucket_count), maximum - minimum + 2)
928 << " ScaledLinearHistogram requires buckets of size 1";
929
Joshua Berenhaus9c88c1a22020-07-16 16:37:34930 // Normally, |histogram_| should have type LINEAR_HISTOGRAM or be
931 // inherited from it. However, if it's expired, it will be DUMMY_HISTOGRAM.
932 if (histogram_->GetHistogramType() == DUMMY_HISTOGRAM)
933 return;
934
935 DCHECK_EQ(histogram_->GetHistogramType(), LINEAR_HISTOGRAM);
936 LinearHistogram* histogram = static_cast<LinearHistogram*>(histogram_);
937 remainders_.resize(histogram->bucket_count(), 0);
Brian Whitefffb3582018-05-24 21:17:01938}
939
940ScaledLinearHistogram::~ScaledLinearHistogram() = default;
941
Mikhail Khokhlovaa896052023-01-25 11:41:48942void ScaledLinearHistogram::AddScaledCount(Sample value, int64_t count) {
Joshua Berenhaus9c88c1a22020-07-16 16:37:34943 if (histogram_->GetHistogramType() == DUMMY_HISTOGRAM)
944 return;
Farah Charabb114f862018-06-12 11:53:57945 if (count == 0)
946 return;
947 if (count < 0) {
948 NOTREACHED();
949 return;
950 }
Joshua Berenhaus9c88c1a22020-07-16 16:37:34951
952 DCHECK_EQ(histogram_->GetHistogramType(), LINEAR_HISTOGRAM);
953 LinearHistogram* histogram = static_cast<LinearHistogram*>(histogram_);
Peter Kastingfc94f5062022-06-08 16:41:45954 const auto max_value = static_cast<Sample>(histogram->bucket_count() - 1);
Ho Cheung3f166bc2023-05-08 16:07:47955 value = std::clamp(value, 0, max_value);
Brian Whitefffb3582018-05-24 21:17:01956
Mikhail Khokhlovaa896052023-01-25 11:41:48957 int64_t scaled_count = count / scale_;
958 subtle::Atomic32 remainder = static_cast<int>(count - scaled_count * scale_);
Brian Whitefffb3582018-05-24 21:17:01959
960 // ScaledLinearHistogram currently requires 1-to-1 mappings between value
961 // and bucket which alleviates the need to do a bucket lookup here (something
962 // that is internal to the HistogramSamples object).
963 if (remainder > 0) {
Peter Kastingfc94f5062022-06-08 16:41:45964 remainder = subtle::NoBarrier_AtomicIncrement(
965 &remainders_[static_cast<size_t>(value)], remainder);
Brian Whitefffb3582018-05-24 21:17:01966 // If remainder passes 1/2 scale, increment main count (thus rounding up).
967 // The remainder is decremented by the full scale, though, which will
968 // cause it to go negative and thus requrire another increase by the full
969 // scale amount before another bump of the scaled count.
970 if (remainder >= scale_ / 2) {
971 scaled_count += 1;
Peter Kastingfc94f5062022-06-08 16:41:45972 subtle::NoBarrier_AtomicIncrement(
973 &remainders_[static_cast<size_t>(value)], -scale_);
Brian Whitefffb3582018-05-24 21:17:01974 }
975 }
976
Mikhail Khokhlovaa896052023-01-25 11:41:48977 if (scaled_count > 0) {
978 DCHECK(scaled_count <= std::numeric_limits<int>::max());
979 histogram->AddCount(value, static_cast<int>(scaled_count));
980 }
Brian Whitefffb3582018-05-24 21:17:01981}
982
983//------------------------------------------------------------------------------
[email protected]e8829a192009-12-06 00:09:37984// This section provides implementation for BooleanHistogram.
985//------------------------------------------------------------------------------
986
bcwhite5cb99eb2016-02-01 21:07:56987class BooleanHistogram::Factory : public Histogram::Factory {
988 public:
989 Factory(const std::string& name, int32_t flags)
990 : Histogram::Factory(name, BOOLEAN_HISTOGRAM, 1, 2, 3, flags) {}
991
Peter Boström75cd3c02021-09-28 15:23:18992 Factory(const Factory&) = delete;
993 Factory& operator=(const Factory&) = delete;
994
bcwhite5cb99eb2016-02-01 21:07:56995 protected:
996 BucketRanges* CreateRanges() override {
997 BucketRanges* ranges = new BucketRanges(3 + 1);
[email protected]15ce3842013-06-27 14:38:45998 LinearHistogram::InitializeBucketRanges(1, 2, ranges);
bcwhite5cb99eb2016-02-01 21:07:56999 return ranges;
[email protected]e8829a192009-12-06 00:09:371000 }
1001
dcheng093de9b2016-04-04 21:25:511002 std::unique_ptr<HistogramBase> HeapAlloc(
1003 const BucketRanges* ranges) override {
Ali Hijazia8877892022-11-10 20:51:031004 return WrapUnique(new BooleanHistogram(GetPermanentName(*name_), ranges));
bcwhite5cb99eb2016-02-01 21:07:561005 }
bcwhite5cb99eb2016-02-01 21:07:561006};
1007
1008HistogramBase* BooleanHistogram::FactoryGet(const std::string& name,
1009 int32_t flags) {
1010 return Factory(name, flags).Build();
[email protected]e8829a192009-12-06 00:09:371011}
1012
avi9b6f42932015-12-26 22:15:141013HistogramBase* BooleanHistogram::FactoryGet(const char* name, int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:501014 return FactoryGet(std::string(name), flags);
1015}
1016
dcheng093de9b2016-04-04 21:25:511017std::unique_ptr<HistogramBase> BooleanHistogram::PersistentCreate(
Brian Whited1c91082017-11-03 14:46:421018 const char* name,
bcwhite5cb99eb2016-02-01 21:07:561019 const BucketRanges* ranges,
bcwhitefa8485b2017-05-01 16:43:251020 const DelayedPersistentAllocation& counts,
1021 const DelayedPersistentAllocation& logged_counts,
bcwhitec85a1f822016-02-18 21:22:141022 HistogramSamples::Metadata* meta,
1023 HistogramSamples::Metadata* logged_meta) {
bcwhitefa8485b2017-05-01 16:43:251024 return WrapUnique(new BooleanHistogram(name, ranges, counts, logged_counts,
1025 meta, logged_meta));
bcwhite5cb99eb2016-02-01 21:07:561026}
1027
[email protected]07c02402012-10-31 06:20:251028HistogramType BooleanHistogram::GetHistogramType() const {
[email protected]5d91c9e2010-07-28 17:25:281029 return BOOLEAN_HISTOGRAM;
1030}
1031
Brian Whited1c91082017-11-03 14:46:421032BooleanHistogram::BooleanHistogram(const char* name, const BucketRanges* ranges)
Brian White16f612b42022-02-24 01:02:511033 : LinearHistogram(name, ranges) {}
initial.commitd7cae122008-07-26 21:49:381034
bcwhitefa8485b2017-05-01 16:43:251035BooleanHistogram::BooleanHistogram(
Brian Whited1c91082017-11-03 14:46:421036 const char* name,
bcwhitefa8485b2017-05-01 16:43:251037 const BucketRanges* ranges,
1038 const DelayedPersistentAllocation& counts,
1039 const DelayedPersistentAllocation& logged_counts,
1040 HistogramSamples::Metadata* meta,
1041 HistogramSamples::Metadata* logged_meta)
Brian White16f612b42022-02-24 01:02:511042 : LinearHistogram(name, ranges, counts, logged_counts, meta, logged_meta) {}
bcwhite5cb99eb2016-02-01 21:07:561043
[email protected]c50c21d2013-01-11 21:52:441044HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:141045 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:441046 int flags;
1047 int declared_min;
1048 int declared_max;
Peter Kastingfc94f5062022-06-08 16:41:451049 size_t bucket_count;
avi9b6f42932015-12-26 22:15:141050 uint32_t range_checksum;
[email protected]c50c21d2013-01-11 21:52:441051
1052 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
1053 &declared_max, &bucket_count, &range_checksum)) {
Brian White7eb91482017-08-09 19:54:451054 return nullptr;
[email protected]c50c21d2013-01-11 21:52:441055 }
1056
1057 HistogramBase* histogram = BooleanHistogram::FactoryGet(
1058 histogram_name, flags);
Brian White82027ff5d2017-08-21 19:50:221059 if (!histogram)
1060 return nullptr;
1061
[email protected]c50c21d2013-01-11 21:52:441062 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
1063 // The serialized histogram might be corrupted.
Brian White7eb91482017-08-09 19:54:451064 return nullptr;
[email protected]c50c21d2013-01-11 21:52:441065 }
1066 return histogram;
1067}
1068
initial.commitd7cae122008-07-26 21:49:381069//------------------------------------------------------------------------------
[email protected]70cc56e42010-04-29 22:39:551070// CustomHistogram:
1071//------------------------------------------------------------------------------
1072
bcwhite5cb99eb2016-02-01 21:07:561073class CustomHistogram::Factory : public Histogram::Factory {
1074 public:
1075 Factory(const std::string& name,
1076 const std::vector<Sample>* custom_ranges,
1077 int32_t flags)
1078 : Histogram::Factory(name, CUSTOM_HISTOGRAM, 0, 0, 0, flags) {
1079 custom_ranges_ = custom_ranges;
1080 }
1081
Peter Boström75cd3c02021-09-28 15:23:181082 Factory(const Factory&) = delete;
1083 Factory& operator=(const Factory&) = delete;
1084
bcwhite5cb99eb2016-02-01 21:07:561085 protected:
1086 BucketRanges* CreateRanges() override {
1087 // Remove the duplicates in the custom ranges array.
1088 std::vector<int> ranges = *custom_ranges_;
1089 ranges.push_back(0); // Ensure we have a zero value.
1090 ranges.push_back(HistogramBase::kSampleType_MAX);
Anton Bikineeva61fb572020-10-18 08:54:441091 ranges::sort(ranges);
1092 ranges.erase(ranges::unique(ranges), ranges.end());
bcwhite5cb99eb2016-02-01 21:07:561093
1094 BucketRanges* bucket_ranges = new BucketRanges(ranges.size());
Peter Kastingfc94f5062022-06-08 16:41:451095 for (size_t i = 0; i < ranges.size(); i++) {
bcwhite5cb99eb2016-02-01 21:07:561096 bucket_ranges->set_range(i, ranges[i]);
1097 }
1098 bucket_ranges->ResetChecksum();
1099 return bucket_ranges;
1100 }
1101
dcheng093de9b2016-04-04 21:25:511102 std::unique_ptr<HistogramBase> HeapAlloc(
1103 const BucketRanges* ranges) override {
Ali Hijazia8877892022-11-10 20:51:031104 return WrapUnique(new CustomHistogram(GetPermanentName(*name_), ranges));
bcwhite5cb99eb2016-02-01 21:07:561105 }
1106
1107 private:
Keishi Hattori0e45c022021-11-27 09:25:521108 raw_ptr<const std::vector<Sample>> custom_ranges_;
bcwhite5cb99eb2016-02-01 21:07:561109};
1110
asvitkine24d3e9a2015-05-27 05:22:141111HistogramBase* CustomHistogram::FactoryGet(
1112 const std::string& name,
1113 const std::vector<Sample>& custom_ranges,
avi9b6f42932015-12-26 22:15:141114 int32_t flags) {
[email protected]34d062322012-08-01 21:34:081115 CHECK(ValidateCustomRanges(custom_ranges));
[email protected]70cc56e42010-04-29 22:39:551116
bcwhite5cb99eb2016-02-01 21:07:561117 return Factory(name, &custom_ranges, flags).Build();
[email protected]70cc56e42010-04-29 22:39:551118}
1119
asvitkine5c2d5022015-06-19 00:37:501120HistogramBase* CustomHistogram::FactoryGet(
1121 const char* name,
1122 const std::vector<Sample>& custom_ranges,
avi9b6f42932015-12-26 22:15:141123 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:501124 return FactoryGet(std::string(name), custom_ranges, flags);
1125}
1126
dcheng093de9b2016-04-04 21:25:511127std::unique_ptr<HistogramBase> CustomHistogram::PersistentCreate(
Brian Whited1c91082017-11-03 14:46:421128 const char* name,
bcwhite5cb99eb2016-02-01 21:07:561129 const BucketRanges* ranges,
bcwhitefa8485b2017-05-01 16:43:251130 const DelayedPersistentAllocation& counts,
1131 const DelayedPersistentAllocation& logged_counts,
bcwhitec85a1f822016-02-18 21:22:141132 HistogramSamples::Metadata* meta,
1133 HistogramSamples::Metadata* logged_meta) {
bcwhitefa8485b2017-05-01 16:43:251134 return WrapUnique(new CustomHistogram(name, ranges, counts, logged_counts,
1135 meta, logged_meta));
bcwhite5cb99eb2016-02-01 21:07:561136}
1137
[email protected]07c02402012-10-31 06:20:251138HistogramType CustomHistogram::GetHistogramType() const {
[email protected]5d91c9e2010-07-28 17:25:281139 return CUSTOM_HISTOGRAM;
1140}
1141
[email protected]961fefb2011-05-24 13:59:581142// static
Ryan Sleevi9c79c3542018-05-12 01:38:091143std::vector<Sample> CustomHistogram::ArrayToCustomEnumRanges(
1144 base::span<const Sample> values) {
asvitkine24d3e9a2015-05-27 05:22:141145 std::vector<Sample> all_values;
Ryan Sleevi9c79c3542018-05-12 01:38:091146 for (Sample value : values) {
[email protected]961fefb2011-05-24 13:59:581147 all_values.push_back(value);
1148
1149 // Ensure that a guard bucket is added. If we end up with duplicate
1150 // values, FactoryGet will take care of removing them.
1151 all_values.push_back(value + 1);
1152 }
1153 return all_values;
1154}
1155
Brian Whited1c91082017-11-03 14:46:421156CustomHistogram::CustomHistogram(const char* name, const BucketRanges* ranges)
Brian White16f612b42022-02-24 01:02:511157 : Histogram(name, ranges) {}
[email protected]70cc56e42010-04-29 22:39:551158
bcwhitefa8485b2017-05-01 16:43:251159CustomHistogram::CustomHistogram(
Brian Whited1c91082017-11-03 14:46:421160 const char* name,
bcwhitefa8485b2017-05-01 16:43:251161 const BucketRanges* ranges,
1162 const DelayedPersistentAllocation& counts,
1163 const DelayedPersistentAllocation& logged_counts,
1164 HistogramSamples::Metadata* meta,
1165 HistogramSamples::Metadata* logged_meta)
Brian White16f612b42022-02-24 01:02:511166 : Histogram(name, ranges, counts, logged_counts, meta, logged_meta) {}
bcwhite5cb99eb2016-02-01 21:07:561167
Daniel Cheng0d89f9222017-09-22 05:05:071168void CustomHistogram::SerializeInfoImpl(Pickle* pickle) const {
1169 Histogram::SerializeInfoImpl(pickle);
[email protected]cd56dff2011-11-13 04:19:151170
[email protected]c50c21d2013-01-11 21:52:441171 // Serialize ranges. First and last ranges are alwasy 0 and INT_MAX, so don't
1172 // write them.
Peter Kastingfc94f5062022-06-08 16:41:451173 for (size_t i = 1; i < bucket_ranges()->bucket_count(); ++i)
Daniel Cheng0d89f9222017-09-22 05:05:071174 pickle->WriteInt(bucket_ranges()->range(i));
[email protected]cd56dff2011-11-13 04:19:151175}
1176
[email protected]34d062322012-08-01 21:34:081177// static
[email protected]c50c21d2013-01-11 21:52:441178HistogramBase* CustomHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:141179 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:441180 int flags;
1181 int declared_min;
1182 int declared_max;
Peter Kastingfc94f5062022-06-08 16:41:451183 size_t bucket_count;
avi9b6f42932015-12-26 22:15:141184 uint32_t range_checksum;
[email protected]c50c21d2013-01-11 21:52:441185
1186 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
1187 &declared_max, &bucket_count, &range_checksum)) {
Brian White7eb91482017-08-09 19:54:451188 return nullptr;
[email protected]c50c21d2013-01-11 21:52:441189 }
1190
1191 // First and last ranges are not serialized.
asvitkine24d3e9a2015-05-27 05:22:141192 std::vector<Sample> sample_ranges(bucket_count - 1);
[email protected]c50c21d2013-01-11 21:52:441193
Peter Kastingfc94f5062022-06-08 16:41:451194 for (Sample& sample : sample_ranges) {
1195 if (!iter->ReadInt(&sample))
Brian White7eb91482017-08-09 19:54:451196 return nullptr;
[email protected]c50c21d2013-01-11 21:52:441197 }
1198
1199 HistogramBase* histogram = CustomHistogram::FactoryGet(
1200 histogram_name, sample_ranges, flags);
Brian White82027ff5d2017-08-21 19:50:221201 if (!histogram)
1202 return nullptr;
1203
[email protected]c50c21d2013-01-11 21:52:441204 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
1205 // The serialized histogram might be corrupted.
Brian White7eb91482017-08-09 19:54:451206 return nullptr;
[email protected]c50c21d2013-01-11 21:52:441207 }
1208 return histogram;
1209}
1210
1211// static
[email protected]34d062322012-08-01 21:34:081212bool CustomHistogram::ValidateCustomRanges(
asvitkine24d3e9a2015-05-27 05:22:141213 const std::vector<Sample>& custom_ranges) {
[email protected]640d95ef2012-08-04 06:23:031214 bool has_valid_range = false;
Peter Kastingfc94f5062022-06-08 16:41:451215 for (Sample sample : custom_ranges) {
[email protected]640d95ef2012-08-04 06:23:031216 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1)
[email protected]34d062322012-08-01 21:34:081217 return false;
[email protected]640d95ef2012-08-04 06:23:031218 if (sample != 0)
1219 has_valid_range = true;
[email protected]34d062322012-08-01 21:34:081220 }
[email protected]640d95ef2012-08-04 06:23:031221 return has_valid_range;
[email protected]34d062322012-08-01 21:34:081222}
1223
[email protected]835d7c82010-10-14 04:38:381224} // namespace base