diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-14 18:48:15 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-14 18:48:15 +0000 |
commit | e31aab1840df828c907d32b41cc61b024e12e132 (patch) | |
tree | b3e457e98d478a8d6b37bfaedc516f933ff41225 /net | |
parent | df9fd0d0bfef305243b90b193a6211ae64193fd5 (diff) | |
download | chromium_src-e31aab1840df828c907d32b41cc61b024e12e132.zip chromium_src-e31aab1840df828c907d32b41cc61b024e12e132.tar.gz chromium_src-e31aab1840df828c907d32b41cc61b024e12e132.tar.bz2 |
Disk cache: Enable the code that sets the max size automatically.
(still limiting the ceiling).
Review URL: http://codereview.chromium.org/68007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13684 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/disk_cache/backend_impl.cc | 46 | ||||
-rw-r--r-- | net/disk_cache/backend_impl.h | 3 | ||||
-rw-r--r-- | net/disk_cache/backend_unittest.cc | 30 |
3 files changed, 69 insertions, 10 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc index fd39495..d717a83 100644 --- a/net/disk_cache/backend_impl.cc +++ b/net/disk_cache/backend_impl.cc @@ -189,6 +189,32 @@ Backend* CreateCacheBackend(const std::wstring& full_path, bool force, return NULL; } +int PreferedCacheSize(int64 available) { + // If there is not enough space to use kDefaultCacheSize, use 80% of the + // available space. + if (available < kDefaultCacheSize) + return static_cast<int32>(available * 8 / 10); + + // Don't use more than 10% of the available space. + if (available < 10 * kDefaultCacheSize) + return kDefaultCacheSize; + + // Use 10% of the free space until we reach 2.5 * kDefaultCacheSize. + if (available < static_cast<int64>(kDefaultCacheSize) * 25) + return static_cast<int32>(available / 10); + + // After reaching our target size (2.5 * kDefaultCacheSize), attempt to use + // 1% of the availabe space. + if (available < static_cast<int64>(kDefaultCacheSize) * 100) + return kDefaultCacheSize * 5 / 2; + + int64 one_percent = available / 100; + if (one_percent > kint32max) + return kint32max; + + return static_cast<int32>(one_percent); +} + // ------------------------------------------------------------------------ bool BackendImpl::Init() { @@ -878,10 +904,15 @@ bool BackendImpl::InitBackingStore(bool* file_created) { return true; } +// The maximum cache size will be either set explicitly by the caller, or +// calculated by this code. void BackendImpl::AdjustMaxCacheSize(int table_len) { if (max_size_) return; + // If table_len is provided, the index file exists. + DCHECK(!table_len || data_->header.magic); + // The user is not setting the size, let's figure it out. int64 available = base::SysInfo::AmountOfFreeDiskSpace(path_); if (available < 0) { @@ -889,20 +920,15 @@ void BackendImpl::AdjustMaxCacheSize(int table_len) { return; } - // Attempt to use 1% of the disk available for this user. - available /= 100; + if (table_len) + available += data_->header.num_bytes; - if (available < kDefaultCacheSize) - max_size_ = kDefaultCacheSize; - else if (available > kint32max) - max_size_ = kint32max; - else - max_size_ = static_cast<int32>(available); + max_size_ = PreferedCacheSize(available); // Let's not use more than the default size while we tune-up the performance // of bigger caches. TODO(rvargas): remove this limit. - if (max_size_ > kDefaultCacheSize) - max_size_ = kDefaultCacheSize; + if (max_size_ > kDefaultCacheSize * 4) + max_size_ = kDefaultCacheSize * 4; if (!table_len) return; diff --git a/net/disk_cache/backend_impl.h b/net/disk_cache/backend_impl.h index b1b6478..1960125 100644 --- a/net/disk_cache/backend_impl.h +++ b/net/disk_cache/backend_impl.h @@ -247,6 +247,9 @@ class BackendImpl : public Backend { DISALLOW_EVIL_CONSTRUCTORS(BackendImpl); }; +// Returns the prefered max cache size given the available disk space. +int PreferedCacheSize(int64 available); + } // namespace disk_cache #endif // NET_DISK_CACHE_BACKEND_IMPL_H_ diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc index a13762f..b18a5af 100644 --- a/net/disk_cache/backend_unittest.cc +++ b/net/disk_cache/backend_unittest.cc @@ -1040,3 +1040,33 @@ TEST_F(DiskCacheTest, MultipleInstances) { entry->Close(); } } + +// Test the four regions of the curve that determines the max cache size. +TEST_F(DiskCacheTest, AutomaticMaxSize) { + const int kDefaultSize = 80 * 1024 * 1024; + int64 large_size = kDefaultSize; + + EXPECT_EQ(kDefaultSize, disk_cache::PreferedCacheSize(large_size)); + EXPECT_EQ((kDefaultSize / 2) * 8 / 10, + disk_cache::PreferedCacheSize(large_size / 2)); + + EXPECT_EQ(kDefaultSize, disk_cache::PreferedCacheSize(large_size * 2)); + EXPECT_EQ(kDefaultSize, disk_cache::PreferedCacheSize(large_size * 4)); + EXPECT_EQ(kDefaultSize, disk_cache::PreferedCacheSize(large_size * 10)); + + EXPECT_EQ(kDefaultSize * 2, disk_cache::PreferedCacheSize(large_size * 20)); + EXPECT_EQ(kDefaultSize * 5 / 2, + disk_cache::PreferedCacheSize(large_size * 50 / 2)); + + EXPECT_EQ(kDefaultSize * 5 / 2, + disk_cache::PreferedCacheSize(large_size * 51 / 2)); + EXPECT_EQ(kDefaultSize * 5 / 2, + disk_cache::PreferedCacheSize(large_size * 100 / 2)); + EXPECT_EQ(kDefaultSize * 5 / 2, + disk_cache::PreferedCacheSize(large_size * 500 / 2)); + + EXPECT_EQ(kDefaultSize * 6 / 2, + disk_cache::PreferedCacheSize(large_size * 600 / 2)); + EXPECT_EQ(kDefaultSize * 7 / 2, + disk_cache::PreferedCacheSize(large_size * 700 / 2)); +} |