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

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

Issue 121643003: Reorganize net/disk_cache into backend specific directories. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase to upstream Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // The eviction policy is a very simple pure LRU, so the elements at the end of
6 // the list are evicted until kCleanUpMargin free space is available. There is
7 // only one list in use (Rankings::NO_USE), and elements are sent to the front
8 // of the list whenever they are accessed.
9
10 // The new (in-development) eviction policy adds re-use as a factor to evict
11 // an entry. The story so far:
12
13 // Entries are linked on separate lists depending on how often they are used.
14 // When we see an element for the first time, it goes to the NO_USE list; if
15 // the object is reused later on, we move it to the LOW_USE list, until it is
16 // used kHighUse times, at which point it is moved to the HIGH_USE list.
17 // Whenever an element is evicted, we move it to the DELETED list so that if the
18 // element is accessed again, we remember the fact that it was already stored
19 // and maybe in the future we don't evict that element.
20
21 // When we have to evict an element, first we try to use the last element from
22 // the NO_USE list, then we move to the LOW_USE and only then we evict an entry
23 // from the HIGH_USE. We attempt to keep entries on the cache for at least
24 // kTargetTime hours (with frequently accessed items stored for longer periods),
25 // but if we cannot do that, we fall-back to keep each list roughly the same
26 // size so that we have a chance to see an element again and move it to another
27 // list.
28
29 #include "net/disk_cache/eviction.h"
30
31 #include "base/bind.h"
32 #include "base/compiler_specific.h"
33 #include "base/logging.h"
34 #include "base/message_loop/message_loop.h"
35 #include "base/strings/string_util.h"
36 #include "base/time/time.h"
37 #include "net/disk_cache/backend_impl.h"
38 #include "net/disk_cache/disk_format.h"
39 #include "net/disk_cache/entry_impl.h"
40 #include "net/disk_cache/experiments.h"
41 #include "net/disk_cache/trace.h"
42
43 #define CACHE_HISTOGRAM_MACROS_BACKEND_IMPL_OBJ backend_
44 #include "net/disk_cache/histogram_macros.h"
45
46 using base::Time;
47 using base::TimeTicks;
48
49 namespace {
50
51 const int kCleanUpMargin = 1024 * 1024;
52 const int kHighUse = 10; // Reuse count to be on the HIGH_USE list.
53 const int kTargetTime = 24 * 7; // Time to be evicted (hours since last use).
54 const int kMaxDelayedTrims = 60;
55
56 int LowWaterAdjust(int high_water) {
57 if (high_water < kCleanUpMargin)
58 return 0;
59
60 return high_water - kCleanUpMargin;
61 }
62
63 bool FallingBehind(int current_size, int max_size) {
64 return current_size > max_size - kCleanUpMargin * 20;
65 }
66
67 } // namespace
68
69 namespace disk_cache {
70
71 // The real initialization happens during Init(), init_ is the only member that
72 // has to be initialized here.
73 Eviction::Eviction()
74 : backend_(NULL),
75 init_(false),
76 ptr_factory_(this) {
77 }
78
79 Eviction::~Eviction() {
80 }
81
82 void Eviction::Init(BackendImpl* backend) {
83 // We grab a bunch of info from the backend to make the code a little cleaner
84 // when we're actually doing work.
85 backend_ = backend;
86 rankings_ = &backend->rankings_;
87 header_ = &backend_->data_->header;
88 max_size_ = LowWaterAdjust(backend_->max_size_);
89 index_size_ = backend->mask_ + 1;
90 new_eviction_ = backend->new_eviction_;
91 first_trim_ = true;
92 trimming_ = false;
93 delay_trim_ = false;
94 trim_delays_ = 0;
95 init_ = true;
96 test_mode_ = false;
97 }
98
99 void Eviction::Stop() {
100 // It is possible for the backend initialization to fail, in which case this
101 // object was never initialized... and there is nothing to do.
102 if (!init_)
103 return;
104
105 // We want to stop further evictions, so let's pretend that we are busy from
106 // this point on.
107 DCHECK(!trimming_);
108 trimming_ = true;
109 ptr_factory_.InvalidateWeakPtrs();
110 }
111
112 void Eviction::TrimCache(bool empty) {
113 if (backend_->disabled_ || trimming_)
114 return;
115
116 if (!empty && !ShouldTrim())
117 return PostDelayedTrim();
118
119 if (new_eviction_)
120 return TrimCacheV2(empty);
121
122 Trace("*** Trim Cache ***");
123 trimming_ = true;
124 TimeTicks start = TimeTicks::Now();
125 Rankings::ScopedRankingsBlock node(rankings_);
126 Rankings::ScopedRankingsBlock next(
127 rankings_, rankings_->GetPrev(node.get(), Rankings::NO_USE));
128 int deleted_entries = 0;
129 int target_size = empty ? 0 : max_size_;
130 while ((header_->num_bytes > target_size || test_mode_) && next.get()) {
131 // The iterator could be invalidated within EvictEntry().
132 if (!next->HasData())
133 break;
134 node.reset(next.release());
135 next.reset(rankings_->GetPrev(node.get(), Rankings::NO_USE));
136 if (node->Data()->dirty != backend_->GetCurrentEntryId() || empty) {
137 // This entry is not being used by anybody.
138 // Do NOT use node as an iterator after this point.
139 rankings_->TrackRankingsBlock(node.get(), false);
140 if (EvictEntry(node.get(), empty, Rankings::NO_USE) && !test_mode_)
141 deleted_entries++;
142
143 if (!empty && test_mode_)
144 break;
145 }
146 if (!empty && (deleted_entries > 20 ||
147 (TimeTicks::Now() - start).InMilliseconds() > 20)) {
148 base::MessageLoop::current()->PostTask(
149 FROM_HERE,
150 base::Bind(&Eviction::TrimCache, ptr_factory_.GetWeakPtr(), false));
151 break;
152 }
153 }
154
155 if (empty) {
156 CACHE_UMA(AGE_MS, "TotalClearTimeV1", 0, start);
157 } else {
158 CACHE_UMA(AGE_MS, "TotalTrimTimeV1", 0, start);
159 }
160 CACHE_UMA(COUNTS, "TrimItemsV1", 0, deleted_entries);
161
162 trimming_ = false;
163 Trace("*** Trim Cache end ***");
164 return;
165 }
166
167 void Eviction::UpdateRank(EntryImpl* entry, bool modified) {
168 if (new_eviction_)
169 return UpdateRankV2(entry, modified);
170
171 rankings_->UpdateRank(entry->rankings(), modified, GetListForEntry(entry));
172 }
173
174 void Eviction::OnOpenEntry(EntryImpl* entry) {
175 if (new_eviction_)
176 return OnOpenEntryV2(entry);
177 }
178
179 void Eviction::OnCreateEntry(EntryImpl* entry) {
180 if (new_eviction_)
181 return OnCreateEntryV2(entry);
182
183 rankings_->Insert(entry->rankings(), true, GetListForEntry(entry));
184 }
185
186 void Eviction::OnDoomEntry(EntryImpl* entry) {
187 if (new_eviction_)
188 return OnDoomEntryV2(entry);
189
190 if (entry->LeaveRankingsBehind())
191 return;
192
193 rankings_->Remove(entry->rankings(), GetListForEntry(entry), true);
194 }
195
196 void Eviction::OnDestroyEntry(EntryImpl* entry) {
197 if (new_eviction_)
198 return OnDestroyEntryV2(entry);
199 }
200
201 void Eviction::SetTestMode() {
202 test_mode_ = true;
203 }
204
205 void Eviction::TrimDeletedList(bool empty) {
206 DCHECK(test_mode_ && new_eviction_);
207 TrimDeleted(empty);
208 }
209
210 void Eviction::PostDelayedTrim() {
211 // Prevent posting multiple tasks.
212 if (delay_trim_)
213 return;
214 delay_trim_ = true;
215 trim_delays_++;
216 base::MessageLoop::current()->PostDelayedTask(
217 FROM_HERE,
218 base::Bind(&Eviction::DelayedTrim, ptr_factory_.GetWeakPtr()),
219 base::TimeDelta::FromMilliseconds(1000));
220 }
221
222 void Eviction::DelayedTrim() {
223 delay_trim_ = false;
224 if (trim_delays_ < kMaxDelayedTrims && backend_->IsLoaded())
225 return PostDelayedTrim();
226
227 TrimCache(false);
228 }
229
230 bool Eviction::ShouldTrim() {
231 if (!FallingBehind(header_->num_bytes, max_size_) &&
232 trim_delays_ < kMaxDelayedTrims && backend_->IsLoaded()) {
233 return false;
234 }
235
236 UMA_HISTOGRAM_COUNTS("DiskCache.TrimDelays", trim_delays_);
237 trim_delays_ = 0;
238 return true;
239 }
240
241 bool Eviction::ShouldTrimDeleted() {
242 int index_load = header_->num_entries * 100 / index_size_;
243
244 // If the index is not loaded, the deleted list will tend to double the size
245 // of the other lists 3 lists (40% of the total). Otherwise, all lists will be
246 // about the same size.
247 int max_length = (index_load < 25) ? header_->num_entries * 2 / 5 :
248 header_->num_entries / 4;
249 return (!test_mode_ && header_->lru.sizes[Rankings::DELETED] > max_length);
250 }
251
252 void Eviction::ReportTrimTimes(EntryImpl* entry) {
253 if (first_trim_) {
254 first_trim_ = false;
255 if (backend_->ShouldReportAgain()) {
256 CACHE_UMA(AGE, "TrimAge", 0, entry->GetLastUsed());
257 ReportListStats();
258 }
259
260 if (header_->lru.filled)
261 return;
262
263 header_->lru.filled = 1;
264
265 if (header_->create_time) {
266 // This is the first entry that we have to evict, generate some noise.
267 backend_->FirstEviction();
268 } else {
269 // This is an old file, but we may want more reports from this user so
270 // lets save some create_time.
271 Time::Exploded old = {0};
272 old.year = 2009;
273 old.month = 3;
274 old.day_of_month = 1;
275 header_->create_time = Time::FromLocalExploded(old).ToInternalValue();
276 }
277 }
278 }
279
280 Rankings::List Eviction::GetListForEntry(EntryImpl* entry) {
281 return Rankings::NO_USE;
282 }
283
284 bool Eviction::EvictEntry(CacheRankingsBlock* node, bool empty,
285 Rankings::List list) {
286 EntryImpl* entry = backend_->GetEnumeratedEntry(node, list);
287 if (!entry) {
288 Trace("NewEntry failed on Trim 0x%x", node->address().value());
289 return false;
290 }
291
292 ReportTrimTimes(entry);
293 if (empty || !new_eviction_) {
294 entry->DoomImpl();
295 } else {
296 entry->DeleteEntryData(false);
297 EntryStore* info = entry->entry()->Data();
298 DCHECK_EQ(ENTRY_NORMAL, info->state);
299
300 rankings_->Remove(entry->rankings(), GetListForEntryV2(entry), true);
301 info->state = ENTRY_EVICTED;
302 entry->entry()->Store();
303 rankings_->Insert(entry->rankings(), true, Rankings::DELETED);
304 }
305 if (!empty)
306 backend_->OnEvent(Stats::TRIM_ENTRY);
307
308 entry->Release();
309
310 return true;
311 }
312
313 // -----------------------------------------------------------------------
314
315 void Eviction::TrimCacheV2(bool empty) {
316 Trace("*** Trim Cache ***");
317 trimming_ = true;
318 TimeTicks start = TimeTicks::Now();
319
320 const int kListsToSearch = 3;
321 Rankings::ScopedRankingsBlock next[kListsToSearch];
322 int list = Rankings::LAST_ELEMENT;
323
324 // Get a node from each list.
325 for (int i = 0; i < kListsToSearch; i++) {
326 bool done = false;
327 next[i].set_rankings(rankings_);
328 if (done)
329 continue;
330 next[i].reset(rankings_->GetPrev(NULL, static_cast<Rankings::List>(i)));
331 if (!empty && NodeIsOldEnough(next[i].get(), i)) {
332 list = static_cast<Rankings::List>(i);
333 done = true;
334 }
335 }
336
337 // If we are not meeting the time targets lets move on to list length.
338 if (!empty && Rankings::LAST_ELEMENT == list)
339 list = SelectListByLength(next);
340
341 if (empty)
342 list = 0;
343
344 Rankings::ScopedRankingsBlock node(rankings_);
345 int deleted_entries = 0;
346 int target_size = empty ? 0 : max_size_;
347
348 for (; list < kListsToSearch; list++) {
349 while ((header_->num_bytes > target_size || test_mode_) &&
350 next[list].get()) {
351 // The iterator could be invalidated within EvictEntry().
352 if (!next[list]->HasData())
353 break;
354 node.reset(next[list].release());
355 next[list].reset(rankings_->GetPrev(node.get(),
356 static_cast<Rankings::List>(list)));
357 if (node->Data()->dirty != backend_->GetCurrentEntryId() || empty) {
358 // This entry is not being used by anybody.
359 // Do NOT use node as an iterator after this point.
360 rankings_->TrackRankingsBlock(node.get(), false);
361 if (EvictEntry(node.get(), empty, static_cast<Rankings::List>(list)))
362 deleted_entries++;
363
364 if (!empty && test_mode_)
365 break;
366 }
367 if (!empty && (deleted_entries > 20 ||
368 (TimeTicks::Now() - start).InMilliseconds() > 20)) {
369 base::MessageLoop::current()->PostTask(
370 FROM_HERE,
371 base::Bind(&Eviction::TrimCache, ptr_factory_.GetWeakPtr(), false));
372 break;
373 }
374 }
375 if (!empty)
376 list = kListsToSearch;
377 }
378
379 if (empty) {
380 TrimDeleted(true);
381 } else if (ShouldTrimDeleted()) {
382 base::MessageLoop::current()->PostTask(
383 FROM_HERE,
384 base::Bind(&Eviction::TrimDeleted, ptr_factory_.GetWeakPtr(), empty));
385 }
386
387 if (empty) {
388 CACHE_UMA(AGE_MS, "TotalClearTimeV2", 0, start);
389 } else {
390 CACHE_UMA(AGE_MS, "TotalTrimTimeV2", 0, start);
391 }
392 CACHE_UMA(COUNTS, "TrimItemsV2", 0, deleted_entries);
393
394 Trace("*** Trim Cache end ***");
395 trimming_ = false;
396 return;
397 }
398
399 void Eviction::UpdateRankV2(EntryImpl* entry, bool modified) {
400 rankings_->UpdateRank(entry->rankings(), modified, GetListForEntryV2(entry));
401 }
402
403 void Eviction::OnOpenEntryV2(EntryImpl* entry) {
404 EntryStore* info = entry->entry()->Data();
405 DCHECK_EQ(ENTRY_NORMAL, info->state);
406
407 if (info->reuse_count < kint32max) {
408 info->reuse_count++;
409 entry->entry()->set_modified();
410
411 // We may need to move this to a new list.
412 if (1 == info->reuse_count) {
413 rankings_->Remove(entry->rankings(), Rankings::NO_USE, true);
414 rankings_->Insert(entry->rankings(), false, Rankings::LOW_USE);
415 entry->entry()->Store();
416 } else if (kHighUse == info->reuse_count) {
417 rankings_->Remove(entry->rankings(), Rankings::LOW_USE, true);
418 rankings_->Insert(entry->rankings(), false, Rankings::HIGH_USE);
419 entry->entry()->Store();
420 }
421 }
422 }
423
424 void Eviction::OnCreateEntryV2(EntryImpl* entry) {
425 EntryStore* info = entry->entry()->Data();
426 switch (info->state) {
427 case ENTRY_NORMAL: {
428 DCHECK(!info->reuse_count);
429 DCHECK(!info->refetch_count);
430 break;
431 };
432 case ENTRY_EVICTED: {
433 if (info->refetch_count < kint32max)
434 info->refetch_count++;
435
436 if (info->refetch_count > kHighUse && info->reuse_count < kHighUse) {
437 info->reuse_count = kHighUse;
438 } else {
439 info->reuse_count++;
440 }
441 info->state = ENTRY_NORMAL;
442 entry->entry()->Store();
443 rankings_->Remove(entry->rankings(), Rankings::DELETED, true);
444 break;
445 };
446 default:
447 NOTREACHED();
448 }
449
450 rankings_->Insert(entry->rankings(), true, GetListForEntryV2(entry));
451 }
452
453 void Eviction::OnDoomEntryV2(EntryImpl* entry) {
454 EntryStore* info = entry->entry()->Data();
455 if (ENTRY_NORMAL != info->state)
456 return;
457
458 if (entry->LeaveRankingsBehind()) {
459 info->state = ENTRY_DOOMED;
460 entry->entry()->Store();
461 return;
462 }
463
464 rankings_->Remove(entry->rankings(), GetListForEntryV2(entry), true);
465
466 info->state = ENTRY_DOOMED;
467 entry->entry()->Store();
468 rankings_->Insert(entry->rankings(), true, Rankings::DELETED);
469 }
470
471 void Eviction::OnDestroyEntryV2(EntryImpl* entry) {
472 if (entry->LeaveRankingsBehind())
473 return;
474
475 rankings_->Remove(entry->rankings(), Rankings::DELETED, true);
476 }
477
478 Rankings::List Eviction::GetListForEntryV2(EntryImpl* entry) {
479 EntryStore* info = entry->entry()->Data();
480 DCHECK_EQ(ENTRY_NORMAL, info->state);
481
482 if (!info->reuse_count)
483 return Rankings::NO_USE;
484
485 if (info->reuse_count < kHighUse)
486 return Rankings::LOW_USE;
487
488 return Rankings::HIGH_USE;
489 }
490
491 // This is a minimal implementation that just discards the oldest nodes.
492 // TODO(rvargas): Do something better here.
493 void Eviction::TrimDeleted(bool empty) {
494 Trace("*** Trim Deleted ***");
495 if (backend_->disabled_)
496 return;
497
498 TimeTicks start = TimeTicks::Now();
499 Rankings::ScopedRankingsBlock node(rankings_);
500 Rankings::ScopedRankingsBlock next(
501 rankings_, rankings_->GetPrev(node.get(), Rankings::DELETED));
502 int deleted_entries = 0;
503 while (next.get() &&
504 (empty || (deleted_entries < 20 &&
505 (TimeTicks::Now() - start).InMilliseconds() < 20))) {
506 node.reset(next.release());
507 next.reset(rankings_->GetPrev(node.get(), Rankings::DELETED));
508 if (RemoveDeletedNode(node.get()))
509 deleted_entries++;
510 if (test_mode_)
511 break;
512 }
513
514 if (deleted_entries && !empty && ShouldTrimDeleted()) {
515 base::MessageLoop::current()->PostTask(
516 FROM_HERE,
517 base::Bind(&Eviction::TrimDeleted, ptr_factory_.GetWeakPtr(), false));
518 }
519
520 CACHE_UMA(AGE_MS, "TotalTrimDeletedTime", 0, start);
521 CACHE_UMA(COUNTS, "TrimDeletedItems", 0, deleted_entries);
522 Trace("*** Trim Deleted end ***");
523 return;
524 }
525
526 bool Eviction::RemoveDeletedNode(CacheRankingsBlock* node) {
527 EntryImpl* entry = backend_->GetEnumeratedEntry(node, Rankings::DELETED);
528 if (!entry) {
529 Trace("NewEntry failed on Trim 0x%x", node->address().value());
530 return false;
531 }
532
533 bool doomed = (entry->entry()->Data()->state == ENTRY_DOOMED);
534 entry->entry()->Data()->state = ENTRY_DOOMED;
535 entry->DoomImpl();
536 entry->Release();
537 return !doomed;
538 }
539
540 bool Eviction::NodeIsOldEnough(CacheRankingsBlock* node, int list) {
541 if (!node)
542 return false;
543
544 // If possible, we want to keep entries on each list at least kTargetTime
545 // hours. Each successive list on the enumeration has 2x the target time of
546 // the previous list.
547 Time used = Time::FromInternalValue(node->Data()->last_used);
548 int multiplier = 1 << list;
549 return (Time::Now() - used).InHours() > kTargetTime * multiplier;
550 }
551
552 int Eviction::SelectListByLength(Rankings::ScopedRankingsBlock* next) {
553 int data_entries = header_->num_entries -
554 header_->lru.sizes[Rankings::DELETED];
555 // Start by having each list to be roughly the same size.
556 if (header_->lru.sizes[0] > data_entries / 3)
557 return 0;
558
559 int list = (header_->lru.sizes[1] > data_entries / 3) ? 1 : 2;
560
561 // Make sure that frequently used items are kept for a minimum time; we know
562 // that this entry is not older than its current target, but it must be at
563 // least older than the target for list 0 (kTargetTime), as long as we don't
564 // exhaust list 0.
565 if (!NodeIsOldEnough(next[list].get(), 0) &&
566 header_->lru.sizes[0] > data_entries / 10)
567 list = 0;
568
569 return list;
570 }
571
572 void Eviction::ReportListStats() {
573 if (!new_eviction_)
574 return;
575
576 Rankings::ScopedRankingsBlock last1(rankings_,
577 rankings_->GetPrev(NULL, Rankings::NO_USE));
578 Rankings::ScopedRankingsBlock last2(rankings_,
579 rankings_->GetPrev(NULL, Rankings::LOW_USE));
580 Rankings::ScopedRankingsBlock last3(rankings_,
581 rankings_->GetPrev(NULL, Rankings::HIGH_USE));
582 Rankings::ScopedRankingsBlock last4(rankings_,
583 rankings_->GetPrev(NULL, Rankings::DELETED));
584
585 if (last1.get())
586 CACHE_UMA(AGE, "NoUseAge", 0,
587 Time::FromInternalValue(last1.get()->Data()->last_used));
588 if (last2.get())
589 CACHE_UMA(AGE, "LowUseAge", 0,
590 Time::FromInternalValue(last2.get()->Data()->last_used));
591 if (last3.get())
592 CACHE_UMA(AGE, "HighUseAge", 0,
593 Time::FromInternalValue(last3.get()->Data()->last_used));
594 if (last4.get())
595 CACHE_UMA(AGE, "DeletedAge", 0,
596 Time::FromInternalValue(last4.get()->Data()->last_used));
597 }
598
599 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698