From 83b099a48c07a0c787f6895a5ced4c2256c94a82 Mon Sep 17 00:00:00 2001 From: Artur Harasimiuk Date: Thu, 21 Oct 2021 16:09:45 +0000 Subject: [PATCH] improve pci path search to use regex to make it more flexible Related-To: NEO-6364 Signed-off-by: Artur Harasimiuk --- .../test/unit_test/linux/main_linux_dll.cpp | 60 +++++++++---------- opencl/test/unit_test/linux/mock_os_layer.cpp | 7 ++- .../linux/by-path/pci-0000-device-card | 0 .../linux/by-path/pci-0000-device-render | 0 shared/source/os_interface/linux/drm_neo.cpp | 12 ++-- .../test/common/libult/linux/CMakeLists.txt | 3 + .../common/libult/linux/directory_linux.cpp | 28 +++++++++ .../os_interface/linux/drm_neo_create.cpp | 6 +- .../linux/sys_calls_linux_ult.cpp | 3 +- 9 files changed, 79 insertions(+), 40 deletions(-) delete mode 100644 opencl/test/unit_test/test_files/linux/by-path/pci-0000-device-card delete mode 100644 opencl/test/unit_test/test_files/linux/by-path/pci-0000-device-render create mode 100644 shared/test/common/libult/linux/directory_linux.cpp diff --git a/opencl/test/unit_test/linux/main_linux_dll.cpp b/opencl/test/unit_test/linux/main_linux_dll.cpp index 39dd64793c..36792af854 100644 --- a/opencl/test/unit_test/linux/main_linux_dll.cpp +++ b/opencl/test/unit_test/linux/main_linux_dll.cpp @@ -124,7 +124,7 @@ TEST_F(DrmSimpleTests, GivenSelectedNotExistingDeviceWhenGetDeviceFdThenFail) { TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceWhenGetDeviceFdThenReturnFd) { DebugManagerStateRestore stateRestore; - DebugManager.flags.ForceDeviceId.set("1234"); + DebugManager.flags.ForceDeviceId.set("0000:00:02.0"); VariableBackup backupOpenFull(&openFull); openRetVal = 1023; // fakeFd openFull = testOpen; @@ -147,16 +147,16 @@ TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceWhenOpenDirSuccedsThenHwDevice auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); EXPECT_EQ(1u, hwDeviceIds.size()); EXPECT_NE(nullptr, hwDeviceIds[0].get()); - EXPECT_STREQ("0000:test1", hwDeviceIds[0]->as()->getPciPath()); + EXPECT_STREQ("0000:00:03.1", hwDeviceIds[0]->as()->getPciPath()); entryIndex = 0; openCounter = 2; hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); EXPECT_EQ(2u, hwDeviceIds.size()); EXPECT_NE(nullptr, hwDeviceIds[0].get()); - EXPECT_STREQ("0000:test1", hwDeviceIds[0]->as()->getPciPath()); + EXPECT_STREQ("0000:00:03.1", hwDeviceIds[0]->as()->getPciPath()); EXPECT_NE(nullptr, hwDeviceIds[1].get()); - EXPECT_STREQ("0000:test2", hwDeviceIds[1]->as()->getPciPath()); + EXPECT_STREQ("0000:00:02.0", hwDeviceIds[1]->as()->getPciPath()); } TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceWhenOpenDirFailsThenRetryOpeningRenderDevices) { @@ -368,12 +368,12 @@ TEST_F(DrmSimpleTests, GivenMultipleAvailableDevicesWhenCreateMultipleRootDevice openCounter = 4; auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment); - EXPECT_STREQ("/dev/dri/by-path/pci-0000:test2-render", lastOpenedPath.c_str()); + EXPECT_STREQ("/dev/dri/by-path/platform-4010000000.pcie-pci-0000:00:02.0-render", lastOpenedPath.c_str()); EXPECT_EQ(requestedNumRootDevices, hwDeviceIds.size()); EXPECT_NE(nullptr, hwDeviceIds[0].get()); - EXPECT_STREQ("0000:test1", hwDeviceIds[0]->as()->getPciPath()); + EXPECT_STREQ("0000:00:03.1", hwDeviceIds[0]->as()->getPciPath()); EXPECT_NE(nullptr, hwDeviceIds[1].get()); - EXPECT_STREQ("0000:test2", hwDeviceIds[1]->as()->getPciPath()); + EXPECT_STREQ("0000:00:02.0", hwDeviceIds[1]->as()->getPciPath()); } TEST_F(DrmSimpleTests, GivenSelectedIncorectDeviceWhenGetDeviceFdThenFail) { @@ -805,48 +805,46 @@ TEST(PlatformsDestructor, whenGlobalPlatformsDestructorIsCalledThenGlobalPlatfor platformsImpl = new std::vector>; } -TEST_F(DrmTests, givenInvalidPciPathThenPciBusInfoIsNotAvailable) { - VariableBackup backupOpenFull(&openFull); - VariableBackup backupOpenDir(&failOnOpenDir, false); - VariableBackup backupEntryIndex(&entryIndex, 0u); - - openFull = openWithCounter; - entryIndex = 1; - openCounter = 1; - - const uint32_t invVal = PhysicalDevicePciBusInfo::InvalidValue; - - auto drm = DrmWrap::createDrm(*rootDeviceEnvironment); - ASSERT_NE(drm, nullptr); - EXPECT_EQ(drm->getPciBusInfo().pciDomain, invVal); - EXPECT_EQ(drm->getPciBusInfo().pciBus, invVal); - EXPECT_EQ(drm->getPciBusInfo().pciDevice, invVal); - EXPECT_EQ(drm->getPciBusInfo().pciFunction, invVal); -} - TEST_F(DrmTests, givenValidPciPathThenPciBusInfoIsAvailable) { VariableBackup backupOpenFull(&openFull); VariableBackup backupOpenDir(&failOnOpenDir, false); VariableBackup backupEntryIndex(&entryIndex, 0u); openFull = openWithCounter; - entryIndex = 4; + entryIndex = 1; openCounter = 2; auto drm = DrmWrap::createDrm(*rootDeviceEnvironment); ASSERT_NE(drm, nullptr); EXPECT_EQ(drm->getPciBusInfo().pciDomain, 0u); EXPECT_EQ(drm->getPciBusInfo().pciBus, 0u); - EXPECT_EQ(drm->getPciBusInfo().pciDevice, 2u); + EXPECT_EQ(drm->getPciBusInfo().pciDevice, 3u); EXPECT_EQ(drm->getPciBusInfo().pciFunction, 1u); - entryIndex = 5; + entryIndex = 2; openCounter = 1; drm = DrmWrap::createDrm(*rootDeviceEnvironment); ASSERT_NE(drm, nullptr); EXPECT_EQ(drm->getPciBusInfo().pciDomain, 0u); - EXPECT_EQ(drm->getPciBusInfo().pciBus, 3u); - EXPECT_EQ(drm->getPciBusInfo().pciDevice, 0u); + EXPECT_EQ(drm->getPciBusInfo().pciBus, 0u); + EXPECT_EQ(drm->getPciBusInfo().pciDevice, 2u); EXPECT_EQ(drm->getPciBusInfo().pciFunction, 0u); + + uint32_t referenceData[4][4] = { + {0x0a00, 0x00, 0x03, 0x1}, + {0x0000, 0xb3, 0x03, 0x1}, + {0x0000, 0x00, 0xb3, 0x1}, + {0x0000, 0x00, 0x03, 0xa}}; + for (uint32_t idx = 7; idx < 11; idx++) { + entryIndex = idx; + openCounter = 1; + drm = DrmWrap::createDrm(*rootDeviceEnvironment); + ASSERT_NE(drm, nullptr); + + EXPECT_EQ(drm->getPciBusInfo().pciDomain, referenceData[idx - 7][0]); + EXPECT_EQ(drm->getPciBusInfo().pciBus, referenceData[idx - 7][1]); + EXPECT_EQ(drm->getPciBusInfo().pciDevice, referenceData[idx - 7][2]); + EXPECT_EQ(drm->getPciBusInfo().pciFunction, referenceData[idx - 7][3]); + } } diff --git a/opencl/test/unit_test/linux/mock_os_layer.cpp b/opencl/test/unit_test/linux/mock_os_layer.cpp index 98b1fd2107..7a6ba940de 100644 --- a/opencl/test/unit_test/linux/mock_os_layer.cpp +++ b/opencl/test/unit_test/linux/mock_os_layer.cpp @@ -105,11 +105,16 @@ int closedir(DIR *dirp) { struct dirent entries[] = { {0, 0, 0, 0, "."}, + {0, 0, 0, 0, "pci-0000:00:03.1-render"}, + {0, 0, 0, 0, "platform-4010000000.pcie-pci-0000:00:02.0-render"}, {0, 0, 0, 0, "pci-0000:test1-render"}, {0, 0, 0, 0, "pci-0000:test2-render"}, {0, 0, 0, 0, "pci-0000:1234-render"}, - {0, 0, 0, 0, "pci-0000:0:2.1-render"}, {0, 0, 0, 0, "pci-0000:3:0.0-render"}, + {0, 0, 0, 0, "pci-0a00:00:03.1-render"}, + {0, 0, 0, 0, "pci-0000:b3:03.1-render"}, + {0, 0, 0, 0, "pci-0000:00:b3.1-render"}, + {0, 0, 0, 0, "pci-0000:00:03.a-render"}, }; uint32_t entryIndex = 0u; diff --git a/opencl/test/unit_test/test_files/linux/by-path/pci-0000-device-card b/opencl/test/unit_test/test_files/linux/by-path/pci-0000-device-card deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/opencl/test/unit_test/test_files/linux/by-path/pci-0000-device-render b/opencl/test/unit_test/test_files/linux/by-path/pci-0000-device-render deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/shared/source/os_interface/linux/drm_neo.cpp b/shared/source/os_interface/linux/drm_neo.cpp index 6f4d631d1c..229b609f0c 100644 --- a/shared/source/os_interface/linux/drm_neo.cpp +++ b/shared/source/os_interface/linux/drm_neo.cpp @@ -33,6 +33,7 @@ #include #include #include +#include namespace NEO { @@ -514,8 +515,6 @@ void appendHwDeviceId(std::vector> &hwDeviceIds, int std::vector> Drm::discoverDevices(ExecutionEnvironment &executionEnvironment) { std::vector> hwDeviceIds; executionEnvironment.osEnvironment = std::make_unique(); - std::string devicePrefix = std::string(Os::pciDevicesDirectory) + "/pci-"; - const char *renderDeviceSuffix = "-render"; size_t numRootDevices = 0u; if (DebugManager.flags.CreateMultipleRootDevices.get()) { numRootDevices = DebugManager.flags.CreateMultipleRootDevices.get(); @@ -543,11 +542,14 @@ std::vector> Drm::discoverDevices(ExecutionEnvironme } do { + const std::regex renderRegex(".*([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]\\.[0-9a-fA-F])-render$"); for (std::vector::iterator file = files.begin(); file != files.end(); ++file) { - if (file->find(renderDeviceSuffix) == std::string::npos) { + std::smatch fnMatch; + auto reMatch = std::regex_match(*file, fnMatch, renderRegex); + if (!reMatch) { continue; } - std::string pciPath = file->substr(devicePrefix.size(), file->size() - devicePrefix.size() - strlen(renderDeviceSuffix)); + std::string pciPath = fnMatch[1]; if (DebugManager.flags.ForceDeviceId.get() != "unk") { if (file->find(DebugManager.flags.ForceDeviceId.get().c_str()) == std::string::npos) { @@ -729,7 +731,7 @@ PhysicalDevicePciBusInfo Drm::getPciBusInfo() const { PhysicalDevicePciBusInfo pciBusInfo(PhysicalDevicePciBusInfo::InvalidValue, PhysicalDevicePciBusInfo::InvalidValue, PhysicalDevicePciBusInfo::InvalidValue, PhysicalDevicePciBusInfo::InvalidValue); if (adapterBDF.Data != std::numeric_limits::max()) { - pciBusInfo.pciDomain = 0; + pciBusInfo.pciDomain = this->pciDomain; pciBusInfo.pciBus = adapterBDF.Bus; pciBusInfo.pciDevice = adapterBDF.Device; pciBusInfo.pciFunction = adapterBDF.Function; diff --git a/shared/test/common/libult/linux/CMakeLists.txt b/shared/test/common/libult/linux/CMakeLists.txt index 601f575aa8..6e90233cff 100644 --- a/shared/test/common/libult/linux/CMakeLists.txt +++ b/shared/test/common/libult/linux/CMakeLists.txt @@ -10,6 +10,9 @@ if(UNIX) ${CMAKE_CURRENT_SOURCE_DIR}/drm_mock.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drm_mock.h ) + target_sources(neo_libult PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/directory_linux.cpp + ) add_subdirectories() endif() diff --git a/shared/test/common/libult/linux/directory_linux.cpp b/shared/test/common/libult/linux/directory_linux.cpp new file mode 100644 index 0000000000..e0d251cf5d --- /dev/null +++ b/shared/test/common/libult/linux/directory_linux.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2021 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "shared/source/utilities/directory.h" + +#include +#include + +namespace NEO { + +std::vector Directory::getFiles(const std::string &path) { + std::vector files; + if (path == "./test_files/linux/by-path") { + files.push_back("./test_files/linux/by-path/pci-0000:00:02.0-card"); + files.push_back("./test_files/linux/by-path/pci-0000:00:02.0-render"); + return files; + } + if (path == "./test_files/linux/devices/device/drm") { + files.push_back("./test_files/linux/devices/device/drm/card1"); + return files; + } + return files; +} +}; // namespace NEO diff --git a/shared/test/common/os_interface/linux/drm_neo_create.cpp b/shared/test/common/os_interface/linux/drm_neo_create.cpp index 9dd3b7f876..181bc7c781 100644 --- a/shared/test/common/os_interface/linux/drm_neo_create.cpp +++ b/shared/test/common/os_interface/linux/drm_neo_create.cpp @@ -17,13 +17,15 @@ namespace NEO { class DrmMockDefault : public DrmMock { public: - DrmMockDefault(RootDeviceEnvironment &rootDeviceEnvironment) : DrmMock(rootDeviceEnvironment) { + DrmMockDefault(std::unique_ptr &&hwDeviceIdIn, RootDeviceEnvironment &rootDeviceEnvironment) : DrmMock(rootDeviceEnvironment) { storedRetVal = 0; storedRetValForDeviceID = 0; storedRetValForDeviceRevID = 0; storedRetValForPooledEU = 0; storedRetValForMinEUinPool = 0; setGtType(GTTYPE_GT1); + if (hwDeviceIdIn != nullptr) + this->hwDeviceId = std::move(hwDeviceIdIn); } }; @@ -35,7 +37,7 @@ Drm *Drm::create(std::unique_ptr &&hwDeviceId, RootDeviceEnvironm if (pDrmToReturnFromCreateFunc) { return *pDrmToReturnFromCreateFunc; } - auto drm = new DrmMockDefault(rootDeviceEnvironment); + auto drm = new DrmMockDefault(std::move(hwDeviceId), rootDeviceEnvironment); const HardwareInfo *hwInfo = rootDeviceEnvironment.getHardwareInfo(); 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 7181d3d51c..e303a80f69 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 @@ -54,9 +54,10 @@ int open(const char *file, int flags) { if (strcmp(file, "/dev/dri/by-path/pci-0000:invalid-render") == 0) { return 0; } - if (strcmp(file, "./test_files/linux/by-path/pci-0000-device-render") == 0) { + if (strcmp(file, "./test_files/linux/by-path/pci-0000:00:02.0-render") == 0) { return fakeFileDescriptor; } + return 0; }