From e4c9a90fe9aa702d2e1f7d932fc29a9c9f23fd4a Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 30 Mar 2017 09:37:47 +0200 Subject: [PATCH] util: Work around (virtual) memory exhaustion on 32-bit w/ glibc glibc-specific: On 32-bit systems set the number of arenas to 1. By default, since glibc 2.10, the C library will create up to two heap arenas per core. This is known to cause excessive virtual address space usage in our usage. Work around it by setting the maximum number of arenas to 1. Github-Pull: #10120 Rebased-From: 625488ace5c41ccba1b68323a72588000782f820 --- configure.ac | 8 ++++++++ src/util.cpp | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/configure.ac b/configure.ac index a2332578a..da42a0a16 100644 --- a/configure.ac +++ b/configure.ac @@ -557,6 +557,14 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], [ AC_MSG_RESULT(no)] ) +dnl Check for mallopt(M_ARENA_MAX) (to set glibc arenas) +AC_MSG_CHECKING(for mallopt M_ARENA_MAX) +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[ mallopt(M_ARENA_MAX, 1); ]])], + [ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_MALLOPT_ARENA_MAX, 1,[Define this symbol if you have mallopt with M_ARENA_MAX]) ], + [ AC_MSG_RESULT(no)] +) + AC_MSG_CHECKING([for visibility attribute]) AC_LINK_IFELSE([AC_LANG_SOURCE([ int foo_def( void ) __attribute__((visibility("default"))); diff --git a/src/util.cpp b/src/util.cpp index 78c353dfe..6f812d910 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -72,6 +72,10 @@ #include #endif +#ifdef HAVE_MALLOPT_ARENA_MAX +#include +#endif + #include // for to_lower() #include #include // for startswith() and endswith() @@ -792,6 +796,16 @@ void RenameThread(const char* name) void SetupEnvironment() { +#ifdef HAVE_MALLOPT_ARENA_MAX + // glibc-specific: On 32-bit systems set the number of arenas to 1. + // By default, since glibc 2.10, the C library will create up to two heap + // arenas per core. This is known to cause excessive virtual address space + // usage in our usage. Work around it by setting the maximum number of + // arenas to 1. + if (sizeof(void*) == 4) { + mallopt(M_ARENA_MAX, 1); + } +#endif // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale // may be invalid, in which case the "C" locale is used as fallback. #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)