Skip to content

Commit 1671e0a

Browse files
Add new debug flags for filtering devices to open
FilterBdfPath is used only on Linux as a filter for BDF when opening from /dev/dri/by-path FilterDeviceId is used on both OSes as a filter for device id Signed-off-by: Mateusz Jablonski <[email protected]>
1 parent 36f8f67 commit 1671e0a

File tree

11 files changed

+127
-30
lines changed

11 files changed

+127
-30
lines changed

opencl/test/unit_test/linux/main_linux_dll.cpp

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,51 @@ TEST_F(DrmSimpleTests, GivenTwoOpenableDevicesWhenDiscoverDevicesThenCreateTwoHw
114114
EXPECT_EQ(2u, hwDeviceIds.size());
115115
}
116116

117-
TEST_F(DrmSimpleTests, GivenSelectedNotExistingDeviceWhenGetDeviceFdThenFail) {
117+
TEST_F(DrmSimpleTests, GivenSelectedNotExistingDeviceUsingForceDeviceIdFlagWhenGetDeviceFdThenFail) {
118118
DebugManagerStateRestore stateRestore;
119-
DebugManager.flags.ForceDeviceId.set("1234");
120-
VariableBackup<decltype(openFull)> backupOpenFull(&openFull);
121-
openFull = testOpen;
122-
openRetVal = -1;
119+
DebugManager.flags.ForceDeviceId.set("invalid");
120+
openFull = nullptr; // open shouldn't be called
123121
ExecutionEnvironment executionEnvironment;
124122
auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment);
125123
EXPECT_TRUE(hwDeviceIds.empty());
126124
}
127125

128-
TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceWhenGetDeviceFdThenReturnFd) {
126+
TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceUsingForceDeviceIdFlagWhenGetDeviceFdThenReturnFd) {
129127
DebugManagerStateRestore stateRestore;
130128
DebugManager.flags.ForceDeviceId.set("0000:00:02.0");
131129
VariableBackup<decltype(openFull)> backupOpenFull(&openFull);
132-
openRetVal = 1023; // fakeFd
133-
openFull = testOpen;
130+
openFull = openWithCounter;
131+
openCounter = 10;
132+
ExecutionEnvironment executionEnvironment;
133+
auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment);
134+
EXPECT_EQ(1u, hwDeviceIds.size());
135+
EXPECT_NE(nullptr, hwDeviceIds[0].get());
136+
EXPECT_STREQ("/dev/dri/by-path/platform-4010000000.pcie-pci-0000:00:02.0-render", lastOpenedPath.c_str());
137+
EXPECT_EQ(9, openCounter); // only one opened file
138+
}
139+
140+
TEST_F(DrmSimpleTests, GivenSelectedNotExistingDeviceUsingFilterBdfWhenGetDeviceFdThenFail) {
141+
DebugManagerStateRestore stateRestore;
142+
DebugManager.flags.FilterBdfPath.set("invalid");
143+
VariableBackup<decltype(openFull)> backupOpenFull(&openFull);
144+
openFull = nullptr; // open shouldn't be called
145+
ExecutionEnvironment executionEnvironment;
146+
auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment);
147+
EXPECT_TRUE(hwDeviceIds.empty());
148+
}
149+
150+
TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceUsingFilterBdfWhenGetDeviceFdThenReturnFd) {
151+
DebugManagerStateRestore stateRestore;
152+
DebugManager.flags.FilterBdfPath.set("0000:00:02.0");
153+
VariableBackup<decltype(openFull)> backupOpenFull(&openFull);
154+
openFull = openWithCounter;
155+
openCounter = 10;
134156
ExecutionEnvironment executionEnvironment;
135157
auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment);
136158
EXPECT_EQ(1u, hwDeviceIds.size());
137159
EXPECT_NE(nullptr, hwDeviceIds[0].get());
160+
EXPECT_STREQ("/dev/dri/by-path/platform-4010000000.pcie-pci-0000:00:02.0-render", lastOpenedPath.c_str());
161+
EXPECT_EQ(9, openCounter); // only one opened file
138162
}
139163

