Skip to content

Commit f09ed03

Browse files
committed
[umf] Make pool tracking optional
This adds a new CMake option 'UMF_ENABLE_POOL_TRACKING' that enables pool tracking in UMF. Pool tracking is turned off by default. The pool tracking tests were wrapped around the compile definition 'UMF_ENABLE_POOL_TRACKING_TESTS'.
1 parent 2ffcda3 commit f09ed03

File tree

10 files changed

+268
-118
lines changed

10 files changed

+268
-118
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ option(UMF_BUILD_SHARED_LIBRARY "Build UMF as shared library" OFF)
3434
option(UR_ENABLE_TRACING "enable api tracing through xpti" OFF)
3535
option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace for linux" OFF)
3636
option(UR_BUILD_TOOLS "build ur tools" ON)
37+
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" OFF)
3738

3839
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
3940
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

source/common/unified_malloc_framework/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ else()
2222
${UMF_SOURCES})
2323
endif()
2424

25+
if (UMF_ENABLE_POOL_TRACKING)
26+
target_sources(unified_malloc_framework PRIVATE src/memory_pool_tracking.c)
27+
else()
28+
target_sources(unified_malloc_framework PRIVATE src/memory_pool_default.c)
29+
endif()
30+
2531
add_library(${PROJECT_NAME}::unified_malloc_framework ALIAS unified_malloc_framework)
2632

2733
target_include_directories(unified_malloc_framework PUBLIC include)

source/common/unified_malloc_framework/src/memory_pool.c

Lines changed: 1 addition & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -8,96 +8,14 @@
88
*
99
*/
1010

11-
#include "memory_provider_internal.h"
12-
#include "memory_tracker.h"
11+
#include "memory_pool_internal.h"
1312

1413
#include <umf/memory_pool.h>
1514
#include <umf/memory_pool_ops.h>
1615

1716
#include <assert.h>
1817
#include <stdlib.h>
1918

20-
struct umf_memory_pool_t {
21-
void *pool_priv;
22-
struct umf_memory_pool_ops_t ops;
23-
24-
// Holds array of memory providers. All providers are wrapped
25-
// by memory tracking providers (owned and released by UMF).
26-
umf_memory_provider_handle_t *providers;
27-
28-
size_t numProviders;
29-
};
30-
31-
static void
32-
destroyMemoryProviderWrappers(umf_memory_provider_handle_t *providers,
33-
size_t numProviders) {
34-
for (size_t i = 0; i < numProviders; i++) {
35-
umfMemoryProviderDestroy(providers[i]);
36-
}
37-
38-
free(providers);
39-
}
40-
41-
enum umf_result_t umfPoolCreate(const struct umf_memory_pool_ops_t *ops,
42-
umf_memory_provider_handle_t *providers,
43-
size_t numProviders, void *params,
44-
umf_memory_pool_handle_t *hPool) {
45-
if (!numProviders || !providers) {
46-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
47-
}
48-
49-
enum umf_result_t ret = UMF_RESULT_SUCCESS;
50-
umf_memory_pool_handle_t pool = malloc(sizeof(struct umf_memory_pool_t));
51-
if (!pool) {
52-
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
53-
}
54-
55-
assert(ops->version == UMF_VERSION_CURRENT);
56-
57-
pool->providers =
58-
calloc(numProviders, sizeof(umf_memory_provider_handle_t));
59-
if (!pool->providers) {
60-
ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
61-
goto err_providers_alloc;
62-
}
63-
64-
size_t providerInd = 0;
65-
pool->numProviders = numProviders;
66-
67-
// Wrap each provider with memory tracking provider.
68-
for (providerInd = 0; providerInd < numProviders; providerInd++) {
69-
ret = umfTrackingMemoryProviderCreate(providers[providerInd], pool,
70-
&pool->providers[providerInd]);
71-
if (ret != UMF_RESULT_SUCCESS) {
72-
goto err_providers_init;
73-
}
74-
}
75-
76-
pool->ops = *ops;
77-
ret = ops->initialize(pool->providers, pool->numProviders, params,
78-
&pool->pool_priv);
79-
if (ret != UMF_RESULT_SUCCESS) {
80-
goto err_pool_init;
81-
}
82-
83-
*hPool = pool;
84-
return UMF_RESULT_SUCCESS;
85-
86-
err_pool_init:
87-
err_providers_init:
88-
destroyMemoryProviderWrappers(pool->providers, providerInd);
89-
err_providers_alloc:
90-
free(pool);
91-
92-
return ret;
93-
}
94-
95-
void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
96-
hPool->ops.finalize(hPool->pool_priv);
97-
destroyMemoryProviderWrappers(hPool->providers, hPool->numProviders);
98-
free(hPool);
99-
}
100-
10119
void *umfPoolMalloc(umf_memory_pool_handle_t hPool, size_t size) {
10220
return hPool->ops.malloc(hPool->pool_priv, size);
10321
}
@@ -123,41 +41,7 @@ enum umf_result_t umfPoolFree(umf_memory_pool_handle_t hPool, void *ptr) {
12341
return hPool->ops.free(hPool->pool_priv, ptr);
12442
}
12543

