Skip to content

Add jemalloc test for metadata allocation #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/umf/providers/provider_os_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ typedef enum umf_os_memory_provider_native_error {

extern umf_memory_provider_ops_t UMF_OS_MEMORY_PROVIDER_OPS;

/// @brief Create default params for os memory provider
static inline umf_os_memory_provider_params_t
umfOsMemoryProviderParamsDefault(void) {
umf_os_memory_provider_params_t params = {
UMF_PROTECTION_READ | UMF_PROTECTION_WRITE, /* protection */
UMF_VISIBILITY_PRIVATE, /* visibility */
NULL, /* nodemask */
0, /* maxnode */
UMF_NUMA_MODE_DEFAULT, /* numa_mode */
0, /* numa_flags */
0 /* traces */
};

return params;
}

#ifdef __cplusplus
}
#endif
Expand Down
21 changes: 3 additions & 18 deletions src/memory_targets/memory_target_numa.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,6 @@ static umf_result_t numa_initialize(void *params, void **memTarget) {

static void numa_finalize(void *memTarget) { free(memTarget); }

static const umf_os_memory_provider_params_t
UMF_OS_MEMORY_PROVIDER_PARAMS_DEFAULT = {
// Visibility & protection
.protection = UMF_PROTECTION_READ | UMF_PROTECTION_WRITE,
.visibility = UMF_VISIBILITY_PRIVATE,

// NUMA config
.nodemask = NULL,
.maxnode = 0, // TODO: numa_max_node/GetNumaHighestNodeNumber
.numa_mode = UMF_NUMA_MODE_BIND,
.numa_flags = UMF_NUMA_FLAGS_STRICT, // TODO: determine default behavior

// Logging
.traces = 0, // TODO: parse env variable for log level?
};

static umf_result_t
numa_targets_create_nodemask(struct numa_memory_target_t **targets,
size_t numTargets, unsigned long **mask,
Expand Down Expand Up @@ -118,10 +102,11 @@ static enum umf_result_t numa_memory_provider_create_from_memspace(
return ret;
}

umf_os_memory_provider_params_t params =
UMF_OS_MEMORY_PROVIDER_PARAMS_DEFAULT;
umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();
params.nodemask = nodemask;
params.maxnode = maxnode;
params.numa_mode = UMF_NUMA_MODE_BIND;
params.numa_flags = UMF_NUMA_FLAGS_STRICT;

umf_memory_provider_handle_t numaProvider = NULL;
ret = umfMemoryProviderCreate(&UMF_OS_MEMORY_PROVIDER_OPS, &params,
Expand Down
43 changes: 28 additions & 15 deletions test/pools/jemalloc_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,35 @@
#include "pool.hpp"
#include "poolFixtures.hpp"

static umf_os_memory_provider_params_t UMF_OS_MEMORY_PROVIDER_PARAMS = {
/* .protection = */ UMF_PROTECTION_READ | UMF_PROTECTION_WRITE,
/* .visibility = */ UMF_VISIBILITY_PRIVATE,

// NUMA config
/* .nodemask = */ NULL,
/* .maxnode = */ 0,
/* .numa_mode = */ UMF_NUMA_MODE_DEFAULT,
/* .numa_flags = */ 0,

// others
/* .traces = */ 0,
};
using umf_test::test;
using namespace umf_test;

auto defaultParams = umfOsMemoryProviderParamsDefault();
INSTANTIATE_TEST_SUITE_P(jemallocPoolTest, umfPoolTest,
::testing::Values(poolCreateExtParams{
&UMF_JEMALLOC_POOL_OPS, nullptr,
&UMF_OS_MEMORY_PROVIDER_OPS,
&UMF_OS_MEMORY_PROVIDER_PARAMS}));
&UMF_OS_MEMORY_PROVIDER_OPS, &defaultParams}));

// this test makes sure that jemalloc does not use
// memory provider to allocate metadata (and hence
// is suitable for cases where memory is not accessible
// on the host)
TEST_F(test, metadataNotAllocatedUsingProvider) {
static constexpr size_t allocSize = 1024;
static constexpr size_t numAllocs = 1024;

// set coarse grain allocations to PROT_NONE so that we can be sure
// jemalloc does not touch any of the allocated memory
auto params = umfOsMemoryProviderParamsDefault();
params.protection = UMF_PROTECTION_NONE;

auto pool = poolCreateExtUnique({&UMF_JEMALLOC_POOL_OPS, nullptr,
&UMF_OS_MEMORY_PROVIDER_OPS, &params});

std::vector<std::shared_ptr<void>> allocs;
for (size_t i = 0; i < numAllocs; i++) {
allocs.emplace_back(
umfPoolMalloc(pool.get(), allocSize),
[pool = pool.get()](void *ptr) { umfPoolFree(pool, ptr); });
}
}
18 changes: 2 additions & 16 deletions test/pools/scalable_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,8 @@
#include "pool.hpp"
#include "poolFixtures.hpp"

static umf_os_memory_provider_params_t UMF_OS_MEMORY_PROVIDER_PARAMS = {
/* .protection = */ UMF_PROTECTION_READ | UMF_PROTECTION_WRITE,
/* .visibility = */ UMF_VISIBILITY_PRIVATE,

// NUMA config
/* .nodemask = */ NULL,
/* .maxnode = */ 0,
/* .numa_mode = */ UMF_NUMA_MODE_DEFAULT,
/* .numa_flags = */ 0,

// others
/* .traces = */ 0,
};

