Chromium Code Reviews| 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/sample_vector.h" | |
|
Ilya Sherman
2012/09/12 03:20:58
nit: Alpha-sort
kaiwang
2012/09/20 22:54:59
Done.
| |
| 10 #include "base/metrics/bucket_ranges.h" | |
| 11 #include "base/metrics/histogram_base.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 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 void StatsHistogram::InitializeBucketRanges(const Stats* stats, |
|
Ilya Sherman
2012/09/12 03:20:58
nit: Add a comment indicating that this is static
kaiwang
2012/09/20 22:54:59
Done.
| |
| 23 stats_ = NULL; | 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(); | |
| 24 } | 40 } |
| 25 | 41 |
| 26 StatsHistogram* StatsHistogram::FactoryGet(const std::string& name) { | 42 StatsHistogram* StatsHistogram::FactoryGet(const std::string& name, |
| 43 const Stats* stats) { | |
| 27 Sample minimum = 1; | 44 Sample minimum = 1; |
| 28 Sample maximum = disk_cache::Stats::kDataSizesLength - 1; | 45 Sample maximum = disk_cache::Stats::kDataSizesLength - 1; |
| 29 size_t bucket_count = disk_cache::Stats::kDataSizesLength; | 46 size_t bucket_count = disk_cache::Stats::kDataSizesLength; |
| 30 | |
| 31 Histogram* histogram = StatisticsRecorder::FindHistogram(name); | 47 Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 32 if (!histogram) { | 48 if (!histogram) { |
| 49 CHECK(stats); | |
|
Ilya Sherman
2012/09/12 03:20:58
nit: DCHECK
kaiwang
2012/09/20 22:54:59
Done.
| |
| 50 | |
| 51 BucketRanges* ranges = new BucketRanges(bucket_count + 1); | |
|
Ilya Sherman
2012/09/12 03:20:58
Who owns this memory? Please either write this in
kaiwang
2012/09/20 22:54:59
Done.
| |
| 52 InitializeBucketRanges(stats, ranges); | |
| 53 | |
| 33 // To avoid racy destruction at shutdown, the following will be leaked. | 54 // To avoid racy destruction at shutdown, the following will be leaked. |
| 34 StatsHistogram* stats_histogram = | 55 StatsHistogram* stats_histogram = |
| 35 new StatsHistogram(name, minimum, maximum, bucket_count); | 56 new StatsHistogram(name, minimum, maximum, bucket_count, ranges, stats); |
| 36 stats_histogram->SetFlags(kUmaTargetedHistogramFlag); | 57 stats_histogram->SetFlags(kUmaTargetedHistogramFlag); |
| 37 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate(stats_histogram); | 58 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate(stats_histogram); |
| 38 } | 59 } |
| 39 | 60 |
| 40 DCHECK(HISTOGRAM == histogram->histogram_type()); | 61 DCHECK(HISTOGRAM == histogram->histogram_type()); |
| 41 DCHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count)); | 62 DCHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count)); |
| 42 | 63 |
| 43 // We're preparing for an otherwise unsafe upcast by ensuring we have the | 64 // We're preparing for an otherwise unsafe upcast by ensuring we have the |
| 44 // proper class type. | 65 // proper class type. |
| 45 StatsHistogram* return_histogram = static_cast<StatsHistogram*>(histogram); | 66 StatsHistogram* return_histogram = static_cast<StatsHistogram*>(histogram); |
| 46 return return_histogram; | 67 return return_histogram; |
| 47 } | 68 } |
| 48 | 69 |
| 49 bool StatsHistogram::Init(const Stats* stats) { | 70 scoped_ptr<SampleVector> StatsHistogram::SnapshotSamples() const { |
| 50 DCHECK(stats); | 71 scoped_ptr<SampleVector> samples(new SampleVector(bucket_ranges())); |
| 51 if (stats_) | 72 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 | 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 |