Skip to content

Commit e53e52f

Browse files
authored
Merge pull request #927 from vinser52/svinogra_file_params
Update File provider config API
2 parents e226aff + 31372be commit e53e52f

13 files changed

+503
-79
lines changed

examples/dram_and_fsdax/dram_and_fsdax.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,25 @@ static umf_memory_pool_handle_t create_fsdax_pool(const char *path) {
4848
umf_memory_pool_handle_t pool_fsdax;
4949
umf_result_t umf_result;
5050

51-
umf_file_memory_provider_params_t params_fsdax =
52-
umfFileMemoryProviderParamsDefault(path);
51+
umf_file_memory_provider_params_handle_t params_fsdax = NULL;
52+
umf_result = umfFileMemoryProviderParamsCreate(&params_fsdax, path);
53+
if (umf_result != UMF_RESULT_SUCCESS) {
54+
fprintf(stderr, "Failed to create the File Memory Provider params");
55+
return NULL;
56+
}
5357
// FSDAX requires mapping the UMF_MEM_MAP_SHARED flag
54-
params_fsdax.visibility = UMF_MEM_MAP_SHARED;
58+
umf_result = umfFileMemoryProviderParamsSetVisibility(params_fsdax,
59+
UMF_MEM_MAP_SHARED);
60+
if (umf_result != UMF_RESULT_SUCCESS) {
61+
fprintf(stderr,
62+
"Failed to set the visibility of the FSDAX file provider");
63+
umfFileMemoryProviderParamsDestroy(params_fsdax);
64+
return NULL;
65+
}
5566

5667
umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(),
57-
&params_fsdax, &provider_fsdax);
68+
params_fsdax, &provider_fsdax);
69+
umfFileMemoryProviderParamsDestroy(params_fsdax);
5870
if (umf_result != UMF_RESULT_SUCCESS) {
5971
fprintf(stderr, "Failed to create the FSDAX file provider");
6072
return NULL;

include/umf/providers/provider_file_memory.h

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,45 @@ extern "C" {
1818
#define UMF_FILE_RESULTS_START_FROM 3000
1919
/// @endcond
2020

21-
/// @brief Memory provider settings struct
22-
typedef struct umf_file_memory_provider_params_t {
23-
/// a path to the file (of maximum length PATH_MAX characters)
24-
const char *path;
25-
/// combination of 'umf_mem_protection_flags_t' flags
26-
unsigned protection;
27-
/// memory visibility mode
28-
umf_memory_visibility_t visibility;
29-
} umf_file_memory_provider_params_t;
21+
struct umf_file_memory_provider_params_t;
22+
23+
typedef struct umf_file_memory_provider_params_t
24+
*umf_file_memory_provider_params_handle_t;
25+
26+
/// @brief Create a struct to store parameters of the File Memory Provider.
27+
/// @param hParams [out] handle to the newly created parameters struct.
28+
/// @param path path to the file.
29+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
30+
umf_result_t umfFileMemoryProviderParamsCreate(
31+
umf_file_memory_provider_params_handle_t *hParams, const char *path);
32+
33+
/// @brief Destroy parameters struct.
34+
/// @param hParams handle to the parameters of the File Memory Provider.
35+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
36+
umf_result_t umfFileMemoryProviderParamsDestroy(
37+
umf_file_memory_provider_params_handle_t hParams);
38+
39+
/// @brief Set the path in the parameters struct.
40+
/// @param hParams handle to the parameters of the File Memory Provider.
41+
/// @param path path to the file.
42+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
43+
umf_result_t umfFileMemoryProviderParamsSetPath(
44+
umf_file_memory_provider_params_handle_t hParams, const char *path);
45+
46+
/// @brief Set the protection in the parameters struct.
47+
/// @param hParams handle to the parameters of the File Memory Provider.
48+
/// @param protection protection. Combination of \p umf_mem_protection_flags_t flags
49+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
50+
umf_result_t umfFileMemoryProviderParamsSetProtection(
51+
umf_file_memory_provider_params_handle_t hParams, unsigned protection);
52+
53+
/// @brief Set the visibility in the parameters struct.
54+
/// @param hParams handle to the parameters of the File Memory Provider.
55+
/// @param visibility memory visibility mode.
56+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
57+
umf_result_t umfFileMemoryProviderParamsSetVisibility(
58+
umf_file_memory_provider_params_handle_t hParams,
59+
umf_memory_visibility_t visibility);
3060

3161
/// @brief File Memory Provider operation results
3262
typedef enum umf_file_memory_provider_native_error {
@@ -38,18 +68,6 @@ typedef enum umf_file_memory_provider_native_error {
3868

3969
umf_memory_provider_ops_t *umfFileMemoryProviderOps(void);
4070

41-
/// @brief Create default params for the file memory provider
42-
static inline umf_file_memory_provider_params_t
43-
umfFileMemoryProviderParamsDefault(const char *path) {
44-
umf_file_memory_provider_params_t params = {
45-
path, /* a path to the file */
46-
UMF_PROTECTION_READ | UMF_PROTECTION_WRITE, /* protection */
47-
UMF_MEM_MAP_PRIVATE, /* visibility mode */
48-
};
49-
50-
return params;
51-
}
52-
5371
#ifdef __cplusplus
5472
}
5573
#endif

src/libumf.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ EXPORTS
2525
umfDevDaxMemoryProviderOps
2626
umfFree
2727
umfFileMemoryProviderOps
28+
umfFileMemoryProviderParamsCreate
29+
umfFileMemoryProviderParamsDestroy
30+
umfFileMemoryProviderParamsSetPath
31+
umfFileMemoryProviderParamsSetProtection
32+
umfFileMemoryProviderParamsSetVisibility
2833
umfGetIPCHandle
2934
umfGetLastFailedMemoryProvider
3035
umfLevelZeroMemoryProviderOps

src/libumf.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ UMF_1.0 {
1919
umfDevDaxMemoryProviderOps;
2020
umfFree;
2121
umfFileMemoryProviderOps;
22+
umfFileMemoryProviderParamsCreate;
23+
umfFileMemoryProviderParamsDestroy;
24+
umfFileMemoryProviderParamsSetPath;
25+
umfFileMemoryProviderParamsSetProtection;
26+
umfFileMemoryProviderParamsSetVisibility;
2227
umfGetIPCHandle;
2328
umfGetLastFailedMemoryProvider;
2429
umfLevelZeroMemoryProviderOps;

src/provider/provider_file_memory.c

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,46 @@ umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) {
2525
return NULL;
2626
}
2727

28+
umf_result_t umfFileMemoryProviderParamsCreate(
29+
umf_file_memory_provider_params_handle_t *hParams, const char *path) {
30+
(void)hParams;
31+
(void)path;
32+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
33+
}
34+
35+
umf_result_t umfFileMemoryProviderParamsDestroy(
36+
umf_file_memory_provider_params_handle_t hParams) {
37+
(void)hParams;
38+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
39+
}
40+
41+
umf_result_t umfFileMemoryProviderParamsSetPath(
42+
umf_file_memory_provider_params_handle_t hParams, const char *path) {
43+
(void)hParams;
44+
(void)path;
45+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
46+
}
47+
48+
umf_result_t umfFileMemoryProviderParamsSetProtection(
49+
umf_file_memory_provider_params_handle_t hParams, unsigned protection) {
50+
(void)hParams;
51+
(void)protection;
52+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
53+
}
54+
55+
umf_result_t umfFileMemoryProviderParamsSetVisibility(
56+
umf_file_memory_provider_params_handle_t hParams,
57+
umf_memory_visibility_t visibility) {
58+
(void)hParams;
59+
(void)visibility;
60+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
61+
}
62+
2863
#else // !defined(_WIN32) && !defined(UMF_NO_HWLOC)
2964

3065
#include "base_alloc_global.h"
3166
#include "critnib.h"
67+
#include "libumf.h"
3268
#include "utils_common.h"
3369
#include "utils_concurrency.h"
3470
#include "utils_log.h"
@@ -67,6 +103,13 @@ typedef struct file_memory_provider_t {
67103
critnib *fd_offset_map;
68104
} file_memory_provider_t;
69105

106+
// File Memory Provider settings struct
107+
typedef struct umf_file_memory_provider_params_t {
108+
char *path;
109+
unsigned protection;
110+
umf_memory_visibility_t visibility;
111+
} umf_file_memory_provider_params_t;
112+
70113
typedef struct file_last_native_error_t {
71114
int32_t native_error;
72115
int errno_value;
@@ -748,4 +791,108 @@ umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) {
748791
return &UMF_FILE_MEMORY_PROVIDER_OPS;
749792
}
750793

794+
umf_result_t umfFileMemoryProviderParamsCreate(
795+
umf_file_memory_provider_params_handle_t *hParams, const char *path) {
796+
libumfInit();
797+
if (hParams == NULL) {
798+
LOG_ERR("File Memory Provider params handle is NULL");
799+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
800+
}
801+
802+
if (path == NULL) {
803+
LOG_ERR("File path is NULL");
804+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
805+
}
806+
807+
umf_file_memory_provider_params_handle_t params =
808+
umf_ba_global_alloc(sizeof(*params));
809+
if (params == NULL) {
810+
LOG_ERR("allocating memory for File Memory Provider params failed");
811+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
812+
}
813+
814+
params->path = NULL;
815+
params->protection = UMF_PROTECTION_READ | UMF_PROTECTION_WRITE;
816+
params->visibility = UMF_MEM_MAP_PRIVATE;
817+
818+
umf_result_t res = umfFileMemoryProviderParamsSetPath(params, path);
819+
if (res != UMF_RESULT_SUCCESS) {
820+
umf_ba_global_free(params);
821+
return res;
822+
}
823+
824+
*hParams = params;
825+
826+
return UMF_RESULT_SUCCESS;
827+
}
828+
829+
umf_result_t umfFileMemoryProviderParamsDestroy(
830+
umf_file_memory_provider_params_handle_t hParams) {
831+
if (hParams != NULL) {
832+
umf_ba_global_free(hParams->path);
833+
umf_ba_global_free(hParams);
834+
}
835+
836+
return UMF_RESULT_SUCCESS;
837+
}
838+
839+
umf_result_t umfFileMemoryProviderParamsSetPath(
840+
umf_file_memory_provider_params_handle_t hParams, const char *path) {
841+
if (hParams == NULL) {
842+
LOG_ERR("File Memory Provider params handle is NULL");
843+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
844+
}
845+
846+
if (path == NULL) {
847+
LOG_ERR("File path is NULL");
848+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
849+
}
850+
851+
size_t len = strlen(path);
852+
if (len == 0) {
853+
LOG_ERR("File path is empty");
854+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
855+
}
856+
857+
len += 1; // for the null terminator
858+
char *new_path = NULL;
859+
new_path = umf_ba_global_alloc(len);
860+
if (new_path == NULL) {
861+
LOG_ERR("allocating memory for the file path failed");
862+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
863+
}
864+
865+
strncpy(new_path, path, len);
866+
867+
umf_ba_global_free(hParams->path);
868+
hParams->path = new_path;
869+
870+
return UMF_RESULT_SUCCESS;
871+
}
872+
873+
umf_result_t umfFileMemoryProviderParamsSetProtection(
874+
umf_file_memory_provider_params_handle_t hParams, unsigned protection) {
875+
if (hParams == NULL) {
876+
LOG_ERR("File Memory Provider params handle is NULL");
877+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
878+
}
879+
880+
hParams->protection = protection;
881+
882+
return UMF_RESULT_SUCCESS;
883+
}
884+
885+
umf_result_t umfFileMemoryProviderParamsSetVisibility(
886+
umf_file_memory_provider_params_handle_t hParams,
887+
umf_memory_visibility_t visibility) {
888+
if (hParams == NULL) {
889+
LOG_ERR("File Memory Provider params handle is NULL");
890+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
891+
}
892+
893+
hParams->visibility = visibility;
894+
895+
return UMF_RESULT_SUCCESS;
896+
}
897+
751898
#endif // !defined(_WIN32) && !defined(UMF_NO_HWLOC)

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ if(LINUX AND (NOT UMF_DISABLE_HWLOC)) # OS-specific functions are implemented
338338
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND UMF_BUILD_FUZZTESTS)
339339
add_subdirectory(fuzz)
340340
endif()
341+
else()
342+
add_umf_test(
343+
NAME provider_file_memory_not_impl
344+
SRCS provider_file_memory_not_impl.cpp
345+
LIBS ${UMF_UTILS_FOR_TEST})
341346
endif()
342347

343348
if(UMF_BUILD_GPU_TESTS AND UMF_BUILD_LEVEL_ZERO_PROVIDER)

test/ipc_file_prov_consumer.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,36 @@ int main(int argc, char *argv[]) {
2222
return -1;
2323
}
2424

25+
int ret = 0;
2526
int port = atoi(argv[1]);
2627
char *file_name = argv[2];
2728

28-
umf_file_memory_provider_params_t file_params;
29-
file_params = umfFileMemoryProviderParamsDefault(file_name);
30-
file_params.visibility = UMF_MEM_MAP_SHARED;
29+
umf_file_memory_provider_params_handle_t file_params = NULL;
30+
umf_result_t umf_result =
31+
umfFileMemoryProviderParamsCreate(&file_params, file_name);
32+
if (umf_result != UMF_RESULT_SUCCESS) {
33+
fprintf(
34+
stderr,
35+
"[consumer] ERROR: creating File Memory Provider params failed\n");
36+
return -1;
37+
}
38+
39+
umf_result = umfFileMemoryProviderParamsSetVisibility(file_params,
40+
UMF_MEM_MAP_SHARED);
41+
if (umf_result != UMF_RESULT_SUCCESS) {
42+
fprintf(stderr, "[consumer] ERROR: setting File Memory Provider "
43+
"visibility failed\n");
44+
ret = -1;
45+
goto destroy_provider_params;
46+
}
3147

3248
void *pool_params = NULL;
3349

34-
return run_consumer(port, umfScalablePoolOps(), pool_params,
35-
umfFileMemoryProviderOps(), &file_params, memcopy,
36-
NULL);
50+
ret = run_consumer(port, umfScalablePoolOps(), pool_params,
51+
umfFileMemoryProviderOps(), file_params, memcopy, NULL);
52+
53+
destroy_provider_params:
54+
umfFileMemoryProviderParamsDestroy(file_params);
55+
56+
return ret;
3757
}

test/ipc_file_prov_producer.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,36 @@ int main(int argc, char *argv[]) {
2222
return -1;
2323
}
2424

25+
int ret = 0;
2526
int port = atoi(argv[1]);
2627
char *file_name = argv[2];
2728

28-
umf_file_memory_provider_params_t file_params;
29-
file_params = umfFileMemoryProviderParamsDefault(file_name);
30-
file_params.visibility = UMF_MEM_MAP_SHARED;
29+
umf_file_memory_provider_params_handle_t file_params = NULL;
30+
umf_result_t umf_result =
31+
umfFileMemoryProviderParamsCreate(&file_params, file_name);
32+
if (umf_result != UMF_RESULT_SUCCESS) {
33+
fprintf(
34+
stderr,
35+
"[producer] ERROR: creating File Memory Provider params failed\n");
36+
return -1;
37+
}
38+
39+
umf_result = umfFileMemoryProviderParamsSetVisibility(file_params,
40+
UMF_MEM_MAP_SHARED);
41+
if (umf_result != UMF_RESULT_SUCCESS) {
42+
fprintf(stderr, "[producer] ERROR: setting File Memory Provider "
43+
"visibility failed\n");
44+
ret = -1;
45+
goto destroy_provider_params;
46+
}
3147

3248
void *pool_params = NULL;
3349

34-
return run_producer(port, umfScalablePoolOps(), pool_params,
35-
umfFileMemoryProviderOps(), &file_params, memcopy,
36-
NULL);
50+
ret = run_producer(port, umfScalablePoolOps(), pool_params,
51+
umfFileMemoryProviderOps(), file_params, memcopy, NULL);
52+
53+
destroy_provider_params:
54+
umfFileMemoryProviderParamsDestroy(file_params);
55+
56+
return ret;
3757
}

0 commit comments

Comments
 (0)