Skip to content

Commit 7166723

Browse files
committed
add memory properties API
1 parent 0740522 commit 7166723

38 files changed

+1383
-108
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: 16 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,22 @@ stdout
5760
Tiering
5861
tiering
5962
topologies
63+
uint
64+
uintptr
6065
umf
6166
umfGetIPCHandle
6267
umfMemoryProviderAlloc
6368
umfMemoryProviderGetLastNativeError
6469
umfMemoryProviderOpenIPCHandle
70+
umfMemspaceMemtargetAdd
71+
umfMemspaceUserFilter
6572
umfOsMemoryProviderParamsDestroy
6673
umfPool
6774
umfPoolCalloc
6875
umfPoolDestroy
6976
umfPoolGetTag
7077
umfPoolMallocUsableSize
7178
umfPoolRealloc
72-
umfMemspaceUserFilter
73-
umfMemspaceMemtargetAdd
74-
unfreed
79+
unfreed
80+
usm
81+
ze

include/umf/base.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,39 @@ 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 =
63+
0, ///< Handle to the memory provider (void*)
64+
UMF_MEMORY_PROPERTY_POOL_HANDLE = 1, ///< Handle to the memory pool (void*)
65+
66+
// generic pointer properties
67+
UMF_MEMORY_PROPERTY_BASE_ADDRESS =
68+
10, ///< Base address of the allocation (uintptr_t)
69+
UMF_MEMORY_PROPERTY_BASE_SIZE =
70+
11, ///< Base size of the allocation (size_t)
71+
UMF_MEMORY_PROPERTY_BUFFER_ID =
72+
12, ///< Unique identifier for the buffer (uint64_t)
73+
74+
// GPU specific
75+
UMF_MEMORY_PROPERTY_POINTER_TYPE =
76+
20, ///< Type of the pointer (umf_usm_memory_type_t)
77+
UMF_MEMORY_PROPERTY_CONTEXT =
78+
21, ///< GPU context of the allocation (depending on GPU provider, e.g. ze_context_handle_t, CUcontext)
79+
UMF_MEMORY_PROPERTY_DEVICE =
80+
22, ///< GPU device where the allocation resides (depending on GPU provider, e.g. ze_device_handle_t, CUdevice)
81+
82+
/// @cond
83+
UMF_MEMORY_PROPERTY_MAX_RESERVED = 0x1000, ///< Maximum reserved value
84+
/// @endcond
85+
} umf_memory_property_id_t;
86+
5487
/// @brief Type of the CTL query
5588
typedef enum umf_ctl_query_type {
5689
CTL_QUERY_READ,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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_PROPS_H
11+
#define UMF_MEMORY_PROPS_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 a specific memory property from the properties handle
31+
/// @param props_handle handle to the memory properties
32+
/// @param memory_property_id ID of the memory property to get
33+
/// @param max_property_size size of the property value buffer
34+
/// @param property_value [out] pointer to the value of the memory property
35+
/// which will be filled
36+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
37+
umf_result_t umfGetMemoryProperty(umf_memory_properties_handle_t props_handle,
38+
umf_memory_property_id_t memory_property_id,
39+
size_t max_property_size,
40+
void *property_value);
41+
42+
#ifdef __cplusplus
43+
}
44+
#endif
45+
46+
#endif /* UMF_MEMORY_PROPS_H */

include/umf/memory_provider_ops.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,21 @@ 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+
///
282+
/// @brief Retrieve properties of the memory allocation.
283+
/// @param provider pointer to the memory provider
284+
/// @param ptr pointer to the allocated memory
285+
/// @param memory_property_id identifier of the memory property to retrieve
286+
/// @param property_value [out] pointer to the value of the memory property
287+
/// which will be filled
288+
/// @param max_property_size size of the property value buffer
289+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
290+
///
291+
umf_result_t (*ext_get_allocation_properties)(
292+
void *provider, const void *ptr,
293+
umf_memory_property_id_t memory_property_id, void *property_value,
294+
size_t max_property_size);
295+
281296
} umf_memory_provider_ops_t;
282297

283298
#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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,6 @@ EXPORTS
144144
umfJemallocPoolParamsDestroy
145145
umfJemallocPoolParamsSetNumArenas
146146
umfPoolGetName
147+
; Added in UMF_1.1
148+
umfGetMemoryPropertiesHandle
149+
umfGetMemoryProperty

src/libumf.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,8 @@ UMF_1.0 {
141141
local:
142142
*;
143143
};
144+
145+
UMF_1.1 {
146+
umfGetMemoryPropertiesHandle;
147+
umfGetMemoryProperty;
148+
} UMF_1.0;

0 commit comments

Comments
 (0)