Skip to content

Truly independent web browser

LibGC+LibWeb: Add a heap census to internals

Andreas Klingf127eaf733dd
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cpp
index ee9a44b2a49..4756839644b 100644
--- a/Libraries/LibGC/Heap.cpp
+++ b/Libraries/LibGC/Heap.cpp
@@ -919,6 +919,112 @@ void Heap::dump_allocators()
dbgln("Total wasted on fragmentation: {} KiB", total_waste / KiB);
}
+// Counts the edges to cells that cells visit, in their own fields or in storage they own, without marking anything.
+class CensusVisitor final : public Cell::Visitor {
+public:
+ size_t cell_edges { 0 };
+
+ virtual void visit_possible_values(ReadonlyBytes) override { }
+
+private:
+ virtual void visit_impl(Cell&) override { ++cell_edges; }
+ virtual void visit_impl(ReadonlySpan<NanBoxedValue> values) override
+ {
+ for (auto const& value : values) {
+ if (value.is_cell())
+ ++cell_edges;
+ }
+ }
+};
+
+AK::JsonObject Heap::census()
+{
+ VERIFY(!m_collecting_garbage);
+
+ // NB: Unswept cells of the last collection are still in the Live state, and cells an allocator has taken out of
+ // its block's lazy region would count as dead ones, so finish the sweep and give those back first.
+ finish_pending_incremental_sweep();
+ give_back_local_free_lists();
+
+ size_t total_block_bytes = 0;
+ size_t total_live_bytes = 0;
+ AK::JsonArray allocators;
+ for (auto& allocator : m_all_cell_allocators) {
+ auto const& type_info = allocator.type_info();
+
+ size_t blocks = 0;
+ size_t cell_slots = 0;
+ size_t live_cells = 0;
+ size_t external_bytes = 0;
+ CensusVisitor visitor;
+ struct ClassCounts {
+ size_t live_cells { 0 };
+ size_t cell_edges { 0 };
+ };
+ HashMap<StringView, ClassCounts> counts_by_class;
+ allocator.for_each_block([&](HeapBlock& block) {
+ ++blocks;
+ cell_slots += block.cell_count();
+ block.for_each_cell([&](Cell* cell) {
+ if (cell->state() != Cell::State::Live)
+ return;
+ ++live_cells;
+ auto edges_before = visitor.cell_edges;
+ type_info.visit_edges(cell, &visitor);
+ if (type_info.external_memory_size)
+ external_bytes += type_info.external_memory_size(cell);
+ // A C++ allocator holds exactly one class, but cells defined in other languages can share an allocator
+ // with their subclasses.
+ if (type_info.class_name) {
+ size_t length = 0;
+ auto const* name = type_info.class_name(cell, &length);
+ auto& counts = counts_by_class.ensure(StringView { name, length }, [] { return ClassCounts {}; });
+ ++counts.live_cells;
+ counts.cell_edges += visitor.cell_edges - edges_before;
+ }
+ });
+ return IterationDecision::Continue;
+ });
+ if (blocks == 0)
+ continue;
+
+ auto block_bytes = blocks * HeapBlock::BLOCK_SIZE;
+ auto live_bytes = live_cells * allocator.cell_size();
+ total_block_bytes += block_bytes;
+ total_live_bytes += live_bytes;
+
+ AK::JsonObject entry;
+ entry.set("name"sv, allocator.class_name().value_or("generic"sv));
+ entry.set("cell_size"sv, allocator.cell_size());
+ entry.set("blocks"sv, blocks);
+ entry.set("block_bytes"sv, block_bytes);
+ entry.set("cell_slots"sv, cell_slots);
+ entry.set("live_cells"sv, live_cells);
+ entry.set("live_bytes"sv, live_bytes);
+ entry.set("free_bytes"sv, block_bytes - live_bytes);
+ entry.set("external_bytes"sv, external_bytes);
+ entry.set("cell_edges"sv, visitor.cell_edges);
+ if (!counts_by_class.is_empty()) {
+ AK::JsonObject classes;
+ for (auto& it : counts_by_class) {
+ AK::JsonObject counts;
+ counts.set("live_cells"sv, it.value.live_cells);
+ counts.set("cell_edges"sv, it.value.cell_edges);
+ classes.set(it.key, move(counts));
+ }
+ entry.set("classes"sv, move(classes));
+ }
+ allocators.must_append(move(entry));
+ }
+
+ AK::JsonObject census;
+ census.set("block_size"sv, HeapBlock::BLOCK_SIZE);
+ census.set("block_bytes"sv, total_block_bytes);
+ census.set("live_bytes"sv, total_live_bytes);
+ census.set("allocators"sv, move(allocators));
+ return census;
+}
+
void Heap::enqueue_post_gc_task(AK::Function<void()> task)
{
m_post_gc_tasks.append(move(task));
diff --git a/Libraries/LibGC/Heap.h b/Libraries/LibGC/Heap.h
index bcb54d74443..54804ef3bd2 100644
--- a/Libraries/LibGC/Heap.h
+++ b/Libraries/LibGC/Heap.h
@@ -122,6 +122,10 @@ public:
void enable_stats();
void add_stats(AK::JsonObject&) const;
+ // Per allocator: its blocks, its live cells and bytes, and per class the live cells and the edges to cells they
+ // visit. Finishes any pending sweep first.
+ AK::JsonObject census();
+
void register_sweep_callback(AK::Function<void()>);
void register_cell_allocator(Badge<CellAllocator>, CellAllocator&);
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index c04818021b5..7758699d4a5 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -1906,6 +1906,14 @@ Utf16String Internals::dump_gc_graph()
return dump_string_to_utf16(GC::Heap::the().dump_graph().serialized());
}
+Utf16String Internals::heap_census()
+{
+ // Collect first, so that the census counts what is reachable rather than what the last collection left behind.
+ auto& heap = window().principal_realm().vm().heap();
+ heap.collect_garbage();
+ return dump_string_to_utf16(heap.census().serialized());
+}
+
Utf16String Internals::dump_session_history()
{
auto& document = window().associated_document();
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index 9c56b7cec09..6ac94a65212 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -229,6 +229,7 @@ public:
Utf16String dump_stacking_context_tree();
Utf16String stacking_context_structure_verification_report();
Utf16String dump_gc_graph();
+ Utf16String heap_census();
Utf16String dump_session_history();
Utf16String dump_ui_process_session_history();
Utf16String dump_ui_process_session_history_without_update();
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 415cc5f5953..cefeedcb279 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -229,6 +229,7 @@ interface Internals {
DOMString dumpStackingContextTree();
DOMString stackingContextStructureVerificationReport();
DOMString dumpGCGraph();
+ DOMString heapCensus();
DOMString dumpSessionHistory();
DOMString dumpUIProcessSessionHistory();
DOMString dumpUIProcessSessionHistoryWithoutUpdate();
diff --git a/Tests/LibWeb/Crash/Internals/heap-census.html b/Tests/LibWeb/Crash/Internals/heap-census.html
new file mode 100644
index 00000000000..b87bc08f1da
--- /dev/null
+++ b/Tests/LibWeb/Crash/Internals/heap-census.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<script>
+ internals.heapCensus();
+</script>