Skip to content

Commit cb120dd

Browse files
committed
Introduce config params for scalable pool
1 parent 1e8a44a commit cb120dd

File tree

4 files changed

+125
-4
lines changed

4 files changed

+125
-4
lines changed

include/umf/pools/pool_scalable.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,47 @@
1414
extern "C" {
1515
#endif
1616

17+
#include <stdbool.h>
18+
1719
#include <umf/memory_pool.h>
1820
#include <umf/memory_provider.h>
1921

22+
struct umf_scalable_pool_params_t;
23+
24+
/// @brief handle to the parameters of the scalable pool.
25+
typedef struct umf_scalable_pool_params_t *umf_scalable_pool_params_handle_t;
26+
27+
/// @brief Create a struct to store parameters of scalable pool.
28+
/// @param hParams [out] handle to the newly created parameters struct.
29+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
30+
umf_result_t
31+
umfScalablePoolParamsCreate(umf_scalable_pool_params_handle_t *hParams);
32+
33+
/// @brief Destroy parameters struct.
34+
/// @param hParams handle to the parameters of the scalable pool.
35+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
36+
umf_result_t
37+
umfScalablePoolParamsDestroy(umf_scalable_pool_params_handle_t hParams);
38+
39+
/// @brief Set granularity of allocations that scalable pool requests from a memory provider.
40+
/// @param hParams handle to the parameters of the scalable pool.
41+
/// @param granularity granularity in bytes.
42+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
43+
umf_result_t
44+
umfScalablePoolParamsSetGranularity(umf_scalable_pool_params_handle_t hParams,
45+
size_t granularity);
46+
47+
/// @brief Set if scalable pool should keep all memory allocated from memory provider till destruction.
48+
/// @param hParams handle to the parameters of the scalable pool.
49+
/// @param keepAllMemory \p true if the scalable pool should not call
50+
/// \p umfMemoryProviderFree until it is destroyed, \p false otherwise.
51+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
52+
umf_result_t
53+
umfScalablePoolParamsSetKeepAllMemory(umf_scalable_pool_params_handle_t hParams,
54+
bool keepAllMemory);
55+
56+
/// @brief Return \p ops structure containing pointers to the scalable pool implementation.
57+
/// @return pointer to the \p umf_memory_pool_ops_t struct.
2058
umf_memory_pool_ops_t *umfScalablePoolOps(void);
2159

2260
#ifdef __cplusplus

src/libumf.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ EXPORTS
8282
umfProxyPoolOps
8383
umfPutIPCHandle
8484
umfScalablePoolOps
85+
umfScalablePoolParamsCreate
86+
umfScalablePoolParamsDestroy
87+
umfScalablePoolParamsSetGranularity
88+
umfScalablePoolParamsSetKeepAllMemory

src/libumf.map

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ UMF_1.0 {
7676
umfProxyPoolOps;
7777
umfPutIPCHandle;
7878
umfScalablePoolOps;
79+
umfScalablePoolParamsCreate;
80+
umfScalablePoolParamsDestroy;
81+
umfScalablePoolParamsSetGranularity;
82+
umfScalablePoolParamsSetKeepAllMemory;
7983
local:
8084
*;
8185
};

src/pool/pool_scalable.c

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef void (*raw_free_tbb_type)(intptr_t, void *, size_t);
3131
static __TLS umf_result_t TLS_last_allocation_error;
3232
static __TLS umf_result_t TLS_last_free_error;
3333

34+
static const size_t DEFAULT_GRANULARITY = 2 * 1024 * 1024; // 2MB
3435
typedef struct tbb_mem_pool_policy_t {
3536
raw_alloc_tbb_type pAlloc;
3637
raw_free_tbb_type pFree;
@@ -39,6 +40,11 @@ typedef struct tbb_mem_pool_policy_t {
3940
unsigned fixed_pool : 1, keep_all_memory : 1, reserved : 30;
4041
} tbb_mem_pool_policy_t;
4142

43+
typedef struct umf_scalable_pool_params_t {
44+
size_t granularity;
45+
bool keep_all_memory;
46+
} umf_scalable_pool_params_t;
47+
4248
typedef struct tbb_callbacks_t {
4349
void *(*pool_malloc)(void *, size_t);
4450
void *(*pool_realloc)(void *, void *, size_t);
@@ -167,19 +173,88 @@ static void tbb_raw_free_wrapper(intptr_t pool_id, void *ptr, size_t bytes) {
167173
}
168174
}
169175

176+
umf_result_t
177+
umfScalablePoolParamsCreate(umf_scalable_pool_params_handle_t *params) {
178+
if (!params) {
179+
LOG_ERR("scalable pool params handle is NULL");
180+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
181+
}
182+
183+
umf_scalable_pool_params_t *params_data =
184+
umf_ba_global_alloc(sizeof(umf_scalable_pool_params_t));
185+
if (!params_data) {
186+
LOG_ERR("cannot allocate memory for scalable poolparams");
187+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
188+
}
189+
190+
params_data->granularity = DEFAULT_GRANULARITY;
191+
params_data->keep_all_memory = false;
192+
193+
*params = (umf_scalable_pool_params_handle_t)params_data;
194+
195+
return UMF_RESULT_SUCCESS;
196+
}
197+
198+
umf_result_t
199+
umfScalablePoolParamsDestroy(umf_scalable_pool_params_handle_t params) {
200+
if (!params) {
201+
LOG_ERR("params is NULL");
202+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
203+
}
204+
205+
umf_ba_global_free(params);
206+
207+
return UMF_RESULT_SUCCESS;
208+
}
209+
210+
umf_result_t
211+
umfScalablePoolParamsSetGranularity(umf_scalable_pool_params_handle_t params,
212+
size_t granularity) {
213+
if (!params) {
214+
LOG_ERR("params is NULL");
215+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
216+
}
217+
218+
if (granularity == 0) {
219+
LOG_ERR("granularity cannot be 0");
220+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
221+
}
222+
223+
params->granularity = granularity;
224+
225+
return UMF_RESULT_SUCCESS;
226+
}
227+
228+
umf_result_t
229+
umfScalablePoolParamsSetKeepAllMemory(umf_scalable_pool_params_handle_t params,
230+
bool keep_all_memory) {
231+
if (!params) {
232+
LOG_ERR("params is NULL");
233+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
234+
}
235+
236+
params->keep_all_memory = keep_all_memory;
237+
238+
return UMF_RESULT_SUCCESS;
239+
}
240+
170241
static umf_result_t tbb_pool_initialize(umf_memory_provider_handle_t provider,
171242
void *params, void **pool) {
172-
(void)params; // unused
173-
174-
const size_t GRANULARITY = 2 * 1024 * 1024;
175243
tbb_mem_pool_policy_t policy = {.pAlloc = tbb_raw_alloc_wrapper,
176244
.pFree = tbb_raw_free_wrapper,
177-
.granularity = GRANULARITY,
245+
.granularity = DEFAULT_GRANULARITY,
178246
.version = 1,
179247
.fixed_pool = false,
180248
.keep_all_memory = false,
181249
.reserved = 0};
182250

251+
if (params) {
252+
umf_scalable_pool_params_handle_t scalable_params =
253+
(umf_scalable_pool_params_handle_t)params;
254+
policy.granularity = scalable_params->granularity;
255+
policy.keep_all_memory = scalable_params->keep_all_memory;
256+
}
257+
183258
tbb_memory_pool_t *pool_data =
184259
umf_ba_global_alloc(sizeof(tbb_memory_pool_t));
185260
if (!pool_data) {

0 commit comments

Comments
 (0)