140164
TEST_F(DrmSimpleTests, GivenSelectedExistingDeviceWhenOpenDirSuccedsThenHwDeviceIdsHaveProperPciPaths) {
@@ -405,16 +429,21 @@ TEST_F(DrmSimpleTests, GivenMultipleAvailableDevicesWhenCreateMultipleRootDevice
405429
EXPECT_STREQ("0000:00:02.0", hwDeviceIds[1]->as<HwDeviceIdDrm>()->getPciPath());
406430
}
407431

408-
TEST_F(DrmSimpleTests, GivenSelectedIncorectDeviceWhenGetDeviceFdThenFail) {
432+
TEST_F(DrmTests, GivenSelectedIncorectDeviceByDeviceIdWhenGetDeviceFdThenFail) {
409433
DebugManagerStateRestore stateRestore;
410-
DebugManager.flags.ForceDeviceId.set("1234");
411-
VariableBackup<decltype(openFull)> backupOpenFull(&openFull);
412-
openFull = testOpen;
413-
openRetVal = 1024;
414-
ExecutionEnvironment executionEnvironment;
434+
DebugManager.flags.FilterDeviceId.set("invalid");
435+
auto drm1 = DrmWrap::createDrm(*rootDeviceEnvironment);
436+
EXPECT_EQ(drm1, nullptr);
437+
}
415438

416-
auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment);
417-
EXPECT_TRUE(hwDeviceIds.empty());
439+
TEST_F(DrmTests, GivenSelectedCorrectDeviceByDeviceIdWhenGetDeviceFdThenSucceed) {
440+
DebugManagerStateRestore stateRestore;
441+
std::stringstream deviceIdStr;
442+
deviceIdStr << std::hex << deviceId;
443+
444+
DebugManager.flags.FilterDeviceId.set(deviceIdStr.str());
445+
auto drm1 = DrmWrap::createDrm(*rootDeviceEnvironment);
446+
EXPECT_NE(drm1, nullptr);
418447
}
419448

420449
TEST_F(DrmSimpleTests, givenUseVmBindFlagWhenOverrideBindSupportThenReturnProperValue) {

opencl/test/unit_test/os_interface/device_factory_tests.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2021 Intel Corporation
2+
* Copyright (C) 2018-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -377,6 +377,25 @@ TEST(DiscoverDevices, whenDiscoverDevicesAndForceDeviceIdIsDifferentFromTheExist
377377
EXPECT_FALSE(result);
378378
}
379379

380+
TEST(DiscoverDevices, whenDiscoverDevicesAndFilterDifferentFromTheExistingDeviceThenReturnNullptr) {
381+
DebugManagerStateRestore stateRestore;
382+
DebugManager.flags.FilterDeviceId.set("invalid");
383+
DebugManager.flags.FilterBdfPath.set("invalid");
384+
ExecutionEnvironment executionEnviornment;
385+
auto hwDeviceIds = OSInterface::discoverDevices(executionEnviornment);
386+
EXPECT_TRUE(hwDeviceIds.empty());
387+
}
388+
389+
TEST(DiscoverDevices, whenDiscoverDevicesAndFilterDifferentFromTheExistingDeviceThenPrepareDeviceEnvironmentsReturnsFalse) {
390+
DebugManagerStateRestore stateRestore;
391+
DebugManager.flags.FilterDeviceId.set("invalid");
392+
DebugManager.flags.FilterBdfPath.set("invalid");
393+
ExecutionEnvironment executionEnviornment;
394+
395+
auto result = DeviceFactory::prepareDeviceEnvironments(executionEnviornment);
396+
EXPECT_FALSE(result);
397+
}
398+
380399
using UltDeviceFactoryTest = DeviceFactoryTest;
381400

382401
TEST_F(UltDeviceFactoryTest, givenExecutionEnvironmentWhenCreatingUltDeviceFactoryThenMockMemoryManagerIsAllocated) {

opencl/test/unit_test/os_interface/windows/wddm20_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,15 @@ TEST(WddmGfxPartitionTests, givenInternalFrontWindowHeapWhenAllocatingSmallOrBig
12321232
}
12331233
}
12341234

1235+
TEST_F(Wddm20Tests, givenWddmWhenDiscoverDevicesAndFilterDeviceIdIsTheSameAsTheExistingDeviceThenReturnTheAdapter) {
1236+
DebugManagerStateRestore stateRestore;
1237+
DebugManager.flags.FilterDeviceId.set("1234"); // Existing device Id
1238+
ExecutionEnvironment executionEnvironment;
1239+
auto hwDeviceIds = OSInterface::discoverDevices(executionEnvironment);
1240+
EXPECT_EQ(1u, hwDeviceIds.size());
1241+
EXPECT_NE(nullptr, hwDeviceIds[0].get());
1242+
}
1243+
12351244
TEST_F(Wddm20Tests, givenWddmWhenDiscoverDevicesAndForceDeviceIdIsTheSameAsTheExistingDeviceThenReturnTheAdapter) {
12361245
DebugManagerStateRestore stateRestore;
12371246
DebugManager.flags.ForceDeviceId.set("1234"); // Existing device Id

opencl/test/unit_test/test_files/igdrcl.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ GenerateAubFilePerProcessId = 0
3232
EnableSWTags = 0
3333
DumpSWTagsBXML = 0
3434
ForceDeviceId = unk
35+
FilterDeviceId = unk
36+
FilterBdfPath = unk
3537
LoadBinarySipFromFile = unk
3638
InjectInternalBuildOptions = unk
3739
OverrideCsrAllocationSize = -1

shared/source/debug_settings/debug_variables_base.inl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ DECLARE_DEBUG_VARIABLE(bool, AllowPatchingVfeStateInCommandLists, false, "true:
7575
DECLARE_DEBUG_VARIABLE(bool, PrintMemoryRegionSizes, false, "print memory bank type, instance and it's size")
7676
DECLARE_DEBUG_VARIABLE(bool, UpdateCrossThreadDataSize, false, "Turn on cross thread data size calculation for PATCH TOKEN binary")
7777
DECLARE_DEBUG_VARIABLE(std::string, ForceDeviceId, std::string("unk"), "DeviceId selected for testing")
78+
DECLARE_DEBUG_VARIABLE(std::string, FilterDeviceId, std::string("unk"), "Device id filter, adapter matching device id will be opened. Ignored when unk.")
79+
DECLARE_DEBUG_VARIABLE(std::string, FilterBdfPath, std::string("unk"), "Linux-only, BDF path filter, only matching paths will be opened. Ignored when unk.")
7880
DECLARE_DEBUG_VARIABLE(std::string, LoadBinarySipFromFile, std::string("unk"), "Select binary file to load SIP kernel raw binary. When file named *_header.* exists, it is used as header")
7981
DECLARE_DEBUG_VARIABLE(std::string, InjectInternalBuildOptions, std::string("unk"), "Appends internal build options string to user modules")
8082
DECLARE_DEBUG_VARIABLE(int64_t, OverrideMultiStoragePlacement, -1, "-1: disable, 0+: tile mask, each bit corresponds to tile")

shared/source/dll/linux/drm_neo_create.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "shared/source/gmm_helper/gmm_helper.h"
1212
#include "shared/source/helpers/hw_helper.h"
1313
#include "shared/source/helpers/hw_info.h"
14+
#include "shared/source/os_interface/device_factory.h"
1415
#include "shared/source/os_interface/linux/drm_neo.h"
1516
#include "shared/source/os_interface/linux/drm_null_device.h"
1617

@@ -45,6 +46,9 @@ Drm *Drm::create(std::unique_ptr<HwDeviceIdDrm> &&hwDeviceId, RootDeviceEnvironm
4546
printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "FATAL: Cannot query device ID parameter!\n");
4647
return nullptr;
4748
}
49+
if (!DeviceFactory::isAllowedDeviceId(drmObject->deviceId, DebugManager.flags.FilterDeviceId.get())) {
50+
return nullptr;
51+
}
4852

4953
// Get HW Revision (I915_drm.h)
5054
ret = drmObject->getDeviceRevID(drmObject->revisionId);

shared/source/os_interface/device_factory.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2021 Intel Corporation
2+
* Copyright (C) 2018-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -206,4 +206,12 @@ bool (*DeviceFactory::createMemoryManagerFunc)(ExecutionEnvironment &) = [](Exec
206206
return executionEnvironment.initializeMemoryManager();
207207
};
208208

209+
bool DeviceFactory::isAllowedDeviceId(uint32_t deviceId, const std::string &deviceIdString) {
210+
if (deviceIdString != "unk") {
211+
char *endptr = nullptr;
212+
auto reqDeviceId = strtoul(deviceIdString.c_str(), &endptr, 16);
213+
return (static_cast<uint32_t>(reqDeviceId) == deviceId);
214+
}
215+
return true;
216+
}
209217
} // namespace NEO

shared/source/os_interface/device_factory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2021 Intel Corporation
2+
* Copyright (C) 2018-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -28,5 +28,6 @@ class DeviceFactory {
2828

2929
static std::unique_ptr<Device> (*createRootDeviceFunc)(ExecutionEnvironment &executionEnvironment, uint32_t rootDeviceIndex);
3030
static bool (*createMemoryManagerFunc)(ExecutionEnvironment &executionEnvironment);
31+
static bool isAllowedDeviceId(uint32_t deviceId, const std::string &deviceIdString);
3132
};
3233
} // namespace NEO

shared/source/os_interface/linux/drm_neo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ std::vector<std::unique_ptr<HwDeviceId>> Drm::discoverDevices(ExecutionEnvironme
597597
}
598598
}
599599

