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> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 // is left. | 47 // is left. |
| 48 const uint32_t kEvictionMarginDivisor = 20; | 48 const uint32_t kEvictionMarginDivisor = 20; |
| 49 | 49 |
| 50 const uint32_t kBytesInKb = 1024; | 50 const uint32_t kBytesInKb = 1024; |
| 51 | 51 |
| 52 } // namespace | 52 } // namespace |
| 53 | 53 |
| 54 namespace disk_cache { | 54 namespace disk_cache { |
| 55 | 55 |
| 56 EntryMetadata::EntryMetadata() | 56 EntryMetadata::EntryMetadata() |
| 57 : last_used_time_seconds_since_epoch_(0), | 57 : last_used_time_seconds_since_epoch_(0), |
| 58 entry_size_(0) { | 58 entry_size_(0), |
| 59 } | 59 memory_entry_data_(0) {} |
| 60 | 60 |
| 61 EntryMetadata::EntryMetadata(base::Time last_used_time, | 61 EntryMetadata::EntryMetadata(base::Time last_used_time, |
| 62 base::StrictNumeric<uint32_t> entry_size) | 62 base::StrictNumeric<uint32_t> entry_size) |
| 63 : last_used_time_seconds_since_epoch_(0), entry_size_(entry_size) { | 63 : last_used_time_seconds_since_epoch_(0), |
| 64 entry_size_(0), | |
|
gavinp
2017/08/04 18:42:54
Is this needed now? Removing it might actually imp
Maks Orlovich
2017/08/23 19:29:06
Not strictly, no. Probably the same fate should be
| |
| 65 memory_entry_data_(0) { | |
| 66 SetEntrySize(entry_size); // to round/pack properly. | |
| 64 SetLastUsedTime(last_used_time); | 67 SetLastUsedTime(last_used_time); |
| 65 } | 68 } |
| 66 | 69 |
| 67 base::Time EntryMetadata::GetLastUsedTime() const { | 70 base::Time EntryMetadata::GetLastUsedTime() const { |
| 68 // Preserve nullity. | 71 // Preserve nullity. |
| 69 if (last_used_time_seconds_since_epoch_ == 0) | 72 if (last_used_time_seconds_since_epoch_ == 0) |
| 70 return base::Time(); | 73 return base::Time(); |
| 71 | 74 |
| 72 return base::Time::UnixEpoch() + | 75 return base::Time::UnixEpoch() + |
| 73 base::TimeDelta::FromSeconds(last_used_time_seconds_since_epoch_); | 76 base::TimeDelta::FromSeconds(last_used_time_seconds_since_epoch_); |
| 74 } | 77 } |
| 75 | 78 |
| 76 void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) { | 79 void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) { |
| 77 // Preserve nullity. | 80 // Preserve nullity. |
| 78 if (last_used_time.is_null()) { | 81 if (last_used_time.is_null()) { |
| 79 last_used_time_seconds_since_epoch_ = 0; | 82 last_used_time_seconds_since_epoch_ = 0; |
| 80 return; | 83 return; |
| 81 } | 84 } |
| 82 | 85 |
| 83 last_used_time_seconds_since_epoch_ = base::saturated_cast<uint32_t>( | 86 last_used_time_seconds_since_epoch_ = base::saturated_cast<uint32_t>( |
| 84 (last_used_time - base::Time::UnixEpoch()).InSeconds()); | 87 (last_used_time - base::Time::UnixEpoch()).InSeconds()); |
| 85 // Avoid accidental nullity. | 88 // Avoid accidental nullity. |
| 86 if (last_used_time_seconds_since_epoch_ == 0) | 89 if (last_used_time_seconds_since_epoch_ == 0) |
| 87 last_used_time_seconds_since_epoch_ = 1; | 90 last_used_time_seconds_since_epoch_ = 1; |
| 88 } | 91 } |
| 89 | 92 |
| 90 uint32_t EntryMetadata::GetEntrySize() const { | 93 uint32_t EntryMetadata::GetEntrySize() const { |
| 91 return entry_size_; | 94 return entry_size_ << 8; |
| 92 } | 95 } |
| 93 | 96 |
| 94 void EntryMetadata::SetEntrySize(base::StrictNumeric<uint32_t> entry_size) { | 97 void EntryMetadata::SetEntrySize(base::StrictNumeric<uint32_t> entry_size) { |
| 95 entry_size_ = entry_size; | 98 // This should not overflow since we limit entries to 1/8th of the cache. |
| 99 entry_size_ = (static_cast<uint32_t>(entry_size) + 255) >> 8; | |
|
pasko
2017/06/29 16:31:49
would be nice to rename the member to something li
Maks Orlovich
2017/08/23 19:29:06
Done.
| |
| 96 } | 100 } |
| 97 | 101 |
| 98 void EntryMetadata::Serialize(base::Pickle* pickle) const { | 102 void EntryMetadata::Serialize(base::Pickle* pickle) const { |
| 99 DCHECK(pickle); | 103 DCHECK(pickle); |
| 100 int64_t internal_last_used_time = GetLastUsedTime().ToInternalValue(); | 104 int64_t internal_last_used_time = GetLastUsedTime().ToInternalValue(); |
| 101 // If you modify the size of the size of the pickle, be sure to update | 105 // If you modify the size of the size of the pickle, be sure to update |
| 102 // kOnDiskSizeBytes. | 106 // kOnDiskSizeBytes. |
| 107 uint32_t packed_entry_info = (entry_size_ << 8) | memory_entry_data_; | |
| 103 pickle->WriteInt64(internal_last_used_time); | 108 pickle->WriteInt64(internal_last_used_time); |
| 104 pickle->WriteUInt64(entry_size_); | 109 pickle->WriteUInt64(packed_entry_info); |
| 105 } | 110 } |
| 106 | 111 |
| 107 bool EntryMetadata::Deserialize(base::PickleIterator* it) { | 112 bool EntryMetadata::Deserialize(base::PickleIterator* it, |
| 113 bool has_memory_entry_data) { | |
| 108 DCHECK(it); | 114 DCHECK(it); |
| 109 int64_t tmp_last_used_time; | 115 int64_t tmp_last_used_time; |
| 110 uint64_t tmp_entry_size; | 116 uint64_t tmp_entry_size; |
| 111 if (!it->ReadInt64(&tmp_last_used_time) || !it->ReadUInt64(&tmp_entry_size) || | 117 if (!it->ReadInt64(&tmp_last_used_time) || !it->ReadUInt64(&tmp_entry_size) || |
| 112 tmp_entry_size > std::numeric_limits<decltype(entry_size_)>::max()) | 118 tmp_entry_size > std::numeric_limits<decltype(entry_size_)>::max()) |
| 113 return false; | 119 return false; |
| 114 SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); | 120 SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); |
| 115 entry_size_ = static_cast<uint32_t>(tmp_entry_size); | 121 if (has_memory_entry_data) { |
| 122 // tmp_entry_size actually packs entry_size_ and memory_entry_data_. | |
| 123 SetEntrySize(static_cast<uint32_t>(tmp_entry_size & 0xFFFFFF00)); | |
| 124 SetMemoryEntryData(static_cast<uint8_t>(tmp_entry_size & 0xFF)); | |
| 125 } else { | |
| 126 SetEntrySize(static_cast<uint32_t>(tmp_entry_size)); | |
| 127 SetMemoryEntryData(0); | |
| 128 } | |
| 116 return true; | 129 return true; |
| 117 } | 130 } |
| 118 | 131 |
| 119 SimpleIndex::SimpleIndex( | 132 SimpleIndex::SimpleIndex( |
| 120 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, | 133 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, |
| 121 SimpleIndexDelegate* delegate, | 134 SimpleIndexDelegate* delegate, |
| 122 net::CacheType cache_type, | 135 net::CacheType cache_type, |
| 123 std::unique_ptr<SimpleIndexFile> index_file) | 136 std::unique_ptr<SimpleIndexFile> index_file) |
| 124 : delegate_(delegate), | 137 : delegate_(delegate), |
| 125 cache_type_(cache_type), | 138 cache_type_(cache_type), |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 removed_entries_.insert(entry_hash); | 287 removed_entries_.insert(entry_hash); |
| 275 PostponeWritingToDisk(); | 288 PostponeWritingToDisk(); |
| 276 } | 289 } |
| 277 | 290 |
| 278 bool SimpleIndex::Has(uint64_t hash) const { | 291 bool SimpleIndex::Has(uint64_t hash) const { |
| 279 DCHECK(io_thread_checker_.CalledOnValidThread()); | 292 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 280 // If not initialized, always return true, forcing it to go to the disk. | 293 // If not initialized, always return true, forcing it to go to the disk. |
| 281 return !initialized_ || entries_set_.count(hash) > 0; | 294 return !initialized_ || entries_set_.count(hash) > 0; |
| 282 } | 295 } |
| 283 | 296 |
| 297 uint8_t SimpleIndex::GetMemoryEntryData(uint64_t entry_hash) const { | |
| 298 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 299 EntrySet::const_iterator it = entries_set_.find(entry_hash); | |
| 300 if (it == entries_set_.end()) | |
| 301 return 0; | |
| 302 return it->second.GetMemoryEntryData(); | |
| 303 } | |
| 304 | |
| 305 void SimpleIndex::SetMemoryEntryData(uint64_t entry_hash, uint8_t value) { | |
| 306 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 307 EntrySet::iterator it = entries_set_.find(entry_hash); | |
| 308 if (it == entries_set_.end()) | |
| 309 return; | |
| 310 return it->second.SetMemoryEntryData(value); | |
| 311 } | |
| 312 | |
| 284 bool SimpleIndex::UseIfExists(uint64_t entry_hash) { | 313 bool SimpleIndex::UseIfExists(uint64_t entry_hash) { |
| 285 DCHECK(io_thread_checker_.CalledOnValidThread()); | 314 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 286 // Always update the last used time, even if it is during initialization. | 315 // Always update the last used time, even if it is during initialization. |
| 287 // It will be merged later. | 316 // It will be merged later. |
| 288 EntrySet::iterator it = entries_set_.find(entry_hash); | 317 EntrySet::iterator it = entries_set_.find(entry_hash); |
| 289 if (it == entries_set_.end()) | 318 if (it == entries_set_.end()) |
| 290 // If not initialized, always return true, forcing it to go to the disk. | 319 // If not initialized, always return true, forcing it to go to the disk. |
| 291 return !initialized_; | 320 return !initialized_; |
| 292 it->second.SetLastUsedTime(base::Time::Now()); | 321 it->second.SetLastUsedTime(base::Time::Now()); |
| 293 PostponeWritingToDisk(); | 322 PostponeWritingToDisk(); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 403 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_); | 432 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_); |
| 404 } | 433 } |
| 405 | 434 |
| 406 void SimpleIndex::UpdateEntryIteratorSize( | 435 void SimpleIndex::UpdateEntryIteratorSize( |
| 407 EntrySet::iterator* it, | 436 EntrySet::iterator* it, |
| 408 base::StrictNumeric<uint32_t> entry_size) { | 437 base::StrictNumeric<uint32_t> entry_size) { |
| 409 // Update the total cache size with the new entry size. | 438 // Update the total cache size with the new entry size. |
| 410 DCHECK(io_thread_checker_.CalledOnValidThread()); | 439 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 411 DCHECK_GE(cache_size_, (*it)->second.GetEntrySize()); | 440 DCHECK_GE(cache_size_, (*it)->second.GetEntrySize()); |
| 412 cache_size_ -= (*it)->second.GetEntrySize(); | 441 cache_size_ -= (*it)->second.GetEntrySize(); |
| 413 cache_size_ += static_cast<uint32_t>(entry_size); | |
| 414 (*it)->second.SetEntrySize(entry_size); | 442 (*it)->second.SetEntrySize(entry_size); |
| 443 // We use GetEntrySize to get consistent rounding. | |
| 444 cache_size_ += (*it)->second.GetEntrySize(); | |
| 415 } | 445 } |
| 416 | 446 |
| 417 void SimpleIndex::MergeInitializingSet( | 447 void SimpleIndex::MergeInitializingSet( |
| 418 std::unique_ptr<SimpleIndexLoadResult> load_result) { | 448 std::unique_ptr<SimpleIndexLoadResult> load_result) { |
| 419 DCHECK(io_thread_checker_.CalledOnValidThread()); | 449 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 420 DCHECK(load_result->did_load); | 450 DCHECK(load_result->did_load); |
| 421 | 451 |
| 422 EntrySet* index_file_entries = &load_result->entries; | 452 EntrySet* index_file_entries = &load_result->entries; |
| 423 | 453 |
| 424 for (std::unordered_set<uint64_t>::const_iterator it = | 454 for (std::unordered_set<uint64_t>::const_iterator it = |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |
| OLD | NEW |