OLD | NEW |
| (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/v3/backend_impl_v3.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/errors.h" | |
26 #include "net/disk_cache/experiments.h" | |
27 #include "net/disk_cache/file.h" | |
28 #include "net/disk_cache/storage_block-inl.h" | |
29 #include "net/disk_cache/v3/disk_format_v3.h" | |
30 #include "net/disk_cache/v3/entry_impl_v3.h" | |
31 #include "net/disk_cache/v3/index_table.h" | |
32 | |
33 #define CACHE_HISTOGRAM_MACROS_BACKEND_OBJ this | |
34 #include "net/disk_cache/v3/histogram_macros.h" | |
35 | |
36 using base::Time; | |
37 using base::TimeDelta; | |
38 using base::TimeTicks; | |
39 | |
40 namespace { | |
41 | |
42 #if defined(V3_NOT_JUST_YET_READY) | |
43 const int kDefaultCacheSize = 80 * 1024 * 1024; | |
44 | |
45 // Avoid trimming the cache for the first 5 minutes (10 timer ticks). | |
46 const int kTrimDelay = 10; | |
47 #endif // defined(V3_NOT_JUST_YET_READY). | |
48 | |
49 } // namespace | |
50 | |
51 // ------------------------------------------------------------------------ | |
52 | |
53 namespace disk_cache { | |
54 | |
55 BackendImplV3::BackendImplV3(const base::FilePath& path, | |
56 base::MessageLoopProxy* cache_thread, | |
57 net::NetLog* net_log) | |
58 : index_(NULL), | |
59 path_(path), | |
60 block_files_(), | |
61 max_size_(0), | |
62 up_ticks_(0), | |
63 cache_type_(net::DISK_CACHE), | |
64 uma_report_(0), | |
65 user_flags_(0), | |
66 init_(false), | |
67 restarted_(false), | |
68 read_only_(false), | |
69 disabled_(false), | |
70 lru_eviction_(true), | |
71 first_timer_(true), | |
72 user_load_(false), | |
73 net_log_(net_log), | |
74 ptr_factory_(this) { | |
75 } | |
76 | |
77 BackendImplV3::~BackendImplV3() { | |
78 CleanupCache(); | |
79 } | |
80 | |
81 int BackendImplV3::Init(const CompletionCallback& callback) { | |
82 DCHECK(!init_); | |
83 if (init_) | |
84 return net::ERR_FAILED; | |
85 | |
86 return net::ERR_IO_PENDING; | |
87 } | |
88 | |
89 // ------------------------------------------------------------------------ | |
90 | |
91 #if defined(V3_NOT_JUST_YET_READY) | |
92 int BackendImplV3::OpenPrevEntry(void** iter, Entry** prev_entry, | |
93 const CompletionCallback& callback) { | |
94 DCHECK(!callback.is_null()); | |
95 return OpenFollowingEntry(true, iter, prev_entry, callback); | |
96 } | |
97 #endif // defined(V3_NOT_JUST_YET_READY). | |
98 | |
99 bool BackendImplV3::SetMaxSize(int max_bytes) { | |
100 COMPILE_ASSERT(sizeof(max_bytes) == sizeof(max_size_), unsupported_int_model); | |
101 if (max_bytes < 0) | |
102 return false; | |
103 | |
104 // Zero size means use the default. | |
105 if (!max_bytes) | |
106 return true; | |
107 | |
108 // Avoid a DCHECK later on. | |
109 if (max_bytes >= kint32max - kint32max / 10) | |
110 max_bytes = kint32max - kint32max / 10 - 1; | |
111 | |
112 user_flags_ |= MAX_SIZE; | |
113 max_size_ = max_bytes; | |
114 return true; | |
115 } | |
116 | |
117 void BackendImplV3::SetType(net::CacheType type) { | |
118 DCHECK_NE(net::MEMORY_CACHE, type); | |
119 cache_type_ = type; | |
120 } | |
121 | |
122 bool BackendImplV3::CreateBlock(FileType block_type, int block_count, | |
123 Addr* block_address) { | |
124 return block_files_.CreateBlock(block_type, block_count, block_address); | |
125 } | |
126 | |
127 #if defined(V3_NOT_JUST_YET_READY) | |
128 void BackendImplV3::UpdateRank(EntryImplV3* entry, bool modified) { | |
129 if (read_only_ || (!modified && cache_type() == net::SHADER_CACHE)) | |
130 return; | |
131 eviction_.UpdateRank(entry, modified); | |
132 } | |
133 | |
134 void BackendImplV3::InternalDoomEntry(EntryImplV3* entry) { | |
135 uint32 hash = entry->GetHash(); | |
136 std::string key = entry->GetKey(); | |
137 Addr entry_addr = entry->entry()->address(); | |
138 bool error; | |
139 EntryImpl* parent_entry = MatchEntry(key, hash, true, entry_addr, &error); | |
140 CacheAddr child(entry->GetNextAddress()); | |
141 | |
142 Trace("Doom entry 0x%p", entry); | |
143 | |
144 if (!entry->doomed()) { | |
145 // We may have doomed this entry from within MatchEntry. | |
146 eviction_.OnDoomEntry(entry); | |
147 entry->InternalDoom(); | |
148 if (!new_eviction_) { | |
149 DecreaseNumEntries(); | |
150 } | |
151 stats_.OnEvent(Stats::DOOM_ENTRY); | |
152 } | |
153 | |
154 if (parent_entry) { | |
155 parent_entry->SetNextAddress(Addr(child)); | |
156 parent_entry->Release(); | |
157 } else if (!error) { | |
158 data_->table[hash & mask_] = child; | |
159 } | |
160 | |
161 FlushIndex(); | |
162 } | |
163 | |
164 void BackendImplV3::OnEntryDestroyBegin(Addr address) { | |
165 EntriesMap::iterator it = open_entries_.find(address.value()); | |
166 if (it != open_entries_.end()) | |
167 open_entries_.erase(it); | |
168 } | |
169 | |
170 void BackendImplV3::OnEntryDestroyEnd() { | |
171 DecreaseNumRefs(); | |
172 if (data_->header.num_bytes > max_size_ && !read_only_ && | |
173 (up_ticks_ > kTrimDelay || user_flags_ & kNoRandom)) | |
174 eviction_.TrimCache(false); | |
175 } | |
176 | |
177 EntryImplV3* BackendImplV3::GetOpenEntry(Addr address) const { | |
178 DCHECK(rankings->HasData()); | |
179 EntriesMap::const_iterator it = | |
180 open_entries_.find(rankings->Data()->contents); | |
181 if (it != open_entries_.end()) { | |
182 // We have this entry in memory. | |
183 return it->second; | |
184 } | |
185 | |
186 return NULL; | |
187 } | |
188 | |
189 int BackendImplV3::MaxFileSize() const { | |
190 return max_size_ / 8; | |
191 } | |
192 | |
193 void BackendImplV3::ModifyStorageSize(int32 old_size, int32 new_size) { | |
194 if (disabled_ || old_size == new_size) | |
195 return; | |
196 if (old_size > new_size) | |
197 SubstractStorageSize(old_size - new_size); | |
198 else | |
199 AddStorageSize(new_size - old_size); | |
200 | |
201 // Update the usage statistics. | |
202 stats_.ModifyStorageStats(old_size, new_size); | |
203 } | |
204 | |
205 void BackendImplV3::TooMuchStorageRequested(int32 size) { | |
206 stats_.ModifyStorageStats(0, size); | |
207 } | |
208 | |
209 bool BackendImplV3::IsAllocAllowed(int current_size, int new_size) { | |
210 DCHECK_GT(new_size, current_size); | |
211 if (user_flags_ & NO_BUFFERING) | |
212 return false; | |
213 | |
214 int to_add = new_size - current_size; | |
215 if (buffer_bytes_ + to_add > MaxBuffersSize()) | |
216 return false; | |
217 | |
218 buffer_bytes_ += to_add; | |
219 CACHE_UMA(COUNTS_50000, "BufferBytes", buffer_bytes_ / 1024); | |
220 return true; | |
221 } | |
222 #endif // defined(V3_NOT_JUST_YET_READY). | |
223 | |
224 void BackendImplV3::BufferDeleted(int size) { | |
225 DCHECK_GE(size, 0); | |
226 buffer_bytes_ -= size; | |
227 DCHECK_GE(buffer_bytes_, 0); | |
228 } | |
229 | |
230 bool BackendImplV3::IsLoaded() const { | |
231 if (user_flags_ & NO_LOAD_PROTECTION) | |
232 return false; | |
233 | |
234 return user_load_; | |
235 } | |
236 | |
237 std::string BackendImplV3::HistogramName(const char* name) const { | |
238 static const char* names[] = { "Http", "", "Media", "AppCache", "Shader" }; | |
239 DCHECK_NE(cache_type_, net::MEMORY_CACHE); | |
240 return base::StringPrintf("DiskCache3.%s_%s", name, names[cache_type_]); | |
241 } | |
242 | |
243 base::WeakPtr<BackendImplV3> BackendImplV3::GetWeakPtr() { | |
244 return ptr_factory_.GetWeakPtr(); | |
245 } | |
246 | |
247 #if defined(V3_NOT_JUST_YET_READY) | |
248 // We want to remove biases from some histograms so we only send data once per | |
249 // week. | |
250 bool BackendImplV3::ShouldReportAgain() { | |
251 if (uma_report_) | |
252 return uma_report_ == 2; | |
253 | |
254 uma_report_++; | |
255 int64 last_report = stats_.GetCounter(Stats::LAST_REPORT); | |
256 Time last_time = Time::FromInternalValue(last_report); | |
257 if (!last_report || (Time::Now() - last_time).InDays() >= 7) { | |
258 stats_.SetCounter(Stats::LAST_REPORT, Time::Now().ToInternalValue()); | |
259 uma_report_++; | |
260 return true; | |
261 } | |
262 return false; | |
263 } | |
264 | |
265 void BackendImplV3::FirstEviction() { | |
266 IndexHeaderV3* header = index_.header(); | |
267 header->flags |= CACHE_EVICTED; | |
268 DCHECK(header->create_time); | |
269 if (!GetEntryCount()) | |
270 return; // This is just for unit tests. | |
271 | |
272 Time create_time = Time::FromInternalValue(header->create_time); | |
273 CACHE_UMA(AGE, "FillupAge", create_time); | |
274 | |
275 int64 use_time = stats_.GetCounter(Stats::TIMER); | |
276 CACHE_UMA(HOURS, "FillupTime", static_cast<int>(use_time / 120)); | |
277 CACHE_UMA(PERCENTAGE, "FirstHitRatio", stats_.GetHitRatio()); | |
278 | |
279 if (!use_time) | |
280 use_time = 1; | |
281 CACHE_UMA(COUNTS_10000, "FirstEntryAccessRate", | |
282 static_cast<int>(header->num_entries / use_time)); | |
283 CACHE_UMA(COUNTS, "FirstByteIORate", | |
284 static_cast<int>((header->num_bytes / 1024) / use_time)); | |
285 | |
286 int avg_size = header->num_bytes / GetEntryCount(); | |
287 CACHE_UMA(COUNTS, "FirstEntrySize", avg_size); | |
288 | |
289 int large_entries_bytes = stats_.GetLargeEntriesSize(); | |
290 int large_ratio = large_entries_bytes * 100 / header->num_bytes; | |
291 CACHE_UMA(PERCENTAGE, "FirstLargeEntriesRatio", large_ratio); | |
292 | |
293 if (!lru_eviction_) { | |
294 CACHE_UMA(PERCENTAGE, "FirstResurrectRatio", stats_.GetResurrectRatio()); | |
295 CACHE_UMA(PERCENTAGE, "FirstNoUseRatio", | |
296 header->num_no_use_entries * 100 / header->num_entries); | |
297 CACHE_UMA(PERCENTAGE, "FirstLowUseRatio", | |
298 header->num_low_use_entries * 100 / header->num_entries); | |
299 CACHE_UMA(PERCENTAGE, "FirstHighUseRatio", | |
300 header->num_high_use_entries * 100 / header->num_entries); | |
301 } | |
302 | |
303 stats_.ResetRatios(); | |
304 } | |
305 | |
306 void BackendImplV3::OnEvent(Stats::Counters an_event) { | |
307 stats_.OnEvent(an_event); | |
308 } | |
309 | |
310 void BackendImplV3::OnRead(int32 bytes) { | |
311 DCHECK_GE(bytes, 0); | |
312 byte_count_ += bytes; | |
313 if (byte_count_ < 0) | |
314 byte_count_ = kint32max; | |
315 } | |
316 | |
317 void BackendImplV3::OnWrite(int32 bytes) { | |
318 // We use the same implementation as OnRead... just log the number of bytes. | |
319 OnRead(bytes); | |
320 } | |
321 | |
322 void BackendImplV3::OnTimerTick() { | |
323 stats_.OnEvent(Stats::TIMER); | |
324 int64 time = stats_.GetCounter(Stats::TIMER); | |
325 int64 current = stats_.GetCounter(Stats::OPEN_ENTRIES); | |
326 | |
327 // OPEN_ENTRIES is a sampled average of the number of open entries, avoiding | |
328 // the bias towards 0. | |
329 if (num_refs_ && (current != num_refs_)) { | |
330 int64 diff = (num_refs_ - current) / 50; | |
331 if (!diff) | |
332 diff = num_refs_ > current ? 1 : -1; | |
333 current = current + diff; | |
334 stats_.SetCounter(Stats::OPEN_ENTRIES, current); | |
335 stats_.SetCounter(Stats::MAX_ENTRIES, max_refs_); | |
336 } | |
337 | |
338 CACHE_UMA(COUNTS, "NumberOfReferences", num_refs_); | |
339 | |
340 CACHE_UMA(COUNTS_10000, "EntryAccessRate", entry_count_); | |
341 CACHE_UMA(COUNTS, "ByteIORate", byte_count_ / 1024); | |
342 | |
343 // These values cover about 99.5% of the population (Oct 2011). | |
344 user_load_ = (entry_count_ > 300 || byte_count_ > 7 * 1024 * 1024); | |
345 entry_count_ = 0; | |
346 byte_count_ = 0; | |
347 up_ticks_++; | |
348 | |
349 if (!data_) | |
350 first_timer_ = false; | |
351 if (first_timer_) { | |
352 first_timer_ = false; | |
353 if (ShouldReportAgain()) | |
354 ReportStats(); | |
355 } | |
356 | |
357 // Save stats to disk at 5 min intervals. | |
358 if (time % 10 == 0) | |
359 StoreStats(); | |
360 } | |
361 | |
362 void BackendImplV3::SetUnitTestMode() { | |
363 user_flags_ |= UNIT_TEST_MODE; | |
364 } | |
365 | |
366 void BackendImplV3::SetUpgradeMode() { | |
367 user_flags_ |= UPGRADE_MODE; | |
368 read_only_ = true; | |
369 } | |
370 | |
371 void BackendImplV3::SetNewEviction() { | |
372 user_flags_ |= EVICTION_V2; | |
373 lru_eviction_ = false; | |
374 } | |
375 | |
376 void BackendImplV3::SetFlags(uint32 flags) { | |
377 user_flags_ |= flags; | |
378 } | |
379 | |
380 int BackendImplV3::FlushQueueForTest(const CompletionCallback& callback) { | |
381 background_queue_.FlushQueue(callback); | |
382 return net::ERR_IO_PENDING; | |
383 } | |
384 | |
385 void BackendImplV3::TrimForTest(bool empty) { | |
386 eviction_.SetTestMode(); | |
387 eviction_.TrimCache(empty); | |
388 } | |
389 | |
390 void BackendImplV3::TrimDeletedListForTest(bool empty) { | |
391 eviction_.SetTestMode(); | |
392 eviction_.TrimDeletedList(empty); | |
393 } | |
394 | |
395 int BackendImplV3::SelfCheck() { | |
396 if (!init_) { | |
397 LOG(ERROR) << "Init failed"; | |
398 return ERR_INIT_FAILED; | |
399 } | |
400 | |
401 int num_entries = rankings_.SelfCheck(); | |
402 if (num_entries < 0) { | |
403 LOG(ERROR) << "Invalid rankings list, error " << num_entries; | |
404 #if !defined(NET_BUILD_STRESS_CACHE) | |
405 return num_entries; | |
406 #endif | |
407 } | |
408 | |
409 if (num_entries != data_->header.num_entries) { | |
410 LOG(ERROR) << "Number of entries mismatch"; | |
411 #if !defined(NET_BUILD_STRESS_CACHE) | |
412 return ERR_NUM_ENTRIES_MISMATCH; | |
413 #endif | |
414 } | |
415 | |
416 return CheckAllEntries(); | |
417 } | |
418 | |
419 // ------------------------------------------------------------------------ | |
420 | |
421 net::CacheType BackendImplV3::GetCacheType() const { | |
422 return cache_type_; | |
423 } | |
424 | |
425 int32 BackendImplV3::GetEntryCount() const { | |
426 if (disabled_) | |
427 return 0; | |
428 DCHECK(init_); | |
429 return index_.header()->num_entries; | |
430 } | |
431 | |
432 int BackendImplV3::OpenEntry(const std::string& key, Entry** entry, | |
433 const CompletionCallback& callback) { | |
434 if (disabled_) | |
435 return NULL; | |
436 | |
437 TimeTicks start = TimeTicks::Now(); | |
438 uint32 hash = base::Hash(key); | |
439 Trace("Open hash 0x%x", hash); | |
440 | |
441 bool error; | |
442 EntryImpl* cache_entry = MatchEntry(key, hash, false, Addr(), &error); | |
443 if (cache_entry && ENTRY_NORMAL != cache_entry->entry()->Data()->state) { | |
444 // The entry was already evicted. | |
445 cache_entry->Release(); | |
446 cache_entry = NULL; | |
447 } | |
448 | |
449 int current_size = data_->header.num_bytes / (1024 * 1024); | |
450 int64 total_hours = stats_.GetCounter(Stats::TIMER) / 120; | |
451 int64 no_use_hours = stats_.GetCounter(Stats::LAST_REPORT_TIMER) / 120; | |
452 int64 use_hours = total_hours - no_use_hours; | |
453 | |
454 if (!cache_entry) { | |
455 CACHE_UMA(AGE_MS, "OpenTime.Miss", 0, start); | |
456 CACHE_UMA(COUNTS_10000, "AllOpenBySize.Miss", 0, current_size); | |
457 CACHE_UMA(HOURS, "AllOpenByTotalHours.Miss", 0, total_hours); | |
458 CACHE_UMA(HOURS, "AllOpenByUseHours.Miss", 0, use_hours); | |
459 stats_.OnEvent(Stats::OPEN_MISS); | |
460 return NULL; | |
461 } | |
462 | |
463 eviction_.OnOpenEntry(cache_entry); | |
464 entry_count_++; | |
465 | |
466 Trace("Open hash 0x%x end: 0x%x", hash, | |
467 cache_entry->entry()->address().value()); | |
468 CACHE_UMA(AGE_MS, "OpenTime", 0, start); | |
469 CACHE_UMA(COUNTS_10000, "AllOpenBySize.Hit", 0, current_size); | |
470 CACHE_UMA(HOURS, "AllOpenByTotalHours.Hit", 0, total_hours); | |
471 CACHE_UMA(HOURS, "AllOpenByUseHours.Hit", 0, use_hours); | |
472 stats_.OnEvent(Stats::OPEN_HIT); | |
473 SIMPLE_STATS_COUNTER("disk_cache.hit"); | |
474 return cache_entry; | |
475 } | |
476 | |
477 int BackendImplV3::CreateEntry(const std::string& key, Entry** entry, | |
478 const CompletionCallback& callback) { | |
479 if (disabled_ || key.empty()) | |
480 return NULL; | |
481 | |
482 TimeTicks start = TimeTicks::Now(); | |
483 Trace("Create hash 0x%x", hash); | |
484 | |
485 scoped_refptr<EntryImpl> parent; | |
486 Addr entry_address(data_->table[hash & mask_]); | |
487 if (entry_address.is_initialized()) { | |
488 // We have an entry already. It could be the one we are looking for, or just | |
489 // a hash conflict. | |
490 bool error; | |
491 EntryImpl* old_entry = MatchEntry(key, hash, false, Addr(), &error); | |
492 if (old_entry) | |
493 return ResurrectEntry(old_entry); | |
494 | |
495 EntryImpl* parent_entry = MatchEntry(key, hash, true, Addr(), &error); | |
496 DCHECK(!error); | |
497 if (parent_entry) { | |
498 parent.swap(&parent_entry); | |
499 } else if (data_->table[hash & mask_]) { | |
500 // We should have corrected the problem. | |
501 NOTREACHED(); | |
502 return NULL; | |
503 } | |
504 } | |
505 | |
506 // The general flow is to allocate disk space and initialize the entry data, | |
507 // followed by saving that to disk, then linking the entry though the index | |
508 // and finally through the lists. If there is a crash in this process, we may | |
509 // end up with: | |
510 // a. Used, unreferenced empty blocks on disk (basically just garbage). | |
511 // b. Used, unreferenced but meaningful data on disk (more garbage). | |
512 // c. A fully formed entry, reachable only through the index. | |
513 // d. A fully formed entry, also reachable through the lists, but still dirty. | |
514 // | |
515 // Anything after (b) can be automatically cleaned up. We may consider saving | |
516 // the current operation (as we do while manipulating the lists) so that we | |
517 // can detect and cleanup (a) and (b). | |
518 | |
519 int num_blocks = EntryImpl::NumBlocksForEntry(key.size()); | |
520 if (!block_files_.CreateBlock(BLOCK_256, num_blocks, &entry_address)) { | |
521 LOG(ERROR) << "Create entry failed " << key.c_str(); | |
522 stats_.OnEvent(Stats::CREATE_ERROR); | |
523 return NULL; | |
524 } | |
525 | |
526 Addr node_address(0); | |
527 if (!block_files_.CreateBlock(RANKINGS, 1, &node_address)) { | |
528 block_files_.DeleteBlock(entry_address, false); | |
529 LOG(ERROR) << "Create entry failed " << key.c_str(); | |
530 stats_.OnEvent(Stats::CREATE_ERROR); | |
531 return NULL; | |
532 } | |
533 | |
534 scoped_refptr<EntryImpl> cache_entry( | |
535 new EntryImpl(this, entry_address, false)); | |
536 IncreaseNumRefs(); | |
537 | |
538 if (!cache_entry->CreateEntry(node_address, key, hash)) { | |
539 block_files_.DeleteBlock(entry_address, false); | |
540 block_files_.DeleteBlock(node_address, false); | |
541 LOG(ERROR) << "Create entry failed " << key.c_str(); | |
542 stats_.OnEvent(Stats::CREATE_ERROR); | |
543 return NULL; | |
544 } | |
545 | |
546 cache_entry->BeginLogging(net_log_, true); | |
547 | |
548 // We are not failing the operation; let's add this to the map. | |
549 open_entries_[entry_address.value()] = cache_entry.get(); | |
550 | |
551 // Save the entry. | |
552 cache_entry->entry()->Store(); | |
553 cache_entry->rankings()->Store(); | |
554 IncreaseNumEntries(); | |
555 entry_count_++; | |
556 | |
557 // Link this entry through the index. | |
558 if (parent.get()) { | |
559 parent->SetNextAddress(entry_address); | |
560 } else { | |
561 data_->table[hash & mask_] = entry_address.value(); | |
562 } | |
563 | |
564 // Link this entry through the lists. | |
565 eviction_.OnCreateEntry(cache_entry.get()); | |
566 | |
567 CACHE_UMA(AGE_MS, "CreateTime", 0, start); | |
568 stats_.OnEvent(Stats::CREATE_HIT); | |
569 SIMPLE_STATS_COUNTER("disk_cache.miss"); | |
570 Trace("create entry hit "); | |
571 FlushIndex(); | |
572 cache_entry->AddRef(); | |
573 return cache_entry.get(); | |
574 } | |
575 | |
576 int BackendImplV3::DoomEntry(const std::string& key, | |
577 const CompletionCallback& callback) { | |
578 if (disabled_) | |
579 return net::ERR_FAILED; | |
580 | |
581 EntryImpl* entry = OpenEntryImpl(key); | |
582 if (!entry) | |
583 return net::ERR_FAILED; | |
584 | |
585 entry->DoomImpl(); | |
586 entry->Release(); | |
587 return net::OK; | |
588 } | |
589 | |
590 int BackendImplV3::DoomAllEntries(const CompletionCallback& callback) { | |
591 // This is not really an error, but it is an interesting condition. | |
592 ReportError(ERR_CACHE_DOOMED); | |
593 stats_.OnEvent(Stats::DOOM_CACHE); | |
594 if (!num_refs_) { | |
595 RestartCache(false); | |
596 return disabled_ ? net::ERR_FAILED : net::OK; | |
597 } else { | |
598 if (disabled_) | |
599 return net::ERR_FAILED; | |
600 | |
601 eviction_.TrimCache(true); | |
602 return net::OK; | |
603 } | |
604 } | |
605 | |
606 int BackendImplV3::DoomEntriesBetween(base::Time initial_time, | |
607 base::Time end_time, | |
608 const CompletionCallback& callback) { | |
609 DCHECK_NE(net::APP_CACHE, cache_type_); | |
610 if (end_time.is_null()) | |
611 return SyncDoomEntriesSince(initial_time); | |
612 | |
613 DCHECK(end_time >= initial_time); | |
614 | |
615 if (disabled_) | |
616 return net::ERR_FAILED; | |
617 | |
618 EntryImpl* node; | |
619 void* iter = NULL; | |
620 EntryImpl* next = OpenNextEntryImpl(&iter); | |
621 if (!next) | |
622 return net::OK; | |
623 | |
624 while (next) { | |
625 node = next; | |
626 next = OpenNextEntryImpl(&iter); | |
627 | |
628 if (node->GetLastUsed() >= initial_time && | |
629 node->GetLastUsed() < end_time) { | |
630 node->DoomImpl(); | |
631 } else if (node->GetLastUsed() < initial_time) { | |
632 if (next) | |
633 next->Release(); | |
634 next = NULL; | |
635 SyncEndEnumeration(iter); | |
636 } | |
637 | |
638 node->Release(); | |
639 } | |
640 | |
641 return net::OK; | |
642 } | |
643 | |
644 int BackendImplV3::DoomEntriesSince(base::Time initial_time, | |
645 const CompletionCallback& callback) { | |
646 DCHECK_NE(net::APP_CACHE, cache_type_); | |
647 if (disabled_) | |
648 return net::ERR_FAILED; | |
649 | |
650 stats_.OnEvent(Stats::DOOM_RECENT); | |
651 for (;;) { | |
652 void* iter = NULL; | |
653 EntryImpl* entry = OpenNextEntryImpl(&iter); | |
654 if (!entry) | |
655 return net::OK; | |
656 | |
657 if (initial_time > entry->GetLastUsed()) { | |
658 entry->Release(); | |
659 SyncEndEnumeration(iter); | |
660 return net::OK; | |
661 } | |
662 | |
663 entry->DoomImpl(); | |
664 entry->Release(); | |
665 SyncEndEnumeration(iter); // Dooming the entry invalidates the iterator. | |
666 } | |
667 } | |
668 | |
669 int BackendImplV3::OpenNextEntry(void** iter, Entry** next_entry, | |
670 const CompletionCallback& callback) { | |
671 DCHECK(!callback.is_null()); | |
672 background_queue_.OpenNextEntry(iter, next_entry, callback); | |
673 return net::ERR_IO_PENDING; | |
674 } | |
675 | |
676 void BackendImplV3::EndEnumeration(void** iter) { | |
677 scoped_ptr<IndexIterator> iterator( | |
678 reinterpret_cast<IndexIterator*>(*iter)); | |
679 *iter = NULL; | |
680 } | |
681 | |
682 void BackendImplV3::GetStats(StatsItems* stats) { | |
683 if (disabled_) | |
684 return; | |
685 | |
686 std::pair<std::string, std::string> item; | |
687 | |
688 item.first = "Entries"; | |
689 item.second = base::StringPrintf("%d", data_->header.num_entries); | |
690 stats->push_back(item); | |
691 | |
692 item.first = "Pending IO"; | |
693 item.second = base::StringPrintf("%d", num_pending_io_); | |
694 stats->push_back(item); | |
695 | |
696 item.first = "Max size"; | |
697 item.second = base::StringPrintf("%d", max_size_); | |
698 stats->push_back(item); | |
699 | |
700 item.first = "Current size"; | |
701 item.second = base::StringPrintf("%d", data_->header.num_bytes); | |
702 stats->push_back(item); | |
703 | |
704 item.first = "Cache type"; | |
705 item.second = "Blockfile Cache"; | |
706 stats->push_back(item); | |
707 | |
708 stats_.GetItems(stats); | |
709 } | |
710 | |
711 void BackendImplV3::OnExternalCacheHit(const std::string& key) { | |
712 if (disabled_) | |
713 return; | |
714 | |
715 uint32 hash = base::Hash(key); | |
716 bool error; | |
717 EntryImpl* cache_entry = MatchEntry(key, hash, false, Addr(), &error); | |
718 if (cache_entry) { | |
719 if (ENTRY_NORMAL == cache_entry->entry()->Data()->state) { | |
720 UpdateRank(cache_entry, cache_type() == net::SHADER_CACHE); | |
721 } | |
722 cache_entry->Release(); | |
723 } | |
724 } | |
725 | |
726 // ------------------------------------------------------------------------ | |
727 | |
728 // The maximum cache size will be either set explicitly by the caller, or | |
729 // calculated by this code. | |
730 void BackendImplV3::AdjustMaxCacheSize(int table_len) { | |
731 if (max_size_) | |
732 return; | |
733 | |
734 // If table_len is provided, the index file exists. | |
735 DCHECK(!table_len || data_->header.magic); | |
736 | |
737 // The user is not setting the size, let's figure it out. | |
738 int64 available = base::SysInfo::AmountOfFreeDiskSpace(path_); | |
739 if (available < 0) { | |
740 max_size_ = kDefaultCacheSize; | |
741 return; | |
742 } | |
743 | |
744 if (table_len) | |
745 available += data_->header.num_bytes; | |
746 | |
747 max_size_ = PreferedCacheSize(available); | |
748 | |
749 // Let's not use more than the default size while we tune-up the performance | |
750 // of bigger caches. TODO(rvargas): remove this limit. | |
751 if (max_size_ > kDefaultCacheSize * 4) | |
752 max_size_ = kDefaultCacheSize * 4; | |
753 | |
754 if (!table_len) | |
755 return; | |
756 | |
757 // If we already have a table, adjust the size to it. | |
758 int current_max_size = MaxStorageSizeForTable(table_len); | |
759 if (max_size_ > current_max_size) | |
760 max_size_= current_max_size; | |
761 } | |
762 | |
763 bool BackendImplV3::InitStats() { | |
764 Addr address(data_->header.stats); | |
765 int size = stats_.StorageSize(); | |
766 | |
767 if (!address.is_initialized()) { | |
768 FileType file_type = Addr::RequiredFileType(size); | |
769 DCHECK_NE(file_type, EXTERNAL); | |
770 int num_blocks = Addr::RequiredBlocks(size, file_type); | |
771 | |
772 if (!CreateBlock(file_type, num_blocks, &address)) | |
773 return false; | |
774 return stats_.Init(NULL, 0, address); | |
775 } | |
776 | |
777 if (!address.is_block_file()) { | |
778 NOTREACHED(); | |
779 return false; | |
780 } | |
781 | |
782 // Load the required data. | |
783 size = address.num_blocks() * address.BlockSize(); | |
784 MappedFile* file = File(address); | |
785 if (!file) | |
786 return false; | |
787 | |
788 scoped_ptr<char[]> data(new char[size]); | |
789 size_t offset = address.start_block() * address.BlockSize() + | |
790 kBlockHeaderSize; | |
791 if (!file->Read(data.get(), size, offset)) | |
792 return false; | |
793 | |
794 if (!stats_.Init(data.get(), size, address)) | |
795 return false; | |
796 if (cache_type_ == net::DISK_CACHE && ShouldReportAgain()) | |
797 stats_.InitSizeHistogram(); | |
798 return true; | |
799 } | |
800 | |
801 void BackendImplV3::StoreStats() { | |
802 int size = stats_.StorageSize(); | |
803 scoped_ptr<char[]> data(new char[size]); | |
804 Addr address; | |
805 size = stats_.SerializeStats(data.get(), size, &address); | |
806 DCHECK(size); | |
807 if (!address.is_initialized()) | |
808 return; | |
809 | |
810 MappedFile* file = File(address); | |
811 if (!file) | |
812 return; | |
813 | |
814 size_t offset = address.start_block() * address.BlockSize() + | |
815 kBlockHeaderSize; | |
816 file->Write(data.get(), size, offset); // ignore result. | |
817 } | |
818 | |
819 void BackendImplV3::RestartCache(bool failure) { | |
820 int64 errors = stats_.GetCounter(Stats::FATAL_ERROR); | |
821 int64 full_dooms = stats_.GetCounter(Stats::DOOM_CACHE); | |
822 int64 partial_dooms = stats_.GetCounter(Stats::DOOM_RECENT); | |
823 int64 last_report = stats_.GetCounter(Stats::LAST_REPORT); | |
824 | |
825 PrepareForRestart(); | |
826 if (failure) { | |
827 DCHECK(!num_refs_); | |
828 DCHECK(!open_entries_.size()); | |
829 DelayedCacheCleanup(path_); | |
830 } else { | |
831 DeleteCache(path_, false); | |
832 } | |
833 | |
834 // Don't call Init() if directed by the unit test: we are simulating a failure | |
835 // trying to re-enable the cache. | |
836 if (unit_test_) | |
837 init_ = true; // Let the destructor do proper cleanup. | |
838 else if (SyncInit() == net::OK) { | |
839 stats_.SetCounter(Stats::FATAL_ERROR, errors); | |
840 stats_.SetCounter(Stats::DOOM_CACHE, full_dooms); | |
841 stats_.SetCounter(Stats::DOOM_RECENT, partial_dooms); | |
842 stats_.SetCounter(Stats::LAST_REPORT, last_report); | |
843 } | |
844 } | |
845 | |
846 void BackendImplV3::PrepareForRestart() { | |
847 if (!(user_flags_ & EVICTION_V2)) | |
848 lru_eviction_ = true; | |
849 | |
850 disabled_ = true; | |
851 data_->header.crash = 0; | |
852 index_->Flush(); | |
853 index_ = NULL; | |
854 data_ = NULL; | |
855 block_files_.CloseFiles(); | |
856 rankings_.Reset(); | |
857 init_ = false; | |
858 restarted_ = true; | |
859 } | |
860 | |
861 void BackendImplV3::CleanupCache() { | |
862 Trace("Backend Cleanup"); | |
863 eviction_.Stop(); | |
864 timer_.reset(); | |
865 | |
866 if (init_) { | |
867 StoreStats(); | |
868 if (data_) | |
869 data_->header.crash = 0; | |
870 | |
871 if (user_flags_ & kNoRandom) { | |
872 // This is a net_unittest, verify that we are not 'leaking' entries. | |
873 File::WaitForPendingIO(&num_pending_io_); | |
874 DCHECK(!num_refs_); | |
875 } else { | |
876 File::DropPendingIO(); | |
877 } | |
878 } | |
879 block_files_.CloseFiles(); | |
880 FlushIndex(); | |
881 index_ = NULL; | |
882 ptr_factory_.InvalidateWeakPtrs(); | |
883 done_.Signal(); | |
884 } | |
885 | |
886 int BackendImplV3::NewEntry(Addr address, EntryImplV3** entry) { | |
887 EntriesMap::iterator it = open_entries_.find(address.value()); | |
888 if (it != open_entries_.end()) { | |
889 // Easy job. This entry is already in memory. | |
890 EntryImpl* this_entry = it->second; | |
891 this_entry->AddRef(); | |
892 *entry = this_entry; | |
893 return 0; | |
894 } | |
895 | |
896 STRESS_DCHECK(block_files_.IsValid(address)); | |
897 | |
898 if (!address.SanityCheckForEntry()) { | |
899 LOG(WARNING) << "Wrong entry address."; | |
900 STRESS_NOTREACHED(); | |
901 return ERR_INVALID_ADDRESS; | |
902 } | |
903 | |
904 scoped_refptr<EntryImpl> cache_entry( | |
905 new EntryImpl(this, address, read_only_)); | |
906 IncreaseNumRefs(); | |
907 *entry = NULL; | |
908 | |
909 TimeTicks start = TimeTicks::Now(); | |
910 if (!cache_entry->entry()->Load()) | |
911 return ERR_READ_FAILURE; | |
912 | |
913 if (IsLoaded()) { | |
914 CACHE_UMA(AGE_MS, "LoadTime", 0, start); | |
915 } | |
916 | |
917 if (!cache_entry->SanityCheck()) { | |
918 LOG(WARNING) << "Messed up entry found."; | |
919 STRESS_NOTREACHED(); | |
920 return ERR_INVALID_ENTRY; | |
921 } | |
922 | |
923 STRESS_DCHECK(block_files_.IsValid( | |
924 Addr(cache_entry->entry()->Data()->rankings_node))); | |
925 | |
926 if (!cache_entry->LoadNodeAddress()) | |
927 return ERR_READ_FAILURE; | |
928 | |
929 if (!rankings_.SanityCheck(cache_entry->rankings(), false)) { | |
930 STRESS_NOTREACHED(); | |
931 cache_entry->SetDirtyFlag(0); | |
932 // Don't remove this from the list (it is not linked properly). Instead, | |
933 // break the link back to the entry because it is going away, and leave the | |
934 // rankings node to be deleted if we find it through a list. | |
935 rankings_.SetContents(cache_entry->rankings(), 0); | |
936 } else if (!rankings_.DataSanityCheck(cache_entry->rankings(), false)) { | |
937 STRESS_NOTREACHED(); | |
938 cache_entry->SetDirtyFlag(0); | |
939 rankings_.SetContents(cache_entry->rankings(), address.value()); | |
940 } | |
941 | |
942 if (!cache_entry->DataSanityCheck()) { | |
943 LOG(WARNING) << "Messed up entry found."; | |
944 cache_entry->SetDirtyFlag(0); | |
945 cache_entry->FixForDelete(); | |
946 } | |
947 | |
948 // Prevent overwriting the dirty flag on the destructor. | |
949 cache_entry->SetDirtyFlag(GetCurrentEntryId()); | |
950 | |
951 if (cache_entry->dirty()) { | |
952 Trace("Dirty entry 0x%p 0x%x", reinterpret_cast<void*>(cache_entry.get()), | |
953 address.value()); | |
954 } | |
955 | |
956 open_entries_[address.value()] = cache_entry.get(); | |
957 | |
958 cache_entry->BeginLogging(net_log_, false); | |
959 cache_entry.swap(entry); | |
960 return 0; | |
961 } | |
962 | |
963 // This is the actual implementation for OpenNextEntry and OpenPrevEntry. | |
964 int BackendImplV3::OpenFollowingEntry(bool forward, void** iter, | |
965 Entry** next_entry, | |
966 const CompletionCallback& callback) { | |
967 if (disabled_) | |
968 return net::ERR_FAILED; | |
969 | |
970 DCHECK(iter); | |
971 | |
972 const int kListsToSearch = 3; | |
973 scoped_refptr<EntryImpl> entries[kListsToSearch]; | |
974 scoped_ptr<Rankings::Iterator> iterator( | |
975 reinterpret_cast<Rankings::Iterator*>(*iter)); | |
976 *iter = NULL; | |
977 | |
978 if (!iterator.get()) { | |
979 iterator.reset(new Rankings::Iterator(&rankings_)); | |
980 bool ret = false; | |
981 | |
982 // Get an entry from each list. | |
983 for (int i = 0; i < kListsToSearch; i++) { | |
984 EntryImpl* temp = NULL; | |
985 ret |= OpenFollowingEntryFromList(forward, static_cast<Rankings::List>(i), | |
986 &iterator->nodes[i], &temp); | |
987 entries[i].swap(&temp); // The entry was already addref'd. | |
988 } | |
989 if (!ret) | |
990 return NULL; | |
991 } else { | |
992 // Get the next entry from the last list, and the actual entries for the | |
993 // elements on the other lists. | |
994 for (int i = 0; i < kListsToSearch; i++) { | |
995 EntryImpl* temp = NULL; | |
996 if (iterator->list == i) { | |
997 OpenFollowingEntryFromList(forward, iterator->list, | |
998 &iterator->nodes[i], &temp); | |
999 } else { | |
1000 temp = GetEnumeratedEntry(iterator->nodes[i], | |
1001 static_cast<Rankings::List>(i)); | |
1002 } | |
1003 | |
1004 entries[i].swap(&temp); // The entry was already addref'd. | |
1005 } | |
1006 } | |
1007 | |
1008 int newest = -1; | |
1009 int oldest = -1; | |
1010 Time access_times[kListsToSearch]; | |
1011 for (int i = 0; i < kListsToSearch; i++) { | |
1012 if (entries[i].get()) { | |
1013 access_times[i] = entries[i]->GetLastUsed(); | |
1014 if (newest < 0) { | |
1015 DCHECK_LT(oldest, 0); | |
1016 newest = oldest = i; | |
1017 continue; | |
1018 } | |
1019 if (access_times[i] > access_times[newest]) | |
1020 newest = i; | |
1021 if (access_times[i] < access_times[oldest]) | |
1022 oldest = i; | |
1023 } | |
1024 } | |
1025 | |
1026 if (newest < 0 || oldest < 0) | |
1027 return NULL; | |
1028 | |
1029 EntryImpl* next_entry; | |
1030 if (forward) { | |
1031 next_entry = entries[newest].get(); | |
1032 iterator->list = static_cast<Rankings::List>(newest); | |
1033 } else { | |
1034 next_entry = entries[oldest].get(); | |
1035 iterator->list = static_cast<Rankings::List>(oldest); | |
1036 } | |
1037 | |
1038 *iter = iterator.release(); | |
1039 next_entry->AddRef(); | |
1040 return next_entry; | |
1041 } | |
1042 | |
1043 void BackendImplV3::AddStorageSize(int32 bytes) { | |
1044 data_->header.num_bytes += bytes; | |
1045 DCHECK_GE(data_->header.num_bytes, 0); | |
1046 } | |
1047 | |
1048 void BackendImplV3::SubstractStorageSize(int32 bytes) { | |
1049 data_->header.num_bytes -= bytes; | |
1050 DCHECK_GE(data_->header.num_bytes, 0); | |
1051 } | |
1052 | |
1053 void BackendImplV3::IncreaseNumRefs() { | |
1054 num_refs_++; | |
1055 if (max_refs_ < num_refs_) | |
1056 max_refs_ = num_refs_; | |
1057 } | |
1058 | |
1059 void BackendImplV3::DecreaseNumRefs() { | |
1060 DCHECK(num_refs_); | |
1061 num_refs_--; | |
1062 | |
1063 if (!num_refs_ && disabled_) | |
1064 base::MessageLoop::current()->PostTask( | |
1065 FROM_HERE, base::Bind(&BackendImpl::RestartCache, GetWeakPtr(), true)); | |
1066 } | |
1067 | |
1068 void BackendImplV3::IncreaseNumEntries() { | |
1069 index_.header()->num_entries++; | |
1070 DCHECK_GT(index_.header()->num_entries, 0); | |
1071 } | |
1072 | |
1073 void BackendImplV3::DecreaseNumEntries() { | |
1074 index_.header()->num_entries--; | |
1075 if (index_.header()->num_entries < 0) { | |
1076 NOTREACHED(); | |
1077 index_.header()->num_entries = 0; | |
1078 } | |
1079 } | |
1080 | |
1081 int BackendImplV3::SyncInit() { | |
1082 #if defined(NET_BUILD_STRESS_CACHE) | |
1083 // Start evictions right away. | |
1084 up_ticks_ = kTrimDelay * 2; | |
1085 #endif | |
1086 DCHECK(!init_); | |
1087 if (init_) | |
1088 return net::ERR_FAILED; | |
1089 | |
1090 bool create_files = false; | |
1091 if (!InitBackingStore(&create_files)) { | |
1092 ReportError(ERR_STORAGE_ERROR); | |
1093 return net::ERR_FAILED; | |
1094 } | |
1095 | |
1096 num_refs_ = num_pending_io_ = max_refs_ = 0; | |
1097 entry_count_ = byte_count_ = 0; | |
1098 | |
1099 if (!restarted_) { | |
1100 buffer_bytes_ = 0; | |
1101 trace_object_ = TraceObject::GetTraceObject(); | |
1102 // Create a recurrent timer of 30 secs. | |
1103 int timer_delay = unit_test_ ? 1000 : 30000; | |
1104 timer_.reset(new base::RepeatingTimer<BackendImplV3>()); | |
1105 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(timer_delay), this, | |
1106 &BackendImplV3::OnStatsTimer); | |
1107 } | |
1108 | |
1109 init_ = true; | |
1110 Trace("Init"); | |
1111 | |
1112 if (data_->header.experiment != NO_EXPERIMENT && | |
1113 cache_type_ != net::DISK_CACHE) { | |
1114 // No experiment for other caches. | |
1115 return net::ERR_FAILED; | |
1116 } | |
1117 | |
1118 if (!(user_flags_ & kNoRandom)) { | |
1119 // The unit test controls directly what to test. | |
1120 new_eviction_ = (cache_type_ == net::DISK_CACHE); | |
1121 } | |
1122 | |
1123 if (!CheckIndex()) { | |
1124 ReportError(ERR_INIT_FAILED); | |
1125 return net::ERR_FAILED; | |
1126 } | |
1127 | |
1128 if (!restarted_ && (create_files || !data_->header.num_entries)) | |
1129 ReportError(ERR_CACHE_CREATED); | |
1130 | |
1131 if (!(user_flags_ & kNoRandom) && cache_type_ == net::DISK_CACHE && | |
1132 !InitExperiment(&data_->header, create_files)) { | |
1133 return net::ERR_FAILED; | |
1134 } | |
1135 | |
1136 // We don't care if the value overflows. The only thing we care about is that | |
1137 // the id cannot be zero, because that value is used as "not dirty". | |
1138 // Increasing the value once per second gives us many years before we start | |
1139 // having collisions. | |
1140 data_->header.this_id++; | |
1141 if (!data_->header.this_id) | |
1142 data_->header.this_id++; | |
1143 | |
1144 bool previous_crash = (data_->header.crash != 0); | |
1145 data_->header.crash = 1; | |
1146 | |
1147 if (!block_files_.Init(create_files)) | |
1148 return net::ERR_FAILED; | |
1149 | |
1150 // We want to minimize the changes to cache for an AppCache. | |
1151 if (cache_type() == net::APP_CACHE) { | |
1152 DCHECK(!new_eviction_); | |
1153 read_only_ = true; | |
1154 } else if (cache_type() == net::SHADER_CACHE) { | |
1155 DCHECK(!new_eviction_); | |
1156 } | |
1157 | |
1158 eviction_.Init(this); | |
1159 | |
1160 // stats_ and rankings_ may end up calling back to us so we better be enabled. | |
1161 disabled_ = false; | |
1162 if (!InitStats()) | |
1163 return net::ERR_FAILED; | |
1164 | |
1165 disabled_ = !rankings_.Init(this, new_eviction_); | |
1166 | |
1167 #if defined(STRESS_CACHE_EXTENDED_VALIDATION) | |
1168 trace_object_->EnableTracing(false); | |
1169 int sc = SelfCheck(); | |
1170 if (sc < 0 && sc != ERR_NUM_ENTRIES_MISMATCH) | |
1171 NOTREACHED(); | |
1172 trace_object_->EnableTracing(true); | |
1173 #endif | |
1174 | |
1175 if (previous_crash) { | |
1176 ReportError(ERR_PREVIOUS_CRASH); | |
1177 } else if (!restarted_) { | |
1178 ReportError(ERR_NO_ERROR); | |
1179 } | |
1180 | |
1181 FlushIndex(); | |
1182 | |
1183 return disabled_ ? net::ERR_FAILED : net::OK; | |
1184 } | |
1185 | |
1186 EntryImpl* BackendImplV3::ResurrectEntry(EntryImpl* deleted_entry) { | |
1187 if (ENTRY_NORMAL == deleted_entry->entry()->Data()->state) { | |
1188 deleted_entry->Release(); | |
1189 stats_.OnEvent(Stats::CREATE_MISS); | |
1190 Trace("create entry miss "); | |
1191 return NULL; | |
1192 } | |
1193 | |
1194 // We are attempting to create an entry and found out that the entry was | |
1195 // previously deleted. | |
1196 | |
1197 eviction_.OnCreateEntry(deleted_entry); | |
1198 entry_count_++; | |
1199 | |
1200 stats_.OnEvent(Stats::RESURRECT_HIT); | |
1201 Trace("Resurrect entry hit "); | |
1202 return deleted_entry; | |
1203 } | |
1204 | |
1205 EntryImpl* BackendImplV3::CreateEntryImpl(const std::string& key) { | |
1206 if (disabled_ || key.empty()) | |
1207 return NULL; | |
1208 | |
1209 TimeTicks start = TimeTicks::Now(); | |
1210 Trace("Create hash 0x%x", hash); | |
1211 | |
1212 scoped_refptr<EntryImpl> parent; | |
1213 Addr entry_address(data_->table[hash & mask_]); | |
1214 if (entry_address.is_initialized()) { | |
1215 // We have an entry already. It could be the one we are looking for, or just | |
1216 // a hash conflict. | |
1217 bool error; | |
1218 EntryImpl* old_entry = MatchEntry(key, hash, false, Addr(), &error); | |
1219 if (old_entry) | |
1220 return ResurrectEntry(old_entry); | |
1221 | |
1222 EntryImpl* parent_entry = MatchEntry(key, hash, true, Addr(), &error); | |
1223 DCHECK(!error); | |
1224 if (parent_entry) { | |
1225 parent.swap(&parent_entry); | |
1226 } else if (data_->table[hash & mask_]) { | |
1227 // We should have corrected the problem. | |
1228 NOTREACHED(); | |
1229 return NULL; | |
1230 } | |
1231 } | |
1232 | |
1233 // The general flow is to allocate disk space and initialize the entry data, | |
1234 // followed by saving that to disk, then linking the entry though the index | |
1235 // and finally through the lists. If there is a crash in this process, we may | |
1236 // end up with: | |
1237 // a. Used, unreferenced empty blocks on disk (basically just garbage). | |
1238 // b. Used, unreferenced but meaningful data on disk (more garbage). | |
1239 // c. A fully formed entry, reachable only through the index. | |
1240 // d. A fully formed entry, also reachable through the lists, but still dirty. | |
1241 // | |
1242 // Anything after (b) can be automatically cleaned up. We may consider saving | |
1243 // the current operation (as we do while manipulating the lists) so that we | |
1244 // can detect and cleanup (a) and (b). | |
1245 | |
1246 int num_blocks = EntryImpl::NumBlocksForEntry(key.size()); | |
1247 if (!block_files_.CreateBlock(BLOCK_256, num_blocks, &entry_address)) { | |
1248 LOG(ERROR) << "Create entry failed " << key.c_str(); | |
1249 stats_.OnEvent(Stats::CREATE_ERROR); | |
1250 return NULL; | |
1251 } | |
1252 | |
1253 Addr node_address(0); | |
1254 if (!block_files_.CreateBlock(RANKINGS, 1, &node_address)) { | |
1255 block_files_.DeleteBlock(entry_address, false); | |
1256 LOG(ERROR) << "Create entry failed " << key.c_str(); | |
1257 stats_.OnEvent(Stats::CREATE_ERROR); | |
1258 return NULL; | |
1259 } | |
1260 | |
1261 scoped_refptr<EntryImpl> cache_entry( | |
1262 new EntryImpl(this, entry_address, false)); | |
1263 IncreaseNumRefs(); | |
1264 | |
1265 if (!cache_entry->CreateEntry(node_address, key, hash)) { | |
1266 block_files_.DeleteBlock(entry_address, false); | |
1267 block_files_.DeleteBlock(node_address, false); | |
1268 LOG(ERROR) << "Create entry failed " << key.c_str(); | |
1269 stats_.OnEvent(Stats::CREATE_ERROR); | |
1270 return NULL; | |
1271 } | |
1272 | |
1273 cache_entry->BeginLogging(net_log_, true); | |
1274 | |
1275 // We are not failing the operation; let's add this to the map. | |
1276 open_entries_[entry_address.value()] = cache_entry; | |
1277 | |
1278 // Save the entry. | |
1279 cache_entry->entry()->Store(); | |
1280 cache_entry->rankings()->Store(); | |
1281 IncreaseNumEntries(); | |
1282 entry_count_++; | |
1283 | |
1284 // Link this entry through the index. | |
1285 if (parent.get()) { | |
1286 parent->SetNextAddress(entry_address); | |
1287 } else { | |
1288 data_->table[hash & mask_] = entry_address.value(); | |
1289 } | |
1290 | |
1291 // Link this entry through the lists. | |
1292 eviction_.OnCreateEntry(cache_entry); | |
1293 | |
1294 CACHE_UMA(AGE_MS, "CreateTime", 0, start); | |
1295 stats_.OnEvent(Stats::CREATE_HIT); | |
1296 SIMPLE_STATS_COUNTER("disk_cache.miss"); | |
1297 Trace("create entry hit "); | |
1298 FlushIndex(); | |
1299 cache_entry->AddRef(); | |
1300 return cache_entry.get(); | |
1301 } | |
1302 | |
1303 void BackendImplV3::LogStats() { | |
1304 StatsItems stats; | |
1305 GetStats(&stats); | |
1306 | |
1307 for (size_t index = 0; index < stats.size(); index++) | |
1308 VLOG(1) << stats[index].first << ": " << stats[index].second; | |
1309 } | |
1310 | |
1311 void BackendImplV3::ReportStats() { | |
1312 IndexHeaderV3* header = index_.header(); | |
1313 CACHE_UMA(COUNTS, "Entries", header->num_entries); | |
1314 | |
1315 int current_size = header->num_bytes / (1024 * 1024); | |
1316 int max_size = max_size_ / (1024 * 1024); | |
1317 | |
1318 CACHE_UMA(COUNTS_10000, "Size", current_size); | |
1319 CACHE_UMA(COUNTS_10000, "MaxSize", max_size); | |
1320 if (!max_size) | |
1321 max_size++; | |
1322 CACHE_UMA(PERCENTAGE, "UsedSpace", current_size * 100 / max_size); | |
1323 | |
1324 CACHE_UMA(COUNTS_10000, "AverageOpenEntries", | |
1325 static_cast<int>(stats_.GetCounter(Stats::OPEN_ENTRIES))); | |
1326 CACHE_UMA(COUNTS_10000, "MaxOpenEntries", | |
1327 static_cast<int>(stats_.GetCounter(Stats::MAX_ENTRIES))); | |
1328 stats_.SetCounter(Stats::MAX_ENTRIES, 0); | |
1329 | |
1330 CACHE_UMA(COUNTS_10000, "TotalFatalErrors", | |
1331 static_cast<int>(stats_.GetCounter(Stats::FATAL_ERROR))); | |
1332 CACHE_UMA(COUNTS_10000, "TotalDoomCache", | |
1333 static_cast<int>(stats_.GetCounter(Stats::DOOM_CACHE))); | |
1334 CACHE_UMA(COUNTS_10000, "TotalDoomRecentEntries", | |
1335 static_cast<int>(stats_.GetCounter(Stats::DOOM_RECENT))); | |
1336 stats_.SetCounter(Stats::FATAL_ERROR, 0); | |
1337 stats_.SetCounter(Stats::DOOM_CACHE, 0); | |
1338 stats_.SetCounter(Stats::DOOM_RECENT, 0); | |
1339 | |
1340 int64 total_hours = stats_.GetCounter(Stats::TIMER) / 120; | |
1341 if (!(header->flags & CACHE_EVICTED)) { | |
1342 CACHE_UMA(HOURS, "TotalTimeNotFull", static_cast<int>(total_hours)); | |
1343 return; | |
1344 } | |
1345 | |
1346 // This is an up to date client that will report FirstEviction() data. After | |
1347 // that event, start reporting this: | |
1348 | |
1349 CACHE_UMA(HOURS, "TotalTime", static_cast<int>(total_hours)); | |
1350 | |
1351 int64 use_hours = stats_.GetCounter(Stats::LAST_REPORT_TIMER) / 120; | |
1352 stats_.SetCounter(Stats::LAST_REPORT_TIMER, stats_.GetCounter(Stats::TIMER)); | |
1353 | |
1354 // We may see users with no use_hours at this point if this is the first time | |
1355 // we are running this code. | |
1356 if (use_hours) | |
1357 use_hours = total_hours - use_hours; | |
1358 | |
1359 if (!use_hours || !GetEntryCount() || !header->num_bytes) | |
1360 return; | |
1361 | |
1362 CACHE_UMA(HOURS, "UseTime", static_cast<int>(use_hours)); | |
1363 | |
1364 int64 trim_rate = stats_.GetCounter(Stats::TRIM_ENTRY) / use_hours; | |
1365 CACHE_UMA(COUNTS, "TrimRate", static_cast<int>(trim_rate)); | |
1366 | |
1367 int avg_size = header->num_bytes / GetEntryCount(); | |
1368 CACHE_UMA(COUNTS, "EntrySize", avg_size); | |
1369 CACHE_UMA(COUNTS, "EntriesFull", header->num_entries); | |
1370 | |
1371 int large_entries_bytes = stats_.GetLargeEntriesSize(); | |
1372 int large_ratio = large_entries_bytes * 100 / header->num_bytes; | |
1373 CACHE_UMA(PERCENTAGE, "LargeEntriesRatio", large_ratio); | |
1374 | |
1375 if (!lru_eviction_) { | |
1376 CACHE_UMA(PERCENTAGE, "ResurrectRatio", stats_.GetResurrectRatio()); | |
1377 CACHE_UMA(PERCENTAGE, "NoUseRatio", | |
1378 header->num_no_use_entries * 100 / header->num_entries); | |
1379 CACHE_UMA(PERCENTAGE, "LowUseRatio", | |
1380 header->num_low_use_entries * 100 / header->num_entries); | |
1381 CACHE_UMA(PERCENTAGE, "HighUseRatio", | |
1382 header->num_high_use_entries * 100 / header->num_entries); | |
1383 CACHE_UMA(PERCENTAGE, "DeletedRatio", | |
1384 header->num_evicted_entries * 100 / header->num_entries); | |
1385 } | |
1386 | |
1387 stats_.ResetRatios(); | |
1388 stats_.SetCounter(Stats::TRIM_ENTRY, 0); | |
1389 | |
1390 if (cache_type_ == net::DISK_CACHE) | |
1391 block_files_.ReportStats(); | |
1392 } | |
1393 | |
1394 void BackendImplV3::ReportError(int error) { | |
1395 STRESS_DCHECK(!error || error == ERR_PREVIOUS_CRASH || | |
1396 error == ERR_CACHE_CREATED); | |
1397 | |
1398 // We transmit positive numbers, instead of direct error codes. | |
1399 DCHECK_LE(error, 0); | |
1400 CACHE_UMA(CACHE_ERROR, "Error", error * -1); | |
1401 } | |
1402 | |
1403 bool BackendImplV3::CheckIndex() { | |
1404 DCHECK(data_); | |
1405 | |
1406 size_t current_size = index_->GetLength(); | |
1407 if (current_size < sizeof(Index)) { | |
1408 LOG(ERROR) << "Corrupt Index file"; | |
1409 return false; | |
1410 } | |
1411 | |
1412 if (new_eviction_) { | |
1413 // We support versions 2.0 and 2.1, upgrading 2.0 to 2.1. | |
1414 if (kIndexMagic != data_->header.magic || | |
1415 kCurrentVersion >> 16 != data_->header.version >> 16) { | |
1416 LOG(ERROR) << "Invalid file version or magic"; | |
1417 return false; | |
1418 } | |
1419 if (kCurrentVersion == data_->header.version) { | |
1420 // We need file version 2.1 for the new eviction algorithm. | |
1421 UpgradeTo2_1(); | |
1422 } | |
1423 } else { | |
1424 if (kIndexMagic != data_->header.magic || | |
1425 kCurrentVersion != data_->header.version) { | |
1426 LOG(ERROR) << "Invalid file version or magic"; | |
1427 return false; | |
1428 } | |
1429 } | |
1430 | |
1431 if (!data_->header.table_len) { | |
1432 LOG(ERROR) << "Invalid table size"; | |
1433 return false; | |
1434 } | |
1435 | |
1436 if (current_size < GetIndexSize(data_->header.table_len) || | |
1437 data_->header.table_len & (kBaseTableLen - 1)) { | |
1438 LOG(ERROR) << "Corrupt Index file"; | |
1439 return false; | |
1440 } | |
1441 | |
1442 AdjustMaxCacheSize(data_->header.table_len); | |
1443 | |
1444 #if !defined(NET_BUILD_STRESS_CACHE) | |
1445 if (data_->header.num_bytes < 0 || | |
1446 (max_size_ < kint32max - kDefaultCacheSize && | |
1447 data_->header.num_bytes > max_size_ + kDefaultCacheSize)) { | |
1448 LOG(ERROR) << "Invalid cache (current) size"; | |
1449 return false; | |
1450 } | |
1451 #endif | |
1452 | |
1453 if (data_->header.num_entries < 0) { | |
1454 LOG(ERROR) << "Invalid number of entries"; | |
1455 return false; | |
1456 } | |
1457 | |
1458 if (!mask_) | |
1459 mask_ = data_->header.table_len - 1; | |
1460 | |
1461 // Load the table into memory with a single read. | |
1462 scoped_ptr<char[]> buf(new char[current_size]); | |
1463 return index_->Read(buf.get(), current_size, 0); | |
1464 } | |
1465 | |
1466 int BackendImplV3::CheckAllEntries() { | |
1467 int num_dirty = 0; | |
1468 int num_entries = 0; | |
1469 DCHECK(mask_ < kuint32max); | |
1470 for (unsigned int i = 0; i <= mask_; i++) { | |
1471 Addr address(data_->table[i]); | |
1472 if (!address.is_initialized()) | |
1473 continue; | |
1474 for (;;) { | |
1475 EntryImpl* tmp; | |
1476 int ret = NewEntry(address, &tmp); | |
1477 if (ret) { | |
1478 STRESS_NOTREACHED(); | |
1479 return ret; | |
1480 } | |
1481 scoped_refptr<EntryImpl> cache_entry; | |
1482 cache_entry.swap(&tmp); | |
1483 | |
1484 if (cache_entry->dirty()) | |
1485 num_dirty++; | |
1486 else if (CheckEntry(cache_entry.get())) | |
1487 num_entries++; | |
1488 else | |
1489 return ERR_INVALID_ENTRY; | |
1490 | |
1491 DCHECK_EQ(i, cache_entry->entry()->Data()->hash & mask_); | |
1492 address.set_value(cache_entry->GetNextAddress()); | |
1493 if (!address.is_initialized()) | |
1494 break; | |
1495 } | |
1496 } | |
1497 | |
1498 Trace("CheckAllEntries End"); | |
1499 if (num_entries + num_dirty != data_->header.num_entries) { | |
1500 LOG(ERROR) << "Number of entries " << num_entries << " " << num_dirty << | |
1501 " " << data_->header.num_entries; | |
1502 DCHECK_LT(num_entries, data_->header.num_entries); | |
1503 return ERR_NUM_ENTRIES_MISMATCH; | |
1504 } | |
1505 | |
1506 return num_dirty; | |
1507 } | |
1508 | |
1509 bool BackendImplV3::CheckEntry(EntryImpl* cache_entry) { | |
1510 bool ok = block_files_.IsValid(cache_entry->entry()->address()); | |
1511 ok = ok && block_files_.IsValid(cache_entry->rankings()->address()); | |
1512 EntryStore* data = cache_entry->entry()->Data(); | |
1513 for (size_t i = 0; i < arraysize(data->data_addr); i++) { | |
1514 if (data->data_addr[i]) { | |
1515 Addr address(data->data_addr[i]); | |
1516 if (address.is_block_file()) | |
1517 ok = ok && block_files_.IsValid(address); | |
1518 } | |
1519 } | |
1520 | |
1521 return ok && cache_entry->rankings()->VerifyHash(); | |
1522 } | |
1523 | |
1524 int BackendImplV3::MaxBuffersSize() { | |
1525 static int64 total_memory = base::SysInfo::AmountOfPhysicalMemory(); | |
1526 static bool done = false; | |
1527 | |
1528 if (!done) { | |
1529 const int kMaxBuffersSize = 30 * 1024 * 1024; | |
1530 | |
1531 // We want to use up to 2% of the computer's memory. | |
1532 total_memory = total_memory * 2 / 100; | |
1533 if (total_memory > kMaxBuffersSize || total_memory <= 0) | |
1534 total_memory = kMaxBuffersSize; | |
1535 | |
1536 done = true; | |
1537 } | |
1538 | |
1539 return static_cast<int>(total_memory); | |
1540 } | |
1541 | |
1542 #endif // defined(V3_NOT_JUST_YET_READY). | |
1543 | |
1544 bool BackendImplV3::IsAllocAllowed(int current_size, int new_size) { | |
1545 return false; | |
1546 } | |
1547 | |
1548 net::CacheType BackendImplV3::GetCacheType() const { | |
1549 return cache_type_; | |
1550 } | |
1551 | |
1552 int32 BackendImplV3::GetEntryCount() const { | |
1553 return 0; | |
1554 } | |
1555 | |
1556 int BackendImplV3::OpenEntry(const std::string& key, Entry** entry, | |
1557 const CompletionCallback& callback) { | |
1558 return net::ERR_FAILED; | |
1559 } | |
1560 | |
1561 int BackendImplV3::CreateEntry(const std::string& key, Entry** entry, | |
1562 const CompletionCallback& callback) { | |
1563 return net::ERR_FAILED; | |
1564 } | |
1565 | |
1566 int BackendImplV3::DoomEntry(const std::string& key, | |
1567 const CompletionCallback& callback) { | |
1568 return net::ERR_FAILED; | |
1569 } | |
1570 | |
1571 int BackendImplV3::DoomAllEntries(const CompletionCallback& callback) { | |
1572 return net::ERR_FAILED; | |
1573 } | |
1574 | |
1575 int BackendImplV3::DoomEntriesBetween(base::Time initial_time, | |
1576 base::Time end_time, | |
1577 const CompletionCallback& callback) { | |
1578 return net::ERR_FAILED; | |
1579 } | |
1580 | |
1581 int BackendImplV3::DoomEntriesSince(base::Time initial_time, | |
1582 const CompletionCallback& callback) { | |
1583 return net::ERR_FAILED; | |
1584 } | |
1585 | |
1586 int BackendImplV3::OpenNextEntry(void** iter, Entry** next_entry, | |
1587 const CompletionCallback& callback) { | |
1588 return net::ERR_FAILED; | |
1589 } | |
1590 | |
1591 void BackendImplV3::EndEnumeration(void** iter) { | |
1592 NOTIMPLEMENTED(); | |
1593 } | |
1594 | |
1595 void BackendImplV3::GetStats(StatsItems* stats) { | |
1596 NOTIMPLEMENTED(); | |
1597 } | |
1598 | |
1599 void BackendImplV3::OnExternalCacheHit(const std::string& key) { | |
1600 NOTIMPLEMENTED(); | |
1601 } | |
1602 | |
1603 void BackendImplV3::CleanupCache() { | |
1604 } | |
1605 | |
1606 } // namespace disk_cache | |
OLD | NEW |