1 /*
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21 */
22
23 #include "config.h"
24 #include "Cache.h"
25
26 #include "CachedCSSStyleSheet.h"
27 #include "CachedFont.h"
28 #include "CachedImage.h"
29 #include "CachedScript.h"
30 #include "CachedXSLStyleSheet.h"
31 #include "DocLoader.h"
32 #include "Document.h"
33 #if USE(LOW_BANDWIDTH_DISPLAY)
34 #include "Frame.h"
35 #endif
36 #include "FrameLoader.h"
37 #include "FrameView.h"
38 #include "Image.h"
39 #include "ResourceHandle.h"
40 #include <stdio.h>
41 #include <wtf/CurrentTime.h>
42
43
44 using namespace std;
45
46 namespace WebCore {
47
48 static const int cDefaultCacheCapacity = 8192 * 1024;
49 static const double cMinDelayBeforeLiveDecodedPrune = 1; // Seconds.
50 static const float cTargetPrunePercentage = .95f; // Percentage of capacity toward which we prune, to avoid immediately pruning again.
51 static const double cDefaultDecodedDataDeletionInterval = 0;
52
cache()53 Cache* cache()
54 {
55 static Cache* staticCache = new Cache;
56 return staticCache;
57 }
58
Cache()59 Cache::Cache()
60 : m_disabled(false)
61 , m_pruneEnabled(true)
62 , m_inPruneDeadResources(false)
63 , m_capacity(cDefaultCacheCapacity)
64 , m_minDeadCapacity(0)
65 , m_maxDeadCapacity(cDefaultCacheCapacity)
66 , m_deadDecodedDataDeletionInterval(cDefaultDecodedDataDeletionInterval)
67 , m_liveSize(0)
68 , m_deadSize(0)
69 {
70 }
71
createResource(CachedResource::Type type,const KURL & url,const String & charset)72 static CachedResource* createResource(CachedResource::Type type, const KURL& url, const String& charset)
73 {
74 switch (type) {
75 case CachedResource::ImageResource:
76 return new CachedImage(url.string());
77 case CachedResource::CSSStyleSheet:
78 return new CachedCSSStyleSheet(url.string(), charset);
79 case CachedResource::Script:
80 return new CachedScript(url.string(), charset);
81 case CachedResource::FontResource:
82 return new CachedFont(url.string());
83 #if ENABLE(XSLT)
84 case CachedResource::XSLStyleSheet:
85 return new CachedXSLStyleSheet(url.string());
86 #endif
87 #if ENABLE(XBL)
88 case CachedResource::XBLStyleSheet:
89 return new CachedXBLDocument(url.string());
90 #endif
91 default:
92 break;
93 }
94
95 return 0;
96 }
97
requestResource(DocLoader * docLoader,CachedResource::Type type,const KURL & url,const String & charset,bool requestIsPreload)98 CachedResource* Cache::requestResource(DocLoader* docLoader, CachedResource::Type type, const KURL& url, const String& charset, bool requestIsPreload)
99 {
100 // FIXME: Do we really need to special-case an empty URL?
101 // Would it be better to just go on with the cache code and let it fail later?
102 if (url.isEmpty())
103 return 0;
104
105 // Look up the resource in our map.
106 CachedResource* resource = resourceForURL(url.string());
107
108 if (resource && requestIsPreload && !resource->isPreloaded())
109 return 0;
110
111 if (FrameLoader::restrictAccessToLocal() && !FrameLoader::canLoad(url, String(), docLoader->doc())) {
112 Document* doc = docLoader->doc();
113 if (doc && !requestIsPreload)
114 FrameLoader::reportLocalLoadFailed(doc->frame(), url.string());
115 return 0;
116 }
117
118 if (!resource) {
119 // The resource does not exist. Create it.
120 resource = createResource(type, url, charset);
121 ASSERT(resource);
122
123 // Pretend the resource is in the cache, to prevent it from being deleted during the load() call.
124 // FIXME: CachedResource should just use normal refcounting instead.
125 resource->setInCache(true);
126
127 resource->load(docLoader);
128
129 if (!disabled())
130 m_resources.set(url.string(), resource); // The size will be added in later once the resource is loaded and calls back to us with the new size.
131 else {
132 // Kick the resource out of the cache, because the cache is disabled.
133 resource->setInCache(false);
134 resource->setDocLoader(docLoader);
135 if (resource->errorOccurred()) {
136 // We don't support immediate loads, but we do support immediate failure.
137 // In that case we should to delete the resource now and return 0 because otherwise
138 // it would leak if no ref/deref was ever done on it.
139 delete resource;
140 return 0;
141 }
142 }
143 }
144
145 if (resource->type() != type)
146 return 0;
147
148 #if USE(LOW_BANDWIDTH_DISPLAY)
149 // addLowBandwidthDisplayRequest() returns true if requesting CSS or JS during low bandwidth display.
150 // Here, return 0 to not block parsing or layout.
151 if (docLoader->frame() && docLoader->frame()->loader()->addLowBandwidthDisplayRequest(resource))
152 return 0;
153 #endif
154
155 if (!disabled()) {
156 // This will move the resource to the front of its LRU list and increase its access count.
157 resourceAccessed(resource);
158 }
159
160 return resource;
161 }
162
requestUserCSSStyleSheet(DocLoader * docLoader,const String & url,const String & charset)163 CachedCSSStyleSheet* Cache::requestUserCSSStyleSheet(DocLoader* docLoader, const String& url, const String& charset)
164 {
165 CachedCSSStyleSheet* userSheet;
166 if (CachedResource* existing = resourceForURL(url)) {
167 if (existing->type() != CachedResource::CSSStyleSheet)
168 return 0;
169 userSheet = static_cast<CachedCSSStyleSheet*>(existing);
170 } else {
171 userSheet = new CachedCSSStyleSheet(url, charset);
172
173 // Pretend the resource is in the cache, to prevent it from being deleted during the load() call.
174 // FIXME: CachedResource should just use normal refcounting instead.
175 userSheet->setInCache(true);
176 // Don't load incrementally, skip load checks, don't send resource load callbacks.
177 userSheet->load(docLoader, false, true, false);
178 if (!disabled())
179 m_resources.set(url, userSheet);
180 else
181 userSheet->setInCache(false);
182 }
183
184 if (!disabled()) {
185 // This will move the resource to the front of its LRU list and increase its access count.
186 resourceAccessed(userSheet);
187 }
188
189 return userSheet;
190 }
191
revalidateResource(CachedResource * resource,DocLoader * docLoader)192 void Cache::revalidateResource(CachedResource* resource, DocLoader* docLoader)
193 {
194 ASSERT(resource);
195 ASSERT(!disabled());
196 if (resource->resourceToRevalidate())
197 return;
198 if (!resource->canUseCacheValidator()) {
199 evict(resource);
200 return;
201 }
202 const String& url = resource->url();
203 CachedResource* newResource = createResource(resource->type(), KURL(url), resource->encoding());
204 newResource->setResourceToRevalidate(resource);
205 evict(resource);
206 m_resources.set(url, newResource);
207 newResource->setInCache(true);
208 resourceAccessed(newResource);
209 newResource->load(docLoader);
210 }
211
revalidationSucceeded(CachedResource * revalidatingResource,const ResourceResponse & response)212 void Cache::revalidationSucceeded(CachedResource* revalidatingResource, const ResourceResponse& response)
213 {
214 CachedResource* resource = revalidatingResource->resourceToRevalidate();
215 ASSERT(resource);
216 ASSERT(!resource->inCache());
217 ASSERT(resource->isLoaded());
218
219 evict(revalidatingResource);
220
221 ASSERT(!m_resources.get(resource->url()));
222 m_resources.set(resource->url(), resource);
223 resource->setInCache(true);
224 resource->setExpirationDate(response.expirationDate());
225 insertInLRUList(resource);
226 int delta = resource->size();
227 if (resource->decodedSize() && resource->hasClients())
228 insertInLiveDecodedResourcesList(resource);
229 if (delta)
230 adjustSize(resource->hasClients(), delta);
231
232 revalidatingResource->switchClientsToRevalidatedResource();
233 // this deletes the revalidating resource
234 revalidatingResource->clearResourceToRevalidate();
235 }
236
revalidationFailed(CachedResource * revalidatingResource)237 void Cache::revalidationFailed(CachedResource* revalidatingResource)
238 {
239 ASSERT(revalidatingResource->resourceToRevalidate());
240 revalidatingResource->clearResourceToRevalidate();
241 }
242
resourceForURL(const String & url)243 CachedResource* Cache::resourceForURL(const String& url)
244 {
245 CachedResource* resource = m_resources.get(url);
246 if (resource && !resource->makePurgeable(false)) {
247 ASSERT(!resource->hasClients());
248 evict(resource);
249 return 0;
250 }
251 return resource;
252 }
253
deadCapacity() const254 unsigned Cache::deadCapacity() const
255 {
256 // Dead resource capacity is whatever space is not occupied by live resources, bounded by an independent minimum and maximum.
257 unsigned capacity = m_capacity - min(m_liveSize, m_capacity); // Start with available capacity.
258 capacity = max(capacity, m_minDeadCapacity); // Make sure it's above the minimum.
259 capacity = min(capacity, m_maxDeadCapacity); // Make sure it's below the maximum.
260 return capacity;
261 }
262
liveCapacity() const263 unsigned Cache::liveCapacity() const
264 {
265 // Live resource capacity is whatever is left over after calculating dead resource capacity.
266 return m_capacity - deadCapacity();
267 }
268
pruneLiveResources()269 void Cache::pruneLiveResources()
270 {
271 if (!m_pruneEnabled)
272 return;
273
274 unsigned capacity = liveCapacity();
275 if (capacity && m_liveSize <= capacity)
276 return;
277
278 unsigned targetSize = static_cast<unsigned>(capacity * cTargetPrunePercentage); // Cut by a percentage to avoid immediately pruning again.
279 double currentTime = FrameView::currentPaintTimeStamp();
280 if (!currentTime) // In case prune is called directly, outside of a Frame paint.
281 currentTime = WTF::currentTime();
282
283 // Destroy any decoded data in live objects that we can.
284 // Start from the tail, since this is the least recently accessed of the objects.
285 CachedResource* current = m_liveDecodedResources.m_tail;
286 while (current) {
287 CachedResource* prev = current->m_prevInLiveResourcesList;
288 ASSERT(current->hasClients());
289 if (current->isLoaded() && current->decodedSize()) {
290 // Check to see if the remaining resources are too new to prune.
291 double elapsedTime = currentTime - current->m_lastDecodedAccessTime;
292 if (elapsedTime < cMinDelayBeforeLiveDecodedPrune)
293 return;
294
295 // Destroy our decoded data. This will remove us from
296 // m_liveDecodedResources, and possibly move us to a differnt LRU
297 // list in m_allResources.
298 current->destroyDecodedData();
299
300 if (targetSize && m_liveSize <= targetSize)
301 return;
302 }
303 current = prev;
304 }
305 }
306
pruneDeadResources()307 void Cache::pruneDeadResources()
308 {
309 if (!m_pruneEnabled)
310 return;
311
312 unsigned capacity = deadCapacity();
313 if (capacity && m_deadSize <= capacity)
314 return;
315
316 unsigned targetSize = static_cast<unsigned>(capacity * cTargetPrunePercentage); // Cut by a percentage to avoid immediately pruning again.
317 int size = m_allResources.size();
318
319 if (!m_inPruneDeadResources) {
320 // See if we have any purged resources we can evict.
321 for (int i = 0; i < size; i++) {
322 CachedResource* current = m_allResources[i].m_tail;
323 while (current) {
324 CachedResource* prev = current->m_prevInAllResourcesList;
325 if (current->wasPurged()) {
326 ASSERT(!current->hasClients());
327 ASSERT(!current->isPreloaded());
328 evict(current);
329 }
330 current = prev;
331 }
332 }
333 if (targetSize && m_deadSize <= targetSize)
334 return;
335 }
336
337 bool canShrinkLRULists = true;
338 m_inPruneDeadResources = true;
339 for (int i = size - 1; i >= 0; i--) {
340 // Remove from the tail, since this is the least frequently accessed of the objects.
341 CachedResource* current = m_allResources[i].m_tail;
342
343 // First flush all the decoded data in this queue.
344 while (current) {
345 CachedResource* prev = current->m_prevInAllResourcesList;
346 if (!current->hasClients() && !current->isPreloaded() && current->isLoaded()) {
347 // Destroy our decoded data. This will remove us from
348 // m_liveDecodedResources, and possibly move us to a differnt
349 // LRU list in m_allResources.
350 current->destroyDecodedData();
351
352 if (targetSize && m_deadSize <= targetSize) {
353 m_inPruneDeadResources = false;
354 return;
355 }
356 }
357 current = prev;
358 }
359
360 // Now evict objects from this queue.
361 current = m_allResources[i].m_tail;
362 while (current) {
363 CachedResource* prev = current->m_prevInAllResourcesList;
364 if (!current->hasClients() && !current->isPreloaded()) {
365 evict(current);
366 // If evict() caused pruneDeadResources() to be re-entered, bail out. This can happen when removing an
367 // SVG CachedImage that has subresources.
368 if (!m_inPruneDeadResources)
369 return;
370
371 if (targetSize && m_deadSize <= targetSize) {
372 m_inPruneDeadResources = false;
373 return;
374 }
375 }
376 current = prev;
377 }
378
379 // Shrink the vector back down so we don't waste time inspecting
380 // empty LRU lists on future prunes.
381 if (m_allResources[i].m_head)
382 canShrinkLRULists = false;
383 else if (canShrinkLRULists)
384 m_allResources.resize(i);
385 }
386 m_inPruneDeadResources = false;
387 }
388
setCapacities(unsigned minDeadBytes,unsigned maxDeadBytes,unsigned totalBytes)389 void Cache::setCapacities(unsigned minDeadBytes, unsigned maxDeadBytes, unsigned totalBytes)
390 {
391 ASSERT(minDeadBytes <= maxDeadBytes);
392 ASSERT(maxDeadBytes <= totalBytes);
393 m_minDeadCapacity = minDeadBytes;
394 m_maxDeadCapacity = maxDeadBytes;
395 m_capacity = totalBytes;
396 prune();
397 }
398
evict(CachedResource * resource)399 void Cache::evict(CachedResource* resource)
400 {
401 // The resource may have already been removed by someone other than our caller,
402 // who needed a fresh copy for a reload. See <http://bugs.webkit.org/show_bug.cgi?id=12479#c6>.
403 if (resource->inCache()) {
404 if (!resource->isCacheValidator()) {
405 // Notify all doc loaders that might be observing this object still that it has been
406 // extracted from the set of resources.
407 // No need to do this for cache validator resources, they are replaced automatically by using CachedResourceHandles.
408 HashSet<DocLoader*>::iterator end = m_docLoaders.end();
409 for (HashSet<DocLoader*>::iterator itr = m_docLoaders.begin(); itr != end; ++itr)
410 (*itr)->removeCachedResource(resource);
411 }
412
413 // Remove from the resource map.
414 m_resources.remove(resource->url());
415 resource->setInCache(false);
416
417 // Remove from the appropriate LRU list.
418 removeFromLRUList(resource);
419 removeFromLiveDecodedResourcesList(resource);
420
421 // Subtract from our size totals.
422 int delta = -static_cast<int>(resource->size());
423 if (delta)
424 adjustSize(resource->hasClients(), delta);
425 } else
426 ASSERT(m_resources.get(resource->url()) != resource);
427
428 if (resource->canDelete())
429 delete resource;
430 }
431
addDocLoader(DocLoader * docLoader)432 void Cache::addDocLoader(DocLoader* docLoader)
433 {
434 m_docLoaders.add(docLoader);
435 }
436
removeDocLoader(DocLoader * docLoader)437 void Cache::removeDocLoader(DocLoader* docLoader)
438 {
439 m_docLoaders.remove(docLoader);
440 }
441
fastLog2(unsigned i)442 static inline unsigned fastLog2(unsigned i)
443 {
444 unsigned log2 = 0;
445 if (i & (i - 1))
446 log2 += 1;
447 if (i >> 16)
448 log2 += 16, i >>= 16;
449 if (i >> 8)
450 log2 += 8, i >>= 8;
451 if (i >> 4)
452 log2 += 4, i >>= 4;
453 if (i >> 2)
454 log2 += 2, i >>= 2;
455 if (i >> 1)
456 log2 += 1;
457 return log2;
458 }
459
lruListFor(CachedResource * resource)460 Cache::LRUList* Cache::lruListFor(CachedResource* resource)
461 {
462 unsigned accessCount = max(resource->accessCount(), 1U);
463 unsigned queueIndex = fastLog2(resource->size() / accessCount);
464 #ifndef NDEBUG
465 resource->m_lruIndex = queueIndex;
466 #endif
467 if (m_allResources.size() <= queueIndex)
468 m_allResources.grow(queueIndex + 1);
469 return &m_allResources[queueIndex];
470 }
471
removeFromLRUList(CachedResource * resource)472 void Cache::removeFromLRUList(CachedResource* resource)
473 {
474 // If we've never been accessed, then we're brand new and not in any list.
475 if (resource->accessCount() == 0)
476 return;
477
478 #ifndef NDEBUG
479 unsigned oldListIndex = resource->m_lruIndex;
480 #endif
481
482 LRUList* list = lruListFor(resource);
483
484 #ifndef NDEBUG
485 // Verify that the list we got is the list we want.
486 ASSERT(resource->m_lruIndex == oldListIndex);
487
488 // Verify that we are in fact in this list.
489 bool found = false;
490 for (CachedResource* current = list->m_head; current; current = current->m_nextInAllResourcesList) {
491 if (current == resource) {
492 found = true;
493 break;
494 }
495 }
496 ASSERT(found);
497 #endif
498
499 CachedResource* next = resource->m_nextInAllResourcesList;
500 CachedResource* prev = resource->m_prevInAllResourcesList;
501
502 if (next == 0 && prev == 0 && list->m_head != resource)
503 return;
504
505 resource->m_nextInAllResourcesList = 0;
506 resource->m_prevInAllResourcesList = 0;
507
508 if (next)
509 next->m_prevInAllResourcesList = prev;
510 else if (list->m_tail == resource)
511 list->m_tail = prev;
512
513 if (prev)
514 prev->m_nextInAllResourcesList = next;
515 else if (list->m_head == resource)
516 list->m_head = next;
517 }
518
insertInLRUList(CachedResource * resource)519 void Cache::insertInLRUList(CachedResource* resource)
520 {
521 // Make sure we aren't in some list already.
522 ASSERT(!resource->m_nextInAllResourcesList && !resource->m_prevInAllResourcesList);
523 ASSERT(resource->inCache());
524 ASSERT(resource->accessCount() > 0);
525
526 LRUList* list = lruListFor(resource);
527
528 resource->m_nextInAllResourcesList = list->m_head;
529 if (list->m_head)
530 list->m_head->m_prevInAllResourcesList = resource;
531 list->m_head = resource;
532
533 if (!resource->m_nextInAllResourcesList)
534 list->m_tail = resource;
535
536 #ifndef NDEBUG
537 // Verify that we are in now in the list like we should be.
538 list = lruListFor(resource);
539 bool found = false;
540 for (CachedResource* current = list->m_head; current; current = current->m_nextInAllResourcesList) {
541 if (current == resource) {
542 found = true;
543 break;
544 }
545 }
546 ASSERT(found);
547 #endif
548
549 }
550
resourceAccessed(CachedResource * resource)551 void Cache::resourceAccessed(CachedResource* resource)
552 {
553 ASSERT(resource->inCache());
554
555 // Need to make sure to remove before we increase the access count, since
556 // the queue will possibly change.
557 removeFromLRUList(resource);
558
559 // If this is the first time the resource has been accessed, adjust the size of the cache to account for its initial size.
560 if (!resource->accessCount())
561 adjustSize(resource->hasClients(), resource->size());
562
563 // Add to our access count.
564 resource->increaseAccessCount();
565
566 // Now insert into the new queue.
567 insertInLRUList(resource);
568 }
569
removeFromLiveDecodedResourcesList(CachedResource * resource)570 void Cache::removeFromLiveDecodedResourcesList(CachedResource* resource)
571 {
572 // If we've never been accessed, then we're brand new and not in any list.
573 if (!resource->m_inLiveDecodedResourcesList)
574 return;
575 resource->m_inLiveDecodedResourcesList = false;
576
577 #ifndef NDEBUG
578 // Verify that we are in fact in this list.
579 bool found = false;
580 for (CachedResource* current = m_liveDecodedResources.m_head; current; current = current->m_nextInLiveResourcesList) {
581 if (current == resource) {
582 found = true;
583 break;
584 }
585 }
586 ASSERT(found);
587 #endif
588
589 CachedResource* next = resource->m_nextInLiveResourcesList;
590 CachedResource* prev = resource->m_prevInLiveResourcesList;
591
592 if (next == 0 && prev == 0 && m_liveDecodedResources.m_head != resource)
593 return;
594
595 resource->m_nextInLiveResourcesList = 0;
596 resource->m_prevInLiveResourcesList = 0;
597
598 if (next)
599 next->m_prevInLiveResourcesList = prev;
600 else if (m_liveDecodedResources.m_tail == resource)
601 m_liveDecodedResources.m_tail = prev;
602
603 if (prev)
604 prev->m_nextInLiveResourcesList = next;
605 else if (m_liveDecodedResources.m_head == resource)
606 m_liveDecodedResources.m_head = next;
607 }
608
insertInLiveDecodedResourcesList(CachedResource * resource)609 void Cache::insertInLiveDecodedResourcesList(CachedResource* resource)
610 {
611 // Make sure we aren't in the list already.
612 ASSERT(!resource->m_nextInLiveResourcesList && !resource->m_prevInLiveResourcesList && !resource->m_inLiveDecodedResourcesList);
613 resource->m_inLiveDecodedResourcesList = true;
614
615 resource->m_nextInLiveResourcesList = m_liveDecodedResources.m_head;
616 if (m_liveDecodedResources.m_head)
617 m_liveDecodedResources.m_head->m_prevInLiveResourcesList = resource;
618 m_liveDecodedResources.m_head = resource;
619
620 if (!resource->m_nextInLiveResourcesList)
621 m_liveDecodedResources.m_tail = resource;
622
623 #ifndef NDEBUG
624 // Verify that we are in now in the list like we should be.
625 bool found = false;
626 for (CachedResource* current = m_liveDecodedResources.m_head; current; current = current->m_nextInLiveResourcesList) {
627 if (current == resource) {
628 found = true;
629 break;
630 }
631 }
632 ASSERT(found);
633 #endif
634
635 }
636
addToLiveResourcesSize(CachedResource * resource)637 void Cache::addToLiveResourcesSize(CachedResource* resource)
638 {
639 m_liveSize += resource->size();
640 m_deadSize -= resource->size();
641 }
642
removeFromLiveResourcesSize(CachedResource * resource)643 void Cache::removeFromLiveResourcesSize(CachedResource* resource)
644 {
645 m_liveSize -= resource->size();
646 m_deadSize += resource->size();
647 }
648
adjustSize(bool live,int delta)649 void Cache::adjustSize(bool live, int delta)
650 {
651 if (live) {
652 ASSERT(delta >= 0 || ((int)m_liveSize + delta >= 0));
653 m_liveSize += delta;
654 } else {
655 ASSERT(delta >= 0 || ((int)m_deadSize + delta >= 0));
656 m_deadSize += delta;
657 }
658 }
659
addResource(CachedResource * o)660 void Cache::TypeStatistic::addResource(CachedResource* o)
661 {
662 bool purged = o->wasPurged();
663 bool purgeable = o->isPurgeable() && !purged;
664 int pageSize = (o->encodedSize() + o->overheadSize() + 4095) & ~4095;
665 count++;
666 size += purged ? 0 : o->size();
667 liveSize += o->hasClients() ? o->size() : 0;
668 decodedSize += o->decodedSize();
669 purgeableSize += purgeable ? pageSize : 0;
670 purgedSize += purged ? pageSize : 0;
671 }
672
getStatistics()673 Cache::Statistics Cache::getStatistics()
674 {
675 Statistics stats;
676 CachedResourceMap::iterator e = m_resources.end();
677 for (CachedResourceMap::iterator i = m_resources.begin(); i != e; ++i) {
678 CachedResource* resource = i->second;
679 switch (resource->type()) {
680 case CachedResource::ImageResource:
681 stats.images.addResource(resource);
682 break;
683 case CachedResource::CSSStyleSheet:
684 stats.cssStyleSheets.addResource(resource);
685 break;
686 case CachedResource::Script:
687 stats.scripts.addResource(resource);
688 break;
689 #if ENABLE(XSLT)
690 case CachedResource::XSLStyleSheet:
691 stats.xslStyleSheets.addResource(resource);
692 break;
693 #endif
694 case CachedResource::FontResource:
695 stats.fonts.addResource(resource);
696 break;
697 #if ENABLE(XBL)
698 case CachedResource::XBL:
699 stats.xblDocs.addResource(resource)
700 break;
701 #endif
702 default:
703 break;
704 }
705 }
706 return stats;
707 }
708
setDisabled(bool disabled)709 void Cache::setDisabled(bool disabled)
710 {
711 m_disabled = disabled;
712 if (!m_disabled)
713 return;
714
715 for (;;) {
716 CachedResourceMap::iterator i = m_resources.begin();
717 if (i == m_resources.end())
718 break;
719 evict(i->second);
720 }
721 }
722
723 #ifndef NDEBUG
dumpStats()724 void Cache::dumpStats()
725 {
726 Statistics s = getStatistics();
727 printf("%-11s %-11s %-11s %-11s %-11s %-11s %-11s\n", "", "Count", "Size", "LiveSize", "DecodedSize", "PurgeableSize", "PurgedSize");
728 printf("%-11s %-11s %-11s %-11s %-11s %-11s %-11s\n", "-----------", "-----------", "-----------", "-----------", "-----------", "-----------", "-----------");
729 printf("%-11s %11d %11d %11d %11d %11d %11d\n", "Images", s.images.count, s.images.size, s.images.liveSize, s.images.decodedSize, s.images.purgeableSize, s.images.purgedSize);
730 printf("%-11s %11d %11d %11d %11d %11d %11d\n", "CSS", s.cssStyleSheets.count, s.cssStyleSheets.size, s.cssStyleSheets.liveSize, s.cssStyleSheets.decodedSize, s.cssStyleSheets.purgeableSize, s.cssStyleSheets.purgedSize);
731 #if ENABLE(XSLT)
732 printf("%-11s %11d %11d %11d %11d %11d %11d\n", "XSL", s.xslStyleSheets.count, s.xslStyleSheets.size, s.xslStyleSheets.liveSize, s.xslStyleSheets.decodedSize, s.xslStyleSheets.purgeableSize, s.xslStyleSheets.purgedSize);
733 #endif
734 printf("%-11s %11d %11d %11d %11d %11d %11d\n", "JavaScript", s.scripts.count, s.scripts.size, s.scripts.liveSize, s.scripts.decodedSize, s.scripts.purgeableSize, s.scripts.purgedSize);
735 printf("%-11s %11d %11d %11d %11d %11d %11d\n", "Fonts", s.fonts.count, s.fonts.size, s.fonts.liveSize, s.fonts.decodedSize, s.fonts.purgeableSize, s.fonts.purgedSize);
736 printf("%-11s %-11s %-11s %-11s %-11s %-11s %-11s\n\n", "-----------", "-----------", "-----------", "-----------", "-----------", "-----------", "-----------");
737 }
738
dumpLRULists(bool includeLive) const739 void Cache::dumpLRULists(bool includeLive) const
740 {
741 printf("LRU-SP lists in eviction order (Kilobytes decoded, Kilobytes encoded, Access count, Referenced):\n");
742
743 int size = m_allResources.size();
744 for (int i = size - 1; i >= 0; i--) {
745 printf("\n\nList %d: ", i);
746 CachedResource* current = m_allResources[i].m_tail;
747 while (current) {
748 CachedResource* prev = current->m_prevInAllResourcesList;
749 if (includeLive || !current->hasClients())
750 printf("(%.1fK, %.1fK, %uA, %dR); ", current->decodedSize() / 1024.0f, (current->encodedSize() + current->overheadSize()) / 1024.0f, current->accessCount(), current->hasClients());
751 current = prev;
752 }
753 }
754 }
755 #endif
756
757 } // namespace WebCore
758