Skip to content

Commit e420760

Browse files
committed
add memory properties API
1 parent e05b3a7 commit e420760

35 files changed

+1587
-107
lines changed

.github/workflows/.spellcheck-conf.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[default]
22
# Don't correct the following words:
3-
extend-ignore-words-re = ["ASSER", "Tne", "ba", "BA", "PN"]
3+
extend-ignore-words-re = ["ASSER", "Tne", "ba", "BA", "PN", "usm"]
44

55
[files]
66
# completely exclude those files from consideration:

docs/config/api.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,19 @@ Memtarget
170170
.. doxygenfile:: experimental/memtarget.h
171171
:sections: define enum typedef func
172172

173+
Memory Properties
174+
==========================================
175+
176+
TODO: Add general information about memory properties.
177+
178+
.. note::
179+
The memory properties APIs are experimental and may change in future releases.
180+
181+
Memory Properties
182+
------------------------------------------
183+
.. doxygenfile:: experimental/memory_props.h
184+
:sections: define enum typedef func var
185+
173186
Inter-Process Communication
174187
==========================================
175188

docs/config/spelling_exceptions.txt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ allocatable
33
allocator
44
allocators
55
calloc
6-
CXL
76
copyable
7+
CUcontext
8+
CUdevice
89
customizable
10+
CXL
911
daxX
10-
deallocation
1112
deallocating
13+
deallocation
1214
deallocations
13-
Devdax
1415
dev
16+
Devdax
1517
Globals
18+
highPtr
1619
hMemtarget
1720
hPool
1821
hProvider
19-
highPtr
20-
io
2122
interprocess
23+
io
2224
ipc
2325
jemalloc
2426
lowPtr
@@ -47,8 +49,9 @@ partList
4749
pid
4850
poolable
4951
preallocated
50-
providerIpcData
52+
propertyId
5153
providential
54+
providerIpcData
5255
ptr
5356
realloc
5457
Scalable
@@ -57,18 +60,23 @@ stdout
5760
Tiering
5861
tiering
5962
topologies
63+
uint
64+
uintptr
6065
umf
6166
umfGetIPCHandle
67+
umfGetMemoryPropertySize
6268
umfMemoryProviderAlloc
6369
umfMemoryProviderGetLastNativeError
6470
umfMemoryProviderOpenIPCHandle
71+
umfMemspaceMemtargetAdd
72+
umfMemspaceUserFilter
6573
umfOsMemoryProviderParamsDestroy
6674
umfPool
6775
umfPoolCalloc
6876
umfPoolDestroy
6977
umfPoolGetTag
7078
umfPoolMallocUsableSize
7179
umfPoolRealloc
72-
umfMemspaceUserFilter
73-
umfMemspaceMemtargetAdd
74-
unfreed
80+
unfreed
81+
usm
82+
ze

include/umf/base.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ typedef enum umf_result_t {
5151
UMF_RESULT_ERROR_UNKNOWN = 0x7ffffffe ///< Unknown error
5252
} umf_result_t;
5353

