| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/blockfile/stats.h" | 5 #include "net/disk_cache/blockfile/stats.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/bucket_ranges.h" |
| 10 #include "base/metrics/histogram.h" |
| 9 #include "base/metrics/histogram_samples.h" | 11 #include "base/metrics/histogram_samples.h" |
| 12 #include "base/metrics/sample_vector.h" |
| 13 #include "base/metrics/statistics_recorder.h" |
| 10 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 12 | 16 |
| 13 namespace { | 17 namespace { |
| 14 | 18 |
| 15 const int32 kDiskSignature = 0xF01427E0; | 19 const int32 kDiskSignature = 0xF01427E0; |
| 16 | 20 |
| 17 struct OnDiskStats { | 21 struct OnDiskStats { |
| 18 int32 signature; | 22 int32 signature; |
| 19 int size; | 23 int size; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 stats->signature = kDiskSignature; | 85 stats->signature = kDiskSignature; |
| 82 } else if (static_cast<unsigned int>(stats->size) != sizeof(*stats)) { | 86 } else if (static_cast<unsigned int>(stats->size) != sizeof(*stats)) { |
| 83 size_t delta = sizeof(*stats) - static_cast<unsigned int>(stats->size); | 87 size_t delta = sizeof(*stats) - static_cast<unsigned int>(stats->size); |
| 84 memset(reinterpret_cast<char*>(stats) + stats->size, 0, delta); | 88 memset(reinterpret_cast<char*>(stats) + stats->size, 0, delta); |
| 85 stats->size = sizeof(*stats); | 89 stats->size = sizeof(*stats); |
| 86 } | 90 } |
| 87 | 91 |
| 88 return true; | 92 return true; |
| 89 } | 93 } |
| 90 | 94 |
| 91 Stats::Stats() : size_histogram_(NULL) { | 95 Stats::Stats() { |
| 92 } | 96 } |
| 93 | 97 |
| 94 Stats::~Stats() { | 98 Stats::~Stats() { |
| 95 if (size_histogram_) { | |
| 96 size_histogram_->Disable(); | |
| 97 } | |
| 98 } | 99 } |
| 99 | 100 |
| 100 bool Stats::Init(void* data, int num_bytes, Addr address) { | 101 bool Stats::Init(void* data, int num_bytes, Addr address) { |
| 101 OnDiskStats local_stats; | 102 OnDiskStats local_stats; |
| 102 OnDiskStats* stats = &local_stats; | 103 OnDiskStats* stats = &local_stats; |
| 103 if (!num_bytes) { | 104 if (!num_bytes) { |
| 104 memset(stats, 0, sizeof(local_stats)); | 105 memset(stats, 0, sizeof(local_stats)); |
| 105 local_stats.signature = kDiskSignature; | 106 local_stats.signature = kDiskSignature; |
| 106 local_stats.size = sizeof(local_stats); | 107 local_stats.size = sizeof(local_stats); |
| 107 } else if (num_bytes >= static_cast<int>(sizeof(*stats))) { | 108 } else if (num_bytes >= static_cast<int>(sizeof(*stats))) { |
| 108 stats = reinterpret_cast<OnDiskStats*>(data); | 109 stats = reinterpret_cast<OnDiskStats*>(data); |
| 109 if (!VerifyStats(stats)) | 110 if (!VerifyStats(stats)) |
| 110 return false; | 111 return false; |
| 111 } else { | 112 } else { |
| 112 return false; | 113 return false; |
| 113 } | 114 } |
| 114 | 115 |
| 115 storage_addr_ = address; | 116 storage_addr_ = address; |
| 116 | 117 |
| 117 memcpy(data_sizes_, stats->data_sizes, sizeof(data_sizes_)); | 118 memcpy(data_sizes_, stats->data_sizes, sizeof(data_sizes_)); |
| 118 memcpy(counters_, stats->counters, sizeof(counters_)); | 119 memcpy(counters_, stats->counters, sizeof(counters_)); |
| 119 | 120 |
| 120 // Clean up old value. | 121 // Clean up old value. |
| 121 SetCounter(UNUSED, 0); | 122 SetCounter(UNUSED, 0); |
| 122 return true; | 123 return true; |
| 123 } | 124 } |
| 124 | 125 |
| 125 void Stats::InitSizeHistogram() { | 126 void Stats::InitSizeHistogram() { |
| 126 // It seems impossible to support this histogram for more than one | 127 // Only generate this histogram for the main cache. |
| 127 // simultaneous objects with the current infrastructure. | |
| 128 static bool first_time = true; | 128 static bool first_time = true; |
| 129 if (first_time) { | 129 if (!first_time) |
| 130 first_time = false; | 130 return; |
| 131 if (!size_histogram_) { | 131 |
| 132 // Stats may be reused when the cache is re-created, but we want only one | 132 first_time = false; |
| 133 // histogram at any given time. | 133 int min = 1; |
| 134 size_histogram_ = StatsHistogram::FactoryGet("DiskCache.SizeStats", this); | 134 int max = 64 * 1024; |
| 135 } | 135 int num_buckets = 75; |
| 136 base::BucketRanges ranges(num_buckets + 1); |
| 137 base::Histogram::InitializeBucketRanges(min, max, &ranges); |
| 138 |
| 139 base::HistogramBase* stats_histogram = base::Histogram::FactoryGet( |
| 140 "DiskCache.SizeStats2", min, max, num_buckets, |
| 141 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 142 |
| 143 base::SampleVector samples(&ranges); |
| 144 for (int i = 0; i < kDataSizesLength; i++) { |
| 145 // This is a good time to fix any inconsistent data. The count should be |
| 146 // always positive, but if it's not, reset the value now. |
| 147 if (data_sizes_[i] < 0) |
| 148 data_sizes_[i] = 0; |
| 149 |
| 150 samples.Accumulate(GetBucketRange(i) / 1024, data_sizes_[i]); |
| 136 } | 151 } |
| 152 stats_histogram->AddSamples(samples); |
| 137 } | 153 } |
| 138 | 154 |
| 139 int Stats::StorageSize() { | 155 int Stats::StorageSize() { |
| 140 // If we have more than 512 bytes of counters, change kDiskSignature so we | 156 // If we have more than 512 bytes of counters, change kDiskSignature so we |
| 141 // don't overwrite something else (LoadStats must fail). | 157 // don't overwrite something else (LoadStats must fail). |
| 142 COMPILE_ASSERT(sizeof(OnDiskStats) <= 256 * 2, use_more_blocks); | 158 COMPILE_ASSERT(sizeof(OnDiskStats) <= 256 * 2, use_more_blocks); |
| 143 return 256 * 2; | 159 return 256 * 2; |
| 144 } | 160 } |
| 145 | 161 |
| 146 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { | 162 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 if (i > static_cast<size_t>(kDataSizesLength)) { | 257 if (i > static_cast<size_t>(kDataSizesLength)) { |
| 242 NOTREACHED(); | 258 NOTREACHED(); |
| 243 i = kDataSizesLength; | 259 i = kDataSizesLength; |
| 244 } | 260 } |
| 245 | 261 |
| 246 i -= 17; | 262 i -= 17; |
| 247 n <<= i; | 263 n <<= i; |
| 248 return n; | 264 return n; |
| 249 } | 265 } |
| 250 | 266 |
| 251 void Stats::Snapshot(base::HistogramSamples* samples) const { | |
| 252 for (int i = 0; i < kDataSizesLength; i++) { | |
| 253 int count = data_sizes_[i]; | |
| 254 if (count < 0) | |
| 255 count = 0; | |
| 256 samples->Accumulate(GetBucketRange(i), count); | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 // The array will be filled this way: | 267 // The array will be filled this way: |
| 261 // index size | 268 // index size |
| 262 // 0 [0, 1024) | 269 // 0 [0, 1024) |
| 263 // 1 [1024, 2048) | 270 // 1 [1024, 2048) |
| 264 // 2 [2048, 4096) | 271 // 2 [2048, 4096) |
| 265 // 3 [4K, 6K) | 272 // 3 [4K, 6K) |
| 266 // ... | 273 // ... |
| 267 // 10 [18K, 20K) | 274 // 10 [18K, 20K) |
| 268 // 11 [20K, 24K) | 275 // 11 [20K, 24K) |
| 269 // 12 [24k, 28K) | 276 // 12 [24k, 28K) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 int Stats::GetRatio(Counters hit, Counters miss) const { | 310 int Stats::GetRatio(Counters hit, Counters miss) const { |
| 304 int64 ratio = GetCounter(hit) * 100; | 311 int64 ratio = GetCounter(hit) * 100; |
| 305 if (!ratio) | 312 if (!ratio) |
| 306 return 0; | 313 return 0; |
| 307 | 314 |
| 308 ratio /= (GetCounter(hit) + GetCounter(miss)); | 315 ratio /= (GetCounter(hit) + GetCounter(miss)); |
| 309 return static_cast<int>(ratio); | 316 return static_cast<int>(ratio); |
| 310 } | 317 } |
| 311 | 318 |
| 312 } // namespace disk_cache | 319 } // namespace disk_cache |
| OLD | NEW |