Skip to content

Update File provider config API #927

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
Nov 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
20 changes: 16 additions & 4 deletions examples/dram_and_fsdax/dram_and_fsdax.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params_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(),
&params_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;
Expand Down
60 changes: 39 additions & 21 deletions include/umf/providers/provider_file_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/libumf.def
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ EXPORTS
umfDevDaxMemoryProviderOps
umfFree
umfFileMemoryProviderOps
umfFileMemoryProviderParamsCreate
umfFileMemoryProviderParamsDestroy
umfFileMemoryProviderParamsSetPath
umfFileMemoryProviderParamsSetProtection
umfFileMemoryProviderParamsSetVisibility
umfGetIPCHandle
umfGetLastFailedMemoryProvider
umfLevelZeroMemoryProviderOps
Expand Down
5 changes: 5 additions & 0 deletions src/libumf.map
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ UMF_1.0 {
umfDevDaxMemoryProviderOps;
umfFree;
umfFileMemoryProviderOps;
umfFileMemoryProviderParamsCreate;
umfFileMemoryProviderParamsDestroy;
umfFileMemoryProviderParamsSetPath;
umfFileMemoryProviderParamsSetProtection;
umfFileMemoryProviderParamsSetVisibility;
umfGetIPCHandle;
umfGetLastFailedMemoryProvider;
umfLevelZeroMemoryProviderOps;
Expand Down
147 changes: 147 additions & 0 deletions src/provider/provider_file_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 26 additions & 6 deletions test/ipc_file_prov_consumer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
32 changes: 26 additions & 6 deletions test/ipc_file_prov_producer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading
Loading