Skip to content

Commit 4865d89

Browse files
authored
Merge pull request #429 from ldorau/Add_support_for_MAP_SHARED
Add support for MAP_SHARED for Linux only
2 parents f988831 + aeb0c72 commit 4865d89

17 files changed

+1227
-70
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ More detailed documentation is available here: https://oneapi-src.github.io/unif
138138
#### OS memory provider
139139

140140
A memory provider that provides memory from an operating system.
141+
It supports two types of memory mappings
142+
1) private memory mapping (`UMF_MEM_MAP_PRIVATE`)
143+
2) shared memory mapping (`UMF_MEM_MAP_SHARED` - supported on Linux only yet)
144+
145+
If the shared memory mapping is used then an anonymous file descriptor for memory mapping is created using:
146+
1) `memfd_secret()` syscall - (if it is implemented and) if the `UMF_MEM_FD_FUNC` environment variable does not contain the "memfd_create" string or
147+
2) `memfd_create()` syscall - otherwise (and if it is implemented).
141148

142149
##### Requirements
143150

@@ -226,6 +233,10 @@ In case of Linux it can be done without any code changes using the `LD_PRELOAD`
226233
$ LD_PRELOAD=/usr/lib/libumf_proxy.so myprogram
227234
```
228235

236+
The memory used by the proxy memory allocator is mmap'ed:
237+
1) with the `MAP_PRIVATE` flag by default or
238+
2) with the `MAP_SHARED` flag only if the `UMF_PROXY` environment variable contains the `page.disposition=shared` string.
239+
229240
#### Windows
230241

231242
In case of Windows it requires:

benchmark/ubench.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ UBENCH_EX(simple, glibc_malloc) {
107107

108108
static umf_os_memory_provider_params_t UMF_OS_MEMORY_PROVIDER_PARAMS = {
109109
/* .protection = */ UMF_PROTECTION_READ | UMF_PROTECTION_WRITE,
110+
/* .visibility */ UMF_MEM_MAP_PRIVATE,
110111

111112
// NUMA config
112113
/* .nodemask = */ NULL,

include/umf/memory_provider_ops.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ typedef struct umf_memory_provider_ext_ops_t {
5050
/// @brief Merges two coarse grain allocations into a single allocation that
5151
/// can be managed (freed) as a whole.
5252
/// allocation_split and allocation_merge should be both set or both NULL.
53+
/// allocation_merge should NOT be called concurrently with allocation_split()
54+
/// with the same pointer.
5355
/// @param hProvider handle to the memory provider
5456
/// @param lowPtr pointer to the first allocation
5557
/// @param highPtr pointer to the second allocation (should be > lowPtr)
@@ -64,6 +66,8 @@ typedef struct umf_memory_provider_ext_ops_t {
6466
/// @brief Splits a coarse grain allocation into 2 adjacent allocations that
6567
/// can be managed (freed) separately.
6668
/// allocation_split and allocation_merge should be both set or both NULL.
69+
/// allocation_split should NOT be called concurrently with allocation_merge()
70+
/// with the same pointer.
6771
/// @param hProvider handle to the memory provider
6872
/// @param ptr pointer to the beginning of the allocation
6973
/// @param totalSize total size of the allocation to be split

include/umf/providers/provider_os_memory.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ typedef enum umf_mem_protection_flags_t {
2929
/// @endcond
3030
} umf_mem_protection_flags_t;
3131

32+
/// @brief Memory visibility mode
33+
typedef enum umf_memory_visibility_t {
34+
UMF_MEM_MAP_PRIVATE = 1, ///< private memory mapping
35+
UMF_MEM_MAP_SHARED, ///< shared memory mapping (supported on Linux only)
36+
} umf_memory_visibility_t;
37+
3238
/// @brief Memory binding mode
3339
/// Specifies how memory is bound to NUMA nodes on systems that support NUMA.
3440
/// Not every mode is supported on every system.
@@ -61,6 +67,8 @@ typedef enum umf_numa_mode_t {
6167
typedef struct umf_os_memory_provider_params_t {
6268
/// Combination of 'umf_mem_protection_flags_t' flags
6369
unsigned protection;
70+
/// memory visibility mode
71+
umf_memory_visibility_t visibility;
6472

6573
// NUMA config
6674
/// ordered list of numa nodes
@@ -91,6 +99,7 @@ static inline umf_os_memory_provider_params_t
9199
umfOsMemoryProviderParamsDefault(void) {
92100
umf_os_memory_provider_params_t params = {
93101
UMF_PROTECTION_READ | UMF_PROTECTION_WRITE, /* protection */
102+
UMF_MEM_MAP_PRIVATE, /* visibility mode */
94103
NULL, /* numa_list */
95104
0, /* numa_list_len */
96105
UMF_NUMA_MODE_DEFAULT, /* numa_mode */

src/CMakeLists.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,36 @@ set(UMF_SOURCES
6666

6767
set(UMF_SOURCES_LINUX libumf_linux.c)
6868

69+
set(UMF_SOURCES_MACOSX libumf_linux.c)
70+
6971
set(UMF_SOURCES_WINDOWS libumf_windows.c)
7072

7173
# Compile definitions for UMF library.
7274
#
7375
# TODO: Cleanup the compile definitions across all the CMake files
7476
set(UMF_PRIVATE_COMPILE_DEFINITIONS "")
7577

76-
set(UMF_SOURCES_LINUX
77-
${UMF_SOURCES_LINUX}
78+
set(UMF_SOURCES_COMMON_LINUX_MACOSX
7879
provider/provider_os_memory.c
79-
provider/provider_os_memory_linux.c
80+
provider/provider_os_memory_posix.c
8081
memory_targets/memory_target_numa.c
8182
memspaces/memspace_numa.c
8283
memspaces/memspace_host_all.c
8384
memspaces/memspace_highest_capacity.c)
8485

86+
set(UMF_SOURCES_LINUX ${UMF_SOURCES_LINUX} ${UMF_SOURCES_COMMON_LINUX_MACOSX}
87+
provider/provider_os_memory_linux.c)
88+
89+
set(UMF_SOURCES_MACOSX ${UMF_SOURCES_MACOSX} ${UMF_SOURCES_COMMON_LINUX_MACOSX}
90+
provider/provider_os_memory_macosx.c)
91+
8592
set(UMF_SOURCES_WINDOWS ${UMF_SOURCES_WINDOWS} provider/provider_os_memory.c
8693
provider/provider_os_memory_windows.c)
8794

8895
set(UMF_LIBS ${UMF_LIBS} ${LIBHWLOC_LIBRARIES})
8996
set(UMF_PRIVATE_LIBRARY_DIRS ${UMF_PRIVATE_LIBRARY_DIRS}
9097
${LIBHWLOC_LIBRARY_DIRS})
9198

92-
set(UMF_SOURCES_MACOSX ${UMF_SOURCES_MACOSX} ${UMF_SOURCES_LINUX})
93-
9499
if(LINUX)
95100
set(UMF_SOURCES ${UMF_SOURCES} ${UMF_SOURCES_LINUX})
96101
elseif(WINDOWS)

0 commit comments

Comments
 (0)