diff --git a/level_zero/tools/source/sysman/power/linux/CMakeLists.txt b/level_zero/tools/source/sysman/power/linux/CMakeLists.txt index 403a308906..c952f28512 100644 --- a/level_zero/tools/source/sysman/power/linux/CMakeLists.txt +++ b/level_zero/tools/source/sysman/power/linux/CMakeLists.txt @@ -6,6 +6,7 @@ set(L0_SRCS_TOOLS_SYSMAN_POWER_LINUX ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/os_power_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/os_power_imp.h ) if(UNIX) @@ -15,4 +16,4 @@ if(UNIX) endif() # Make our source files visible to parent -set_property(GLOBAL PROPERTY L0_SRCS_TOOLS_SYSMAN_POWER_LINUX ${L0_SRCS_TOOLS_SYSMAN_POWER_LINUX}) \ No newline at end of file +set_property(GLOBAL PROPERTY L0_SRCS_TOOLS_SYSMAN_POWER_LINUX ${L0_SRCS_TOOLS_SYSMAN_POWER_LINUX}) diff --git a/level_zero/tools/source/sysman/power/linux/os_power_imp.cpp b/level_zero/tools/source/sysman/power/linux/os_power_imp.cpp index 507218556f..f198c58819 100644 --- a/level_zero/tools/source/sysman/power/linux/os_power_imp.cpp +++ b/level_zero/tools/source/sysman/power/linux/os_power_imp.cpp @@ -5,22 +5,14 @@ * */ -#include "shared/source/helpers/non_copyable_or_moveable.h" - -#include "level_zero/tools/source/sysman/power/os_power.h" - -#include "sysman/linux/os_sysman_imp.h" +#include "level_zero/tools/source/sysman/power/linux/os_power_imp.h" namespace L0 { -class LinuxPowerImp : public OsPower, public NEO::NonCopyableClass { - public: - LinuxPowerImp(OsSysman *pOsSysman); - ~LinuxPowerImp() override = default; +ze_result_t LinuxPowerImp::getEnergyCounter(uint64_t &energy) { - private: - SysfsAccess *pSysfsAccess = nullptr; -}; + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} LinuxPowerImp::LinuxPowerImp(OsSysman *pOsSysman) { LinuxSysmanImp *pLinuxSysmanImp = static_cast(pOsSysman); @@ -32,4 +24,4 @@ OsPower *OsPower::create(OsSysman *pOsSysman) { return static_cast(pLinuxPowerImp); } -} // namespace L0 \ No newline at end of file +} // namespace L0 diff --git a/level_zero/tools/source/sysman/power/linux/os_power_imp.h b/level_zero/tools/source/sysman/power/linux/os_power_imp.h new file mode 100644 index 0000000000..e23a6d33d0 --- /dev/null +++ b/level_zero/tools/source/sysman/power/linux/os_power_imp.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/non_copyable_or_moveable.h" + +#include "level_zero/tools/source/sysman/power/os_power.h" + +#include "sysman/linux/os_sysman_imp.h" + +namespace L0 { + +class LinuxPowerImp : public OsPower, public NEO::NonCopyableClass { + public: + ze_result_t getEnergyCounter(uint64_t &energy) override; + LinuxPowerImp(OsSysman *pOsSysman); + LinuxPowerImp() = default; + ~LinuxPowerImp() override = default; + + protected: + SysfsAccess *pSysfsAccess = nullptr; +}; +} // namespace L0 diff --git a/level_zero/tools/source/sysman/power/os_power.h b/level_zero/tools/source/sysman/power/os_power.h index 76ed21de9c..bd75b0b7ce 100644 --- a/level_zero/tools/source/sysman/power/os_power.h +++ b/level_zero/tools/source/sysman/power/os_power.h @@ -14,6 +14,7 @@ namespace L0 { struct OsSysman; class OsPower { public: + virtual ze_result_t getEnergyCounter(uint64_t &energy) = 0; static OsPower *create(OsSysman *pOsSysman); virtual ~OsPower() = default; }; diff --git a/level_zero/tools/source/sysman/power/power_imp.cpp b/level_zero/tools/source/sysman/power/power_imp.cpp index 86c55c0e91..3e793c3546 100644 --- a/level_zero/tools/source/sysman/power/power_imp.cpp +++ b/level_zero/tools/source/sysman/power/power_imp.cpp @@ -11,14 +11,30 @@ #include "level_zero/core/source/device/device.h" +#include + namespace L0 { +void powerGetTimestamp(uint64_t ×tamp) { + std::chrono::time_point ts = std::chrono::steady_clock::now(); + timestamp = std::chrono::duration_cast(ts.time_since_epoch()).count(); +} + ze_result_t PowerImp::powerGetProperties(zet_power_properties_t *pProperties) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } ze_result_t PowerImp::powerGetEnergyCounter(zet_power_energy_counter_t *pEnergy) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + + ze_result_t result = pOsPower->getEnergyCounter(pEnergy->energy); + + if (result != ZE_RESULT_SUCCESS) { + return result; + } + + powerGetTimestamp(pEnergy->timestamp); + + return result; } ze_result_t PowerImp::powerGetLimits(zet_power_sustained_limit_t *pSustained, zet_power_burst_limit_t *pBurst, zet_power_peak_limit_t *pPeak) { diff --git a/level_zero/tools/source/sysman/power/windows/os_power_imp.cpp b/level_zero/tools/source/sysman/power/windows/os_power_imp.cpp index 9e5f01ce59..8cdb7b3957 100644 --- a/level_zero/tools/source/sysman/power/windows/os_power_imp.cpp +++ b/level_zero/tools/source/sysman/power/windows/os_power_imp.cpp @@ -9,11 +9,19 @@ namespace L0 { -class WddmPowerImp : public OsPower {}; +class WddmPowerImp : public OsPower { + public: + ze_result_t getEnergyCounter(uint64_t &energy) override; +}; + +ze_result_t WddmPowerImp::getEnergyCounter(uint64_t &energy) { + + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} OsPower *OsPower::create(OsSysman *pOsSysman) { WddmPowerImp *pWddmPowerImp = new WddmPowerImp(); return static_cast(pWddmPowerImp); } -} // namespace L0 \ No newline at end of file +} // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/power/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/power/CMakeLists.txt new file mode 100644 index 0000000000..292a5dd53a --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/power/CMakeLists.txt @@ -0,0 +1,11 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +target_sources(${TARGET_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt +) + +add_subdirectories() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/power/linux/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/power/linux/CMakeLists.txt new file mode 100644 index 0000000000..6571c9bd5b --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/power/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}${BRANCH_DIR_SUFFIX}/test_sysman_power.cpp + ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/mock_sysfs_power.h + ) +endif() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/power/linux/mock_sysfs_power.h b/level_zero/tools/test/unit_tests/sources/sysman/power/linux/mock_sysfs_power.h new file mode 100644 index 0000000000..8d7569cceb --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/power/linux/mock_sysfs_power.h @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "level_zero/tools/source/sysman/power/linux/os_power_imp.h" + +namespace L0 { +namespace ult { + +class PublicLinuxPowerImp : public L0::LinuxPowerImp { + public: + using LinuxPowerImp::pSysfsAccess; +}; +} // namespace ult +} // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/power/linux/test_sysman_power.cpp b/level_zero/tools/test/unit_tests/sources/sysman/power/linux/test_sysman_power.cpp new file mode 100644 index 0000000000..5aedfbb889 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/power/linux/test_sysman_power.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "mock_sysfs_power.h" +#include "sysman/power/power_imp.h" + +using ::testing::_; + +namespace L0 { +namespace ult { + +class SysmanPowerFixture : public DeviceFixture, public ::testing::Test { + + protected: + std::unique_ptr sysmanImp; + zet_sysman_handle_t hSysman; + + OsPower *pOsPower = nullptr; + PublicLinuxPowerImp linuxPowerImp; + PowerImp *pPowerImp = nullptr; + + void SetUp() override { + + DeviceFixture::SetUp(); + sysmanImp = std::make_unique(device->toHandle()); + pOsPower = static_cast(&linuxPowerImp); + pPowerImp = new PowerImp(); + pPowerImp->pOsPower = pOsPower; + pPowerImp->init(); + hSysman = sysmanImp->toHandle(); + } + void TearDown() override { + //pOsPower is static_cast of LinuxPowerImp class , hence in cleanup assign to nullptr + if (pPowerImp != nullptr) { + pPowerImp->pOsPower = nullptr; + delete pPowerImp; + pPowerImp = nullptr; + } + DeviceFixture::TearDown(); + } +}; + +TEST_F(SysmanPowerFixture, GivenComponentCountZeroWhenCallingZetSysmanPowerGetThenZeroCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + uint32_t count = 0; + ze_result_t result = zetSysmanPowerGet(hSysman, &count, NULL); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(count, 0u); + + uint32_t testcount = count + 1; + + result = zetSysmanPowerGet(hSysman, &testcount, NULL); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(testcount, count); +} +} // namespace ult +} // namespace L0