1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
/*
Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "core/fetch/MemoryCache.h"
#include "core/fetch/ResourceLoadingLog.h"
#include "platform/instrumentation/tracing/TraceEvent.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "platform/weborigin/SecurityOriginHash.h"
#include "public/platform/Platform.h"
#include "wtf/Assertions.h"
#include "wtf/AutoReset.h"
#include "wtf/CurrentTime.h"
#include "wtf/MathExtras.h"
#include "wtf/text/CString.h"
namespace blink {
static Persistent<MemoryCache>* gMemoryCache;
static const unsigned cDefaultCacheCapacity = 8192 * 1024;
static const int cMinDelayBeforeLiveDecodedPrune = 1; // Seconds.
static const double cMaxPruneDeferralDelay = 0.5; // Seconds.
// Percentage of capacity toward which we prune, to avoid immediately pruning
// again.
static const float cTargetPrunePercentage = .95f;
MemoryCache* memoryCache() {
DCHECK(WTF::isMainThread());
if (!gMemoryCache)
gMemoryCache = new Persistent<MemoryCache>(MemoryCache::create());
return gMemoryCache->get();
}
MemoryCache* replaceMemoryCacheForTesting(MemoryCache* cache) {
memoryCache();
MemoryCache* oldCache = gMemoryCache->release();
*gMemoryCache = cache;
MemoryCacheDumpProvider::instance()->setMemoryCache(cache);
return oldCache;
}
DEFINE_TRACE(MemoryCacheEntry) {
visitor->template registerWeakMembers<MemoryCacheEntry,
&MemoryCacheEntry::clearResourceWeak>(
this);
}
void MemoryCacheEntry::clearResourceWeak(Visitor* visitor) {
if (!m_resource || ThreadHeap::isHeapObjectAlive(m_resource))
return;
memoryCache()->remove(m_resource.get());
m_resource.clear();
}
inline MemoryCache::MemoryCache()
: m_inPruneResources(false),
m_prunePending(false),
m_maxPruneDeferralDelay(cMaxPruneDeferralDelay),
m_pruneTimeStamp(0.0),
m_pruneFrameTimeStamp(0.0),
m_lastFramePaintTimeStamp(0.0),
m_capacity(cDefaultCacheCapacity),
m_delayBeforeLiveDecodedPrune(cMinDelayBeforeLiveDecodedPrune),
m_size(0) {
MemoryCacheDumpProvider::instance()->setMemoryCache(this);
if (MemoryCoordinator::isLowEndDevice())
MemoryCoordinator::instance().registerClient(this);
}
MemoryCache* MemoryCache::create() {
return new MemoryCache;
}
MemoryCache::~MemoryCache() {
if (m_prunePending)
Platform::current()->currentThread()->removeTaskObserver(this);
}
DEFINE_TRACE(MemoryCache) {
visitor->trace(m_resourceMaps);
MemoryCacheDumpClient::trace(visitor);
MemoryCoordinatorClient::trace(visitor);
}
KURL MemoryCache::removeFragmentIdentifierIfNeeded(const KURL& originalURL) {
if (!originalURL.hasFragmentIdentifier())
return originalURL;
// Strip away fragment identifier from HTTP URLs. Data URLs must be
// unmodified. For file and custom URLs clients may expect resources to be
// unique even when they differ by the fragment identifier only.
if (!originalURL.protocolIsInHTTPFamily())
return originalURL;
KURL url = originalURL;
url.removeFragmentIdentifier();
return url;
}
String MemoryCache::defaultCacheIdentifier() {
return emptyString();
}
MemoryCache::ResourceMap* MemoryCache::ensureResourceMap(
const String& cacheIdentifier) {
if (!m_resourceMaps.contains(cacheIdentifier)) {
ResourceMapIndex::AddResult result =
m_resourceMaps.add(cacheIdentifier, new ResourceMap);
CHECK(result.isNewEntry);
}
return m_resourceMaps.get(cacheIdentifier);
}
void MemoryCache::add(Resource* resource) {
DCHECK(resource);
ResourceMap* resources = ensureResourceMap(resource->cacheIdentifier());
addInternal(resources, MemoryCacheEntry::create(resource));
RESOURCE_LOADING_DVLOG(1) << "MemoryCache::add Added "
<< resource->url().getString() << ", resource "
<< resource;
}
void MemoryCache::addInternal(ResourceMap* resourceMap,
MemoryCacheEntry* entry) {
DCHECK(WTF::isMainThread());
DCHECK(resourceMap);
Resource* resource = entry->resource();
if (!resource)
return;
DCHECK(resource->url().isValid());
KURL url = removeFragmentIdentifierIfNeeded(resource->url());
ResourceMap::iterator it = resourceMap->find(url);
if (it != resourceMap->end()) {
Resource* oldResource = it->value->resource();
CHECK_NE(oldResource, resource);
update(oldResource, oldResource->size(), 0);
}
resourceMap->set(url, entry);
update(resource, 0, resource->size());
}
void MemoryCache::remove(Resource* resource) {
DCHECK(WTF::isMainThread());
DCHECK(resource);
RESOURCE_LOADING_DVLOG(1) << "Evicting resource " << resource << " for "
<< resource->url().getString() << " from cache";
TRACE_EVENT1("blink", "MemoryCache::evict", "resource",
resource->url().getString().utf8());
ResourceMap* resources = m_resourceMaps.get(resource->cacheIdentifier());
if (!resources)
return;
KURL url = removeFragmentIdentifierIfNeeded(resource->url());
ResourceMap::iterator it = resources->find(url);
if (it == resources->end() || it->value->resource() != resource)
return;
removeInternal(resources, it);
}
void MemoryCache::removeInternal(ResourceMap* resourceMap,
const ResourceMap::iterator& it) {
DCHECK(WTF::isMainThread());
DCHECK(resourceMap);
Resource* resource = it->value->resource();
DCHECK(resource);
update(resource, resource->size(), 0);
resourceMap->remove(it);
}
bool MemoryCache::contains(const Resource* resource) const {
if (!resource || resource->url().isEmpty())
return false;
const ResourceMap* resources =
m_resourceMaps.get(resource->cacheIdentifier());
if (!resources)
return false;
KURL url = removeFragmentIdentifierIfNeeded(resource->url());
MemoryCacheEntry* entry = resources->get(url);
return entry && resource == entry->resource();
}
Resource* MemoryCache::resourceForURL(const KURL& resourceURL) const {
return resourceForURL(resourceURL, defaultCacheIdentifier());
}
Resource* MemoryCache::resourceForURL(const KURL& resourceURL,
const String& cacheIdentifier) const {
DCHECK(WTF::isMainThread());
if (!resourceURL.isValid() || resourceURL.isNull())
return nullptr;
DCHECK(!cacheIdentifier.isNull());
const ResourceMap* resources = m_resourceMaps.get(cacheIdentifier);
if (!resources)
return nullptr;
MemoryCacheEntry* entry =
resources->get(removeFragmentIdentifierIfNeeded(resourceURL));
if (!entry)
return nullptr;
return entry->resource();
}
HeapVector<Member<Resource>> MemoryCache::resourcesForURL(
const KURL& resourceURL) const {
DCHECK(WTF::isMainThread());
KURL url = removeFragmentIdentifierIfNeeded(resourceURL);
HeapVector<Member<Resource>> results;
for (const auto& resourceMapIter : m_resourceMaps) {
if (MemoryCacheEntry* entry = resourceMapIter.value->get(url)) {
Resource* resource = entry->resource();
DCHECK(resource);
results.push_back(resource);
}
}
return results;
}
void MemoryCache::pruneResources(PruneStrategy strategy) {
DCHECK(!m_prunePending);
const size_t sizeLimit = (strategy == MaximalPrune) ? 0 : capacity();
if (m_size <= sizeLimit)
return;
// Cut by a percentage to avoid immediately pruning again.
size_t targetSize = static_cast<size_t>(sizeLimit * cTargetPrunePercentage);
for (const auto& resourceMapIter : m_resourceMaps) {
for (const auto& resourceIter : *resourceMapIter.value) {
Resource* resource = resourceIter.value->resource();
DCHECK(resource);
if (resource->isLoaded() && resource->decodedSize()) {
// Check to see if the remaining resources are too new to prune.
double elapsedTime =
m_pruneFrameTimeStamp - resourceIter.value->m_lastDecodedAccessTime;
if (strategy == AutomaticPrune &&
elapsedTime < m_delayBeforeLiveDecodedPrune)
continue;
resource->prune();
if (m_size <= targetSize)
return;
}
}
}
}
void MemoryCache::setCapacity(size_t totalBytes) {
m_capacity = totalBytes;
prune();
}
void MemoryCache::update(Resource* resource, size_t oldSize, size_t newSize) {
if (!contains(resource))
return;
ptrdiff_t delta = newSize - oldSize;
DCHECK(delta >= 0 || m_size >= static_cast<size_t>(-delta));
m_size += delta;
}
void MemoryCache::removeURLFromCache(const KURL& url) {
HeapVector<Member<Resource>> resources = resourcesForURL(url);
for (Resource* resource : resources)
remove(resource);
}
void MemoryCache::TypeStatistic::addResource(Resource* o) {
count++;
size += o->size();
decodedSize += o->decodedSize();
encodedSize += o->encodedSize();
overheadSize += o->overheadSize();
encodedSizeDuplicatedInDataURLs +=
o->url().protocolIsData() ? o->encodedSize() : 0;
}
MemoryCache::Statistics MemoryCache::getStatistics() const {
Statistics stats;
for (const auto& resourceMapIter : m_resourceMaps) {
for (const auto& resourceIter : *resourceMapIter.value) {
Resource* resource = resourceIter.value->resource();
DCHECK(resource);
switch (resource->getType()) {
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(EvictResourcePolicy policy) {
for (auto resourceMapIter = m_resourceMaps.begin();
resourceMapIter != m_resourceMaps.end();) {
ResourceMap* resources = resourceMapIter->value.get();
HeapVector<Member<MemoryCacheEntry>> unusedPreloads;
for (auto resourceIter = resources->begin();
resourceIter != resources->end(); resourceIter = resources->begin()) {
DCHECK(resourceIter.get());
DCHECK(resourceIter->value.get());
DCHECK(resourceIter->value->resource());
Resource* resource = resourceIter->value->resource();
DCHECK(resource);
if (policy != EvictAllResources && resource->isUnusedPreload()) {
// Store unused preloads aside, so they could be added back later.
// That is in order to avoid the performance impact of iterating over
// the same resource multiple times.
unusedPreloads.push_back(resourceIter->value.get());
}
removeInternal(resources, resourceIter);
}
for (const auto& unusedPreload : unusedPreloads) {
addInternal(resources, unusedPreload);
}
// We may iterate multiple times over resourceMaps with unused preloads.
// That's extremely unlikely to have any real-life performance impact.
if (!resources->size()) {
m_resourceMaps.remove(resourceMapIter);
resourceMapIter = m_resourceMaps.begin();
} else {
++resourceMapIter;
}
}
}
void MemoryCache::prune() {
TRACE_EVENT0("renderer", "MemoryCache::prune()");
if (m_inPruneResources)
return;
if (m_size <= m_capacity) // Fast path.
return;
// To avoid burdening the current thread with repetitive pruning jobs, pruning
// is postponed until the end of the current task. If it has been more than
// m_maxPruneDeferralDelay since the last prune, then we prune immediately. If
// the current thread's run loop is not active, then pruning will happen
// immediately only if it has been over m_maxPruneDeferralDelay since the last
// prune.
double currentTime = WTF::currentTime();
if (m_prunePending) {
if (currentTime - m_pruneTimeStamp >= m_maxPruneDeferralDelay) {
pruneNow(currentTime, AutomaticPrune);
}
} else {
if (currentTime - m_pruneTimeStamp >= m_maxPruneDeferralDelay) {
pruneNow(currentTime, AutomaticPrune); // Delay exceeded, prune now.
} else {
// Defer.
Platform::current()->currentThread()->addTaskObserver(this);
m_prunePending = true;
}
}
}
void MemoryCache::willProcessTask() {}
void MemoryCache::didProcessTask() {
// Perform deferred pruning
DCHECK(m_prunePending);
pruneNow(WTF::currentTime(), AutomaticPrune);
}
void MemoryCache::pruneAll() {
double currentTime = WTF::currentTime();
pruneNow(currentTime, MaximalPrune);
}
void MemoryCache::pruneNow(double currentTime, PruneStrategy strategy) {
if (m_prunePending) {
m_prunePending = false;
Platform::current()->currentThread()->removeTaskObserver(this);
}
AutoReset<bool> reentrancyProtector(&m_inPruneResources, true);
pruneResources(strategy);
m_pruneFrameTimeStamp = m_lastFramePaintTimeStamp;
m_pruneTimeStamp = currentTime;
}
void MemoryCache::updateFramePaintTimestamp() {
m_lastFramePaintTimeStamp = currentTime();
}
bool MemoryCache::onMemoryDump(WebMemoryDumpLevelOfDetail levelOfDetail,
WebProcessMemoryDump* memoryDump) {
if (levelOfDetail == WebMemoryDumpLevelOfDetail::Background) {
Statistics stats = getStatistics();
WebMemoryAllocatorDump* dump1 =
memoryDump->createMemoryAllocatorDump("web_cache/Image_resources");
dump1->addScalar("size", "bytes",
stats.images.encodedSize + stats.images.overheadSize);
WebMemoryAllocatorDump* dump2 = memoryDump->createMemoryAllocatorDump(
"web_cache/CSS stylesheet_resources");
dump2->addScalar("size", "bytes", stats.cssStyleSheets.encodedSize +
stats.cssStyleSheets.overheadSize);
WebMemoryAllocatorDump* dump3 =
memoryDump->createMemoryAllocatorDump("web_cache/Script_resources");
dump3->addScalar("size", "bytes",
stats.scripts.encodedSize + stats.scripts.overheadSize);
WebMemoryAllocatorDump* dump4 = memoryDump->createMemoryAllocatorDump(
"web_cache/XSL stylesheet_resources");
dump4->addScalar("size", "bytes", stats.xslStyleSheets.encodedSize +
stats.xslStyleSheets.overheadSize);
WebMemoryAllocatorDump* dump5 =
memoryDump->createMemoryAllocatorDump("web_cache/Font_resources");
dump5->addScalar("size", "bytes",
stats.fonts.encodedSize + stats.fonts.overheadSize);
WebMemoryAllocatorDump* dump6 =
memoryDump->createMemoryAllocatorDump("web_cache/Other_resources");
dump6->addScalar("size", "bytes",
stats.other.encodedSize + stats.other.overheadSize);
return true;
}
for (const auto& resourceMapIter : m_resourceMaps) {
for (const auto& resourceIter : *resourceMapIter.value) {
Resource* resource = resourceIter.value->resource();
resource->onMemoryDump(levelOfDetail, memoryDump);
}
}
return true;
}
void MemoryCache::onMemoryPressure(WebMemoryPressureLevel level) {
pruneAll();
}
} // namespace blink
|