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

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

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

Powered by Google App Engine
This is Rietveld 408576698