126-
enum umf_result_t umfFree(void *ptr) {
127-
umf_memory_pool_handle_t hPool = umfPoolByPtr(ptr);
128-
if (hPool) {
129-
return umfPoolFree(hPool, ptr);
130-
}
131-
return UMF_RESULT_SUCCESS;
132-
}
133-
13444
enum umf_result_t
13545
umfPoolGetLastAllocationError(umf_memory_pool_handle_t hPool) {
13646
return hPool->ops.get_last_allocation_error(hPool->pool_priv);
13747
}
138-
139-
umf_memory_pool_handle_t umfPoolByPtr(const void *ptr) {
140-
return umfMemoryTrackerGetPool(umfMemoryTrackerGet(), ptr);
141-
}
142-
143-
enum umf_result_t
144-
umfPoolGetMemoryProviders(umf_memory_pool_handle_t hPool, size_t numProviders,
145-
umf_memory_provider_handle_t *hProviders,
146-
size_t *numProvidersRet) {
147-
if (hProviders && numProviders < hPool->numProviders) {
148-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
149-
}
150-
151-
if (numProvidersRet) {
152-
*numProvidersRet = hPool->numProviders;
153-
}
154-
155-
if (hProviders) {
156-
for (size_t i = 0; i < hPool->numProviders; i++) {
157-
umfTrackingMemoryProviderGetUpstreamProvider(
158-
umfMemoryProviderGetPriv(hPool->providers[i]), hProviders + i);
159-
}
160-
}
161-
162-
return UMF_RESULT_SUCCESS;
163-
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
*
3+
* Copyright (C) 2023 Intel Corporation
4+
*
5+
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
6+
* See LICENSE.TXT
7+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
*
9+
*/
10+
11+
#include "memory_pool_internal.h"
12+
13+
#include <umf/memory_pool.h>
14+
15+
#include <assert.h>
16+
#include <stdlib.h>
17+
18+
enum umf_result_t umfPoolCreate(const struct umf_memory_pool_ops_t *ops,
19+
umf_memory_provider_handle_t *providers,
20+
size_t numProviders, void *params,
21+
umf_memory_pool_handle_t *hPool) {
22+
if (!numProviders || !providers) {
23+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
24+
}
25+
26+
enum umf_result_t ret = UMF_RESULT_SUCCESS;
27+
umf_memory_pool_handle_t pool = malloc(sizeof(struct umf_memory_pool_t));
28+
if (!pool) {
29+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
30+
}
31+
32+
assert(ops->version == UMF_VERSION_CURRENT);
33+
34+
pool->providers =
35+
calloc(numProviders, sizeof(umf_memory_provider_handle_t));
36+
if (!pool->providers) {
37+
ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
38+
goto err_providers_alloc;
39+
}
40+
41+
size_t providerInd = 0;
42+
pool->numProviders = numProviders;
43+
44+
for (providerInd = 0; providerInd < numProviders; providerInd++) {
45+
pool->providers[providerInd] = providers[providerInd];
46+
}
47+
48+
pool->ops = *ops;
49+
ret = ops->initialize(pool->providers, pool->numProviders, params,
50+
&pool->pool_priv);
51+
if (ret != UMF_RESULT_SUCCESS) {
52+
goto err_pool_init;
53+
}
54+
55+
*hPool = pool;
56+
return UMF_RESULT_SUCCESS;
57+
58+
err_pool_init:
59+
err_providers_alloc:
60+
free(pool);
61+
62+
return ret;
63+
}
64+
65+
void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
66+
hPool->ops.finalize(hPool->pool_priv);
67+
free(hPool);
68+
}
69+
70+
enum umf_result_t umfFree(void *ptr) { return UMF_RESULT_ERROR_NOT_SUPPORTED; }
71+
72+
umf_memory_pool_handle_t umfPoolByPtr(const void *ptr) { return NULL; }
73+
74+
enum umf_result_t
75+
umfPoolGetMemoryProviders(umf_memory_pool_handle_t hPool, size_t numProviders,
76+
umf_memory_provider_handle_t *hProviders,
77+
size_t *numProvidersRet) {
78+
if (hProviders && numProviders < hPool->numProviders) {
79+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
80+
}
81+
82+
if (numProvidersRet) {
83+
*numProvidersRet = hPool->numProviders;
84+
}
85+
86+
if (hProviders) {
87+
for (size_t i = 0; i < hPool->numProviders; i++) {
88+
hProviders[i] = hPool->providers[i];
89+
}
90+
}
91+
92+
return UMF_RESULT_SUCCESS;
93+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
*
3+
* Copyright (C) 2023 Intel Corporation
4+
*
5+
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
6+
* See LICENSE.TXT
7+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
*
9+
*/
10+
11+
#ifndef UMF_MEMORY_POOL_INTERNAL_H
12+
#define UMF_MEMORY_POOL_INTERNAL_H 1
13+
14+
#include <umf/base.h>
15+
#include <umf/memory_pool_ops.h>
16+
#include <umf/memory_provider.h>
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
struct umf_memory_pool_t {
23+
void *pool_priv;
24+
struct umf_memory_pool_ops_t ops;
25+
26+
// Holds array of memory providers. All providers are wrapped
27+
// by memory tracking providers (owned and released by UMF).
28+
umf_memory_provider_handle_t *providers;
29+
30+
size_t numProviders;
31+
};
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif
36+
37+
#endif /* UMF_MEMORY_POOL_INTERNAL_H */

0 commit comments

Comments
 (0)