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

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

Issue 121643003: Reorganize net/disk_cache into backend specific directories. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase to upstream Created 6 years, 10 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/disk_cache/rankings.h"
6
7 #include "base/metrics/histogram.h"
8 #include "net/disk_cache/backend_impl.h"
9 #include "net/disk_cache/disk_format.h"
10 #include "net/disk_cache/entry_impl.h"
11 #include "net/disk_cache/errors.h"
12 #include "net/disk_cache/stress_support.h"
13
14 #define CACHE_HISTOGRAM_MACROS_BACKEND_IMPL_OBJ backend_
15 #include "net/disk_cache/histogram_macros.h"
16
17 using base::Time;
18 using base::TimeTicks;
19
20 namespace disk_cache {
21 // This is used by crash_cache.exe to generate unit test files.
22 NET_EXPORT_PRIVATE RankCrashes g_rankings_crash = NO_CRASH;
23 }
24
25 namespace {
26
27 enum Operation {
28 INSERT = 1,
29 REMOVE
30 };
31
32 // This class provides a simple lock for the LRU list of rankings. Whenever an
33 // entry is to be inserted or removed from the list, a transaction object should
34 // be created to keep track of the operation. If the process crashes before
35 // finishing the operation, the transaction record (stored as part of the user
36 // data on the file header) can be used to finish the operation.
37 class Transaction {
38 public:
39 // addr is the cache addres of the node being inserted or removed. We want to
40 // avoid having the compiler doing optimizations on when to read or write
41 // from user_data because it is the basis of the crash detection. Maybe
42 // volatile is not enough for that, but it should be a good hint.
43 Transaction(volatile disk_cache::LruData* data, disk_cache::Addr addr,
44 Operation op, int list);
45 ~Transaction();
46 private:
47 volatile disk_cache::LruData* data_;
48 DISALLOW_COPY_AND_ASSIGN(Transaction);
49 };
50
51 Transaction::Transaction(volatile disk_cache::LruData* data,
52 disk_cache::Addr addr, Operation op, int list)
53 : data_(data) {
54 DCHECK(!data_->transaction);
55 DCHECK(addr.is_initialized());
56 data_->operation = op;
57 data_->operation_list = list;
58 data_->transaction = addr.value();
59 }
60
61 Transaction::~Transaction() {
62 DCHECK(data_->transaction);
63 data_->transaction = 0;
64 data_->operation = 0;
65 data_->operation_list = 0;
66 }
67
68 // Code locations that can generate crashes.
69 enum CrashLocation {
70 ON_INSERT_1, ON_INSERT_2, ON_INSERT_3, ON_INSERT_4, ON_REMOVE_1, ON_REMOVE_2,
71 ON_REMOVE_3, ON_REMOVE_4, ON_REMOVE_5, ON_REMOVE_6, ON_REMOVE_7, ON_REMOVE_8
72 };
73
74 #ifndef NDEBUG
75 void TerminateSelf() {
76 #if defined(OS_WIN)
77 // Windows does more work on _exit() than we would like, so we force exit.
78 TerminateProcess(GetCurrentProcess(), 0);
79 #elif defined(OS_POSIX)
80 // On POSIX, _exit() will terminate the process with minimal cleanup,
81 // and it is cleaner than killing.
82 _exit(0);
83 #endif
84 }
85 #endif // NDEBUG
86
87 // Generates a crash on debug builds, acording to the value of g_rankings_crash.
88 // This used by crash_cache.exe to generate unit-test files.
89 void GenerateCrash(CrashLocation location) {
90 #ifndef NDEBUG
91 if (disk_cache::NO_CRASH == disk_cache::g_rankings_crash)
92 return;
93 switch (location) {
94 case ON_INSERT_1:
95 switch (disk_cache::g_rankings_crash) {
96 case disk_cache::INSERT_ONE_1:
97 case disk_cache::INSERT_LOAD_1:
98 TerminateSelf();
99 default:
100 break;
101 }
102 break;
103 case ON_INSERT_2:
104 if (disk_cache::INSERT_EMPTY_1 == disk_cache::g_rankings_crash)
105 TerminateSelf();
106 break;
107 case ON_INSERT_3:
108 switch (disk_cache::g_rankings_crash) {
109 case disk_cache::INSERT_EMPTY_2:
110 case disk_cache::INSERT_ONE_2:
111 case disk_cache::INSERT_LOAD_2:
112 TerminateSelf();
113 default:
114 break;
115 }
116 break;
117 case ON_INSERT_4:
118 switch (disk_cache::g_rankings_crash) {
119 case disk_cache::INSERT_EMPTY_3:
120 case disk_cache::INSERT_ONE_3:
121 TerminateSelf();
122 default:
123 break;
124 }
125 break;
126 case ON_REMOVE_1:
127 switch (disk_cache::g_rankings_crash) {
128 case disk_cache::REMOVE_ONE_1:
129 case disk_cache::REMOVE_HEAD_1:
130 case disk_cache::REMOVE_TAIL_1:
131 case disk_cache::REMOVE_LOAD_1:
132 TerminateSelf();
133 default:
134 break;
135 }
136 break;
137 case ON_REMOVE_2:
138 if (disk_cache::REMOVE_ONE_2 == disk_cache::g_rankings_crash)
139 TerminateSelf();
140 break;
141 case ON_REMOVE_3:
142 if (disk_cache::REMOVE_ONE_3 == disk_cache::g_rankings_crash)
143 TerminateSelf();
144 break;
145 case ON_REMOVE_4:
146 if (disk_cache::REMOVE_HEAD_2 == disk_cache::g_rankings_crash)
147 TerminateSelf();
148 break;
149 case ON_REMOVE_5:
150 if (disk_cache::REMOVE_TAIL_2 == disk_cache::g_rankings_crash)
151 TerminateSelf();
152 break;
153 case ON_REMOVE_6:
154 if (disk_cache::REMOVE_TAIL_3 == disk_cache::g_rankings_crash)
155 TerminateSelf();
156 break;
157 case ON_REMOVE_7:
158 switch (disk_cache::g_rankings_crash) {
159 case disk_cache::REMOVE_ONE_4:
160 case disk_cache::REMOVE_LOAD_2:
161 case disk_cache::REMOVE_HEAD_3:
162 TerminateSelf();
163 default:
164 break;
165 }
166 break;
167 case ON_REMOVE_8:
168 switch (disk_cache::g_rankings_crash) {
169 case disk_cache::REMOVE_HEAD_4:
170 case disk_cache::REMOVE_LOAD_3:
171 TerminateSelf();
172 default:
173 break;
174 }
175 break;
176 default:
177 NOTREACHED();
178 return;
179 }
180 #endif // NDEBUG
181 }
182
183 // Update the timestamp fields of |node|.
184 void UpdateTimes(disk_cache::CacheRankingsBlock* node, bool modified) {
185 base::Time now = base::Time::Now();
186 node->Data()->last_used = now.ToInternalValue();
187 if (modified)
188 node->Data()->last_modified = now.ToInternalValue();
189 }
190
191 } // namespace
192
193 namespace disk_cache {
194
195 Rankings::ScopedRankingsBlock::ScopedRankingsBlock() : rankings_(NULL) {}
196
197 Rankings::ScopedRankingsBlock::ScopedRankingsBlock(Rankings* rankings)
198 : rankings_(rankings) {}
199
200 Rankings::ScopedRankingsBlock::ScopedRankingsBlock(
201 Rankings* rankings, CacheRankingsBlock* node)
202 : scoped_ptr<CacheRankingsBlock>(node), rankings_(rankings) {}
203
204 Rankings::Iterator::Iterator(Rankings* rankings) {
205 memset(this, 0, sizeof(Iterator));
206 my_rankings = rankings;
207 }
208
209 Rankings::Iterator::~Iterator() {
210 for (int i = 0; i < 3; i++)
211 ScopedRankingsBlock(my_rankings, nodes[i]);
212 }
213
214 Rankings::Rankings() : init_(false) {}
215
216 Rankings::~Rankings() {}
217
218 bool Rankings::Init(BackendImpl* backend, bool count_lists) {
219 DCHECK(!init_);
220 if (init_)
221 return false;
222
223 backend_ = backend;
224 control_data_ = backend_->GetLruData();
225 count_lists_ = count_lists;
226
227 ReadHeads();
228 ReadTails();
229
230 if (control_data_->transaction)
231 CompleteTransaction();
232
233 init_ = true;
234 return true;
235 }
236
237 void Rankings::Reset() {
238 init_ = false;
239 for (int i = 0; i < LAST_ELEMENT; i++) {
240 heads_[i].set_value(0);
241 tails_[i].set_value(0);
242 }
243 control_data_ = NULL;
244 }
245
246 void Rankings::Insert(CacheRankingsBlock* node, bool modified, List list) {
247 Trace("Insert 0x%x l %d", node->address().value(), list);
248 DCHECK(node->HasData());
249 Addr& my_head = heads_[list];
250 Addr& my_tail = tails_[list];
251 Transaction lock(control_data_, node->address(), INSERT, list);
252 CacheRankingsBlock head(backend_->File(my_head), my_head);
253 if (my_head.is_initialized()) {
254 if (!GetRanking(&head))
255 return;
256
257 if (head.Data()->prev != my_head.value() && // Normal path.
258 head.Data()->prev != node->address().value()) { // FinishInsert().
259 backend_->CriticalError(ERR_INVALID_LINKS);
260 return;
261 }
262
263 head.Data()->prev = node->address().value();
264 head.Store();
265 GenerateCrash(ON_INSERT_1);
266 UpdateIterators(&head);
267 }
268
269 node->Data()->next = my_head.value();
270 node->Data()->prev = node->address().value();
271 my_head.set_value(node->address().value());
272
273 if (!my_tail.is_initialized() || my_tail.value() == node->address().value()) {
274 my_tail.set_value(node->address().value());
275 node->Data()->next = my_tail.value();
276 WriteTail(list);
277 GenerateCrash(ON_INSERT_2);
278 }
279
280 UpdateTimes(node, modified);
281 node->Store();
282 GenerateCrash(ON_INSERT_3);
283
284 // The last thing to do is move our head to point to a node already stored.
285 WriteHead(list);
286 IncrementCounter(list);
287 GenerateCrash(ON_INSERT_4);
288 backend_->FlushIndex();
289 }
290
291 // If a, b and r are elements on the list, and we want to remove r, the possible
292 // states for the objects if a crash happens are (where y(x, z) means for object
293 // y, prev is x and next is z):
294 // A. One element:
295 // 1. r(r, r), head(r), tail(r) initial state
296 // 2. r(r, r), head(0), tail(r) WriteHead()
297 // 3. r(r, r), head(0), tail(0) WriteTail()
298 // 4. r(0, 0), head(0), tail(0) next.Store()
299 //
300 // B. Remove a random element:
301 // 1. a(x, r), r(a, b), b(r, y), head(x), tail(y) initial state
302 // 2. a(x, r), r(a, b), b(a, y), head(x), tail(y) next.Store()
303 // 3. a(x, b), r(a, b), b(a, y), head(x), tail(y) prev.Store()
304 // 4. a(x, b), r(0, 0), b(a, y), head(x), tail(y) node.Store()
305 //
306 // C. Remove head:
307 // 1. r(r, b), b(r, y), head(r), tail(y) initial state
308 // 2. r(r, b), b(r, y), head(b), tail(y) WriteHead()
309 // 3. r(r, b), b(b, y), head(b), tail(y) next.Store()
310 // 4. r(0, 0), b(b, y), head(b), tail(y) prev.Store()
311 //
312 // D. Remove tail:
313 // 1. a(x, r), r(a, r), head(x), tail(r) initial state
314 // 2. a(x, r), r(a, r), head(x), tail(a) WriteTail()
315 // 3. a(x, a), r(a, r), head(x), tail(a) prev.Store()
316 // 4. a(x, a), r(0, 0), head(x), tail(a) next.Store()
317 void Rankings::Remove(CacheRankingsBlock* node, List list, bool strict) {
318 Trace("Remove 0x%x (0x%x 0x%x) l %d", node->address().value(),
319 node->Data()->next, node->Data()->prev, list);
320 DCHECK(node->HasData());
321 if (strict)
322 InvalidateIterators(node);
323
324 Addr next_addr(node->Data()->next);
325 Addr prev_addr(node->Data()->prev);
326 if (!next_addr.is_initialized() || next_addr.is_separate_file() ||
327 !prev_addr.is_initialized() || prev_addr.is_separate_file()) {
328 if (next_addr.is_initialized() || prev_addr.is_initialized()) {
329 LOG(ERROR) << "Invalid rankings info.";
330 STRESS_NOTREACHED();
331 }
332 return;
333 }
334
335 CacheRankingsBlock next(backend_->File(next_addr), next_addr);
336 CacheRankingsBlock prev(backend_->File(prev_addr), prev_addr);
337 if (!GetRanking(&next) || !GetRanking(&prev)) {
338 STRESS_NOTREACHED();
339 return;
340 }
341
342 if (!CheckLinks(node, &prev, &next, &list))
343 return;
344
345 Transaction lock(control_data_, node->address(), REMOVE, list);
346 prev.Data()->next = next.address().value();
347 next.Data()->prev = prev.address().value();
348 GenerateCrash(ON_REMOVE_1);
349
350 CacheAddr node_value = node->address().value();
351 Addr& my_head = heads_[list];
352 Addr& my_tail = tails_[list];
353 if (node_value == my_head.value() || node_value == my_tail.value()) {
354 if (my_head.value() == my_tail.value()) {
355 my_head.set_value(0);
356 my_tail.set_value(0);
357
358 WriteHead(list);
359 GenerateCrash(ON_REMOVE_2);
360 WriteTail(list);
361 GenerateCrash(ON_REMOVE_3);
362 } else if (node_value == my_head.value()) {
363 my_head.set_value(next.address().value());
364 next.Data()->prev = next.address().value();
365
366 WriteHead(list);
367 GenerateCrash(ON_REMOVE_4);
368 } else if (node_value == my_tail.value()) {
369 my_tail.set_value(prev.address().value());
370 prev.Data()->next = prev.address().value();
371
372 WriteTail(list);
373 GenerateCrash(ON_REMOVE_5);
374
375 // Store the new tail to make sure we can undo the operation if we crash.
376 prev.Store();
377 GenerateCrash(ON_REMOVE_6);
378 }
379 }
380
381 // Nodes out of the list can be identified by invalid pointers.
382 node->Data()->next = 0;
383 node->Data()->prev = 0;
384
385 // The last thing to get to disk is the node itself, so before that there is
386 // enough info to recover.
387 next.Store();
388 GenerateCrash(ON_REMOVE_7);
389 prev.Store();
390 GenerateCrash(ON_REMOVE_8);
391 node->Store();
392 DecrementCounter(list);
393 UpdateIterators(&next);
394 UpdateIterators(&prev);
395 backend_->FlushIndex();
396 }
397
398 // A crash in between Remove and Insert will lead to a dirty entry not on the
399 // list. We want to avoid that case as much as we can (as while waiting for IO),
400 // but the net effect is just an assert on debug when attempting to remove the
401 // entry. Otherwise we'll need reentrant transactions, which is an overkill.
402 void Rankings::UpdateRank(CacheRankingsBlock* node, bool modified, List list) {
403 Addr& my_head = heads_[list];
404 if (my_head.value() == node->address().value()) {
405 UpdateTimes(node, modified);
406 node->set_modified();
407 return;
408 }
409
410 TimeTicks start = TimeTicks::Now();
411 Remove(node, list, true);
412 Insert(node, modified, list);
413 CACHE_UMA(AGE_MS, "UpdateRank", 0, start);
414 }
415
416 CacheRankingsBlock* Rankings::GetNext(CacheRankingsBlock* node, List list) {
417 ScopedRankingsBlock next(this);
418 if (!node) {
419 Addr& my_head = heads_[list];
420 if (!my_head.is_initialized())
421 return NULL;
422 next.reset(new CacheRankingsBlock(backend_->File(my_head), my_head));
423 } else {
424 if (!node->HasData())
425 node->Load();
426 Addr& my_tail = tails_[list];
427 if (!my_tail.is_initialized())
428 return NULL;
429 if (my_tail.value() == node->address().value())
430 return NULL;
431 Addr address(node->Data()->next);
432 if (address.value() == node->address().value())
433 return NULL; // Another tail? fail it.
434 next.reset(new CacheRankingsBlock(backend_->File(address), address));
435 }
436
437 TrackRankingsBlock(next.get(), true);
438
439 if (!GetRanking(next.get()))
440 return NULL;
441
442 ConvertToLongLived(next.get());
443 if (node && !CheckSingleLink(node, next.get()))
444 return NULL;
445
446 return next.release();
447 }
448
449 CacheRankingsBlock* Rankings::GetPrev(CacheRankingsBlock* node, List list) {
450 ScopedRankingsBlock prev(this);
451 if (!node) {
452 Addr& my_tail = tails_[list];
453 if (!my_tail.is_initialized())
454 return NULL;
455 prev.reset(new CacheRankingsBlock(backend_->File(my_tail), my_tail));
456 } else {
457 if (!node->HasData())
458 node->Load();
459 Addr& my_head = heads_[list];
460 if (!my_head.is_initialized())
461 return NULL;
462 if (my_head.value() == node->address().value())
463 return NULL;
464 Addr address(node->Data()->prev);
465 if (address.value() == node->address().value())
466 return NULL; // Another head? fail it.
467 prev.reset(new CacheRankingsBlock(backend_->File(address), address));
468 }
469
470 TrackRankingsBlock(prev.get(), true);
471
472 if (!GetRanking(prev.get()))
473 return NULL;
474
475 ConvertToLongLived(prev.get());
476 if (node && !CheckSingleLink(prev.get(), node))
477 return NULL;
478
479 return prev.release();
480 }
481
482 void Rankings::FreeRankingsBlock(CacheRankingsBlock* node) {
483 TrackRankingsBlock(node, false);
484 }
485
486 void Rankings::TrackRankingsBlock(CacheRankingsBlock* node,
487 bool start_tracking) {
488 if (!node)
489 return;
490
491 IteratorPair current(node->address().value(), node);
492
493 if (start_tracking)
494 iterators_.push_back(current);
495 else
496 iterators_.remove(current);
497 }
498
499 int Rankings::SelfCheck() {
500 int total = 0;
501 int error = 0;
502 for (int i = 0; i < LAST_ELEMENT; i++) {
503 int partial = CheckList(static_cast<List>(i));
504 if (partial < 0 && !error)
505 error = partial;
506 else if (partial > 0)
507 total += partial;
508 }
509
510 return error ? error : total;
511 }
512
513 bool Rankings::SanityCheck(CacheRankingsBlock* node, bool from_list) const {
514 if (!node->VerifyHash())
515 return false;
516
517 const RankingsNode* data = node->Data();
518
519 if ((!data->next && data->prev) || (data->next && !data->prev))
520 return false;
521
522 // Both pointers on zero is a node out of the list.
523 if (!data->next && !data->prev && from_list)
524 return false;
525
526 List list = NO_USE; // Initialize it to something.
527 if ((node->address().value() == data->prev) && !IsHead(data->prev, &list))
528 return false;
529
530 if ((node->address().value() == data->next) && !IsTail(data->next, &list))
531 return false;
532
533 if (!data->next && !data->prev)
534 return true;
535
536 Addr next_addr(data->next);
537 Addr prev_addr(data->prev);
538 if (!next_addr.SanityCheckV2() || next_addr.file_type() != RANKINGS ||
539 !prev_addr.SanityCheckV2() || prev_addr.file_type() != RANKINGS)
540 return false;
541
542 return true;
543 }
544
545 bool Rankings::DataSanityCheck(CacheRankingsBlock* node, bool from_list) const {
546 const RankingsNode* data = node->Data();
547 if (!data->contents)
548 return false;
549
550 // It may have never been inserted.
551 if (from_list && (!data->last_used || !data->last_modified))
552 return false;
553
554 return true;
555 }
556
557 void Rankings::SetContents(CacheRankingsBlock* node, CacheAddr address) {
558 node->Data()->contents = address;
559 node->Store();
560 }
561
562 void Rankings::ReadHeads() {
563 for (int i = 0; i < LAST_ELEMENT; i++)
564 heads_[i] = Addr(control_data_->heads[i]);
565 }
566
567 void Rankings::ReadTails() {
568 for (int i = 0; i < LAST_ELEMENT; i++)
569 tails_[i] = Addr(control_data_->tails[i]);
570 }
571
572 void Rankings::WriteHead(List list) {
573 control_data_->heads[list] = heads_[list].value();
574 }
575
576 void Rankings::WriteTail(List list) {
577 control_data_->tails[list] = tails_[list].value();
578 }
579
580 bool Rankings::GetRanking(CacheRankingsBlock* rankings) {
581 if (!rankings->address().is_initialized())
582 return false;
583
584 TimeTicks start = TimeTicks::Now();
585 if (!rankings->Load())
586 return false;
587
588 if (!SanityCheck(rankings, true)) {
589 backend_->CriticalError(ERR_INVALID_LINKS);
590 return false;
591 }
592
593 backend_->OnEvent(Stats::OPEN_RANKINGS);
594
595 // Note that if the cache is in read_only mode, open entries are not marked
596 // as dirty, except when an entry is doomed. We have to look for open entries.
597 if (!backend_->read_only() && !rankings->Data()->dirty)
598 return true;
599
600 EntryImpl* entry = backend_->GetOpenEntry(rankings);
601 if (!entry) {
602 if (backend_->read_only())
603 return true;
604
605 // We cannot trust this entry, but we cannot initiate a cleanup from this
606 // point (we may be in the middle of a cleanup already). The entry will be
607 // deleted when detected from a regular open/create path.
608 rankings->Data()->dirty = backend_->GetCurrentEntryId() - 1;
609 if (!rankings->Data()->dirty)
610 rankings->Data()->dirty--;
611 return true;
612 }
613
614 // Note that we should not leave this module without deleting rankings first.
615 rankings->SetData(entry->rankings()->Data());
616
617 CACHE_UMA(AGE_MS, "GetRankings", 0, start);
618 return true;
619 }
620
621 void Rankings::ConvertToLongLived(CacheRankingsBlock* rankings) {
622 if (rankings->own_data())
623 return;
624
625 // We cannot return a shared node because we are not keeping a reference
626 // to the entry that owns the buffer. Make this node a copy of the one that
627 // we have, and let the iterator logic update it when the entry changes.
628 CacheRankingsBlock temp(NULL, Addr(0));
629 *temp.Data() = *rankings->Data();
630 rankings->StopSharingData();
631 *rankings->Data() = *temp.Data();
632 }
633
634 void Rankings::CompleteTransaction() {
635 Addr node_addr(static_cast<CacheAddr>(control_data_->transaction));
636 if (!node_addr.is_initialized() || node_addr.is_separate_file()) {
637 NOTREACHED();
638 LOG(ERROR) << "Invalid rankings info.";
639 return;
640 }
641
642 Trace("CompleteTransaction 0x%x", node_addr.value());
643
644 CacheRankingsBlock node(backend_->File(node_addr), node_addr);
645 if (!node.Load())
646 return;
647
648 node.Store();
649
650 Addr& my_head = heads_[control_data_->operation_list];
651 Addr& my_tail = tails_[control_data_->operation_list];
652
653 // We want to leave the node inside the list. The entry must me marked as
654 // dirty, and will be removed later. Otherwise, we'll get assertions when
655 // attempting to remove the dirty entry.
656 if (INSERT == control_data_->operation) {
657 Trace("FinishInsert h:0x%x t:0x%x", my_head.value(), my_tail.value());
658 FinishInsert(&node);
659 } else if (REMOVE == control_data_->operation) {
660 Trace("RevertRemove h:0x%x t:0x%x", my_head.value(), my_tail.value());
661 RevertRemove(&node);
662 } else {
663 NOTREACHED();
664 LOG(ERROR) << "Invalid operation to recover.";
665 }
666 }
667
668 void Rankings::FinishInsert(CacheRankingsBlock* node) {
669 control_data_->transaction = 0;
670 control_data_->operation = 0;
671 Addr& my_head = heads_[control_data_->operation_list];
672 Addr& my_tail = tails_[control_data_->operation_list];
673 if (my_head.value() != node->address().value()) {
674 if (my_tail.value() == node->address().value()) {
675 // This part will be skipped by the logic of Insert.
676 node->Data()->next = my_tail.value();
677 }
678
679 Insert(node, true, static_cast<List>(control_data_->operation_list));
680 }
681
682 // Tell the backend about this entry.
683 backend_->RecoveredEntry(node);
684 }
685
686 void Rankings::RevertRemove(CacheRankingsBlock* node) {
687 Addr next_addr(node->Data()->next);
688 Addr prev_addr(node->Data()->prev);
689 if (!next_addr.is_initialized() || !prev_addr.is_initialized()) {
690 // The operation actually finished. Nothing to do.
691 control_data_->transaction = 0;
692 return;
693 }
694 if (next_addr.is_separate_file() || prev_addr.is_separate_file()) {
695 NOTREACHED();
696 LOG(WARNING) << "Invalid rankings info.";
697 control_data_->transaction = 0;
698 return;
699 }
700
701 CacheRankingsBlock next(backend_->File(next_addr), next_addr);
702 CacheRankingsBlock prev(backend_->File(prev_addr), prev_addr);
703 if (!next.Load() || !prev.Load())
704 return;
705
706 CacheAddr node_value = node->address().value();
707 DCHECK(prev.Data()->next == node_value ||
708 prev.Data()->next == prev_addr.value() ||
709 prev.Data()->next == next.address().value());
710 DCHECK(next.Data()->prev == node_value ||
711 next.Data()->prev == next_addr.value() ||
712 next.Data()->prev == prev.address().value());
713
714 if (node_value != prev_addr.value())
715 prev.Data()->next = node_value;
716 if (node_value != next_addr.value())
717 next.Data()->prev = node_value;
718
719 List my_list = static_cast<List>(control_data_->operation_list);
720 Addr& my_head = heads_[my_list];
721 Addr& my_tail = tails_[my_list];
722 if (!my_head.is_initialized() || !my_tail.is_initialized()) {
723 my_head.set_value(node_value);
724 my_tail.set_value(node_value);
725 WriteHead(my_list);
726 WriteTail(my_list);
727 } else if (my_head.value() == next.address().value()) {
728 my_head.set_value(node_value);
729 prev.Data()->next = next.address().value();
730 WriteHead(my_list);
731 } else if (my_tail.value() == prev.address().value()) {
732 my_tail.set_value(node_value);
733 next.Data()->prev = prev.address().value();
734 WriteTail(my_list);
735 }
736
737 next.Store();
738 prev.Store();
739 control_data_->transaction = 0;
740 control_data_->operation = 0;
741 backend_->FlushIndex();
742 }
743
744 bool Rankings::CheckLinks(CacheRankingsBlock* node, CacheRankingsBlock* prev,
745 CacheRankingsBlock* next, List* list) {
746 CacheAddr node_addr = node->address().value();
747 if (prev->Data()->next == node_addr &&
748 next->Data()->prev == node_addr) {
749 // A regular linked node.
750 return true;
751 }
752
753 Trace("CheckLinks 0x%x (0x%x 0x%x)", node_addr,
754 prev->Data()->next, next->Data()->prev);
755
756 if (node_addr != prev->address().value() &&
757 node_addr != next->address().value() &&
758 prev->Data()->next == next->address().value() &&
759 next->Data()->prev == prev->address().value()) {
760 // The list is actually ok, node is wrong.
761 Trace("node 0x%x out of list %d", node_addr, list);
762 node->Data()->next = 0;
763 node->Data()->prev = 0;
764 node->Store();
765 return false;
766 }
767
768 if (prev->Data()->next == node_addr ||
769 next->Data()->prev == node_addr) {
770 // Only one link is weird, lets double check.
771 if (prev->Data()->next != node_addr && IsHead(node_addr, list))
772 return true;
773
774 if (next->Data()->prev != node_addr && IsTail(node_addr, list))
775 return true;
776 }
777
778 LOG(ERROR) << "Inconsistent LRU.";
779 STRESS_NOTREACHED();
780
781 backend_->CriticalError(ERR_INVALID_LINKS);
782 return false;
783 }
784
785 bool Rankings::CheckSingleLink(CacheRankingsBlock* prev,
786 CacheRankingsBlock* next) {
787 if (prev->Data()->next != next->address().value() ||
788 next->Data()->prev != prev->address().value()) {
789 LOG(ERROR) << "Inconsistent LRU.";
790
791 backend_->CriticalError(ERR_INVALID_LINKS);
792 return false;
793 }
794
795 return true;
796 }
797
798 int Rankings::CheckList(List list) {
799 Addr last1, last2;
800 int head_items;
801 int rv = CheckListSection(list, last1, last2, true, // Head to tail.
802 &last1, &last2, &head_items);
803 if (rv == ERR_NO_ERROR)
804 return head_items;
805
806 return rv;
807 }
808
809 // Note that the returned error codes assume a forward walk (from head to tail)
810 // so they have to be adjusted accordingly by the caller. We use two stop values
811 // to be able to detect a corrupt node at the end that is not linked going back.
812 int Rankings::CheckListSection(List list, Addr end1, Addr end2, bool forward,
813 Addr* last, Addr* second_last, int* num_items) {
814 Addr current = forward ? heads_[list] : tails_[list];
815 *last = *second_last = current;
816 *num_items = 0;
817 if (!current.is_initialized())
818 return ERR_NO_ERROR;
819
820 if (!current.SanityCheckForRankings())
821 return ERR_INVALID_HEAD;
822
823 scoped_ptr<CacheRankingsBlock> node;
824 Addr prev_addr(current);
825 do {
826 node.reset(new CacheRankingsBlock(backend_->File(current), current));
827 node->Load();
828 if (!SanityCheck(node.get(), true))
829 return ERR_INVALID_ENTRY;
830
831 CacheAddr next = forward ? node->Data()->next : node->Data()->prev;
832 CacheAddr prev = forward ? node->Data()->prev : node->Data()->next;
833
834 if (prev != prev_addr.value())
835 return ERR_INVALID_PREV;
836
837 Addr next_addr(next);
838 if (!next_addr.SanityCheckForRankings())
839 return ERR_INVALID_NEXT;
840
841 prev_addr = current;
842 current = next_addr;
843 *second_last = *last;
844 *last = current;
845 (*num_items)++;
846
847 if (next_addr == prev_addr) {
848 Addr last = forward ? tails_[list] : heads_[list];
849 if (next_addr == last)
850 return ERR_NO_ERROR;
851 return ERR_INVALID_TAIL;
852 }
853 } while (current != end1 && current != end2);
854 return ERR_NO_ERROR;
855 }
856
857 bool Rankings::IsHead(CacheAddr addr, List* list) const {
858 for (int i = 0; i < LAST_ELEMENT; i++) {
859 if (addr == heads_[i].value()) {
860 if (*list != i)
861 Trace("Changing list %d to %d", *list, i);
862 *list = static_cast<List>(i);
863 return true;
864 }
865 }
866 return false;
867 }
868
869 bool Rankings::IsTail(CacheAddr addr, List* list) const {
870 for (int i = 0; i < LAST_ELEMENT; i++) {
871 if (addr == tails_[i].value()) {
872 if (*list != i)
873 Trace("Changing list %d to %d", *list, i);
874 *list = static_cast<List>(i);
875 return true;
876 }
877 }
878 return false;
879 }
880
881 // We expect to have just a few iterators at any given time, maybe two or three,
882 // But we could have more than one pointing at the same mode. We walk the list
883 // of cache iterators and update all that are pointing to the given node.
884 void Rankings::UpdateIterators(CacheRankingsBlock* node) {
885 CacheAddr address = node->address().value();
886 for (IteratorList::iterator it = iterators_.begin(); it != iterators_.end();
887 ++it) {
888 if (it->first == address && it->second->HasData()) {
889 CacheRankingsBlock* other = it->second;
890 *other->Data() = *node->Data();
891 }
892 }
893 }
894
895 void Rankings::InvalidateIterators(CacheRankingsBlock* node) {
896 CacheAddr address = node->address().value();
897 for (IteratorList::iterator it = iterators_.begin(); it != iterators_.end();
898 ++it) {
899 if (it->first == address) {
900 DLOG(INFO) << "Invalidating iterator at 0x" << std::hex << address;
901 it->second->Discard();
902 }
903 }
904 }
905
906 void Rankings::IncrementCounter(List list) {
907 if (!count_lists_)
908 return;
909
910 DCHECK(control_data_->sizes[list] < kint32max);
911 if (control_data_->sizes[list] < kint32max)
912 control_data_->sizes[list]++;
913 }
914
915 void Rankings::DecrementCounter(List list) {
916 if (!count_lists_)
917 return;
918
919 DCHECK(control_data_->sizes[list] > 0);
920 if (control_data_->sizes[list] > 0)
921 control_data_->sizes[list]--;
922 }
923
924 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698