Title: [275428] trunk/Source/WebCore
Revision
275428
Author
[email protected]
Date
2021-04-02 11:40:03 -0700 (Fri, 02 Apr 2021)

Log Message

Delete JS code and trigger garbage collection in worker threads on memory pressure
https://bugs.webkit.org/show_bug.cgi?id=224110

Reviewed by Geoffrey Garen.

Delete JS code and trigger garbage collection in worker threads on memory pressure,
to release as much memory as possible. We were previously only doing this in the
common (main thread) VM.

For the garbage collection logic, I tried to match what the GCController is doing
for the commonVM but apply it to worker VMs.

* page/MemoryRelease.cpp:
(WebCore::releaseCriticalMemory):
* workers/WorkerGlobalScope.cpp:
(WebCore::allWorkerGlobalScopeIdentifiers):
(WebCore::WorkerGlobalScope::WorkerGlobalScope):
(WebCore::WorkerGlobalScope::~WorkerGlobalScope):
(WebCore::WorkerGlobalScope::releaseMemory):
(WebCore::WorkerGlobalScope::releaseMemoryInWorkers):
* workers/WorkerGlobalScope.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (275427 => 275428)


--- trunk/Source/WebCore/ChangeLog	2021-04-02 18:35:21 UTC (rev 275427)
+++ trunk/Source/WebCore/ChangeLog	2021-04-02 18:40:03 UTC (rev 275428)
@@ -1,3 +1,27 @@
+2021-04-02  Chris Dumez  <[email protected]>
+
+        Delete JS code and trigger garbage collection in worker threads on memory pressure
+        https://bugs.webkit.org/show_bug.cgi?id=224110
+
+        Reviewed by Geoffrey Garen.
+
+        Delete JS code and trigger garbage collection in worker threads on memory pressure,
+        to release as much memory as possible. We were previously only doing this in the
+        common (main thread) VM.
+
+        For the garbage collection logic, I tried to match what the GCController is doing
+        for the commonVM but apply it to worker VMs.
+
+        * page/MemoryRelease.cpp:
+        (WebCore::releaseCriticalMemory):
+        * workers/WorkerGlobalScope.cpp:
+        (WebCore::allWorkerGlobalScopeIdentifiers):
+        (WebCore::WorkerGlobalScope::WorkerGlobalScope):
+        (WebCore::WorkerGlobalScope::~WorkerGlobalScope):
+        (WebCore::WorkerGlobalScope::releaseMemory):
+        (WebCore::WorkerGlobalScope::releaseMemoryInWorkers):
+        * workers/WorkerGlobalScope.h:
+
 2021-04-02  Aditya Keerthi  <[email protected]>
 
         Do not paint native decorations for search fields without '-webkit-appearance: searchfield'

Modified: trunk/Source/WebCore/page/MemoryRelease.cpp (275427 => 275428)


--- trunk/Source/WebCore/page/MemoryRelease.cpp	2021-04-02 18:35:21 UTC (rev 275427)
+++ trunk/Source/WebCore/page/MemoryRelease.cpp	2021-04-02 18:40:03 UTC (rev 275428)
@@ -50,6 +50,7 @@
 #include "StyleScope.h"
 #include "StyledElement.h"
 #include "TextPainter.h"
+#include "WorkerGlobalScope.h"
 #include "WorkerThread.h"
 #include <_javascript_Core/VM.h>
 #include <wtf/FastMalloc.h>
@@ -129,6 +130,8 @@
         GCController::singleton().garbageCollectSoon();
 #endif
     }
