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

Side by Side Diff: net/disk_cache/simple/simple_index.cc

Issue 2918893002: evict larger entries first (Closed)
Patch Set: use rank instead of time Created 3 years, 6 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
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/simple/simple_index.h" 5 #include "net/disk_cache/simple/simple_index.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/files/file_enumerator.h" 14 #include "base/files/file_enumerator.h"
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/metrics/field_trial.h" 18 #include "base/metrics/field_trial.h"
19 #include "base/numerics/safe_conversions.h" 19 #include "base/numerics/safe_conversions.h"
20 #include "base/pickle.h" 20 #include "base/pickle.h"
21 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_tokenizer.h" 22 #include "base/strings/string_tokenizer.h"
23 #include "base/task_runner.h" 23 #include "base/task_runner.h"
24 #include "base/time/time.h" 24 #include "base/time/time.h"
25 #include "base/trace_event/memory_usage_estimator.h" 25 #include "base/trace_event/memory_usage_estimator.h"
26 #include "net/base/net_errors.h" 26 #include "net/base/net_errors.h"
27 #include "net/disk_cache/simple/simple_entry_format.h" 27 #include "net/disk_cache/simple/simple_entry_format.h"
28 #include "net/disk_cache/simple/simple_experiment.h"
28 #include "net/disk_cache/simple/simple_histogram_macros.h" 29 #include "net/disk_cache/simple/simple_histogram_macros.h"
29 #include "net/disk_cache/simple/simple_index_delegate.h" 30 #include "net/disk_cache/simple/simple_index_delegate.h"
30 #include "net/disk_cache/simple/simple_index_file.h" 31 #include "net/disk_cache/simple/simple_index_file.h"
31 #include "net/disk_cache/simple/simple_synchronous_entry.h" 32 #include "net/disk_cache/simple/simple_synchronous_entry.h"
32 #include "net/disk_cache/simple/simple_util.h" 33 #include "net/disk_cache/simple/simple_util.h"
33 34
34 #if defined(OS_POSIX) 35 #if defined(OS_POSIX)
35 #include <sys/stat.h> 36 #include <sys/stat.h>
36 #include <sys/time.h> 37 #include <sys/time.h>
37 #endif 38 #endif
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 // It will be merged later. 288 // It will be merged later.
288 EntrySet::iterator it = entries_set_.find(entry_hash); 289 EntrySet::iterator it = entries_set_.find(entry_hash);
289 if (it == entries_set_.end()) 290 if (it == entries_set_.end())
290 // If not initialized, always return true, forcing it to go to the disk. 291 // If not initialized, always return true, forcing it to go to the disk.
291 return !initialized_; 292 return !initialized_;
292 it->second.SetLastUsedTime(base::Time::Now()); 293 it->second.SetLastUsedTime(base::Time::Now());
293 PostponeWritingToDisk(); 294 PostponeWritingToDisk();
294 return true; 295 return true;
295 } 296 }
296 297
298 namespace {
299
300 struct EvictionSortHelper {
jkarlin 2017/06/08 14:32:49 Struct needs comment
hubbe 2017/06/08 17:45:03 Done.
301 explicit EvictionSortHelper(
302 const std::pair<const uint64_t, EntryMetadata>* entry)
303 : entry(entry) {}
304 uint32_t SortValue1() const { return entry->second.RawTimeForSorting(); }
305 int64_t SortValue2() const { return sort_value_; }
306 void SetSortValue(uint64_t rank) {
307 sort_value_ = rank * (entry->second.GetEntrySize() +
308 EntryMetadata::kEstimatedEntryOverhead);
jkarlin 2017/06/08 14:32:49 The kEstimatedEntryOverhead bit is strange. Each
hubbe 2017/06/08 17:45:03 Eh? No? sv = rank * (size + overhead) => sv = rank
jkarlin 2017/06/08 18:18:53 Whoops :) Still, where does the size of overhead c
hubbe 2017/06/08 18:23:36 The overhead comes from the block size of most dev
309 }
310
311 const std::pair<const uint64_t, EntryMetadata>* entry;
312 uint64_t sort_value_;
313 };
314
315 } // namespace
316
297 void SimpleIndex::StartEvictionIfNeeded() { 317 void SimpleIndex::StartEvictionIfNeeded() {
298 DCHECK(io_thread_checker_.CalledOnValidThread()); 318 DCHECK(io_thread_checker_.CalledOnValidThread());
299 if (eviction_in_progress_ || cache_size_ <= high_watermark_) 319 if (eviction_in_progress_ || cache_size_ <= high_watermark_)
300 return; 320 return;
301 // Take all live key hashes from the index and sort them by time. 321 // Take all live key hashes from the index and sort them by time.
302 eviction_in_progress_ = true; 322 eviction_in_progress_ = true;
303 eviction_start_time_ = base::TimeTicks::Now(); 323 eviction_start_time_ = base::TimeTicks::Now();
304 SIMPLE_CACHE_UMA( 324 SIMPLE_CACHE_UMA(
305 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_, 325 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_,
306 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb)); 326 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb));
307 SIMPLE_CACHE_UMA( 327 SIMPLE_CACHE_UMA(
308 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_, 328 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_,
309 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb)); 329 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb));
310 330
311 // Flatten for sorting. 331 // Flatten for sorting.
312 std::vector<const std::pair<const uint64_t, EntryMetadata>*> entries; 332 std::vector<EvictionSortHelper> entries;
313 entries.reserve(entries_set_.size()); 333 entries.reserve(entries_set_.size());
314 for (EntrySet::const_iterator i = entries_set_.begin(); 334 for (EntrySet::const_iterator i = entries_set_.begin();
315 i != entries_set_.end(); ++i) { 335 i != entries_set_.end(); ++i) {
316 entries.push_back(&*i); 336 entries.emplace_back(&*i);
317 } 337 }
318 338
319 std::sort(entries.begin(), entries.end(), 339 std::sort(
320 [](const std::pair<const uint64_t, EntryMetadata>* a, 340 entries.begin(), entries.end(),
321 const std::pair<const uint64_t, EntryMetadata>* b) -> bool { 341 [](const EvictionSortHelper& a, const EvictionSortHelper& b) -> bool {
322 return a->second.RawTimeForSorting() < 342 return a.SortValue1() < b.SortValue1();
jkarlin 2017/06/08 14:32:49 If you use > here instead of < then you don't need
hubbe 2017/06/08 17:45:03 That won't work when kSimpleCacheEvictionWithSize
jkarlin 2017/06/08 18:18:53 Acknowledged.
323 b->second.RawTimeForSorting(); 343 });
324 }); 344
345 if (base::FeatureList::IsEnabled(kSimpleCacheEvictionWithSize)) {
346 for (size_t i = 0; i < entries.size(); i++) {
347 entries[i].SetSortValue(entries.size() - i);
348 }
349
350 std::sort(
351 entries.begin(), entries.end(),
352 [](const EvictionSortHelper& a, const EvictionSortHelper& b) -> bool {
353 return a.SortValue2() > b.SortValue2();
354 });
355 }
325 356
326 // Remove as many entries from the index to get below |low_watermark_|, 357 // Remove as many entries from the index to get below |low_watermark_|,
327 // collecting least recently used hashes into |entry_hashes|. 358 // collecting least recently used hashes into |entry_hashes|.
328 std::vector<uint64_t> entry_hashes; 359 std::vector<uint64_t> entry_hashes;
329 std::vector<const std::pair<const uint64_t, EntryMetadata>*>::iterator it = 360 std::vector<EvictionSortHelper>::iterator it = entries.begin();
330 entries.begin();
331 uint64_t evicted_so_far_size = 0; 361 uint64_t evicted_so_far_size = 0;
332 while (evicted_so_far_size < cache_size_ - low_watermark_) { 362 while (evicted_so_far_size < cache_size_ - low_watermark_) {
333 DCHECK(it != entries.end()); 363 DCHECK(it != entries.end());
334 entry_hashes.push_back((*it)->first); 364 entry_hashes.push_back(it->entry->first);
335 evicted_so_far_size += (*it)->second.GetEntrySize(); 365 evicted_so_far_size += it->entry->second.GetEntrySize();
336 ++it; 366 ++it;
337 } 367 }
338 368
339 SIMPLE_CACHE_UMA(COUNTS, 369 SIMPLE_CACHE_UMA(COUNTS,
340 "Eviction.EntryCount", cache_type_, entry_hashes.size()); 370 "Eviction.EntryCount", cache_type_, entry_hashes.size());
341 SIMPLE_CACHE_UMA(TIMES, 371 SIMPLE_CACHE_UMA(TIMES,
342 "Eviction.TimeToSelectEntries", cache_type_, 372 "Eviction.TimeToSelectEntries", cache_type_,
343 base::TimeTicks::Now() - eviction_start_time_); 373 base::TimeTicks::Now() - eviction_start_time_);
344 SIMPLE_CACHE_UMA( 374 SIMPLE_CACHE_UMA(
345 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_, 375 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_,
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 start - last_write_to_disk_); 544 start - last_write_to_disk_);
515 } 545 }
516 } 546 }
517 last_write_to_disk_ = start; 547 last_write_to_disk_ = start;
518 548
519 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, 549 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start,
520 app_on_background_, base::Closure()); 550 app_on_background_, base::Closure());
521 } 551 }
522 552
523 } // namespace disk_cache 553 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698