fix: assign unique ContextId for secondary engines

- add secondary engines to allRegisteredEngines

Related-To: NEO-11219

Signed-off-by: Mateusz Hoppe <mateusz.hoppe@intel.com>
This commit is contained in:
Mateusz Hoppe 2024-05-20 11:34:33 +00:00 committed by Compute-Runtime-Automation
parent 7bcef39388
commit bfc3e8fcf0
6 changed files with 130 additions and 10 deletions

View File

@ -25,6 +25,7 @@
#include "level_zero/core/source/builtin/builtin_functions_lib.h"
#include "level_zero/core/test/unit_tests/fixtures/cmdlist_fixture.h"
#include "level_zero/core/test/unit_tests/mocks/mock_built_ins.h"
#include "level_zero/core/test/unit_tests/mocks/mock_cmdlist.h"
#include "level_zero/core/test/unit_tests/mocks/mock_cmdqueue.h"
#include "level_zero/core/test/unit_tests/mocks/mock_event.h"
@ -2868,7 +2869,17 @@ HWTEST2_F(CommandListStateBaseAddressGlobalStatelessTest,
otherCommandQueue->destroy();
}
HWTEST2_F(CommandListStateBaseAddressGlobalStatelessTest,
struct ContextGroupStateBaseAddressGlobalStatelessFixture : public CommandListGlobalHeapsFixture<static_cast<int32_t>(NEO::HeapAddressModel::globalStateless)> {
using BaseClass = CommandListGlobalHeapsFixture<static_cast<int32_t>(NEO::HeapAddressModel::globalStateless)>;
void setUp() {
debugManager.flags.ContextGroupSize.set(5);
BaseClass::setUpParams(static_cast<int32_t>(NEO::HeapAddressModel::globalStateless));
}
DebugManagerStateRestore restorer;
};
using ContextGroupStateBaseAddressGlobalStatelessTest = Test<ContextGroupStateBaseAddressGlobalStatelessFixture>;
HWTEST2_F(ContextGroupStateBaseAddressGlobalStatelessTest,
givenContextGroupEnabledAndCommandQueueUsingGlobalStatelessWhenQueueInHeaplessModeThenUsingScratchControllerAndHeapAllocationFromPrimaryCsr,
IsAtLeastXeHpCore) {
@ -2877,9 +2888,6 @@ HWTEST2_F(CommandListStateBaseAddressGlobalStatelessTest,
GTEST_SKIP();
}
DebugManagerStateRestore dbgRestorer;
debugManager.flags.ContextGroupSize.set(5);
hwInfo.featureTable.flags.ftrCCSNode = true;
hwInfo.capabilityTable.defaultEngineType = aub_stream::ENGINE_CCS;
hwInfo.gtSystemInfo.CCSInfo.NumberOfCCSEnabled = 1;
@ -2903,7 +2911,8 @@ HWTEST2_F(CommandListStateBaseAddressGlobalStatelessTest,
auto otherCommandQueue = new MockCommandQueueHw<gfxCoreFamily>(device, secondaryCsr, &desc);
otherCommandQueue->initialize(false, false, false);
otherCommandQueue->heaplessModeEnabled = true;
ze_result_t returnValue;
commandList.reset(CommandList::whiteboxCast(CommandList::create(productFamily, device, engineGroupType, 0u, returnValue, false)));
commandList->close();
ze_command_list_handle_t cmdListHandle = commandList->toHandle();

View File

@ -70,6 +70,7 @@ Device::~Device() {
syncBufferHandler.reset();
isaPoolAllocator.releasePools();
secondaryCsrs.clear();
executionEnvironment->memoryManager->releaseSecondaryOsContexts(this->getRootDeviceIndex());
commandStreamReceivers.clear();
executionEnvironment->memoryManager->waitForDeletions();
@ -500,13 +501,13 @@ bool Device::createSecondaryEngine(CommandStreamReceiver *primaryCsr, uint32_t i
EngineDescriptor engineDescriptor(engineTypeUsage, getDeviceBitfield(), preemptionMode, false, false);
auto osContext = executionEnvironment->memoryManager->createAndRegisterSecondaryOsContext(&primaryCsr->getOsContext(), commandStreamReceiver.get(), engineDescriptor);
osContext->incRefInternal();
commandStreamReceiver->setupContext(*osContext);
commandStreamReceiver->setPrimaryCsr(primaryCsr);
EngineControl engine{commandStreamReceiver.get(), osContext};
secondaryEngines[index].engines.push_back(engine);
commandStreamReceivers.push_back(std::move(commandStreamReceiver));
secondaryCsrs.push_back(std::move(commandStreamReceiver));
return true;
}

View File

@ -242,6 +242,7 @@ class Device : public ReferenceTrackedObject<Device> {
EnginesT allEngines;
std::vector<SecondaryContexts> secondaryEngines;
std::vector<std::unique_ptr<CommandStreamReceiver>> secondaryCsrs;
EngineGroupsT regularEngineGroups;
std::vector<SubDevice *> subdevices;

View File

@ -367,7 +367,7 @@ OsContext *MemoryManager::createAndRegisterSecondaryOsContext(const OsContext *p
updateLatestContextIdForRootDevice(rootDeviceIndex);
auto contextId = primaryContext->getContextId();
auto contextId = ++latestContextId;
auto osContext = OsContext::create(peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->osInterface.get(), rootDeviceIndex, contextId, engineDescriptor);
osContext->incRefInternal();
@ -376,6 +376,7 @@ OsContext *MemoryManager::createAndRegisterSecondaryOsContext(const OsContext *p
UNRECOVERABLE_IF(rootDeviceIndex != osContext->getRootDeviceIndex());
secondaryEngines[rootDeviceIndex].emplace_back(commandStreamReceiver, osContext);
allRegisteredEngines[rootDeviceIndex].emplace_back(commandStreamReceiver, osContext);
return osContext;
}

View File

@ -5339,3 +5339,53 @@ HWTEST_F(CommandStreamReceiverContextGroupTest, givenSecondaryCsrWhenGettingInte
EXPECT_EQ(primaryCsr->getPrimaryScratchSpaceController(), device->secondaryEngines[ccsIndex].engines[secondaryIndex].commandStreamReceiver->getPrimaryScratchSpaceController());
}
}
HWTEST_F(CommandStreamReceiverContextGroupTest, givenSecondaryCsrsWhenSameResourcesAreUsedThenResidencyIsProperlyHandled) {
HardwareInfo hwInfo = *defaultHwInfo;
if (hwInfo.capabilityTable.defaultEngineType != aub_stream::EngineType::ENGINE_CCS) {
GTEST_SKIP();
}
DebugManagerStateRestore dbgRestorer;
debugManager.flags.ContextGroupSize.set(5);
hwInfo.featureTable.flags.ftrCCSNode = true;
hwInfo.capabilityTable.defaultEngineType = aub_stream::ENGINE_CCS;
hwInfo.gtSystemInfo.CCSInfo.NumberOfCCSEnabled = 1;
auto device = std::unique_ptr<MockDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(&hwInfo));
const auto ccsIndex = 0;
auto &commandStreamReceiver0 = *device->getSecondaryEngineCsr(ccsIndex, {EngineHelpers::mapCcsIndexToEngineType(ccsIndex), EngineUsage::regular})->commandStreamReceiver;
auto &commandStreamReceiver1 = *device->getSecondaryEngineCsr(ccsIndex, {EngineHelpers::mapCcsIndexToEngineType(ccsIndex), EngineUsage::regular})->commandStreamReceiver;
auto csr0ContextId = commandStreamReceiver0.getOsContext().getContextId();
auto csr1ContextId = commandStreamReceiver1.getOsContext().getContextId();
auto initialTaskCount0 = commandStreamReceiver0.peekTaskCount();
auto initialTaskCount1 = commandStreamReceiver1.peekTaskCount();
MockGraphicsAllocation graphicsAllocation;
commandStreamReceiver0.makeResident(graphicsAllocation);
EXPECT_EQ(1u, commandStreamReceiver0.getResidencyAllocations().size());
EXPECT_EQ(0u, commandStreamReceiver1.getResidencyAllocations().size());
commandStreamReceiver1.makeResident(graphicsAllocation);
EXPECT_EQ(1u, commandStreamReceiver0.getResidencyAllocations().size());
EXPECT_EQ(1u, commandStreamReceiver1.getResidencyAllocations().size());
EXPECT_EQ(initialTaskCount0 + 1u, graphicsAllocation.getResidencyTaskCount(csr0ContextId));
EXPECT_EQ(initialTaskCount1 + 1u, graphicsAllocation.getResidencyTaskCount(csr1ContextId));
commandStreamReceiver0.makeNonResident(graphicsAllocation);
EXPECT_FALSE(graphicsAllocation.isResident(csr0ContextId));
EXPECT_TRUE(graphicsAllocation.isResident(csr1ContextId));
commandStreamReceiver1.makeNonResident(graphicsAllocation);
EXPECT_FALSE(graphicsAllocation.isResident(csr0ContextId));
EXPECT_FALSE(graphicsAllocation.isResident(csr1ContextId));
EXPECT_EQ(1u, commandStreamReceiver0.getEvictionAllocations().size());
EXPECT_EQ(1u, commandStreamReceiver1.getEvictionAllocations().size());
}

View File

@ -9,6 +9,7 @@
#include "shared/source/gmm_helper/gmm.h"
#include "shared/source/helpers/driver_model_type.h"
#include "shared/source/helpers/gfx_core_helper.h"
#include "shared/source/memory_manager/allocations_list.h"
#include "shared/source/memory_manager/gfx_partition.h"
#include "shared/source/os_interface/device_factory.h"
#include "shared/source/os_interface/driver_info.h"
@ -21,6 +22,7 @@
#include "shared/test/common/helpers/default_hw_info.h"
#include "shared/test/common/helpers/ult_hw_config.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/mocks/mock_allocation_properties.h"
#include "shared/test/common/mocks/mock_builtins.h"
#include "shared/test/common/mocks/mock_compiler_interface.h"
#include "shared/test/common/mocks/mock_compilers.h"
@ -1135,8 +1137,6 @@ HWCMDTEST_F(IGFX_XE_HP_CORE, DeviceTests, givenCCSEngineAndContextGroupSizeEnabl
UltDeviceFactory deviceFactory{1, 0, executionEnvironment};
deviceFactory.rootDevices[0]->createEngine(0, {aub_stream::EngineType::ENGINE_CCS, EngineUsage::regular});
auto defaultEngine = deviceFactory.rootDevices[0]->getDefaultEngine();
EXPECT_NE(nullptr, &defaultEngine);
@ -1310,3 +1310,61 @@ HWTEST_F(DeviceTests, givenContextGroupEnabledWhenDeviceIsDestroyedThenSecondary
EXPECT_EQ(0u, memoryManager->secondaryEngines[0].size());
executionEnvironment->decRefInternal();
}
HWTEST_F(DeviceTests, givenContextGroupEnabledAndAllocationUsedBySeconadryContextWhenDeviceIsDestroyedThenNotCompletedAllocationsAreWaitedOn) {
HardwareInfo hwInfo = *defaultHwInfo;
if (hwInfo.capabilityTable.defaultEngineType != aub_stream::EngineType::ENGINE_CCS) {
GTEST_SKIP();
}
DebugManagerStateRestore dbgRestorer;
debugManager.flags.ContextGroupSize.set(5);
hwInfo.featureTable.flags.ftrCCSNode = true;
hwInfo.capabilityTable.defaultEngineType = aub_stream::ENGINE_CCS;
hwInfo.gtSystemInfo.CCSInfo.NumberOfCCSEnabled = 1;
auto executionEnvironment = NEO::MockDevice::prepareExecutionEnvironment(&hwInfo, 0u);
executionEnvironment->incRefInternal();
auto device = std::unique_ptr<MockDevice>(MockDevice::createWithExecutionEnvironment<MockDevice>(&hwInfo, executionEnvironment, 0));
auto memoryManager = static_cast<MockMemoryManager *>(executionEnvironment->memoryManager.get());
const auto ccsIndex = 0;
auto secondaryEnginesCount = device->secondaryEngines[ccsIndex].engines.size();
ASSERT_EQ(5u, secondaryEnginesCount);
auto engine = device->getSecondaryEngineCsr(ccsIndex, {aub_stream::ENGINE_CCS, EngineUsage::regular});
ASSERT_NE(nullptr, engine);
auto csr = engine->commandStreamReceiver;
auto engine2 = device->getSecondaryEngineCsr(ccsIndex, {aub_stream::ENGINE_CCS, EngineUsage::regular});
ASSERT_NE(nullptr, engine2);
auto csr2 = engine2->commandStreamReceiver;
ASSERT_NE(csr, csr2);
auto tagAddress = csr->getTagAddress();
auto tagAddress2 = csr2->getTagAddress();
EXPECT_NE(csr->getOsContext().getContextId(), csr2->getOsContext().getContextId());
EXPECT_NE(tagAddress, tagAddress2);
auto usedAllocationAndNotGpuCompleted = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{csr->getRootDeviceIndex(), MemoryConstants::pageSize});
usedAllocationAndNotGpuCompleted->updateTaskCount(*tagAddress + 1, csr->getOsContext().getContextId());
auto usedAllocationAndNotGpuCompleted2 = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{csr->getRootDeviceIndex(), MemoryConstants::pageSize});
usedAllocationAndNotGpuCompleted2->updateTaskCount(*tagAddress2 + 1, csr2->getOsContext().getContextId());
memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(usedAllocationAndNotGpuCompleted);
memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(usedAllocationAndNotGpuCompleted2);
EXPECT_TRUE(csr->getTemporaryAllocations().peekIsEmpty());
EXPECT_FALSE(csr->getDeferredAllocations().peekIsEmpty());
EXPECT_EQ(csr->getDeferredAllocations().peekHead(), usedAllocationAndNotGpuCompleted);
usedAllocationAndNotGpuCompleted->updateTaskCount(csr->peekLatestFlushedTaskCount(), csr->getOsContext().getContextId());
usedAllocationAndNotGpuCompleted2->updateTaskCount(csr2->peekLatestFlushedTaskCount(), csr2->getOsContext().getContextId());
device.reset(nullptr);
EXPECT_EQ(0u, memoryManager->secondaryEngines[0].size());
EXPECT_EQ(0u, memoryManager->allRegisteredEngines[0].size());
executionEnvironment->decRefInternal();
}