From 070c9b060bec5c0368f7f5f89fca38417a849e3b Mon Sep 17 00:00:00 2001 From: Mraghuwa Date: Tue, 12 May 2020 22:24:33 +0530 Subject: [PATCH] Redesign ULT by mocking sysfs and implementing for standby Change-Id: Id2558122406f23e96e3115d57e7b82d04463bfc4 --- .../tools/source/sysman/linux/fs_access.h | 4 +- .../sysman/standby/linux/CMakeLists.txt | 3 +- .../sysman/standby/linux/os_standby_imp.cpp | 29 +---- .../sysman/standby/linux/os_standby_imp.h | 34 +++++ .../sources/sysman/standby/CMakeLists.txt | 5 +- .../sysman/standby/linux/CMakeLists.txt | 14 ++ .../sysman/standby/linux/mock_sysfs_standby.h | 64 +++++++++ .../standby/linux/test_sysman_standby.cpp | 122 ++++++++++++++++++ .../sysman/standby/windows/CMakeLists.txt | 14 ++ .../standby/{ => windows}/mock_standby.h | 0 .../{ => windows}/test_sysman_standby.cpp | 0 11 files changed, 255 insertions(+), 34 deletions(-) create mode 100644 level_zero/tools/source/sysman/standby/linux/os_standby_imp.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/standby/linux/CMakeLists.txt create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/standby/linux/mock_sysfs_standby.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/standby/linux/test_sysman_standby.cpp create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/standby/windows/CMakeLists.txt rename level_zero/tools/test/unit_tests/sources/sysman/standby/{ => windows}/mock_standby.h (100%) rename level_zero/tools/test/unit_tests/sources/sysman/standby/{ => windows}/test_sysman_standby.cpp (100%) diff --git a/level_zero/tools/source/sysman/linux/fs_access.h b/level_zero/tools/source/sysman/linux/fs_access.h index 796194c337..9e2b64419a 100644 --- a/level_zero/tools/source/sysman/linux/fs_access.h +++ b/level_zero/tools/source/sysman/linux/fs_access.h @@ -80,13 +80,13 @@ class SysfsAccess : private FsAccess { ze_result_t getFileMode(const std::string file, ::mode_t &mode); MOCKABLE_VIRTUAL ze_result_t read(const std::string file, std::string &val); - ze_result_t read(const std::string file, int &val); + MOCKABLE_VIRTUAL ze_result_t read(const std::string file, int &val); MOCKABLE_VIRTUAL ze_result_t read(const std::string file, uint64_t &val); ze_result_t read(const std::string file, double &val); ze_result_t read(const std::string file, std::vector &val); ze_result_t write(const std::string file, const std::string val); - ze_result_t write(const std::string file, const int val); + MOCKABLE_VIRTUAL ze_result_t write(const std::string file, const int val); MOCKABLE_VIRTUAL ze_result_t write(const std::string file, const uint64_t val); ze_result_t write(const std::string file, const double val); diff --git a/level_zero/tools/source/sysman/standby/linux/CMakeLists.txt b/level_zero/tools/source/sysman/standby/linux/CMakeLists.txt index 6b9c5f7b2f..7f76b8e774 100644 --- a/level_zero/tools/source/sysman/standby/linux/CMakeLists.txt +++ b/level_zero/tools/source/sysman/standby/linux/CMakeLists.txt @@ -6,7 +6,8 @@ set(L0_SRCS_TOOLS_SYSMAN_STANDBY_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/os_standby_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/os_standby_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/os_standby_imp.h ) if(UNIX) diff --git a/level_zero/tools/source/sysman/standby/linux/os_standby_imp.cpp b/level_zero/tools/source/sysman/standby/linux/os_standby_imp.cpp index 48581dca0a..5ac1a81aff 100644 --- a/level_zero/tools/source/sysman/standby/linux/os_standby_imp.cpp +++ b/level_zero/tools/source/sysman/standby/linux/os_standby_imp.cpp @@ -5,37 +5,10 @@ * */ -#include "shared/source/os_interface/linux/drm_neo.h" -#include "shared/source/os_interface/linux/os_interface.h" - -#include "level_zero/core/source/device/device.h" - -#include "sysman/linux/os_sysman_imp.h" -#include "sysman/standby/os_standby.h" -#include "sysman/standby/standby_imp.h" +#include "level_zero/tools/source/sysman/standby/linux/os_standby_imp.h" namespace L0 { -class LinuxStandbyImp : public OsStandby { - public: - ze_result_t getMode(zet_standby_promo_mode_t &mode) override; - ze_result_t setMode(zet_standby_promo_mode_t mode) override; - - LinuxStandbyImp(OsSysman *pOsSysman); - ~LinuxStandbyImp() override = default; - - // Don't allow copies of the LinuxStandbyImp object - LinuxStandbyImp(const LinuxStandbyImp &obj) = delete; - LinuxStandbyImp &operator=(const LinuxStandbyImp &obj) = delete; - - private: - SysfsAccess *pSysfsAccess; - - static const std::string standbyModeFile; - static const int standbyModeDefault = 1; - static const int standbyModeNever = 0; -}; - const std::string LinuxStandbyImp::standbyModeFile("power/rc6_enable"); ze_result_t LinuxStandbyImp::getMode(zet_standby_promo_mode_t &mode) { diff --git a/level_zero/tools/source/sysman/standby/linux/os_standby_imp.h b/level_zero/tools/source/sysman/standby/linux/os_standby_imp.h new file mode 100644 index 0000000000..ffec465167 --- /dev/null +++ b/level_zero/tools/source/sysman/standby/linux/os_standby_imp.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/non_copyable_or_moveable.h" + +#include "sysman/linux/os_sysman_imp.h" +#include "sysman/standby/os_standby.h" +#include "sysman/standby/standby_imp.h" + +namespace L0 { + +class LinuxStandbyImp : public OsStandby, public NEO::NonCopyableClass { + public: + ze_result_t getMode(zet_standby_promo_mode_t &mode) override; + ze_result_t setMode(zet_standby_promo_mode_t mode) override; + LinuxStandbyImp() = default; + LinuxStandbyImp(OsSysman *pOsSysman); + ~LinuxStandbyImp() override = default; + + protected: + SysfsAccess *pSysfsAccess = nullptr; + + private: + static const std::string standbyModeFile; + static const int standbyModeDefault = 1; + static const int standbyModeNever = 0; +}; + +} // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/standby/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/standby/CMakeLists.txt index 9df5df9d2e..292a5dd53a 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/standby/CMakeLists.txt +++ b/level_zero/tools/test/unit_tests/sources/sysman/standby/CMakeLists.txt @@ -5,8 +5,7 @@ # target_sources(${TARGET_NAME} PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_standby.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/mock_standby.h + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt ) +add_subdirectories() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/CMakeLists.txt new file mode 100644 index 0000000000..2ac95312ff --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/CMakeLists.txt @@ -0,0 +1,14 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +if(UNIX) + target_sources(${TARGET_NAME} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_standby.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_sysfs_standby.h + ) +endif() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/mock_sysfs_standby.h b/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/mock_sysfs_standby.h new file mode 100644 index 0000000000..9eafa7c45b --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/mock_sysfs_standby.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "level_zero/core/test/unit_tests/mock.h" +#include "level_zero/tools/source/sysman/standby/linux/os_standby_imp.h" + +#include "sysman/linux/fs_access.h" +#include "sysman/standby/os_standby.h" +#include "sysman/standby/standby_imp.h" +#include "sysman/sysman.h" +#include "sysman/sysman_imp.h" + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Winconsistent-missing-override" +#endif + +using ::testing::_; + +namespace L0 { +namespace ult { + +const std::string standbyModeFile("power/rc6_enable"); + +class StandbySysfsAccess : public SysfsAccess {}; + +template <> +struct Mock : public StandbySysfsAccess { + int mockStandbyMode = -1; + MOCK_METHOD2(read, ze_result_t(const std::string file, int &val)); + MOCK_METHOD2(write, ze_result_t(const std::string file, const int val)); + + ze_result_t getVal(const std::string file, int &val) { + if (file.compare(standbyModeFile) == 0) { + val = mockStandbyMode; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t setVal(const std::string file, const int val) { + if (file.compare(standbyModeFile) == 0) { + mockStandbyMode = val; + } + return ZE_RESULT_SUCCESS; + } + + Mock() = default; +}; + +class PublicLinuxStandbyImp : public L0::LinuxStandbyImp { + public: + using LinuxStandbyImp::pSysfsAccess; +}; +} // namespace ult +} // namespace L0 + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif diff --git a/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/test_sysman_standby.cpp b/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/test_sysman_standby.cpp new file mode 100644 index 0000000000..324d288de9 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/test_sysman_standby.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" +#include "level_zero/tools/source/sysman/standby/linux/os_standby_imp.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "mock_sysfs_standby.h" +#include "sysman/standby/os_standby.h" + +#include + +using ::testing::_; +using ::testing::DoAll; +using ::testing::InSequence; +using ::testing::Invoke; +using ::testing::Matcher; +using ::testing::NiceMock; +using ::testing::Return; + +namespace L0 { +namespace ult { + +constexpr int standbyModeDefault = 1; + +class SysmanStandbyFixture : public DeviceFixture, public ::testing::Test { + + protected: + std::unique_ptr sysmanImp; + zet_sysman_handle_t hSysman; + zet_sysman_standby_handle_t hSysmanStandby; + Mock *pSysfsAccess = nullptr; + + OsStandby *pOsStandby = nullptr; + PublicLinuxStandbyImp linuxStandbyImp; + StandbyImp *pStandbyImp = nullptr; + + void SetUp() override { + DeviceFixture::SetUp(); + sysmanImp = std::make_unique(device->toHandle()); + pSysfsAccess = new NiceMock>; + linuxStandbyImp.pSysfsAccess = pSysfsAccess; + pOsStandby = static_cast(&linuxStandbyImp); + pStandbyImp = new StandbyImp(); + pStandbyImp->pOsStandby = pOsStandby; + pSysfsAccess->setVal(standbyModeFile, standbyModeDefault); + ON_CALL(*pSysfsAccess, read(_, Matcher(_))) + .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getVal)); + + pStandbyImp->init(); + sysmanImp->pStandbyHandleContext->handle_list.push_back(pStandbyImp); + + hSysman = sysmanImp->toHandle(); + hSysmanStandby = pStandbyImp->toHandle(); + } + + void TearDown() override { + pStandbyImp->pOsStandby = nullptr; + + if (pSysfsAccess != nullptr) { + delete pSysfsAccess; + pSysfsAccess = nullptr; + } + DeviceFixture::TearDown(); + } +}; + +TEST_F(SysmanStandbyFixture, GivenComponentCountZeroWhenCallingzetSysmanStandbyGetThenNonZeroCountIsReturnedAndVerifyzetSysmanStandbyGetCallSucceeds) { + zet_sysman_standby_handle_t standbyHandle; + uint32_t count; + + ze_result_t result = zetSysmanStandbyGet(hSysman, &count, NULL); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_GT(count, 0u); + + uint32_t testCount = count + 1; + + result = zetSysmanStandbyGet(hSysman, &testCount, NULL); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(testCount, count); + + result = zetSysmanStandbyGet(hSysman, &count, &standbyHandle); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, standbyHandle); + EXPECT_GT(count, 0u); +} + +TEST_F(SysmanStandbyFixture, GivenValidStandbyHandleWhenCallingzetSysmanStandbyGetPropertiesThenVerifyzetSysmanStandbyGetPropertiesCallSucceeds) { + zet_standby_properties_t properties; + + ze_result_t result = zetSysmanStandbyGetProperties(hSysmanStandby, &properties); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(ZET_STANDBY_TYPE_GLOBAL, properties.type); + EXPECT_FALSE(properties.onSubdevice); +} + +TEST_F(SysmanStandbyFixture, GivenValidStandbyHandleWhenCallingzetSysmanStandbyGetModeThenVerifyzetSysmanStandbyGetModeCallSucceeds) { + zet_standby_promo_mode_t mode; + + ze_result_t result = zetSysmanStandbyGetMode(hSysmanStandby, &mode); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(ZET_STANDBY_PROMO_MODE_DEFAULT, mode); +} + +TEST_F(SysmanStandbyFixture, GivenValidStandbyHandleWhenCallingzetSysmanStandbySetModeThenVerifySysmanzetStandbySetModeCallSucceeds) { + ze_result_t result = zetSysmanStandbySetMode(hSysmanStandby, ZET_STANDBY_PROMO_MODE_NEVER); + + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); +} + +} // namespace ult +} // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/standby/windows/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/standby/windows/CMakeLists.txt new file mode 100644 index 0000000000..a969f150e7 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/standby/windows/CMakeLists.txt @@ -0,0 +1,14 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +if(WIN32) + target_sources(${TARGET_NAME} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_standby.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_standby.h + ) +endif() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/standby/mock_standby.h b/level_zero/tools/test/unit_tests/sources/sysman/standby/windows/mock_standby.h similarity index 100% rename from level_zero/tools/test/unit_tests/sources/sysman/standby/mock_standby.h rename to level_zero/tools/test/unit_tests/sources/sysman/standby/windows/mock_standby.h diff --git a/level_zero/tools/test/unit_tests/sources/sysman/standby/test_sysman_standby.cpp b/level_zero/tools/test/unit_tests/sources/sysman/standby/windows/test_sysman_standby.cpp similarity index 100% rename from level_zero/tools/test/unit_tests/sources/sysman/standby/test_sysman_standby.cpp rename to level_zero/tools/test/unit_tests/sources/sysman/standby/windows/test_sysman_standby.cpp