Skip to content

Truly independent web browser

LibGC: Collect steadily allocating heaps from the idle timer

Andreas Kling03fad729b391
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cpp
index a1b43d3854f..ce17ee09d5b 100644
--- a/Libraries/LibGC/Heap.cpp
+++ b/Libraries/LibGC/Heap.cpp
@@ -58,6 +58,13 @@ static constexpr size_t GC_MIN_BYTES_THRESHOLD { 32 * 1024 * 1024 };
// 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 };
+// A heap that allocates steadily is collected from the idle timer once its garbage reaches this share of its live
+// size (or GC_MIN_BYTES_THRESHOLD, whichever is more), well before the allocation threshold. Nobody waits for those
+// collections, but they are not free either, so marking for them may take at most GC_STEADY_TIME_FRACTION of the
+// running time. See idle_gc_on_timer().
+static constexpr double GC_STEADY_GROWTH_FACTOR { 0.25 };
+static constexpr double GC_STEADY_TIME_FRACTION { 0.02 };
+
// 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.
@@ -294,6 +301,8 @@ StringView collection_trigger_name(CollectionTrigger trigger)
return "allocation"sv;
case CollectionTrigger::IdlePolicy:
return "idle policy"sv;
+ case CollectionTrigger::SteadyBudget:
+ return "steady budget"sv;
case CollectionTrigger::AbandonedObjectGraph:
return "abandoned object graph"sv;
case CollectionTrigger::Compaction:
@@ -2430,7 +2439,13 @@ void Heap::idle_gc_on_timer()
// NB: Only a heap whose outermost event loop turns hold no cells ever runs a collection that moves them, so no other
// heap looks for blocks to compact.
bool may_reduce_memory = libgc_compact() && !m_moves_everything && m_outermost_event_loop_turn_holds_no_cells;
- switch (m_idle_collection_policy.evaluate(m_total_allocated_bytes, m_allocated_bytes_since_last_gc, m_gc_bytes_threshold)) {
+ IdleCollectionPolicy::SteadyBudget steady_budget {
+ .bytes = max(static_cast<size_t>(static_cast<double>(m_marked_cell_bytes) * GC_STEADY_GROWTH_FACTOR), GC_MIN_BYTES_THRESHOLD),
+ .ns_since_last_collection = MonotonicTime::now().nanoseconds() - m_last_collection_end_ns,
+ .min_ns_between_collections = static_cast<i64>(static_cast<double>(m_mark_duration_ns) * (1 - GC_STEADY_TIME_FRACTION) / GC_STEADY_TIME_FRACTION),
+ };
+ auto decision = m_idle_collection_policy.evaluate(m_total_allocated_bytes, m_allocated_bytes_since_last_gc, m_gc_bytes_threshold, steady_budget);
+ switch (decision) {
case IdleCollectionPolicy::Decision::KeepWaiting:
// NB: A heap that went quiet with little garbage still has the holes that its last sweep left, which
// compacting collections close.
@@ -2445,6 +2460,7 @@ void Heap::idle_gc_on_timer()
m_idle_gc_timer->stop();
return;
case IdleCollectionPolicy::Decision::Collect:
+ case IdleCollectionPolicy::Decision::CollectSteadyBudget:
// NB: Whatever the event loop has queued, like input or a task, goes first, and the next tick looks again.
// What has not reached the queue yet, like a message still in its socket, waits for the collection. It
// waits for one tick at most, so that a queue that is never empty does not hold off the collection and the
@@ -2458,7 +2474,7 @@ void Heap::idle_gc_on_timer()
m_reducing_memory = may_reduce_memory && m_idle_collection_policy.last_tick_was_quiet();
m_compacted_since_activity = false;
m_allocated_bytes_since_last_gc = 0;
- collect_garbage_from_timer(CollectionTrigger::IdlePolicy);
+ collect_garbage_from_timer(decision == IdleCollectionPolicy::Decision::Collect ? CollectionTrigger::IdlePolicy : CollectionTrigger::SteadyBudget);
m_idle_gc_timer->stop();
return;
}
diff --git a/Libraries/LibGC/Heap.h b/Libraries/LibGC/Heap.h
index 79e9106f37b..494edb17f1f 100644
--- a/Libraries/LibGC/Heap.h
+++ b/Libraries/LibGC/Heap.h
@@ -41,6 +41,7 @@ enum class CollectionTrigger : u8 {
Explicit,
Allocation,
IdlePolicy,
+ SteadyBudget,
AbandonedObjectGraph,
Compaction,
DeferralEnded,
diff --git a/Libraries/LibGC/IdleCollectionPolicy.h b/Libraries/LibGC/IdleCollectionPolicy.h
index 6d6aceeca23..83f719d09d5 100644
--- a/Libraries/LibGC/IdleCollectionPolicy.h
+++ b/Libraries/LibGC/IdleCollectionPolicy.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/NumericLimits.h>
#include <AK/StdLibExtras.h>
#include <AK/Types.h>
@@ -20,14 +21,27 @@ namespace GC {
// to be worth marking the whole live heap. A tick that allocates next to nothing is a quiet one, which collects once
// a few MiB of garbage are there, however large the heap and its threshold: nobody waits for a quiet heap, and what
// the garbage holds goes back to the system long before the threshold would have come. The watchdog bounds how long
-// garbage can sit when neither trigger fires, e.g. on a heap that allocates too steadily to show a drop or too
-// slowly to clear the gates.
+// garbage can sit when neither trigger fires, e.g. on a heap that allocates too slowly to clear the gates.
+//
+// A heap that allocates steadily never shows a quiet tick and only shows a rate drop by accident, and its allocation
+// threshold can sit far above its live size. So once the garbage reaches a steady budget, we collect even though the
+// mutator is still busy, as long as enough time has passed since the last collection for marking to stay a small share
+// of the running time.
class IdleCollectionPolicy {
public:
enum class Decision : u8 {
- KeepWaiting, // No collection yet; leave the idle timer running.
- Park, // Nothing left to collect; stop the timer until allocation resumes.
- Collect, // Collect now.
+ KeepWaiting, // No collection yet; leave the idle timer running.
+ Park, // Nothing left to collect; stop the timer until allocation resumes.
+ Collect, // Collect now.
+ CollectSteadyBudget, // Collect now, because the garbage reached the steady budget.
+ };
+
+ // When a heap that keeps allocating has piled up enough garbage to collect it anyway.
+ struct SteadyBudget {
+ size_t bytes { NumericLimits<size_t>::max() };
+ // How long it has been since the last collection, and how long it must be at least before this one.
+ i64 ns_since_last_collection { 0 };
+ i64 min_ns_between_collections { 0 };
};
// Begins a fresh episode. `total_allocated_bytes` is the heap's monotonic allocation counter.
@@ -40,8 +54,9 @@ public:
}
// Evaluates one idle-timer tick. `total_allocated_bytes` is the monotonic allocation counter, `garbage_bytes` is
- // the amount allocated since the last collection, and `gc_threshold` is the allocation-driven GC threshold.
- Decision evaluate(u64 total_allocated_bytes, size_t garbage_bytes, size_t gc_threshold)
+ // the amount allocated since the last collection, `gc_threshold` is the allocation-driven GC threshold, and
+ // `steady_budget` says when garbage is worth collecting while the heap is still allocating.
+ Decision evaluate(u64 total_allocated_bytes, size_t garbage_bytes, size_t gc_threshold, SteadyBudget steady_budget)
{
if (garbage_bytes == 0)
return Decision::Park;
@@ -58,9 +73,17 @@ public:
if ((rate_dropped && enough_garbage) || (m_last_tick_was_quiet && enough_garbage_for_a_quiet_heap) || watchdog_elapsed)
return Decision::Collect;
+ if (garbage_bytes >= steady_budget.bytes && steady_budget.ns_since_last_collection >= steady_budget.min_ns_between_collections)
+ return Decision::CollectSteadyBudget;
return Decision::KeepWaiting;
}
+ // Evaluates one idle-timer tick for a heap that never collects on a steady budget.
+ Decision evaluate(u64 total_allocated_bytes, size_t garbage_bytes, size_t gc_threshold)
+ {
+ return evaluate(total_allocated_bytes, garbage_bytes, gc_threshold, SteadyBudget {});
+ }
+
// Whether the last tick that was evaluated allocated next to nothing.
bool last_tick_was_quiet() const { return m_last_tick_was_quiet; }