diff --git a/musl_config.gni b/musl_config.gni index 55614278364d6870693648a41c241fdbd414aa09..3480888bbf41aaadfd8629de13fb49f4013f45e5 100644 --- a/musl_config.gni +++ b/musl_config.gni @@ -36,6 +36,7 @@ declare_args() { user_custom_libc = true use_jemalloc = false use_jemalloc_dfx_intf = false + use_jemalloc_recycle_func = false use_pthread_cancel = false musl_ported_dir = "intermidiates/${musl_target_os}/musl_src_ported" musl_inc_out_dir = "usr/include/${musl_target_triple}" diff --git a/musl_template.gni b/musl_template.gni index 1f430a833833dd2d78c0b250958275feea036c36..fa02c85973ecfb98d853cececbd4d123fda06328 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -584,6 +584,9 @@ template("musl_libs") { if (use_jemalloc_dfx_intf) { defines += [ "USE_JEMALLOC_DFX_INTF" ] } + if (use_jemalloc_recycle_func) { + defines += [ "USE_JEMALLOC_RECYCLE_FUNC" ] + } include_dirs = [ "//third_party/jemalloc/include/jemalloc" ] } @@ -797,6 +800,9 @@ template("musl_libs") { if (use_jemalloc_dfx_intf) { defines += [ "USE_JEMALLOC_DFX_INTF" ] } + if (use_jemalloc_recycle_func) { + defines += [ "USE_JEMALLOC_RECYCLE_FUNC" ] + } include_dirs = [ "//third_party/jemalloc/include/jemalloc" ] } diff --git a/porting/linux/user/include/signal.h b/porting/linux/user/include/signal.h index a330d38646ad1033043fbccb53c74e2fa3172326..a7d9452e3d7cd7c267a01a5496619f5c885412ce 100644 --- a/porting/linux/user/include/signal.h +++ b/porting/linux/user/include/signal.h @@ -217,6 +217,9 @@ int __libc_current_sigrtmax(void); #define MUSL_SIGNAL_NATIVE_LOCAL (SIGRTMIN + 3) #define MUSL_SIGNAL_JSHEAP (SIGRTMIN + 4) #define MUSL_SIGNAL_JSHEAP_PRIV (SIGRTMIN + 5) +#ifdef USE_JEMALLOC_RECYCLE_FUNC +#define MUSL_SIGNAL_RECYCLE_JEMALLOC(SIGRTMIN + 8) +#endif #define MUSL_SIGNAL_MEMCHECK (SIGRTMIN + 9) int kill(pid_t, int); diff --git a/porting/linux/user/src/hook/musl_preinit.c b/porting/linux/user/src/hook/musl_preinit.c index d03d5db8c5185b9852a4056c2831cd799d98407d..0b69f39318d6ec72302578e1ef53c2c9dbe0b1fc 100644 --- a/porting/linux/user/src/hook/musl_preinit.c +++ b/porting/linux/user/src/hook/musl_preinit.c @@ -271,6 +271,28 @@ static bool init_prctl_function(void* malloc_shared_library_handler, MallocPrctl return true; } +#ifdef USE_JEMALLOC_RECYCLE_FUNC +extern int je_reclaim_cache(); +static void handle_recycle_cache() +{ + int ret; + ret = je_reclaim_cache(); + MUSL_LOGD("je_reclaim_cache result=%{public}d", ret); +} + +static void init_jemalloc_recycle_handler() +{ + struct sigaction action = {}; + action.sa_handler = NULL; + action.sa_sigaction = handle_recycle_cache; + sigemptyset(&action.sa_mask); + sigaddset(&action.sa_mask, MUSL_SIGNAL_RECYCLE_JEMALLOC); + action.sa_flags = SA_SIGINFO | SA_RESTART; + action.sa_restorer = NULL; + sigaction(MUSL_SIGNAL_RECYCLE_JEMALLOC, &action, NULL); + +} +#endif static bool init_hook_functions(void* shared_library_handler, struct MallocDispatchType* table, const char* prefix) { @@ -626,6 +648,9 @@ static void __install_malloc_hook_signal_handler() static void __initialize_malloc() { __install_malloc_hook_signal_handler(); +#ifdef USE_JEMALLOC_RECYCLE_FUNC + init_jemalloc_recycle_handler(); +#endif } __attribute__((constructor(1))) static void __musl_initialize()