+
+    WorkerGlobalScope::releaseMemoryInWorkers(synchronous);
 }
 
 void releaseMemory(Critical critical, Synchronous synchronous, MaintainBackForwardCache maintainBackForwardCache, MaintainMemoryCache maintainMemoryCache)

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.cpp (275427 => 275428)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2021-04-02 18:35:21 UTC (rev 275427)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2021-04-02 18:40:03 UTC (rev 275428)
@@ -59,6 +59,16 @@
 namespace WebCore {
 using namespace Inspector;
 
+static Lock allWorkerGlobalScopeIdentifiersLock;
+static HashSet<ScriptExecutionContextIdentifier>& allWorkerGlobalScopeIdentifiers(LockHolder& holder)
+{
+    UNUSED_PARAM(holder);
+
+    static NeverDestroyed<HashSet<ScriptExecutionContextIdentifier>> identifiers;
+    ASSERT(allWorkerGlobalScopeIdentifiersLock.isLocked());
+    return identifiers;
+}
+
 WTF_MAKE_ISO_ALLOCATED_IMPL(WorkerGlobalScope);
 
 WorkerGlobalScope::WorkerGlobalScope(WorkerThreadType type, const WorkerParameters& params, Ref<SecurityOrigin>&& origin, WorkerThread& thread, Ref<SecurityOrigin>&& topOrigin, IDBClient::IDBConnectionProxy* connectionProxy, SocketProvider* socketProvider)
@@ -77,6 +87,11 @@
     , m_workerType(params.workerType)
     , m_credentials(params.credentials)
 {
+    {
+        auto locker = holdLock(allWorkerGlobalScopeIdentifiersLock);
+        allWorkerGlobalScopeIdentifiers(locker).add(contextIdentifier());
+    }
+
     if (m_topOrigin->hasUniversalAccess())
         origin->grantUniversalAccess();
     if (m_topOrigin->needsStorageAccessFromFileURLsQuirk())
@@ -92,6 +107,11 @@
     // We need to remove from the contexts map very early in the destructor so that calling postTask() on this WorkerGlobalScope from another thread is safe.
     removeFromContextsMap();
 
+    {
+        auto locker = holdLock(allWorkerGlobalScopeIdentifiersLock);
+        allWorkerGlobalScopeIdentifiers(locker).remove(contextIdentifier());
+    }
+
     if (m_cssFontSelector)
         m_cssFontSelector->stopLoadingAndClearFonts();
     m_performance = nullptr;
@@ -506,4 +526,41 @@
     return *static_cast<WorkerThread*>(workerOrWorkletThread());
 }
 
+void WorkerGlobalScope::releaseMemory(Synchronous synchronous)
+{
+    ASSERT(isContextThread());
+
+    JSC::JSLockHolder lock(vm());
+    vm().deleteAllCode(JSC::DeleteAllCodeIfNotCollecting);
+
+    if (synchronous == Synchronous::Yes) {
+        if (!vm().heap.isCurrentThreadBusy()) {
+            vm().heap.collectNow(JSC::Sync, JSC::CollectionScope::Full);
+            WTF::releaseFastMallocFreeMemory();
+            return;
+        }
+    }
+#if PLATFORM(IOS_FAMILY)
+    if (!vm().heap.isCurrentThreadBusy()) {
+        vm().heap.collectNowFullIfNotDoneRecently(JSC::Async);
+        return;
+    }
+#endif
+#if USE(CF) || USE(GLIB)
+    vm().heap.reportAbandonedObjectGraph();
+#else
+    vm().heap.collectNow(JSC::Async, JSC::CollectionScope::Full);
+#endif
+}
+
+void WorkerGlobalScope::releaseMemoryInWorkers(Synchronous synchronous)
+{
+    auto locker = holdLock(allWorkerGlobalScopeIdentifiersLock);
+    for (auto& globalScopeIdentifier : allWorkerGlobalScopeIdentifiers(locker)) {
+        postTaskTo(globalScopeIdentifier, [synchronous](auto& context) {
+            downcast<WorkerGlobalScope>(context).releaseMemory(synchronous);
+        });
+    }
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.h (275427 => 275428)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.h	2021-04-02 18:35:21 UTC (rev 275427)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.h	2021-04-02 18:40:03 UTC (rev 275428)
@@ -39,6 +39,7 @@
 #include "WorkerThread.h"
 #include <_javascript_Core/ConsoleMessage.h>
 #include <memory>
+#include <wtf/MemoryPressureHandler.h>
 
 namespace WebCore {
 
@@ -126,6 +127,9 @@
 
     FetchOptions::Credentials credentials() const { return m_credentials; }
 
+    void releaseMemory(Synchronous);
+    static void releaseMemoryInWorkers(Synchronous);
+
 protected:
     WorkerGlobalScope(WorkerThreadType, const WorkerParameters&, Ref<SecurityOrigin>&&, WorkerThread&, Ref<SecurityOrigin>&& topOrigin, IDBClient::IDBConnectionProxy*, SocketProvider*);
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to