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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 } // namespace | 76 } // namespace |
77 | 77 |
78 namespace disk_cache { | 78 namespace disk_cache { |
79 | 79 |
80 EntryMetadata::EntryMetadata() | 80 EntryMetadata::EntryMetadata() |
81 : last_used_time_seconds_since_epoch_(0), | 81 : last_used_time_seconds_since_epoch_(0), |
82 entry_size_(0) { | 82 entry_size_(0) { |
83 } | 83 } |
84 | 84 |
85 EntryMetadata::EntryMetadata(base::Time last_used_time, int entry_size) | 85 EntryMetadata::EntryMetadata(base::Time last_used_time, uint64 entry_size) |
86 : last_used_time_seconds_since_epoch_(0), | 86 : last_used_time_seconds_since_epoch_(0), |
87 entry_size_(entry_size) { | 87 entry_size_(base::checked_cast<int32>(entry_size)) { |
88 SetLastUsedTime(last_used_time); | 88 SetLastUsedTime(last_used_time); |
89 } | 89 } |
90 | 90 |
91 base::Time EntryMetadata::GetLastUsedTime() const { | 91 base::Time EntryMetadata::GetLastUsedTime() const { |
92 // Preserve nullity. | 92 // Preserve nullity. |
93 if (last_used_time_seconds_since_epoch_ == 0) | 93 if (last_used_time_seconds_since_epoch_ == 0) |
94 return base::Time(); | 94 return base::Time(); |
95 | 95 |
96 return base::Time::UnixEpoch() + | 96 return base::Time::UnixEpoch() + |
97 base::TimeDelta::FromSeconds(last_used_time_seconds_since_epoch_); | 97 base::TimeDelta::FromSeconds(last_used_time_seconds_since_epoch_); |
98 } | 98 } |
99 | 99 |
100 void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) { | 100 void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) { |
101 // Preserve nullity. | 101 // Preserve nullity. |
102 if (last_used_time.is_null()) { | 102 if (last_used_time.is_null()) { |
103 last_used_time_seconds_since_epoch_ = 0; | 103 last_used_time_seconds_since_epoch_ = 0; |
104 return; | 104 return; |
105 } | 105 } |
106 | 106 |
107 last_used_time_seconds_since_epoch_ = base::checked_cast<uint32>( | 107 last_used_time_seconds_since_epoch_ = base::checked_cast<uint32>( |
108 (last_used_time - base::Time::UnixEpoch()).InSeconds()); | 108 (last_used_time - base::Time::UnixEpoch()).InSeconds()); |
109 // Avoid accidental nullity. | 109 // Avoid accidental nullity. |
110 if (last_used_time_seconds_since_epoch_ == 0) | 110 if (last_used_time_seconds_since_epoch_ == 0) |
111 last_used_time_seconds_since_epoch_ = 1; | 111 last_used_time_seconds_since_epoch_ = 1; |
112 } | 112 } |
113 | 113 |
| 114 uint64 EntryMetadata::GetEntrySize() const { |
| 115 return entry_size_; |
| 116 } |
| 117 |
| 118 void EntryMetadata::SetEntrySize(uint64 entry_size) { |
| 119 entry_size_ = base::checked_cast<int32>(entry_size); |
| 120 } |
| 121 |
114 void EntryMetadata::Serialize(Pickle* pickle) const { | 122 void EntryMetadata::Serialize(Pickle* pickle) const { |
115 DCHECK(pickle); | 123 DCHECK(pickle); |
116 int64 internal_last_used_time = GetLastUsedTime().ToInternalValue(); | 124 int64 internal_last_used_time = GetLastUsedTime().ToInternalValue(); |
117 pickle->WriteInt64(internal_last_used_time); | 125 pickle->WriteInt64(internal_last_used_time); |
118 pickle->WriteUInt64(entry_size_); | 126 pickle->WriteUInt64(entry_size_); |
119 } | 127 } |
120 | 128 |
121 bool EntryMetadata::Deserialize(PickleIterator* it) { | 129 bool EntryMetadata::Deserialize(PickleIterator* it) { |
122 DCHECK(it); | 130 DCHECK(it); |
123 int64 tmp_last_used_time; | 131 int64 tmp_last_used_time; |
124 uint64 tmp_entry_size; | 132 uint64 tmp_entry_size; |
125 if (!it->ReadInt64(&tmp_last_used_time) || !it->ReadUInt64(&tmp_entry_size)) | 133 if (!it->ReadInt64(&tmp_last_used_time) || !it->ReadUInt64(&tmp_entry_size) || |
| 134 tmp_entry_size > static_cast<uint64>(std::numeric_limits<int32>::max())) |
126 return false; | 135 return false; |
127 SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); | 136 SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); |
128 entry_size_ = tmp_entry_size; | 137 entry_size_ = static_cast<int32>(tmp_entry_size); |
129 return true; | 138 return true; |
130 } | 139 } |
131 | 140 |
132 SimpleIndex::SimpleIndex( | 141 SimpleIndex::SimpleIndex( |
133 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, | 142 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, |
134 SimpleIndexDelegate* delegate, | 143 SimpleIndexDelegate* delegate, |
135 net::CacheType cache_type, | 144 net::CacheType cache_type, |
136 scoped_ptr<SimpleIndexFile> index_file) | 145 scoped_ptr<SimpleIndexFile> index_file) |
137 : delegate_(delegate), | 146 : delegate_(delegate), |
138 cache_type_(cache_type), | 147 cache_type_(cache_type), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 181 |
173 SimpleIndexLoadResult* load_result = new SimpleIndexLoadResult(); | 182 SimpleIndexLoadResult* load_result = new SimpleIndexLoadResult(); |
174 scoped_ptr<SimpleIndexLoadResult> load_result_scoped(load_result); | 183 scoped_ptr<SimpleIndexLoadResult> load_result_scoped(load_result); |
175 base::Closure reply = base::Bind( | 184 base::Closure reply = base::Bind( |
176 &SimpleIndex::MergeInitializingSet, | 185 &SimpleIndex::MergeInitializingSet, |
177 AsWeakPtr(), | 186 AsWeakPtr(), |
178 base::Passed(&load_result_scoped)); | 187 base::Passed(&load_result_scoped)); |
179 index_file_->LoadIndexEntries(cache_mtime, reply, load_result); | 188 index_file_->LoadIndexEntries(cache_mtime, reply, load_result); |
180 } | 189 } |
181 | 190 |
182 bool SimpleIndex::SetMaxSize(int max_bytes) { | 191 void SimpleIndex::SetMaxSize(uint64 max_bytes) { |
183 if (max_bytes < 0) | |
184 return false; | |
185 | |
186 // Zero size means use the default. | 192 // Zero size means use the default. |
187 if (!max_bytes) | 193 if (max_bytes) { |
188 return true; | 194 max_size_ = max_bytes; |
189 | 195 high_watermark_ = max_size_ - max_size_ / kEvictionMarginDivisor; |
190 max_size_ = max_bytes; | 196 low_watermark_ = max_size_ - 2 * (max_size_ / kEvictionMarginDivisor); |
191 high_watermark_ = max_size_ - max_size_ / kEvictionMarginDivisor; | 197 } |
192 low_watermark_ = max_size_ - 2 * (max_size_ / kEvictionMarginDivisor); | |
193 return true; | |
194 } | 198 } |
195 | 199 |
196 int SimpleIndex::ExecuteWhenReady(const net::CompletionCallback& task) { | 200 int SimpleIndex::ExecuteWhenReady(const net::CompletionCallback& task) { |
197 DCHECK(io_thread_checker_.CalledOnValidThread()); | 201 DCHECK(io_thread_checker_.CalledOnValidThread()); |
198 if (initialized_) | 202 if (initialized_) |
199 io_thread_->PostTask(FROM_HERE, base::Bind(task, net::OK)); | 203 io_thread_->PostTask(FROM_HERE, base::Bind(task, net::OK)); |
200 else | 204 else |
201 to_run_when_initialized_.push_back(task); | 205 to_run_when_initialized_.push_back(task); |
202 return net::ERR_IO_PENDING; | 206 return net::ERR_IO_PENDING; |
203 } | 207 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 return true; | 283 return true; |
280 } | 284 } |
281 | 285 |
282 void SimpleIndex::StartEvictionIfNeeded() { | 286 void SimpleIndex::StartEvictionIfNeeded() { |
283 DCHECK(io_thread_checker_.CalledOnValidThread()); | 287 DCHECK(io_thread_checker_.CalledOnValidThread()); |
284 if (eviction_in_progress_ || cache_size_ <= high_watermark_) | 288 if (eviction_in_progress_ || cache_size_ <= high_watermark_) |
285 return; | 289 return; |
286 // Take all live key hashes from the index and sort them by time. | 290 // Take all live key hashes from the index and sort them by time. |
287 eviction_in_progress_ = true; | 291 eviction_in_progress_ = true; |
288 eviction_start_time_ = base::TimeTicks::Now(); | 292 eviction_start_time_ = base::TimeTicks::Now(); |
289 SIMPLE_CACHE_UMA(MEMORY_KB, | 293 SIMPLE_CACHE_UMA( |
290 "Eviction.CacheSizeOnStart2", cache_type_, | 294 MEMORY_KB, "Eviction.CacheSizeOnStart2", cache_type_, |
291 cache_size_ / kBytesInKb); | 295 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb)); |
292 SIMPLE_CACHE_UMA(MEMORY_KB, | 296 SIMPLE_CACHE_UMA( |
293 "Eviction.MaxCacheSizeOnStart2", cache_type_, | 297 MEMORY_KB, "Eviction.MaxCacheSizeOnStart2", cache_type_, |
294 max_size_ / kBytesInKb); | 298 static_cast<base::HistogramBase::Sample>(max_size_ / kBytesInKb)); |
295 std::vector<uint64> entry_hashes; | 299 std::vector<uint64> entry_hashes; |
296 entry_hashes.reserve(entries_set_.size()); | 300 entry_hashes.reserve(entries_set_.size()); |
297 for (EntrySet::const_iterator it = entries_set_.begin(), | 301 for (EntrySet::const_iterator it = entries_set_.begin(), |
298 end = entries_set_.end(); it != end; ++it) { | 302 end = entries_set_.end(); it != end; ++it) { |
299 entry_hashes.push_back(it->first); | 303 entry_hashes.push_back(it->first); |
300 } | 304 } |
301 std::sort(entry_hashes.begin(), entry_hashes.end(), | 305 std::sort(entry_hashes.begin(), entry_hashes.end(), |
302 CompareHashesForTimestamp(entries_set_)); | 306 CompareHashesForTimestamp(entries_set_)); |
303 | 307 |
304 // Remove as many entries from the index to get below |low_watermark_|. | 308 // Remove as many entries from the index to get below |low_watermark_|. |
305 std::vector<uint64>::iterator it = entry_hashes.begin(); | 309 std::vector<uint64>::iterator it = entry_hashes.begin(); |
306 uint64 evicted_so_far_size = 0; | 310 uint64 evicted_so_far_size = 0; |
307 while (evicted_so_far_size < cache_size_ - low_watermark_) { | 311 while (evicted_so_far_size < cache_size_ - low_watermark_) { |
308 DCHECK(it != entry_hashes.end()); | 312 DCHECK(it != entry_hashes.end()); |
309 EntrySet::iterator found_meta = entries_set_.find(*it); | 313 EntrySet::iterator found_meta = entries_set_.find(*it); |
310 DCHECK(found_meta != entries_set_.end()); | 314 DCHECK(found_meta != entries_set_.end()); |
311 uint64 to_evict_size = found_meta->second.GetEntrySize(); | 315 evicted_so_far_size += found_meta->second.GetEntrySize(); |
312 evicted_so_far_size += to_evict_size; | |
313 ++it; | 316 ++it; |
314 } | 317 } |
315 | 318 |
316 // Take out the rest of hashes from the eviction list. | 319 // Take out the rest of hashes from the eviction list. |
317 entry_hashes.erase(it, entry_hashes.end()); | 320 entry_hashes.erase(it, entry_hashes.end()); |
318 SIMPLE_CACHE_UMA(COUNTS, | 321 SIMPLE_CACHE_UMA(COUNTS, |
319 "Eviction.EntryCount", cache_type_, entry_hashes.size()); | 322 "Eviction.EntryCount", cache_type_, entry_hashes.size()); |
320 SIMPLE_CACHE_UMA(TIMES, | 323 SIMPLE_CACHE_UMA(TIMES, |
321 "Eviction.TimeToSelectEntries", cache_type_, | 324 "Eviction.TimeToSelectEntries", cache_type_, |
322 base::TimeTicks::Now() - eviction_start_time_); | 325 base::TimeTicks::Now() - eviction_start_time_); |
323 SIMPLE_CACHE_UMA(MEMORY_KB, | 326 SIMPLE_CACHE_UMA( |
324 "Eviction.SizeOfEvicted2", cache_type_, | 327 MEMORY_KB, "Eviction.SizeOfEvicted2", cache_type_, |
325 evicted_so_far_size / kBytesInKb); | 328 static_cast<base::HistogramBase::Sample>( |
| 329 evicted_so_far_size / kBytesInKb)); |
326 | 330 |
327 delegate_->DoomEntries(&entry_hashes, base::Bind(&SimpleIndex::EvictionDone, | 331 delegate_->DoomEntries(&entry_hashes, base::Bind(&SimpleIndex::EvictionDone, |
328 AsWeakPtr())); | 332 AsWeakPtr())); |
329 } | 333 } |
330 | 334 |
331 bool SimpleIndex::UpdateEntrySize(uint64 entry_hash, int entry_size) { | 335 bool SimpleIndex::UpdateEntrySize(uint64 entry_hash, int64 entry_size) { |
332 DCHECK(io_thread_checker_.CalledOnValidThread()); | 336 DCHECK(io_thread_checker_.CalledOnValidThread()); |
333 EntrySet::iterator it = entries_set_.find(entry_hash); | 337 EntrySet::iterator it = entries_set_.find(entry_hash); |
334 if (it == entries_set_.end()) | 338 if (it == entries_set_.end()) |
335 return false; | 339 return false; |
336 | 340 |
337 UpdateEntryIteratorSize(&it, entry_size); | 341 UpdateEntryIteratorSize(&it, entry_size); |
338 PostponeWritingToDisk(); | 342 PostponeWritingToDisk(); |
339 StartEvictionIfNeeded(); | 343 StartEvictionIfNeeded(); |
340 return true; | 344 return true; |
341 } | 345 } |
342 | 346 |
343 void SimpleIndex::EvictionDone(int result) { | 347 void SimpleIndex::EvictionDone(int result) { |
344 DCHECK(io_thread_checker_.CalledOnValidThread()); | 348 DCHECK(io_thread_checker_.CalledOnValidThread()); |
345 | 349 |
346 // Ignore the result of eviction. We did our best. | 350 // Ignore the result of eviction. We did our best. |
347 eviction_in_progress_ = false; | 351 eviction_in_progress_ = false; |
348 SIMPLE_CACHE_UMA(BOOLEAN, "Eviction.Result", cache_type_, result == net::OK); | 352 SIMPLE_CACHE_UMA(BOOLEAN, "Eviction.Result", cache_type_, result == net::OK); |
349 SIMPLE_CACHE_UMA(TIMES, | 353 SIMPLE_CACHE_UMA(TIMES, |
350 "Eviction.TimeToDone", cache_type_, | 354 "Eviction.TimeToDone", cache_type_, |
351 base::TimeTicks::Now() - eviction_start_time_); | 355 base::TimeTicks::Now() - eviction_start_time_); |
352 SIMPLE_CACHE_UMA(MEMORY_KB, | 356 SIMPLE_CACHE_UMA( |
353 "Eviction.SizeWhenDone2", cache_type_, | 357 MEMORY_KB, "Eviction.SizeWhenDone2", cache_type_, |
354 cache_size_ / kBytesInKb); | 358 static_cast<base::HistogramBase::Sample>(cache_size_ / kBytesInKb)); |
355 } | 359 } |
356 | 360 |
357 // static | 361 // static |
358 void SimpleIndex::InsertInEntrySet( | 362 void SimpleIndex::InsertInEntrySet( |
359 uint64 entry_hash, | 363 uint64 entry_hash, |
360 const disk_cache::EntryMetadata& entry_metadata, | 364 const disk_cache::EntryMetadata& entry_metadata, |
361 EntrySet* entry_set) { | 365 EntrySet* entry_set) { |
362 DCHECK(entry_set); | 366 DCHECK(entry_set); |
363 entry_set->insert(std::make_pair(entry_hash, entry_metadata)); | 367 entry_set->insert(std::make_pair(entry_hash, entry_metadata)); |
364 } | 368 } |
365 | 369 |
366 void SimpleIndex::PostponeWritingToDisk() { | 370 void SimpleIndex::PostponeWritingToDisk() { |
367 if (!initialized_) | 371 if (!initialized_) |
368 return; | 372 return; |
369 const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs | 373 const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs |
370 : kWriteToDiskDelayMSecs; | 374 : kWriteToDiskDelayMSecs; |
371 // If the timer is already active, Start() will just Reset it, postponing it. | 375 // If the timer is already active, Start() will just Reset it, postponing it. |
372 write_to_disk_timer_.Start( | 376 write_to_disk_timer_.Start( |
373 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_); | 377 FROM_HERE, base::TimeDelta::FromMilliseconds(delay), write_to_disk_cb_); |
374 } | 378 } |
375 | 379 |
376 void SimpleIndex::UpdateEntryIteratorSize(EntrySet::iterator* it, | 380 void SimpleIndex::UpdateEntryIteratorSize(EntrySet::iterator* it, |
377 int entry_size) { | 381 int64 entry_size) { |
378 // Update the total cache size with the new entry size. | 382 // Update the total cache size with the new entry size. |
379 DCHECK(io_thread_checker_.CalledOnValidThread()); | 383 DCHECK(io_thread_checker_.CalledOnValidThread()); |
380 DCHECK_GE(cache_size_, implicit_cast<uint64>((*it)->second.GetEntrySize())); | 384 DCHECK_GE(cache_size_, (*it)->second.GetEntrySize()); |
381 cache_size_ -= (*it)->second.GetEntrySize(); | 385 cache_size_ -= (*it)->second.GetEntrySize(); |
382 cache_size_ += entry_size; | 386 cache_size_ += entry_size; |
383 (*it)->second.SetEntrySize(entry_size); | 387 (*it)->second.SetEntrySize(entry_size); |
384 } | 388 } |
385 | 389 |
386 void SimpleIndex::MergeInitializingSet( | 390 void SimpleIndex::MergeInitializingSet( |
387 scoped_ptr<SimpleIndexLoadResult> load_result) { | 391 scoped_ptr<SimpleIndexLoadResult> load_result) { |
388 DCHECK(io_thread_checker_.CalledOnValidThread()); | 392 DCHECK(io_thread_checker_.CalledOnValidThread()); |
389 DCHECK(load_result->did_load); | 393 DCHECK(load_result->did_load); |
390 | 394 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 start - last_write_to_disk_); | 471 start - last_write_to_disk_); |
468 } | 472 } |
469 } | 473 } |
470 last_write_to_disk_ = start; | 474 last_write_to_disk_ = start; |
471 | 475 |
472 index_file_->WriteToDisk(entries_set_, cache_size_, | 476 index_file_->WriteToDisk(entries_set_, cache_size_, |
473 start, app_on_background_, base::Closure()); | 477 start, app_on_background_, base::Closure()); |
474 } | 478 } |
475 | 479 |
476 } // namespace disk_cache | 480 } // namespace disk_cache |
OLD | NEW |