summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristian Monsen <kristianm@google.com>2011-11-01 11:46:33 +0000
committerKristian Monsen <kristianm@google.com>2011-11-15 21:50:40 +0000
commita972e26e14066ec50afdaf4582836fe5fc4c190a (patch)
tree7109f8fe4b914739e14753f6cac6afca20cdc53a
parent2c4085b2006233b5e3a3fe507d62642377b5dc2e (diff)
downloadexternal_chromium-a972e26e14066ec50afdaf4582836fe5fc4c190a.zip
external_chromium-a972e26e14066ec50afdaf4582836fe5fc4c190a.tar.gz
external_chromium-a972e26e14066ec50afdaf4582836fe5fc4c190a.tar.bz2
Part of fix for bug 5523834, backporting cache fixes
This is hopefully a fix for bug 5255299 Cherry-picking CL http://src.chromium.org/viewvc/chrome?view=rev&revision=84877 Change-Id: Ie38ecab599eb0d1bdb8a77efd7dd7f416ef26701
-rw-r--r--net/disk_cache/backend_unittest.cc31
-rw-r--r--net/disk_cache/disk_cache_test_base.h8
-rw-r--r--net/disk_cache/eviction.cc35
-rw-r--r--net/disk_cache/eviction.h4
4 files changed, 56 insertions, 22 deletions
diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc
index 96cf9cc..f1775e1 100644
--- a/net/disk_cache/backend_unittest.cc
+++ b/net/disk_cache/backend_unittest.cc
@@ -494,6 +494,37 @@ TEST_F(DiskCacheBackendTest, AppCacheLoad) {
BackendLoad();
}
+TEST_F(DiskCacheBackendTest, NewEvictionTrim) {
+ SetNewEviction();
+ SetDirectMode();
+ InitCache();
+
+ disk_cache::Entry* entry;
+ for (int i = 0; i < 100; i++) {
+ std::string name(StringPrintf("Key %d", i));
+ ASSERT_EQ(net::OK, CreateEntry(name, &entry));
+ entry->Close();
+ if (i < 90) {
+ // Entries 0 to 89 are in list 1; 90 to 99 are in list 0.
+ ASSERT_EQ(net::OK, OpenEntry(name, &entry));
+ entry->Close();
+ }
+ }
+
+ // The first eviction must come from list 1 (10% limit), the second must come
+ // from list 0.
+ TrimForTest(false);
+ EXPECT_NE(net::OK, OpenEntry("Key 0", &entry));
+ TrimForTest(false);
+ EXPECT_NE(net::OK, OpenEntry("Key 90", &entry));
+
+ // Double check that we still have the list tails.
+ ASSERT_EQ(net::OK, OpenEntry("Key 1", &entry));
+ entry->Close();
+ ASSERT_EQ(net::OK, OpenEntry("Key 91", &entry));
+ entry->Close();
+}
+
// Before looking for invalid entries, let's check a valid entry.
void DiskCacheBackendTest::BackendValidEntry() {
SetDirectMode();
diff --git a/net/disk_cache/disk_cache_test_base.h b/net/disk_cache/disk_cache_test_base.h
index f535896..acd3ebd 100644
--- a/net/disk_cache/disk_cache_test_base.h
+++ b/net/disk_cache/disk_cache_test_base.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -107,12 +107,12 @@ class DiskCacheTestWithCache : public DiskCacheTest {
int WriteSparseData(disk_cache::Entry* entry, int64 offset,
net::IOBuffer* buf, int len);
- // Asks the cache to trim a an entry. If |empty| is true, the whole entry is
+ // Asks the cache to trim an entry. If |empty| is true, the whole cache is
// deleted.
void TrimForTest(bool empty);
- // Asks the cache to trim a an entry from the deleted list. If |empty| is
- // true, the whole entry is deleted.
+ // Asks the cache to trim an entry from the deleted list. If |empty| is
+ // true, the whole list is deleted.
void TrimDeletedListForTest(bool empty);
// DiskCacheTest:
diff --git a/net/disk_cache/eviction.cc b/net/disk_cache/eviction.cc
index 6ba053b..f061622 100644
--- a/net/disk_cache/eviction.cc
+++ b/net/disk_cache/eviction.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -116,7 +116,7 @@ void Eviction::TrimCache(bool empty) {
Rankings::ScopedRankingsBlock next(rankings_,
rankings_->GetPrev(node.get(), Rankings::NO_USE));
int target_size = empty ? 0 : max_size_;
- while (header_->num_bytes > target_size && next.get()) {
+ while ((header_->num_bytes > target_size && next.get()) || test_mode_) {
// The iterator could be invalidated within EvictEntry().
if (!next->HasData())
break;
@@ -304,15 +304,8 @@ void Eviction::TrimCacheV2(bool empty) {
}
// If we are not meeting the time targets lets move on to list length.
- if (!empty && Rankings::LAST_ELEMENT == list) {
- list = SelectListByLenght();
- // Make sure that frequently used items are kept for a minimum time; we know
- // that this entry is not older than its current target, but it must be at
- // least older than the target for list 0 (kTargetTime).
- if ((Rankings::HIGH_USE == list || Rankings::LOW_USE == list) &&
- !NodeIsOldEnough(next[list].get(), 0))
- list = 0;
- }
+ if (!empty && Rankings::LAST_ELEMENT == list)
+ list = SelectListByLength(next);
if (empty)
list = 0;
@@ -321,7 +314,8 @@ void Eviction::TrimCacheV2(bool empty) {
int target_size = empty ? 0 : max_size_;
for (; list < kListsToSearch; list++) {
- while (header_->num_bytes > target_size && next[list].get()) {
+ while ((header_->num_bytes > target_size && next[list].get()) ||
+ test_mode_) {
// The iterator could be invalidated within EvictEntry().
if (!next[list]->HasData())
break;
@@ -514,15 +508,24 @@ bool Eviction::NodeIsOldEnough(CacheRankingsBlock* node, int list) {
return (Time::Now() - used).InHours() > kTargetTime * multiplier;
}
-int Eviction::SelectListByLenght() {
+int Eviction::SelectListByLength(Rankings::ScopedRankingsBlock* next) {
int data_entries = header_->num_entries -
header_->lru.sizes[Rankings::DELETED];
// Start by having each list to be roughly the same size.
if (header_->lru.sizes[0] > data_entries / 3)
return 0;
- if (header_->lru.sizes[1] > data_entries / 3)
- return 1;
- return 2;
+
+ int list = (header_->lru.sizes[1] > data_entries / 3) ? 1 : 2;
+
+ // Make sure that frequently used items are kept for a minimum time; we know
+ // that this entry is not older than its current target, but it must be at
+ // least older than the target for list 0 (kTargetTime), as long as we don't
+ // exhaust list 0.
+ if (!NodeIsOldEnough(next[list].get(), 0) &&
+ header_->lru.sizes[0] > data_entries / 10)
+ list = 0;
+
+ return list;
}
void Eviction::ReportListStats() {
diff --git a/net/disk_cache/eviction.h b/net/disk_cache/eviction.h
index 26f88fe..dd69b71 100644
--- a/net/disk_cache/eviction.h
+++ b/net/disk_cache/eviction.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -66,7 +66,7 @@ class Eviction {
bool RemoveDeletedNode(CacheRankingsBlock* node);
bool NodeIsOldEnough(CacheRankingsBlock* node, int list);
- int SelectListByLenght();
+ int SelectListByLength(Rankings::ScopedRankingsBlock* next);
void ReportListStats();
BackendImpl* backend_;