| 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" | 9 #include "base/metrics/bucket_ranges.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/metrics/histogram_samples.h" | 11 #include "base/metrics/histogram_samples.h" |
| 12 #include "base/metrics/sample_vector.h" | 12 #include "base/metrics/sample_vector.h" |
| 13 #include "base/metrics/statistics_recorder.h" | 13 #include "base/metrics/statistics_recorder.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const int32 kDiskSignature = 0xF01427E0; | 19 const int32_t kDiskSignature = 0xF01427E0; |
| 20 | 20 |
| 21 struct OnDiskStats { | 21 struct OnDiskStats { |
| 22 int32 signature; | 22 int32_t signature; |
| 23 int size; | 23 int size; |
| 24 int data_sizes[disk_cache::Stats::kDataSizesLength]; | 24 int data_sizes[disk_cache::Stats::kDataSizesLength]; |
| 25 int64 counters[disk_cache::Stats::MAX_COUNTER]; | 25 int64_t counters[disk_cache::Stats::MAX_COUNTER]; |
| 26 }; | 26 }; |
| 27 static_assert(sizeof(OnDiskStats) < 512, "needs more than 2 blocks"); | 27 static_assert(sizeof(OnDiskStats) < 512, "needs more than 2 blocks"); |
| 28 | 28 |
| 29 // Returns the "floor" (as opposed to "ceiling") of log base 2 of number. | 29 // Returns the "floor" (as opposed to "ceiling") of log base 2 of number. |
| 30 int LogBase2(int32 number) { | 30 int LogBase2(int32_t number) { |
| 31 unsigned int value = static_cast<unsigned int>(number); | 31 unsigned int value = static_cast<unsigned int>(number); |
| 32 const unsigned int mask[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; | 32 const unsigned int mask[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; |
| 33 const unsigned int s[] = {1, 2, 4, 8, 16}; | 33 const unsigned int s[] = {1, 2, 4, 8, 16}; |
| 34 | 34 |
| 35 unsigned int result = 0; | 35 unsigned int result = 0; |
| 36 for (int i = 4; i >= 0; i--) { | 36 for (int i = 4; i >= 0; i--) { |
| 37 if (value & mask[i]) { | 37 if (value & mask[i]) { |
| 38 value >>= s[i]; | 38 value >>= s[i]; |
| 39 result |= s[i]; | 39 result |= s[i]; |
| 40 } | 40 } |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 stats_histogram->AddSamples(samples); | 162 stats_histogram->AddSamples(samples); |
| 163 } | 163 } |
| 164 | 164 |
| 165 int Stats::StorageSize() { | 165 int Stats::StorageSize() { |
| 166 // If we have more than 512 bytes of counters, change kDiskSignature so we | 166 // If we have more than 512 bytes of counters, change kDiskSignature so we |
| 167 // don't overwrite something else (LoadStats must fail). | 167 // don't overwrite something else (LoadStats must fail). |
| 168 static_assert(sizeof(OnDiskStats) <= 256 * 2, "use more blocks"); | 168 static_assert(sizeof(OnDiskStats) <= 256 * 2, "use more blocks"); |
| 169 return 256 * 2; | 169 return 256 * 2; |
| 170 } | 170 } |
| 171 | 171 |
| 172 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { | 172 void Stats::ModifyStorageStats(int32_t old_size, int32_t new_size) { |
| 173 // We keep a counter of the data block size on an array where each entry is | 173 // We keep a counter of the data block size on an array where each entry is |
| 174 // the adjusted log base 2 of the size. The first entry counts blocks of 256 | 174 // the adjusted log base 2 of the size. The first entry counts blocks of 256 |
| 175 // bytes, the second blocks up to 512 bytes, etc. With 20 entries, the last | 175 // bytes, the second blocks up to 512 bytes, etc. With 20 entries, the last |
| 176 // one stores entries of more than 64 MB | 176 // one stores entries of more than 64 MB |
| 177 int new_index = GetStatsBucket(new_size); | 177 int new_index = GetStatsBucket(new_size); |
| 178 int old_index = GetStatsBucket(old_size); | 178 int old_index = GetStatsBucket(old_size); |
| 179 | 179 |
| 180 if (new_size) | 180 if (new_size) |
| 181 data_sizes_[new_index]++; | 181 data_sizes_[new_index]++; |
| 182 | 182 |
| 183 if (old_size) | 183 if (old_size) |
| 184 data_sizes_[old_index]--; | 184 data_sizes_[old_index]--; |
| 185 } | 185 } |
| 186 | 186 |
| 187 void Stats::OnEvent(Counters an_event) { | 187 void Stats::OnEvent(Counters an_event) { |
| 188 DCHECK(an_event >= MIN_COUNTER && an_event < MAX_COUNTER); | 188 DCHECK(an_event >= MIN_COUNTER && an_event < MAX_COUNTER); |
| 189 counters_[an_event]++; | 189 counters_[an_event]++; |
| 190 } | 190 } |
| 191 | 191 |
| 192 void Stats::SetCounter(Counters counter, int64 value) { | 192 void Stats::SetCounter(Counters counter, int64_t value) { |
| 193 DCHECK(counter >= MIN_COUNTER && counter < MAX_COUNTER); | 193 DCHECK(counter >= MIN_COUNTER && counter < MAX_COUNTER); |
| 194 counters_[counter] = value; | 194 counters_[counter] = value; |
| 195 } | 195 } |
| 196 | 196 |
| 197 int64 Stats::GetCounter(Counters counter) const { | 197 int64_t Stats::GetCounter(Counters counter) const { |
| 198 DCHECK(counter >= MIN_COUNTER && counter < MAX_COUNTER); | 198 DCHECK(counter >= MIN_COUNTER && counter < MAX_COUNTER); |
| 199 return counters_[counter]; | 199 return counters_[counter]; |
| 200 } | 200 } |
| 201 | 201 |
| 202 void Stats::GetItems(StatsItems* items) { | 202 void Stats::GetItems(StatsItems* items) { |
| 203 std::pair<std::string, std::string> item; | 203 std::pair<std::string, std::string> item; |
| 204 for (int i = 0; i < kDataSizesLength; i++) { | 204 for (int i = 0; i < kDataSizesLength; i++) { |
| 205 item.first = base::StringPrintf("Size%02d", i); | 205 item.first = base::StringPrintf("Size%02d", i); |
| 206 item.second = base::StringPrintf("0x%08x", data_sizes_[i]); | 206 item.second = base::StringPrintf("0x%08x", data_sizes_[i]); |
| 207 items->push_back(item); | 207 items->push_back(item); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 // 15 [36k, 40K) | 288 // 15 [36k, 40K) |
| 289 // 16 [40k, 64K) | 289 // 16 [40k, 64K) |
| 290 // 17 [64K, 128K) | 290 // 17 [64K, 128K) |
| 291 // 18 [128K, 256K) | 291 // 18 [128K, 256K) |
| 292 // ... | 292 // ... |
| 293 // 23 [4M, 8M) | 293 // 23 [4M, 8M) |
| 294 // 24 [8M, 16M) | 294 // 24 [8M, 16M) |
| 295 // 25 [16M, 32M) | 295 // 25 [16M, 32M) |
| 296 // 26 [32M, 64M) | 296 // 26 [32M, 64M) |
| 297 // 27 [64M, ...) | 297 // 27 [64M, ...) |
| 298 int Stats::GetStatsBucket(int32 size) { | 298 int Stats::GetStatsBucket(int32_t size) { |
| 299 if (size < 1024) | 299 if (size < 1024) |
| 300 return 0; | 300 return 0; |
| 301 | 301 |
| 302 // 10 slots more, until 20K. | 302 // 10 slots more, until 20K. |
| 303 if (size < 20 * 1024) | 303 if (size < 20 * 1024) |
| 304 return size / 2048 + 1; | 304 return size / 2048 + 1; |
| 305 | 305 |
| 306 // 5 slots more, from 20K to 40K. | 306 // 5 slots more, from 20K to 40K. |
| 307 if (size < 40 * 1024) | 307 if (size < 40 * 1024) |
| 308 return (size - 20 * 1024) / 4096 + 11; | 308 return (size - 20 * 1024) / 4096 + 11; |
| 309 | 309 |
| 310 // From this point on, use a logarithmic scale. | 310 // From this point on, use a logarithmic scale. |
| 311 int result = LogBase2(size) + 1; | 311 int result = LogBase2(size) + 1; |
| 312 | 312 |
| 313 static_assert(kDataSizesLength > 16, "update the scale"); | 313 static_assert(kDataSizesLength > 16, "update the scale"); |
| 314 if (result >= kDataSizesLength) | 314 if (result >= kDataSizesLength) |
| 315 result = kDataSizesLength - 1; | 315 result = kDataSizesLength - 1; |
| 316 | 316 |
| 317 return result; | 317 return result; |
| 318 } | 318 } |
| 319 | 319 |
| 320 int Stats::GetRatio(Counters hit, Counters miss) const { | 320 int Stats::GetRatio(Counters hit, Counters miss) const { |
| 321 int64 ratio = GetCounter(hit) * 100; | 321 int64_t ratio = GetCounter(hit) * 100; |
| 322 if (!ratio) | 322 if (!ratio) |
| 323 return 0; | 323 return 0; |
| 324 | 324 |
| 325 ratio /= (GetCounter(hit) + GetCounter(miss)); | 325 ratio /= (GetCounter(hit) + GetCounter(miss)); |
| 326 return static_cast<int>(ratio); | 326 return static_cast<int>(ratio); |
| 327 } | 327 } |
| 328 | 328 |
| 329 } // namespace disk_cache | 329 } // namespace disk_cache |
| OLD | NEW |