Skip to content

Commit 74e86eb

Browse files
committed
Add logs to the tracking provider
1 parent 62fb5b1 commit 74e86eb

File tree

8 files changed

+171
-111
lines changed

8 files changed

+171
-111
lines changed

src/base_alloc/base_alloc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ void umf_ba_destroy(umf_ba_pool_t *pool) {
289289
#ifndef NDEBUG
290290
ba_debug_checks(pool);
291291
if (pool->metadata.n_allocs) {
292-
LOG_ERR("umf_ba_destroy(): pool->metadata.n_allocs = %zu",
293-
pool->metadata.n_allocs);
292+
LOG_ERR("pool->metadata.n_allocs = %zu", pool->metadata.n_allocs);
294293
}
295294
#endif /* NDEBUG */
296295

src/base_alloc/base_alloc_linear.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool) {
250250
#ifndef NDEBUG
251251
_DEBUG_EXECUTE(ba_debug_checks(pool));
252252
if (pool->metadata.global_n_allocs) {
253-
LOG_ERR("umf_ba_linear_destroy(): global_n_allocs = %zu",
254-
pool->metadata.global_n_allocs);
253+
LOG_ERR("global_n_allocs = %zu", pool->metadata.global_n_allocs);
255254
}
256255
#endif /* NDEBUG */
257256

src/ipc.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
2525
umf_alloc_info_t allocInfo;
2626
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
2727
if (ret != UMF_RESULT_SUCCESS) {
28-
LOG_ERR("umfGetIPCHandle: cannot get alloc info for ptr = %p.", ptr);
28+
LOG_ERR("cannot get alloc info for ptr = %p.", ptr);
2929
return ret;
3030
}
3131

@@ -37,22 +37,22 @@ umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
3737
size_t providerIPCHandleSize;
3838
ret = umfMemoryProviderGetIPCHandleSize(provider, &providerIPCHandleSize);
3939
if (ret != UMF_RESULT_SUCCESS) {
40-
LOG_ERR("umfGetIPCHandle: cannot get IPC handle size.");
40+
LOG_ERR("cannot get IPC handle size.");
4141
return ret;
4242
}
4343

4444
ipcHandleSize = sizeof(umf_ipc_data_t) + providerIPCHandleSize;
4545
umf_ipc_data_t *ipcData = umf_ba_global_alloc(ipcHandleSize);
4646
if (!ipcData) {
47-
LOG_ERR("umfGetIPCHandle: failed to allocate ipcData");
47+
LOG_ERR("failed to allocate ipcData");
4848
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
4949
}
5050

5151
ret = umfMemoryProviderGetIPCHandle(provider, allocInfo.base,
5252
allocInfo.baseSize,
5353
(void *)ipcData->providerIpcData);
5454
if (ret != UMF_RESULT_SUCCESS) {
55-
LOG_ERR("umfGetIPCHandle: failed to get IPC handle.");
55+
LOG_ERR("failed to get IPC handle.");
5656
umf_ba_global_free(ipcData);
5757
return ret;
5858
}
@@ -95,8 +95,7 @@ umf_result_t umfOpenIPCHandle(umf_memory_pool_handle_t hPool,
9595
umf_result_t ret = umfMemoryProviderOpenIPCHandle(
9696
hProvider, (void *)umfIPCHandle->providerIpcData, &base);
9797
if (ret != UMF_RESULT_SUCCESS) {
98-
LOG_ERR(
99-
"umfOpenIPCHandle: memory provider failed to open the IPC handle.");
98+
LOG_ERR("memory provider failed to open the IPC handle.");
10099
return ret;
101100
}
102101
*ptr = (void *)((uintptr_t)base + umfIPCHandle->offset);
@@ -108,7 +107,7 @@ umf_result_t umfCloseIPCHandle(void *ptr) {
108107
umf_alloc_info_t allocInfo;
109108
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
110109
if (ret != UMF_RESULT_SUCCESS) {
111-
LOG_ERR("umfCloseIPCHandle: cannot get alloc info for ptr = %p.", ptr);
110+
LOG_ERR("cannot get alloc info for ptr = %p.", ptr);
112111
return ret;
113112
}
114113

src/memory_pool.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
5959
}
6060

6161
*hPool = pool;
62+
LOG_INFO("Memory pool created: %p", (void *)pool);
6263
return UMF_RESULT_SUCCESS;
6364

6465
err_pool_init:
@@ -83,6 +84,9 @@ void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
8384
// Destroy tracking provider.
8485
umfMemoryProviderDestroy(hPool->provider);
8586
}
87+
88+
LOG_INFO("Memory pool destroyed: %p", (void *)hPool);
89+
8690
// TODO: this free keeps memory in base allocator, so it can lead to OOM in some scenarios (it should be optimized)
8791
umf_ba_global_free(hPool);
8892
}

