From 336f83fbd1ca67bb6edba3a1c99e9b0230159336 Mon Sep 17 00:00:00 2001 From: Daan Date: Sat, 20 Apr 2024 16:09:45 -0700 Subject: [PATCH 1/4] use __builtin_thread_pointer on arm64 with older gcc compilers (issue #851) --- include/mimalloc/prim.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/include/mimalloc/prim.h b/include/mimalloc/prim.h index d14b885b..ebb31df2 100644 --- a/include/mimalloc/prim.h +++ b/include/mimalloc/prim.h @@ -203,12 +203,23 @@ static inline void mi_prim_tls_slot_set(size_t slot, void* value) mi_attr_noexce #endif +// Do we have __builtin_thread_pointer? (do not make this a compound test as it fails on older gcc's, see issue #851) +#if defined(__has_builtin) +#if __has_builtin(__builtin_thread_pointer) +#define MI_HAS_BUILTIN_THREAD_POINTER 1 +#endif +#elif defined(__GNUC__) && (__GNUC__ >= 7) && defined(__aarch64__) // special case aarch64 for older gcc versions (issue #851) +#define MI_HAS_BUILTIN_THREAD_POINTER 1 +#endif + + // defined in `init.c`; do not use these directly extern mi_decl_thread mi_heap_t* _mi_heap_default; // default heap to allocate from extern bool _mi_process_is_initialized; // has mi_process_init been called? static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept; +// Get a unique id for the current thread. #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN @@ -218,7 +229,7 @@ static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept { return (uintptr_t)NtCurrentTeb(); } -#elif defined(__has_builtin) && __has_builtin(__builtin_thread_pointer) && \ +#elif MI_HAS_BUILTIN_THREAD_POINTER && \ (!defined(__APPLE__)) && /* on apple (M1) the wrong register is read (tpidr_el0 instead of tpidrro_el0) so fall back to TLS slot assembly ()*/ \ (!defined(__clang_major__) || __clang_major__ >= 14) // older clang versions emit bad code; fall back to using the TLS slot () From e46c1145a510807e3b22b078ec70c1d38f4f9fbc Mon Sep 17 00:00:00 2001 From: Daan Date: Sat, 20 Apr 2024 16:19:59 -0700 Subject: [PATCH 2/4] add separate MI_LIBC_MUSL option (issue #644) --- CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c0f67af..a6c95dc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ option(MI_OSX_INTERPOSE "Use interpose to override standard malloc on macOS" option(MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" ON) option(MI_WIN_REDIRECT "Use redirection module ('mimalloc-redirect') on Windows if compiling mimalloc as a DLL" ON) option(MI_LOCAL_DYNAMIC_TLS "Use slightly slower, dlopen-compatible TLS mechanism (Unix)" OFF) +option(MI_LIBC_MUSL "Set this when linking with musl libc" OFF) option(MI_BUILD_SHARED "Build shared library" ON) option(MI_BUILD_STATIC "Build static library" ON) option(MI_BUILD_OBJECT "Build object library" ON) @@ -286,6 +287,12 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux|Android") endif() endif() +if(MI_LIBC_MUSL) + message(STATUS "Assume using musl libc (MI_LIBC_MUSL=ON) (this implies MI_LOCAL_DYNAMIC_TLS=ON)") + set(MI_LOCAL_DYNAMIC_TLS "ON") + list(APPEND mi_defines MI_LIBC_MUSL=1) +endif() + # On Haiku use `-DCMAKE_INSTALL_PREFIX` instead, issue #788 # if(CMAKE_SYSTEM_NAME MATCHES "Haiku") # SET(CMAKE_INSTALL_LIBDIR ~/config/non-packaged/lib) From 79ab7c63d7e86a063f547b72549a05c73e03b841 Mon Sep 17 00:00:00 2001 From: Daan Date: Sat, 20 Apr 2024 16:37:09 -0700 Subject: [PATCH 3/4] disable transparent huge pages for a process too if the allow_large_os_pages option is set to false --- src/prim/unix/prim.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index 9c4ecd4b..a7812cb6 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -148,13 +148,20 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) config->has_virtual_reserve = true; // todo: check if this true for NetBSD? (for anonymous mmap with PROT_NONE) // 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); + #if (defined(__linux__) || defined(__ANDROID__)) && defined(PR_GET_THP_DISABLE) + #if defined(MI_NO_THP) + if (true) + #else + if (!mi_option_is_enabled(mi_option_allow_large_os_pages)) // disable THP also if large OS pages are not allowed in the options + #endif + { + 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 } From 7247b9e3269af797d3fc05efd6da6f0d73c81ee8 Mon Sep 17 00:00:00 2001 From: Daan Date: Sat, 20 Apr 2024 16:45:49 -0700 Subject: [PATCH 4/4] allow configuring page and segment sizes (pr #753 and pr #862) --- include/mimalloc/types.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/mimalloc/types.h b/include/mimalloc/types.h index 5d219d68..761c7278 100644 --- a/include/mimalloc/types.h +++ b/include/mimalloc/types.h @@ -160,10 +160,18 @@ typedef int32_t mi_ssize_t; // Main tuning parameters for segment and page sizes // Sizes for 64-bit, divide by two for 32-bit +#ifndef MI_SMALL_PAGE_SHIFT #define MI_SMALL_PAGE_SHIFT (13 + MI_INTPTR_SHIFT) // 64KiB +#endif +#ifndef MI_MEDIUM_PAGE_SHIFT #define MI_MEDIUM_PAGE_SHIFT ( 3 + MI_SMALL_PAGE_SHIFT) // 512KiB +#endif +#ifndef MI_LARGE_PAGE_SHIFT #define MI_LARGE_PAGE_SHIFT ( 3 + MI_MEDIUM_PAGE_SHIFT) // 4MiB +#endif +#ifndef MI_SEGMENT_SHIFT #define MI_SEGMENT_SHIFT ( MI_LARGE_PAGE_SHIFT) // 4MiB -- must be equal to `MI_LARGE_PAGE_SHIFT` +#endif // Derived constants #define MI_SEGMENT_SIZE (MI_ZU(1)<