2323#ifndef UMF_POOL_JEMALLOC_ENABLED
2424
2525umf_memory_pool_ops_t * umfJemallocPoolOps (void ) { return NULL ; }
26+ umf_result_t
27+ umfJemallocPoolParamsCreate (umf_jemalloc_pool_params_handle_t * hParams ) {
28+ return UMF_RESULT_ERROR_NOT_SUPPORTED ;
29+ }
30+
31+ umf_result_t
32+ umfJemallocPoolParamsDestroy (umf_jemalloc_pool_params_handle_t hParams ) {
33+ return UMF_RESULT_ERROR_NOT_SUPPORTED ;
34+ }
35+
36+ umf_result_t
37+ umfJemallocPoolParamsSetNumArenas (umf_jemalloc_pool_params_handle_t hParams ,
38+ size_t numArenas ) {
39+ return UMF_RESULT_ERROR_NOT_SUPPORTED ;
40+ }
2641
2742#else
2843
2944#include <jemalloc/jemalloc.h>
3045
3146#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)
3247
48+ typedef struct umf_jemalloc_pool_params_t {
49+ size_t n_arenas ;
50+ } umf_jemalloc_pool_params_t ;
51+
3352typedef struct jemalloc_memory_pool_t {
3453 umf_memory_provider_handle_t provider ;
35- unsigned int arena_index ; // index of jemalloc arena
54+ size_t n_arenas ;
55+ unsigned int arena_index [];
3656} jemalloc_memory_pool_t ;
3757
3858static __TLS umf_result_t TLS_last_allocation_error ;
@@ -47,6 +67,14 @@ static jemalloc_memory_pool_t *get_pool_by_arena_index(unsigned arena_ind) {
4767 return pool_by_arena_index [arena_ind ];
4868}
4969
70+ // SplitMix64 hash
71+ static uint64_t hash64 (uint64_t x ) {
72+ x += 0x9e3779b97f4a7c15 ;
73+ x = (x ^ (x >> 30 )) * 0xbf58476d1ce4e5b9 ;
74+ x = (x ^ (x >> 27 )) * 0x94d049bb133111eb ;
75+ return x ^ (x >> 31 );
76+ }
77+
5078// arena_extent_alloc - an extent allocation function conforms to the extent_alloc_t type and upon
5179// success returns a pointer to size bytes of mapped memory on behalf of arena arena_ind such that
5280// the extent's base address is a multiple of alignment, as well as setting *zero to indicate
@@ -285,12 +313,22 @@ static extent_hooks_t arena_extent_hooks = {
285313 .merge = arena_extent_merge ,
286314};
287315
316+ static unsigned get_arena_index (jemalloc_memory_pool_t * pool ) {
317+ static __TLS unsigned tid = 0 ;
318+
319+ if (tid == 0 ) {
320+ tid = utils_gettid ();
321+ }
322+
323+ return pool -> arena_index [hash64 (tid ) % pool -> n_arenas ];
324+ }
325+
288326static void * op_malloc (void * pool , size_t size ) {
289327 assert (pool );
290328 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
291329 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
292330 // the tcache, so we wouldn't be able to guarantee isolation of different providers.
293- int flags = MALLOCX_ARENA (je_pool -> arena_index ) | MALLOCX_TCACHE_NONE ;
331+ int flags = MALLOCX_ARENA (get_arena_index ( je_pool ) ) | MALLOCX_TCACHE_NONE ;
294332 void * ptr = je_mallocx (size , flags );
295333 if (ptr == NULL ) {
296334 TLS_last_allocation_error = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
@@ -343,7 +381,7 @@ static void *op_realloc(void *pool, void *ptr, size_t size) {
343381 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
344382 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
345383 // the tcache, so we wouldn't be able to guarantee isolation of different providers.
346- int flags = MALLOCX_ARENA (je_pool -> arena_index ) | MALLOCX_TCACHE_NONE ;
384+ int flags = MALLOCX_ARENA (get_arena_index ( je_pool ) ) | MALLOCX_TCACHE_NONE ;
347385 void * new_ptr = je_rallocx (ptr , size , flags );
348386 if (new_ptr == NULL ) {
349387 TLS_last_allocation_error = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
@@ -364,7 +402,8 @@ static void *op_realloc(void *pool, void *ptr, size_t size) {
364402static void * op_aligned_alloc (void * pool , size_t size , size_t alignment ) {
365403 assert (pool );
366404 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
367- unsigned arena = je_pool -> arena_index ;
405+
406+ unsigned arena = get_arena_index (je_pool );
368407 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
369408 // the tcache, so we wouldn't be able to guarantee isolation of different providers.
370409 int flags =
@@ -382,62 +421,91 @@ static void *op_aligned_alloc(void *pool, size_t size, size_t alignment) {
382421
383422static umf_result_t op_initialize (umf_memory_provider_handle_t provider ,
384423 void * params , void * * out_pool ) {
385- (void )params ; // unused
386424 assert (provider );
387425 assert (out_pool );
388426
389427 extent_hooks_t * pHooks = & arena_extent_hooks ;
390428 size_t unsigned_size = sizeof (unsigned );
391429 int err ;
430+ umf_jemalloc_pool_params_t * jemalloc_params =
431+ (umf_jemalloc_pool_params_t * )params ;
392432
393- jemalloc_memory_pool_t * pool =
394- umf_ba_global_alloc (sizeof (jemalloc_memory_pool_t ));
395- if (!pool ) {
396- return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
433+ size_t n_arenas = 0 ;
434+ if (jemalloc_params ) {
435+ n_arenas = jemalloc_params -> n_arenas ;
397436 }
398437
399- pool -> provider = provider ;
400-
401- unsigned arena_index ;
402- err = je_mallctl ("arenas.create" , (void * )& arena_index , & unsigned_size ,
403- NULL , 0 );
404- if (err ) {
405- LOG_ERR ("Could not create arena." );
406- goto err_free_pool ;
438+ if (n_arenas == 0 ) {
439+ n_arenas = utils_get_num_cores () * 4 ;
407440 }
408-
409- // setup extent_hooks for newly created arena
410- char cmd [64 ];
411- snprintf (cmd , sizeof (cmd ), "arena.%u.extent_hooks" , arena_index );
412- err = je_mallctl (cmd , NULL , NULL , (void * )& pHooks , sizeof (void * ));
413- if (err ) {
414- snprintf (cmd , sizeof (cmd ), "arena.%u.destroy" , arena_index );
415- (void )je_mallctl (cmd , NULL , 0 , NULL , 0 );
416- LOG_ERR ("Could not setup extent_hooks for newly created arena." );
417- goto err_free_pool ;
441+ if (n_arenas > MALLOCX_ARENA_MAX ) {
442+ LOG_ERR ("Number of arenas exceeds the limit." );
443+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
418444 }
419445
420- pool -> arena_index = arena_index ;
421- pool_by_arena_index [arena_index ] = pool ;
446+ jemalloc_memory_pool_t * pool = umf_ba_global_alloc (
447+ sizeof (* pool ) + n_arenas * sizeof (* pool -> arena_index ));
448+ if (!pool ) {
449+ return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
450+ }
422451
452+ pool -> provider = provider ;
453+ pool -> n_arenas = n_arenas ;
454+
455+ size_t num_created = 0 ;
456+ for (size_t i = 0 ; i < n_arenas ; i ++ ) {
457+ unsigned arena_index ;
458+ err = je_mallctl ("arenas.create" , (void * )& arena_index , & unsigned_size ,
459+ NULL , 0 );
460+ if (err ) {
461+ LOG_ERR ("Could not create arena." );
462+ goto err_cleanup ;
463+ }
464+
465+ pool -> arena_index [num_created ++ ] = arena_index ;
466+ if (arena_index >= MALLOCX_ARENA_MAX ) {
467+ LOG_ERR ("Number of arenas exceeds the limit." );
468+ goto err_cleanup ;
469+ }
470+
471+ pool_by_arena_index [arena_index ] = pool ;
472+
473+ // Setup extent_hooks for the newly created arena.
474+ char cmd [64 ];
475+ snprintf (cmd , sizeof (cmd ), "arena.%u.extent_hooks" , arena_index );
476+ err = je_mallctl (cmd , NULL , NULL , (void * )& pHooks , sizeof (void * ));
477+ if (err ) {
478+ LOG_ERR ("Could not setup extent_hooks for newly created arena." );
479+ goto err_cleanup ;
480+ }
481+ }
423482 * out_pool = (umf_memory_pool_handle_t )pool ;
424483
425484 VALGRIND_DO_CREATE_MEMPOOL (pool , 0 , 0 );
426485
427486 return UMF_RESULT_SUCCESS ;
428487
429- err_free_pool :
488+ err_cleanup :
489+ // Destroy any arenas that were successfully created.
490+ for (size_t i = 0 ; i < num_created ; i ++ ) {
491+ char cmd [64 ];
492+ unsigned arena = pool -> arena_index [i ];
493+ snprintf (cmd , sizeof (cmd ), "arena.%u.destroy" , arena );
494+ (void )je_mallctl (cmd , NULL , 0 , NULL , 0 );
495+ }
430496 umf_ba_global_free (pool );
431497 return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
432498}
433499
434500static void op_finalize (void * pool ) {
435501 assert (pool );
436502 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
437- char cmd [64 ];
438- snprintf (cmd , sizeof (cmd ), "arena.%u.destroy" , je_pool -> arena_index );
439- (void )je_mallctl (cmd , NULL , 0 , NULL , 0 );
440- pool_by_arena_index [je_pool -> arena_index ] = NULL ;
503+ for (size_t i = 0 ; i < je_pool -> n_arenas ; i ++ ) {
504+ char cmd [64 ];
505+ unsigned arena = je_pool -> arena_index [i ];
506+ snprintf (cmd , sizeof (cmd ), "arena.%u.destroy" , arena );
507+ (void )je_mallctl (cmd , NULL , 0 , NULL , 0 );
508+ }
441509 umf_ba_global_free (je_pool );
442510
443511 VALGRIND_DO_DESTROY_MEMPOOL (pool );
@@ -469,4 +537,33 @@ static umf_memory_pool_ops_t UMF_JEMALLOC_POOL_OPS = {
469537umf_memory_pool_ops_t * umfJemallocPoolOps (void ) {
470538 return & UMF_JEMALLOC_POOL_OPS ;
471539}
540+
541+ umf_result_t
542+ umfJemallocPoolParamsCreate (umf_jemalloc_pool_params_handle_t * hParams ) {
543+ umf_jemalloc_pool_params_t * params = umf_ba_global_alloc (sizeof (* params ));
544+ if (!params ) {
545+ return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
546+ }
547+ memset (params , 0 , sizeof (* params ));
548+ * hParams = params ;
549+ return UMF_RESULT_SUCCESS ;
550+ }
551+
552+ umf_result_t
553+ umfJemallocPoolParamsDestroy (umf_jemalloc_pool_params_handle_t hParams ) {
554+ umf_ba_global_free (hParams );
555+ return UMF_RESULT_SUCCESS ;
556+ }
557+
558+ umf_result_t
559+ umfJemallocPoolParamsSetNumArenas (umf_jemalloc_pool_params_handle_t hParams ,
560+ size_t numArenas ) {
561+ if (!hParams ) {
562+ LOG_ERR ("jemalloc pool params handle is NULL" );
563+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
564+ }
565+ hParams -> n_arenas = numArenas ;
566+ return UMF_RESULT_SUCCESS ;
567+ }
568+
472569#endif /* UMF_POOL_JEMALLOC_ENABLED */
0 commit comments