Support jemalloc as the default allocator on Linux (#7250)

Add jemalloc as an alternative malloc implementation for the Verilator
binary. When both tcmalloc and jemalloc are available, jemalloc is
preferred due to its better performance on RTLMeter.

The new --enable-jemalloc flag (default=check) mirrors the existing
--enable-tcmalloc behavior: auto-detected at configure time, supports
both static and dynamic linking, and is disabled when --enable-dev-asan
is active.
This commit is contained in:
Yangyu Chen
2026-03-13 17:08:15 -04:00
committed by GitHub
parent 7cd49a8028
commit bb5a9dc247
8 changed files with 78 additions and 12 deletions
+15
View File
@@ -57,6 +57,9 @@ VL_DEFINE_DEBUG_FUNCTIONS;
#ifdef HAVE_TCMALLOC
#include <gperftools/malloc_extension.h>
#endif
#ifdef HAVE_JEMALLOC
#include <jemalloc/jemalloc.h>
#endif
// clang-format off
#if defined(_WIN32) || defined(__MINGW32__)
@@ -413,6 +416,18 @@ void V3Os::releaseMemory() {
#ifdef HAVE_TCMALLOC
MallocExtension::instance()->ReleaseFreeMemory();
#endif
#ifdef HAVE_JEMALLOC
// Purge all unused dirty pages across all arenas
unsigned narenas = 0;
size_t sz = sizeof(narenas);
if (mallctl("arenas.narenas", &narenas, &sz, nullptr, 0)) {
return; // Failed to get number of arenas, give up
}
char buf[64];
// Index equal to narenas represents all arenas
VL_SNPRINTF(buf, sizeof(buf), "arena.%u.purge", narenas);
mallctl(buf, nullptr, nullptr, nullptr, 0);
#endif
}
//######################################################################