54+
/// @brief Handle to the memory properties structure
55+
typedef struct umf_memory_properties_t *umf_memory_properties_handle_t;
56+
57+
/// @brief ID of the memory property
58+
typedef enum umf_memory_property_id_t {
59+
UMF_MEMORY_PROPERTY_INVALID = -1, ///< Invalid property
60+
61+
// UMF specific
62+
UMF_MEMORY_PROPERTY_PROVIDER_HANDLE = 0, ///< Handle to the memory provider
63+
UMF_MEMORY_PROPERTY_POOL_HANDLE = 1, ///< Handle to the memory pool
64+
65+
// generic pointer properties
66+
UMF_MEMORY_PROPERTY_BASE_ADDRESS = 10, ///< Base address of the allocation
67+
UMF_MEMORY_PROPERTY_BASE_SIZE = 11, ///< Base size of the allocation
68+
UMF_MEMORY_PROPERTY_BUFFER_ID = 12, ///< Unique identifier for the buffer
69+
70+
// GPU specific
71+
UMF_MEMORY_PROPERTY_POINTER_TYPE = 20, ///< Type of the pointer
72+
UMF_MEMORY_PROPERTY_CONTEXT = 21, ///< GPU context of the allocation
73+
UMF_MEMORY_PROPERTY_DEVICE =
74+
22, ///< GPU device where the allocation resides
75+
76+
/// @cond
77+
UMF_MEMORY_PROPERTY_MAX_RESERVED = 0x1000, ///< Maximum reserved value
78+
/// @endcond
79+
} umf_memory_property_id_t;
80+
5481
/// @brief Type of the CTL query
5582
typedef enum umf_ctl_query_type {
5683
CTL_QUERY_READ,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
*
3+
* Copyright (C) 2025 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#ifndef UMF_MEMORY_PROPERTIES_H
11+
#define UMF_MEMORY_PROPERTIES_H 1
12+
13+
#include <umf/base.h>
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
/// @brief Get the memory properties handle for a given pointer
20+
/// \details
21+
/// The handle returned by this function is valid until the memory pointed
22+
/// to by the pointer is freed.
23+
/// @param ptr pointer to the allocated memory
24+
/// @param props_handle [out] pointer to the memory properties handle
25+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
26+
umf_result_t
27+
umfGetMemoryPropertiesHandle(const void *ptr,
28+
umf_memory_properties_handle_t *props_handle);
29+
30+
/// @brief Get the size of a specific memory property
31+
/// \details
32+
/// The size of the property can be used to allocate a buffer to hold the
33+
/// value of the property.
34+
/// @param props_handle handle to the memory properties
35+
/// @param memory_property_id ID of the memory property to get the size of
36+
/// @param size [out] pointer to the size of the property
37+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
38+
umf_result_t
39+
umfGetMemoryPropertySize(umf_memory_properties_handle_t props_handle,
40+
umf_memory_property_id_t memory_property_id,
41+
size_t *size);
42+
43+
/// @brief Get a specific memory property from the properties handle
44+
/// \details
45+
/// The type of the property value depends on the property ID. The size of
46+
/// the property value buffer must be large enough to hold the
47+
/// value of the property. The size of the property can be obtained by
48+
/// calling umfGetMemoryPropertySize() with the same property ID.
49+
/// @param props_handle handle to the memory properties
50+
/// @param memory_property_id ID of the memory property to get
51+
/// @param property_value [out] pointer to the value of the memory property
52+
/// which will be filled
53+
/// @param max_property_size size of the property value buffer
54+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
55+
umf_result_t umfGetMemoryProperty(umf_memory_properties_handle_t props_handle,
56+
umf_memory_property_id_t memory_property_id,
57+
void *property_value,
58+
size_t max_property_size);
59+
60+
#ifdef __cplusplus
61+
}
62+
#endif
63+
64+
#endif /* UMF_MEMORY_PROPERTIES_H */

include/umf/memory_provider_ops.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
/// @brief Version of the Memory Provider ops structure.
2222
/// NOTE: This is equal to the latest UMF version, in which the ops structure
2323
/// has been modified.
24-
#define UMF_PROVIDER_OPS_VERSION_CURRENT UMF_MAKE_VERSION(1, 0)
24+
#define UMF_PROVIDER_OPS_VERSION_CURRENT UMF_MAKE_VERSION(1, 1)
2525

2626
///
2727
/// @brief This structure comprises function pointers used by corresponding
@@ -278,6 +278,43 @@ typedef struct umf_memory_provider_ops_t {
278278
const char *name, void *arg, size_t size,
279279
umf_ctl_query_type_t queryType, va_list args);
280280

281+
// The following operations were added in ops version 1.1
282+
283+
///
284+
/// @brief Retrieve provider-specyfic properties of the memory allocation.
285+
/// \details
286+
/// If provider supports allocation properties,
287+
/// ext_get_allocation_properties and ext_get_allocation_properties_size,
288+
/// must either be all set or all NULL.
289+
/// @param provider pointer to the memory provider
290+
/// @param ptr pointer to the allocated memory
291+
/// @param memory_property_id identifier of the memory property to retrieve
292+
/// @param property_value [out] pointer to the value of the memory property
293+
/// which will be filled
294+
/// @param max_property_size size of the property value buffer
295+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
296+
///
297+
umf_result_t (*ext_get_allocation_properties)(
298+
void *provider, const void *ptr,
299+
umf_memory_property_id_t memory_property_id, void *property_value,
300+
size_t max_property_size);
301+
302+
/// @brief Retrieve size of the provider-specyfic properties of the memory
303+
/// allocation.
304+
/// \details
305+
/// If provider supports allocation properties,
306+
/// ext_get_allocation_properties and ext_get_allocation_properties_size,
307+
/// must either be all set or all NULL.
308+
/// @param provider pointer to the memory provider
309+
/// @param memory_property_id identifier of the memory property to retrieve
310+
/// size of the property
311+
/// @param size [out] pointer to the size of the property
312+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
313+
///
314+
umf_result_t (*ext_get_allocation_properties_size)(
315+
void *provider, umf_memory_property_id_t memory_property_id,
316+
size_t *size);
317+
281318
} umf_memory_provider_ops_t;
282319

