Skip to content

[WIP] mem props #1301

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/.spellcheck-conf.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[default]
# Don't correct the following words:
extend-ignore-words-re = ["ASSER", "Tne", "ba", "BA", "PN"]
extend-ignore-words-re = ["ASSER", "Tne", "ba", "BA", "PN", "usm"]

[files]
# completely exclude those files from consideration:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/reusable_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ jobs:
${{ matrix.compiler.cxx == 'icpx' && '. /opt/intel/oneapi/setvars.sh' || true }}
cmake --build ${{env.BUILD_DIR}} -j $(nproc)

# UMF_LOG="level:debug;flush:debug;output:stderr;pid:no"
# -R "test_provider_os_memory"
- name: Run tests
working-directory: ${{env.BUILD_DIR}}
run: |
Expand Down
6 changes: 6 additions & 0 deletions docs/config/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ Memtarget
.. doxygenfile:: experimental/memtarget.h
:sections: define enum typedef func

Memory Properties
==========================================

.. doxygenfile:: memory_props.h
:sections: define enum typedef func var

Inter-Process Communication
==========================================

Expand Down
4 changes: 3 additions & 1 deletion docs/config/spelling_exceptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ partList
pid
poolable
preallocated
propertyId
providerIpcData
providential
ptr
Expand All @@ -71,4 +72,5 @@ umfPoolMallocUsableSize
umfPoolRealloc
umfMemspaceUserFilter
umfMemspaceMemtargetAdd
unfreed
unfreed
usm
27 changes: 27 additions & 0 deletions include/umf/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ typedef enum umf_result_t {
UMF_RESULT_ERROR_UNKNOWN = 0x7ffffffe ///< Unknown error
} umf_result_t;

/// @brief Handle to the memory properties structure
typedef struct umf_memory_properties_t *umf_memory_properties_handle_t;

/// @brief ID of the memory property
typedef enum umf_memory_property_id_t {
UMF_MEMORY_PROPERTY_INVALID = -1, ///< Invalid property

// UMF specific
UMF_MEMORY_PROPERTY_PROVIDER_HANDLE = 0, ///< Handle to the memory provider
UMF_MEMORY_PROPERTY_POOL_HANDLE = 1, ///< Handle to the memory pool

// generic pointer properties
UMF_MEMORY_PROPERTY_POINTER_TYPE =
2, ///< Type of the pointer (umf_usm_memory_type_t)
UMF_MEMORY_PROPERTY_BASE_ADDRESS = 3, ///< Base address of the allocation
UMF_MEMORY_PROPERTY_BASE_SIZE = 4, ///< Base size of the allocation
UMF_MEMORY_PROPERTY_BUFFER_ID = 5, ///< Unique identifier for the buffer

// GPU specific
UMF_MEMORY_PROPERTY_CONTEXT = 6, ///< GPU context of the allocation
UMF_MEMORY_PROPERTY_DEVICE = 7, ///< GPU device where the allocation resides

/// @cond
UMF_MEMORY_PROPERTY_MAX_RESERVED = 0x1000, ///< Maximum reserved value
/// @endcond
} umf_memory_property_id_t;

/// @brief Type of the CTL query
typedef enum umf_ctl_query_type {
CTL_QUERY_READ,
Expand Down
46 changes: 46 additions & 0 deletions include/umf/memory_props.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
* Copyright (C) 2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#ifndef UMF_MEMORY_PROPS_H
#define UMF_MEMORY_PROPS_H 1

#include <umf/base.h>

#ifdef __cplusplus
extern "C" {
#endif

/// @brief Get the memory properties handle for a given pointer
/// \details
/// The handle returned by this function is valid until the memory pointed
/// to by the pointer is freed.
/// @param ptr pointer to the allocated memory
/// @param props_handle [out] pointer to the memory properties handle
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
umf_result_t
umfGetMemoryPropertiesHandle(const void *ptr,
umf_memory_properties_handle_t *props_handle);

/// @brief Get a specific memory property from the properties handle
/// @param props_handle handle to the memory properties
/// @param memory_property_id ID of the memory property to get
/// @param max_property_size size of the property value buffer
/// @param property_value [out] pointer to the value of the memory property
/// which will be filled
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
umf_result_t umfGetMemoryProperty(umf_memory_properties_handle_t props_handle,
umf_memory_property_id_t memory_property_id,
size_t max_property_size,
void *property_value);

#ifdef __cplusplus
}
#endif

#endif /* UMF_MEMORY_PROPS_H */
15 changes: 15 additions & 0 deletions include/umf/memory_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,21 @@ umf_result_t
umfMemoryProviderAllocationMerge(umf_memory_provider_handle_t hProvider,
void *lowPtr, void *highPtr, size_t totalSize);

///
/// @brief Retrieve properties of the memory allocation.
/// @param hProvider pointer to the memory provider
/// @param ptr pointer to the allocated memory
/// @param propertyId identifier of the memory property to retrieve
/// @param max_property_size size of the property value buffer
/// @param property_value [out] pointer to the value of the memory property
/// which will be filled
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
///
umf_result_t umfMemoryProviderGetAllocationProperties(
umf_memory_provider_handle_t hProvider, const void *ptr,
umf_memory_property_id_t propertyId, size_t max_property_size,
void *property_value);

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions include/umf/memory_provider_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ typedef struct umf_memory_provider_ops_t {
void *arg, size_t size,
umf_ctl_query_type_t queryType);

///
/// @brief Retrieve properties of the memory allocation.
/// @param provider pointer to the memory provider
/// @param ptr pointer to the allocated memory
/// @param memory_property_id identifier of the memory property to retrieve
/// @param max_property_size size of the property value buffer
/// @param property_value [out] pointer to the value of the memory property
/// which will be filled
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
///
umf_result_t (*ext_get_allocation_properties)(
void *provider, const void *ptr,
umf_memory_property_id_t memory_property_id, size_t max_property_size,
void *value);

} umf_memory_provider_ops_t;

