summaryrefslogtreecommitdiffstats
path: root/net/disk_cache
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-25 23:44:20 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-25 23:44:20 +0000
commit6192f73a9dd17c950ef1ca97db086e8bc4eaf05e (patch)
tree7af372f9670e74b144d17f1f7c009a9eb13a74a2 /net/disk_cache
parent1ba590018ad58ea18ec3079f089feb64fd951d87 (diff)
downloadchromium_src-6192f73a9dd17c950ef1ca97db086e8bc4eaf05e.zip
chromium_src-6192f73a9dd17c950ef1ca97db086e8bc4eaf05e.tar.gz
chromium_src-6192f73a9dd17c950ef1ca97db086e8bc4eaf05e.tar.bz2
Disk cache: First pass to make it possible to have
multiple instances of BackendImpl. We need multiple objects to be able to support media files on the cache. After this change, histograms will be the only thing that get messed up by multiple disk caches. Review URL: http://codereview.chromium.org/49027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12520 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache')
-rw-r--r--net/disk_cache/backend_impl.cc34
-rw-r--r--net/disk_cache/backend_impl.h18
-rw-r--r--net/disk_cache/backend_unittest.cc11
-rw-r--r--net/disk_cache/disk_cache.h3
-rw-r--r--net/disk_cache/disk_cache_perftest.cc6
-rw-r--r--net/disk_cache/disk_cache_test_base.cc3
-rw-r--r--net/disk_cache/eviction.cc6
-rw-r--r--net/disk_cache/eviction.h3
-rw-r--r--net/disk_cache/stress_cache.cc3
-rw-r--r--net/disk_cache/trace.cc26
-rw-r--r--net/disk_cache/trace.h15
11 files changed, 84 insertions, 44 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc
index f106534..ac68ec9 100644
--- a/net/disk_cache/backend_impl.cc
+++ b/net/disk_cache/backend_impl.cc
@@ -159,9 +159,10 @@ namespace disk_cache {
// a sharing violation), and in that case a cache for this profile (on the
// desired path) cannot be created.
Backend* CreateCacheBackend(const std::wstring& full_path, bool force,
- int max_bytes) {
+ int max_bytes, net::CacheType type) {
BackendImpl* cache = new BackendImpl(full_path);
cache->SetMaxSize(max_bytes);
+ cache->SetType(type);
if (cache->Init())
return cache;
@@ -176,6 +177,7 @@ Backend* CreateCacheBackend(const std::wstring& full_path, bool force,
// is not there anymore... let's create a new set of files.
cache = new BackendImpl(full_path);
cache->SetMaxSize(max_bytes);
+ cache->SetType(type);
if (cache->Init())
return cache;
@@ -204,6 +206,7 @@ bool BackendImpl::Init() {
num_refs_ = num_pending_io_ = max_refs_ = 0;
if (!restarted_) {
+ trace_object_ = TraceObject::GetTraceObject();
// Create a recurrent timer of 30 secs.
int timer_delay = unit_test_ ? 1000 : 30000;
timer_.Start(TimeDelta::FromMilliseconds(timer_delay), this,
@@ -528,6 +531,11 @@ bool BackendImpl::SetMaxSize(int max_bytes) {
return true;
}
+void BackendImpl::SetType(net::CacheType type) {
+ DCHECK(type != net::MEMORY_CACHE);
+ cache_type_ = type;
+}
+
std::wstring BackendImpl::GetFileName(Addr address) const {
if (!address.is_separate_file() || !address.is_initialized()) {
NOTREACHED();
@@ -683,18 +691,15 @@ void BackendImpl::TooMuchStorageRequested(int32 size) {
// We want to remove biases from some histograms so we only send data once per
// week.
bool BackendImpl::ShouldReportAgain() {
- static bool first_time = true;
- static bool should_send = false;
-
- if (!first_time)
- return should_send;
+ if (uma_report_)
+ return uma_report_ == 2;
- first_time = false;
+ uma_report_++;
int64 last_report = stats_.GetCounter(Stats::LAST_REPORT);
Time last_time = Time::FromInternalValue(last_report);
if (!last_report || (Time::Now() - last_time).InDays() >= 7) {
stats_.SetCounter(Stats::LAST_REPORT, Time::Now().ToInternalValue());
- should_send = true;
+ uma_report_++;
return true;
}
return false;
@@ -706,7 +711,7 @@ void BackendImpl::FirstEviction() {
Time create_time = Time::FromInternalValue(data_->header.create_time);
UMA_HISTOGRAM_HOURS("DiskCache.FillupAge",
(Time::Now() - create_time).InHours());
-
+
int64 use_hours = stats_.GetCounter(Stats::TIMER) / 120;
UMA_HISTOGRAM_HOURS("DiskCache.FillupTime", static_cast<int>(use_hours));
UMA_HISTOGRAM_PERCENTAGE("DiskCache.FirstHitRatio", stats_.GetHitRatio());
@@ -760,11 +765,10 @@ void BackendImpl::OnStatsTimer() {
stats_.SetCounter(Stats::OPEN_ENTRIES, current);
stats_.SetCounter(Stats::MAX_ENTRIES, max_refs_);
- static bool first_time = true;
if (!data_)
- first_time = false;
- if (first_time) {
- first_time = false;
+ first_timer_ = false;
+ if (first_timer_) {
+ first_timer_ = false;
if (ShouldReportAgain())
ReportStats();
}
@@ -1307,12 +1311,12 @@ void BackendImpl::ReportStats() {
int64 total_hours = stats_.GetCounter(Stats::TIMER) / 120;
UMA_HISTOGRAM_HOURS("DiskCache.TotalTime", static_cast<int>(total_hours));
-
+
int64 use_hours = stats_.GetCounter(Stats::LAST_REPORT_TIMER) / 120;
if (!use_hours || !GetEntryCount() || !data_->header.num_bytes)
return;
- UMA_HISTOGRAM_HOURS("DiskCache.UseTime", static_cast<int>(use_hours));
+ UMA_HISTOGRAM_HOURS("DiskCache.UseTime", static_cast<int>(use_hours));
UMA_HISTOGRAM_PERCENTAGE("DiskCache.HitRatio", stats_.GetHitRatio());
UMA_HISTOGRAM_PERCENTAGE("DiskCache.ResurrectRatio",
stats_.GetResurrectRatio());
diff --git a/net/disk_cache/backend_impl.h b/net/disk_cache/backend_impl.h
index fd1f9be..b66ab2f 100644
--- a/net/disk_cache/backend_impl.h
+++ b/net/disk_cache/backend_impl.h
@@ -23,14 +23,16 @@ class BackendImpl : public Backend {
friend class Eviction;
public:
explicit BackendImpl(const std::wstring& path)
- : path_(path), block_files_(path), mask_(0), max_size_(0), init_(false),
+ : path_(path), block_files_(path), mask_(0), max_size_(0),
+ cache_type_(net::DISK_CACHE), uma_report_(0), init_(false),
restarted_(false), unit_test_(false), read_only_(false),
- new_eviction_(false) {}
+ new_eviction_(false), first_timer_(true) {}
// mask can be used to limit the usable size of the hash table, for testing.
BackendImpl(const std::wstring& path, uint32 mask)
: path_(path), block_files_(path), mask_(mask), max_size_(0),
- init_(false), restarted_(false), unit_test_(false), read_only_(false),
- new_eviction_(false) {}
+ cache_type_(net::DISK_CACHE), uma_report_(0), init_(false),
+ restarted_(false), unit_test_(false), read_only_(false),
+ new_eviction_(false), first_timer_(true) {}
~BackendImpl();
// Performs general initialization for this current instance of the cache.
@@ -52,6 +54,9 @@ class BackendImpl : public Backend {
// Sets the maximum size for the total amount of data stored by this instance.
bool SetMaxSize(int max_bytes);
+ // Sets the cache type for this backend.
+ void SetType(net::CacheType type);
+
// Returns the full name for an external storage file.
std::wstring GetFileName(Addr address) const;
@@ -216,16 +221,19 @@ class BackendImpl : public Backend {
int num_refs_; // Number of referenced cache entries.
int max_refs_; // Max number of referenced cache entries.
int num_pending_io_; // Number of pending IO operations.
+ net::CacheType cache_type_;
+ int uma_report_; // Controls transmision of UMA data.
bool init_; // controls the initialization of the system.
bool restarted_;
bool unit_test_;
bool read_only_; // Prevents updates of the rankings data (used by tools).
bool disabled_;
bool new_eviction_; // What eviction algorithm should be used.
+ bool first_timer_; // True if the timer has not been called.
Stats stats_; // Usage statistcs.
base::RepeatingTimer<BackendImpl> timer_; // Usage timer.
- TraceObject trace_object_; // Inits and destroys internal tracing.
+ scoped_refptr<TraceObject> trace_object_; // Inits internal tracing.
DISALLOW_EVIL_CONSTRUCTORS(BackendImpl);
};
diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc
index 3de09c4..1562621 100644
--- a/net/disk_cache/backend_unittest.cc
+++ b/net/disk_cache/backend_unittest.cc
@@ -43,7 +43,8 @@ int TestTransaction(const std::wstring& name, int num_entries, bool load) {
scoped_ptr<disk_cache::Backend> cache;
if (!load) {
- cache.reset(disk_cache::CreateCacheBackend(path, false, 0));
+ cache.reset(disk_cache::CreateCacheBackend(path, false, 0,
+ net::DISK_CACHE));
} else {
disk_cache::BackendImpl* cache2 = new disk_cache::BackendImpl(path, 0xf);
if (!cache2 || !cache2->SetMaxSize(0x100000) || !cache2->Init())
@@ -787,7 +788,7 @@ TEST_F(DiskCacheTest, Backend_DeleteOld) {
ASSERT_TRUE(CopyTestCache(L"wrong_version"));
std::wstring path = GetCachePath();
scoped_ptr<disk_cache::Backend> cache;
- cache.reset(disk_cache::CreateCacheBackend(path, true, 0));
+ cache.reset(disk_cache::CreateCacheBackend(path, true, 0, net::DISK_CACHE));
MessageLoopHelper helper;
@@ -803,7 +804,8 @@ TEST_F(DiskCacheTest, Backend_DeleteOld) {
TEST_F(DiskCacheTest, Backend_InvalidEntry) {
ASSERT_TRUE(CopyTestCache(L"bad_entry"));
std::wstring path = GetCachePath();
- disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0);
+ disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0,
+ net::DISK_CACHE);
ASSERT_TRUE(NULL != cache);
disk_cache::Entry *entry1, *entry2;
@@ -819,7 +821,8 @@ TEST_F(DiskCacheTest, Backend_InvalidEntry) {
TEST_F(DiskCacheTest, Backend_InvalidRankings) {
ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
std::wstring path = GetCachePath();
- disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0);
+ disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0,
+ net::DISK_CACHE);
ASSERT_TRUE(NULL != cache);
disk_cache::Entry *entry1, *entry2;
diff --git a/net/disk_cache/disk_cache.h b/net/disk_cache/disk_cache.h
index 50f50c5..5eb6d2c 100644
--- a/net/disk_cache/disk_cache.h
+++ b/net/disk_cache/disk_cache.h
@@ -14,6 +14,7 @@
#include "base/basictypes.h"
#include "base/platform_file.h"
#include "base/time.h"
+#include "net/base/cache_type.h"
#include "net/base/completion_callback.h"
namespace net {
@@ -35,7 +36,7 @@ class Backend;
// based on the available disk space. The returned pointer can be NULL if a
// fatal error is found.
Backend* CreateCacheBackend(const std::wstring& path, bool force,
- int max_bytes);
+ int max_bytes, net::CacheType type);
// Returns an instance of a Backend implemented only in memory. The returned
// object should be deleted when not needed anymore. max_bytes is the maximum
diff --git a/net/disk_cache/disk_cache_perftest.cc b/net/disk_cache/disk_cache_perftest.cc
index 0abbc5b..e6bc88d 100644
--- a/net/disk_cache/disk_cache_perftest.cc
+++ b/net/disk_cache/disk_cache_perftest.cc
@@ -159,7 +159,8 @@ TEST_F(DiskCacheTest, CacheBackendPerformance) {
ScopedTestCache test_cache;
disk_cache::Backend* cache =
- disk_cache::CreateCacheBackend(test_cache.path_wstring(), false, 0);
+ disk_cache::CreateCacheBackend(test_cache.path_wstring(), false, 0,
+ net::DISK_CACHE);
ASSERT_TRUE(NULL != cache);
int seed = static_cast<int>(Time::Now().ToInternalValue());
@@ -185,7 +186,8 @@ TEST_F(DiskCacheTest, CacheBackendPerformance) {
ASSERT_TRUE(file_util::EvictFileFromSystemCache(
test_cache.path().AppendASCII("data_3")));
- cache = disk_cache::CreateCacheBackend(test_cache.path_wstring(), false, 0);
+ cache = disk_cache::CreateCacheBackend(test_cache.path_wstring(), false, 0,
+ net::DISK_CACHE);
ASSERT_TRUE(NULL != cache);
ret = TimeRead(num_entries, cache, entries, true);
diff --git a/net/disk_cache/disk_cache_test_base.cc b/net/disk_cache/disk_cache_test_base.cc
index 942420b..8992ca8 100644
--- a/net/disk_cache/disk_cache_test_base.cc
+++ b/net/disk_cache/disk_cache_test_base.cc
@@ -57,7 +57,8 @@ void DiskCacheTestWithCache::InitDiskCache() {
ASSERT_TRUE(DeleteCache(path.c_str()));
if (!implementation_) {
- cache_ = disk_cache::CreateCacheBackend(path, force_creation_, size_);
+ cache_ = disk_cache::CreateCacheBackend(path, force_creation_, size_,
+ net::DISK_CACHE);
return;
}
diff --git a/net/disk_cache/eviction.cc b/net/disk_cache/eviction.cc
index 3706260..76af8db 100644
--- a/net/disk_cache/eviction.cc
+++ b/net/disk_cache/eviction.cc
@@ -67,6 +67,7 @@ void Eviction::Init(BackendImpl* backend) {
header_ = &backend_->data_->header;
max_size_ = LowWaterAdjust(backend_->max_size_);
new_eviction_ = backend->new_eviction_;
+ first_trim_ = true;
}
void Eviction::TrimCache(bool empty) {
@@ -141,9 +142,8 @@ void Eviction::OnDestroyEntry(EntryImpl* entry) {
}
void Eviction::ReportTrimTimes(EntryImpl* entry) {
- static bool first_time = true;
- if (first_time) {
- first_time = false;
+ if (first_trim_) {
+ first_trim_ = false;
if (backend_->ShouldReportAgain()) {
std::string name(StringPrintf("DiskCache.TrimAge_%d",
header_->experiment));
diff --git a/net/disk_cache/eviction.h b/net/disk_cache/eviction.h
index 56b8439..55fbee7 100644
--- a/net/disk_cache/eviction.h
+++ b/net/disk_cache/eviction.h
@@ -56,7 +56,7 @@ class Eviction {
Rankings::List GetListForEntryV2(EntryImpl* entry);
void TrimDeleted(bool empty);
bool RemoveDeletedNode(CacheRankingsBlock* node);
-
+
bool NodeIsOldEnough(CacheRankingsBlock* node, int list);
int SelectListByLenght();
@@ -65,6 +65,7 @@ class Eviction {
IndexHeader* header_;
int max_size_;
bool new_eviction_;
+ bool first_trim_;
ScopedRunnableMethodFactory<Eviction> factory_;
DISALLOW_COPY_AND_ASSIGN(Eviction);
diff --git a/net/disk_cache/stress_cache.cc b/net/disk_cache/stress_cache.cc
index aa79a86..bf12d30 100644
--- a/net/disk_cache/stress_cache.cc
+++ b/net/disk_cache/stress_cache.cc
@@ -77,7 +77,8 @@ void StressTheCache(int iteration) {
std::wstring path = GetCachePath();
path.append(L"_stress");
disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false,
- cache_size);
+ cache_size,
+ net::DISK_CACHE);
if (NULL == cache) {
printf("Unable to initialize cache.\n");
return;
diff --git a/net/disk_cache/trace.cc b/net/disk_cache/trace.cc
index 958f9e0..da97c40 100644
--- a/net/disk_cache/trace.cc
+++ b/net/disk_cache/trace.cc
@@ -42,24 +42,37 @@ void DebugOutput(const char* msg) {
namespace disk_cache {
+// s_trace_buffer and s_trace_object are not singletons because I want the
+// buffer to be destroyed and re-created when the last user goes away, and it
+// must be straightforward to access the buffer from the debugger.
+static TraceObject* s_trace_object = NULL;
+
+// Static.
+TraceObject* TraceObject::GetTraceObject() {
+ if (s_trace_object)
+ return s_trace_object;
+
+ s_trace_object = new TraceObject();
+ return s_trace_object;
+}
+
#if ENABLE_TRACING
static TraceBuffer* s_trace_buffer = NULL;
-bool InitTrace(void) {
- DCHECK(!s_trace_buffer);
+void InitTrace(void) {
if (s_trace_buffer)
- return false;
+ return;
s_trace_buffer = new TraceBuffer;
memset(s_trace_buffer, 0, sizeof(*s_trace_buffer));
- return true;
}
void DestroyTrace(void) {
DCHECK(s_trace_buffer);
delete s_trace_buffer;
s_trace_buffer = NULL;
+ s_trace_object = NULL;
}
void Trace(const char* format, ...) {
@@ -118,11 +131,12 @@ void DumpTrace(int num_traces) {
#else // ENABLE_TRACING
-bool InitTrace(void) {
- return true;
+void InitTrace(void) {
+ return;
}
void DestroyTrace(void) {
+ s_trace_object = NULL;
}
void Trace(const char* format, ...) {
diff --git a/net/disk_cache/trace.h b/net/disk_cache/trace.h
index 50dda3c..be50417 100644
--- a/net/disk_cache/trace.h
+++ b/net/disk_cache/trace.h
@@ -13,24 +13,29 @@
#include <vector>
#include "base/basictypes.h"
+#include "base/ref_counted.h"
namespace disk_cache {
// Create and destroy the tracing buffer.
-bool InitTrace(void);
+void InitTrace(void);
void DestroyTrace(void);
-// Simple class to handle the trace buffer lifetime.
-class TraceObject {
+// Simple class to handle the trace buffer lifetime. Any object interested in
+// tracing should keep a reference to the object returned by GetTraceObject().
+class TraceObject : public base::RefCounted<TraceObject> {
+ friend class base::RefCounted<TraceObject>;
public:
+ static TraceObject* GetTraceObject();
+
+ private:
TraceObject() {
InitTrace();
}
+
~TraceObject() {
DestroyTrace();
}
-
- private:
DISALLOW_EVIL_CONSTRUCTORS(TraceObject);
};