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

Side by Side Diff: net/disk_cache/v3/index_table.cc

Issue 53313004: Disk cache v3: The main index table. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/disk_cache/v3/index_table.h"
6
7 #include <algorithm>
8 #include <set>
9 #include <utility>
10
11 #include "base/bits.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/disk_cache/disk_cache.h"
15
16 using base::Time;
17 using base::TimeDelta;
18 using disk_cache::CellInfo;
19 using disk_cache::CellList;
20 using disk_cache::IndexCell;
21 using disk_cache::IndexIterator;
22
23 namespace {
24
25 const int kCellHashOffset = 22;
26 const int kCellSmallTableHashOffset = 16;
27 const int kCellTimestampOffset = 40;
28 const int kCellReuseOffset = 60;
29 const int kCellGroupOffset = 3;
30 const int kCellSumOffset = 6;
31
32 const uint64 kCellAddressMask = 0x3FFFFF;
33 const uint64 kCellSmallTableAddressMask = 0xFFFF;
34 const uint64 kCellHashMask = 0x3FFFF;
35 const uint64 kCellSmallTableHashMask = 0xFFFFFF;
36 const uint64 kCellTimestampMask = 0xFFFFF;
37 const uint64 kCellReuseMask = 0xF;
38 const uint8 kCellStateMask = 0x7;
39 const uint8 kCellGroupMask = 0x7;
40 const uint8 kCellSumMask = 0x3;
41
42 const int kHashShift = 14;
43 const int kHashSmallTableShift = 8;
44
45 // Unfortunately we have to break the abstaction a little here: the file number
46 // where entries are stored is outside of the control of this code, and it is
47 // usually part of the stored address. However, for small tables we only store
48 // 16 bits of the address so the file number is never stored on a cell. We have
49 // to infere the file number from the type of entry (normal vs evicted), and
50 // the knowledge that given that the table will not keep more than 64k entries,
51 // a single file of each type is enough.
52 const int kEntriesFile = disk_cache::BLOCK_ENTRIES - 1;
53 const int kEvictedEntriesFile = disk_cache::BLOCK_EVICTED - 1;
54 const int kMaxAddress = 1 << 22;
55 const int kMinFileNumber = 1 << 16;
56
57 uint32 GetCellAddress(const IndexCell& cell) {
58 return cell.first_part & kCellAddressMask;
59 }
60
61 uint32 GetCellSmallTableAddress(const IndexCell& cell) {
62 return cell.first_part & kCellSmallTableAddressMask;
63 }
64
65 uint32 GetCellHash(const IndexCell& cell) {
66 return (cell.first_part >> kCellHashOffset) & kCellHashMask;
67 }
68
69 uint32 GetCellSmallTableHash(const IndexCell& cell) {
70 return (cell.first_part >> kCellSmallTableHashOffset) &
71 kCellSmallTableHashMask;
72 }
73
74 int GetCellTimestamp(const IndexCell& cell) {
75 return (cell.first_part >> kCellTimestampOffset) & kCellTimestampMask;
76 }
77
78 int GetCellReuse(const IndexCell& cell) {
79 return (cell.first_part >> kCellReuseOffset) & kCellReuseMask;
80 }
81
82 int GetCellState(const IndexCell& cell) {
83 return cell.last_part & kCellStateMask;
84 }
85
86 int GetCellGroup(const IndexCell& cell) {
87 return (cell.last_part >> kCellGroupOffset) & kCellGroupMask;
88 }
89
90 int GetCellSum(const IndexCell& cell) {
91 return (cell.last_part >> kCellSumOffset) & kCellSumMask;
92 }
93
94 void SetCellAddress(IndexCell* cell, uint32 address) {
95 DCHECK_LE(address, static_cast<uint32>(kCellAddressMask));
96 cell->first_part &= ~kCellAddressMask;
97 cell->first_part |= address;
98 }
99
100 void SetCellSmallTableAddress(IndexCell* cell, uint32 address) {
101 DCHECK_LE(address, static_cast<uint32>(kCellSmallTableAddressMask));
102 cell->first_part &= ~kCellSmallTableAddressMask;
103 cell->first_part |= address;
104 }
105
106 void SetCellHash(IndexCell* cell, uint32 hash) {
107 DCHECK_LE(hash, static_cast<uint32>(kCellHashMask));
108 cell->first_part &= ~(kCellHashMask << kCellHashOffset);
109 cell->first_part |= static_cast<int64>(hash) << kCellHashOffset;
110 }
111
112 void SetCellSmallTableHash(IndexCell* cell, uint32 hash) {
113 DCHECK_LE(hash, static_cast<uint32>(kCellSmallTableHashMask));
114 cell->first_part &= ~(kCellSmallTableHashMask << kCellSmallTableHashOffset);
115 cell->first_part |= static_cast<int64>(hash) << kCellSmallTableHashOffset;
116 }
117
118 void SetCellTimestamp(IndexCell* cell, int timestamp) {
119 DCHECK_LT(timestamp, 1 << 20);
120 DCHECK_GE(timestamp, 0);
121 cell->first_part &= ~(kCellTimestampMask << kCellTimestampOffset);
122 cell->first_part |= static_cast<int64>(timestamp) << kCellTimestampOffset;
123 }
124
125 void SetCellReuse(IndexCell* cell, int count) {
126 DCHECK_LT(count, 16);
127 DCHECK_GE(count, 0);
128 cell->first_part &= ~(kCellReuseMask << kCellReuseOffset);
129 cell->first_part |= static_cast<int64>(count) << kCellReuseOffset;
130 }
131
132 void SetCellState(IndexCell* cell, disk_cache::EntryState state) {
133 cell->last_part &= ~kCellStateMask;
134 cell->last_part |= state;
135 }
136
137 void SetCellGroup(IndexCell* cell, disk_cache::EntryGroup group) {
138 cell->last_part &= ~(kCellGroupMask << kCellGroupOffset);
139 cell->last_part |= group << kCellGroupOffset;
140 }
141
142 void SetCellSum(IndexCell* cell, int sum) {
143 DCHECK_LT(sum, 4);
144 DCHECK_GE(sum, 0);
145 cell->last_part &= ~(kCellSumMask << kCellSumOffset);
146 cell->last_part |= sum << kCellSumOffset;
147 }
148
149 // This is a very particular way to calculate the sum, so it will not match if
150 // compared a gainst a pure 2 bit, modulo 2 sum.
151 int CalculateCellSum(const IndexCell& cell) {
152 uint32* words = bit_cast<uint32*>(&cell);
153 uint8* bytes = bit_cast<uint8*>(&cell);
154 uint32 result = words[0] + words[1];
155 result += result >> 16;
156 result += (result >> 8) + (bytes[8] & 0x3f);
157 result += result >> 4;
158 result += result >> 2;
159 return result & 3;
160 }
161
162 bool SanityCheck(const IndexCell& cell) {
163 if (GetCellSum(cell) != CalculateCellSum(cell))
164 return false;
165
166 if (GetCellState(cell) > disk_cache::ENTRY_USED ||
167 GetCellGroup(cell) == disk_cache::ENTRY_RESERVED ||
168 GetCellGroup(cell) > disk_cache::ENTRY_EVICTED) {
169 return false;
170 }
171
172 return true;
173 }
174
175 int FileNumberFromAddress(int index_address) {
176 return index_address / kMinFileNumber;
177 }
178
179 int StartBlockFromAddress(int index_address) {
180 return index_address % kMinFileNumber;
181 }
182
183 bool IsValidAddress(disk_cache::Addr address) {
184 if (!address.is_initialized() ||
185 (address.file_type() != disk_cache::BLOCK_EVICTED &&
186 address.file_type() != disk_cache::BLOCK_ENTRIES)) {
187 return false;
188 }
189
190 return address.FileNumber() < FileNumberFromAddress(kMaxAddress);
191 }
192
193 bool IsNormalState(const IndexCell& cell) {
194 disk_cache::EntryState state =
195 static_cast<disk_cache::EntryState>(GetCellState(cell));
196 DCHECK_NE(state, disk_cache::ENTRY_FREE);
197 return state != disk_cache::ENTRY_DELETED &&
198 state != disk_cache::ENTRY_FIXING;
199 }
200
201 inline int GetNextBucket(int min_bucket_id, int max_bucket_id,
202 disk_cache::IndexBucket* table,
203 disk_cache::IndexBucket** bucket) {
204 if (!(*bucket)->next)
205 return 0;
206
207 int bucket_id = (*bucket)->next / disk_cache::kCellsPerBucket;
208 if (bucket_id < min_bucket_id || bucket_id > max_bucket_id) {
209 (*bucket)->next = 0;
210 return 0;
211 }
212 *bucket = &table[bucket_id - min_bucket_id];
213 return bucket_id;
214 }
215
216 // Updates the |iterator| with the current |cell|. This cell may cause all
217 // previous cells to be deleted (when a new target timestamp is found), the cell
218 // may be added to the list (if it matches the target timestamp), or may it be
219 // ignored.
220 void UpdateIterator(const disk_cache::EntryCell& cell,
221 int limit_time,
222 IndexIterator* iterator) {
223 int time = cell.GetTimestamp();
224 // Look for not interesting times.
225 if (iterator->forward && time <= limit_time)
226 return;
227 if (!iterator->forward && time >= limit_time)
228 return;
229
230 if ((iterator->forward && time < iterator->timestamp) ||
231 (!iterator->forward && time > iterator->timestamp)) {
232 // This timestamp is better than the one we had.
233 iterator->timestamp = time;
234 iterator->cells.clear();
235 }
236 if (time == iterator->timestamp) {
237 CellInfo cell_info = { cell.hash(), cell.GetAddress() };
238 iterator->cells.push_back(cell_info);
239 }
240 }
241
242 void InitIterator(IndexIterator* iterator) {
243 iterator->cells.clear();
244 iterator->timestamp = iterator->forward ? kint32max : 0;
245 }
246
247 } // namespace
248
249 namespace disk_cache {
250
251 EntryCell::~EntryCell() {
252 }
253
254 bool EntryCell::IsValid() const {
255 return GetCellAddress(cell_) != 0;
256 }
257
258 // This code has to map the cell address (up to 22 bits) to a general cache Addr
259 // (up to 24 bits of general addressing). It also set the implied file_number
260 // in the case of small tables. See also the comment by the definition of
261 // kEntriesFile.
262 Addr EntryCell::GetAddress() const {
263 uint32 address_value = GetAddressValue();
264 int file_number = FileNumberFromAddress(address_value);
265 if (small_table_) {
266 DCHECK_EQ(0, file_number);
267 file_number = (GetGroup() == ENTRY_EVICTED) ? kEvictedEntriesFile :
268 kEntriesFile;
269 }
270 DCHECK_NE(0, file_number);
271 FileType file_type = (GetGroup() == ENTRY_EVICTED) ? BLOCK_EVICTED :
272 BLOCK_ENTRIES;
273 return Addr(file_type, 1, file_number, StartBlockFromAddress(address_value));
274 }
275
276 EntryState EntryCell::GetState() const {
277 return static_cast<EntryState>(cell_.last_part & kCellStateMask);
278 }
279
280 EntryGroup EntryCell::GetGroup() const {
281 return static_cast<EntryGroup>((cell_.last_part >> kCellGroupOffset) &
282 kCellGroupMask);
283 }
284
285 int EntryCell::GetReuse() const {
286 return (cell_.first_part >> kCellReuseOffset) & kCellReuseMask;
287 }
288
289 int EntryCell::GetTimestamp() const {
290 return GetCellTimestamp(cell_);
291 }
292
293 void EntryCell::SetState(EntryState state) {
294 SetCellState(&cell_, state);
295 }
296
297 void EntryCell::SetGroup(EntryGroup group) {
298 SetCellGroup(&cell_, group);
299 }
300
301 void EntryCell::SetReuse(int count) {
302 SetCellReuse(&cell_, count);
303 }
304
305 void EntryCell::SetTimestamp(int timestamp) {
306 SetCellTimestamp(&cell_, timestamp);
307 }
308
309 // Static.
310 EntryCell EntryCell::GetEntryCellForTest(int32 cell_id,
311 uint32 hash,
312 Addr address,
313 IndexCell* cell,
314 bool small_table) {
315 if (cell) {
316 EntryCell entry_cell(cell_id, hash, *cell, small_table);
317 return entry_cell;
318 }
319
320 return EntryCell(cell_id, hash, address, small_table);
321 }
322
323 void EntryCell::SerializaForTest(IndexCell* destination) {
324 FixSum();
325 Serialize(destination);
326 }
327
328 EntryCell::EntryCell() : cell_id_(0), hash_(0), small_table_(false) {
329 cell_.Clear();
330 }
331
332 EntryCell::EntryCell(int32 cell_id, uint32 hash, Addr address, bool small_table)
333 : cell_id_(cell_id),
334 hash_(hash),
335 small_table_(small_table) {
336 DCHECK(IsValidAddress(address) || !address.value());
337
338 cell_.Clear();
339 SetCellState(&cell_, ENTRY_NEW);
340 SetCellGroup(&cell_, ENTRY_NO_USE);
341 if (small_table) {
342 DCHECK(address.FileNumber() == kEntriesFile ||
343 address.FileNumber() == kEvictedEntriesFile);
344 SetCellSmallTableAddress(&cell_, address.start_block());
345 SetCellSmallTableHash(&cell_, hash >> kHashSmallTableShift);
346 } else {
347 uint32 cell_address = address.FileNumber() << 16 | address.start_block();
348 SetCellAddress(&cell_, cell_address);
349 SetCellHash(&cell_, hash >> kHashShift);
350 }
351 }
352
353 EntryCell::EntryCell(int32 cell_id,
354 uint32 hash,
355 const IndexCell& cell,
356 bool small_table)
357 : cell_id_(cell_id),
358 hash_(hash),
359 cell_(cell),
360 small_table_(small_table) {
361 }
362
363 void EntryCell::FixSum() {
364 SetCellSum(&cell_, CalculateCellSum(cell_));
365 }
366
367 uint32 EntryCell::GetAddressValue() const {
368 if (small_table_)
369 return GetCellSmallTableAddress(cell_);
370
371 return GetCellAddress(cell_);
372 }
373
374 uint32 EntryCell::RecomputeHash() {
375 if (small_table_) {
376 hash_ &= (1 << kHashSmallTableShift) - 1;
377 hash_ |= GetCellSmallTableHash(cell_) << kHashSmallTableShift;
378 return hash_;
379 }
380
381 hash_ &= (1 << kHashShift) - 1;
382 hash_ |= GetCellHash(cell_) << kHashShift;
383 return hash_;
384 }
385
386 void EntryCell::Serialize(IndexCell* destination) const {
387 *destination = cell_;
388 }
389
390 EntrySet::EntrySet() : evicted_count(0), current(0) {
391 }
392
393 EntrySet::~EntrySet() {
394 }
395
396 IndexIterator::IndexIterator() {
397 }
398
399 IndexIterator::~IndexIterator() {
400 }
401
402 IndexTableInitData::IndexTableInitData() {
403 }
404
405 IndexTableInitData::~IndexTableInitData() {
406 }
407
408 // -----------------------------------------------------------------------
409
410 IndexTable::IndexTable(IndexTableBackend* backend)
411 : backend_(backend),
412 header_(NULL),
413 main_table_(NULL),
414 extra_table_(NULL),
415 modified_(false),
416 small_table_(false) {
417 }
418
419 IndexTable::~IndexTable() {
420 }
421
422 // For a general description of the index tables see:
423 // http://www.chromium.org/developers/design-documents/network-stack/disk-cache/ disk-cache-v3#TOC-Index
424 //
425 // The index is split between two tables: the main_table_ and the extra_table_.
426 // The main table can grow only by doubling its number of cells, while the
427 // extra table can grow slowly, because it only contain cells that overflow
428 // from the main table. In order to locate a given cell, part of the hash is
429 // used directly as an index into the main table; once that bucket is located,
430 // all cells with that partial hash (i.e., belonging to that bucket) are
431 // inspected, and if present, the next bucket (located on the extra table) is
432 // then located. For more information on bucket chaining see:
433 // http://www.chromium.org/developers/design-documents/network-stack/disk-cache/ disk-cache-v3#TOC-Buckets
434 //
435 // There are two cases when increasing the size:
436 // - Doubling the size of the main table
437 // - Adding more entries to the extra table
438 //
439 // For example, consider a 64k main table with 8k cells on the extra table (for
440 // a total of 72k cells). Init can be called to add another 8k cells at the end
441 // (grow to 80k cells). When the size of the extra table approaches 64k, Init
442 // can be called to double the main table (to 128k) and go back to a small extra
443 // table.
444 void IndexTable::Init(IndexTableInitData* params) {
445 bool growing = header_ != NULL;
446 scoped_ptr<IndexBucket[]> old_extra_table;
447 header_ = &params->index_bitmap->header;
448
449 if (params->main_table) {
450 if (main_table_) {
451 // This is doubling the size of main table.
452 DCHECK_EQ(base::bits::Log2Floor(header_->table_len),
453 base::bits::Log2Floor(backup_header_->table_len) + 1);
454 int extra_size = (header()->max_bucket - mask_) * kCellsPerBucket;
455 DCHECK_GE(extra_size, 0);
456
457 // Doubling the size implies deleting the extra table and moving as many
458 // cells as we can to the main table, so we first copy the old one. This
459 // is not required when just growing the extra table because we don't
460 // move any cell in that case.
461 old_extra_table.reset(new IndexBucket[extra_size]);
462 memcpy(old_extra_table.get(), extra_table_,
463 extra_size * sizeof(IndexBucket));
464 memset(params->extra_table, 0, extra_size * sizeof(IndexBucket));
465 }
466 main_table_ = params->main_table;
467 }
468 DCHECK(main_table_);
469 extra_table_ = params->extra_table;
470
471 // extra_bits_ is really measured against table-size specific values.
472 const int kMaxAbsoluteExtraBits = 12; // From smallest to largest table.
473 const int kMaxExtraBitsSmallTable = 6; // From smallest to 64K table.
474
475 extra_bits_ = base::bits::Log2Floor(header_->table_len) -
476 base::bits::Log2Floor(kBaseTableLen);
477 DCHECK_GE(extra_bits_, 0);
478 DCHECK_LT(extra_bits_, kMaxAbsoluteExtraBits);
479
480 // Note that following the previous code the constants could be derived as
481 // kMaxAbsoluteExtraBits = base::bits::Log2Floor(max table len) -
482 // base::bits::Log2Floor(kBaseTableLen);
483 // = 22 - base::bits::Log2Floor(1024) = 22 - 10;
484 // kMaxExtraBitsSmallTable = base::bits::Log2Floor(max 16 bit table) - 10.
485
486 mask_ = ((kBaseTableLen / kCellsPerBucket) << extra_bits_) - 1;
487 small_table_ = extra_bits_ < kMaxExtraBitsSmallTable;
488 if (!small_table_)
489 extra_bits_ -= kMaxExtraBitsSmallTable;
490
491 // table_len keeps the max number of cells stored by the index. We need a
492 // bitmap with 1 bit per cell, and that bitmap has num_words 32-bit words.
493 int num_words = (header_->table_len + 31) / 32;
494
495 if (old_extra_table) {
496 // All the cells from the extra table are moving to the new tables so before
497 // creating the bitmaps, clear the part of the bitmap referring to the extra
498 // table.
499 int old_main_table_bit_words = ((mask_ >> 1) + 1) * kCellsPerBucket / 32;
500 DCHECK_GT(num_words, old_main_table_bit_words);
501 memset(params->index_bitmap->bitmap + old_main_table_bit_words, 0,
502 (num_words - old_main_table_bit_words) * sizeof(int32));
503
504 DCHECK(growing);
505 int old_num_words = (backup_header_.get()->table_len + 31) / 32;
506 DCHECK_GT(old_num_words, old_main_table_bit_words);
507 memset(backup_bitmap_storage_.get() + old_main_table_bit_words, 0,
508 (old_num_words - old_main_table_bit_words) * sizeof(int32));
509 }
510 bitmap_.reset(new Bitmap(params->index_bitmap->bitmap, header_->table_len,
511 num_words));
512
513 if (growing) {
514 int old_num_words = (backup_header_.get()->table_len + 31) / 32;
515 DCHECK_GE(num_words, old_num_words);
516 scoped_ptr<uint32[]> storage(new uint32[num_words]);
517 memcpy(storage.get(), backup_bitmap_storage_.get(),
518 old_num_words * sizeof(int32));
519 memset(storage.get() + old_num_words, 0,
520 (num_words - old_num_words) * sizeof(int32));
521
522 backup_bitmap_storage_.swap(storage);
523 backup_header_->table_len = header_->table_len;
524 } else {
525 backup_bitmap_storage_.reset(params->backup_bitmap.release());
526 backup_header_.reset(params->backup_header.release());
527 }
528
529 num_words = (backup_header_->table_len + 31) / 32;
530 backup_bitmap_.reset(new Bitmap(backup_bitmap_storage_.get(),
531 backup_header_->table_len, num_words));
532 if (old_extra_table)
533 MoveCells(old_extra_table.get());
534
535 if (small_table_)
536 DCHECK(header_->flags & SMALL_CACHE);
537
538 // All tables and backups are needed for operation.
539 DCHECK(main_table_);
540 DCHECK(extra_table_);
541 DCHECK(bitmap_.get());
542 }
543
544 void IndexTable::Reset() {
545 header_ = NULL;
546 main_table_ = NULL;
547 extra_table_ = NULL;
548 bitmap_.reset();
549 backup_bitmap_.reset();
550 backup_header_.reset();
551 backup_bitmap_storage_.reset();
552 modified_ = false;
553 }
554
555 // The general method for locating cells is to:
556 // 1. Get the first bucket. This usually means directly indexing the table (as
557 // this method does), or iterating through all possible buckets.
558 // 2. Iterate through all the cells in that first bucket.
559 // 3. If there is a linked bucket, locate it directly in the extra table.
560 // 4. Go back to 2, as needed.
561 //
562 // One consequence of this pattern is that we never start looking at buckets in
563 // the extra table, unless we are following a link from the main table.
564 EntrySet IndexTable::LookupEntries(uint32 hash) {
565 EntrySet entries;
566 int bucket_id = static_cast<int>(hash & mask_);
567 IndexBucket* bucket = &main_table_[bucket_id];
568 do {
569 for (int i = 0; i < kCellsPerBucket; i++) {
570 IndexCell* current_cell = &bucket->cells[i];
571 if (!GetAddressValue(*current_cell))
572 continue;
573 if (!SanityCheck(*current_cell)) {
574 NOTREACHED();
575 int cell_id = bucket_id * kCellsPerBucket + i;
576 current_cell->Clear();
577 bitmap_->Set(cell_id, false);
578 backup_bitmap_->Set(cell_id, false);
579 modified_ = true;
580 continue;
581 }
582 int cell_id = bucket_id * kCellsPerBucket + i;
583 if (MisplacedHash(*current_cell, hash)) {
584 HandleMisplacedCell(current_cell, cell_id, hash & mask_);
585 } else if (IsHashMatch(*current_cell, hash)) {
586 EntryCell entry_cell(cell_id, hash, *current_cell, small_table_);
587 CheckState(entry_cell);
588 if (entry_cell.GetState() != ENTRY_DELETED) {
589 entries.cells.push_back(entry_cell);
590 if (entry_cell.GetGroup() == ENTRY_EVICTED)
591 entries.evicted_count++;
592 }
593 }
594 }
595 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
596 &bucket);
597 } while (bucket_id);
598 return entries;
599 }
600
601 EntryCell IndexTable::CreateEntryCell(uint32 hash, Addr address) {
602 DCHECK(IsValidAddress(address));
603 DCHECK(address.FileNumber() || address.start_block());
604
605 int bucket_id = static_cast<int>(hash & mask_);
606 int cell_id = 0;
607 IndexBucket* bucket = &main_table_[bucket_id];
608 IndexCell* current_cell = NULL;
609 bool found = false;
610 do {
611 for (int i = 0; i < kCellsPerBucket && !found; i++) {
612 current_cell = &bucket->cells[i];
613 if (!GetAddressValue(*current_cell)) {
614 cell_id = bucket_id * kCellsPerBucket + i;
615 found = true;
616 }
617 }
618 if (found)
619 break;
620 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
621 &bucket);
622 } while (bucket_id);
623
624 if (!found) {
625 bucket_id = NewExtraBucket();
626 if (bucket_id) {
627 cell_id = bucket_id * kCellsPerBucket;
628 bucket->next = cell_id;
629 bucket = &extra_table_[bucket_id - (mask_ + 1)];
630 bucket->hash = hash & mask_;
631 found = true;
632 } else {
633 // address 0 is a reserved value, and the caller interprets it as invalid.
634 address.set_value(0);
635 }
636 }
637
638 EntryCell entry_cell(cell_id, hash, address, small_table_);
639 if (address.file_type() == BLOCK_EVICTED)
640 entry_cell.SetGroup(ENTRY_EVICTED);
641 else
642 entry_cell.SetGroup(ENTRY_NO_USE);
643 Save(&entry_cell);
644
645 if (found) {
646 bitmap_->Set(cell_id, true);
647 backup_bitmap_->Set(cell_id, true);
648 header()->used_cells++;
649 modified_ = true;
650 }
651
652 return entry_cell;
653 }
654
655 EntryCell IndexTable::FindEntryCell(uint32 hash, Addr address) {
656 return FindEntryCellImpl(hash, address, false);
657 }
658
659 int IndexTable::CalculateTimestamp(Time time) {
660 TimeDelta delta = time - Time::FromInternalValue(header_->base_time);
661 return std::max(delta.InMinutes(), 0);
662 }
663
664 base::Time IndexTable::TimeFromTimestamp(int timestamp) {
665 return Time::FromInternalValue(header_->base_time) +
666 TimeDelta::FromMinutes(timestamp);
667 }
668
669 void IndexTable::SetSate(uint32 hash, Addr address, EntryState state) {
670 EntryCell cell = FindEntryCellImpl(hash, address, state == ENTRY_FREE);
671 if (!cell.IsValid()) {
672 NOTREACHED();
673 return;
674 }
675
676 EntryState old_state = cell.GetState();
677 switch (state) {
678 case ENTRY_FREE:
679 DCHECK_EQ(old_state, ENTRY_DELETED);
680 break;
681 case ENTRY_NEW:
682 DCHECK_EQ(old_state, ENTRY_FREE);
683 break;
684 case ENTRY_OPEN:
685 DCHECK_EQ(old_state, ENTRY_USED);
686 break;
687 case ENTRY_MODIFIED:
688 DCHECK_EQ(old_state, ENTRY_OPEN);
689 break;
690 case ENTRY_DELETED:
691 DCHECK(old_state == ENTRY_NEW || old_state == ENTRY_OPEN ||
692 old_state == ENTRY_MODIFIED);
693 break;
694 case ENTRY_USED:
695 DCHECK(old_state == ENTRY_NEW || old_state == ENTRY_OPEN ||
696 old_state == ENTRY_MODIFIED);
697 break;
698 case ENTRY_FIXING:
699 break;
700 };
701
702 modified_ = true;
703 if (state == ENTRY_DELETED) {
704 bitmap_->Set(cell.cell_id(), false);
705 backup_bitmap_->Set(cell.cell_id(), false);
706 } else if (state == ENTRY_FREE) {
707 cell.Clear();
708 Write(cell);
709 header()->used_cells--;
710 return;
711 }
712 cell.SetState(state);
713
714 Save(&cell);
715 }
716
717 void IndexTable::UpdateTime(uint32 hash, Addr address, base::Time current) {
718 EntryCell cell = FindEntryCell(hash, address);
719 if (!cell.IsValid())
720 return;
721
722 int minutes = CalculateTimestamp(current);
723
724 // Keep about 3 months of headroom.
725 const int kMaxTimestamp = (1 << 20) - 60 * 24 * 90;
726 if (minutes > kMaxTimestamp) {
727 // TODO(rvargas):
728 // Update header->old_time and trigger a timer
729 // Rebaseline timestamps and don't update sums
730 // Start a timer (about 2 backups)
731 // fix all ckecksums and trigger another timer
732 // update header->old_time because rebaseline is done.
733 minutes = std::min(minutes, (1 << 20) - 1);
734 }
735
736 cell.SetTimestamp(minutes);
737 Save(&cell);
738 }
739
740 void IndexTable::Save(EntryCell* cell) {
741 cell->FixSum();
742 Write(*cell);
743 }
744
745 void IndexTable::GetOldest(IndexIterator* no_use,
746 IndexIterator* low_use,
747 IndexIterator* high_use) {
748 no_use->forward = true;
749 low_use->forward = true;
750 high_use->forward = true;
751 InitIterator(no_use);
752 InitIterator(low_use);
753 InitIterator(high_use);
754
755 WalkTables(-1, no_use, low_use, high_use);
756 }
757
758 bool IndexTable::GetNextCells(IndexIterator* iterator) {
759 int current_time = iterator->timestamp;
760 InitIterator(iterator);
761
762 WalkTables(current_time, iterator, iterator, iterator);
763 return !iterator->cells.empty();
764 }
765
766 void IndexTable::OnBackupTimer() {
767 if (!modified_)
768 return;
769
770 int num_words = (header_->table_len + 31) / 32;
771 int num_bytes = num_words * 4 + static_cast<int>(sizeof(*header_));
772 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(num_bytes));
773 memcpy(buffer->data(), header_, sizeof(*header_));
774 memcpy(buffer->data() + sizeof(*header_), backup_bitmap_storage_.get(),
775 num_words * 4);
776 backend_->SaveIndex(buffer, num_bytes);
777 modified_ = false;
778 }
779
780 // -----------------------------------------------------------------------
781
782 EntryCell IndexTable::FindEntryCellImpl(uint32 hash, Addr address,
783 bool allow_deleted) {
784 int bucket_id = static_cast<int>(hash & mask_);
785 IndexBucket* bucket = &main_table_[bucket_id];
786 do {
787 for (int i = 0; i < kCellsPerBucket; i++) {
788 IndexCell* current_cell = &bucket->cells[i];
789 if (!GetAddressValue(*current_cell))
790 continue;
791 DCHECK(SanityCheck(*current_cell));
792 if (IsHashMatch(*current_cell, hash)) {
793 // We have a match.
794 int cell_id = bucket_id * kCellsPerBucket + i;
795 EntryCell entry_cell(cell_id, hash, *current_cell, small_table_);
796 if (entry_cell.GetAddress() != address)
797 continue;
798
799 if (!allow_deleted && entry_cell.GetState() == ENTRY_DELETED)
800 continue;
801
802 return entry_cell;
803 }
804 }
805 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
806 &bucket);
807 } while (bucket_id);
808 return EntryCell();
809 }
810
811 void IndexTable::CheckState(const EntryCell& cell) {
812 int current_state = cell.GetState();
813 if (current_state != ENTRY_FIXING) {
814 bool present = ((current_state & 3) != 0); // Look at the last two bits.
815 if (present != bitmap_->Get(cell.cell_id()) ||
816 present != backup_bitmap_->Get(cell.cell_id())) {
817 // There's a mismatch.
818 if (current_state == ENTRY_DELETED) {
819 // We were in the process of deleting this entry. Finish now.
820 backend_->DeleteCell(cell);
821 } else {
822 current_state = ENTRY_FIXING;
823 EntryCell bad_cell(cell);
824 bad_cell.SetState(ENTRY_FIXING);
825 Save(&bad_cell);
826 }
827 }
828 }
829
830 if (current_state == ENTRY_FIXING)
831 backend_->FixCell(cell);
832 }
833
834 void IndexTable::Write(const EntryCell& cell) {
835 IndexBucket* bucket = NULL;
836 int bucket_id = cell.cell_id() / kCellsPerBucket;
837 if (bucket_id < static_cast<int32>(mask_ + 1)) {
838 bucket = &main_table_[bucket_id];
839 } else {
840 DCHECK_LE(bucket_id, header()->max_bucket);
841 bucket = &extra_table_[bucket_id - (mask_ + 1)];
842 }
843
844 int cell_number = cell.cell_id() % kCellsPerBucket;
845 if (GetAddressValue(bucket->cells[cell_number]) && cell.GetAddressValue()) {
846 DCHECK_EQ(cell.GetAddressValue(),
847 GetAddressValue(bucket->cells[cell_number]));
848 }
849 cell.Serialize(&bucket->cells[cell_number]);
850 }
851
852 int IndexTable::NewExtraBucket() {
853 int safe_window = (header()->table_len < kNumExtraBlocks * 2) ?
854 kNumExtraBlocks / 4 : kNumExtraBlocks;
855 if (header()->table_len - header()->max_bucket * kCellsPerBucket <
856 safe_window) {
857 backend_->GrowIndex();
858 }
859
860 if (header()->max_bucket * kCellsPerBucket ==
861 header()->table_len - kCellsPerBucket) {
862 return 0;
863 }
864
865 header()->max_bucket++;
866 return header()->max_bucket;
867 }
868
869 void IndexTable::WalkTables(int limit_time,
870 IndexIterator* no_use,
871 IndexIterator* low_use,
872 IndexIterator* high_use) {
873 header_->num_no_use_entries = 0;
874 header_->num_low_use_entries = 0;
875 header_->num_high_use_entries = 0;
876 header_->num_evicted_entries = 0;
877
878 for (int i = 0; i < static_cast<int32>(mask_ + 1); i++) {
879 int bucket_id = i;
880 IndexBucket* bucket = &main_table_[i];
881 do {
882 UpdateFromBucket(bucket, i, limit_time, no_use, low_use, high_use);
883
884 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
885 &bucket);
886 } while (bucket_id);
887 }
888 header_->num_entries = header_->num_no_use_entries +
889 header_->num_low_use_entries +
890 header_->num_high_use_entries +
891 header_->num_evicted_entries;
892 modified_ = true;
893 }
894
895 void IndexTable::UpdateFromBucket(IndexBucket* bucket, int bucket_hash,
896 int limit_time,
897 IndexIterator* no_use,
898 IndexIterator* low_use,
899 IndexIterator* high_use) {
900 for (int i = 0; i < kCellsPerBucket; i++) {
901 IndexCell& current_cell = bucket->cells[i];
902 if (!GetAddressValue(current_cell))
903 continue;
904 DCHECK(SanityCheck(current_cell));
905 if (!IsNormalState(current_cell))
906 continue;
907
908 EntryCell entry_cell(0, GetFullHash(current_cell, bucket_hash),
909 current_cell, small_table_);
910 switch (GetCellGroup(current_cell)) {
911 case ENTRY_NO_USE:
912 UpdateIterator(entry_cell, limit_time, no_use);
913 header_->num_no_use_entries++;
914 break;
915 case ENTRY_LOW_USE:
916 UpdateIterator(entry_cell, limit_time, low_use);
917 header_->num_low_use_entries++;
918 break;
919 case ENTRY_HIGH_USE:
920 UpdateIterator(entry_cell, limit_time, high_use);
921 header_->num_high_use_entries++;
922 break;
923 case ENTRY_EVICTED:
924 header_->num_evicted_entries++;
925 break;
926 default:
927 NOTREACHED();
928 }
929 }
930 }
931
932 // This code is only called from Init() so the internal state of this object is
933 // in flux (this method is performing the last steps of re-initialization). As
934 // such, random methods are not supposed to work at this point, so whatever this
935 // method calls should be relatively well controlled and it may require some
936 // degree of "stable state faking".
937 void IndexTable::MoveCells(IndexBucket* old_extra_table) {
938 int max_hash = (mask_ + 1) / 2;
939 int max_bucket = header()->max_bucket;
940 header()->max_bucket = mask_;
941 int used_cells = header()->used_cells;
942
943 // Consider a large cache: a cell stores the upper 18 bits of the hash
944 // (h >> 14). If the table is say 8 times the original size (growing from 4x),
945 // the bit that we are interested in would be the 3rd bit of the stored value,
946 // in other words 'multiplier' >> 1.
947 uint32 new_bit = (1 << extra_bits_) >> 1;
948
949 scoped_ptr<IndexBucket[]> old_main_table;
950 IndexBucket* source_table = main_table_;
951 bool upgrade_format = !extra_bits_;
952 if (upgrade_format) {
953 // This method should deal with migrating a small table to a big one. Given
954 // that the first thing to do is read the old table, set small_table_ for
955 // the size of the old table. Now, when moving a cell, the result cannot be
956 // placed in the old table or we will end up reading it again and attempting
957 // to move it, so we have to copy the whole table at once.
958 DCHECK(!small_table_);
959 small_table_ = true;
960 old_main_table.reset(new IndexBucket[max_hash]);
961 memcpy(old_main_table.get(), main_table_, max_hash * sizeof(IndexBucket));
962 memset(main_table_, 0, max_hash * sizeof(IndexBucket));
963 source_table = old_main_table.get();
964 }
965
966 for (int i = 0; i < max_hash; i++) {
967 int bucket_id = i;
968 IndexBucket* bucket = &source_table[i];
969 do {
970 for (int j = 0; j < kCellsPerBucket; j++) {
971 IndexCell& current_cell = bucket->cells[j];
972 if (!GetAddressValue(current_cell))
973 continue;
974 DCHECK(SanityCheck(current_cell));
975 if (bucket_id == i) {
976 if (upgrade_format || (GetHashValue(current_cell) & new_bit)) {
977 // Move this cell to the upper half of the table.
978 MoveSingleCell(&current_cell, bucket_id * kCellsPerBucket + j, i,
979 true);
980 }
981 } else {
982 // All cells on extra buckets have to move.
983 MoveSingleCell(&current_cell, bucket_id * kCellsPerBucket + j, i,
984 true);
985 }
986 }
987
988 // There is no need to clear the old bucket->next value because if falls
989 // within the main table so it will be fixed when attempting to follow
990 // the link.
Randy Smith (Not in Mondays) 2013/12/26 21:45:49 This comment is useful for someone reading through
rvargas (doing something else) 2013/12/27 19:31:47 Done.
991 bucket_id = GetNextBucket(max_hash, max_bucket, old_extra_table, &bucket);
992 } while (bucket_id);
993 }
994
995 DCHECK_EQ(header()->used_cells, used_cells);
996
997 if (upgrade_format) {
998 small_table_ = false;
999 header()->flags &= ~SMALL_CACHE;
1000 }
1001 }
1002
1003 void IndexTable::MoveSingleCell(IndexCell* current_cell, int cell_id,
1004 int main_table_index, bool growing) {
1005 uint32 hash = GetFullHash(*current_cell, main_table_index);
1006 EntryCell old_cell(cell_id, hash, *current_cell, small_table_);
1007
1008 // This method may be called when moving entries from a small table to a
1009 // normal table. In that case, the caller (MoveCells) has to read the old
1010 // table, so it needs small_table_ set to true, but this method needs to
1011 // write to the new table so small_table_ has to be set to false, and the
1012 // value restored to true before returning.
1013 bool upgrade_format = !extra_bits_ && growing;
1014 if (upgrade_format)
1015 small_table_ = false;
1016 EntryCell new_cell = CreateEntryCell(hash, old_cell.GetAddress());
1017
1018 if (!new_cell.IsValid()) {
1019 // We'll deal with this entry later.
1020 if (upgrade_format)
1021 small_table_ = true;
1022 return;
1023 }
1024
1025 new_cell.SetState(old_cell.GetState());
1026 new_cell.SetGroup(old_cell.GetGroup());
1027 new_cell.SetReuse(old_cell.GetReuse());
1028 new_cell.SetTimestamp(old_cell.GetTimestamp());
1029 Save(&new_cell);
1030 modified_ = true;
1031 if (upgrade_format)
1032 small_table_ = true;
1033
1034 if (old_cell.GetState() == ENTRY_DELETED) {
1035 bitmap_->Set(new_cell.cell_id(), false);
1036 backup_bitmap_->Set(new_cell.cell_id(), false);
1037 }
1038
1039 if (!growing || cell_id / kCellsPerBucket == main_table_index) {
1040 // Only delete entries that live on the main table.
1041 if (!upgrade_format) {
1042 old_cell.Clear();
1043 Write(old_cell);
1044 }
1045
1046 if (cell_id != new_cell.cell_id()) {
1047 bitmap_->Set(old_cell.cell_id(), false);
1048 backup_bitmap_->Set(old_cell.cell_id(), false);
1049 }
1050 }
1051 header()->used_cells--;
1052 }
1053
1054 void IndexTable::HandleMisplacedCell(IndexCell* current_cell, int cell_id,
1055 int main_table_index) {
1056 NOTREACHED(); // No unit tests yet.
1057
1058 // The cell may be misplaced, or a duplicate cell exists with this data.
1059 uint32 hash = GetFullHash(*current_cell, main_table_index);
1060 MoveSingleCell(current_cell, cell_id, main_table_index, false);
1061
1062 // Now look for a duplicate cell.
1063 CheckBucketList(hash & mask_);
1064 }
1065
1066 void IndexTable::CheckBucketList(int bucket_id) {
1067 typedef std::pair<int, EntryGroup> AddressAndGroup;
1068 std::set<AddressAndGroup> entries;
1069 IndexBucket* bucket = &main_table_[bucket_id];
1070 int bucket_hash = bucket_id;
1071 do {
1072 for (int i = 0; i < kCellsPerBucket; i++) {
1073 IndexCell* current_cell = &bucket->cells[i];
1074 if (!GetAddressValue(*current_cell))
1075 continue;
1076 if (!SanityCheck(*current_cell)) {
1077 NOTREACHED();
1078 current_cell->Clear();
1079 continue;
1080 }
1081 int cell_id = bucket_id * kCellsPerBucket + i;
1082 EntryCell cell(cell_id, GetFullHash(*current_cell, bucket_hash),
1083 *current_cell, small_table_);
1084 if (!entries.insert(std::make_pair(cell.GetAddress().value(),
1085 cell.GetGroup())).second) {
1086 current_cell->Clear();
1087 continue;
1088 }
1089 CheckState(cell);
1090 }
1091
1092 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
1093 &bucket);
1094 } while (bucket_id);
1095 }
1096
1097 uint32 IndexTable::GetAddressValue(const IndexCell& cell) {
1098 if (small_table_)
1099 return GetCellSmallTableAddress(cell);
1100
1101 return GetCellAddress(cell);
1102 }
1103
1104 uint32 IndexTable::GetHashValue(const IndexCell& cell) {
1105 if (small_table_)
1106 return GetCellSmallTableHash(cell);
1107
1108 return GetCellHash(cell);
1109 }
1110
1111 uint32 IndexTable::GetFullHash(const IndexCell& cell, uint32 lower_part) {
1112 // It is OK for the high order bits of lower_part to overlap with the stored
1113 // part of the hash.
1114 if (small_table_)
1115 return (GetCellSmallTableHash(cell) << kHashSmallTableShift) | lower_part;
1116
1117 return (GetCellHash(cell) << kHashShift) | lower_part;
1118 }
1119
1120 // All the bits stored in the cell should match the provided hash.
1121 bool IndexTable::IsHashMatch(const IndexCell& cell, uint32 hash) {
1122 hash = small_table_ ? hash >> kHashSmallTableShift : hash >> kHashShift;
1123 return GetHashValue(cell) == hash;
1124 }
1125
1126 bool IndexTable::MisplacedHash(const IndexCell& cell, uint32 hash) {
1127 if (!extra_bits_)
1128 return false;
1129
1130 uint32 mask = (1 << extra_bits_) - 1;
1131 hash = small_table_ ? hash >> kHashSmallTableShift : hash >> kHashShift;
1132 return (GetHashValue(cell) & mask) != (hash & mask);
1133 }
1134
1135 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698