src/provider/provider_tracking.c

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
3737

3838
tracker_value_t *value = umf_ba_alloc(hTracker->tracker_allocator);
3939
if (value == NULL) {
40+
LOG_ERR("failed to allocate tracker value, ptr=%p, size=%zu", ptr,
41+
size);
4042
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
4143
}
4244

@@ -46,16 +48,20 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
4648
int ret = critnib_insert(hTracker->map, (uintptr_t)ptr, value, 0);
4749

4850
if (ret == 0) {
51+
LOG_DEBUG("memory region is added, tracker=%p, ptr=%p, size=%zu",
52+
(void *)hTracker, ptr, size);
4953
return UMF_RESULT_SUCCESS;
5054
}
5155

56+
LOG_ERR("failed to insert tracker value, ret=%d, ptr=%p, size=%zu", ret,
57+
ptr, size);
58+
5259
umf_ba_free(hTracker->tracker_allocator, value);
5360

5461
if (ret == ENOMEM) {
5562
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
5663
}
5764

58-
LOG_ERR("umfMemoryTrackerAdd: Unknown Error %d", ret);
5965
return UMF_RESULT_ERROR_UNKNOWN;
6066
}
6167

@@ -70,7 +76,7 @@ static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
7076

7177
void *value = critnib_remove(hTracker->map, (uintptr_t)ptr);
7278
if (!value) {
73-
LOG_ERR("umfMemoryTrackerRemove: pointer %p not found in the map", ptr);
79+
LOG_ERR("pointer %p not found in the map", ptr);
7480
return UMF_RESULT_ERROR_UNKNOWN;
7581
}
7682

