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

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

Issue 121643003: Reorganize net/disk_cache into backend specific directories. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase & remediate 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/backend_impl.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/hash.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/metrics/histogram.h"
15 #include "base/metrics/stats_counters.h"
16 #include "base/rand_util.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/sys_info.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "base/time/time.h"
22 #include "base/timer/timer.h"
23 #include "net/base/net_errors.h"
24 #include "net/disk_cache/cache_util.h"
25 #include "net/disk_cache/disk_format.h"
26 #include "net/disk_cache/entry_impl.h"
27 #include "net/disk_cache/errors.h"
28 #include "net/disk_cache/experiments.h"
29 #include "net/disk_cache/file.h"
30
31 // Define BLOCKFILE_BACKEND_IMPL_OBJ to be a disk_cache::BackendImpl* in order
32 // to use the CACHE_UMA histogram macro.
33 #define BLOCKFILE_BACKEND_IMPL_OBJ this
34 #include "net/disk_cache/histogram_macros.h"
35
36 using base::Time;
37 using base::TimeDelta;
38 using base::TimeTicks;
39
40 namespace {
41
42 const char* kIndexName = "index";
43
44 // Seems like ~240 MB correspond to less than 50k entries for 99% of the people.
45 // Note that the actual target is to keep the index table load factor under 55%
46 // for most users.
47 const int k64kEntriesStore = 240 * 1000 * 1000;
48 const int kBaseTableLen = 64 * 1024;
49
50 // Avoid trimming the cache for the first 5 minutes (10 timer ticks).
51 const int kTrimDelay = 10;
52
53 int DesiredIndexTableLen(int32 storage_size) {
54 if (storage_size <= k64kEntriesStore)
55 return kBaseTableLen;
56 if (storage_size <= k64kEntriesStore * 2)
57 return kBaseTableLen * 2;
58 if (storage_size <= k64kEntriesStore * 4)
59 return kBaseTableLen * 4;
60 if (storage_size <= k64kEntriesStore * 8)
61 return kBaseTableLen * 8;
62
63 // The biggest storage_size for int32 requires a 4 MB table.
64 return kBaseTableLen * 16;
65 }
66
67 int MaxStorageSizeForTable(int table_len) {
68 return table_len * (k64kEntriesStore / kBaseTableLen);
69 }
70
71 size_t GetIndexSize(int table_len) {
72 size_t table_size = sizeof(disk_cache::CacheAddr) * table_len;
73 return sizeof(disk_cache::IndexHeader) + table_size;
74 }
75
76 // ------------------------------------------------------------------------
77
78 // Sets group for the current experiment. Returns false if the files should be
79 // discarded.
80 bool InitExperiment(disk_cache::IndexHeader* header, bool cache_created) {
81 if (header->experiment == disk_cache::EXPERIMENT_OLD_FILE1 ||
82 header->experiment == disk_cache::EXPERIMENT_OLD_FILE2) {
83 // Discard current cache.
84 return false;
85 }
86
87 if (base::FieldTrialList::FindFullName("SimpleCacheTrial") ==
88 "ExperimentControl") {
89 if (cache_created) {
90 header->experiment = disk_cache::EXPERIMENT_SIMPLE_CONTROL;
91 return true;
92 }
93 return header->experiment == disk_cache::EXPERIMENT_SIMPLE_CONTROL;
94 }
95
96 header->experiment = disk_cache::NO_EXPERIMENT;
97 return true;
98 }
99
100 // A callback to perform final cleanup on the background thread.
101 void FinalCleanupCallback(disk_cache::BackendImpl* backend) {
102 backend->CleanupCache();
103 }
104
105 } // namespace
106
107 // ------------------------------------------------------------------------
108
109 namespace disk_cache {
110
111 BackendImpl::BackendImpl(const base::FilePath& path,
112 base::MessageLoopProxy* cache_thread,
113 net::NetLog* net_log)
114 : background_queue_(this, cache_thread),
115 path_(path),
116 block_files_(path),
117 mask_(0),
118 max_size_(0),
119 up_ticks_(0),
120 cache_type_(net::DISK_CACHE),
121 uma_report_(0),
122 user_flags_(0),
123 init_(false),
124 restarted_(false),
125 unit_test_(false),
126 read_only_(false),
127 disabled_(false),
128 new_eviction_(false),
129 first_timer_(true),
130 user_load_(false),
131 net_log_(net_log),
132 done_(true, false),
133 ptr_factory_(this) {
134 }
135
136 BackendImpl::BackendImpl(const base::FilePath& path,
137 uint32 mask,
138 base::MessageLoopProxy* cache_thread,
139 net::NetLog* net_log)
140 : background_queue_(this, cache_thread),
141 path_(path),
142 block_files_(path),
143 mask_(mask),
144 max_size_(0),
145 up_ticks_(0),
146 cache_type_(net::DISK_CACHE),
147 uma_report_(0),
148 user_flags_(kMask),
149 init_(false),
150 restarted_(false),
151 unit_test_(false),
152 read_only_(false),
153 disabled_(false),
154 new_eviction_(false),
155 first_timer_(true),
156 user_load_(false),
157 net_log_(net_log),
158 done_(true, false),
159 ptr_factory_(this) {
160 }
161
162 BackendImpl::~BackendImpl() {
163 if (user_flags_ & kNoRandom) {
164 // This is a unit test, so we want to be strict about not leaking entries
165 // and completing all the work.
166 background_queue_.WaitForPendingIO();
167 } else {
168 // This is most likely not a test, so we want to do as little work as
169 // possible at this time, at the price of leaving dirty entries behind.
170 background_queue_.DropPendingIO();
171 }
172
173 if (background_queue_.BackgroundIsCurrentThread()) {
174 // Unit tests may use the same thread for everything.
175 CleanupCache();
176 } else {
177 background_queue_.background_thread()->PostTask(
178 FROM_HERE, base::Bind(&FinalCleanupCallback, base::Unretained(this)));
179 // http://crbug.com/74623
180 base::ThreadRestrictions::ScopedAllowWait allow_wait;
181 done_.Wait();
182 }
183 }
184
185 int BackendImpl::Init(const CompletionCallback& callback) {
186 background_queue_.Init(callback);
187 return net::ERR_IO_PENDING;
188 }
189
190 int BackendImpl::SyncInit() {
191 #if defined(NET_BUILD_STRESS_CACHE)
192 // Start evictions right away.
193 up_ticks_ = kTrimDelay * 2;
194 #endif
195 DCHECK(!init_);
196 if (init_)
197 return net::ERR_FAILED;
198
199 bool create_files = false;
200 if (!InitBackingStore(&create_files)) {
201 ReportError(ERR_STORAGE_ERROR);
202 return net::ERR_FAILED;
203 }
204
205 num_refs_ = num_pending_io_ = max_refs_ = 0;
206 entry_count_ = byte_count_ = 0;
207
208 bool should_create_timer = false;
209 if (!restarted_) {
210 buffer_bytes_ = 0;
211 trace_object_ = TraceObject::GetTraceObject();
212 should_create_timer = true;
213 }
214
215 init_ = true;
216 Trace("Init");
217
218 if (data_->header.experiment != NO_EXPERIMENT &&
219 cache_type_ != net::DISK_CACHE) {
220 // No experiment for other caches.
221 return net::ERR_FAILED;
222 }
223
224 if (!(user_flags_ & kNoRandom)) {
225 // The unit test controls directly what to test.
226 new_eviction_ = (cache_type_ == net::DISK_CACHE);
227 }
228
229 if (!CheckIndex()) {
230 ReportError(ERR_INIT_FAILED);
231 return net::ERR_FAILED;
232 }
233
234 if (!restarted_ && (create_files || !data_->header.num_entries))
235 ReportError(ERR_CACHE_CREATED);
236
237 if (!(user_flags_ & kNoRandom) && cache_type_ == net::DISK_CACHE &&
238 !InitExperiment(&data_->header, create_files)) {
239 return net::ERR_FAILED;
240 }
241
242 // We don't care if the value overflows. The only thing we care about is that
243 // the id cannot be zero, because that value is used as "not dirty".
244 // Increasing the value once per second gives us many years before we start
245 // having collisions.
246 data_->header.this_id++;
247 if (!data_->header.this_id)
248 data_->header.this_id++;
249
250 bool previous_crash = (data_->header.crash != 0);
251 data_->header.crash = 1;
252
253 if (!block_files_.Init(create_files))
254 return net::ERR_FAILED;
255
256 // We want to minimize the changes to cache for an AppCache.
257 if (cache_type() == net::APP_CACHE) {
258 DCHECK(!new_eviction_);
259 read_only_ = true;
260 } else if (cache_type() == net::SHADER_CACHE) {
261 DCHECK(!new_eviction_);
262 }
263
264 eviction_.Init(this);
265
266 // stats_ and rankings_ may end up calling back to us so we better be enabled.
267 disabled_ = false;
268 if (!InitStats())
269 return net::ERR_FAILED;
270
271 disabled_ = !rankings_.Init(this, new_eviction_);
272
273 #if defined(STRESS_CACHE_EXTENDED_VALIDATION)
274 trace_object_->EnableTracing(false);
275 int sc = SelfCheck();
276 if (sc < 0 && sc != ERR_NUM_ENTRIES_MISMATCH)
277 NOTREACHED();
278 trace_object_->EnableTracing(true);
279 #endif
280
281 if (previous_crash) {
282 ReportError(ERR_PREVIOUS_CRASH);
283 } else if (!restarted_) {
284 ReportError(ERR_NO_ERROR);
285 }
286
287 FlushIndex();
288
289 if (!disabled_ && should_create_timer) {
290 // Create a recurrent timer of 30 secs.
291 int timer_delay = unit_test_ ? 1000 : 30000;
292 timer_.reset(new base::RepeatingTimer<BackendImpl>());
293 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(timer_delay), this,
294 &BackendImpl::OnStatsTimer);
295 }
296
297 return disabled_ ? net::ERR_FAILED : net::OK;
298 }
299
300 void BackendImpl::CleanupCache() {
301 Trace("Backend Cleanup");
302 eviction_.Stop();
303 timer_.reset();
304
305 if (init_) {
306 StoreStats();
307 if (data_)
308 data_->header.crash = 0;
309
310 if (user_flags_ & kNoRandom) {
311 // This is a net_unittest, verify that we are not 'leaking' entries.
312 File::WaitForPendingIO(&num_pending_io_);
313 DCHECK(!num_refs_);
314 } else {
315 File::DropPendingIO();
316 }
317 }
318 block_files_.CloseFiles();
319 FlushIndex();
320 index_ = NULL;
321 ptr_factory_.InvalidateWeakPtrs();
322 done_.Signal();
323 }
324
325 // ------------------------------------------------------------------------
326
327 int BackendImpl::OpenPrevEntry(void** iter, Entry** prev_entry,
328 const CompletionCallback& callback) {
329 DCHECK(!callback.is_null());
330 background_queue_.OpenPrevEntry(iter, prev_entry, callback);
331 return net::ERR_IO_PENDING;
332 }
333
334 int BackendImpl::SyncOpenEntry(const std::string& key, Entry** entry) {
335 DCHECK(entry);
336 *entry = OpenEntryImpl(key);
337 return (*entry) ? net::OK : net::ERR_FAILED;
338 }
339
340 int BackendImpl::SyncCreateEntry(const std::string& key, Entry** entry) {
341 DCHECK(entry);
342 *entry = CreateEntryImpl(key);
343 return (*entry) ? net::OK : net::ERR_FAILED;
344 }
345
346 int BackendImpl::SyncDoomEntry(const std::string& key) {
347 if (disabled_)
348 return net::ERR_FAILED;
349
350 EntryImpl* entry = OpenEntryImpl(key);
351 if (!entry)
352 return net::ERR_FAILED;
353
354 entry->DoomImpl();
355 entry->Release();
356 return net::OK;
357 }
358
359 int BackendImpl::SyncDoomAllEntries() {
360 // This is not really an error, but it is an interesting condition.
361 ReportError(ERR_CACHE_DOOMED);
362 stats_.OnEvent(Stats::DOOM_CACHE);
363 if (!num_refs_) {
364 RestartCache(false);
365 return disabled_ ? net::ERR_FAILED : net::OK;
366 } else {
367 if (disabled_)
368 return net::ERR_FAILED;
369
370 eviction_.TrimCache(true);
371 return net::OK;
372 }
373 }
374
375 int BackendImpl::SyncDoomEntriesBetween(const base::Time initial_time,
376 const base::Time end_time) {
377 DCHECK_NE(net::APP_CACHE, cache_type_);
378 if (end_time.is_null())
379 return SyncDoomEntriesSince(initial_time);
380
381 DCHECK(end_time >= initial_time);
382
383 if (disabled_)
384 return net::ERR_FAILED;
385
386 EntryImpl* node;
387 void* iter = NULL;
388 EntryImpl* next = OpenNextEntryImpl(&iter);
389 if (!next)
390 return net::OK;
391
392 while (next) {
393 node = next;
394 next = OpenNextEntryImpl(&iter);
395
396 if (node->GetLastUsed() >= initial_time &&
397 node->GetLastUsed() < end_time) {
398 node->DoomImpl();
399 } else if (node->GetLastUsed() < initial_time) {
400 if (next)
401 next->Release();
402 next = NULL;
403 SyncEndEnumeration(iter);
404 }
405
406 node->Release();
407 }
408
409 return net::OK;
410 }
411
412 // We use OpenNextEntryImpl to retrieve elements from the cache, until we get
413 // entries that are too old.
414 int BackendImpl::SyncDoomEntriesSince(const base::Time initial_time) {
415 DCHECK_NE(net::APP_CACHE, cache_type_);
416 if (disabled_)
417 return net::ERR_FAILED;
418
419 stats_.OnEvent(Stats::DOOM_RECENT);
420 for (;;) {
421 void* iter = NULL;
422 EntryImpl* entry = OpenNextEntryImpl(&iter);
423 if (!entry)
424 return net::OK;
425
426 if (initial_time > entry->GetLastUsed()) {
427 entry->Release();
428 SyncEndEnumeration(iter);
429 return net::OK;
430 }
431
432 entry->DoomImpl();
433 entry->Release();
434 SyncEndEnumeration(iter); // Dooming the entry invalidates the iterator.
435 }
436 }
437
438 int BackendImpl::SyncOpenNextEntry(void** iter, Entry** next_entry) {
439 *next_entry = OpenNextEntryImpl(iter);
440 return (*next_entry) ? net::OK : net::ERR_FAILED;
441 }
442
443 int BackendImpl::SyncOpenPrevEntry(void** iter, Entry** prev_entry) {
444 *prev_entry = OpenPrevEntryImpl(iter);
445 return (*prev_entry) ? net::OK : net::ERR_FAILED;
446 }
447
448 void BackendImpl::SyncEndEnumeration(void* iter) {
449 scoped_ptr<Rankings::Iterator> iterator(
450 reinterpret_cast<Rankings::Iterator*>(iter));
451 }
452
453 void BackendImpl::SyncOnExternalCacheHit(const std::string& key) {
454 if (disabled_)
455 return;
456
457 uint32 hash = base::Hash(key);
458 bool error;
459 EntryImpl* cache_entry = MatchEntry(key, hash, false, Addr(), &error);
460 if (cache_entry) {
461 if (ENTRY_NORMAL == cache_entry->entry()->Data()->state) {
462 UpdateRank(cache_entry, cache_type() == net::SHADER_CACHE);
463 }
464 cache_entry->Release();
465 }
466 }
467
468 EntryImpl* BackendImpl::OpenEntryImpl(const std::string& key) {
469 if (disabled_)
470 return NULL;
471
472 TimeTicks start = TimeTicks::Now();
473 uint32 hash = base::Hash(key);
474 Trace("Open hash 0x%x", hash);
475
476 bool error;
477 EntryImpl* cache_entry = MatchEntry(key, hash, false, Addr(), &error);
478 if (cache_entry && ENTRY_NORMAL != cache_entry->entry()->Data()->state) {
479 // The entry was already evicted.
480 cache_entry->Release();
481 cache_entry = NULL;
482 }
483
484 int current_size = data_->header.num_bytes / (1024 * 1024);
485 int64 total_hours = stats_.GetCounter(Stats::TIMER) / 120;
486 int64 no_use_hours = stats_.GetCounter(Stats::LAST_REPORT_TIMER) / 120;
487 int64 use_hours = total_hours - no_use_hours;
488
489 if (!cache_entry) {
490 CACHE_UMA(AGE_MS, "OpenTime.Miss", 0, start);
491 CACHE_UMA(COUNTS_10000, "AllOpenBySize.Miss", 0, current_size);
492 CACHE_UMA(HOURS, "AllOpenByTotalHours.Miss", 0, total_hours);
493 CACHE_UMA(HOURS, "AllOpenByUseHours.Miss", 0, use_hours);
494 stats_.OnEvent(Stats::OPEN_MISS);
495 return NULL;
496 }
497
498 eviction_.OnOpenEntry(cache_entry);
499 entry_count_++;
500
501 Trace("Open hash 0x%x end: 0x%x", hash,
502 cache_entry->entry()->address().value());
503 CACHE_UMA(AGE_MS, "OpenTime", 0, start);
504 CACHE_UMA(COUNTS_10000, "AllOpenBySize.Hit", 0, current_size);
505 CACHE_UMA(HOURS, "AllOpenByTotalHours.Hit", 0, total_hours);
506 CACHE_UMA(HOURS, "AllOpenByUseHours.Hit", 0, use_hours);
507 stats_.OnEvent(Stats::OPEN_HIT);
508 SIMPLE_STATS_COUNTER("disk_cache.hit");
509 return cache_entry;
510 }
511
512 EntryImpl* BackendImpl::CreateEntryImpl(const std::string& key) {
513 if (disabled_ || key.empty())
514 return NULL;
515
516 TimeTicks start = TimeTicks::Now();
517 uint32 hash = base::Hash(key);
518 Trace("Create hash 0x%x", hash);
519
520 scoped_refptr<EntryImpl> parent;
521 Addr entry_address(data_->table[hash & mask_]);
522 if (entry_address.is_initialized()) {
523 // We have an entry already. It could be the one we are looking for, or just
524 // a hash conflict.
525 bool error;
526 EntryImpl* old_entry = MatchEntry(key, hash, false, Addr(), &error);
527 if (old_entry)
528 return ResurrectEntry(old_entry);
529
530 EntryImpl* parent_entry = MatchEntry(key, hash, true, Addr(), &error);
531 DCHECK(!error);
532 if (parent_entry) {
533 parent.swap(&parent_entry);
534 } else if (data_->table[hash & mask_]) {
535 // We should have corrected the problem.
536 NOTREACHED();
537 return NULL;
538 }
539 }
540
541 // The general flow is to allocate disk space and initialize the entry data,
542 // followed by saving that to disk, then linking the entry though the index
543 // and finally through the lists. If there is a crash in this process, we may
544 // end up with:
545 // a. Used, unreferenced empty blocks on disk (basically just garbage).
546 // b. Used, unreferenced but meaningful data on disk (more garbage).
547 // c. A fully formed entry, reachable only through the index.
548 // d. A fully formed entry, also reachable through the lists, but still dirty.
549 //
550 // Anything after (b) can be automatically cleaned up. We may consider saving
551 // the current operation (as we do while manipulating the lists) so that we
552 // can detect and cleanup (a) and (b).
553
554 int num_blocks = EntryImpl::NumBlocksForEntry(key.size());
555 if (!block_files_.CreateBlock(BLOCK_256, num_blocks, &entry_address)) {
556 LOG(ERROR) << "Create entry failed " << key.c_str();
557 stats_.OnEvent(Stats::CREATE_ERROR);
558 return NULL;
559 }
560
561 Addr node_address(0);
562 if (!block_files_.CreateBlock(RANKINGS, 1, &node_address)) {
563 block_files_.DeleteBlock(entry_address, false);
564 LOG(ERROR) << "Create entry failed " << key.c_str();
565 stats_.OnEvent(Stats::CREATE_ERROR);
566 return NULL;
567 }
568
569 scoped_refptr<EntryImpl> cache_entry(
570 new EntryImpl(this, entry_address, false));
571 IncreaseNumRefs();
572
573 if (!cache_entry->CreateEntry(node_address, key, hash)) {
574 block_files_.DeleteBlock(entry_address, false);
575 block_files_.DeleteBlock(node_address, false);
576 LOG(ERROR) << "Create entry failed " << key.c_str();
577 stats_.OnEvent(Stats::CREATE_ERROR);
578 return NULL;
579 }
580
581 cache_entry->BeginLogging(net_log_, true);
582
583 // We are not failing the operation; let's add this to the map.
584 open_entries_[entry_address.value()] = cache_entry.get();
585
586 // Save the entry.
587 cache_entry->entry()->Store();
588 cache_entry->rankings()->Store();
589 IncreaseNumEntries();
590 entry_count_++;
591
592 // Link this entry through the index.
593 if (parent.get()) {
594 parent->SetNextAddress(entry_address);
595 } else {
596 data_->table[hash & mask_] = entry_address.value();
597 }
598
599 // Link this entry through the lists.
600 eviction_.OnCreateEntry(cache_entry.get());
601
602 CACHE_UMA(AGE_MS, "CreateTime", 0, start);
603 stats_.OnEvent(Stats::CREATE_HIT);
604 SIMPLE_STATS_COUNTER("disk_cache.miss");
605 Trace("create entry hit ");
606 FlushIndex();
607 cache_entry->AddRef();
608 return cache_entry.get();
609 }
610
611 EntryImpl* BackendImpl::OpenNextEntryImpl(void** iter) {
612 return OpenFollowingEntry(true, iter);
613 }
614
615 EntryImpl* BackendImpl::OpenPrevEntryImpl(void** iter) {
616 return OpenFollowingEntry(false, iter);
617 }
618
619 bool BackendImpl::SetMaxSize(int max_bytes) {
620 COMPILE_ASSERT(sizeof(max_bytes) == sizeof(max_size_), unsupported_int_model);
621 if (max_bytes < 0)
622 return false;
623
624 // Zero size means use the default.
625 if (!max_bytes)
626 return true;
627
628 // Avoid a DCHECK later on.
629 if (max_bytes >= kint32max - kint32max / 10)
630 max_bytes = kint32max - kint32max / 10 - 1;
631
632 user_flags_ |= kMaxSize;
633 max_size_ = max_bytes;
634 return true;
635 }
636
637 void BackendImpl::SetType(net::CacheType type) {
638 DCHECK_NE(net::MEMORY_CACHE, type);
639 cache_type_ = type;
640 }
641
642 base::FilePath BackendImpl::GetFileName(Addr address) const {
643 if (!address.is_separate_file() || !address.is_initialized()) {
644 NOTREACHED();
645 return base::FilePath();
646 }
647
648 std::string tmp = base::StringPrintf("f_%06x", address.FileNumber());
649 return path_.AppendASCII(tmp);
650 }
651
652 MappedFile* BackendImpl::File(Addr address) {
653 if (disabled_)
654 return NULL;
655 return block_files_.GetFile(address);
656 }
657
658 base::WeakPtr<InFlightBackendIO> BackendImpl::GetBackgroundQueue() {
659 return background_queue_.GetWeakPtr();
660 }
661
662 bool BackendImpl::CreateExternalFile(Addr* address) {
663 int file_number = data_->header.last_file + 1;
664 Addr file_address(0);
665 bool success = false;
666 for (int i = 0; i < 0x0fffffff; i++, file_number++) {
667 if (!file_address.SetFileNumber(file_number)) {
668 file_number = 1;
669 continue;
670 }
671 base::FilePath name = GetFileName(file_address);
672 int flags = base::PLATFORM_FILE_READ |
673 base::PLATFORM_FILE_WRITE |
674 base::PLATFORM_FILE_CREATE |
675 base::PLATFORM_FILE_EXCLUSIVE_WRITE;
676 base::PlatformFileError error;
677 scoped_refptr<disk_cache::File> file(new disk_cache::File(
678 base::CreatePlatformFile(name, flags, NULL, &error)));
679 if (!file->IsValid()) {
680 if (error != base::PLATFORM_FILE_ERROR_EXISTS) {
681 LOG(ERROR) << "Unable to create file: " << error;
682 return false;
683 }
684 continue;
685 }
686
687 success = true;
688 break;
689 }
690
691 DCHECK(success);
692 if (!success)
693 return false;
694
695 data_->header.last_file = file_number;
696 address->set_value(file_address.value());
697 return true;
698 }
699
700 bool BackendImpl::CreateBlock(FileType block_type, int block_count,
701 Addr* block_address) {
702 return block_files_.CreateBlock(block_type, block_count, block_address);
703 }
704
705 void BackendImpl::DeleteBlock(Addr block_address, bool deep) {
706 block_files_.DeleteBlock(block_address, deep);
707 }
708
709 LruData* BackendImpl::GetLruData() {
710 return &data_->header.lru;
711 }
712
713 void BackendImpl::UpdateRank(EntryImpl* entry, bool modified) {
714 if (read_only_ || (!modified && cache_type() == net::SHADER_CACHE))
715 return;
716 eviction_.UpdateRank(entry, modified);
717 }
718
719 void BackendImpl::RecoveredEntry(CacheRankingsBlock* rankings) {
720 Addr address(rankings->Data()->contents);
721 EntryImpl* cache_entry = NULL;
722 if (NewEntry(address, &cache_entry)) {
723 STRESS_NOTREACHED();
724 return;
725 }
726
727 uint32 hash = cache_entry->GetHash();
728 cache_entry->Release();
729
730 // Anything on the table means that this entry is there.
731 if (data_->table[hash & mask_])
732 return;
733
734 data_->table[hash & mask_] = address.value();
735 FlushIndex();
736 }
737
738 void BackendImpl::InternalDoomEntry(EntryImpl* entry) {
739 uint32 hash = entry->GetHash();
740 std::string key = entry->GetKey();
741 Addr entry_addr = entry->entry()->address();
742 bool error;
743 EntryImpl* parent_entry = MatchEntry(key, hash, true, entry_addr, &error);
744 CacheAddr child(entry->GetNextAddress());
745
746 Trace("Doom entry 0x%p", entry);
747
748 if (!entry->doomed()) {
749 // We may have doomed this entry from within MatchEntry.
750 eviction_.OnDoomEntry(entry);
751 entry->InternalDoom();
752 if (!new_eviction_) {
753 DecreaseNumEntries();
754 }
755 stats_.OnEvent(Stats::DOOM_ENTRY);
756 }
757
758 if (parent_entry) {
759 parent_entry->SetNextAddress(Addr(child));
760 parent_entry->Release();
761 } else if (!error) {
762 data_->table[hash & mask_] = child;
763 }
764
765 FlushIndex();
766 }
767
768 #if defined(NET_BUILD_STRESS_CACHE)
769
770 CacheAddr BackendImpl::GetNextAddr(Addr address) {
771 EntriesMap::iterator it = open_entries_.find(address.value());
772 if (it != open_entries_.end()) {
773 EntryImpl* this_entry = it->second;
774 return this_entry->GetNextAddress();
775 }
776 DCHECK(block_files_.IsValid(address));
777 DCHECK(!address.is_separate_file() && address.file_type() == BLOCK_256);
778
779 CacheEntryBlock entry(File(address), address);
780 CHECK(entry.Load());
781 return entry.Data()->next;
782 }
783
784 void BackendImpl::NotLinked(EntryImpl* entry) {
785 Addr entry_addr = entry->entry()->address();
786 uint32 i = entry->GetHash() & mask_;
787 Addr address(data_->table[i]);
788 if (!address.is_initialized())
789 return;
790
791 for (;;) {
792 DCHECK(entry_addr.value() != address.value());
793 address.set_value(GetNextAddr(address));
794 if (!address.is_initialized())
795 break;
796 }
797 }
798 #endif // NET_BUILD_STRESS_CACHE
799
800 // An entry may be linked on the DELETED list for a while after being doomed.
801 // This function is called when we want to remove it.
802 void BackendImpl::RemoveEntry(EntryImpl* entry) {
803 #if defined(NET_BUILD_STRESS_CACHE)
804 NotLinked(entry);
805 #endif
806 if (!new_eviction_)
807 return;
808
809 DCHECK_NE(ENTRY_NORMAL, entry->entry()->Data()->state);
810
811 Trace("Remove entry 0x%p", entry);
812 eviction_.OnDestroyEntry(entry);
813 DecreaseNumEntries();
814 }
815
816 void BackendImpl::OnEntryDestroyBegin(Addr address) {
817 EntriesMap::iterator it = open_entries_.find(address.value());
818 if (it != open_entries_.end())
819 open_entries_.erase(it);
820 }
821
822 void BackendImpl::OnEntryDestroyEnd() {
823 DecreaseNumRefs();
824 if (data_->header.num_bytes > max_size_ && !read_only_ &&
825 (up_ticks_ > kTrimDelay || user_flags_ & kNoRandom))
826 eviction_.TrimCache(false);
827 }
828
829 EntryImpl* BackendImpl::GetOpenEntry(CacheRankingsBlock* rankings) const {
830 DCHECK(rankings->HasData());
831 EntriesMap::const_iterator it =
832 open_entries_.find(rankings->Data()->contents);
833 if (it != open_entries_.end()) {
834 // We have this entry in memory.
835 return it->second;
836 }
837
838 return NULL;
839 }
840
841 int32 BackendImpl::GetCurrentEntryId() const {
842 return data_->header.this_id;
843 }
844
845 int BackendImpl::MaxFileSize() const {
846 return cache_type() == net::PNACL_CACHE ? max_size_ : max_size_ / 8;
847 }
848
849 void BackendImpl::ModifyStorageSize(int32 old_size, int32 new_size) {
850 if (disabled_ || old_size == new_size)
851 return;
852 if (old_size > new_size)
853 SubstractStorageSize(old_size - new_size);
854 else
855 AddStorageSize(new_size - old_size);
856
857 FlushIndex();
858
859 // Update the usage statistics.
860 stats_.ModifyStorageStats(old_size, new_size);
861 }
862
863 void BackendImpl::TooMuchStorageRequested(int32 size) {
864 stats_.ModifyStorageStats(0, size);
865 }
866
867 bool BackendImpl::IsAllocAllowed(int current_size, int new_size) {
868 DCHECK_GT(new_size, current_size);
869 if (user_flags_ & kNoBuffering)
870 return false;
871
872 int to_add = new_size - current_size;
873 if (buffer_bytes_ + to_add > MaxBuffersSize())
874 return false;
875
876 buffer_bytes_ += to_add;
877 CACHE_UMA(COUNTS_50000, "BufferBytes", 0, buffer_bytes_ / 1024);
878 return true;
879 }
880
881 void BackendImpl::BufferDeleted(int size) {
882 buffer_bytes_ -= size;
883 DCHECK_GE(size, 0);
884 }
885
886 bool BackendImpl::IsLoaded() const {
887 CACHE_UMA(COUNTS, "PendingIO", 0, num_pending_io_);
888 if (user_flags_ & kNoLoadProtection)
889 return false;
890
891 return (num_pending_io_ > 5 || user_load_);
892 }
893
894 std::string BackendImpl::HistogramName(const char* name, int experiment) const {
895 if (!experiment)
896 return base::StringPrintf("DiskCache.%d.%s", cache_type_, name);
897 return base::StringPrintf("DiskCache.%d.%s_%d", cache_type_,
898 name, experiment);
899 }
900
901 base::WeakPtr<BackendImpl> BackendImpl::GetWeakPtr() {
902 return ptr_factory_.GetWeakPtr();
903 }
904
905 // We want to remove biases from some histograms so we only send data once per
906 // week.
907 bool BackendImpl::ShouldReportAgain() {
908 if (uma_report_)
909 return uma_report_ == 2;
910
911 uma_report_++;
912 int64 last_report = stats_.GetCounter(Stats::LAST_REPORT);
913 Time last_time = Time::FromInternalValue(last_report);
914 if (!last_report || (Time::Now() - last_time).InDays() >= 7) {
915 stats_.SetCounter(Stats::LAST_REPORT, Time::Now().ToInternalValue());
916 uma_report_++;
917 return true;
918 }
919 return false;
920 }
921
922 void BackendImpl::FirstEviction() {
923 DCHECK(data_->header.create_time);
924 if (!GetEntryCount())
925 return; // This is just for unit tests.
926
927 Time create_time = Time::FromInternalValue(data_->header.create_time);
928 CACHE_UMA(AGE, "FillupAge", 0, create_time);
929
930 int64 use_time = stats_.GetCounter(Stats::TIMER);
931 CACHE_UMA(HOURS, "FillupTime", 0, static_cast<int>(use_time / 120));
932 CACHE_UMA(PERCENTAGE, "FirstHitRatio", 0, stats_.GetHitRatio());
933
934 if (!use_time)
935 use_time = 1;
936 CACHE_UMA(COUNTS_10000, "FirstEntryAccessRate", 0,
937 static_cast<int>(data_->header.num_entries / use_time));
938 CACHE_UMA(COUNTS, "FirstByteIORate", 0,
939 static_cast<int>((data_->header.num_bytes / 1024) / use_time));
940
941 int avg_size = data_->header.num_bytes / GetEntryCount();
942 CACHE_UMA(COUNTS, "FirstEntrySize", 0, avg_size);
943
944 int large_entries_bytes = stats_.GetLargeEntriesSize();
945 int large_ratio = large_entries_bytes * 100 / data_->header.num_bytes;
946 CACHE_UMA(PERCENTAGE, "FirstLargeEntriesRatio", 0, large_ratio);
947
948 if (new_eviction_) {
949 CACHE_UMA(PERCENTAGE, "FirstResurrectRatio", 0, stats_.GetResurrectRatio());
950 CACHE_UMA(PERCENTAGE, "FirstNoUseRatio", 0,
951 data_->header.lru.sizes[0] * 100 / data_->header.num_entries);
952 CACHE_UMA(PERCENTAGE, "FirstLowUseRatio", 0,
953 data_->header.lru.sizes[1] * 100 / data_->header.num_entries);
954 CACHE_UMA(PERCENTAGE, "FirstHighUseRatio", 0,
955 data_->header.lru.sizes[2] * 100 / data_->header.num_entries);
956 }
957
958 stats_.ResetRatios();
959 }
960
961 void BackendImpl::CriticalError(int error) {
962 STRESS_NOTREACHED();
963 LOG(ERROR) << "Critical error found " << error;
964 if (disabled_)
965 return;
966
967 stats_.OnEvent(Stats::FATAL_ERROR);
968 LogStats();
969 ReportError(error);
970
971 // Setting the index table length to an invalid value will force re-creation
972 // of the cache files.
973 data_->header.table_len = 1;
974 disabled_ = true;
975
976 if (!num_refs_)
977 base::MessageLoop::current()->PostTask(
978 FROM_HERE, base::Bind(&BackendImpl::RestartCache, GetWeakPtr(), true));
979 }
980
981 void BackendImpl::ReportError(int error) {
982 STRESS_DCHECK(!error || error == ERR_PREVIOUS_CRASH ||
983 error == ERR_CACHE_CREATED);
984
985 // We transmit positive numbers, instead of direct error codes.
986 DCHECK_LE(error, 0);
987 CACHE_UMA(CACHE_ERROR, "Error", 0, error * -1);
988 }
989
990 void BackendImpl::OnEvent(Stats::Counters an_event) {
991 stats_.OnEvent(an_event);
992 }
993
994 void BackendImpl::OnRead(int32 bytes) {
995 DCHECK_GE(bytes, 0);
996 byte_count_ += bytes;
997 if (byte_count_ < 0)
998 byte_count_ = kint32max;
999 }
1000
1001 void BackendImpl::OnWrite(int32 bytes) {
1002 // We use the same implementation as OnRead... just log the number of bytes.
1003 OnRead(bytes);
1004 }
1005
1006 void BackendImpl::OnStatsTimer() {
1007 if (disabled_)
1008 return;
1009
1010 stats_.OnEvent(Stats::TIMER);
1011 int64 time = stats_.GetCounter(Stats::TIMER);
1012 int64 current = stats_.GetCounter(Stats::OPEN_ENTRIES);
1013
1014 // OPEN_ENTRIES is a sampled average of the number of open entries, avoiding
1015 // the bias towards 0.
1016 if (num_refs_ && (current != num_refs_)) {
1017 int64 diff = (num_refs_ - current) / 50;
1018 if (!diff)
1019 diff = num_refs_ > current ? 1 : -1;
1020 current = current + diff;
1021 stats_.SetCounter(Stats::OPEN_ENTRIES, current);
1022 stats_.SetCounter(Stats::MAX_ENTRIES, max_refs_);
1023 }
1024
1025 CACHE_UMA(COUNTS, "NumberOfReferences", 0, num_refs_);
1026
1027 CACHE_UMA(COUNTS_10000, "EntryAccessRate", 0, entry_count_);
1028 CACHE_UMA(COUNTS, "ByteIORate", 0, byte_count_ / 1024);
1029
1030 // These values cover about 99.5% of the population (Oct 2011).
1031 user_load_ = (entry_count_ > 300 || byte_count_ > 7 * 1024 * 1024);
1032 entry_count_ = 0;
1033 byte_count_ = 0;
1034 up_ticks_++;
1035
1036 if (!data_)
1037 first_timer_ = false;
1038 if (first_timer_) {
1039 first_timer_ = false;
1040 if (ShouldReportAgain())
1041 ReportStats();
1042 }
1043
1044 // Save stats to disk at 5 min intervals.
1045 if (time % 10 == 0)
1046 StoreStats();
1047 }
1048
1049 void BackendImpl::IncrementIoCount() {
1050 num_pending_io_++;
1051 }
1052
1053 void BackendImpl::DecrementIoCount() {
1054 num_pending_io_--;
1055 }
1056
1057 void BackendImpl::SetUnitTestMode() {
1058 user_flags_ |= kUnitTestMode;
1059 unit_test_ = true;
1060 }
1061
1062 void BackendImpl::SetUpgradeMode() {
1063 user_flags_ |= kUpgradeMode;
1064 read_only_ = true;
1065 }
1066
1067 void BackendImpl::SetNewEviction() {
1068 user_flags_ |= kNewEviction;
1069 new_eviction_ = true;
1070 }
1071
1072 void BackendImpl::SetFlags(uint32 flags) {
1073 user_flags_ |= flags;
1074 }
1075
1076 void BackendImpl::ClearRefCountForTest() {
1077 num_refs_ = 0;
1078 }
1079
1080 int BackendImpl::FlushQueueForTest(const CompletionCallback& callback) {
1081 background_queue_.FlushQueue(callback);
1082 return net::ERR_IO_PENDING;
1083 }
1084
1085 int BackendImpl::RunTaskForTest(const base::Closure& task,
1086 const CompletionCallback& callback) {
1087 background_queue_.RunTask(task, callback);
1088 return net::ERR_IO_PENDING;
1089 }
1090
1091 void BackendImpl::TrimForTest(bool empty) {
1092 eviction_.SetTestMode();
1093 eviction_.TrimCache(empty);
1094 }
1095
1096 void BackendImpl::TrimDeletedListForTest(bool empty) {
1097 eviction_.SetTestMode();
1098 eviction_.TrimDeletedList(empty);
1099 }
1100
1101 base::RepeatingTimer<BackendImpl>* BackendImpl::GetTimerForTest() {
1102 return timer_.get();
1103 }
1104
1105 int BackendImpl::SelfCheck() {
1106 if (!init_) {
1107 LOG(ERROR) << "Init failed";
1108 return ERR_INIT_FAILED;
1109 }
1110
1111 int num_entries = rankings_.SelfCheck();
1112 if (num_entries < 0) {
1113 LOG(ERROR) << "Invalid rankings list, error " << num_entries;
1114 #if !defined(NET_BUILD_STRESS_CACHE)
1115 return num_entries;
1116 #endif
1117 }
1118
1119 if (num_entries != data_->header.num_entries) {
1120 LOG(ERROR) << "Number of entries mismatch";
1121 #if !defined(NET_BUILD_STRESS_CACHE)
1122 return ERR_NUM_ENTRIES_MISMATCH;
1123 #endif
1124 }
1125
1126 return CheckAllEntries();
1127 }
1128
1129 void BackendImpl::FlushIndex() {
1130 if (index_.get() && !disabled_)
1131 index_->Flush();
1132 }
1133
1134 // ------------------------------------------------------------------------
1135
1136 net::CacheType BackendImpl::GetCacheType() const {
1137 return cache_type_;
1138 }
1139
1140 int32 BackendImpl::GetEntryCount() const {
1141 if (!index_.get() || disabled_)
1142 return 0;
1143 // num_entries includes entries already evicted.
1144 int32 not_deleted = data_->header.num_entries -
1145 data_->header.lru.sizes[Rankings::DELETED];
1146
1147 if (not_deleted < 0) {
1148 NOTREACHED();
1149 not_deleted = 0;
1150 }
1151
1152 return not_deleted;
1153 }
1154
1155 int BackendImpl::OpenEntry(const std::string& key, Entry** entry,
1156 const CompletionCallback& callback) {
1157 DCHECK(!callback.is_null());
1158 background_queue_.OpenEntry(key, entry, callback);
1159 return net::ERR_IO_PENDING;
1160 }
1161
1162 int BackendImpl::CreateEntry(const std::string& key, Entry** entry,
1163 const CompletionCallback& callback) {
1164 DCHECK(!callback.is_null());
1165 background_queue_.CreateEntry(key, entry, callback);
1166 return net::ERR_IO_PENDING;
1167 }
1168
1169 int BackendImpl::DoomEntry(const std::string& key,
1170 const CompletionCallback& callback) {
1171 DCHECK(!callback.is_null());
1172 background_queue_.DoomEntry(key, callback);
1173 return net::ERR_IO_PENDING;
1174 }
1175
1176 int BackendImpl::DoomAllEntries(const CompletionCallback& callback) {
1177 DCHECK(!callback.is_null());
1178 background_queue_.DoomAllEntries(callback);
1179 return net::ERR_IO_PENDING;
1180 }
1181
1182 int BackendImpl::DoomEntriesBetween(const base::Time initial_time,
1183 const base::Time end_time,
1184 const CompletionCallback& callback) {
1185 DCHECK(!callback.is_null());
1186 background_queue_.DoomEntriesBetween(initial_time, end_time, callback);
1187 return net::ERR_IO_PENDING;
1188 }
1189
1190 int BackendImpl::DoomEntriesSince(const base::Time initial_time,
1191 const CompletionCallback& callback) {
1192 DCHECK(!callback.is_null());
1193 background_queue_.DoomEntriesSince(initial_time, callback);
1194 return net::ERR_IO_PENDING;
1195 }
1196
1197 int BackendImpl::OpenNextEntry(void** iter, Entry** next_entry,
1198 const CompletionCallback& callback) {
1199 DCHECK(!callback.is_null());
1200 background_queue_.OpenNextEntry(iter, next_entry, callback);
1201 return net::ERR_IO_PENDING;
1202 }
1203
1204 void BackendImpl::EndEnumeration(void** iter) {
1205 background_queue_.EndEnumeration(*iter);
1206 *iter = NULL;
1207 }
1208
1209 void BackendImpl::GetStats(StatsItems* stats) {
1210 if (disabled_)
1211 return;
1212
1213 std::pair<std::string, std::string> item;
1214
1215 item.first = "Entries";
1216 item.second = base::StringPrintf("%d", data_->header.num_entries);
1217 stats->push_back(item);
1218
1219 item.first = "Pending IO";
1220 item.second = base::StringPrintf("%d", num_pending_io_);
1221 stats->push_back(item);
1222
1223 item.first = "Max size";
1224 item.second = base::StringPrintf("%d", max_size_);
1225 stats->push_back(item);
1226
1227 item.first = "Current size";
1228 item.second = base::StringPrintf("%d", data_->header.num_bytes);
1229 stats->push_back(item);
1230
1231 item.first = "Cache type";
1232 item.second = "Blockfile Cache";
1233 stats->push_back(item);
1234
1235 stats_.GetItems(stats);
1236 }
1237
1238 void BackendImpl::OnExternalCacheHit(const std::string& key) {
1239 background_queue_.OnExternalCacheHit(key);
1240 }
1241
1242 // ------------------------------------------------------------------------
1243
1244 // We just created a new file so we're going to write the header and set the
1245 // file length to include the hash table (zero filled).
1246 bool BackendImpl::CreateBackingStore(disk_cache::File* file) {
1247 AdjustMaxCacheSize(0);
1248
1249 IndexHeader header;
1250 header.table_len = DesiredIndexTableLen(max_size_);
1251
1252 // We need file version 2.1 for the new eviction algorithm.
1253 if (new_eviction_)
1254 header.version = 0x20001;
1255
1256 header.create_time = Time::Now().ToInternalValue();
1257
1258 if (!file->Write(&header, sizeof(header), 0))
1259 return false;
1260
1261 return file->SetLength(GetIndexSize(header.table_len));
1262 }
1263
1264 bool BackendImpl::InitBackingStore(bool* file_created) {
1265 if (!base::CreateDirectory(path_))
1266 return false;
1267
1268 base::FilePath index_name = path_.AppendASCII(kIndexName);
1269
1270 int flags = base::PLATFORM_FILE_READ |
1271 base::PLATFORM_FILE_WRITE |
1272 base::PLATFORM_FILE_OPEN_ALWAYS |
1273 base::PLATFORM_FILE_EXCLUSIVE_WRITE;
1274 scoped_refptr<disk_cache::File> file(new disk_cache::File(
1275 base::CreatePlatformFile(index_name, flags, file_created, NULL)));
1276
1277 if (!file->IsValid())
1278 return false;
1279
1280 bool ret = true;
1281 if (*file_created)
1282 ret = CreateBackingStore(file.get());
1283
1284 file = NULL;
1285 if (!ret)
1286 return false;
1287
1288 index_ = new MappedFile();
1289 data_ = reinterpret_cast<Index*>(index_->Init(index_name, 0));
1290 if (!data_) {
1291 LOG(ERROR) << "Unable to map Index file";
1292 return false;
1293 }
1294
1295 if (index_->GetLength() < sizeof(Index)) {
1296 // We verify this again on CheckIndex() but it's easier to make sure now
1297 // that the header is there.
1298 LOG(ERROR) << "Corrupt Index file";
1299 return false;
1300 }
1301
1302 return true;
1303 }
1304
1305 // The maximum cache size will be either set explicitly by the caller, or
1306 // calculated by this code.
1307 void BackendImpl::AdjustMaxCacheSize(int table_len) {
1308 if (max_size_)
1309 return;
1310
1311 // If table_len is provided, the index file exists.
1312 DCHECK(!table_len || data_->header.magic);
1313
1314 // The user is not setting the size, let's figure it out.
1315 int64 available = base::SysInfo::AmountOfFreeDiskSpace(path_);
1316 if (available < 0) {
1317 max_size_ = kDefaultCacheSize;
1318 return;
1319 }
1320
1321 if (table_len)
1322 available += data_->header.num_bytes;
1323
1324 max_size_ = PreferredCacheSize(available);
1325
1326 if (!table_len)
1327 return;
1328
1329 // If we already have a table, adjust the size to it.
1330 int current_max_size = MaxStorageSizeForTable(table_len);
1331 if (max_size_ > current_max_size)
1332 max_size_= current_max_size;
1333 }
1334
1335 bool BackendImpl::InitStats() {
1336 Addr address(data_->header.stats);
1337 int size = stats_.StorageSize();
1338
1339 if (!address.is_initialized()) {
1340 FileType file_type = Addr::RequiredFileType(size);
1341 DCHECK_NE(file_type, EXTERNAL);
1342 int num_blocks = Addr::RequiredBlocks(size, file_type);
1343
1344 if (!CreateBlock(file_type, num_blocks, &address))
1345 return false;
1346
1347 data_->header.stats = address.value();
1348 return stats_.Init(NULL, 0, address);
1349 }
1350
1351 if (!address.is_block_file()) {
1352 NOTREACHED();
1353 return false;
1354 }
1355
1356 // Load the required data.
1357 size = address.num_blocks() * address.BlockSize();
1358 MappedFile* file = File(address);
1359 if (!file)
1360 return false;
1361
1362 scoped_ptr<char[]> data(new char[size]);
1363 size_t offset = address.start_block() * address.BlockSize() +
1364 kBlockHeaderSize;
1365 if (!file->Read(data.get(), size, offset))
1366 return false;
1367
1368 if (!stats_.Init(data.get(), size, address))
1369 return false;
1370 if (cache_type_ == net::DISK_CACHE && ShouldReportAgain())
1371 stats_.InitSizeHistogram();
1372 return true;
1373 }
1374
1375 void BackendImpl::StoreStats() {
1376 int size = stats_.StorageSize();
1377 scoped_ptr<char[]> data(new char[size]);
1378 Addr address;
1379 size = stats_.SerializeStats(data.get(), size, &address);
1380 DCHECK(size);
1381 if (!address.is_initialized())
1382 return;
1383
1384 MappedFile* file = File(address);
1385 if (!file)
1386 return;
1387
1388 size_t offset = address.start_block() * address.BlockSize() +
1389 kBlockHeaderSize;
1390 file->Write(data.get(), size, offset); // ignore result.
1391 }
1392
1393 void BackendImpl::RestartCache(bool failure) {
1394 int64 errors = stats_.GetCounter(Stats::FATAL_ERROR);
1395 int64 full_dooms = stats_.GetCounter(Stats::DOOM_CACHE);
1396 int64 partial_dooms = stats_.GetCounter(Stats::DOOM_RECENT);
1397 int64 last_report = stats_.GetCounter(Stats::LAST_REPORT);
1398
1399 PrepareForRestart();
1400 if (failure) {
1401 DCHECK(!num_refs_);
1402 DCHECK(!open_entries_.size());
1403 DelayedCacheCleanup(path_);
1404 } else {
1405 DeleteCache(path_, false);
1406 }
1407
1408 // Don't call Init() if directed by the unit test: we are simulating a failure
1409 // trying to re-enable the cache.
1410 if (unit_test_)
1411 init_ = true; // Let the destructor do proper cleanup.
1412 else if (SyncInit() == net::OK) {
1413 stats_.SetCounter(Stats::FATAL_ERROR, errors);
1414 stats_.SetCounter(Stats::DOOM_CACHE, full_dooms);
1415 stats_.SetCounter(Stats::DOOM_RECENT, partial_dooms);
1416 stats_.SetCounter(Stats::LAST_REPORT, last_report);
1417 }
1418 }
1419
1420 void BackendImpl::PrepareForRestart() {
1421 // Reset the mask_ if it was not given by the user.
1422 if (!(user_flags_ & kMask))
1423 mask_ = 0;
1424
1425 if (!(user_flags_ & kNewEviction))
1426 new_eviction_ = false;
1427
1428 disabled_ = true;
1429 data_->header.crash = 0;
1430 index_->Flush();
1431 index_ = NULL;
1432 data_ = NULL;
1433 block_files_.CloseFiles();
1434 rankings_.Reset();
1435 init_ = false;
1436 restarted_ = true;
1437 }
1438
1439 int BackendImpl::NewEntry(Addr address, EntryImpl** entry) {
1440 EntriesMap::iterator it = open_entries_.find(address.value());
1441 if (it != open_entries_.end()) {
1442 // Easy job. This entry is already in memory.
1443 EntryImpl* this_entry = it->second;
1444 this_entry->AddRef();
1445 *entry = this_entry;
1446 return 0;
1447 }
1448
1449 STRESS_DCHECK(block_files_.IsValid(address));
1450
1451 if (!address.SanityCheckForEntryV2()) {
1452 LOG(WARNING) << "Wrong entry address.";
1453 STRESS_NOTREACHED();
1454 return ERR_INVALID_ADDRESS;
1455 }
1456
1457 scoped_refptr<EntryImpl> cache_entry(
1458 new EntryImpl(this, address, read_only_));
1459 IncreaseNumRefs();
1460 *entry = NULL;
1461
1462 TimeTicks start = TimeTicks::Now();
1463 if (!cache_entry->entry()->Load())
1464 return ERR_READ_FAILURE;
1465
1466 if (IsLoaded()) {
1467 CACHE_UMA(AGE_MS, "LoadTime", 0, start);
1468 }
1469
1470 if (!cache_entry->SanityCheck()) {
1471 LOG(WARNING) << "Messed up entry found.";
1472 STRESS_NOTREACHED();
1473 return ERR_INVALID_ENTRY;
1474 }
1475
1476 STRESS_DCHECK(block_files_.IsValid(
1477 Addr(cache_entry->entry()->Data()->rankings_node)));
1478
1479 if (!cache_entry->LoadNodeAddress())
1480 return ERR_READ_FAILURE;
1481
1482 if (!rankings_.SanityCheck(cache_entry->rankings(), false)) {
1483 STRESS_NOTREACHED();
1484 cache_entry->SetDirtyFlag(0);
1485 // Don't remove this from the list (it is not linked properly). Instead,
1486 // break the link back to the entry because it is going away, and leave the
1487 // rankings node to be deleted if we find it through a list.
1488 rankings_.SetContents(cache_entry->rankings(), 0);
1489 } else if (!rankings_.DataSanityCheck(cache_entry->rankings(), false)) {
1490 STRESS_NOTREACHED();
1491 cache_entry->SetDirtyFlag(0);
1492 rankings_.SetContents(cache_entry->rankings(), address.value());
1493 }
1494
1495 if (!cache_entry->DataSanityCheck()) {
1496 LOG(WARNING) << "Messed up entry found.";
1497 cache_entry->SetDirtyFlag(0);
1498 cache_entry->FixForDelete();
1499 }
1500
1501 // Prevent overwriting the dirty flag on the destructor.
1502 cache_entry->SetDirtyFlag(GetCurrentEntryId());
1503
1504 if (cache_entry->dirty()) {
1505 Trace("Dirty entry 0x%p 0x%x", reinterpret_cast<void*>(cache_entry.get()),
1506 address.value());
1507 }
1508
1509 open_entries_[address.value()] = cache_entry.get();
1510
1511 cache_entry->BeginLogging(net_log_, false);
1512 cache_entry.swap(entry);
1513 return 0;
1514 }
1515
1516 EntryImpl* BackendImpl::MatchEntry(const std::string& key, uint32 hash,
1517 bool find_parent, Addr entry_addr,
1518 bool* match_error) {
1519 Addr address(data_->table[hash & mask_]);
1520 scoped_refptr<EntryImpl> cache_entry, parent_entry;
1521 EntryImpl* tmp = NULL;
1522 bool found = false;
1523 std::set<CacheAddr> visited;
1524 *match_error = false;
1525
1526 for (;;) {
1527 if (disabled_)
1528 break;
1529
1530 if (visited.find(address.value()) != visited.end()) {
1531 // It's possible for a buggy version of the code to write a loop. Just
1532 // break it.
1533 Trace("Hash collision loop 0x%x", address.value());
1534 address.set_value(0);
1535 parent_entry->SetNextAddress(address);
1536 }
1537 visited.insert(address.value());
1538
1539 if (!address.is_initialized()) {
1540 if (find_parent)
1541 found = true;
1542 break;
1543 }
1544
1545 int error = NewEntry(address, &tmp);
1546 cache_entry.swap(&tmp);
1547
1548 if (error || cache_entry->dirty()) {
1549 // This entry is dirty on disk (it was not properly closed): we cannot
1550 // trust it.
1551 Addr child(0);
1552 if (!error)
1553 child.set_value(cache_entry->GetNextAddress());
1554
1555 if (parent_entry.get()) {
1556 parent_entry->SetNextAddress(child);
1557 parent_entry = NULL;
1558 } else {
1559 data_->table[hash & mask_] = child.value();
1560 }
1561
1562 Trace("MatchEntry dirty %d 0x%x 0x%x", find_parent, entry_addr.value(),
1563 address.value());
1564
1565 if (!error) {
1566 // It is important to call DestroyInvalidEntry after removing this
1567 // entry from the table.
1568 DestroyInvalidEntry(cache_entry.get());
1569 cache_entry = NULL;
1570 } else {
1571 Trace("NewEntry failed on MatchEntry 0x%x", address.value());
1572 }
1573
1574 // Restart the search.
1575 address.set_value(data_->table[hash & mask_]);
1576 visited.clear();
1577 continue;
1578 }
1579
1580 DCHECK_EQ(hash & mask_, cache_entry->entry()->Data()->hash & mask_);
1581 if (cache_entry->IsSameEntry(key, hash)) {
1582 if (!cache_entry->Update())
1583 cache_entry = NULL;
1584 found = true;
1585 if (find_parent && entry_addr.value() != address.value()) {
1586 Trace("Entry not on the index 0x%x", address.value());
1587 *match_error = true;
1588 parent_entry = NULL;
1589 }
1590 break;
1591 }
1592 if (!cache_entry->Update())
1593 cache_entry = NULL;
1594 parent_entry = cache_entry;
1595 cache_entry = NULL;
1596 if (!parent_entry.get())
1597 break;
1598
1599 address.set_value(parent_entry->GetNextAddress());
1600 }
1601
1602 if (parent_entry.get() && (!find_parent || !found))
1603 parent_entry = NULL;
1604
1605 if (find_parent && entry_addr.is_initialized() && !cache_entry.get()) {
1606 *match_error = true;
1607 parent_entry = NULL;
1608 }
1609
1610 if (cache_entry.get() && (find_parent || !found))
1611 cache_entry = NULL;
1612
1613 find_parent ? parent_entry.swap(&tmp) : cache_entry.swap(&tmp);
1614 FlushIndex();
1615 return tmp;
1616 }
1617
1618 // This is the actual implementation for OpenNextEntry and OpenPrevEntry.
1619 EntryImpl* BackendImpl::OpenFollowingEntry(bool forward, void** iter) {
1620 if (disabled_)
1621 return NULL;
1622
1623 DCHECK(iter);
1624
1625 const int kListsToSearch = 3;
1626 scoped_refptr<EntryImpl> entries[kListsToSearch];
1627 scoped_ptr<Rankings::Iterator> iterator(
1628 reinterpret_cast<Rankings::Iterator*>(*iter));
1629 *iter = NULL;
1630
1631 if (!iterator.get()) {
1632 iterator.reset(new Rankings::Iterator(&rankings_));
1633 bool ret = false;
1634
1635 // Get an entry from each list.
1636 for (int i = 0; i < kListsToSearch; i++) {
1637 EntryImpl* temp = NULL;
1638 ret |= OpenFollowingEntryFromList(forward, static_cast<Rankings::List>(i),
1639 &iterator->nodes[i], &temp);
1640 entries[i].swap(&temp); // The entry was already addref'd.
1641 }
1642 if (!ret)
1643 return NULL;
1644 } else {
1645 // Get the next entry from the last list, and the actual entries for the
1646 // elements on the other lists.
1647 for (int i = 0; i < kListsToSearch; i++) {
1648 EntryImpl* temp = NULL;
1649 if (iterator->list == i) {
1650 OpenFollowingEntryFromList(forward, iterator->list,
1651 &iterator->nodes[i], &temp);
1652 } else {
1653 temp = GetEnumeratedEntry(iterator->nodes[i],
1654 static_cast<Rankings::List>(i));
1655 }
1656
1657 entries[i].swap(&temp); // The entry was already addref'd.
1658 }
1659 }
1660
1661 int newest = -1;
1662 int oldest = -1;
1663 Time access_times[kListsToSearch];
1664 for (int i = 0; i < kListsToSearch; i++) {
1665 if (entries[i].get()) {
1666 access_times[i] = entries[i]->GetLastUsed();
1667 if (newest < 0) {
1668 DCHECK_LT(oldest, 0);
1669 newest = oldest = i;
1670 continue;
1671 }
1672 if (access_times[i] > access_times[newest])
1673 newest = i;
1674 if (access_times[i] < access_times[oldest])
1675 oldest = i;
1676 }
1677 }
1678
1679 if (newest < 0 || oldest < 0)
1680 return NULL;
1681
1682 EntryImpl* next_entry;
1683 if (forward) {
1684 next_entry = entries[newest].get();
1685 iterator->list = static_cast<Rankings::List>(newest);
1686 } else {
1687 next_entry = entries[oldest].get();
1688 iterator->list = static_cast<Rankings::List>(oldest);
1689 }
1690
1691 *iter = iterator.release();
1692 next_entry->AddRef();
1693 return next_entry;
1694 }
1695
1696 bool BackendImpl::OpenFollowingEntryFromList(bool forward, Rankings::List list,
1697 CacheRankingsBlock** from_entry,
1698 EntryImpl** next_entry) {
1699 if (disabled_)
1700 return false;
1701
1702 if (!new_eviction_ && Rankings::NO_USE != list)
1703 return false;
1704
1705 Rankings::ScopedRankingsBlock rankings(&rankings_, *from_entry);
1706 CacheRankingsBlock* next_block = forward ?
1707 rankings_.GetNext(rankings.get(), list) :
1708 rankings_.GetPrev(rankings.get(), list);
1709 Rankings::ScopedRankingsBlock next(&rankings_, next_block);
1710 *from_entry = NULL;
1711
1712 *next_entry = GetEnumeratedEntry(next.get(), list);
1713 if (!*next_entry)
1714 return false;
1715
1716 *from_entry = next.release();
1717 return true;
1718 }
1719
1720 EntryImpl* BackendImpl::GetEnumeratedEntry(CacheRankingsBlock* next,
1721 Rankings::List list) {
1722 if (!next || disabled_)
1723 return NULL;
1724
1725 EntryImpl* entry;
1726 int rv = NewEntry(Addr(next->Data()->contents), &entry);
1727 if (rv) {
1728 STRESS_NOTREACHED();
1729 rankings_.Remove(next, list, false);
1730 if (rv == ERR_INVALID_ADDRESS) {
1731 // There is nothing linked from the index. Delete the rankings node.
1732 DeleteBlock(next->address(), true);
1733 }
1734 return NULL;
1735 }
1736
1737 if (entry->dirty()) {
1738 // We cannot trust this entry.
1739 InternalDoomEntry(entry);
1740 entry->Release();
1741 return NULL;
1742 }
1743
1744 if (!entry->Update()) {
1745 STRESS_NOTREACHED();
1746 entry->Release();
1747 return NULL;
1748 }
1749
1750 // Note that it is unfortunate (but possible) for this entry to be clean, but
1751 // not actually the real entry. In other words, we could have lost this entry
1752 // from the index, and it could have been replaced with a newer one. It's not
1753 // worth checking that this entry is "the real one", so we just return it and
1754 // let the enumeration continue; this entry will be evicted at some point, and
1755 // the regular path will work with the real entry. With time, this problem
1756 // will disasappear because this scenario is just a bug.
1757
1758 // Make sure that we save the key for later.
1759 entry->GetKey();
1760
1761 return entry;
1762 }
1763
1764 EntryImpl* BackendImpl::ResurrectEntry(EntryImpl* deleted_entry) {
1765 if (ENTRY_NORMAL == deleted_entry->entry()->Data()->state) {
1766 deleted_entry->Release();
1767 stats_.OnEvent(Stats::CREATE_MISS);
1768 Trace("create entry miss ");
1769 return NULL;
1770 }
1771
1772 // We are attempting to create an entry and found out that the entry was
1773 // previously deleted.
1774
1775 eviction_.OnCreateEntry(deleted_entry);
1776 entry_count_++;
1777
1778 stats_.OnEvent(Stats::RESURRECT_HIT);
1779 Trace("Resurrect entry hit ");
1780 return deleted_entry;
1781 }
1782
1783 void BackendImpl::DestroyInvalidEntry(EntryImpl* entry) {
1784 LOG(WARNING) << "Destroying invalid entry.";
1785 Trace("Destroying invalid entry 0x%p", entry);
1786
1787 entry->SetPointerForInvalidEntry(GetCurrentEntryId());
1788
1789 eviction_.OnDoomEntry(entry);
1790 entry->InternalDoom();
1791
1792 if (!new_eviction_)
1793 DecreaseNumEntries();
1794 stats_.OnEvent(Stats::INVALID_ENTRY);
1795 }
1796
1797 void BackendImpl::AddStorageSize(int32 bytes) {
1798 data_->header.num_bytes += bytes;
1799 DCHECK_GE(data_->header.num_bytes, 0);
1800 }
1801
1802 void BackendImpl::SubstractStorageSize(int32 bytes) {
1803 data_->header.num_bytes -= bytes;
1804 DCHECK_GE(data_->header.num_bytes, 0);
1805 }
1806
1807 void BackendImpl::IncreaseNumRefs() {
1808 num_refs_++;
1809 if (max_refs_ < num_refs_)
1810 max_refs_ = num_refs_;
1811 }
1812
1813 void BackendImpl::DecreaseNumRefs() {
1814 DCHECK(num_refs_);
1815 num_refs_--;
1816
1817 if (!num_refs_ && disabled_)
1818 base::MessageLoop::current()->PostTask(
1819 FROM_HERE, base::Bind(&BackendImpl::RestartCache, GetWeakPtr(), true));
1820 }
1821
1822 void BackendImpl::IncreaseNumEntries() {
1823 data_->header.num_entries++;
1824 DCHECK_GT(data_->header.num_entries, 0);
1825 }
1826
1827 void BackendImpl::DecreaseNumEntries() {
1828 data_->header.num_entries--;
1829 if (data_->header.num_entries < 0) {
1830 NOTREACHED();
1831 data_->header.num_entries = 0;
1832 }
1833 }
1834
1835 void BackendImpl::LogStats() {
1836 StatsItems stats;
1837 GetStats(&stats);
1838
1839 for (size_t index = 0; index < stats.size(); index++)
1840 VLOG(1) << stats[index].first << ": " << stats[index].second;
1841 }
1842
1843 void BackendImpl::ReportStats() {
1844 CACHE_UMA(COUNTS, "Entries", 0, data_->header.num_entries);
1845
1846 int current_size = data_->header.num_bytes / (1024 * 1024);
1847 int max_size = max_size_ / (1024 * 1024);
1848 int hit_ratio_as_percentage = stats_.GetHitRatio();
1849
1850 CACHE_UMA(COUNTS_10000, "Size2", 0, current_size);
1851 // For any bin in HitRatioBySize2, the hit ratio of caches of that size is the
1852 // ratio of that bin's total count to the count in the same bin in the Size2
1853 // histogram.
1854 if (base::RandInt(0, 99) < hit_ratio_as_percentage)
1855 CACHE_UMA(COUNTS_10000, "HitRatioBySize2", 0, current_size);
1856 CACHE_UMA(COUNTS_10000, "MaxSize2", 0, max_size);
1857 if (!max_size)
1858 max_size++;
1859 CACHE_UMA(PERCENTAGE, "UsedSpace", 0, current_size * 100 / max_size);
1860
1861 CACHE_UMA(COUNTS_10000, "AverageOpenEntries2", 0,
1862 static_cast<int>(stats_.GetCounter(Stats::OPEN_ENTRIES)));
1863 CACHE_UMA(COUNTS_10000, "MaxOpenEntries2", 0,
1864 static_cast<int>(stats_.GetCounter(Stats::MAX_ENTRIES)));
1865 stats_.SetCounter(Stats::MAX_ENTRIES, 0);
1866
1867 CACHE_UMA(COUNTS_10000, "TotalFatalErrors", 0,
1868 static_cast<int>(stats_.GetCounter(Stats::FATAL_ERROR)));
1869 CACHE_UMA(COUNTS_10000, "TotalDoomCache", 0,
1870 static_cast<int>(stats_.GetCounter(Stats::DOOM_CACHE)));
1871 CACHE_UMA(COUNTS_10000, "TotalDoomRecentEntries", 0,
1872 static_cast<int>(stats_.GetCounter(Stats::DOOM_RECENT)));
1873 stats_.SetCounter(Stats::FATAL_ERROR, 0);
1874 stats_.SetCounter(Stats::DOOM_CACHE, 0);
1875 stats_.SetCounter(Stats::DOOM_RECENT, 0);
1876
1877 int age = (Time::Now() -
1878 Time::FromInternalValue(data_->header.create_time)).InHours();
1879 if (age)
1880 CACHE_UMA(HOURS, "FilesAge", 0, age);
1881
1882 int64 total_hours = stats_.GetCounter(Stats::TIMER) / 120;
1883 if (!data_->header.create_time || !data_->header.lru.filled) {
1884 int cause = data_->header.create_time ? 0 : 1;
1885 if (!data_->header.lru.filled)
1886 cause |= 2;
1887 CACHE_UMA(CACHE_ERROR, "ShortReport", 0, cause);
1888 CACHE_UMA(HOURS, "TotalTimeNotFull", 0, static_cast<int>(total_hours));
1889 return;
1890 }
1891
1892 // This is an up to date client that will report FirstEviction() data. After
1893 // that event, start reporting this:
1894
1895 CACHE_UMA(HOURS, "TotalTime", 0, static_cast<int>(total_hours));
1896 // For any bin in HitRatioByTotalTime, the hit ratio of caches of that total
1897 // time is the ratio of that bin's total count to the count in the same bin in
1898 // the TotalTime histogram.
1899 if (base::RandInt(0, 99) < hit_ratio_as_percentage)
1900 CACHE_UMA(HOURS, "HitRatioByTotalTime", 0, implicit_cast<int>(total_hours));
1901
1902 int64 use_hours = stats_.GetCounter(Stats::LAST_REPORT_TIMER) / 120;
1903 stats_.SetCounter(Stats::LAST_REPORT_TIMER, stats_.GetCounter(Stats::TIMER));
1904
1905 // We may see users with no use_hours at this point if this is the first time
1906 // we are running this code.
1907 if (use_hours)
1908 use_hours = total_hours - use_hours;
1909
1910 if (!use_hours || !GetEntryCount() || !data_->header.num_bytes)
1911 return;
1912
1913 CACHE_UMA(HOURS, "UseTime", 0, static_cast<int>(use_hours));
1914 // For any bin in HitRatioByUseTime, the hit ratio of caches of that use time
1915 // is the ratio of that bin's total count to the count in the same bin in the
1916 // UseTime histogram.
1917 if (base::RandInt(0, 99) < hit_ratio_as_percentage)
1918 CACHE_UMA(HOURS, "HitRatioByUseTime", 0, implicit_cast<int>(use_hours));
1919 CACHE_UMA(PERCENTAGE, "HitRatio", 0, hit_ratio_as_percentage);
1920
1921 int64 trim_rate = stats_.GetCounter(Stats::TRIM_ENTRY) / use_hours;
1922 CACHE_UMA(COUNTS, "TrimRate", 0, static_cast<int>(trim_rate));
1923
1924 int avg_size = data_->header.num_bytes / GetEntryCount();
1925 CACHE_UMA(COUNTS, "EntrySize", 0, avg_size);
1926 CACHE_UMA(COUNTS, "EntriesFull", 0, data_->header.num_entries);
1927
1928 CACHE_UMA(PERCENTAGE, "IndexLoad", 0,
1929 data_->header.num_entries * 100 / (mask_ + 1));
1930
1931 int large_entries_bytes = stats_.GetLargeEntriesSize();
1932 int large_ratio = large_entries_bytes * 100 / data_->header.num_bytes;
1933 CACHE_UMA(PERCENTAGE, "LargeEntriesRatio", 0, large_ratio);
1934
1935 if (new_eviction_) {
1936 CACHE_UMA(PERCENTAGE, "ResurrectRatio", 0, stats_.GetResurrectRatio());
1937 CACHE_UMA(PERCENTAGE, "NoUseRatio", 0,
1938 data_->header.lru.sizes[0] * 100 / data_->header.num_entries);
1939 CACHE_UMA(PERCENTAGE, "LowUseRatio", 0,
1940 data_->header.lru.sizes[1] * 100 / data_->header.num_entries);
1941 CACHE_UMA(PERCENTAGE, "HighUseRatio", 0,
1942 data_->header.lru.sizes[2] * 100 / data_->header.num_entries);
1943 CACHE_UMA(PERCENTAGE, "DeletedRatio", 0,
1944 data_->header.lru.sizes[4] * 100 / data_->header.num_entries);
1945 }
1946
1947 stats_.ResetRatios();
1948 stats_.SetCounter(Stats::TRIM_ENTRY, 0);
1949
1950 if (cache_type_ == net::DISK_CACHE)
1951 block_files_.ReportStats();
1952 }
1953
1954 void BackendImpl::UpgradeTo2_1() {
1955 // 2.1 is basically the same as 2.0, except that new fields are actually
1956 // updated by the new eviction algorithm.
1957 DCHECK(0x20000 == data_->header.version);
1958 data_->header.version = 0x20001;
1959 data_->header.lru.sizes[Rankings::NO_USE] = data_->header.num_entries;
1960 }
1961
1962 bool BackendImpl::CheckIndex() {
1963 DCHECK(data_);
1964
1965 size_t current_size = index_->GetLength();
1966 if (current_size < sizeof(Index)) {
1967 LOG(ERROR) << "Corrupt Index file";
1968 return false;
1969 }
1970
1971 if (new_eviction_) {
1972 // We support versions 2.0 and 2.1, upgrading 2.0 to 2.1.
1973 if (kIndexMagic != data_->header.magic ||
1974 kCurrentVersion >> 16 != data_->header.version >> 16) {
1975 LOG(ERROR) << "Invalid file version or magic";
1976 return false;
1977 }
1978 if (kCurrentVersion == data_->header.version) {
1979 // We need file version 2.1 for the new eviction algorithm.
1980 UpgradeTo2_1();
1981 }
1982 } else {
1983 if (kIndexMagic != data_->header.magic ||
1984 kCurrentVersion != data_->header.version) {
1985 LOG(ERROR) << "Invalid file version or magic";
1986 return false;
1987 }
1988 }
1989
1990 if (!data_->header.table_len) {
1991 LOG(ERROR) << "Invalid table size";
1992 return false;
1993 }
1994
1995 if (current_size < GetIndexSize(data_->header.table_len) ||
1996 data_->header.table_len & (kBaseTableLen - 1)) {
1997 LOG(ERROR) << "Corrupt Index file";
1998 return false;
1999 }
2000
2001 AdjustMaxCacheSize(data_->header.table_len);
2002
2003 #if !defined(NET_BUILD_STRESS_CACHE)
2004 if (data_->header.num_bytes < 0 ||
2005 (max_size_ < kint32max - kDefaultCacheSize &&
2006 data_->header.num_bytes > max_size_ + kDefaultCacheSize)) {
2007 LOG(ERROR) << "Invalid cache (current) size";
2008 return false;
2009 }
2010 #endif
2011
2012 if (data_->header.num_entries < 0) {
2013 LOG(ERROR) << "Invalid number of entries";
2014 return false;
2015 }
2016
2017 if (!mask_)
2018 mask_ = data_->header.table_len - 1;
2019
2020 // Load the table into memory with a single read.
2021 scoped_ptr<char[]> buf(new char[current_size]);
2022 return index_->Read(buf.get(), current_size, 0);
2023 }
2024
2025 int BackendImpl::CheckAllEntries() {
2026 int num_dirty = 0;
2027 int num_entries = 0;
2028 DCHECK(mask_ < kuint32max);
2029 for (unsigned int i = 0; i <= mask_; i++) {
2030 Addr address(data_->table[i]);
2031 if (!address.is_initialized())
2032 continue;
2033 for (;;) {
2034 EntryImpl* tmp;
2035 int ret = NewEntry(address, &tmp);
2036 if (ret) {
2037 STRESS_NOTREACHED();
2038 return ret;
2039 }
2040 scoped_refptr<EntryImpl> cache_entry;
2041 cache_entry.swap(&tmp);
2042
2043 if (cache_entry->dirty())
2044 num_dirty++;
2045 else if (CheckEntry(cache_entry.get()))
2046 num_entries++;
2047 else
2048 return ERR_INVALID_ENTRY;
2049
2050 DCHECK_EQ(i, cache_entry->entry()->Data()->hash & mask_);
2051 address.set_value(cache_entry->GetNextAddress());
2052 if (!address.is_initialized())
2053 break;
2054 }
2055 }
2056
2057 Trace("CheckAllEntries End");
2058 if (num_entries + num_dirty != data_->header.num_entries) {
2059 LOG(ERROR) << "Number of entries " << num_entries << " " << num_dirty <<
2060 " " << data_->header.num_entries;
2061 DCHECK_LT(num_entries, data_->header.num_entries);
2062 return ERR_NUM_ENTRIES_MISMATCH;
2063 }
2064
2065 return num_dirty;
2066 }
2067
2068 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) {
2069 bool ok = block_files_.IsValid(cache_entry->entry()->address());
2070 ok = ok && block_files_.IsValid(cache_entry->rankings()->address());
2071 EntryStore* data = cache_entry->entry()->Data();
2072 for (size_t i = 0; i < arraysize(data->data_addr); i++) {
2073 if (data->data_addr[i]) {
2074 Addr address(data->data_addr[i]);
2075 if (address.is_block_file())
2076 ok = ok && block_files_.IsValid(address);
2077 }
2078 }
2079
2080 return ok && cache_entry->rankings()->VerifyHash();
2081 }
2082
2083 int BackendImpl::MaxBuffersSize() {
2084 static int64 total_memory = base::SysInfo::AmountOfPhysicalMemory();
2085 static bool done = false;
2086
2087 if (!done) {
2088 const int kMaxBuffersSize = 30 * 1024 * 1024;
2089
2090 // We want to use up to 2% of the computer's memory.
2091 total_memory = total_memory * 2 / 100;
2092 if (total_memory > kMaxBuffersSize || total_memory <= 0)
2093 total_memory = kMaxBuffersSize;
2094
2095 done = true;
2096 }
2097
2098 return static_cast<int>(total_memory);
2099 }
2100
2101 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698