283320
#ifdef __cplusplus

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ set(UMF_SOURCES
4242
ipc.c
4343
ipc_cache.c
4444
memory_pool.c
45+
memory_props.c
4546
memory_provider.c
4647
memory_provider_get_last_failed.c
4748
memtarget.c

src/ipc.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,19 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
5858
}
5959

6060
size_t ipcHandleSize = 0;
61-
umf_alloc_info_t allocInfo;
62-
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
61+
umf_memory_properties_handle_t props = NULL;
62+
umf_result_t ret = umfGetMemoryPropertiesHandle(ptr, &props);
6363
if (ret != UMF_RESULT_SUCCESS) {
64-
LOG_ERR("cannot get alloc info for ptr = %p.", ptr);
64+
LOG_ERR("cannot get alloc props for ptr = %p.", ptr);
6565
return ret;
6666
}
6767

68-
ret = umfPoolGetIPCHandleSize(allocInfo.pool, &ipcHandleSize);
68+
if (props == NULL || props->pool == NULL) {
69+
LOG_ERR("cannot get pool from alloc info for ptr = %p.", ptr);
70+
return UMF_RESULT_ERROR_UNKNOWN;
71+
}
72+
73+
ret = umfPoolGetIPCHandleSize(props->pool, &ipcHandleSize);
6974
if (ret != UMF_RESULT_SUCCESS) {
7075
LOG_ERR("cannot get IPC handle size.");
7176
return ret;
@@ -79,11 +84,14 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
7984

8085
// We cannot use umfPoolGetMemoryProvider function because it returns
8186
// upstream provider but we need tracking one
82-
umf_memory_provider_handle_t provider = allocInfo.pool->provider;
83-
assert(provider);
87+
if (props->pool->provider == NULL) {
88+
LOG_ERR("cannot get memory provider from pool");
89+
umf_ba_global_free(ipcData);
90+
return UMF_RESULT_ERROR_UNKNOWN;
91+
}
92+
umf_memory_provider_handle_t provider = props->pool->provider;
8493

85-
ret = umfMemoryProviderGetIPCHandle(provider, allocInfo.base,
86-
allocInfo.baseSize,
94+
ret = umfMemoryProviderGetIPCHandle(provider, props->base, props->base_size,
8795
(void *)ipcData->providerIpcData);
8896
if (ret != UMF_RESULT_SUCCESS) {
8997
LOG_ERR("failed to get IPC handle.");
@@ -92,10 +100,10 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
92100
}
93101

94102
// ipcData->handle_id is filled by tracking provider
95-
ipcData->base = allocInfo.base;
103+
ipcData->base = props->base;
96104
ipcData->pid = utils_getpid();
97-
ipcData->baseSize = allocInfo.baseSize;
98-
ipcData->offset = (uintptr_t)ptr - (uintptr_t)allocInfo.base;
105+
ipcData->baseSize = props->base_size;
106+
ipcData->offset = (uintptr_t)ptr - (uintptr_t)props->base;
99107

100108
*umfIPCHandle = ipcData;
101109
*size = ipcHandleSize;

src/libumf.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
LIBRARY UMF
88

9-
VERSION 1.0
9+
VERSION 1.1
1010

1111
EXPORTS
1212
DllMain
@@ -149,6 +149,9 @@ EXPORTS
149149
umfDevDaxMemoryProviderParamsSetName
150150
umfFileMemoryProviderParamsSetName
151151
umfFixedMemoryProviderParamsSetName
152+
umfGetMemoryPropertiesHandle
153+
umfGetMemoryProperty
154+
umfGetMemoryPropertySize
152155
umfJemallocPoolParamsSetName
153156
umfLevelZeroMemoryProviderParamsSetName
154157
umfOsMemoryProviderParamsSetName

src/libumf.map

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ UMF_1.1 {
147147
umfDevDaxMemoryProviderParamsSetName;
148148
umfFileMemoryProviderParamsSetName;
149149
umfFixedMemoryProviderParamsSetName;
150+
umfGetMemoryPropertiesHandle;
151+
umfGetMemoryProperty;
152+
umfGetMemoryPropertySize;
150153
umfJemallocPoolParamsSetName;
151154
umfLevelZeroMemoryProviderParamsSetName;
152155
umfOsMemoryProviderParamsSetName;

0 commit comments

Comments
 (0)