diff --git a/source/common/uma_helpers.hpp b/source/common/uma_helpers.hpp index 67eddd6811..da0743987e 100644 --- a/source/common/uma_helpers.hpp +++ b/source/common/uma_helpers.hpp @@ -92,7 +92,7 @@ auto memoryProviderMakeUnique(Args &&...args) { UMA_ASSIGN_OP(ops, T, get_min_page_size, UMA_RESULT_ERROR_UNKNOWN); UMA_ASSIGN_OP(ops, T, purge_lazy, UMA_RESULT_ERROR_UNKNOWN); UMA_ASSIGN_OP(ops, T, purge_force, UMA_RESULT_ERROR_UNKNOWN); - UMA_ASSIGN_OP_NORETURN(ops, T, get_name); + UMA_ASSIGN_OP(ops, T, get_name, ""); uma_memory_provider_handle_t hProvider = nullptr; auto ret = umaMemoryProviderCreate(&ops, &argsTuple, &hProvider); @@ -106,14 +106,16 @@ auto memoryProviderMakeUnique(Args &&...args) { /// replaced by dtor). All arguments passed to this function are /// forwarded to T::initialize(). template -auto poolMakeUnique(uma_memory_provider_handle_t *providers, - size_t numProviders, Args &&...args) { +auto poolMakeUnique(uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, + Args &&...args) { uma_memory_pool_ops_t ops; auto argsTuple = std::make_tuple(std::forward(args)...); ops.version = UMA_VERSION_CURRENT; - ops.initialize = [](uma_memory_provider_handle_t *providers, - size_t numProviders, void *params, void **obj) { + ops.initialize = [](uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, + void *params, void **obj) { auto *tuple = reinterpret_cast(params); T *pool; @@ -126,10 +128,11 @@ auto poolMakeUnique(uma_memory_provider_handle_t *providers, *obj = pool; try { - auto ret = std::apply( - &T::initialize, - std::tuple_cat(std::make_tuple(pool, providers, numProviders), - *tuple)); + auto ret = + std::apply(&T::initialize, + std::tuple_cat(std::make_tuple(pool, data_provider, + metadata_provider), + *tuple)); if (ret != UMA_RESULT_SUCCESS) { delete pool; } @@ -148,9 +151,14 @@ auto poolMakeUnique(uma_memory_provider_handle_t *providers, UMA_ASSIGN_OP(ops, T, malloc_usable_size, ((size_t)0)); UMA_ASSIGN_OP_NORETURN(ops, T, free); UMA_ASSIGN_OP(ops, T, get_last_result, UMA_RESULT_ERROR_UNKNOWN); + UMA_ASSIGN_OP(ops, T, get_data_memory_provider, + (uma_memory_provider_handle_t) nullptr); + UMA_ASSIGN_OP(ops, T, get_metadata_memory_provider, + (uma_memory_provider_handle_t) nullptr); uma_memory_pool_handle_t hPool = nullptr; - auto ret = umaPoolCreate(&ops, providers, numProviders, &argsTuple, &hPool); + auto ret = umaPoolCreate(&ops, data_provider, metadata_provider, &argsTuple, + &hPool); return std::pair{ ret, pool_unique_handle_t(hPool, &umaPoolDestroy)}; } diff --git a/source/common/uma_pools/disjoint_pool.cpp b/source/common/uma_pools/disjoint_pool.cpp index e91e9dbdae..4e76715775 100644 --- a/source/common/uma_pools/disjoint_pool.cpp +++ b/source/common/uma_pools/disjoint_pool.cpp @@ -840,14 +840,12 @@ void DisjointPool::AllocImpl::printStats(bool &TitlePrinted, } } -uma_result_t DisjointPool::initialize(uma_memory_provider_handle_t *providers, - size_t numProviders, - DisjointPoolConfig parameters) { - if (numProviders != 1 || !providers[0]) { - return UMA_RESULT_ERROR_INVALID_ARGUMENT; - } - - impl = std::make_unique(providers[0], parameters); +uma_result_t +DisjointPool::initialize(uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, + DisjointPoolConfig parameters) { + (void)metadata_provider; + impl = std::make_unique(data_provider, parameters); return UMA_RESULT_SUCCESS; } @@ -920,6 +918,14 @@ enum uma_result_t DisjointPool::get_last_result(const char **ppMessage) { return UMA_RESULT_ERROR_UNKNOWN; } +uma_memory_provider_handle_t DisjointPool::get_data_memory_provider() { + return impl->getMemHandle(); +} + +uma_memory_provider_handle_t DisjointPool::get_metadata_memory_provider() { + return nullptr; +} + DisjointPool::DisjointPool() {} // Define destructor for use with unique_ptr diff --git a/source/common/uma_pools/disjoint_pool.hpp b/source/common/uma_pools/disjoint_pool.hpp index c991fa07bb..9f47eed23b 100644 --- a/source/common/uma_pools/disjoint_pool.hpp +++ b/source/common/uma_pools/disjoint_pool.hpp @@ -60,8 +60,9 @@ class DisjointPool { class AllocImpl; using Config = DisjointPoolConfig; - uma_result_t initialize(uma_memory_provider_handle_t *providers, - size_t numProviders, DisjointPoolConfig parameters); + uma_result_t initialize(uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, + DisjointPoolConfig parameters); void *malloc(size_t size); void *calloc(size_t, size_t); void *realloc(void *, size_t); @@ -69,6 +70,8 @@ class DisjointPool { size_t malloc_usable_size(void *); void free(void *ptr); enum uma_result_t get_last_result(const char **ppMessage); + uma_memory_provider_handle_t get_data_memory_provider(); + uma_memory_provider_handle_t get_metadata_memory_provider(); DisjointPool(); ~DisjointPool(); diff --git a/source/common/unified_memory_allocation/include/uma/base.h b/source/common/unified_memory_allocation/include/uma/base.h index 7d5d34f987..1e1b6ba634 100644 --- a/source/common/unified_memory_allocation/include/uma/base.h +++ b/source/common/unified_memory_allocation/include/uma/base.h @@ -39,13 +39,16 @@ enum uma_result_t { UMA_RESULT_ERROR_POOL_SPECIFIC = 2, ///< A pool specific warning/error has been reported and can be ///< Retrieved via the umaPoolGetLastResult entry point. - UMA_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC = - 3, ///< A provider specific warning/error has been reported and can be + UMA_RESULT_ERROR_METADATA_MEMORY_PROVIDER_SPECIFIC = + 3, ///< A metadata provider specific warning/error has been reported and can be ///< Retrieved via the umaMemoryProviderGetLastResult entry point. + UMA_RESULT_ERROR_DATA_MEMORY_PROVIDER_SPECIFIC = + 4, ///< A data provider specific warning/error has been reported and can be + ///< Retrieved via the umaMemoryProviderGetLastNativeError entry point. UMA_RESULT_ERROR_INVALID_ARGUMENT = - 4, ///< Generic error code for invalid arguments - UMA_RESULT_ERROR_INVALID_ALIGNMENT = 5, /// Invalid alignment of an argument - UMA_RESULT_ERROR_NOT_SUPPORTED = 6, /// Operation not supported + 5, ///< Generic error code for invalid arguments + UMA_RESULT_ERROR_INVALID_ALIGNMENT = 6, /// Invalid alignment of an argument + UMA_RESULT_ERROR_NOT_SUPPORTED = 7, /// Operation not supported UMA_RESULT_ERROR_UNKNOWN = 0x7ffffffe ///< Unknown or internal error }; diff --git a/source/common/unified_memory_allocation/include/uma/memory_pool.h b/source/common/unified_memory_allocation/include/uma/memory_pool.h index d2cc044a58..71dde981cd 100644 --- a/source/common/unified_memory_allocation/include/uma/memory_pool.h +++ b/source/common/unified_memory_allocation/include/uma/memory_pool.h @@ -25,17 +25,16 @@ struct uma_memory_pool_ops_t; /// /// \brief Creates new memory pool. /// \param ops instance of uma_memory_pool_ops_t -/// \param providers array of memory providers that will be used for coarse-grain allocations. -/// Should contain at least one memory provider. -/// \param numProvider number of elements in the providers array +/// \param data_provider memory provider that should be used for coarse-grain data allocation +/// \param metadata_provider [optional] memory provider that should be used for metadata allocations /// \param params pointer to pool-specific parameters /// \param hPool [out] handle to the newly created memory pool /// \return UMA_RESULT_SUCCESS on success or appropriate error code on failure. /// enum uma_result_t umaPoolCreate(struct uma_memory_pool_ops_t *ops, - uma_memory_provider_handle_t *providers, - size_t numProviders, void *params, - uma_memory_pool_handle_t *hPool); + uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, + void *params, uma_memory_pool_handle_t *hPool); /// /// \brief Destroys memory pool. @@ -134,17 +133,18 @@ enum uma_result_t umaPoolGetLastResult(uma_memory_pool_handle_t hPool, uma_memory_pool_handle_t umaPoolByPtr(const void *ptr); /// -/// \brief Retrieve memory providers associated with a given pool. +/// \brief Retrieve metadata memory provider associated with a given pool. /// \param hPool specified memory pool -/// \param hProviders [out] pointer to an array of memory providers. If numProviders is not equal to or -/// greater than the real number of providers, UMA_RESULT_ERROR_INVALID_ARGUMENT is returned. -/// \param numProviders [in] number of memory providers to return -/// \param numProvidersRet pointer to the actual number of memory providers -/// \return UMA_RESULT_SUCCESS on success or appropriate error code on failure. -enum uma_result_t -umaPoolGetMemoryProviders(uma_memory_pool_handle_t hPool, size_t numProviders, - uma_memory_provider_handle_t *hProviders, - size_t *numProvidersRet); +/// \return handle to the metadata memory provider +uma_memory_provider_handle_t +umaPoolGetMetadataMemoryProvider(uma_memory_pool_handle_t hPool); + +/// +/// \brief Retrieve data memory provider associated with a given pool. +/// \param hPool specified memory pool +/// \return handle to the data memory provider +uma_memory_provider_handle_t +umaPoolGetDataMemoryProvider(uma_memory_pool_handle_t hPool); #ifdef __cplusplus } diff --git a/source/common/unified_memory_allocation/include/uma/memory_pool_ops.h b/source/common/unified_memory_allocation/include/uma/memory_pool_ops.h index 31a15ca02f..47f4cb99c7 100644 --- a/source/common/unified_memory_allocation/include/uma/memory_pool_ops.h +++ b/source/common/unified_memory_allocation/include/uma/memory_pool_ops.h @@ -27,15 +27,15 @@ struct uma_memory_pool_ops_t { /// /// \brief Intializes memory pool. - /// \param providers array of memory providers that will be used for coarse-grain allocations. - /// Should contain at least one memory provider. - /// \param numProvider number of elements in the providers array + /// \param data_provider memory provider that should be used for coarse-grain data allocation + /// \param metadata_provider [optional] memory provider that should be used for metadata allocations /// \param params pool-specific params /// \param pool [out] returns pointer to the pool /// \return UMA_RESULT_SUCCESS on success or appropriate error code on failure. - enum uma_result_t (*initialize)(uma_memory_provider_handle_t *providers, - size_t numProviders, void *params, - void **pool); + enum uma_result_t (*initialize)( + uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, void *params, + void **pool); /// /// \brief Finalizes memory pool @@ -50,6 +50,8 @@ struct uma_memory_pool_ops_t { size_t (*malloc_usable_size)(void *pool, void *ptr); void (*free)(void *pool, void *); enum uma_result_t (*get_last_result)(void *pool, const char **ppMessage); + uma_memory_provider_handle_t (*get_data_memory_provider)(void *pool); + uma_memory_provider_handle_t (*get_metadata_memory_provider)(void *pool); }; #ifdef __cplusplus diff --git a/source/common/unified_memory_allocation/include/uma/memory_provider.h b/source/common/unified_memory_allocation/include/uma/memory_provider.h index d3c2df3ac4..7d203b047a 100644 --- a/source/common/unified_memory_allocation/include/uma/memory_provider.h +++ b/source/common/unified_memory_allocation/include/uma/memory_provider.h @@ -63,7 +63,8 @@ enum uma_result_t umaMemoryProviderFree(uma_memory_provider_handle_t hProvider, /// /// \brief Retrieve string representation of the underlying provider specific /// result reported by the last API that returned -/// UMA_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC. Allows for a provider +/// UMA_RESULT_ERROR_METADATA_MEMORY_PROVIDER_SPECIFIC or +/// UMA_RESULT_ERROR_DATA_MEMORY_PROVIDER_SPECIFIC. Allows for a provider /// independent way to return a provider specific result. /// /// \details @@ -136,8 +137,7 @@ umaMemoryProviderPurgeForce(uma_memory_provider_handle_t hProvider, void *ptr, /// \brief Retrive name of a given memory provider. /// \param hProvider handle to the memory provider /// \param ppName [out] pointer to a string containing name of the provider -void umaMemoryProviderGetName(uma_memory_provider_handle_t hProvider, - const char **ppName); +const char *umaMemoryProviderGetName(uma_memory_provider_handle_t hProvider); #ifdef __cplusplus } diff --git a/source/common/unified_memory_allocation/include/uma/memory_provider_ops.h b/source/common/unified_memory_allocation/include/uma/memory_provider_ops.h index 40c2ac8a0f..81dd13349a 100644 --- a/source/common/unified_memory_allocation/include/uma/memory_provider_ops.h +++ b/source/common/unified_memory_allocation/include/uma/memory_provider_ops.h @@ -49,7 +49,7 @@ struct uma_memory_provider_ops_t { size_t *pageSize); enum uma_result_t (*purge_lazy)(void *provider, void *ptr, size_t size); enum uma_result_t (*purge_force)(void *provider, void *ptr, size_t size); - void (*get_name)(void *provider, const char **ppName); + const char *(*get_name)(void *provider); }; #ifdef __cplusplus diff --git a/source/common/unified_memory_allocation/src/memory_pool.c b/source/common/unified_memory_allocation/src/memory_pool.c index 754e4697bd..73f35ea2dc 100644 --- a/source/common/unified_memory_allocation/src/memory_pool.c +++ b/source/common/unified_memory_allocation/src/memory_pool.c @@ -21,28 +21,18 @@ struct uma_memory_pool_t { void *pool_priv; struct uma_memory_pool_ops_t ops; - // Holds array of memory providers. All providers are wrapped - // by memory tracking providers (owned and released by UMA). - uma_memory_provider_handle_t *providers; + // All providers are wrapped by memory tracking providers (owned and released by UMA). + uma_memory_provider_handle_t data_provider; + uma_memory_provider_handle_t metadata_provider; size_t numProviders; }; -static void -destroyMemoryProviderWrappers(uma_memory_provider_handle_t *providers, - size_t numProviders) { - for (size_t i = 0; i < numProviders; i++) { - umaMemoryProviderDestroy(providers[i]); - } - - free(providers); -} - enum uma_result_t umaPoolCreate(struct uma_memory_pool_ops_t *ops, - uma_memory_provider_handle_t *providers, - size_t numProviders, void *params, - uma_memory_pool_handle_t *hPool) { - if (!numProviders || !providers) { + uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, + void *params, uma_memory_pool_handle_t *hPool) { + if (!data_provider) { return UMA_RESULT_ERROR_INVALID_ARGUMENT; } @@ -54,27 +44,25 @@ enum uma_result_t umaPoolCreate(struct uma_memory_pool_ops_t *ops, assert(ops->version == UMA_VERSION_CURRENT); - pool->providers = - calloc(numProviders, sizeof(uma_memory_provider_handle_t)); - if (!pool->providers) { - ret = UMA_RESULT_ERROR_OUT_OF_HOST_MEMORY; - goto err_providers_alloc; + // Wrap each provider with memory tracking provider. + ret = umaTrackingMemoryProviderCreate(data_provider, pool, + &pool->data_provider); + if (ret != UMA_RESULT_SUCCESS) { + goto err_providers_wrap; } - size_t providerInd = 0; - pool->numProviders = numProviders; - - // Wrap each provider with memory tracking provider. - for (providerInd = 0; providerInd < numProviders; providerInd++) { - ret = umaTrackingMemoryProviderCreate(providers[providerInd], pool, - &pool->providers[providerInd]); + if (metadata_provider) { + ret = umaTrackingMemoryProviderCreate(metadata_provider, pool, + &pool->metadata_provider); if (ret != UMA_RESULT_SUCCESS) { - goto err_providers_init; + goto err_providers_metadata_wrap; } + } else { + pool->metadata_provider = NULL; } pool->ops = *ops; - ret = ops->initialize(pool->providers, pool->numProviders, params, + ret = ops->initialize(pool->data_provider, pool->metadata_provider, params, &pool->pool_priv); if (ret != UMA_RESULT_SUCCESS) { goto err_pool_init; @@ -84,9 +72,12 @@ enum uma_result_t umaPoolCreate(struct uma_memory_pool_ops_t *ops, return UMA_RESULT_SUCCESS; err_pool_init: -err_providers_init: - destroyMemoryProviderWrappers(pool->providers, providerInd); -err_providers_alloc: + if (pool->metadata_provider) { + umaMemoryProviderDestroy(pool->metadata_provider); + } +err_providers_metadata_wrap: + umaMemoryProviderDestroy(pool->data_provider); +err_providers_wrap: free(pool); return ret; @@ -94,7 +85,10 @@ enum uma_result_t umaPoolCreate(struct uma_memory_pool_ops_t *ops, void umaPoolDestroy(uma_memory_pool_handle_t hPool) { hPool->ops.finalize(hPool->pool_priv); - destroyMemoryProviderWrappers(hPool->providers, hPool->numProviders); + if (hPool->metadata_provider) { + umaMemoryProviderDestroy(hPool->metadata_provider); + } + umaMemoryProviderDestroy(hPool->data_provider); free(hPool); } @@ -139,24 +133,12 @@ uma_memory_pool_handle_t umaPoolByPtr(const void *ptr) { return umaMemoryTrackerGetPool(umaMemoryTrackerGet(), ptr); } -enum uma_result_t -umaPoolGetMemoryProviders(uma_memory_pool_handle_t hPool, size_t numProviders, - uma_memory_provider_handle_t *hProviders, - size_t *numProvidersRet) { - if (hProviders && numProviders < hPool->numProviders) { - return UMA_RESULT_ERROR_INVALID_ARGUMENT; - } - - if (numProvidersRet) { - *numProvidersRet = hPool->numProviders; - } - - if (hProviders) { - for (size_t i = 0; i < hPool->numProviders; i++) { - umaTrackingMemoryProviderGetUpstreamProvider( - umaMemoryProviderGetPriv(hPool->providers[i]), hProviders + i); - } - } +uma_memory_provider_handle_t +umaPoolGetMetadataMemoryProvider(uma_memory_pool_handle_t hPool) { + return hPool->ops.get_metadata_memory_provider(hPool->pool_priv); +} - return UMA_RESULT_SUCCESS; +uma_memory_provider_handle_t +umaPoolGetDataMemoryProvider(uma_memory_pool_handle_t hPool) { + return hPool->ops.get_data_memory_provider(hPool->pool_priv); } diff --git a/source/common/unified_memory_allocation/src/memory_provider.c b/source/common/unified_memory_allocation/src/memory_provider.c index 23b05cd4d4..a73374d7b4 100644 --- a/source/common/unified_memory_allocation/src/memory_provider.c +++ b/source/common/unified_memory_allocation/src/memory_provider.c @@ -98,7 +98,6 @@ umaMemoryProviderPurgeForce(uma_memory_provider_handle_t hProvider, void *ptr, return hProvider->ops.purge_force(hProvider->provider_priv, ptr, size); } -void umaMemoryProviderGetName(uma_memory_provider_handle_t hProvider, - const char **ppName) { - hProvider->ops.get_name(hProvider->provider_priv, ppName); +const char *umaMemoryProviderGetName(uma_memory_provider_handle_t hProvider) { + return hProvider->ops.get_name(hProvider->provider_priv); } diff --git a/source/common/unified_memory_allocation/src/memory_tracker.cpp b/source/common/unified_memory_allocation/src/memory_tracker.cpp index 6f56134db2..1116853d75 100644 --- a/source/common/unified_memory_allocation/src/memory_tracker.cpp +++ b/source/common/unified_memory_allocation/src/memory_tracker.cpp @@ -204,10 +204,10 @@ static enum uma_result_t trackingPurgeForce(void *provider, void *ptr, return umaMemoryProviderPurgeForce(p->hUpstream, ptr, size); } -static void trackingName(void *provider, const char **ppName) { +static const char *trackingName(void *provider) { uma_tracking_memory_provider_t *p = (uma_tracking_memory_provider_t *)provider; - return umaMemoryProviderGetName(p->hUpstream, ppName); + return umaMemoryProviderGetName(p->hUpstream); } enum uma_result_t umaTrackingMemoryProviderCreate( @@ -235,13 +235,4 @@ enum uma_result_t umaTrackingMemoryProviderCreate( return umaMemoryProviderCreate(&trackingMemoryProviderOps, ¶ms, hTrackingProvider); } - -void umaTrackingMemoryProviderGetUpstreamProvider( - uma_memory_provider_handle_t hTrackingProvider, - uma_memory_provider_handle_t *hUpstream) { - assert(hUpstream); - uma_tracking_memory_provider_t *p = - (uma_tracking_memory_provider_t *)hTrackingProvider; - *hUpstream = p->hUpstream; -} } diff --git a/source/common/unified_memory_allocation/src/memory_tracker.h b/source/common/unified_memory_allocation/src/memory_tracker.h index 055942174f..06436799ea 100644 --- a/source/common/unified_memory_allocation/src/memory_tracker.h +++ b/source/common/unified_memory_allocation/src/memory_tracker.h @@ -31,10 +31,6 @@ enum uma_result_t umaTrackingMemoryProviderCreate( uma_memory_provider_handle_t hUpstream, uma_memory_pool_handle_t hPool, uma_memory_provider_handle_t *hTrackingProvider); -void umaTrackingMemoryProviderGetUpstreamProvider( - uma_memory_provider_handle_t hTrackingProvider, - uma_memory_provider_handle_t *hUpstream); - #ifdef __cplusplus } #endif diff --git a/test/unified_memory_allocation/common/pool.c b/test/unified_memory_allocation/common/pool.c index 7d229cf11f..6ccf79c259 100644 --- a/test/unified_memory_allocation/common/pool.c +++ b/test/unified_memory_allocation/common/pool.c @@ -11,13 +11,13 @@ #include #include -static enum uma_result_t nullInitialize(uma_memory_provider_handle_t *providers, - size_t numProviders, void *params, - void **pool) { - (void)providers; - (void)numProviders; +static enum uma_result_t +nullInitialize(uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, void *params, + void **pool) { + (void)data_provider; + (void)metadata_provider; (void)params; - assert(providers && numProviders); *pool = NULL; return UMA_RESULT_SUCCESS; } @@ -68,22 +68,34 @@ enum uma_result_t nullGetLastResult(void *pool, const char **ppMsg) { return UMA_RESULT_SUCCESS; } +static uma_memory_provider_handle_t nullGetDataProvider(void *pool) { + (void)pool; + return NULL; +} + +static uma_memory_provider_handle_t nullGetMetadataProvider(void *pool) { + (void)pool; + return NULL; +} + uma_memory_pool_handle_t nullPoolCreate(void) { - struct uma_memory_pool_ops_t ops = {.version = UMA_VERSION_CURRENT, - .initialize = nullInitialize, - .finalize = nullFinalize, - .malloc = nullMalloc, - .realloc = nullRealloc, - .calloc = nullCalloc, - .aligned_malloc = nullAlignedMalloc, - .malloc_usable_size = - nullMallocUsableSize, - .free = nullFree, - .get_last_result = nullGetLastResult}; - - uma_memory_provider_handle_t providerDesc = nullProviderCreate(); + struct uma_memory_pool_ops_t ops = { + .version = UMA_VERSION_CURRENT, + .initialize = nullInitialize, + .finalize = nullFinalize, + .malloc = nullMalloc, + .realloc = nullRealloc, + .calloc = nullCalloc, + .aligned_malloc = nullAlignedMalloc, + .malloc_usable_size = nullMallocUsableSize, + .free = nullFree, + .get_last_result = nullGetLastResult, + .get_data_memory_provider = nullGetDataProvider, + .get_metadata_memory_provider = nullGetMetadataProvider}; + + uma_memory_provider_handle_t provider = nullProviderCreate(); uma_memory_pool_handle_t hPool; - enum uma_result_t ret = umaPoolCreate(&ops, &providerDesc, 1, NULL, &hPool); + enum uma_result_t ret = umaPoolCreate(&ops, provider, NULL, NULL, &hPool); (void)ret; /* silence unused variable warning */ assert(ret == UMA_RESULT_SUCCESS); @@ -100,15 +112,15 @@ struct tracePool { }; static enum uma_result_t -traceInitialize(uma_memory_provider_handle_t *providers, size_t numProviders, - void *params, void **pool) { +traceInitialize(uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, void *params, + void **pool) { struct tracePool *tracePool = (struct tracePool *)malloc(sizeof(struct tracePool)); tracePool->params = *((struct traceParams *)params); - (void)providers; - (void)numProviders; - assert(providers && numProviders); + (void)data_provider; + (void)metadata_provider; *pool = tracePool; return UMA_RESULT_SUCCESS; @@ -166,28 +178,43 @@ enum uma_result_t traceGetLastResult(void *pool, const char **ppMsg) { return umaPoolGetLastResult(tracePool->params.hUpstreamPool, ppMsg); } -uma_memory_pool_handle_t -tracePoolCreate(uma_memory_pool_handle_t hUpstreamPool, - uma_memory_provider_handle_t providerDesc, - void (*trace)(const char *)) { - struct uma_memory_pool_ops_t ops = {.version = UMA_VERSION_CURRENT, - .initialize = traceInitialize, - .finalize = traceFinalize, - .malloc = traceMalloc, - .realloc = traceRealloc, - .calloc = traceCalloc, - .aligned_malloc = traceAlignedMalloc, - .malloc_usable_size = - traceMallocUsableSize, - .free = traceFree, - .get_last_result = traceGetLastResult}; +static uma_memory_provider_handle_t traceGetDataProvider(void *pool) { + struct tracePool *tracePool = (struct tracePool *)pool; + + tracePool->params.trace("get_data_memory_provider"); + return umaPoolGetDataMemoryProvider(tracePool->params.hUpstreamPool); +} + +static uma_memory_provider_handle_t traceGetMetadataProvider(void *pool) { + struct tracePool *tracePool = (struct tracePool *)pool; + + tracePool->params.trace("get_metadata_memory_provider"); + return umaPoolGetMetadataMemoryProvider(tracePool->params.hUpstreamPool); +} + +uma_memory_pool_handle_t tracePoolCreate(uma_memory_pool_handle_t hUpstreamPool, + void (*trace)(const char *)) { + struct uma_memory_pool_ops_t ops = { + .version = UMA_VERSION_CURRENT, + .initialize = traceInitialize, + .finalize = traceFinalize, + .malloc = traceMalloc, + .realloc = traceRealloc, + .calloc = traceCalloc, + .aligned_malloc = traceAlignedMalloc, + .malloc_usable_size = traceMallocUsableSize, + .free = traceFree, + .get_last_result = traceGetLastResult, + .get_data_memory_provider = traceGetDataProvider, + .get_metadata_memory_provider = traceGetMetadataProvider}; struct traceParams params = {.hUpstreamPool = hUpstreamPool, .trace = trace}; uma_memory_pool_handle_t hPool; - enum uma_result_t ret = - umaPoolCreate(&ops, &providerDesc, 1, ¶ms, &hPool); + enum uma_result_t ret = umaPoolCreate( + &ops, umaPoolGetDataMemoryProvider(hUpstreamPool), + umaPoolGetMetadataMemoryProvider(hUpstreamPool), ¶ms, &hPool); (void)ret; /* silence unused variable warning */ assert(ret == UMA_RESULT_SUCCESS); diff --git a/test/unified_memory_allocation/common/pool.h b/test/unified_memory_allocation/common/pool.h index d9719ad458..37e7cb8b41 100644 --- a/test/unified_memory_allocation/common/pool.h +++ b/test/unified_memory_allocation/common/pool.h @@ -13,10 +13,8 @@ extern "C" { #endif uma_memory_pool_handle_t nullPoolCreate(void); -uma_memory_pool_handle_t -tracePoolCreate(uma_memory_pool_handle_t hUpstreamPool, - uma_memory_provider_handle_t providerDesc, - void (*trace)(const char *)); +uma_memory_pool_handle_t tracePoolCreate(uma_memory_pool_handle_t hUpstreamPool, + void (*trace)(const char *)); #if defined(__cplusplus) } diff --git a/test/unified_memory_allocation/common/pool.hpp b/test/unified_memory_allocation/common/pool.hpp index f360af34dd..60e5b0a27f 100644 --- a/test/unified_memory_allocation/common/pool.hpp +++ b/test/unified_memory_allocation/common/pool.hpp @@ -28,7 +28,8 @@ auto wrapPoolUnique(uma_memory_pool_handle_t hPool) { } struct pool_base { - uma_result_t initialize(uma_memory_provider_handle_t *, size_t) noexcept { + uma_result_t initialize(uma_memory_provider_handle_t, + uma_memory_provider_handle_t) noexcept { return UMA_RESULT_SUCCESS; }; void *malloc(size_t size) noexcept { return nullptr; } @@ -40,6 +41,12 @@ struct pool_base { enum uma_result_t get_last_result(const char **ppMessage) noexcept { return UMA_RESULT_ERROR_UNKNOWN; } + uma_memory_provider_handle_t get_data_memory_provider() noexcept { + return NULL; + } + uma_memory_provider_handle_t get_metadata_memory_provider() noexcept { + return NULL; + } }; struct malloc_pool : public pool_base { @@ -69,9 +76,11 @@ struct malloc_pool : public pool_base { }; struct proxy_pool : public pool_base { - uma_result_t initialize(uma_memory_provider_handle_t *providers, - size_t numProviders) noexcept { - this->provider = providers[0]; + uma_result_t + initialize(uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider) noexcept { + this->provider = data_provider; + this->metadata_provider = metadata_provider; return UMA_RESULT_SUCCESS; } void *malloc(size_t size) noexcept { return aligned_malloc(size, 0); } @@ -105,7 +114,14 @@ struct proxy_pool : public pool_base { enum uma_result_t get_last_result(const char **ppMessage) noexcept { return umaMemoryProviderGetLastResult(provider, ppMessage); } + uma_memory_provider_handle_t get_data_memory_provider() noexcept { + return provider; + } + uma_memory_provider_handle_t get_metadata_memory_provider() noexcept { + return metadata_provider; + } uma_memory_provider_handle_t provider; + uma_memory_provider_handle_t metadata_provider; }; } // namespace uma_test diff --git a/test/unified_memory_allocation/common/provider.c b/test/unified_memory_allocation/common/provider.c index 05dce162ed..4608e55061 100644 --- a/test/unified_memory_allocation/common/provider.c +++ b/test/unified_memory_allocation/common/provider.c @@ -72,9 +72,9 @@ static enum uma_result_t nullPurgeForce(void *provider, void *ptr, return UMA_RESULT_SUCCESS; } -static void nullName(void *provider, const char **ppName) { +static const char *nullName(void *provider) { (void)provider; - *ppName = "null"; + return "null"; } uma_memory_provider_handle_t nullProviderCreate(void) { @@ -177,11 +177,11 @@ static enum uma_result_t tracePurgeForce(void *provider, void *ptr, size); } -static void traceName(void *provider, const char **ppName) { +static const char *traceName(void *provider) { struct traceParams *traceProvider = (struct traceParams *)provider; traceProvider->trace("name"); - umaMemoryProviderGetName(traceProvider->hUpstreamProvider, ppName); + return umaMemoryProviderGetName(traceProvider->hUpstreamProvider); } uma_memory_provider_handle_t diff --git a/test/unified_memory_allocation/common/provider.hpp b/test/unified_memory_allocation/common/provider.hpp index c60ebf0c3d..b9ea4041fa 100644 --- a/test/unified_memory_allocation/common/provider.hpp +++ b/test/unified_memory_allocation/common/provider.hpp @@ -49,7 +49,7 @@ struct provider_base { enum uma_result_t purge_force(void *ptr, size_t size) noexcept { return UMA_RESULT_ERROR_UNKNOWN; } - void get_name(const char **ppName) noexcept { *ppName = "base"; } + const char *get_name() noexcept { return "base"; } }; struct provider_malloc : public provider_base { @@ -75,7 +75,7 @@ struct provider_malloc : public provider_base { #endif return UMA_RESULT_SUCCESS; } - void get_name(const char **ppName) noexcept { *ppName = "malloc"; } + const char *get_name() noexcept { return "malloc"; } }; } // namespace uma_test diff --git a/test/unified_memory_allocation/memoryPoolAPI.cpp b/test/unified_memory_allocation/memoryPoolAPI.cpp index af5aa5f077..d95ebfda2c 100644 --- a/test/unified_memory_allocation/memoryPoolAPI.cpp +++ b/test/unified_memory_allocation/memoryPoolAPI.cpp @@ -30,12 +30,11 @@ TEST_F(test, memoryPoolTrace) { auto provider = tracingProvider.get(); auto [ret, proxyPool] = - uma::poolMakeUnique(&provider, 1); + uma::poolMakeUnique(provider, nullptr); ASSERT_EQ(ret, UMA_RESULT_SUCCESS); - uma_memory_provider_handle_t providerDesc = nullProviderCreate(); - auto tracingPool = uma_test::wrapPoolUnique( - tracePoolCreate(proxyPool.get(), providerDesc, tracePool)); + auto tracingPool = + uma_test::wrapPoolUnique(tracePoolCreate(proxyPool.get(), tracePool)); size_t pool_call_count = 0; size_t provider_call_count = 0; @@ -89,51 +88,50 @@ TEST_F(test, memoryPoolTrace) { ASSERT_EQ(providerCalls["get_last_result"], 1); ASSERT_EQ(providerCalls.size(), ++provider_call_count); - umaMemoryProviderDestroy(providerDesc); -} + auto p = umaPoolGetDataMemoryProvider(tracingPool.get()); + ASSERT_NE(p, nullptr); + ASSERT_EQ(poolCalls["get_data_memory_provider"], 1); + ASSERT_EQ(poolCalls.size(), ++pool_call_count); -TEST_F(test, memoryPoolWithCustomProviders) { - uma_memory_provider_handle_t providers[] = {nullProviderCreate(), - nullProviderCreate()}; + p = umaPoolGetMetadataMemoryProvider(tracingPool.get()); + ASSERT_EQ(poolCalls["get_metadata_memory_provider"], 1); + ASSERT_EQ(poolCalls.size(), ++pool_call_count); +} - struct pool : public uma_test::pool_base { - uma_result_t initialize(uma_memory_provider_handle_t *providers, - size_t numProviders) noexcept { - EXPECT_NE_NOEXCEPT(providers, nullptr); - EXPECT_EQ_NOEXCEPT(numProviders, 2); +TEST_F(test, retrieveMemoryProviders) { + struct memory_provider : uma_test::provider_base { + const char *name; + uma_result_t initialize(const char *name) { + this->name = name; return UMA_RESULT_SUCCESS; } + const char *get_name() noexcept { return name; } }; - auto ret = uma::poolMakeUnique(providers, 2); - ASSERT_EQ(ret.first, UMA_RESULT_SUCCESS); - ASSERT_NE(ret.second, nullptr); + auto [ret1, data_provider] = + uma::memoryProviderMakeUnique("data_provider"); + auto [ret2, metadata_provider] = + uma::memoryProviderMakeUnique("metadata_provider"); - for (auto &provider : providers) { - umaMemoryProviderDestroy(provider); - } -} - -TEST_F(test, retrieveMemoryProviders) { - static constexpr size_t numProviders = 4; - std::array providers = { - (uma_memory_provider_handle_t)0x1, (uma_memory_provider_handle_t)0x2, - (uma_memory_provider_handle_t)0x3, (uma_memory_provider_handle_t)0x4}; + ASSERT_EQ(ret1, UMA_RESULT_SUCCESS); + ASSERT_EQ(ret2, UMA_RESULT_SUCCESS); auto [ret, pool] = uma::poolMakeUnique( - providers.data(), numProviders); - - std::array retProviders; - size_t numProvidersRet = 0; + data_provider.get(), metadata_provider.get()); - ret = umaPoolGetMemoryProviders(pool.get(), 0, nullptr, &numProvidersRet); ASSERT_EQ(ret, UMA_RESULT_SUCCESS); - ASSERT_EQ(numProvidersRet, numProviders); - ret = umaPoolGetMemoryProviders(pool.get(), numProviders, - retProviders.data(), nullptr); - ASSERT_EQ(ret, UMA_RESULT_SUCCESS); - ASSERT_EQ(retProviders, providers); + uma_memory_provider_handle_t ret_data_provider = + umaPoolGetDataMemoryProvider(pool.get()); + uma_memory_provider_handle_t ret_metadata_provider = + umaPoolGetMetadataMemoryProvider(pool.get()); + + // comapre names of the providers: handles might point to a different object if providers are wrapped by memory tracking-provider + ASSERT_EQ(std::string_view(umaMemoryProviderGetName(data_provider.get())), + std::string_view(umaMemoryProviderGetName(ret_data_provider))); + ASSERT_EQ( + std::string_view(umaMemoryProviderGetName(metadata_provider.get())), + std::string_view(umaMemoryProviderGetName(ret_metadata_provider))); } template @@ -141,7 +139,7 @@ static auto makePool(std::function makeProvider) { auto providerUnique = makeProvider(); uma_memory_provider_handle_t provider = providerUnique.get(); - auto pool = uma::poolMakeUnique(&provider, 1).second; + auto pool = uma::poolMakeUnique(provider, nullptr).second; auto dtor = [provider = providerUnique.release()](uma_memory_pool_handle_t hPool) { umaPoolDestroy(hPool); @@ -176,15 +174,7 @@ INSTANTIATE_TEST_SUITE_P( ////////////////// Negative test cases ///////////////// TEST_F(test, memoryPoolInvalidProvidersNullptr) { - auto ret = uma::poolMakeUnique(nullptr, 1); - ASSERT_EQ(ret.first, UMA_RESULT_ERROR_INVALID_ARGUMENT); -} - -TEST_F(test, memoryPoolInvalidProvidersNum) { - auto nullProvider = uma_test::wrapProviderUnique(nullProviderCreate()); - uma_memory_provider_handle_t providers[] = {nullProvider.get()}; - - auto ret = uma::poolMakeUnique(providers, 0); + auto ret = uma::poolMakeUnique(nullptr, nullptr); ASSERT_EQ(ret.first, UMA_RESULT_ERROR_INVALID_ARGUMENT); } @@ -195,35 +185,22 @@ INSTANTIATE_TEST_SUITE_P( poolInitializeTest, poolInitializeTest, ::testing::Values(UMA_RESULT_ERROR_OUT_OF_HOST_MEMORY, UMA_RESULT_ERROR_POOL_SPECIFIC, - UMA_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC, UMA_RESULT_ERROR_INVALID_ARGUMENT, + UMA_RESULT_ERROR_METADATA_MEMORY_PROVIDER_SPECIFIC, + UMA_RESULT_ERROR_DATA_MEMORY_PROVIDER_SPECIFIC, UMA_RESULT_ERROR_UNKNOWN)); TEST_P(poolInitializeTest, errorPropagation) { auto nullProvider = uma_test::wrapProviderUnique(nullProviderCreate()); - uma_memory_provider_handle_t providers[] = {nullProvider.get()}; - struct pool : public uma_test::pool_base { - uma_result_t initialize(uma_memory_provider_handle_t *providers, - size_t numProviders, + uma_result_t initialize(uma_memory_provider_handle_t data_provider, + uma_memory_provider_handle_t metadata_provider, uma_result_t errorToReturn) noexcept { return errorToReturn; } }; - auto ret = uma::poolMakeUnique(providers, 1, this->GetParam()); + auto ret = uma::poolMakeUnique(nullProvider.get(), nullptr, + this->GetParam()); ASSERT_EQ(ret.first, this->GetParam()); ASSERT_EQ(ret.second, nullptr); } - -TEST_F(test, retrieveMemoryProvidersError) { - static constexpr size_t numProviders = 4; - std::array providers = { - (uma_memory_provider_handle_t)0x1, (uma_memory_provider_handle_t)0x2, - (uma_memory_provider_handle_t)0x3, (uma_memory_provider_handle_t)0x4}; - - auto [ret, pool] = uma::poolMakeUnique( - providers.data(), numProviders); - - ret = umaPoolGetMemoryProviders(pool.get(), 1, providers.data(), nullptr); - ASSERT_EQ(ret, UMA_RESULT_ERROR_INVALID_ARGUMENT); -} diff --git a/test/unified_memory_allocation/memoryProviderAPI.cpp b/test/unified_memory_allocation/memoryProviderAPI.cpp index f770050f32..37ec352062 100644 --- a/test/unified_memory_allocation/memoryProviderAPI.cpp +++ b/test/unified_memory_allocation/memoryProviderAPI.cpp @@ -60,8 +60,7 @@ TEST_F(test, memoryProviderTrace) { ASSERT_EQ(calls["purge_force"], 1); ASSERT_EQ(calls.size(), ++call_count); - const char *pName; - umaMemoryProviderGetName(tracingProvider.get(), &pName); + const char *pName = umaMemoryProviderGetName(tracingProvider.get()); ASSERT_EQ(calls["name"], 1); ASSERT_EQ(calls.size(), ++call_count); ASSERT_EQ(std::string(pName), std::string("null")); @@ -77,7 +76,8 @@ INSTANTIATE_TEST_SUITE_P( providerInitializeTest, providerInitializeTest, ::testing::Values(UMA_RESULT_ERROR_OUT_OF_HOST_MEMORY, UMA_RESULT_ERROR_POOL_SPECIFIC, - UMA_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC, + UMA_RESULT_ERROR_METADATA_MEMORY_PROVIDER_SPECIFIC, + UMA_RESULT_ERROR_DATA_MEMORY_PROVIDER_SPECIFIC, UMA_RESULT_ERROR_INVALID_ARGUMENT, UMA_RESULT_ERROR_UNKNOWN)); diff --git a/test/unified_memory_allocation/uma_pools/disjoint_pool.cpp b/test/unified_memory_allocation/uma_pools/disjoint_pool.cpp index 88b6d769d8..1eeea0aeb9 100644 --- a/test/unified_memory_allocation/uma_pools/disjoint_pool.cpp +++ b/test/unified_memory_allocation/uma_pools/disjoint_pool.cpp @@ -29,7 +29,7 @@ static auto makePool() { EXPECT_EQ(ret, UMA_RESULT_SUCCESS); auto provider = providerUnique.release(); auto [retp, pool] = - uma::poolMakeUnique(&provider, 1, poolConfig()); + uma::poolMakeUnique(provider, nullptr, poolConfig()); EXPECT_EQ(retp, UMA_RESULT_SUCCESS); auto dtor = [provider = provider](uma_memory_pool_handle_t hPool) { umaPoolDestroy(hPool);