Skip to content

Commit b6ccfed

Browse files
Redesign ULT by mocking sysfs and implementing for pci
Change-Id: I5ee2219208d05c125d1fce1a4a5b992bc8c4646e
1 parent e25eb40 commit b6ccfed

File tree

11 files changed

+267
-221
lines changed

11 files changed

+267
-221
lines changed

level_zero/tools/source/sysman/linux/fs_access.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ class SysfsAccess : private FsAccess {
8383
MOCKABLE_VIRTUAL ze_result_t read(const std::string file, int &val);
8484
MOCKABLE_VIRTUAL ze_result_t read(const std::string file, uint64_t &val);
8585
MOCKABLE_VIRTUAL ze_result_t read(const std::string file, double &val);
86-
ze_result_t read(const std::string file, std::vector<std::string> &val);
86+
MOCKABLE_VIRTUAL ze_result_t read(const std::string file, std::vector<std::string> &val);
8787

8888
ze_result_t write(const std::string file, const std::string val);
8989
MOCKABLE_VIRTUAL ze_result_t write(const std::string file, const int val);
9090
MOCKABLE_VIRTUAL ze_result_t write(const std::string file, const uint64_t val);
9191
MOCKABLE_VIRTUAL ze_result_t write(const std::string file, const double val);
92+
ze_result_t write(const std::string file, std::vector<std::string> val);
9293

9394
MOCKABLE_VIRTUAL ze_result_t scanDirEntries(const std::string path, std::vector<std::string> &list);
94-
ze_result_t readSymLink(const std::string path, std::string &buf);
95+
MOCKABLE_VIRTUAL ze_result_t readSymLink(const std::string path, std::string &buf);
9596
ze_result_t getRealPath(const std::string path, std::string &buf);
9697
ze_result_t bindDevice(const std::string device);
9798
ze_result_t unbindDevice(const std::string device);

level_zero/tools/source/sysman/pci/linux/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
set(L0_SRCS_TOOLS_SYSMAN_PCI_LINUX
88
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
99
${CMAKE_CURRENT_SOURCE_DIR}/os_pci_imp.cpp
10+
${CMAKE_CURRENT_SOURCE_DIR}/os_pci_imp.h
1011
)
1112

1213
if(UNIX)

level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,20 @@
55
*
66
*/
77

8-
#include "shared/source/os_interface/linux/drm_neo.h"
9-
#include "shared/source/os_interface/linux/os_interface.h"
8+
#include "level_zero/tools/source/sysman/pci/linux/os_pci_imp.h"
109

11-
#include "level_zero/core/source/device/device.h"
10+
#include "level_zero/tools/source/sysman/linux/fs_access.h"
1211

1312
#include "sysman/linux/os_sysman_imp.h"
14-
#include "sysman/pci/os_pci.h"
1513
#include "sysman/pci/pci_imp.h"
1614

17-
#include <unistd.h>
18-
1915
namespace L0 {
20-
constexpr uint8_t maxPciBars = 6;
21-
class LinuxPciImp : public OsPci {
22-
public:
23-
ze_result_t getPciBdf(std::string &bdf) override;
24-
ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override;
25-
ze_result_t getMaxLinkWidth(uint32_t &maxLinkwidth) override;
26-
ze_result_t getLinkGen(uint32_t &linkGen) override;
27-
ze_result_t initializeBarProperties(std::vector<zet_pci_bar_properties_t *> &pBarProperties) override;
28-
LinuxPciImp(OsSysman *pOsSysman);
29-
~LinuxPciImp() override = default;
30-
31-
// Don't allow copies of the LinuxPciImp object
32-
LinuxPciImp(const LinuxPciImp &obj) = delete;
33-
LinuxPciImp &operator=(const LinuxPciImp &obj) = delete;
34-
35-
private:
36-
SysfsAccess *pSysfsAccess;
37-
static const std::string deviceDir;
38-
static const std::string resourceFile;
39-
static const std::string maxLinkSpeedFile;
40-
static const std::string maxLinkWidthFile;
41-
};
4216

4317
const std::string LinuxPciImp::deviceDir("device");
4418
const std::string LinuxPciImp::resourceFile("device/resource");
4519
const std::string LinuxPciImp::maxLinkSpeedFile("device/max_link_speed");
4620
const std::string LinuxPciImp::maxLinkWidthFile("device/max_link_width");
21+
constexpr uint8_t maxPciBars = 6;
4722

4823
ze_result_t LinuxPciImp::getPciBdf(std::string &bdf) {
4924
std::string bdfDir;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2020 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
#include "shared/source/helpers/non_copyable_or_moveable.h"
10+
11+
#include "sysman/pci/os_pci.h"
12+
13+
namespace L0 {
14+
15+
class SysfsAccess;
16+
17+
class LinuxPciImp : public NEO::NonCopyableClass, public OsPci {
18+
public:
19+
ze_result_t getPciBdf(std::string &bdf) override;
20+
ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override;
21+
ze_result_t getMaxLinkWidth(uint32_t &maxLinkwidth) override;
22+
ze_result_t getLinkGen(uint32_t &linkGen) override;
23+
ze_result_t initializeBarProperties(std::vector<zet_pci_bar_properties_t *> &pBarProperties) override;
24+
LinuxPciImp() = default;
25+
LinuxPciImp(OsSysman *pOsSysman);
26+
~LinuxPciImp() override = default;
27+
28+
protected:
29+
SysfsAccess *pSysfsAccess = nullptr;
30+
31+
private:
32+
static const std::string deviceDir;
33+
static const std::string resourceFile;
34+
static const std::string maxLinkSpeedFile;
35+
static const std::string maxLinkWidthFile;
36+
};
37+
38+
} // namespace L0

level_zero/tools/source/sysman/pci/pci_imp.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
#include "shared/source/helpers/basic_math.h"
1111
#include "shared/source/helpers/debug_helpers.h"
12+
#include "shared/source/helpers/string.h"
1213

14+
#include <cstring>
1315
namespace L0 {
1416

1517
//
@@ -91,9 +93,13 @@ void PciImp::init() {
9193
}
9294

9395
PciImp::~PciImp() {
96+
for (zet_pci_bar_properties_t *pProperties : pciBarProperties) {
97+
delete pProperties;
98+
pProperties = nullptr;
99+
}
94100
if (nullptr != pOsPci) {
95101
delete pOsPci;
102+
pOsPci = nullptr;
96103
}
97104
}
98-
99105
} // namespace L0

level_zero/tools/test/unit_tests/sources/sysman/pci/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66

77
target_sources(${TARGET_NAME} PRIVATE
8-
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
9-
${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_pci.cpp
10-
${CMAKE_CURRENT_SOURCE_DIR}/mock_pci.h
8+
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
119
)
10+
11+
add_subdirectories()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# Copyright (C) 2020 Intel Corporation
3+
#
4+
# SPDX-License-Identifier: MIT
5+
#
6+
7+
if(UNIX)
8+
target_sources(${TARGET_NAME}
9+
PRIVATE
10+
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
11+
${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_pci.cpp
12+
${CMAKE_CURRENT_SOURCE_DIR}/mock_sysfs_pci.h
13+
)
14+
endif()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (C) 2020 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
#include "level_zero/core/test/unit_tests/mock.h"
10+
#include "level_zero/tools/source/sysman/linux/fs_access.h"
11+
#include "level_zero/tools/source/sysman/pci/linux/os_pci_imp.h"
12+
13+
#include "sysman/linux/os_sysman_imp.h"
14+
#include "sysman/pci/pci_imp.h"
15+
16+
using ::testing::_;
17+
18+
namespace L0 {
19+
namespace ult {
20+
21+
const std::string deviceDir("device");
22+
const std::string resourceFile("device/resource");
23+
const std::string maxLinkSpeedFile("device/max_link_speed");
24+
const std::string maxLinkWidthFile("device/max_link_width");
25+
const std::string mockBdf = "0000:00:02.0";
26+
constexpr double mockMaxLinkSpeed = 2.5;
27+
constexpr int mockMaxLinkWidth = 1;
28+
const std::vector<std::string> mockReadBytes =
29+
{
30+
"0x00000000bf000000 0x00000000bfffffff 0x0000000000140204",
31+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
32+
"0x0000000080000000 0x000000008fffffff 0x000000000014220c",
33+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
34+
"0x0000000000004000 0x000000000000403f 0x0000000000040101",
35+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
36+
"0x00000000000c0000 0x00000000000dffff 0x0000000000000212",
37+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
38+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
39+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
40+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
41+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
42+
"0x0000000000000000 0x0000000000000000 0x0000000000000000",
43+
};
44+
45+
class PciSysfsAccess : public SysfsAccess {};
46+
47+
template <>
48+
struct Mock<PciSysfsAccess> : public SysfsAccess {
49+
MOCK_METHOD(ze_result_t, read, (const std::string file, double &val), (override));
50+
MOCK_METHOD(ze_result_t, read, (const std::string file, int &val), (override));
51+
MOCK_METHOD(ze_result_t, read, (const std::string file, std::vector<std::string> &val), (override));
52+
MOCK_METHOD(ze_result_t, readSymLink, (const std::string file, std::string &buf), (override));
53+
54+
ze_result_t getValDouble(const std::string file, double &val) {
55+
if (file.compare(maxLinkSpeedFile) == 0) {
56+
val = mockMaxLinkSpeed;
57+
}
58+
return ZE_RESULT_SUCCESS;
59+
}
60+
61+
ze_result_t getValInt(const std::string file, int &val) {
62+
if (file.compare(maxLinkWidthFile) == 0) {
63+
val = mockMaxLinkWidth;
64+
}
65+
return ZE_RESULT_SUCCESS;
66+
}
67+
68+
ze_result_t getValString(const std::string file, std::string &val) {
69+
if (file.compare(deviceDir) == 0) {
70+
val = mockBdf;
71+
}
72+
return ZE_RESULT_SUCCESS;
73+
}
74+
75+
ze_result_t getValVector(const std::string file, std::vector<std::string> &val) {
76+
if (file.compare(resourceFile) == 0) {
77+
val = mockReadBytes;
78+
}
79+
return ZE_RESULT_SUCCESS;
80+
}
81+
82+
Mock<PciSysfsAccess>() = default;
83+
};
84+
85+
class PublicLinuxPciImp : public L0::LinuxPciImp {
86+
public:
87+
using LinuxPciImp::pSysfsAccess;
88+
};
89+
90+
} // namespace ult
91+
} // namespace L0
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (C) 2020 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h"
9+
#include "level_zero/tools/source/sysman/pci/linux/os_pci_imp.h"
10+
11+
#include "gmock/gmock.h"
12+
#include "gtest/gtest.h"
13+
#include "mock_sysfs_pci.h"
14+
15+
#include <string>
16+
17+
using ::testing::_;
18+
using ::testing::DoAll;
19+
using ::testing::InSequence;
20+
using ::testing::Invoke;
21+
using ::testing::Matcher;
22+
using ::testing::NiceMock;
23+
using ::testing::Return;
24+
25+
namespace L0 {
26+
namespace ult {
27+
28+
class SysmanPciFixture : public DeviceFixture, public ::testing::Test {
29+
30+
protected:
31+
std::unique_ptr<SysmanImp> sysmanImp;
32+
zet_sysman_handle_t hSysman;
33+
34+
OsPci *pOsPci = nullptr;
35+
Mock<PciSysfsAccess> *pSysfsAccess = nullptr;
36+
L0::Pci *pPciPrev = nullptr;
37+
L0::PciImp pciImp;
38+
PublicLinuxPciImp linuxPciImp;
39+
40+
void SetUp() override {
41+
DeviceFixture::SetUp();
42+
sysmanImp = std::make_unique<SysmanImp>(device->toHandle());
43+
pSysfsAccess = new NiceMock<Mock<PciSysfsAccess>>;
44+
linuxPciImp.pSysfsAccess = pSysfsAccess;
45+
pOsPci = static_cast<OsPci *>(&linuxPciImp);
46+
47+
ON_CALL(*pSysfsAccess, read(_, Matcher<std::vector<std::string> &>(_)))
48+
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValVector));
49+
ON_CALL(*pSysfsAccess, read(_, Matcher<int &>(_)))
50+
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValInt));
51+
ON_CALL(*pSysfsAccess, readSymLink(_, _))
52+
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValString));
53+
ON_CALL(*pSysfsAccess, read(_, Matcher<double &>(_)))
54+
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValDouble));
55+
56+
pPciPrev = sysmanImp->pPci;
57+
sysmanImp->pPci = static_cast<Pci *>(&pciImp);
58+
pciImp.pOsPci = pOsPci;
59+
pciImp.init();
60+
hSysman = sysmanImp->toHandle();
61+
}
62+
63+
void TearDown() override {
64+
sysmanImp->pPci = pPciPrev;
65+
pciImp.pOsPci = nullptr;
66+
// cleanup
67+
if (pSysfsAccess != nullptr) {
68+
delete pSysfsAccess;
69+
pSysfsAccess = nullptr;
70+
}
71+
72+
DeviceFixture::TearDown();
73+
}
74+
};
75+
76+
TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropertiesThenVerifyzetSysmanPciGetPropertiesCallSucceeds) {
77+
zet_pci_properties_t properties;
78+
ze_result_t result = zetSysmanPciGetProperties(hSysman, &properties);
79+
80+
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
81+
EXPECT_LT(properties.address.bus, 256u);
82+
EXPECT_LT(properties.address.device, 32u);
83+
EXPECT_LT(properties.address.function, 8u);
84+
EXPECT_LE(properties.maxSpeed.gen, 5u);
85+
EXPECT_LE(properties.maxSpeed.width, 32u);
86+
EXPECT_LE(properties.maxSpeed.maxBandwidth, std::numeric_limits<uint64_t>::max());
87+
}
88+
89+
TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVerifyzetSysmanPciGetBarsCallSucceeds) {
90+
uint32_t count = 0;
91+
ze_result_t result = zetSysmanPciGetBars(hSysman, &count, nullptr);
92+
93+
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
94+
EXPECT_GT(count, 0u);
95+
96+
std::vector<zet_pci_bar_properties_t> pciBarProps(count);
97+
result = zetSysmanPciGetBars(hSysman, &count, pciBarProps.data());
98+
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
99+
for (uint32_t i = 0; i < count; i++) {
100+
EXPECT_LE(pciBarProps[i].type, ZET_PCI_BAR_TYPE_OTHER);
101+
EXPECT_NE(pciBarProps[i].base, 0u);
102+
EXPECT_NE(pciBarProps[i].size, 0u);
103+
}
104+
}
105+
106+
} // namespace ult
107+
} // namespace L0

0 commit comments

Comments
 (0)