From 5fd8275c68e6bc77e12a510b99f4dd174cc9a34b Mon Sep 17 00:00:00 2001 From: "Bari, Pratik" Date: Wed, 7 Dec 2022 16:53:50 +0000 Subject: [PATCH] Added Memory ULTs Related-To: LOCI-3730 Signed-off-by: Bari, Pratik --- .../sysman/memory/linux/CMakeLists.txt | 18 +- .../sources/sysman/memory/linux/mock_memory.h | 22 +- .../sysman/memory/linux/mock_memory_prelim.h | 509 ++++++++ .../memory/linux/test_sysman_memory.cpp | 2 +- .../memory/linux/test_sysman_memory_dg1.cpp | 17 +- .../linux/test_sysman_memory_prelim.cpp | 1083 +++++++++++++++++ 6 files changed, 1612 insertions(+), 39 deletions(-) create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory_prelim.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_prelim.cpp 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 94fdc3d5ad..c57b5ddba4 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 @@ -8,26 +8,20 @@ set(L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt ) -if(SUPPORT_DG1 AND "${BRANCH_TYPE}" STREQUAL "") +if(NEO_ENABLE_i915_PRELIM_DETECTION) list(APPEND L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX - ${CMAKE_CURRENT_SOURCE_DIR}/mock_memory.h - ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_memory_dg1.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_memory_prelim.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_memory_prelim.h ) -else() - list(APPEND L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}mock_memory.h - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}test_sysman_memory.cpp - ) -endif() -if((NEO_ENABLE_i915_PRELIM_DETECTION) AND ("${BRANCH_TYPE}" STREQUAL "")) +else() if(SUPPORT_DG1) - list(REMOVE_ITEM L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX + list(APPEND L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_memory_dg1.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_memory.h ) else() - list(REMOVE_ITEM L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX + list(APPEND L0_TESTS_TOOLS_SYSMAN_MEMORY_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_memory.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_memory.h ) diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory.h b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory.h index 808b0efff9..2ff75e6682 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory.h +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory.h @@ -38,10 +38,14 @@ class MemoryNeoDrm : public Drm { MemoryNeoDrm(RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique(mockFd, ""), rootDeviceEnvironment) {} }; -template <> -struct Mock : public MemoryNeoDrm { - Mock(RootDeviceEnvironment &rootDeviceEnvironment) : MemoryNeoDrm(rootDeviceEnvironment) {} - bool queryMemoryInfoMockPositiveTest() { +struct MockMemoryNeoDrm : public MemoryNeoDrm { + MockMemoryNeoDrm(RootDeviceEnvironment &rootDeviceEnvironment) : MemoryNeoDrm(rootDeviceEnvironment) {} + std::vector mockQueryMemoryInfoReturnStatus{}; + bool queryMemoryInfo() override { + if (!mockQueryMemoryInfoReturnStatus.empty()) { + return mockQueryMemoryInfoReturnStatus.front(); + } + std::vector regionInfo(2); regionInfo[0].region = {drm_i915_gem_memory_class::I915_MEMORY_CLASS_SYSTEM, 0}; regionInfo[0].probedSize = probedSizeRegionZero; @@ -53,16 +57,6 @@ struct Mock : public MemoryNeoDrm { this->memoryInfo.reset(new MemoryInfo(regionInfo, *this)); return true; } - - bool queryMemoryInfoMockReturnFalse() { - return false; - } - - bool queryMemoryInfoMockReturnFakeTrue() { - return true; - } - - MOCK_METHOD(bool, queryMemoryInfo, (), (override)); }; } // namespace ult diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory_prelim.h b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory_prelim.h new file mode 100644 index 0000000000..32d97d269f --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory_prelim.h @@ -0,0 +1,509 @@ +/* + * Copyright (C) 2021-2022 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/os_interface/linux/ioctl_helper.h" +#include "shared/source/os_interface/linux/memory_info.h" +#include "shared/source/os_interface/linux/system_info.h" + +#include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" +#include "level_zero/tools/source/sysman/memory/linux/os_memory_imp_prelim.h" +#include "level_zero/tools/source/sysman/memory/memory_imp.h" + +#include "drm/i915_drm.h" +#include "drm/intel_hwconfig_types.h" +#include "sysman/linux/os_sysman_imp.h" + +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 probedSizeRegionFour = 32 * 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; +constexpr uint64_t unallocatedSizeRegionFour = 4 * GB; + +constexpr uint16_t vF0VfidIndex = 88; +constexpr uint16_t vF0Hbm0ReadIndex = 92; +constexpr uint16_t vF0Hbm0WriteIndex = 96; +constexpr uint16_t vF0Hbm1ReadIndex = 104; +constexpr uint16_t vF0Hbm1WriteIndex = 108; +constexpr uint16_t vF0TimestampLIndex = 168; +constexpr uint16_t vF0TimestampHIndex = 172; +constexpr uint16_t vF1VfidIndex = 176; +constexpr uint16_t vF1Hbm0ReadIndex = 180; +constexpr uint16_t vF1Hbm0WriteIndex = 184; +constexpr uint16_t vF1Hbm1ReadIndex = 192; +constexpr uint16_t vF1Hbm1WriteIndex = 196; +constexpr uint16_t vF1TimestampLIndex = 256; +constexpr uint16_t vF1TimestampHIndex = 260; +constexpr uint16_t vF0Hbm2ReadIndex = 312; +constexpr uint16_t vF0Hbm2WriteIndex = 316; +constexpr uint16_t vF0Hbm3ReadIndex = 328; +constexpr uint16_t vF0Hbm3WriteIndex = 332; +constexpr uint16_t vF1Hbm2ReadIndex = 344; +constexpr uint16_t vF1Hbm2WriteIndex = 348; +constexpr uint16_t vF1Hbm3ReadIndex = 360; +constexpr uint16_t vF1Hbm3WriteIndex = 364; + +constexpr uint8_t vF0VfidValue = 1; +constexpr uint8_t vF0Hbm0ReadValue = 92; +constexpr uint8_t vF0Hbm0WriteValue = 96; +constexpr uint8_t vF0Hbm1ReadValue = 104; +constexpr uint8_t vF0Hbm1WriteValue = 108; +constexpr uint8_t vF0TimestampLValue = 168; +constexpr uint8_t vF0TimestampHValue = 172; +constexpr uint8_t vF1VfidValue = 0; +constexpr uint8_t vF0Hbm2ReadValue = 113; +constexpr uint8_t vF0Hbm2WriteValue = 125; +constexpr uint8_t vF0Hbm3ReadValue = 135; +constexpr uint8_t vF0Hbm3WriteValue = 20; + +constexpr uint8_t vF1Hbm0ReadValue = 92; +constexpr uint8_t vF1Hbm0WriteValue = 96; +constexpr uint8_t vF1Hbm1ReadValue = 104; +constexpr uint8_t vF1Hbm1WriteValue = 108; +constexpr uint8_t vF1TimestampLValue = 168; +constexpr uint8_t vF1TimestampHValue = 172; +constexpr uint8_t vF1Hbm2ReadValue = 113; +constexpr uint8_t vF1Hbm2WriteValue = 125; +constexpr uint8_t vF1Hbm3ReadValue = 135; +constexpr uint8_t vF1Hbm3WriteValue = 20; +constexpr uint64_t mockIdiReadVal = 8u; +constexpr uint64_t mockIdiWriteVal = 9u; +constexpr uint64_t mockDisplayVc1ReadVal = 10u; +constexpr uint64_t numberMcChannels = 16; +constexpr uint64_t transactionSize = 32; +constexpr uint64_t mcTimeStamp = 62000000000u; + +namespace L0 { +namespace ult { +uint32_t mockMemoryType = INTEL_HWCONFIG_MEMORY_TYPE_HBM2e; +std::string mockPhysicalSize = "0x00000040000000"; +uint64_t hbmRP0Frequency = 4200; // in MHz +const std::string deviceMemoryHealth("device_memory_health"); +std::string gpuUpstreamPortPathInMemory = "/sys/devices/pci0000:89/0000:89:02.0/0000:8a:00.0"; +const std::string baseTelemSysFS("/sys/class/intel_pmt"); +const std::string realPathTelem1 = "/sys/devices/pci0000:89/0000:89:02.0/0000:86:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem1"; +const std::string realPathTelem2 = "/sys/devices/pci0000:89/0000:89:02.0/0000:86:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem2"; +const std::string realPathTelem3 = "/sys/devices/pci0000:89/0000:89:02.0/0000:86:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem3"; +const std::string realPathTelem4 = "/sys/devices/pci0000:89/0000:89:02.0/0000:86:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem4"; +const std::string realPathTelem5 = "/sys/devices/pci0000:89/0000:89:02.0/0000:86:00.0/0000:8b:02.0/0000:8e:00.1/pmt_telemetry.1.auto/intel_pmt/telem5"; +const std::string sysfsPahTelem1 = "/sys/class/intel_pmt/telem1"; +const std::string sysfsPahTelem2 = "/sys/class/intel_pmt/telem2"; +const std::string sysfsPahTelem3 = "/sys/class/intel_pmt/telem3"; +const std::string sysfsPahTelem4 = "/sys/class/intel_pmt/telem4"; +const std::string sysfsPahTelem5 = "/sys/class/intel_pmt/telem5"; + +class MemorySysfsAccess : public SysfsAccess {}; + +struct MockMemorySysfsAccess : public MemorySysfsAccess { + + std::vector mockReadReturnStatus{}; + std::vector mockReadStringValue{}; + std::vector mockReadUInt64Value{}; + bool isRepeated = false; + + ze_result_t getVal(const std::string file, std::string &val) { + if ((file.compare("gt/gt0/addr_range") == 0) || (file.compare("gt/gt1/addr_range") == 0)) { + val = mockPhysicalSize; + return ZE_RESULT_SUCCESS; + } + val = "0"; + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + + ze_result_t getValError(const std::string file, std::string &val) { + val = "0"; + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + + ze_result_t getMemHealthValReturnErrorNotAvailable(const std::string file, std::string &val) { + if (file.compare(deviceMemoryHealth) == 0) { + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t getMemHealthReturnErrorUnknown(const std::string file, std::string &val) { + if (file.compare(deviceMemoryHealth) == 0) { + return ZE_RESULT_ERROR_UNKNOWN; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t read(const std::string file, std::string &val) override { + ze_result_t result = ZE_RESULT_SUCCESS; + if (!mockReadReturnStatus.empty()) { + result = mockReadReturnStatus.front(); + if (!mockReadStringValue.empty()) { + val = mockReadStringValue.front(); + } + + if (isRepeated != true) { + mockReadReturnStatus.erase(mockReadReturnStatus.begin()); + if (!mockReadStringValue.empty()) { + mockReadStringValue.erase(mockReadStringValue.begin()); + } + } + return result; + } + + if (file.compare(deviceMemoryHealth) == 0) { + val = "OK"; + } + return result; + } + + ze_result_t read(const std::string file, uint64_t &val) override { + ze_result_t result = ZE_RESULT_SUCCESS; + if (!mockReadReturnStatus.empty()) { + result = mockReadReturnStatus.front(); + if (!mockReadUInt64Value.empty()) { + val = mockReadUInt64Value.front(); + } + + if (isRepeated != true) { + mockReadReturnStatus.erase(mockReadReturnStatus.begin()); + if (!mockReadUInt64Value.empty()) { + mockReadUInt64Value.erase(mockReadUInt64Value.begin()); + } + } + } + return result; + } + + ze_result_t getMemHealthDegraded(const std::string file, std::string &val) { + if (file.compare(deviceMemoryHealth) == 0) { + val = "REBOOT_ALARM"; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t getMemHealthCritical(const std::string file, std::string &val) { + if (file.compare(deviceMemoryHealth) == 0) { + val = "DEGRADED"; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t getMemHealthReplace(const std::string file, std::string &val) { + if (file.compare(deviceMemoryHealth) == 0) { + val = "DEGRADED_FAILED"; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t getMemHealthUnknown(const std::string file, std::string &val) { + if (file.compare(deviceMemoryHealth) == 0) { + val = "RANDOM"; + } + return ZE_RESULT_SUCCESS; + } +}; + +struct MockMemoryManagerSysman : public MemoryManagerMock { + MockMemoryManagerSysman(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerMock(const_cast(executionEnvironment)) {} +}; + +class MemoryNeoDrm : public Drm { + public: + using Drm::ioctlHelper; + const int mockFd = 33; + MemoryNeoDrm(RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique(mockFd, ""), rootDeviceEnvironment) {} +}; + +struct MockMemoryNeoDrm : public MemoryNeoDrm { + std::vector mockQuerySystemInfoReturnValue{}; + bool isRepeated = false; + bool mockReturnEmptyRegions = false; + MockMemoryNeoDrm(RootDeviceEnvironment &rootDeviceEnvironment) : MemoryNeoDrm(rootDeviceEnvironment) {} + + std::vector getMemoryRegions() override { + if (mockReturnEmptyRegions == true) { + return {}; + } + + uint32_t regionCount = 3; + uint32_t allocSize = sizeof(drm_i915_query_memory_regions) + regionCount * sizeof(drm_i915_memory_region_info); + auto regions = std::vector(allocSize, 0); + drm_i915_query_memory_regions *memRegions = reinterpret_cast(regions.data()); + memRegions->regions[0].region = {I915_MEMORY_CLASS_SYSTEM, 0}; + memRegions->regions[0].probed_size = probedSizeRegionZero; + memRegions->regions[0].unallocated_size = unallocatedSizeRegionZero; + memRegions->regions[1].region = {I915_MEMORY_CLASS_DEVICE, 0}; + memRegions->regions[1].probed_size = probedSizeRegionOne; + memRegions->regions[1].unallocated_size = unallocatedSizeRegionOne; + memRegions->regions[2].region = {I915_MEMORY_CLASS_DEVICE, 1}; + memRegions->regions[2].probed_size = probedSizeRegionFour; + memRegions->regions[2].unallocated_size = unallocatedSizeRegionFour; + memRegions->num_regions = 3; + + return regions; + } + + void setMemoryType(uint32_t memory) { + mockMemoryType = memory; + } + + std::vector getMemoryRegionsReturnsEmpty() { + return {}; + } + + bool querySystemInfo() override { + bool returnValue = true; + if (!mockQuerySystemInfoReturnValue.empty()) { + returnValue = mockQuerySystemInfoReturnValue.front(); + if (isRepeated != true) { + mockQuerySystemInfoReturnValue.erase(mockQuerySystemInfoReturnValue.begin()); + } + return returnValue; + } + + uint32_t hwBlob[] = {INTEL_HWCONFIG_MAX_MEMORY_CHANNELS, 1, 8, INTEL_HWCONFIG_MEMORY_TYPE, 0, mockMemoryType}; + std::vector inputBlobData(reinterpret_cast(hwBlob), reinterpret_cast(hwBlob) + sizeof(hwBlob)); + this->systemInfo.reset(new SystemInfo(inputBlobData)); + return returnValue; + } +}; + +class MemoryPmt : public PlatformMonitoringTech { + public: + MemoryPmt(FsAccess *pFsAccess, ze_bool_t onSubdevice, uint32_t subdeviceId) : PlatformMonitoringTech(pFsAccess, onSubdevice, subdeviceId) {} + using PlatformMonitoringTech::keyOffsetMap; +}; + +struct MockMemoryPmt : public MemoryPmt { + std::vector mockReadValueReturnStatus{}; + std::vector mockReadArgumentValue{}; + ze_result_t mockIdiReadValueFailureReturnStatus = ZE_RESULT_SUCCESS; + ze_result_t mockIdiWriteFailureReturnStatus = ZE_RESULT_SUCCESS; + ze_result_t mockDisplayVc1ReadFailureReturnStatus = ZE_RESULT_SUCCESS; + ze_result_t mockReadTimeStampFailureReturnStatus = ZE_RESULT_SUCCESS; + bool mockVfid0Status = false; + bool mockVfid1Status = false; + bool isRepeated = false; + + MockMemoryPmt(FsAccess *pFsAccess, ze_bool_t onSubdevice, uint32_t subdeviceId) : MemoryPmt(pFsAccess, onSubdevice, subdeviceId) {} + + ze_result_t readValue(const std::string key, uint32_t &val) override { + ze_result_t result = ZE_RESULT_SUCCESS; + + if (mockVfid0Status == true) { + return mockedReadValueWithVfid0True(key, val); + } + + if (mockVfid1Status == true) { + return mockedReadValueWithVfid1True(key, val); + } + + if (!mockReadValueReturnStatus.empty()) { + result = mockReadValueReturnStatus.front(); + if (!mockReadArgumentValue.empty()) { + val = mockReadArgumentValue.front(); + } + + if (isRepeated != true) { + mockReadValueReturnStatus.erase(mockReadValueReturnStatus.begin()); + if (!mockReadArgumentValue.empty()) { + mockReadArgumentValue.erase(mockReadArgumentValue.begin()); + } + } + } + return result; + } + + ze_result_t mockedReadValueWithVfid0True(const std::string key, uint32_t &val) { + if (key.compare("VF0_VFID") == 0) { + val = 1; + } else if (key.compare("VF1_VFID") == 0) { + val = 0; + } else if (key.compare("VF0_HBM0_READ") == 0) { + val = vF0Hbm0ReadValue; + } else if (key.compare("VF0_HBM0_WRITE") == 0) { + val = vF0Hbm0WriteValue; + } else if (key.compare("VF0_HBM1_READ") == 0) { + val = vF0Hbm1ReadValue; + } else if (key.compare("VF0_HBM1_WRITE") == 0) { + val = vF0Hbm1WriteValue; + } else if (key.compare("VF0_TIMESTAMP_L") == 0) { + val = vF0TimestampLValue; + } else if (key.compare("VF0_TIMESTAMP_H") == 0) { + val = vF0TimestampHValue; + } else if (key.compare("VF0_HBM2_READ") == 0) { + val = vF0Hbm2ReadValue; + } else if (key.compare("VF0_HBM2_WRITE") == 0) { + val = vF0Hbm2WriteValue; + } else if (key.compare("VF0_HBM3_READ") == 0) { + val = vF0Hbm3ReadValue; + } else if (key.compare("VF0_HBM3_WRITE") == 0) { + val = vF0Hbm3WriteValue; + } else { + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t mockedReadValueWithVfid1True(const std::string key, uint32_t &val) { + if (key.compare("VF0_VFID") == 0) { + val = 0; + } else if (key.compare("VF1_VFID") == 0) { + val = 1; + } else if (key.compare("VF1_HBM0_READ") == 0) { + val = vF1Hbm0ReadValue; + } else if (key.compare("VF1_HBM0_WRITE") == 0) { + val = vF1Hbm0WriteValue; + } else if (key.compare("VF1_HBM1_READ") == 0) { + val = vF1Hbm1ReadValue; + } else if (key.compare("VF1_HBM1_WRITE") == 0) { + val = vF1Hbm1WriteValue; + } else if (key.compare("VF1_TIMESTAMP_L") == 0) { + val = vF1TimestampLValue; + } else if (key.compare("VF1_TIMESTAMP_H") == 0) { + val = vF1TimestampHValue; + } else if (key.compare("VF1_HBM2_READ") == 0) { + val = vF1Hbm2ReadValue; + } else if (key.compare("VF1_HBM2_WRITE") == 0) { + val = vF1Hbm2WriteValue; + } else if (key.compare("VF1_HBM3_READ") == 0) { + val = vF1Hbm3ReadValue; + } else if (key.compare("VF1_HBM3_WRITE") == 0) { + val = vF1Hbm3WriteValue; + } else { + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t readValue(const std::string key, uint64_t &val) override { + ze_result_t result = ZE_RESULT_SUCCESS; + + if (mockIdiReadValueFailureReturnStatus != ZE_RESULT_SUCCESS) { + return mockIdiReadValueFailure(key, val); + } + + if (mockIdiWriteFailureReturnStatus != ZE_RESULT_SUCCESS) { + return mockIdiWriteFailure(key, val); + } + + if (mockDisplayVc1ReadFailureReturnStatus != ZE_RESULT_SUCCESS) { + return mockDisplayVc1ReadFailure(key, val); + } + + if (mockReadTimeStampFailureReturnStatus != ZE_RESULT_SUCCESS) { + return mockReadTimeStampFailure(key, val); + } + + if (key.compare("IDI_READS[0]") == 0 || key.compare("IDI_READS[1]") == 0 || key.compare("IDI_READS[2]") == 0 || key.compare("IDI_READS[3]") == 0 || key.compare("IDI_READS[4]") == 0 || key.compare("IDI_READS[5]") == 0 || key.compare("IDI_READS[6]") == 0 || key.compare("IDI_READS[7]") == 0 || key.compare("IDI_READS[8]") == 0 || key.compare("IDI_READS[9]") == 0 || key.compare("IDI_READS[10]") == 0 || key.compare("IDI_READS[11]") == 0 || key.compare("IDI_READS[12]") == 0 || key.compare("IDI_READS[13]") == 0 || key.compare("IDI_READS[14]") == 0 || key.compare("IDI_READS[15]") == 0) { + val = mockIdiReadVal; + } else if (key.compare("IDI_WRITES[0]") == 0 || key.compare("IDI_WRITES[1]") == 0 || key.compare("IDI_WRITES[2]") == 0 || key.compare("IDI_WRITES[3]") == 0 || key.compare("IDI_WRITES[4]") == 0 || key.compare("IDI_WRITES[5]") == 0 || key.compare("IDI_WRITES[6]") == 0 || key.compare("IDI_WRITES[7]") == 0 || key.compare("IDI_WRITES[8]") == 0 || key.compare("IDI_WRITES[9]") == 0 || key.compare("IDI_WRITES[10]") == 0 || key.compare("IDI_WRITES[11]") == 0 || key.compare("IDI_WRITES[12]") == 0 || key.compare("IDI_WRITES[13]") == 0 || key.compare("IDI_WRITES[14]") == 0 || key.compare("IDI_WRITES[15]") == 0) { + val = mockIdiWriteVal; + } else if (key.compare("DISPLAY_VC1_READS[0]") == 0 || key.compare("DISPLAY_VC1_READS[1]") == 0 || key.compare("DISPLAY_VC1_READS[2]") == 0 || key.compare("DISPLAY_VC1_READS[3]") == 0 || key.compare("DISPLAY_VC1_READS[4]") == 0 || key.compare("DISPLAY_VC1_READS[5]") == 0 || key.compare("DISPLAY_VC1_READS[6]") == 0 || key.compare("DISPLAY_VC1_READS[7]") == 0 || key.compare("DISPLAY_VC1_READS[8]") == 0 || key.compare("DISPLAY_VC1_READS[9]") == 0 || key.compare("DISPLAY_VC1_READS[10]") == 0 || key.compare("DISPLAY_VC1_READS[11]") == 0 || key.compare("DISPLAY_VC1_READS[12]") == 0 || key.compare("DISPLAY_VC1_READS[13]") == 0 || key.compare("DISPLAY_VC1_READS[14]") == 0 || key.compare("DISPLAY_VC1_READS[15]") == 0) { + val = mockDisplayVc1ReadVal; + } else if (key.compare("MC_CAPTURE_TIMESTAMP") == 0) { + val = mcTimeStamp; + } else { + result = ZE_RESULT_ERROR_NOT_AVAILABLE; + } + return result; + } + + ze_result_t mockIdiReadValueFailure(const std::string key, uint64_t &val) { + return ZE_RESULT_ERROR_UNKNOWN; + } + + ze_result_t mockIdiWriteFailure(const std::string key, uint64_t &val) { + if (key.compare("IDI_READS[0]") == 0 || key.compare("IDI_READS[1]") == 0 || key.compare("IDI_READS[2]") == 0 || key.compare("IDI_READS[3]") == 0 || key.compare("IDI_READS[4]") == 0 || key.compare("IDI_READS[5]") == 0 || key.compare("IDI_READS[6]") == 0 || key.compare("IDI_READS[7]") == 0 || key.compare("IDI_READS[8]") == 0 || key.compare("IDI_READS[9]") == 0 || key.compare("IDI_READS[10]") == 0 || key.compare("IDI_READS[11]") == 0 || key.compare("IDI_READS[12]") == 0 || key.compare("IDI_READS[13]") == 0 || key.compare("IDI_READS[14]") == 0 || key.compare("IDI_READS[15]") == 0) { + val = mockIdiReadVal; + } else if (key.compare("IDI_WRITES[0]") == 0 || key.compare("IDI_WRITES[1]") == 0 || key.compare("IDI_WRITES[2]") == 0 || key.compare("IDI_WRITES[3]") == 0 || key.compare("IDI_WRITES[4]") == 0 || key.compare("IDI_WRITES[5]") == 0 || key.compare("IDI_WRITES[6]") == 0 || key.compare("IDI_WRITES[7]") == 0 || key.compare("IDI_WRITES[8]") == 0 || key.compare("IDI_WRITES[9]") == 0 || key.compare("IDI_WRITES[10]") == 0 || key.compare("IDI_WRITES[11]") == 0 || key.compare("IDI_WRITES[12]") == 0 || key.compare("IDI_WRITES[13]") == 0 || key.compare("IDI_WRITES[14]") == 0 || key.compare("IDI_WRITES[15]") == 0) { + return ZE_RESULT_ERROR_UNKNOWN; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t mockDisplayVc1ReadFailure(const std::string key, uint64_t &val) { + if (key.compare("IDI_READS[0]") == 0 || key.compare("IDI_READS[1]") == 0 || key.compare("IDI_READS[2]") == 0 || key.compare("IDI_READS[3]") == 0 || key.compare("IDI_READS[4]") == 0 || key.compare("IDI_READS[5]") == 0 || key.compare("IDI_READS[6]") == 0 || key.compare("IDI_READS[7]") == 0 || key.compare("IDI_READS[8]") == 0 || key.compare("IDI_READS[9]") == 0 || key.compare("IDI_READS[10]") == 0 || key.compare("IDI_READS[11]") == 0 || key.compare("IDI_READS[12]") == 0 || key.compare("IDI_READS[13]") == 0 || key.compare("IDI_READS[14]") == 0 || key.compare("IDI_READS[15]") == 0) { + val = mockIdiReadVal; + } else if (key.compare("IDI_WRITES[0]") == 0 || key.compare("IDI_WRITES[1]") == 0 || key.compare("IDI_WRITES[2]") == 0 || key.compare("IDI_WRITES[3]") == 0 || key.compare("IDI_WRITES[4]") == 0 || key.compare("IDI_WRITES[5]") == 0 || key.compare("IDI_WRITES[6]") == 0 || key.compare("IDI_WRITES[7]") == 0 || key.compare("IDI_WRITES[8]") == 0 || key.compare("IDI_WRITES[9]") == 0 || key.compare("IDI_WRITES[10]") == 0 || key.compare("IDI_WRITES[11]") == 0 || key.compare("IDI_WRITES[12]") == 0 || key.compare("IDI_WRITES[13]") == 0 || key.compare("IDI_WRITES[14]") == 0 || key.compare("IDI_WRITES[15]") == 0) { + val = mockIdiWriteVal; + } else if (key.compare("DISPLAY_VC1_READS[0]") == 0 || key.compare("DISPLAY_VC1_READS[1]") == 0 || key.compare("DISPLAY_VC1_READS[2]") == 0 || key.compare("DISPLAY_VC1_READS[3]") == 0 || key.compare("DISPLAY_VC1_READS[4]") == 0 || key.compare("DISPLAY_VC1_READS[5]") == 0 || key.compare("DISPLAY_VC1_READS[6]") == 0 || key.compare("DISPLAY_VC1_READS[7]") == 0 || key.compare("DISPLAY_VC1_READS[8]") == 0 || key.compare("DISPLAY_VC1_READS[9]") == 0 || key.compare("DISPLAY_VC1_READS[10]") == 0 || key.compare("DISPLAY_VC1_READS[11]") == 0 || key.compare("DISPLAY_VC1_READS[12]") == 0 || key.compare("DISPLAY_VC1_READS[13]") == 0 || key.compare("DISPLAY_VC1_READS[14]") == 0 || key.compare("DISPLAY_VC1_READS[15]") == 0) { + return ZE_RESULT_ERROR_UNKNOWN; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t mockReadTimeStampFailure(const std::string key, uint64_t &val) { + if (key.compare("IDI_READS[0]") == 0 || key.compare("IDI_READS[1]") == 0 || key.compare("IDI_READS[2]") == 0 || key.compare("IDI_READS[3]") == 0 || key.compare("IDI_READS[4]") == 0 || key.compare("IDI_READS[5]") == 0 || key.compare("IDI_READS[6]") == 0 || key.compare("IDI_READS[7]") == 0 || key.compare("IDI_READS[8]") == 0 || key.compare("IDI_READS[9]") == 0 || key.compare("IDI_READS[10]") == 0 || key.compare("IDI_READS[11]") == 0 || key.compare("IDI_READS[12]") == 0 || key.compare("IDI_READS[13]") == 0 || key.compare("IDI_READS[14]") == 0 || key.compare("IDI_READS[15]") == 0) { + val = mockIdiReadVal; + } else if (key.compare("IDI_WRITES[0]") == 0 || key.compare("IDI_WRITES[1]") == 0 || key.compare("IDI_WRITES[2]") == 0 || key.compare("IDI_WRITES[3]") == 0 || key.compare("IDI_WRITES[4]") == 0 || key.compare("IDI_WRITES[5]") == 0 || key.compare("IDI_WRITES[6]") == 0 || key.compare("IDI_WRITES[7]") == 0 || key.compare("IDI_WRITES[8]") == 0 || key.compare("IDI_WRITES[9]") == 0 || key.compare("IDI_WRITES[10]") == 0 || key.compare("IDI_WRITES[11]") == 0 || key.compare("IDI_WRITES[12]") == 0 || key.compare("IDI_WRITES[13]") == 0 || key.compare("IDI_WRITES[14]") == 0 || key.compare("IDI_WRITES[15]") == 0) { + val = mockIdiWriteVal; + } else if (key.compare("DISPLAY_VC1_READS[0]") == 0 || key.compare("DISPLAY_VC1_READS[1]") == 0 || key.compare("DISPLAY_VC1_READS[2]") == 0 || key.compare("DISPLAY_VC1_READS[3]") == 0 || key.compare("DISPLAY_VC1_READS[4]") == 0 || key.compare("DISPLAY_VC1_READS[5]") == 0 || key.compare("DISPLAY_VC1_READS[6]") == 0 || key.compare("DISPLAY_VC1_READS[7]") == 0 || key.compare("DISPLAY_VC1_READS[8]") == 0 || key.compare("DISPLAY_VC1_READS[9]") == 0 || key.compare("DISPLAY_VC1_READS[10]") == 0 || key.compare("DISPLAY_VC1_READS[11]") == 0 || key.compare("DISPLAY_VC1_READS[12]") == 0 || key.compare("DISPLAY_VC1_READS[13]") == 0 || key.compare("DISPLAY_VC1_READS[14]") == 0 || key.compare("DISPLAY_VC1_READS[15]") == 0) { + val = mockDisplayVc1ReadVal; + } else if (key.compare("MC_CAPTURE_TIMESTAMP") == 0) { + return ZE_RESULT_ERROR_UNKNOWN; + } else { + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + return ZE_RESULT_SUCCESS; + } +}; + +class MemoryFsAccess : public FsAccess {}; + +struct MockMemoryFsAccess : public MemoryFsAccess { + ze_result_t listDirectory(const std::string directory, std::vector &listOfTelemNodes) override { + if (directory.compare(baseTelemSysFS) == 0) { + listOfTelemNodes.push_back("telem1"); + listOfTelemNodes.push_back("telem2"); + listOfTelemNodes.push_back("telem3"); + listOfTelemNodes.push_back("telem4"); + listOfTelemNodes.push_back("telem5"); + return ZE_RESULT_SUCCESS; + } + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + + ze_result_t listDirectoryFailure(const std::string directory, std::vector &events) { + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + + ze_result_t getRealPath(const std::string path, std::string &buf) override { + if (path.compare(sysfsPahTelem1) == 0) { + buf = realPathTelem1; + } else if (path.compare(sysfsPahTelem2) == 0) { + buf = realPathTelem2; + } else if (path.compare(sysfsPahTelem3) == 0) { + buf = realPathTelem3; + } else if (path.compare(sysfsPahTelem4) == 0) { + buf = realPathTelem4; + } else if (path.compare(sysfsPahTelem5) == 0) { + buf = realPathTelem5; + } else { + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + return ZE_RESULT_SUCCESS; + } + + ze_result_t getRealPathFailure(const std::string path, std::string &buf) { + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + + MockMemoryFsAccess() = default; +}; + +class PublicLinuxMemoryImp : public L0::LinuxMemoryImp { + public: + PublicLinuxMemoryImp(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) : LinuxMemoryImp(pOsSysman, onSubdevice, subdeviceId) {} + PublicLinuxMemoryImp() = default; + using LinuxMemoryImp::getHbmFrequency; +}; + +} // 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 a29ef4fe63..a2473c6ca0 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 @@ -27,7 +27,7 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { SysmanDeviceFixture::SetUp(); pMemoryManagerOld = device->getDriverHandle()->getMemoryManager(); - pMemoryManager = new ::testing::NiceMock(*neoDevice->getExecutionEnvironment()); + pMemoryManager = new MockMemoryManagerSysman(*neoDevice->getExecutionEnvironment()); pMemoryManager->localMemorySupported[0] = false; device->getDriverHandle()->setMemoryManager(pMemoryManager); diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_dg1.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_dg1.cpp index fe845de0ec..6a7cedfcee 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_dg1.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_dg1.cpp @@ -18,7 +18,7 @@ namespace ult { constexpr uint32_t memoryHandleComponentCount = 1u; class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { protected: - Mock *pDrm = nullptr; + MockMemoryNeoDrm *pDrm = nullptr; Drm *pOriginalDrm = nullptr; void SetUp() override { @@ -28,11 +28,11 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { SysmanDeviceFixture::SetUp(); pMemoryManagerOld = device->getDriverHandle()->getMemoryManager(); - pMemoryManager = new ::testing::NiceMock(*neoDevice->getExecutionEnvironment()); + pMemoryManager = new MockMemoryManagerSysman(*neoDevice->getExecutionEnvironment()); pMemoryManager->localMemorySupported[0] = false; device->getDriverHandle()->setMemoryManager(pMemoryManager); - pDrm = new NiceMock>(const_cast(neoDevice->getRootDeviceEnvironment())); + pDrm = new MockMemoryNeoDrm(const_cast(neoDevice->getRootDeviceEnvironment())); pSysmanDevice = device->getSysmanHandle(); pSysmanDeviceImp = static_cast(pSysmanDevice); @@ -40,9 +40,6 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { pLinuxSysmanImp = static_cast(pOsSysman); pLinuxSysmanImp->pDrm = pDrm; - ON_CALL(*pDrm, queryMemoryInfo()) - .WillByDefault(::testing::Invoke(pDrm, &Mock::queryMemoryInfoMockPositiveTest)); - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { delete handle; } @@ -217,9 +214,7 @@ TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenGettingStateThenCall TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleAndIfQueryMemoryInfoFailsWhenGettingStateThenErrorIsReturned) { setLocalSupportedAndReinit(true); - - ON_CALL(*pDrm, queryMemoryInfo()) - .WillByDefault(::testing::Invoke(pDrm, &Mock::queryMemoryInfoMockReturnFalse)); + pDrm->mockQueryMemoryInfoReturnStatus.push_back(false); auto handles = getMemoryHandles(memoryHandleComponentCount); @@ -231,9 +226,7 @@ TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleAndIfQueryMemoryInfoFail TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleAndIfQueryMemoryInfoAndIfMemoryInfoIsNotCorrectWhenGettingStateThenErrorIsReturned) { setLocalSupportedAndReinit(true); - - ON_CALL(*pDrm, queryMemoryInfo()) - .WillByDefault(::testing::Invoke(pDrm, &Mock::queryMemoryInfoMockReturnFakeTrue)); + pDrm->mockQueryMemoryInfoReturnStatus.push_back(true); auto handles = getMemoryHandles(memoryHandleComponentCount); diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_prelim.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_prelim.cpp new file mode 100644 index 0000000000..9c34ee1562 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_prelim.cpp @@ -0,0 +1,1083 @@ +/* + * Copyright (C) 2021-2022 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/tools/source/sysman/linux/pmt/pmt_xml_offsets.h" +#include "level_zero/tools/source/sysman/memory/linux/os_memory_imp_prelim.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" + +#include "mock_memory_prelim.h" + +extern bool sysmanUltsEnable; + +using ::testing::Matcher; +using ::testing::Return; +namespace L0 { +namespace ult { + +constexpr int32_t memoryBusWidth = 128; // bus width in bits +constexpr int32_t numMemoryChannels = 8; +constexpr uint32_t memoryHandleComponentCount = 1u; +const std::string sampleGuid1 = "0xb15a0edc"; +class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { + protected: + std::unique_ptr pSysfsAccess; + std::unique_ptr pFsAccess; + SysfsAccess *pSysfsAccessOld = nullptr; + FsAccess *pFsAccessOriginal = nullptr; + MockMemoryNeoDrm *pDrm = nullptr; + Drm *pOriginalDrm = nullptr; + std::vector deviceHandles; + PRODUCT_FAMILY productFamily; + uint16_t stepping; + std::map pmtMapOriginal; + + void SetUp() override { + if (!sysmanUltsEnable) { + GTEST_SKIP(); + } + SysmanDeviceFixture::SetUp(); + + pSysfsAccessOld = pLinuxSysmanImp->pSysfsAccess; + pSysfsAccess = std::make_unique(); + pLinuxSysmanImp->pSysfsAccess = pSysfsAccess.get(); + + pMemoryManagerOld = device->getDriverHandle()->getMemoryManager(); + pMemoryManager = new MockMemoryManagerSysman(*neoDevice->getExecutionEnvironment()); + pMemoryManager->localMemorySupported[0] = false; + device->getDriverHandle()->setMemoryManager(pMemoryManager); + + pDrm = new MockMemoryNeoDrm(const_cast(neoDevice->getRootDeviceEnvironment())); + + pSysmanDevice = device->getSysmanHandle(); + pSysmanDeviceImp = static_cast(pSysmanDevice); + pOsSysman = pSysmanDeviceImp->pOsSysman; + pLinuxSysmanImp = static_cast(pOsSysman); + pLinuxSysmanImp->pDrm = pDrm; + pFsAccess = std::make_unique(); + pFsAccessOriginal = pLinuxSysmanImp->pFsAccess; + pLinuxSysmanImp->pFsAccess = pFsAccess.get(); + pDrm->setMemoryType(INTEL_HWCONFIG_MEMORY_TYPE_HBM2e); + pDrm->ioctlHelper = static_cast>(std::make_unique(*pDrm)); + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + uint32_t subDeviceCount = 0; + // We received a device handle. Check for subdevices in this device + Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr); + if (subDeviceCount == 0) { + deviceHandles.resize(1, device->toHandle()); + } else { + deviceHandles.resize(subDeviceCount, nullptr); + Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data()); + } + pmtMapOriginal = pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject; + pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.clear(); + for (auto &deviceHandle : deviceHandles) { + ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES}; + Device::fromHandle(deviceHandle)->getProperties(&deviceProperties); + auto pPmt = new MockMemoryPmt(pFsAccess.get(), deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE, + deviceProperties.subdeviceId); + pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject.emplace(deviceProperties.subdeviceId, pPmt); + } + auto &hwInfo = pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getHardwareInfo(); + productFamily = hwInfo.platform.eProductFamily; + stepping = NEO::HwInfoConfig::get(productFamily)->getSteppingFromHwRevId(hwInfo); + getMemoryHandles(0); + } + + void TearDown() override { + if (!sysmanUltsEnable) { + GTEST_SKIP(); + } + pLinuxSysmanImp->releasePmtObject(); + pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject = pmtMapOriginal; + device->getDriverHandle()->setMemoryManager(pMemoryManagerOld); + pLinuxSysmanImp->pFsAccess = pFsAccessOriginal; + pLinuxSysmanImp->pSysfsAccess = pSysfsAccessOld; + pLinuxSysmanImp->pDrm = pOriginalDrm; + if (pDrm != nullptr) { + delete pDrm; + pDrm = nullptr; + } + if (pMemoryManager != nullptr) { + delete pMemoryManager; + pMemoryManager = nullptr; + } + SysmanDeviceFixture::TearDown(); + } + + void setLocalSupportedAndReinit(bool supported) { + pMemoryManager->localMemorySupported[0] = supported; + + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + uint32_t subDeviceCount = 0; + std::vector deviceHandles; + // We received a device handle. Check for subdevices in this device + Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr); + if (subDeviceCount == 0) { + deviceHandles.resize(1, device->toHandle()); + } else { + deviceHandles.resize(subDeviceCount, nullptr); + Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data()); + } + pSysmanDeviceImp->pMemoryHandleContext->init(deviceHandles); + } + + std::vector getMemoryHandles(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, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidCountIsReturned) { + setLocalSupportedAndReinit(true); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, memoryHandleComponentCount); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenInvalidComponentCountWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidCountIsReturned) { + 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, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithLocalMemorySupportThenValidHandlesIsReturned) { + 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, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenZeroCountIsReturned) { + setLocalSupportedAndReinit(false); + + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, 0u); +} + +TEST_F(SysmanDeviceMemoryFixture, GivenInvalidComponentCountWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenZeroCountIsReturned) { + 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, GivenComponentCountZeroWhenEnumeratingMemoryModulesWithNoLocalMemorySupportThenValidHandlesAreReturned) { + 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 = getMemoryHandles(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_HBM); + + 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, numMemoryChannels); + EXPECT_EQ(properties.busWidth, memoryBusWidth); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetPropertiesAndQuerySystemInfoFailsThenVerifySysmanMemoryGetPropertiesCallReturnsMemoryTypeAsDdrAndNumberOfChannelsAsUnknown) { + pDrm->mockQuerySystemInfoReturnValue.push_back(false); + setLocalSupportedAndReinit(true); + + auto handles = getMemoryHandles(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_EQ(properties.numChannels, -1); + EXPECT_FALSE(properties.onSubdevice); + EXPECT_EQ(properties.subdeviceId, 0u); + EXPECT_EQ(properties.physicalSize, 0u); + EXPECT_EQ(properties.busWidth, memoryBusWidth); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetPropertiesAndQuerySystemInfoSucceedsButMemSysInfoIsNullThenVerifySysmanMemoryGetPropertiesCallReturnsMemoryTypeAsDdrAndNumberOfChannelsAsUnknown) { + pDrm->mockQuerySystemInfoReturnValue.push_back(true); + setLocalSupportedAndReinit(true); + + auto handles = getMemoryHandles(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_EQ(properties.numChannels, -1); + EXPECT_FALSE(properties.onSubdevice); + EXPECT_EQ(properties.subdeviceId, 0u); + EXPECT_EQ(properties.physicalSize, 0u); + EXPECT_EQ(properties.busWidth, memoryBusWidth); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetPropertiesWithHBMLocalMemoryThenVerifySysmanMemoryGetPropertiesCallSucceeds) { + pDrm->setMemoryType(INTEL_HWCONFIG_MEMORY_TYPE_HBM2); + setLocalSupportedAndReinit(true); + + auto handles = getMemoryHandles(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_HBM); + 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, numMemoryChannels); + EXPECT_EQ(properties.busWidth, memoryBusWidth); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetPropertiesWithLPDDR4LocalMemoryThenVerifySysmanMemoryGetPropertiesCallSucceeds) { + pDrm->setMemoryType(INTEL_HWCONFIG_MEMORY_TYPE_LPDDR4); + setLocalSupportedAndReinit(true); + + auto handles = getMemoryHandles(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_LPDDR4); + 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, numMemoryChannels); + EXPECT_EQ(properties.busWidth, memoryBusWidth); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetPropertiesWithLPDDR5LocalMemoryThenVerifySysmanMemoryGetPropertiesCallSucceeds) { + pDrm->setMemoryType(INTEL_HWCONFIG_MEMORY_TYPE_LPDDR5); + setLocalSupportedAndReinit(true); + + auto handles = getMemoryHandles(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_LPDDR5); + 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, numMemoryChannels); + EXPECT_EQ(properties.busWidth, memoryBusWidth); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetPropertiesWithDDRLocalMemoryThenVerifySysmanMemoryGetPropertiesCallSucceeds) { + pDrm->setMemoryType(INTEL_HWCONFIG_MEMORY_TYPE_GDDR6); + setLocalSupportedAndReinit(true); + + auto handles = getMemoryHandles(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, numMemoryChannels); + EXPECT_EQ(properties.busWidth, memoryBusWidth); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetStateThenVerifySysmanMemoryGetStateCallSucceeds) { + setLocalSupportedAndReinit(true); + + auto handles = getMemoryHandles(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, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthWhenPmtObjectIsNullThenFailureRetuned) { + for (auto &subDeviceIdToPmtEntry : pLinuxSysmanImp->mapOfSubDeviceIdToPmtObject) { + if (subDeviceIdToPmtEntry.second != nullptr) { + delete subDeviceIdToPmtEntry.second; + subDeviceIdToPmtEntry.second = nullptr; + } + } + setLocalSupportedAndReinit(true); + auto handles = getMemoryHandles(memoryHandleComponentCount); + for (auto &handle : handles) { + zes_mem_bandwidth_t bandwidth; + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + } +} + +HWTEST2_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthWhenVFID0IsActiveThenSuccessIsReturnedAndBandwidthIsValid, IsPVC) { + setLocalSupportedAndReinit(true); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto &handle : handles) { + zes_mem_bandwidth_t bandwidth{}; + uint64_t expectedReadCounters = 0, expectedWriteCounters = 0; + uint64_t expectedTimestamp = 0, expectedBandwidth = 0; + zes_mem_properties_t properties = {ZES_STRUCTURE_TYPE_MEM_PROPERTIES}; + zesMemoryGetProperties(handle, &properties); + + auto hwInfo = pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironment().getMutableHardwareInfo(); + hwInfo->platform.usRevId = NEO::HwInfoConfig::get(productFamily)->getHwRevIdFromStepping(REVISION_B, *hwInfo); + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + + pPmt->mockVfid0Status = true; + pSysfsAccess->mockReadUInt64Value.push_back(hbmRP0Frequency); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_SUCCESS); + expectedReadCounters = vF0Hbm0ReadValue + vF0Hbm1ReadValue + vF0Hbm2ReadValue + vF0Hbm3ReadValue; + EXPECT_EQ(bandwidth.readCounter, expectedReadCounters); + expectedWriteCounters = vF0Hbm0WriteValue + vF0Hbm1WriteValue + vF0Hbm2WriteValue + vF0Hbm3WriteValue; + EXPECT_EQ(bandwidth.writeCounter, expectedWriteCounters); + expectedTimestamp |= vF0TimestampHValue; + expectedTimestamp = (expectedTimestamp << 32) | vF0TimestampLValue; + EXPECT_EQ(bandwidth.timestamp, expectedTimestamp); + EXPECT_EQ(bandwidth.timestamp, expectedTimestamp); + expectedBandwidth = 128 * hbmRP0Frequency * 1000 * 1000 * 4; + expectedBandwidth /= 8; + EXPECT_EQ(bandwidth.maxBandwidth, expectedBandwidth); + } +} + +HWTEST2_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthWhenVFID1IsActiveThenSuccessIsReturnedAndBandwidthIsValid, IsPVC) { + setLocalSupportedAndReinit(true); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto &handle : handles) { + zes_mem_bandwidth_t bandwidth{}; + uint64_t expectedReadCounters = 0, expectedWriteCounters = 0; + uint64_t expectedTimestamp = 0, expectedBandwidth = 0; + zes_mem_properties_t properties = {ZES_STRUCTURE_TYPE_MEM_PROPERTIES}; + zesMemoryGetProperties(handle, &properties); + + auto hwInfo = pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironment().getMutableHardwareInfo(); + hwInfo->platform.usRevId = NEO::HwInfoConfig::get(productFamily)->getHwRevIdFromStepping(REVISION_B, *hwInfo); + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + + pPmt->mockVfid1Status = true; + pSysfsAccess->mockReadUInt64Value.push_back(hbmRP0Frequency); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_SUCCESS); + expectedReadCounters = vF1Hbm0ReadValue + vF1Hbm1ReadValue + vF1Hbm2ReadValue + vF1Hbm3ReadValue; + EXPECT_EQ(bandwidth.readCounter, expectedReadCounters); + expectedWriteCounters = vF1Hbm0WriteValue + vF1Hbm1WriteValue + vF1Hbm2WriteValue + vF1Hbm3WriteValue; + EXPECT_EQ(bandwidth.writeCounter, expectedWriteCounters); + expectedTimestamp |= vF1TimestampHValue; + expectedTimestamp = (expectedTimestamp << 32) | vF1TimestampLValue; + EXPECT_EQ(bandwidth.timestamp, expectedTimestamp); + expectedBandwidth = 128 * hbmRP0Frequency * 1000 * 1000 * 4; + expectedBandwidth /= 8; + EXPECT_EQ(bandwidth.maxBandwidth, expectedBandwidth); + } +} + +HWTEST2_F(SysmanDeviceMemoryFixture, GivenValidUsRevIdForRevisionBWhenCallingzesSysmanMemoryGetBandwidthThenSuccessIsReturnedAndBandwidthIsValid, IsPVC) { + setLocalSupportedAndReinit(true); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto &handle : handles) { + zes_mem_bandwidth_t bandwidth{}; + zes_mem_properties_t properties = {ZES_STRUCTURE_TYPE_MEM_PROPERTIES}; + zesMemoryGetProperties(handle, &properties); + + auto hwInfo = pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironment().getMutableHardwareInfo(); + hwInfo->platform.usRevId = NEO::HwInfoConfig::get(productFamily)->getHwRevIdFromStepping(REVISION_B, *hwInfo); + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + + pPmt->mockVfid1Status = true; + pSysfsAccess->mockReadUInt64Value.push_back(hbmRP0Frequency); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_SUCCESS); + uint64_t expectedBandwidth = 128 * hbmRP0Frequency * 1000 * 1000 * 4; + expectedBandwidth /= 8; + EXPECT_EQ(bandwidth.maxBandwidth, expectedBandwidth); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthForDg2PlatformThenSuccessIsReturnedAndBandwidthIsValid) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_DG2; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (const auto &handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + uint64_t expectedReadCounters = 0, expectedWriteCounters = 0; + uint64_t expectedTimestamp = 0, expectedBandwidth = 0; + uint64_t mockMaxBwDg2 = 1343616u; + pSysfsAccess->mockReadUInt64Value.push_back(mockMaxBwDg2); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_SUCCESS); + expectedReadCounters = numberMcChannels * (mockIdiReadVal + mockDisplayVc1ReadVal) * transactionSize; + EXPECT_EQ(expectedReadCounters, bandwidth.readCounter); + expectedWriteCounters = numberMcChannels * mockIdiWriteVal * transactionSize; + EXPECT_EQ(expectedWriteCounters, bandwidth.writeCounter); + expectedBandwidth = mockMaxBwDg2 * MbpsToBytesPerSecond; + EXPECT_EQ(expectedBandwidth, bandwidth.maxBandwidth); + expectedTimestamp = mcTimeStamp * 1e-8; + EXPECT_EQ(expectedTimestamp, bandwidth.timestamp); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthForUnknownPlatformThenFailureIsReturned) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_UNKNOWN; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (const auto &handle : handles) { + zes_mem_bandwidth_t bandwidth; + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthForDg2PlatformIfIdiReadFailsTheFailureIsReturned) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_DG2; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + pPmt->mockIdiReadValueFailureReturnStatus = ZE_RESULT_ERROR_UNKNOWN; + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + } +} + +HWTEST2_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthForDg2PlatformAndReadingMaxBwFailsThenMaxBwIsReturnedAsZero, IsDG2) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_DG2; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (const auto &handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_SUCCESS); + EXPECT_EQ(bandwidth.maxBandwidth, 0u); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthForDg2PlatformIfIdiWriteFailsTheFailureIsReturned) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_DG2; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + pPmt->mockIdiWriteFailureReturnStatus = ZE_RESULT_ERROR_UNKNOWN; + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthForDg2PlatformIfDisplayVc1ReadFailsTheFailureIsReturned) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_DG2; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + pPmt->mockDisplayVc1ReadFailureReturnStatus = ZE_RESULT_ERROR_UNKNOWN; + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingzesSysmanMemoryGetBandwidthForDg2PlatformIfTimeStampReadFailsTheFailureIsReturned) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_DG2; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + pPmt->mockReadTimeStampFailureReturnStatus = ZE_RESULT_ERROR_UNKNOWN; + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenCallinggetHbmFrequencyWhenProductFamilyIsPVCForSteppingIsBAndOnSubDeviceThenHbmFrequencyShouldNotBeZero) { + PublicLinuxMemoryImp *pLinuxMemoryImp = new PublicLinuxMemoryImp(pOsSysman, true, 1); + uint64_t hbmFrequency = 0; + pSysfsAccess->mockReadUInt64Value.push_back(hbmRP0Frequency); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + pLinuxMemoryImp->getHbmFrequency(IGFX_PVC, REVISION_B, hbmFrequency); + EXPECT_EQ(hbmFrequency, hbmRP0Frequency * 1000 * 1000); + delete pLinuxMemoryImp; +} + +TEST_F(SysmanDeviceMemoryFixture, GivenCallinggetHbmFrequencyWhenProductFamilyIsPVCForSteppingA0ThenHbmFrequencyShouldBeNotZero) { + PublicLinuxMemoryImp *pLinuxMemoryImp = new PublicLinuxMemoryImp(pOsSysman, true, 1); + uint64_t hbmFrequency = 0; + pLinuxMemoryImp->getHbmFrequency(IGFX_PVC, REVISION_A0, hbmFrequency); + uint64_t expectedHbmFrequency = 3.2 * gigaUnitTransferToUnitTransfer; + EXPECT_EQ(hbmFrequency, expectedHbmFrequency); + delete pLinuxMemoryImp; +} + +TEST_F(SysmanDeviceMemoryFixture, GivenCallinggetHbmFrequencyWhenProductFamilyIsXE_HP_SDVThenHbmFrequencyShouldBeNotZero) { + PublicLinuxMemoryImp *pLinuxMemoryImp = new PublicLinuxMemoryImp(pOsSysman, true, 1); + uint64_t hbmFrequency = 0; + pLinuxMemoryImp->getHbmFrequency(IGFX_XE_HP_SDV, REVISION_A0, hbmFrequency); + uint64_t expectedHbmFrequency = 2.8 * gigaUnitTransferToUnitTransfer; + EXPECT_EQ(hbmFrequency, expectedHbmFrequency); + delete pLinuxMemoryImp; +} + +TEST_F(SysmanDeviceMemoryFixture, GivenCallinggetHbmFrequencyWhenProductFamilyIsUnsupportedThenHbmFrequencyShouldBeZero) { + PublicLinuxMemoryImp *pLinuxMemoryImp = new PublicLinuxMemoryImp(pOsSysman, true, 1); + uint64_t hbmFrequency = 0; + pLinuxMemoryImp->getHbmFrequency(PRODUCT_FAMILY_FORCE_ULONG, REVISION_B, hbmFrequency); + EXPECT_EQ(hbmFrequency, 0u); + delete pLinuxMemoryImp; +} + +TEST_F(SysmanDeviceMemoryFixture, GivenCallinggetHbmFrequencyWhenProductFamilyIsPVCWhenSteppingIsUnknownThenHbmFrequencyShouldBeZero) { + PublicLinuxMemoryImp *pLinuxMemoryImp = new PublicLinuxMemoryImp(pOsSysman, true, 1); + uint64_t hbmFrequency = 0; + pLinuxMemoryImp->getHbmFrequency(IGFX_PVC, 255, hbmFrequency); + EXPECT_EQ(hbmFrequency, 0u); + delete pLinuxMemoryImp; +} + +TEST_F(SysmanDeviceMemoryFixture, GivenCallinggetHbmFrequencyWhenProductFamilyIsPVCForSteppingIsBAndFailedToReadFrequencyThenHbmFrequencyShouldBeZero) { + PublicLinuxMemoryImp *pLinuxMemoryImp = new PublicLinuxMemoryImp(pOsSysman, true, 1); + uint64_t hbmFrequency = 0; + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_ERROR_NOT_AVAILABLE); + pLinuxMemoryImp->getHbmFrequency(IGFX_PVC, REVISION_B, hbmFrequency); + EXPECT_EQ(hbmFrequency, 0u); + delete pLinuxMemoryImp; +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenBothVfid0AndVfid1AreTrueThenErrorIsReturnedWhileGettingMemoryBandwidth) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_PVC; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + pPmt->mockReadArgumentValue.push_back(1); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF0_VFID + pPmt->mockReadArgumentValue.push_back(1); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF1_VFID + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenBothVfid0AndVfid1AreFalseThenErrorIsReturnedWhileGettingMemoryBandwidth) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_PVC; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF0_VFID + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF1_VFID + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenGettingBandwidthAndIfPmtReadValueFailsThenErrorIsReturned) { + setLocalSupportedAndReinit(true); + auto hwInfo = *NEO::defaultHwInfo.get(); + hwInfo.platform.eProductFamily = IGFX_XE_HP_SDV; + pLinuxSysmanImp->getDeviceHandle()->getNEODevice()->getRootDeviceEnvironmentRef().setHwInfo(&hwInfo); + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_properties_t properties = {}; + zesMemoryGetProperties(handle, &properties); + + zes_mem_bandwidth_t bandwidth; + + auto pPmt = static_cast(pLinuxSysmanImp->getPlatformMonitoringTechAccess(properties.subdeviceId)); + + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_ERROR_UNKNOWN); // Return Failure while reading VF0_VFID + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + + pPmt->mockReadArgumentValue.push_back(1); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF0_VFID + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_ERROR_UNKNOWN); // Return Failure while reading VF1_VFID + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + + if (productFamily == IGFX_XE_HP_SDV) { + pPmt->mockReadArgumentValue.push_back(1); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF0_VFID + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF1_VFID + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_ERROR_UNKNOWN); // Return Failure while reading VF0_HBM0_READ + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + + pPmt->mockReadArgumentValue.push_back(1); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF0_VFID + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF1_VFID + pPmt->mockReadArgumentValue.push_back(vF0Hbm0ReadValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM0_READ + pPmt->mockReadArgumentValue.push_back(vF0Hbm0WriteValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_ERROR_UNKNOWN); // Return Failure while reading VF0_HBM0_Write + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + + pPmt->mockReadArgumentValue.push_back(1); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF0_VFID + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF1_VFID + pPmt->mockReadArgumentValue.push_back(vF0Hbm0ReadValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM0_READ + pPmt->mockReadArgumentValue.push_back(vF0Hbm0WriteValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM0_Write + pPmt->mockReadArgumentValue.push_back(vF0Hbm1ReadValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM1_READ + pPmt->mockReadArgumentValue.push_back(vF0Hbm1WriteValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM1_Write + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_ERROR_UNKNOWN); // Return Failure while reading VF0__TIMESTAMP_L + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + + pPmt->mockReadArgumentValue.push_back(1); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success after reading VF0_VFID + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF1_VFID + pPmt->mockReadArgumentValue.push_back(vF0Hbm0ReadValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM0_READ + pPmt->mockReadArgumentValue.push_back(vF0Hbm0WriteValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM0_Write + pPmt->mockReadArgumentValue.push_back(vF0Hbm1ReadValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM1_READ + pPmt->mockReadArgumentValue.push_back(vF0Hbm1WriteValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0_HBM1_Write + pPmt->mockReadArgumentValue.push_back(vF0TimestampLValue); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_SUCCESS); // Return success while reading VF0__TIMESTAMP_L + pPmt->mockReadArgumentValue.push_back(0); + pPmt->mockReadValueReturnStatus.push_back(ZE_RESULT_ERROR_UNKNOWN); // Return Failure while reading VF0__TIMESTAMP_H + EXPECT_EQ(zesMemoryGetBandwidth(handle, &bandwidth), ZE_RESULT_ERROR_UNKNOWN); + } + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenCallinggetHbmFrequencyWhenProductFamilyIsPVCAndSteppingIsNotA0ThenHbmFrequencyWillBeZero) { + PublicLinuxMemoryImp *pLinuxMemoryImp = new PublicLinuxMemoryImp; + uint64_t hbmFrequency = 0; + pLinuxMemoryImp->getHbmFrequency(IGFX_PVC, REVISION_A1, hbmFrequency); + EXPECT_EQ(hbmFrequency, 0u); + delete pLinuxMemoryImp; +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetStateAndIfMemoryHealthSysfsEntryNotAvaliableThenMemoryHealthWillsetUnknown) { + setLocalSupportedAndReinit(true); + + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_ERROR_NOT_AVAILABLE); + auto handles = getMemoryHandles(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_UNKNOWN); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetStateAndIfMemoryHealthSetRebootAlaramThenMemoryHealthWillsetDegraded) { + setLocalSupportedAndReinit(true); + + pSysfsAccess->mockReadStringValue.push_back("REBOOT_ALARM"); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + auto handles = getMemoryHandles(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_DEGRADED); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetStateAndIfMemoryHealthSetDegradedThenMemoryHealthWillReturnsetCritical) { + setLocalSupportedAndReinit(true); + + pSysfsAccess->mockReadStringValue.push_back("DEGRADED"); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + auto handles = getMemoryHandles(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_CRITICAL); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetStateAndIfMockMemoryHealthSetDegradeFailThenMemoryHealthWillsetReplace) { + setLocalSupportedAndReinit(true); + + pSysfsAccess->mockReadStringValue.push_back("DEGRADED_FAILED"); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + auto handles = getMemoryHandles(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_REPLACE); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetStateAndIfMockMemoryHealthSetRandomValueThenMemoryHealthWillsetUnknown) { + setLocalSupportedAndReinit(true); + + pSysfsAccess->mockReadStringValue.push_back("RANDOM"); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + auto handles = getMemoryHandles(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_UNKNOWN); + } +} + +TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetStateAndIfGetMemoryRegionsFailsThenErrorIsReturned) { + setLocalSupportedAndReinit(true); + + pDrm->mockReturnEmptyRegions = true; + auto handles = getMemoryHandles(memoryHandleComponentCount); + + for (auto handle : handles) { + zes_mem_state_t state; + EXPECT_EQ(zesMemoryGetState(handle, &state), ZE_RESULT_ERROR_UNSUPPORTED_FEATURE); + } +} + +TEST_F(SysmanMultiDeviceFixture, GivenValidDevicePointerWhenGettingMemoryPropertiesThenValidMemoryPropertiesRetrieved) { + zes_mem_properties_t properties = {}; + ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES}; + ze_bool_t isSubDevice = deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE; + Device::fromHandle(device)->getProperties(&deviceProperties); + LinuxMemoryImp *pLinuxMemoryImp = new LinuxMemoryImp(pOsSysman, isSubDevice, deviceProperties.subdeviceId); + EXPECT_EQ(ZE_RESULT_SUCCESS, pLinuxMemoryImp->getProperties(&properties)); + EXPECT_EQ(properties.subdeviceId, deviceProperties.subdeviceId); + EXPECT_EQ(properties.onSubdevice, isSubDevice); + delete pLinuxMemoryImp; +} + +class SysmanMultiDeviceMemoryFixture : public SysmanMultiDeviceFixture { + protected: + std::unique_ptr pSysfsAccess; + SysfsAccess *pSysfsAccessOld = nullptr; + MockMemoryNeoDrm *pDrm = nullptr; + Drm *pOriginalDrm = nullptr; + std::vector deviceHandles; + uint32_t subDeviceCount = 0; + + void SetUp() override { + if (!sysmanUltsEnable) { + GTEST_SKIP(); + } + SysmanMultiDeviceFixture::SetUp(); + + pSysfsAccessOld = pLinuxSysmanImp->pSysfsAccess; + pSysfsAccess = std::make_unique(); + pLinuxSysmanImp->pSysfsAccess = pSysfsAccess.get(); + + pMemoryManagerOld = device->getDriverHandle()->getMemoryManager(); + pMemoryManager = new MockMemoryManagerSysman(*neoDevice->getExecutionEnvironment()); + pMemoryManager->localMemorySupported[0] = true; + device->getDriverHandle()->setMemoryManager(pMemoryManager); + + pDrm = new MockMemoryNeoDrm(const_cast(neoDevice->getRootDeviceEnvironment())); + pDrm->ioctlHelper = static_cast>(std::make_unique(*pDrm)); + + pSysmanDevice = device->getSysmanHandle(); + pSysmanDeviceImp = static_cast(pSysmanDevice); + pOsSysman = pSysmanDeviceImp->pOsSysman; + pLinuxSysmanImp = static_cast(pOsSysman); + pLinuxSysmanImp->pDrm = pDrm; + + for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { + delete handle; + } + + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); + // We received a device handle. Check for subdevices in this device + Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr); + if (subDeviceCount == 0) { + deviceHandles.resize(1, device->toHandle()); + } else { + deviceHandles.resize(subDeviceCount, nullptr); + Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data()); + } + getMemoryHandles(0); + } + + void TearDown() override { + if (!sysmanUltsEnable) { + GTEST_SKIP(); + } + device->getDriverHandle()->setMemoryManager(pMemoryManagerOld); + SysmanMultiDeviceFixture::TearDown(); + pLinuxSysmanImp->pSysfsAccess = pSysfsAccessOld; + pLinuxSysmanImp->pDrm = pOriginalDrm; + if (pDrm != nullptr) { + delete pDrm; + pDrm = 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(); + uint32_t subDeviceCount = 0; + std::vector deviceHandles; + // We received a device handle. Check for subdevices in this device + Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr); + if (subDeviceCount == 0) { + deviceHandles.resize(1, device->toHandle()); + } else { + deviceHandles.resize(subDeviceCount, nullptr); + Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, deviceHandles.data()); + } + pSysmanDeviceImp->pMemoryHandleContext->init(deviceHandles); + } + + std::vector getMemoryHandles(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(SysmanMultiDeviceMemoryFixture, GivenValidMemoryHandleWhenGettingMemoryPropertiesWhileCallingGetValErrorThenValidMemoryPropertiesRetrieved) { + pSysfsAccess->mockReadStringValue.push_back("0"); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_ERROR_NOT_AVAILABLE); + + pSysmanDeviceImp->pMemoryHandleContext->init(deviceHandles); + for (auto deviceHandle : deviceHandles) { + zes_mem_properties_t properties = {}; + ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES}; + Device::fromHandle(deviceHandle)->getProperties(&deviceProperties); + bool isSubDevice = deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE; + LinuxMemoryImp *pLinuxMemoryImp = new LinuxMemoryImp(pOsSysman, isSubDevice, deviceProperties.subdeviceId); + EXPECT_EQ(ZE_RESULT_SUCCESS, pLinuxMemoryImp->getProperties(&properties)); + EXPECT_EQ(properties.subdeviceId, deviceProperties.subdeviceId); + EXPECT_EQ(properties.onSubdevice, isSubDevice); + EXPECT_EQ(properties.physicalSize, 0u); + delete pLinuxMemoryImp; + } +} + +TEST_F(SysmanMultiDeviceMemoryFixture, GivenValidDevicePointerWhenGettingMemoryPropertiesThenValidMemoryPropertiesRetrieved) { + pSysfsAccess->mockReadStringValue.push_back(mockPhysicalSize); + pSysfsAccess->mockReadReturnStatus.push_back(ZE_RESULT_SUCCESS); + pSysfsAccess->isRepeated = true; + + setLocalSupportedAndReinit(true); + std::vector devicesProperties; + for (auto deviceHandle : deviceHandles) { + ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES}; + Device::fromHandle(deviceHandle)->getProperties(&deviceProperties); + devicesProperties.push_back(deviceProperties); + } + uint32_t count = 0; + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS); + EXPECT_EQ(count, deviceHandles.size()); + + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + uint32_t subDeviceIndex = 0; + for (auto handle : handles) { + zes_mem_properties_t properties = {}; + EXPECT_EQ(zesMemoryGetProperties(handle, &properties), ZE_RESULT_SUCCESS); + EXPECT_EQ(properties.subdeviceId, devicesProperties[subDeviceIndex].subdeviceId); + EXPECT_TRUE(properties.onSubdevice); + EXPECT_EQ(properties.physicalSize, strtoull(mockPhysicalSize.c_str(), nullptr, 16)); + subDeviceIndex++; + } +} + +TEST_F(SysmanMultiDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingZetSysmanMemoryGetStateThenVerifySysmanMemoryGetStateCallSucceeds) { + setLocalSupportedAndReinit(true); + + auto handles = getMemoryHandles(subDeviceCount); + zes_mem_state_t state1; + ze_result_t result = zesMemoryGetState(handles[0], &state1); + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + EXPECT_EQ(state1.health, ZES_MEM_HEALTH_OK); + EXPECT_EQ(state1.size, probedSizeRegionOne); + EXPECT_EQ(state1.free, unallocatedSizeRegionOne); + + zes_mem_state_t state2; + result = zesMemoryGetState(handles[1], &state2); + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + EXPECT_EQ(state2.health, ZES_MEM_HEALTH_OK); + EXPECT_EQ(state2.size, probedSizeRegionFour); + EXPECT_EQ(state2.free, unallocatedSizeRegionFour); +} + +} // namespace ult +} // namespace L0