Unify reading pci paths

Signed-off-by: Kamil Diedrich <kamil.diedrich@intel.com>
This commit is contained in:
Kamil Diedrich
2021-03-29 13:43:50 +02:00
committed by Compute-Runtime-Automation
parent cee785f8a1
commit 7d64d8e00e
8 changed files with 63 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Intel Corporation
* Copyright (C) 2020-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -8,7 +8,6 @@
#include "shared/source/device/device.h"
#include "shared/source/helpers/hw_helper.h"
#include "shared/source/kernel/debug_data.h"
#include "shared/source/os_interface/linux/os_interface.h"
#include "level_zero/core/source/debugger/debugger_l0.h"

View File

@@ -5,7 +5,6 @@
*
*/
#include "shared/source/os_interface/linux/os_interface.h"
#include "shared/test/common/mocks/mock_device.h"
#include "test.h"

View File

@@ -8,7 +8,6 @@
#include "shared/source/device/device.h"
#include "shared/source/os_interface/linux/drm_neo.h"
#include "shared/source/os_interface/linux/os_interface.h"
#include "shared/source/os_interface/linux/sys_calls.h"
#include "opencl/source/cl_device/cl_device.h"
#include "opencl/source/platform/platform.h"
@@ -28,35 +27,19 @@ ClDevice *VADevice::getRootDeviceFromVaDisplay(Platform *pPlatform, VADisplay va
UNRECOVERABLE_IF(deviceFd < 0);
char path[256] = {0};
size_t pathlen = 256;
auto devicePath = OSInterface::OSInterfaceImpl::getPciPath(deviceFd);
if (SysCalls::getDevicePath(deviceFd, path, pathlen)) {
if (devicePath == std::nullopt) {
return nullptr;
}
if (SysCalls::access(path, F_OK)) {
return nullptr;
}
int readLinkSize = 0;
char devicePath[256] = {0};
readLinkSize = SysCalls::readlink(path, devicePath, pathlen);
if (readLinkSize == -1) {
return nullptr;
}
std::string_view devicePathView(devicePath, static_cast<size_t>(readLinkSize));
devicePathView = devicePathView.substr(devicePathView.find("/drm/render") - 7u, 7u);
for (size_t i = 0; i < pPlatform->getNumDevices(); ++i) {
auto device = pPlatform->getClDevice(i);
NEO::Device *neoDevice = &device->getDevice();
auto *drm = neoDevice->getRootDeviceEnvironment().osInterface->get()->getDrm();
auto pciPath = drm->getPciPath();
if (devicePathView == pciPath) {
if (devicePath == pciPath) {
return device;
}
}

View File

@@ -135,6 +135,7 @@ TEST(DrmTest, GivenSelectedExistingDeviceWhenOpenDirSuccedsThenHwDeviceIdsHavePr
TEST(DrmTest, GivenSelectedExistingDeviceWhenOpenDirFailsThenRetryOpeningRenderDevices) {
VariableBackup<decltype(openFull)> backupOpenFull(&openFull);
VariableBackup<decltype(failOnOpenDir)> backupOpenDir(&failOnOpenDir, true);
VariableBackup<decltype(readLinkCalledTimes)> backupReadlink(&readLinkCalledTimes, 0);
openFull = openWithCounter;
openCounter = 1;
@@ -152,7 +153,7 @@ TEST(DrmTest, GivenSelectedExistingDeviceWhenOpenDirFailsThenRetryOpeningRenderD
EXPECT_NE(nullptr, hwDeviceIds[0].get());
EXPECT_STREQ("00:02.0", hwDeviceIds[0]->getPciPath());
EXPECT_NE(nullptr, hwDeviceIds[1].get());
EXPECT_STREQ("00:02.0", hwDeviceIds[1]->getPciPath());
EXPECT_STREQ("00:03.0", hwDeviceIds[1]->getPciPath());
}
TEST(DrmTest, GivenSelectedNonExistingDeviceWhenOpenDirFailsThenRetryOpeningRenderDevicesAndNoDevicesAreCreated) {
@@ -170,6 +171,7 @@ TEST(DrmTest, GivenFailingOpenDirAndMultipleAvailableDevicesWhenCreateMultipleRo
DebugManagerStateRestore stateRestore;
VariableBackup<decltype(openFull)> backupOpenFull(&openFull);
VariableBackup<decltype(failOnOpenDir)> backupOpenDir(&failOnOpenDir, true);
VariableBackup<decltype(readLinkCalledTimes)> backupReadlink(&readLinkCalledTimes, 0);
openFull = openWithCounter;
ExecutionEnvironment executionEnvironment;
const uint32_t requestedNumRootDevices = 2u;
@@ -182,7 +184,7 @@ TEST(DrmTest, GivenFailingOpenDirAndMultipleAvailableDevicesWhenCreateMultipleRo
EXPECT_NE(nullptr, hwDeviceIds[0].get());
EXPECT_STREQ("00:02.0", hwDeviceIds[0]->getPciPath());
EXPECT_NE(nullptr, hwDeviceIds[1].get());
EXPECT_STREQ("00:02.0", hwDeviceIds[1]->getPciPath());
EXPECT_STREQ("00:03.0", hwDeviceIds[1]->getPciPath());
}
TEST(DrmTest, GivenMultipleAvailableDevicesWhenCreateMultipleRootDevicesFlagIsSetThenTheFlagIsRespected) {

View File

@@ -7,6 +7,8 @@
#include "mock_os_layer.h"
#include "shared/source/helpers/string.h"
#include <cassert>
#include <dirent.h>
#include <iostream>
@@ -59,7 +61,16 @@ int access(const char *pathname, int mode) {
ssize_t readlink(const char *path, char *buf, size_t bufsiz) {
++readLinkCalledTimes;
return -1;
if (readLinkCalledTimes % 2 == 1) {
return -1;
}
constexpr size_t sizeofPath = sizeof("../../devices/pci0000:4a/0000:4a:02.0/0000:4b:00.0/0000:4c:01.0/0000:00:03.0/drm/renderD128");
strcpy_s(buf, sizeofPath, "../../devices/pci0000:4a/0000:4a:02.0/0000:4b:00.0/0000:4c:01.0/0000:00:03.0/drm/renderD128");
return sizeofPath;
}
int open(const char *pathname, int flags, ...) {

View File

@@ -17,6 +17,7 @@
#include "shared/source/helpers/ptr_math.h"
#include "shared/source/os_interface/linux/hw_device_id.h"
#include "shared/source/os_interface/linux/os_inc.h"
#include "shared/source/os_interface/linux/os_interface.h"
#include "shared/source/os_interface/linux/sys_calls.h"
#include "shared/source/os_interface/linux/system_info.h"
#include "shared/source/os_interface/os_environment.h"
@@ -362,7 +363,10 @@ std::vector<std::unique_ptr<HwDeviceId>> OSInterface::discoverDevices(ExecutionE
for (unsigned int i = 0; i < maxDrmDevices; i++) {
std::string path = std::string(pathPrefix) + std::to_string(i + startNum);
int fileDescriptor = SysCalls::open(path.c_str(), O_RDWR);
appendHwDeviceId(hwDeviceIds, fileDescriptor, "00:02.0");
auto pciPath = OSInterface::OSInterfaceImpl::getPciPath(fileDescriptor);
appendHwDeviceId(hwDeviceIds, fileDescriptor, pciPath.value_or("00:02.0").c_str());
if (!hwDeviceIds.empty() && hwDeviceIds.size() == numRootDevices) {
break;
}

View File

@@ -13,6 +13,13 @@
#include "shared/source/os_interface/hw_info_config.h"
#include "shared/source/os_interface/linux/drm_memory_operations_handler.h"
#include "shared/source/os_interface/linux/drm_neo.h"
#include "shared/source/os_interface/linux/sys_calls.h"
#include <optional>
#include <string_view>
#include <sys/stat.h>
#include <system_error>
#include <unistd.h>
namespace NEO {
@@ -69,4 +76,30 @@ bool RootDeviceEnvironment::initOsInterface(std::unique_ptr<HwDeviceId> &&hwDevi
memoryOperationsInterface = DrmMemoryOperationsHandler::create(*drm, rootDeviceIndex);
return true;
}
std::optional<std::string> OSInterface::OSInterfaceImpl::getPciPath(int deviceFd) {
char path[256] = {0};
size_t pathlen = 256;
if (SysCalls::getDevicePath(deviceFd, path, pathlen)) {
return std::nullopt;
}
if (SysCalls::access(path, F_OK)) {
return std::nullopt;
}
int readLinkSize = 0;
char devicePath[256] = {0};
readLinkSize = SysCalls::readlink(path, devicePath, pathlen);
if (readLinkSize == -1) {
return std::nullopt;
}
std::string_view devicePathView(devicePath, static_cast<size_t>(readLinkSize));
devicePathView = devicePathView.substr(devicePathView.find("/drm/render") - 7u, 7u);
return std::string(devicePathView);
}
} // namespace NEO

View File

@@ -6,9 +6,12 @@
*/
#pragma once
#include "shared/source/os_interface/os_interface.h"
#include <memory>
#include <optional>
#include <string>
namespace NEO {
class Drm;
@@ -24,6 +27,8 @@ class OSInterface::OSInterfaceImpl {
bool isDebugAttachAvailable() const;
static std::optional<std::string> getPciPath(int deviceFd);
protected:
std::unique_ptr<Drm> drm;
};