auto defaultParams = umfOsMemoryProviderParamsDefault();
INSTANTIATE_TEST_SUITE_P(scalablePoolTest, umfPoolTest,
::testing::Values(poolCreateExtParams{
&UMF_SCALABLE_POOL_OPS, nullptr,
&UMF_OS_MEMORY_PROVIDER_OPS,
&UMF_OS_MEMORY_PROVIDER_PARAMS}));
&UMF_OS_MEMORY_PROVIDER_OPS, &defaultParams}));
22 changes: 4 additions & 18 deletions test/provider_os_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ typedef enum purge_t {
PURGE_FORCE = 2,
} purge_t;

static umf_os_memory_provider_params_t UMF_OS_MEMORY_PROVIDER_PARAMS_TEST = {
/* .protection = */ UMF_PROTECTION_READ | UMF_PROTECTION_WRITE,
/* .visibility = */ UMF_VISIBILITY_PRIVATE,

// NUMA config
/* .nodemask = */ NULL,
/* .maxnode = */ 0,
/* .numa_mode = */ UMF_NUMA_MODE_DEFAULT,
/* .numa_flags = */ 0,

// others
/* .traces = */ 1,
};

static const char *Native_error_str[] = {
"success", // UMF_OS_RESULT_SUCCESS
"memory allocation failed", // UMF_OS_RESULT_ERROR_ALLOC_FAILED
Expand Down Expand Up @@ -144,7 +130,7 @@ TEST_F(test, create_WRONG_NUMA_MODE) {
umf_result_t umf_result;
umf_memory_provider_handle_t os_memory_provider = nullptr;
umf_os_memory_provider_params_t os_memory_provider_params =
UMF_OS_MEMORY_PROVIDER_PARAMS_TEST;
umfOsMemoryProviderParamsDefault();

// NUMA binding mode not supported for UMF_VISIBILITY_SHARED
os_memory_provider_params.visibility = UMF_VISIBILITY_SHARED;
Expand All @@ -161,7 +147,7 @@ TEST_F(test, create_WRONG_NUMA_FLAGS) {
umf_result_t umf_result;
umf_memory_provider_handle_t os_memory_provider = nullptr;
umf_os_memory_provider_params_t os_memory_provider_params =
UMF_OS_MEMORY_PROVIDER_PARAMS_TEST;
umfOsMemoryProviderParamsDefault();

// wrong NUMA flags
os_memory_provider_params.numa_flags = (unsigned int)-1;
Expand All @@ -175,10 +161,10 @@ TEST_F(test, create_WRONG_NUMA_FLAGS) {

// positive tests using test_alloc_free_success

auto defaultParams = umfOsMemoryProviderParamsDefault();
INSTANTIATE_TEST_SUITE_P(osProviderTest, umfProviderTest,
::testing::Values(providerCreateExtParams{
&UMF_OS_MEMORY_PROVIDER_OPS,
&UMF_OS_MEMORY_PROVIDER_PARAMS_TEST}));
&UMF_OS_MEMORY_PROVIDER_OPS, &defaultParams}));

TEST_P(umfProviderTest, create_destroy) {}

Expand Down
20 changes: 7 additions & 13 deletions test/provider_os_memory_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,10 @@ static constexpr size_t allocSize = 4096;

struct providerConfigTest : testing::Test {
umf_memory_provider_handle_t provider = nullptr;
umf_os_memory_provider_params_t params = {
/* .protection = */ UMF_PROTECTION_READ | UMF_PROTECTION_WRITE,
/* .visibility = */ UMF_VISIBILITY_PRIVATE,

// NUMA config
/* .nodemask = */ NULL,
/* .maxnode = */ 0,
/* .numa_mode = */ UMF_NUMA_MODE_DEFAULT,
/* .numa_flags = */ 0,

// others
/* .traces = */ 1,
};
const size_t size = 128;
void *ptr = nullptr;
std::string dest = "destination";
umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();

void SetUp() override {
int ret = numa_available();
Expand Down Expand Up @@ -81,6 +69,7 @@ struct providerConfigTest : testing::Test {
TEST_F(providerConfigTest, protection_flag_none) {
// pages may not be accessed - PROT_NONE
params.protection = UMF_PROTECTION_NONE;

create_provider(&params);
allocate_memory();

Expand All @@ -94,6 +83,7 @@ TEST_F(providerConfigTest, protection_flag_none) {
TEST_F(providerConfigTest, protection_flag_read) {
// pages may be read - PROT_READ
params.protection = UMF_PROTECTION_READ;

create_provider(&params);
allocate_memory();

Expand All @@ -107,6 +97,7 @@ TEST_F(providerConfigTest, protection_flag_read) {
TEST_F(providerConfigTest, protection_flag_write) {
// pages may be written to - PROT_WRITE
params.protection = UMF_PROTECTION_WRITE;

create_provider(&params);
allocate_memory();

Expand All @@ -117,6 +108,7 @@ TEST_F(providerConfigTest, protection_flag_write) {
TEST_F(providerConfigTest, protection_flag_read_write) {
// pages may be read and written to - PROT_READ | PROT_WRITE
params.protection = UMF_PROTECTION_READ | UMF_PROTECTION_WRITE;

create_provider(&params);
allocate_memory();

Expand All @@ -131,6 +123,7 @@ struct providerConfigTestNumaMode
: providerConfigTest,
testing::WithParamInterface<umf_numa_mode_t> {
struct bitmask *allowed_nodes = nullptr;
umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();

void SetUp() override {
providerConfigTest::SetUp();
Expand Down Expand Up @@ -203,6 +196,7 @@ struct providerConfigTestVisibility
std::string primary_str = "primary";
std::string new_str = "new";
std::string expected_str;
umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();

void SetUp() override {
providerConfigTest::SetUp();
Expand Down