• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2008 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 // Static.
13 const Stats* StatsHistogram::stats_ = NULL;
14 
StatsHistogramFactoryGet(const std::string & name)15 scoped_refptr<StatsHistogram> StatsHistogram::StatsHistogramFactoryGet(
16     const std::string& name) {
17   scoped_refptr<Histogram> histogram(NULL);
18 
19   Sample minimum = 1;
20   Sample maximum = disk_cache::Stats::kDataSizesLength - 1;
21   size_t bucket_count = disk_cache::Stats::kDataSizesLength;
22 
23   if (StatisticsRecorder::FindHistogram(name, &histogram)) {
24     DCHECK(histogram.get() != NULL);
25   } else {
26     histogram = new StatsHistogram(name, minimum, maximum, bucket_count);
27     scoped_refptr<Histogram> registered_histogram(NULL);
28     StatisticsRecorder::FindHistogram(name, &registered_histogram);
29     if (registered_histogram.get() != NULL &&
30         registered_histogram.get() != histogram.get())
31       histogram = registered_histogram;
32   }
33 
34   DCHECK(HISTOGRAM == histogram->histogram_type());
35   DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count));
36 
37   // We're preparing for an otherwise unsafe upcast by ensuring we have the
38   // proper class type.
39   Histogram* temp_histogram = histogram.get();
40   StatsHistogram* temp_stats_histogram =
41       static_cast<StatsHistogram*>(temp_histogram);
42   scoped_refptr<StatsHistogram> return_histogram = temp_stats_histogram;
43   return return_histogram;
44 }
45 
Init(const Stats * stats)46 bool StatsHistogram::Init(const Stats* stats) {
47   DCHECK(stats);
48   if (stats_)
49     return false;
50 
51   SetFlags(kUmaTargetedHistogramFlag);
52 
53   // We support statistics report for only one cache.
54   init_ = true;
55   stats_ = stats;
56   return true;
57 }
58 
~StatsHistogram()59 StatsHistogram::~StatsHistogram() {
60   // Only cleanup what we set.
61   if (init_)
62     stats_ = NULL;
63 }
64 
ranges(size_t i) const65 Histogram::Sample StatsHistogram::ranges(size_t i) const {
66   DCHECK(stats_);
67   return stats_->GetBucketRange(i);
68 }
69 
bucket_count() const70 size_t StatsHistogram::bucket_count() const {
71   return disk_cache::Stats::kDataSizesLength;
72 }
73 
SnapshotSample(SampleSet * sample) const74 void StatsHistogram::SnapshotSample(SampleSet* sample) const {
75   DCHECK(stats_);
76   StatsSamples my_sample;
77   stats_->Snapshot(&my_sample);
78 
79   *sample = my_sample;
80 
81   // Only report UMA data once.
82   StatsHistogram* mutable_me = const_cast<StatsHistogram*>(this);
83   mutable_me->ClearFlags(kUmaTargetedHistogramFlag);
84 }
85 
86 }  // namespace disk_cache
87