Skip to content

Commit b7b9d0a

Browse files
committed
Refactor memory tracker implementation
1 parent 5a515c5 commit b7b9d0a

File tree

2 files changed

+66
-61
lines changed

2 files changed

+66
-61
lines changed

src/provider/provider_tracking.c

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@
2828

2929
uint64_t IPC_HANDLE_ID = 0;
3030

31-
typedef struct tracker_value_t {
31+
struct umf_memory_tracker_t {
32+
umf_ba_pool_t *alloc_info_allocator;
33+
critnib *alloc_segments_map;
34+
utils_mutex_t splitMergeMutex;
35+
};
36+
37+
typedef struct tracker_alloc_info_t {
3238
umf_memory_pool_handle_t pool;
3339
size_t size;
34-
} tracker_value_t;
40+
} tracker_alloc_info_t;
3541

3642
static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
3743
umf_memory_pool_handle_t pool,
3844
const void *ptr, size_t size) {
3945
assert(ptr);
4046

41-
tracker_value_t *value = umf_ba_alloc(hTracker->tracker_allocator);
47+
tracker_alloc_info_t *value = umf_ba_alloc(hTracker->alloc_info_allocator);
4248
if (value == NULL) {
4349
LOG_ERR("failed to allocate tracker value, ptr=%p, size=%zu", ptr,
4450
size);
@@ -48,7 +54,8 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
4854
value->pool = pool;
4955
value->size = size;
5056

51-
int ret = critnib_insert(hTracker->map, (uintptr_t)ptr, value, 0);
57+
int ret =
58+
critnib_insert(hTracker->alloc_segments_map, (uintptr_t)ptr, value, 0);
5259

5360
if (ret == 0) {
5461
LOG_DEBUG(
@@ -60,7 +67,7 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
6067
LOG_ERR("failed to insert tracker value, ret=%d, ptr=%p, pool=%p, size=%zu",
6168
ret, ptr, (void *)pool, size);
6269

63-
umf_ba_free(hTracker->tracker_allocator, value);
70+
umf_ba_free(hTracker->alloc_info_allocator, value);
6471

6572
if (ret == ENOMEM) {
6673
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
@@ -78,18 +85,18 @@ static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
7885
// Every umfMemoryTrackerAdd(..., ptr, ...) should have a corresponding
7986
// umfMemoryTrackerRemove call with the same ptr value.
8087

81-
void *value = critnib_remove(hTracker->map, (uintptr_t)ptr);
88+
void *value = critnib_remove(hTracker->alloc_segments_map, (uintptr_t)ptr);
8289
if (!value) {
83-
LOG_ERR("pointer %p not found in the map", ptr);
90+
LOG_ERR("pointer %p not found in the alloc_segments_map", ptr);
8491
return UMF_RESULT_ERROR_UNKNOWN;
8592
}
8693

87-
tracker_value_t *v = value;
94+
tracker_alloc_info_t *v = value;
8895

8996
LOG_DEBUG("memory region removed: tracker=%p, ptr=%p, size=%zu",
9097
(void *)hTracker, ptr, v->size);
9198

92-
umf_ba_free(hTracker->tracker_allocator, value);
99+
umf_ba_free(hTracker->alloc_info_allocator, value);
93100

94101
return UMF_RESULT_SUCCESS;
95102
}
@@ -117,15 +124,15 @@ umf_result_t umfMemoryTrackerGetAllocInfo(const void *ptr,
117124
return UMF_RESULT_ERROR_NOT_SUPPORTED;
118125
}
119126

120-
if (TRACKER->map == NULL) {
121-
LOG_ERR("tracker's map does not exist");
127+
if (TRACKER->alloc_segments_map == NULL) {
128+
LOG_ERR("tracker's alloc_segments_map does not exist");
122129
return UMF_RESULT_ERROR_NOT_SUPPORTED;
123130
}
124131

125132
uintptr_t rkey;
126-
tracker_value_t *rvalue;
127-
int found = critnib_find(TRACKER->map, (uintptr_t)ptr, FIND_LE,
128-
(void *)&rkey, (void **)&rvalue);
133+
tracker_alloc_info_t *rvalue;
134+
int found = critnib_find(TRACKER->alloc_segments_map, (uintptr_t)ptr,
135+
FIND_LE, (void *)&rkey, (void **)&rvalue);
129136
if (!found || (uintptr_t)ptr >= rkey + rvalue->size) {
130137
LOG_DEBUG("pointer %p not found in the tracker, TRACKER=%p", ptr,
131138
(void *)TRACKER);
@@ -188,8 +195,8 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
188195
umf_tracking_memory_provider_t *provider =
189196
(umf_tracking_memory_provider_t *)hProvider;
190197

191-
tracker_value_t *splitValue =
192-
umf_ba_alloc(provider->hTracker->tracker_allocator);
198+
tracker_alloc_info_t *splitValue =
199+
umf_ba_alloc(provider->hTracker->alloc_info_allocator);
193200
if (!splitValue) {
194201
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
195202
}
@@ -202,8 +209,8 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
202209
goto err_lock;
203210
}
204211

205-
tracker_value_t *value =
206-
(tracker_value_t *)critnib_get(provider->hTracker->map, (uintptr_t)ptr);
212+
tracker_alloc_info_t *value = (tracker_alloc_info_t *)critnib_get(
213+
provider->hTracker->alloc_segments_map, (uintptr_t)ptr);
207214
if (!value) {
208215
LOG_ERR("region for split is not found in the tracker");
209216
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
@@ -240,22 +247,23 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
240247
goto err;
241248
}
242249

243-
int cret = critnib_insert(provider->hTracker->map, (uintptr_t)ptr,
244-
(void *)splitValue, 1 /* update */);
250+
int cret =
251+
critnib_insert(provider->hTracker->alloc_segments_map, (uintptr_t)ptr,
252+
(void *)splitValue, 1 /* update */);
245253
// this cannot fail since we know the element exists (nothing to allocate)
246254
assert(cret == 0);
247255
(void)cret;
248256

249257
// free the original value
250-
umf_ba_free(provider->hTracker->tracker_allocator, value);
258+
umf_ba_free(provider->hTracker->alloc_info_allocator, value);
251259
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
252260

253261
return UMF_RESULT_SUCCESS;
254262

255263
err:
256264
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
257265
err_lock:
258-
umf_ba_free(provider->hTracker->tracker_allocator, splitValue);
266+
umf_ba_free(provider->hTracker->alloc_info_allocator, splitValue);
259267
return ret;
260268
}
261269

@@ -265,8 +273,8 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
265273
umf_tracking_memory_provider_t *provider =
266274
(umf_tracking_memory_provider_t *)hProvider;
267275

268-
tracker_value_t *mergedValue =
269-
umf_ba_alloc(provider->hTracker->tracker_allocator);
276+
tracker_alloc_info_t *mergedValue =
277+
umf_ba_alloc(provider->hTracker->alloc_info_allocator);
270278

271279
if (!mergedValue) {
272280
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
@@ -280,15 +288,15 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
280288
goto err_lock;
281289
}
282290

283-
tracker_value_t *lowValue = (tracker_value_t *)critnib_get(
284-
provider->hTracker->map, (uintptr_t)lowPtr);
291+
tracker_alloc_info_t *lowValue = (tracker_alloc_info_t *)critnib_get(
292+
provider->hTracker->alloc_segments_map, (uintptr_t)lowPtr);
285293
if (!lowValue) {
286294
LOG_ERR("no left value");
287295
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
288296
goto err;
289297
}
290-
tracker_value_t *highValue = (tracker_value_t *)critnib_get(
291-
provider->hTracker->map, (uintptr_t)highPtr);
298+
tracker_alloc_info_t *highValue = (tracker_alloc_info_t *)critnib_get(
299+
provider->hTracker->alloc_segments_map, (uintptr_t)highPtr);
292300
if (!highValue) {
293301
LOG_ERR("no right value");
294302
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
@@ -314,20 +322,21 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
314322

315323
// We'll have a duplicate entry for the range [highPtr, highValue->size] but this is fine,
316324
// the value is the same anyway and we forbid removing that range concurrently
317-
int cret = critnib_insert(provider->hTracker->map, (uintptr_t)lowPtr,
318-
(void *)mergedValue, 1 /* update */);
325+
int cret =
326+
critnib_insert(provider->hTracker->alloc_segments_map,
327+
(uintptr_t)lowPtr, (void *)mergedValue, 1 /* update */);
319328
// this cannot fail since we know the element exists (nothing to allocate)
320329
assert(cret == 0);
321330
(void)cret;
322331

323332
// free old value that we just replaced with mergedValue
324-
umf_ba_free(provider->hTracker->tracker_allocator, lowValue);
333+
umf_ba_free(provider->hTracker->alloc_info_allocator, lowValue);
325334

326-
void *erasedhighValue =
327-
critnib_remove(provider->hTracker->map, (uintptr_t)highPtr);
335+
void *erasedhighValue = critnib_remove(
336+
provider->hTracker->alloc_segments_map, (uintptr_t)highPtr);
328337
assert(erasedhighValue == highValue);
329338

330-
umf_ba_free(provider->hTracker->tracker_allocator, erasedhighValue);
339+
umf_ba_free(provider->hTracker->alloc_info_allocator, erasedhighValue);
331340

332341
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
333342

@@ -340,7 +349,7 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
340349
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
341350

342351
err_lock:
343-
umf_ba_free(provider->hTracker->tracker_allocator, mergedValue);
352+
umf_ba_free(provider->hTracker->alloc_info_allocator, mergedValue);
344353
return ret;
345354
}
346355

@@ -425,19 +434,20 @@ static void clear_tracker_for_the_pool(umf_memory_tracker_handle_t hTracker,
425434
size_t n_items = 0;
426435
uintptr_t last_key = 0;
427436

428-
while (1 == critnib_find((critnib *)hTracker->map, last_key, FIND_G, &rkey,
429-
&rvalue)) {
430-
tracker_value_t *value = (tracker_value_t *)rvalue;
437+
while (1 == critnib_find((critnib *)hTracker->alloc_segments_map, last_key,
438+
FIND_G, &rkey, &rvalue)) {
439+
tracker_alloc_info_t *value = (tracker_alloc_info_t *)rvalue;
431440
if (value->pool != pool && pool != NULL) {
432441
last_key = rkey;
433442
continue;
434443
}
435444

436445
n_items++;
437446

438-
void *removed_value = critnib_remove(hTracker->map, rkey);
447+
void *removed_value =
448+
critnib_remove(hTracker->alloc_segments_map, rkey);
439449
assert(removed_value == rvalue);
440-
umf_ba_free(hTracker->tracker_allocator, removed_value);
450+
umf_ba_free(hTracker->alloc_info_allocator, removed_value);
441451

442452
last_key = rkey;
443453
}
@@ -816,33 +826,33 @@ umf_memory_tracker_handle_t umfMemoryTrackerCreate(void) {
816826
return NULL;
817827
}
818828

819-
umf_ba_pool_t *tracker_allocator =
820-
umf_ba_create(sizeof(struct tracker_value_t));
821-
if (!tracker_allocator) {
829+
umf_ba_pool_t *alloc_info_allocator =
830+
umf_ba_create(sizeof(struct tracker_alloc_info_t));
831+
if (!alloc_info_allocator) {
822832
goto err_free_handle;
823833
}
824834

825-
handle->tracker_allocator = tracker_allocator;
835+
handle->alloc_info_allocator = alloc_info_allocator;
826836

827837
void *mutex_ptr = utils_mutex_init(&handle->splitMergeMutex);
828838
if (!mutex_ptr) {
829-
goto err_destroy_tracker_allocator;
839+
goto err_destroy_alloc_info_allocator;
830840
}
831841

832-
handle->map = critnib_new();
833-
if (!handle->map) {
842+
handle->alloc_segments_map = critnib_new();
843+
if (!handle->alloc_segments_map) {
834844
goto err_destroy_mutex;
835845
}
836846

837-
LOG_DEBUG("tracker created, handle=%p, segment map=%p", (void *)handle,
838-
(void *)handle->map);
847+
LOG_DEBUG("tracker created, handle=%p, alloc_segments_map=%p",
848+
(void *)handle, (void *)handle->alloc_segments_map);
839849

840850
return handle;
841851

842852
err_destroy_mutex:
843853
utils_mutex_destroy_not_free(&handle->splitMergeMutex);
844-
err_destroy_tracker_allocator:
845-
umf_ba_destroy(tracker_allocator);
854+
err_destroy_alloc_info_allocator:
855+
umf_ba_destroy(alloc_info_allocator);
846856
err_free_handle:
847857
umf_ba_global_free(handle);
848858
return NULL;
@@ -865,10 +875,10 @@ void umfMemoryTrackerDestroy(umf_memory_tracker_handle_t handle) {
865875
// We have to zero all inner pointers,
866876
// because the tracker handle can be copied
867877
// and used in many places.
868-
critnib_delete(handle->map);
869-
handle->map = NULL;
878+
critnib_delete(handle->alloc_segments_map);
879+
handle->alloc_segments_map = NULL;
870880
utils_mutex_destroy_not_free(&handle->splitMergeMutex);
871-
umf_ba_destroy(handle->tracker_allocator);
872-
handle->tracker_allocator = NULL;
881+
umf_ba_destroy(handle->alloc_info_allocator);
882+
handle->alloc_info_allocator = NULL;
873883
umf_ba_global_free(handle);
874884
}

src/provider/provider_tracking.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@
2626
extern "C" {
2727
#endif
2828

29-
struct umf_memory_tracker_t {
30-
umf_ba_pool_t *tracker_allocator;
31-
critnib *map;
32-
utils_mutex_t splitMergeMutex;
33-
};
34-
29+
struct umf_memory_tracker_t;
3530
typedef struct umf_memory_tracker_t *umf_memory_tracker_handle_t;
3631

3732
extern umf_memory_tracker_handle_t TRACKER;

0 commit comments

Comments
 (0)