Skip to content

Commit c2e4dae

Browse files
committed
rebase
1 parent 940b2d4 commit c2e4dae

File tree

3 files changed

+207
-33
lines changed

3 files changed

+207
-33
lines changed

src/pool/pool_disjoint.c

Lines changed: 172 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ void slab_unreg(slab_t *slab) {
231231
slab_unreg_by_addr(end_addr, slab);
232232
}
233233

234-
bucket_t *create_bucket(size_t sz, disjoint_pool_t *pool,
235-
umf_disjoint_pool_shared_limits_t *shared_limits) {
234+
bucket_t *
235+
create_bucket(size_t sz, disjoint_pool_t *pool,
236+
umf_disjoint_pool_shared_limits_handle_t shared_limits) {
236237

237238
bucket_t *bucket = (bucket_t *)umf_ba_global_alloc(sizeof(bucket_t));
238239
if (bucket == NULL) {
@@ -344,7 +345,7 @@ size_t bucket_slab_alloc_size(bucket_t *bucket) {
344345
}
345346

346347
size_t bucket_slab_min_size(bucket_t *bucket) {
347-
return bucket->pool->params.SlabMinSize;
348+
return bucket->pool->params.slab_min_size;
348349
}
349350

350351
slab_list_item_t *bucket_get_avail_full_slab(bucket_t *bucket,
@@ -439,12 +440,12 @@ size_t bucket_capacity(bucket_t *bucket) {
439440
if (bucket->size <= bucket_chunk_cut_off(bucket)) {
440441
return 1;
441442
} else {
442-
return bucket->pool->params.Capacity;
443+
return bucket->pool->params.capacity;
443444
}
444445
}
445446

446447
void bucket_update_stats(bucket_t *bucket, int in_use, int in_pool) {
447-
if (bucket->pool->params.PoolTrace == 0) {
448+
if (bucket->pool->params.pool_trace == 0) {
448449
return;
449450
}
450451

@@ -458,7 +459,7 @@ void bucket_update_stats(bucket_t *bucket, int in_use, int in_pool) {
458459

459460
// Increment or decrement current pool sizes based on whether
460461
// slab was added to or removed from pool.
461-
bucket->pool->params.CurPoolSize +=
462+
bucket->pool->params.cur_pool_size +=
462463
in_pool * bucket_slab_alloc_size(bucket);
463464
}
464465

@@ -595,8 +596,8 @@ static size_t size_to_idx(disjoint_pool_t *pool, size_t size) {
595596

596597
umf_disjoint_pool_shared_limits_t *
597598
disjoint_pool_get_limits(disjoint_pool_t *pool) {
598-
if (pool->params.SharedLimits) {
599-
return pool->params.SharedLimits;
599+
if (pool->params.shared_limits) {
600+
return pool->params.shared_limits;
600601
} else {
601602
return pool->default_shared_limits;
602603
}
@@ -675,7 +676,7 @@ void *disjoint_pool_allocate(disjoint_pool_t *pool, size_t size,
675676

676677
void *ptr = NULL;
677678

678-
if (size > pool->params.MaxPoolableSize) {
679+
if (size > pool->params.max_poolable_size) {
679680
umf_result_t ret =
680681
umfMemoryProviderAlloc(pool->provider, size, 0, &ptr);
681682
if (ret != UMF_RESULT_SUCCESS) {
@@ -702,7 +703,7 @@ void *disjoint_pool_allocate(disjoint_pool_t *pool, size_t size,
702703
return NULL;
703704
}
704705

705-
if (pool->params.PoolTrace > 1) {
706+
if (pool->params.pool_trace > 1) {
706707
bucket_count_alloc(bucket, from_pool);
707708
}
708709

@@ -757,10 +758,10 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
757758
umf_disjoint_pool_params_t *dp_params =
758759
(umf_disjoint_pool_params_t *)params;
759760

760-
// MinBucketSize parameter must be a power of 2 for bucket sizes
761+
// min_bucket_size parameter must be a power of 2 for bucket sizes
761762
// to generate correctly.
762-
if (!dp_params->MinBucketSize ||
763-
!((dp_params->MinBucketSize & (dp_params->MinBucketSize - 1)) == 0)) {
763+
if (!dp_params->min_bucket_size ||
764+
!((dp_params->min_bucket_size & (dp_params->min_bucket_size - 1)) == 0)) {
764765
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
765766
}
766767

@@ -774,15 +775,15 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
774775

775776
// Generate buckets sized such as: 64, 96, 128, 192, ..., CutOff.
776777
// Powers of 2 and the value halfway between the powers of 2.
777-
size_t Size1 = disjoint_pool->params.MinBucketSize;
778+
size_t Size1 = disjoint_pool->params.min_bucket_size;
778779

779-
// MinBucketSize cannot be larger than CutOff.
780+
// min_bucket_size cannot be larger than CutOff.
780781
Size1 = utils_min(Size1, CutOff);
781782

782783
// Buckets sized smaller than the bucket default size- 8 aren't needed.
783784
Size1 = utils_max(Size1, UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE);
784785

785-
// Calculate the exponent for MinBucketSize used for finding buckets.
786+
// Calculate the exponent for min_bucket_size used for finding buckets.
786787
disjoint_pool->min_bucket_size_exp = (size_t)log2Utils(Size1);
787788
disjoint_pool->default_shared_limits =
788789
umfDisjointPoolSharedLimitsCreate(SIZE_MAX);
@@ -829,8 +830,8 @@ void *disjoint_pool_malloc(void *pool, size_t size) {
829830
bool from_pool;
830831
void *ptr = disjoint_pool_allocate(hPool, size, &from_pool);
831832

832-
if (hPool->params.PoolTrace > 2) {
833-
const char *MT = hPool->params.Name;
833+
if (hPool->params.pool_trace > 2) {
834+
const char *MT = hPool->params.name;
834835
(void)MT;
835836
//std::cout << "Allocated " << std::setw(8) << size << " " << MT
836837
// << " bytes from " << (FromPool ? "Pool" : "Provider") << " ->"
@@ -890,7 +891,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
890891
// Check if requested allocation size is within pooling limit.
891892
// If not, just request aligned pointer from the system.
892893
from_pool = false;
893-
if (aligned_size > disjoint_pool->params.MaxPoolableSize) {
894+
if (aligned_size > disjoint_pool->params.max_poolable_size) {
894895

895896
umf_result_t ret = umfMemoryProviderAlloc(disjoint_pool->provider, size,
896897
alignment, &ptr);
@@ -913,7 +914,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
913914
}
914915

915916
assert(ptr);
916-
if (disjoint_pool->params.PoolTrace > 1) {
917+
if (disjoint_pool->params.pool_trace > 1) {
917918
bucket_count_alloc(bucket, from_pool);
918919
}
919920

@@ -922,8 +923,8 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
922923
annotate_memory_undefined((void *)ALIGN_UP((size_t)ptr, alignment), size);
923924
return (void *)ALIGN_UP((size_t)ptr, alignment);
924925

925-
if (disjoint_pool->params.PoolTrace > 2) {
926-
const char *MT = disjoint_pool->params.Name;
926+
if (disjoint_pool->params.pool_trace > 2) {
927+
const char *MT = disjoint_pool->params.name;
927928
(void)MT;
928929
//std::cout << "Allocated " << std::setw(8) << size << " " << MT
929930
// << " bytes aligned at " << alignment << " from "
@@ -949,7 +950,7 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
949950
}
950951

951952
void *slab_ptr =
952-
(void *)ALIGN_DOWN((size_t)ptr, disjoint_pool->params.SlabMinSize);
953+
(void *)ALIGN_DOWN((size_t)ptr, disjoint_pool->params.slab_min_size);
953954

954955
// Lock the map on read
955956
utils_mutex_lock(&disjoint_pool->known_slabs_map_lock);
@@ -975,7 +976,7 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
975976
utils_mutex_unlock(&disjoint_pool->known_slabs_map_lock);
976977
bucket_t *bucket = slab->bucket;
977978

978-
if (disjoint_pool->params.PoolTrace > 1) {
979+
if (disjoint_pool->params.pool_trace > 1) {
979980
bucket->free_count++;
980981
}
981982

@@ -994,14 +995,14 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
994995
/*
995996
if (ret == UMF_RESULT_SUCCESS) {
996997
997-
if (impl->getParams().PoolTrace > 2) {
998+
if (impl->getParams().pool_trace > 2) {
998999
auto MT = impl->getParams().Name;
9991000
std::cout << "Freed " << MT << " " << ptr << " to "
10001001
<< (ToPool ? "Pool" : "Provider")
10011002
<< ", Current total pool size "
10021003
<< impl->getLimits()->TotalSize.load()
10031004
<< ", Current pool size for " << MT << " "
1004-
<< impl->getParams().CurPoolSize << "\n";
1005+
<< impl->getParams().cur_pool_size << "\n";
10051006
}
10061007
}*/
10071008

@@ -1037,7 +1038,7 @@ void disjoint_pool_finalize(void *pool) {
10371038
umf_ba_global_free(hPool);
10381039

10391040
/*
1040-
if (impl->getParams().PoolTrace > 1) {
1041+
if (impl->getParams().pool_trace > 1) {
10411042
bool TitlePrinted = false;
10421043
size_t HighBucketSize;
10431044
size_t HighPeakSlabsInUse;
@@ -1089,3 +1090,147 @@ void umfDisjointPoolSharedLimitsDestroy(
10891090
umf_disjoint_pool_shared_limits_t *limits) {
10901091
umf_ba_global_free(limits);
10911092
}
1093+
1094+
umf_result_t
1095+
umfDisjointPoolParamsCreate(umf_disjoint_pool_params_handle_t *hParams) {
1096+
static const char *DEFAULT_NAME = "disjoint_pool";
1097+
1098+
if (!hParams) {
1099+
LOG_ERR("disjoint pool params handle is NULL");
1100+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1101+
}
1102+
1103+
umf_disjoint_pool_params_handle_t params =
1104+
umf_ba_global_alloc(sizeof(umf_disjoint_pool_params_t));
1105+
if (params == NULL) {
1106+
LOG_ERR("cannot allocate memory for disjoint pool params");
1107+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
1108+
}
1109+
1110+
params->slab_min_size = 0;
1111+
params->max_poolable_size = 0;
1112+
params->capacity = 0;
1113+
params->min_bucket_size = UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE;
1114+
params->cur_pool_size = 0;
1115+
params->pool_trace = 0;
1116+
params->shared_limits = NULL;
1117+
params->name = NULL;
1118+
1119+
umf_result_t ret = umfDisjointPoolParamsSetName(params, DEFAULT_NAME);
1120+
if (ret != UMF_RESULT_SUCCESS) {
1121+
umf_ba_global_free(params);
1122+
return ret;
1123+
}
1124+
1125+
*hParams = params;
1126+
1127+
return UMF_RESULT_SUCCESS;
1128+
}
1129+
1130+
umf_result_t
1131+
umfDisjointPoolParamsDestroy(umf_disjoint_pool_params_handle_t hParams) {
1132+
if (hParams) {
1133+
umf_ba_global_free(hParams->name);
1134+
umf_ba_global_free(hParams);
1135+
}
1136+
1137+
return UMF_RESULT_SUCCESS;
1138+
}
1139+
1140+
umf_result_t
1141+
umfDisjointPoolParamsSetSlabMinSize(umf_disjoint_pool_params_handle_t hParams,
1142+
size_t slabMinSize) {
1143+
if (!hParams) {
1144+
LOG_ERR("disjoint pool params handle is NULL");
1145+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1146+
}
1147+
1148+
hParams->slab_min_size = slabMinSize;
1149+
return UMF_RESULT_SUCCESS;
1150+
}
1151+
1152+
umf_result_t umfDisjointPoolParamsSetMaxPoolableSize(
1153+
umf_disjoint_pool_params_handle_t hParams, size_t maxPoolableSize) {
1154+
if (!hParams) {
1155+
LOG_ERR("disjoint pool params handle is NULL");
1156+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1157+
}
1158+
1159+
hParams->max_poolable_size = maxPoolableSize;
1160+
return UMF_RESULT_SUCCESS;
1161+
}
1162+
1163+
umf_result_t
1164+
umfDisjointPoolParamsSetCapacity(umf_disjoint_pool_params_handle_t hParams,
1165+
size_t maxCapacity) {
1166+
if (!hParams) {
1167+
LOG_ERR("disjoint pool params handle is NULL");
1168+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1169+
}
1170+
1171+
hParams->capacity = maxCapacity;
1172+
return UMF_RESULT_SUCCESS;
1173+
}
1174+
1175+
umf_result_t
1176+
umfDisjointPoolParamsSetMinBucketSize(umf_disjoint_pool_params_handle_t hParams,
1177+
size_t minBucketSize) {
1178+
if (!hParams) {
1179+
LOG_ERR("disjoint pool params handle is NULL");
1180+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1181+
}
1182+
1183+
// minBucketSize parameter must be a power of 2 and greater than 0.
1184+
if (minBucketSize == 0 || (minBucketSize & (minBucketSize - 1))) {
1185+
LOG_ERR("minBucketSize must be a power of 2 and greater than 0");
1186+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1187+
}
1188+
1189+
hParams->min_bucket_size = minBucketSize;
1190+
return UMF_RESULT_SUCCESS;
1191+
}
1192+
1193+
umf_result_t
1194+
umfDisjointPoolParamsSetTrace(umf_disjoint_pool_params_handle_t hParams,
1195+
int poolTrace) {
1196+
if (!hParams) {
1197+
LOG_ERR("disjoint pool params handle is NULL");
1198+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1199+
}
1200+
1201+
hParams->pool_trace = poolTrace;
1202+
return UMF_RESULT_SUCCESS;
1203+
}
1204+
1205+
umf_result_t umfDisjointPoolParamsSetSharedLimits(
1206+
umf_disjoint_pool_params_handle_t hParams,
1207+
umf_disjoint_pool_shared_limits_handle_t hSharedLimits) {
1208+
if (!hParams) {
1209+
LOG_ERR("disjoint pool params handle is NULL");
1210+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1211+
}
1212+
1213+
hParams->shared_limits = hSharedLimits;
1214+
return UMF_RESULT_SUCCESS;
1215+
}
1216+
1217+
umf_result_t
1218+
umfDisjointPoolParamsSetName(umf_disjoint_pool_params_handle_t hParams,
1219+
const char *name) {
1220+
if (!hParams) {
1221+
LOG_ERR("disjoint pool params handle is NULL");
1222+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1223+
}
1224+
1225+
char *newName = umf_ba_global_alloc(sizeof(char) * (strlen(name) + 1));
1226+
if (newName == NULL) {
1227+
LOG_ERR("cannot allocate memory for disjoint pool name");
1228+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
1229+
}
1230+
1231+
umf_ba_global_free(hParams->name);
1232+
hParams->name = newName;
1233+
strcpy(hParams->name, name);
1234+
1235+
return UMF_RESULT_SUCCESS;
1236+
}

src/pool/pool_disjoint_internal.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2024 Intel Corporation
2+
* Copyright (C) 2024 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -51,7 +51,7 @@ typedef struct bucket_t {
5151
// routines, slab map and etc.
5252
disjoint_pool_t *pool;
5353

54-
umf_disjoint_pool_shared_limits_t *shared_limits;
54+
umf_disjoint_pool_shared_limits_handle_t shared_limits;
5555

5656
// For buckets used in chunked mode, a counter of slabs in the pool.
5757
// For allocations that use an entire slab each, the entries in the Available
@@ -121,6 +121,35 @@ typedef struct umf_disjoint_pool_shared_limits_t {
121121
size_t total_size; // requires atomic access
122122
} umf_disjoint_pool_shared_limits_t;
123123

124+
typedef struct umf_disjoint_pool_params_t {
125+
// Minimum allocation size that will be requested from the memory provider.
126+
size_t slab_min_size;
127+
128+
// Allocations up to this limit will be subject to chunking/pooling
129+
size_t max_poolable_size;
130+
131+
// When pooling, each bucket will hold a max of 'capacity' unfreed slabs
132+
size_t capacity;
133+
134+
// Holds the minimum bucket size valid for allocation of a memory type.
135+
// This value must be a power of 2.
136+
size_t min_bucket_size;
137+
138+
// Holds size of the pool managed by the allocator.
139+
size_t cur_pool_size;
140+
141+
// Whether to print pool usage statistics
142+
int pool_trace;
143+
144+
// Memory limits that can be shared between multiple pool instances,
145+
// i.e. if multiple pools use the same shared_limits sum of those pools'
146+
// sizes cannot exceed max_size.
147+
umf_disjoint_pool_shared_limits_handle_t shared_limits;
148+
149+
// Name used in traces
150+
char *name;
151+
} umf_disjoint_pool_params_t;
152+
124153
typedef struct disjoint_pool_t {
125154
// It's important for the map to be destroyed last after buckets and their
126155
// slabs This is because slab's destructor removes the object from the map.
@@ -139,7 +168,7 @@ typedef struct disjoint_pool_t {
139168
// Configuration for this instance
140169
umf_disjoint_pool_params_t params;
141170

142-
umf_disjoint_pool_shared_limits_t *default_shared_limits;
171+
umf_disjoint_pool_shared_limits_handle_t default_shared_limits;
143172

144173
// Used in algorithm for finding buckets
145174
size_t min_bucket_size_exp;

0 commit comments

Comments
 (0)