user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [RFC] daemon: experimental PREALLOC_NR env knob
Date: Sat, 24 Feb 2024 08:52:50 +0000	[thread overview]
Message-ID: <20240224085250.3632004-1-e@80x24.org> (raw)

As I've observed using the mwrap-perl LD_PRELOAD wrapper,
permanent 4080-byte arenas allocated by Perl late in the
process will impede consolidation of freed adjacent blocks.
In long-lived processes, this fragmentation from immortal
arenas near the "wilderness"[1] area can force excessive
memory to be requested from the kernel (via sbrk or mmap).

Combining this with MALLOC_MMAP_THRESHOLD_=131072 appears
to reduce memory use of long-lived, heavily-trafficked
processes.

[1] this is dlmalloc terminology:
    https://gee.cs.oswego.edu/dl/html/malloc.html
---

 Been testing various iterations and refining this off/on for a
 few weeks, now.  Immortal allocations sprinkled throughout the
 heap is bad and Perl's arenas are a major remaining culprit...

 lib/PublicInbox/Daemon.pm | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index e578f2e8..a80f2cbb 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -36,6 +36,22 @@ my %SCHEME2PORT = map { $KNOWN_TLS{$_} => $_ + 0 } keys %KNOWN_TLS;
 for (keys %KNOWN_STARTTLS) { $SCHEME2PORT{$KNOWN_STARTTLS{$_}} = $_ + 0 }
 $SCHEME2PORT{http} = 80;
 
+# Preallocate SV and HE arenas early so they don't end up in/near
+# the "wilderness" (in dlmalloc terminology).  This should result
+# in lower fragmentation and memory use.
+# Not sure if other arenas can benefit from this.
+# using PREALLOC_NR=500000 for the yhbt.net/lore mirror
+BEGIN {
+	if (my $nr = $ENV{PREALLOC_NR}) {
+		my @tmp = map { $_ => $_ } (0..$nr);
+		# some extra arenas for HVs and AVs
+		$nr >>= 5;
+		$nr = 10000 if $nr < 10000;
+		@tmp = map { +{} } (0..$nr);
+		@tmp = map { [ $_ ] } (0..$nr);
+	}
+}
+
 our ($parent_pipe, %POST_ACCEPT, %XNETD);
 our %WORKER_SIG = (
 	INT => \&worker_quit,

             reply	other threads:[~2024-02-24  8:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-24  8:52 Eric Wong [this message]
2024-03-14 19:36 ` [RFC] daemon: experimental PREALLOC_NR env knob Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240224085250.3632004-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).