#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions scripts/qemu/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ numactl -H

cd build
echo "## Running all tests ..."
#UMF_LOG="level:debug;flush:debug;output:stderr;pid:no"
ctest --verbose
# --output-on-failure -R "memoryPool"

echo "## Running tests bound to a numa node 0 and node 1 ..."
numactl -N 0 ctest --output-on-failure
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(UMF_SOURCES
ipc.c
ipc_cache.c
memory_pool.c
memory_props.c
memory_provider.c
memory_provider_get_last_failed.c
memtarget.c
Expand Down
30 changes: 19 additions & 11 deletions src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,19 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
}

size_t ipcHandleSize = 0;
umf_alloc_info_t allocInfo;
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
umf_memory_properties_handle_t props = NULL;
umf_result_t ret = umfGetMemoryPropertiesHandle(ptr, &props);
if (ret != UMF_RESULT_SUCCESS) {
LOG_ERR("cannot get alloc info for ptr = %p.", ptr);
LOG_ERR("cannot get alloc props for ptr = %p.", ptr);
return ret;
}

ret = umfPoolGetIPCHandleSize(allocInfo.pool, &ipcHandleSize);
if (props == NULL || props->pool == NULL) {
LOG_ERR("cannot get pool from alloc info for ptr = %p.", ptr);
return UMF_RESULT_ERROR_UNKNOWN;
}

ret = umfPoolGetIPCHandleSize(props->pool, &ipcHandleSize);
if (ret != UMF_RESULT_SUCCESS) {
LOG_ERR("cannot get IPC handle size.");
return ret;
Expand All @@ -79,11 +84,14 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,

// We cannot use umfPoolGetMemoryProvider function because it returns
// upstream provider but we need tracking one
umf_memory_provider_handle_t provider = allocInfo.pool->provider;
assert(provider);
if (props == NULL || props->pool == NULL || props->pool->provider == NULL) {
LOG_ERR("cannot get memory provider from pool");
umf_ba_global_free(ipcData);
return UMF_RESULT_ERROR_UNKNOWN;
}
umf_memory_provider_handle_t provider = props->pool->provider;

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

// ipcData->handle_id is filled by tracking provider
ipcData->base = allocInfo.base;
ipcData->base = props->base;
ipcData->pid = utils_getpid();
ipcData->baseSize = allocInfo.baseSize;
ipcData->offset = (uintptr_t)ptr - (uintptr_t)allocInfo.base;
ipcData->baseSize = props->base_size;
ipcData->offset = (uintptr_t)ptr - (uintptr_t)props->base;

*umfIPCHandle = ipcData;
*size = ipcHandleSize;
Expand Down
3 changes: 3 additions & 0 deletions src/libumf.def
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ EXPORTS
umfCtlExec
umfCtlGet
umfCtlSet
umfGetMemoryPropertiesHandle
umfGetMemoryProperty
umfJemallocPoolParamsCreate
umfJemallocPoolParamsDestroy
umfJemallocPoolParamsSetNumArenas
umfMemoryProviderGetAllocationProperties
umfPoolGetName
3 changes: 3 additions & 0 deletions src/libumf.map
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ UMF_1.0 {
umfCtlExec;
umfCtlGet;
umfCtlSet;
umfGetMemoryPropertiesHandle;
umfGetMemoryProperty;
umfJemallocPoolParamsCreate;
umfJemallocPoolParamsDestroy;
umfJemallocPoolParamsSetNumArenas;
umfMemoryProviderGetAllocationProperties;
umfPoolGetName;
local:
*;
Expand Down
12 changes: 10 additions & 2 deletions src/memory_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,16 @@ umf_result_t umfFree(void *ptr) {

umf_result_t umfPoolByPtr(const void *ptr, umf_memory_pool_handle_t *pool) {
UMF_CHECK((pool != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
*pool = umfMemoryTrackerGetPool(ptr);
return *pool ? UMF_RESULT_SUCCESS : UMF_RESULT_ERROR_INVALID_ARGUMENT;

umf_memory_properties_handle_t props = NULL;
umf_result_t ret = umfGetMemoryPropertiesHandle(ptr, &props);
if (ret != UMF_RESULT_SUCCESS || props == NULL || props->pool == NULL) {
*pool = NULL;
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

*pool = props->pool;
return UMF_RESULT_SUCCESS;
}

umf_result_t umfPoolGetMemoryProvider(umf_memory_pool_handle_t hPool,
Expand Down
Loading
Loading