Skip to content

Commit 04bde82

Browse files
committed
[uma] add a function to query name of a provider
To allow interacting with the underlying implementation directly. This will be needed to support returning native error codes (name can be used to figure out how to interpret those errors) and possibly to support provider-specific queries.
1 parent 51dede3 commit 04bde82

File tree

9 files changed

+48
-3
lines changed

9 files changed

+48
-3
lines changed

source/common/uma_helpers.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ auto memoryProviderMakeUnique(Args &&...args) {
9090
noexcept(reinterpret_cast<T *>(obj)->purge_force(args...)));
9191
return reinterpret_cast<T *>(obj)->purge_force(args...);
9292
};
93+
ops.get_name = [](void *obj, auto... args) {
94+
static_assert(noexcept(reinterpret_cast<T *>(obj)->get_name(args...)));
95+
return reinterpret_cast<T *>(obj)->get_name(args...);
96+
};
9397

9498
uma_memory_provider_handle_t hProvider = nullptr;
9599
auto ret = umaMemoryProviderCreate(&ops, &argsTuple, &hProvider);

source/common/unified_memory_allocation/include/uma/memory_provider.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ enum uma_result_t
132132
umaMemoryProviderPurgeForce(uma_memory_provider_handle_t hProvider, void *ptr,
133133
size_t size);
134134

135+
///
136+
/// \brief Retrive name of a given memory provider.
137+
/// \param hProvider handle to the memory provider
138+
/// \param ppName [out] pointer to a string containing name of the provider
139+
void umaMemoryProviderGetName(uma_memory_provider_handle_t hProvider,
140+
const char **ppName);
141+
135142
#ifdef __cplusplus
136143
}
137144
#endif

source/common/unified_memory_allocation/include/uma/memory_provider_ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct uma_memory_provider_ops_t {
4949
size_t *pageSize);
5050
enum uma_result_t (*purge_lazy)(void *provider, void *ptr, size_t size);
5151
enum uma_result_t (*purge_force)(void *provider, void *ptr, size_t size);
52+
void (*get_name)(void *provider, const char **ppName);
5253
};
5354

5455
#ifdef __cplusplus

source/common/unified_memory_allocation/src/memory_provider.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,8 @@ umaMemoryProviderPurgeForce(uma_memory_provider_handle_t hProvider, void *ptr,
9797
size_t size) {
9898
return hProvider->ops.purge_force(hProvider->provider_priv, ptr, size);
9999
}
100+
101+
void umaMemoryProviderGetName(uma_memory_provider_handle_t hProvider,
102+
const char **ppName) {
103+
hProvider->ops.get_name(hProvider->provider_priv, ppName);
104+
}

source/common/unified_memory_allocation/src/memory_tracker.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ static enum uma_result_t trackingPurgeForce(void *provider, void *ptr,
204204
return umaMemoryProviderPurgeForce(p->hUpstream, ptr, size);
205205
}
206206

