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 #include "net/disk_cache/stats_histogram.h"
7 #include "base/debug/leak_annotations.h"
8 #include "base/logging.h"
9 #include "base/metrics/bucket_ranges.h"
10 #include "base/metrics/histogram_base.h"
11 #include "base/metrics/sample_vector.h"
12 #include "base/metrics/statistics_recorder.h"
13 #include "net/disk_cache/stats.h"
15 namespace disk_cache
{
17 using base::BucketRanges
;
18 using base::Histogram
;
19 using base::HistogramSamples
;
20 using base::SampleVector
;
21 using base::StatisticsRecorder
;
23 StatsHistogram::StatsHistogram(const std::string
& name
,
26 const BucketRanges
* ranges
,
28 : Histogram(name
, minimum
, maximum
, ranges
),
31 StatsHistogram::~StatsHistogram() {}
34 void StatsHistogram::InitializeBucketRanges(const Stats
* stats
,
35 BucketRanges
* ranges
) {
36 for (size_t i
= 0; i
< ranges
->size(); ++i
) {
37 ranges
->set_range(i
, stats
->GetBucketRange(i
));
39 ranges
->ResetChecksum();
42 StatsHistogram
* StatsHistogram::FactoryGet(const std::string
& name
,
45 Sample maximum
= disk_cache::Stats::kDataSizesLength
- 1;
46 size_t bucket_count
= disk_cache::Stats::kDataSizesLength
;
47 HistogramBase
* histogram
= StatisticsRecorder::FindHistogram(name
);
51 // To avoid racy destruction at shutdown, the following will be leaked.
52 BucketRanges
* ranges
= new BucketRanges(bucket_count
+ 1);
53 InitializeBucketRanges(stats
, ranges
);
54 const BucketRanges
* registered_ranges
=
55 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges
);
57 // To avoid racy destruction at shutdown, the following will be leaked.
58 StatsHistogram
* stats_histogram
=
59 new StatsHistogram(name
, minimum
, maximum
, registered_ranges
, stats
);
60 stats_histogram
->SetFlags(kUmaTargetedHistogramFlag
);
61 histogram
= StatisticsRecorder::RegisterOrDeleteDuplicate(stats_histogram
);
64 DCHECK(base::HISTOGRAM
== histogram
->GetHistogramType());
65 DCHECK(histogram
->HasConstructionArguments(minimum
, maximum
, bucket_count
));
67 // We're preparing for an otherwise unsafe upcast by ensuring we have the
69 StatsHistogram
* return_histogram
= static_cast<StatsHistogram
*>(histogram
);
70 return return_histogram
;
73 void StatsHistogram::Disable() {
77 scoped_ptr
<HistogramSamples
> StatsHistogram::SnapshotSamples() const {
78 scoped_ptr
<SampleVector
> samples(new SampleVector(bucket_ranges()));
80 stats_
->Snapshot(samples
.get());
82 // Only report UMA data once.
83 StatsHistogram
* mutable_me
= const_cast<StatsHistogram
*>(this);
84 mutable_me
->ClearFlags(kUmaTargetedHistogramFlag
);
86 return samples
.PassAs
<HistogramSamples
>();
89 int StatsHistogram::FindCorruption(const HistogramSamples
& samples
) const {
90 // This class won't monitor inconsistencies.
91 return HistogramBase::NO_INCONSISTENCIES
;
94 } // namespace disk_cache