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

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

Powered by Google App Engine
This is Rietveld 408576698