Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: net/disk_cache/block_files.cc

Issue 17816008: Disk cache: Introduce BlockBitmaps for V3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/block_files.h ('k') | net/disk_cache/disk_format_base.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/block_files.h" 5 #include "net/disk_cache/block_files.h"
6 6
7 #include "base/atomicops.h" 7 #include "base/atomicops.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 BlockHeader::BlockHeader(MappedFile* file) 45 BlockHeader::BlockHeader(MappedFile* file)
46 : header_(reinterpret_cast<BlockFileHeader*>(file->buffer())) { 46 : header_(reinterpret_cast<BlockFileHeader*>(file->buffer())) {
47 } 47 }
48 48
49 BlockHeader::BlockHeader(const BlockHeader& other) : header_(other.header_) { 49 BlockHeader::BlockHeader(const BlockHeader& other) : header_(other.header_) {
50 } 50 }
51 51
52 BlockHeader::~BlockHeader() { 52 BlockHeader::~BlockHeader() {
53 } 53 }
54 54
55 bool BlockHeader::CreateMapBlock(int target, int size, int* index) { 55 bool BlockHeader::CreateMapBlock(int size, int* index) {
56 if (target <= 0 || target > kMaxNumBlocks || 56 DCHECK(size > 0 && size <= kMaxNumBlocks);
57 size <= 0 || size > kMaxNumBlocks) { 57 int target = 0;
58 for (int i = size; i <= kMaxNumBlocks; i++) {
59 if (header_->empty[i - 1]) {
60 target = i;
61 break;
62 }
63 }
64
65 if (!target) {
58 NOTREACHED(); 66 NOTREACHED();
59 return false; 67 return false;
60 } 68 }
61 69
62 TimeTicks start = TimeTicks::Now(); 70 TimeTicks start = TimeTicks::Now();
63 // We are going to process the map on 32-block chunks (32 bits), and on every 71 // We are going to process the map on 32-block chunks (32 bits), and on every
64 // chunk, iterate through the 8 nibbles where the new block can be located. 72 // chunk, iterate through the 8 nibbles where the new block can be located.
65 int current = header_->hints[target - 1]; 73 int current = header_->hints[target - 1];
66 for (int i = 0; i < header_->max_entries / 32; i++, current++) { 74 for (int i = 0; i < header_->max_entries / 32; i++, current++) {
67 if (current == header_->max_entries / 32) 75 if (current == header_->max_entries / 32)
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 DCHECK_GE(header_->empty[bits_at_end - 1], 0); 145 DCHECK_GE(header_->empty[bits_at_end - 1], 0);
138 } 146 }
139 base::subtle::MemoryBarrier(); 147 base::subtle::MemoryBarrier();
140 header_->num_entries--; 148 header_->num_entries--;
141 DCHECK_GE(header_->num_entries, 0); 149 DCHECK_GE(header_->num_entries, 0);
142 HISTOGRAM_TIMES("DiskCache.DeleteBlock", TimeTicks::Now() - start); 150 HISTOGRAM_TIMES("DiskCache.DeleteBlock", TimeTicks::Now() - start);
143 } 151 }
144 152
145 // Note that this is a simplified version of DeleteMapBlock(). 153 // Note that this is a simplified version of DeleteMapBlock().
146 bool BlockHeader::UsedMapBlock(int index, int size) { 154 bool BlockHeader::UsedMapBlock(int index, int size) {
147 if (size < 0 || size > kMaxNumBlocks) { 155 if (size < 0 || size > kMaxNumBlocks)
148 NOTREACHED();
149 return false; 156 return false;
150 } 157
151 int byte_index = index / 8; 158 int byte_index = index / 8;
152 uint8* byte_map = reinterpret_cast<uint8*>(header_->allocation_map); 159 uint8* byte_map = reinterpret_cast<uint8*>(header_->allocation_map);
153 uint8 map_block = byte_map[byte_index]; 160 uint8 map_block = byte_map[byte_index];
154 161
155 if (index % 8 >= 4) 162 if (index % 8 >= 4)
156 map_block >>= 4; 163 map_block >>= 4;
157 164
158 DCHECK((((1 << size) - 1) << (index % 8)) < 0x100); 165 DCHECK((((1 << size) - 1) << (index % 8)) < 0x100);
159 uint8 to_clear = ((1 << size) - 1) << (index % 8); 166 uint8 to_clear = ((1 << size) - 1) << (index % 8);
160 return ((byte_map[byte_index] & to_clear) == to_clear); 167 return ((byte_map[byte_index] & to_clear) == to_clear);
161 } 168 }
162 169
163 void BlockHeader::FixAllocationCounters() { 170 void BlockHeader::FixAllocationCounters() {
164 for (int i = 0; i < kMaxNumBlocks; i++) { 171 for (int i = 0; i < kMaxNumBlocks; i++) {
165 header_->hints[i] = 0; 172 header_->hints[i] = 0;
166 header_->empty[i] = 0; 173 header_->empty[i] = 0;
167 } 174 }
168 175
169 for (int i = 0; i < header_->max_entries / 32; i++) { 176 for (int i = 0; i < header_->max_entries / 32; i++) {
170 uint32 map_block = header_->allocation_map[i]; 177 uint32 map_block = header_->allocation_map[i];
171 178
172 for (int j = 0; j < 8; j++, map_block >>= 4) { 179 for (int j = 0; j < 8; j++, map_block >>= 4) {
173 int type = GetMapBlockType(map_block); 180 int type = GetMapBlockType(map_block);
174 if (type) 181 if (type)
175 header_->empty[type -1]++; 182 header_->empty[type -1]++;
176 } 183 }
177 } 184 }
178 } 185 }
179 186
180 bool BlockHeader::NeedToGrowBlockFile(int block_count) { 187 bool BlockHeader::NeedToGrowBlockFile(int block_count) const {
181 bool have_space = false; 188 bool have_space = false;
182 int empty_blocks = 0; 189 int empty_blocks = 0;
183 for (int i = 0; i < kMaxNumBlocks; i++) { 190 for (int i = 0; i < kMaxNumBlocks; i++) {
184 empty_blocks += header_->empty[i] * (i + 1); 191 empty_blocks += header_->empty[i] * (i + 1);
185 if (i >= block_count - 1 && header_->empty[i]) 192 if (i >= block_count - 1 && header_->empty[i])
186 have_space = true; 193 have_space = true;
187 } 194 }
188 195
189 if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) { 196 if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) {
190 // This file is almost full but we already created another one, don't use 197 // This file is almost full but we already created another one, don't use
191 // this file yet so that it is easier to find empty blocks when we start 198 // this file yet so that it is easier to find empty blocks when we start
192 // using this file again. 199 // using this file again.
193 return true; 200 return true;
194 } 201 }
195 return !have_space; 202 return !have_space;
196 } 203 }
197 204
205 bool BlockHeader::CanAllocate(int block_count) const {
206 DCHECK_GT(block_count, 0);
207 for (int i = block_count - 1; i < kMaxNumBlocks; i++) {
208 if (header_->empty[i])
209 return true;
210 }
211
212 return false;
213 }
214
198 int BlockHeader::EmptyBlocks() const { 215 int BlockHeader::EmptyBlocks() const {
199 int empty_blocks = 0; 216 int empty_blocks = 0;
200 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { 217 for (int i = 0; i < kMaxNumBlocks; i++) {
201 empty_blocks += header_->empty[i] * (i + 1); 218 empty_blocks += header_->empty[i] * (i + 1);
202 if (header_->empty[i] < 0) 219 if (header_->empty[i] < 0)
203 return 0; 220 return 0;
204 } 221 }
205 return empty_blocks; 222 return empty_blocks;
206 } 223 }
207 224
225 int BlockHeader::MinimumAllocations() const {
226 return header_->empty[kMaxNumBlocks - 1];
227 }
228
229 int BlockHeader::Capacity() const {
230 return header_->max_entries;
231 }
232
208 bool BlockHeader::ValidateCounters() const { 233 bool BlockHeader::ValidateCounters() const {
209 if (header_->max_entries < 0 || header_->max_entries > kMaxBlocks || 234 if (header_->max_entries < 0 || header_->max_entries > kMaxBlocks ||
210 header_->num_entries < 0) 235 header_->num_entries < 0)
211 return false; 236 return false;
212 237
213 int empty_blocks = EmptyBlocks(); 238 int empty_blocks = EmptyBlocks();
214 if (empty_blocks + header_->num_entries > header_->max_entries) 239 if (empty_blocks + header_->num_entries > header_->max_entries)
215 return false; 240 return false;
216 241
217 return true; 242 return true;
218 } 243 }
219 244
245 int BlockHeader::FileId() const {
246 return header_->this_file;
247 }
248
249 int BlockHeader::NextFileId() const {
250 return header_->next_file;
251 }
252
220 int BlockHeader::Size() const { 253 int BlockHeader::Size() const {
221 return static_cast<int>(sizeof(*header_)); 254 return static_cast<int>(sizeof(*header_));
222 } 255 }
223 256
257 BlockFileHeader* BlockHeader::Header() {
258 return header_;
259 }
260
224 // ------------------------------------------------------------------------ 261 // ------------------------------------------------------------------------
225 262
226 BlockFiles::BlockFiles(const base::FilePath& path) 263 BlockFiles::BlockFiles(const base::FilePath& path)
227 : init_(false), zero_buffer_(NULL), path_(path) { 264 : init_(false), zero_buffer_(NULL), path_(path) {
228 } 265 }
229 266
230 BlockFiles::~BlockFiles() { 267 BlockFiles::~BlockFiles() {
231 if (zero_buffer_) 268 if (zero_buffer_)
232 delete[] zero_buffer_; 269 delete[] zero_buffer_;
233 CloseFiles(); 270 CloseFiles();
(...skipping 19 matching lines...) Expand all
253 if (!RemoveEmptyFile(static_cast<FileType>(i + 1))) 290 if (!RemoveEmptyFile(static_cast<FileType>(i + 1)))
254 return false; 291 return false;
255 } 292 }
256 293
257 init_ = true; 294 init_ = true;
258 return true; 295 return true;
259 } 296 }
260 297
261 MappedFile* BlockFiles::GetFile(Addr address) { 298 MappedFile* BlockFiles::GetFile(Addr address) {
262 DCHECK(thread_checker_->CalledOnValidThread()); 299 DCHECK(thread_checker_->CalledOnValidThread());
263 DCHECK(block_files_.size() >= 4); 300 DCHECK_GE(block_files_.size(),
301 static_cast<size_t>(kFirstAdditionalBlockFile));
264 DCHECK(address.is_block_file() || !address.is_initialized()); 302 DCHECK(address.is_block_file() || !address.is_initialized());
265 if (!address.is_initialized()) 303 if (!address.is_initialized())
266 return NULL; 304 return NULL;
267 305
268 int file_index = address.FileNumber(); 306 int file_index = address.FileNumber();
269 if (static_cast<unsigned int>(file_index) >= block_files_.size() || 307 if (static_cast<unsigned int>(file_index) >= block_files_.size() ||
270 !block_files_[file_index]) { 308 !block_files_[file_index]) {
271 // We need to open the file 309 // We need to open the file
272 if (!OpenBlockFile(file_index)) 310 if (!OpenBlockFile(file_index))
273 return NULL; 311 return NULL;
274 } 312 }
275 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index)); 313 DCHECK_GE(block_files_.size(), static_cast<unsigned int>(file_index));
276 return block_files_[file_index]; 314 return block_files_[file_index];
277 } 315 }
278 316
279 bool BlockFiles::CreateBlock(FileType block_type, int block_count, 317 bool BlockFiles::CreateBlock(FileType block_type, int block_count,
280 Addr* block_address) { 318 Addr* block_address) {
281 DCHECK(thread_checker_->CalledOnValidThread()); 319 DCHECK(thread_checker_->CalledOnValidThread());
282 if (block_type < RANKINGS || block_type > BLOCK_4K || 320 DCHECK_NE(block_type, EXTERNAL);
283 block_count < 1 || block_count > 4) 321 DCHECK_NE(block_type, BLOCK_FILES);
322 DCHECK_NE(block_type, BLOCK_ENTRIES);
323 DCHECK_NE(block_type, BLOCK_EVICTED);
324 if (block_count < 1 || block_count > kMaxNumBlocks)
284 return false; 325 return false;
326
285 if (!init_) 327 if (!init_)
286 return false; 328 return false;
287 329
288 MappedFile* file = FileForNewBlock(block_type, block_count); 330 MappedFile* file = FileForNewBlock(block_type, block_count);
289 if (!file) 331 if (!file)
290 return false; 332 return false;
291 333
292 ScopedFlush flush(file); 334 ScopedFlush flush(file);
293 BlockHeader header(file); 335 BlockHeader file_header(file);
294 336
295 int target_size = 0;
296 for (int i = block_count; i <= 4; i++) {
297 if (header->empty[i - 1]) {
298 target_size = i;
299 break;
300 }
301 }
302
303 DCHECK(target_size);
304 int index; 337 int index;
305 if (!header.CreateMapBlock(target_size, block_count, &index)) 338 if (!file_header.CreateMapBlock(block_count, &index))
306 return false; 339 return false;
307 340
308 Addr address(block_type, block_count, header->this_file, index); 341 Addr address(block_type, block_count, file_header.FileId(), index);
309 block_address->set_value(address.value()); 342 block_address->set_value(address.value());
310 Trace("CreateBlock 0x%x", address.value()); 343 Trace("CreateBlock 0x%x", address.value());
311 return true; 344 return true;
312 } 345 }
313 346
314 void BlockFiles::DeleteBlock(Addr address, bool deep) { 347 void BlockFiles::DeleteBlock(Addr address, bool deep) {
315 DCHECK(thread_checker_->CalledOnValidThread()); 348 DCHECK(thread_checker_->CalledOnValidThread());
316 if (!address.is_initialized() || address.is_separate_file()) 349 if (!address.is_initialized() || address.is_separate_file())
317 return; 350 return;
318 351
319 if (!zero_buffer_) { 352 if (!zero_buffer_) {
320 zero_buffer_ = new char[Addr::BlockSizeForFileType(BLOCK_4K) * 4]; 353 zero_buffer_ = new char[Addr::BlockSizeForFileType(BLOCK_4K) * 4];
321 memset(zero_buffer_, 0, Addr::BlockSizeForFileType(BLOCK_4K) * 4); 354 memset(zero_buffer_, 0, Addr::BlockSizeForFileType(BLOCK_4K) * 4);
322 } 355 }
323 MappedFile* file = GetFile(address); 356 MappedFile* file = GetFile(address);
324 if (!file) 357 if (!file)
325 return; 358 return;
326 359
327 Trace("DeleteBlock 0x%x", address.value()); 360 Trace("DeleteBlock 0x%x", address.value());
328 361
329 size_t size = address.BlockSize() * address.num_blocks(); 362 size_t size = address.BlockSize() * address.num_blocks();
330 size_t offset = address.start_block() * address.BlockSize() + 363 size_t offset = address.start_block() * address.BlockSize() +
331 kBlockHeaderSize; 364 kBlockHeaderSize;
332 if (deep) 365 if (deep)
333 file->Write(zero_buffer_, size, offset); 366 file->Write(zero_buffer_, size, offset);
334 367
335 BlockHeader header(file); 368 BlockHeader file_header(file);
336 header.DeleteMapBlock(address.start_block(), address.num_blocks()); 369 file_header.DeleteMapBlock(address.start_block(), address.num_blocks());
337 file->Flush(); 370 file->Flush();
338 371
339 if (!header->num_entries) { 372 if (!file_header.Header()->num_entries) {
340 // This file is now empty. Let's try to delete it. 373 // This file is now empty. Let's try to delete it.
341 FileType type = Addr::RequiredFileType(header->entry_size); 374 FileType type = Addr::RequiredFileType(file_header.Header()->entry_size);
342 if (Addr::BlockSizeForFileType(RANKINGS) == header->entry_size) 375 if (Addr::BlockSizeForFileType(RANKINGS) ==
376 file_header.Header()->entry_size) {
343 type = RANKINGS; 377 type = RANKINGS;
378 }
344 RemoveEmptyFile(type); // Ignore failures. 379 RemoveEmptyFile(type); // Ignore failures.
345 } 380 }
346 } 381 }
347 382
348 void BlockFiles::CloseFiles() { 383 void BlockFiles::CloseFiles() {
349 if (init_) { 384 if (init_) {
350 DCHECK(thread_checker_->CalledOnValidThread()); 385 DCHECK(thread_checker_->CalledOnValidThread());
351 } 386 }
352 init_ = false; 387 init_ = false;
353 for (unsigned int i = 0; i < block_files_.size(); i++) { 388 for (unsigned int i = 0; i < block_files_.size(); i++) {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 LOG(ERROR) << "Failed to open " << name.value(); 478 LOG(ERROR) << "Failed to open " << name.value();
444 return false; 479 return false;
445 } 480 }
446 481
447 size_t file_len = file->GetLength(); 482 size_t file_len = file->GetLength();
448 if (file_len < static_cast<size_t>(kBlockHeaderSize)) { 483 if (file_len < static_cast<size_t>(kBlockHeaderSize)) {
449 LOG(ERROR) << "File too small " << name.value(); 484 LOG(ERROR) << "File too small " << name.value();
450 return false; 485 return false;
451 } 486 }
452 487
453 BlockHeader header(file.get()); 488 BlockHeader file_header(file.get());
489 BlockFileHeader* header = file_header.Header();
454 if (kBlockMagic != header->magic || kBlockVersion2 != header->version) { 490 if (kBlockMagic != header->magic || kBlockVersion2 != header->version) {
455 LOG(ERROR) << "Invalid file version or magic " << name.value(); 491 LOG(ERROR) << "Invalid file version or magic " << name.value();
456 return false; 492 return false;
457 } 493 }
458 494
459 if (header->updating || !header.ValidateCounters()) { 495 if (header->updating || !file_header.ValidateCounters()) {
460 // Last instance was not properly shutdown, or counters are out of sync. 496 // Last instance was not properly shutdown, or counters are out of sync.
461 if (!FixBlockFileHeader(file.get())) { 497 if (!FixBlockFileHeader(file.get())) {
462 LOG(ERROR) << "Unable to fix block file " << name.value(); 498 LOG(ERROR) << "Unable to fix block file " << name.value();
463 return false; 499 return false;
464 } 500 }
465 } 501 }
466 502
467 if (static_cast<int>(file_len) < 503 if (static_cast<int>(file_len) <
468 header->max_entries * header->entry_size + kBlockHeaderSize) { 504 header->max_entries * header->entry_size + kBlockHeaderSize) {
469 LOG(ERROR) << "File too small " << name.value(); 505 LOG(ERROR) << "File too small " << name.value();
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 FileLock lock(header); 545 FileLock lock(header);
510 header->empty[3] = (new_size - header->max_entries) / 4; // 4 blocks entries 546 header->empty[3] = (new_size - header->max_entries) / 4; // 4 blocks entries
511 header->max_entries = new_size; 547 header->max_entries = new_size;
512 548
513 return true; 549 return true;
514 } 550 }
515 551
516 MappedFile* BlockFiles::FileForNewBlock(FileType block_type, int block_count) { 552 MappedFile* BlockFiles::FileForNewBlock(FileType block_type, int block_count) {
517 COMPILE_ASSERT(RANKINGS == 1, invalid_file_type); 553 COMPILE_ASSERT(RANKINGS == 1, invalid_file_type);
518 MappedFile* file = block_files_[block_type - 1]; 554 MappedFile* file = block_files_[block_type - 1];
519 BlockHeader header(file); 555 BlockHeader file_header(file);
520 556
521 TimeTicks start = TimeTicks::Now(); 557 TimeTicks start = TimeTicks::Now();
522 while (header.NeedToGrowBlockFile(block_count)) { 558 while (file_header.NeedToGrowBlockFile(block_count)) {
523 if (kMaxBlocks == header->max_entries) { 559 if (kMaxBlocks == file_header.Header()->max_entries) {
524 file = NextFile(file); 560 file = NextFile(file);
525 if (!file) 561 if (!file)
526 return NULL; 562 return NULL;
527 header = BlockHeader(file); 563 file_header = BlockHeader(file);
528 continue; 564 continue;
529 } 565 }
530 566
531 if (!GrowBlockFile(file, header.Get())) 567 if (!GrowBlockFile(file, file_header.Header()))
532 return NULL; 568 return NULL;
533 break; 569 break;
534 } 570 }
535 HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock", TimeTicks::Now() - start); 571 HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock", TimeTicks::Now() - start);
536 return file; 572 return file;
537 } 573 }
538 574
539 MappedFile* BlockFiles::NextFile(MappedFile* file) { 575 MappedFile* BlockFiles::NextFile(MappedFile* file) {
540 ScopedFlush flush(file); 576 ScopedFlush flush(file);
541 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); 577 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer());
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 header = next_header; 645 header = next_header;
610 file = next_file; 646 file = next_file;
611 } 647 }
612 return true; 648 return true;
613 } 649 }
614 650
615 // Note that we expect to be called outside of a FileLock... however, we cannot 651 // Note that we expect to be called outside of a FileLock... however, we cannot
616 // DCHECK on header->updating because we may be fixing a crash. 652 // DCHECK on header->updating because we may be fixing a crash.
617 bool BlockFiles::FixBlockFileHeader(MappedFile* file) { 653 bool BlockFiles::FixBlockFileHeader(MappedFile* file) {
618 ScopedFlush flush(file); 654 ScopedFlush flush(file);
619 BlockHeader header(file); 655 BlockHeader file_header(file);
620 int file_size = static_cast<int>(file->GetLength()); 656 int file_size = static_cast<int>(file->GetLength());
621 if (file_size < header.Size()) 657 if (file_size < file_header.Size())
622 return false; // file_size > 2GB is also an error. 658 return false; // file_size > 2GB is also an error.
623 659
624 const int kMinBlockSize = 36; 660 const int kMinBlockSize = 36;
625 const int kMaxBlockSize = 4096; 661 const int kMaxBlockSize = 4096;
662 BlockFileHeader* header = file_header.Header();
626 if (header->entry_size < kMinBlockSize || 663 if (header->entry_size < kMinBlockSize ||
627 header->entry_size > kMaxBlockSize || header->num_entries < 0) 664 header->entry_size > kMaxBlockSize || header->num_entries < 0)
628 return false; 665 return false;
629 666
630 // Make sure that we survive crashes. 667 // Make sure that we survive crashes.
631 header->updating = 1; 668 header->updating = 1;
632 int expected = header->entry_size * header->max_entries + header.Size(); 669 int expected = header->entry_size * header->max_entries + file_header.Size();
633 if (file_size != expected) { 670 if (file_size != expected) {
634 int max_expected = header->entry_size * kMaxBlocks + header.Size(); 671 int max_expected = header->entry_size * kMaxBlocks + file_header.Size();
635 if (file_size < expected || header->empty[3] || file_size > max_expected) { 672 if (file_size < expected || header->empty[3] || file_size > max_expected) {
636 NOTREACHED(); 673 NOTREACHED();
637 LOG(ERROR) << "Unexpected file size"; 674 LOG(ERROR) << "Unexpected file size";
638 return false; 675 return false;
639 } 676 }
640 // We were in the middle of growing the file. 677 // We were in the middle of growing the file.
641 int num_entries = (file_size - header.Size()) / header->entry_size; 678 int num_entries = (file_size - file_header.Size()) / header->entry_size;
642 header->max_entries = num_entries; 679 header->max_entries = num_entries;
643 } 680 }
644 681
645 header.FixAllocationCounters(); 682 file_header.FixAllocationCounters();
646 int empty_blocks = header.EmptyBlocks(); 683 int empty_blocks = file_header.EmptyBlocks();
647 if (empty_blocks + header->num_entries > header->max_entries) 684 if (empty_blocks + header->num_entries > header->max_entries)
648 header->num_entries = header->max_entries - empty_blocks; 685 header->num_entries = header->max_entries - empty_blocks;
649 686
650 if (!header.ValidateCounters()) 687 if (!file_header.ValidateCounters())
651 return false; 688 return false;
652 689
653 header->updating = 0; 690 header->updating = 0;
654 return true; 691 return true;
655 } 692 }
656 693
657 // We are interested in the total number of blocks used by this file type, and 694 // We are interested in the total number of blocks used by this file type, and
658 // the max number of blocks that we can store (reported as the percentage of 695 // the max number of blocks that we can store (reported as the percentage of
659 // used blocks). In order to find out the number of used blocks, we have to 696 // used blocks). In order to find out the number of used blocks, we have to
660 // substract the empty blocks from the total blocks for each file in the chain. 697 // substract the empty blocks from the total blocks for each file in the chain.
661 void BlockFiles::GetFileStats(int index, int* used_count, int* load) { 698 void BlockFiles::GetFileStats(int index, int* used_count, int* load) {
662 int max_blocks = 0; 699 int max_blocks = 0;
663 *used_count = 0; 700 *used_count = 0;
664 *load = 0; 701 *load = 0;
665 for (;;) { 702 for (;;) {
666 if (!block_files_[index] && !OpenBlockFile(index)) 703 if (!block_files_[index] && !OpenBlockFile(index))
667 return; 704 return;
668 705
669 BlockFileHeader* header = 706 BlockFileHeader* header =
670 reinterpret_cast<BlockFileHeader*>(block_files_[index]->buffer()); 707 reinterpret_cast<BlockFileHeader*>(block_files_[index]->buffer());
671 708
672 max_blocks += header->max_entries; 709 max_blocks += header->max_entries;
673 int used = header->max_entries; 710 int used = header->max_entries;
674 for (int i = 0; i < 4; i++) { 711 for (int i = 0; i < kMaxNumBlocks; i++) {
675 used -= header->empty[i] * (i + 1); 712 used -= header->empty[i] * (i + 1);
676 DCHECK_GE(used, 0); 713 DCHECK_GE(used, 0);
677 } 714 }
678 *used_count += used; 715 *used_count += used;
679 716
680 if (!header->next_file) 717 if (!header->next_file)
681 break; 718 break;
682 index = header->next_file; 719 index = header->next_file;
683 } 720 }
684 if (max_blocks) 721 if (max_blocks)
685 *load = *used_count * 100 / max_blocks; 722 *load = *used_count * 100 / max_blocks;
686 } 723 }
687 724
688 base::FilePath BlockFiles::Name(int index) { 725 base::FilePath BlockFiles::Name(int index) {
689 // The file format allows for 256 files. 726 // The file format allows for 256 files.
690 DCHECK(index < 256 && index >= 0); 727 DCHECK(index < 256 && index >= 0);
691 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); 728 std::string tmp = base::StringPrintf("%s%d", kBlockName, index);
692 return path_.AppendASCII(tmp); 729 return path_.AppendASCII(tmp);
693 } 730 }
694 731
695 } // namespace disk_cache 732 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/block_files.h ('k') | net/disk_cache/disk_format_base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698