diff options
author | pasko@chromium.org <pasko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-19 18:41:37 +0000 |
---|---|---|
committer | pasko@chromium.org <pasko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-19 18:41:37 +0000 |
commit | 18b5b2694e20d489d62a3eb83907200520cb6598 (patch) | |
tree | e3706f2e69982aa5ca3571413dc9f93f5190824c | |
parent | e13279ba892ffb653e57ba9db4e1b9aca54a9962 (diff) | |
download | chromium_src-18b5b2694e20d489d62a3eb83907200520cb6598.zip chromium_src-18b5b2694e20d489d62a3eb83907200520cb6598.tar.gz chromium_src-18b5b2694e20d489d62a3eb83907200520cb6598.tar.bz2 |
Cleanup: Move PreferredCacheSize() to cache_util.h
BUG=173425
Review URL: https://codereview.chromium.org/50373004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@236022 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/disk_cache/backend_impl.cc | 39 | ||||
-rw-r--r-- | net/disk_cache/backend_impl.h | 3 | ||||
-rw-r--r-- | net/disk_cache/backend_unittest.cc | 70 | ||||
-rw-r--r-- | net/disk_cache/cache_util.cc | 44 | ||||
-rw-r--r-- | net/disk_cache/cache_util.h | 7 | ||||
-rw-r--r-- | net/disk_cache/mem_backend_impl.cc | 8 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_backend_impl.cc | 12 |
7 files changed, 93 insertions, 90 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc index 1071757..6a0d124 100644 --- a/net/disk_cache/backend_impl.cc +++ b/net/disk_cache/backend_impl.cc @@ -45,7 +45,6 @@ const char* kIndexName = "index"; // for most users. const int k64kEntriesStore = 240 * 1000 * 1000; const int kBaseTableLen = 64 * 1024; -const int kDefaultCacheSize = 80 * 1024 * 1024; // Avoid trimming the cache for the first 5 minutes (10 timer ticks). const int kTrimDelay = 10; @@ -108,37 +107,6 @@ void FinalCleanupCallback(disk_cache::BackendImpl* backend) { namespace disk_cache { -// Returns the preferred maximum number of bytes for the cache given the -// number of available bytes. -int PreferedCacheSize(int64 available) { - // Return 80% of the available space if there is not enough space to use - // kDefaultCacheSize. - if (available < kDefaultCacheSize * 10 / 8) - return static_cast<int32>(available * 8 / 10); - - // Return kDefaultCacheSize if it uses 80% to 10% of the available space. - if (available < kDefaultCacheSize * 10) - return kDefaultCacheSize; - - // Return 10% of the available space if the target size - // (2.5 * kDefaultCacheSize) is more than 10%. - if (available < static_cast<int64>(kDefaultCacheSize) * 25) - return static_cast<int32>(available / 10); - - // Return the target size (2.5 * kDefaultCacheSize) if it uses 10% to 1% - // of the available space. - if (available < static_cast<int64>(kDefaultCacheSize) * 250) - return kDefaultCacheSize * 5 / 2; - - // Return 1% of the available space if it does not exceed kint32max. - if (available < static_cast<int64>(kint32max) * 100) - return static_cast<int32>(available / 100); - - return kint32max; -} - -// ------------------------------------------------------------------------ - BackendImpl::BackendImpl(const base::FilePath& path, base::MessageLoopProxy* cache_thread, net::NetLog* net_log) @@ -1338,12 +1306,7 @@ void BackendImpl::AdjustMaxCacheSize(int table_len) { if (table_len) available += data_->header.num_bytes; - 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 * 4) - max_size_ = kDefaultCacheSize * 4; + max_size_ = PreferredCacheSize(available); if (!table_len) return; diff --git a/net/disk_cache/backend_impl.h b/net/disk_cache/backend_impl.h index 982bee6..61b95b3 100644 --- a/net/disk_cache/backend_impl.h +++ b/net/disk_cache/backend_impl.h @@ -392,9 +392,6 @@ class NET_EXPORT_PRIVATE BackendImpl : public Backend { DISALLOW_COPY_AND_ASSIGN(BackendImpl); }; -// Returns the preferred max cache size given the available disk space. -NET_EXPORT_PRIVATE 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 ef9dd16..c7b37e4 100644 --- a/net/disk_cache/backend_unittest.cc +++ b/net/disk_cache/backend_unittest.cc @@ -2898,51 +2898,51 @@ TEST_F(DiskCacheTest, MultipleInstances) { // Test the six regions of the curve that determines the max cache size. TEST_F(DiskCacheTest, AutomaticMaxSize) { - const int kDefaultSize = 80 * 1024 * 1024; - int64 large_size = kDefaultSize; - int64 largest_size = kint32max; + using disk_cache::kDefaultCacheSize; + int64 large_size = kDefaultCacheSize; // Region 1: expected = available * 0.8 - EXPECT_EQ((kDefaultSize - 1) * 8 / 10, - disk_cache::PreferedCacheSize(large_size - 1)); - EXPECT_EQ(kDefaultSize * 8 / 10, - disk_cache::PreferedCacheSize(large_size)); - EXPECT_EQ(kDefaultSize - 1, - disk_cache::PreferedCacheSize(large_size * 10 / 8 - 1)); + EXPECT_EQ((kDefaultCacheSize - 1) * 8 / 10, + disk_cache::PreferredCacheSize(large_size - 1)); + EXPECT_EQ(kDefaultCacheSize * 8 / 10, + disk_cache::PreferredCacheSize(large_size)); + EXPECT_EQ(kDefaultCacheSize - 1, + disk_cache::PreferredCacheSize(large_size * 10 / 8 - 1)); // Region 2: expected = default_size - EXPECT_EQ(kDefaultSize, - disk_cache::PreferedCacheSize(large_size * 10 / 8)); - EXPECT_EQ(kDefaultSize, - disk_cache::PreferedCacheSize(large_size * 10 - 1)); + EXPECT_EQ(kDefaultCacheSize, + disk_cache::PreferredCacheSize(large_size * 10 / 8)); + EXPECT_EQ(kDefaultCacheSize, + disk_cache::PreferredCacheSize(large_size * 10 - 1)); // Region 3: expected = available * 0.1 - EXPECT_EQ(kDefaultSize, - disk_cache::PreferedCacheSize(large_size * 10)); - EXPECT_EQ((kDefaultSize * 25 - 1) / 10, - disk_cache::PreferedCacheSize(large_size * 25 - 1)); + EXPECT_EQ(kDefaultCacheSize, + disk_cache::PreferredCacheSize(large_size * 10)); + EXPECT_EQ((kDefaultCacheSize * 25 - 1) / 10, + disk_cache::PreferredCacheSize(large_size * 25 - 1)); // Region 4: expected = default_size * 2.5 - EXPECT_EQ(kDefaultSize * 25 / 10, - disk_cache::PreferedCacheSize(large_size * 25)); - EXPECT_EQ(kDefaultSize * 25 / 10, - disk_cache::PreferedCacheSize(large_size * 100 - 1)); - EXPECT_EQ(kDefaultSize * 25 / 10, - disk_cache::PreferedCacheSize(large_size * 100)); - EXPECT_EQ(kDefaultSize * 25 / 10, - disk_cache::PreferedCacheSize(large_size * 250 - 1)); + EXPECT_EQ(kDefaultCacheSize * 25 / 10, + disk_cache::PreferredCacheSize(large_size * 25)); + EXPECT_EQ(kDefaultCacheSize * 25 / 10, + disk_cache::PreferredCacheSize(large_size * 100 - 1)); + EXPECT_EQ(kDefaultCacheSize * 25 / 10, + disk_cache::PreferredCacheSize(large_size * 100)); + EXPECT_EQ(kDefaultCacheSize * 25 / 10, + disk_cache::PreferredCacheSize(large_size * 250 - 1)); // Region 5: expected = available * 0.1 - EXPECT_EQ(kDefaultSize * 25 / 10, - disk_cache::PreferedCacheSize(large_size * 250)); - EXPECT_EQ(kint32max - 1, - disk_cache::PreferedCacheSize(largest_size * 100 - 1)); - - // Region 6: expected = kint32max - EXPECT_EQ(kint32max, - disk_cache::PreferedCacheSize(largest_size * 100)); - EXPECT_EQ(kint32max, - disk_cache::PreferedCacheSize(largest_size * 10000)); + int64 largest_size = kDefaultCacheSize * 4; + EXPECT_EQ(kDefaultCacheSize * 25 / 10, + disk_cache::PreferredCacheSize(large_size * 250)); + EXPECT_EQ(largest_size - 1, + disk_cache::PreferredCacheSize(largest_size * 100 - 1)); + + // Region 6: expected = largest possible size + EXPECT_EQ(largest_size, + disk_cache::PreferredCacheSize(largest_size * 100)); + EXPECT_EQ(largest_size, + disk_cache::PreferredCacheSize(largest_size * 10000)); } // Tests that we can "migrate" a running instance from one experiment group to diff --git a/net/disk_cache/cache_util.cc b/net/disk_cache/cache_util.cc index 7389960..4452e7b 100644 --- a/net/disk_cache/cache_util.cc +++ b/net/disk_cache/cache_util.cc @@ -48,10 +48,37 @@ base::FilePath GetTempCacheName(const base::FilePath& path, return base::FilePath(); } +int64 PreferredCacheSizeInternal(int64 available) { + using disk_cache::kDefaultCacheSize; + // Return 80% of the available space if there is not enough space to use + // kDefaultCacheSize. + if (available < kDefaultCacheSize * 10 / 8) + return available * 8 / 10; + + // Return kDefaultCacheSize if it uses 10% to 80% of the available space. + if (available < kDefaultCacheSize * 10) + return kDefaultCacheSize; + + // Return 10% of the available space if the target size + // (2.5 * kDefaultCacheSize) is more than 10%. + if (available < static_cast<int64>(kDefaultCacheSize) * 25) + return available / 10; + + // Return the target size (2.5 * kDefaultCacheSize) if it uses 10% to 1% + // of the available space. + if (available < static_cast<int64>(kDefaultCacheSize) * 250) + return kDefaultCacheSize * 5 / 2; + + // Return 1% of the available space. + return available / 100; +} + } // namespace namespace disk_cache { +const int kDefaultCacheSize = 80 * 1024 * 1024; + void DeleteCache(const base::FilePath& path, bool remove_folder) { if (remove_folder) { if (!base::DeleteFile(path, /* recursive */ true)) @@ -111,4 +138,21 @@ bool DelayedCacheCleanup(const base::FilePath& full_path) { return true; } +// Returns the preferred maximum number of bytes for the cache given the +// number of available bytes. +int PreferredCacheSize(int64 available) { + if (available < 0) + return kDefaultCacheSize; + + int64 max_size = PreferredCacheSizeInternal(available); + + // Limit cache size to somewhat less than kint32max to avoid potential + // integer overflows in cache backend implementations. + DCHECK(kDefaultCacheSize * 4 < kint32max); + if (max_size > kDefaultCacheSize * 4) + max_size = kDefaultCacheSize * 4; + + return implicit_cast<int32>(max_size); +} + } // namespace disk_cache diff --git a/net/disk_cache/cache_util.h b/net/disk_cache/cache_util.h index 2005ba5..c4baa2d 100644 --- a/net/disk_cache/cache_util.h +++ b/net/disk_cache/cache_util.h @@ -36,6 +36,13 @@ NET_EXPORT_PRIVATE bool DeleteCacheFile(const base::FilePath& name); // task. Used by cache creator itself or by backends for self-restart on error. bool DelayedCacheCleanup(const base::FilePath& full_path); +// Returns the preferred max cache size given the available disk space. +NET_EXPORT_PRIVATE int PreferredCacheSize(int64 available); + +// The default cache size should not ideally be exposed, but the blockfile +// backend uses it for reasons that include testing. +NET_EXPORT_PRIVATE extern const int kDefaultCacheSize; + } // namespace disk_cache #endif // NET_DISK_CACHE_CACHE_UTIL_H_ diff --git a/net/disk_cache/mem_backend_impl.cc b/net/disk_cache/mem_backend_impl.cc index a6f1bf1..ccd868b 100644 --- a/net/disk_cache/mem_backend_impl.cc +++ b/net/disk_cache/mem_backend_impl.cc @@ -14,7 +14,7 @@ using base::Time; namespace { -const int kDefaultCacheSize = 10 * 1024 * 1024; +const int kDefaultInMemoryCacheSize = 10 * 1024 * 1024; const int kCleanUpMargin = 1024 * 1024; int LowWaterAdjust(int high_water) { @@ -59,15 +59,15 @@ bool MemBackendImpl::Init() { int64 total_memory = base::SysInfo::AmountOfPhysicalMemory(); if (total_memory <= 0) { - max_size_ = kDefaultCacheSize; + max_size_ = kDefaultInMemoryCacheSize; return true; } // We want to use up to 2% of the computer's memory, with a limit of 50 MB, // reached on systemd with more than 2.5 GB of RAM. total_memory = total_memory * 2 / 100; - if (total_memory > kDefaultCacheSize * 5) - max_size_ = kDefaultCacheSize * 5; + if (total_memory > kDefaultInMemoryCacheSize * 5) + max_size_ = kDefaultInMemoryCacheSize * 5; else max_size_ = static_cast<int32>(total_memory); diff --git a/net/disk_cache/simple/simple_backend_impl.cc b/net/disk_cache/simple/simple_backend_impl.cc index 90dfc54..1165da6 100644 --- a/net/disk_cache/simple/simple_backend_impl.cc +++ b/net/disk_cache/simple/simple_backend_impl.cc @@ -26,7 +26,7 @@ #include "base/threading/sequenced_worker_pool.h" #include "base/time/time.h" #include "net/base/net_errors.h" -#include "net/disk_cache/backend_impl.h" +#include "net/disk_cache/cache_util.h" #include "net/disk_cache/simple/simple_entry_format.h" #include "net/disk_cache/simple/simple_entry_impl.h" #include "net/disk_cache/simple/simple_histogram_macros.h" @@ -56,9 +56,6 @@ const int kDefaultMaxWorkerThreads = 50; const char kThreadNamePrefix[] = "SimpleCache"; -// Cache size when all other size heuristics failed. -const uint64 kDefaultCacheSize = 80 * 1024 * 1024; - // Maximum fraction of the cache that one entry can consume. const int kMaxFileRatio = 8; @@ -502,12 +499,7 @@ SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk( DCHECK(mtime_result); if (!result.max_size) { int64 available = base::SysInfo::AmountOfFreeDiskSpace(path); - if (available < 0) - result.max_size = kDefaultCacheSize; - else - // TODO(pasko): Move PreferedCacheSize() to cache_util.h. Also fix the - // spelling. - result.max_size = disk_cache::PreferedCacheSize(available); + result.max_size = disk_cache::PreferredCacheSize(available); } DCHECK(result.max_size); } |