summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorpasko@google.com <pasko@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-24 15:56:28 +0000
committerpasko@google.com <pasko@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-24 15:56:28 +0000
commit231274fcb382c876c9f3654dacdbd00d3c9b1df7 (patch)
tree28cab183ccc4fe78a7faa221de56df1816364d41 /net
parent1b39a6a65701bc52f6f27d8190c80a9a5dfe67f6 (diff)
downloadchromium_src-231274fcb382c876c9f3654dacdbd00d3c9b1df7.zip
chromium_src-231274fcb382c876c9f3654dacdbd00d3c9b1df7.tar.gz
chromium_src-231274fcb382c876c9f3654dacdbd00d3c9b1df7.tar.bz2
Simple Cache: move cache size estimation to cache thread
There was a bug with accessing the filesystem with SysInfo::AmountOfFreeDiskSpace() on IO thread. This was because I misread the assertion in the function. Fix it to estimate the max size together with providing the directory for the cache. BUG=none Review URL: https://chromiumcodereview.appspot.com/13843018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@196158 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/disk_cache/simple/simple_backend_impl.cc51
-rw-r--r--net/disk_cache/simple/simple_backend_impl.h17
-rw-r--r--net/disk_cache/simple/simple_index.cc22
-rw-r--r--net/disk_cache/simple/simple_index.h8
4 files changed, 54 insertions, 44 deletions
diff --git a/net/disk_cache/simple/simple_backend_impl.cc b/net/disk_cache/simple/simple_backend_impl.cc
index a8de799..5061157 100644
--- a/net/disk_cache/simple/simple_backend_impl.cc
+++ b/net/disk_cache/simple/simple_backend_impl.cc
@@ -9,8 +9,10 @@
#include "base/file_util.h"
#include "base/location.h"
#include "base/message_loop_proxy.h"
+#include "base/sys_info.h"
#include "base/threading/worker_pool.h"
#include "net/base/net_errors.h"
+#include "net/disk_cache/backend_impl.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_index.h"
@@ -27,6 +29,9 @@ using file_util::CreateDirectory;
namespace {
+// Cache size when all other size heuristics failed.
+const uint64 kDefaultCacheSize = 80 * 1024 * 1024;
+
// Must run on IO Thread.
void DeleteBackendImpl(disk_cache::Backend** backend,
const net::CompletionCallback& callback,
@@ -121,9 +126,10 @@ SimpleBackendImpl::SimpleBackendImpl(
: path_(path),
index_(new SimpleIndex(cache_thread,
MessageLoopProxy::current(), // io_thread
- path,
- max_bytes)),
- cache_thread_(cache_thread) {}
+ path)),
+ cache_thread_(cache_thread),
+ orig_max_size_(max_bytes) {
+}
SimpleBackendImpl::~SimpleBackendImpl() {
index_->WriteToDisk();
@@ -134,15 +140,18 @@ int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) {
base::Bind(&SimpleBackendImpl::InitializeIndex,
base::Unretained(this),
completion_callback);
- cache_thread_->PostTask(FROM_HERE,
- base::Bind(&SimpleBackendImpl::ProvideDirectory,
- MessageLoopProxy::current(), // io_thread
- path_,
- initialize_index_callback));
+ cache_thread_->PostTask(
+ FROM_HERE,
+ base::Bind(&SimpleBackendImpl::ProvideDirectorySuggestBetterCacheSize,
+ MessageLoopProxy::current(), // io_thread
+ path_,
+ initialize_index_callback,
+ orig_max_size_));
return net::ERR_IO_PENDING;
}
bool SimpleBackendImpl::SetMaxSize(int max_bytes) {
+ orig_max_size_ = max_bytes;
return index_->SetMaxSize(max_bytes);
}
@@ -231,24 +240,40 @@ void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) {
}
void SimpleBackendImpl::InitializeIndex(
- const CompletionCallback& callback, int result) {
- if (result == net::OK)
+ const CompletionCallback& callback, uint64 suggested_max_size, int result) {
+ if (result == net::OK) {
+ index_->SetMaxSize(suggested_max_size);
index_->Initialize();
+ }
callback.Run(result);
}
// static
-void SimpleBackendImpl::ProvideDirectory(
+void SimpleBackendImpl::ProvideDirectorySuggestBetterCacheSize(
SingleThreadTaskRunner* io_thread,
const base::FilePath& path,
- const InitializeIndexCallback& initialize_index_callback) {
+ const InitializeIndexCallback& initialize_index_callback,
+ uint64 suggested_max_size) {
int rv = net::OK;
+ uint64 max_size = suggested_max_size;
if (!FileStructureConsistent(path)) {
LOG(ERROR) << "Simple Cache Backend: wrong file structure on disk: "
<< path.LossyDisplayName();
rv = net::ERR_FAILED;
+ } else {
+ if (!max_size) {
+ int64 available = base::SysInfo::AmountOfFreeDiskSpace(path);
+ if (available < 0)
+ max_size = kDefaultCacheSize;
+ else
+ // TODO(pasko): Move PreferedCacheSize() to cache_util.h. Also fix the
+ // spelling.
+ max_size = PreferedCacheSize(available);
+ }
+ DCHECK(max_size);
}
- io_thread->PostTask(FROM_HERE, base::Bind(initialize_index_callback, rv));
+ io_thread->PostTask(FROM_HERE,
+ base::Bind(initialize_index_callback, max_size, rv));
}
} // namespace disk_cache
diff --git a/net/disk_cache/simple/simple_backend_impl.h b/net/disk_cache/simple/simple_backend_impl.h
index 6b7ac17..cfe8133 100644
--- a/net/disk_cache/simple/simple_backend_impl.h
+++ b/net/disk_cache/simple/simple_backend_impl.h
@@ -72,10 +72,13 @@ class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
virtual void OnExternalCacheHit(const std::string& key) OVERRIDE;
private:
- typedef base::Callback<void(int result)> InitializeIndexCallback;
+ typedef base::Callback<void(uint64 max_size, int result)>
+ InitializeIndexCallback;
// Must run on IO Thread.
- void InitializeIndex(const CompletionCallback& callback, int result);
+ void InitializeIndex(const CompletionCallback& callback,
+ uint64 suggested_max_size,
+ int result);
// Dooms all entries previously accessed between |initial_time| and
// |end_time|. Invoked when the index is ready.
@@ -84,16 +87,18 @@ class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
const CompletionCallback& callback,
int result);
- // Try to create the directory if it doesn't exist.
- // Must run on Cache Thread.
- static void ProvideDirectory(
+ // Try to create the directory if it doesn't exist. Replies with maximum cache
+ // size adjustment. Must run on Cache Thread.
+ static void ProvideDirectorySuggestBetterCacheSize(
base::SingleThreadTaskRunner* io_thread,
const base::FilePath& path,
- const InitializeIndexCallback& initialize_index_callback);
+ const InitializeIndexCallback& initialize_index_callback,
+ uint64 suggested_max_size);
const base::FilePath path_;
scoped_ptr<SimpleIndex> index_;
const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_;
+ int orig_max_size_;
};
} // namespace disk_cache
diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc
index cfd878d..0cf77e3 100644
--- a/net/disk_cache/simple/simple_index.cc
+++ b/net/disk_cache/simple/simple_index.cc
@@ -13,12 +13,10 @@
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/pickle.h"
-#include "base/sys_info.h"
#include "base/task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/worker_pool.h"
#include "net/base/net_errors.h"
-#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_index_file.h"
#include "net/disk_cache/simple/simple_synchronous_entry.h"
@@ -36,9 +34,6 @@ namespace {
const int kWriteToDiskDelayMSecs = 20000;
const int kWriteToDiskOnBackgroundDelayMSecs = 100;
-// Cache size when all other size heuristics failed.
-const uint64 kDefaultCacheSize = 80 * 1024 * 1024;
-
// Divides the cache space into this amount of parts to evict when only one part
// is left.
const uint32 kEvictionMarginDivisor = 20;
@@ -159,10 +154,9 @@ void EntryMetadata::MergeWith(const EntryMetadata& from) {
SimpleIndex::SimpleIndex(
base::SingleThreadTaskRunner* cache_thread,
base::SingleThreadTaskRunner* io_thread,
- const base::FilePath& path,
- int max_size)
+ const base::FilePath& path)
: cache_size_(0),
- max_size_(max_size),
+ max_size_(0),
high_watermark_(0),
low_watermark_(0),
eviction_in_progress_(false),
@@ -190,18 +184,6 @@ SimpleIndex::~SimpleIndex() {
void SimpleIndex::Initialize() {
DCHECK(io_thread_checker_.CalledOnValidThread());
- if (!max_size_) {
- int64 available = base::SysInfo::AmountOfFreeDiskSpace(index_filename_);
- if (available < 0)
- max_size_ = kDefaultCacheSize;
- else
- // TODO(pasko): Move PreferedCacheSize() to cache_util.h. Also fix the
- // spelling.
- max_size_ = PreferedCacheSize(available);
- }
- DCHECK(max_size_);
- SetMaxSize(max_size_);
-
IndexCompletionCallback merge_callback =
base::Bind(&SimpleIndex::MergeInitializingSet, AsWeakPtr());
base::WorkerPool::PostTask(FROM_HERE,
diff --git a/net/disk_cache/simple/simple_index.h b/net/disk_cache/simple/simple_index.h
index 4323b30..d6d8410 100644
--- a/net/disk_cache/simple/simple_index.h
+++ b/net/disk_cache/simple/simple_index.h
@@ -78,11 +78,9 @@ class NET_EXPORT_PRIVATE EntryMetadata {
class NET_EXPORT_PRIVATE SimpleIndex
: public base::SupportsWeakPtr<SimpleIndex> {
public:
- SimpleIndex(
- base::SingleThreadTaskRunner* cache_thread,
- base::SingleThreadTaskRunner* io_thread,
- const base::FilePath& path,
- int max_size);
+ SimpleIndex(base::SingleThreadTaskRunner* cache_thread,
+ base::SingleThreadTaskRunner* io_thread,
+ const base::FilePath& path);
virtual ~SimpleIndex();