Truly independent web browser
LibIPC: Release allocator memory when an IO thread goes idle
Aliaksandr Kalenike669c1141344
diff --git a/Libraries/LibIPC/TransportMachPort.cpp b/Libraries/LibIPC/TransportMachPort.cppindex ea66f68f39d..0df1d47142d 100644--- a/Libraries/LibIPC/TransportMachPort.cpp+++ b/Libraries/LibIPC/TransportMachPort.cpp@@ -8,6 +8,7 @@#include <AK/NonnullOwnPtr.h>#include <AK/NumericLimits.h>#include <AK/Time.h>+#include <AK/kmalloc.h>#include <LibCore/MachPort.h>#include <LibCore/Notifier.h>#include <LibCore/Platform/ThreadQoS.h>@@ -288,6 +289,8 @@ void TransportMachPort::mark_peer_eof()write_read_notification_byte();}+static constexpr mach_msg_timeout_t IDLE_MEMORY_RELEASE_DELAY_IN_MILLISECONDS = 1000;+intptr_t TransportMachPort::io_thread_loop(){static constexpr size_t RECV_BUFFER_SIZE = 65536;@@ -300,6 +303,7 @@ intptr_t TransportMachPort::io_thread_loop()// behind a cursor until the whole backlog has gone, so a retry costs nothing per message still waiting.Vector<PendingMessage> backlog;size_t backlog_sent = 0;+ bool released_memory_since_last_message = true;for (;;) {if (m_io_thread_state.load() == IOThreadState::Stopped)@@ -345,13 +349,24 @@ intptr_t TransportMachPort::io_thread_loop()// Wait on the whole port set so one loop can handle peer messages, internal wakeups, and kernel// notifications like MACH_NOTIFY_NO_SENDERS.// With a backlog, the wait is short, so the sends are retried as the peer makes room.- auto const receive_options = MACH_RCV_MSG | MACH_RCV_LARGE | (backlog_is_empty ? 0 : MACH_RCV_TIMEOUT);- auto const receive_timeout = backlog_is_empty ? MACH_MSG_TIMEOUT_NONE : 1;+ bool const waits_without_timeout = backlog_is_empty && released_memory_since_last_message;+ auto const receive_options = MACH_RCV_MSG | MACH_RCV_LARGE | (waits_without_timeout ? 0 : MACH_RCV_TIMEOUT);+ auto const receive_timeout = waits_without_timeout ? MACH_MSG_TIMEOUT_NONE+ : backlog_is_empty ? IDLE_MEMORY_RELEASE_DELAY_IN_MILLISECONDS+ : 1;auto const ret = mach_msg(header, receive_options, 0, buffer.size(),m_port_set.port(), receive_timeout, MACH_PORT_NULL);- if (ret == MACH_RCV_TIMED_OUT)+ if (ret == MACH_RCV_TIMED_OUT) {+ // NB: The messages this thread receives are freed by the thread that handles them, and the allocator keeps+ // that memory until this thread allocates again or collects, which an idle transport may never do.+ if (backlog_is_empty) {+ ak_kmalloc_collect();+ released_memory_since_last_message = true;+ }continue;+ }+ released_memory_since_last_message = false;if (ret == MACH_RCV_TOO_LARGE) {auto needed_size = header->msgh_size + sizeof(mach_msg_trailer_t);diff --git a/Libraries/LibIPC/TransportSocket.cpp b/Libraries/LibIPC/TransportSocket.cppindex bee6e04e8dd..ff12f32060a 100644--- a/Libraries/LibIPC/TransportSocket.cpp+++ b/Libraries/LibIPC/TransportSocket.cpp@@ -10,6 +10,7 @@#include <AK/NonnullOwnPtr.h>#include <AK/ScopeGuard.h>#include <AK/Types.h>+#include <AK/kmalloc.h>#include <LibCore/Socket.h>#include <LibCore/System.h>#include <LibIPC/Attachment.h>@@ -38,6 +39,8 @@ static constexpr size_t READ_BUFFER_SIZE = 4096;// Maximum number of reads before the IO thread publishes what it has parsed and gets back to its other workstatic constexpr size_t MAX_READS_PER_ROUND = 16;+static constexpr int IDLE_MEMORY_RELEASE_DELAY_IN_MILLISECONDS = 1000;+Atomic<u32> TransportSocket::s_eof_drain_window_for_test_ms { 0 };Atomic<bool> TransportSocket::s_skip_inloop_read_for_test { false };@@ -242,6 +245,7 @@ TransportSocket::TransportSocket(NonnullOwnPtr<Core::LocalSocket> socket)intptr_t TransportSocket::io_thread_loop(){Array<struct pollfd, 2> pollfds;+ bool released_memory_since_last_wakeup = true;for (;;) {auto want_to_write = [&] {auto [bytes, fds] = m_send_queue->peek(1);@@ -264,7 +268,7 @@ intptr_t TransportSocket::io_thread_loop()ErrorOr<int> result { 0 };do {- result = Core::System::poll(pollfds, -1);+ result = Core::System::poll(pollfds, released_memory_since_last_wakeup ? -1 : IDLE_MEMORY_RELEASE_DELAY_IN_MILLISECONDS);} while (result.is_error() && result.error().code() == EINTR);if (result.is_error()) {dbgln("TransportSocket poll error: {}", result.error());@@ -272,6 +276,15 @@ intptr_t TransportSocket::io_thread_loop()break;}+ // NB: The messages this thread reads are freed by the thread that handles them, and the allocator keeps that+ // memory until this thread allocates again or collects, which an idle transport may never do.+ if (result.value() == 0) {+ ak_kmalloc_collect();+ released_memory_since_last_wakeup = true;+ continue;+ }+ released_memory_since_last_wakeup = false;+if (pollfds[1].revents & POLLIN) {char buf[64];// The wakeup pipe is non-blocking, so EAGAIN is possible if there's a spurious wakeup.