Skip to content

Commit

Permalink
Add support for dumping GC heap snapshots of Workers
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=269853
rdar://123390121

Reviewed by Simon Fraser.

This will help us understand memory usuage for Workers.

* Source/WebCore/bindings/js/GCController.cpp:
(WebCore::GCController::dumpHeapForVM):
(WebCore::GCController::dumpHeap):
* Source/WebCore/bindings/js/GCController.h:
* Source/WebCore/workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::dumpGCHeapForWorkers):
* Source/WebCore/workers/WorkerGlobalScope.h:

Canonical link: https://commits.webkit.org/275242@main
  • Loading branch information
szewai committed Feb 23, 2024
1 parent d11c7e8 commit 3fbaadf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Source/WebCore/bindings/js/GCController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "CommonVM.h"
#include "JSHTMLDocument.h"
#include "Location.h"
#include "WorkerGlobalScope.h"
#include <JavaScriptCore/Heap.h>
#include <JavaScriptCore/HeapSnapshotBuilder.h>
#include <JavaScriptCore/JSLock.h>
Expand Down Expand Up @@ -138,7 +139,7 @@ void GCController::deleteAllLinkedCode(DeleteAllCodeEffort effort)
commonVM().deleteAllLinkedCode(effort);
}

void GCController::dumpHeap()
void GCController::dumpHeapForVM(VM& vm)
{
FileSystem::PlatformFileHandle fileHandle;
String tempFilePath = FileSystem::openTemporaryFile("GCHeap"_s, fileHandle);
Expand All @@ -147,9 +148,7 @@ void GCController::dumpHeap()
return;
}

VM& vm = commonVM();
JSLockHolder lock(vm);

sanitizeStackForVM(vm);

String jsonData;
Expand All @@ -166,8 +165,13 @@ void GCController::dumpHeap()

FileSystem::writeToFile(fileHandle, utf8String.data(), utf8String.length());
FileSystem::closeFile(fileHandle);

WTFLogAlways("Dumped GC heap to %s", tempFilePath.utf8().data());
WTFLogAlways("Dumped GC heap to %s%s", tempFilePath.utf8().data(), isMainThread() ? ""_s : " for Worker");
}

void GCController::dumpHeap()
{
dumpHeapForVM(commonVM());
WorkerGlobalScope::dumpGCHeapForWorkers();
}

} // namespace WebCore
1 change: 1 addition & 0 deletions Source/WebCore/bindings/js/GCController.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GCController {
friend class WTF::NeverDestroyed<GCController>;
public:
WEBCORE_EXPORT static GCController& singleton();
WEBCORE_EXPORT static void dumpHeapForVM(JSC::VM&);

WEBCORE_EXPORT void garbageCollectSoon();
WEBCORE_EXPORT void garbageCollectNow(); // It's better to call garbageCollectSoon, unless you have a specific reason not to.
Expand Down
11 changes: 11 additions & 0 deletions Source/WebCore/workers/WorkerGlobalScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "DocumentInlines.h"
#include "FontCustomPlatformData.h"
#include "FontFaceSet.h"
#include "GCController.h"
#include "IDBConnectionProxy.h"
#include "ImageBitmapOptions.h"
#include "InspectorInstrumentation.h"
Expand Down Expand Up @@ -633,6 +634,16 @@ void WorkerGlobalScope::releaseMemoryInWorkers(Synchronous synchronous)
}
}

void WorkerGlobalScope::dumpGCHeapForWorkers()
{
Locker locker { allWorkerGlobalScopeIdentifiersLock };
for (auto& globalScopeIdentifier : allWorkerGlobalScopeIdentifiers()) {
postTaskTo(globalScopeIdentifier, [](auto& context) {
GCController::dumpHeapForVM(downcast<WorkerGlobalScope>(context).vm());
});
}
}

void WorkerGlobalScope::setMainScriptSourceProvider(ScriptBufferSourceProvider& provider)
{
ASSERT(!m_mainScriptSourceProvider);
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/workers/WorkerGlobalScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class WorkerGlobalScope : public Supplementable<WorkerGlobalScope>, public Base6

void releaseMemory(Synchronous);
static void releaseMemoryInWorkers(Synchronous);
static void dumpGCHeapForWorkers();

void setMainScriptSourceProvider(ScriptBufferSourceProvider&);
void addImportedScriptSourceProvider(const URL&, ScriptBufferSourceProvider&);
Expand Down

0 comments on commit 3fbaadf

Please sign in to comment.