Skip to content

Commit 6619d63

Browse files
authored
[UR][L0] Add pool statistics (#19360)
Support pool query via urUSMPoolGetInfoExp for information about reserved or in-use memory and their peak sizes. Fixes #17772.
1 parent def8cb4 commit 6619d63

File tree

9 files changed

+297
-97
lines changed

9 files changed

+297
-97
lines changed

sycl/test-e2e/AsyncAlloc/device/memory_pool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %{build} -o %t.out
22
// RUN: %{run} %t.out
33

4-
// XFAIL: level_zero
5-
// XFAIL-TRACKER: https://github.com/intel/llvm/issues/17772
4+
// UNSUPPORTED: level_zero_v2_adapter
5+
// UNSUPPORTED-INTENDED: v2 adapter does not support pool statistics.
66

77
#include <iostream>
88
#include <sycl/detail/core.hpp>

unified-runtime/source/adapters/level_zero/async_alloc.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ static ur_result_t enqueueUSMAllocHelper(
2828
std::scoped_lock<ur_shared_mutex> lock(Queue->Mutex);
2929

3030
// Allocate USM memory
31-
ur_usm_pool_handle_t USMPool = nullptr;
31+
ur_usm_pool_handle_t UrPool = nullptr;
3232
if (Pool) {
33-
USMPool = Pool;
33+
UrPool = Pool;
3434
} else {
35-
USMPool = &Queue->Context->AsyncPool;
35+
UrPool = &Queue->Context->AsyncPool;
3636
}
3737

3838
auto Device = (Type == UR_USM_TYPE_HOST) ? nullptr : Queue->Device;
3939

4040
std::vector<ur_event_handle_t> ExtEventWaitList;
4141
ur_event_handle_t OriginAllocEvent = nullptr;
4242
auto AsyncAlloc =
43-
USMPool->allocateEnqueued(Queue, Device, nullptr, Type, Size);
43+
UrPool->allocateEnqueued(Queue, Device, nullptr, Type, Size);
4444
if (!AsyncAlloc) {
4545
auto Ret =
46-
USMPool->allocate(Queue->Context, Device, nullptr, Type, Size, RetMem);
46+
UrPool->allocate(Queue->Context, Device, nullptr, Type, Size, RetMem);
4747
if (Ret) {
4848
return Ret;
4949
}
@@ -235,26 +235,26 @@ ur_result_t urEnqueueUSMFreeExp(
235235
(ZeCommandList, WaitList.Length, WaitList.ZeEventList));
236236
}
237237

238-
umf_memory_pool_handle_t hPool = nullptr;
239-
auto umfRet = umfPoolByPtr(Mem, &hPool);
240-
if (umfRet != UMF_RESULT_SUCCESS || !hPool) {
238+
umf_memory_pool_handle_t UmfPool = nullptr;
239+
auto UmfRet = umfPoolByPtr(Mem, &UmfPool);
240+
if (UmfRet != UMF_RESULT_SUCCESS || !UmfPool) {
241241
return USMFreeHelper(Queue->Context, Mem);
242242
}
243243

244-
UsmPool *usmPool = nullptr;
245-
umfRet = umfPoolGetTag(hPool, (void **)&usmPool);
246-
if (umfRet != UMF_RESULT_SUCCESS || usmPool == nullptr) {
244+
UsmPool *UsmPool = nullptr;
245+
UmfRet = umfPoolGetTag(UmfPool, (void **)&UsmPool);
246+
if (UmfRet != UMF_RESULT_SUCCESS || UsmPool == nullptr) {
247247
return USMFreeHelper(Queue->Context, Mem);
248248
}
249249

250-
size_t size = 0;
251-
umfRet = umfPoolMallocUsableSize(hPool, Mem, &size);
252-
if (umfRet != UMF_RESULT_SUCCESS) {
250+
size_t Size = 0;
251+
UmfRet = umfPoolMallocUsableSize(UmfPool, Mem, &Size);
252+
if (UmfRet != UMF_RESULT_SUCCESS) {
253253
return USMFreeHelper(Queue->Context, Mem);
254254
}
255255

256256
(*Event)->RefCount.retain();
257-
usmPool->AsyncPool.insert(Mem, size, *Event, Queue);
257+
UsmPool->AsyncPool.insert(Mem, Size, *Event, Queue);
258258

259259
// Signal that USM free event was finished
260260
ZE2UR_CALL(zeCommandListAppendSignalEvent, (ZeCommandList, ZeEvent));

unified-runtime/source/adapters/level_zero/enqueued_pool.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "enqueued_pool.hpp"
12+
#include "usm.hpp"
1213

1314
#include <ur_api.h>
1415

@@ -58,17 +59,13 @@ bool EnqueuedPool::cleanup() {
5859
auto Lock = std::lock_guard(Mutex);
5960
auto FreedAllocations = !Freelist.empty();
6061

61-
auto umfRet [[maybe_unused]] = UMF_RESULT_SUCCESS;
62+
auto Ret [[maybe_unused]] = UR_RESULT_SUCCESS;
6263
for (auto It : Freelist) {
63-
umf_memory_pool_handle_t hPool = nullptr;
64-
umfRet = umfPoolByPtr(It.Ptr, &hPool);
65-
assert(hPool != nullptr);
66-
67-
umfRet = umfPoolFree(hPool, It.Ptr);
68-
assert(umfRet == UMF_RESULT_SUCCESS);
64+
Ret = MemFreeFn(It.Ptr);
65+
assert(Ret == UR_RESULT_SUCCESS);
6966

7067
if (It.Event)
71-
eventRelease(It.Event);
68+
EventReleaseFn(It.Event);
7269
}
7370
Freelist.clear();
7471

@@ -84,17 +81,13 @@ bool EnqueuedPool::cleanupForQueue(void *Queue) {
8481

8582
bool FreedAllocations = false;
8683

87-
auto umfRet [[maybe_unused]] = UMF_RESULT_SUCCESS;
84+
auto Ret [[maybe_unused]] = UR_RESULT_SUCCESS;
8885
while (It != Freelist.end() && It->Queue == Queue) {
89-
umf_memory_pool_handle_t hPool = nullptr;
90-
umfRet = umfPoolByPtr(It->Ptr, &hPool);
91-
assert(hPool != nullptr);
92-
93-
umfRet = umfPoolFree(hPool, It->Ptr);
94-
assert(umfRet == UMF_RESULT_SUCCESS);
86+
Ret = MemFreeFn(It->Ptr);
87+
assert(Ret == UR_RESULT_SUCCESS);
9588

9689
if (It->Event)
97-
eventRelease(It->Event);
90+
EventReleaseFn(It->Event);
9891

9992
// Erase the current allocation and move to the next one
10093
It = Freelist.erase(It);

unified-runtime/source/adapters/level_zero/enqueued_pool.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ class EnqueuedPool {
3030
};
3131

3232
using event_release_callback_t = ur_result_t (*)(ur_event_handle_t);
33+
using memory_free_callback_t = std::function<ur_result_t(void *)>;
3334

34-
EnqueuedPool(event_release_callback_t eventRelease)
35-
: eventRelease(eventRelease) {}
35+
EnqueuedPool(event_release_callback_t EventReleaseFn,
36+
memory_free_callback_t MemFreeFn)
37+
: EventReleaseFn(EventReleaseFn), MemFreeFn(MemFreeFn) {}
3638

3739
~EnqueuedPool();
3840
std::optional<Allocation> getBestFit(size_t Size, size_t Alignment,
@@ -60,5 +62,6 @@ class EnqueuedPool {
6062
using AllocationSet = std::set<Allocation, Comparator>;
6163
ur_mutex Mutex;
6264
AllocationSet Freelist;
63-
event_release_callback_t eventRelease;
65+
event_release_callback_t EventReleaseFn;
66+
memory_free_callback_t MemFreeFn;
6467
};

0 commit comments

Comments
 (0)