Revert "fix: Remove fence handling when reuse cmd buffer"

This reverts commit f3bbd70a58.

Signed-off-by: Compute-Runtime-Validation <compute-runtime-validation@intel.com>
This commit is contained in:
Compute-Runtime-Validation 2024-01-27 07:53:40 +01:00 committed by Compute-Runtime-Automation
parent 46fb73026e
commit 63a5b64c7e
4 changed files with 99 additions and 1 deletions

View File

@ -119,6 +119,8 @@ CommandList *CommandList::create(uint32_t productFamily, Device *device, NEO::En
if (returnValue != ZE_RESULT_SUCCESS) {
commandList->destroy();
commandList = nullptr;
} else {
commandList->getCmdContainer().setHandleFenceCompletionRequired();
}
}

View File

@ -296,9 +296,28 @@ void CommandContainer::handleCmdBufferAllocations(size_t startIndex) {
}
for (size_t i = startIndex; i < cmdBufferAllocations.size(); i++) {
if (this->reusableAllocationList) {
if (isHandleFenceCompletionRequired) {
bool allocationHandled = false;
for (auto &engine : this->device->getMemoryManager()->getRegisteredEngines(cmdBufferAllocations[i]->getRootDeviceIndex())) {
auto osContextId = engine.osContext->getContextId();
if (cmdBufferAllocations[i]->isUsedByOsContext(osContextId) && engine.commandStreamReceiver->isAnyDirectSubmissionEnabled()) {
auto lock = engine.commandStreamReceiver->obtainUniqueOwnership();
auto taskCount = engine.commandStreamReceiver->peekTaskCount() + 1;
cmdBufferAllocations[i]->updateTaskCount(taskCount, osContextId);
cmdBufferAllocations[i]->updateResidencyTaskCount(taskCount, osContextId);
engine.commandStreamReceiver->flushTagUpdate();
engine.commandStreamReceiver->waitForTaskCount(taskCount);
allocationHandled = true;
}
}
if (!allocationHandled && isHandleFenceCompletionRequired) {
this->device->getMemoryManager()->handleFenceCompletion(cmdBufferAllocations[i]);
}
for (auto &engine : this->device->getMemoryManager()->getRegisteredEngines(cmdBufferAllocations[i]->getRootDeviceIndex())) {
auto osContextId = engine.osContext->getContextId();
cmdBufferAllocations[i]->releaseUsageInOsContext(osContextId);
}
reusableAllocationList->pushFrontOne(*cmdBufferAllocations[i]);
} else {
this->device->getMemoryManager()->freeGraphicsMemory(cmdBufferAllocations[i]);

View File

@ -202,6 +202,9 @@ class CommandContainer : public NonCopyableOrMovableClass {
return this->alignedPrimarySize;
}
void endAlignedPrimaryBuffer();
void setHandleFenceCompletionRequired() {
this->isHandleFenceCompletionRequired = true;
}
protected:
size_t getAlignedCmdBufferSize() const;

View File

@ -302,6 +302,66 @@ TEST_F(CommandContainerTest, givenCmdContainerWithAllocsListWhenAllocateAndReset
allocList.freeAllGraphicsAllocations(pDevice);
}
HWTEST_F(CommandContainerTest, givenCmdContainerAndHandleFenceWithAllocsListWhenAllocateAndResetThenCmdBufferAllocIsReused) {
AllocationsList allocList;
auto cmdContainer = std::make_unique<CommandContainer>();
cmdContainer->setHandleFenceCompletionRequired();
cmdContainer->initialize(pDevice, &allocList, true, HeapSize::defaultHeapSize, false);
auto &cmdBufferAllocs = cmdContainer->getCmdBufferAllocations();
auto memoryManager = static_cast<MockMemoryManager *>(pDevice->getMemoryManager());
auto csr = reinterpret_cast<UltCommandStreamReceiver<FamilyType> *>(memoryManager->getRegisteredEngines(0u)[0].commandStreamReceiver);
csr->directSubmissionAvailable = true;
csr->callFlushTagUpdate = false;
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 0u);
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
EXPECT_TRUE(allocList.peekIsEmpty());
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
auto cmdBuffer0 = cmdBufferAllocs[0];
auto cmdBuffer1 = cmdBufferAllocs[1];
cmdContainer->reset();
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 1u);
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
EXPECT_EQ(cmdBufferAllocs[0], cmdBuffer0);
EXPECT_FALSE(allocList.peekIsEmpty());
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
cmdContainer->reset();
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 2u);
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
EXPECT_EQ(cmdBufferAllocs[0], cmdBuffer0);
EXPECT_FALSE(allocList.peekIsEmpty());
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
EXPECT_EQ(cmdBufferAllocs[0], cmdBuffer0);
EXPECT_EQ(cmdBufferAllocs[1], cmdBuffer1);
EXPECT_TRUE(allocList.peekIsEmpty());
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
cmdBuffer1->updateTaskCount(1u, 0u);
cmdContainer.reset();
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
csr = reinterpret_cast<UltCommandStreamReceiver<FamilyType> *>(memoryManager->getRegisteredEngines(0u)[1].commandStreamReceiver);
EXPECT_FALSE(csr->stopDirectSubmissionCalled);
EXPECT_FALSE(csr->stopDirectSubmissionCalledBlocking);
EXPECT_EQ(memoryManager->handleFenceCompletionCalled, 3u);
EXPECT_FALSE(allocList.peekIsEmpty());
cmdBuffer1->releaseUsageInOsContext(0u);
allocList.freeAllGraphicsAllocations(pDevice);
}
TEST_F(CommandContainerTest, givenReusableAllocationsAndRemoveUserFenceInCmdlistResetAndDestroyFlagWhenAllocateAndResetThenHandleFenceCompletionIsCalled) {
DebugManagerStateRestore restore;
debugManager.flags.RemoveUserFenceInCmdlistResetAndDestroy.set(0);
@ -315,14 +375,21 @@ TEST_F(CommandContainerTest, givenReusableAllocationsAndRemoveUserFenceInCmdlist
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
cmdContainer->reset();
EXPECT_EQ(1u, memoryManager->handleFenceCompletionCalled);
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
cmdBufferAllocs[1]->updateTaskCount(2u, 0u);
cmdContainer->reset();
EXPECT_EQ(2u, memoryManager->handleFenceCompletionCalled);
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
EXPECT_FALSE(cmdBufferAllocs[1]->isUsedByOsContext(0u));
cmdBufferAllocs[0]->updateTaskCount(5u, 0u);
cmdBufferAllocs[1]->updateTaskCount(5u, 0u);
cmdContainer.reset();
EXPECT_EQ(4u, memoryManager->handleFenceCompletionCalled);
allocList.freeAllGraphicsAllocations(pDevice);
@ -341,14 +408,21 @@ TEST_F(CommandContainerTest, givenReusableAllocationsAndRemoveUserFenceInCmdlist
EXPECT_EQ(cmdBufferAllocs.size(), 1u);
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
cmdContainer->reset();
EXPECT_EQ(0u, memoryManager->handleFenceCompletionCalled);
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
cmdBufferAllocs[1]->updateTaskCount(2u, 0u);
cmdContainer->reset();
EXPECT_EQ(0u, memoryManager->handleFenceCompletionCalled);
cmdContainer->allocateNextCommandBuffer();
EXPECT_EQ(cmdBufferAllocs.size(), 2u);
EXPECT_FALSE(cmdBufferAllocs[1]->isUsedByOsContext(0u));
cmdBufferAllocs[0]->updateTaskCount(5u, 0u);
cmdBufferAllocs[1]->updateTaskCount(5u, 0u);
cmdContainer.reset();
EXPECT_EQ(0u, memoryManager->handleFenceCompletionCalled);
allocList.freeAllGraphicsAllocations(pDevice);