diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index a04ff3208f..7fd2b29e64 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -1250,13 +1250,19 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromExistingStorag } uint64_t DrmMemoryManager::getSystemSharedMemory(uint32_t rootDeviceIndex) { - uint64_t hostMemorySize = MemoryConstants::pageSize * (uint64_t)(sysconf(_SC_PHYS_PAGES)); + uint64_t hostMemorySize = MemoryConstants::pageSize * static_cast(SysCalls::sysconf(_SC_PHYS_PAGES)); uint64_t gpuMemorySize = 0u; [[maybe_unused]] auto ret = getDrm(rootDeviceIndex).queryGttSize(gpuMemorySize, false); DEBUG_BREAK_IF(ret != 0); + auto memoryInfo = getDrm(rootDeviceIndex).getMemoryInfo(); + if (memoryInfo) { + auto systemMemoryRegionSize = memoryInfo->getMemoryRegion(static_cast(systemMemoryBitfield.to_ulong())).probedSize; + gpuMemorySize = std::min(gpuMemorySize, systemMemoryRegionSize); + } + return std::min(hostMemorySize, gpuMemorySize); } diff --git a/shared/source/os_interface/linux/sys_calls.h b/shared/source/os_interface/linux/sys_calls.h index ad652fb90f..4e5e724d3f 100644 --- a/shared/source/os_interface/linux/sys_calls.h +++ b/shared/source/os_interface/linux/sys_calls.h @@ -53,5 +53,6 @@ DIR *opendir(const char *name); struct dirent *readdir(DIR *dir); int closedir(DIR *dir); off_t lseek(int fd, off_t offset, int whence) noexcept; +long sysconf(int name); } // namespace SysCalls } // namespace NEO diff --git a/shared/source/os_interface/linux/sys_calls_linux.cpp b/shared/source/os_interface/linux/sys_calls_linux.cpp index 343a242032..ab71610d1e 100644 --- a/shared/source/os_interface/linux/sys_calls_linux.cpp +++ b/shared/source/os_interface/linux/sys_calls_linux.cpp @@ -193,5 +193,8 @@ int closedir(DIR *dir) { off_t lseek(int fd, off_t offset, int whence) noexcept { return ::lseek(fd, offset, whence); } +long sysconf(int name) { + return ::sysconf(name); +} } // namespace SysCalls } // namespace NEO diff --git a/shared/test/common/os_interface/linux/sys_calls_linux_ult.cpp b/shared/test/common/os_interface/linux/sys_calls_linux_ult.cpp index 791c31a731..a8848607f1 100644 --- a/shared/test/common/os_interface/linux/sys_calls_linux_ult.cpp +++ b/shared/test/common/os_interface/linux/sys_calls_linux_ult.cpp @@ -107,6 +107,7 @@ int (*sysCallsClosedir)(DIR *dir) = nullptr; int (*sysCallsGetDevicePath)(int deviceFd, char *buf, size_t &bufSize) = nullptr; off_t lseekReturn = 4096u; std::atomic lseekCalledCount(0); +long sysconfReturn = 1ull << 30; int mkdir(const std::string &path) { if (sysCallsMkdir != nullptr) { @@ -474,6 +475,9 @@ off_t lseek(int fd, off_t offset, int whence) noexcept { lseekCalledCount++; return lseekReturn; } +long sysconf(int name) { + return sysconfReturn; +} } // namespace SysCalls } // namespace NEO diff --git a/shared/test/common/os_interface/linux/sys_calls_linux_ult.h b/shared/test/common/os_interface/linux/sys_calls_linux_ult.h index 168b02c225..e5c8706cab 100644 --- a/shared/test/common/os_interface/linux/sys_calls_linux_ult.h +++ b/shared/test/common/os_interface/linux/sys_calls_linux_ult.h @@ -83,5 +83,7 @@ extern uint32_t munmapFuncCalled; extern off_t lseekReturn; extern std::atomic lseekCalledCount; + +extern long sysconfReturn; } // namespace SysCalls } // namespace NEO diff --git a/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp b/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp index 6d77024cb3..13d30e3f1a 100644 --- a/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp @@ -1476,8 +1476,8 @@ TEST(DrmMemoryManagerTest2, WhenGetMinimumSystemSharedMemoryThenCorrectValueIsRe auto mock = executionEnvironment->rootDeviceEnvironments[i]->osInterface->getDriverModel()->as(); mock->callBaseQueryGttSize = true; - auto hostMemorySize = MemoryConstants::pageSize * (uint64_t)(sysconf(_SC_PHYS_PAGES)); - // gpuMemSize < hostMemSize + auto hostMemorySize = MemoryConstants::pageSize * SysCalls::sysconfReturn; + // gpuMemSize < hostMemSize, no memory info auto gpuMemorySize = hostMemorySize - 1u; mock->ioctlExpected.contextGetParam = 1; @@ -1490,7 +1490,7 @@ TEST(DrmMemoryManagerTest2, WhenGetMinimumSystemSharedMemoryThenCorrectValueIsRe mock->ioctlExpected.contextCreate = 0; mock->testIoctls(); - // gpuMemSize > hostMemSize + // gpuMemSize > hostMemSize, no memory info gpuMemorySize = hostMemorySize + 1u; mock->getContextParamRetValue = gpuMemorySize; systemSharedMemorySize = memoryManager->getSystemSharedMemory(i); @@ -1498,6 +1498,32 @@ TEST(DrmMemoryManagerTest2, WhenGetMinimumSystemSharedMemoryThenCorrectValueIsRe EXPECT_EQ(hostMemorySize, systemSharedMemorySize); mock->testIoctls(); + // gpuMemSize > hostMemSize > systemMemoryRegionSize + + MemoryRegion memoryRegion{}; + memoryRegion.probedSize = hostMemorySize - 2; + + MemoryInfo::RegionContainer regionInfo{}; + regionInfo.push_back(memoryRegion); + + mock->memoryInfo = std::make_unique(regionInfo, *mock); + + gpuMemorySize = hostMemorySize + 1u; + mock->getContextParamRetValue = gpuMemorySize; + systemSharedMemorySize = memoryManager->getSystemSharedMemory(i); + mock->ioctlExpected.contextGetParam = 3; + EXPECT_EQ(memoryRegion.probedSize, systemSharedMemorySize); + mock->testIoctls(); + + // hostMemSize > systemMemoryRegionSize > gpuMemSize + + gpuMemorySize = hostMemorySize - 3; + mock->getContextParamRetValue = gpuMemorySize; + systemSharedMemorySize = memoryManager->getSystemSharedMemory(i); + mock->ioctlExpected.contextGetParam = 4; + EXPECT_EQ(gpuMemorySize, systemSharedMemorySize); + mock->testIoctls(); + executionEnvironment->rootDeviceEnvironments[i]->osInterface.reset(); } }