From 2fa30d7d6c926bd8ad68d41baa2cad6edd9678c0 Mon Sep 17 00:00:00 2001 From: Daniel Enriquez Date: Thu, 6 Aug 2020 19:27:43 -0700 Subject: [PATCH] Update memory component to latest spec. Change-Id: I766a3b87d240d26118cf72ea0717bc504784e764 --- level_zero/api/sysman/zes_sysman.cpp | 8 +- .../tools/source/sysman/linux/os_sysman_imp.h | 2 +- .../sysman/memory/linux/dg1/os_memory_imp.cpp | 28 +- .../sysman/memory/linux/dg1/os_memory_imp.h | 5 +- .../sysman/memory/linux/os_memory_imp.cpp | 8 +- .../sysman/memory/linux/os_memory_imp.h | 5 +- .../tools/source/sysman/memory/memory.cpp | 2 +- .../tools/source/sysman/memory/memory_imp.cpp | 17 +- .../tools/source/sysman/memory/memory_imp.h | 3 +- .../tools/source/sysman/memory/os_memory.h | 5 +- .../sysman/memory/windows/CMakeLists.txt | 1 + .../sysman/memory/windows/os_memory_imp.cpp | 177 ++++++++++++- .../sysman/memory/windows/os_memory_imp.h | 32 +++ level_zero/tools/source/sysman/sysman.h | 1 + level_zero/tools/source/sysman/sysman_imp.cpp | 8 + level_zero/tools/source/sysman/sysman_imp.h | 2 + .../tools/source/sysman/windows/kmd_sys.h | 4 +- .../source/sysman/windows/os_sysman_imp.cpp | 4 + .../source/sysman/windows/os_sysman_imp.h | 3 +- .../unit_tests/sources/sysman/CMakeLists.txt | 10 +- .../sysman/engine/linux/test_zes_engine.cpp | 2 +- .../linux/test_zes_fabric_port.cpp | 2 +- .../frequency/linux/test_zes_frequency.cpp | 2 +- .../linux/test_zes_global_operations.cpp | 2 +- .../sources/sysman/linux/CMakeLists.txt | 15 ++ .../sysman/{ => linux}/mock_sysman_fixture.h | 1 + .../sysman/{ => linux}/test_sysman.cpp | 2 +- .../sysman/memory/linux/CMakeLists.txt | 9 +- .../sysman/memory/linux/dg1/mock_memory.h | 13 +- .../memory/linux/dg1/test_sysman_memory.cpp | 248 +++++++++++++----- .../memory/linux/test_sysman_memory.cpp | 150 +++++++++-- .../sysman/memory/windows/CMakeLists.txt | 14 + .../sysman/memory/windows/mock_memory.h | 135 ++++++++++ .../sysman/memory/windows/test_zes_memory.cpp | 206 +++++++++++++++ .../sources/sysman/pci/linux/test_zes_pci.cpp | 2 +- .../sysman/power/linux/test_zes_power.cpp | 2 +- .../sources/sysman/ras/linux/test_zes_ras.cpp | 2 +- .../scheduler/linux/test_zes_scheduler.cpp | 2 +- .../standby/linux/test_zes_sysman_standby.cpp | 2 +- .../linux/test_zes_temperature.cpp | 2 +- .../sources/sysman/windows/CMakeLists.txt | 2 + .../sysman/windows/mock_kmd_sys_manager.h | 11 +- .../sysman/windows/mock_sysman_fixture.h | 57 ++++ .../sources/sysman/windows/test_sysman.cpp | 31 +++ .../sysman/windows/test_sysman_manager.cpp | 5 +- 45 files changed, 1057 insertions(+), 187 deletions(-) create mode 100644 level_zero/tools/source/sysman/memory/windows/os_memory_imp.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/linux/CMakeLists.txt rename level_zero/tools/test/unit_tests/sources/sysman/{ => linux}/mock_sysman_fixture.h (98%) rename level_zero/tools/test/unit_tests/sources/sysman/{ => linux}/test_sysman.cpp (97%) create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/memory/windows/CMakeLists.txt create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/memory/windows/mock_memory.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/windows/mock_sysman_fixture.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/windows/test_sysman.cpp diff --git a/level_zero/api/sysman/zes_sysman.cpp b/level_zero/api/sysman/zes_sysman.cpp index a3f0d5e7cf..a6d46f3dfa 100644 --- a/level_zero/api/sysman/zes_sysman.cpp +++ b/level_zero/api/sysman/zes_sysman.cpp @@ -398,28 +398,28 @@ zesDeviceEnumMemoryModules( zes_device_handle_t hDevice, uint32_t *pCount, zes_mem_handle_t *phMemory) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::SysmanDevice::fromHandle(hDevice)->memoryGet(pCount, phMemory); } ZE_APIEXPORT ze_result_t ZE_APICALL zesMemoryGetProperties( zes_mem_handle_t hMemory, zes_mem_properties_t *pProperties) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::Memory::fromHandle(hMemory)->memoryGetProperties(pProperties); } ZE_APIEXPORT ze_result_t ZE_APICALL zesMemoryGetState( zes_mem_handle_t hMemory, zes_mem_state_t *pState) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::Memory::fromHandle(hMemory)->memoryGetState(pState); } ZE_APIEXPORT ze_result_t ZE_APICALL zesMemoryGetBandwidth( zes_mem_handle_t hMemory, zes_mem_bandwidth_t *pBandwidth) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::Memory::fromHandle(hMemory)->memoryGetBandwidth(pBandwidth); } ZE_APIEXPORT ze_result_t ZE_APICALL diff --git a/level_zero/tools/source/sysman/linux/os_sysman_imp.h b/level_zero/tools/source/sysman/linux/os_sysman_imp.h index 71f5e1f827..357e679a4b 100644 --- a/level_zero/tools/source/sysman/linux/os_sysman_imp.h +++ b/level_zero/tools/source/sysman/linux/os_sysman_imp.h @@ -38,11 +38,11 @@ class LinuxSysmanImp : public OsSysman, NEO::NonCopyableOrMovableClass { ProcfsAccess *pProcfsAccess = nullptr; SysfsAccess *pSysfsAccess = nullptr; PlatformMonitoringTech *pPmt = nullptr; + NEO::Drm *pDrm = nullptr; private: LinuxSysmanImp() = delete; SysmanDeviceImp *pParentSysmanDeviceImp = nullptr; - NEO::Drm *pDrm = nullptr; }; } // namespace L0 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 index 331f813b82..4d4ca07192 100644 --- 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 @@ -18,11 +18,29 @@ LinuxMemoryImp::LinuxMemoryImp(OsSysman *pOsSysman) { pDrm = &pLinuxSysmanImp->getDrm(); } -ze_result_t LinuxMemoryImp::getMemorySize(uint64_t &maxSize, uint64_t &allocSize) { +ze_result_t LinuxMemoryImp::getProperties(zes_mem_properties_t *pProperties) { + pProperties->type = ZES_MEM_TYPE_DDR; + pProperties->location = ZES_MEM_LOC_DEVICE; + pProperties->onSubdevice = false; + pProperties->subdeviceId = 0; + pProperties->busWidth = -1; + pProperties->numChannels = -1; + pProperties->physicalSize = 0; + return ZE_RESULT_SUCCESS; +} + +ze_result_t LinuxMemoryImp::getBandwidth(zes_mem_bandwidth_t *pBandwidth) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + +ze_result_t LinuxMemoryImp::getState(zes_mem_state_t *pState) { if (pDrm->queryMemoryInfo() == false) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } + + pState->health = ZES_MEM_HEALTH_OK; + 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); @@ -30,13 +48,9 @@ ze_result_t LinuxMemoryImp::getMemorySize(uint64_t &maxSize, uint64_t &allocSize 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(zes_mem_health_t &memHealth) { - memHealth = ZES_MEM_HEALTH_OK; + pState->free = region->unallocated_size; + pState->size = region->probed_size; return ZE_RESULT_SUCCESS; } 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 index c3222c5229..1dbfc5b426 100644 --- 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 @@ -17,8 +17,9 @@ class SysfsAccess; class LinuxMemoryImp : public OsMemory, NEO::NonCopyableOrMovableClass { public: - ze_result_t getMemorySize(uint64_t &maxSize, uint64_t &allocSize) override; - ze_result_t getMemHealth(zes_mem_health_t &memHealth) override; + ze_result_t getProperties(zes_mem_properties_t *pProperties) override; + ze_result_t getBandwidth(zes_mem_bandwidth_t *pBandwidth) override; + ze_result_t getState(zes_mem_state_t *pState) override; LinuxMemoryImp(OsSysman *pOsSysman); LinuxMemoryImp() = default; ~LinuxMemoryImp() override = default; diff --git a/level_zero/tools/source/sysman/memory/linux/os_memory_imp.cpp b/level_zero/tools/source/sysman/memory/linux/os_memory_imp.cpp index 0fbde2339f..8e9412959d 100644 --- a/level_zero/tools/source/sysman/memory/linux/os_memory_imp.cpp +++ b/level_zero/tools/source/sysman/memory/linux/os_memory_imp.cpp @@ -9,11 +9,15 @@ namespace L0 { -ze_result_t LinuxMemoryImp::getMemorySize(uint64_t &maxSize, uint64_t &allocSize) { +ze_result_t LinuxMemoryImp::getProperties(zes_mem_properties_t *pProperties) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } -ze_result_t LinuxMemoryImp::getMemHealth(zes_mem_health_t &memHealth) { +ze_result_t LinuxMemoryImp::getBandwidth(zes_mem_bandwidth_t *pBandwidth) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + +ze_result_t LinuxMemoryImp::getState(zes_mem_state_t *pState) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } diff --git a/level_zero/tools/source/sysman/memory/linux/os_memory_imp.h b/level_zero/tools/source/sysman/memory/linux/os_memory_imp.h index dd04595227..8786b37fe0 100644 --- a/level_zero/tools/source/sysman/memory/linux/os_memory_imp.h +++ b/level_zero/tools/source/sysman/memory/linux/os_memory_imp.h @@ -16,8 +16,9 @@ class SysfsAccess; class LinuxMemoryImp : public OsMemory, NEO::NonCopyableOrMovableClass { public: - ze_result_t getMemorySize(uint64_t &maxSize, uint64_t &allocSize) override; - ze_result_t getMemHealth(zes_mem_health_t &memHealth) override; + ze_result_t getProperties(zes_mem_properties_t *pProperties) override; + ze_result_t getBandwidth(zes_mem_bandwidth_t *pBandwidth) override; + ze_result_t getState(zes_mem_state_t *pState) override; LinuxMemoryImp(OsSysman *pOsSysman); LinuxMemoryImp() = default; ~LinuxMemoryImp() override = default; diff --git a/level_zero/tools/source/sysman/memory/memory.cpp b/level_zero/tools/source/sysman/memory/memory.cpp index 8173d93fc9..5617a5af30 100644 --- a/level_zero/tools/source/sysman/memory/memory.cpp +++ b/level_zero/tools/source/sysman/memory/memory.cpp @@ -25,7 +25,7 @@ ze_result_t MemoryHandleContext::init() { isLmemSupported = device->getDriverHandle()->getMemoryManager()->isLocalMemorySupported(device->getRootDeviceIndex()); if (isLmemSupported) { - Memory *pMemory = new MemoryImp(pOsSysman, hCoreDevice); + Memory *pMemory = new MemoryImp(pOsSysman); handleList.push_back(pMemory); } return ZE_RESULT_SUCCESS; diff --git a/level_zero/tools/source/sysman/memory/memory_imp.cpp b/level_zero/tools/source/sysman/memory/memory_imp.cpp index 35fad6f626..3cf490a052 100644 --- a/level_zero/tools/source/sysman/memory/memory_imp.cpp +++ b/level_zero/tools/source/sysman/memory/memory_imp.cpp @@ -10,15 +10,11 @@ namespace L0 { ze_result_t MemoryImp::memoryGetBandwidth(zes_mem_bandwidth_t *pBandwidth) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return pOsMemory->getBandwidth(pBandwidth); } ze_result_t MemoryImp::memoryGetState(zes_mem_state_t *pState) { - ze_result_t result = pOsMemory->getMemorySize(pState->size, pState->size); - if (ZE_RESULT_SUCCESS != result) { - return result; - } - return pOsMemory->getMemHealth(pState->health); + return pOsMemory->getState(pState); } ze_result_t MemoryImp::memoryGetProperties(zes_mem_properties_t *pProperties) { @@ -27,16 +23,11 @@ ze_result_t MemoryImp::memoryGetProperties(zes_mem_properties_t *pProperties) { } void MemoryImp::init() { - memoryProperties.type = ZES_MEM_TYPE_DDR; - memoryProperties.onSubdevice = false; - memoryProperties.subdeviceId = 0; - memoryProperties.physicalSize = 0; + pOsMemory->getProperties(&memoryProperties); } -MemoryImp::MemoryImp(OsSysman *pOsSysman, ze_device_handle_t hDevice) { +MemoryImp::MemoryImp(OsSysman *pOsSysman) { pOsMemory = OsMemory::create(pOsSysman); - hCoreDevice = hDevice; - init(); } diff --git a/level_zero/tools/source/sysman/memory/memory_imp.h b/level_zero/tools/source/sysman/memory/memory_imp.h index 4d3c9e9971..925526a669 100644 --- a/level_zero/tools/source/sysman/memory/memory_imp.h +++ b/level_zero/tools/source/sysman/memory/memory_imp.h @@ -20,7 +20,7 @@ class MemoryImp : public Memory, NEO::NonCopyableOrMovableClass { ze_result_t memoryGetBandwidth(zes_mem_bandwidth_t *pBandwidth) override; ze_result_t memoryGetState(zes_mem_state_t *pState) override; - MemoryImp(OsSysman *pOsSysman, ze_device_handle_t hDevice); + MemoryImp(OsSysman *pOsSysman); ~MemoryImp() override; MemoryImp() = default; @@ -29,7 +29,6 @@ class MemoryImp : public Memory, NEO::NonCopyableOrMovableClass { private: zes_mem_properties_t memoryProperties = {}; - ze_device_handle_t hCoreDevice = {}; }; } // namespace L0 diff --git a/level_zero/tools/source/sysman/memory/os_memory.h b/level_zero/tools/source/sysman/memory/os_memory.h index e8b3caae17..6b4ac07d5e 100644 --- a/level_zero/tools/source/sysman/memory/os_memory.h +++ b/level_zero/tools/source/sysman/memory/os_memory.h @@ -14,8 +14,9 @@ namespace L0 { struct OsSysman; class OsMemory { public: - virtual ze_result_t getMemorySize(uint64_t &maxSize, uint64_t &allocSize) = 0; - virtual ze_result_t getMemHealth(zes_mem_health_t &memHealth) = 0; + virtual ze_result_t getProperties(zes_mem_properties_t *pProperties) = 0; + virtual ze_result_t getBandwidth(zes_mem_bandwidth_t *pBandwidth) = 0; + virtual ze_result_t getState(zes_mem_state_t *pState) = 0; static OsMemory *create(OsSysman *pOsSysman); virtual ~OsMemory() {} }; diff --git a/level_zero/tools/source/sysman/memory/windows/CMakeLists.txt b/level_zero/tools/source/sysman/memory/windows/CMakeLists.txt index 5bb81ac0f4..a640024c0b 100755 --- a/level_zero/tools/source/sysman/memory/windows/CMakeLists.txt +++ b/level_zero/tools/source/sysman/memory/windows/CMakeLists.txt @@ -5,6 +5,7 @@ # set(L0_SRCS_TOOLS_SYSMAN_MEMORY_WINDOWS + ${CMAKE_CURRENT_SOURCE_DIR}/os_memory_imp.h ${CMAKE_CURRENT_SOURCE_DIR}/os_memory_imp.cpp ) diff --git a/level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp b/level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp index 84c1af6485..54990f55ed 100644 --- a/level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp +++ b/level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp @@ -5,27 +5,182 @@ * */ -#include "sysman/memory/os_memory.h" +#include "sysman/memory/windows/os_memory_imp.h" namespace L0 { -class WddmMemoryImp : public OsMemory { +ze_result_t WddmMemoryImp::getProperties(zes_mem_properties_t *pProperties) { + ze_result_t status = ZE_RESULT_SUCCESS; + uint32_t valueSmall = 0; + uint64_t valueLarge = 0; + KmdSysman::RequestProperty request; + KmdSysman::ResponseProperty response; - public: - ze_result_t getMemorySize(uint64_t &maxSize, uint64_t &allocSize) override; - ze_result_t getMemHealth(zes_mem_health_t &memHealth) override; -}; + pProperties->onSubdevice = false; + pProperties->subdeviceId = 0; -ze_result_t WddmMemoryImp::getMemorySize(uint64_t &maxSize, uint64_t &allocSize) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + request.commandId = KmdSysman::Command::Get; + request.componentId = KmdSysman::Component::MemoryComponent; + request.requestId = KmdSysman::Requests::Memory::MemoryType; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + switch (valueSmall) { + case KmdSysman::MemoryType::DDR4: { + pProperties->type = ZES_MEM_TYPE_DDR4; + } break; + case KmdSysman::MemoryType::DDR5: { + pProperties->type = ZES_MEM_TYPE_DDR5; + } break; + case KmdSysman::MemoryType::LPDDR5: { + pProperties->type = ZES_MEM_TYPE_LPDDR5; + } break; + case KmdSysman::MemoryType::LPDDR4: { + pProperties->type = ZES_MEM_TYPE_LPDDR4; + } break; + case KmdSysman::MemoryType::DDR3: { + pProperties->type = ZES_MEM_TYPE_DDR3; + } break; + case KmdSysman::MemoryType::LPDDR3: { + pProperties->type = ZES_MEM_TYPE_LPDDR3; + } break; + default: { + pProperties->type = ZES_MEM_TYPE_FORCE_UINT32; + } break; + } + + request.requestId = KmdSysman::Requests::Memory::PhysicalSize; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueLarge, sizeof(uint64_t), response.dataBuffer, sizeof(uint64_t)); + pProperties->physicalSize = valueLarge; + + request.requestId = KmdSysman::Requests::Memory::NumChannels; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + pProperties->numChannels = valueSmall; + + request.requestId = KmdSysman::Requests::Memory::MemoryLocation; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + pProperties->location = static_cast(valueSmall); + + request.requestId = KmdSysman::Requests::Memory::MemoryWidth; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + pProperties->busWidth = valueSmall; + + return ZE_RESULT_SUCCESS; } -ze_result_t WddmMemoryImp::getMemHealth(zes_mem_health_t &memHealth) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +ze_result_t WddmMemoryImp::getBandwidth(zes_mem_bandwidth_t *pBandwidth) { + ze_result_t status = ZE_RESULT_SUCCESS; + uint32_t valueSmall = 0; + KmdSysman::RequestProperty request; + KmdSysman::ResponseProperty response; + + pBandwidth->writeCounter = 0; + + request.commandId = KmdSysman::Command::Get; + request.componentId = KmdSysman::Component::MemoryComponent; + request.requestId = KmdSysman::Requests::Memory::MaxBandwidth; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + pBandwidth->maxBandwidth = valueSmall; + + request.requestId = KmdSysman::Requests::Memory::CurrentBandwidthRead; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + pBandwidth->readCounter = valueSmall * MbpsToBytesPerSecond; + + std::chrono::time_point ts = std::chrono::steady_clock::now(); + pBandwidth->timestamp = std::chrono::duration_cast(ts.time_since_epoch()).count(); + + return ZE_RESULT_SUCCESS; +} + +ze_result_t WddmMemoryImp::getState(zes_mem_state_t *pState) { + ze_result_t status = ZE_RESULT_SUCCESS; + uint64_t valueLarge = 0; + KmdSysman::RequestProperty request; + KmdSysman::ResponseProperty response; + + pState->health = ZES_MEM_HEALTH_OK; + + request.commandId = KmdSysman::Command::Get; + request.componentId = KmdSysman::Component::MemoryComponent; + request.requestId = KmdSysman::Requests::Memory::PhysicalSize; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueLarge, sizeof(uint64_t), response.dataBuffer, sizeof(uint64_t)); + pState->size = valueLarge; + + request.requestId = KmdSysman::Requests::Memory::CurrentFreeMemorySize; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueLarge, sizeof(uint64_t), response.dataBuffer, sizeof(uint64_t)); + pState->free = valueLarge; + + return ZE_RESULT_SUCCESS; +} + +WddmMemoryImp::WddmMemoryImp(OsSysman *pOsSysman) { + WddmSysmanImp *pWddmSysmanImp = static_cast(pOsSysman); + pKmdSysManager = &pWddmSysmanImp->getKmdSysManager(); } OsMemory *OsMemory::create(OsSysman *pOsSysman) { - WddmMemoryImp *pWddmMemoryImp = new WddmMemoryImp(); + WddmMemoryImp *pWddmMemoryImp = new WddmMemoryImp(pOsSysman); return static_cast(pWddmMemoryImp); } diff --git a/level_zero/tools/source/sysman/memory/windows/os_memory_imp.h b/level_zero/tools/source/sysman/memory/windows/os_memory_imp.h new file mode 100644 index 0000000000..482e85049c --- /dev/null +++ b/level_zero/tools/source/sysman/memory/windows/os_memory_imp.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/non_copyable_or_moveable.h" + +#include "sysman/memory/os_memory.h" +#include "sysman/windows/os_sysman_imp.h" + +namespace L0 { +class KmdSysManager; + +constexpr uint32_t MbpsToBytesPerSecond = 125000; + +class WddmMemoryImp : public OsMemory, NEO::NonCopyableOrMovableClass { + public: + ze_result_t getProperties(zes_mem_properties_t *pProperties) override; + ze_result_t getBandwidth(zes_mem_bandwidth_t *pBandwidth) override; + ze_result_t getState(zes_mem_state_t *pState) override; + WddmMemoryImp(OsSysman *pOsSysman); + WddmMemoryImp() = default; + ~WddmMemoryImp() override = default; + + protected: + KmdSysManager *pKmdSysManager = nullptr; +}; + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/sysman.h b/level_zero/tools/source/sysman/sysman.h index 4d0fc8c10c..10b4a7f7ab 100644 --- a/level_zero/tools/source/sysman/sysman.h +++ b/level_zero/tools/source/sysman/sysman.h @@ -47,6 +47,7 @@ struct SysmanDevice : _ze_device_handle_t { virtual ze_result_t pciGetStats(zes_pci_stats_t *pStats) = 0; virtual ze_result_t schedulerGet(uint32_t *pCount, zes_sched_handle_t *phScheduler) = 0; virtual ze_result_t rasGet(uint32_t *pCount, zes_ras_handle_t *phRas) = 0; + virtual ze_result_t memoryGet(uint32_t *pCount, zes_mem_handle_t *phMemory) = 0; virtual ~SysmanDevice() = default; }; diff --git a/level_zero/tools/source/sysman/sysman_imp.cpp b/level_zero/tools/source/sysman/sysman_imp.cpp index a12739ffbd..3d1197e28d 100644 --- a/level_zero/tools/source/sysman/sysman_imp.cpp +++ b/level_zero/tools/source/sysman/sysman_imp.cpp @@ -30,11 +30,13 @@ SysmanDeviceImp::SysmanDeviceImp(ze_device_handle_t hDevice) { pEngineHandleContext = new EngineHandleContext(pOsSysman); pSchedulerHandleContext = new SchedulerHandleContext(pOsSysman); pRasHandleContext = new RasHandleContext(pOsSysman); + pMemoryHandleContext = new MemoryHandleContext(pOsSysman, hCoreDevice); pGlobalOperations = new GlobalOperationsImp(pOsSysman, hCoreDevice); } SysmanDeviceImp::~SysmanDeviceImp() { freeResource(pGlobalOperations); + freeResource(pMemoryHandleContext); freeResource(pRasHandleContext); freeResource(pSchedulerHandleContext); freeResource(pEngineHandleContext); @@ -76,6 +78,9 @@ void SysmanDeviceImp::init() { if (pRasHandleContext) { pRasHandleContext->init(); } + if (pMemoryHandleContext) { + pMemoryHandleContext->init(); + } if (pGlobalOperations) { pGlobalOperations->init(); } @@ -145,4 +150,7 @@ ze_result_t SysmanDeviceImp::rasGet(uint32_t *pCount, zes_ras_handle_t *phRas) { return pRasHandleContext->rasGet(pCount, phRas); } +ze_result_t SysmanDeviceImp::memoryGet(uint32_t *pCount, zes_mem_handle_t *phMemory) { + return pMemoryHandleContext->memoryGet(pCount, phMemory); +} } // namespace L0 diff --git a/level_zero/tools/source/sysman/sysman_imp.h b/level_zero/tools/source/sysman/sysman_imp.h index 1025978e48..866670c155 100644 --- a/level_zero/tools/source/sysman/sysman_imp.h +++ b/level_zero/tools/source/sysman/sysman_imp.h @@ -36,6 +36,7 @@ struct SysmanDeviceImp : SysmanDevice, NEO::NonCopyableOrMovableClass { EngineHandleContext *pEngineHandleContext = nullptr; SchedulerHandleContext *pSchedulerHandleContext = nullptr; RasHandleContext *pRasHandleContext = nullptr; + MemoryHandleContext *pMemoryHandleContext = nullptr; ze_result_t powerGet(uint32_t *pCount, zes_pwr_handle_t *phPower) override; ze_result_t frequencyGet(uint32_t *pCount, zes_freq_handle_t *phFrequency) override; @@ -53,6 +54,7 @@ struct SysmanDeviceImp : SysmanDevice, NEO::NonCopyableOrMovableClass { ze_result_t pciGetStats(zes_pci_stats_t *pStats) override; ze_result_t schedulerGet(uint32_t *pCount, zes_sched_handle_t *phScheduler) override; ze_result_t rasGet(uint32_t *pCount, zes_ras_handle_t *phRas) override; + ze_result_t memoryGet(uint32_t *pCount, zes_mem_handle_t *phMemory) override; private: template diff --git a/level_zero/tools/source/sysman/windows/kmd_sys.h b/level_zero/tools/source/sysman/windows/kmd_sys.h index 9f92252f3e..a2f71acea0 100644 --- a/level_zero/tools/source/sysman/windows/kmd_sys.h +++ b/level_zero/tools/source/sysman/windows/kmd_sys.h @@ -468,7 +468,7 @@ struct RequestProperty { uint32_t componentId; uint32_t paramInfo; uint32_t dataSize; - uint8_t dataBuffer[MaxPropertyBufferSize]; + uint8_t dataBuffer[MaxPropertyBufferSize] = {0}; }; struct ResponseProperty { @@ -510,7 +510,7 @@ struct ResponseProperty { uint32_t returnCode; uint32_t componentId; uint32_t dataSize; - uint8_t dataBuffer[MaxPropertyBufferSize]; + uint8_t dataBuffer[MaxPropertyBufferSize] = {0}; }; } // namespace KmdSysman } // namespace L0 \ No newline at end of file diff --git a/level_zero/tools/source/sysman/windows/os_sysman_imp.cpp b/level_zero/tools/source/sysman/windows/os_sysman_imp.cpp index 4acb6ece26..3e302436de 100644 --- a/level_zero/tools/source/sysman/windows/os_sysman_imp.cpp +++ b/level_zero/tools/source/sysman/windows/os_sysman_imp.cpp @@ -36,6 +36,10 @@ KmdSysManager &WddmSysmanImp::getKmdSysManager() { return *pKmdSysManager; } +WddmSysmanImp::WddmSysmanImp(SysmanDeviceImp *pParentSysmanDeviceImp) { + this->pParentSysmanDeviceImp = pParentSysmanDeviceImp; +} + WddmSysmanImp::~WddmSysmanImp() { if (nullptr != pKmdSysManager) { delete pKmdSysManager; diff --git a/level_zero/tools/source/sysman/windows/os_sysman_imp.h b/level_zero/tools/source/sysman/windows/os_sysman_imp.h index 244385badd..a2e563cfca 100644 --- a/level_zero/tools/source/sysman/windows/os_sysman_imp.h +++ b/level_zero/tools/source/sysman/windows/os_sysman_imp.h @@ -18,8 +18,7 @@ namespace L0 { class WddmSysmanImp : public OsSysman, NEO::NonCopyableOrMovableClass { public: - WddmSysmanImp() = default; - WddmSysmanImp(SysmanDeviceImp *pParentSysmanDeviceImp) : pParentSysmanDeviceImp(pParentSysmanDeviceImp){}; + WddmSysmanImp(SysmanDeviceImp *pParentSysmanDeviceImp); ~WddmSysmanImp() override; ze_result_t init() override; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/CMakeLists.txt index 8b52c36ea6..cff7916a34 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/CMakeLists.txt +++ b/level_zero/tools/test/unit_tests/sources/sysman/CMakeLists.txt @@ -4,12 +4,8 @@ # SPDX-License-Identifier: MIT # -if(UNIX) - target_sources(${TARGET_NAME} PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/mock_sysman_fixture.h - ) -endif() +target_sources(${TARGET_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt +) add_subdirectories() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/engine/linux/test_zes_engine.cpp b/level_zero/tools/test/unit_tests/sources/sysman/engine/linux/test_zes_engine.cpp index 5ee472da38..b89685e17d 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/engine/linux/test_zes_engine.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/engine/linux/test_zes_engine.cpp @@ -5,7 +5,7 @@ * */ -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "mock_sysfs_engine.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/fabric_port/linux/test_zes_fabric_port.cpp b/level_zero/tools/test/unit_tests/sources/sysman/fabric_port/linux/test_zes_fabric_port.cpp index 94893261e4..527a5b869a 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/fabric_port/linux/test_zes_fabric_port.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/fabric_port/linux/test_zes_fabric_port.cpp @@ -6,7 +6,7 @@ */ #include "level_zero/tools/test/unit_tests/sources/sysman/fabric_port/linux/mock_fabric_device.h" -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "gmock/gmock.h" #include "gtest/gtest.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/frequency/linux/test_zes_frequency.cpp b/level_zero/tools/test/unit_tests/sources/sysman/frequency/linux/test_zes_frequency.cpp index 86734d8110..34b38327c0 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/frequency/linux/test_zes_frequency.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/frequency/linux/test_zes_frequency.cpp @@ -5,7 +5,7 @@ * */ -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "gmock/gmock.h" #include "gtest/gtest.h" 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 89767c53cc..91b89a6aae 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 @@ -5,7 +5,7 @@ * */ -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "mock_global_operations.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/linux/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/linux/CMakeLists.txt new file mode 100644 index 0000000000..8b52c36ea6 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/linux/CMakeLists.txt @@ -0,0 +1,15 @@ +# +# 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.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_sysman_fixture.h + ) +endif() + +add_subdirectories() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h b/level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h similarity index 98% rename from level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h rename to level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h index e257cc672d..10f177d7c3 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h +++ b/level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h @@ -31,6 +31,7 @@ class SysmanMockDrm : public Drm { class PublicLinuxSysmanImp : public L0::LinuxSysmanImp { public: + using LinuxSysmanImp::pDrm; using LinuxSysmanImp::pFsAccess; using LinuxSysmanImp::pPmt; using LinuxSysmanImp::pProcfsAccess; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/test_sysman.cpp b/level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp similarity index 97% rename from level_zero/tools/test/unit_tests/sources/sysman/test_sysman.cpp rename to level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp index d8867de5b0..b320d152ff 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/test_sysman.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp @@ -7,7 +7,7 @@ #include "test.h" -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" namespace L0 { namespace ult { 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 3c94606af7..d0632fdd13 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 @@ -6,13 +6,14 @@ 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 + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/dg1/mock_memory.h + ${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 + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_memory.cpp ) 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 index 6138360063..3ff4f1ff13 100644 --- 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 @@ -9,11 +9,12 @@ #include "shared/source/os_interface/linux/memory_info_impl.h" #include "level_zero/core/test/unit_tests/mock.h" +#include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.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; @@ -25,6 +26,11 @@ constexpr uint64_t unallocatedSizeRegionTwo = 25 * GB; constexpr uint64_t unallocatedSizeRegionThree = 3 * GB; namespace L0 { namespace ult { + +struct MockMemoryManagerSysman : public MemoryManagerMock { + MockMemoryManagerSysman(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerMock(const_cast(executionEnvironment)) {} +}; + class MemoryNeoDrm : public Drm { public: using Drm::memoryInfo; @@ -77,10 +83,5 @@ struct Mock : public MemoryNeoDrm { 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 index 9866cc6db8..6ac77b187e 100644 --- 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 @@ -5,115 +5,225 @@ * */ -#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" #include "level_zero/tools/source/sysman/sysman_imp.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.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 { - +constexpr uint32_t memoryHandleComponentCount = 1u; +class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { 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; - zes__mem_handle_t hSysmanMemory; + Drm *pOriginalDrm = nullptr; 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); + SysmanDeviceFixture::SetUp(); + + pMemoryManagerOld = device->getDriverHandle()->getMemoryManager(); + pMemoryManager = new ::testing::NiceMock(*neoDevice->getExecutionEnvironment()); + pMemoryManager->localMemorySupported[0] = false; + device->getDriverHandle()->setMemoryManager(pMemoryManager); + + pDrm = new NiceMock>(const_cast(neoDevice->getRootDeviceEnvironment())); + + pSysmanDevice = device->getSysmanHandle(); + pSysmanDeviceImp = static_cast(pSysmanDevice); + pOsSysman = pSysmanDeviceImp->pOsSysman; + pLinuxSysmanImp = static_cast(pOsSysman); + pLinuxSysmanImp->pDrm = pDrm; 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; + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + pSysmanDeviceImp->pMemoryHandleContext->init(); + } + + void TearDown() override { + device->getDriverHandle()->setMemoryManager(pMemoryManagerOld); + SysmanDeviceFixture::TearDown(); + pLinuxSysmanImp->pDrm = pOriginalDrm; if (pDrm != nullptr) { delete pDrm; pDrm = nullptr; } - DeviceFixture::TearDown(); + if (pMemoryManager != nullptr) { + delete pMemoryManager; + pMemoryManager = nullptr; + } } + + void setLocalSupportedAndReinit(bool supported) { + pMemoryManager->localMemorySupported[0] = supported; + + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + pSysmanDeviceImp->pMemoryHandleContext->init(); + } + + std::vector get_memory_handles(uint32_t count) { + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + return handles; + } + + MockMemoryManagerSysman *pMemoryManager = nullptr; + MemoryManager *pMemoryManagerOld; }; -TEST_F(SysmanMemoryFixture, GivenComponentCountZeroWhenCallingZetSysmanMemoryGetThenZeroCountIsReturnedAndVerifySysmanMemoryGetCallSucceeds) { +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(true); + 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); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); } -TEST_F(SysmanMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetPropertiesThenVerifySysmanMemoryGetPropertiesCallSucceeds) { - zes_mem_properties_t properties; +TEST_F(SysmanDeviceMemoryFixture, GivenInvalidComponentCountWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(true); - ze_result_t result = zetSysmanMemoryGetProperties(hSysmanMemory, &properties); + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_EQ(ZES_MEM_TYPE_DDR, properties.type); - EXPECT_FALSE(properties.onSubdevice); - EXPECT_EQ(0u, properties.subdeviceId); - EXPECT_EQ(0u, properties.physicalSize); + count = count + 1; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); } -TEST_F(SysmanMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStateThenVerifySysmanMemoryGetStateCallSucceeds) { - zes_mem_state_t state; +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidPowerHandlesIsReturned) { + setLocalSupportedAndReinit(true); - EXPECT_EQ(ZE_RESULT_SUCCESS, zetSysmanMemoryGetState(hSysmanMemory, &state)); - EXPECT_EQ(ZES_MEM_HEALTH_OK, state.health); - EXPECT_EQ((probedSizeRegionOne - unallocatedSizeRegionOne), state.allocatedSize); - EXPECT_EQ(probedSizeRegionOne, state.maxSize); + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); + + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + for (auto handle : handles) { + EXPECT_NE(handle, nullptr); + } } -TEST_F(SysmanMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStateAndIfQueryMemoryInfoFailsThenErrorIsReturned) { - zes_mem_state_t state; +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenZeroCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(false); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenInvalidComponentCountWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenZeroCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(false); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); + + count = count + 1; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenValidPowerHandlesIsReturned) { + setLocalSupportedAndReinit(false); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); + + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + for (auto handle : handles) { + EXPECT_NE(handle, nullptr); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetPropertiesWithLocalMemoryThenVerifySysmanMemoryGetPropertiesCallSucceeds) { + setLocalSupportedAndReinit(true); + + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties; + + ze_result_t result = zesMemoryGetProperties(handle, &properties); + + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + EXPECT_EQ(properties.type, ZES_MEM_TYPE_DDR); + EXPECT_EQ(properties.location, ZES_MEM_LOC_DEVICE); + EXPECT_FALSE(properties.onSubdevice); + EXPECT_EQ(properties.subdeviceId, 0u); + EXPECT_EQ(properties.physicalSize, 0u); + EXPECT_EQ(properties.numChannels, -1); + EXPECT_EQ(properties.busWidth, -1); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStatehenVerifySysmanMemoryGetStateCallSucceeds) { + setLocalSupportedAndReinit(true); + + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_state_t state; + + ze_result_t result = zesMemoryGetState(handle, &state); + + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + EXPECT_EQ(state.health, ZES_MEM_HEALTH_OK); + EXPECT_EQ(state.size, probedSizeRegionOne); + EXPECT_EQ(state.free, unallocatedSizeRegionOne); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetBandwidthhenVerifySysmanMemoryGetBandwidthCallReturnUnsupportedFeature) { + setLocalSupportedAndReinit(true); + + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_bandwidth_t bandwidth; + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStateAndIfQueryMemoryInfoFailsThenErrorIsReturned) { + setLocalSupportedAndReinit(true); ON_CALL(*pDrm, queryMemoryInfo()) .WillByDefault(::testing::Invoke(pDrm, &Mock::queryMemoryInfoMockReturnFalse)); - EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zetSysmanMemoryGetState(hSysmanMemory, &state)); + + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_state_t state; + EXPECT_EQ(zesMemoryGetState(handle, &state), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + } } -TEST_F(SysmanMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStateAndIfQueryMemoryDidntProvideDeviceMemoryThenErrorIsReturned) { - zes_mem_state_t state; +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStateAndIfQueryMemoryDidntProvideDeviceMemoryThenErrorIsReturned) { + setLocalSupportedAndReinit(true); ON_CALL(*pDrm, queryMemoryInfo()) .WillByDefault(::testing::Invoke(pDrm, &Mock::queryMemoryInfoMockWithoutDevice)); - EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zetSysmanMemoryGetState(hSysmanMemory, &state)); + + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_state_t state; + EXPECT_EQ(zesMemoryGetState(handle, &state), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + } } } // namespace ult } // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory.cpp index 11d756f9fa..f41a1c0e6d 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory.cpp @@ -5,46 +5,144 @@ * */ -#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" #include "level_zero/tools/source/sysman/sysman_imp.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "gtest/gtest.h" -using ::testing::_; - namespace L0 { namespace ult { -class SysmanMemoryFixture : public DeviceFixture, public ::testing::Test { - - protected: - std::unique_ptr sysmanImp; - zet_sysman_handle_t hSysman; - - void SetUp() override { - - DeviceFixture::SetUp(); - sysmanImp = std::make_unique(device->toHandle()); - hSysman = sysmanImp->toHandle(); - } - void TearDown() override { - DeviceFixture::TearDown(); - } +struct MockMemoryManagerSysman : public MemoryManagerMock { + MockMemoryManagerSysman(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerMock(const_cast(executionEnvironment)) {} }; -TEST_F(SysmanMemoryFixture, GivenComponentCountZeroWhenCallingZetSysmanMemoryGetThenZeroCountIsReturnedAndVerifySysmanMemoryGetCallSucceeds) { - uint32_t count = 0; - ze_result_t result = zetSysmanMemoryGet(hSysman, &count, NULL); +constexpr uint32_t memoryHandleComponentCount = 1u; +class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { + protected: + void SetUp() override { + SysmanDeviceFixture::SetUp(); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); + pMemoryManagerOld = device->getDriverHandle()->getMemoryManager(); + pMemoryManager = new ::testing::NiceMock(*neoDevice->getExecutionEnvironment()); + pMemoryManager->localMemorySupported[0] = false; + device->getDriverHandle()->setMemoryManager(pMemoryManager); + + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + pSysmanDeviceImp->pMemoryHandleContext->init(); + } + + void TearDown() override { + device->getDriverHandle()->setMemoryManager(pMemoryManagerOld); + SysmanDeviceFixture::TearDown(); + if (pMemoryManager != nullptr) { + delete pMemoryManager; + pMemoryManager = nullptr; + } + } + + void setLocalSupportedAndReinit(bool supported) { + pMemoryManager->localMemorySupported[0] = supported; + + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + pSysmanDeviceImp->pMemoryHandleContext->init(); + } + + std::vector get_memory_handles(uint32_t count) { + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + return handles; + } + + MockMemoryManagerSysman *pMemoryManager = nullptr; + MemoryManager *pMemoryManagerOld; +}; + +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(false); + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenInvalidComponentCountWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(false); + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); EXPECT_EQ(count, 0u); - uint32_t testcount = count + 1; + count = count + 1; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); +} - result = zetSysmanMemoryGet(hSysman, &testcount, NULL); +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(true); + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); +} - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_EQ(testcount, count); +TEST_F(SysmanDeviceMemoryFixture, GivenInvalidComponentCountWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(true); + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); + + count = count + 1; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesThenValidPowerHandlesIsReturned) { + setLocalSupportedAndReinit(true); + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); + + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + for (auto handle : handles) { + EXPECT_NE(handle, nullptr); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetPropertiesThenVerifySysmanMemoryGetPropertiesCallReturnSuccess) { + setLocalSupportedAndReinit(true); + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties; + EXPECT_EQ(zesMemoryGetProperties(handle, &properties), ZE_RESULT_SUCCESS); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStatehenVerifySysmanMemoryGetStateCallReturnUnsupportedFeature) { + setLocalSupportedAndReinit(true); + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_state_t state; + EXPECT_EQ(zesMemoryGetState(handle, &state), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetBandwidthhenVerifySysmanMemoryGetBandwidthCallReturnUnsupportedFeature) { + setLocalSupportedAndReinit(true); + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_bandwidth_t bandwidth; + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + } } } // namespace ult diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/CMakeLists.txt new file mode 100644 index 0000000000..c3d978fe9d --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/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_zes_memory.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_memory.h + ) +endif() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/mock_memory.h b/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/mock_memory.h new file mode 100644 index 0000000000..05c70763ec --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/mock_memory.h @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "level_zero/core/test/unit_tests/mock.h" +#include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/windows/mock_kmd_sys_manager.h" + +#include "sysman/memory/memory_imp.h" + +namespace L0 { +namespace ult { + +struct MockMemoryManagerSysman : public MemoryManagerMock { + MockMemoryManagerSysman(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerMock(const_cast(executionEnvironment)) {} +}; + +class MemoryKmdSysManager : public Mock {}; + +template <> +struct Mock : public MemoryKmdSysManager { + + uint32_t mockMemoryType = KmdSysman::MemoryType::DDR5; + uint32_t mockMemoryLocation = KmdSysman::MemoryLocationsType::DeviceMemory; + uint64_t mockMemoryPhysicalSize = 4294967296; + uint64_t mockMemoryStolen = 0; + uint64_t mockMemorySystem = 17179869184; + uint64_t mockMemoryDedicated = 0; + uint64_t mockMemoryFree = 4294813696; + uint32_t mockMemoryBus = 256; + uint32_t mockMemoryChannels = 2; + uint32_t mockMemoryMaxBandwidth = 4256000000; + uint32_t mockMemoryCurrentBandwidth = 561321; + + void getMemoryProperty(KmdSysman::GfxSysmanReqHeaderIn *pRequest, KmdSysman::GfxSysmanReqHeaderOut *pResponse) override { + uint8_t *pBuffer = reinterpret_cast(pResponse); + pBuffer += sizeof(KmdSysman::GfxSysmanReqHeaderOut); + + switch (pRequest->inRequestId) { + case KmdSysman::Requests::Memory::MemoryType: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryType; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Memory::MemoryLocation: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryLocation; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Memory::PhysicalSize: { + uint64_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryPhysicalSize; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint64_t); + } break; + case KmdSysman::Requests::Memory::StolenSize: { + uint64_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryStolen; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint64_t); + } break; + case KmdSysman::Requests::Memory::SystemSize: { + uint64_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemorySystem; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint64_t); + } break; + case KmdSysman::Requests::Memory::DedicatedSize: { + uint64_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryDedicated; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint64_t); + } break; + case KmdSysman::Requests::Memory::CurrentFreeMemorySize: { + uint64_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryFree; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint64_t); + } break; + case KmdSysman::Requests::Memory::MemoryWidth: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryBus; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Memory::NumChannels: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryChannels; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Memory::MaxBandwidth: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryMaxBandwidth; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Memory::CurrentBandwidthRead: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMemoryCurrentBandwidth; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + default: { + pResponse->outDataSize = 0; + pResponse->outReturnCode = KmdSysman::KmdSysmanFail; + } break; + } + } + + void setMemoryProperty(KmdSysman::GfxSysmanReqHeaderIn *pRequest, KmdSysman::GfxSysmanReqHeaderOut *pResponse) override { + uint8_t *pBuffer = reinterpret_cast(pRequest); + pBuffer += sizeof(KmdSysman::GfxSysmanReqHeaderIn); + + pResponse->outDataSize = 0; + pResponse->outReturnCode = KmdSysman::KmdSysmanFail; + } + + Mock() = default; + ~Mock() = default; +}; + +class PublicWddmPowerImp : public L0::WddmMemoryImp { + public: + using WddmMemoryImp::pKmdSysManager; +}; + +} // namespace ult +} // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp new file mode 100644 index 0000000000..3631c7aeaf --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/tools/source/sysman/memory/windows/os_memory_imp.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/memory/windows/mock_memory.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/windows/mock_sysman_fixture.h" + +namespace L0 { +namespace ult { + +constexpr uint32_t memoryHandleComponentCount = 1u; +class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { + protected: + Mock *pKmdSysManager = nullptr; + KmdSysManager *pOriginalKmdSysManager = nullptr; + void SetUp() override { + SysmanDeviceFixture::SetUp(); + + pMemoryManagerOld = device->getDriverHandle()->getMemoryManager(); + + pMemoryManager = new ::testing::NiceMock(*neoDevice->getExecutionEnvironment()); + + pMemoryManager->localMemorySupported[0] = false; + + device->getDriverHandle()->setMemoryManager(pMemoryManager); + + pKmdSysManager = new Mock; + + EXPECT_CALL(*pKmdSysManager, escape(_, _, _, _, _)) + .WillRepeatedly(::testing::Invoke(pKmdSysManager, &Mock::mock_escape)); + + pOriginalKmdSysManager = pWddmSysmanImp->pKmdSysManager; + pWddmSysmanImp->pKmdSysManager = pKmdSysManager; + + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + pSysmanDeviceImp->pMemoryHandleContext->init(); + } + + void TearDown() override { + device->getDriverHandle()->setMemoryManager(pMemoryManagerOld); + SysmanDeviceFixture::TearDown(); + pWddmSysmanImp->pKmdSysManager = pOriginalKmdSysManager; + if (pKmdSysManager != nullptr) { + delete pKmdSysManager; + pKmdSysManager = nullptr; + } + if (pMemoryManager != nullptr) { + delete pMemoryManager; + pMemoryManager = nullptr; + } + } + + void setLocalSupportedAndReinit(bool supported) { + pMemoryManager->localMemorySupported[0] = supported; + + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + pSysmanDeviceImp->pMemoryHandleContext->init(); + } + + std::vector get_memory_handles(uint32_t count) { + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + return handles; + } + + MockMemoryManagerSysman *pMemoryManager = nullptr; + MemoryManager *pMemoryManagerOld; +}; + +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(true); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenInvalidComponentCountWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(true); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); + + count = count + 1; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidPowerHandlesIsReturned) { + setLocalSupportedAndReinit(true); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); + + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + for (auto handle : handles) { + EXPECT_NE(handle, nullptr); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenZeroCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(false); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenInvalidComponentCountWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenZeroCountIsReturnedAndVerifySysmanPowerGetCallSucceeds) { + setLocalSupportedAndReinit(false); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); + + count = count + 1; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenValidPowerHandlesIsReturned) { + setLocalSupportedAndReinit(false); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); + + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + for (auto handle : handles) { + EXPECT_NE(handle, nullptr); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetPropertiesWithLocalMemoryThenVerifySysmanMemoryGetPropertiesCallSucceeds) { + pKmdSysManager->mockMemoryLocation = KmdSysman::MemoryLocationsType::DeviceMemory; + setLocalSupportedAndReinit(true); + + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties; + + ze_result_t result = zesMemoryGetProperties(handle, &properties); + + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + EXPECT_EQ(properties.type, ZES_MEM_TYPE_DDR5); + EXPECT_EQ(properties.location, ZES_MEM_LOC_DEVICE); + EXPECT_FALSE(properties.onSubdevice); + EXPECT_EQ(properties.subdeviceId, 0u); + EXPECT_EQ(properties.physicalSize, pKmdSysManager->mockMemoryPhysicalSize); + EXPECT_EQ(properties.numChannels, pKmdSysManager->mockMemoryChannels); + EXPECT_EQ(properties.busWidth, pKmdSysManager->mockMemoryBus); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetStatehenVerifySysmanMemoryGetStateCallSucceeds) { + setLocalSupportedAndReinit(true); + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_state_t state; + + ze_result_t result = zesMemoryGetState(handle, &state); + + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + EXPECT_EQ(state.health, ZES_MEM_HEALTH_OK); + EXPECT_GT(state.size, 0u); + EXPECT_GT(state.free, 0u); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzetSysmanMemoryGetBandwidthhenVerifySysmanMemoryGetBandwidthCallSucceeds) { + setLocalSupportedAndReinit(true); + auto handles = get_memory_handles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_bandwidth_t bandwidth; + + ze_result_t result = zesMemoryGetBandwidth(handle, &bandwidth); + + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + EXPECT_EQ(bandwidth.maxBandwidth, pKmdSysManager->mockMemoryMaxBandwidth); + EXPECT_EQ(bandwidth.readCounter, pKmdSysManager->mockMemoryCurrentBandwidth * MbpsToBytesPerSecond); + EXPECT_EQ(bandwidth.writeCounter, 0u); + EXPECT_GT(bandwidth.timestamp, 0u); + } +} + +} // namespace ult +} // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp index 287e575bc1..ba1b5a2515 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp @@ -6,7 +6,7 @@ */ #include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "mock_sysfs_pci.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/power/linux/test_zes_power.cpp b/level_zero/tools/test/unit_tests/sources/sysman/power/linux/test_zes_power.cpp index 6055141259..10762f59d3 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/power/linux/test_zes_power.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/power/linux/test_zes_power.cpp @@ -6,7 +6,7 @@ */ #include "level_zero/tools/source/sysman/power/linux/os_power_imp.h" -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "gmock/gmock.h" #include "gtest/gtest.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/ras/linux/test_zes_ras.cpp b/level_zero/tools/test/unit_tests/sources/sysman/ras/linux/test_zes_ras.cpp index a0ba03d66d..7cd6356df3 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/ras/linux/test_zes_ras.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/ras/linux/test_zes_ras.cpp @@ -5,7 +5,7 @@ * */ -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "mock_fs_ras.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/scheduler/linux/test_zes_scheduler.cpp b/level_zero/tools/test/unit_tests/sources/sysman/scheduler/linux/test_zes_scheduler.cpp index a805a2cfb0..e900ca7c61 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/scheduler/linux/test_zes_scheduler.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/scheduler/linux/test_zes_scheduler.cpp @@ -5,7 +5,7 @@ * */ -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "mock_sysfs_scheduler.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/test_zes_sysman_standby.cpp b/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/test_zes_sysman_standby.cpp index 1eb3efb9b8..0db61c00d3 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/test_zes_sysman_standby.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/standby/linux/test_zes_sysman_standby.cpp @@ -5,7 +5,7 @@ * */ -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "mock_sysfs_standby.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/temperature/linux/test_zes_temperature.cpp b/level_zero/tools/test/unit_tests/sources/sysman/temperature/linux/test_zes_temperature.cpp index 3a7dd27e7c..eb365cf137 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/temperature/linux/test_zes_temperature.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/temperature/linux/test_zes_temperature.cpp @@ -5,7 +5,7 @@ * */ -#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" #include "level_zero/tools/test/unit_tests/sources/sysman/temperature/linux/mock_sysfs_temperature.h" namespace L0 { diff --git a/level_zero/tools/test/unit_tests/sources/sysman/windows/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/windows/CMakeLists.txt index 548a9349ed..bfceb29468 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/windows/CMakeLists.txt +++ b/level_zero/tools/test/unit_tests/sources/sysman/windows/CMakeLists.txt @@ -9,6 +9,8 @@ if(WIN32) PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt ${CMAKE_CURRENT_SOURCE_DIR}/mock_kmd_sys_manager.h + ${CMAKE_CURRENT_SOURCE_DIR}/mock_sysman_fixture.h + ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_manager.cpp ) endif() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/windows/mock_kmd_sys_manager.h b/level_zero/tools/test/unit_tests/sources/sysman/windows/mock_kmd_sys_manager.h index a3cbac820c..96c4c7d734 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/windows/mock_kmd_sys_manager.h +++ b/level_zero/tools/test/unit_tests/sources/sysman/windows/mock_kmd_sys_manager.h @@ -6,12 +6,10 @@ */ #pragma once -#include "level_zero/core/test/unit_tests/mock.h" +#include "level_zero/tools/source/sysman/windows/kmd_sys_manager.h" #include "level_zero/tools/source/sysman/windows/os_sysman_imp.h" -#include "sysman/windows/kmd_sys_manager.h" - -using ::testing::_; +#include "gmock/gmock.h" namespace L0 { namespace ult { @@ -373,10 +371,5 @@ struct Mock : public MockKmdSysManager { ~Mock() = default; }; -class PublicWddmSysmanImp : public L0::WddmSysmanImp { - public: - using WddmSysmanImp::pKmdSysManager; -}; - } // namespace ult } // namespace L0 \ No newline at end of file diff --git a/level_zero/tools/test/unit_tests/sources/sysman/windows/mock_sysman_fixture.h b/level_zero/tools/test/unit_tests/sources/sysman/windows/mock_sysman_fixture.h new file mode 100644 index 0000000000..c396d02791 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/windows/mock_sysman_fixture.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "shared/source/execution_environment/root_device_environment.h" +#include "shared/source/os_interface/device_factory.h" +#include "shared/source/os_interface/windows/os_interface.h" +#include "shared/source/os_interface/windows/wddm/wddm.h" + +#include "test.h" + +#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" +#include "level_zero/tools/source/sysman/sysman.h" + +#include "sysman/windows/os_sysman_imp.h" + +using ::testing::_; +using namespace NEO; + +namespace L0 { +namespace ult { + +class PublicWddmSysmanImp : public L0::WddmSysmanImp { + public: + using WddmSysmanImp::pKmdSysManager; +}; + +class SysmanDeviceFixture : public DeviceFixture, public ::testing::Test { + public: + void SetUp() override { + DeviceFixture::SetUp(); + neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->osInterface = std::make_unique(); + + _putenv_s("ZES_ENABLE_SYSMAN", "1"); + device->setSysmanHandle(L0::SysmanDeviceHandleContext::init(device->toHandle())); + pSysmanDevice = device->getSysmanHandle(); + pSysmanDeviceImp = static_cast(pSysmanDevice); + pOsSysman = pSysmanDeviceImp->pOsSysman; + pWddmSysmanImp = static_cast(pOsSysman); + } + + void TearDown() override { + DeviceFixture::TearDown(); + _putenv_s("ZES_ENABLE_SYSMAN", "0"); + } + + SysmanDevice *pSysmanDevice = nullptr; + SysmanDeviceImp *pSysmanDeviceImp = nullptr; + OsSysman *pOsSysman = nullptr; + PublicWddmSysmanImp *pWddmSysmanImp = nullptr; +}; + +} // namespace ult +} // namespace L0 \ No newline at end of file diff --git a/level_zero/tools/test/unit_tests/sources/sysman/windows/test_sysman.cpp b/level_zero/tools/test/unit_tests/sources/sysman/windows/test_sysman.cpp new file mode 100644 index 0000000000..a94d2c88b0 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/windows/test_sysman.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "test.h" + +#include "level_zero/tools/test/unit_tests/sources/sysman/windows/mock_sysman_fixture.h" + +namespace L0 { +namespace ult { + +using MockDeviceSysmanGetTest = Test; +TEST_F(MockDeviceSysmanGetTest, GivenValidSysmanHandleSetInDeviceStructWhenGetThisSysmanHandleThenHandlesShouldBeSimilar) { + SysmanDeviceImp *sysman = new SysmanDeviceImp(device->toHandle()); + device->setSysmanHandle(sysman); + EXPECT_EQ(sysman, device->getSysmanHandle()); +} + +TEST_F(SysmanDeviceFixture, GivenValidDeviceHandleInSysmanInitThenValidSysmanHandleReceived) { + ze_device_handle_t hSysman = device->toHandle(); + auto pSysmanDevice = L0::SysmanDeviceHandleContext::init(hSysman); + EXPECT_NE(pSysmanDevice, nullptr); + delete pSysmanDevice; + pSysmanDevice = nullptr; +} + +} // namespace ult +} // namespace L0 \ No newline at end of file diff --git a/level_zero/tools/test/unit_tests/sources/sysman/windows/test_sysman_manager.cpp b/level_zero/tools/test/unit_tests/sources/sysman/windows/test_sysman_manager.cpp index 8e2c5a1b84..2eeeaff2fa 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/windows/test_sysman_manager.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/windows/test_sysman_manager.cpp @@ -8,10 +8,10 @@ #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" #include "level_zero/tools/source/sysman/sysman_imp.h" #include "level_zero/tools/source/sysman/windows/os_sysman_imp.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/windows/mock_kmd_sys_manager.h" #include "gmock/gmock.h" #include "gtest/gtest.h" -#include "mock_kmd_sys_manager.h" using ::testing::_; using ::testing::DoAll; @@ -27,11 +27,8 @@ class SysmanKmdManagerFixture : public ::testing::Test { protected: Mock *pKmdSysManager = nullptr; - OsSysman *pOsSysman = nullptr; - PublicWddmSysmanImp wddmSysmanImp; void SetUp() { - ; pKmdSysManager = new Mock; EXPECT_CALL(*pKmdSysManager, escape(_, _, _, _, _))