refactor: prepare CLOS logic for extension

Prepare cache setup and reservation logic to be extended w.r.t other
cache-levels.

Conceptually this change is like adding a switch-statement, in several
places, in which existing code makes a single (and only) case. This is
caused by splitting larger development to ease the review. Further cases
will be added in following steps. Such approach sometimes creates code
which may seem redundant but it is meant to simplify plugging following
extensions in an easy way.

Related-To: NEO-12837
Signed-off-by: Maciej Bielski <maciej.bielski@intel.com>
This commit is contained in:
Maciej Bielski
2025-02-13 17:01:04 +00:00
committed by Compute-Runtime-Automation
parent 9f3a95b7d6
commit 6924a48ca6
29 changed files with 256 additions and 114 deletions

View File

@@ -12,6 +12,7 @@
#include "shared/test/common/helpers/default_hw_info.h"
#include "shared/test/common/mocks/linux/mock_drm_wrappers.h"
#include "shared/test/common/os_interface/linux/device_command_stream_fixture.h"
#include "shared/test/common/test_macros/mock_method_macros.h"
#include <cstdio>
#include <fstream>
@@ -24,8 +25,10 @@ using namespace NEO;
class DrmMock : public Drm {
public:
using BaseClass = Drm;
using Drm::adapterBDF;
using Drm::bindAvailable;
using Drm::cacheInfo;
using Drm::checkQueueSliceSupport;
using Drm::chunkingAvailable;
using Drm::chunkingMode;
@@ -40,7 +43,6 @@ class DrmMock : public Drm {
using Drm::getQueueSliceCount;
using Drm::ioctlHelper;
using Drm::isSharedSystemAllocEnabled;
using Drm::l3CacheInfo;
using Drm::memoryInfo;
using Drm::memoryInfoQueried;
using Drm::minimalChunkingSize;
@@ -190,6 +192,8 @@ class DrmMock : public Drm {
return mockProcessCount;
}
ADDMETHOD_CONST(getDriverModelType, DriverModelType, true, DriverModelType::drm, (), ());
static const int mockFd = 33;
bool failRetTopology = false;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -27,7 +27,7 @@ class DrmQueryMock : public DrmMock {
DrmMockPrelimContext context{
nullptr,
rootDeviceEnvironment,
getL3CacheInfo(),
getCacheInfo(),
failRetTopology,
supportedCopyEnginesMask,
contextDebugSupported,

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2024 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -110,11 +110,11 @@ struct DrmMockCustom : public Drm {
static std::unique_ptr<DrmMockCustom> create(std::unique_ptr<HwDeviceIdDrm> &&hwDeviceId, RootDeviceEnvironment &rootDeviceEnvironment);
using Drm::bindAvailable;
using Drm::cacheInfo;
using Drm::checkToDisableScratchPage;
using Drm::completionFenceSupported;
using Drm::disableScratch;
using Drm::ioctlHelper;
using Drm::l3CacheInfo;
using Drm::memoryInfo;
using Drm::pageFaultSupported;
using Drm::queryTopology;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -12,8 +12,8 @@
#include "shared/test/common/os_interface/linux/device_command_stream_fixture_context.h"
struct DrmMockCustomPrelim : public DrmMockCustom {
using Drm::cacheInfo;
using Drm::ioctlHelper;
using Drm::l3CacheInfo;
using Drm::memoryInfo;
static auto create(RootDeviceEnvironment &rootDeviceEnvironment) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2024 Intel Corporation
* Copyright (C) 2021-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -19,8 +19,8 @@ struct MockCacheInfo : public CacheInfo {
using CacheInfo::reservedCacheRegionsSize;
using CacheInfo::reserveRegion;
MockCacheInfo(IoctlHelper &ioctlHelper, size_t maxReservationCacheSize, uint32_t maxReservationNumCacheRegions, uint16_t maxReservationNumWays)
: CacheInfo(ioctlHelper, maxReservationCacheSize, maxReservationNumCacheRegions, maxReservationNumWays) {}
MockCacheInfo(IoctlHelper &ioctlHelper, CacheReservationParameters l3CacheReservationLimits)
: CacheInfo(ioctlHelper, l3CacheReservationLimits) {}
~MockCacheInfo() override = default;
@@ -28,7 +28,7 @@ struct MockCacheInfo : public CacheInfo {
if (regionIndex >= CacheRegion::count) {
return false;
}
if (regionSize > (maxReservationCacheSize / maxReservationNumCacheRegions)) {
if (regionSize > (l3ReservationLimits.maxSize / l3ReservationLimits.maxNumRegions)) {
return false;
}
return true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Intel Corporation
* Copyright (C) 2023-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -124,7 +124,7 @@ int DrmMockExtended::handleRemainingRequests(DrmIoctl request, void *arg) {
if (cacheReserveArg->clos_index > closIndex) {
return EINVAL;
}
auto cacheInfo = this->getL3CacheInfo();
auto cacheInfo = this->getCacheInfo();
auto maxReservationNumWays = cacheInfo ? cacheInfo->getMaxReservationNumWays() : maxNumWays;
if (cacheReserveArg->num_ways > maxReservationNumWays) {
return EINVAL;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Intel Corporation
* Copyright (C) 2023-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -25,9 +25,9 @@ using namespace NEO;
class DrmMockExtended : public DrmMock {
public:
using Drm::cacheInfo;
using Drm::engineInfo;
using Drm::ioctlHelper;
using Drm::l3CacheInfo;
using Drm::memoryInfo;
using Drm::pageFaultSupported;
using Drm::rootDeviceEnvironment;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -26,7 +26,7 @@ TEST(DrmCacheInfoTest, givenCacheRegionsExistsWhenCallingSetUpCacheInfoThenCache
drm.setupCacheInfo(*defaultHwInfo.get());
auto cacheInfo = drm.getL3CacheInfo();
auto cacheInfo = drm.getCacheInfo();
EXPECT_NE(nullptr, cacheInfo);
if (productHelper.getNumCacheRegions() == 0) {
@@ -56,8 +56,8 @@ TEST(DrmCacheInfoTest, givenDebugFlagSetWhenCallingSetUpCacheInfoThenCacheInfoIs
DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
drm.setupCacheInfo(*defaultHwInfo.get());
EXPECT_NE(nullptr, drm.getL3CacheInfo());
auto cacheInfo = drm.getL3CacheInfo();
EXPECT_NE(nullptr, drm.getCacheInfo());
auto cacheInfo = drm.getCacheInfo();
EXPECT_EQ(0u, cacheInfo->getMaxReservationCacheSize());
EXPECT_EQ(0u, cacheInfo->getMaxReservationNumCacheRegions());
@@ -68,7 +68,11 @@ TEST(DrmCacheInfoTest, givenCacheInfoCreatedWhenGetCacheRegionSucceedsToReserveC
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
CacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
CacheInfo cacheInfo(*drm.getIoctlHelper(), l3CacheParameters);
size_t cacheReservationSize = cacheInfo.getMaxReservationCacheSize();
EXPECT_TRUE(cacheInfo.getCacheRegion(cacheReservationSize, CacheRegion::region1));
@@ -80,7 +84,11 @@ TEST(DrmCacheInfoTest, givenCacheInfoCreatedWhenGetCacheRegionFailsToReserveCach
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
CacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
CacheInfo cacheInfo(*drm.getIoctlHelper(), l3CacheParameters);
size_t cacheReservationSize = cacheInfo.getMaxReservationCacheSize();
drm.context.closIndex = 0xFFFF;
@@ -93,7 +101,11 @@ TEST(DrmCacheInfoTest, givenCacheInfoWithReservedCacheRegionWhenGetCacheRegionIs
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
CacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
CacheInfo cacheInfo(*drm.getIoctlHelper(), l3CacheParameters);
size_t cacheReservationSize = cacheInfo.getMaxReservationCacheSize();
EXPECT_EQ(CacheRegion::region1, cacheInfo.reserveCacheRegion(cacheReservationSize));
@@ -107,7 +119,11 @@ TEST(DrmCacheInfoTest, givenCacheInfoCreatedWhenGetCacheRegionIsCalledForReserva
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
CacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
CacheInfo cacheInfo(*drm.getIoctlHelper(), l3CacheParameters);
size_t regionSize = cacheInfo.getMaxReservationCacheSize() / cacheInfo.getMaxReservationNumCacheRegions();
EXPECT_TRUE(cacheInfo.getCacheRegion(regionSize, CacheRegion::region1));
@@ -126,7 +142,11 @@ TEST(DrmCacheInfoTest, givenCacheInfoWhenSpecificNumCacheWaysIsRequestedThenRese
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
MockCacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, maxNumCacheWays);
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = maxNumCacheWays;
MockCacheInfo cacheInfo(*drm.getIoctlHelper(), l3CacheParameters);
size_t maxReservationCacheSize = cacheInfo.getMaxReservationCacheSize();
EXPECT_EQ(CacheRegion::region1, cacheInfo.reserveCacheRegion(maxReservationCacheSize));
@@ -144,7 +164,11 @@ TEST(DrmCacheInfoTest, givenCacheInfoWhenNumCacheWaysIsExceededThenDontReserveCa
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
MockCacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, maxNumCacheWays);
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = maxNumCacheWays;
MockCacheInfo cacheInfo(*drm.getIoctlHelper(), l3CacheParameters);
size_t maxReservationCacheSize = cacheInfo.getMaxReservationCacheSize();
EXPECT_EQ(CacheRegion::region1, cacheInfo.reserveCacheRegion(maxReservationCacheSize));
@@ -155,10 +179,15 @@ TEST(DrmCacheInfoTest, givenCacheInfoWhenNumCacheWaysIsExceededThenDontReserveCa
}
TEST(DrmCacheInfoTest, givenCacheInfoCreatedWhenFreeCacheRegionIsCalledForNonReservedRegionThenItFails) {
constexpr uint16_t maxNumCacheWays = 32;
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
MockCacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = maxNumCacheWays;
MockCacheInfo cacheInfo(*drm.getIoctlHelper(), l3CacheParameters);
cacheInfo.reservedCacheRegionsSize[toUnderlying(CacheRegion::region1)] = MemoryConstants::kiloByte;
EXPECT_EQ(CacheRegion::none, cacheInfo.freeCacheRegion(CacheRegion::region1));

View File

@@ -4944,7 +4944,11 @@ TEST_F(DrmAllocationTests, givenDrmAllocationWhenDefaultCacheInfoIsAvailableThen
TEST_F(DrmAllocationTests, givenDrmAllocationWhenCacheRegionIsNotSetThenReturnFalse) {
const uint32_t rootDeviceIndex = 0u;
DrmMock drm(*executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]);
drm.l3CacheInfo.reset(new MockCacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32));
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
drm.cacheInfo.reset(new MockCacheInfo(*drm.getIoctlHelper(), l3CacheParameters));
MockDrmAllocation allocation(rootDeviceIndex, AllocationType::buffer, MemoryPool::localMemory);
@@ -4955,7 +4959,12 @@ TEST_F(DrmAllocationTests, givenDrmAllocationWhenCacheRegionIsSetSuccessfullyThe
const uint32_t rootDeviceIndex = 0u;
DrmMock drm(*executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]);
drm.queryAndSetVmBindPatIndexProgrammingSupport();
drm.l3CacheInfo.reset(new MockCacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32));
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
drm.cacheInfo.reset(new MockCacheInfo(*drm.getIoctlHelper(), l3CacheParameters));
MockDrmAllocation allocation(rootDeviceIndex, AllocationType::buffer, MemoryPool::localMemory);
auto &productHelper = executionEnvironment->rootDeviceEnvironments[0]->getHelper<ProductHelper>();
@@ -4972,7 +4981,12 @@ TEST_F(DrmAllocationTests, givenDrmAllocationWhenCacheRegionIsSetSuccessfullyThe
const uint32_t rootDeviceIndex = 0u;
DrmMock drm(*executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]);
drm.queryAndSetVmBindPatIndexProgrammingSupport();
drm.l3CacheInfo.reset(new MockCacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32));
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
drm.cacheInfo.reset(new MockCacheInfo(*drm.getIoctlHelper(), l3CacheParameters));
MockBufferObject bo(rootDeviceIndex, &drm, 3, 0, 0, 1);
MockDrmAllocation allocation(rootDeviceIndex, AllocationType::buffer, MemoryPool::localMemory);
@@ -5128,7 +5142,12 @@ TEST_F(DrmMemoryManagerTest, givenDrmAllocationWithHostPtrWhenItIsCreatedWithCac
}
mock->ioctlExpected.total = -1;
auto drm = static_cast<DrmMockCustom *>(executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface->getDriverModel()->as<Drm>());
drm->l3CacheInfo.reset(new MockCacheInfo(*drm->getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32));
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 32 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
drm->cacheInfo.reset(new MockCacheInfo(*drm->getIoctlHelper(), l3CacheParameters));
auto ptr = reinterpret_cast<void *>(0x1000);
auto size = MemoryConstants::pageSize;
@@ -6264,7 +6283,7 @@ TEST_F(DrmMemoryManagerWithLocalMemoryAndExplicitExpectationsTest, givenUAllocat
allocData.cacheRegion = 0xFFFF;
auto &drm = static_cast<DrmMockCustom &>(memoryManager->getDrm(0));
drm.l3CacheInfo.reset(nullptr);
drm.cacheInfo.reset(nullptr);
auto allocation = memoryManager->allocatePhysicalLocalDeviceMemory(allocData, status);
EXPECT_EQ(nullptr, allocation);

View File

@@ -1294,7 +1294,11 @@ TEST_F(DrmMemoryOperationsHandlerBindTest, givenClosEnabledAndAllocationToBeCach
auto osContext = memoryManager->createAndRegisterOsContext(csr.get(), EngineDescriptorHelper::getDefaultDescriptor());
csr->setupContext(*osContext);
mock->l3CacheInfo.reset(new CacheInfo(*mock->getIoctlHelper(), 64 * MemoryConstants::kiloByte, 2, 32));
CacheReservationParameters l3CacheParameters{};
l3CacheParameters.maxSize = 64 * MemoryConstants::kiloByte;
l3CacheParameters.maxNumRegions = 2;
l3CacheParameters.maxNumWays = 32;
mock->cacheInfo.reset(new CacheInfo(*mock->getIoctlHelper(), l3CacheParameters));
auto &productHelper = executionEnvironment->rootDeviceEnvironments[0]->getHelper<ProductHelper>();