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/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 |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 // How many milliseconds we delay writing the index to disk since the last cache | 42 // How many milliseconds we delay writing the index to disk since the last cache |
| 42 // operation has happened. | 43 // operation has happened. |
| 43 const int kWriteToDiskDelayMSecs = 20000; | 44 const int kWriteToDiskDelayMSecs = 20000; |
| 44 const int kWriteToDiskOnBackgroundDelayMSecs = 100; | 45 const int kWriteToDiskOnBackgroundDelayMSecs = 100; |
| 45 | 46 |
| 46 // Divides the cache space into this amount of parts to evict when only one part | 47 // Divides the cache space into this amount of parts to evict when only one part |
| 47 // is left. | 48 // is left. |
| 48 const uint32_t kEvictionMarginDivisor = 20; | 49 const uint32_t kEvictionMarginDivisor = 20; |
| 49 | 50 |
| 50 const uint32_t kBytesInKb = 1024; | 51 const uint32_t kBytesInKb = 1024; |
| 51 | 52 |
| 53 // This is added to the size of each entry before using the size | |
| 54 // to determine which entries to evict first. It's basically an | |
| 55 // estimate of the filesystem overhead, but it also serves to flatten | |
| 56 // the curve so that 1-byte entries and 2-byte entries are basically | |
| 57 // treated the same. | |
| 58 static const int kEstimatedEntryOverhead = 512; | |
| 59 | |
| 52 } // namespace | 60 } // namespace |
| 53 | 61 |
| 54 namespace disk_cache { | 62 namespace disk_cache { |
| 55 | 63 |
| 56 EntryMetadata::EntryMetadata() | 64 EntryMetadata::EntryMetadata() |
| 57 : last_used_time_seconds_since_epoch_(0), | 65 : last_used_time_seconds_since_epoch_(0), |
| 58 entry_size_(0) { | 66 entry_size_(0) { |
| 59 } | 67 } |
| 60 | 68 |
| 61 EntryMetadata::EntryMetadata(base::Time last_used_time, | 69 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; | 310 eviction_in_progress_ = true; |
| 303 eviction_start_time_ = base::TimeTicks::Now(); | 311 eviction_start_time_ = base::TimeTicks::Now(); |
| 304 SIMPLE_CACHE_UMA( | 312 SIMPLE_CACHE_UMA( |
| 305 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_, | 313 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_, |
| 306 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb)); | 314 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb)); |
| 307 SIMPLE_CACHE_UMA( | 315 SIMPLE_CACHE_UMA( |
| 308 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_, | 316 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_, |
| 309 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb)); | 317 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb)); |
| 310 | 318 |
| 311 // Flatten for sorting. | 319 // Flatten for sorting. |
| 312 std::vector<const std::pair<const uint64_t, EntryMetadata>*> entries; | 320 using SortHelper = std::pair<uint64_t, const EntrySet::value_type*>; |
|
pasko
2017/07/18 15:30:01
It would be clearer with more concrete types and i
hubbe
2017/07/18 18:27:39
I disagree, but will change it if you insist.
pasko
2017/07/19 14:42:28
OK, after noting that the type I suggested was wro
hubbe
2017/07/19 17:50:33
SortHelper was useful when the code was a lot more
| |
| 321 std::vector<SortHelper> entries; | |
| 313 entries.reserve(entries_set_.size()); | 322 entries.reserve(entries_set_.size()); |
| 323 uint32_t now = (base::Time::Now() - base::Time::UnixEpoch()).InSeconds(); | |
| 324 bool use_size = false; | |
| 325 SimpleExperiment experiment = GetSimpleExperiment(cache_type_); | |
| 326 if (experiment.type == SimpleExperimentType::EVICT_WITH_SIZE && | |
| 327 experiment.param) { | |
|
pasko
2017/07/18 15:30:01
why do we need to check that param is non-zero? I
hubbe
2017/07/18 18:27:40
If we don't check the param, how do we know if we'
pasko
2017/07/19 14:42:28
Oh, I forgot, sorry, thank you for clarification.
| |
| 328 use_size = true; | |
| 329 } | |
| 314 for (EntrySet::const_iterator i = entries_set_.begin(); | 330 for (EntrySet::const_iterator i = entries_set_.begin(); |
| 315 i != entries_set_.end(); ++i) { | 331 i != entries_set_.end(); ++i) { |
| 316 entries.push_back(&*i); | 332 uint64_t sort_value = now - i->second.RawTimeForSorting(); |
| 333 if (use_size) { | |
| 334 // Will not overflow since we're multiplying two 32-bit values and storing | |
| 335 // them in a 64-bit variable. | |
| 336 sort_value *= i->second.GetEntrySize() + kEstimatedEntryOverhead; | |
| 337 } | |
| 338 // Subtract so we don't need a custom comparator. | |
| 339 entries.emplace_back(std::numeric_limits<uint64_t>::max() - sort_value, | |
| 340 &*i); | |
| 317 } | 341 } |
| 318 | 342 |
| 319 std::sort(entries.begin(), entries.end(), | 343 uint64_t evicted_so_far_size = 0; |
| 320 [](const std::pair<const uint64_t, EntryMetadata>* a, | 344 const uint64_t amount_to_evict = cache_size_ - low_watermark_; |
| 321 const std::pair<const uint64_t, EntryMetadata>* b) -> bool { | |
| 322 return a->second.RawTimeForSorting() < | |
| 323 b->second.RawTimeForSorting(); | |
| 324 }); | |
| 325 | |
| 326 // Remove as many entries from the index to get below |low_watermark_|, | |
| 327 // collecting least recently used hashes into |entry_hashes|. | |
| 328 std::vector<uint64_t> entry_hashes; | 345 std::vector<uint64_t> entry_hashes; |
| 329 std::vector<const std::pair<const uint64_t, EntryMetadata>*>::iterator it = | 346 auto begin = entries.begin(); |
|
pasko
2017/07/18 15:30:01
something slightly shorter:
std::sort(std::begin(
hubbe
2017/07/18 18:27:39
Done
pasko
2017/07/19 14:42:28
Acknowledged. Thanks.
| |
| 330 entries.begin(); | 347 auto end = entries.end(); |
| 331 uint64_t evicted_so_far_size = 0; | 348 std::sort(begin, end); |
| 332 while (evicted_so_far_size < cache_size_ - low_watermark_) { | 349 for (auto i = begin; i < end; ++i) { |
| 333 DCHECK(it != entries.end()); | 350 if (evicted_so_far_size >= amount_to_evict) |
| 334 entry_hashes.push_back((*it)->first); | 351 break; |
| 335 evicted_so_far_size += (*it)->second.GetEntrySize(); | 352 evicted_so_far_size += i->second->second.GetEntrySize(); |
| 336 ++it; | 353 entry_hashes.push_back(i->second->first); |
| 337 } | 354 } |
| 338 | 355 |
| 339 SIMPLE_CACHE_UMA(COUNTS_1M, | 356 SIMPLE_CACHE_UMA(COUNTS_1M, |
| 340 "Eviction.EntryCount", cache_type_, entry_hashes.size()); | 357 "Eviction.EntryCount", cache_type_, entry_hashes.size()); |
| 341 SIMPLE_CACHE_UMA(TIMES, | 358 SIMPLE_CACHE_UMA(TIMES, |
| 342 "Eviction.TimeToSelectEntries", cache_type_, | 359 "Eviction.TimeToSelectEntries", cache_type_, |
| 343 base::TimeTicks::Now() - eviction_start_time_); | 360 base::TimeTicks::Now() - eviction_start_time_); |
| 344 SIMPLE_CACHE_UMA( | 361 SIMPLE_CACHE_UMA( |
| 345 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_, | 362 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_, |
| 346 static_cast<base::HistogramBase::Sample>( | 363 static_cast<base::HistogramBase::Sample>( |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 514 start - last_write_to_disk_); | 531 start - last_write_to_disk_); |
| 515 } | 532 } |
| 516 } | 533 } |
| 517 last_write_to_disk_ = start; | 534 last_write_to_disk_ = start; |
| 518 | 535 |
| 519 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, | 536 index_file_->WriteToDisk(reason, entries_set_, cache_size_, start, |
| 520 app_on_background_, base::Closure()); | 537 app_on_background_, base::Closure()); |
| 521 } | 538 } |
| 522 | 539 |
| 523 } // namespace disk_cache | 540 } // namespace disk_cache |
| OLD | NEW |