From 128c7c1876f69812bfe497f2a3f49b5fbce9e0f8 Mon Sep 17 00:00:00 2001 From: Daan Date: Sat, 2 Mar 2024 16:55:13 -0800 Subject: [PATCH] cleanup thp disable a bit --- CMakeLists.txt | 2 +- src/prim/unix/prim.c | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b5464226..af00e2ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +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) +option(MI_NO_THP "Disable transparent huge pages support on Linux/Android for the mimalloc process only" OFF) # deprecated options option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode (deprecated, use MI_DEBUG_FULL instead)" OFF) diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index 33b83322..2035e1a4 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -31,7 +31,9 @@ terms of the MIT license. A copy of the license can be found in the file #if defined(__linux__) #include + #if defined(MI_NO_THP) #include + #endif #if defined(__GLIBC__) #include // linux mmap flags #else @@ -129,21 +131,8 @@ 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 ) { +void _mi_prim_mem_init( mi_os_mem_config_t* config ) +{ long psize = sysconf(_SC_PAGESIZE); if (psize > 0) { config->page_size = (size_t)psize; @@ -153,7 +142,17 @@ 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(); + + // disable transparent huge pages for this process? + #if defined(MI_NO_THP) && (defined(__linux__) || defined(__ANDROID__)) + int val = 0; + 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 }