1 // Copyright (c) 2011 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. 4 5 #include "net/disk_cache/stats_histogram.h" 6 7 #include "base/logging.h" 8 #include "net/disk_cache/stats.h" 9 10 namespace disk_cache { 11 12 using base::Histogram; 13 using base::StatisticsRecorder; 14 15 // Static. 16 const Stats* StatsHistogram::stats_ = NULL; 17 ~StatsHistogram()18StatsHistogram::~StatsHistogram() { 19 // Only cleanup what we set. 20 if (init_) 21 stats_ = NULL; 22 } 23 StatsHistogramFactoryGet(const std::string & name)24StatsHistogram* StatsHistogram::StatsHistogramFactoryGet( 25 const std::string& name) { 26 Histogram* histogram(NULL); 27 28 Sample minimum = 1; 29 Sample maximum = disk_cache::Stats::kDataSizesLength - 1; 30 size_t bucket_count = disk_cache::Stats::kDataSizesLength; 31 32 if (StatisticsRecorder::FindHistogram(name, &histogram)) { 33 DCHECK(histogram != NULL); 34 } else { 35 // To avoid racy destruction at shutdown, the following will be leaked. 36 StatsHistogram* stats_histogram = 37 new StatsHistogram(name, minimum, maximum, bucket_count); 38 stats_histogram->InitializeBucketRange(); 39 stats_histogram->SetFlags(kUmaTargetedHistogramFlag); 40 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate(stats_histogram); 41 } 42 43 DCHECK(HISTOGRAM == histogram->histogram_type()); 44 DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count)); 45 46 // We're preparing for an otherwise unsafe upcast by ensuring we have the 47 // proper class type. 48 StatsHistogram* return_histogram = static_cast<StatsHistogram*>(histogram); 49 // Validate upcast by seeing that we're probably providing the checksum. 50 CHECK_EQ(return_histogram->StatsHistogram::CalculateRangeChecksum(), 51 return_histogram->CalculateRangeChecksum()); 52 return return_histogram; 53 } 54 Init(const Stats * stats)55bool StatsHistogram::Init(const Stats* stats) { 56 DCHECK(stats); 57 if (stats_) 58 return false; 59 60 // We support statistics report for only one cache. 61 init_ = true; 62 stats_ = stats; 63 return true; 64 } 65 ranges(size_t i) const66Histogram::Sample StatsHistogram::ranges(size_t i) const { 67 DCHECK(stats_); 68 return stats_->GetBucketRange(i); 69 } 70 bucket_count() const71size_t StatsHistogram::bucket_count() const { 72 return disk_cache::Stats::kDataSizesLength; 73 } 74 SnapshotSample(SampleSet * sample) const75void StatsHistogram::SnapshotSample(SampleSet* sample) const { 76 DCHECK(stats_); 77 StatsSamples my_sample; 78 stats_->Snapshot(&my_sample); 79 80 *sample = my_sample; 81 82 // Only report UMA data once. 83 StatsHistogram* mutable_me = const_cast<StatsHistogram*>(this); 84 mutable_me->ClearFlags(kUmaTargetedHistogramFlag); 85 } 86 FindCorruption(const SampleSet & snapshot) const87Histogram::Inconsistencies StatsHistogram::FindCorruption( 88 const SampleSet& snapshot) const { 89 return NO_INCONSISTENCIES; // This class won't monitor inconsistencies. 90 } 91 CalculateRangeChecksum() const92uint32 StatsHistogram::CalculateRangeChecksum() const { 93 // We don't calculate checksums, so at least establish a unique constant. 94 const uint32 kStatsHistogramChecksum = 0x0cecce; 95 return kStatsHistogramChecksum; 96 } 97 98 } // namespace disk_cache 99