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 kDiskSignature = 0xF01427E0; |
20 | 20 |
21 struct OnDiskStats { | 21 struct OnDiskStats { |
22 int32 signature; | 22 int32 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 counters[disk_cache::Stats::MAX_COUNTER]; |
26 }; | 26 }; |
27 COMPILE_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 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]) { |
(...skipping 22 matching lines...) Expand all Loading... |
60 "Read data", | 60 "Read data", |
61 "Write data", | 61 "Write data", |
62 "Open rankings", | 62 "Open rankings", |
63 "Get rankings", | 63 "Get rankings", |
64 "Fatal error", | 64 "Fatal error", |
65 "Last report", | 65 "Last report", |
66 "Last report timer", | 66 "Last report timer", |
67 "Doom recent entries", | 67 "Doom recent entries", |
68 "unused" | 68 "unused" |
69 }; | 69 }; |
70 COMPILE_ASSERT(arraysize(kCounterNames) == disk_cache::Stats::MAX_COUNTER, | 70 static_assert(arraysize(kCounterNames) == disk_cache::Stats::MAX_COUNTER, |
71 update_the_names); | 71 "update the names"); |
72 | 72 |
73 } // namespace | 73 } // namespace |
74 | 74 |
75 namespace disk_cache { | 75 namespace disk_cache { |
76 | 76 |
77 bool VerifyStats(OnDiskStats* stats) { | 77 bool VerifyStats(OnDiskStats* stats) { |
78 if (stats->signature != kDiskSignature) | 78 if (stats->signature != kDiskSignature) |
79 return false; | 79 return false; |
80 | 80 |
81 // We don't want to discard the whole cache every time we have one extra | 81 // We don't want to discard the whole cache every time we have one extra |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 data_sizes_[i] = 0; | 148 data_sizes_[i] = 0; |
149 | 149 |
150 samples.Accumulate(GetBucketRange(i) / 1024, data_sizes_[i]); | 150 samples.Accumulate(GetBucketRange(i) / 1024, data_sizes_[i]); |
151 } | 151 } |
152 stats_histogram->AddSamples(samples); | 152 stats_histogram->AddSamples(samples); |
153 } | 153 } |
154 | 154 |
155 int Stats::StorageSize() { | 155 int Stats::StorageSize() { |
156 // 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 |
157 // don't overwrite something else (LoadStats must fail). | 157 // don't overwrite something else (LoadStats must fail). |
158 COMPILE_ASSERT(sizeof(OnDiskStats) <= 256 * 2, use_more_blocks); | 158 static_assert(sizeof(OnDiskStats) <= 256 * 2, "use more blocks"); |
159 return 256 * 2; | 159 return 256 * 2; |
160 } | 160 } |
161 | 161 |
162 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { | 162 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { |
163 // We keep a counter of the data block size on an array where each entry is | 163 // We keep a counter of the data block size on an array where each entry is |
164 // the adjusted log base 2 of the size. The first entry counts blocks of 256 | 164 // the adjusted log base 2 of the size. The first entry counts blocks of 256 |
165 // bytes, the second blocks up to 512 bytes, etc. With 20 entries, the last | 165 // bytes, the second blocks up to 512 bytes, etc. With 20 entries, the last |
166 // one stores entries of more than 64 MB | 166 // one stores entries of more than 64 MB |
167 int new_index = GetStatsBucket(new_size); | 167 int new_index = GetStatsBucket(new_size); |
168 int old_index = GetStatsBucket(old_size); | 168 int old_index = GetStatsBucket(old_size); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 if (size < 20 * 1024) | 293 if (size < 20 * 1024) |
294 return size / 2048 + 1; | 294 return size / 2048 + 1; |
295 | 295 |
296 // 5 slots more, from 20K to 40K. | 296 // 5 slots more, from 20K to 40K. |
297 if (size < 40 * 1024) | 297 if (size < 40 * 1024) |
298 return (size - 20 * 1024) / 4096 + 11; | 298 return (size - 20 * 1024) / 4096 + 11; |
299 | 299 |
300 // From this point on, use a logarithmic scale. | 300 // From this point on, use a logarithmic scale. |
301 int result = LogBase2(size) + 1; | 301 int result = LogBase2(size) + 1; |
302 | 302 |
303 COMPILE_ASSERT(kDataSizesLength > 16, update_the_scale); | 303 static_assert(kDataSizesLength > 16, "update the scale"); |
304 if (result >= kDataSizesLength) | 304 if (result >= kDataSizesLength) |
305 result = kDataSizesLength - 1; | 305 result = kDataSizesLength - 1; |
306 | 306 |
307 return result; | 307 return result; |
308 } | 308 } |
309 | 309 |
310 int Stats::GetRatio(Counters hit, Counters miss) const { | 310 int Stats::GetRatio(Counters hit, Counters miss) const { |
311 int64 ratio = GetCounter(hit) * 100; | 311 int64 ratio = GetCounter(hit) * 100; |
312 if (!ratio) | 312 if (!ratio) |
313 return 0; | 313 return 0; |
314 | 314 |
315 ratio /= (GetCounter(hit) + GetCounter(miss)); | 315 ratio /= (GetCounter(hit) + GetCounter(miss)); |
316 return static_cast<int>(ratio); | 316 return static_cast<int>(ratio); |
317 } | 317 } |
318 | 318 |
319 } // namespace disk_cache | 319 } // namespace disk_cache |
OLD | NEW |