From 731e468f315a5ebe12dafcfec7ed0a32635ed0bc Mon Sep 17 00:00:00 2001 From: lizzie Date: Sun, 26 Apr 2026 09:39:11 +0000 Subject: [PATCH] [ports, freebsd] use shm_create_largepage(3) with a DEFAULT allocation to exploit defragmented RAM situations Signed-off-by: lizzie --- src/common/host_memory.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 04f3a65778..86af04596b 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -22,6 +22,7 @@ #include #include #include "common/scope_exit.h" +#include "common/literals.h" #if defined(__linux__) #include @@ -506,7 +507,32 @@ public: #elif defined(__OpenBSD__) fd = shm_open_anon(O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600); #elif defined(__FreeBSD__) + int n_page_sizes = getpagesizes(nullptr, 0); + if (n_page_sizes > 0) { + std::vector page_sizes(n_page_sizes); + if (getpagesizes(page_sizes.data(), n_page_sizes) > 0) { + size_t max_size = page_sizes[0]; + size_t max_index = 0; + for (size_t i = 0; i < page_sizes.size(); ++i) { + using namespace Common::Literals; + if (page_sizes[i] <= 4_GiB) { + max_size = (std::max)(max_size, page_sizes[i]); + max_index = i; + } + } + LOG_WARNING(Common_Memory, "using largepage of size {} #{}", max_size, max_index); + // Do not use SHM_LARGEPAGE_ALLOC_HARD, yknow what will happen when you do? + // the entire Eden process will hang for eternity! that's what will happen + // Want to fuck around and find out? Go ahead, I tempt you change the "default" to "hard" + fd = shm_create_largepage(SHM_ANON, O_RDWR, max_index, SHM_LARGEPAGE_ALLOC_DEFAULT, 0600); + } + } + if (fd >= 0) { + + } else { + LOG_WARNING(Common_Memory, "unable to force largepage: {}", strerror(errno)); fd = shm_open(SHM_ANON, O_RDWR, 0600); + } #elif defined(__APPLE__) // macOS doesn't have memfd_create, use anonymous temporary file char template_path[] = "/tmp/eden_mem_XXXXXX"; @@ -538,7 +564,12 @@ public: close(fd); } } else { +#ifdef __FreeBSD__ + // Prevent dirty tracking of memfd + backing_base = static_cast(mmap(nullptr, backing_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NOSYNC, fd, 0)); +#else backing_base = static_cast(mmap(nullptr, backing_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); +#endif } ASSERT_MSG(backing_base != MAP_FAILED, "mmap failed: {}", strerror(errno));