Changeset 179823 in webkit
- Timestamp:
- Feb 9, 2015, 3:03:06 AM (12 years ago)
- Location:
- trunk/Source/WebKit2
- Files:
-
- 3 edited
-
ChangeLog (modified) (1 diff)
-
NetworkProcess/cache/NetworkCacheStorage.h (modified) (3 diffs)
-
NetworkProcess/cache/NetworkCacheStorageCocoa.mm (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebKit2/ChangeLog
r179821 r179823 1 2015-02-08 Antti Koivisto <antti@apple.com> 2 3 Measure cache size more accurately 4 https://bugs.webkit.org/show_bug.cgi?id=141378 5 <rdar://problem/19760224> 6 7 Reviewed by Chris Dumez. 8 9 Estimate the cache disk space usage from the actual entry sizes instead of the item count. 10 This prevents large cache items from making the cache grow beyond its bounds. 11 12 * NetworkProcess/cache/NetworkCacheStorage.h: 13 * NetworkProcess/cache/NetworkCacheStorageCocoa.mm: 14 (WebKit::NetworkCacheStorage::initialize): 15 (WebKit::NetworkCacheStorage::removeEntry): 16 (WebKit::NetworkCacheStorage::store): 17 (WebKit::NetworkCacheStorage::clear): 18 (WebKit::NetworkCacheStorage::shrinkIfNeeded): 19 1 20 2015-02-09 Carlos Garcia Campos <cgarcia@igalia.com> 2 21 -
trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h
r179804 r179823 30 30 31 31 #include "NetworkCacheKey.h" 32 #include <WebCore/ResourceResponse.h>33 32 #include <wtf/BloomFilter.h> 34 33 #include <wtf/Deque.h> … … 46 45 47 46 namespace WebKit { 48 49 class ShareableResource;50 47 51 48 #if PLATFORM(COCOA) … … 193 190 194 191 BloomFilter<20> m_contentsFilter; 195 std::atomic<size_t> m_approximate EntryCount{ 0 };192 std::atomic<size_t> m_approximateSize { 0 }; 196 193 std::atomic<bool> m_shrinkInProgress { false }; 197 194 -
trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm
r179804 r179823 35 35 #include <sys/mman.h> 36 36 #include <sys/stat.h> 37 #include <wtf/RandomNumber.h> 37 38 #include <wtf/RunLoop.h> 38 39 #include <wtf/text/CString.h> … … 110 111 111 112 StringCapture cachePathCapture(m_directoryPath); 112 auto& entryCount = m_approximateEntryCount; 113 114 dispatch_async(m_backgroundIOQueue.get(), [this, cachePathCapture, &entryCount] { 113 114 dispatch_async(m_backgroundIOQueue.get(), [this, cachePathCapture] { 115 115 String cachePath = cachePathCapture.string(); 116 traverseCacheFiles(cachePath, [this , &entryCount](const String& fileName, const String&) {116 traverseCacheFiles(cachePath, [this](const String& fileName, const String& partitionPath) { 117 117 NetworkCacheKey::HashType hash; 118 118 if (!NetworkCacheKey::stringToHash(fileName, hash)) … … 122 122 m_contentsFilter.add(shortHash); 123 123 }); 124 ++entryCount; 124 auto filePath = WebCore::pathByAppendingComponent(partitionPath, fileName); 125 long long fileSize = 0; 126 WebCore::getFileSize(filePath, fileSize); 127 m_approximateSize += fileSize; 125 128 }); 126 129 }); … … 324 327 ASSERT(RunLoop::isMain()); 325 328 329 // For simplicity we don't reduce m_approximateSize on removals caused by load or decode errors. 330 // The next cache shrink will update the size. 331 326 332 if (m_contentsFilter.mayContain(key.shortHash())) 327 333 m_contentsFilter.remove(key.shortHash()); 328 334 329 335 StringCapture filePathCapture(filePathForKey(key, m_directoryPath)); 330 dispatch_async(m_ ioQueue.get(), [this, filePathCapture] {336 dispatch_async(m_backgroundIOQueue.get(), [this, filePathCapture] { 331 337 WebCore::deleteFile(filePathCapture.string()); 332 if (m_approximateEntryCount)333 --m_approximateEntryCount;334 338 }); 335 339 } … … 430 434 431 435 m_contentsFilter.add(key.shortHash()); 432 ++m_approximateEntryCount;433 436 434 437 auto storeOperation = std::make_unique<StoreOperation>(StoreOperation { key, entry, WTF::move(completionHandler) }); … … 442 445 443 446 size_t bodyOffset = dispatch_data_get_size(encodedHeader.get()); 444 size_t bodySize = store.entry.body.size();445 447 446 448 int fd; 447 449 auto channel = openFileForKey(store.key, FileOpenType::Create, cachePathCapture.string(), fd); 448 dispatch_io_write(channel.get(), 0, writeData.get(), dispatch_get_main_queue(), [this, &store, fd, bodyOffset , bodySize](bool done, dispatch_data_t, int error) {450 dispatch_io_write(channel.get(), 0, writeData.get(), dispatch_get_main_queue(), [this, &store, fd, bodyOffset](bool done, dispatch_data_t, int error) { 449 451 ASSERT_UNUSED(done, done); 450 452 LOG(NetworkCacheStorage, "(NetworkProcess) write complete error=%d", error); … … 452 454 if (m_contentsFilter.mayContain(store.key.shortHash())) 453 455 m_contentsFilter.remove(store.key.shortHash()); 454 if (m_approximateEntryCount)455 --m_approximateEntryCount;456 456 } 457 size_t bodySize = store.entry.body.size(); 458 size_t totalSize = bodyOffset + bodySize; 459 460 m_approximateSize += totalSize; 457 461 458 462 bool shouldMapBody = !error && bodySize >= vm_page_size; … … 532 536 533 537 m_contentsFilter.clear(); 534 m_approximate EntryCount= 0;538 m_approximateSize = 0; 535 539 536 540 StringCapture directoryPathCapture(m_directoryPath); … … 550 554 void NetworkCacheStorage::shrinkIfNeeded() 551 555 { 552 const size_t assumedAverageResourceSize { 48 * 1024 }; 553 const size_t everyNthResourceToDelete { 4 }; 554 555 size_t estimatedCacheSize = assumedAverageResourceSize * m_approximateEntryCount; 556 557 if (estimatedCacheSize <= m_maximumSize) 556 ASSERT(RunLoop::isMain()); 557 558 static const double deletionProbability { 0.25 }; 559 560 if (m_approximateSize <= m_maximumSize) 558 561 return; 559 562 if (m_shrinkInProgress) … … 561 564 m_shrinkInProgress = true; 562 565 563 LOG(NetworkCacheStorage, "(NetworkProcess) shrinking cache m_approximateEntryCount=%d estimatedCacheSize=%d, m_maximumSize=%d", static_cast<size_t>(m_approximateEntryCount), estimatedCacheSize, m_maximumSize); 566 LOG(NetworkCacheStorage, "(NetworkProcess) shrinking cache approximateSize=%d, m_maximumSize=%d", static_cast<size_t>(m_approximateSize), m_maximumSize); 567 568 m_approximateSize = 0; 564 569 565 570 StringCapture cachePathCapture(m_directoryPath); 566 571 dispatch_async(m_backgroundIOQueue.get(), [this, cachePathCapture] { 567 572 String cachePath = cachePathCapture.string(); 568 size_t foundEntryCount = 0; 569 size_t deletedCount = 0; 570 traverseCacheFiles(cachePath, [this, &foundEntryCount, &deletedCount](const String& fileName, const String& partitionPath) { 571 ++foundEntryCount; 572 if (foundEntryCount % everyNthResourceToDelete) 573 traverseCacheFiles(cachePath, [this](const String& fileName, const String& partitionPath) { 574 auto filePath = WebCore::pathByAppendingComponent(partitionPath, fileName); 575 576 bool shouldDelete = randomNumber() < deletionProbability; 577 if (!shouldDelete) { 578 long long fileSize = 0; 579 WebCore::getFileSize(filePath, fileSize); 580 m_approximateSize += fileSize; 573 581 return; 574 ++deletedCount; 575 576 WebCore::deleteFile(WebCore::pathByAppendingComponent(partitionPath, fileName)); 577 582 } 583 584 WebCore::deleteFile(filePath); 578 585 NetworkCacheKey::HashType hash; 579 586 if (!NetworkCacheKey::stringToHash(fileName, hash)) … … 585 592 }); 586 593 }); 587 m_approximateEntryCount = foundEntryCount - deletedCount;588 594 m_shrinkInProgress = false; 589 595 590 LOG(NetworkCacheStorage, "(NetworkProcess) cache shrink completed m_approximateEntryCount=%d", static_cast<size_t>(m_approximateEntryCount));596 LOG(NetworkCacheStorage, "(NetworkProcess) cache shrink completed approximateSize=%d", static_cast<size_t>(m_approximateSize)); 591 597 }); 592 598 }
Note:
See TracChangeset
for help on using the changeset viewer.