Skip to content

Commit e6b4824

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

File tree

5 files changed

+258
-4
lines changed

5 files changed

+258
-4
lines changed

.github/workflows/dax.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# The FSDAX device should be mounted in the OS (e.g. /mnt/pmem1)
1313
# and the UMF_TESTS_FSDAX_PATH environment variable
14-
# should contain a path to a file o this FSDAX device.
14+
# should contain a path to a file on this FSDAX device.
1515
#
1616

1717
name: Dax
@@ -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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_dram_and_fsdax 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+
pkg_check_modules(TBB tbb)
30+
if(NOT TBB_FOUND)
31+
find_package(TBB REQUIRED tbb)
32+
endif()
33+
34+
# build the example
35+
set(EXAMPLE_NAME umf_example_dram_and_fsdax)
36+
add_executable(${EXAMPLE_NAME} dram_and_fsdax.c)
37+
target_include_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS})
38+
target_link_directories(
39+
${EXAMPLE_NAME}
40+
PRIVATE
41+
${LIBUMF_LIBRARY_DIRS}
42+
${LIBHWLOC_LIBRARY_DIRS}
43+
${JEMALLOC_LIBRARY_DIRS}
44+
${TBB_LIBRARY_DIRS})
45+
target_link_libraries(
46+
${EXAMPLE_NAME} PRIVATE hwloc jemalloc_pool ${JEMALLOC_LIBRARIES}
47+
${LIBUMF_LIBRARIES})
48+
49+
# an optional part - adds a test of this example
50+
add_test(
51+
NAME ${EXAMPLE_NAME}
52+
COMMAND ${EXAMPLE_NAME}
53+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
54+
55+
set_tests_properties(${EXAMPLE_NAME} PROPERTIES LABELS "example-standalone")
56+
57+
if(LINUX)
58+
# set LD_LIBRARY_PATH
59+
set_property(
60+
TEST ${EXAMPLE_NAME}
61+
PROPERTY
62+
ENVIRONMENT_MODIFICATION
63+
"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}"
64+
)
65+
endif()
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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 an FSDAX 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 an 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 an 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+
// This example requires:
94+
// - the FSDAX device to be mounted in the OS (e.g. /mnt/pmem1) and
95+
// - the UMF_TESTS_FSDAX_PATH environment variable to contain
96+
// a path to a file on this FSDAX device.
97+
char *path = getenv("UMF_TESTS_FSDAX_PATH");
98+
if (path == NULL || path[0] == 0) {
99+
fprintf(
100+
stderr,
101+
"Warning: UMF_TESTS_FSDAX_PATH is not set, skipping testing ...\n");
102+
return 0;
103+
}
104+
105+
umf_memory_pool_handle_t dram_pool = create_dram_pool();
106+
if (dram_pool == NULL) {
107+
fprintf(stderr, "Failed to create a DRAM memory pool!\n");
108+
return -1;
109+
}
110+
111+
fprintf(stderr, "Created a DRAM memory pool\n");
112+
113+
umf_memory_pool_handle_t fsdax_pool = create_fsdax_pool(path);
114+
if (fsdax_pool == NULL) {
115+
fprintf(stderr, "Failed to create an FSDAX memory pool!\n");
116+
return -1;
117+
}
118+
119+
fprintf(stderr, "Created an FSDAX memory pool\n");
120+
121+
size_t size = 2 * 1024 * 1024; // == 2 MB
122+
123+
// Allocate from the DRAM memory pool
124+
char *dram_buf = umfPoolCalloc(dram_pool, 1, size);
125+
if (dram_buf == NULL) {
126+
fprintf(stderr,
127+
"Failed to allocate memory from the DRAM memory pool!\n");
128+
goto err_destroy_pools;
129+
}
130+
131+
fprintf(stderr, "Allocated memory from the DRAM memory pool\n");
132+
133+
// Allocate from the FSDAX memory pool
134+
char *fsdax_buf = umfPoolCalloc(fsdax_pool, 1, size);
135+
if (fsdax_buf == NULL) {
136+
fprintf(stderr,
137+
"Failed to allocate memory from the FSDAX memory pool!\n");
138+
goto err_free_dram;
139+
}
140+
141+
fprintf(stderr, "Allocated memory from the FSDAX memory pool\n");
142+
143+
// Use the allocation from DRAM
144+
dram_buf[0] = '.';
145+
146+
// Use the allocation from FSDAX
147+
fsdax_buf[0] = '.';
148+
149+
// success
150+
ret = 0;
151+
152+
// The file memory provider does not support the free() operation,
153+
// so we do not need to call: umfPoolFree(fsdax_pool, fsdax_buf);
154+
155+
err_free_dram:
156+
fprintf(stderr, "Freeing the allocation from the DRAM memory pool ...\n");
157+
umfPoolFree(dram_pool, dram_buf);
158+
159+
err_destroy_pools:
160+
fprintf(stderr, "Destroying the DRAM memory pool ...\n");
161+
umfPoolDestroy(dram_pool);
162+
163+
fprintf(stderr, "Destroying the FSDAX memory pool ...\n");
164+
umfPoolDestroy(fsdax_pool);
165+
166+
return ret;
167+
}

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)