1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
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.
10 #include "base/metrics/histogram.h"
17 #include "base/compiler_specific.h"
18 #include "base/debug/alias.h"
19 #include "base/logging.h"
20 #include "base/metrics/sample_vector.h"
21 #include "base/metrics/statistics_recorder.h"
22 #include "base/pickle.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/stringprintf.h"
25 #include "base/synchronization/lock.h"
26 #include "base/values.h"
35 bool ReadHistogramArguments(PickleIterator
* iter
,
36 string
* histogram_name
,
41 uint32
* range_checksum
) {
42 if (!iter
->ReadString(histogram_name
) ||
43 !iter
->ReadInt(flags
) ||
44 !iter
->ReadInt(declared_min
) ||
45 !iter
->ReadInt(declared_max
) ||
46 !iter
->ReadUInt64(bucket_count
) ||
47 !iter
->ReadUInt32(range_checksum
)) {
48 DLOG(ERROR
) << "Pickle error decoding Histogram: " << *histogram_name
;
52 // Since these fields may have come from an untrusted renderer, do additional
53 // checks above and beyond those in Histogram::Initialize()
54 if (*declared_max
<= 0 ||
56 *declared_max
< *declared_min
||
57 INT_MAX
/ sizeof(HistogramBase::Count
) <= *bucket_count
||
59 DLOG(ERROR
) << "Values error decoding Histogram: " << histogram_name
;
63 // We use the arguments to find or create the local version of the histogram
64 // in this process, so we need to clear the IPC flag.
65 DCHECK(*flags
& HistogramBase::kIPCSerializationSourceFlag
);
66 *flags
&= ~HistogramBase::kIPCSerializationSourceFlag
;
71 bool ValidateRangeChecksum(const HistogramBase
& histogram
,
72 uint32 range_checksum
) {
73 const Histogram
& casted_histogram
=
74 static_cast<const Histogram
&>(histogram
);
76 return casted_histogram
.bucket_ranges()->checksum() == range_checksum
;
81 typedef HistogramBase::Count Count
;
82 typedef HistogramBase::Sample Sample
;
85 const size_t Histogram::kBucketCount_MAX
= 16384u;
87 HistogramBase
* Histogram::FactoryGet(const string
& name
,
92 bool valid_arguments
=
93 InspectConstructionArguments(name
, &minimum
, &maximum
, &bucket_count
);
94 DCHECK(valid_arguments
);
96 HistogramBase
* histogram
= StatisticsRecorder::FindHistogram(name
);
98 // To avoid racy destruction at shutdown, the following will be leaked.
99 BucketRanges
* ranges
= new BucketRanges(bucket_count
+ 1);
100 InitializeBucketRanges(minimum
, maximum
, ranges
);
101 const BucketRanges
* registered_ranges
=
102 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
);
104 Histogram
* tentative_histogram
=
105 new Histogram(name
, minimum
, maximum
, registered_ranges
);
107 tentative_histogram
->SetFlags(flags
);
109 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram
);
112 DCHECK_EQ(HISTOGRAM
, histogram
->GetHistogramType());
113 if (!histogram
->HasConstructionArguments(minimum
, maximum
, bucket_count
)) {
114 // The construction arguments do not match the existing histogram. This can
115 // come about if an extension updates in the middle of a chrome run and has
116 // changed one of them, or simply by bad code within Chrome itself. We
117 // return NULL here with the expectation that bad code in Chrome will crash
118 // on dereference, but extension/Pepper APIs will guard against NULL and not
120 DLOG(ERROR
) << "Histogram " << name
<< " has bad construction arguments";
126 HistogramBase
* Histogram::FactoryTimeGet(const string
& name
,
131 return FactoryGet(name
, minimum
.InMilliseconds(), maximum
.InMilliseconds(),
132 bucket_count
, flags
);
135 // Calculate what range of values are held in each bucket.
136 // We have to be careful that we don't pick a ratio between starting points in
137 // consecutive buckets that is sooo small, that the integer bounds are the same
138 // (effectively making one bucket get no values). We need to avoid:
139 // ranges(i) == ranges(i + 1)
140 // To avoid that, we just do a fine-grained bucket width as far as we need to
141 // until we get a ratio that moves us along at least 2 units at a time. From
142 // that bucket onward we do use the exponential growth of buckets.
145 void Histogram::InitializeBucketRanges(Sample minimum
,
147 BucketRanges
* ranges
) {
148 double log_max
= log(static_cast<double>(maximum
));
151 size_t bucket_index
= 1;
152 Sample current
= minimum
;
153 ranges
->set_range(bucket_index
, current
);
154 size_t bucket_count
= ranges
->bucket_count();
155 while (bucket_count
> ++bucket_index
) {
157 log_current
= log(static_cast<double>(current
));
158 // Calculate the count'th root of the range.
159 log_ratio
= (log_max
- log_current
) / (bucket_count
- bucket_index
);
160 // See where the next bucket would start.
161 log_next
= log_current
+ log_ratio
;
163 next
= static_cast<int>(floor(exp(log_next
) + 0.5));
167 ++current
; // Just do a narrow bucket, and keep trying.
168 ranges
->set_range(bucket_index
, current
);
170 ranges
->set_range(ranges
->bucket_count(), HistogramBase::kSampleType_MAX
);
171 ranges
->ResetChecksum();
175 const int Histogram::kCommonRaceBasedCountMismatch
= 5;
177 int Histogram::FindCorruption(const HistogramSamples
& samples
) const {
178 int inconsistencies
= NO_INCONSISTENCIES
;
179 Sample previous_range
= -1; // Bottom range is always 0.
180 for (size_t index
= 0; index
< bucket_count(); ++index
) {
181 int new_range
= ranges(index
);
182 if (previous_range
>= new_range
)
183 inconsistencies
|= BUCKET_ORDER_ERROR
;
184 previous_range
= new_range
;
187 if (!bucket_ranges()->HasValidChecksum())
188 inconsistencies
|= RANGE_CHECKSUM_ERROR
;
190 int64 delta64
= samples
.redundant_count() - samples
.TotalCount();
192 int delta
= static_cast<int>(delta64
);
193 if (delta
!= delta64
)
194 delta
= INT_MAX
; // Flag all giant errors as INT_MAX.
196 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta
);
197 if (delta
> kCommonRaceBasedCountMismatch
)
198 inconsistencies
|= COUNT_HIGH_ERROR
;
201 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta
);
202 if (-delta
> kCommonRaceBasedCountMismatch
)
203 inconsistencies
|= COUNT_LOW_ERROR
;
206 return inconsistencies
;
209 Sample
Histogram::ranges(size_t i
) const {
210 return bucket_ranges_
->range(i
);
213 size_t Histogram::bucket_count() const {
214 return bucket_ranges_
->bucket_count();
218 bool Histogram::InspectConstructionArguments(const string
& name
,
221 size_t* bucket_count
) {
222 // Defensive code for backward compatibility.
224 DVLOG(1) << "Histogram: " << name
<< " has bad minimum: " << *minimum
;
227 if (*maximum
>= kSampleType_MAX
) {
228 DVLOG(1) << "Histogram: " << name
<< " has bad maximum: " << *maximum
;
229 *maximum
= kSampleType_MAX
- 1;
231 if (*bucket_count
>= kBucketCount_MAX
) {
232 DVLOG(1) << "Histogram: " << name
<< " has bad bucket_count: "
234 *bucket_count
= kBucketCount_MAX
- 1;
237 if (*minimum
>= *maximum
)
239 if (*bucket_count
< 3)
241 if (*bucket_count
> static_cast<size_t>(*maximum
- *minimum
+ 2))
246 HistogramType
Histogram::GetHistogramType() const {
250 bool Histogram::HasConstructionArguments(Sample expected_minimum
,
251 Sample expected_maximum
,
252 size_t expected_bucket_count
) const {
253 return ((expected_minimum
== declared_min_
) &&
254 (expected_maximum
== declared_max_
) &&
255 (expected_bucket_count
== bucket_count()));
258 void Histogram::Add(int value
) {
259 DCHECK_EQ(0, ranges(0));
260 DCHECK_EQ(kSampleType_MAX
, ranges(bucket_count()));
262 if (value
> kSampleType_MAX
- 1)
263 value
= kSampleType_MAX
- 1;
266 samples_
->Accumulate(value
, 1);
269 scoped_ptr
<HistogramSamples
> Histogram::SnapshotSamples() const {
270 return SnapshotSampleVector().PassAs
<HistogramSamples
>();
273 void Histogram::AddSamples(const HistogramSamples
& samples
) {
274 samples_
->Add(samples
);
277 bool Histogram::AddSamplesFromPickle(PickleIterator
* iter
) {
278 return samples_
->AddFromPickle(iter
);
281 // The following methods provide a graphical histogram display.
282 void Histogram::WriteHTMLGraph(string
* output
) const {
283 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
284 output
->append("<PRE>");
285 WriteAsciiImpl(true, "<br>", output
);
286 output
->append("</PRE>");
289 void Histogram::WriteAscii(string
* output
) const {
290 WriteAsciiImpl(true, "\n", output
);
293 bool Histogram::SerializeInfoImpl(Pickle
* pickle
) const {
294 DCHECK(bucket_ranges()->HasValidChecksum());
295 return pickle
->WriteString(histogram_name()) &&
296 pickle
->WriteInt(flags()) &&
297 pickle
->WriteInt(declared_min()) &&
298 pickle
->WriteInt(declared_max()) &&
299 pickle
->WriteUInt64(bucket_count()) &&
300 pickle
->WriteUInt32(bucket_ranges()->checksum());
303 Histogram::Histogram(const string
& name
,
306 const BucketRanges
* ranges
)
307 : HistogramBase(name
),
308 bucket_ranges_(ranges
),
309 declared_min_(minimum
),
310 declared_max_(maximum
) {
312 samples_
.reset(new SampleVector(ranges
));
315 Histogram::~Histogram() {
318 bool Histogram::PrintEmptyBucket(size_t index
) const {
322 // Use the actual bucket widths (like a linear histogram) until the widths get
323 // over some transition value, and then use that transition width. Exponentials
324 // get so big so fast (and we don't expect to see a lot of entries in the large
325 // buckets), so we need this to make it possible to see what is going on and
326 // not have 0-graphical-height buckets.
327 double Histogram::GetBucketSize(Count current
, size_t i
) const {
328 DCHECK_GT(ranges(i
+ 1), ranges(i
));
329 static const double kTransitionWidth
= 5;
330 double denominator
= ranges(i
+ 1) - ranges(i
);
331 if (denominator
> kTransitionWidth
)
332 denominator
= kTransitionWidth
; // Stop trying to normalize.
333 return current
/denominator
;
336 const string
Histogram::GetAsciiBucketRange(size_t i
) const {
337 return GetSimpleAsciiBucketRange(ranges(i
));
340 //------------------------------------------------------------------------------
344 HistogramBase
* Histogram::DeserializeInfoImpl(PickleIterator
* iter
) {
345 string histogram_name
;
350 uint32 range_checksum
;
352 if (!ReadHistogramArguments(iter
, &histogram_name
, &flags
, &declared_min
,
353 &declared_max
, &bucket_count
, &range_checksum
)) {
357 // Find or create the local version of the histogram in this process.
358 HistogramBase
* histogram
= Histogram::FactoryGet(
359 histogram_name
, declared_min
, declared_max
, bucket_count
, flags
);
361 if (!ValidateRangeChecksum(*histogram
, range_checksum
)) {
362 // The serialized histogram might be corrupted.
368 scoped_ptr
<SampleVector
> Histogram::SnapshotSampleVector() const {
369 scoped_ptr
<SampleVector
> samples(new SampleVector(bucket_ranges()));
370 samples
->Add(*samples_
);
371 return samples
.Pass();
374 void Histogram::WriteAsciiImpl(bool graph_it
,
375 const string
& newline
,
376 string
* output
) const {
377 // Get local (stack) copies of all effectively volatile class data so that we
378 // are consistent across our output activities.
379 scoped_ptr
<SampleVector
> snapshot
= SnapshotSampleVector();
380 Count sample_count
= snapshot
->TotalCount();
382 WriteAsciiHeader(*snapshot
, sample_count
, output
);
383 output
->append(newline
);
385 // Prepare to normalize graphical rendering of bucket contents.
388 max_size
= GetPeakBucketSize(*snapshot
);
390 // Calculate space needed to print bucket range numbers. Leave room to print
391 // nearly the largest bucket range without sliding over the histogram.
392 size_t largest_non_empty_bucket
= bucket_count() - 1;
393 while (0 == snapshot
->GetCountAtIndex(largest_non_empty_bucket
)) {
394 if (0 == largest_non_empty_bucket
)
395 break; // All buckets are empty.
396 --largest_non_empty_bucket
;
399 // Calculate largest print width needed for any of our bucket range displays.
400 size_t print_width
= 1;
401 for (size_t i
= 0; i
< bucket_count(); ++i
) {
402 if (snapshot
->GetCountAtIndex(i
)) {
403 size_t width
= GetAsciiBucketRange(i
).size() + 1;
404 if (width
> print_width
)
409 int64 remaining
= sample_count
;
411 // Output the actual histogram graph.
412 for (size_t i
= 0; i
< bucket_count(); ++i
) {
413 Count current
= snapshot
->GetCountAtIndex(i
);
414 if (!current
&& !PrintEmptyBucket(i
))
416 remaining
-= current
;
417 string range
= GetAsciiBucketRange(i
);
418 output
->append(range
);
419 for (size_t j
= 0; range
.size() + j
< print_width
+ 1; ++j
)
420 output
->push_back(' ');
421 if (0 == current
&& i
< bucket_count() - 1 &&
422 0 == snapshot
->GetCountAtIndex(i
+ 1)) {
423 while (i
< bucket_count() - 1 &&
424 0 == snapshot
->GetCountAtIndex(i
+ 1)) {
427 output
->append("... ");
428 output
->append(newline
);
429 continue; // No reason to plot emptiness.
431 double current_size
= GetBucketSize(current
, i
);
433 WriteAsciiBucketGraph(current_size
, max_size
, output
);
434 WriteAsciiBucketContext(past
, current
, remaining
, i
, output
);
435 output
->append(newline
);
438 DCHECK_EQ(sample_count
, past
);
441 double Histogram::GetPeakBucketSize(const SampleVector
& samples
) const {
443 for (size_t i
= 0; i
< bucket_count() ; ++i
) {
444 double current_size
= GetBucketSize(samples
.GetCountAtIndex(i
), i
);
445 if (current_size
> max
)
451 void Histogram::WriteAsciiHeader(const SampleVector
& samples
,
453 string
* output
) const {
454 StringAppendF(output
,
455 "Histogram: %s recorded %d samples",
456 histogram_name().c_str(),
458 if (0 == sample_count
) {
459 DCHECK_EQ(samples
.sum(), 0);
461 double average
= static_cast<float>(samples
.sum()) / sample_count
;
463 StringAppendF(output
, ", average = %.1f", average
);
465 if (flags() & ~kHexRangePrintingFlag
)
466 StringAppendF(output
, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag
);
469 void Histogram::WriteAsciiBucketContext(const int64 past
,
471 const int64 remaining
,
473 string
* output
) const {
474 double scaled_sum
= (past
+ current
+ remaining
) / 100.0;
475 WriteAsciiBucketValue(current
, scaled_sum
, output
);
477 double percentage
= past
/ scaled_sum
;
478 StringAppendF(output
, " {%3.1f%%}", percentage
);
482 void Histogram::GetParameters(DictionaryValue
* params
) const {
483 params
->SetString("type", HistogramTypeToString(GetHistogramType()));
484 params
->SetInteger("min", declared_min());
485 params
->SetInteger("max", declared_max());
486 params
->SetInteger("bucket_count", static_cast<int>(bucket_count()));
489 void Histogram::GetCountAndBucketData(Count
* count
,
491 ListValue
* buckets
) const {
492 scoped_ptr
<SampleVector
> snapshot
= SnapshotSampleVector();
493 *count
= snapshot
->TotalCount();
494 *sum
= snapshot
->sum();
496 for (size_t i
= 0; i
< bucket_count(); ++i
) {
497 Sample count
= snapshot
->GetCountAtIndex(i
);
499 scoped_ptr
<DictionaryValue
> bucket_value(new DictionaryValue());
500 bucket_value
->SetInteger("low", ranges(i
));
501 if (i
!= bucket_count() - 1)
502 bucket_value
->SetInteger("high", ranges(i
+ 1));
503 bucket_value
->SetInteger("count", count
);
504 buckets
->Set(index
, bucket_value
.release());
510 //------------------------------------------------------------------------------
511 // LinearHistogram: This histogram uses a traditional set of evenly spaced
513 //------------------------------------------------------------------------------
515 LinearHistogram::~LinearHistogram() {}
517 HistogramBase
* LinearHistogram::FactoryGet(const string
& name
,
522 return FactoryGetWithRangeDescription(
523 name
, minimum
, maximum
, bucket_count
, flags
, NULL
);
526 HistogramBase
* LinearHistogram::FactoryTimeGet(const string
& name
,
531 return FactoryGet(name
, minimum
.InMilliseconds(), maximum
.InMilliseconds(),
532 bucket_count
, flags
);
535 HistogramBase
* LinearHistogram::FactoryGetWithRangeDescription(
536 const std::string
& name
,
541 const DescriptionPair descriptions
[]) {
542 bool valid_arguments
= Histogram::InspectConstructionArguments(
543 name
, &minimum
, &maximum
, &bucket_count
);
544 DCHECK(valid_arguments
);
546 HistogramBase
* histogram
= StatisticsRecorder::FindHistogram(name
);
548 // To avoid racy destruction at shutdown, the following will be leaked.
549 BucketRanges
* ranges
= new BucketRanges(bucket_count
+ 1);
550 InitializeBucketRanges(minimum
, maximum
, ranges
);
551 const BucketRanges
* registered_ranges
=
552 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
);
554 LinearHistogram
* tentative_histogram
=
555 new LinearHistogram(name
, minimum
, maximum
, registered_ranges
);
557 // Set range descriptions.
559 for (int i
= 0; descriptions
[i
].description
; ++i
) {
560 tentative_histogram
->bucket_description_
[descriptions
[i
].sample
] =
561 descriptions
[i
].description
;
565 tentative_histogram
->SetFlags(flags
);
567 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram
);
570 DCHECK_EQ(LINEAR_HISTOGRAM
, histogram
->GetHistogramType());
571 if (!histogram
->HasConstructionArguments(minimum
, maximum
, bucket_count
)) {
572 // The construction arguments do not match the existing histogram. This can
573 // come about if an extension updates in the middle of a chrome run and has
574 // changed one of them, or simply by bad code within Chrome itself. We
575 // return NULL here with the expectation that bad code in Chrome will crash
576 // on dereference, but extension/Pepper APIs will guard against NULL and not
578 DLOG(ERROR
) << "Histogram " << name
<< " has bad construction arguments";
584 HistogramType
LinearHistogram::GetHistogramType() const {
585 return LINEAR_HISTOGRAM
;
588 LinearHistogram::LinearHistogram(const string
& name
,
591 const BucketRanges
* ranges
)
592 : Histogram(name
, minimum
, maximum
, ranges
) {
595 double LinearHistogram::GetBucketSize(Count current
, size_t i
) const {
596 DCHECK_GT(ranges(i
+ 1), ranges(i
));
597 // Adjacent buckets with different widths would have "surprisingly" many (few)
598 // samples in a histogram if we didn't normalize this way.
599 double denominator
= ranges(i
+ 1) - ranges(i
);
600 return current
/denominator
;
603 const string
LinearHistogram::GetAsciiBucketRange(size_t i
) const {
604 int range
= ranges(i
);
605 BucketDescriptionMap::const_iterator it
= bucket_description_
.find(range
);
606 if (it
== bucket_description_
.end())
607 return Histogram::GetAsciiBucketRange(i
);
611 bool LinearHistogram::PrintEmptyBucket(size_t index
) const {
612 return bucket_description_
.find(ranges(index
)) == bucket_description_
.end();
616 void LinearHistogram::InitializeBucketRanges(Sample minimum
,
618 BucketRanges
* ranges
) {
619 double min
= minimum
;
620 double max
= maximum
;
621 size_t bucket_count
= ranges
->bucket_count();
622 for (size_t i
= 1; i
< bucket_count
; ++i
) {
623 double linear_range
=
624 (min
* (bucket_count
- 1 - i
) + max
* (i
- 1)) / (bucket_count
- 2);
625 ranges
->set_range(i
, static_cast<Sample
>(linear_range
+ 0.5));
627 ranges
->set_range(ranges
->bucket_count(), HistogramBase::kSampleType_MAX
);
628 ranges
->ResetChecksum();
632 HistogramBase
* LinearHistogram::DeserializeInfoImpl(PickleIterator
* iter
) {
633 string histogram_name
;
638 uint32 range_checksum
;
640 if (!ReadHistogramArguments(iter
, &histogram_name
, &flags
, &declared_min
,
641 &declared_max
, &bucket_count
, &range_checksum
)) {
645 HistogramBase
* histogram
= LinearHistogram::FactoryGet(
646 histogram_name
, declared_min
, declared_max
, bucket_count
, flags
);
647 if (!ValidateRangeChecksum(*histogram
, range_checksum
)) {
648 // The serialized histogram might be corrupted.
654 //------------------------------------------------------------------------------
655 // This section provides implementation for BooleanHistogram.
656 //------------------------------------------------------------------------------
658 HistogramBase
* BooleanHistogram::FactoryGet(const string
& name
, int32 flags
) {
659 HistogramBase
* histogram
= StatisticsRecorder::FindHistogram(name
);
661 // To avoid racy destruction at shutdown, the following will be leaked.
662 BucketRanges
* ranges
= new BucketRanges(4);
663 LinearHistogram::InitializeBucketRanges(1, 2, ranges
);
664 const BucketRanges
* registered_ranges
=
665 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
);
667 BooleanHistogram
* tentative_histogram
=
668 new BooleanHistogram(name
, registered_ranges
);
670 tentative_histogram
->SetFlags(flags
);
672 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram
);
675 DCHECK_EQ(BOOLEAN_HISTOGRAM
, histogram
->GetHistogramType());
679 HistogramType
BooleanHistogram::GetHistogramType() const {
680 return BOOLEAN_HISTOGRAM
;
683 BooleanHistogram::BooleanHistogram(const string
& name
,
684 const BucketRanges
* ranges
)
685 : LinearHistogram(name
, 1, 2, ranges
) {}
687 HistogramBase
* BooleanHistogram::DeserializeInfoImpl(PickleIterator
* iter
) {
688 string histogram_name
;
693 uint32 range_checksum
;
695 if (!ReadHistogramArguments(iter
, &histogram_name
, &flags
, &declared_min
,
696 &declared_max
, &bucket_count
, &range_checksum
)) {
700 HistogramBase
* histogram
= BooleanHistogram::FactoryGet(
701 histogram_name
, flags
);
702 if (!ValidateRangeChecksum(*histogram
, range_checksum
)) {
703 // The serialized histogram might be corrupted.
709 //------------------------------------------------------------------------------
711 //------------------------------------------------------------------------------
713 HistogramBase
* CustomHistogram::FactoryGet(const string
& name
,
714 const vector
<Sample
>& custom_ranges
,
716 CHECK(ValidateCustomRanges(custom_ranges
));
718 HistogramBase
* histogram
= StatisticsRecorder::FindHistogram(name
);
720 BucketRanges
* ranges
= CreateBucketRangesFromCustomRanges(custom_ranges
);
721 const BucketRanges
* registered_ranges
=
722 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
);
724 // To avoid racy destruction at shutdown, the following will be leaked.
725 CustomHistogram
* tentative_histogram
=
726 new CustomHistogram(name
, registered_ranges
);
728 tentative_histogram
->SetFlags(flags
);
731 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram
);
734 DCHECK_EQ(histogram
->GetHistogramType(), CUSTOM_HISTOGRAM
);
738 HistogramType
CustomHistogram::GetHistogramType() const {
739 return CUSTOM_HISTOGRAM
;
743 vector
<Sample
> CustomHistogram::ArrayToCustomRanges(
744 const Sample
* values
, size_t num_values
) {
745 vector
<Sample
> all_values
;
746 for (size_t i
= 0; i
< num_values
; ++i
) {
747 Sample value
= values
[i
];
748 all_values
.push_back(value
);
750 // Ensure that a guard bucket is added. If we end up with duplicate
751 // values, FactoryGet will take care of removing them.
752 all_values
.push_back(value
+ 1);
757 CustomHistogram::CustomHistogram(const string
& name
,
758 const BucketRanges
* ranges
)
761 ranges
->range(ranges
->bucket_count() - 1),
764 bool CustomHistogram::SerializeInfoImpl(Pickle
* pickle
) const {
765 if (!Histogram::SerializeInfoImpl(pickle
))
768 // Serialize ranges. First and last ranges are alwasy 0 and INT_MAX, so don't
770 for (size_t i
= 1; i
< bucket_ranges()->bucket_count(); ++i
) {
771 if (!pickle
->WriteInt(bucket_ranges()->range(i
)))
777 double CustomHistogram::GetBucketSize(Count current
, size_t i
) const {
782 HistogramBase
* CustomHistogram::DeserializeInfoImpl(PickleIterator
* iter
) {
783 string histogram_name
;
788 uint32 range_checksum
;
790 if (!ReadHistogramArguments(iter
, &histogram_name
, &flags
, &declared_min
,
791 &declared_max
, &bucket_count
, &range_checksum
)) {
795 // First and last ranges are not serialized.
796 vector
<Sample
> sample_ranges(bucket_count
- 1);
798 for (size_t i
= 0; i
< sample_ranges
.size(); ++i
) {
799 if (!iter
->ReadInt(&sample_ranges
[i
]))
803 HistogramBase
* histogram
= CustomHistogram::FactoryGet(
804 histogram_name
, sample_ranges
, flags
);
805 if (!ValidateRangeChecksum(*histogram
, range_checksum
)) {
806 // The serialized histogram might be corrupted.
813 bool CustomHistogram::ValidateCustomRanges(
814 const vector
<Sample
>& custom_ranges
) {
815 bool has_valid_range
= false;
816 for (size_t i
= 0; i
< custom_ranges
.size(); i
++) {
817 Sample sample
= custom_ranges
[i
];
818 if (sample
< 0 || sample
> HistogramBase::kSampleType_MAX
- 1)
821 has_valid_range
= true;
823 return has_valid_range
;
827 BucketRanges
* CustomHistogram::CreateBucketRangesFromCustomRanges(
828 const vector
<Sample
>& custom_ranges
) {
829 // Remove the duplicates in the custom ranges array.
830 vector
<int> ranges
= custom_ranges
;
831 ranges
.push_back(0); // Ensure we have a zero value.
832 ranges
.push_back(HistogramBase::kSampleType_MAX
);
833 std::sort(ranges
.begin(), ranges
.end());
834 ranges
.erase(std::unique(ranges
.begin(), ranges
.end()), ranges
.end());
836 BucketRanges
* bucket_ranges
= new BucketRanges(ranges
.size());
837 for (size_t i
= 0; i
< ranges
.size(); i
++) {
838 bucket_ranges
->set_range(i
, ranges
[i
]);
840 bucket_ranges
->ResetChecksum();
841 return bucket_ranges
;