600+
if (DebugManager.flags.FilterBdfPath.get() != "unk") {
601+
if (devicePathView.find(DebugManager.flags.FilterBdfPath.get().c_str()) == std::string::npos) {
602+
continue;
603+
}
604+
}
600605
if (DebugManager.flags.ForceDeviceId.get() != "unk") {
601606
if (devicePathView.find(DebugManager.flags.ForceDeviceId.get().c_str()) == std::string::npos) {
602607
continue;

shared/source/os_interface/windows/wddm/adapter_factory.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 Intel Corporation
2+
* Copyright (C) 2021-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -9,6 +9,7 @@
99

1010
#include "shared/source/debug_settings/debug_settings_manager.h"
1111
#include "shared/source/helpers/debug_helpers.h"
12+
#include "shared/source/os_interface/device_factory.h"
1213
#include "shared/source/utilities/stackvec.h"
1314

1415
#include <cstdlib>
@@ -24,13 +25,8 @@ bool canUseAdapterBasedOnDriverDesc(const char *driverDescription) {
2425
}
2526

2627
bool isAllowedDeviceId(uint32_t deviceId) {
27-
if (DebugManager.flags.ForceDeviceId.get() == "unk") {
28-
return true;
29-
}
30-
31-
char *endptr = nullptr;
32-
auto reqDeviceId = strtoul(DebugManager.flags.ForceDeviceId.get().c_str(), &endptr, 16);
33-
return (static_cast<uint32_t>(reqDeviceId) == deviceId);
28+
return DeviceFactory::isAllowedDeviceId(deviceId, DebugManager.flags.FilterDeviceId.get()) &&
29+
DeviceFactory::isAllowedDeviceId(deviceId, DebugManager.flags.ForceDeviceId.get());
3430
}
3531

3632
} // namespace NEO

0 commit comments

Comments
 (0)