207+
static void trackingName(void *provider, const char **ppName) {
208+
uma_tracking_memory_provider_t *p =
209+
(uma_tracking_memory_provider_t *)provider;
210+
return umaMemoryProviderGetName(p->hUpstream, ppName);
211+
}
212+
207213
enum uma_result_t umaTrackingMemoryProviderCreate(
208214
uma_memory_provider_handle_t hUpstream, uma_memory_pool_handle_t hPool,
209215
uma_memory_provider_handle_t *hTrackingProvider) {
@@ -224,6 +230,7 @@ enum uma_result_t umaTrackingMemoryProviderCreate(
224230
trackingGetRecommendedPageSize;
225231
trackingMemoryProviderOps.purge_force = trackingPurgeForce;
226232
trackingMemoryProviderOps.purge_lazy = trackingPurgeLazy;
233+
trackingMemoryProviderOps.get_name = trackingName;
227234

228235
return umaMemoryProviderCreate(&trackingMemoryProviderOps, &params,
229236
hTrackingProvider);

test/unified_memory_allocation/common/provider.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ static enum uma_result_t nullPurgeForce(void *provider, void *ptr,
7272
return UMA_RESULT_SUCCESS;
7373
}
7474

75+
static void nullName(void *provider, const char **ppName) {
76+
(void)provider;
77+
*ppName = "null";
78+
}
79+
7580
uma_memory_provider_handle_t nullProviderCreate(void) {
7681
struct uma_memory_provider_ops_t ops = {
7782
.version = UMA_VERSION_CURRENT,
@@ -83,7 +88,8 @@ uma_memory_provider_handle_t nullProviderCreate(void) {
8388
.get_recommended_page_size = nullGetRecommendedPageSize,
8489
.get_min_page_size = nullGetPageSize,
8590
.purge_lazy = nullPurgeLazy,
86-
.purge_force = nullPurgeForce};
91+
.purge_force = nullPurgeForce,
92+
.get_name = nullName};
8793

8894
uma_memory_provider_handle_t hProvider;
8995
enum uma_result_t ret = umaMemoryProviderCreate(&ops, NULL, &hProvider);
@@ -171,6 +177,13 @@ static enum uma_result_t tracePurgeForce(void *provider, void *ptr,
171177
size);
172178
}
173179

180+
static void traceName(void *provider, const char **ppName) {
181+
struct traceParams *traceProvider = (struct traceParams *)provider;
182+
183+
traceProvider->trace("name");
184+
umaMemoryProviderGetName(traceProvider->hUpstreamProvider, ppName);
185+
}
186+
174187
uma_memory_provider_handle_t
175188
traceProviderCreate(uma_memory_provider_handle_t hUpstreamProvider,
176189
void (*trace)(const char *)) {
@@ -184,7 +197,8 @@ traceProviderCreate(uma_memory_provider_handle_t hUpstreamProvider,
184197
.get_recommended_page_size = traceGetRecommendedPageSize,
185198
.get_min_page_size = traceGetPageSize,
186199
.purge_lazy = tracePurgeLazy,
187-
.purge_force = tracePurgeForce};
200+
.purge_force = tracePurgeForce,
201+
.get_name = traceName};
188202

189203
struct traceParams params = {.hUpstreamProvider = hUpstreamProvider,
190204
.trace = trace};

test/unified_memory_allocation/common/provider.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ uma_memory_provider_handle_t nullProviderCreate(void);
1616
uma_memory_provider_handle_t
1717
traceProviderCreate(uma_memory_provider_handle_t hUpstreamProvider,
1818
void (*trace)(const char *));
19-
uma_memory_provider_handle_t mallocProviderCreate(void);
2019

2120
#if defined(__cplusplus)
2221
}

test/unified_memory_allocation/common/provider.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct provider_base {
4949
enum uma_result_t purge_force(void *ptr, size_t size) noexcept {
5050
return UMA_RESULT_ERROR_UNKNOWN;
5151
}
52+
void get_name(const char **ppName) noexcept { *ppName = "base"; }
5253
};
5354

5455
struct provider_malloc : public provider_base {
@@ -74,6 +75,7 @@ struct provider_malloc : public provider_base {
7475
#endif
7576
return UMA_RESULT_SUCCESS;
7677
}
78+
void get_name(const char **ppName) noexcept { *ppName = "malloc"; }
7779
};
7880

7981
} // namespace uma_test

test/unified_memory_allocation/memoryProviderAPI.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ TEST_F(test, memoryProviderTrace) {
5959
ASSERT_EQ(ret, UMA_RESULT_SUCCESS);
6060
ASSERT_EQ(calls["purge_force"], 1);
6161
ASSERT_EQ(calls.size(), ++call_count);
62+
63+
const char *pName;
64+
umaMemoryProviderGetName(tracingProvider.get(), &pName);
65+
ASSERT_EQ(calls["name"], 1);
66+
ASSERT_EQ(calls.size(), ++call_count);
67+
ASSERT_EQ(std::string(pName), std::string("null"));
6268
}
6369

6470
//////////////////////////// Negative test cases

0 commit comments

Comments
 (0)