-
Notifications
You must be signed in to change notification settings - Fork 35
Add dram_and_fsdax example #750
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
lukaszstolarczuk
merged 2 commits into
oneapi-src:main
from
ldorau:Add_dram_and_fsdax_example
Sep 26, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 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 | ||
|
||
message(STATUS "Checking for module 'jemalloc' using find_library()") | ||
|
||
find_library(JEMALLOC_LIBRARY NAMES libjemalloc jemalloc) | ||
set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY}) | ||
|
||
get_filename_component(JEMALLOC_LIB_DIR ${JEMALLOC_LIBRARIES} DIRECTORY) | ||
set(JEMALLOC_LIBRARY_DIRS ${JEMALLOC_LIB_DIR}) | ||
|
||
find_file(JEMALLOC_HEADER NAMES "jemalloc/jemalloc.h") | ||
if(JEMALLOC_HEADER) | ||
get_filename_component(JEMALLOC_INCLUDE_DIR_TBB ${JEMALLOC_HEADER} | ||
DIRECTORY) | ||
get_filename_component(JEMALLOC_INCLUDE_DIR ${JEMALLOC_INCLUDE_DIR_TBB} | ||
DIRECTORY) | ||
set(JEMALLOC_INCLUDE_DIRS ${JEMALLOC_INCLUDE_DIR}) | ||
else() | ||
set(MSG_NOT_FOUND "<jemalloc/jemalloc.h> header NOT found " | ||
"(set CMAKE_PREFIX_PATH to point the location)") | ||
if(JEMALLOC_FIND_REQUIRED) | ||
message(FATAL_ERROR ${MSG_NOT_FOUND}) | ||
else() | ||
message(WARNING ${MSG_NOT_FOUND}) | ||
endif() | ||
endif() | ||
|
||
if(WINDOWS) | ||
find_file(JEMALLOC_DLL NAMES "bin/jemalloc.dll" "jemalloc.dll") | ||
get_filename_component(JEMALLOC_DLL_DIR ${JEMALLOC_DLL} DIRECTORY) | ||
set(JEMALLOC_DLL_DIRS ${JEMALLOC_DLL_DIR}) | ||
endif() | ||
|
||
if(JEMALLOC_LIBRARY) | ||
message(STATUS " Found jemalloc using find_library()") | ||
message(STATUS " JEMALLOC_LIBRARIES = ${JEMALLOC_LIBRARIES}") | ||
message(STATUS " JEMALLOC_INCLUDE_DIRS = ${JEMALLOC_INCLUDE_DIRS}") | ||
message(STATUS " JEMALLOC_LIBRARY_DIRS = ${JEMALLOC_LIBRARY_DIRS}") | ||
if(WINDOWS) | ||
message(STATUS " JEMALLOC_DLL_DIRS = ${JEMALLOC_DLL_DIRS}") | ||
endif() | ||
else() | ||
set(MSG_NOT_FOUND | ||
"jemalloc NOT found (set CMAKE_PREFIX_PATH to point the location)") | ||
if(JEMALLOC_FIND_REQUIRED) | ||
message(FATAL_ERROR ${MSG_NOT_FOUND}) | ||
else() | ||
message(WARNING ${MSG_NOT_FOUND}) | ||
endif() | ||
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 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 | ||
|
||
cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR) | ||
project(umf_example_dram_and_fsdax LANGUAGES C) | ||
enable_testing() | ||
|
||
set(UMF_EXAMPLE_DIR "${CMAKE_SOURCE_DIR}/..") | ||
list(APPEND CMAKE_MODULE_PATH "${UMF_EXAMPLE_DIR}/cmake") | ||
message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") | ||
|
||
find_package(PkgConfig) | ||
pkg_check_modules(LIBUMF libumf) | ||
if(NOT LIBUMF_FOUND) | ||
find_package(LIBUMF REQUIRED libumf) | ||
endif() | ||
|
||
pkg_check_modules(LIBHWLOC hwloc>=2.3.0) | ||
if(NOT LIBHWLOC_FOUND) | ||
find_package(LIBHWLOC 2.3.0 REQUIRED hwloc) | ||
endif() | ||
|
||
pkg_check_modules(JEMALLOC jemalloc) | ||
if(NOT JEMALLOC_FOUND) | ||
find_package(JEMALLOC REQUIRED jemalloc) | ||
endif() | ||
|
||
# build the example | ||
set(EXAMPLE_NAME umf_example_dram_and_fsdax) | ||
add_executable(${EXAMPLE_NAME} dram_and_fsdax.c) | ||
target_include_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS}) | ||
|
||
target_link_directories( | ||
${EXAMPLE_NAME} | ||
PRIVATE | ||
${LIBUMF_LIBRARY_DIRS} | ||
${LIBHWLOC_LIBRARY_DIRS} | ||
${JEMALLOC_LIBRARY_DIRS}) | ||
|
||
target_link_libraries( | ||
${EXAMPLE_NAME} PRIVATE hwloc jemalloc_pool ${JEMALLOC_LIBRARIES} | ||
${LIBUMF_LIBRARIES}) | ||
|
||
# an optional part - adds a test of this example | ||
add_test( | ||
NAME ${EXAMPLE_NAME} | ||
COMMAND ${EXAMPLE_NAME} | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
||
set_tests_properties(${EXAMPLE_NAME} PROPERTIES LABELS "example-standalone") | ||
|
||
if(LINUX) | ||
# set LD_LIBRARY_PATH | ||
set_property( | ||
TEST ${EXAMPLE_NAME} | ||
PROPERTY | ||
ENVIRONMENT_MODIFICATION | ||
"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}" | ||
) | ||
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* 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 <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include <umf/memory_provider.h> | ||
#include <umf/providers/provider_file_memory.h> | ||
#include <umf/providers/provider_os_memory.h> | ||
|
||
#include <umf/memory_pool.h> | ||
#include <umf/pools/pool_jemalloc.h> | ||
|
||
static umf_memory_pool_handle_t create_dram_pool(void) { | ||
umf_memory_provider_handle_t provider_dram = NULL; | ||
umf_memory_pool_handle_t pool_dram; | ||
umf_result_t umf_result; | ||
|
||
umf_os_memory_provider_params_t params_dram = | ||
umfOsMemoryProviderParamsDefault(); | ||
|
||
umf_result = umfMemoryProviderCreate(umfOsMemoryProviderOps(), ¶ms_dram, | ||
&provider_dram); | ||
if (umf_result != UMF_RESULT_SUCCESS) { | ||
fprintf(stderr, "Creation of the OS memory provider failed"); | ||
return NULL; | ||
} | ||
|
||
// Create a DRAM memory pool | ||
umf_result = umfPoolCreate(umfJemallocPoolOps(), provider_dram, NULL, | ||
UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &pool_dram); | ||
if (umf_result != UMF_RESULT_SUCCESS) { | ||
fprintf(stderr, "Failed to create a DRAM memory pool!\n"); | ||
umfMemoryProviderDestroy(provider_dram); | ||
return NULL; | ||
} | ||
|
||
return pool_dram; | ||
} | ||
|
||
static umf_memory_pool_handle_t create_fsdax_pool(const char *path) { | ||
umf_memory_provider_handle_t provider_fsdax = NULL; | ||
umf_memory_pool_handle_t pool_fsdax; | ||
umf_result_t umf_result; | ||
|
||
umf_file_memory_provider_params_t params_fsdax = | ||
umfFileMemoryProviderParamsDefault(path); | ||
// FSDAX requires mapping the UMF_MEM_MAP_SYNC flag | ||
params_fsdax.visibility = UMF_MEM_MAP_SYNC; | ||
|
||
umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(), | ||
¶ms_fsdax, &provider_fsdax); | ||
if (umf_result != UMF_RESULT_SUCCESS) { | ||
fprintf(stderr, "Failed to create the FSDAX file provider"); | ||
return NULL; | ||
} | ||
|
||
// Create an FSDAX memory pool | ||
// | ||
// The file memory provider does not support the free operation | ||
// (`umfMemoryProviderFree()` always returns `UMF_RESULT_ERROR_NOT_SUPPORTED`), | ||
// so it should be used with a pool manager that will take over | ||
// the managing of the provided memory - for example the jemalloc pool | ||
// with the `disable_provider_free` parameter set to true. | ||
umf_jemalloc_pool_params_t pool_params; | ||
pool_params.disable_provider_free = true; | ||
|
||
// Create an FSDAX memory pool | ||
umf_result = | ||
umfPoolCreate(umfJemallocPoolOps(), provider_fsdax, &pool_params, | ||
UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &pool_fsdax); | ||
if (umf_result != UMF_RESULT_SUCCESS) { | ||
fprintf(stderr, "Failed to create an FSDAX memory pool!\n"); | ||
umfMemoryProviderDestroy(provider_fsdax); | ||
return NULL; | ||
} | ||
|
||
return pool_fsdax; | ||
} | ||
|
||
int main(void) { | ||
int ret = -1; | ||
|
||
// This example requires: | ||
// - the FSDAX device to be mounted in the OS (e.g. /mnt/pmem1) and | ||
// - the UMF_TESTS_FSDAX_PATH environment variable to contain | ||
// a path to a file on this FSDAX device. | ||
char *path = getenv("UMF_TESTS_FSDAX_PATH"); | ||
if (path == NULL || path[0] == 0) { | ||
fprintf( | ||
stderr, | ||
"Warning: UMF_TESTS_FSDAX_PATH is not set, skipping testing ...\n"); | ||
return 0; | ||
} | ||
|
||
umf_memory_pool_handle_t dram_pool = create_dram_pool(); | ||
if (dram_pool == NULL) { | ||
fprintf(stderr, "Failed to create a DRAM memory pool!\n"); | ||
return -1; | ||
} | ||
|
||
fprintf(stderr, "Created a DRAM memory pool\n"); | ||
|
||
umf_memory_pool_handle_t fsdax_pool = create_fsdax_pool(path); | ||
if (fsdax_pool == NULL) { | ||
fprintf(stderr, "Failed to create an FSDAX memory pool!\n"); | ||
goto err_destroy_dram_pool; | ||
} | ||
|
||
fprintf(stderr, "Created an FSDAX memory pool\n"); | ||
lukaszstolarczuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
size_t size = 2 * 1024 * 1024; // == 2 MB | ||
|
||
// Allocate from the DRAM memory pool | ||
char *dram_buf = umfPoolCalloc(dram_pool, 1, size); | ||
if (dram_buf == NULL) { | ||
fprintf(stderr, | ||
"Failed to allocate memory from the DRAM memory pool!\n"); | ||
goto err_destroy_pools; | ||
} | ||
|
||
fprintf(stderr, "Allocated memory from the DRAM memory pool\n"); | ||
|
||
// Allocate from the FSDAX memory pool | ||
char *fsdax_buf = umfPoolCalloc(fsdax_pool, 1, size); | ||
if (fsdax_buf == NULL) { | ||
fprintf(stderr, | ||
"Failed to allocate memory from the FSDAX memory pool!\n"); | ||
goto err_free_dram; | ||
} | ||
|
||
fprintf(stderr, "Allocated memory from the FSDAX memory pool\n"); | ||
|
||
// Use the allocation from DRAM | ||
dram_buf[0] = '.'; | ||
|
||
// Use the allocation from FSDAX | ||
fsdax_buf[0] = '.'; | ||
|
||
// success | ||
ret = 0; | ||
|
||
// The file memory provider does not support the free() operation, | ||
// so we do not need to call: umfPoolFree(fsdax_pool, fsdax_buf); | ||
|
||
err_free_dram: | ||
fprintf(stderr, "Freeing the allocation from the DRAM memory pool ...\n"); | ||
umfPoolFree(dram_pool, dram_buf); | ||
|
||
err_destroy_pools: | ||
fprintf(stderr, "Destroying the FSDAX memory pool ...\n"); | ||
umfPoolDestroy(fsdax_pool); | ||
|
||
err_destroy_dram_pool: | ||
fprintf(stderr, "Destroying the DRAM memory pool ...\n"); | ||
umfPoolDestroy(dram_pool); | ||
|
||
return ret; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.