Skip to content

Commit 63a5b64

Browse files
Revert "fix: Remove fence handling when reuse cmd buffer"
This reverts commit f3bbd70. Signed-off-by: Compute-Runtime-Validation <[email protected]>
1 parent 46fb730 commit 63a5b64

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

level_zero/core/source/cmdlist/cmdlist_imp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ CommandList *CommandList::create(uint32_t productFamily, Device *device, NEO::En
119119
if (returnValue != ZE_RESULT_SUCCESS) {
120120
commandList->destroy();
121121
commandList = nullptr;
122+
} else {
123+
commandList->getCmdContainer().setHandleFenceCompletionRequired();
122124
}
123125
}
124126

shared/source/command_container/cmdcontainer.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,28 @@ void CommandContainer::handleCmdBufferAllocations(size_t startIndex) {
296296
}
297297
for (size_t i = startIndex; i < cmdBufferAllocations.size(); i++) {
298298
if (this->reusableAllocationList) {
299-
if (isHandleFenceCompletionRequired) {
299+
bool allocationHandled = false;
300+
for (auto &engine : this->device->getMemoryManager()->getRegisteredEngines(cmdBufferAllocations[i]->getRootDeviceIndex())) {
301+
auto osContextId = engine.osContext->getContextId();
302+
if (cmdBufferAllocations[i]->isUsedByOsContext(osContextId) && engine.commandStreamReceiver->isAnyDirectSubmissionEnabled()) {
303+
auto lock = engine.commandStreamReceiver->obtainUniqueOwnership();
304+
auto taskCount = engine.commandStreamReceiver->peekTaskCount() + 1;
305+
cmdBufferAllocations[i]->updateTaskCount(taskCount, osContextId);
306+
cmdBufferAllocations[i]->updateResidencyTaskCount(taskCount, osContextId);
307+
engine.commandStreamReceiver->flushTagUpdate();
308+
engine.commandStreamReceiver->waitForTaskCount(taskCount);
309+
allocationHandled = true;
310+
}
311+
}
312+
if (!allocationHandled && isHandleFenceCompletionRequired) {
300313
this->device->getMemoryManager()->handleFenceCompletion(cmdBufferAllocations[i]);
301314
}
315+
316+
for (auto &engine : this->device->getMemoryManager()->getRegisteredEngines(cmdBufferAllocations[i]->getRootDeviceIndex())) {
317+
auto osContextId = engine.osContext->getContextId();
318+
cmdBufferAllocations[i]->releaseUsageInOsContext(osContextId);
319+
}
320+
302321
reusableAllocationList->pushFrontOne(*cmdBufferAllocations[i]);
303322
} else {
304323
this->device->getMemoryManager()->freeGraphicsMemory(cmdBufferAllocations[i]);

shared/source/command_container/cmdcontainer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ class CommandContainer : public NonCopyableOrMovableClass {
202202
return this->alignedPrimarySize;
203203
}
204204
void endAlignedPrimaryBuffer();
205+
void setHandleFenceCompletionRequired() {
206+
this->isHandleFenceCompletionRequired = true;
207+
}
205208

206209
protected:
207210
size_t getAlignedCmdBufferSize() const;

shared/test/unit_test/command_container/command_container_tests.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,66 @@ TEST_F(CommandContainerTest, givenCmdContainerWithAllocsListWhenAllocateAndReset
302302
allocList.freeAllGraphicsAllocations(pDevice);
303303
}
304304

305+
HWTEST_F(CommandContainerTest, givenCmdContainerAndHandleFenceWithAllocsListWhenAllocateAndResetThenCmdBufferAllocIsReused) {
306+
AllocationsList allocList;
307+
auto cmdContainer = std::make_unique<CommandContainer>();
308+
cmdContainer->setHandleFenceCompletionRequired();
309+
cmdContainer->initialize(pDevice, &allocList, true, HeapSize::defaultHeapSize, false);
310+
auto &cmdBufferAllocs = cmdContainer->getCmdBufferAllocations();
311+
auto memoryManager = static_cast<MockMemoryManager *>(pDevice->getMemoryManager());
312+
auto csr = reinterpret_cast<UltCommandStreamReceiver<FamilyType> *>(memoryManager->getRegisteredEngines(0u)[0].commandStreamReceiver);
313+
csr->directSubmissionAvailable = true;
314+
csr->callFlushTagUpdate = false;
315+
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 0u);
316+
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
317+
EXPECT_TRUE(allocList.peekIsEmpty());
318+
319+
cmdContainer->allocateNextCommandBuffer();
320+
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
321+
322+
auto cmdBuffer0 = cmdBufferAllocs[0];
323+
auto cmdBuffer1 = cmdBufferAllocs[1];
324+
325+
cmdContainer->reset();
326+
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 1u);
327+
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
328+
EXPECT_EQ(cmdBufferAllocs[0], cmdBuffer0);
329+
EXPECT_FALSE(allocList.peekIsEmpty());
330+
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
331+
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
332+
333+
cmdContainer->allocateNextCommandBuffer();
334+
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
335+
cmdContainer->reset();
336+
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 2u);
337+
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
338+
EXPECT_EQ(cmdBufferAllocs[0], cmdBuffer0);
339+
EXPECT_FALSE(allocList.peekIsEmpty());
340+
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
341+
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
342+
343+
cmdContainer->allocateNextCommandBuffer();
344+
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
345+
EXPECT_EQ(cmdBufferAllocs[0], cmdBuffer0);
346+
EXPECT_EQ(cmdBufferAllocs[1], cmdBuffer1);
347+
EXPECT_TRUE(allocList.peekIsEmpty());
348+
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
349+
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
350+
cmdBuffer1->updateTaskCount(1u, 0u);
351+
352+
cmdContainer.reset();
353+
354+
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
355+
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
356+
csr = reinterpret_cast<UltCommandStreamReceiver<FamilyType> *>(memoryManager->getRegisteredEngines(0u)[1].commandStreamReceiver);
357+
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
358+
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
359+
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 3u);
360+
EXPECT_FALSE(allocList.peekIsEmpty());
361+
cmdBuffer1->releaseUsageInOsContext(0u);
362+
allocList.freeAllGraphicsAllocations(pDevice);
363+
}
364+
305365
TEST_F(CommandContainerTest, givenReusableAllocationsAndRemoveUserFenceInCmdlistResetAndDestroyFlagWhenAllocateAndResetThenHandleFenceCompletionIsCalled) {
306366
DebugManagerStateRestore restore;
307367
debugManager.flags.RemoveUserFenceInCmdlistResetAndDestroy.set(0);
@@ -315,14 +375,21 @@ TEST_F(CommandContainerTest, givenReusableAllocationsAndRemoveUserFenceInCmdlist
315375
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
316376
cmdContainer->allocateNextCommandBuffer();
317377
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
378+
318379
cmdContainer->reset();
319380
EXPECT_EQ(1u, memoryManager->handleFenceCompletionCalled);
320381
cmdContainer->allocateNextCommandBuffer();
321382
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
383+
384+
cmdBufferAllocs[1]->updateTaskCount(2u, 0u);
322385
cmdContainer->reset();
323386
EXPECT_EQ(2u, memoryManager->handleFenceCompletionCalled);
324387
cmdContainer->allocateNextCommandBuffer();
325388
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
389+
EXPECT_FALSE(cmdBufferAllocs[1]->isUsedByOsContext(0u));
390+
391+
cmdBufferAllocs[0]->updateTaskCount(5u, 0u);
392+
cmdBufferAllocs[1]->updateTaskCount(5u, 0u);
326393
cmdContainer.reset();
327394
EXPECT_EQ(4u, memoryManager->handleFenceCompletionCalled);
328395
allocList.freeAllGraphicsAllocations(pDevice);
@@ -341,14 +408,21 @@ TEST_F(CommandContainerTest, givenReusableAllocationsAndRemoveUserFenceInCmdlist
341408
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
342409
cmdContainer->allocateNextCommandBuffer();
343410
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
411+
344412
cmdContainer->reset();
345413
EXPECT_EQ(0u, memoryManager->handleFenceCompletionCalled);
346414
cmdContainer->allocateNextCommandBuffer();
347415
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
416+
417+
cmdBufferAllocs[1]->updateTaskCount(2u, 0u);
348418
cmdContainer->reset();
349419
EXPECT_EQ(0u, memoryManager->handleFenceCompletionCalled);
350420
cmdContainer->allocateNextCommandBuffer();
351421
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
422+
EXPECT_FALSE(cmdBufferAllocs[1]->isUsedByOsContext(0u));
423+
424+
cmdBufferAllocs[0]->updateTaskCount(5u, 0u);
425+
cmdBufferAllocs[1]->updateTaskCount(5u, 0u);
352426
cmdContainer.reset();
353427
EXPECT_EQ(0u, memoryManager->handleFenceCompletionCalled);
354428
allocList.freeAllGraphicsAllocations(pDevice);

0 commit comments

Comments
 (0)