@@ -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 n_arenas ;
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,22 @@ 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+ static __TLS unsigned tid = 0 ;
303+
304+ if (tid == 0 ) {
305+ tid = utils_gettid ();
306+ }
307+
308+ return pool -> arena_index [hash64 (tid ) % pool -> n_arenas ];
309+ }
310+
288311static void * op_malloc (void * pool , size_t size ) {
289312 assert (pool );
290313 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
291314 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
292315 // 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 ;
316+ int flags = MALLOCX_ARENA (get_arena_index ( je_pool ) ) | MALLOCX_TCACHE_NONE ;
294317 void * ptr = je_mallocx (size , flags );
295318 if (ptr == NULL ) {
296319 TLS_last_allocation_error = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
@@ -343,7 +366,7 @@ static void *op_realloc(void *pool, void *ptr, size_t size) {
343366 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
344367 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
345368 // 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 ;
369+ int flags = MALLOCX_ARENA (get_arena_index ( je_pool ) ) | MALLOCX_TCACHE_NONE ;
347370 void * new_ptr = je_rallocx (ptr , size , flags );
348371 if (new_ptr == NULL ) {
349372 TLS_last_allocation_error = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
@@ -364,7 +387,8 @@ static void *op_realloc(void *pool, void *ptr, size_t size) {
364387static void * op_aligned_alloc (void * pool , size_t size , size_t alignment ) {
365388 assert (pool );
366389 jemalloc_memory_pool_t * je_pool = (jemalloc_memory_pool_t * )pool ;
367- unsigned arena = je_pool -> arena_index ;
390+
391+ unsigned arena = get_arena_index (je_pool );
368392 // MALLOCX_TCACHE_NONE is set, because jemalloc can mix objects from different arenas inside
369393 // the tcache, so we wouldn't be able to guarantee isolation of different providers.
370394 int flags =
@@ -382,62 +406,91 @@ static void *op_aligned_alloc(void *pool, size_t size, size_t alignment) {
382406
383407static umf_result_t op_initialize (umf_memory_provider_handle_t provider ,
384408 void * params , void * * out_pool ) {
385- (void )params ; // unused
386409 assert (provider );
387410 assert (out_pool );
388411
389412 extent_hooks_t * pHooks = & arena_extent_hooks ;
390413 size_t unsigned_size = sizeof (unsigned );
391414 int err ;
415+ umf_jemalloc_pool_params_t * jemalloc_params =
416+ (umf_jemalloc_pool_params_t * )params ;
392417
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 ;
418+ size_t n_arenas = 0 ;
419+ if (jemalloc_params ) {
420+ n_arenas = jemalloc_params -> n_arenas ;
397421 }
398422
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 ;
423+ if (n_arenas == 0 ) {
424+ n_arenas = utils_get_num_cores () * 4 ;
407425 }
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 ;
426+ if (n_arenas > MALLOCX_ARENA_MAX ) {
427+ LOG_ERR ("Number of arenas exceeds the limit." );
428+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
418429 }
419430
420- pool -> arena_index = arena_index ;
421- pool_by_arena_index [arena_index ] = pool ;
431+ jemalloc_memory_pool_t * pool = umf_ba_global_alloc (
432+ sizeof (* pool ) + n_arenas * sizeof (* pool -> arena_index ));
433+ if (!pool ) {
434+ return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
435+ }
422436
437+ pool -> provider = provider ;
438+ pool -> n_arenas = n_arenas ;
439+
440+ size_t num_created = 0 ;
441+ for (size_t i = 0 ; i < n_arenas ; i ++ ) {
442+ unsigned arena_index ;
443+ err = je_mallctl ("arenas.create" , (void * )& arena_index , & unsigned_size ,
444+ NULL , 0 );
445+ if (err ) {
446+ LOG_ERR ("Could not create arena." );
447+ goto err_cleanup ;
448+ }
449+
450+ pool -> arena_index [num_created ++ ] = arena_index ;
451+ if (arena_index >= MALLOCX_ARENA_MAX ) {
452+ LOG_ERR ("Number of arenas exceeds the limit." );
453+ goto err_cleanup ;
454+ }
455+
456+ pool_by_arena_index [arena_index ] = pool ;
457+
458+ // Setup extent_hooks for the newly created arena.
459+ char cmd [64 ];
460+ snprintf (cmd , sizeof (cmd ), "arena.%u.extent_hooks" , arena_index );
461+ err = je_mallctl (cmd , NULL , NULL , (void * )& pHooks , sizeof (void * ));
462+ if (err ) {
463+ LOG_ERR ("Could not setup extent_hooks for newly created arena." );
464+ goto err_cleanup ;
465+ }
466+ }
423467 * out_pool = (umf_memory_pool_handle_t )pool ;
424468
425469 VALGRIND_DO_CREATE_MEMPOOL (pool , 0 , 0 );
426470
427471 return UMF_RESULT_SUCCESS ;
428472
429- err_free_pool :
473+ err_cleanup :
474+ // Destroy any arenas that were successfully created.
475+ for (size_t i = 0 ; i < num_created ; i ++ ) {
476+ char cmd [64 ];
477+ unsigned arena = pool -> arena_index [i ];
478+ snprintf (cmd , sizeof (cmd ), "arena.%u.destroy" , arena );
479+ (void )je_mallctl (cmd , NULL , 0 , NULL , 0 );
480+ }
430481 umf_ba_global_free (pool );
431482 return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
432483}
433484
434485static void op_finalize (void * pool ) {
435486 assert (pool );
436487 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 ;
488+ for (size_t i = 0 ; i < je_pool -> n_arenas ; i ++ ) {
489+ char cmd [64 ];
490+ unsigned arena = je_pool -> arena_index [i ];
491+ snprintf (cmd , sizeof (cmd ), "arena.%u.destroy" , arena );
492+ (void )je_mallctl (cmd , NULL , 0 , NULL , 0 );
493+ }
441494 umf_ba_global_free (je_pool );
442495
443496 VALGRIND_DO_DESTROY_MEMPOOL (pool );
@@ -469,4 +522,33 @@ static umf_memory_pool_ops_t UMF_JEMALLOC_POOL_OPS = {
469522umf_memory_pool_ops_t * umfJemallocPoolOps (void ) {
470523 return & UMF_JEMALLOC_POOL_OPS ;
471524}
525+
526+ umf_result_t
527+ umfJemallocPoolParamsCreate (umf_jemalloc_pool_params_handle_t * hParams ) {
528+ umf_jemalloc_pool_params_t * params = umf_ba_global_alloc (sizeof (* params ));
529+ if (!params ) {
530+ return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY ;
531+ }
532+ memset (params , 0 , sizeof (* params ));
533+ * hParams = params ;
534+ return UMF_RESULT_SUCCESS ;
535+ }
536+
537+ umf_result_t
538+ umfJemallocPoolParamsDestroy (umf_jemalloc_pool_params_handle_t hParams ) {
539+ umf_ba_global_free (hParams );
540+ return UMF_RESULT_SUCCESS ;
541+ }
542+
543+ umf_result_t
544+ umfJemallocPoolParamsSetNumArenas (umf_jemalloc_pool_params_handle_t hParams ,
545+ size_t numArenas ) {
546+ if (!hParams ) {
547+ LOG_ERR ("jemalloc pool params handle is NULL" );
548+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
549+ }
550+ hParams -> n_arenas = numArenas ;
551+ return UMF_RESULT_SUCCESS ;
552+ }
553+
472554#endif /* UMF_POOL_JEMALLOC_ENABLED */
0 commit comments