OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/stats_histogram.h" | 5 #include "net/disk_cache/stats_histogram.h" |
6 | 6 |
7 #include "base/debug/leak_annotations.h" | 7 #include "base/debug/leak_annotations.h" |
8 #include "base/logging.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" |
9 #include "base/metrics/statistics_recorder.h" | 12 #include "base/metrics/statistics_recorder.h" |
10 #include "net/disk_cache/stats.h" | 13 #include "net/disk_cache/stats.h" |
11 | 14 |
12 namespace disk_cache { | 15 namespace disk_cache { |
13 | 16 |
| 17 using base::BucketRanges; |
14 using base::Histogram; | 18 using base::Histogram; |
| 19 using base::HistogramSamples; |
| 20 using base::SampleVector; |
15 using base::StatisticsRecorder; | 21 using base::StatisticsRecorder; |
16 | 22 |
17 // Static. | 23 StatsHistogram::StatsHistogram(const std::string& name, |
18 const Stats* StatsHistogram::stats_ = NULL; | 24 Sample minimum, |
| 25 Sample maximum, |
| 26 size_t bucket_count, |
| 27 const BucketRanges* ranges, |
| 28 const Stats* stats) |
| 29 : Histogram(name, minimum, maximum, bucket_count, ranges), |
| 30 stats_(stats) {} |
19 | 31 |
20 StatsHistogram::~StatsHistogram() { | 32 StatsHistogram::~StatsHistogram() {} |
21 // Only cleanup what we set. | 33 |
22 if (init_) | 34 // static |
23 stats_ = NULL; | 35 void StatsHistogram::InitializeBucketRanges(const Stats* stats, |
| 36 BucketRanges* ranges) { |
| 37 for (size_t i = 0; i < ranges->size(); i++) { |
| 38 ranges->set_range(i, stats->GetBucketRange(i)); |
| 39 } |
| 40 ranges->ResetChecksum(); |
24 } | 41 } |
25 | 42 |
26 StatsHistogram* StatsHistogram::FactoryGet(const std::string& name) { | 43 StatsHistogram* StatsHistogram::FactoryGet(const std::string& name, |
| 44 const Stats* stats) { |
27 Sample minimum = 1; | 45 Sample minimum = 1; |
28 Sample maximum = disk_cache::Stats::kDataSizesLength - 1; | 46 Sample maximum = disk_cache::Stats::kDataSizesLength - 1; |
29 size_t bucket_count = disk_cache::Stats::kDataSizesLength; | 47 size_t bucket_count = disk_cache::Stats::kDataSizesLength; |
30 | |
31 Histogram* histogram = StatisticsRecorder::FindHistogram(name); | 48 Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
32 if (!histogram) { | 49 if (!histogram) { |
| 50 DCHECK(stats); |
| 51 |
| 52 // To avoid racy destruction at shutdown, the following will be leaked. |
| 53 BucketRanges* ranges = new BucketRanges(bucket_count + 1); |
| 54 InitializeBucketRanges(stats, ranges); |
| 55 const BucketRanges* registered_ranges = |
| 56 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 57 |
33 // To avoid racy destruction at shutdown, the following will be leaked. | 58 // To avoid racy destruction at shutdown, the following will be leaked. |
34 StatsHistogram* stats_histogram = | 59 StatsHistogram* stats_histogram = |
35 new StatsHistogram(name, minimum, maximum, bucket_count); | 60 new StatsHistogram(name, minimum, maximum, bucket_count, |
| 61 registered_ranges, stats); |
36 stats_histogram->SetFlags(kUmaTargetedHistogramFlag); | 62 stats_histogram->SetFlags(kUmaTargetedHistogramFlag); |
37 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate(stats_histogram); | 63 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate(stats_histogram); |
38 } | 64 } |
39 | 65 |
40 DCHECK(HISTOGRAM == histogram->histogram_type()); | 66 DCHECK(HISTOGRAM == histogram->histogram_type()); |
41 DCHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count)); | 67 DCHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count)); |
42 | 68 |
43 // We're preparing for an otherwise unsafe upcast by ensuring we have the | 69 // We're preparing for an otherwise unsafe upcast by ensuring we have the |
44 // proper class type. | 70 // proper class type. |
45 StatsHistogram* return_histogram = static_cast<StatsHistogram*>(histogram); | 71 StatsHistogram* return_histogram = static_cast<StatsHistogram*>(histogram); |
46 return return_histogram; | 72 return return_histogram; |
47 } | 73 } |
48 | 74 |
49 bool StatsHistogram::Init(const Stats* stats) { | 75 scoped_ptr<SampleVector> StatsHistogram::SnapshotSamples() const { |
50 DCHECK(stats); | 76 scoped_ptr<SampleVector> samples(new SampleVector(bucket_ranges())); |
51 if (stats_) | 77 stats_->Snapshot(samples.get()); |
52 return false; | |
53 | |
54 // We support statistics report for only one cache. | |
55 init_ = true; | |
56 stats_ = stats; | |
57 return true; | |
58 } | |
59 | |
60 Histogram::Sample StatsHistogram::ranges(size_t i) const { | |
61 DCHECK(stats_); | |
62 return stats_->GetBucketRange(i); | |
63 } | |
64 | |
65 size_t StatsHistogram::bucket_count() const { | |
66 return disk_cache::Stats::kDataSizesLength; | |
67 } | |
68 | |
69 void StatsHistogram::SnapshotSample(SampleSet* sample) const { | |
70 DCHECK(stats_); | |
71 StatsSamples my_sample; | |
72 stats_->Snapshot(&my_sample); | |
73 | |
74 *sample = my_sample; | |
75 | 78 |
76 // Only report UMA data once. | 79 // Only report UMA data once. |
77 StatsHistogram* mutable_me = const_cast<StatsHistogram*>(this); | 80 StatsHistogram* mutable_me = const_cast<StatsHistogram*>(this); |
78 mutable_me->ClearFlags(kUmaTargetedHistogramFlag); | 81 mutable_me->ClearFlags(kUmaTargetedHistogramFlag); |
| 82 |
| 83 return samples.Pass(); |
79 } | 84 } |
80 | 85 |
81 Histogram::Inconsistencies StatsHistogram::FindCorruption( | 86 Histogram::Inconsistencies StatsHistogram::FindCorruption( |
82 const SampleSet& snapshot) const { | 87 const HistogramSamples& samples) const { |
83 return NO_INCONSISTENCIES; // This class won't monitor inconsistencies. | 88 return NO_INCONSISTENCIES; // This class won't monitor inconsistencies. |
84 } | 89 } |
85 | 90 |
86 } // namespace disk_cache | 91 } // namespace disk_cache |
OLD | NEW |