@@ -30,9 +30,14 @@ umf_memory_pool_ops_t *umfJemallocPoolOps(void) { return NULL; }
3030
3131#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)
3232
33+ typedef struct umf_jemalloc_pool_params_t {
34+ size_t numArenas ;
35+ } umf_jemalloc_pool_params_t ;
36+
3337typedef struct jemalloc_memory_pool_t {
3438 umf_memory_provider_handle_t provider ;
35- unsigned int arena_index ; // index of jemalloc arena
39+ size_t n_arenas ;
40+ unsigned int arena_index [];
3641} jemalloc_memory_pool_t ;
3742
3843static __TLS umf_result_t TLS_last_allocation_error ;
@@ -47,6 +52,14 @@ static jemalloc_memory_pool_t *get_pool_by_arena_index(unsigned arena_ind) {
4752 return pool_by_arena_index [arena_ind ];
4853}
4954
55+ // SplitMix64 hash
56+ static uint64_t hash64 (uint64_t x ) {
57+ x += 0x9e3779b97f4a7c15 ;
58+ x = (x ^ (x >> 30 )) * 0xbf58476d1ce4e5b9 ;
59+ x = (x ^ (x >> 27 )) * 0x94d049bb133111eb ;
60+ return x ^ (x >> 31 );
61+ }
62+
5063// arena_extent_alloc - an extent allocation function conforms to the extent_alloc_t type and upon
5164// success returns a pointer to size bytes of mapped memory on behalf of arena arena_ind such that
5265// the extent's base address is a multiple of alignment, as well as setting *zero to indicate
@@ -285,12 +298,17 @@ static extent_hooks_t arena_extent_hooks = {
285298 .merge = arena_extent_merge ,
286299};
287300
301+ static unsigned get_arena_index (jemalloc_memory_pool_t * pool ) {
302+ unsigned pid = utils_getpid ();
303+ return pool -> arena_index [hash64 (pid ) % pool -> n_arenas ];
304+ }
305+
288306static void * op_malloc (void * pool , size_t size ) {
289307 assert (pool );
290308 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
291309 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
292310 // 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 ;
311+ int flags = MALLOCX_ARENA (get_arena_index ( je_pool ) ) | MALLOCX_TCACHE_NONE ;
294312 void * ptr = je_mallocx (size , flags );
295313 if (ptr == NULL ) {
296314 TLS_last_allocation_error = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
@@ -343,7 +361,7 @@ static void *op_realloc(void *pool, void *ptr, size_t size) {
343361 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
344362 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
345363 // 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 ;
364+ int flags = MALLOCX_ARENA (get_arena_index ( je_pool ) ) | MALLOCX_TCACHE_NONE ;
347365 void * new_ptr = je_rallocx (ptr , size , flags );
348366 if (new_ptr == NULL ) {
349367 TLS_last_allocation_error = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
@@ -364,7 +382,8 @@ static void *op_realloc(void *pool, void *ptr, size_t size) {
364382static void * op_aligned_alloc (void * pool , size_t size , size_t alignment ) {
365383 assert (pool );
366384 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
367- unsigned arena = je_pool -> arena_index ;
385+
386+ unsigned arena = get_arena_index (je_pool );
368387 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
369388 // the tcache, so we wouldn't be able to guarantee isolation of different providers.
370389 int flags =
@@ -382,62 +401,85 @@ static void *op_aligned_alloc(void *pool, size_t size, size_t alignment) {
382401
383402static umf_result_t op_initialize (umf_memory_provider_handle_t provider ,
384403 void * params , void * * out_pool ) {
385- (void )params ; // unused
386404 assert (provider );
387405 assert (out_pool );
388406
389407 extent_hooks_t * pHooks = & arena_extent_hooks ;
390408 size_t unsigned_size = sizeof (unsigned );
391409 int err ;
410+ umf_jemalloc_pool_params_t * jemalloc_params =
411+ (umf_jemalloc_pool_params_t * )params ;
392412
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 ;
413+ size_t nArenas = 0 ;
414+ if (jemalloc_params ) {
415+ nArenas = jemalloc_params -> numArenas ;
397416 }
398417
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 ;
418+ if (nArenas == 0 ) {
419+ nArenas = utils_get_num_cores () * 4 ;
407420 }
408421
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 ;
422+ jemalloc_memory_pool_t * pool = umf_ba_global_alloc (
423+ sizeof (* pool ) + nArenas * sizeof (* pool -> arena_index ));
424+ if (!pool ) {
425+ return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
418426 }
419427
420- pool -> arena_index = arena_index ;
421- pool_by_arena_index [arena_index ] = pool ;
422-
428+ pool -> provider = provider ;
429+ pool -> n_arenas = nArenas ;
430+
431+ size_t num_created = 0 ;
432+ for (size_t i = 0 ; i < nArenas ; i ++ ) {
433+ unsigned arena_index ;
434+ err = je_mallctl ("arenas.create" , (void * )& arena_index , & unsigned_size ,
435+ NULL , 0 );
436+ if (err ) {
437+ LOG_ERR ("Could not create arena." );
438+ goto err_cleanup ;
439+ }
440+
441+ // Setup extent_hooks for the newly created arena.
442+ char cmd [64 ];
443+ snprintf (cmd , sizeof (cmd ), "arena.%u.extent_hooks" , arena_index );
444+ err = je_mallctl (cmd , NULL , NULL , (void * )& pHooks , sizeof (void * ));
445+ if (err ) {
446+ LOG_ERR ("Could not setup extent_hooks for newly created arena." );
447+ goto err_cleanup ;
448+ }
449+
450+ pool -> arena_index [num_created ] = arena_index ;
451+ pool_by_arena_index [arena_index ] = pool ;
452+ num_created ++ ;
453+ }
423454 * out_pool = (umf_memory_pool_handle_t )pool ;
424455
425456 VALGRIND_DO_CREATE_MEMPOOL (pool , 0 , 0 );
426457
427458 return UMF_RESULT_SUCCESS ;
428459
429- err_free_pool :
460+ err_cleanup :
461+ // Destroy any arenas that were successfully created.
462+ for (size_t i = 0 ; i < num_created ; i ++ ) {
463+ char cmd [64 ];
464+ unsigned arena = pool -> arena_index [i ];
465+ snprintf (cmd , sizeof (cmd ), "arena.%u.destroy" , arena );
466+ (void )je_mallctl (cmd , NULL , 0 , NULL , 0 );
467+ pool_by_arena_index [arena ] = NULL ;
468+ }
430469 umf_ba_global_free (pool );
431470 return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
432471}
433472
434473static void op_finalize (void * pool ) {
435474 assert (pool );
436475 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 ;
476+ for (size_t i = 0 ; i < je_pool -> n_arenas ; i ++ ) {
477+ char cmd [64 ];
478+ unsigned arena = je_pool -> arena_index [i ];
479+ snprintf (cmd , sizeof (cmd ), "arena.%u.destroy" , arena );
480+ (void )je_mallctl (cmd , NULL , 0 , NULL , 0 );
481+ pool_by_arena_index [arena ] = NULL ;
482+ }
441483 umf_ba_global_free (je_pool );
442484
443485 VALGRIND_DO_DESTROY_MEMPOOL (pool );
@@ -469,4 +511,32 @@ static umf_memory_pool_ops_t UMF_JEMALLOC_POOL_OPS = {
469511umf_memory_pool_ops_t * umfJemallocPoolOps (void ) {
470512 return & UMF_JEMALLOC_POOL_OPS ;
471513}
514+
515+ umf_result_t
516+ umfJemallocPoolParamsCreate (umf_jemalloc_pool_params_handle_t * hParams ) {
517+ umf_jemalloc_pool_params_t * params = umf_ba_global_alloc (sizeof (* params ));
518+ if (!params ) {
519+ return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
520+ }
521+ * hParams = params ;
522+ return UMF_RESULT_SUCCESS ;
523+ }
524+
525+ umf_result_t
526+ umfJemallocPoolParamsDestroy (umf_jemalloc_pool_params_handle_t hParams ) {
527+ umf_ba_global_free (hParams );
528+ return UMF_RESULT_SUCCESS ;
529+ }
530+
531+ umf_result_t
532+ umfJemallocPoolParamsSetNumArenas (umf_jemalloc_pool_params_handle_t hParams ,
533+ size_t numArenas ) {
534+ if (!hParams ) {
535+ LOG_ERR ("jemalloc pool params handle is NULL" );
536+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
537+ }
538+ hParams -> numArenas = numArenas ;
539+ return UMF_RESULT_SUCCESS ;
540+ }
541+
472542#endif /* UMF_POOL_JEMALLOC_ENABLED */
0 commit comments