Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1126)

Unified Diff: net/disk_cache/backend_impl.cc

Issue 15772003: Disk Cache: Make Stats independent of the backend implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/disk_cache/backend_impl.cc
===================================================================
--- net/disk_cache/backend_impl.cc (revision 201602)
+++ net/disk_cache/backend_impl.cc (working copy)
@@ -299,7 +299,7 @@
// stats_ and rankings_ may end up calling back to us so we better be enabled.
disabled_ = false;
- if (!stats_.Init(this, &data_->header.stats))
+ if (!InitStats())
return net::ERR_FAILED;
disabled_ = !rankings_.Init(this, new_eviction_);
@@ -329,7 +329,7 @@
timer_.reset();
if (init_) {
- stats_.Store();
+ StoreStats();
if (data_)
data_->header.crash = 0;
@@ -1066,7 +1066,7 @@
// Save stats to disk at 5 min intervals.
if (time % 10 == 0)
- stats_.Store();
+ StoreStats();
}
void BackendImpl::IncrementIoCount() {
@@ -1352,6 +1352,62 @@
max_size_= current_max_size;
}
+bool BackendImpl::InitStats() {
+ Addr address(data_->header.stats);
+ int size = stats_.StorageSize();
+
+ if (!address.is_initialized()) {
+ FileType file_type = Addr::RequiredFileType(size);
+ DCHECK_NE(file_type, EXTERNAL);
+ int num_blocks = Addr::RequiredBlocks(size, file_type);
+
+ if (!CreateBlock(file_type, num_blocks, &address))
+ return false;
+ return stats_.Init(NULL, 0, address);
+ }
+
+ if (!address.is_block_file()) {
+ NOTREACHED();
+ return false;
+ }
+
+ // Load the required data.
+ size = address.num_blocks() * address.BlockSize();
+ MappedFile* file = File(address);
+ if (!file)
+ return false;
+
+ scoped_ptr<char[]> data(new char[size]);
+ size_t offset = address.start_block() * address.BlockSize() +
+ kBlockHeaderSize;
+ if (!file->Read(data.get(), size, offset))
+ return false;
+
+ if (!stats_.Init(data.get(), size, address))
+ return false;
+ if (cache_type_ == net::DISK_CACHE && ShouldReportAgain())
+ stats_.InitSizeHistogram();
+ return true;
+}
+
+void BackendImpl::StoreStats() {
+ int size = stats_.StorageSize();
+ scoped_ptr<char[]> data(new char[size]);
+ Addr address;
+ bool rv = stats_.SerializeStats(data.get(), size, &address);
+ DCHECK(rv);
+ if (!address.is_initialized())
+ return;
+
+ MappedFile* file = File(address);
+ if (!file)
+ return;
+
+ size_t offset = address.start_block() * address.BlockSize() +
+ kBlockHeaderSize;
+ rv = file->Write(data.get(), size, offset); // ignore result.
+}
+
void BackendImpl::RestartCache(bool failure) {
int64 errors = stats_.GetCounter(Stats::FATAL_ERROR);
int64 full_dooms = stats_.GetCounter(Stats::DOOM_CACHE);

Powered by Google App Engine
This is Rietveld 408576698