-
Notifications
You must be signed in to change notification settings - Fork 35
IPC API #88
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
Merged
IPC API #88
Changes from all commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* | ||
* Copyright (C) 2023-2024 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_IPC_H | ||
#define UMF_IPC_H 1 | ||
|
||
#include <umf/base.h> | ||
#include <umf/memory_pool.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef struct umf_ipc_data_t *umf_ipc_handle_t; | ||
|
||
/// | ||
/// @brief Creates an IPC handle for the specified UMF allocation. | ||
/// @param ptr pointer to the allocated memory. | ||
/// @param ipcHandle [out] returned IPC handle. | ||
/// @param size [out] size of IPC handle in bytes. | ||
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. | ||
umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *ipcHandle, | ||
size_t *size); | ||
|
||
/// | ||
/// @brief Release IPC handle retrieved by umfGetIPCHandle. | ||
/// @param ipcHandle IPC handle. | ||
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. | ||
umf_result_t umfPutIPCHandle(umf_ipc_handle_t ipcHandle); | ||
|
||
/// | ||
/// @brief Open IPC handle retrieved by umfGetIPCHandle. | ||
/// @param hPool [in] Pool handle where to open the the IPC handle. | ||
/// @param ipcHandle [in] IPC handle. | ||
/// @param ptr [out] pointer to the memory in the current process. | ||
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. | ||
umf_result_t umfOpenIPCHandle(umf_memory_pool_handle_t hPool, | ||
umf_ipc_handle_t ipcHandle, void **ptr); | ||
|
||
/// | ||
/// @brief Close IPC handle. | ||
/// @param ptr [in] pointer to the memory. | ||
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. | ||
umf_result_t umfCloseIPCHandle(void *ptr); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* UMF_IPC_H */ |
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
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,117 @@ | ||
/* | ||
* | ||
* Copyright (C) 2023-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 <assert.h> | ||
#include <stdlib.h> | ||
|
||
#include <umf/ipc.h> | ||
|
||
#include "base_alloc_global.h" | ||
#include "ipc_internal.h" | ||
#include "memory_pool_internal.h" | ||
#include "provider/provider_tracking.h" | ||
#include "utils_log.h" | ||
|
||
umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle, | ||
size_t *size) { | ||
size_t ipcHandleSize = 0; | ||
umf_alloc_info_t allocInfo; | ||
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
LOG_ERR("umfGetIPCHandle: cannot get alloc info for ptr = %p.", ptr); | ||
return ret; | ||
} | ||
|
||
// 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); | ||
|
||
size_t providerIPCHandleSize; | ||
ret = umfMemoryProviderGetIPCHandleSize(provider, &providerIPCHandleSize); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
LOG_ERR("umfGetIPCHandle: cannot get IPC handle size."); | ||
return ret; | ||
} | ||
|
||
ipcHandleSize = sizeof(umf_ipc_data_t) + providerIPCHandleSize; | ||
umf_ipc_data_t *ipcData = umf_ba_global_alloc(ipcHandleSize); | ||
if (!ipcData) { | ||
LOG_ERR("umfGetIPCHandle: failed to allocate ipcData"); | ||
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; | ||
} | ||
|
||
ret = | ||
umfMemoryProviderGetIPCHandle(provider, allocInfo.base, allocInfo.size, | ||
(void *)ipcData->providerIpcData); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
LOG_ERR("umfGetIPCHandle: failed to get IPC handle."); | ||
umf_ba_global_free(ipcData); | ||
return ret; | ||
} | ||
|
||
ipcData->size = allocInfo.size; | ||
ipcData->offset = (uintptr_t)ptr - (uintptr_t)allocInfo.base; | ||
|
||
*umfIPCHandle = ipcData; | ||
*size = ipcHandleSize; | ||
|
||
return ret; | ||
} | ||
|
||
umf_result_t umfPutIPCHandle(umf_ipc_handle_t umfIPCHandle) { | ||
umf_result_t ret = UMF_RESULT_SUCCESS; | ||
|
||
// TODO: Just return SUCCESS because current tracking memory provider | ||
// implementation does nothing in Put function. Tracking memory | ||
// provider relies on IPC cache and actually Put IPC handle back | ||
// to upstream memory provider when umfMemoryProviderFree is called. | ||
// To support incapsulation we should not take into account | ||
// implementation details of tracking memory provider and find the | ||
// approrpiate pool, get memory provider of that pool and call | ||
// umfMemoryProviderPutIPCHandle(hProvider, | ||
// umfIPCHandle->providerIpcData); | ||
umf_ba_global_free(umfIPCHandle); | ||
|
||
return ret; | ||
} | ||
|
||
umf_result_t umfOpenIPCHandle(umf_memory_pool_handle_t hPool, | ||
umf_ipc_handle_t umfIPCHandle, void **ptr) { | ||
|
||
// We cannot use umfPoolGetMemoryProvider function because it returns | ||
// upstream provider but we need tracking one | ||
umf_memory_provider_handle_t hProvider = hPool->provider; | ||
void *base = NULL; | ||
|
||
umf_result_t ret = umfMemoryProviderOpenIPCHandle( | ||
hProvider, (void *)umfIPCHandle->providerIpcData, &base); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
LOG_ERR("umfOpenIPCHandle: memory provider failed to IPC handle."); | ||
return ret; | ||
} | ||
*ptr = (void *)((uintptr_t)base + umfIPCHandle->offset); | ||
|
||
return UMF_RESULT_SUCCESS; | ||
} | ||
|
||
umf_result_t umfCloseIPCHandle(void *ptr) { | ||
umf_alloc_info_t allocInfo; | ||
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
LOG_ERR("umfCloseIPCHandle: cannot get alloc info for ptr = %p.", ptr); | ||
return ret; | ||
} | ||
|
||
// We cannot use umfPoolGetMemoryProvider function because it returns | ||
// upstream provider but we need tracking one | ||
umf_memory_provider_handle_t hProvider = allocInfo.pool->provider; | ||
|
||
return umfMemoryProviderCloseIPCHandle(hProvider, allocInfo.base); | ||
} |
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,33 @@ | ||
/* | ||
* | ||
* Copyright (C) 2023-2024 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_IPC_INTERNAL_H | ||
#define UMF_IPC_INTERNAL_H 1 | ||
|
||
#include <umf/base.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// UMF representation of IPC handle. It contains UMF-specific common data | ||
// and provider-specific IPC data, stored in providerIpcData. | ||
// providerIpcData is a Flexible Array Member because its size varies | ||
// depending on the provider. | ||
typedef struct umf_ipc_data_t { | ||
size_t size; // size of base allocation | ||
uint64_t offset; | ||
char providerIpcData[]; | ||
} umf_ipc_data_t; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* UMF_IPC_INTERNAL_H */ |
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.