diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-18 00:22:08 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-18 00:22:08 +0000 |
commit | df4825733e94311ffdefbbed6fecc93203886243 (patch) | |
tree | 5dd5fbe5a2d3163cff7752a5abf647bfcb2ffb0e /net/disk_cache/eviction.cc | |
parent | 4364d3ed5abb7fc2788508cac79ecd8a8703289a (diff) | |
download | chromium_src-df4825733e94311ffdefbbed6fecc93203886243.zip chromium_src-df4825733e94311ffdefbbed6fecc93203886243.tar.gz chromium_src-df4825733e94311ffdefbbed6fecc93203886243.tar.bz2 |
Disk cache: move eviction code to a separate file.
There should be no change in behavior with this CL.
Review URL: http://codereview.chromium.org/14183
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7190 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache/eviction.cc')
-rw-r--r-- | net/disk_cache/eviction.cc | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/net/disk_cache/eviction.cc b/net/disk_cache/eviction.cc new file mode 100644 index 0000000..11ff969 --- /dev/null +++ b/net/disk_cache/eviction.cc @@ -0,0 +1,122 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/disk_cache/eviction.h" + +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/string_util.h" +#include "base/time.h" +#include "net/disk_cache/backend_impl.h" +#include "net/disk_cache/entry_impl.h" +#include "net/disk_cache/trace.h" + +using base::Time; + +namespace { + +const int kCleanUpMargin = 1024 * 1024; + +int LowWaterAdjust(int high_water) { + if (high_water < kCleanUpMargin) + return 0; + + return high_water - kCleanUpMargin; +} + +} // namespace + +namespace disk_cache { + +void Eviction::Init(BackendImpl* backend) { + // We grab a bunch of info from the backend to make the code a little cleaner + // when we're actually doing work. + backend_ = backend; + rankings_ = &backend->rankings_; + header_ = &backend_->data_->header; + max_size_ = LowWaterAdjust(backend_->max_size_); +} + +void Eviction::TrimCache(bool empty) { + Trace("*** Trim Cache ***"); + if (backend_->disabled_) + return; + + Time start = Time::Now(); + Rankings::ScopedRankingsBlock node(rankings_); + Rankings::ScopedRankingsBlock next(rankings_, + rankings_->GetPrev(node.get(), Rankings::NO_USE)); + DCHECK(next.get()); + int target_size = empty ? 0 : max_size_; + int deleted = 0; + while (header_->num_bytes > target_size && next.get()) { + node.reset(next.release()); + next.reset(rankings_->GetPrev(node.get(), Rankings::NO_USE)); + if (!node->Data()->pointer || empty) { + // This entry is not being used by anybody. + EntryImpl* entry; + bool dirty; + if (backend_->NewEntry(Addr(node->Data()->contents), &entry, &dirty)) { + Trace("NewEntry failed on Trim 0x%x", node->address().value()); + continue; + } + + if (node->Data()->pointer) { + entry = EntryImpl::Update(entry); + } + ReportTrimTimes(entry); + entry->Doom(); + entry->Release(); + if (!empty) + backend_->OnEvent(Stats::TRIM_ENTRY); + if (++deleted == 4 && !empty) { +#if defined(OS_WIN) + MessageLoop::current()->PostTask(FROM_HERE, + factory_.NewRunnableMethod(&Eviction::TrimCache, false)); + break; +#endif + } + } + } + + UMA_HISTOGRAM_TIMES(L"DiskCache.TotalTrimTime", Time::Now() - start); + Trace("*** Trim Cache end ***"); + return; +} + +void Eviction::UpdateRank(EntryImpl* entry, bool modified) { + rankings_->UpdateRank(entry->rankings(), modified, GetListForEntry(entry)); +} + +void Eviction::OnOpenEntry(EntryImpl* entry) { +} + +void Eviction::OnCreateEntry(EntryImpl* entry) { + rankings_->Insert(entry->rankings(), true, GetListForEntry(entry)); +} + +void Eviction::OnDoomEntry(EntryImpl* entry) { + rankings_->Remove(entry->rankings(), GetListForEntry(entry)); +} + +void Eviction::OnDestroyEntry(EntryImpl* entry) { +} + +void Eviction::ReportTrimTimes(EntryImpl* entry) { + static bool first_time = true; + if (first_time) { + first_time = false; + std::wstring name(StringPrintf(L"DiskCache.TrimAge_%d", + header_->experiment)); + static Histogram counter(name.c_str(), 1, 10000, 50); + counter.SetFlags(kUmaTargetedHistogramFlag); + counter.Add((Time::Now() - entry->GetLastUsed()).InHours()); + } +} + +Rankings::List Eviction::GetListForEntry(EntryImpl* entry) { + return Rankings::NO_USE; +} + +} // namespace disk_cache |