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