mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: mwrap-perl@80x24.org
Subject: [PATCH v2 1/2] use file-backed mmap on Linux
Date: Wed, 30 Nov 2022 21:08:36 +0000	[thread overview]
Message-ID: <20221130210837.17163-2-e@80x24.org> (raw)
In-Reply-To: <20221130210837.17163-1-e@80x24.org>

Due to the high memory usage of mwrap, it may be useful to force
allocations onto file-backed maps.  This means more physical
memory for the rest of the system.  As opposed to adding swap
indiscriminately, this provides the ability to give the
equivalent of swap space only to the processes using mwrap.

O_TMPFILE in Linux makes supporting this a breeze, since we
can't expect to use tmpfile(3) before malloc is initialized.
---
 mymalloc.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/mymalloc.h b/mymalloc.h
index 4904b74..88190d5 100644
--- a/mymalloc.h
+++ b/mymalloc.h
@@ -24,6 +24,49 @@
 #include <urcu/rculist.h>
 #include <urcu/wfcqueue.h>
 #include <urcu-bp.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#if !defined(MWRAP_FILE_BACKED) && defined(__linux__) && defined(O_TMPFILE)
+#	define MWRAP_FILE_BACKED 1
+#else
+#	define MWRAP_FILE_BACKED 0
+#endif
+#if MWRAP_FILE_BACKED
+#	include <sys/mman.h>
+static void *my_mmap(size_t size)
+{
+	int flags = MAP_PRIVATE;
+	const char *tmpdir = getenv("TMPDIR");
+	int fd;
+	void *ret;
+
+	if (!tmpdir)
+		tmpdir = "/tmp";
+
+	fd = open(tmpdir, O_TMPFILE|O_RDWR|S_IRUSR);
+	if (fd < 0) {
+		flags |= MAP_ANONYMOUS;
+	} else {
+		ssize_t w = pwrite(fd, "", 1, size - 1); /* sparse file */
+
+		if (w <= 0) { /* 0 may be ENOSPC */
+			flags |= MAP_ANONYMOUS;
+			close(fd);
+			fd = -1;
+		}
+	}
+	ret = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
+	if (fd >= 0) {
+		int err = errno;
+		close(fd);
+		errno = err;
+	}
+	return ret;
+}
+#endif /* MWRAP_FILE_BACKED */
 
 /* knobs for dlmalloc */
 #define FOOTERS 1 /* required for remote_free_* stuff */
@@ -32,6 +75,10 @@
 #define DLMALLOC_EXPORT static inline
 /* #define NO_MALLOC_STATS 1 */
 #define USE_LOCKS 0 /* we do our own global_mtx + ms_tsd */
+#if MWRAP_FILE_BACKED
+#	define MMAP(size) my_mmap(size)
+#	define HAVE_MREMAP 0
+#endif
 #include "dlmalloc_c.h"
 #undef ABORT /* conflicts with Perl */
 

  reply	other threads:[~2022-11-30 21:08 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-30  6:49 [PATCH 0/2] support file-backed mmap on Linux Eric Wong
2022-11-30  6:49 ` [PATCH 1/2] use " Eric Wong
2022-11-30 20:43   ` Eric Wong
2022-11-30  6:49 ` [PATCH 2/2] increase DEFAULT_GRANULARITY to 64M Eric Wong
2022-11-30 21:08 ` [PATCH v2 0/2] support file-backed mmap on Linux Eric Wong
2022-11-30 21:08   ` Eric Wong [this message]
2022-12-02 10:14     ` [PATCH v2 1/2] use " Eric Wong
2022-11-30 21:08   ` [PATCH v2 2/2] increase DEFAULT_GRANULARITY to 64M 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

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

  git send-email \
    --in-reply-to=20221130210837.17163-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=mwrap-perl@80x24.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/mwrap-perl.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).