Truly independent web browser
LibGC: Base the collection budget on allocation rate and GC cost
Andreas Kling95dc53c0ba3c
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cppindex 2d77b6d15cc..3097d263882 100644--- a/Libraries/LibGC/Heap.cpp+++ b/Libraries/LibGC/Heap.cpp@@ -49,12 +49,24 @@namespace GC {static constexpr size_t GC_MIN_BYTES_THRESHOLD { 32 * 1024 * 1024 };-static constexpr size_t GC_HEAP_GROWTH_FACTOR_NUMERATOR { 16 };-static constexpr size_t GC_HEAP_GROWTH_FACTOR_DENOMINATOR { 4 };++// The share of running time we want to spend collecting. A cycle costs its mark and sweep time and lasts as long as it+// takes to allocate its budget, so meeting this share takes a budget of allocation_rate * cost * (1 - u) / u.+static constexpr double GC_TARGET_TIME_FRACTION { 0.1 };++// Growth factors relative to live cell bytes. The minimum lets the heap triple between collections, which is where a+// page that allocates slowly ends up. A lower minimum would save more memory, but marking pauses the page for as long+// as it takes to mark everything alive, and it would do so more often while the page is busy.+static constexpr double GC_MIN_GROWTH_FACTOR { 2 };+static constexpr double GC_MAX_GROWTH_FACTOR { 8 };++// OPTIMIZATION: A program that allocates fast and keeps little alive gains from a budget far above the maximum factor,+// so the maximum never limits a budget below this.+static constexpr size_t GC_MAX_BUDGET_FLOOR_BYTES { 256 * 1024 * 1024 };// NB: Memory that cells own outside the heap (array buffer contents, decoded images) is mostly large, long-lived data,-// and allocating more cells does not grow it. Letting it grow the threshold by the cell factor makes a page with-// 1 GiB of such memory allocate ~4 GiB of cells between collections, so it only grows the threshold by a tenth.+// and allocating more cells does not grow it. Letting it grow the threshold by the growth factor makes a page with+// 1 GiB of such memory allocate gigabytes of cells between collections, so it only grows the threshold by a tenth.static constexpr size_t GC_EXTERNAL_GROWTH_FACTOR_NUMERATOR { 1 };static constexpr size_t GC_EXTERNAL_GROWTH_FACTOR_DENOMINATOR { 10 };@@ -305,6 +317,7 @@ Heap::Heap(AK::Function<void(HashMap<Cell*, GC::HeapRoot>&)> gather_embedder_rooif (become_process_default == BecomeProcessDefault::Yes)s_the = this;m_gc_bytes_threshold = GC_MIN_BYTES_THRESHOLD;+ m_last_collection_end_ns = MonotonicTime::now().nanoseconds();static_assert(HeapBlock::min_possible_cell_size <= 32, "Heap Cell tracking uses too much data!");}@@ -418,15 +431,35 @@ void Heap::did_free_external_memory(size_t size)m_allocated_bytes_since_last_gc -= size;}-void Heap::update_gc_bytes_threshold(size_t live_cell_bytes, size_t live_external_bytes)+double Heap::next_growth_factor(size_t live_cell_bytes)+{+ if (live_cell_bytes == 0)+ return GC_MIN_GROWTH_FACTOR;++ auto live = static_cast<double>(live_cell_bytes);+ auto cost_seconds = static_cast<double>(m_mark_duration_ns + m_sweep_duration_ns) / 1e9;+ auto wanted_budget = m_allocation_bytes_per_second * cost_seconds * (1 - GC_TARGET_TIME_FRACTION) / GC_TARGET_TIME_FRACTION;++ // NB: Allocation rates swing from one cycle to the next, so keep an average that the clamp below does not touch.+ m_wanted_growth_factor = m_wanted_growth_factor.has_value()+ ? (wanted_budget / live + m_wanted_growth_factor.value()) / 2+ : wanted_budget / live;++ auto max_growth_factor = max(GC_MAX_GROWTH_FACTOR, static_cast<double>(GC_MAX_BUDGET_FLOOR_BYTES) / live);+ return clamp(m_wanted_growth_factor.value(), GC_MIN_GROWTH_FACTOR, max_growth_factor);+}++void Heap::update_gc_bytes_threshold(size_t live_external_bytes){+ // NB: Only marking knows how much the last collection found alive. A sweep also sees the cells allocated into+ // blocks it has yet to reach, which are marked to keep them alive.+ auto live_cell_bytes = m_marked_cell_bytes;if (m_stats) {m_stats->live_cell_bytes = live_cell_bytes;m_stats->live_external_bytes = live_external_bytes;}- Checked<size_t> next_gc_bytes_threshold = live_cell_bytes;- next_gc_bytes_threshold *= GC_HEAP_GROWTH_FACTOR_NUMERATOR;- next_gc_bytes_threshold /= GC_HEAP_GROWTH_FACTOR_DENOMINATOR;+ Checked<size_t> next_gc_bytes_threshold = static_cast<size_t>(static_cast<double>(live_cell_bytes) * next_growth_factor(live_cell_bytes));+ m_sweep_duration_ns = 0;Checked<size_t> external_growth = live_external_bytes;external_growth *= GC_EXTERNAL_GROWTH_FACTOR_NUMERATOR;@@ -799,8 +832,9 @@ void Heap::run_collection(ReadonlySpan<FlatPtr> callee_saved_registers, CollectiVERIFY(!m_collecting_garbage);+ update_allocation_rate();finish_pending_incremental_sweep();- auto stats_start_ns = m_stats ? MonotonicTime::now().nanoseconds() : 0;+ auto mark_start_ns = MonotonicTime::now().nanoseconds();give_back_local_free_lists();g_next_incremental_sweep_should_report = false;@@ -841,10 +875,13 @@ void Heap::run_collection(ReadonlySpan<FlatPtr> callee_saved_registers, CollectiScopedPhaseTimer timer { report, g_phase_timings.mark_live_cells_us };mark_live_cells(roots);}+ } else {+ m_marked_cell_bytes = 0;}clear_weak_pointers_to_dead_cells(report);run_post_mark_phases(report);m_weak_pointers_to_dead_cells_are_cleared = false;+ m_mark_duration_ns = MonotonicTime::now().nanoseconds() - mark_start_ns;// For CollectEverything we must finish sweeping synchronously so that// every cell is collected before the Heap destructor returns. All@@ -877,8 +914,11 @@ void Heap::run_collection(ReadonlySpan<FlatPtr> callee_saved_registers, Collectielseg_next_incremental_sweep_should_report = false;+ m_last_collection_end_ns = MonotonicTime::now().nanoseconds();+ m_total_allocated_bytes_at_last_collection = m_total_allocated_bytes;+if (m_stats) {- m_stats->pause_duration_ns = MonotonicTime::now().nanoseconds() - stats_start_ns;+ m_stats->pause_duration_ns = m_last_collection_end_ns - mark_start_ns;if (!m_incremental_sweep_active)finish_stats_collection();}@@ -890,6 +930,13 @@ void Heap::run_collection(ReadonlySpan<FlatPtr> callee_saved_registers, Collectim_idle_collection_policy.reset(m_total_allocated_bytes);}+void Heap::update_allocation_rate()+{+ auto now_ns = MonotonicTime::now().nanoseconds();+ if (now_ns > m_last_collection_end_ns)+ m_allocation_bytes_per_second = static_cast<double>(m_total_allocated_bytes - m_total_allocated_bytes_at_last_collection) * 1e9 / static_cast<double>(now_ns - m_last_collection_end_ns);+}+void Heap::set_should_collect_on_every_allocation(bool should_collect){m_should_collect_on_every_allocation = should_collect;@@ -1437,6 +1484,8 @@ public:auto cell = m_work_queue.take_last();if (cell->state() != Cell::State::Live)continue;+ auto* block = HeapBlock::from_cell(cell.ptr());+ block->heap().m_marked_cell_bytes += block->cell_size();cell->type_info().visit_edges(cell.ptr(), this);}@@ -1485,6 +1534,9 @@ void Heap::mark_live_cells_across(ReadonlySpan<Heap* const> heaps, HashMap<Cell*}}+ for (auto* heap : heaps)+ heap->m_marked_cell_bytes = 0;+Optional<MarkingVisitor> visitor;{ScopedPhaseTimer timer { g_recording_phase_timings, g_phase_timings.mark_initial_visit_us };@@ -1530,6 +1582,7 @@ void Heap::sweep_weak_blocks()void Heap::sweep_dead_cells(bool print_report, Core::ElapsedTimer const& measurement_timer){dbgln_if(HEAP_DEBUG, "sweep_dead_cells:");+ auto sweep_start_ns = MonotonicTime::now().nanoseconds();Vector<HeapBlock*, 32> empty_blocks;Vector<HeapBlock*, 32> full_blocks_that_became_usable;@@ -1594,7 +1647,8 @@ void Heap::sweep_dead_cells(bool print_report, Core::ElapsedTimer const& measure{ScopedPhaseTimer timer { g_recording_phase_timings, g_phase_timings.sweep_update_threshold_us };- update_gc_bytes_threshold(live_cell_bytes, live_external_bytes);+ m_sweep_duration_ns += MonotonicTime::now().nanoseconds() - sweep_start_ns;+ update_gc_bytes_threshold(live_external_bytes);}if (print_report) {@@ -1617,11 +1671,15 @@ void Heap::sweep_dead_cells(bool print_report, Core::ElapsedTimer const& measurevoid Heap::sweep_block(HeapBlock& block){// A destructor can allocate and sweep another block. Its time is included in the outermost sweep.- bool record_stats = m_stats && m_block_sweeps_in_progress == 0;- auto stats_start_ns = record_stats ? MonotonicTime::now().nanoseconds() : 0;- ScopeGuard record_duration = [&] {- if (record_stats)- m_stats->sweep_duration_ns += MonotonicTime::now().nanoseconds() - stats_start_ns;+ bool record_duration = m_block_sweeps_in_progress == 0;+ auto start_ns = record_duration ? MonotonicTime::now().nanoseconds() : 0;+ ScopeGuard record_duration_guard = [&] {+ if (!record_duration)+ return;+ auto duration_ns = MonotonicTime::now().nanoseconds() - start_ns;+ m_sweep_duration_ns += duration_ns;+ if (m_stats)+ m_stats->sweep_duration_ns += duration_ns;};++m_block_sweeps_in_progress;ScopeGuard finish_block_sweep = [&] { --m_block_sweeps_in_progress; };@@ -1644,7 +1702,6 @@ void Heap::sweep_block(HeapBlock& block)} else {cell->set_marked(false);block_has_live_cells = true;- m_sweep_live_cell_bytes += block.cell_size();++live_cells;if (!external_memory_size)return;@@ -1700,7 +1757,6 @@ void Heap::start_incremental_sweep()dbgln_if(INCREMENTAL_SWEEP_DEBUG, "[sweep] === Starting incremental sweep ===");m_incremental_sweep_active = true;- m_sweep_live_cell_bytes = 0;m_sweep_live_external_bytes = 0;incremental_sweep_stats().should_report = false;incremental_sweep_stats().total_blocks = 0;@@ -1733,13 +1789,13 @@ void Heap::start_incremental_sweep()void Heap::finish_incremental_sweep(){- update_gc_bytes_threshold(m_sweep_live_cell_bytes, m_sweep_live_external_bytes);+ update_gc_bytes_threshold(m_sweep_live_external_bytes);dbgln_if(INCREMENTAL_SWEEP_DEBUG, "[sweep] === Sweep complete ===");- dbgln_if(INCREMENTAL_SWEEP_DEBUG, "[sweep] Live cell bytes: {} ({} KiB)", m_sweep_live_cell_bytes, m_sweep_live_cell_bytes / KiB);+ dbgln_if(INCREMENTAL_SWEEP_DEBUG, "[sweep] Live cell bytes: {} ({} KiB)", m_marked_cell_bytes, m_marked_cell_bytes / KiB);dbgln_if(INCREMENTAL_SWEEP_DEBUG, "[sweep] Live external bytes: {} ({} KiB)", m_sweep_live_external_bytes, m_sweep_live_external_bytes / KiB);dbgln_if(INCREMENTAL_SWEEP_DEBUG, "[sweep] Next GC threshold: {} ({} KiB)", m_gc_bytes_threshold, m_gc_bytes_threshold / KiB);- print_incremental_sweep_report(m_sweep_live_cell_bytes, m_sweep_live_external_bytes, m_gc_bytes_threshold);+ print_incremental_sweep_report(m_marked_cell_bytes, m_sweep_live_external_bytes, m_gc_bytes_threshold);// Clear marks on cells allocated during sweep. Sweep already cleared// marks on cells it visited, so only these remain marked.diff --git a/Libraries/LibGC/Heap.h b/Libraries/LibGC/Heap.hindex fcc3bc4f376..f43d9a6ffbe 100644--- a/Libraries/LibGC/Heap.h+++ b/Libraries/LibGC/Heap.h@@ -11,6 +11,7 @@#include <AK/HashTable.h>#include <AK/Noncopyable.h>#include <AK/NonnullOwnPtr.h>+#include <AK/Optional.h>#include <AK/OwnPtr.h>#include <AK/RefPtr.h>#include <AK/StackInfo.h>@@ -226,7 +227,9 @@ private:void give_back_local_free_lists();void will_allocate(size_t, TriggersCollection = TriggersCollection::Yes);- void update_gc_bytes_threshold(size_t live_cell_bytes, size_t live_external_bytes);+ void update_allocation_rate();+ double next_growth_factor(size_t live_cell_bytes);+ void update_gc_bytes_threshold(size_t live_external_bytes);enum class IncludeIncomingCrossHeapMembers {No,@@ -334,7 +337,7 @@ private:bool m_incremental_sweep_active { false };size_t m_block_sweeps_in_progress { 0 };- size_t m_sweep_live_cell_bytes { 0 };+ size_t m_marked_cell_bytes { 0 };size_t m_sweep_live_external_bytes { 0 };Vector<GC::Ptr<Cell>> m_cells_allocated_during_sweep;CellAllocator::SweepList m_allocators_to_sweep;@@ -345,6 +348,14 @@ private:// The context a heap created through LibGC/CAPI.h was created with.void* m_foreign_context { nullptr };IdleCollectionPolicy m_idle_collection_policy;++ // What decides the next collection budget. See next_growth_factor().+ double m_allocation_bytes_per_second { 0 };+ Optional<double> m_wanted_growth_factor;+ u64 m_total_allocated_bytes_at_last_collection { 0 };+ i64 m_last_collection_end_ns { 0 };+ i64 m_mark_duration_ns { 0 };+ i64 m_sweep_duration_ns { 0 };};inline void Heap::did_create_root(Badge<RootImpl>, RootImpl& impl)diff --git a/Tests/LibGC/TestGCIncrementalSweep.cpp b/Tests/LibGC/TestGCIncrementalSweep.cppindex 484dab46fb4..f7a2cf619f2 100644--- a/Tests/LibGC/TestGCIncrementalSweep.cpp+++ b/Tests/LibGC/TestGCIncrementalSweep.cpp@@ -4,8 +4,10 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/JsonObject.h>#include <LibGC/Cell.h>#include <LibGC/CellAllocator.h>+#include <LibGC/DeferGC.h>#include <LibGC/Heap.h>#include <LibGC/HeapBlock.h>#include <LibGC/Root.h>@@ -137,3 +139,49 @@ TEST_CASE(free_cells_that_a_destructor_takes_while_a_collection_finishes_the_sweauto next_cell = GC::make_root(heap.allocate<CellOfAnotherType>());EXPECT_EQ(next_cell.ptr(), cell_allocated_by_the_destructor);}++TEST_CASE(cells_allocated_into_blocks_pending_sweep_do_not_count_as_live)+{+ GC::Heap heap([](auto&) { }, GC::Heap::BecomeProcessDefault::No);+ heap.enable_stats();++ auto reachable_cell = GC::make_root(heap.allocate<CellOfAnotherType>());+ auto& block = *GC::HeapBlock::from_cell(reachable_cell.ptr());+ GC::scrub_stack_for_testing();++ heap.collect_garbage();+ EXPECT(heap.is_incremental_sweep_active());++ // Under a deferral the allocator takes cells from the block without sweeping it, and they get marked to survive.+ {+ GC::DeferGC defer_gc { heap };+ for (size_t i = 0; i < 16; ++i) {+ auto cell = heap.allocate<CellOfAnotherType>();+ EXPECT_EQ(GC::HeapBlock::from_cell(cell.ptr()), &block);+ EXPECT(block.is_pending_sweep());+ }+ }++ // The next collection finishes that sweep, which records what the collection before it found alive.+ heap.collect_garbage();+ AK::JsonObject stats;+ heap.add_stats(stats);+ EXPECT_EQ(stats.get_u64("gc.live_cell_bytes"sv), block.cell_size());+}++TEST_CASE(collect_everything_leaves_no_live_cell_bytes)+{+ GC::Heap heap([](auto&) { }, GC::Heap::BecomeProcessDefault::No);+ heap.enable_stats();++ {+ auto reachable_cell = GC::make_root(heap.allocate<CellOfAnotherType>());+ GC::scrub_stack_for_testing();+ heap.collect_garbage();+ }++ heap.collect_garbage(GC::Heap::CollectionType::CollectEverything);+ AK::JsonObject stats;+ heap.add_stats(stats);+ EXPECT_EQ(stats.get_u64("gc.live_cell_bytes"sv), 0u);+}