Merge pull request #783 from devnexen/dev_thp_disable

new Linux/Android option proposal.
This commit is contained in:
Daan 2024-03-02 16:49:34 -08:00 committed by GitHub
commit c0da600ed3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View File

@ -28,6 +28,7 @@ option(MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clan
option(MI_SKIP_COLLECT_ON_EXIT "Skip collecting memory on program exit" OFF)
option(MI_NO_PADDING "Force no use of padding even in DEBUG mode etc." OFF)
option(MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF)
option(MI_NO_THP "Force disable transparent huge pages support on Linux/Android process wise only" OFF)
# deprecated options
option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode (deprecated, use MI_DEBUG_FULL instead)" OFF)
@ -263,6 +264,13 @@ if(MI_USE_CXX)
endif()
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux|Android")
if(MI_NO_THP)
message(STATUS "Disable transparent huge pages support (MI_NO_THP=ON)")
list(APPEND mi_defines MI_NO_THP=1)
endif()
endif()
# On Haiku use `-DCMAKE_INSTALL_PREFIX` instead, issue #788
# if(CMAKE_SYSTEM_NAME MATCHES "Haiku")
# SET(CMAKE_INSTALL_LIBDIR ~/config/non-packaged/lib)

View File

@ -31,6 +31,7 @@ terms of the MIT license. A copy of the license can be found in the file
#if defined(__linux__)
#include <features.h>
#include <sys/prctl.h>
#if defined(__GLIBC__)
#include <linux/mman.h> // linux mmap flags
#else
@ -128,6 +129,20 @@ static bool unix_detect_overcommit(void) {
return os_overcommit;
}
void unix_set_thp(void) {
#if defined(__linux__) || defined(__ANDROID__)
#if MI_NO_THP
int val;
if (prctl(PR_GET_THP_DISABLE, &val, 0, 0, 0) != 0) {
// Most likely since distros often come with always/madvise settings.
val = 1;
// Disabling only for mimalloc process rather than touching system wide settings
(void)prctl(PR_SET_THP_DISABLE, &val, 0, 0, 0);
}
#endif
#endif
}
void _mi_prim_mem_init( mi_os_mem_config_t* config ) {
long psize = sysconf(_SC_PAGESIZE);
if (psize > 0) {
@ -138,6 +153,7 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) {
config->has_overcommit = unix_detect_overcommit();
config->must_free_whole = false; // mmap can free in parts
config->has_virtual_reserve = true; // todo: check if this true for NetBSD? (for anonymous mmap with PROT_NONE)
unix_set_thp();
}