OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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.h" | 5 #include "net/disk_cache/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/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // If we have more than 512 bytes of counters, change kDiskSignature so we | 109 // If we have more than 512 bytes of counters, change kDiskSignature so we |
110 // don't overwrite something else (LoadStats must fail). | 110 // don't overwrite something else (LoadStats must fail). |
111 COMPILE_ASSERT(sizeof(*stats) <= 256 * 2, use_more_blocks); | 111 COMPILE_ASSERT(sizeof(*stats) <= 256 * 2, use_more_blocks); |
112 memset(stats, 0, sizeof(*stats)); | 112 memset(stats, 0, sizeof(*stats)); |
113 stats->signature = kDiskSignature; | 113 stats->signature = kDiskSignature; |
114 stats->size = sizeof(*stats); | 114 stats->size = sizeof(*stats); |
115 | 115 |
116 return StoreStats(backend, *address, stats); | 116 return StoreStats(backend, *address, stats); |
117 } | 117 } |
118 | 118 |
| 119 Stats::Stats() : backend_(NULL) { |
| 120 } |
| 121 |
| 122 Stats::~Stats() { |
| 123 } |
| 124 |
119 bool Stats::Init(BackendImpl* backend, uint32* storage_addr) { | 125 bool Stats::Init(BackendImpl* backend, uint32* storage_addr) { |
120 OnDiskStats stats; | 126 OnDiskStats stats; |
121 Addr address(*storage_addr); | 127 Addr address(*storage_addr); |
122 if (address.is_initialized()) { | 128 if (address.is_initialized()) { |
123 if (!LoadStats(backend, address, &stats)) | 129 if (!LoadStats(backend, address, &stats)) |
124 return false; | 130 return false; |
125 } else { | 131 } else { |
126 if (!CreateStats(backend, &address, &stats)) | 132 if (!CreateStats(backend, &address, &stats)) |
127 return false; | 133 return false; |
128 *storage_addr = address.value(); | 134 *storage_addr = address.value(); |
(...skipping 17 matching lines...) Expand all Loading... |
146 // histogram at any given time. | 152 // histogram at any given time. |
147 size_histogram_ = | 153 size_histogram_ = |
148 StatsHistogram::StatsHistogramFactoryGet("DiskCache.SizeStats"); | 154 StatsHistogram::StatsHistogramFactoryGet("DiskCache.SizeStats"); |
149 size_histogram_->Init(this); | 155 size_histogram_->Init(this); |
150 } | 156 } |
151 } | 157 } |
152 | 158 |
153 return true; | 159 return true; |
154 } | 160 } |
155 | 161 |
156 Stats::Stats() : backend_(NULL) { | |
157 } | |
158 | |
159 Stats::~Stats() { | |
160 } | |
161 | |
162 // The array will be filled this way: | |
163 // index size | |
164 // 0 [0, 1024) | |
165 // 1 [1024, 2048) | |
166 // 2 [2048, 4096) | |
167 // 3 [4K, 6K) | |
168 // ... | |
169 // 10 [18K, 20K) | |
170 // 11 [20K, 24K) | |
171 // 12 [24k, 28K) | |
172 // ... | |
173 // 15 [36k, 40K) | |
174 // 16 [40k, 64K) | |
175 // 17 [64K, 128K) | |
176 // 18 [128K, 256K) | |
177 // ... | |
178 // 23 [4M, 8M) | |
179 // 24 [8M, 16M) | |
180 // 25 [16M, 32M) | |
181 // 26 [32M, 64M) | |
182 // 27 [64M, ...) | |
183 int Stats::GetStatsBucket(int32 size) { | |
184 if (size < 1024) | |
185 return 0; | |
186 | |
187 // 10 slots more, until 20K. | |
188 if (size < 20 * 1024) | |
189 return size / 2048 + 1; | |
190 | |
191 // 5 slots more, from 20K to 40K. | |
192 if (size < 40 * 1024) | |
193 return (size - 20 * 1024) / 4096 + 11; | |
194 | |
195 // From this point on, use a logarithmic scale. | |
196 int result = LogBase2(size) + 1; | |
197 | |
198 COMPILE_ASSERT(kDataSizesLength > 16, update_the_scale); | |
199 if (result >= kDataSizesLength) | |
200 result = kDataSizesLength - 1; | |
201 | |
202 return result; | |
203 } | |
204 | |
205 int Stats::GetBucketRange(size_t i) const { | |
206 if (i < 2) | |
207 return static_cast<int>(1024 * i); | |
208 | |
209 if (i < 12) | |
210 return static_cast<int>(2048 * (i - 1)); | |
211 | |
212 if (i < 17) | |
213 return static_cast<int>(4096 * (i - 11)) + 20 * 1024; | |
214 | |
215 int n = 64 * 1024; | |
216 if (i > static_cast<size_t>(kDataSizesLength)) { | |
217 NOTREACHED(); | |
218 i = kDataSizesLength; | |
219 } | |
220 | |
221 i -= 17; | |
222 n <<= i; | |
223 return n; | |
224 } | |
225 | |
226 void Stats::Snapshot(StatsHistogram::StatsSamples* samples) const { | |
227 samples->GetCounts()->resize(kDataSizesLength); | |
228 for (int i = 0; i < kDataSizesLength; i++) { | |
229 int count = data_sizes_[i]; | |
230 if (count < 0) | |
231 count = 0; | |
232 samples->GetCounts()->at(i) = count; | |
233 } | |
234 } | |
235 | |
236 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { | 162 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { |
237 // 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 |
238 // 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 |
239 // 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 |
240 // one stores entries of more than 64 MB | 166 // one stores entries of more than 64 MB |
241 int new_index = GetStatsBucket(new_size); | 167 int new_index = GetStatsBucket(new_size); |
242 int old_index = GetStatsBucket(old_size); | 168 int old_index = GetStatsBucket(old_size); |
243 | 169 |
244 if (new_size) | 170 if (new_size) |
245 data_sizes_[new_index]++; | 171 data_sizes_[new_index]++; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 } | 205 } |
280 | 206 |
281 int Stats::GetHitRatio() const { | 207 int Stats::GetHitRatio() const { |
282 return GetRatio(OPEN_HIT, OPEN_MISS); | 208 return GetRatio(OPEN_HIT, OPEN_MISS); |
283 } | 209 } |
284 | 210 |
285 int Stats::GetResurrectRatio() const { | 211 int Stats::GetResurrectRatio() const { |
286 return GetRatio(RESURRECT_HIT, CREATE_HIT); | 212 return GetRatio(RESURRECT_HIT, CREATE_HIT); |
287 } | 213 } |
288 | 214 |
289 int Stats::GetRatio(Counters hit, Counters miss) const { | |
290 int64 ratio = GetCounter(hit) * 100; | |
291 if (!ratio) | |
292 return 0; | |
293 | |
294 ratio /= (GetCounter(hit) + GetCounter(miss)); | |
295 return static_cast<int>(ratio); | |
296 } | |
297 | |
298 void Stats::ResetRatios() { | 215 void Stats::ResetRatios() { |
299 SetCounter(OPEN_HIT, 0); | 216 SetCounter(OPEN_HIT, 0); |
300 SetCounter(OPEN_MISS, 0); | 217 SetCounter(OPEN_MISS, 0); |
301 SetCounter(RESURRECT_HIT, 0); | 218 SetCounter(RESURRECT_HIT, 0); |
302 SetCounter(CREATE_HIT, 0); | 219 SetCounter(CREATE_HIT, 0); |
303 } | 220 } |
304 | 221 |
305 int Stats::GetLargeEntriesSize() { | 222 int Stats::GetLargeEntriesSize() { |
306 int total = 0; | 223 int total = 0; |
307 // data_sizes_[20] stores values between 512 KB and 1 MB (see comment before | 224 // data_sizes_[20] stores values between 512 KB and 1 MB (see comment before |
(...skipping 11 matching lines...) Expand all Loading... |
319 OnDiskStats stats; | 236 OnDiskStats stats; |
320 stats.signature = kDiskSignature; | 237 stats.signature = kDiskSignature; |
321 stats.size = sizeof(stats); | 238 stats.size = sizeof(stats); |
322 memcpy(stats.data_sizes, data_sizes_, sizeof(data_sizes_)); | 239 memcpy(stats.data_sizes, data_sizes_, sizeof(data_sizes_)); |
323 memcpy(stats.counters, counters_, sizeof(counters_)); | 240 memcpy(stats.counters, counters_, sizeof(counters_)); |
324 | 241 |
325 Addr address(storage_addr_); | 242 Addr address(storage_addr_); |
326 StoreStats(backend_, address, &stats); | 243 StoreStats(backend_, address, &stats); |
327 } | 244 } |
328 | 245 |
| 246 int Stats::GetBucketRange(size_t i) const { |
| 247 if (i < 2) |
| 248 return static_cast<int>(1024 * i); |
| 249 |
| 250 if (i < 12) |
| 251 return static_cast<int>(2048 * (i - 1)); |
| 252 |
| 253 if (i < 17) |
| 254 return static_cast<int>(4096 * (i - 11)) + 20 * 1024; |
| 255 |
| 256 int n = 64 * 1024; |
| 257 if (i > static_cast<size_t>(kDataSizesLength)) { |
| 258 NOTREACHED(); |
| 259 i = kDataSizesLength; |
| 260 } |
| 261 |
| 262 i -= 17; |
| 263 n <<= i; |
| 264 return n; |
| 265 } |
| 266 |
| 267 void Stats::Snapshot(StatsHistogram::StatsSamples* samples) const { |
| 268 samples->GetCounts()->resize(kDataSizesLength); |
| 269 for (int i = 0; i < kDataSizesLength; i++) { |
| 270 int count = data_sizes_[i]; |
| 271 if (count < 0) |
| 272 count = 0; |
| 273 samples->GetCounts()->at(i) = count; |
| 274 } |
| 275 } |
| 276 |
| 277 // The array will be filled this way: |
| 278 // index size |
| 279 // 0 [0, 1024) |
| 280 // 1 [1024, 2048) |
| 281 // 2 [2048, 4096) |
| 282 // 3 [4K, 6K) |
| 283 // ... |
| 284 // 10 [18K, 20K) |
| 285 // 11 [20K, 24K) |
| 286 // 12 [24k, 28K) |
| 287 // ... |
| 288 // 15 [36k, 40K) |
| 289 // 16 [40k, 64K) |
| 290 // 17 [64K, 128K) |
| 291 // 18 [128K, 256K) |
| 292 // ... |
| 293 // 23 [4M, 8M) |
| 294 // 24 [8M, 16M) |
| 295 // 25 [16M, 32M) |
| 296 // 26 [32M, 64M) |
| 297 // 27 [64M, ...) |
| 298 int Stats::GetStatsBucket(int32 size) { |
| 299 if (size < 1024) |
| 300 return 0; |
| 301 |
| 302 // 10 slots more, until 20K. |
| 303 if (size < 20 * 1024) |
| 304 return size / 2048 + 1; |
| 305 |
| 306 // 5 slots more, from 20K to 40K. |
| 307 if (size < 40 * 1024) |
| 308 return (size - 20 * 1024) / 4096 + 11; |
| 309 |
| 310 // From this point on, use a logarithmic scale. |
| 311 int result = LogBase2(size) + 1; |
| 312 |
| 313 COMPILE_ASSERT(kDataSizesLength > 16, update_the_scale); |
| 314 if (result >= kDataSizesLength) |
| 315 result = kDataSizesLength - 1; |
| 316 |
| 317 return result; |
| 318 } |
| 319 |
| 320 int Stats::GetRatio(Counters hit, Counters miss) const { |
| 321 int64 ratio = GetCounter(hit) * 100; |
| 322 if (!ratio) |
| 323 return 0; |
| 324 |
| 325 ratio /= (GetCounter(hit) + GetCounter(miss)); |
| 326 return static_cast<int>(ratio); |
| 327 } |
| 328 |
329 } // namespace disk_cache | 329 } // namespace disk_cache |
OLD | NEW |