Title: [183181] trunk/Source
Revision
183181
Author
[email protected]
Date
2015-04-23 01:22:13 -0700 (Thu, 23 Apr 2015)

Log Message

There should only be one way to get the system memory size.
<https://webkit.org/b/144081>

Reviewed by Antti Koivisto.

Source/WebKit/mac:

* Misc/WebKitSystemBits.h:
* Misc/WebKitSystemBits.m:
(WebMemorySize): Deleted.
* WebView/WebView.mm:
(+[WebView _setCacheModel:]):
(roundUpToPowerOf2): Deleted.

Source/WebKit/win:

* WebView.cpp:
(WebView::setCacheModel):

Source/WebKit2:

* NetworkProcess/cocoa/NetworkProcessCocoa.mm:
(WebKit::NetworkProcess::platformSetCacheModel):
(WebKit::memorySize): Deleted.
* WebProcess/cocoa/WebProcessCocoa.mm:
(WebKit::WebProcess::platformSetCacheModel):
(WebKit::memorySize): Deleted.

Source/WTF:

Merge all the logic from other RAM size fetcher helpers into WTF's.

Darwin ports now use host_info() instead of sysctl(), since that was the more common way
of doing this.

Also bumped the fallback answer from 128 MB to 512 MB to bring it closer to today's hardware.

Finally, the number is rounded up to the next multiple of 128 MB, to avoid misunderstandings
on some systems where the number returned by the kernel is slightly lower than the marketing
number. Removed the "fudging" that was used in some places, since this fixes that cleanly.

* wtf/RAMSize.cpp:
(WTF::computeRAMSize):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (183180 => 183181)


--- trunk/Source/WTF/ChangeLog	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WTF/ChangeLog	2015-04-23 08:22:13 UTC (rev 183181)
@@ -1,3 +1,24 @@
+2015-04-23  Andreas Kling  <[email protected]>
+
+        There should only be one way to get the system memory size.
+        <https://webkit.org/b/144081>
+
+        Reviewed by Antti Koivisto.
+
+        Merge all the logic from other RAM size fetcher helpers into WTF's.
+
+        Darwin ports now use host_info() instead of sysctl(), since that was the more common way
+        of doing this.
+
+        Also bumped the fallback answer from 128 MB to 512 MB to bring it closer to today's hardware.
+
+        Finally, the number is rounded up to the next multiple of 128 MB, to avoid misunderstandings
+        on some systems where the number returned by the kernel is slightly lower than the marketing
+        number. Removed the "fudging" that was used in some places, since this fixes that cleanly.
+
+        * wtf/RAMSize.cpp:
+        (WTF::computeRAMSize):
+
 2015-04-22  Darin Adler  <[email protected]>
 
         Eliminate remaining uses of OwnPtr and PassOwnPtr in WebCore outside the editing and platform directories

Modified: trunk/Source/WTF/wtf/RAMSize.cpp (183180 => 183181)


--- trunk/Source/WTF/wtf/RAMSize.cpp	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WTF/wtf/RAMSize.cpp	2015-04-23 08:22:13 UTC (rev 183181)
@@ -27,10 +27,14 @@
 #include "RAMSize.h"
 
 #include "StdLibExtras.h"
+#include <mutex>
+
 #if OS(DARWIN)
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/sysctl.h>
+#import <dispatch/dispatch.h>
+#import <mach/host_info.h>
+#import <mach/mach.h>
+#import <mach/mach_error.h>
+#import <math.h>
 #elif OS(UNIX)
 #include <unistd.h>
 #elif OS(WINDOWS)
