Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/bits.h" | |
| 14 #include "base/files/file_enumerator.h" | 15 #include "base/files/file_enumerator.h" |
| 15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 18 #include "base/metrics/field_trial.h" | 19 #include "base/metrics/field_trial.h" |
| 19 #include "base/numerics/safe_conversions.h" | 20 #include "base/numerics/safe_conversions.h" |
| 20 #include "base/pickle.h" | 21 #include "base/pickle.h" |
| 21 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/strings/string_tokenizer.h" | 23 #include "base/strings/string_tokenizer.h" |
| 23 #include "base/task_runner.h" | 24 #include "base/task_runner.h" |
| 24 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 25 #include "base/trace_event/memory_usage_estimator.h" | 26 #include "base/trace_event/memory_usage_estimator.h" |
| 26 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 27 #include "net/disk_cache/simple/simple_entry_format.h" | 28 #include "net/disk_cache/simple/simple_entry_format.h" |
| 29 #include "net/disk_cache/simple/simple_experiment.h" | |
| 28 #include "net/disk_cache/simple/simple_histogram_macros.h" | 30 #include "net/disk_cache/simple/simple_histogram_macros.h" |
| 29 #include "net/disk_cache/simple/simple_index_delegate.h" | 31 #include "net/disk_cache/simple/simple_index_delegate.h" |
| 30 #include "net/disk_cache/simple/simple_index_file.h" | 32 #include "net/disk_cache/simple/simple_index_file.h" |
| 31 #include "net/disk_cache/simple/simple_synchronous_entry.h" | 33 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
| 32 #include "net/disk_cache/simple/simple_util.h" | 34 #include "net/disk_cache/simple/simple_util.h" |
| 33 | 35 |
| 34 #if defined(OS_POSIX) | 36 #if defined(OS_POSIX) |
| 35 #include <sys/stat.h> | 37 #include <sys/stat.h> |
| 36 #include <sys/time.h> | 38 #include <sys/time.h> |
| 37 #endif | 39 #endif |
| 38 | 40 |
| 39 namespace { | 41 namespace { |
| 40 | 42 |
| 41 // How many milliseconds we delay writing the index to disk since the last cache | 43 // How many milliseconds we delay writing the index to disk since the last cache |
| 42 // operation has happened. | 44 // operation has happened. |
| 43 const int kWriteToDiskDelayMSecs = 20000; | 45 const int kWriteToDiskDelayMSecs = 20000; |
| 44 const int kWriteToDiskOnBackgroundDelayMSecs = 100; | 46 const int kWriteToDiskOnBackgroundDelayMSecs = 100; |
| 45 | 47 |
| 46 // Divides the cache space into this amount of parts to evict when only one part | 48 // Divides the cache space into this amount of parts to evict when only one part |
| 47 // is left. | 49 // is left. |
| 48 const uint32_t kEvictionMarginDivisor = 20; | 50 const uint32_t kEvictionMarginDivisor = 20; |
| 49 | 51 |
| 50 const uint32_t kBytesInKb = 1024; | 52 const uint32_t kBytesInKb = 1024; |
| 51 | 53 |
| 54 // This is added to the size of each entry before using the size | |
| 55 // to determine which entries to evict first. It's basically an | |
| 56 // estimate of the filesystem overhead, but it also serves to flatten | |
| 57 // the curve so that 1-byte entries and 2-byte entries are basically | |
| 58 // treated the same. | |
| 59 static const int kEstimatedEntryOverhead = 512; | |
| 60 | |
| 52 } // namespace | 61 } // namespace |
| 53 | 62 |
| 54 namespace disk_cache { | 63 namespace disk_cache { |
| 55 | 64 |
| 56 EntryMetadata::EntryMetadata() | 65 EntryMetadata::EntryMetadata() |
| 57 : last_used_time_seconds_since_epoch_(0), | 66 : last_used_time_seconds_since_epoch_(0), |
| 58 entry_size_(0) { | 67 entry_size_(0) { |
| 59 } | 68 } |
| 60 | 69 |
| 61 EntryMetadata::EntryMetadata(base::Time last_used_time, | 70 EntryMetadata::EntryMetadata(base::Time last_used_time, |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 eviction_in_progress_ = true; | 311 eviction_in_progress_ = true; |
| 303 eviction_start_time_ = base::TimeTicks::Now(); | 312 eviction_start_time_ = base::TimeTicks::Now(); |
| 304 SIMPLE_CACHE_UMA( | 313 SIMPLE_CACHE_UMA( |
| 305 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_, | 314 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_, |
| 306 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb)); | 315 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb)); |
| 307 SIMPLE_CACHE_UMA( | 316 SIMPLE_CACHE_UMA( |
| 308 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_, | 317 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_, |
| 309 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb)); | 318 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb)); |
| 310 | 319 |
| 311 // Flatten for sorting. | 320 // Flatten for sorting. |
| 312 std::vector<const std::pair<const uint64_t, EntryMetadata>*> entries; | 321 typedef std::pair<uint64_t, const EntrySet::value_type*> SortHelper; |
| 322 std::vector<SortHelper> entries; | |
| 313 entries.reserve(entries_set_.size()); | 323 entries.reserve(entries_set_.size()); |
| 324 uint32_t now = (base::Time::Now() - base::Time::UnixEpoch()).InSeconds(); | |
| 325 bool use_size = base::FeatureList::IsEnabled(kSimpleCacheEvictionWithSize); | |
|
jkarlin
2017/07/06 13:58:18
We shouldn't use base::FeatureList::IsEnabled for
hubbe
2017/07/10 18:23:53
Done.
Also changed the experiment enabling code to
| |
| 314 for (EntrySet::const_iterator i = entries_set_.begin(); | 326 for (EntrySet::const_iterator i = entries_set_.begin(); |
| 315 i != entries_set_.end(); ++i) { | 327 i != entries_set_.end(); ++i) { |
| 316 entries.push_back(&*i); | 328 uint64_t sort_value = now - i->second.RawTimeForSorting(); |
| 329 if (use_size) { | |
| 330 // Will not overflow since we're multiplying two 32-bit values and storing | |
| 331 // them in a 64-bit variable. | |
| 332 sort_value *= i->second.GetEntrySize() + kEstimatedEntryOverhead; | |
| 333 } | |
| 334 // Subtract so we don't need a custom comparator. | |
| 335 entries.emplace_back(std::numeric_limits<uint64_t>::max() - sort_value, | |
| 336 &*i); | |
| 317 } | 337 } |
| 318 | 338 |
| 319 std::sort(entries.begin(), entries.end(), | 339 // introselect algorithm: |
| 320 [](const std::pair<const uint64_t, EntryMetadata>* a, | 340 // Pick a pivot element, partition elements into elements |
| 321 const std::pair<const uint64_t, EntryMetadata>* b) -> bool { | 341 // smaller than the pivot and not smaller than pivot. |
| 322 return a->second.RawTimeForSorting() < | 342 // Then sum all the sizes from the "smaller" bucket. If it |
| 323 b->second.RawTimeForSorting(); | 343 // is still too large, then iterate again, but only consider |
| 324 }); | 344 // elements from the "smaller" bucket. If not, add all elements |
| 345 // in the "smaller" bucket to the evict list. | |
| 346 // Then iterate again, to partition the "not smaller" bucket. | |
| 347 // If we go over 2 * log2(|entries|) iterations, resort to calling | |
| 348 // std::sort() to avoid N-squared worst case scenario. | |
| 349 uint64_t evicted_so_far_size = 0; | |
| 350 const uint64_t amount_to_evict = cache_size_ - low_watermark_; | |
| 351 std::vector<uint64_t> entry_hashes; | |
| 352 int loops = 2 * base::bits::Log2Ceiling(entries.size()); | |
| 353 auto begin = entries.begin(); | |
| 354 auto end = entries.end(); | |
| 355 while (loops > 0 && evicted_so_far_size < amount_to_evict && | |
| 356 end - begin >= 3) { | |
| 357 loops--; | |
| 325 | 358 |
| 326 // Remove as many entries from the index to get below |low_watermark_|, | 359 // Select a pivot. |
| 327 // collecting least recently used hashes into |entry_hashes|. | 360 uint64_t tmp[3]; |
| 328 std::vector<uint64_t> entry_hashes; | 361 tmp[0] = begin->first; |
| 329 std::vector<const std::pair<const uint64_t, EntryMetadata>*>::iterator it = | 362 tmp[1] = (end - 1)->first; |
| 330 entries.begin(); | 363 tmp[2] = (begin + (end - begin) / 2)->first; |
| 331 uint64_t evicted_so_far_size = 0; | 364 std::sort(tmp, tmp + 3); |
| 332 while (evicted_so_far_size < cache_size_ - low_watermark_) { | 365 uint64_t pivot = tmp[1]; |
| 333 DCHECK(it != entries.end()); | 366 |
| 334 entry_hashes.push_back((*it)->first); | 367 // Partition |
| 335 evicted_so_far_size += (*it)->second.GetEntrySize(); | 368 auto middle = std::partition(begin, end, [pivot](const SortHelper& entry) { |
| 336 ++it; | 369 return entry.first < pivot; |
| 370 }); | |
| 371 if (middle == end || middle == begin) { | |
| 372 // No progress will be made. | |
| 373 break; | |
| 374 } | |
| 375 uint64_t lt_sum = 0; | |
| 376 for (auto i = begin; i != middle; ++i) | |
| 377 lt_sum += i->second->second.GetEntrySize(); | |
| 378 | |
| 379 if (evicted_so_far_size + lt_sum > amount_to_evict) { | |
| 380 end = middle; | |
| 381 } else { | |
| 382 evicted_so_far_size += lt_sum; | |
| 383 for (auto i = begin; i != middle; ++i) | |
| 384 entry_hashes.push_back(i->second->first); | |
| 385 | |
| 386 begin = middle; | |
| 387 } | |
| 388 } | |
| 389 | |
| 390 if (end > begin && evicted_so_far_size < amount_to_evict) { | |
| 391 std::sort(begin, end); | |
| 392 for (auto i = begin; i < end; ++i) { | |
| 393 if (evicted_so_far_size >= amount_to_evict) | |
| 394 break; | |
| 395 evicted_so_far_size += i->second->second.GetEntrySize(); | |
| 396 entry_hashes.push_back(i->second->first); | |
| 397 } | |
| 337 } | 398 } |
| 338 | 399 |
| 339 SIMPLE_CACHE_UMA(COUNTS_1M, | 400 SIMPLE_CACHE_UMA(COUNTS_1M, |
| 340 "Eviction.EntryCount", cache_type_, entry_hashes.size()); | 401 "Eviction.EntryCount", cache_type_, entry_hashes.size()); |
| 341 SIMPLE_CACHE_UMA(TIMES, | 402 SIMPLE_CACHE_UMA(TIMES, |
| 342 "Eviction.TimeToSelectEntries", cache_type_, | 403 "Eviction.TimeToSelectEntries", cache_type_, |
| 343 base::TimeTicks::Now() - eviction_start_time_); | 404 base::TimeTicks::Now() - eviction_start_time_); |
| 344 SIMPLE_CACHE_UMA( | 405 SIMPLE_CACHE_UMA( |
| 345 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_, | 406 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_, |
| 346 static_cast<base::HistogramBase::Sample>( | 407 static_cast<base::HistogramBase::Sample>( |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 514 start - last_write_to_disk_); | 575 start - last_write_to_disk_); |
| 515 } | 576 } |
| 516 } | 577 } |
| 517 last_write_to_disk_ = start; | 578 last_write_to_disk_ = start; |
| 518 | 579 |
| 519 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, | 580 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, |
| 520 app_on_background_, base::Closure()); | 581 app_on_background_, base::Closure()); |
| 521 } | 582 } |
| 522 | 583 |
| 523 } // namespace disk_cache | 584 } // namespace disk_cache |
| OLD | NEW |