6
6
*/
7
7
8
8
#include <assert.h>
9
+ #include <errno.h>
9
10
#include <stdio.h>
10
11
#include <stdlib.h>
11
12
#include <string.h>
@@ -430,19 +431,26 @@ static umf_result_t op_initialize(umf_memory_provider_handle_t provider,
430
431
431
432
extent_hooks_t * pHooks = & arena_extent_hooks ;
432
433
size_t unsigned_size = sizeof (unsigned );
434
+ int n_arenas_set_from_params = 0 ;
433
435
int err ;
434
436
const umf_jemalloc_pool_params_t * jemalloc_params = params ;
435
437
436
438
size_t n_arenas = 0 ;
437
439
if (jemalloc_params ) {
438
440
n_arenas = jemalloc_params -> n_arenas ;
441
+ n_arenas_set_from_params = 1 ;
439
442
}
440
443
441
444
if (n_arenas == 0 ) {
442
445
n_arenas = utils_get_num_cores () * 4 ;
446
+ if (n_arenas > MALLOCX_ARENA_MAX ) {
447
+ n_arenas = MALLOCX_ARENA_MAX ;
448
+ }
443
449
}
450
+
444
451
if (n_arenas > MALLOCX_ARENA_MAX ) {
445
- LOG_ERR ("Number of arenas exceeds the limit." );
452
+ LOG_ERR ("Number of arenas %zu exceeds the limit (%i)." , n_arenas ,
453
+ MALLOCX_ARENA_MAX );
446
454
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
447
455
}
448
456
@@ -461,8 +469,21 @@ static umf_result_t op_initialize(umf_memory_provider_handle_t provider,
461
469
err = je_mallctl ("arenas.create" , (void * )& arena_index , & unsigned_size ,
462
470
NULL , 0 );
463
471
if (err ) {
464
- LOG_ERR ("Could not create arena." );
465
- goto err_cleanup ;
472
+ // EAGAIN - means that a memory allocation failure occurred
473
+ // (2 * utils_get_num_cores()) is the required minimum number of arenas
474
+ if (n_arenas_set_from_params || err != EAGAIN ||
475
+ (i < (2 * utils_get_num_cores ()))) {
476
+ LOG_ERR ("Could not create a jemalloc arena (n_arenas = %zu, i "
477
+ "= %zu, arena_index = %u, unsigned_size = %zu): %s" ,
478
+ n_arenas , i , arena_index , unsigned_size , strerror (err ));
479
+ goto err_cleanup ;
480
+ }
481
+
482
+ LOG_WARN ("Could not create the #%zu jemalloc arena (%s), setting "
483
+ "n_arenas = %zu" ,
484
+ i + 1 , strerror (err ), i );
485
+ n_arenas = i ;
486
+ break ;
466
487
}
467
488
468
489
pool -> arena_index [num_created ++ ] = arena_index ;
0 commit comments