From c063605f67c3bae69cfe23a45db8fa877323d95e Mon Sep 17 00:00:00 2001 From: "Vinogradov, Sergei" Date: Fri, 22 Nov 2024 13:55:53 +0100 Subject: [PATCH 1/2] Update File provider config API --- examples/dram_and_fsdax/dram_and_fsdax.c | 20 ++- include/umf/providers/provider_file_memory.h | 60 +++++--- src/libumf.def | 5 + src/libumf.map | 5 + src/provider/provider_file_memory.c | 147 +++++++++++++++++++ test/ipc_file_prov_consumer.c | 32 +++- test/ipc_file_prov_producer.c | 32 +++- test/pools/jemalloc_coarse_file.cpp | 20 ++- test/pools/scalable_coarse_file.cpp | 20 ++- test/provider_file_memory.cpp | 136 ++++++++++++++--- test/provider_file_memory_ipc.cpp | 67 ++++++--- 11 files changed, 465 insertions(+), 79 deletions(-) diff --git a/examples/dram_and_fsdax/dram_and_fsdax.c b/examples/dram_and_fsdax/dram_and_fsdax.c index 0d21ce6204..ef11c186e3 100644 --- a/examples/dram_and_fsdax/dram_and_fsdax.c +++ b/examples/dram_and_fsdax/dram_and_fsdax.c @@ -48,13 +48,25 @@ static umf_memory_pool_handle_t create_fsdax_pool(const char *path) { umf_memory_pool_handle_t pool_fsdax; umf_result_t umf_result; - umf_file_memory_provider_params_t params_fsdax = - umfFileMemoryProviderParamsDefault(path); + umf_file_memory_provider_params_handle_t params_fsdax = NULL; + umf_result = umfFileMemoryProviderParamsCreate(¶ms_fsdax, path); + if (umf_result != UMF_RESULT_SUCCESS) { + fprintf(stderr, "Failed to create the File Memory Provider params"); + return NULL; + } // FSDAX requires mapping the UMF_MEM_MAP_SHARED flag - params_fsdax.visibility = UMF_MEM_MAP_SHARED; + umf_result = umfFileMemoryProviderParamsSetVisibility(params_fsdax, + UMF_MEM_MAP_SHARED); + if (umf_result != UMF_RESULT_SUCCESS) { + fprintf(stderr, + "Failed to set the visibility of the FSDAX file provider"); + umfFileMemoryProviderParamsDestroy(params_fsdax); + return NULL; + } umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(), - ¶ms_fsdax, &provider_fsdax); + params_fsdax, &provider_fsdax); + umfFileMemoryProviderParamsDestroy(params_fsdax); if (umf_result != UMF_RESULT_SUCCESS) { fprintf(stderr, "Failed to create the FSDAX file provider"); return NULL; diff --git a/include/umf/providers/provider_file_memory.h b/include/umf/providers/provider_file_memory.h index 4b5b59b812..f652e2cb8a 100644 --- a/include/umf/providers/provider_file_memory.h +++ b/include/umf/providers/provider_file_memory.h @@ -18,15 +18,45 @@ extern "C" { #define UMF_FILE_RESULTS_START_FROM 3000 /// @endcond -/// @brief Memory provider settings struct -typedef struct umf_file_memory_provider_params_t { - /// a path to the file (of maximum length PATH_MAX characters) - const char *path; - /// combination of 'umf_mem_protection_flags_t' flags - unsigned protection; - /// memory visibility mode - umf_memory_visibility_t visibility; -} umf_file_memory_provider_params_t; +struct umf_file_memory_provider_params_t; + +typedef struct umf_file_memory_provider_params_t + *umf_file_memory_provider_params_handle_t; + +/// @brief Create a struct to store parameters of the File Memory Provider. +/// @param hParams [out] handle to the newly created parameters struct. +/// @param path path to the file. +/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. +umf_result_t umfFileMemoryProviderParamsCreate( + umf_file_memory_provider_params_handle_t *hParams, const char *path); + +/// @brief Destroy parameters struct. +/// @param hParams handle to the parameters of the File Memory Provider. +/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. +umf_result_t umfFileMemoryProviderParamsDestroy( + umf_file_memory_provider_params_handle_t hParams); + +/// @brief Set the path in the parameters struct. +/// @param hParams handle to the parameters of the File Memory Provider. +/// @param path path to the file. +/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. +umf_result_t umfFileMemoryProviderParamsSetPath( + umf_file_memory_provider_params_handle_t hParams, const char *path); + +/// @brief Set the protection in the parameters struct. +/// @param hParams handle to the parameters of the File Memory Provider. +/// @param protection protection. Combination of \p umf_mem_protection_flags_t flags +/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. +umf_result_t umfFileMemoryProviderParamsSetProtection( + umf_file_memory_provider_params_handle_t hParams, unsigned protection); + +/// @brief Set the visibility in the parameters struct. +/// @param hParams handle to the parameters of the File Memory Provider. +/// @param visibility memory visibility mode. +/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. +umf_result_t umfFileMemoryProviderParamsSetVisibility( + umf_file_memory_provider_params_handle_t hParams, + umf_memory_visibility_t visibility); /// @brief File Memory Provider operation results typedef enum umf_file_memory_provider_native_error { @@ -38,18 +68,6 @@ typedef enum umf_file_memory_provider_native_error { umf_memory_provider_ops_t *umfFileMemoryProviderOps(void); -/// @brief Create default params for the file memory provider -static inline umf_file_memory_provider_params_t -umfFileMemoryProviderParamsDefault(const char *path) { - umf_file_memory_provider_params_t params = { - path, /* a path to the file */ - UMF_PROTECTION_READ | UMF_PROTECTION_WRITE, /* protection */ - UMF_MEM_MAP_PRIVATE, /* visibility mode */ - }; - - return params; -} - #ifdef __cplusplus } #endif diff --git a/src/libumf.def b/src/libumf.def index 42a924bd71..abb25204c4 100644 --- a/src/libumf.def +++ b/src/libumf.def @@ -25,6 +25,11 @@ EXPORTS umfDevDaxMemoryProviderOps umfFree umfFileMemoryProviderOps + umfFileMemoryProviderParamsCreate + umfFileMemoryProviderParamsDestroy + umfFileMemoryProviderParamsSetPath + umfFileMemoryProviderParamsSetProtection + umfFileMemoryProviderParamsSetVisibility umfGetIPCHandle umfGetLastFailedMemoryProvider umfLevelZeroMemoryProviderOps diff --git a/src/libumf.map b/src/libumf.map index 443194e55a..3a5e73f448 100644 --- a/src/libumf.map +++ b/src/libumf.map @@ -19,6 +19,11 @@ UMF_1.0 { umfDevDaxMemoryProviderOps; umfFree; umfFileMemoryProviderOps; + umfFileMemoryProviderParamsCreate; + umfFileMemoryProviderParamsDestroy; + umfFileMemoryProviderParamsSetPath; + umfFileMemoryProviderParamsSetProtection; + umfFileMemoryProviderParamsSetVisibility; umfGetIPCHandle; umfGetLastFailedMemoryProvider; umfLevelZeroMemoryProviderOps; diff --git a/src/provider/provider_file_memory.c b/src/provider/provider_file_memory.c index 09d50a6255..32383a5ecc 100644 --- a/src/provider/provider_file_memory.c +++ b/src/provider/provider_file_memory.c @@ -25,10 +25,46 @@ umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) { return NULL; } +umf_result_t umfFileMemoryProviderParamsCreate( + umf_file_memory_provider_params_handle_t *hParams, const char *path) { + (void)hParams; + (void)path; + return UMF_RESULT_ERROR_NOT_SUPPORTED; +} + +umf_result_t umfFileMemoryProviderParamsDestroy( + umf_file_memory_provider_params_handle_t hParams) { + (void)hParams; + return UMF_RESULT_ERROR_NOT_SUPPORTED; +} + +umf_result_t umfFileMemoryProviderParamsSetPath( + umf_file_memory_provider_params_handle_t hParams, const char *path) { + (void)hParams; + (void)path; + return UMF_RESULT_ERROR_NOT_SUPPORTED; +} + +umf_result_t umfFileMemoryProviderParamsSetProtection( + umf_file_memory_provider_params_handle_t hParams, unsigned protection) { + (void)hParams; + (void)protection; + return UMF_RESULT_ERROR_NOT_SUPPORTED; +} + +umf_result_t umfFileMemoryProviderParamsSetVisibility( + umf_file_memory_provider_params_handle_t hParams, + umf_memory_visibility_t visibility) { + (void)hParams; + (void)visibility; + return UMF_RESULT_ERROR_NOT_SUPPORTED; +} + #else // !defined(_WIN32) && !defined(UMF_NO_HWLOC) #include "base_alloc_global.h" #include "critnib.h" +#include "libumf.h" #include "utils_common.h" #include "utils_concurrency.h" #include "utils_log.h" @@ -67,6 +103,13 @@ typedef struct file_memory_provider_t { critnib *fd_offset_map; } file_memory_provider_t; +// File Memory Provider settings struct +typedef struct umf_file_memory_provider_params_t { + char *path; + unsigned protection; + umf_memory_visibility_t visibility; +} umf_file_memory_provider_params_t; + typedef struct file_last_native_error_t { int32_t native_error; int errno_value; @@ -748,4 +791,108 @@ umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) { return &UMF_FILE_MEMORY_PROVIDER_OPS; } +umf_result_t umfFileMemoryProviderParamsCreate( + umf_file_memory_provider_params_handle_t *hParams, const char *path) { + libumfInit(); + if (hParams == NULL) { + LOG_ERR("File Memory Provider params handle is NULL"); + return UMF_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (path == NULL) { + LOG_ERR("File path is NULL"); + return UMF_RESULT_ERROR_INVALID_ARGUMENT; + } + + umf_file_memory_provider_params_handle_t params = + umf_ba_global_alloc(sizeof(*params)); + if (params == NULL) { + LOG_ERR("allocating memory for File Memory Provider params failed"); + return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; + } + + params->path = NULL; + params->protection = UMF_PROTECTION_READ | UMF_PROTECTION_WRITE; + params->visibility = UMF_MEM_MAP_PRIVATE; + + umf_result_t res = umfFileMemoryProviderParamsSetPath(params, path); + if (res != UMF_RESULT_SUCCESS) { + umf_ba_global_free(params); + return res; + } + + *hParams = params; + + return UMF_RESULT_SUCCESS; +} + +umf_result_t umfFileMemoryProviderParamsDestroy( + umf_file_memory_provider_params_handle_t hParams) { + if (hParams != NULL) { + umf_ba_global_free(hParams->path); + umf_ba_global_free(hParams); + } + + return UMF_RESULT_SUCCESS; +} + +umf_result_t umfFileMemoryProviderParamsSetPath( + umf_file_memory_provider_params_handle_t hParams, const char *path) { + if (hParams == NULL) { + LOG_ERR("File Memory Provider params handle is NULL"); + return UMF_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (path == NULL) { + LOG_ERR("File path is NULL"); + return UMF_RESULT_ERROR_INVALID_ARGUMENT; + } + + size_t len = strlen(path); + if (len == 0) { + LOG_ERR("File path is empty"); + return UMF_RESULT_ERROR_INVALID_ARGUMENT; + } + + len += 1; // for the null terminator + char *new_path = NULL; + new_path = umf_ba_global_alloc(len); + if (new_path == NULL) { + LOG_ERR("allocating memory for the file path failed"); + return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; + } + + strncpy(new_path, path, len); + + umf_ba_global_free(hParams->path); + hParams->path = new_path; + + return UMF_RESULT_SUCCESS; +} + +umf_result_t umfFileMemoryProviderParamsSetProtection( + umf_file_memory_provider_params_handle_t hParams, unsigned protection) { + if (hParams == NULL) { + LOG_ERR("File Memory Provider params handle is NULL"); + return UMF_RESULT_ERROR_INVALID_ARGUMENT; + } + + hParams->protection = protection; + + return UMF_RESULT_SUCCESS; +} + +umf_result_t umfFileMemoryProviderParamsSetVisibility( + umf_file_memory_provider_params_handle_t hParams, + umf_memory_visibility_t visibility) { + if (hParams == NULL) { + LOG_ERR("File Memory Provider params handle is NULL"); + return UMF_RESULT_ERROR_INVALID_ARGUMENT; + } + + hParams->visibility = visibility; + + return UMF_RESULT_SUCCESS; +} + #endif // !defined(_WIN32) && !defined(UMF_NO_HWLOC) diff --git a/test/ipc_file_prov_consumer.c b/test/ipc_file_prov_consumer.c index 6333552a2d..0c50991a9b 100644 --- a/test/ipc_file_prov_consumer.c +++ b/test/ipc_file_prov_consumer.c @@ -22,16 +22,36 @@ int main(int argc, char *argv[]) { return -1; } + int ret = 0; int port = atoi(argv[1]); char *file_name = argv[2]; - umf_file_memory_provider_params_t file_params; - file_params = umfFileMemoryProviderParamsDefault(file_name); - file_params.visibility = UMF_MEM_MAP_SHARED; + umf_file_memory_provider_params_handle_t file_params = NULL; + umf_result_t umf_result = + umfFileMemoryProviderParamsCreate(&file_params, file_name); + if (umf_result != UMF_RESULT_SUCCESS) { + fprintf( + stderr, + "[consumer] ERROR: creating File Memory Provider params failed\n"); + return -1; + } + + umf_result = umfFileMemoryProviderParamsSetVisibility(file_params, + UMF_MEM_MAP_SHARED); + if (umf_result != UMF_RESULT_SUCCESS) { + fprintf(stderr, "[consumer] ERROR: setting File Memory Provider " + "visibility failed\n"); + ret = -1; + goto destroy_provider_params; + } void *pool_params = NULL; - return run_consumer(port, umfScalablePoolOps(), pool_params, - umfFileMemoryProviderOps(), &file_params, memcopy, - NULL); + ret = run_consumer(port, umfScalablePoolOps(), pool_params, + umfFileMemoryProviderOps(), file_params, memcopy, NULL); + +destroy_provider_params: + umfFileMemoryProviderParamsDestroy(file_params); + + return ret; } diff --git a/test/ipc_file_prov_producer.c b/test/ipc_file_prov_producer.c index efcbdd3bf5..c620437e08 100644 --- a/test/ipc_file_prov_producer.c +++ b/test/ipc_file_prov_producer.c @@ -22,16 +22,36 @@ int main(int argc, char *argv[]) { return -1; } + int ret = 0; int port = atoi(argv[1]); char *file_name = argv[2]; - umf_file_memory_provider_params_t file_params; - file_params = umfFileMemoryProviderParamsDefault(file_name); - file_params.visibility = UMF_MEM_MAP_SHARED; + umf_file_memory_provider_params_handle_t file_params = NULL; + umf_result_t umf_result = + umfFileMemoryProviderParamsCreate(&file_params, file_name); + if (umf_result != UMF_RESULT_SUCCESS) { + fprintf( + stderr, + "[producer] ERROR: creating File Memory Provider params failed\n"); + return -1; + } + + umf_result = umfFileMemoryProviderParamsSetVisibility(file_params, + UMF_MEM_MAP_SHARED); + if (umf_result != UMF_RESULT_SUCCESS) { + fprintf(stderr, "[producer] ERROR: setting File Memory Provider " + "visibility failed\n"); + ret = -1; + goto destroy_provider_params; + } void *pool_params = NULL; - return run_producer(port, umfScalablePoolOps(), pool_params, - umfFileMemoryProviderOps(), &file_params, memcopy, - NULL); + ret = run_producer(port, umfScalablePoolOps(), pool_params, + umfFileMemoryProviderOps(), file_params, memcopy, NULL); + +destroy_provider_params: + umfFileMemoryProviderParamsDestroy(file_params); + + return ret; } diff --git a/test/pools/jemalloc_coarse_file.cpp b/test/pools/jemalloc_coarse_file.cpp index 7eb9049037..74ad36d56e 100644 --- a/test/pools/jemalloc_coarse_file.cpp +++ b/test/pools/jemalloc_coarse_file.cpp @@ -7,11 +7,27 @@ #include "pool_coarse.hpp" +using file_params_unique_handle_t = + std::unique_ptr; + +file_params_unique_handle_t get_file_params_default(char *path) { + umf_file_memory_provider_params_handle_t file_params = NULL; + umf_result_t res = umfFileMemoryProviderParamsCreate(&file_params, path); + if (res != UMF_RESULT_SUCCESS) { + throw std::runtime_error( + "Failed to create File Memory Provider params"); + } + + return file_params_unique_handle_t(file_params, + &umfFileMemoryProviderParamsDestroy); +} + auto coarseParams = umfCoarseMemoryProviderParamsDefault(); -auto fileParams = umfFileMemoryProviderParamsDefault(FILE_PATH); +file_params_unique_handle_t fileParams = get_file_params_default(FILE_PATH); INSTANTIATE_TEST_SUITE_P(jemallocCoarseFileTest, umfPoolTest, ::testing::Values(poolCreateExtParams{ umfJemallocPoolOps(), nullptr, - umfFileMemoryProviderOps(), &fileParams, + umfFileMemoryProviderOps(), fileParams.get(), &coarseParams})); diff --git a/test/pools/scalable_coarse_file.cpp b/test/pools/scalable_coarse_file.cpp index b83a123389..b45c112be2 100644 --- a/test/pools/scalable_coarse_file.cpp +++ b/test/pools/scalable_coarse_file.cpp @@ -7,11 +7,27 @@ #include "pool_coarse.hpp" +using file_params_unique_handle_t = + std::unique_ptr; + +file_params_unique_handle_t get_file_params_default(char *path) { + umf_file_memory_provider_params_handle_t file_params = NULL; + umf_result_t res = umfFileMemoryProviderParamsCreate(&file_params, path); + if (res != UMF_RESULT_SUCCESS) { + throw std::runtime_error( + "Failed to create File Memory Provider params"); + } + + return file_params_unique_handle_t(file_params, + &umfFileMemoryProviderParamsDestroy); +} + auto coarseParams = umfCoarseMemoryProviderParamsDefault(); -auto fileParams = umfFileMemoryProviderParamsDefault(FILE_PATH); +file_params_unique_handle_t fileParams = get_file_params_default(FILE_PATH); INSTANTIATE_TEST_SUITE_P(scalableCoarseFileTest, umfPoolTest, ::testing::Values(poolCreateExtParams{ umfScalablePoolOps(), nullptr, - umfFileMemoryProviderOps(), &fileParams, + umfFileMemoryProviderOps(), fileParams.get(), &coarseParams})); diff --git a/test/provider_file_memory.cpp b/test/provider_file_memory.cpp index eba7e82056..d3124aa11f 100644 --- a/test/provider_file_memory.cpp +++ b/test/provider_file_memory.cpp @@ -136,11 +136,17 @@ TEST_F(test, test_if_mapped_with_MAP_SYNC) { GTEST_SKIP() << "Test skipped, UMF_TESTS_FSDAX_PATH is not set"; } - auto params = umfFileMemoryProviderParamsDefault(path); - params.visibility = UMF_MEM_MAP_SHARED; + umf_file_memory_provider_params_handle_t params = nullptr; + umf_result = umfFileMemoryProviderParamsCreate(¶ms, path); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); + ASSERT_NE(params, nullptr); - umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(), ¶ms, - &hProvider); + umf_result = + umfFileMemoryProviderParamsSetVisibility(params, UMF_MEM_MAP_SHARED); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); + + umf_result = + umfMemoryProviderCreate(umfFileMemoryProviderOps(), params, &hProvider); ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); ASSERT_NE(hProvider, nullptr); @@ -163,23 +169,52 @@ TEST_F(test, test_if_mapped_with_MAP_SYNC) { // positive tests using test_alloc_free_success -umf_file_memory_provider_params_t file_params_default = - umfFileMemoryProviderParamsDefault(FILE_PATH); +using file_params_unique_handle_t = + std::unique_ptr; + +file_params_unique_handle_t get_file_params_default(char *path) { + umf_file_memory_provider_params_handle_t file_params = NULL; + umf_result_t res = umfFileMemoryProviderParamsCreate(&file_params, path); + if (res != UMF_RESULT_SUCCESS) { + throw std::runtime_error( + "Failed to create File Memory Provider params"); + } + + return file_params_unique_handle_t(file_params, + &umfFileMemoryProviderParamsDestroy); +} + +file_params_unique_handle_t file_params_default = + get_file_params_default(FILE_PATH); + +file_params_unique_handle_t get_file_params_shared(char *path) { + umf_file_memory_provider_params_handle_t file_params = NULL; + umf_result_t res = umfFileMemoryProviderParamsCreate(&file_params, path); + if (res != UMF_RESULT_SUCCESS) { + throw std::runtime_error( + "Failed to create File Memory Provider params"); + } + + res = umfFileMemoryProviderParamsSetVisibility(file_params, + UMF_MEM_MAP_SHARED); + if (res != UMF_RESULT_SUCCESS) { + umfFileMemoryProviderParamsDestroy(file_params); + throw std::runtime_error("Failed to set visibility to shared for File " + "Memory Provider params"); + } -umf_file_memory_provider_params_t get_file_params_shared(char *path) { - umf_file_memory_provider_params_t file_params = - umfFileMemoryProviderParamsDefault(path); - file_params.visibility = UMF_MEM_MAP_SHARED; - return file_params; + return file_params_unique_handle_t(file_params, + &umfFileMemoryProviderParamsDestroy); } -umf_file_memory_provider_params_t file_params_shared = +file_params_unique_handle_t file_params_shared = get_file_params_shared(FILE_PATH); INSTANTIATE_TEST_SUITE_P(fileProviderTest, FileProviderParamsDefault, ::testing::Values(providerCreateExtParams{ umfFileMemoryProviderOps(), - &file_params_default})); + file_params_default.get()})); TEST_P(FileProviderParamsDefault, create_destroy) {} @@ -341,14 +376,74 @@ TEST_P(FileProviderParamsDefault, free_NULL) { // other negative tests +TEST_F(test, params_null_handle) { + umf_result_t umf_result = + umfFileMemoryProviderParamsCreate(nullptr, FILE_PATH); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + umf_result = umfFileMemoryProviderParamsDestroy(nullptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); + + umf_result = umfFileMemoryProviderParamsSetPath(nullptr, FILE_PATH); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + umf_result = + umfFileMemoryProviderParamsSetProtection(nullptr, UMF_PROTECTION_READ); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + umf_result = + umfFileMemoryProviderParamsSetVisibility(nullptr, UMF_MEM_MAP_PRIVATE); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + TEST_F(test, create_empty_path) { - umf_memory_provider_handle_t hProvider = nullptr; const char *path = ""; - auto wrong_params = umfFileMemoryProviderParamsDefault((char *)path); - auto ret = umfMemoryProviderCreate(umfFileMemoryProviderOps(), - &wrong_params, &hProvider); - EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); - EXPECT_EQ(hProvider, nullptr); + + umf_file_memory_provider_params_handle_t wrong_params = nullptr; + umf_result_t umf_result = + umfFileMemoryProviderParamsCreate(&wrong_params, path); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + ASSERT_EQ(wrong_params, nullptr); +} + +TEST_F(test, create_null_path) { + const char *path = nullptr; + + umf_file_memory_provider_params_handle_t wrong_params = nullptr; + umf_result_t umf_result = + umfFileMemoryProviderParamsCreate(&wrong_params, path); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + ASSERT_EQ(wrong_params, nullptr); +} + +TEST_F(test, set_empty_path) { + const char *empty_path = ""; + + umf_file_memory_provider_params_handle_t params = nullptr; + umf_result_t umf_result = + umfFileMemoryProviderParamsCreate(¶ms, FILE_PATH); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); + + umf_result = umfFileMemoryProviderParamsSetPath(params, empty_path); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + umf_result = umfFileMemoryProviderParamsDestroy(params); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); +} + +TEST_F(test, set_null_path) { + const char *null_path = nullptr; + + umf_file_memory_provider_params_handle_t params = nullptr; + umf_result_t umf_result = + umfFileMemoryProviderParamsCreate(¶ms, FILE_PATH); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); + + umf_result = umfFileMemoryProviderParamsSetPath(params, null_path); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + umf_result = umfFileMemoryProviderParamsDestroy(params); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } TEST_P(FileProviderParamsDefault, free_INVALID_POINTER_SIZE_GT_0) { @@ -376,7 +471,8 @@ TEST_P(FileProviderParamsDefault, purge_force_INVALID_POINTER) { INSTANTIATE_TEST_SUITE_P(fileProviderTest, FileProviderParamsShared, ::testing::Values(providerCreateExtParams{ - umfFileMemoryProviderOps(), &file_params_shared})); + umfFileMemoryProviderOps(), + file_params_shared.get()})); TEST_P(FileProviderParamsShared, IPC_base_success_test) { umf_result_t umf_result; diff --git a/test/provider_file_memory_ipc.cpp b/test/provider_file_memory_ipc.cpp index 5bba5de501..ee7ab6c8ff 100644 --- a/test/provider_file_memory_ipc.cpp +++ b/test/provider_file_memory_ipc.cpp @@ -17,24 +17,55 @@ using umf_test::test; #define FILE_PATH ((char *)"tmp_file") -umf_file_memory_provider_params_t get_file_params_shared(char *path) { - umf_file_memory_provider_params_t file_params = - umfFileMemoryProviderParamsDefault(path); - file_params.visibility = UMF_MEM_MAP_SHARED; - return file_params; +using file_params_unique_handle_t = + std::unique_ptr; + +file_params_unique_handle_t get_file_params_shared(char *path) { + umf_file_memory_provider_params_handle_t file_params = NULL; + umf_result_t res = umfFileMemoryProviderParamsCreate(&file_params, path); + if (res != UMF_RESULT_SUCCESS) { + throw std::runtime_error( + "Failed to create File Memory Provider params"); + } + + res = umfFileMemoryProviderParamsSetVisibility(file_params, + UMF_MEM_MAP_SHARED); + if (res != UMF_RESULT_SUCCESS) { + umfFileMemoryProviderParamsDestroy(file_params); + throw std::runtime_error("Failed to set visibility to shared for File " + "Memory Provider params"); + } + + return file_params_unique_handle_t(file_params, + &umfFileMemoryProviderParamsDestroy); } -umf_file_memory_provider_params_t file_params_shared = +file_params_unique_handle_t file_params_shared = get_file_params_shared(FILE_PATH); -umf_file_memory_provider_params_t get_file_params_fsdax(char *path) { - umf_file_memory_provider_params_t file_params = - umfFileMemoryProviderParamsDefault(path); - file_params.visibility = UMF_MEM_MAP_SHARED; - return file_params; +file_params_unique_handle_t get_file_params_fsdax(char *path) { + umf_file_memory_provider_params_handle_t file_params = NULL; + umf_result_t res = umfFileMemoryProviderParamsCreate(&file_params, path); + if (res != UMF_RESULT_SUCCESS) { + //test will be skipped. + return file_params_unique_handle_t(nullptr, + &umfFileMemoryProviderParamsDestroy); + } + + res = umfFileMemoryProviderParamsSetVisibility(file_params, + UMF_MEM_MAP_SHARED); + if (res != UMF_RESULT_SUCCESS) { + umfFileMemoryProviderParamsDestroy(file_params); + throw std::runtime_error("Failed to set visibility to shared for File " + "Memory Provider params"); + } + + return file_params_unique_handle_t(file_params, + &umfFileMemoryProviderParamsDestroy); } -umf_file_memory_provider_params_t file_params_fsdax = +file_params_unique_handle_t file_params_fsdax = get_file_params_fsdax(getenv("UMF_TESTS_FSDAX_PATH")); HostMemoryAccessor hostAccessor; @@ -42,14 +73,14 @@ HostMemoryAccessor hostAccessor; static std::vector ipcManyPoolsTestParamsList = { // TODO: enable it when sizes of allocations in ipcFixtures.hpp are fixed // {umfProxyPoolOps(), nullptr, umfFileMemoryProviderOps(), -// &file_params_shared, &hostAccessor, true}, +// file_params_shared.get(), &hostAccessor, true}, #ifdef UMF_POOL_JEMALLOC_ENABLED {umfJemallocPoolOps(), nullptr, umfFileMemoryProviderOps(), - &file_params_shared, &hostAccessor, false}, + file_params_shared.get(), &hostAccessor, false}, #endif #ifdef UMF_POOL_SCALABLE_ENABLED {umfScalablePoolOps(), nullptr, umfFileMemoryProviderOps(), - &file_params_shared, &hostAccessor, false}, + file_params_shared.get(), &hostAccessor, false}, #endif }; @@ -65,14 +96,14 @@ static std::vector getIpcFsDaxTestParamsList(void) { ipcFsDaxTestParamsList = { // TODO: enable it when sizes of allocations in ipcFixtures.hpp are fixed // {umfProxyPoolOps(), nullptr, umfFileMemoryProviderOps(), -// &file_params_fsdax, &hostAccessor, true}, +// file_params_fsdax.get(), &hostAccessor, true}, #ifdef UMF_POOL_JEMALLOC_ENABLED {umfJemallocPoolOps(), nullptr, umfFileMemoryProviderOps(), - &file_params_fsdax, &hostAccessor, false}, + file_params_fsdax.get(), &hostAccessor, false}, #endif #ifdef UMF_POOL_SCALABLE_ENABLED {umfScalablePoolOps(), nullptr, umfFileMemoryProviderOps(), - &file_params_fsdax, &hostAccessor, false}, + file_params_fsdax.get(), &hostAccessor, false}, #endif }; From 31372be8ce1199f924eee9e5a79b845c58bbeff2 Mon Sep 17 00:00:00 2001 From: "Vinogradov, Sergei" Date: Tue, 26 Nov 2024 10:26:57 +0100 Subject: [PATCH 2/2] Add test for not implemented file provider --- test/CMakeLists.txt | 5 ++++ test/provider_file_memory_not_impl.cpp | 33 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 test/provider_file_memory_not_impl.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1f2ef5959d..051e759493 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -338,6 +338,11 @@ if(LINUX AND (NOT UMF_DISABLE_HWLOC)) # OS-specific functions are implemented if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND UMF_BUILD_FUZZTESTS) add_subdirectory(fuzz) endif() +else() + add_umf_test( + NAME provider_file_memory_not_impl + SRCS provider_file_memory_not_impl.cpp + LIBS ${UMF_UTILS_FOR_TEST}) endif() if(UMF_BUILD_GPU_TESTS AND UMF_BUILD_LEVEL_ZERO_PROVIDER) diff --git a/test/provider_file_memory_not_impl.cpp b/test/provider_file_memory_not_impl.cpp new file mode 100644 index 0000000000..c82b8163cb --- /dev/null +++ b/test/provider_file_memory_not_impl.cpp @@ -0,0 +1,33 @@ +// Copyright (C) 2024 Intel Corporation +// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include "base.hpp" + +#include + +using umf_test::test; + +TEST_F(test, file_provider_not_implemented) { + umf_file_memory_provider_params_handle_t params = nullptr; + umf_result_t umf_result = + umfFileMemoryProviderParamsCreate(¶ms, "path"); + EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + EXPECT_EQ(params, nullptr); + + umf_result = umfFileMemoryProviderParamsDestroy(nullptr); + EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + + umf_result = umfFileMemoryProviderParamsSetPath(nullptr, "path"); + EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + + umf_result = umfFileMemoryProviderParamsSetProtection(nullptr, 0); + EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + + umf_result = + umfFileMemoryProviderParamsSetVisibility(nullptr, UMF_MEM_MAP_PRIVATE); + EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + + umf_memory_provider_ops_t *ops = umfFileMemoryProviderOps(); + EXPECT_EQ(ops, nullptr); +} \ No newline at end of file