[Qt][WK2] Compute and set cache capacities using the current CacheModel
https://bugs.webkit.org/show_bug.cgi?id=73918
Patch by Michael BrĂ¼ning <michael.bruning@nokia.com> on 2012-02-07
Reviewed by Kenneth Rohde Christiansen.
Source/WebCore:
No new tests. (Not applicable)
Added OS-specific implementation for retrieving the free disk space.
* platform/FileSystem.h:
(WebCore):
* platform/qt/FileSystemQt.cpp:
(WebCore::getVolumeFreeSizeForPath):
(WebCore):
Source/WebKit2:
First part of the implementation. The next steps are the implementation
of the API for the Qt WebKit 2 port and the hybrid memory and disk cache.
* Shared/WebProcessCreationParameters.cpp:
(WebKit::WebProcessCreationParameters::encode):
(WebKit::WebProcessCreationParameters::decode):
* Shared/WebProcessCreationParameters.h:
(WebProcessCreationParameters):
* UIProcess/qt/WebContextQt.cpp:
(WebKit::defaultDiskCacheDirectory):
(WebKit):
(WebKit::WebContext::platformInitializeWebProcess):
* WebProcess/qt/WebProcessQt.cpp:
(WebKit::physicalMemorySizeInBytes):
(WebKit):
(WebKit::WebProcess::platformSetCacheModel):
(WebKit::WebProcess::platformInitializeWebProcess):
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@106920 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index fbcd206..792c24d 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,20 @@
+2012-02-07 Michael Brüning <michael.bruning@nokia.com>
+
+ [Qt][WK2] Compute and set cache capacities using the current CacheModel
+ https://bugs.webkit.org/show_bug.cgi?id=73918
+
+ Reviewed by Kenneth Rohde Christiansen.
+
+ No new tests. (Not applicable)
+
+ Added OS-specific implementation for retrieving the free disk space.
+
+ * platform/FileSystem.h:
+ (WebCore):
+ * platform/qt/FileSystemQt.cpp:
+ (WebCore::getVolumeFreeSizeForPath):
+ (WebCore):
+
2012-02-07 Kenneth Rohde Christiansen <kenneth@webkit.org>
[Inspector] Add the Nokia N9 user agent
diff --git a/Source/WebCore/platform/FileSystem.h b/Source/WebCore/platform/FileSystem.h
index 33c643e..934d22c 100644
--- a/Source/WebCore/platform/FileSystem.h
+++ b/Source/WebCore/platform/FileSystem.h
@@ -198,6 +198,8 @@
String filenameToString(const char*);
String filenameForDisplay(const String&);
CString applicationDirectoryPath();
+#endif
+#if PLATFORM(GTK) || PLATFORM(QT)
uint64_t getVolumeFreeSizeForPath(const char*);
#endif
diff --git a/Source/WebCore/platform/qt/FileSystemQt.cpp b/Source/WebCore/platform/qt/FileSystemQt.cpp
index 32644d1..a6896e1 100644
--- a/Source/WebCore/platform/qt/FileSystemQt.cpp
+++ b/Source/WebCore/platform/qt/FileSystemQt.cpp
@@ -39,6 +39,7 @@
#include <QFile>
#include <QFileInfo>
#include <QTemporaryFile>
+#include <sys/statvfs.h>
#include <wtf/text/CString.h>
namespace WebCore {
@@ -190,6 +191,23 @@
return -1;
}
+uint64_t getVolumeFreeSizeForPath(const char* path)
+{
+#if defined(Q_OS_WIN)
+ ULARGE_INTEGER freeBytesToCaller;
+ BOOL result = GetDiskFreeSpaceExW((LPCWSTR)path, &freeBytesToCaller, 0, 0);
+ if (!result)
+ return 0;
+ return static_cast<uint64_t>freeBytesToCaller.QuadPart;
+#else
+ struct statvfs volumeInfo;
+ if (statvfs(path, &volumeInfo))
+ return 0;
+
+ return static_cast<uint64_t>(volumeInfo.f_bavail) * static_cast<uint64_t>(volumeInfo.f_frsize);
+#endif
+}
+
int writeToFile(PlatformFileHandle handle, const char* data, int length)
{
if (handle && handle->exists() && handle->isWritable())
diff --git a/Source/WebKit2/ChangeLog b/Source/WebKit2/ChangeLog
index 410d0a6..2b44e88 100644
--- a/Source/WebKit2/ChangeLog
+++ b/Source/WebKit2/ChangeLog
@@ -1,3 +1,28 @@
+2012-02-07 Michael Brüning <michael.bruning@nokia.com>
+
+ [Qt][WK2] Compute and set cache capacities using the current CacheModel
+ https://bugs.webkit.org/show_bug.cgi?id=73918
+
+ Reviewed by Kenneth Rohde Christiansen.
+
+ First part of the implementation. The next steps are the implementation
+ of the API for the Qt WebKit 2 port and the hybrid memory and disk cache.
+
+ * Shared/WebProcessCreationParameters.cpp:
+ (WebKit::WebProcessCreationParameters::encode):
+ (WebKit::WebProcessCreationParameters::decode):
+ * Shared/WebProcessCreationParameters.h:
+ (WebProcessCreationParameters):
+ * UIProcess/qt/WebContextQt.cpp:
+ (WebKit::defaultDiskCacheDirectory):
+ (WebKit):
+ (WebKit::WebContext::platformInitializeWebProcess):
+ * WebProcess/qt/WebProcessQt.cpp:
+ (WebKit::physicalMemorySizeInBytes):
+ (WebKit):
+ (WebKit::WebProcess::platformSetCacheModel):
+ (WebKit::WebProcess::platformInitializeWebProcess):
+
2012-02-06 Shinya Kawanaka <shinyak@google.com>
Remove Element::ensureShadowRoot export.
diff --git a/Source/WebKit2/Shared/WebProcessCreationParameters.cpp b/Source/WebKit2/Shared/WebProcessCreationParameters.cpp
index 8e1d164..7c7995d 100644
--- a/Source/WebKit2/Shared/WebProcessCreationParameters.cpp
+++ b/Source/WebKit2/Shared/WebProcessCreationParameters.cpp
@@ -98,6 +98,7 @@
#endif
#if PLATFORM(QT)
encoder->encode(cookieStorageDirectory);
+ encoder->encode(diskCacheDirectory);
#endif
#if ENABLE(NOTIFICATIONS)
@@ -195,6 +196,8 @@
#if PLATFORM(QT)
if (!decoder->decode(parameters.cookieStorageDirectory))
return false;
+ if (!decoder->decode(parameters.diskCacheDirectory))
+ return false;
#endif
#if ENABLE(NOTIFICATIONS)
diff --git a/Source/WebKit2/Shared/WebProcessCreationParameters.h b/Source/WebKit2/Shared/WebProcessCreationParameters.h
index 3a3764d..c73db8c 100644
--- a/Source/WebKit2/Shared/WebProcessCreationParameters.h
+++ b/Source/WebKit2/Shared/WebProcessCreationParameters.h
@@ -121,6 +121,7 @@
#endif // PLATFORM(WIN)
#if PLATFORM(QT)
String cookieStorageDirectory;
+ String diskCacheDirectory;
#endif
#if ENABLE(NOTIFICATIONS)
diff --git a/Source/WebKit2/UIProcess/qt/WebContextQt.cpp b/Source/WebKit2/UIProcess/qt/WebContextQt.cpp
index b0d5ac4..74e321c 100644
--- a/Source/WebKit2/UIProcess/qt/WebContextQt.cpp
+++ b/Source/WebKit2/UIProcess/qt/WebContextQt.cpp
@@ -57,6 +57,18 @@
return s_dataLocation;
}
+static String defaultDiskCacheDirectory()
+{
+ static String s_defaultDiskCacheDirectory;
+
+ if (!s_defaultDiskCacheDirectory.isEmpty())
+ return s_defaultDiskCacheDirectory;
+
+ s_defaultDiskCacheDirectory = WebCore::pathByAppendingComponent(defaultDataLocation(), "cache/");
+ WebCore::makeAllDirectories(s_defaultDiskCacheDirectory);
+ return s_defaultDiskCacheDirectory;
+}
+
static QString s_defaultDatabaseDirectory;
static QString s_defaultLocalStorageDirectory;
@@ -69,6 +81,8 @@
{
qRegisterMetaType<QProcess::ExitStatus>("QProcess::ExitStatus");
parameters.cookieStorageDirectory = defaultDataLocation();
+ parameters.diskCacheDirectory = defaultDiskCacheDirectory();
+
#if ENABLE(GEOLOCATION)
static WebGeolocationProviderQt* location = WebGeolocationProviderQt::create(toAPI(geolocationManagerProxy()));
WKGeolocationManagerSetProvider(toAPI(geolocationManagerProxy()), WebGeolocationProviderQt::provider(location));
diff --git a/Source/WebKit2/WebProcess/qt/WebProcessQt.cpp b/Source/WebKit2/WebProcess/qt/WebProcessQt.cpp
index a2beac1..bdac9f5 100644
--- a/Source/WebKit2/WebProcess/qt/WebProcessQt.cpp
+++ b/Source/WebKit2/WebProcess/qt/WebProcessQt.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
+ * Copyright (C) 2011, 2012 Nokia Corporation and/or its subsidiary(-ies)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -35,24 +36,94 @@
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkCookieJar>
+#include <QNetworkDiskCache>
#include <WebCore/CookieJarQt.h>
+#include <WebCore/FileSystem.h>
+#include <WebCore/MemoryCache.h>
#include <WebCore/PageCache.h>
#include <WebCore/RuntimeEnabledFeatures.h>
#if defined(Q_OS_MACX)
#include <dispatch/dispatch.h>
+#include <mach/host_info.h>
+#include <mach/mach.h>
+#include <mach/mach_error.h>
+#elif defined(Q_OS_WIN)
+
+#else
+#include <unistd.h>
#endif
using namespace WebCore;
namespace WebKit {
-static const int DefaultPageCacheCapacity = 20;
-
-void WebProcess::platformSetCacheModel(CacheModel)
+static uint64_t physicalMemorySizeInBytes()
{
- // FIXME: see bug 73918
- pageCache()->setCapacity(DefaultPageCacheCapacity);
+ static uint64_t physicalMemorySize = 0;
+
+ if (!physicalMemorySize) {
+#if defined(Q_OS_MACX)
+ host_basic_info_data_t hostInfo;
+ mach_port_t host = mach_host_self();
+ mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
+ kern_return_t r = host_info(host, HOST_BASIC_INFO, (host_info_t)&hostInfo, &count);
+ mach_port_deallocate(mach_task_self(), host);
+
+ if (r == KERN_SUCCESS)
+ physicalMemorySize = hostInfo.max_mem;
+
+#elif defined(Q_OS_WIN)
+ MEMORYSTATUSEX statex;
+ statex.dwLength = sizeof(statex);
+ GlobalMemoryStatusEx(&statex);
+ physicalMemorySize = static_cast<uint64_t>(statex.ullTotalPhys);
+
+#else
+ long pageSize = sysconf(_SC_PAGESIZE);
+ long numberOfPages = sysconf(_SC_PHYS_PAGES);
+
+ if (pageSize > 0 && numberOfPages > 0)
+ physicalMemorySize = static_cast<uint64_t>(pageSize) * static_cast<uint64_t>(numberOfPages);
+
+#endif
+ }
+ return physicalMemorySize;
+}
+
+void WebProcess::platformSetCacheModel(CacheModel cacheModel)
+{
+ QNetworkDiskCache* diskCache = qobject_cast<QNetworkDiskCache*>(m_networkAccessManager->cache());
+ ASSERT(diskCache);
+
+ ASSERT(WebCore::platformInfo());
+ uint64_t physicalMemorySizeInMegabytes = physicalMemorySizeInBytes() / 1024 / 1024;
+
+ // The Mac port of WebKit2 uses a fudge factor of 1000 here to account for misalignment, however,
+ // that tends to overestimate the memory quite a bit (1 byte misalignment ~ 48 MiB misestimation).
+ // We use 1024 * 1023 for now to keep the estimation error down to +/- ~1 MiB.
+ uint64_t freeVolumeSpace = WebCore::getVolumeFreeSizeForPath(diskCache->cacheDirectory().toAscii().constData()) / 1024 / 1023;
+
+ // The following variables are initialised to 0 because WebProcess::calculateCacheSizes might not
+ // set them in some rare cases.
+ unsigned cacheTotalCapacity = 0;
+ unsigned cacheMinDeadCapacity = 0;
+ unsigned cacheMaxDeadCapacity = 0;
+ double deadDecodedDataDeletionInterval = 0;
+ unsigned pageCacheCapacity = 0;
+ unsigned long urlCacheMemoryCapacity = 0;
+ unsigned long urlCacheDiskCapacity = 0;
+
+ calculateCacheSizes(cacheModel, physicalMemorySizeInMegabytes, freeVolumeSpace,
+ cacheTotalCapacity, cacheMinDeadCapacity, cacheMaxDeadCapacity, deadDecodedDataDeletionInterval,
+ pageCacheCapacity, urlCacheMemoryCapacity, urlCacheDiskCapacity);
+
+ memoryCache()->setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
+ memoryCache()->setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
+ pageCache()->setCapacity(pageCacheCapacity);
+
+ // FIXME: Implement hybrid in-memory- and disk-caching as e.g. the Mac port does.
+ diskCache->setMaximumCacheSize(static_cast<qint64>(urlCacheDiskCapacity));
}
void WebProcess::platformClearResourceCaches(ResourceCachesToClear)
@@ -69,6 +140,13 @@
void WebProcess::platformInitializeWebProcess(const WebProcessCreationParameters& parameters, CoreIPC::ArgumentDecoder* arguments)
{
m_networkAccessManager = new QtNetworkAccessManager(this);
+
+ ASSERT(!parameters.diskCacheDirectory.isEmpty() && !parameters.diskCacheDirectory.isNull());
+ QNetworkDiskCache* diskCache = new QNetworkDiskCache();
+ diskCache->setCacheDirectory(parameters.diskCacheDirectory);
+ // The m_networkAccessManager takes ownership of the diskCache object upon the following call.
+ m_networkAccessManager->setCache(diskCache);
+
ASSERT(!parameters.cookieStorageDirectory.isEmpty() && !parameters.cookieStorageDirectory.isNull());
WebCore::SharedCookieJarQt* jar = WebCore::SharedCookieJarQt::create(parameters.cookieStorageDirectory);
m_networkAccessManager->setCookieJar(jar);