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

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, 1 month 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 uint32 kMaxAddress = 1 << 22;
26
27 const int kCellHashOffset = 22;
28 const int kCellSmallTableHashOffset = 16;
29 const int kCellTimestampOffset = 40;
30 const int kCellReuseOffset = 60;
31 const int kCellGroupOffset = 3;
32 const int kCellSumOffset = 6;
33
34 const uint64 kCellAddressMask = 0x3FFFFF;
35 const uint64 kCellSmallTableAddressMask = 0xFFFF;
36 const uint64 kCellHashMask = 0x3FFFF;
37 const uint64 kCellSmallTableHashMask = 0xFFFFFF;
38 const uint64 kCellTimestampMask = 0xFFFFF;
39 const uint64 kCellReuseMask = 0xF;
40 const uint8 kCellStateMask = 0x7;
41 const uint8 kCellGroupMask = 0x7;
42 const uint8 kCellSumMask = 0x3;
43
44 const int kHashShift = 14;
45 const int kHashSmallTableShift = 8;
46
47 // Unfortunately we have to break the abstaction a little here: the file number
48 // where entries are stored is outside of the control of this code, and it is
49 // usually part of the stored address. However, for small tables we only store
50 // 16 bits of the address so the file number is never stored on a cell. We have
51 // to infere the file number from the type of entry (normal vs evicted), and
52 // the knowledge that given that the table will not keep more than 64k entries,
53 // a single file of each type is enough.
54 const int kEntriesFile = disk_cache::BLOCK_ENTRIES - 1;
55 const int kEvictedEntriesFile = disk_cache::BLOCK_EVICTED - 1;
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 bool IsValidAddress(disk_cache::Addr address) {
176 if (!address.is_initialized() ||
177 (address.file_type() != disk_cache::BLOCK_EVICTED &&
178 address.file_type() != disk_cache::BLOCK_ENTRIES)) {
179 return false;
180 }
181
182 return address.ToIndexEntryAddress() < kMaxAddress;
183 }
184
185 bool IsNormalState(const IndexCell& cell) {
186 disk_cache::EntryState state =
187 static_cast<disk_cache::EntryState>(GetCellState(cell));
188 DCHECK_NE(state, disk_cache::ENTRY_FREE);
189 return state != disk_cache::ENTRY_DELETED &&
190 state != disk_cache::ENTRY_FIXING;
191 }
192
193 inline int GetNextBucket(int min_bucket_id, int max_bucket_id,
194 disk_cache::IndexBucket* table,
195 disk_cache::IndexBucket** bucket) {
196 if (!(*bucket)->next)
197 return 0;
198
199 int bucket_id = (*bucket)->next / disk_cache::kCellsPerBucket;
200 if (bucket_id < min_bucket_id || bucket_id > max_bucket_id) {
201 (*bucket)->next = 0;
202 return 0;
203 }
204 *bucket = &table[bucket_id - min_bucket_id];
205 return bucket_id;
206 }
207
208 void UpdateListWithCell(int bucket_hash,
Randy Smith (Not in Mondays) 2013/11/07 20:25:15 A quick comment describing what this does? It's n
Randy Smith (Not in Mondays) 2013/11/07 20:25:15 Why is |bucket_hash| an argument to this function?
rvargas (doing something else) 2013/11/08 04:22:30 will do
rvargas (doing something else) 2013/11/08 04:22:30 Sorry about that... it's just the result of refact
209 const disk_cache::EntryCell& cell,
210 CellList* list,
211 int* list_time) {
212 if (!list)
213 return;
214
215 int time = cell.GetTimestamp();
216 if (time < *list_time) {
217 *list_time = time;
218 list->clear();
219 }
220 if (time == *list_time) {
221 CellInfo cell_info = { cell.hash(), cell.GetAddress() };
222 list->push_back(cell_info);
223 }
224 }
225
226 } // namespace
227
228 namespace disk_cache {
229
230 EntryCell::~EntryCell() {
231 }
232
233 bool EntryCell::IsValid() const {
234 return GetCellAddress(cell_) != 0;
235 }
236
237 Addr EntryCell::GetAddress() const {
238 uint32 address_value = GetAddressValue();
239 if (small_table_) {
240 if (GetGroup() == ENTRY_EVICTED)
241 return Addr(BLOCK_EVICTED, 1, kEvictedEntriesFile, address_value);
242
243 return Addr(BLOCK_ENTRIES, 1, kEntriesFile, address_value);
244 }
245
246 if (GetGroup() == ENTRY_EVICTED)
247 return Addr::FromEvictedAddress(address_value);
248 else
249 return Addr::FromEntryAddress(address_value);
250 }
251
252 EntryState EntryCell::GetState() const {
253 return static_cast<EntryState>(cell_.last_part & kCellStateMask);
254 }
255
256 EntryGroup EntryCell::GetGroup() const {
257 return static_cast<EntryGroup>((cell_.last_part >> kCellGroupOffset) &
258 kCellGroupMask);
259 }
260
261 int EntryCell::GetReuse() const {
262 return (cell_.first_part >> kCellReuseOffset) & kCellReuseMask;
263 }
264
265 int EntryCell::GetTimestamp() const {
266 return GetCellTimestamp(cell_);
267 }
268
269 void EntryCell::SetState(EntryState state) {
270 SetCellState(&cell_, state);
271 }
272
273 void EntryCell::SetGroup(EntryGroup group) {
274 SetCellGroup(&cell_, group);
275 }
276
277 void EntryCell::SetReuse(int count) {
278 SetCellReuse(&cell_, count);
279 }
280
281 void EntryCell::SetTimestamp(int timestamp) {
282 SetCellTimestamp(&cell_, timestamp);
283 }
284
285 // Static.
286 EntryCell EntryCell::GetEntryCellForTest(int32 cell_id,
287 uint32 hash,
288 Addr address,
289 IndexCell* cell,
290 bool small_table) {
291 if (cell) {
292 EntryCell entry_cell(cell_id, hash, *cell, small_table);
293 return entry_cell;
294 }
295
296 return EntryCell(cell_id, hash, address, small_table);
297 }
298
299 void EntryCell::SerializaForTest(IndexCell* destination) {
300 FixSum();
301 Serialize(destination);
302 }
303
304 EntryCell::EntryCell() : cell_id_(0), hash_(0), small_table_(false) {
305 cell_.Clear();
306 }
307
308 EntryCell::EntryCell(int32 cell_id, uint32 hash, Addr address, bool small_table)
309 : cell_id_(cell_id),
310 hash_(hash),
311 small_table_(small_table) {
312 DCHECK(IsValidAddress(address) || !address.value());
313
314 cell_.Clear();
315 SetCellState(&cell_, ENTRY_NEW);
316 SetCellGroup(&cell_, ENTRY_NO_USE);
317 if (small_table) {
318 DCHECK(address.FileNumber() == kEntriesFile ||
319 address.FileNumber() == kEvictedEntriesFile);
320 SetCellSmallTableAddress(&cell_, address.start_block());
321 SetCellSmallTableHash(&cell_, hash >> kHashSmallTableShift);
322 } else {
323 SetCellAddress(&cell_, address.ToIndexEntryAddress());
324 SetCellHash(&cell_, hash >> kHashShift);
325 }
326 }
327
328 EntryCell::EntryCell(int32 cell_id,
329 uint32 hash,
330 const IndexCell& cell,
331 bool small_table)
332 : cell_id_(cell_id),
333 hash_(hash),
334 cell_(cell),
335 small_table_(small_table) {
336 }
337
338 void EntryCell::FixSum() {
339 SetCellSum(&cell_, CalculateCellSum(cell_));
340 }
341
342 uint32 EntryCell::GetAddressValue() const {
343 if (small_table_)
344 return GetCellSmallTableAddress(cell_);
345
346 return GetCellAddress(cell_);
347 }
348
349 uint32 EntryCell::RecomputeHash() {
350 if (small_table_) {
351 hash_ &= (1 << kHashSmallTableShift) - 1;
352 hash_ |= GetCellSmallTableHash(cell_) << kHashSmallTableShift;
353 return hash_;
354 }
355
356 hash_ &= (1 << kHashShift) - 1;
357 hash_ |= GetCellHash(cell_) << kHashShift;
358 return hash_;
359 }
360
361 void EntryCell::Serialize(IndexCell* destination) const {
362 *destination = cell_;
363 }
364
365 EntrySet::EntrySet() : evicted_count(0), current(0) {
366 }
367
368 EntrySet::~EntrySet() {
369 }
370
371 IndexIterator::IndexIterator() {
372 }
373
374 IndexIterator::~IndexIterator() {
375 }
376
377 IndexTableInitData::IndexTableInitData() {
378 }
379
380 IndexTableInitData::~IndexTableInitData() {
381 }
382
383 // -----------------------------------------------------------------------
384
385 IndexTable::IndexTable(IndexTableBackend* backend)
386 : backend_(backend),
387 header_(NULL),
388 main_table_(NULL),
389 extra_table_(NULL),
390 modified_(false),
391 small_table_(false) {
392 }
393
394 // There are two cases when increasing the size:
395 // - Doubling the size of the main table
396 // - Adding more entries to the extra table
397 //
398 // For example, consider a 64k main table with 8k cells on the extra table (for
399 // a total of 72k cells). Init can be called to add another 8k cells at the end
400 // (grow to 80k cells). When the size of the extra table approaches 64k, Init
401 // can be called to double the main table (to 128k) and go back to a small extra
402 // table.
403 void IndexTable::Init(IndexTableInitData* params) {
404 bool growing = header_ != NULL;
405 scoped_ptr<IndexBucket[]> old_extra_table;
406 header_ = &params->index_bitmap->header;
407
408 if (params->main_table) {
409 if (main_table_) {
410 // This is doubling the size of main table.
411 DCHECK_EQ(base::bits::Log2Floor(header_->table_len),
412 base::bits::Log2Floor(backup_header_->table_len) + 1);
413 int extra_size = (header()->max_bucket - mask_) * kCellsPerBucket;
414 DCHECK_GE(extra_size, 0);
415 old_extra_table.reset(new IndexBucket[extra_size]);
416 memcpy(old_extra_table.get(), extra_table_,
417 extra_size * sizeof(IndexBucket));
418 memset(params->extra_table, 0, extra_size * sizeof(IndexBucket));
419 }
420 main_table_ = params->main_table;
421 }
422 DCHECK(main_table_);
423 extra_table_ = params->extra_table;
Randy Smith (Not in Mondays) 2013/11/07 20:25:15 Could you put a comment in as to why this is safe
rvargas (doing something else) 2013/11/08 04:22:30 Will do, and you're correct.
424
425 extra_bits_ = base::bits::Log2Floor(header_->table_len) -
426 base::bits::Log2Floor(kBaseTableLen);
427 DCHECK_GE(extra_bits_, 0);
428 DCHECK_LE(extra_bits_, 11);
429 mask_ = ((kBaseTableLen / kCellsPerBucket) << extra_bits_) - 1;
430 small_table_ = extra_bits_ < kHashShift - kHashSmallTableShift;
431 if (!small_table_)
432 extra_bits_ -= kHashShift - kHashSmallTableShift;
433
434 int num_words = (header_->table_len + 31) / 32;
Randy Smith (Not in Mondays) 2013/11/07 20:25:15 table_len is in bits? I wouldn't have naturally a
rvargas (doing something else) 2013/11/08 04:22:30 table_len is in cells (aka, buckets * 4). But we n
435
436 if (old_extra_table) {
437 // All the cells from the extra table are moving to the new tables so before
438 // creating the bitmaps, clear the part of the extra table.
439 int main_table_bit_words = ((mask_ >> 1) + 1) * kCellsPerBucket / 32;
440 DCHECK_GT(num_words, main_table_bit_words);
441 memset(params->index_bitmap->bitmap + main_table_bit_words, 0,
442 (num_words - main_table_bit_words) * sizeof(int32));
443
444 DCHECK(growing);
445 int old_num_words = (backup_header_.get()->table_len + 31) / 32;
446 DCHECK_GT(old_num_words, main_table_bit_words);
447 memset(backup_bitmap_storage_.get() + main_table_bit_words, 0,
448 (old_num_words - main_table_bit_words) * sizeof(int32));
449 }
450 bitmap_.reset(new Bitmap(params->index_bitmap->bitmap, header_->table_len,
451 num_words));
452
453 if (growing) {
454 int old_num_words = (backup_header_.get()->table_len + 31) / 32;
455 DCHECK_GE(num_words, old_num_words);
456 scoped_ptr<uint32[]> storage(new uint32[num_words]);
457 memcpy(storage.get(), backup_bitmap_storage_.get(),
458 old_num_words * sizeof(int32));
459 memset(storage.get() + old_num_words, 0,
460 (num_words - old_num_words) * sizeof(int32));
461
462 backup_bitmap_storage_.swap(storage);
463 backup_header_->table_len = header_->table_len;
464 } else {
465 backup_bitmap_storage_.reset(params->backup_bitmap.release());
466 backup_header_.reset(params->backup_header.release());
467 }
468
469 num_words = (backup_header_->table_len + 31) / 32;
470 backup_bitmap_.reset(new Bitmap(backup_bitmap_storage_.get(),
471 backup_header_->table_len, num_words));
472 if (old_extra_table)
473 MoveCells(old_extra_table.get());
474
475 if (small_table_)
476 DCHECK(header_->flags & SMALL_CACHE);
477 }
478
479 void IndexTable::Reset() {
480 header_ = NULL;
481 main_table_ = NULL;
482 extra_table_ = NULL;
483 bitmap_.reset();
484 backup_bitmap_.reset();
485 backup_header_.reset();
486 backup_bitmap_storage_.reset();
487 modified_ = false;
488 }
489
490 EntrySet IndexTable::LookupEntry(uint32 hash) {
491 EntrySet entries;
492 int bucket_id = static_cast<int>(hash & mask_);
493 IndexBucket* bucket = &main_table_[bucket_id];
494 for (;;) {
495 for (int i = 0; i < kCellsPerBucket; i++) {
496 IndexCell* current_cell = &bucket->cells[i];
497 if (!GetAddressValue(*current_cell))
498 continue;
499 if (!SanityCheck(*current_cell)) {
500 NOTREACHED();
501 int cell_id = bucket_id * kCellsPerBucket + i;
502 current_cell->Clear();
503 bitmap_->Set(cell_id, false);
504 backup_bitmap_->Set(cell_id, false);
505 modified_ = true;
506 continue;
507 }
508 int cell_id = bucket_id * kCellsPerBucket + i;
509 if (MisplacedHash(*current_cell, hash)) {
510 HandleMisplacedCell(current_cell, cell_id, hash & mask_);
511 } else if (IsHashMatch(*current_cell, hash)) {
512 EntryCell entry_cell(cell_id, hash, *current_cell, small_table_);
513 CheckState(entry_cell);
514 if (entry_cell.GetState() != ENTRY_DELETED) {
515 entries.cells.push_back(entry_cell);
516 if (entry_cell.GetGroup() == ENTRY_EVICTED)
517 entries.evicted_count++;
518 }
519 }
520 }
521 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
522 &bucket);
523 if (!bucket_id)
524 break;
525 }
526 return entries;
527 }
528
529 EntryCell IndexTable::CreateEntryCell(uint32 hash, Addr address) {
530 DCHECK(IsValidAddress(address));
531 DCHECK(address.ToIndexEntryAddress());
532
533 int bucket_id = static_cast<int>(hash & mask_);
534 int cell_id = 0;
535 IndexBucket* bucket = &main_table_[bucket_id];
536 IndexCell* current_cell = NULL;
537 bool found = false;
538 for (; !found;) {
539 for (int i = 0; i < kCellsPerBucket && !found; i++) {
540 current_cell = &bucket->cells[i];
541 if (!GetAddressValue(*current_cell)) {
542 cell_id = bucket_id * kCellsPerBucket + i;
543 found = true;
544 }
545 }
546 if (found)
547 break;
548 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
549 &bucket);
550 if (!bucket_id)
551 break;
552 }
553
554 if (!found) {
555 bucket_id = NewExtraBucket();
556 if (bucket_id) {
557 cell_id = bucket_id * kCellsPerBucket;
558 bucket->next = cell_id;
559 bucket = &extra_table_[bucket_id - (mask_ + 1)];
560 bucket->hash = hash & mask_;
561 found = true;
562 } else {
563 // address 0 is a reserved value, and the caller interprets it as invalid.
564 address.set_value(0);
565 }
566 }
567
568 EntryCell entry_cell(cell_id, hash, address, small_table_);
569 if (address.file_type() == BLOCK_EVICTED)
570 entry_cell.SetGroup(ENTRY_EVICTED);
571 else
572 entry_cell.SetGroup(ENTRY_NO_USE);
573 Save(&entry_cell);
574
575 if (found) {
576 bitmap_->Set(cell_id, true);
577 backup_bitmap_->Set(cell_id, true);
578 header()->used_cells++;
579 modified_ = true;
580 }
581
582 return entry_cell;
583 }
584
585 EntryCell IndexTable::FindEntryCell(uint32 hash, Addr address) {
586 return FindEntryCellImpl(hash, address, false);
587 }
588
589 int IndexTable::CalculateTimestamp(Time time) {
590 TimeDelta delta = time - Time::FromInternalValue(header_->base_time);
591 return std::max(delta.InMinutes(), 0);
592 }
593
594 void IndexTable::SetSate(uint32 hash, Addr address, EntryState state) {
595 EntryCell cell = FindEntryCellImpl(hash, address, state == ENTRY_FREE);
596 if (!cell.IsValid()) {
597 NOTREACHED();
598 return;
599 }
600
601 EntryState old_state = cell.GetState();
602 if (state == ENTRY_FREE) {
603 DCHECK_EQ(old_state, ENTRY_DELETED);
604 } else if (state == ENTRY_NEW) {
605 DCHECK_EQ(old_state, ENTRY_FREE);
606 } else if (state == ENTRY_OPEN) {
607 DCHECK_EQ(old_state, ENTRY_USED);
608 } else if (state == ENTRY_MODIFIED) {
609 DCHECK_EQ(old_state, ENTRY_OPEN);
610 } else if (state == ENTRY_DELETED) {
611 DCHECK(old_state == ENTRY_NEW || old_state == ENTRY_OPEN ||
612 old_state == ENTRY_MODIFIED);
613 } else if (state == ENTRY_USED) {
614 DCHECK(old_state == ENTRY_NEW || old_state == ENTRY_OPEN ||
615 old_state == ENTRY_MODIFIED);
616 }
617
618 modified_ = true;
619 if (state == ENTRY_DELETED) {
620 bitmap_->Set(cell.cell_id(), false);
621 backup_bitmap_->Set(cell.cell_id(), false);
622 } else if (state == ENTRY_FREE) {
623 cell.Clear();
624 Write(cell);
625 header()->used_cells--;
626 return;
627 }
628 cell.SetState(state);
629
630 Save(&cell);
631 }
632
633 void IndexTable::UpdateTime(uint32 hash, Addr address, base::Time current) {
634 EntryCell cell = FindEntryCell(hash, address);
635 if (!cell.IsValid())
636 return;
637
638 int minutes = CalculateTimestamp(current);
639
640 // Keep about 3 months of headroom.
641 const int kMaxTimestamp = (1 << 20) - 60 * 24 * 90;
642 if (minutes > kMaxTimestamp) {
643 // TODO(rvargas):
644 // Update header->old_time and trigger a timer
645 // Rebaseline timestamps and don't update sums
646 // Start a timer (about 2 backups)
647 // fix all ckecksums and trigger another timer
648 // update header->old_time because rebaseline is done.
649 minutes = std::min(minutes, (1 << 20) - 1);
650 }
651
652 cell.SetTimestamp(minutes);
653 Save(&cell);
654 }
655
656 void IndexTable::Save(EntryCell* cell) {
657 cell->FixSum();
658 Write(*cell);
659 }
660
661 void IndexTable::GetOldest(CellList* no_use, CellList* low_use,
662 CellList* high_use) {
663 header_->num_no_use_entries = 0;
664 header_->num_low_use_entries = 0;
665 header_->num_high_use_entries = 0;
666 header_->num_evicted_entries = 0;
667
668 int no_use_time = kint32max;
669 int low_use_time = kint32max;
670 int high_use_time = kint32max;
671 for (int i = 0; i < static_cast<int32>(mask_ + 1); i++) {
672 int bucket_id = i;
673 IndexBucket* bucket = &main_table_[i];
674 for (;;) {
675 GetOldestFromBucket(bucket, i, no_use, &no_use_time, low_use,
676 &low_use_time, high_use, &high_use_time);
677
678 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
679 &bucket);
680 if (!bucket_id)
681 break;
682 }
683 }
684 header_->num_entries = header_->num_no_use_entries +
685 header_->num_low_use_entries +
686 header_->num_high_use_entries +
687 header_->num_evicted_entries;
688 modified_ = true;
689 }
690
691 bool IndexTable::GetNextCells(IndexIterator* iterator) {
692 int current_time = iterator->timestamp;
693 iterator->cells.clear();
694 iterator->timestamp = iterator->forward ? kint32max : 0;
695
696 for (int i = 0; i < static_cast<int32>(mask_ + 1); i++) {
697 int bucket_id = i;
698 IndexBucket* bucket = &main_table_[i];
699 for (;;) {
700 GetNewestFromBucket(bucket, i, current_time, iterator);
701
702 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
703 &bucket);
704 if (!bucket_id)
705 break;
706 }
707 }
708 return !iterator->cells.empty();
709 }
710
711 void IndexTable::OnBackupTimer() {
712 if (!modified_)
713 return;
714
715 int num_words = (header_->table_len + 31) / 32;
716 int num_bytes = num_words * 4 + static_cast<int>(sizeof(*header_));
717 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(num_bytes));
718 memcpy(buffer->data(), header_, sizeof(*header_));
719 memcpy(buffer->data() + sizeof(*header_), backup_bitmap_storage_.get(),
720 num_words * 4);
721 backend_->SaveIndex(buffer, num_bytes);
722 modified_ = false;
723 }
724
725 // -----------------------------------------------------------------------
726
727 EntryCell IndexTable::FindEntryCellImpl(uint32 hash, Addr address,
728 bool allow_deleted) {
729 int bucket_id = static_cast<int>(hash & mask_);
730 IndexBucket* bucket = &main_table_[bucket_id];
731 for (;;) {
732 for (int i = 0; i < kCellsPerBucket; i++) {
733 IndexCell* current_cell = &bucket->cells[i];
734 if (!GetAddressValue(*current_cell))
735 continue;
736 DCHECK(SanityCheck(*current_cell));
737 if (IsHashMatch(*current_cell, hash)) {
738 // We have a match.
739 int cell_id = bucket_id * kCellsPerBucket + i;
740 EntryCell entry_cell(cell_id, hash, *current_cell, small_table_);
741 if (entry_cell.GetAddress() != address)
742 continue;
743
744 if (!allow_deleted && entry_cell.GetState() == ENTRY_DELETED)
745 continue;
746
747 return entry_cell;
748 }
749 }
750 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
751 &bucket);
752 if (!bucket_id)
753 break;
754 }
755 return EntryCell();
756 }
757
758 void IndexTable::CheckState(const EntryCell& cell) {
759 int current_state = cell.GetState();
760 if (current_state != ENTRY_FIXING) {
761 bool present = ((current_state & 3) != 0); // Look at the last two bits.
762 if (present != bitmap_->Get(cell.cell_id()) ||
763 present != backup_bitmap_->Get(cell.cell_id())) {
764 // There's a mismatch.
765 if (current_state == ENTRY_DELETED) {
766 // We were in the process of deleting this entry. Finish now.
767 backend_->DeleteCell(cell);
768 } else {
769 current_state = ENTRY_FIXING;
770 EntryCell bad_cell(cell);
771 bad_cell.SetState(ENTRY_FIXING);
772 Save(&bad_cell);
773 }
774 }
775 }
776
777 if (current_state == ENTRY_FIXING)
778 backend_->FixCell(cell);
779 }
780
781 void IndexTable::Write(const EntryCell& cell) {
782 IndexBucket* bucket = NULL;
783 int bucket_id = cell.cell_id() / kCellsPerBucket;
784 if (bucket_id < static_cast<int32>(mask_ + 1)) {
785 bucket = &main_table_[bucket_id];
786 } else {
787 DCHECK_LE(bucket_id, header()->max_bucket);
788 bucket = &extra_table_[bucket_id - (mask_ + 1)];
789 }
790
791 int cell_number = cell.cell_id() % kCellsPerBucket;
792 if (GetAddressValue(bucket->cells[cell_number]) && cell.GetAddressValue()) {
793 DCHECK_EQ(cell.GetAddressValue(),
794 GetAddressValue(bucket->cells[cell_number]));
795 }
796 cell.Serialize(&bucket->cells[cell_number]);
797 }
798
799 int IndexTable::NewExtraBucket() {
800 int safe_window = (header()->table_len < kNumExtraBlocks * 2) ?
801 kNumExtraBlocks / 4 : kNumExtraBlocks;
802 if (header()->table_len - header()->max_bucket * kCellsPerBucket <
803 safe_window) {
804 backend_->GrowIndex();
805 }
806
807 if (header()->max_bucket * kCellsPerBucket ==
808 header()->table_len - kCellsPerBucket) {
809 return 0;
810 }
811
812 header()->max_bucket++;
813 return header()->max_bucket;
814 }
815
816 void IndexTable::GetOldestFromBucket(IndexBucket* bucket, int bucket_hash,
817 CellList* no_use, int* no_use_time,
818 CellList* low_use, int* low_use_time,
819 CellList* high_use, int* high_use_time) {
820 for (int i = 0; i < kCellsPerBucket; i++) {
821 IndexCell& current_cell = bucket->cells[i];
822 if (!GetAddressValue(current_cell))
823 continue;
824 DCHECK(SanityCheck(current_cell));
825 if (!IsNormalState(current_cell))
826 continue;
827
828 EntryCell entry_cell(0, GetFullHash(current_cell, bucket_hash),
829 current_cell, small_table_);
830 switch (GetCellGroup(current_cell)) {
831 case ENTRY_NO_USE:
832 UpdateListWithCell(bucket_hash, entry_cell, no_use, no_use_time);
833 header_->num_no_use_entries++;
834 break;
835 case ENTRY_LOW_USE:
836 UpdateListWithCell(bucket_hash, entry_cell, low_use, low_use_time);
837 header_->num_low_use_entries++;
838 break;
839 case ENTRY_HIGH_USE:
840 UpdateListWithCell(bucket_hash, entry_cell, high_use, high_use_time);
841 header_->num_high_use_entries++;
842 break;
843 case ENTRY_EVICTED:
844 header_->num_evicted_entries++;
845 break;
846 default:
847 NOTREACHED();
848 }
849 }
850 }
851
852 void IndexTable::GetNewestFromBucket(IndexBucket* bucket,
853 int bucket_hash,
854 int limit_time,
855 IndexIterator* iterator) {
856 for (int i = 0; i < kCellsPerBucket; i++) {
857 IndexCell& current_cell = bucket->cells[i];
858 if (!GetAddressValue(current_cell))
859 continue;
860 DCHECK(SanityCheck(current_cell));
861 if (!IsNormalState(current_cell))
862 continue;
863
864 int time = GetCellTimestamp(current_cell);
865 switch (GetCellGroup(current_cell)) {
866 case disk_cache::ENTRY_NO_USE:
867 case disk_cache::ENTRY_LOW_USE:
868 case disk_cache::ENTRY_HIGH_USE:
869 if (iterator->forward && time <= limit_time)
870 continue;
871 if (!iterator->forward && time >= limit_time)
872 continue;
873
874 if ((iterator->forward && time < iterator->timestamp) ||
Randy Smith (Not in Mondays) 2013/11/07 20:25:15 This next section of code looks to have a lot of o
rvargas (doing something else) 2013/11/08 04:22:30 I'll look at it again. This was the main part wher
rvargas (doing something else) 2013/11/08 23:58:19 Done.
875 (!iterator->forward && time > iterator->timestamp)) {
876 iterator->timestamp = time;
877 iterator->cells.clear();
878 }
879 if (time == iterator->timestamp) {
880 EntryCell entry_cell(0, GetFullHash(current_cell, bucket_hash),
881 current_cell, small_table_);
882 CellInfo cell_info = {
883 entry_cell.hash(),
884 entry_cell.GetAddress()
885 };
886 iterator->cells.push_back(cell_info);
887 }
888 }
889 }
890 }
891
892 void IndexTable::MoveCells(IndexBucket* old_extra_table) {
893 int max_hash = (mask_ + 1) / 2;
894 int max_bucket = header()->max_bucket;
895 header()->max_bucket = mask_;
896 int used_cells = header()->used_cells;
897
898 // Consider a large cache: a cell stores the upper 18 bits of the hash
899 // (h >> 14). If the table is say 8 times the original size (growing from 4x),
900 // the bit that we are interested in would be the 3rd bit of the stored value,
901 // in other words 'multiplier' >> 1.
902 uint32 new_bit = (1 << extra_bits_) >> 1;
903
904 scoped_ptr<IndexBucket[]> old_main_table;
905 IndexBucket* source_table = main_table_;
906 bool upgrade_format = !extra_bits_;
907 if (upgrade_format) {
908 // This method should deal with migrating a small table to a big one. Given
909 // that the first thing to do is read the old table, set small_table_ for
910 // the size of the old table. Now, when moving a cell, the result cannot be
911 // placed in the old table or we will end up reading it again and attempting
912 // to move it, so we have to copy the whole table at once.
913 DCHECK(!small_table_);
914 small_table_ = true;
915 old_main_table.reset(new IndexBucket[max_hash]);
916 memcpy(old_main_table.get(), main_table_, max_hash * sizeof(IndexBucket));
917 memset(main_table_, 0, max_hash * sizeof(IndexBucket));
918 source_table = old_main_table.get();
919 }
920
921 for (int i = 0; i < max_hash; i++) {
922 int bucket_id = i;
923 IndexBucket* bucket = &source_table[i];
924 for (;;) {
925 for (int j = 0; j < kCellsPerBucket; j++) {
926 IndexCell& current_cell = bucket->cells[j];
927 if (!GetAddressValue(current_cell))
928 continue;
929 DCHECK(SanityCheck(current_cell));
930 if (bucket_id == i) {
931 if (upgrade_format || (GetHashValue(current_cell) & new_bit)) {
932 // Move this cell to the upper half of the table.
933 MoveSingleCell(&current_cell, bucket_id * kCellsPerBucket + j, i,
934 true);
935 }
936 } else {
937 // All cells on extra buckets have to move.
938 MoveSingleCell(&current_cell, bucket_id * kCellsPerBucket + j, i,
939 true);
940 }
941 }
942
943 bucket_id = GetNextBucket(max_hash, max_bucket, old_extra_table, &bucket);
944 if (!bucket_id)
945 break;
946 }
947 }
948
949 DCHECK_EQ(header()->used_cells, used_cells);
950
951 if (upgrade_format) {
952 small_table_ = false;
953 header()->flags &= ~SMALL_CACHE;
954 }
955 }
956
957 void IndexTable::MoveSingleCell(IndexCell* current_cell, int cell_id,
958 int main_table_index, bool growing) {
959 uint32 hash = GetFullHash(*current_cell, main_table_index);
960 EntryCell old_cell(cell_id, hash, *current_cell, small_table_);
961
962 bool upgrade_format = !extra_bits_ && growing;
963 if (upgrade_format)
964 small_table_ = false;
965 EntryCell new_cell = CreateEntryCell(hash, old_cell.GetAddress());
966
967 if (!new_cell.IsValid()) {
968 // We'll deal with this entry later.
969 if (upgrade_format)
970 small_table_ = true;
971 return;
972 }
973
974 new_cell.SetState(old_cell.GetState());
975 new_cell.SetGroup(old_cell.GetGroup());
976 new_cell.SetReuse(old_cell.GetReuse());
977 new_cell.SetTimestamp(old_cell.GetTimestamp());
978 Save(&new_cell);
979 modified_ = true;
980 if (upgrade_format)
981 small_table_ = true;
982
983 if (old_cell.GetState() == ENTRY_DELETED) {
984 bitmap_->Set(new_cell.cell_id(), false);
985 backup_bitmap_->Set(new_cell.cell_id(), false);
986 }
987
988 if (!growing || cell_id / kCellsPerBucket == main_table_index) {
989 // Only delete entries that live on the main table.
990 if (!upgrade_format) {
991 old_cell.Clear();
992 Write(old_cell);
993 }
994
995 if (cell_id != new_cell.cell_id()) {
996 bitmap_->Set(old_cell.cell_id(), false);
997 backup_bitmap_->Set(old_cell.cell_id(), false);
998 }
999 }
1000 header()->used_cells--;
1001 }
1002
1003 void IndexTable::HandleMisplacedCell(IndexCell* current_cell, int cell_id,
1004 int main_table_index) {
1005 // The cell may be misplaced, or a duplicate cell exists with this data.
1006 uint32 hash = GetFullHash(*current_cell, main_table_index);
1007 MoveSingleCell(current_cell, cell_id, main_table_index, false);
1008
1009 // Now look for a duplicate cell.
1010 CheckBucketList(hash & mask_);
1011 }
1012
1013 void IndexTable::CheckBucketList(int bucket_id) {
1014 typedef std::pair<int, EntryGroup> AddressAndGroup;
1015 std::set<AddressAndGroup> entries;
1016 IndexBucket* bucket = &main_table_[bucket_id];
1017 int bucket_hash = bucket_id;
1018 for (;;) {
1019 for (int i = 0; i < kCellsPerBucket; i++) {
1020 IndexCell* current_cell = &bucket->cells[i];
1021 if (!GetAddressValue(*current_cell))
1022 continue;
1023 if (!SanityCheck(*current_cell)) {
1024 NOTREACHED();
1025 current_cell->Clear();
1026 continue;
1027 }
1028 int cell_id = bucket_id * kCellsPerBucket + i;
1029 EntryCell cell(cell_id, GetFullHash(*current_cell, bucket_hash),
1030 *current_cell, small_table_);
1031 if (!entries.insert(std::make_pair(cell.GetAddress().value(),
1032 cell.GetGroup())).second) {
1033 current_cell->Clear();
1034 continue;
1035 }
1036 CheckState(cell);
1037 }
1038
1039 bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
1040 &bucket);
1041 if (!bucket_id)
1042 break;
1043 }
1044 }
1045
1046 uint32 IndexTable::GetAddressValue(const IndexCell& cell) {
1047 if (small_table_)
1048 return GetCellSmallTableAddress(cell);
1049
1050 return GetCellAddress(cell);
1051 }
1052
1053 uint32 IndexTable::GetHashValue(const IndexCell& cell) {
1054 if (small_table_)
1055 return GetCellSmallTableHash(cell);
1056
1057 return GetCellHash(cell);
1058 }
1059
1060 uint32 IndexTable::GetFullHash(const IndexCell& cell, uint32 lower_part) {
1061 // It is OK for the high order bits of lower_part to overlap with the stored
1062 // part of the hash.
1063 if (small_table_)
1064 return (GetCellSmallTableHash(cell) << kHashSmallTableShift) | lower_part;
1065
1066 return (GetCellHash(cell) << kHashShift) | lower_part;
1067 }
1068
1069 // All the bits stored in the cell should match the provided hash.
1070 bool IndexTable::IsHashMatch(const IndexCell& cell, uint32 hash) {
1071 hash = small_table_ ? hash >> kHashSmallTableShift : hash >> kHashShift;
1072 return GetHashValue(cell) == hash;
1073 }
1074
1075 bool IndexTable::MisplacedHash(const IndexCell& cell, uint32 hash) {
1076 if (!extra_bits_)
1077 return false;
1078
1079 uint32 mask = (1 << extra_bits_) - 1;
1080 hash = small_table_ ? hash >> kHashSmallTableShift : hash >> kHashShift;
1081 return (GetHashValue(cell) & mask) != (hash & mask);
1082 }
1083
1084 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698