There should only be one way to get the system memory size.
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 23 Apr 2015 08:22:13 +0000 (08:22 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 23 Apr 2015 08:22:13 +0000 (08:22 +0000)
<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):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@183181 268f45cc-cd09-0410-ab3c-d52691b4dbfc

15 files changed:
Source/WTF/ChangeLog
Source/WTF/wtf/RAMSize.cpp
Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm
Source/WebCore/platform/ios/LegacyTileCache.mm
Source/WebCore/platform/ios/SystemMemory.h
Source/WebCore/platform/ios/SystemMemoryIOS.cpp
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/Misc/WebKitSystemBits.h
Source/WebKit/mac/Misc/WebKitSystemBits.m
Source/WebKit/mac/WebView/WebView.mm
Source/WebKit/win/ChangeLog
Source/WebKit/win/WebView.cpp
Source/WebKit2/ChangeLog
Source/WebKit2/NetworkProcess/cocoa/NetworkProcessCocoa.mm
Source/WebKit2/WebProcess/cocoa/WebProcessCocoa.mm

index 0d03e87..9695426 100644 (file)
@@ -1,3 +1,24 @@
+2015-04-23  Andreas Kling  <akling@apple.com>
+
+        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  <darin@apple.com>
 
         Eliminate remaining uses of OwnPtr and PassOwnPtr in WebCore outside the editing and platform directories
index a6d1f64..deb69e1 100644 (file)
 #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)
 
 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 @@ static size_t computeRAMSize()
 
 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;
 }
 
index 3633f0d..0444271 100644 (file)
@@ -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 @@ void MediaSessionManageriOS::resetRestrictions()
     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);
     }
 
index 1ae7497..221f3f9 100644 (file)
@@ -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 @@ unsigned LegacyTileCache::tileCapacityForGrid(LegacyTileGrid* grid)
 {
     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)
index 357cc31..9476796 100644 (file)
@@ -33,7 +33,6 @@
 
 namespace WebCore {
 WEBCORE_EXPORT int systemMemoryLevel(); // In [0, 100]
-WEBCORE_EXPORT size_t systemTotalMemory();
 }
 
 #endif
index 7afb77e..d888cd2 100644 (file)
@@ -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 @@ int systemMemoryLevel()
 #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
index cc60162..8e6b96f 100644 (file)
@@ -1,3 +1,17 @@
+2015-04-23  Andreas Kling  <akling@apple.com>
+
+        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  <darin@apple.com>
 
         Remove OwnPtr and PassOwnPtr use from WebKit/cf, WebKit/mac, and WebKit2
index e2f9e27..b770d0e 100644 (file)
@@ -32,7 +32,6 @@
 extern "C" {
 #endif
 
-uint64_t WebMemorySize(void);
 unsigned long long WebVolumeFreeSize(NSString *path);
 int WebNumberOfCPUs(void);
 
index c88974c..6ee93c0 100644 (file)
 #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;
index 0386464..fec619f 100644 (file)
 #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 @@ static WebFrameView *containingFrameView(NSView *view)
 }
 #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 @@ static inline uint64_t roundUpToPowerOf2(uint64_t num)
     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];
 
index 685ffbf..d16aed1 100644 (file)
@@ -1,3 +1,13 @@
+2015-04-23  Andreas Kling  <akling@apple.com>
+
+        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  <jer.noble@apple.com>
 
         [Fullscreen] ChromeClient::exitVideoFullscreen() should take a pointer to a HTMLVideoElement.
index 7dacb47..e015734 100644 (file)
 #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 @@ void WebView::setCacheModel(WebCacheModel cacheModel)
     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;
index 780afc1..c6a14af 100644 (file)
@@ -1,3 +1,17 @@
+2015-04-23  Andreas Kling  <akling@apple.com>
+
+        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  <cdumez@apple.com>
 
         [WK2] WebDiagnosticLoggingClient is leaking
index 4b3ffb8..672e33a 100644 (file)
@@ -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 @@ void NetworkProcess::platformInitializeNetworkProcessCocoa(const NetworkProcessC
     _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 @@ static uint64_t volumeFreeSize(const String& path)
 
 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;
index a65fbf9..36d530f 100644 (file)
 #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 @@ using namespace WebCore;
 
 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 @@ void WebProcess::platformSetCacheModel(CacheModel cacheModel)
     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;