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