@@ -109,6 +115,9 @@ umf_result_t umfMemoryTrackerGetAllocInfo(const void *ptr,
109115
int found = critnib_find(TRACKER->map, (uintptr_t)ptr, FIND_LE,
110116
(void *)&rkey, (void **)&rvalue);
111117
if (!found || (uintptr_t)ptr >= rkey + rvalue->size) {
118+
LOG_WARN("pointer %p not found in the "
119+
"tracker, TRACKER=%p",
120+
ptr, (void *)TRACKER);
112121
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
113122
}
114123

@@ -151,7 +160,9 @@ static umf_result_t trackingAlloc(void *hProvider, size_t size,
151160

152161
umf_result_t ret2 = umfMemoryTrackerAdd(p->hTracker, p->pool, *ptr, size);
153162
if (ret2 != UMF_RESULT_SUCCESS) {
154-
LOG_ERR("umfMemoryTrackerAdd failed: %d", ret2);
163+
LOG_ERR("failed to add allocated region to the tracker, ptr = %p, size "
164+
"= %zu, ret = %d",
165+
*ptr, size, ret2);
155166
}
156167

157168
return ret;
@@ -181,20 +192,21 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
181192
tracker_value_t *value =
182193
(tracker_value_t *)critnib_get(provider->hTracker->map, (uintptr_t)ptr);
183194
if (!value) {
184-
LOG_ERR("tracking split: no such value");
195+
LOG_ERR("region for split is not found in the tracker");
185196
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
186197
goto err;
187198
}
188199
if (value->size != totalSize) {
189-
LOG_ERR("tracking split: %zu != %zu", value->size, totalSize);
200+
LOG_ERR("tracked size %zu does not match requested size to split: %zu",
201+
value->size, totalSize);
190202
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
191203
goto err;
192204
}
193205

194206
ret = umfMemoryProviderAllocationSplit(provider->hUpstream, ptr, totalSize,
195207
firstSize);
196208
if (ret != UMF_RESULT_SUCCESS) {
197-
LOG_ERR("tracking split: umfMemoryProviderAllocationSplit failed");
209+
LOG_ERR("upstream provider failed to split the region");
198210
goto err;
199211
}
200212

@@ -206,7 +218,9 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
206218
ret = umfMemoryTrackerAdd(provider->hTracker, provider->pool, highPtr,
207219
secondSize);
208220
if (ret != UMF_RESULT_SUCCESS) {
209-
LOG_ERR("tracking split: umfMemoryTrackerAdd failed");
221+
LOG_ERR("failed to add split region to the tracker, ptr = %p, size "
222+
"= %zu, ret = %d",
223+
highPtr, secondSize, ret);
210224
// TODO: what now? should we rollback the split? This can only happen due to ENOMEM
211225
// so it's unlikely but probably the best solution would be to try to preallocate everything
212226
// (value and critnib nodes) before calling umfMemoryProviderAllocationSplit.
@@ -256,33 +270,32 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
256270
tracker_value_t *lowValue = (tracker_value_t *)critnib_get(
257271
provider->hTracker->map, (uintptr_t)lowPtr);
258272
if (!lowValue) {
259-
LOG_ERR("tracking merge: no left value");
273+
LOG_ERR("no left value");
260274
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
261275
goto err;
262276
}
263277
tracker_value_t *highValue = (tracker_value_t *)critnib_get(
264278
provider->hTracker->map, (uintptr_t)highPtr);
265279
if (!highValue) {
266-
LOG_ERR("tracking merge: no right value");
280+
LOG_ERR("no right value");
267281
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
268282
goto err;
269283
}
270284
if (lowValue->pool != highValue->pool) {
271-
LOG_ERR("tracking merge: pool mismatch");
285+
LOG_ERR("pool mismatch");
272286
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
273287
goto err;
274288
}
275289
if (lowValue->size + highValue->size != totalSize) {
276-
LOG_ERR("tracking merge: lowValue->size + highValue->size != "
277-
"totalSize");
290+
LOG_ERR("lowValue->size + highValue->size != totalSize");
278291
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
279292
goto err;
280293
}
281294

282295
ret = umfMemoryProviderAllocationMerge(provider->hUpstream, lowPtr, highPtr,
283296
totalSize);
284297
if (ret != UMF_RESULT_SUCCESS) {
285-
LOG_ERR("tracking merge: umfMemoryProviderAllocationMerge failed");
298+
LOG_ERR("upstream provider failed to merge regions");
286299
goto err;
287300
}
288301

@@ -328,7 +341,9 @@ static umf_result_t trackingFree(void *hProvider, void *ptr, size_t size) {
328341
if (ret != UMF_RESULT_SUCCESS) {
329342
// DO NOT return an error here, because the tracking provider
330343
// cannot change behaviour of the upstream provider.
331-
// TODO: LOG
344+
LOG_ERR("failed to remove the region from the tracker, ptr=%p, "
345+
"size=%zu, ret = %d",
346+
ptr, size, ret);
332347
}
333348
}
334349

@@ -338,17 +353,21 @@ static umf_result_t trackingFree(void *hProvider, void *ptr, size_t size) {
338353
ret = umfMemoryProviderPutIPCHandle(p->hUpstream,
339354
cache_value->providerIpcData);
340355
if (ret != UMF_RESULT_SUCCESS) {
341-
LOG_ERR("tracking free: failed to put IPC handle");
356+
LOG_ERR("upstream provider is failed to put IPC handle, ptr=%p, "
357+
"size=%zu, ret = %d",
358+
ptr, size, ret);
342359
}
343360
umf_ba_global_free(value);
344361
}
345362

346363
ret = umfMemoryProviderFree(p->hUpstream, ptr, size);
347364
if (ret != UMF_RESULT_SUCCESS) {
348-
LOG_ERR("tracking free: umfMemoryProviderFree failed");
365+
LOG_ERR("upstream provider is failed to free the memory");
349366
if (umfMemoryTrackerAdd(p->hTracker, p->pool, ptr, size) !=
350367
UMF_RESULT_SUCCESS) {
351-
LOG_ERR("tracking free: umfMemoryTrackerAdd failed");
368+
LOG_ERR(
369+
"cannot add memory back to the tracker, ptr = %p, size = %zu",
370+
ptr, size);
352371
}
353372
return ret;
354373
}
@@ -485,34 +504,30 @@ static umf_result_t trackingGetIpcHandle(void *provider, const void *ptr,
485504
ret = umfMemoryProviderGetIPCHandle(p->hUpstream, ptr, size,
486505
providerIpcData);
487506
if (ret != UMF_RESULT_SUCCESS) {
488-
LOG_ERR("tracking get ipc handle: "
489-
"umfMemoryProviderGetIPCHandle failed");
507+
LOG_ERR("upstream provider is failed to get IPC handle");
490508
return ret;
491509
}
492510

493511
ret = umfMemoryProviderGetIPCHandleSize(p->hUpstream, &ipcDataSize);
494512
if (ret != UMF_RESULT_SUCCESS) {
495-
LOG_ERR("tracking get ipc handle: "
496-
"umfMemoryProviderGetIPCHandleSize failed");
513+
LOG_ERR("upstream provider is failed to get the size of IPC "
514+
"handle");
497515
ret = umfMemoryProviderPutIPCHandle(p->hUpstream,
498516
providerIpcData);
499517
if (ret != UMF_RESULT_SUCCESS) {
500-
LOG_ERR("tracking get ipc handle: "
501-
"umfMemoryProviderPutIPCHandle failed");
518+
LOG_ERR("upstream provider is failed to put IPC handle");
502519
}
503520
return ret;
504521
}
505522

506523
size_t value_size = sizeof(ipc_cache_value_t) + ipcDataSize;
507524
ipc_cache_value_t *cache_value = umf_ba_global_alloc(value_size);
508525
if (!cache_value) {
509-
LOG_ERR(
510-
"tracking get ipc handle: failed to allocate cache_value");
526+
LOG_ERR("failed to allocate cache_value");
511527
ret = umfMemoryProviderPutIPCHandle(p->hUpstream,
512528
providerIpcData);
513529
if (ret != UMF_RESULT_SUCCESS) {
514-
LOG_ERR("tracking get ipc handle: "
515-
"umfMemoryProviderPutIPCHandle failed");
530+
LOG_ERR("upstream provider is failed to put IPC handle");
516531
}
517532
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
518533
}
@@ -538,13 +553,11 @@ static umf_result_t trackingGetIpcHandle(void *provider, const void *ptr,
538553
ret = umfMemoryProviderPutIPCHandle(p->hUpstream,
539554
providerIpcData);
540555
if (ret != UMF_RESULT_SUCCESS) {
541-
LOG_ERR("tracking get ipc handle: "
542-
"umfMemoryProviderPutIPCHandle failed");
556+
LOG_ERR("upstream provider is failed to put IPC handle");
543557
return ret;
544558
}
545559
if (insRes == ENOMEM) {
546-
LOG_ERR(
547-
"tracking get ipc handle: insert to IPC cache failed");
560+
LOG_ERR("insert to IPC cache failed due to OOM");
548561
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
549562
}
550563
}
@@ -585,13 +598,19 @@ static umf_result_t trackingOpenIpcHandle(void *provider, void *providerIpcData,
585598

586599
ret = umfMemoryProviderOpenIPCHandle(p->hUpstream, providerIpcData, ptr);
587600
if (ret != UMF_RESULT_SUCCESS) {
601+
LOG_ERR("upstream provider is failed to open IPC handle");
588602
return ret;
589603
}
590604
size_t bufferSize = getDataSizeFromIpcHandle(providerIpcData);
591605
ret = umfMemoryTrackerAdd(p->hTracker, p->pool, *ptr, bufferSize);
592606
if (ret != UMF_RESULT_SUCCESS) {
607+
LOG_ERR("failed to add IPC region to the tracker, ptr=%p, size=%zu, "
608+
"ret = %d",
609+
*ptr, bufferSize, ret);
593610
if (umfMemoryProviderCloseIPCHandle(p->hUpstream, *ptr, bufferSize)) {
594-
// TODO: LOG
611+
LOG_ERR("upstream provider is failed to close IPC handle, ptr=%p, "
612+
"size=%zu",
613+
*ptr, bufferSize);
595614
}
596615
}
597616
return ret;
@@ -611,7 +630,9 @@ static umf_result_t trackingCloseIpcHandle(void *provider, void *ptr,
611630
if (ret != UMF_RESULT_SUCCESS) {
612631
// DO NOT return an error here, because the tracking provider
613632
// cannot change behaviour of the upstream provider.
614-
LOG_ERR("tracking free: umfMemoryTrackerRemove failed");
633+
LOG_ERR("failed to remove the region from the tracker, ptr=%p, "
634+
"size=%zu, ret = %d",
635+
ptr, size, ret);
615636
}
616637
}
617638
return umfMemoryProviderCloseIPCHandle(p->hUpstream, ptr, size);
@@ -645,14 +666,21 @@ umf_result_t umfTrackingMemoryProviderCreate(
645666
params.hUpstream = hUpstream;
646667
params.hTracker = TRACKER;
647668
if (!params.hTracker) {
669+
LOG_ERR("failed, TRACKER is NULL");
648670
return UMF_RESULT_ERROR_UNKNOWN;
649671
}
650672
params.pool = hPool;
651673
params.ipcCache = critnib_new();
652674
if (!params.ipcCache) {
675+
LOG_ERR("failed to create IPC cache");
653676
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
654677
}
655678

679+
LOG_DEBUG("upstream=%p, tracker=%p, "
680+
"pool=%p, ipcCache=%p",
681+
(void *)params.hUpstream, (void *)params.hTracker,
682+
(void *)params.pool, (void *)params.ipcCache);
683+
656684
return umfMemoryProviderCreate(&UMF_TRACKING_MEMORY_PROVIDER_OPS, &params,
657685
hTrackingProvider);
658686
}
@@ -691,6 +719,9 @@ umf_memory_tracker_handle_t umfMemoryTrackerCreate(void) {
691719
goto err_destroy_mutex;
692720
}
693721

722+
LOG_DEBUG("tracker created, handle=%p, segment map=%p", (void *)handle,
723+
(void *)handle->map);
724+
694725
return handle;
695726

696727
err_destroy_mutex:

0 commit comments

Comments
 (0)