Home | History | Annotate | Download | only in disk_cache
      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.
      4 
      5 #include "net/disk_cache/stats_histogram.h"
      6 
      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"
     14 
     15 namespace disk_cache {
     16 
     17 using base::BucketRanges;
     18 using base::Histogram;
     19 using base::HistogramSamples;
     20 using base::SampleVector;
     21 using base::StatisticsRecorder;
     22 
     23 StatsHistogram::StatsHistogram(const std::string& name,
     24                                Sample minimum,
     25                                Sample maximum,
     26                                const BucketRanges* ranges,
     27                                const Stats* stats)
     28     : Histogram(name, minimum, maximum, ranges),
     29       stats_(stats) {}
     30 
     31 StatsHistogram::~StatsHistogram() {}
     32 
     33 // static
     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));
     38   }
     39   ranges->ResetChecksum();
     40 }
     41 
     42 StatsHistogram* StatsHistogram::FactoryGet(const std::string& name,
     43                                            const Stats* stats) {
     44   Sample minimum = 1;
     45   Sample maximum = disk_cache::Stats::kDataSizesLength - 1;
     46   size_t bucket_count = disk_cache::Stats::kDataSizesLength;
     47   HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
     48   if (!histogram) {
     49     DCHECK(stats);
     50 
     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);
     56 
     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);
     62   }
     63 
     64   DCHECK(base::HISTOGRAM == histogram->GetHistogramType());
     65   DCHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count));
     66 
     67   // We're preparing for an otherwise unsafe upcast by ensuring we have the
     68   // proper class type.
     69   StatsHistogram* return_histogram = static_cast<StatsHistogram*>(histogram);
     70   return return_histogram;
     71 }
     72 
     73 void StatsHistogram::Disable() {
     74   stats_ = NULL;
     75 }
     76 
     77 scoped_ptr<HistogramSamples> StatsHistogram::SnapshotSamples() const {
     78   scoped_ptr<SampleVector> samples(new SampleVector(bucket_ranges()));
     79   if (stats_)
     80      stats_->Snapshot(samples.get());
     81 
     82   // Only report UMA data once.
     83   StatsHistogram* mutable_me = const_cast<StatsHistogram*>(this);
     84   mutable_me->ClearFlags(kUmaTargetedHistogramFlag);
     85 
     86   return samples.PassAs<HistogramSamples>();
     87 }
     88 
     89 int StatsHistogram::FindCorruption(const HistogramSamples& samples) const {
     90   // This class won't monitor inconsistencies.
     91   return HistogramBase::NO_INCONSISTENCIES;
     92 }
     93 
     94 }  // namespace disk_cache
     95