Add strong root logging and $vm helpers for memory leak debugging
bf5c23d50f3cce166b4bf04b8b4a13c62c02e651
Add strong root logging and $vm helpers for memory leak debugging https://bugs.webkit.org/show_bug.cgi?id=277835 Reviewed by NOBODY (OOPS!). This patch adds some new options for debugging memory leaks. It also allows $vm to be enabled again on GTK developer builds. For example, we can debug a memory leak in the JSC heap by: 1) Run: ``` JSC_validateOptions=1 JSC_useDollarVM=1 JSC_logLiveStrongs=1 JSC_dumpHeapOnLowMemory=1 JSC_useSourceProviderCache=0 JSC_useCodeCache=0 WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1 Tools/Scripts/run-minibrowser --gtk --debug https://webkit.org ``` Alternatively, to track C allocations, disable system malloc in CMakeLists.txt (I recommend adding a #if !USE_SYSTEM_MALLOC #error #endif to confirmn), run: ``` JSC_useSourceProviderCache=0 JSC_useCodeCache=0 WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1 MALLOC=0 WEB_PROCESS_CMD_PREFIX="/usr/bin/valgrind --tool=massif " Tools/Scripts/run-minibrowser --gtk --debug https://webkit.org ``` Then, to release memory, dump the JSC heap, and log all remaining Strong<> references, run: ``` javascript:$vm.gc() javascript:$vm.triggerMemoryPressure() ``` You should get a gc snapshot file. To graph it (or just do dump it to a human-readable format), run: ``` Tools/Scripts/heap-snapshot-to-graphviz.js /tmp/GCHeapM8FVBp ~/ dot -Tsvg ~/GCDebugging.gv.txt -O ``` You can edit `heap-snapshot-to-graphviz.js` to print root paths, and there are many other graph-related things you can do in js. You can also open this file in WebInspector by adding a JavaScript allocations track, and clicking the second import button (not the main import button). If you see that your heap objects are kept alive by StrongReferences, then the strong logging may point to which C++ object is keeping them alive, and you can continue your investigation from there. As a general tip, you should ensure instrumentingAgents.inspectorEnvironment().developerExtrasEnabled() is false on your platform when diagnosing these bugs, since WebInspector can keep things alive unnecesarily. It may also be helpful to disable Process Swap on Naviagation. * Source/JavaScriptCore/heap/Heap.cpp: (JSC::Heap::addCoreConstraints): (JSC::StrongLoggingDisabledScope::StrongLoggingDisabledScope): (JSC::StrongLoggingDisabledScope::~StrongLoggingDisabledScope): (JSC::debugReportLiveStrong): (JSC::debugReportDeadStrong): (JSC::debugLogAllLiveStrongs): * Source/JavaScriptCore/heap/Heap.h: * Source/JavaScriptCore/heap/MarkedSpace.h: * Source/JavaScriptCore/heap/MarkedSpaceInlines.h: (JSC::MarkedSpace::forEachWeakInParallel): * Source/JavaScriptCore/heap/Strong.h: (JSC::Strong::Strong): (JSC::Strong::~Strong): * Source/JavaScriptCore/heap/StrongInlines.h: (JSC::shouldStrongDestructorGrabLock>::Strong): (JSC::shouldStrongDestructorGrabLock>::set): * Source/JavaScriptCore/runtime/OptionsList.h: * Source/JavaScriptCore/runtime/VM.cpp: (JSC::VM::VM): * Source/JavaScriptCore/tools/JSDollarVM.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): (JSC::JSDollarVM::finishCreation): * Source/WTF/wtf/MemoryPressureHandler.cpp: (WTF::MemoryPressureHandler::setShouldUsePeriodicMemoryMonitor): * Source/WebCore/bindings/js/GCController.h: * Source/WebInspectorUI/UserInterface/Workers/HeapSnapshot/HeapSnapshot.js: (HeapSnapshot): (HeapSnapshot.prototype.shortestGCRootPath): (HeapSnapshot.prototype.serializeEdge): (HeapSnapshot.prototype._determineGCRootPaths): * Source/WebKit/NetworkProcess/soup/NetworkProcessSoup.cpp: (WebKit::NetworkProcess::platformInitializeNetworkProcess): * Source/WebKit/WebProcess/WebProcess.cpp: (WebKit::WebProcess::initializeWebProcess): * Source/cmake/OptionsGTK.cmake: * Tools/Scripts/heap-snapshot-to-graphviz.js: Added. (true.console.log.string_appeared_here.escapeOutput): (return.unsafe.replace):
https://github.com/WebKit/WebKit/commit/3cc7b3cd3ebe21596a75a73eb5738df556114ca7
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/bf5c23d50f3cce166b4bf04b8b4a13c62c02e651)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/61aa9284ef102fefe1289d9b398769e38ccf73c4)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/49ef3fe062397a2a3a6448dddeb3a1c5c8fcda1b)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/15f6af372066a49abebcb94860355e3b1cfd512c)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/3cc7b3cd3ebe21596a75a73eb5738df556114ca7)
This patch adds some new options for debugging memory leaks
Can you define what kind of "memory leaks" you are referring to here? (1) C/C++ code memory leaks? (2) JS heap "memory leaks" due to JS code retaining memory longer than needed?
In my case, it appeared that a WebInspector log kept a js global object alive even after navigating away from the page. WPE doesn't enable PSON unfortunately, so bugs like these casue embedded devices to leak memory
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/529b547bc9c65cac74a4dd6847416b5c38b05042)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/ecbd20b20cca27f26d0586652cb5f649f46685cb)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/87b2c313c86826fdfa2c906f27e10bd8e41d9e9b)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/5c3a5a8bdd9cad896fe32f59776408ee87183e74)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/38738c71171ccc06f3ed97aed9eeea7e075272dd)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/4b2f9113ee5c67dbde5b45a3c57805b00131f5ee)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/83e7a41b4d7a33adf98e2a12f11a6d01f625451f)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/2f4c764e7ada3c2e85703c551442948e721a819c)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/a8ab55648ca9f476b007454461ee916d3457f051)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/317e61d901abaf8d921bcf56aef5ea6c62bf885c)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/a5c915838306279ff762335e84297652ee77b57c)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/d0549b04898b4b9e1ad67150d63bf8d4371ffc45)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/bfa0fc09056d81f72a446576151b34c978a0b5cf)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/9fbf0dd43c48c3f1d93a692b4e6b45bbbb29f2c6)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/90627255fd06c5195e5bedbb3d91b8f1dc3a9673)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/82226fca6244c4e6f3d6670da2adcfc1e063a4b7)
EWS run on previous version of this PR (hash https://github.com/WebKit/WebKit/commit/abc74689f44a60a49ad473db574794809ee11d74)
EWS run on current version of this PR (hash https://github.com/WebKit/WebKit/commit/55aa5bf44292a913eaea3eec38691d38b90493cf)
Committed 284791@main (41dc316f40f4): https://commits.webkit.org/284791@main
Reviewed commits have been landed. Closing PR #31924 and removing active labels.
๐ ios-sim