From 47a2d309bbe5a3195154f111bd5f4a4785fcfa92 Mon Sep 17 00:00:00 2001 From: Bellekallu Rajkiran Date: Thu, 17 Nov 2022 10:49:10 +0000 Subject: [PATCH] Fix issue with board number property Buffer usage of less size resulted in invalid board number. Added logic to use sufficient size to retrieve board number from PMT. Added logic to provide decoded values rather than ASCII characters. Related-To: LOCI-3545 Signed-off-by: Bellekallu Rajkiran --- .../linux/os_global_operations_imp.cpp | 44 ++++++++++++------- .../linux/os_global_operations_imp.h | 2 +- .../linux/test_zes_global_operations.cpp | 42 ++++++++++++------ 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.cpp b/level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.cpp index e79f04936f..1c75a5ec3b 100644 --- a/level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.cpp +++ b/level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.cpp @@ -42,7 +42,7 @@ static const std::map engineMap = { {3, ZES_ENGINE_TYPE_FLAG_MEDIA}, {4, ZES_ENGINE_TYPE_FLAG_COMPUTE}}; -bool LinuxGlobalOperationsImp::readTelemData(char (&data)[ZES_STRING_PROPERTY_SIZE], const ssize_t bytesToRead, const std::string &key) { +bool LinuxGlobalOperationsImp::getTelemOffsetAndTelemDir(uint64_t &telemOffset, const std::string &key, std::string &telemDir) { auto pDrm = &pLinuxSysmanImp->getDrm(); std::optional rootPath = NEO::getPciRootPath(pDrm->getFileDescriptor()); if (!rootPath.has_value()) { @@ -56,7 +56,7 @@ bool LinuxGlobalOperationsImp::readTelemData(char (&data)[ZES_STRING_PROPERTY_SI } auto iterator = telemPciPath.begin(); - std::string_view telemDir = iterator->second; + telemDir = iterator->second; std::array guidString = {}; if (!NEO::PmtUtil::readGuid(telemDir, guidString)) { @@ -72,24 +72,30 @@ bool LinuxGlobalOperationsImp::readTelemData(char (&data)[ZES_STRING_PROPERTY_SI if (ZE_RESULT_SUCCESS == PlatformMonitoringTech::getKeyOffsetMap(guidString.data(), keyOffsetMap)) { auto keyOffset = keyOffsetMap.find(key.c_str()); if (keyOffset != keyOffsetMap.end()) { - uint64_t value; - ssize_t bytesRead = NEO::PmtUtil::readTelem(telemDir.data(), bytesToRead, keyOffset->second + offset, &value); - if (bytesRead == bytesToRead) { - std::ostringstream telemDataString; - telemDataString << std::hex << std::showbase << value; - memcpy_s(data, ZES_STRING_PROPERTY_SIZE, telemDataString.str().c_str(), telemDataString.str().size()); - return true; - } + telemOffset = keyOffset->second + offset; + return true; } } return false; } bool LinuxGlobalOperationsImp::getSerialNumber(char (&serialNumber)[ZES_STRING_PROPERTY_SIZE]) { - if (!LinuxGlobalOperationsImp::readTelemData(serialNumber, sizeof(uint64_t), "PPIN")) { + uint64_t offset = 0; + std::string telemDir = {}; + if (!LinuxGlobalOperationsImp::getTelemOffsetAndTelemDir(offset, "PPIN", telemDir)) { return false; } - return true; + + uint64_t value; + ssize_t bytesRead = NEO::PmtUtil::readTelem(telemDir.data(), sizeof(uint64_t), offset, &value); + if (bytesRead == sizeof(uint64_t)) { + std::ostringstream telemDataString; + telemDataString << std::hex << std::showbase << value; + memcpy_s(serialNumber, ZES_STRING_PROPERTY_SIZE, telemDataString.str().c_str(), telemDataString.str().size()); + return true; + } + + return false; } Device *LinuxGlobalOperationsImp::getDevice() { @@ -97,12 +103,20 @@ Device *LinuxGlobalOperationsImp::getDevice() { } bool LinuxGlobalOperationsImp::getBoardNumber(char (&boardNumber)[ZES_STRING_PROPERTY_SIZE]) { - const ssize_t boardNumberSize = 32; - if (!LinuxGlobalOperationsImp::readTelemData(boardNumber, boardNumberSize, "BoardNumber")) { + uint64_t offset = 0; + std::string telemDir = {}; + constexpr uint32_t boardNumberSize = 32; + if (!LinuxGlobalOperationsImp::getTelemOffsetAndTelemDir(offset, "BoardNumber", telemDir)) { return false; } + std::array value; + ssize_t bytesRead = NEO::PmtUtil::readTelem(telemDir.data(), boardNumberSize, offset, value.data()); + if (bytesRead == boardNumberSize) { + memcpy_s(boardNumber, ZES_STRING_PROPERTY_SIZE, value.data(), bytesRead); + return true; + } - return true; + return false; } void LinuxGlobalOperationsImp::getBrandName(char (&brandName)[ZES_STRING_PROPERTY_SIZE]) { diff --git a/level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.h b/level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.h index 8ac78c73d3..b1249384a1 100644 --- a/level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.h +++ b/level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.h @@ -54,7 +54,7 @@ class LinuxGlobalOperationsImp : public OsGlobalOperations, NEO::NonCopyableOrMo static const std::string srcVersionFile; static const std::string agamaVersionFile; static const std::string ueventWedgedFile; - bool readTelemData(char (&serialNumber)[ZES_STRING_PROPERTY_SIZE], const ssize_t bytesToRead, const std::string &key); + bool getTelemOffsetAndTelemDir(uint64_t &telemOffset, const std::string &key, std::string &telemDir); std::string devicePciBdf = ""; NEO::ExecutionEnvironment *executionEnvironment = nullptr; uint32_t rootDeviceIndex = 0u; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/global_operations/linux/test_zes_global_operations.cpp b/level_zero/tools/test/unit_tests/sources/sysman/global_operations/linux/test_zes_global_operations.cpp index 72792c5808..1114af768f 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/global_operations/linux/test_zes_global_operations.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/global_operations/linux/test_zes_global_operations.cpp @@ -220,9 +220,17 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleWhenCallingzesGlobal if ((--fd >= 0) && (fd < static_cast(supportedFiles.size()))) { if (supportedFiles[fd].second == "dummy") { - uint64_t data = 0x3e8c9dfe1c2e4d5c; - memcpy(buf, &data, sizeof(data)); - return count; + if (count == sizeof(uint64_t)) { + uint64_t data = 0x3e8c9dfe1c2e4d5c; + memcpy(buf, &data, sizeof(data)); + return count; + } else { + // Board number will be in ASCII format, Expected board number should be decoded value + // i.e 0821VPTW910091000821VPTW91009100 for data provided below. + uint64_t data[] = {0x5754505631323830, 0x3030313930303139, 0x5754505631323830, 0x3030313930303139}; + memcpy(buf, &data, sizeof(data)); + return count; + } } memcpy(buf, supportedFiles[fd].second.c_str(), supportedFiles[fd].second.size()); return count; @@ -236,21 +244,22 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleWhenCallingzesGlobal pLinuxSysmanImp->pDrm = pDrmMock.get(); zes_device_properties_t properties; - const std::string expectedData("0x3e8c9dfe1c2e4d5c"); + const std::string expectedSerialNumber("0x3e8c9dfe1c2e4d5c"); + const std::string expectedBoardNumber("0821VPTW910091000821VPTW91009100"); ze_result_t result = zesDeviceGetProperties(device, &properties); EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(properties.numSubdevices, 0u); - EXPECT_TRUE(0 == expectedData.compare(properties.boardNumber)); + EXPECT_TRUE(0 == expectedBoardNumber.compare(properties.boardNumber)); EXPECT_TRUE(0 == vendorIntel.compare(properties.brandName)); EXPECT_TRUE(0 == driverVersion.compare(properties.driverVersion)); EXPECT_TRUE(0 == expectedModelName.compare(properties.modelName)); - EXPECT_TRUE(0 == expectedData.compare(properties.serialNumber)); + EXPECT_TRUE(0 == expectedSerialNumber.compare(properties.serialNumber)); EXPECT_TRUE(0 == vendorIntel.compare(properties.vendorName)); pLinuxSysmanImp->pDrm = pOriginalDrm; } -TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadTelemOffsetFailsWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberIsReturned) { +TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadTelemOffsetFailsWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberAndBoardNumberAreReturned) { VariableBackup mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int { std::map fileNameLinkMap = { @@ -310,10 +319,11 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadTelemOffsetFa ze_result_t result = zesDeviceGetProperties(device, &properties); EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_TRUE(0 == unknown.compare(properties.serialNumber)); + EXPECT_TRUE(0 == unknown.compare(properties.boardNumber)); pLinuxSysmanImp->pDrm = pOriginalDrm; } -TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndInvalidGuidWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberIsReturned) { +TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndInvalidGuidWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberAndBoardNumberAreReturned) { VariableBackup mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int { std::map fileNameLinkMap = { {"/sys/dev/char/226:128", "../../devices/pci0000:89/0000:89:02.0/0000:8a:00.0/0000:8b:02.0/0000:8a:00.0/drm/renderD128"}, @@ -371,10 +381,11 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndInvalidGuidWhenCa EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_TRUE(0 == unknown.compare(properties.serialNumber)); + EXPECT_TRUE(0 == unknown.compare(properties.boardNumber)); pLinuxSysmanImp->pDrm = pOriginalDrm; } -TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndPpinOffsetIsAbsentWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberIsReturned) { +TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndPpinandBoardNumberOffsetAreAbsentWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberAndBoardNumberAreReturned) { VariableBackup mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int { std::map fileNameLinkMap = { @@ -433,10 +444,11 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndPpinOffsetIsAbsen EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_TRUE(0 == unknown.compare(properties.serialNumber)); + EXPECT_TRUE(0 == unknown.compare(properties.boardNumber)); pLinuxSysmanImp->pDrm = pOriginalDrm; } -TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadPpinInfoFailsWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberIsReturned) { +TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadTelemDataFailsWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberAndBoardNumberAreReturned) { VariableBackup mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int { std::map fileNameLinkMap = { @@ -493,10 +505,11 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadPpinInfoFails EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_TRUE(0 == unknown.compare(properties.serialNumber)); + EXPECT_TRUE(0 == unknown.compare(properties.boardNumber)); pLinuxSysmanImp->pDrm = pOriginalDrm; } -TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndOpenSysCallFailsWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberIsReturned) { +TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndOpenSysCallFailsWhenCallingzesGlobalOperationsGetPropertiesThenInvalidSerialNumberAndBoardNumberAreReturned) { VariableBackup mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int { std::map fileNameLinkMap = { @@ -525,10 +538,11 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndOpenSysCallFailsW EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_TRUE(0 == unknown.compare(properties.serialNumber)); + EXPECT_TRUE(0 == unknown.compare(properties.boardNumber)); pLinuxSysmanImp->pDrm = pOriginalDrm; } -TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndTelemNodeReadLinkFailsWhenCallingzesGlobalOperationsGetPropertiesThenVerifyInvalidSerialNumberIsReturned) { +TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndTelemNodeReadLinkFailsWhenCallingzesGlobalOperationsGetPropertiesThenVerifyInvalidSerialNumberAndBoardNumberAreReturned) { VariableBackup mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int { std::map fileNameLinkMap = { @@ -551,10 +565,11 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndTelemNodeReadLink EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_TRUE(0 == unknown.compare(properties.serialNumber)); + EXPECT_TRUE(0 == unknown.compare(properties.boardNumber)); pLinuxSysmanImp->pDrm = pOriginalDrm; } -TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadLinkFailsWhenCallingzesGlobalOperationsGetPropertiesThenVerifyInvalidSerialNumberIsReturned) { +TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadLinkFailsWhenCallingzesGlobalOperationsGetPropertiesThenVerifyInvalidSerialNumberAndBoardNumberAreReturned) { VariableBackup mockReadLink(&NEO::SysCalls::sysCallsReadlink, [](const char *path, char *buf, size_t bufsize) -> int { return -1; @@ -569,6 +584,7 @@ TEST_F(SysmanGlobalOperationsFixture, GivenValidDeviceHandleAndReadLinkFailsWhen EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_TRUE(0 == unknown.compare(properties.serialNumber)); + EXPECT_TRUE(0 == unknown.compare(properties.boardNumber)); pLinuxSysmanImp->pDrm = pOriginalDrm; }