[iOS WebKit2] Some memory pressure relief tweaks.
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 10 Jul 2014 20:24:12 +0000 (20:24 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 10 Jul 2014 20:24:12 +0000 (20:24 +0000)
<https://webkit.org/b/134811>

Split memory pressure relief into critical and non-critical sections.
Non-critical relief is for clearing out things that are really not
essential, e.g unused font data, text measurement caches, etc.

On iOS, only flip the "WebKit is under memory pressure" flag when we
are under *critical* memroy pressure, rather than doing it early on
and gimping ourselves because other processes are too big.

Also added logging for when we transition in/out of system pressure.

Reviewed by Geoffrey Garen.

* platform/MemoryPressureHandler.cpp:
(WebCore::MemoryPressureHandler::releaseNoncriticalMemory):
(WebCore::MemoryPressureHandler::releaseCriticalMemory):
(WebCore::MemoryPressureHandler::releaseMemory):
* platform/MemoryPressureHandler.h:
(WebCore::MemoryPressureHandler::ReliefLogger::loggingEnabled):
* platform/cocoa/MemoryPressureHandlerCocoa.mm:
(WebCore::MemoryPressureHandler::install):

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

Source/WebCore/ChangeLog
Source/WebCore/platform/MemoryPressureHandler.cpp
Source/WebCore/platform/MemoryPressureHandler.h
Source/WebCore/platform/cocoa/MemoryPressureHandlerCocoa.mm

index 5c3ab16..7dc01e2 100644 (file)
@@ -1,3 +1,29 @@
+2014-07-10  Andreas Kling  <akling@apple.com>
+
+        [iOS WebKit2] Some memory pressure relief tweaks.
+        <https://webkit.org/b/134811>
+
+        Split memory pressure relief into critical and non-critical sections.
+        Non-critical relief is for clearing out things that are really not
+        essential, e.g unused font data, text measurement caches, etc.
+
+        On iOS, only flip the "WebKit is under memory pressure" flag when we
+        are under *critical* memroy pressure, rather than doing it early on
+        and gimping ourselves because other processes are too big.
+
+        Also added logging for when we transition in/out of system pressure.
+
+        Reviewed by Geoffrey Garen.
+
+        * platform/MemoryPressureHandler.cpp:
+        (WebCore::MemoryPressureHandler::releaseNoncriticalMemory):
+        (WebCore::MemoryPressureHandler::releaseCriticalMemory):
+        (WebCore::MemoryPressureHandler::releaseMemory):
+        * platform/MemoryPressureHandler.h:
+        (WebCore::MemoryPressureHandler::ReliefLogger::loggingEnabled):
+        * platform/cocoa/MemoryPressureHandlerCocoa.mm:
+        (WebCore::MemoryPressureHandler::install):
+
 2014-07-10  Beth Dakin  <bdakin@apple.com>
 
         Need Setting/WKPreference that allows clients to prevent scrollbars from drawing 
index d5202db..cd7575e 100644 (file)
@@ -68,7 +68,31 @@ MemoryPressureHandler::MemoryPressureHandler()
 {
 }
 
-void MemoryPressureHandler::releaseMemory(bool critical)
+void MemoryPressureHandler::releaseNoncriticalMemory()
+{
+    {
+        ReliefLogger log("Purge inactive FontData");
+        fontCache().purgeInactiveFontData();
+    }
+
+    {
+        ReliefLogger log("Clear WidthCaches");
+        clearWidthCaches();
+    }
+
+    {
+        ReliefLogger log("Discard Selector Query Cache");
+        for (auto* document : Document::allDocuments())
+            document->clearSelectorQueryCache();
+    }
+
+    {
+        ReliefLogger log("Clearing JS string cache");
+        JSDOMWindow::commonVM().stringCache.clear();
+    }
+}
+
+void MemoryPressureHandler::releaseCriticalMemory()
 {
     {
         ReliefLogger log("Empty the PageCache");
@@ -78,11 +102,6 @@ void MemoryPressureHandler::releaseMemory(bool critical)
     }
 
     {
-        ReliefLogger log("Purge inactive FontData");
-        fontCache().purgeInactiveFontData();
-    }
-
-    {
         ReliefLogger log("Prune MemoryCache");
         memoryCache()->pruneToPercentage(0);
     }
@@ -93,31 +112,23 @@ void MemoryPressureHandler::releaseMemory(bool critical)
     }
 
     {
-        ReliefLogger log("Clear WidthCaches");
-        clearWidthCaches();
-    }
-
-    {
         ReliefLogger log("Discard StyleResolvers");
         for (auto* document : Document::allDocuments())
             document->clearStyleResolver();
     }
 
     {
-        ReliefLogger log("Discard Selector Query Cache");
-        for (auto* document : Document::allDocuments())
-            document->clearSelectorQueryCache();
-    }
-
-    {
         ReliefLogger log("Discard all JIT-compiled code");
         gcController().discardAllCompiledCode();
     }
+}
 
-    {
-        ReliefLogger log("Clearing JS string cache");
-        JSDOMWindow::commonVM().stringCache.clear();
-    }
+void MemoryPressureHandler::releaseMemory(bool critical)
+{
+    releaseNoncriticalMemory();
+
+    if (critical)
+        releaseCriticalMemory();
 
     platformReleaseMemory(critical);
 
index 83c812f..276d95b 100644 (file)
@@ -87,6 +87,7 @@ public:
 
         const char* logString() const { return m_logString; }
         static void setLoggingEnabled(bool enabled) { s_loggingEnabled = enabled; }
+        static bool loggingEnabled() { return s_loggingEnabled; }
 
     private:
         size_t platformMemoryUsage();
@@ -101,6 +102,9 @@ public:
     static void releaseMemory(bool critical);
 
 private:
+    static void releaseNoncriticalMemory();
+    static void releaseCriticalMemory();
+
     void uninstall();
 
     void holdOff(unsigned);
index e7ddd35..a733eaa 100644 (file)
@@ -96,11 +96,16 @@ void MemoryPressureHandler::install()
 #if PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000
                 unsigned long status = dispatch_source_get_data(_cache_event_source);
                 critical = status == DISPATCH_MEMORYPRESSURE_CRITICAL;
+                bool wasCritical = memoryPressureHandler().isUnderMemoryPressure();
+                memoryPressureHandler().setUnderMemoryPressure(critical);
                 if (status == DISPATCH_MEMORYSTATUS_PRESSURE_NORMAL) {
-                    memoryPressureHandler().setUnderMemoryPressure(false);
+                    if (ReliefLogger::loggingEnabled())
+                        NSLog(@"System is no longer under (%s) memory pressure.", wasCritical ? "critical" : "non-critical");
                     return;
                 }
-                memoryPressureHandler().setUnderMemoryPressure(true);
+
+                if (ReliefLogger::loggingEnabled())
+                    NSLog(@"Got memory pressure notification (%s)", critical ? "critical" : "non-critical");
 #endif
                 memoryPressureHandler().respondToMemoryPressure(critical);
             });