Skip to content

Truly independent web browser

LibGC: Give the pages of blocks that stay unused back to the system

Andreas Kling7959a601dd60
diff --git a/Libraries/LibGC/BlockAllocator.cpp b/Libraries/LibGC/BlockAllocator.cpp
index c05265cbdb3..773ba857909 100644
--- a/Libraries/LibGC/BlockAllocator.cpp
+++ b/Libraries/LibGC/BlockAllocator.cpp
@@ -10,6 +10,7 @@
#include <AK/Checked.h>
#include <AK/NeverDestroyed.h>
#include <AK/Platform.h>
+#include <AK/Time.h>
#include <AK/Try.h>
#include <AK/Vector.h>
#include <LibCore/System.h>
@@ -49,6 +50,17 @@ namespace GC {
// before the worker sees them skip the madvise pair entirely.
static constexpr size_t CHUNK_SIZE = 2 * MiB;
static constexpr size_t BLOCKS_PER_CHUNK = CHUNK_SIZE / HeapBlock::BLOCK_SIZE;
+
+// How long a block with MADV_FREE'd pages can sit unused before its pages go back for real. This is longer than most
+// heaps take between collections, so that blocks they reuse keep skipping the page faults. And as the collections of
+// every heap drive the decommit worker, it keeps a busy heap from making the blocks of a slower one look cold.
+static Atomic<i64, AK::MemoryOrder::memory_order_relaxed> s_cold_block_age_in_milliseconds { 5000 };
+
+static AK::Duration cold_block_age()
+{
+ return AK::Duration::from_milliseconds(s_cold_block_age_in_milliseconds.load());
+}
+
static_assert((HeapBlock::BLOCK_SIZE & (HeapBlock::BLOCK_SIZE - 1)) == 0);
static_assert(CHUNK_SIZE % HeapBlock::BLOCK_SIZE == 0);
static_assert(BLOCKS_PER_CHUNK == 128);
@@ -296,8 +308,13 @@ public:
DecommitWorker();
private:
+ enum class ReturnColdBlocks {
+ No,
+ Yes,
+ };
+
void run();
- void process_one(BlockAllocator&);
+ void process_one(BlockAllocator&, ReturnColdBlocks);
Mutex m_mutex;
ConditionVariable m_cv { m_mutex };
@@ -305,6 +322,7 @@ private:
Vector<BlockAllocator*> m_pending;
bool m_kicked { false };
bool m_chunk_requested { false };
+ MonotonicTime m_last_return_of_cold_blocks { MonotonicTime::now_coarse() };
static Atomic<DecommitWorker*> s_the;
};
@@ -376,8 +394,14 @@ void DecommitWorker::run()
bool chunk_requested = false;
{
MutexLocker locker(m_mutex);
- while (!m_kicked && !m_chunk_requested)
- m_cv.wait();
+ while (!m_kicked && !m_chunk_requested) {
+ // NB: Collections may never come again, and what is on the list must not wait for one. Each block
+ // only takes a few rounds of this, after which the list is empty and this thread sleeps for good.
+ if (m_pending.is_empty())
+ m_cv.wait();
+ else if (!m_cv.wait_for(cold_block_age()))
+ m_kicked = true;
+ }
chunk_requested = exchange(m_chunk_requested, false);
if (m_kicked) {
m_kicked = false;
@@ -400,8 +424,14 @@ void DecommitWorker::run()
// syscall bandwidth.
sleep_before_decommit();
+ auto return_cold_blocks = ReturnColdBlocks::No;
+ if (auto now = MonotonicTime::now_coarse(); now - m_last_return_of_cold_blocks >= cold_block_age()) {
+ return_cold_blocks = ReturnColdBlocks::Yes;
+ m_last_return_of_cold_blocks = now;
+ }
+
for (auto* a : snapshot) {
- process_one(*a);
+ process_one(*a, return_cold_blocks);
int prev_refcount = a->m_worker_refcount.fetch_sub(1);
if (prev_refcount == 1) {
MutexLocker locker(a->m_mutex);
@@ -411,13 +441,20 @@ void DecommitWorker::run()
}
}
-void DecommitWorker::process_one(BlockAllocator& a)
+void DecommitWorker::process_one(BlockAllocator& a, [[maybe_unused]] ReturnColdBlocks return_cold_blocks)
{
Vector<void*> to_process;
+ Vector<void*> cold_blocks;
{
MutexLocker locker(a.m_mutex);
a.m_in_decommit_registry = false;
to_process = move(a.m_freshly_freed);
+#if defined(AK_OS_LINUX)
+ // Blocks that went through MADV_FREE stay in the process until the system runs short of memory, and only their
+ // own type can reuse them. What that type did not reuse since the last time will likely stay unused.
+ if (return_cold_blocks == ReturnColdBlocks::Yes)
+ cold_blocks = move(a.m_blocks);
+#endif
}
// Madvise each slot outside the per-allocator lock so the JS thread can
@@ -429,12 +466,27 @@ void DecommitWorker::process_one(BlockAllocator& a)
if ((i + 1) % BATCH == 0)
yield_during_decommit();
}
+ for (size_t i = 0; i < cold_blocks.size(); ++i) {
+ return_block_to_system(cold_blocks[i]);
+ if ((i + 1) % BATCH == 0)
+ yield_during_decommit();
+ }
+ bool need_to_register = false;
{
MutexLocker locker(a.m_mutex);
- for (auto* slot : to_process)
- a.m_blocks.append(slot);
+ a.m_blocks.extend(move(to_process));
+ a.m_returned_blocks.extend(move(cold_blocks));
+#if defined(AK_OS_LINUX)
+ // NB: So an allocator is only on the list while it has blocks to look at, however many allocators there are.
+ if (!a.m_blocks.is_empty() && !a.m_in_decommit_registry) {
+ a.m_in_decommit_registry = true;
+ need_to_register = true;
+ }
+#endif
}
+ if (need_to_register)
+ register_pending(a);
}
void BlockAllocator::wake_decommit_worker_async()
@@ -442,6 +494,11 @@ void BlockAllocator::wake_decommit_worker_async()
DecommitWorker::the().kick();
}
+AK::Duration BlockAllocator::set_cold_block_age_for_testing(AK::Duration age)
+{
+ return AK::Duration::from_milliseconds(s_cold_block_age_in_milliseconds.exchange(age.to_milliseconds()));
+}
+
void BlockAllocator::return_freed_blocks_to_system()
{
Vector<void*> blocks;
@@ -470,12 +527,17 @@ BlockAllocator::~BlockAllocator()
{
// Chunks are permanent -- we never tear them down. The destructor only
// exists to make sure the global decommit worker has finished any
- // in-flight processing of *this before our storage goes away.
- DecommitWorker::the().deregister(*this);
+ // in-flight processing of *this before our storage goes away. That
+ // processing can put us back on its list, so look again after it.
+ while (true) {
+ DecommitWorker::the().deregister(*this);
- MutexLocker locker(m_mutex);
- while (m_worker_refcount.load() != 0)
- m_worker_cv.wait();
+ MutexLocker locker(m_mutex);
+ while (m_worker_refcount.load() != 0)
+ m_worker_cv.wait();
+ if (!exchange(m_in_decommit_registry, false))
+ break;
+ }
}
size_t BlockAllocator::block_count()
diff --git a/Libraries/LibGC/BlockAllocator.h b/Libraries/LibGC/BlockAllocator.h
index 8541d601826..bdcd59da60a 100644
--- a/Libraries/LibGC/BlockAllocator.h
+++ b/Libraries/LibGC/BlockAllocator.h
@@ -9,6 +9,7 @@
#include <AK/Atomic.h>
#include <AK/ConditionVariable.h>
#include <AK/Mutex.h>
+#include <AK/Time.h>
#include <AK/Vector.h>
#include <AK/kmalloc.h>
#include <LibGC/Forward.h>
@@ -48,6 +49,10 @@ public:
// work that's piled up. Call this at the end of a GC sweep.
static void wake_decommit_worker_async();
+ // How long a block can sit unused before its pages go back to the system. Tests make it short to see this happen.
+ // Returns the age it replaces.
+ static AK::Duration set_cold_block_age_for_testing(AK::Duration);
+
private:
friend class DecommitWorker;
@@ -55,8 +60,9 @@ private:
// the worker (Darwin) so allocate_block pairs them with MADV_FREE_REUSE.
Vector<void*> m_blocks;
- // Slots whose pages went back with return_freed_blocks_to_system(). They
- // are the last ones to reuse, as their pages have to be faulted in again.
+ // Slots whose pages went back for real, as their heap went away or they
+ // stayed unused for long. They are the last ones to reuse, as their pages
+ // have to be faulted in again.
Vector<void*> m_returned_blocks;
// Slots freed by deallocate_block but not yet madvised. allocate_block
@@ -76,7 +82,8 @@ private:
// True iff this allocator is currently in the worker's pending list.
// Avoids re-registering on every dealloc; cleared by the worker at the
- // start of process_one, set by deallocate_block under m_mutex.
+ // start of process_one, set by deallocate_block under m_mutex, and by
+ // process_one for blocks that it has to look at again.
bool m_in_decommit_registry { false };
};
diff --git a/Tests/LibGC/TestBlockAllocator.cpp b/Tests/LibGC/TestBlockAllocator.cpp
index 9a58186046f..4862a70af79 100644
--- a/Tests/LibGC/TestBlockAllocator.cpp
+++ b/Tests/LibGC/TestBlockAllocator.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/ScopeGuard.h>
#include <AK/Time.h>
#include <AK/Vector.h>
#include <LibCore/System.h>
@@ -95,3 +96,30 @@ TEST_CASE(decommitted_block_can_be_reused)
allocator.deallocate_block(reused_block, GC::DeferDecommit::No);
}
+
+#if defined(AK_OS_LINUX) && !defined(HAS_ADDRESS_SANITIZER)
+TEST_CASE(pages_of_a_block_that_stays_unused_go_back_to_the_system)
+{
+ auto cold_block_age = GC::BlockAllocator::set_cold_block_age_for_testing(AK::Duration::from_milliseconds(10));
+ ScopeGuard restore_cold_block_age = [&] { GC::BlockAllocator::set_cold_block_age_for_testing(cold_block_age); };
+
+ GC::BlockAllocator allocator;
+
+ auto* block = static_cast<u8 volatile*>(allocator.allocate_block("cold"));
+ block[0] = 0xab;
+ allocator.deallocate_block(const_cast<u8*>(block), GC::DeferDecommit::Yes);
+ GC::BlockAllocator::wake_decommit_worker_async();
+
+ // NB: MADV_FREE leaves the contents until the system runs short of memory, so only a real return zeroes the page.
+ // Nothing has to collect garbage for this to happen.
+ auto deadline = MonotonicTime::now() + AK::Duration::from_seconds(30);
+ while (block[0] != 0 && MonotonicTime::now() < deadline)
+ (void)Core::System::sleep_ms(1);
+ u8 first_byte = block[0];
+ EXPECT_EQ(first_byte, 0);
+
+ auto* reused_block = allocator.allocate_block("cold");
+ EXPECT_EQ(reused_block, const_cast<u8*>(block));
+ allocator.deallocate_block(reused_block, GC::DeferDecommit::No);
+}
+#endif