Skip to content

Commit 43d33c2

Browse files
Merge pull request #1305 from ldorau/Fix_maximum_number_of_arenas_in_op_initialize
Fix maximum number of arenas in op_initialize()
2 parents 14a8d70 + fd7a862 commit 43d33c2

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/pool/pool_jemalloc.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <assert.h>
9+
#include <errno.h>
910
#include <stdio.h>
1011
#include <stdlib.h>
1112
#include <string.h>
@@ -430,19 +431,26 @@ static umf_result_t op_initialize(umf_memory_provider_handle_t provider,
430431

431432
extent_hooks_t *pHooks = &arena_extent_hooks;
432433
size_t unsigned_size = sizeof(unsigned);
434+
int n_arenas_set_from_params = 0;
433435
int err;
434436
const umf_jemalloc_pool_params_t *jemalloc_params = params;
435437

436438
size_t n_arenas = 0;
437439
if (jemalloc_params) {
438440
n_arenas = jemalloc_params->n_arenas;
441+
n_arenas_set_from_params = 1;
439442
}
440443

441444
if (n_arenas == 0) {
442445
n_arenas = utils_get_num_cores() * 4;
446+
if (n_arenas > MALLOCX_ARENA_MAX) {
447+
n_arenas = MALLOCX_ARENA_MAX;
448+
}
443449
}
450+
444451
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);
446454
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
447455
}
448456

@@ -461,8 +469,21 @@ static umf_result_t op_initialize(umf_memory_provider_handle_t provider,
461469
err = je_mallctl("arenas.create", (void *)&arena_index, &unsigned_size,
462470
NULL, 0);
463471
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;
466487
}
467488

468489
pool->arena_index[num_created++] = arena_index;

0 commit comments

Comments
 (0)