Truly independent web browser
LibWeb+LibWebView+WebContent: Report leaked documents
Shannon Boothba154740e9d5
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cppindex 0f877510ed7..fa4464310f7 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp@@ -1776,15 +1776,20 @@ void EventLoop::add_stats(JsonObject& values) constsize_t shared_image_requests = 0;size_t style_sheets = 0;size_t font_faces = 0;+ size_t destroyed_documents = 0;for (auto const& document : m_documents) {if (!document)continue;+ // NB: Unless script still holds its objects, a destroyed document that survives a collection is leaked.+ if (document->has_been_destroyed())+ ++destroyed_documents;available_images += document->list_of_available_images().size();shared_image_requests += document->shared_resource_requests().size();style_sheets += document->style_scope().style_sheets().size();font_faces += document->fonts()->set_size();}values.set("dom.documents"sv, m_documents.size());+ values.set("dom.destroyed_documents"sv, destroyed_documents);values.set("images.available_entries"sv, available_images);values.set("images.shared_requests"sv, shared_image_requests);values.set("css.document_style_sheet_entries"sv, style_sheets);diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex acb191be49e..b025eceeab6 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -2929,6 +2929,7 @@ void Application::initialize_actions()m_debug_menu->add_action(*m_show_caret_hit_test_debug_overlay_action);m_debug_menu->add_separator();+ m_debug_menu->add_action(Action::create("Report Leaked Documents"sv, ActionID::ReportLeakedDocuments, debug_request("report-leaked-documents"sv)));m_debug_menu->add_action(Action::create("Collect Garbage"sv, ActionID::CollectGarbage, debug_request("collect-garbage"sv)));m_debug_menu->add_action(Action::create("Crash Current Page"sv, ActionID::CrashCurrentPage, debug_request("crash-current-page"sv)));m_debug_menu->add_action(Action::create("Crash Compositor Process"sv, ActionID::CrashCompositorProcess, [this]() { crash_compositor_process(); }));diff --git a/Libraries/LibWebView/Menu.h b/Libraries/LibWebView/Menu.hindex e6ad6d41ae2..8988c492674 100644--- a/Libraries/LibWebView/Menu.h+++ b/Libraries/LibWebView/Menu.h@@ -125,6 +125,7 @@ enum class ActionID {DumpLocalStorage,DumpSessionStorage,DumpGCGraph,+ ReportLeakedDocuments,ShowLineBoxBorders,ShowCaretHitTestDebugOverlay,CollectGarbage,diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cppindex 3549a53da08..7b06a6d9d55 100644--- a/Services/WebContent/ConnectionFromClient.cpp+++ b/Services/WebContent/ConnectionFromClient.cpp@@ -21,6 +21,7 @@#include <LibCompositing/FontServiceClient.h>#include <LibCore/EventLoop.h>#include <LibCore/Process.h>+#include <LibCore/StatsLog.h>#include <LibDevTools/IndexedDBSerialization.h>#include <LibGC/Heap.h>#include <LibGfx/Bitmap.h>@@ -1039,6 +1040,71 @@ void ConnectionFromClient::enqueue_input_event(Web::QueuedInputEvent event)Web::HTML::main_thread_event_loop().queue_task_to_process_input_events();}+// Prints the shortest path from a root to each destroyed document that is still reachable, grouped by the classes along+// it. Unless script still holds objects of the document, as of a removed iframe, whatever keeps it alive leaks it.+static void report_leaked_documents()+{+ static constexpr size_t max_documents = 1000;+ auto paths = Web::Bindings::main_thread_vm().heap().find_retaining_paths([](GC::Cell& cell) {+ // NB: Most cells are not C++ objects, so check the class their heap block records before casting.+ if (!GC::class_name_of(cell).is_one_of("Document"sv, "HTMLDocument"sv, "XMLDocument"sv))+ return false;+ return static_cast<Web::DOM::Document&>(cell).has_been_destroyed();+ },+ max_documents);++ HashMap<String, size_t> counts;+ Vector<String> order;+ for (auto const& path : paths) {+ StringBuilder builder;+ builder.append(GC::Heap::describe_root(path.root));+ for (size_t i = 0; i < path.steps.size(); ++i) {+ auto const& step = path.steps[i];+ if (i == 0) {+ builder.append(": "sv);+ } else {+ switch (step.edge_kind) {+ case GC::Heap::RetainingEdgeKind::Precise:+ builder.append(" -> "sv);+ break;+ case GC::Heap::RetainingEdgeKind::ConservativeWord:+ builder.append(" ~word~> "sv);+ break;+ case GC::Heap::RetainingEdgeKind::ConservativeHalf:+ builder.append(" ~half~> "sv);+ break;+ }+ }+ builder.append(step.class_name);+ }+ auto signature = builder.to_string_without_validation();+ auto& count = counts.ensure(signature, [&] -> size_t {+ order.append(signature);+ return 0;+ });+ ++count;+ }+ quick_sort(order, [&](auto const& a, auto const& b) { return counts.get(a).value() > counts.get(b).value(); });++ dbgln("Leaked documents: {} destroyed documents are reachable{}", paths.size(), paths.size() >= max_documents ? " (or more)"sv : ""sv);+ JsonArray groups;+ for (auto const& signature : order) {+ auto count = counts.get(signature).value();+ dbgln(" {}x {}", count, signature);+ JsonObject group;+ group.set("count"sv, count);+ group.set("path"sv, signature);+ groups.must_append(move(group));+ }++ if (auto* log = Core::StatsLog::the()) {+ JsonObject data;+ data.set("documents"sv, paths.size());+ data.set("paths"sv, move(groups));+ log->event("leaked_documents"sv, move(data));+ }+}+void ConnectionFromClient::debug_request(Web::PageId page_id, ByteString request, ByteString argument){auto page = this->page(page_id);@@ -1150,6 +1216,14 @@ void ConnectionFromClient::debug_request(Web::PageId page_id, ByteString requestreturn;}+ if (request == "report-leaked-documents") {+ // NOTE: We use deferred_invoke here so that as little as possible on the stack becomes a root.+ Core::deferred_invoke([] {+ report_leaked_documents();+ });+ return;+ }+if (request == "collect-garbage") {// NOTE: We use deferred_invoke here to ensure that GC runs with as little on the stack as possible.Core::deferred_invoke([] {