This source file includes following definitions.
- memoryCache
- setMemoryCacheForTesting
- m_statsTimer
- removeFragmentIdentifierIfNeeded
- add
- replace
- remove
- contains
- resourceForURL
- deadCapacity
- liveCapacity
- pruneLiveResources
- pruneDeadResources
- setCapacities
- evict
- lruListFor
- removeFromLRUList
- insertInLRUList
- removeFromLiveDecodedResourcesList
- insertInLiveDecodedResourcesList
- isInLiveDecodedResourcesList
- addToLiveResourcesSize
- removeFromLiveResourcesSize
- update
- removeURLFromCache
- removeURLFromCacheInternal
- addResource
- getStatistics
- evictResources
- prune
- willProcessTask
- didProcessTask
- pruneNow
- dumpStats
- dumpLRULists
#include "config.h"
#include "core/fetch/MemoryCache.h"
#include "core/dom/CrossThreadTask.h"
#include "core/dom/Document.h"
#include "core/fetch/ResourcePtr.h"
#include "core/frame/FrameView.h"
#include "core/workers/WorkerGlobalScope.h"
#include "core/workers/WorkerLoaderProxy.h"
#include "core/workers/WorkerThread.h"
#include "platform/Logging.h"
#include "platform/TraceEvent.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "platform/weborigin/SecurityOriginHash.h"
#include "public/platform/Platform.h"
#include "wtf/Assertions.h"
#include "wtf/CurrentTime.h"
#include "wtf/MathExtras.h"
#include "wtf/TemporaryChange.h"
#include "wtf/text/CString.h"
namespace WebCore {
static MemoryCache* gMemoryCache;
static const unsigned cDefaultCacheCapacity = 8192 * 1024;
static const unsigned cDeferredPruneDeadCapacityFactor = 2;
static const int cMinDelayBeforeLiveDecodedPrune = 1;
static const double cMaxPruneDeferralDelay = 0.5;
static const float cTargetPrunePercentage = .95f;
MemoryCache* memoryCache()
{
ASSERT(WTF::isMainThread());
if (!gMemoryCache)
gMemoryCache = new MemoryCache();
return gMemoryCache;
}
void setMemoryCacheForTesting(MemoryCache* memoryCache)
{
gMemoryCache = memoryCache;
}
MemoryCache::MemoryCache()
: m_inPruneResources(false)
, m_prunePending(false)
, m_maxPruneDeferralDelay(cMaxPruneDeferralDelay)
, m_capacity(cDefaultCacheCapacity)
, m_minDeadCapacity(0)
, m_maxDeadCapacity(cDefaultCacheCapacity)
, m_maxDeferredPruneDeadCapacity(cDeferredPruneDeadCapacityFactor * cDefaultCacheCapacity)
, m_delayBeforeLiveDecodedPrune(cMinDelayBeforeLiveDecodedPrune)
, m_liveSize(0)
, m_deadSize(0)
#ifdef MEMORY_CACHE_STATS
, m_statsTimer(this, &MemoryCache::dumpStats)
#endif
{
#ifdef MEMORY_CACHE_STATS
const double statsIntervalInSeconds = 15;
m_statsTimer.startRepeating(statsIntervalInSeconds, FROM_HERE);
#endif
m_pruneTimeStamp = m_pruneFrameTimeStamp = FrameView::currentFrameTimeStamp();
}
MemoryCache::~MemoryCache()
{
if (m_prunePending)
blink::Platform::current()->currentThread()->removeTaskObserver(this);
}
KURL MemoryCache::removeFragmentIdentifierIfNeeded(const KURL& originalURL)
{
if (!originalURL.hasFragmentIdentifier())
return originalURL;
if (!originalURL.protocolIsInHTTPFamily())
return originalURL;
KURL url = originalURL;
url.removeFragmentIdentifier();
return url;
}
void MemoryCache::add(Resource* resource)
{
ASSERT(WTF::isMainThread());
ASSERT(!m_resources.contains(resource->url()));
m_resources.set(resource->url(), MemoryCacheEntry::create(resource));
update(resource, 0, resource->size(), true);
WTF_LOG(ResourceLoading, "MemoryCache::add Added '%s', resource %p\n", resource->url().string().latin1().data(), resource);
}
void MemoryCache::replace(Resource* newResource, Resource* oldResource)
{
if (MemoryCacheEntry* oldEntry = m_resources.get(oldResource->url()))
evict(oldEntry);
add(newResource);
if (newResource->decodedSize() && newResource->hasClients())
insertInLiveDecodedResourcesList(newResource);
}
void MemoryCache::remove(Resource* resource)
{
if (!contains(resource))
return;
evict(m_resources.get(resource->url()));
}
bool MemoryCache::contains(const Resource* resource) const
{
if (resource->url().isNull())
return false;
const MemoryCacheEntry* entry = m_resources.get(resource->url());
return entry && entry->m_resource == resource;
}
Resource* MemoryCache::resourceForURL(const KURL& resourceURL)
{
ASSERT(WTF::isMainThread());
KURL url = removeFragmentIdentifierIfNeeded(resourceURL);
MemoryCacheEntry* entry = m_resources.get(url);
if (!entry)
return 0;
Resource* resource = entry->m_resource.get();
if (resource && !resource->lock()) {
ASSERT(!resource->hasClients());
bool didEvict = evict(entry);
ASSERT_UNUSED(didEvict, didEvict);
return 0;
}
return resource;
}
size_t MemoryCache::deadCapacity() const
{
size_t capacity = m_capacity - std::min(m_liveSize, m_capacity);
capacity = std::max(capacity, m_minDeadCapacity);
capacity = std::min(capacity, m_maxDeadCapacity);
return capacity;
}
size_t MemoryCache::liveCapacity() const
{
return m_capacity - deadCapacity();
}
void MemoryCache::pruneLiveResources()
{
ASSERT(!m_prunePending);
size_t capacity = liveCapacity();
if (!m_liveSize || (capacity && m_liveSize <= capacity))
return;
size_t targetSize = static_cast<size_t>(capacity * cTargetPrunePercentage);
for (int priority = Resource::CacheLiveResourcePriorityLow; priority <= Resource::CacheLiveResourcePriorityHigh; ++priority) {
MemoryCacheEntry* current = m_liveDecodedResources[priority].m_tail;
while (current) {
MemoryCacheEntry* previous = current->m_previousInLiveResourcesList;
ASSERT(current->m_resource->hasClients());
if (current->m_resource->isLoaded() && current->m_resource->decodedSize()) {
double elapsedTime = m_pruneFrameTimeStamp - current->m_resource->m_lastDecodedAccessTime;
if (elapsedTime < m_delayBeforeLiveDecodedPrune)
return;
current->m_resource->prune();
if (targetSize && m_liveSize <= targetSize)
return;
}
current = previous;
}
}
}
void MemoryCache::pruneDeadResources()
{
size_t capacity = deadCapacity();
if (!m_deadSize || (capacity && m_deadSize <= capacity))
return;
size_t targetSize = static_cast<size_t>(capacity * cTargetPrunePercentage);
int size = m_allResources.size();
for (int i = 0; i < size; i++) {
MemoryCacheEntry* current = m_allResources[i].m_tail;
while (current) {
MemoryCacheEntry* previous = current->m_previousInAllResourcesList;
if (current->m_resource->wasPurged()) {
ASSERT(!current->m_resource->hasClients());
ASSERT(!current->m_resource->isPreloaded());
evict(current);
}
current = previous;
}
}
if (targetSize && m_deadSize <= targetSize)
return;
bool canShrinkLRULists = true;
for (int i = size - 1; i >= 0; i--) {
MemoryCacheEntry* current = m_allResources[i].m_tail;
while (current) {
MemoryCacheEntry* previous = current->m_previousInAllResourcesList;
ASSERT(!previous || contains(previous->m_resource.get()));
if (!current->m_resource->hasClients() && !current->m_resource->isPreloaded() && current->m_resource->isLoaded()) {
current->m_resource->prune();
if (targetSize && m_deadSize <= targetSize)
return;
}
if (previous && !contains(previous->m_resource.get()))
break;
current = previous;
}
current = m_allResources[i].m_tail;
while (current) {
MemoryCacheEntry* previous = current->m_previousInAllResourcesList;
ASSERT(!previous || contains(previous->m_resource.get()));
if (!current->m_resource->hasClients() && !current->m_resource->isPreloaded() && !current->m_resource->isCacheValidator()) {
evict(current);
if (targetSize && m_deadSize <= targetSize)
return;
}
if (previous && !contains(previous->m_resource.get()))
break;
current = previous;
}
if (m_allResources[i].m_head)
canShrinkLRULists = false;
else if (canShrinkLRULists)
m_allResources.resize(i);
}
}
void MemoryCache::setCapacities(size_t minDeadBytes, size_t maxDeadBytes, size_t totalBytes)
{
ASSERT(minDeadBytes <= maxDeadBytes);
ASSERT(maxDeadBytes <= totalBytes);
m_minDeadCapacity = minDeadBytes;
m_maxDeadCapacity = maxDeadBytes;
m_maxDeferredPruneDeadCapacity = cDeferredPruneDeadCapacityFactor * maxDeadBytes;
m_capacity = totalBytes;
prune();
}
bool MemoryCache::evict(MemoryCacheEntry* entry)
{
ASSERT(WTF::isMainThread());
Resource* resource = entry->m_resource.get();
WTF_LOG(ResourceLoading, "Evicting resource %p for '%s' from cache", resource, resource->url().string().latin1().data());
update(resource, resource->size(), 0, false);
removeFromLiveDecodedResourcesList(resource);
m_resources.remove(resource->url());
return resource->deleteIfPossible();
}
MemoryCache::LRUList* MemoryCache::lruListFor(unsigned accessCount, size_t size)
{
ASSERT(accessCount > 0);
unsigned queueIndex = WTF::fastLog2(size / accessCount);
if (m_allResources.size() <= queueIndex)
m_allResources.grow(queueIndex + 1);
return &m_allResources[queueIndex];
}
void MemoryCache::removeFromLRUList(MemoryCacheEntry* entry, LRUList* list)
{
#if !ASSERT_DISABLED
bool found = false;
for (MemoryCacheEntry* current = list->m_head; current; current = current->m_nextInAllResourcesList) {
if (current == entry) {
found = true;
break;
}
}
ASSERT(found);
#endif
MemoryCacheEntry* next = entry->m_nextInAllResourcesList;
MemoryCacheEntry* previous = entry->m_previousInAllResourcesList;
entry->m_nextInAllResourcesList = 0;
entry->m_previousInAllResourcesList = 0;
if (next)
next->m_previousInAllResourcesList = previous;
else
list->m_tail = previous;
if (previous)
previous->m_nextInAllResourcesList = next;
else
list->m_head = next;
}
void MemoryCache::insertInLRUList(MemoryCacheEntry* entry, LRUList* list)
{
ASSERT(!entry->m_nextInAllResourcesList && !entry->m_previousInAllResourcesList);
entry->m_nextInAllResourcesList = list->m_head;
list->m_head = entry;
if (entry->m_nextInAllResourcesList)
entry->m_nextInAllResourcesList->m_previousInAllResourcesList = entry;
else
list->m_tail = entry;
#if !ASSERT_DISABLED
bool found = false;
for (MemoryCacheEntry* current = list->m_head; current; current = current->m_nextInAllResourcesList) {
if (current == entry) {
found = true;
break;
}
}
ASSERT(found);
#endif
}
void MemoryCache::removeFromLiveDecodedResourcesList(Resource* resource)
{
MemoryCacheEntry* entry = m_resources.get(resource->url());
ASSERT(entry->m_resource == resource);
if (!entry->m_inLiveDecodedResourcesList)
return;
entry->m_inLiveDecodedResourcesList = false;
LRUList* list = &m_liveDecodedResources[resource->cacheLiveResourcePriority()];
#if !ASSERT_DISABLED
bool found = false;
for (MemoryCacheEntry* current = list->m_head; current; current = current->m_nextInLiveResourcesList) {
if (current == entry) {
found = true;
break;
}
}
ASSERT(found);
#endif
MemoryCacheEntry* next = entry->m_nextInLiveResourcesList;
MemoryCacheEntry* previous = entry->m_previousInLiveResourcesList;
entry->m_nextInLiveResourcesList = 0;
entry->m_previousInLiveResourcesList = 0;
if (next)
next->m_previousInLiveResourcesList = previous;
else
list->m_tail = previous;
if (previous)
previous->m_nextInLiveResourcesList = next;
else
list->m_head = next;
}
void MemoryCache::insertInLiveDecodedResourcesList(Resource* resource)
{
MemoryCacheEntry* entry = m_resources.get(resource->url());
ASSERT(entry->m_resource == resource);
ASSERT(!entry->m_nextInLiveResourcesList && !entry->m_previousInLiveResourcesList && !entry->m_inLiveDecodedResourcesList);
entry->m_inLiveDecodedResourcesList = true;
LRUList* list = &m_liveDecodedResources[resource->cacheLiveResourcePriority()];
entry->m_nextInLiveResourcesList = list->m_head;
if (list->m_head)
list->m_head->m_previousInLiveResourcesList = entry;
list->m_head = entry;
if (!entry->m_nextInLiveResourcesList)
list->m_tail = entry;
#if !ASSERT_DISABLED
bool found = false;
for (MemoryCacheEntry* current = list->m_head; current; current = current->m_nextInLiveResourcesList) {
if (current == entry) {
found = true;
break;
}
}
ASSERT(found);
#endif
}
bool MemoryCache::isInLiveDecodedResourcesList(Resource* resource)
{
MemoryCacheEntry* entry = m_resources.get(resource->url());
ASSERT(entry && entry->m_resource == resource);
return entry->m_inLiveDecodedResourcesList;
}
void MemoryCache::addToLiveResourcesSize(Resource* resource)
{
ASSERT(m_deadSize >= resource->size());
m_liveSize += resource->size();
m_deadSize -= resource->size();
}
void MemoryCache::removeFromLiveResourcesSize(Resource* resource)
{
ASSERT(m_liveSize >= resource->size());
m_liveSize -= resource->size();
m_deadSize += resource->size();
}
void MemoryCache::update(Resource* resource, size_t oldSize, size_t newSize, bool wasAccessed)
{
ASSERT(contains(resource));
MemoryCacheEntry* entry = m_resources.get(resource->url());
if (oldSize)
removeFromLRUList(entry, lruListFor(entry->m_accessCount, oldSize));
if (wasAccessed)
entry->m_accessCount++;
if (newSize)
insertInLRUList(entry, lruListFor(entry->m_accessCount, newSize));
ptrdiff_t delta = newSize - oldSize;
if (resource->hasClients()) {
ASSERT(delta >= 0 || m_liveSize >= static_cast<size_t>(-delta) );
m_liveSize += delta;
} else {
ASSERT(delta >= 0 || m_deadSize >= static_cast<size_t>(-delta) );
m_deadSize += delta;
}
}
void MemoryCache::removeURLFromCache(ExecutionContext* context, const KURL& url)
{
if (context->isWorkerGlobalScope()) {
WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context);
workerGlobalScope->thread()->workerLoaderProxy().postTaskToLoader(createCallbackTask(&removeURLFromCacheInternal, url));
return;
}
removeURLFromCacheInternal(context, url);
}
void MemoryCache::removeURLFromCacheInternal(ExecutionContext*, const KURL& url)
{
if (Resource* resource = memoryCache()->resourceForURL(url))
memoryCache()->remove(resource);
}
void MemoryCache::TypeStatistic::addResource(Resource* o)
{
bool purged = o->wasPurged();
bool purgeable = o->isPurgeable() && !purged;
size_t pageSize = (o->encodedSize() + o->overheadSize() + 4095) & ~4095;
count++;
size += purged ? 0 : o->size();
liveSize += o->hasClients() ? o->size() : 0;
decodedSize += o->decodedSize();
encodedSize += o->encodedSize();
encodedSizeDuplicatedInDataURLs += o->url().protocolIsData() ? o->encodedSize() : 0;
purgeableSize += purgeable ? pageSize : 0;
purgedSize += purged ? pageSize : 0;
}
MemoryCache::Statistics MemoryCache::getStatistics()
{
Statistics stats;
ResourceMap::iterator e = m_resources.end();
for (ResourceMap::iterator i = m_resources.begin(); i != e; ++i) {
Resource* resource = i->value->m_resource.get();
switch (resource->type()) {
case Resource::Image:
stats.images.addResource(resource);
break;
case Resource::CSSStyleSheet:
stats.cssStyleSheets.addResource(resource);
break;
case Resource::Script:
stats.scripts.addResource(resource);
break;
case Resource::XSLStyleSheet:
stats.xslStyleSheets.addResource(resource);
break;
case Resource::Font:
stats.fonts.addResource(resource);
break;
default:
stats.other.addResource(resource);
break;
}
}
return stats;
}
void MemoryCache::evictResources()
{
for (;;) {
ResourceMap::iterator i = m_resources.begin();
if (i == m_resources.end())
break;
evict(i->value.get());
}
}
void MemoryCache::prune(Resource* justReleasedResource)
{
TRACE_EVENT0("renderer", "MemoryCache::prune()");
if (m_inPruneResources)
return;
if (m_liveSize + m_deadSize <= m_capacity && m_maxDeadCapacity && m_deadSize <= m_maxDeadCapacity)
return;
double currentTime = WTF::currentTime();
if (m_prunePending) {
if (currentTime - m_pruneTimeStamp >= m_maxPruneDeferralDelay) {
pruneNow(currentTime);
}
} else {
if (currentTime - m_pruneTimeStamp >= m_maxPruneDeferralDelay) {
pruneNow(currentTime);
} else {
blink::Platform::current()->currentThread()->addTaskObserver(this);
m_prunePending = true;
}
}
if (m_prunePending && m_deadSize > m_maxDeferredPruneDeadCapacity && justReleasedResource) {
if (contains(justReleasedResource))
evict(m_resources.get(justReleasedResource->url()));
if (m_deadSize > m_maxDeferredPruneDeadCapacity)
pruneNow(currentTime);
}
}
void MemoryCache::willProcessTask()
{
}
void MemoryCache::didProcessTask()
{
ASSERT(m_prunePending);
pruneNow(WTF::currentTime());
}
void MemoryCache::pruneNow(double currentTime)
{
if (m_prunePending) {
m_prunePending = false;
blink::Platform::current()->currentThread()->removeTaskObserver(this);
}
TemporaryChange<bool> reentrancyProtector(m_inPruneResources, true);
pruneDeadResources();
pruneLiveResources();
m_pruneFrameTimeStamp = FrameView::currentFrameTimeStamp();
m_pruneTimeStamp = currentTime;
}
#ifdef MEMORY_CACHE_STATS
void MemoryCache::dumpStats(Timer<MemoryCache>*)
{
Statistics s = getStatistics();
printf("%-13s %-13s %-13s %-13s %-13s %-13s %-13s\n", "", "Count", "Size", "LiveSize", "DecodedSize", "PurgeableSize", "PurgedSize");
printf("%-13s %-13s %-13s %-13s %-13s %-13s %-13s\n", "-------------", "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
printf("%-13s %13d %13d %13d %13d %13d %13d\n", "Images", s.images.count, s.images.size, s.images.liveSize, s.images.decodedSize, s.images.purgeableSize, s.images.purgedSize);
printf("%-13s %13d %13d %13d %13d %13d %13d\n", "CSS", s.cssStyleSheets.count, s.cssStyleSheets.size, s.cssStyleSheets.liveSize, s.cssStyleSheets.decodedSize, s.cssStyleSheets.purgeableSize, s.cssStyleSheets.purgedSize);
printf("%-13s %13d %13d %13d %13d %13d %13d\n", "XSL", s.xslStyleSheets.count, s.xslStyleSheets.size, s.xslStyleSheets.liveSize, s.xslStyleSheets.decodedSize, s.xslStyleSheets.purgeableSize, s.xslStyleSheets.purgedSize);
printf("%-13s %13d %13d %13d %13d %13d %13d\n", "JavaScript", s.scripts.count, s.scripts.size, s.scripts.liveSize, s.scripts.decodedSize, s.scripts.purgeableSize, s.scripts.purgedSize);
printf("%-13s %13d %13d %13d %13d %13d %13d\n", "Fonts", s.fonts.count, s.fonts.size, s.fonts.liveSize, s.fonts.decodedSize, s.fonts.purgeableSize, s.fonts.purgedSize);
printf("%-13s %13d %13d %13d %13d %13d %13d\n", "Other", s.other.count, s.other.size, s.other.liveSize, s.other.decodedSize, s.other.purgeableSize, s.other.purgedSize);
printf("%-13s %-13s %-13s %-13s %-13s %-13s %-13s\n\n", "-------------", "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
printf("Duplication of encoded data from data URLs\n");
printf("%-13s %13d of %13d\n", "Images", s.images.encodedSizeDuplicatedInDataURLs, s.images.encodedSize);
printf("%-13s %13d of %13d\n", "CSS", s.cssStyleSheets.encodedSizeDuplicatedInDataURLs, s.cssStyleSheets.encodedSize);
printf("%-13s %13d of %13d\n", "XSL", s.xslStyleSheets.encodedSizeDuplicatedInDataURLs, s.xslStyleSheets.encodedSize);
printf("%-13s %13d of %13d\n", "JavaScript", s.scripts.encodedSizeDuplicatedInDataURLs, s.scripts.encodedSize);
printf("%-13s %13d of %13d\n", "Fonts", s.fonts.encodedSizeDuplicatedInDataURLs, s.fonts.encodedSize);
printf("%-13s %13d of %13d\n", "Other", s.other.encodedSizeDuplicatedInDataURLs, s.other.encodedSize);
}
void MemoryCache::dumpLRULists(bool includeLive) const
{
printf("LRU-SP lists in eviction order (Kilobytes decoded, Kilobytes encoded, Access count, Referenced, isPurgeable, wasPurged):\n");
int size = m_allResources.size();
for (int i = size - 1; i >= 0; i--) {
printf("\n\nList %d: ", i);
Resource* current = m_allResources[i].m_tail;
while (current) {
Resource* prev = current->m_prevInAllResourcesList;
if (includeLive || !current->hasClients())
printf("(%.1fK, %.1fK, %uA, %dR, %d, %d); ", current->decodedSize() / 1024.0f, (current->encodedSize() + current->overheadSize()) / 1024.0f, current->accessCount(), current->hasClients(), current->isPurgeable(), current->wasPurged());
current = prev;
}
}
}
#endif
}