feature: unify local memory size getter for i915 and xe

Previously i915 was using a sysfs node to query the amount of local
memory of a tile. For xe such sysfs node does not exist so the function
was useless.

Instead of a sysfs node, leverage the local memory size present in
MemoryInfo (queried at init-stage) so that the output is now the same
for both i915 and xe.

Related-To: NEO-11127
Signed-off-by: Maciej Bielski <maciej.bielski@intel.com>
This commit is contained in:
Maciej Bielski
2025-03-06 09:57:13 +00:00
committed by Compute-Runtime-Automation
parent f08b32761e
commit 8ad6a7a86b
6 changed files with 71 additions and 101 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -69,6 +69,7 @@ class DriverQueryPeerTest : public ::testing::Test {
devices[i]->getExecutionEnvironment()->rootDeviceEnvironments[i]->osInterface = std::make_unique<NEO::OSInterface>();
auto osInterface = devices[i]->getExecutionEnvironment()->rootDeviceEnvironments[i]->osInterface.get();
auto drmMock = new DrmMockQueryFabricStats(*executionEnvironment->rootDeviceEnvironments[i]);
drmMock->useBaseGetDeviceMemoryPhysicalSizeInBytes = false;
drmMock->ioctlHelper.reset(new MockIoctlQueryFabricStats(*drmMock));
osInterface->setDriverModel(std::unique_ptr<Drm>(drmMock));
}
@@ -303,6 +304,7 @@ class DriverQueryPeerTestOsInterfaceFail : public ::testing::Test {
devices[i]->getExecutionEnvironment()->rootDeviceEnvironments[i]->osInterface = std::make_unique<NEO::OSInterface>();
auto osInterface = devices[i]->getExecutionEnvironment()->rootDeviceEnvironments[i]->osInterface.get();
auto drmMock = new DrmMockQueryFabricStats(*executionEnvironment->rootDeviceEnvironments[i]);
drmMock->useBaseGetDeviceMemoryPhysicalSizeInBytes = false;
osInterface->setDriverModel(std::unique_ptr<Drm>(drmMock));
}
}
@@ -378,6 +380,7 @@ class DriverQueryPeerTestFail : public ::testing::Test {
devices[i]->getExecutionEnvironment()->rootDeviceEnvironments[i]->osInterface = std::make_unique<NEO::OSInterface>();
auto osInterface = devices[i]->getExecutionEnvironment()->rootDeviceEnvironments[i]->osInterface.get();
auto drmMock = new DrmMockQueryFabricStats(*executionEnvironment->rootDeviceEnvironments[i]);
drmMock->useBaseGetDeviceMemoryPhysicalSizeInBytes = false;
drmMock->ioctlHelper.reset(new MockIoctlQueryFabricStatsFail(*drmMock));
osInterface->setDriverModel(std::unique_ptr<Drm>(drmMock));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -12,6 +12,8 @@
#include "shared/source/os_interface/linux/drm_buffer_object.h"
#include "shared/source/os_interface/linux/drm_gem_close_worker.h"
#include "shared/source/os_interface/linux/drm_memory_manager.h"
#include "shared/source/os_interface/linux/ioctl_helper.h"
#include "shared/source/os_interface/linux/memory_info.h"
#include "shared/test/common/libult/linux/drm_mock.h"
#include "shared/test/common/mocks/linux/mock_drm_allocation.h"
#include "shared/test/common/mocks/mock_device.h"
@@ -180,7 +182,16 @@ struct MemoryExportImportObtainFdTest : public ::testing::Test {
for (auto i = 0u; i < executionEnvironment->rootDeviceEnvironments.size(); i++) {
devices.push_back(std::unique_ptr<NEO::Device>(deviceFactory->rootDevices[i]));
executionEnvironment->rootDeviceEnvironments[i]->osInterface.reset(new NEO::OSInterface());
auto drmMock = new DrmMockResources(*executionEnvironment->rootDeviceEnvironments[i]);
auto ioctlHelper{drmMock->getIoctlHelper()};
const auto memoryClassSystem = static_cast<uint16_t>(ioctlHelper->getDrmParamValue(DrmParam::memoryClassSystem));
const auto memoryClassDevice = static_cast<uint16_t>(ioctlHelper->getDrmParamValue(DrmParam::memoryClassDevice));
std::vector<MemoryRegion> mockMemRegions(3);
mockMemRegions[0] = {{memoryClassSystem, 0}, 1024};
mockMemRegions[1] = {{memoryClassDevice, 0}, 2048};
drmMock->memoryInfo.reset(new MemoryInfo{mockMemRegions, *drmMock});
executionEnvironment->rootDeviceEnvironments[i]->osInterface->setDriverModel(std::unique_ptr<Drm>(drmMock));
}
@@ -604,7 +615,16 @@ struct MemoryObtainFdTest : public ::testing::Test {
for (auto i = 0u; i < executionEnvironment->rootDeviceEnvironments.size(); i++) {
devices.push_back(std::unique_ptr<NEO::Device>(deviceFactory->rootDevices[i]));
executionEnvironment->rootDeviceEnvironments[i]->osInterface.reset(new NEO::OSInterface());
auto drmMock = new DrmMockResources(*executionEnvironment->rootDeviceEnvironments[i]);
auto ioctlHelper{drmMock->getIoctlHelper()};
const auto memoryClassSystem = static_cast<uint16_t>(ioctlHelper->getDrmParamValue(DrmParam::memoryClassSystem));
const auto memoryClassDevice = static_cast<uint16_t>(ioctlHelper->getDrmParamValue(DrmParam::memoryClassDevice));
std::vector<MemoryRegion> mockMemRegions(3);
mockMemRegions[0] = {{memoryClassSystem, 0}, 1024};
mockMemRegions[1] = {{memoryClassDevice, 0}, 2048};
drmMock->memoryInfo.reset(new MemoryInfo{mockMemRegions, *drmMock});
executionEnvironment->rootDeviceEnvironments[i]->osInterface->setDriverModel(std::unique_ptr<Drm>(drmMock));
}

View File

@@ -984,19 +984,12 @@ bool Drm::getDeviceMemoryMaxClockRateInMhz(uint32_t tileId, uint32_t &clkRate) {
}
bool Drm::getDeviceMemoryPhysicalSizeInBytes(uint32_t tileId, uint64_t &physicalSize) {
const std::string relativefilePath = "/gt/gt" + std::to_string(tileId) + "/addr_range";
std::string readString(64, '\0');
errno = 0;
if (readSysFsAsString(relativefilePath, readString) == false) {
if (memoryInfo == nullptr || memoryInfo->getLocalMemoryRegions().size() == 0U) {
physicalSize = 0U;
return false;
}
char *endPtr = nullptr;
uint64_t retSize = static_cast<uint64_t>(std::strtoull(readString.data(), &endPtr, 16));
if ((endPtr == readString.data()) || (errno != 0)) {
return false;
}
physicalSize = retSize;
physicalSize = memoryInfo->getLocalMemoryRegionSize(tileId);
return true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2024 Intel Corporation
* Copyright (C) 2021-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -98,6 +98,12 @@ uint32_t MemoryInfo::getLocalMemoryRegionIndex(DeviceBitfield deviceBitfield) co
return tileToLocalMemoryRegionIndexMap[tileIndex];
}
uint64_t MemoryInfo::getLocalMemoryRegionSize(uint32_t tileIndex) const {
UNRECOVERABLE_IF(tileIndex >= tileToLocalMemoryRegionIndexMap.size());
const auto regionIndex{tileToLocalMemoryRegionIndexMap[tileIndex]};
return localMemoryRegions[regionIndex].probedSize;
}
MemoryClassInstance MemoryInfo::getMemoryRegionClassAndInstance(DeviceBitfield deviceBitfield, const HardwareInfo &hwInfo) {
auto &gfxCoreHelper = this->drm.getRootDeviceEnvironment().getHelper<GfxCoreHelper>();

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2024 Intel Corporation
* Copyright (C) 2019-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -42,6 +42,7 @@ class MemoryInfo {
MOCKABLE_VIRTUAL int createGemExtWithMultipleRegions(DeviceBitfield memoryBanks, size_t allocSize, uint32_t &handle, uint64_t patIndex, bool isUSMHostAllocation);
MOCKABLE_VIRTUAL int createGemExtWithMultipleRegions(DeviceBitfield memoryBanks, size_t allocSize, uint32_t &handle, uint64_t patIndex, int32_t pairHandle, bool isChunked, uint32_t numOfChunks, bool isUSMHostAllocation);
void populateTileToLocalMemoryRegionIndexMap();
uint64_t getLocalMemoryRegionSize(uint32_t tileId) const;
const RegionContainer &getLocalMemoryRegions() const { return localMemoryRegions; }
const RegionContainer &getDrmRegionInfos() const { return drmQueryRegions; }

View File

@@ -140,25 +140,48 @@ TEST(DrmTest, GivenValidSysfsNodeWhenGetDeviceMemoryMaxClockRateInMhzIsCalledThe
EXPECT_EQ(clkRate, 800u);
}
TEST(DrmTest, GivenValidSysfsNodeWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenReturnSuccess) {
TEST(DrmTest, GivenMemoryInfoWithLocalMemoryRegionsWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenCorrectSizeReturned) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
drm.setPciPath("device");
VariableBackup<decltype(SysCalls::sysCallsOpen)> mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
return 1;
});
auto ioctlHelper{drm.getIoctlHelper()};
const auto memoryClassSystem = static_cast<uint16_t>(ioctlHelper->getDrmParamValue(DrmParam::memoryClassSystem));
const auto memoryClassDevice = static_cast<uint16_t>(ioctlHelper->getDrmParamValue(DrmParam::memoryClassDevice));
std::vector<MemoryRegion> memRegions(3);
memRegions[0] = {{memoryClassSystem, 0}, 1024};
memRegions[1] = {{memoryClassDevice, 0}, 2048};
memRegions[2] = {{memoryClassDevice, 1}, 3072};
drm.memoryInfo.reset(new MemoryInfo{memRegions, drm});
VariableBackup<decltype(SysCalls::sysCallsPread)> mockPread(&SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
const std::string testData("800");
memcpy(buf, testData.data(), testData.length() + 1);
return 4;
});
uint64_t size = 0;
uint64_t size{0U};
EXPECT_TRUE(drm.getDeviceMemoryPhysicalSizeInBytes(0, size));
EXPECT_EQ(2048u, size);
}
TEST(DrmTest, GivenMemoryInfoWithNoLocalMemoryRegionsWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenZeroIsReturned) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
auto ioctlHelper{drm.getIoctlHelper()};
const auto memoryClassSystem = static_cast<uint16_t>(ioctlHelper->getDrmParamValue(DrmParam::memoryClassSystem));
std::vector<MemoryRegion> memRegions(1);
memRegions[0] = {{memoryClassSystem, 0}, 2048};
drm.memoryInfo.reset(new MemoryInfo{memRegions, drm});
uint64_t size{0U};
EXPECT_FALSE(drm.getDeviceMemoryPhysicalSizeInBytes(0, size));
EXPECT_EQ(0U, size);
}
TEST(DrmTest, GivenNoMemoryInfoWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenZeroIsReturned) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
uint64_t size{0U};
EXPECT_FALSE(drm.getDeviceMemoryPhysicalSizeInBytes(0, size));
EXPECT_EQ(0U, size);
}
TEST(DrmTest, GivenInValidSysfsNodeWhenGetDeviceMemoryMaxClockRateInMhzIsCalledThenReturnSuccess) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
@@ -172,82 +195,6 @@ TEST(DrmTest, GivenInValidSysfsNodeWhenGetDeviceMemoryMaxClockRateInMhzIsCalledT
EXPECT_FALSE(drm.getDeviceMemoryMaxClockRateInMhz(0, clkRate));
}
TEST(DrmTest, GivenPciPathCouldNotBeRetrievedWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenReturnZero) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
drm.setPciPath("InvaliDdevice");
uint64_t size = 0;
EXPECT_FALSE(drm.getDeviceMemoryPhysicalSizeInBytes(0, size));
}
TEST(DrmTest, GivenInValidSysfsNodeWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenReturnSuccess) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
drm.setPciPath("device");
VariableBackup<decltype(SysCalls::sysCallsOpen)> mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
return -1;
});
uint64_t size = 0;
EXPECT_FALSE(drm.getDeviceMemoryPhysicalSizeInBytes(0, size));
}
TEST(DrmTest, GivenSysfsNodeReadFailsWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenReturnError) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
drm.setPciPath("device");
VariableBackup<decltype(SysCalls::sysCallsOpen)> mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
return 1;
});
VariableBackup<decltype(SysCalls::sysCallsPread)> mockPread(&SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
const std::string testData("800");
memcpy(buf, testData.data(), testData.length() + 1);
return 0;
});
uint64_t size = 0;
EXPECT_FALSE(drm.getDeviceMemoryPhysicalSizeInBytes(0, size));
}
TEST(DrmTest, givenSysfsNodeReadFailsWithErrnoWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenReturnError) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
drm.setPciPath("device");
VariableBackup<decltype(SysCalls::sysCallsOpen)> mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
return 1;
});
VariableBackup<decltype(SysCalls::sysCallsPread)> mockPread(&SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
const std::string testData("800");
memcpy(buf, testData.data(), testData.length() + 1);
errno = 1;
return 4;
});
uint64_t size = 0;
EXPECT_FALSE(drm.getDeviceMemoryPhysicalSizeInBytes(0, size));
}
TEST(DrmTest, givenSysfsNodeReadFailsWithImproperDataWhenGetDeviceMemoryPhysicalSizeInBytesIsCalledThenReturnError) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};
drm.setPciPath("device");
VariableBackup<decltype(SysCalls::sysCallsOpen)> mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int {
return 1;
});
VariableBackup<decltype(SysCalls::sysCallsPread)> mockPread(&SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t {
const std::string testData("pqr");
memcpy(buf, testData.data(), testData.length() + 1);
return 4;
});
uint64_t size = 0;
EXPECT_FALSE(drm.getDeviceMemoryPhysicalSizeInBytes(0, size));
}
TEST(DrmTest, givenSysfsNodeReadFailsWithErrnoWhenGetDeviceMemoryMaxClockRateInMhzIsCalledThenReturnError) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]};