Truly independent web browser
AK+LibGC+LibWeb: Give the memory of a worker back when it ends
Andreas Klingd6918ed66a12
diff --git a/AK/FFIHelpers.h b/AK/FFIHelpers.hindex e7ab2ffef11..bef3f5af6fb 100644--- a/AK/FFIHelpers.h+++ b/AK/FFIHelpers.h@@ -34,6 +34,11 @@ void ladybird_release_unused_allocator_memory();void* ladybird_enter_scratch_heap();void ladybird_leave_scratch_heap(void* previous_heap);+// For a thread that frees all of its memory when it ends: while one runs, what the allocator purges goes back to the+// system right away.+void ladybird_did_start_short_lived_thread();+void ladybird_did_end_short_lived_thread();+// Allocations in HeapPartition::JSObjectStorage, where JS objects keep their property values. ak_kfree frees them.void* ladybird_js_object_storage_alloc(size_t size);void* ladybird_js_object_storage_realloc(void*, size_t new_size);diff --git a/AK/kmalloc.cpp b/AK/kmalloc.cppindex 02d299ef6a7..759ea183bcb 100644--- a/AK/kmalloc.cpp+++ b/AK/kmalloc.cpp@@ -6,6 +6,8 @@*/#include <AK/Assertions.h>+#include <AK/Atomic.h>+#include <AK/FFIHelpers.h>#include <AK/Platform.h>#include <AK/StdLibExtras.h>#include <AK/kmalloc.h>@@ -96,6 +98,14 @@ void ak_leave_scratch_heap(void*){}+extern "C" void ladybird_did_start_short_lived_thread()+{+}++extern "C" void ladybird_did_end_short_lived_thread()+{+}+#else# if defined(AK_OS_LINUX) || defined(AK_OS_MACOS)@@ -205,4 +215,28 @@ void ak_leave_scratch_heap(void* previous_heap)mi_heap_set_default(static_cast<mi_heap_t*>(previous_heap));}+// MADV_FREE leaves the pages in this process until the system runs short of memory. That pays off when they get reused+// soon, which is not what happens to the memory of a thread that ended. mimalloc reads this option each time it purges.+// NB: A thread that starts while another ends can leave the wrong value behind, until the next one of them. All that+// costs is the choice between two ways to purge.+# ifdef AK_OS_LINUX+static AK::Atomic<size_t> s_short_lived_thread_count { 0 };+# endif++extern "C" void ladybird_did_start_short_lived_thread()+{+# ifdef AK_OS_LINUX+ if (s_short_lived_thread_count.fetch_add(1) == 0)+ mi_option_set(mi_option_purge_decommits, 1);+# endif+}++extern "C" void ladybird_did_end_short_lived_thread()+{+# ifdef AK_OS_LINUX+ if (s_short_lived_thread_count.fetch_sub(1) == 1)+ mi_option_set(mi_option_purge_decommits, 0);+# endif+}+#endifdiff --git a/Libraries/LibGC/BlockAllocator.cpp b/Libraries/LibGC/BlockAllocator.cppindex 03d40f4000a..d0a9080934a 100644--- a/Libraries/LibGC/BlockAllocator.cpp+++ b/Libraries/LibGC/BlockAllocator.cpp@@ -220,6 +220,18 @@ static void madvise_block_for_decommit(void* block)#endif}+static void return_block_to_system(void* block)+{+#if defined(AK_OS_LINUX)+ if (madvise(block, HeapBlock::BLOCK_SIZE, MADV_DONTNEED) < 0) {+ perror("madvise(MADV_DONTNEED)");+ VERIFY_NOT_REACHED();+ }+#else+ madvise_block_for_decommit(block);+#endif+}+static void sleep_before_decommit(){#if defined(AK_OS_WINDOWS)@@ -397,6 +409,25 @@ void BlockAllocator::wake_decommit_worker_async()DecommitWorker::the().kick();}+void BlockAllocator::return_freed_blocks_to_system()+{+ Vector<void*> blocks;+ {+ MutexLocker locker(m_mutex);+ blocks = move(m_freshly_freed);+#if defined(AK_OS_LINUX)+ // NB: MADV_FREE leaves these pages in the process until the system runs short of memory.+ blocks.extend(move(m_blocks));+#endif+ }++ for (auto* block : blocks)+ return_block_to_system(block);++ MutexLocker locker(m_mutex);+ m_returned_blocks.extend(move(blocks));+}+BlockAllocator::BlockAllocator(): m_worker_cv(m_mutex){@@ -417,7 +448,7 @@ BlockAllocator::~BlockAllocator()size_t BlockAllocator::block_count(){MutexLocker locker(m_mutex);- return m_blocks.size();+ return m_blocks.size() + m_returned_blocks.size();}HeapRegionBaseCopyRegistration::HeapRegionBaseCopyRegistration(FlatPtr& copy)@@ -458,6 +489,9 @@ void* BlockAllocator::allocate_block([[maybe_unused]] char const* name)} else if (!m_blocks.is_empty()) {block = m_blocks.take_last();needs_madvise_reuse = true;+ } else if (!m_returned_blocks.is_empty()) {+ block = m_returned_blocks.take_last();+ needs_madvise_reuse = true;}}diff --git a/Libraries/LibGC/BlockAllocator.h b/Libraries/LibGC/BlockAllocator.hindex 13feceb8450..9ed700a1b09 100644--- a/Libraries/LibGC/BlockAllocator.h+++ b/Libraries/LibGC/BlockAllocator.h@@ -31,6 +31,10 @@ public:size_t block_count();+ // Gives the pages of the free blocks back to the system right away, rather than leaving them for the system to take+ // when it runs short of memory. For a heap that went away, whose blocks may never be reused.+ void return_freed_blocks_to_system();+static FlatPtr heap_region_start();static FlatPtr heap_region_end();// The end of the part of the region that chunks have been committed from so far. No block starts at or above it.@@ -47,12 +51,16 @@ 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.+ Vector<void*> m_returned_blocks;+// Slots freed by deallocate_block but not yet madvised. allocate_block// pops directly from here to skip the madvise round-trip on hot recycle// paths -- this is the main payoff of deferring decommit.Vector<void*> m_freshly_freed;- // Protects m_blocks, m_freshly_freed, and m_in_decommit_registry. Held+ // Protects the slots above and m_in_decommit_registry. Held// briefly on the alloc/dealloc hot path; uncontended in the common case.Mutex m_mutex;diff --git a/Libraries/LibGC/CellAllocator.cpp b/Libraries/LibGC/CellAllocator.cppindex 8a6033183fa..85ef801040d 100644--- a/Libraries/LibGC/CellAllocator.cpp+++ b/Libraries/LibGC/CellAllocator.cpp@@ -66,11 +66,14 @@ CellAllocator::~CellAllocator()auto reclaim_all = [this](BlockList& blocks) {while (auto* block = blocks.take_first()) {block->~HeapBlock();- m_block_allocator.deallocate_block(block, DeferDecommit::Yes);+ m_block_allocator.deallocate_block(block, DeferDecommit::No);}};reclaim_all(m_full_blocks);reclaim_all(m_usable_blocks);++ // NB: This heap is gone, and another one may not come along to use the blocks of this type for a long time.+ m_block_allocator.return_freed_blocks_to_system();}Cell* CellAllocator::allocate_cell_slow(Heap& heap)diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Libraries/LibWeb/Bindings/MainThreadVM.cppindex b99b8c07299..630c7e5f641 100644--- a/Libraries/LibWeb/Bindings/MainThreadVM.cpp+++ b/Libraries/LibWeb/Bindings/MainThreadVM.cpp@@ -9,6 +9,7 @@*/#include <AK/Atomic.h>+#include <AK/FFIHelpers.h>#include <AK/NeverDestroyed.h>#include <LibCore/AnonymousBuffer.h>#include <LibCore/Platform/ThreadQoS.h>@@ -176,6 +177,7 @@ void initialize_agent(HTML::AgentType type)main_thread_vm_ptr() = new_vm;} else {++s_worker_agent_count;+ ladybird_did_start_short_lived_thread();}vm.set_agent(create_agent(vm.heap(), type));@@ -822,6 +824,11 @@ void shut_down_agent()// NB: The VM stays reachable while it is destroyed, as the finalizers of the cells in its heap use it.vm.unref();+ // NB: What this agent used is of no use to this process unless another worker comes along, which may be never. What+ // this thread's allocator still holds goes back when the thread exits.+ ak_kmalloc_collect();+ ladybird_did_end_short_lived_thread();+--s_worker_agent_count;}