@@ -39,22 +43,34 @@
 
 namespace WTF {
 
-static const size_t ramSizeGuess = 128 * MB;
+static const size_t ramSizeGuess = 512 * MB;
 
 static size_t computeRAMSize()
 {
-#if OS(DARWIN)
-    int mib[2];
-    uint64_t ramSize;
-    size_t length;
+#if PLATFORM(IOS_SIMULATOR)
+    // Pretend we have 512MB of memory to make cache sizes behave like on device.
+    return ramSizeGuess;
+#elif OS(DARWIN)
+    host_basic_info_data_t hostInfo;
 
-    mib[0] = CTL_HW;
-    mib[1] = HW_MEMSIZE;
-    length = sizeof(int64_t);
-    int sysctlResult = sysctl(mib, 2, &ramSize, &length, 0, 0);
-    if (sysctlResult == -1)
+    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) {
+        LOG_ERROR("%s : host_info(%d) : %s.\n", __FUNCTION__, r, mach_error_string(r));
         return ramSizeGuess;
-    return ramSize > std::numeric_limits<size_t>::max() ? std::numeric_limits<size_t>::max() : static_cast<size_t>(ramSize);
+    }
+
+    if (hostInfo.max_mem > std::numeric_limits<size_t>::max())
+        return std::numeric_limits<size_t>::max();
+
+    size_t sizeAccordingToKernel = static_cast<size_t>(hostInfo.max_mem);
+    size_t multiple = 128 * MB;
+
+    // Round up the memory size to a multiple of 128MB because max_mem may not be exactly 512MB
+    // (for example) and we have code that depends on those boundaries.
+    return ((sizeAccordingToKernel + multiple - 1) / multiple) * multiple;
 #elif OS(UNIX)
     long pages = sysconf(_SC_PHYS_PAGES);
     long pageSize = sysconf(_SC_PAGE_SIZE);
@@ -73,7 +89,11 @@
 
 size_t ramSize()
 {
-    static const size_t ramSize = computeRAMSize();
+    static size_t ramSize;
+    static std::once_flag onceFlag;
+    std::call_once(onceFlag, [] {
+        ramSize = computeRAMSize();
+    });
     return ramSize;
 }
 

Modified: trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm (183180 => 183181)


--- trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm	2015-04-23 08:22:13 UTC (rev 183181)
@@ -42,6 +42,7 @@
 #import <MediaPlayer/MPVolumeView.h>
 #import <UIKit/UIApplication.h>
 #import <objc/runtime.h>
+#import <wtf/RAMSize.h>
 #import <wtf/RetainPtr.h>
 
 SOFT_LINK_FRAMEWORK(AVFoundation)
@@ -139,8 +140,8 @@
     if (deviceClass == wkDeviceClassiPhone || deviceClass == wkDeviceClassiPod)
         addRestriction(MediaSession::Video, InlineVideoPlaybackRestricted);
 
-    if (systemTotalMemory() < systemMemoryRequiredForVideoInBackgroundTabs) {
-        LOG(Media, "MediaSessionManageriOS::resetRestrictions - restricting video in background tabs because system memory = %zul", systemTotalMemory());
+    if (ramSize() < systemMemoryRequiredForVideoInBackgroundTabs) {
+        LOG(Media, "MediaSessionManageriOS::resetRestrictions - restricting video in background tabs because system memory = %zul", ramSize());
         addRestriction(MediaSession::Video, BackgroundTabPlaybackRestricted);
     }
 

Modified: trunk/Source/WebCore/platform/ios/LegacyTileCache.mm (183180 => 183181)


--- trunk/Source/WebCore/platform/ios/LegacyTileCache.mm	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebCore/platform/ios/LegacyTileCache.mm	2015-04-23 08:22:13 UTC (rev 183181)
@@ -42,6 +42,7 @@
 #include "WebCoreSystemInterface.h"
 #include "WebCoreThreadRun.h"
 #include <wtf/CurrentTime.h>
+#include <wtf/RAMSize.h>
 
 @interface WAKView (WebViewExtras)
 - (void)_dispatchTileDidDraw:(CALayer*)tile;
@@ -436,8 +437,7 @@
 {
     static unsigned capacity;
     if (!capacity) {
-        size_t totalMemory = systemTotalMemory();
-        totalMemory /= 1024 * 1024;
+        size_t totalMemory = ramSize() / 1024 / 1024;
         if (totalMemory >= 1024)
             capacity = 128 * 1024 * 1024;
         else if (totalMemory >= 512)

Modified: trunk/Source/WebCore/platform/ios/SystemMemory.h (183180 => 183181)


--- trunk/Source/WebCore/platform/ios/SystemMemory.h	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebCore/platform/ios/SystemMemory.h	2015-04-23 08:22:13 UTC (rev 183181)
@@ -33,7 +33,6 @@
 
 namespace WebCore {
 WEBCORE_EXPORT int systemMemoryLevel(); // In [0, 100]
-WEBCORE_EXPORT size_t systemTotalMemory();
 }
 
 #endif

Modified: trunk/Source/WebCore/platform/ios/SystemMemoryIOS.cpp (183180 => 183181)


--- trunk/Source/WebCore/platform/ios/SystemMemoryIOS.cpp	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebCore/platform/ios/SystemMemoryIOS.cpp	2015-04-23 08:22:13 UTC (rev 183181)
@@ -29,7 +29,6 @@
 #include "config.h"
 #include "SystemMemory.h"
 
-#include <mach/mach.h>
 #include <sys/sysctl.h>
 #include <wtf/Assertions.h>
 #include <wtf/CurrentTime.h>
@@ -53,35 +52,4 @@
 #endif
 }
 
-#if !PLATFORM(IOS_SIMULATOR)
-static host_basic_info_data_t gHostBasicInfo;
-
-static void initCapabilities(void)
-{
-    // Discover our CPU type
-    mach_port_t host = mach_host_self();
-    mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
-    kern_return_t returnValue = host_info(host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&gHostBasicInfo), &count);
-    mach_port_deallocate(mach_task_self(), host);
-    if (returnValue != KERN_SUCCESS)
-        LOG_ERROR("%s : host_info(%d) : %s.\n", __FUNCTION__, returnValue, mach_error_string(returnValue));
-}
-#endif
-
-size_t systemTotalMemory()
-{
-#if PLATFORM(IOS_SIMULATOR)
-    return 512 * 1024 * 1024;
-#else
-    // FIXME: Consider using C++11 thread primitives.
-    static pthread_once_t initControl = PTHREAD_ONCE_INIT;
-
-    pthread_once(&initControl, initCapabilities);
-    // The value in gHostBasicInfo.max_mem is often lower than the amount we're
-    // interested in (e.g., does this device have at least 256MB of RAM?)
-    // Round the value to the nearest power of 2.
-    return static_cast<size_t>(exp2(ceil(log2(gHostBasicInfo.max_mem))));
-#endif
-}
-
 } // namespace WebCore

Modified: trunk/Source/WebKit/mac/ChangeLog (183180 => 183181)


--- trunk/Source/WebKit/mac/ChangeLog	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit/mac/ChangeLog	2015-04-23 08:22:13 UTC (rev 183181)
@@ -1,3 +1,17 @@
+2015-04-23  Andreas Kling  <[email protected]>
+
+        There should only be one way to get the system memory size.
+        <https://webkit.org/b/144081>
+
+        Reviewed by Antti Koivisto.
+
+        * Misc/WebKitSystemBits.h:
+        * Misc/WebKitSystemBits.m:
+        (WebMemorySize): Deleted.
+        * WebView/WebView.mm:
+        (+[WebView _setCacheModel:]):
+        (roundUpToPowerOf2): Deleted.
+
 2015-04-22  Darin Adler  <[email protected]>
 
         Remove OwnPtr and PassOwnPtr use from WebKit/cf, WebKit/mac, and WebKit2

Modified: trunk/Source/WebKit/mac/Misc/WebKitSystemBits.h (183180 => 183181)


--- trunk/Source/WebKit/mac/Misc/WebKitSystemBits.h	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit/mac/Misc/WebKitSystemBits.h	2015-04-23 08:22:13 UTC (rev 183181)
@@ -32,7 +32,6 @@
 extern "C" {
 #endif
 
-uint64_t WebMemorySize(void);
 unsigned long long WebVolumeFreeSize(NSString *path);
 int WebNumberOfCPUs(void);
 

Modified: trunk/Source/WebKit/mac/Misc/WebKitSystemBits.m (183180 => 183181)


--- trunk/Source/WebKit/mac/Misc/WebKitSystemBits.m	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit/mac/Misc/WebKitSystemBits.m	2015-04-23 08:22:13 UTC (rev 183181)
@@ -36,46 +36,6 @@
 #import <sys/types.h>
 #import <wtf/Assertions.h>
 
-#if !PLATFORM(IOS_SIMULATOR)
-
-static host_basic_info_data_t gHostBasicInfo;
-static pthread_once_t initControl = PTHREAD_ONCE_INIT;
-
-static void initCapabilities(void)
-{
-    mach_msg_type_number_t  count;
-    kern_return_t r;
-    mach_port_t host;
-
-    /* Discover our CPU type */
-    host = mach_host_self();
-    count = HOST_BASIC_INFO_COUNT;
-    r = host_info(host, HOST_BASIC_INFO, (host_info_t) &gHostBasicInfo, &count);
-    mach_port_deallocate(mach_task_self(), host);
-    if (r != KERN_SUCCESS) {
-        LOG_ERROR("%s : host_info(%d) : %s.\n", __FUNCTION__, r, mach_error_string(r));
-    }
-}
-
-#endif
-
-uint64_t WebMemorySize(void)
-{
-#if PLATFORM(IOS_SIMULATOR)
-    // Pretend we have 512MB of memory to make cache sizes behave like on device
-    return 512 * 1024 * 1024;
-#else
-    pthread_once(&initControl, initCapabilities);
-#if PLATFORM(IOS)
-    // On iOS, round up the memory size to a power of 2 because max_mem may not be exactly 256MB
-    // (for example) and we have code that depends on those boundaries.
-    return powf(2.0, ceilf(log2f(gHostBasicInfo.max_mem)));
-#else
-    return gHostBasicInfo.max_mem;
-#endif
-#endif
-}
-
 int WebNumberOfCPUs(void)
 {
     static int numCPUs = 0;

Modified: trunk/Source/WebKit/mac/WebView/WebView.mm (183180 => 183181)


--- trunk/Source/WebKit/mac/WebView/WebView.mm	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit/mac/WebView/WebView.mm	2015-04-23 08:22:13 UTC (rev 183181)
@@ -204,6 +204,7 @@
 #import <wtf/HashTraits.h>
 #import <wtf/MainThread.h>
 #import <wtf/ObjcRuntimeExtras.h>
+#import <wtf/RAMSize.h>
 #import <wtf/RefCountedLeakCounter.h>
 #import <wtf/RefPtr.h>
 #import <wtf/RunLoop.h>
@@ -7718,11 +7719,6 @@
 }
 #endif
 
-static inline uint64_t roundUpToPowerOf2(uint64_t num)
-{
-    return powf(2.0, ceilf(log2f(num)));
-}
-
 + (void)_setCacheModel:(WebCacheModel)cacheModel
 {
     if (s_didSetCacheModel && cacheModel == s_cacheModel)
@@ -7732,7 +7728,7 @@
     if (!nsurlCacheDirectory)
         nsurlCacheDirectory = NSHomeDirectory();
 
-    static uint64_t memSize = roundUpToPowerOf2(WebMemorySize() / 1024 / 1024);
+    static uint64_t memSize = ramSize() / 1024 / 1024;
     unsigned long long diskFreeSize = WebVolumeFreeSize(nsurlCacheDirectory) / 1024 / 1000;
     NSURLCache *nsurlCache = [NSURLCache sharedURLCache];
 

Modified: trunk/Source/WebKit/win/ChangeLog (183180 => 183181)


--- trunk/Source/WebKit/win/ChangeLog	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit/win/ChangeLog	2015-04-23 08:22:13 UTC (rev 183181)
@@ -1,3 +1,13 @@
+2015-04-23  Andreas Kling  <[email protected]>
+
+        There should only be one way to get the system memory size.
+        <https://webkit.org/b/144081>
+
+        Reviewed by Antti Koivisto.
+
+        * WebView.cpp:
+        (WebView::setCacheModel):
+
 2015-04-13  Jer Noble  <[email protected]>
 
         [Fullscreen] ChromeClient::exitVideoFullscreen() should take a pointer to a HTMLVideoElement.

Modified: trunk/Source/WebKit/win/WebView.cpp (183180 => 183181)


--- trunk/Source/WebKit/win/WebView.cpp	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit/win/WebView.cpp	2015-04-23 08:22:13 UTC (rev 183181)
@@ -155,6 +155,7 @@
 #include <WebCore/WindowsTouch.h>
 #include <bindings/ScriptValue.h>
 #include <wtf/MainThread.h>
+#include <wtf/RAMSize.h>
 
 #if USE(CG)
 #include <CoreGraphics/CGContext.h>
@@ -521,9 +522,10 @@
     long cacheDiskCapacity = 0;
 #endif
 
+    unsigned long long memSize = ramSize() / 1024 / 1024;
+
     // As a fudge factor, use 1000 instead of 1024, in case the reported byte 
     // count doesn't align exactly to a megabyte boundary.
-    unsigned long long memSize = WebMemorySize() / 1024 / 1000;
     unsigned long long diskFreeSize = WebVolumeFreeSize(cacheDirectory) / 1024 / 1000;
 
     unsigned cacheTotalCapacity = 0;

Modified: trunk/Source/WebKit2/ChangeLog (183180 => 183181)


--- trunk/Source/WebKit2/ChangeLog	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit2/ChangeLog	2015-04-23 08:22:13 UTC (rev 183181)
@@ -1,3 +1,17 @@
+2015-04-23  Andreas Kling  <[email protected]>
+
+        There should only be one way to get the system memory size.
+        <https://webkit.org/b/144081>
+
+        Reviewed by Antti Koivisto.
+
+        * NetworkProcess/cocoa/NetworkProcessCocoa.mm:
+        (WebKit::NetworkProcess::platformSetCacheModel):
+        (WebKit::memorySize): Deleted.
+        * WebProcess/cocoa/WebProcessCocoa.mm:
+        (WebKit::WebProcess::platformSetCacheModel):
+        (WebKit::memorySize): Deleted.
+
 2015-04-23  Chris Dumez  <[email protected]>
 
         [WK2] WebDiagnosticLoggingClient is leaking

Modified: trunk/Source/WebKit2/NetworkProcess/cocoa/NetworkProcessCocoa.mm (183180 => 183181)


--- trunk/Source/WebKit2/NetworkProcess/cocoa/NetworkProcessCocoa.mm	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit2/NetworkProcess/cocoa/NetworkProcessCocoa.mm	2015-04-23 08:22:13 UTC (rev 183181)
@@ -33,9 +33,7 @@
 #import "NetworkResourceLoader.h"
 #import "SandboxExtension.h"
 #import <WebCore/CFNetworkSPI.h>
-#import <mach/host_info.h>
-#import <mach/mach.h>
-#import <mach/mach_error.h>
+#import <wtf/RAMSize.h>
 
 namespace WebKit {
 
@@ -94,24 +92,6 @@
     _CFURLCacheSetMinSizeForVMCachedResource(cache.get(), NetworkResourceLoader::fileBackedResourceMinimumSize());
 }
 
-static uint64_t memorySize()
-{
-    static host_basic_info_data_t hostInfo;
-
-    static dispatch_once_t once;
-    dispatch_once(&once, ^() {
-        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)
-            LOG_ERROR("%s : host_info(%d) : %s.\n", __FUNCTION__, r, mach_error_string(r));
-    });
-
-    return hostInfo.max_mem;
-}
-
 static uint64_t volumeFreeSize(const String& path)
 {
     NSDictionary *fileSystemAttributesDictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:(NSString *)path error:NULL];
@@ -120,9 +100,10 @@
 
 void NetworkProcess::platformSetCacheModel(CacheModel cacheModel)
 {
+    uint64_t memSize = ramSize() / 1024 / 1024;
+
     // As a fudge factor, use 1000 instead of 1024, in case the reported byte
     // count doesn't align exactly to a megabyte boundary.
-    uint64_t memSize = memorySize() / 1024 / 1000;
     uint64_t diskFreeSize = volumeFreeSize(m_diskCacheDirectory) / 1024 / 1000;
 
     unsigned cacheTotalCapacity = 0;

Modified: trunk/Source/WebKit2/WebProcess/cocoa/WebProcessCocoa.mm (183180 => 183181)


--- trunk/Source/WebKit2/WebProcess/cocoa/WebProcessCocoa.mm	2015-04-23 08:15:04 UTC (rev 183180)
+++ trunk/Source/WebKit2/WebProcess/cocoa/WebProcessCocoa.mm	2015-04-23 08:22:13 UTC (rev 183181)
@@ -53,11 +53,9 @@
 #import <WebKitSystemInterface.h>
 #import <algorithm>
 #import <dispatch/dispatch.h>
-#import <mach/host_info.h>
-#import <mach/mach.h>
-#import <mach/mach_error.h>
 #import <objc/runtime.h>
 #import <stdio.h>
+#import <wtf/RamSize.h>
 
 #define ENABLE_MANUAL_WEBPROCESS_SANDBOXING !PLATFORM(IOS)
 
@@ -65,24 +63,6 @@
 
 namespace WebKit {
 
-static uint64_t memorySize()
-{
-    static host_basic_info_data_t hostInfo;
-
-    static dispatch_once_t once;
-    dispatch_once(&once, ^() {
-        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)
-            LOG_ERROR("%s : host_info(%d) : %s.\n", __FUNCTION__, r, mach_error_string(r));
-    });
-
-    return hostInfo.max_mem;
-}
-
 static uint64_t volumeFreeSize(NSString *path)
 {
     NSDictionary *fileSystemAttributesDictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:path error:NULL];
@@ -95,9 +75,10 @@
     if (!nsurlCacheDirectory)
         nsurlCacheDirectory = NSHomeDirectory();
 
+    uint64_t memSize = ramSize() / 1024 / 1024;
+
     // As a fudge factor, use 1000 instead of 1024, in case the reported byte 
     // count doesn't align exactly to a megabyte boundary.
-    uint64_t memSize = memorySize() / 1024 / 1000;
     uint64_t diskFreeSize = volumeFreeSize(nsurlCacheDirectory.get()) / 1024 / 1000;
 
     unsigned cacheTotalCapacity = 0;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to