Part of fix for bug 5523834, backporting cache fixes

Cherry-picking CL
http://src.chromium.org/viewvc/chrome?view=rev&revision=84877

Change-Id: Iaaa408fbf4375570e5d8bbb59314f8bedc7393a1
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 @@
   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 @@
   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 @@
   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 @@
   }
 
   // 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 @@
 
   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 @@
   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 @@
   bool RemoveDeletedNode(CacheRankingsBlock* node);
 
   bool NodeIsOldEnough(CacheRankingsBlock* node, int list);
-  int SelectListByLenght();
+  int SelectListByLength(Rankings::ScopedRankingsBlock* next);
   void ReportListStats();
 
   BackendImpl* backend_;