99#include " shared/source/memory_manager/allocations_list.h"
1010#include " shared/source/memory_manager/unified_memory_manager.h"
1111#include " shared/source/os_interface/os_context.h"
12+ #include " shared/test/common/helpers/debug_manager_state_restore.h"
1213#include " shared/test/common/libult/ult_command_stream_receiver.h"
1314#include " shared/test/common/mocks/mock_allocation_properties.h"
1415#include " shared/test/common/mocks/mock_device.h"
@@ -53,6 +54,7 @@ class MyCsr : public UltCommandStreamReceiver<Family> {
5354void CL_CALLBACK emptyDestructorCallback (cl_mem memObj, void *userData) {
5455}
5556
57+ template <bool useMultiGraphicsAllocation = false >
5658class MemObjDestructionTest : public ::testing::TestWithParam<bool > {
5759 public:
5860 void SetUp () override {
@@ -61,12 +63,20 @@ class MemObjDestructionTest : public ::testing::TestWithParam<bool> {
6163 executionEnvironment->memoryManager .reset (memoryManager);
6264 device = std::make_unique<MockClDevice>(MockDevice::create<MockDevice>(executionEnvironment, 0 ));
6365 context.reset (new MockContext (device.get ()));
64-
6566 allocation = memoryManager->allocateGraphicsMemoryWithProperties (MockAllocationProperties{device->getRootDeviceIndex (), size});
66- memObj = new MemObj (context.get (), CL_MEM_OBJECT_BUFFER,
67- ClMemoryPropertiesHelper::createMemoryProperties (CL_MEM_READ_WRITE, 0 , 0 , &device->getDevice ()),
68- CL_MEM_READ_WRITE, 0 , size,
69- nullptr , nullptr , GraphicsAllocationHelper::toMultiGraphicsAllocation (allocation), true , false , false );
67+ if constexpr (useMultiGraphicsAllocation) {
68+ MultiGraphicsAllocation multiAllocation (1u );
69+ multiAllocation.addAllocation (allocation);
70+ memObj = new MemObj (context.get (), CL_MEM_OBJECT_BUFFER,
71+ ClMemoryPropertiesHelper::createMemoryProperties (CL_MEM_READ_WRITE, 0 , 0 , &device->getDevice ()),
72+ CL_MEM_READ_WRITE, 0 , size,
73+ nullptr , nullptr , std::move (multiAllocation), true , false , false );
74+ } else {
75+ memObj = new MemObj (context.get (), CL_MEM_OBJECT_BUFFER,
76+ ClMemoryPropertiesHelper::createMemoryProperties (CL_MEM_READ_WRITE, 0 , 0 , &device->getDevice ()),
77+ CL_MEM_READ_WRITE, 0 , size,
78+ nullptr , nullptr , GraphicsAllocationHelper::toMultiGraphicsAllocation (allocation), true , false , false );
79+ }
7080 csr = device->getDefaultEngine ().commandStreamReceiver ;
7181 *csr->getTagAddress () = 0 ;
7282 contextId = device->getDefaultEngine ().osContext ->getContextId ();
@@ -102,30 +112,40 @@ class MemObjDestructionTest : public ::testing::TestWithParam<bool> {
102112 size_t size = MemoryConstants::pageSize;
103113};
104114
105- class MemObjAsyncDestructionTest : public MemObjDestructionTest {
115+ class MemObjAsyncDestructionTest : public MemObjDestructionTest <> {
116+ public:
117+ void SetUp () override {
118+ DebugManager.flags .EnableAsyncDestroyAllocations .set (true );
119+ MemObjDestructionTest::SetUp ();
120+ }
121+ void TearDown () override {
122+ MemObjDestructionTest::TearDown ();
123+ }
124+ DebugManagerStateRestore restorer;
125+ };
126+
127+ class MemObjMulitAllocationAsyncDestructionTest : public MemObjDestructionTest <true > {
106128 public:
107129 void SetUp () override {
108130 DebugManager.flags .EnableAsyncDestroyAllocations .set (true );
109131 MemObjDestructionTest::SetUp ();
110132 }
111133 void TearDown () override {
112134 MemObjDestructionTest::TearDown ();
113- DebugManager.flags .EnableAsyncDestroyAllocations .set (defaultFlag);
114135 }
115- bool defaultFlag = DebugManager.flags.EnableAsyncDestroyAllocations.get() ;
136+ DebugManagerStateRestore restorer ;
116137};
117138
118- class MemObjSyncDestructionTest : public MemObjDestructionTest {
139+ class MemObjSyncDestructionTest : public MemObjDestructionTest <> {
119140 public:
120141 void SetUp () override {
121142 DebugManager.flags .EnableAsyncDestroyAllocations .set (false );
122143 MemObjDestructionTest::SetUp ();
123144 }
124145 void TearDown () override {
125146 MemObjDestructionTest::TearDown ();
126- DebugManager.flags .EnableAsyncDestroyAllocations .set (defaultFlag);
127147 }
128- bool defaultFlag = DebugManager.flags.EnableAsyncDestroyAllocations.get() ;
148+ DebugManagerStateRestore restorer ;
129149};
130150
131151TEST_P (MemObjAsyncDestructionTest, givenMemObjWithDestructableAllocationWhenAsyncDestructionsAreEnabledAndAllocationIsNotReadyAndMemObjectIsDestructedThenAllocationIsDeferred) {
@@ -150,6 +170,34 @@ TEST_P(MemObjAsyncDestructionTest, givenMemObjWithDestructableAllocationWhenAsyn
150170 }
151171}
152172
173+ HWTEST_F (MemObjMulitAllocationAsyncDestructionTest, givenUsedMemObjWithAsyncDestructionsEnabledThatHasMultiGraphicsAllocationWhenItIsDestroyedThenDestructorWaitsOnTaskCount) {
174+ auto rootDeviceIndex = device->getRootDeviceIndex ();
175+ auto mockCsr0 = new MyCsr<FamilyType>(*device->executionEnvironment , device->getDeviceBitfield ());
176+ auto mockCsr1 = new MyCsr<FamilyType>(*device->executionEnvironment , device->getDeviceBitfield ());
177+ device->resetCommandStreamReceiver (mockCsr0, 0 );
178+ device->resetCommandStreamReceiver (mockCsr1, 1 );
179+ *mockCsr0->getTagAddress () = 0 ;
180+ *mockCsr1->getTagAddress () = 0 ;
181+ mockCsr0->getTagAddressValue = taskCountReady;
182+ mockCsr1->getTagAddressValue = taskCountReady;
183+ auto osContextId0 = mockCsr0->getOsContext ().getContextId ();
184+ auto osContextId1 = mockCsr1->getOsContext ().getContextId ();
185+ memObj->getGraphicsAllocation (rootDeviceIndex)->updateTaskCount (taskCountReady, osContextId0);
186+ memObj->getGraphicsAllocation (rootDeviceIndex)->updateTaskCount (taskCountReady, osContextId1);
187+ auto expectedTaskCount0 = allocation->getTaskCount (osContextId0);
188+ auto expectedTaskCount1 = allocation->getTaskCount (osContextId1);
189+
190+ delete memObj;
191+
192+ EXPECT_EQ (1u , mockCsr0->waitForCompletionWithTimeoutCalled );
193+ EXPECT_EQ (TimeoutControls::maxTimeout, mockCsr0->waitForCompletionWithTimeoutParamsPassed [0 ].timeoutMs );
194+ EXPECT_EQ (expectedTaskCount0, mockCsr0->waitForCompletionWithTimeoutParamsPassed [0 ].taskCountToWait );
195+
196+ EXPECT_EQ (1u , mockCsr1->waitForCompletionWithTimeoutCalled );
197+ EXPECT_EQ (TimeoutControls::maxTimeout, mockCsr1->waitForCompletionWithTimeoutParamsPassed [0 ].timeoutMs );
198+ EXPECT_EQ (expectedTaskCount1, mockCsr1->waitForCompletionWithTimeoutParamsPassed [0 ].taskCountToWait );
199+ }
200+
153201HWTEST_P (MemObjAsyncDestructionTest, givenUsedMemObjWithAsyncDestructionsEnabledThatHasDestructorCallbacksWhenItIsDestroyedThenDestructorWaitsOnTaskCount) {
154202 bool hasCallbacks = GetParam ();
155203
0 commit comments