Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: oneapi-src/unified-memory-framework
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 574ae1617f4b18f8d3334eb4109eaf68150227d9
Choose a base ref
..
head repository: oneapi-src/unified-memory-framework
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1890d07871a76d539ada7f854dd4b5ef5ff32d47
Choose a head ref
Showing with 65 additions and 28 deletions.
  1. +3 −3 src/libumf.def
  2. +3 −3 src/libumf.map
  3. +21 −13 src/pool/pool_jemalloc.c
  4. +3 −3 src/utils/utils_posix_common.c
  5. +35 −6 test/pools/jemalloc_pool.cpp
6 changes: 3 additions & 3 deletions src/libumf.def
Original file line number Diff line number Diff line change
@@ -35,9 +35,6 @@ EXPORTS
umfGetIPCHandle
umfGetLastFailedMemoryProvider
umfJemallocPoolOps
umfJemallocPoolParamsCreate
umfJemallocPoolParamsDestroy
umfJemallocPoolParamsSetNumArenas
umfLevelZeroMemoryProviderOps
umfLevelZeroMemoryProviderParamsCreate
umfLevelZeroMemoryProviderParamsDestroy
@@ -143,3 +140,6 @@ EXPORTS
umfCtlExec
umfCtlGet
umfCtlSet
umfJemallocPoolParamsCreate
umfJemallocPoolParamsDestroy
umfJemallocPoolParamsSetNumArenas
6 changes: 3 additions & 3 deletions src/libumf.map
Original file line number Diff line number Diff line change
@@ -29,9 +29,6 @@ UMF_0.10 {
umfGetIPCHandle;
umfGetLastFailedMemoryProvider;
umfJemallocPoolOps;
umfJemallocPoolParamsCreate;
umfJemallocPoolParamsDestroy;
umfJemallocPoolParamsSetNumArenas;
umfLevelZeroMemoryProviderOps;
umfLevelZeroMemoryProviderParamsCreate;
umfLevelZeroMemoryProviderParamsDestroy;
@@ -143,4 +140,7 @@ UMF_0.12 {
umfCtlExec;
umfCtlGet;
umfCtlSet;
umfJemallocPoolParamsCreate;
umfJemallocPoolParamsDestroy;
umfJemallocPoolParamsSetNumArenas;
} UMF_0.11;
34 changes: 21 additions & 13 deletions src/pool/pool_jemalloc.c
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ umf_memory_pool_ops_t *umfJemallocPoolOps(void) { return NULL; }
#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)

typedef struct umf_jemalloc_pool_params_t {
size_t numArenas;
size_t n_arenas;
} umf_jemalloc_pool_params_t;

typedef struct jemalloc_memory_pool_t {
@@ -299,8 +299,13 @@ static extent_hooks_t arena_extent_hooks = {
};

static unsigned get_arena_index(jemalloc_memory_pool_t *pool) {
unsigned pid = utils_getpid();
return pool->arena_index[hash64(pid) % pool->n_arenas];
static __TLS unsigned tid = 0;

if (tid == 0) {
tid = utils_gettid();
}

return pool->arena_index[hash64(tid) % pool->n_arenas];
}

static void *op_malloc(void *pool, size_t size) {
@@ -410,26 +415,30 @@ static umf_result_t op_initialize(umf_memory_provider_handle_t provider,
umf_jemalloc_pool_params_t *jemalloc_params =
(umf_jemalloc_pool_params_t *)params;

size_t nArenas = 0;
size_t n_arenas = 0;
if (jemalloc_params) {
nArenas = jemalloc_params->numArenas;
n_arenas = jemalloc_params->n_arenas;
}

if (nArenas == 0) {
nArenas = utils_get_num_cores() * 4;
if (n_arenas == 0) {
n_arenas = utils_get_num_cores() * 4;
}
if (n_arenas > MALLOCX_ARENA_MAX) {
LOG_ERR("Number of arenas exceeds the limit.");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

jemalloc_memory_pool_t *pool = umf_ba_global_alloc(
sizeof(*pool) + nArenas * sizeof(*pool->arena_index));
sizeof(*pool) + n_arenas * sizeof(*pool->arena_index));
if (!pool) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

pool->provider = provider;
pool->n_arenas = nArenas;
pool->n_arenas = n_arenas;

size_t num_created = 0;
for (size_t i = 0; i < nArenas; i++) {
for (size_t i = 0; i < n_arenas; i++) {
unsigned arena_index;
err = je_mallctl("arenas.create", (void *)&arena_index, &unsigned_size,
NULL, 0);
@@ -468,7 +477,6 @@ static umf_result_t op_initialize(umf_memory_provider_handle_t provider,
unsigned arena = pool->arena_index[i];
snprintf(cmd, sizeof(cmd), "arena.%u.destroy", arena);
(void)je_mallctl(cmd, NULL, 0, NULL, 0);
pool_by_arena_index[arena] = NULL;
}
umf_ba_global_free(pool);
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
@@ -482,7 +490,6 @@ static void op_finalize(void *pool) {
unsigned arena = je_pool->arena_index[i];
snprintf(cmd, sizeof(cmd), "arena.%u.destroy", arena);
(void)je_mallctl(cmd, NULL, 0, NULL, 0);
pool_by_arena_index[arena] = NULL;
}
umf_ba_global_free(je_pool);

@@ -522,6 +529,7 @@ umfJemallocPoolParamsCreate(umf_jemalloc_pool_params_handle_t *hParams) {
if (!params) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
memset(params, 0, sizeof(*params));
*hParams = params;
return UMF_RESULT_SUCCESS;
}
@@ -539,7 +547,7 @@ umfJemallocPoolParamsSetNumArenas(umf_jemalloc_pool_params_handle_t hParams,
LOG_ERR("jemalloc pool params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
hParams->numArenas = numArenas;
hParams->n_arenas = numArenas;
return UMF_RESULT_SUCCESS;
}

6 changes: 3 additions & 3 deletions src/utils/utils_posix_common.c
Original file line number Diff line number Diff line change
@@ -39,11 +39,11 @@

static UTIL_ONCE_FLAG System_info_is_initialized = UTIL_ONCE_FLAG_INIT;
static size_t Page_size;
static unsigned Core_conut;
static unsigned Core_count;

static void _utils_get_system_info(void) {
Page_size = sysconf(_SC_PAGE_SIZE);
Core_conut = sysconf(_SC_NPROCESSORS_ONLN);
Core_count = sysconf(_SC_NPROCESSORS_ONLN);
}

size_t utils_get_page_size(void) {
@@ -68,7 +68,7 @@ int utils_gettid(void) {

unsigned utils_get_num_cores(void) {
utils_init_once(&System_info_is_initialized, _utils_get_system_info);
return Core_conut;
return Core_count;
}

int utils_close_fd(int fd) { return close(fd); }
41 changes: 35 additions & 6 deletions test/pools/jemalloc_pool.cpp
Original file line number Diff line number Diff line change
@@ -55,14 +55,14 @@ umf_result_t destroyFixedMemoryProviderParams(void *params) {
(umf_fixed_memory_provider_params_handle_t)params);
}

void *createJemallocParams() {
template <unsigned arenas = 0> void *createJemallocParams() {
umf_jemalloc_pool_params_handle_t params = nullptr;
auto ret = umfJemallocPoolParamsCreate(&params);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);

ret = umfJemallocPoolParamsSetNumArenas(params, 1);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);

if (arenas != 0) {
ret = umfJemallocPoolParamsSetNumArenas(params, arenas);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
}
ret = umfJemallocPoolParamsDestroy(params);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
return params;
@@ -79,11 +79,14 @@ INSTANTIATE_TEST_SUITE_P(
poolCreateExtParams{
umfJemallocPoolOps(), nullptr, nullptr, umfOsMemoryProviderOps(),
createOsMemoryProviderParams, destroyOsMemoryProviderParams},

poolCreateExtParams{
umfJemallocPoolOps(), nullptr, nullptr, umfFixedMemoryProviderOps(),
createFixedMemoryProviderParams, destroyFixedMemoryProviderParams},
poolCreateExtParams{umfJemallocPoolOps(), createJemallocParams,
destroyJemallocParams, umfOsMemoryProviderOps(),
createOsMemoryProviderParams,
destroyOsMemoryProviderParams},
poolCreateExtParams{umfJemallocPoolOps(), createJemallocParams<1>,
destroyJemallocParams, umfOsMemoryProviderOps(),
createOsMemoryProviderParams,
destroyOsMemoryProviderParams}));
@@ -158,3 +161,29 @@ TEST_F(test, jemallocPoolParams) {
ret = umfJemallocPoolParamsDestroy(params);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
}

TEST_F(test, jemallocPoolParamsInvalid) {
umf_jemalloc_pool_params_handle_t params = nullptr;
auto ret = umfJemallocPoolParamsCreate(&params);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);

ret = umfJemallocPoolParamsSetNumArenas(params, SIZE_MAX);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);

umf_os_memory_provider_params_handle_t provider_params = nullptr;
ret = umfOsMemoryProviderParamsCreate(&provider_params);
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
umf_memory_provider_handle_t provider;
ret = umfMemoryProviderCreate(umfOsMemoryProviderOps(), provider_params,
&provider);
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);

umf_memory_pool_handle_t pool;
ret = umfPoolCreate(umfJemallocPoolOps(), provider, params, 0, &pool);
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);

umfMemoryProviderDestroy(provider);

ret = umfJemallocPoolParamsDestroy(params);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
}