From e6673e44c7646a2f6c3ab8c17c11dfb1dd94ac2d Mon Sep 17 00:00:00 2001 From: tporadowski Date: Thu, 2 Jul 2020 22:16:09 +0200 Subject: [PATCH] added jemalloc 5.2.1 customizations for Redis --- .../internal/jemalloc_internal_inlines_c.h | 28 +++++++++++++++++++ .../include/jemalloc/jemalloc_macros.h | 4 +++ .../include/jemalloc/jemalloc_macros.h.in | 4 +++ deps/jemalloc-5.2.1/src/jemalloc.c | 12 ++++++++ msvs/RedisServer.vcxproj | 4 +-- src/zmalloc.h | 2 +- 6 files changed, 51 insertions(+), 3 deletions(-) diff --git a/deps/jemalloc-5.2.1/include/jemalloc/internal/jemalloc_internal_inlines_c.h b/deps/jemalloc-5.2.1/include/jemalloc/internal/jemalloc_internal_inlines_c.h index cdb10eb21..17b2472ff 100644 --- a/deps/jemalloc-5.2.1/include/jemalloc/internal/jemalloc_internal_inlines_c.h +++ b/deps/jemalloc-5.2.1/include/jemalloc/internal/jemalloc_internal_inlines_c.h @@ -219,4 +219,32 @@ ixalloc(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size, size_t extra, newsize); } +JEMALLOC_ALWAYS_INLINE int +iget_defrag_hint(tsdn_t *tsdn, void* ptr, int *bin_util, int *run_util) { + int defrag = 0; + rtree_ctx_t rtree_ctx_fallback; + rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback); + szind_t szind; + bool is_slab; + rtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr, true, &szind, &is_slab); + if (likely(is_slab)) { + /* Small allocation. */ + extent_t *slab = iealloc(tsdn, ptr); + arena_t *arena = extent_arena_get(slab); + szind_t binind = extent_szind_get(slab); + bin_t *bin = &arena->bins[binind]; + malloc_mutex_lock(tsdn, &bin->lock); + /* don't bother moving allocations from the slab currently used for new allocations */ + if (slab != bin->slabcur) { + const bin_info_t *bin_info = &bin_infos[binind]; + size_t availregs = bin_info->nregs * bin->stats.curslabs; + *bin_util = ((long long)bin->stats.curregs<<16) / availregs; + *run_util = ((long long)(bin_info->nregs - extent_nfree_get(slab))<<16) / bin_info->nregs; + defrag = 1; + } + malloc_mutex_unlock(tsdn, &bin->lock); + } + return defrag; +} + #endif /* JEMALLOC_INTERNAL_INLINES_C_H */ diff --git a/deps/jemalloc-5.2.1/include/jemalloc/jemalloc_macros.h b/deps/jemalloc-5.2.1/include/jemalloc/jemalloc_macros.h index 342358942..26aa99084 100644 --- a/deps/jemalloc-5.2.1/include/jemalloc/jemalloc_macros.h +++ b/deps/jemalloc-5.2.1/include/jemalloc/jemalloc_macros.h @@ -127,3 +127,7 @@ # define JEMALLOC_RESTRICT_RETURN # define JEMALLOC_ALLOCATOR #endif + +/* This version of Jemalloc, modified for Redis, has the je_get_defrag_hint() + * function. */ +#define JEMALLOC_FRAG_HINT diff --git a/deps/jemalloc-5.2.1/include/jemalloc/jemalloc_macros.h.in b/deps/jemalloc-5.2.1/include/jemalloc/jemalloc_macros.h.in index 59e29558c..3421321a4 100644 --- a/deps/jemalloc-5.2.1/include/jemalloc/jemalloc_macros.h.in +++ b/deps/jemalloc-5.2.1/include/jemalloc/jemalloc_macros.h.in @@ -127,3 +127,7 @@ # define JEMALLOC_RESTRICT_RETURN # define JEMALLOC_ALLOCATOR #endif + +/* This version of Jemalloc, modified for Redis, has the je_get_defrag_hint() + * function. */ +#define JEMALLOC_FRAG_HINT diff --git a/deps/jemalloc-5.2.1/src/jemalloc.c b/deps/jemalloc-5.2.1/src/jemalloc.c index ed13718d4..3447ebe1c 100644 --- a/deps/jemalloc-5.2.1/src/jemalloc.c +++ b/deps/jemalloc-5.2.1/src/jemalloc.c @@ -3920,3 +3920,15 @@ jemalloc_postfork_child(void) { } /******************************************************************************/ + + +/* Helps the application decide if a pointer is worth re-allocating in order to reduce fragmentation. + * returns 0 if the allocation is in the currently active run, + * or when it is not causing any frag issue (large or huge bin) + * returns the bin utilization and run utilization both in fixed point 16:16. + * If the application decides to re-allocate it should use MALLOCX_TCACHE_NONE when doing so. */ +JEMALLOC_EXPORT int JEMALLOC_NOTHROW +je_get_defrag_hint(void* ptr, int *bin_util, int *run_util) { + assert(ptr != NULL); + return iget_defrag_hint(TSDN_NULL, ptr, bin_util, run_util); +} diff --git a/msvs/RedisServer.vcxproj b/msvs/RedisServer.vcxproj index 8bc88fba5..1ca7235f4 100644 --- a/msvs/RedisServer.vcxproj +++ b/msvs/RedisServer.vcxproj @@ -95,7 +95,7 @@ - USE_JEMALLOC;_OFF_T_DEFINED;WIN32;LACKS_STDLIB_H;_DEBUG;_CONSOLE;__x86_64__;%(PreprocessorDefinitions) + USE_JEMALLOC;JEMALLOC_FRAG_HINT;_OFF_T_DEFINED;WIN32;LACKS_STDLIB_H;_DEBUG;_CONSOLE;__x86_64__;%(PreprocessorDefinitions) $(SolutionDir)..\deps\lua\src;$(SolutionDir)..\deps\geohash-int;$(SolutionDir);..\deps\hiredis;..\deps\jemalloc-5.2.1\include;..\deps\jemalloc-5.2.1\include\msvc_compat MultiThreadedDebug Level3 @@ -143,7 +143,7 @@ copy /Y $(OutputPath)redis-server.pdb $(OutputPath)redis-check-aof.pdb - USE_JEMALLOC;_OFF_T_DEFINED;WIN32;LACKS_STDLIB_H;NDEBUG;_CONSOLE;__x86_64__;%(PreprocessorDefinitions) + USE_JEMALLOC;JEMALLOC_FRAG_HINT;_OFF_T_DEFINED;WIN32;LACKS_STDLIB_H;NDEBUG;_CONSOLE;__x86_64__;%(PreprocessorDefinitions) $(SolutionDir)..\deps\lua\src;$(SolutionDir)..\deps\geohash-int;$(SolutionDir);..\deps\hiredis;$(SolutionDir)..\deps\jemalloc-5.2.1\include;$(SolutionDir)..\deps\jemalloc-5.2.1\include\msvc_compat MultiThreaded Level3 diff --git a/src/zmalloc.h b/src/zmalloc.h index 47bac0651..1bcbb4aee 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -51,7 +51,7 @@ #endif #elif defined(USE_JEMALLOC) -#define ZMALLOC_LIB ("jemalloc-" __xstr(JEMALLOC_VERSION_MAJOR) "." __xstr(JEMALLOC_VERSION_MINOR) "." __xstr(JEMALLOC_VERSION_BUGFIX)) +#define ZMALLOC_LIB ("jemalloc-" __xstr(JEMALLOC_VERSION_MAJOR) "." __xstr(JEMALLOC_VERSION_MINOR) "." __xstr(JEMALLOC_VERSION_BUGFIX) "-redis") #include #if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2) #define HAVE_MALLOC_SIZE 1