From be6ea36769f19826aa3d5b9c437ecb424a1d5042 Mon Sep 17 00:00:00 2001 From: SaiKishore Konda Date: Wed, 15 Jul 2020 06:39:28 -0400 Subject: [PATCH] Implementing zetSysmanMemoryGetState - zetSysmanMemoryGetState api Implementation - Corresponding ULT for validation Change-Id: I2dcd53af4dd0f7cd3e19f594d70fefe21d2206e7 Signed-off-by: SaiKishore Konda --- .../tools/source/sysman/memory/CMakeLists.txt | 2 +- .../source/sysman/memory/linux/CMakeLists.txt | 12 +- .../sysman/memory/linux/dg1/os_memory_imp.cpp | 49 ++++++++ .../sysman/memory/linux/dg1/os_memory_imp.h | 29 +++++ .../tools/source/sysman/memory/memory_imp.cpp | 15 ++- .../sysman/memory/linux/CMakeLists.txt | 18 ++- .../sysman/memory/linux/dg1/mock_memory.h | 86 +++++++++++++ .../memory/linux/dg1/test_sysman_memory.cpp | 119 ++++++++++++++++++ 8 files changed, 320 insertions(+), 10 deletions(-) create mode 100644 level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.cpp create mode 100644 level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/memory/linux/dg1/mock_memory.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/memory/linux/dg1/test_sysman_memory.cpp diff --git a/level_zero/tools/source/sysman/memory/CMakeLists.txt b/level_zero/tools/source/sysman/memory/CMakeLists.txt index bc31fdc869..e69d3a2504 100755 --- a/level_zero/tools/source/sysman/memory/CMakeLists.txt +++ b/level_zero/tools/source/sysman/memory/CMakeLists.txt @@ -7,7 +7,7 @@ set(L0_SRCS_TOOLS_SYSMAN_MEMORY ${CMAKE_CURRENT_SOURCE_DIR}/memory.cpp ${CMAKE_CURRENT_SOURCE_DIR}/memory.h - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/memory_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/memory_imp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/memory_imp.h ${CMAKE_CURRENT_SOURCE_DIR}/os_memory.h ) diff --git a/level_zero/tools/source/sysman/memory/linux/CMakeLists.txt b/level_zero/tools/source/sysman/memory/linux/CMakeLists.txt index 2cea56e5dd..d538f65709 100755 --- a/level_zero/tools/source/sysman/memory/linux/CMakeLists.txt +++ b/level_zero/tools/source/sysman/memory/linux/CMakeLists.txt @@ -4,11 +4,19 @@ # SPDX-License-Identifier: MIT # +if(SUPPORT_DG1) set(L0_SRCS_TOOLS_SYSMAN_MEMORY_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/os_memory_imp.cpp - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/os_memory_imp.h + ${CMAKE_CURRENT_SOURCE_DIR}/dg1/os_memory_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/dg1/os_memory_imp.h ) +else() +set(L0_SRCS_TOOLS_SYSMAN_MEMORY_LINUX + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/os_memory_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/os_memory_imp.h +) +endif() if(UNIX) target_sources(${L0_STATIC_LIB_NAME} diff --git a/level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.cpp b/level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.cpp new file mode 100644 index 0000000000..5d21f8c080 --- /dev/null +++ b/level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.h" + +#include "shared/source/os_interface/linux/memory_info_impl.h" + +#include "sysman/linux/os_sysman_imp.h" + +namespace L0 { + +LinuxMemoryImp::LinuxMemoryImp(OsSysman *pOsSysman) { + LinuxSysmanImp *pLinuxSysmanImp = static_cast(pOsSysman); + pDrm = &pLinuxSysmanImp->getDrm(); +} + +ze_result_t LinuxMemoryImp::getMemorySize(uint64_t &maxSize, uint64_t &allocSize) { + + if (pDrm->queryMemoryInfo() == false) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + } + auto memoryInfo = static_cast(pDrm->getMemoryInfo()); + auto region = std::find_if(memoryInfo->regions.begin(), memoryInfo->regions.end(), [](auto tempRegion) { + return (tempRegion.region.memory_class == I915_MEMORY_CLASS_DEVICE); + }); + if (region == memoryInfo->regions.end()) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + } + maxSize = region->probed_size; + allocSize = maxSize - region->unallocated_size; + return ZE_RESULT_SUCCESS; +} + +ze_result_t LinuxMemoryImp::getMemHealth(zet_mem_health_t &memHealth) { + memHealth = ZET_MEM_HEALTH_OK; + + return ZE_RESULT_SUCCESS; +} + +OsMemory *OsMemory::create(OsSysman *pOsSysman) { + LinuxMemoryImp *pLinuxMemoryImp = new LinuxMemoryImp(pOsSysman); + return static_cast(pLinuxMemoryImp); +} + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.h b/level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.h new file mode 100644 index 0000000000..01e2046267 --- /dev/null +++ b/level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/non_copyable_or_moveable.h" +#include "shared/source/os_interface/linux/drm_neo.h" + +#include "sysman/memory/os_memory.h" + +namespace L0 { + +class SysfsAccess; + +class LinuxMemoryImp : public OsMemory, NEO::NonCopyableOrMovableClass { + public: + ze_result_t getMemorySize(uint64_t &maxSize, uint64_t &allocSize) override; + ze_result_t getMemHealth(zet_mem_health_t &memHealth) override; + LinuxMemoryImp(OsSysman *pOsSysman); + LinuxMemoryImp() = default; + ~LinuxMemoryImp() override = default; + + protected: + NEO::Drm *pDrm = nullptr; +}; +} // namespace L0 diff --git a/level_zero/tools/source/sysman/memory/memory_imp.cpp b/level_zero/tools/source/sysman/memory/memory_imp.cpp index 59c73b7812..99be4408b1 100644 --- a/level_zero/tools/source/sysman/memory/memory_imp.cpp +++ b/level_zero/tools/source/sysman/memory/memory_imp.cpp @@ -7,8 +7,6 @@ #include "level_zero/tools/source/sysman/memory/memory_imp.h" -#include "level_zero/core/source/device/device.h" - namespace L0 { ze_result_t MemoryImp::memoryGetBandwidth(zet_mem_bandwidth_t *pBandwidth) { @@ -24,11 +22,22 @@ ze_result_t MemoryImp::memoryGetState(zet_mem_state_t *pState) { } ze_result_t MemoryImp::memoryGetProperties(zet_mem_properties_t *pProperties) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + *pProperties = memoryProperties; + return ZE_RESULT_SUCCESS; } + +void MemoryImp::init() { + memoryProperties.type = ZET_MEM_TYPE_DDR; + memoryProperties.onSubdevice = false; + memoryProperties.subdeviceId = 0; + memoryProperties.physicalSize = 0; +} + MemoryImp::MemoryImp(OsSysman *pOsSysman, ze_device_handle_t hDevice) { pOsMemory = OsMemory::create(pOsSysman); hCoreDevice = hDevice; + + init(); } MemoryImp::~MemoryImp() { diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/CMakeLists.txt index a7ea14df84..4c21ed4129 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/CMakeLists.txt +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/CMakeLists.txt @@ -4,10 +4,20 @@ # SPDX-License-Identifier: MIT # +if(SUPPORT_DG1) +set(L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/dg1/test_sysman_memory.cpp +) +else() +set(L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_memory.cpp +) +endif() + if(UNIX) target_sources(${TARGET_NAME} - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/test_sysman_memory.cpp - ) + PRIVATE + ${L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX}) endif() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/dg1/mock_memory.h b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/dg1/mock_memory.h new file mode 100644 index 0000000000..6138360063 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/dg1/mock_memory.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/os_interface/linux/memory_info_impl.h" + +#include "level_zero/core/test/unit_tests/mock.h" +#include "level_zero/tools/source/sysman/memory/linux/dg1/os_memory_imp.h" +#include "level_zero/tools/source/sysman/memory/memory_imp.h" + +#include "sysman/linux/os_sysman_imp.h" +using ::testing::_; +using namespace NEO; +constexpr uint64_t probedSizeRegionZero = 8 * GB; +constexpr uint64_t probedSizeRegionOne = 16 * GB; +constexpr uint64_t probedSizeRegionTwo = 4 * GB; +constexpr uint64_t probedSizeRegionThree = 16 * GB; +constexpr uint64_t unallocatedSizeRegionZero = 6 * GB; +constexpr uint64_t unallocatedSizeRegionOne = 12 * GB; +constexpr uint64_t unallocatedSizeRegionTwo = 25 * GB; +constexpr uint64_t unallocatedSizeRegionThree = 3 * GB; +namespace L0 { +namespace ult { +class MemoryNeoDrm : public Drm { + public: + using Drm::memoryInfo; + const int mockFd = 33; + MemoryNeoDrm(RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique(mockFd, ""), rootDeviceEnvironment) {} +}; + +template <> +struct Mock : public MemoryNeoDrm { + Mock(RootDeviceEnvironment &rootDeviceEnvironment) : MemoryNeoDrm(rootDeviceEnvironment) {} + bool queryMemoryInfoMockPositiveTest() { + drm_i915_memory_region_info regionInfo[4] = {}; + regionInfo[0].region = {I915_MEMORY_CLASS_SYSTEM, 0}; + regionInfo[0].probed_size = probedSizeRegionZero; + regionInfo[0].unallocated_size = unallocatedSizeRegionZero; + regionInfo[1].region = {I915_MEMORY_CLASS_DEVICE, 0}; + regionInfo[1].probed_size = probedSizeRegionOne; + regionInfo[1].unallocated_size = unallocatedSizeRegionOne; + regionInfo[2].region = {I915_MEMORY_CLASS_STOLEN_DEVICE, 0}; + regionInfo[2].probed_size = probedSizeRegionTwo; + regionInfo[2].unallocated_size = unallocatedSizeRegionTwo; + regionInfo[3].region = {I915_MEMORY_CLASS_STOLEN_SYSTEM, 0}; + regionInfo[3].probed_size = probedSizeRegionThree; + regionInfo[3].unallocated_size = unallocatedSizeRegionThree; + + this->memoryInfo.reset(new MemoryInfoImpl(regionInfo, 4)); + return true; + } + + bool queryMemoryInfoMockReturnFalse() { + return false; + } + + bool queryMemoryInfoMockWithoutDevice() { + drm_i915_memory_region_info regionInfo[3] = {}; + regionInfo[0].region = {I915_MEMORY_CLASS_SYSTEM, 0}; + regionInfo[0].probed_size = 8 * GB; + regionInfo[0].unallocated_size = 6 * GB; + regionInfo[1].region = {I915_MEMORY_CLASS_STOLEN_SYSTEM, 0}; + regionInfo[1].probed_size = 16 * GB; + regionInfo[1].unallocated_size = 12 * GB; + regionInfo[2].region = {I915_MEMORY_CLASS_STOLEN_DEVICE, 0}; + regionInfo[2].probed_size = 32 * GB; + regionInfo[2].unallocated_size = 25 * GB; + + this->memoryInfo.reset(new MemoryInfoImpl(regionInfo, 3)); + return true; + } + + MOCK_METHOD(bool, queryMemoryInfo, (), (override)); +}; + +class PublicLinuxMemoryImp : public L0::LinuxMemoryImp { + public: + using LinuxMemoryImp::pDrm; +}; + +} // namespace ult +} // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/dg1/test_sysman_memory.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/dg1/test_sysman_memory.cpp new file mode 100644 index 0000000000..1b1877df45 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/dg1/test_sysman_memory.cpp @@ -0,0 +1,119 @@ +/* + * 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/sysman_imp.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "mock_memory.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::InSequence; +using ::testing::Invoke; +using ::testing::Matcher; +using ::testing::NiceMock; +using ::testing::Return; +namespace L0 { +namespace ult { + +class SysmanMemoryFixture : public DeviceFixture, public ::testing::Test { + + protected: + std::unique_ptr sysmanImp; + zet_sysman_handle_t hSysman; + ze_device_handle_t hCoreDevice; + Mock *pDrm = nullptr; + + OsMemory *pOsMemory = nullptr; + PublicLinuxMemoryImp linuxMemoryImp; + MemoryImp *pMemoryImp = nullptr; + zet_sysman_mem_handle_t hSysmanMemory; + + void SetUp() override { + DeviceFixture::SetUp(); + sysmanImp = std::make_unique(device->toHandle()); + hCoreDevice = device->toHandle(); + auto executionEnvironment = neoDevice->getExecutionEnvironment(); + auto &rootDeviceEnvironment = *executionEnvironment->rootDeviceEnvironments[0]; + pDrm = new NiceMock>(rootDeviceEnvironment); + linuxMemoryImp.pDrm = pDrm; + pOsMemory = static_cast(&linuxMemoryImp); + + ON_CALL(*pDrm, queryMemoryInfo()) + .WillByDefault(::testing::Invoke(pDrm, &Mock::queryMemoryInfoMockPositiveTest)); + pMemoryImp = new MemoryImp(); + pMemoryImp->pOsMemory = pOsMemory; + pMemoryImp->init(); + sysmanImp->pMemoryHandleContext->handleList.push_back(pMemoryImp); + hSysmanMemory = pMemoryImp->toHandle(); + hSysman = sysmanImp->toHandle(); + } + void TearDown() override { + pMemoryImp->pOsMemory = nullptr; + + if (pDrm != nullptr) { + delete pDrm; + pDrm = nullptr; + } + DeviceFixture::TearDown(); + } +}; + +TEST_F(SysmanMemoryFixture, GivenComponentCountZeroWhenCallingZetSysmanMemoryGetThenZeroCountIsReturnedAndVerifySysmanMemoryGetCallSucceeds) { + uint32_t count = 0; + ze_result_t result = zetSysmanMemoryGet(hSysman, &count, NULL); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_GE(count, 1u); + uint32_t testcount = count + 1; + + result = zetSysmanMemoryGet(hSysman, &testcount, NULL); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(testcount, count); +} + +TEST_F(SysmanMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetPropertiesThenVerifySysmanMemoryGetPropertiesCallSucceeds) { + zet_mem_properties_t properties; + + ze_result_t result = zetSysmanMemoryGetProperties(hSysmanMemory, &properties); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(ZET_MEM_TYPE_DDR, properties.type); + EXPECT_FALSE(properties.onSubdevice); + EXPECT_EQ(0u, properties.subdeviceId); + EXPECT_EQ(0u, properties.physicalSize); +} + +TEST_F(SysmanMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStateThenVerifySysmanMemoryGetStateCallSucceeds) { + zet_mem_state_t state; + + EXPECT_EQ(ZE_RESULT_SUCCESS, zetSysmanMemoryGetState(hSysmanMemory, &state)); + EXPECT_EQ(ZET_MEM_HEALTH_OK, state.health); + EXPECT_EQ((probedSizeRegionOne - unallocatedSizeRegionOne), state.allocatedSize); + EXPECT_EQ(probedSizeRegionOne, state.maxSize); +} + +TEST_F(SysmanMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStateAndIfQueryMemoryInfoFailsThenErrorIsReturned) { + zet_mem_state_t state; + + ON_CALL(*pDrm, queryMemoryInfo()) + .WillByDefault(::testing::Invoke(pDrm, &Mock::queryMemoryInfoMockReturnFalse)); + EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zetSysmanMemoryGetState(hSysmanMemory, &state)); +} + +TEST_F(SysmanMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStateAndIfQueryMemoryDidntProvideDeviceMemoryThenErrorIsReturned) { + zet_mem_state_t state; + + ON_CALL(*pDrm, queryMemoryInfo()) + .WillByDefault(::testing::Invoke(pDrm, &Mock::queryMemoryInfoMockWithoutDevice)); + EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zetSysmanMemoryGetState(hSysmanMemory, &state)); +} +} // namespace ult +} // namespace L0