Skip to content

Commit 3f6cea9

Browse files
committed
Add dram_and_fsdax example
Ref: #742 Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 64de550 commit 3f6cea9

File tree

5 files changed

+246
-3
lines changed

5 files changed

+246
-3
lines changed

.github/workflows/dax.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ jobs:
9696
9797
- name: Run the FSDAX tests
9898
working-directory: ${{env.BUILD_DIR}}
99-
run: >
100-
UMF_TESTS_FSDAX_PATH=${{env.UMF_TESTS_FSDAX_PATH}}
101-
ctest -C ${{matrix.build_type}} -R umf-provider_file_memory -V
99+
run: |
100+
UMF_TESTS_FSDAX_PATH=${{env.UMF_TESTS_FSDAX_PATH}} ctest -C ${{matrix.build_type}} -R umf-provider_file_memory -V
101+
UMF_TESTS_FSDAX_PATH=${{env.UMF_TESTS_FSDAX_PATH}} ctest -C ${{matrix.build_type}} -R umf_example_dram_and_fsdax -V

examples/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ if(LINUX)
269269
COMMAND ${EXAMPLE_NAME}
270270
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
271271

272+
if(UMF_BUILD_LIBUMF_POOL_JEMALLOC)
273+
set(EXAMPLE_NAME umf_example_dram_and_fsdax)
274+
275+
add_umf_executable(
276+
NAME ${EXAMPLE_NAME}
277+
SRCS dram_and_fsdax/dram_and_fsdax.c
278+
LIBS umf jemalloc_pool)
279+
280+
add_test(
281+
NAME ${EXAMPLE_NAME}
282+
COMMAND ${EXAMPLE_NAME}
283+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
284+
endif()
272285
else()
273286
message(
274287
STATUS "Memspace examples API are supported on Linux only - skipping")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
6+
project(umf_example_basic LANGUAGES C)
7+
enable_testing()
8+
9+
set(UMF_EXAMPLE_DIR "${CMAKE_SOURCE_DIR}/..")
10+
list(APPEND CMAKE_MODULE_PATH "${UMF_EXAMPLE_DIR}/cmake")
11+
message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}")
12+
13+
find_package(PkgConfig)
14+
pkg_check_modules(LIBUMF libumf)
15+
if(NOT LIBUMF_FOUND)
16+
find_package(LIBUMF REQUIRED libumf)
17+
endif()
18+
19+
pkg_check_modules(LIBHWLOC hwloc>=2.3.0)
20+
if(NOT LIBHWLOC_FOUND)
21+
find_package(LIBHWLOC 2.3.0 REQUIRED hwloc)
22+
endif()
23+
24+
pkg_check_modules(JEMALLOC jemalloc)
25+
if(NOT JEMALLOC_FOUND)
26+
find_package(JEMALLOC REQUIRED jemalloc)
27+
endif()
28+
29+
# build the example
30+
set(EXAMPLE_NAME umf_example_dram_and_fsdax)
31+
add_executable(${EXAMPLE_NAME} dram_and_fsdax.c)
32+
target_include_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS})
33+
target_link_directories(
34+
${EXAMPLE_NAME}
35+
PRIVATE
36+
${LIBUMF_LIBRARY_DIRS}
37+
${LIBHWLOC_LIBRARY_DIRS}
38+
${JEMALLOC_LIBRARY_DIRS})
39+
target_link_libraries(
40+
${EXAMPLE_NAME} PRIVATE hwloc jemalloc_pool ${JEMALLOC_LIBRARIES}
41+
${LIBUMF_LIBRARIES})
42+
43+
# an optional part - adds a test of this example
44+
add_test(
45+
NAME ${EXAMPLE_NAME}
46+
COMMAND ${EXAMPLE_NAME}
47+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
48+
49+
set_tests_properties(${EXAMPLE_NAME} PROPERTIES LABELS "example-standalone")
50+
51+
if(LINUX)
52+
# set LD_LIBRARY_PATH
53+
set_property(
54+
TEST ${EXAMPLE_NAME}
55+
PROPERTY
56+
ENVIRONMENT_MODIFICATION
57+
"LD_LIBRARY_PATH=path_list_append:${LIBUMF_LIBRARY_DIRS};LD_LIBRARY_PATH=path_list_append:${LIBHWLOC_LIBRARY_DIRS};LD_LIBRARY_PATH=path_list_append:${JEMALLOC_LIBRARY_DIRS}"
58+
)
59+
endif()
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
#include <umf/memory_provider.h>
13+
#include <umf/providers/provider_file_memory.h>
14+
#include <umf/providers/provider_os_memory.h>
15+
16+
#include <umf/memory_pool.h>
17+
#include <umf/pools/pool_jemalloc.h>
18+
19+
static umf_memory_pool_handle_t create_dram_pool() {
20+
umf_memory_provider_handle_t provider_dram = NULL;
21+
umf_memory_pool_handle_t pool_dram;
22+
umf_result_t umf_result;
23+
24+
umf_os_memory_provider_params_t params_dram =
25+
umfOsMemoryProviderParamsDefault();
26+
27+
umf_result = umfMemoryProviderCreate(umfOsMemoryProviderOps(), &params_dram,
28+
&provider_dram);
29+
if (umf_result != UMF_RESULT_SUCCESS) {
30+
fprintf(stderr, "Creation of the OS memory provider failed");
31+
return NULL;
32+
}
33+
34+
// Create a DRAM memory pool
35+
umf_result = umfPoolCreate(umfJemallocPoolOps(), provider_dram, NULL,
36+
UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &pool_dram);
37+
if (umf_result != UMF_RESULT_SUCCESS) {
38+
fprintf(stderr, "Failed to create a DRAM memory pool!\n");
39+
umfMemoryProviderDestroy(provider_dram);
40+
return NULL;
41+
}
42+
43+
printf("DRAM memory pool created at %p\n", (void *)pool_dram);
44+
45+
return pool_dram;
46+
}
47+
48+
static umf_memory_pool_handle_t create_fsdax_pool(const char *path) {
49+
umf_memory_provider_handle_t provider_fsdax = NULL;
50+
umf_memory_pool_handle_t pool_fsdax;
51+
umf_result_t umf_result;
52+
53+
umf_file_memory_provider_params_t params_fsdax =
54+
umfFileMemoryProviderParamsDefault(path);
55+
// FSDAX requires mapping the UMF_MEM_MAP_SYNC flag
56+
params_fsdax.visibility = UMF_MEM_MAP_SYNC;
57+
58+
umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(),
59+
&params_fsdax, &provider_fsdax);
60+
if (umf_result != UMF_RESULT_SUCCESS) {
61+
fprintf(stderr, "failed to create the FSDAX file provider");
62+
return NULL;
63+
}
64+
65+
// Create a DRAM memory pool
66+
//
67+
// The file memory provider does not support the free operation
68+
// (`umfMemoryProviderFree()` always returns `UMF_RESULT_ERROR_NOT_SUPPORTED`),
69+
// so it should be used with a pool manager that will take over
70+
// the managing of the provided memory - for example the jemalloc pool
71+
// with the `disable_provider_free` parameter set to true.
72+
umf_jemalloc_pool_params_t pool_params;
73+
pool_params.disable_provider_free = true;
74+
75+
// Create a FSDAX memory pool
76+
umf_result =
77+
umfPoolCreate(umfJemallocPoolOps(), provider_fsdax, &pool_params,
78+
UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &pool_fsdax);
79+
if (umf_result != UMF_RESULT_SUCCESS) {
80+
fprintf(stderr, "Failed to create a FSDAX memory pool!\n");
81+
umfMemoryProviderDestroy(provider_fsdax);
82+
return NULL;
83+
}
84+
85+
printf("FSDAX memory pool created at %p\n", (void *)pool_fsdax);
86+
87+
return pool_fsdax;
88+
}
89+
90+
int main(void) {
91+
int ret = -1;
92+
93+
char *path = getenv("UMF_TESTS_FSDAX_PATH");
94+
if (path == NULL || path[0] == 0) {
95+
fprintf(stderr,
96+
"Warning: UMF_TESTS_FSDAX_PATH is not set, skipping ...\n");
97+
return 0;
98+
}
99+
100+
umf_memory_pool_handle_t dram_pool = create_dram_pool();
101+
if (dram_pool == NULL) {
102+
fprintf(stderr, "Failed to create a DRAM memory pool!\n");
103+
return -1;
104+
}
105+
106+
fprintf(stderr, "Created a DRAM memory pool\n");
107+
108+
umf_memory_pool_handle_t fsdax_pool = create_fsdax_pool(path);
109+
if (fsdax_pool == NULL) {
110+
fprintf(stderr, "Failed to create a FSDAX memory pool!\n");
111+
return -1;
112+
}
113+
114+
fprintf(stderr, "Created an FSDAX memory pool\n");
115+
116+
size_t size = 2 * 1024 * 1024; // == 2 MB
117+
118+
// Allocate from the DRAM memory pool
119+
char *dram_buf = umfPoolCalloc(dram_pool, 1, size);
120+
if (dram_buf == NULL) {
121+
fprintf(stderr,
122+
"Failed to allocate memory from the DRAM memory pool!\n");
123+
goto err_destroy_pools;
124+
}
125+
126+
fprintf(stderr, "Allocated memory from the DRAM memory pool\n");
127+
128+
// Allocate from the FSDAX memory pool
129+
char *fsdax_buf = umfPoolCalloc(fsdax_pool, 1, size);
130+
if (fsdax_buf == NULL) {
131+
fprintf(stderr,
132+
"Failed to allocate memory from the FSDAX memory pool!\n");
133+
goto err_free_dram;
134+
}
135+
136+
fprintf(stderr, "Allocated memory from the FSDAX memory pool\n");
137+
138+
// Use the allocation from DRAM
139+
dram_buf[0] = '.';
140+
141+
// Use the allocation from FSDAX
142+
fsdax_buf[0] = '.';
143+
144+
// success
145+
ret = 0;
146+
147+
// The file memory provider does not support the free() operation,
148+
// so we do need to call: umfPoolFree(fsdax_pool, fsdax_buf);
149+
150+
err_free_dram:
151+
fprintf(stderr, "Freeing the allocation from the DRAM memory pool ...\n");
152+
umfPoolFree(dram_pool, dram_buf);
153+
154+
err_destroy_pools:
155+
fprintf(stderr, "Destroying the DRAM memory pool ...\n");
156+
umfPoolDestroy(dram_pool);
157+
158+
fprintf(stderr, "Destroying the FSDAX memory pool ...\n");
159+
umfPoolDestroy(fsdax_pool);
160+
161+
return ret;
162+
}

test/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,15 @@ if(LINUX
550550
)
551551
endif()
552552

553+
if(LINUX AND UMF_BUILD_LIBUMF_POOL_JEMALLOC)
554+
set(EXAMPLES ${EXAMPLES} dram_and_fsdax)
555+
else()
556+
message(
557+
STATUS
558+
"The dram_and_fsdax example is supported on Linux only and requires UMF_BUILD_LIBUMF_POOL_JEMALLOC to be turned ON - skipping"
559+
)
560+
endif()
561+
553562
if(EXAMPLES AND NOT UMF_DISABLE_HWLOC)
554563
add_test(
555564
NAME umf_standalone_examples

0 commit comments

Comments
 (0)