diff --git a/opencl/test/unit_test/memory_manager/CMakeLists.txt b/opencl/test/unit_test/memory_manager/CMakeLists.txt index 8d26066a69..e6e8092060 100644 --- a/opencl/test/unit_test/memory_manager/CMakeLists.txt +++ b/opencl/test/unit_test/memory_manager/CMakeLists.txt @@ -11,10 +11,8 @@ set(IGDRCL_SRCS_tests_memory_manager ${CMAKE_CURRENT_SOURCE_DIR}/internal_allocation_storage_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/memory_manager_multi_device_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/memory_manager_tests.cpp - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/memory_manager_allocate_in_device_pool_tests.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/memory_manager_allocate_in_device_pool_tests.inl - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/memory_manager_allocate_in_preferred_pool_tests.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/memory_manager_allocate_in_preferred_pool_tests.inl + ${CMAKE_CURRENT_SOURCE_DIR}/memory_manager_allocate_in_device_pool_tests.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/memory_manager_allocate_in_preferred_pool_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/surface_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/storage_info_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/unified_memory_manager_tests.cpp diff --git a/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_device_pool_tests.cpp b/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_device_pool_tests.cpp index b6a4162892..eda1f0d49d 100644 --- a/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_device_pool_tests.cpp +++ b/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_device_pool_tests.cpp @@ -1,77 +1,401 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * */ -#include "opencl/test/unit_test/memory_manager/memory_manager_allocate_in_device_pool_tests.inl" +#include "shared/source/aub/aub_helper.h" +#include "shared/source/execution_environment/execution_environment.h" +#include "shared/source/memory_manager/os_agnostic_memory_manager.h" +#include "shared/test/common/helpers/debug_manager_state_restore.h" +#include "shared/test/common/mocks/mock_device.h" +#include "shared/test/common/mocks/mock_execution_environment.h" +#include "shared/test/common/mocks/ult_device_factory.h" -#include "shared/source/helpers/array_count.h" +#include "opencl/source/helpers/memory_properties_helpers.h" +#include "opencl/source/mem_obj/mem_obj_helper.h" +#include "opencl/test/unit_test/mocks/mock_gmm.h" +#include "opencl/test/unit_test/mocks/mock_memory_manager.h" +#include "test.h" -TEST(MemoryManagerTest, givenNotSetUseSystemMemoryWhenGraphicsAllocationInDevicePoolIsAllocatedThenAllocationIsReturned) { +using namespace NEO; + +TEST(MemoryManagerTest, givenSetUseSytemMemoryWhenGraphicsAllocationInDevicePoolIsAllocatedThenNullptrIsReturned) { MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); MockMemoryManager memoryManager(false, false, executionEnvironment); + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success; + AllocationData allocData; + allocData.size = MemoryConstants::pageSize; + allocData.flags.useSystemMemory = true; + allocData.flags.allocateMemory = true; + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_EQ(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::RetryInNonDevicePool, status); +} + +TEST(MemoryManagerTest, givenAllowed32BitAndFroce32BitWhenGraphicsAllocationInDevicePoolIsAllocatedThenNullptrIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, false, executionEnvironment); + memoryManager.setForce32BitAllocations(true); + + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success; + AllocationData allocData; + allocData.size = MemoryConstants::pageSize; + allocData.flags.allow32Bit = true; + allocData.flags.allocateMemory = true; + + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_EQ(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::RetryInNonDevicePool, status); +} + +TEST(AllocationFlagsTest, givenAllocateMemoryFlagWhenGetAllocationFlagsIsCalledThenAllocateFlagIsCorrectlySet) { + HardwareInfo hwInfo(*defaultHwInfo); + UltDeviceFactory deviceFactory{1, 0}; + auto pDevice = deviceFactory.rootDevices[0]; + MemoryProperties memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, 0, 0, pDevice); + auto allocationProperties = MemoryPropertiesHelper::getAllocationProperties(0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER, false, hwInfo, {}); + EXPECT_TRUE(allocationProperties.flags.allocateMemory); + + auto allocationProperties2 = MemoryPropertiesHelper::getAllocationProperties(0, memoryProperties, false, 0, GraphicsAllocation::AllocationType::BUFFER, false, hwInfo, {}); + EXPECT_FALSE(allocationProperties2.flags.allocateMemory); +} + +TEST(UncacheableFlagsTest, givenUncachedResourceFlagWhenGetAllocationFlagsIsCalledThenUncacheableFlagIsCorrectlySet) { + cl_mem_flags_intel flagsIntel = CL_MEM_LOCALLY_UNCACHED_RESOURCE; + UltDeviceFactory deviceFactory{1, 0}; + auto pDevice = deviceFactory.rootDevices[0]; + MemoryProperties memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, flagsIntel, 0, pDevice); + auto allocationFlags = MemoryPropertiesHelper::getAllocationProperties( + 0, memoryProperties, false, 0, GraphicsAllocation::AllocationType::BUFFER, false, pDevice->getHardwareInfo(), {}); + EXPECT_TRUE(allocationFlags.flags.uncacheable); + + flagsIntel = 0; + memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, flagsIntel, 0, pDevice); + auto allocationFlags2 = MemoryPropertiesHelper::getAllocationProperties( + 0, memoryProperties, false, 0, GraphicsAllocation::AllocationType::BUFFER, false, pDevice->getHardwareInfo(), {}); + EXPECT_FALSE(allocationFlags2.flags.uncacheable); +} + +TEST(AllocationFlagsTest, givenReadOnlyResourceFlagWhenGetAllocationFlagsIsCalledThenFlushL3FlagsAreCorrectlySet) { + cl_mem_flags flags = CL_MEM_READ_ONLY; + UltDeviceFactory deviceFactory{1, 0}; + auto pDevice = deviceFactory.rootDevices[0]; + MemoryProperties memoryProperties = MemoryPropertiesHelper::createMemoryProperties(flags, 0, 0, pDevice); + + auto allocationFlags = + MemoryPropertiesHelper::getAllocationProperties( + 0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER, false, pDevice->getHardwareInfo(), {}); + EXPECT_FALSE(allocationFlags.flags.flushL3RequiredForRead); + EXPECT_FALSE(allocationFlags.flags.flushL3RequiredForWrite); + + memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, 0, 0, pDevice); + auto allocationFlags2 = MemoryPropertiesHelper::getAllocationProperties( + 0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER, false, pDevice->getHardwareInfo(), {}); + EXPECT_TRUE(allocationFlags2.flags.flushL3RequiredForRead); + EXPECT_TRUE(allocationFlags2.flags.flushL3RequiredForWrite); +} + +TEST(StorageInfoTest, whenStorageInfoIsCreatedWithDefaultConstructorThenReturnsOneHandle) { + StorageInfo storageInfo; + EXPECT_EQ(1u, storageInfo.getNumBanks()); +} + +class MockMemoryManagerBaseImplementationOfDevicePool : public MemoryManagerCreate { + public: + using MemoryManagerCreate::MemoryManagerCreate; + + GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) override { + if (failInAllocate) { + return nullptr; + } + return OsAgnosticMemoryManager::allocateGraphicsMemoryInDevicePool(allocationData, status); + } + + GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override { + if (failInAllocate) { + return nullptr; + } + return OsAgnosticMemoryManager::allocateGraphicsMemoryWithAlignment(allocationData); + } + + void *allocateSystemMemory(size_t size, size_t alignment) override { + if (failInAllocate) { + return nullptr; + } + return OsAgnosticMemoryManager::allocateSystemMemory(size, alignment); + } + bool failInAllocate = false; +}; + +using MemoryManagerTests = ::testing::Test; + +TEST(MemoryManagerTest, givenNotSetUseSystemMemoryWhenGraphicsAllocationInDevicePoolIsAllocatedThenLocalMemoryAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Error; AllocationData allocData; + allocData.allFlags = 0; allocData.size = MemoryConstants::pageSize; allocData.flags.allocateMemory = true; auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); EXPECT_NE(nullptr, allocation); EXPECT_EQ(MemoryManager::AllocationStatus::Success, status); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); memoryManager.freeGraphicsMemory(allocation); } -TEST(MemoryManagerTest, givenImageOrSharedResourceCopyWhenGraphicsAllocationInDevicePoolIsAllocatedThenNullptrIsReturned) { +TEST(MemoryManagerTest, givenNotSetUseSystemMemoryWhenGraphicsAllocationInDevicePoolIsAllocatedThenLocalMemoryAllocationHasCorrectStorageInfoAndFlushL3IsSet) { MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - + MockMemoryManager memoryManager(false, true, executionEnvironment); MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Error; AllocationData allocData; + allocData.allFlags = 0; + allocData.size = MemoryConstants::pageSize; + allocData.flags.allocateMemory = true; + allocData.storageInfo.memoryBanks = 0x1; + allocData.storageInfo.pageTablesVisibility = 0x2; + allocData.storageInfo.cloningOfPageTables = false; + allocData.flags.flushL3 = true; + + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_NE(nullptr, allocation); + + EXPECT_EQ(allocData.storageInfo.memoryBanks, allocation->storageInfo.memoryBanks); + EXPECT_EQ(allocData.storageInfo.pageTablesVisibility, allocation->storageInfo.pageTablesVisibility); + EXPECT_FALSE(allocation->storageInfo.cloningOfPageTables); + EXPECT_TRUE(allocation->isFlushL3Required()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenEnabledLocalMemoryAndUseSytemMemoryWhenGraphicsAllocationInDevicePoolIsAllocatedThenNullptrIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success; + AllocationData allocData; + allocData.allFlags = 0; + allocData.size = MemoryConstants::pageSize; + allocData.flags.useSystemMemory = true; + allocData.flags.allocateMemory = true; + + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_EQ(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::RetryInNonDevicePool, status); +} + +TEST(MemoryManagerTest, givenEnabledLocalMemoryAndAllowed32BitAndForce32BitWhenGraphicsAllocationInDevicePoolIsAllocatedThenNullptrIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + memoryManager.setForce32BitAllocations(true); + + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success; + AllocationData allocData; + allocData.allFlags = 0; + allocData.size = MemoryConstants::pageSize; + allocData.flags.allow32Bit = true; + allocData.flags.allocateMemory = true; + + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_EQ(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::RetryInNonDevicePool, status); +} + +TEST(MemoryManagerTest, givenEnabledLocalMemoryAndAllowed32BitWhen32BitIsNotForcedThenGraphicsAllocationInDevicePoolReturnsLocalMemoryAllocation) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + memoryManager.setForce32BitAllocations(false); + + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success; + AllocationData allocData; + allocData.allFlags = 0; + allocData.size = MemoryConstants::pageSize; + allocData.flags.allow32Bit = true; + allocData.flags.allocateMemory = true; + + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + ASSERT_NE(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::Success, status); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenEnabledLocalMemoryWhenAllocate32BitFailsThenGraphicsAllocationInDevicePoolReturnsError) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success; + AllocationData allocData; + allocData.type = GraphicsAllocation::AllocationType::KERNEL_ISA; // HEAP_INTERNAL will be used + allocData.allFlags = 0; allocData.size = MemoryConstants::pageSize; allocData.flags.allocateMemory = true; - GraphicsAllocation::AllocationType types[] = {GraphicsAllocation::AllocationType::IMAGE, - GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY}; + memoryManager.failAllocate32Bit = true; - for (auto type : types) { - allocData.type = type; - auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); - EXPECT_EQ(nullptr, allocation); - EXPECT_EQ(MemoryManager::AllocationStatus::RetryInNonDevicePool, status); - } + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_EQ(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::Error, status); + + memoryManager.freeGraphicsMemory(allocation); } -TEST(MemoryManagerTest, givenSvmGpuAllocationTypeWhenAllocationSystemMemoryFailsThenReturnNull) { +HWTEST_F(MemoryManagerTests, givenEnabledLocalMemoryWhenAllocatingDebugAreaThenHeapInternalDeviceFrontWindowIsUsed) { MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); + MemoryManagerCreate osAgnosticMemoryManager(false, true, executionEnvironment); + NEO::AllocationProperties properties{0, true, MemoryConstants::pageSize64k, + NEO::GraphicsAllocation::AllocationType::DEBUG_MODULE_AREA, + false, + mockDeviceBitfield}; + properties.flags.use32BitFrontWindow = true; + + auto &hwHelper = HwHelper::get(defaultHwInfo->platform.eRenderCoreFamily); + auto systemMemoryPlacement = hwHelper.useSystemMemoryPlacementForISA(*defaultHwInfo); + + HeapIndex expectedHeap = HeapIndex::TOTAL_HEAPS; + HeapIndex baseHeap = HeapIndex::TOTAL_HEAPS; + if (systemMemoryPlacement) { + expectedHeap = HeapIndex::HEAP_INTERNAL_FRONT_WINDOW; + baseHeap = HeapIndex::HEAP_INTERNAL; + } else { + expectedHeap = HeapIndex::HEAP_INTERNAL_DEVICE_FRONT_WINDOW; + baseHeap = HeapIndex::HEAP_INTERNAL_DEVICE_MEMORY; + } + auto moduleDebugArea = osAgnosticMemoryManager.allocateGraphicsMemoryWithProperties(properties); + auto gpuAddress = moduleDebugArea->getGpuAddress(); + EXPECT_LE(GmmHelper::canonize(osAgnosticMemoryManager.getGfxPartition(0)->getHeapBase(expectedHeap)), gpuAddress); + EXPECT_GT(GmmHelper::canonize(osAgnosticMemoryManager.getGfxPartition(0)->getHeapLimit(expectedHeap)), gpuAddress); + EXPECT_EQ(GmmHelper::canonize(osAgnosticMemoryManager.getGfxPartition(0)->getHeapBase(expectedHeap)), moduleDebugArea->getGpuBaseAddress()); + EXPECT_EQ(GmmHelper::canonize(osAgnosticMemoryManager.getGfxPartition(0)->getHeapBase(baseHeap)), moduleDebugArea->getGpuBaseAddress()); + + osAgnosticMemoryManager.freeGraphicsMemory(moduleDebugArea); +} + +TEST(BaseMemoryManagerTest, givenMemoryManagerWithForced32BitsWhenSystemMemoryIsNotSetAnd32BitNotAllowedThenAllocateInDevicePoolReturnsLocalMemory) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManagerBaseImplementationOfDevicePool memoryManager(false, true, executionEnvironment); MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Error; AllocationData allocData; + allocData.allFlags = 0; + allocData.size = MemoryConstants::pageSize; + allocData.flags.allocateMemory = true; + + memoryManager.setForce32BitAllocations(true); + + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_NE(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::Success, status); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(BaseMemoryManagerTest, givenDebugVariableSetWhenCompressedBufferIsCreatedThenCreateCompressedGmm) { + DebugManagerStateRestore restore; + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + executionEnvironment.initGmm(); + MemoryManagerCreate memoryManager(false, true, executionEnvironment); + + AllocationProperties allocPropertiesBuffer(mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + AllocationProperties allocPropertiesBufferCompressed(mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, mockDeviceBitfield); + + DebugManager.flags.RenderCompressedBuffersEnabled.set(1); + auto allocationBuffer = memoryManager.allocateGraphicsMemoryInPreferredPool(allocPropertiesBuffer, nullptr); + auto allocationBufferCompressed = memoryManager.allocateGraphicsMemoryInPreferredPool(allocPropertiesBufferCompressed, nullptr); + EXPECT_EQ(nullptr, allocationBuffer->getDefaultGmm()); + EXPECT_NE(nullptr, allocationBufferCompressed->getDefaultGmm()); + EXPECT_TRUE(allocationBufferCompressed->getDefaultGmm()->isCompressionEnabled); + memoryManager.freeGraphicsMemory(allocationBuffer); + memoryManager.freeGraphicsMemory(allocationBufferCompressed); + + DebugManager.flags.RenderCompressedBuffersEnabled.set(0); + allocationBuffer = memoryManager.allocateGraphicsMemoryInPreferredPool(allocPropertiesBuffer, nullptr); + allocationBufferCompressed = memoryManager.allocateGraphicsMemoryInPreferredPool(allocPropertiesBufferCompressed, nullptr); + EXPECT_EQ(nullptr, allocationBuffer->getDefaultGmm()); + EXPECT_EQ(nullptr, allocationBufferCompressed->getDefaultGmm()); + memoryManager.freeGraphicsMemory(allocationBuffer); + memoryManager.freeGraphicsMemory(allocationBufferCompressed); +} + +TEST(BaseMemoryManagerTest, givenMemoryManagerWithForced32BitsWhenSystemMemoryIsNotSetAnd32BitAllowedThenAllocateInDevicePoolReturnsRetryInNonDevicePool) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManagerBaseImplementationOfDevicePool memoryManager(false, true, executionEnvironment); + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Error; + AllocationData allocData; + allocData.allFlags = 0; + allocData.size = MemoryConstants::pageSize; + allocData.flags.allocateMemory = true; + allocData.flags.allow32Bit = true; + + memoryManager.setForce32BitAllocations(true); + + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_EQ(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::RetryInNonDevicePool, status); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(BaseMemoryManagerTest, givenMemoryManagerWhenAllocateFailsThenAllocateInDevicePoolReturnsError) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManagerBaseImplementationOfDevicePool memoryManager(false, true, executionEnvironment); + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Error; + AllocationData allocData; + allocData.allFlags = 0; + allocData.size = MemoryConstants::pageSize; + allocData.flags.allocateMemory = true; + allocData.flags.allow32Bit = true; + + memoryManager.failInAllocate = true; + auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); + EXPECT_EQ(nullptr, allocation); + EXPECT_EQ(MemoryManager::AllocationStatus::Error, status); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(BaseMemoryManagerTest, givenSvmGpuAllocationTypeWhenAllocateSystemMemoryFailsThenReturnNull) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + + if (!executionEnvironment.rootDeviceEnvironments[0]->isFullRangeSvm()) { + return; + } + + MockMemoryManagerBaseImplementationOfDevicePool memoryManager(false, true, executionEnvironment); + MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Error; + AllocationData allocData; + allocData.allFlags = 0; allocData.size = MemoryConstants::pageSize; allocData.type = GraphicsAllocation::AllocationType::SVM_GPU; allocData.hostPtr = reinterpret_cast(0x1000); - memoryManager.failAllocateSystemMemory = true; + memoryManager.failInAllocate = true; auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); EXPECT_EQ(nullptr, allocation); EXPECT_EQ(MemoryManager::AllocationStatus::Error, status); + + memoryManager.freeGraphicsMemory(allocation); } -TEST(MemoryManagerTest, givenSvmGpuAllocationTypeWhenAllocationSucceedThenReturnGpuAddressAsHostPtr) { - if (defaultHwInfo->capabilityTable.gpuAddressSpace != maxNBitValue(48) && defaultHwInfo->capabilityTable.gpuAddressSpace != maxNBitValue(47)) { +TEST(BaseMemoryManagerTest, givenSvmGpuAllocationTypeWhenAllocationSucceedThenReturnGpuAddressAsHostPtrAndCpuAllocation) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + + if (!executionEnvironment.rootDeviceEnvironments[0]->isFullRangeSvm()) { return; } - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - + MockMemoryManagerBaseImplementationOfDevicePool memoryManager(false, true, executionEnvironment); MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Error; AllocationData allocData; + + allocData.allFlags = 0; allocData.size = MemoryConstants::pageSize; allocData.type = GraphicsAllocation::AllocationType::SVM_GPU; allocData.hostPtr = reinterpret_cast(0x1000); @@ -85,9 +409,170 @@ TEST(MemoryManagerTest, givenSvmGpuAllocationTypeWhenAllocationSucceedThenReturn memoryManager.freeGraphicsMemory(allocation); } +TEST(MemoryAllocationTest, givenMultiTileVisiblityWhenAskedForFlagsThenL3NeedsToBeFlushed) { + HardwareInfo hwInfo(*defaultHwInfo); + UltDeviceFactory deviceFactory{1, 0}; + auto memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, 0, 0, deviceFactory.rootDevices[0]); + auto allocationFlags = MemoryPropertiesHelper::getAllocationProperties(0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER, false, hwInfo, {}); + EXPECT_TRUE(allocationFlags.flags.flushL3RequiredForRead); + EXPECT_TRUE(allocationFlags.flags.flushL3RequiredForWrite); +} + +TEST(MemoryAllocationTest, givenMultiTileVisiblityAndUncachedWhenAskedForFlagsThenL3DoesNotNeedToBeFlushed) { + UltDeviceFactory deviceFactory{1, 0}; + auto pDevice = deviceFactory.rootDevices[0]; + cl_mem_flags_intel flagsIntel = CL_MEM_LOCALLY_UNCACHED_RESOURCE; + MemoryProperties memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, flagsIntel, 0, pDevice); + HardwareInfo hwInfo(*defaultHwInfo); + + auto allocationFlags = MemoryPropertiesHelper::getAllocationProperties( + 0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER, false, hwInfo, {}); + EXPECT_FALSE(allocationFlags.flags.flushL3RequiredForRead); + EXPECT_FALSE(allocationFlags.flags.flushL3RequiredForWrite); +} + +TEST(MemoryAllocationTest, givenAubDumpForceAllToLocalMemoryWhenMemoryAllocationIsCreatedThenItHasLocalMemoryPool) { + DebugManagerStateRestore debugRestorer; + DebugManager.flags.AUBDumpForceAllToLocalMemory.set(true); + + MemoryAllocation allocation(mockRootDeviceIndex, GraphicsAllocation::AllocationType::UNKNOWN, nullptr, reinterpret_cast(0x1000), 0x1000, + 0x1000, 0, MemoryPool::System4KBPages, false, false, mockMaxOsContextCount); + EXPECT_EQ(MemoryPool::LocalMemory, allocation.getMemoryPool()); +} + +TEST(MemoryAllocationTest, givenAubDumpForceAllToLocalMemoryWhenMemoryAllocationIsOverridenThenItHasLocalMemoryPool) { + DebugManagerStateRestore debugRestorer; + DebugManager.flags.AUBDumpForceAllToLocalMemory.set(true); + + MemoryAllocation allocation(mockRootDeviceIndex, GraphicsAllocation::AllocationType::UNKNOWN, nullptr, reinterpret_cast(0x1000), 0x1000, + 0x1000, 0, MemoryPool::System4KBPages, false, false, mockMaxOsContextCount); + allocation.overrideMemoryPool(MemoryPool::System64KBPages); + EXPECT_EQ(MemoryPool::LocalMemory, allocation.getMemoryPool()); +} + +HWTEST_F(MemoryManagerTests, givenEnabledLocalMemoryWhenAllocateInternalAllocationInDevicePoolThen32BitAllocationIsCreated) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::INTERNAL_HEAP, mockDeviceBitfield}, nullptr); + EXPECT_NE(nullptr, allocation); + EXPECT_TRUE(allocation->is32BitAllocation()); + EXPECT_TRUE(memoryManager.allocationInDevicePoolCreated); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenDisabledLocalMemoryWhenAllocateInternalAllocationInDevicePoolThen32BitAllocationIsCreatedInNonDevicePool) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, false, executionEnvironment); + + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::INTERNAL_HEAP, mockDeviceBitfield}, nullptr); + EXPECT_NE(nullptr, allocation); + EXPECT_TRUE(allocation->is32BitAllocation()); + EXPECT_NE(MemoryPool::LocalMemory, allocation->getMemoryPool()); + EXPECT_FALSE(memoryManager.allocationInDevicePoolCreated); + memoryManager.freeGraphicsMemory(allocation); +} + TEST(MemoryManagerTest, givenOsAgnosticMemoryManagerWhenGetLocalMemoryIsCalledThenSizeOfLocalMemoryIsReturned) { MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); MockMemoryManager memoryManager(false, false, executionEnvironment); - EXPECT_EQ(0 * GB, memoryManager.getLocalMemorySize(0u, 0xF)); + EXPECT_EQ(AubHelper::getMemBankSize(executionEnvironment.rootDeviceEnvironments[0]->getHardwareInfo()), memoryManager.getLocalMemorySize(0u, 0xF)); +} + +HWTEST_F(MemoryManagerTests, givenEnabledLocalMemoryWhenAllocatingKernelIsaThenLocalMemoryPoolIsUsed) { + auto hwInfo = *defaultHwInfo; + DebugManagerStateRestore restorer; + DebugManager.flags.EnableLocalMemory.set(true); + hwInfo.featureTable.ftrLocalMemory = true; + + MockExecutionEnvironment executionEnvironment(&hwInfo); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::KERNEL_ISA, mockDeviceBitfield}, nullptr); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); + EXPECT_TRUE(memoryManager.allocationInDevicePoolCreated); + + memoryManager.freeGraphicsMemory(allocation); + + allocation = memoryManager.allocateGraphicsMemoryInPreferredPool({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::KERNEL_ISA_INTERNAL, mockDeviceBitfield}, nullptr); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); + EXPECT_TRUE(memoryManager.allocationInDevicePoolCreated); + + memoryManager.freeGraphicsMemory(allocation); +} + +HWTEST_F(MemoryManagerTests, givenEnabledLocalMemoryWhenAllocateKernelIsaInDevicePoolThenLocalMemoryPoolIsUsed) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::INTERNAL_HEAP, mockDeviceBitfield}, nullptr); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); + EXPECT_TRUE(memoryManager.allocationInDevicePoolCreated); + + memoryManager.freeGraphicsMemory(allocation); +} + +HWTEST_F(MemoryManagerTests, givenEnabledLocalMemoryWhenLinearStreamIsAllocatedInDevicePoolThenLocalMemoryPoolIsUsed) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::LINEAR_STREAM, mockDeviceBitfield}, nullptr); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); + EXPECT_TRUE(memoryManager.allocationInDevicePoolCreated); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(StorageInfoTest, whenMemoryBanksAreSetToZeroThenOneHandleIsReturned) { + StorageInfo storageInfo; + storageInfo.memoryBanks = 0u; + EXPECT_EQ(1u, storageInfo.getNumBanks()); +} + +TEST(StorageInfoTest, whenMemoryBanksAreNotSetToZeroThenNumberOfEnabledBitsIsReturnedAsNumberOfHandles) { + StorageInfo storageInfo; + storageInfo.memoryBanks = 0b001; + EXPECT_EQ(1u, storageInfo.getNumBanks()); + storageInfo.memoryBanks = 0b010; + EXPECT_EQ(1u, storageInfo.getNumBanks()); + storageInfo.memoryBanks = 0b100; + EXPECT_EQ(1u, storageInfo.getNumBanks()); + storageInfo.memoryBanks = 0b011; + EXPECT_EQ(2u, storageInfo.getNumBanks()); + storageInfo.memoryBanks = 0b101; + EXPECT_EQ(2u, storageInfo.getNumBanks()); + storageInfo.memoryBanks = 0b110; + EXPECT_EQ(2u, storageInfo.getNumBanks()); + storageInfo.memoryBanks = 0b111; + EXPECT_EQ(3u, storageInfo.getNumBanks()); +} + +TEST(MemoryManagerTest, givenNotSetUseSystemMemoryWhenGraphicsAllocationInDevicePoolIsAllocatedThenlocalMemoryUsageIsUpdated) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + + MockMemoryManager memoryManager(false, true, executionEnvironment); + + AllocationProperties allocProperties(mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool(allocProperties, nullptr); + EXPECT_NE(nullptr, allocation); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); + EXPECT_EQ(allocation->getUnderlyingBufferSize(), memoryManager.localMemoryUsageBankSelector[allocProperties.rootDeviceIndex]->getOccupiedMemorySizeForBank(0)); + + memoryManager.freeGraphicsMemory(allocation); + EXPECT_EQ(0u, memoryManager.localMemoryUsageBankSelector[allocProperties.rootDeviceIndex]->getOccupiedMemorySizeForBank(0)); +} + +TEST(MemoryManagerTest, givenSetUseSystemMemoryWhenGraphicsAllocationInDevicePoolIsAllocatedThenlocalMemoryUsageIsNotUpdated) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + AllocationProperties allocProperties(mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, mockDeviceBitfield); + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool(allocProperties, nullptr); + EXPECT_NE(nullptr, allocation); + EXPECT_EQ(0u, memoryManager.localMemoryUsageBankSelector[allocProperties.rootDeviceIndex]->getOccupiedMemorySizeForBank(0)); + + memoryManager.freeGraphicsMemory(allocation); + EXPECT_EQ(0u, memoryManager.localMemoryUsageBankSelector[allocProperties.rootDeviceIndex]->getOccupiedMemorySizeForBank(0)); } diff --git a/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_device_pool_tests.inl b/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_device_pool_tests.inl deleted file mode 100644 index d8a8b0568e..0000000000 --- a/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_device_pool_tests.inl +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2018-2021 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#include "shared/source/execution_environment/execution_environment.h" -#include "shared/source/memory_manager/os_agnostic_memory_manager.h" -#include "shared/test/common/mocks/mock_device.h" -#include "shared/test/common/mocks/mock_execution_environment.h" -#include "shared/test/common/mocks/ult_device_factory.h" - -#include "opencl/source/helpers/memory_properties_helpers.h" -#include "opencl/source/mem_obj/mem_obj_helper.h" -#include "opencl/test/unit_test/mocks/mock_memory_manager.h" -#include "test.h" - -#include "gtest/gtest.h" - -using namespace NEO; - -TEST(MemoryManagerTest, givenSetUseSytemMemoryWhenGraphicsAllocationInDevicePoolIsAllocatedThenNullptrIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success; - AllocationData allocData; - allocData.size = MemoryConstants::pageSize; - allocData.flags.useSystemMemory = true; - allocData.flags.allocateMemory = true; - - auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); - EXPECT_EQ(nullptr, allocation); - EXPECT_EQ(MemoryManager::AllocationStatus::RetryInNonDevicePool, status); -} - -TEST(MemoryManagerTest, givenAllowed32BitAndFroce32BitWhenGraphicsAllocationInDevicePoolIsAllocatedThenNullptrIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - memoryManager.setForce32BitAllocations(true); - - MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success; - AllocationData allocData; - allocData.size = MemoryConstants::pageSize; - allocData.flags.allow32Bit = true; - allocData.flags.allocateMemory = true; - - auto allocation = memoryManager.allocateGraphicsMemoryInDevicePool(allocData, status); - EXPECT_EQ(nullptr, allocation); - EXPECT_EQ(MemoryManager::AllocationStatus::RetryInNonDevicePool, status); -} - -TEST(AllocationFlagsTest, givenAllocateMemoryFlagWhenGetAllocationFlagsIsCalledThenAllocateFlagIsCorrectlySet) { - HardwareInfo hwInfo(*defaultHwInfo); - UltDeviceFactory deviceFactory{1, 0}; - auto pDevice = deviceFactory.rootDevices[0]; - MemoryProperties memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, 0, 0, pDevice); - auto allocationProperties = MemoryPropertiesHelper::getAllocationProperties(0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER, false, hwInfo, {}); - EXPECT_TRUE(allocationProperties.flags.allocateMemory); - - auto allocationProperties2 = MemoryPropertiesHelper::getAllocationProperties(0, memoryProperties, false, 0, GraphicsAllocation::AllocationType::BUFFER, false, hwInfo, {}); - EXPECT_FALSE(allocationProperties2.flags.allocateMemory); -} - -TEST(UncacheableFlagsTest, givenUncachedResourceFlagWhenGetAllocationFlagsIsCalledThenUncacheableFlagIsCorrectlySet) { - cl_mem_flags_intel flagsIntel = CL_MEM_LOCALLY_UNCACHED_RESOURCE; - UltDeviceFactory deviceFactory{1, 0}; - auto pDevice = deviceFactory.rootDevices[0]; - MemoryProperties memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, flagsIntel, 0, pDevice); - auto allocationFlags = MemoryPropertiesHelper::getAllocationProperties( - 0, memoryProperties, false, 0, GraphicsAllocation::AllocationType::BUFFER, false, pDevice->getHardwareInfo(), {}); - EXPECT_TRUE(allocationFlags.flags.uncacheable); - - flagsIntel = 0; - memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, flagsIntel, 0, pDevice); - auto allocationFlags2 = MemoryPropertiesHelper::getAllocationProperties( - 0, memoryProperties, false, 0, GraphicsAllocation::AllocationType::BUFFER, false, pDevice->getHardwareInfo(), {}); - EXPECT_FALSE(allocationFlags2.flags.uncacheable); -} - -TEST(AllocationFlagsTest, givenReadOnlyResourceFlagWhenGetAllocationFlagsIsCalledThenFlushL3FlagsAreCorrectlySet) { - cl_mem_flags flags = CL_MEM_READ_ONLY; - UltDeviceFactory deviceFactory{1, 0}; - auto pDevice = deviceFactory.rootDevices[0]; - MemoryProperties memoryProperties = MemoryPropertiesHelper::createMemoryProperties(flags, 0, 0, pDevice); - - auto allocationFlags = - MemoryPropertiesHelper::getAllocationProperties( - 0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER, false, pDevice->getHardwareInfo(), {}); - EXPECT_FALSE(allocationFlags.flags.flushL3RequiredForRead); - EXPECT_FALSE(allocationFlags.flags.flushL3RequiredForWrite); - - memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, 0, 0, pDevice); - auto allocationFlags2 = MemoryPropertiesHelper::getAllocationProperties( - 0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER, false, pDevice->getHardwareInfo(), {}); - EXPECT_TRUE(allocationFlags2.flags.flushL3RequiredForRead); - EXPECT_TRUE(allocationFlags2.flags.flushL3RequiredForWrite); -} - -TEST(StorageInfoTest, whenStorageInfoIsCreatedWithDefaultConstructorThenReturnsOneHandle) { - StorageInfo storageInfo; - EXPECT_EQ(1u, storageInfo.getNumBanks()); -} diff --git a/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_preferred_pool_tests.cpp b/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_preferred_pool_tests.cpp index 02898fb1ca..027f4ec4d4 100644 --- a/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_preferred_pool_tests.cpp +++ b/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_preferred_pool_tests.cpp @@ -1,8 +1,1141 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * */ -#include "opencl/test/unit_test/memory_manager/memory_manager_allocate_in_preferred_pool_tests.inl" +#include "shared/source/debug_settings/debug_settings_manager.h" +#include "shared/source/execution_environment/execution_environment.h" +#include "shared/source/memory_manager/os_agnostic_memory_manager.h" +#include "shared/test/common/helpers/debug_manager_state_restore.h" +#include "shared/test/common/helpers/unit_test_helper.h" +#include "shared/test/common/mocks/mock_execution_environment.h" +#include "shared/test/common/mocks/mock_graphics_allocation.h" +#include "shared/test/common/mocks/ult_device_factory.h" + +#include "opencl/source/mem_obj/mem_obj_helper.h" +#include "opencl/test/unit_test/mocks/mock_allocation_properties.h" +#include "opencl/test/unit_test/mocks/mock_gmm.h" +#include "opencl/test/unit_test/mocks/mock_memory_manager.h" +#include "opencl/test/unit_test/mocks/mock_os_context.h" +#include "test.h" + +using namespace NEO; + +using MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest = testing::TestWithParam; + +using MemoryManagerGetAlloctionDataTests = ::testing::Test; + +TEST_F(MemoryManagerGetAlloctionDataTests, givenHostMemoryAllocationTypeAndAllocateMemoryFlagAndNullptrWhenAllocationDataIsQueriedThenCorrectFlagsAndSizeAreSet) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false, mockDeviceBitfield); + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.useSystemMemory); + EXPECT_EQ(10u, allocData.size); + EXPECT_EQ(nullptr, allocData.hostPtr); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenNonHostMemoryAllocatoinTypeWhenAllocationDataIsQueriedThenUseSystemMemoryFlagsIsNotSet) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_FALSE(allocData.flags.useSystemMemory); + EXPECT_EQ(10u, allocData.size); + EXPECT_EQ(nullptr, allocData.hostPtr); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenForceSystemMemoryFlagWhenAllocationDataIsQueriedThenUseSystemMemoryFlagsIsSet) { + auto firstAllocationIdx = static_cast(GraphicsAllocation::AllocationType::UNKNOWN); + auto lastAllocationIdx = static_cast(GraphicsAllocation::AllocationType::COUNT); + + for (int allocationIdx = firstAllocationIdx + 1; allocationIdx != lastAllocationIdx; allocationIdx++) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, static_cast(allocationIdx), false, mockDeviceBitfield); + properties.flags.forceSystemMemory = true; + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.useSystemMemory); + } +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenMultiRootDeviceIndexAllocationPropertiesWhenAllocationDataIsQueriedThenUseSystemMemoryFlagsIsSet) { + { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); + properties.flags.crossRootDeviceAccess = true; + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.crossRootDeviceAccess); + } + + { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::IMAGE, false, mockDeviceBitfield); + properties.flags.crossRootDeviceAccess = true; + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.crossRootDeviceAccess); + } +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenDisabledCrossRootDeviceAccsessFlagInAllocationPropertiesWhenAllocationDataIsQueriedThenUseSystemMemoryFlagsIsNotSet) { + { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); + properties.flags.crossRootDeviceAccess = false; + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_FALSE(allocData.flags.useSystemMemory); + EXPECT_FALSE(allocData.flags.crossRootDeviceAccess); + } + + { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::IMAGE, false, mockDeviceBitfield); + properties.flags.crossRootDeviceAccess = false; + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_FALSE(allocData.flags.useSystemMemory); + EXPECT_FALSE(allocData.flags.crossRootDeviceAccess); + } +} + +HWTEST_F(MemoryManagerGetAlloctionDataTests, givenCommandBufferAllocationTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::COMMAND_BUFFER, false, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenAllocateMemoryFlagTrueWhenHostPtrIsNotNullThenAllocationDataHasHostPtrNulled) { + AllocationData allocData; + char memory = 0; + AllocationProperties properties(mockRootDeviceIndex, true, sizeof(memory), GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, &memory, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_EQ(sizeof(memory), allocData.size); + EXPECT_EQ(nullptr, allocData.hostPtr); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenBufferTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.forcePin); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenBufferHostMemoryTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.forcePin); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenBufferCompressedTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, false, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.forcePin); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenWriteCombinedTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::WRITE_COMBINED, false, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.forcePin); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenDefaultAllocationFlagsWhenAllocationDataIsQueriedThenAllocateMemoryIsFalse) { + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, false, 0, GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, false, mockDeviceBitfield); + char memory; + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, &memory, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_FALSE(allocData.flags.allocateMemory); +} + +TEST_F(MemoryManagerGetAlloctionDataTests, givenDebugModeWhenCertainAllocationTypesAreSelectedThenSystemPlacementIsChoosen) { + DebugManagerStateRestore restorer; + auto allocationType = GraphicsAllocation::AllocationType::BUFFER; + auto mask = 1llu << (static_cast(allocationType) - 1); + DebugManager.flags.ForceSystemMemoryPlacement.set(mask); + + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 0, allocationType, mockDeviceBitfield); + allocData.flags.useSystemMemory = false; + + MockMemoryManager::overrideAllocationData(allocData, properties); + EXPECT_TRUE(allocData.flags.useSystemMemory); + + allocData.flags.useSystemMemory = false; + allocationType = GraphicsAllocation::AllocationType::WRITE_COMBINED; + mask |= 1llu << (static_cast(allocationType) - 1); + DebugManager.flags.ForceSystemMemoryPlacement.set(mask); + + AllocationProperties properties2(mockRootDeviceIndex, 0, allocationType, mockDeviceBitfield); + MockMemoryManager::overrideAllocationData(allocData, properties2); + EXPECT_TRUE(allocData.flags.useSystemMemory); + + allocData.flags.useSystemMemory = false; + + MockMemoryManager::overrideAllocationData(allocData, properties); + EXPECT_TRUE(allocData.flags.useSystemMemory); + + allocData.flags.useSystemMemory = false; + allocationType = GraphicsAllocation::AllocationType::IMAGE; + mask = 1llu << (static_cast(allocationType) - 1); + DebugManager.flags.ForceSystemMemoryPlacement.set(mask); + + MockMemoryManager::overrideAllocationData(allocData, properties); + EXPECT_FALSE(allocData.flags.useSystemMemory); +} + +TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, givenAllocationTypesWith32BitAnd64kbPagesAllowedWhenAllocationDataIsQueriedThenProperFlagsAreSet) { + AllocationData allocData; + auto allocType = GetParam(); + AllocationProperties properties(mockRootDeviceIndex, 0, allocType, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.mockExecutionEnvironment->initGmm(); + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_TRUE(allocData.flags.allow32Bit); + EXPECT_TRUE(allocData.flags.allow64kbPages); + EXPECT_EQ(allocType, allocData.type); +} + +TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, given64kbAllowedAllocationTypeWhenAllocatingThenPreferRenderCompressionOnlyForSpecificTypes) { + auto allocType = GetParam(); + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, allocType, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager(true, false); + mockMemoryManager.mockExecutionEnvironment->initGmm(); + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + bool bufferCompressedType = (allocType == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); + EXPECT_TRUE(allocData.flags.allow64kbPages); + auto allocation = mockMemoryManager.allocateGraphicsMemory(allocData); + + EXPECT_TRUE(mockMemoryManager.allocation64kbPageCreated); + EXPECT_EQ(mockMemoryManager.preferRenderCompressedFlagPassed, bufferCompressedType); + + mockMemoryManager.freeGraphicsMemory(allocation); +} + +using MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest = MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest; + +TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest, givenAllocationTypesWith32BitAnd64kbPagesDisallowedWhenAllocationDataIsQueriedThenFlagsAreNotSet) { + AllocationData allocData; + auto allocType = GetParam(); + AllocationProperties properties(mockRootDeviceIndex, 0, allocType, mockDeviceBitfield); + + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + + EXPECT_FALSE(allocData.flags.allow32Bit); + EXPECT_FALSE(allocData.flags.allow64kbPages); + EXPECT_EQ(allocType, allocData.type); +} + +static const GraphicsAllocation::AllocationType allocationTypesWith32BitAnd64KbPagesAllowed[] = {GraphicsAllocation::AllocationType::BUFFER, + GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, + GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, + GraphicsAllocation::AllocationType::PIPE, + GraphicsAllocation::AllocationType::SCRATCH_SURFACE, + GraphicsAllocation::AllocationType::WORK_PARTITION_SURFACE, + GraphicsAllocation::AllocationType::PRIVATE_SURFACE, + GraphicsAllocation::AllocationType::PRINTF_SURFACE, + GraphicsAllocation::AllocationType::CONSTANT_SURFACE, + GraphicsAllocation::AllocationType::GLOBAL_SURFACE, + GraphicsAllocation::AllocationType::WRITE_COMBINED}; + +INSTANTIATE_TEST_CASE_P(Allow32BitAnd64kbPagesTypes, + MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, + ::testing::ValuesIn(allocationTypesWith32BitAnd64KbPagesAllowed)); + +static const GraphicsAllocation::AllocationType allocationTypesWith32BitAnd64KbPagesNotAllowed[] = {GraphicsAllocation::AllocationType::COMMAND_BUFFER, + GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER, + GraphicsAllocation::AllocationType::PROFILING_TAG_BUFFER, + GraphicsAllocation::AllocationType::IMAGE, + GraphicsAllocation::AllocationType::INSTRUCTION_HEAP, + GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY}; + +INSTANTIATE_TEST_CASE_P(Disallow32BitAnd64kbPagesTypes, + MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest, + ::testing::ValuesIn(allocationTypesWith32BitAnd64KbPagesNotAllowed)); + +TEST(MemoryManagerTest, givenForced32BitSetWhenGraphicsMemoryFor32BitAllowedTypeIsAllocatedThen32BitAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, false, executionEnvironment); + memoryManager.setForce32BitAllocations(true); + + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + if constexpr (is64bit) { + EXPECT_TRUE(allocation->is32BitAllocation()); + EXPECT_EQ(MemoryPool::System4KBPagesWith32BitGpuAddressing, allocation->getMemoryPool()); + } else { + EXPECT_FALSE(allocation->is32BitAllocation()); + EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); + } + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenEnabledShareableWhenGraphicsAllocationIsAllocatedThenAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + executionEnvironment.initGmm(); + MockMemoryManager memoryManager(false, false, executionEnvironment); + + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + properties.flags.shareable = true; + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + EXPECT_EQ(allocData.flags.shareable, 1u); + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenEnabledShareableWhenGraphicsAllocationIsCalledAndSystemMemoryFailsThenNullAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + executionEnvironment.initGmm(); + MockMemoryManager memoryManager(false, false, executionEnvironment); + + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + properties.flags.shareable = true; + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + EXPECT_EQ(allocData.flags.shareable, 1u); + + memoryManager.failAllocateSystemMemory = true; + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_EQ(nullptr, allocation); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenForced32BitEnabledWhenGraphicsMemoryWihtoutAllow32BitFlagIsAllocatedThenNon32BitAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(executionEnvironment); + memoryManager.setForce32BitAllocations(true); + + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + allocData.flags.allow32Bit = false; + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + EXPECT_FALSE(allocation->is32BitAllocation()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenForced32BitDisabledWhenGraphicsMemoryWith32BitFlagFor32BitAllowedTypeIsAllocatedThenNon32BitAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(executionEnvironment); + memoryManager.setForce32BitAllocations(false); + + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + EXPECT_FALSE(allocation->is32BitAllocation()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThen64kbAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + executionEnvironment.initGmm(); + MockMemoryManager memoryManager(true, false, executionEnvironment); + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, mockDeviceBitfield); + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + EXPECT_EQ(0u, reinterpret_cast(allocation->getUnderlyingBuffer()) & MemoryConstants::page64kMask); + EXPECT_EQ(0u, allocation->getGpuAddress() & MemoryConstants::page64kMask); + EXPECT_EQ(0u, allocation->getUnderlyingBufferSize() & MemoryConstants::page64kMask); + EXPECT_EQ(MemoryPool::System64KBPages, allocation->getMemoryPool()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryWithoutAllow64kbPagesFlagsIsAllocatedThenNon64kbAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(true, false, executionEnvironment); + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + allocData.flags.allow64kbPages = false; + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + EXPECT_FALSE(memoryManager.allocation64kbPageCreated); + EXPECT_TRUE(memoryManager.allocationCreated); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenDisabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThenNon64kbAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, false, executionEnvironment); + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, mockDeviceBitfield); + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + EXPECT_FALSE(memoryManager.allocation64kbPageCreated); + EXPECT_TRUE(memoryManager.allocationCreated); + EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenForced32BitAndEnabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThen32BitAllocationOver64kbIsChosen) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, false, executionEnvironment); + memoryManager.setForce32BitAllocations(true); + + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, mockDeviceBitfield); + + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + if constexpr (is64bit) { + EXPECT_TRUE(allocation->is32BitAllocation()); + } else { + EXPECT_FALSE(allocation->is32BitAllocation()); + } + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryIsAllocatedWithHostPtrForBufferThenExistingMemoryIsUsedForAllocation) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(true, false, executionEnvironment); + AllocationData allocData; + AllocationProperties properties(mockRootDeviceIndex, false, 1, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false, mockDeviceBitfield); + + char memory[1]; + memoryManager.getAllocationData(allocData, properties, &memory, memoryManager.createStorageInfoFromProperties(properties)); + + auto allocation = memoryManager.allocateGraphicsMemory(allocData); + ASSERT_NE(nullptr, allocation); + EXPECT_EQ(1u, allocation->fragmentsStorage.fragmentCount); + EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenMemoryManagerWhenGraphicsMemoryAllocationInDevicePoolFailsThenFallbackAllocationIsReturned) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + memoryManager.failInDevicePool = true; + + auto allocation = memoryManager.allocateGraphicsMemoryWithProperties({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}); + ASSERT_NE(nullptr, allocation); + EXPECT_TRUE(memoryManager.allocationCreated); + EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenMemoryManagerWhenBufferTypeIsPassedThenAllocateGraphicsMemoryInPreferredPoolCanAllocateInDevicePool) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + auto allocation = memoryManager.allocateGraphicsMemoryWithProperties({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}); + EXPECT_NE(nullptr, allocation); + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenMemoryManagerWhenBufferTypeIsPassedAndAllocateInDevicePoolFailsWithErrorThenAllocateGraphicsMemoryInPreferredPoolReturnsNullptr) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + memoryManager.failInDevicePoolWithError = true; + + auto allocation = memoryManager.allocateGraphicsMemoryWithProperties({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}); + ASSERT_EQ(nullptr, allocation); + EXPECT_FALSE(memoryManager.allocationInDevicePoolCreated); + + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenSvmAllocationTypeWhenGetAllocationDataIsCalledThenAllocatingMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::SVM_ZERO_COPY, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.allocateMemory); +} + +TEST(MemoryManagerTest, givenSvmAllocationTypeWhenGetAllocationDataIsCalledThen64kbPagesAreAllowedAnd32BitAllocationIsDisallowed) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::SVM_ZERO_COPY, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.allow64kbPages); + EXPECT_FALSE(allocData.flags.allow32Bit); +} + +TEST(MemoryManagerTest, givenTagBufferTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::TAG_BUFFER, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenGlobalFenceTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::GLOBAL_FENCE, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenPreemptionTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PREEMPTION, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenPreemptionTypeWhenGetAllocationDataIsCalledThen64kbPagesAllowed) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PREEMPTION, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.allow64kbPages); +} + +TEST(MemoryManagerTest, givenPreemptionTypeWhenGetAllocationDataIsCalledThen48BitResourceIsTrue) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PREEMPTION, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.resource48Bit); +} + +TEST(MemoryManagerTest, givenSharedContextImageTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::SHARED_CONTEXT_IMAGE, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenMCSTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::MCS, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenPipeTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PIPE, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenGlobalSurfaceTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::GLOBAL_SURFACE, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenWriteCombinedTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::WRITE_COMBINED, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenDeviceQueueBufferTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::DEVICE_QUEUE_BUFFER, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenInternalHostMemoryTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenFillPatternTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::FILL_PATTERN, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +using GetAllocationDataTestHw = ::testing::Test; + +HWTEST_F(GetAllocationDataTestHw, givenLinearStreamTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::LINEAR_STREAM, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.requiresCpuAccess); +} + +HWTEST_F(GetAllocationDataTestHw, givenTimestampPacketTagBufferTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequestedAndRequireCpuAccess) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_EQ(UnitTestHelper::requiresTimestampPacketsInSystemMemory(), allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.requiresCpuAccess); +} + +TEST(MemoryManagerTest, givenProfilingTagBufferTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PROFILING_TAG_BUFFER, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); + EXPECT_FALSE(allocData.flags.requiresCpuAccess); +} + +TEST(MemoryManagerTest, givenAllocationPropertiesWithMultiOsContextCapableFlagEnabledWhenAllocateMemoryThenAllocationDataIsMultiOsContextCapable) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, false, executionEnvironment); + AllocationProperties properties{mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}; + properties.flags.multiOsContextCapable = true; + + AllocationData allocData; + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.multiOsContextCapable); +} + +TEST(MemoryManagerTest, givenAllocationPropertiesWithMultiOsContextCapableFlagDisabledWhenAllocateMemoryThenAllocationDataIsNotMultiOsContextCapable) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, false, executionEnvironment); + AllocationProperties properties{mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}; + properties.flags.multiOsContextCapable = false; + + AllocationData allocData; + memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.multiOsContextCapable); +} + +TEST(MemoryManagerTest, givenConstantSurfaceTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::CONSTANT_SURFACE, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); +} + +HWTEST_F(GetAllocationDataTestHw, givenInternalHeapTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::INTERNAL_HEAP, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.requiresCpuAccess); +} + +HWTEST_F(GetAllocationDataTestHw, givenKernelIsaTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::KERNEL_ISA, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_NE(defaultHwInfo->featureTable.ftrLocalMemory, allocData.flags.useSystemMemory); + + AllocationProperties properties2{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::KERNEL_ISA_INTERNAL, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties2, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_NE(defaultHwInfo->featureTable.ftrLocalMemory, allocData.flags.useSystemMemory); +} + +HWTEST_F(GetAllocationDataTestHw, givenLinearStreamWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::LINEAR_STREAM, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.requiresCpuAccess); +} + +HWTEST_F(GetAllocationDataTestHw, givenPrintfAllocationWhenGetAllocationDataIsCalledThenDontUseSystemMemoryAndRequireCpuAccess) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PRINTF_SURFACE, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.requiresCpuAccess); +} + +TEST(MemoryManagerTest, givenExternalHostMemoryWhenGetAllocationDataIsCalledThenItHasProperFieldsSet) { + AllocationData allocData; + auto hostPtr = reinterpret_cast(0x1234); + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, false, 1, GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, false, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, hostPtr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); + EXPECT_FALSE(allocData.flags.allocateMemory); + EXPECT_FALSE(allocData.flags.allow32Bit); + EXPECT_FALSE(allocData.flags.allow64kbPages); + EXPECT_EQ(allocData.hostPtr, hostPtr); +} + +TEST(MemoryManagerTest, GivenAllocationPropertiesWhenGettingAllocationDataThenSameRootDeviceIndexIsUsed) { + const uint32_t rootDevicesCount = 100u; + + AllocationData allocData; + MockExecutionEnvironment executionEnvironment{defaultHwInfo.get(), true, rootDevicesCount}; + MockMemoryManager mockMemoryManager{executionEnvironment}; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_EQ(allocData.rootDeviceIndex, 0u); + + AllocationProperties properties2{rootDevicesCount - 1, 1, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties2, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_EQ(allocData.rootDeviceIndex, properties2.rootDeviceIndex); +} + +TEST(MemoryManagerTest, givenMapAllocationWhenGetAllocationDataIsCalledThenItHasProperFieldsSet) { + AllocationData allocData; + auto hostPtr = reinterpret_cast(0x1234); + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, false, 1, GraphicsAllocation::AllocationType::MAP_ALLOCATION, false, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, hostPtr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); + EXPECT_FALSE(allocData.flags.allocateMemory); + EXPECT_FALSE(allocData.flags.allow32Bit); + EXPECT_FALSE(allocData.flags.allow64kbPages); + EXPECT_EQ(allocData.hostPtr, hostPtr); +} + +HWTEST_F(GetAllocationDataTestHw, givenRingBufferAllocationWhenGetAllocationDataIsCalledThenItHasProperFieldsSet) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 0x10000u, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.allocateMemory); + EXPECT_FALSE(allocData.flags.allow32Bit); + EXPECT_FALSE(allocData.flags.allow64kbPages); + EXPECT_EQ(0x10000u, allocData.size); + EXPECT_EQ(nullptr, allocData.hostPtr); + EXPECT_TRUE(allocData.flags.requiresCpuAccess); +} + +HWTEST_F(GetAllocationDataTestHw, givenSemaphoreBufferAllocationWhenGetAllocationDataIsCalledThenItHasProperFieldsSet) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 0x1000u, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_FALSE(allocData.flags.useSystemMemory); + EXPECT_TRUE(allocData.flags.allocateMemory); + EXPECT_FALSE(allocData.flags.allow32Bit); + EXPECT_FALSE(allocData.flags.allow64kbPages); + EXPECT_EQ(0x1000u, allocData.size); + EXPECT_EQ(nullptr, allocData.hostPtr); + EXPECT_TRUE(allocData.flags.requiresCpuAccess); +} + +TEST(MemoryManagerTest, givenDirectBufferPlacementSetWhenDefaultIsUsedThenExpectNoFlagsChanged) { + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(0u, allocationData.flags.requiresCpuAccess); + EXPECT_EQ(0u, allocationData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenDirectBufferPlacementSetWhenOverrideToNonSystemThenExpectNonSystemFlags) { + DebugManagerStateRestore restorer; + DebugManager.flags.DirectSubmissionBufferPlacement.set(0); + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(1u, allocationData.flags.requiresCpuAccess); + EXPECT_EQ(0u, allocationData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenDirectBufferPlacementSetWhenOverrideToSystemThenExpectNonFlags) { + DebugManagerStateRestore restorer; + DebugManager.flags.DirectSubmissionBufferPlacement.set(1); + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(0u, allocationData.flags.requiresCpuAccess); + EXPECT_EQ(1u, allocationData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenDirectSemaphorePlacementSetWhenDefaultIsUsedThenExpectNoFlagsChanged) { + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(0u, allocationData.flags.requiresCpuAccess); + EXPECT_EQ(0u, allocationData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenDirectSemaphorePlacementSetWhenOverrideToNonSystemThenExpectNonSystemFlags) { + DebugManagerStateRestore restorer; + DebugManager.flags.DirectSubmissionSemaphorePlacement.set(0); + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(1u, allocationData.flags.requiresCpuAccess); + EXPECT_EQ(0u, allocationData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenDirectSemaphorePlacementSetWhenOverrideToSystemThenExpectNonFlags) { + DebugManagerStateRestore restorer; + DebugManager.flags.DirectSubmissionSemaphorePlacement.set(1); + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(0u, allocationData.flags.requiresCpuAccess); + EXPECT_EQ(1u, allocationData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenDirectBufferAddressingWhenOverrideToNo48BitThenExpect48BitFlagFalse) { + DebugManagerStateRestore restorer; + DebugManager.flags.DirectSubmissionBufferAddressing.set(0); + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); + allocationData.flags.resource48Bit = 1; + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(0u, allocationData.flags.resource48Bit); +} + +TEST(MemoryManagerTest, givenDirectBufferAddressingWhenOverrideTo48BitThenExpect48BitFlagTrue) { + DebugManagerStateRestore restorer; + DebugManager.flags.DirectSubmissionBufferAddressing.set(1); + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); + allocationData.flags.resource48Bit = 0; + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(1u, allocationData.flags.resource48Bit); +} + +TEST(MemoryManagerTest, givenDirectBufferAddressingDefaultWhenNoOverrideThenExpect48BitFlagSame) { + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); + allocationData.flags.resource48Bit = 0; + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(0u, allocationData.flags.resource48Bit); + + allocationData.flags.resource48Bit = 1; + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(1u, allocationData.flags.resource48Bit); +} + +TEST(MemoryManagerTest, givenDirectSemaphoreAddressingWhenOverrideToNo48BitThenExpect48BitFlagFalse) { + DebugManagerStateRestore restorer; + DebugManager.flags.DirectSubmissionSemaphoreAddressing.set(0); + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); + allocationData.flags.resource48Bit = 1; + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(0u, allocationData.flags.resource48Bit); +} + +TEST(MemoryManagerTest, givenDirectSemaphoreAddressingWhenOverrideTo48BitThenExpect48BitFlagTrue) { + DebugManagerStateRestore restorer; + DebugManager.flags.DirectSubmissionSemaphoreAddressing.set(1); + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); + allocationData.flags.resource48Bit = 0; + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(1u, allocationData.flags.resource48Bit); +} + +TEST(MemoryManagerTest, givenDirectSemaphoreAddressingDefaultWhenNoOverrideThenExpect48BitFlagSame) { + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); + allocationData.flags.resource48Bit = 0; + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(0u, allocationData.flags.resource48Bit); + + allocationData.flags.resource48Bit = 1; + MockMemoryManager::overrideAllocationData(allocationData, properties); + + EXPECT_EQ(1u, allocationData.flags.resource48Bit); +} + +TEST(MemoryManagerTest, givenForceNonSystemMaskWhenAllocationTypeMatchesMaskThenExpectSystemFlagFalse) { + DebugManagerStateRestore restorer; + auto allocationType = GraphicsAllocation::AllocationType::BUFFER; + auto mask = 1llu << (static_cast(allocationType) - 1); + DebugManager.flags.ForceNonSystemMemoryPlacement.set(mask); + + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); + allocationData.flags.useSystemMemory = 1; + MockMemoryManager::overrideAllocationData(allocationData, properties); + EXPECT_EQ(0u, allocationData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenForceNonSystemMaskWhenAllocationTypeNotMatchesMaskThenExpectSystemFlagTrue) { + DebugManagerStateRestore restorer; + auto allocationType = GraphicsAllocation::AllocationType::BUFFER; + auto mask = 1llu << (static_cast(allocationType) - 1); + DebugManager.flags.ForceNonSystemMemoryPlacement.set(mask); + + AllocationData allocationData; + AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::COMMAND_BUFFER, mockDeviceBitfield); + allocationData.flags.useSystemMemory = 1; + MockMemoryManager::overrideAllocationData(allocationData, properties); + EXPECT_EQ(1u, allocationData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenDebugContextSaveAreaTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA, mockDeviceBitfield}; + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocData.flags.useSystemMemory); +} + +TEST(MemoryManagerTest, givenPropertiesWithOsContextWhenGetAllocationDataIsCalledThenOsContextIsSet) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{0, 1, GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA, mockDeviceBitfield}; + + MockOsContext osContext(0u, 1, + HwHelper::get(defaultHwInfo->platform.eRenderCoreFamily).getGpgpuEngineInstances(*defaultHwInfo)[0], + PreemptionMode::Disabled, false); + + properties.osContext = &osContext; + + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_EQ(&osContext, allocData.osContext); +} + +TEST(MemoryManagerTest, givenPropertiesWithGpuAddressWhenGetAllocationDataIsCalledThenGpuAddressIsSet) { + AllocationData allocData; + MockMemoryManager mockMemoryManager; + AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA, mockDeviceBitfield}; + + properties.gpuAddress = 0x4000; + + mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_EQ(properties.gpuAddress, allocData.gpuAddress); +} + +TEST(MemoryManagerTest, givenEnableLocalMemoryAndMemoryManagerWhenBufferTypeIsPassedThenAllocateGraphicsMemoryInPreferredPool) { + MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); + MockMemoryManager memoryManager(false, true, executionEnvironment); + + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}, + nullptr); + EXPECT_NE(nullptr, allocation); + memoryManager.freeGraphicsMemory(allocation); +} + +TEST(MemoryManagerTest, givenEnabledLocalMemoryWhenAllocatingSharedResourceCopyThenLocalMemoryAllocationIsReturnedAndGpuAddresIsInStandard64kHeap) { + UltDeviceFactory deviceFactory{1, 0}; + HardwareInfo localPlatformDevice = {}; + + localPlatformDevice = *defaultHwInfo; + localPlatformDevice.featureTable.ftrLocalMemory = true; + + platformsImpl->clear(); + auto executionEnvironment = constructPlatform()->peekExecutionEnvironment(); + executionEnvironment->prepareRootDeviceEnvironments(1u); + executionEnvironment->rootDeviceEnvironments[0]->setHwInfo(&localPlatformDevice); + executionEnvironment->rootDeviceEnvironments[0]->initGmm(); + + MockMemoryManager memoryManager(false, true, *executionEnvironment); + + cl_image_desc imgDesc = {}; + imgDesc.image_width = 512; + imgDesc.image_height = 1; + imgDesc.image_type = CL_MEM_OBJECT_IMAGE2D; + auto imgInfo = MockGmm::initImgInfo(imgDesc, 0, nullptr); + + auto memoryProperties = MemoryPropertiesHelper::createMemoryProperties(0, 0, 0, deviceFactory.rootDevices[0]); + AllocationProperties allocProperties = MemObjHelper::getAllocationPropertiesWithImageInfo(mockRootDeviceIndex, imgInfo, true, memoryProperties, localPlatformDevice, mockDeviceBitfield); + allocProperties.allocationType = GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY; + + auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool(allocProperties, nullptr); + ASSERT_NE(nullptr, allocation); + EXPECT_EQ(MemoryPool::LocalMemory, allocation->getMemoryPool()); + EXPECT_FALSE(allocation->getDefaultGmm()->useSystemMemoryPool); + EXPECT_LT(GmmHelper::canonize(memoryManager.getGfxPartition(allocation->getRootDeviceIndex())->getHeapBase(HeapIndex::HEAP_STANDARD64KB)), allocation->getGpuAddress()); + EXPECT_GT(GmmHelper::canonize(memoryManager.getGfxPartition(allocation->getRootDeviceIndex())->getHeapLimit(HeapIndex::HEAP_STANDARD64KB)), allocation->getGpuAddress()); + EXPECT_EQ(0llu, allocation->getGpuBaseAddress()); + + memoryManager.freeGraphicsMemory(allocation); +} + +using MemoryManagerGetAlloctionDataHaveToBeForcedTo48BitTest = testing::TestWithParam>; + +TEST_P(MemoryManagerGetAlloctionDataHaveToBeForcedTo48BitTest, givenAllocationTypesHaveToBeForcedTo48BitThenAllocationDataResource48BitIsSet) { + GraphicsAllocation::AllocationType allocationType; + bool propertiesFlag48Bit; + + std::tie(allocationType, propertiesFlag48Bit) = GetParam(); + + AllocationProperties properties(mockRootDeviceIndex, 0, allocationType, mockDeviceBitfield); + properties.flags.resource48Bit = propertiesFlag48Bit; + + AllocationData allocationData; + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocationData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_TRUE(allocationData.flags.resource48Bit); +} + +using MemoryManagerGetAlloctionDataHaveNotToBeForcedTo48BitTest = testing::TestWithParam>; + +TEST_P(MemoryManagerGetAlloctionDataHaveNotToBeForcedTo48BitTest, givenAllocationTypesHaveNotToBeForcedTo48BitThenAllocationDataResource48BitIsSetProperly) { + GraphicsAllocation::AllocationType allocationType; + bool propertiesFlag48Bit; + + std::tie(allocationType, propertiesFlag48Bit) = GetParam(); + + AllocationProperties properties(mockRootDeviceIndex, 0, allocationType, mockDeviceBitfield); + properties.flags.resource48Bit = propertiesFlag48Bit; + + AllocationData allocationData; + MockMemoryManager mockMemoryManager; + mockMemoryManager.getAllocationData(allocationData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); + EXPECT_EQ(allocationData.flags.resource48Bit, propertiesFlag48Bit); +} + +static const GraphicsAllocation::AllocationType allocationHaveToBeForcedTo48Bit[] = { + GraphicsAllocation::AllocationType::COMMAND_BUFFER, + GraphicsAllocation::AllocationType::DEVICE_QUEUE_BUFFER, + GraphicsAllocation::AllocationType::IMAGE, + GraphicsAllocation::AllocationType::INDIRECT_OBJECT_HEAP, + GraphicsAllocation::AllocationType::INSTRUCTION_HEAP, + GraphicsAllocation::AllocationType::INTERNAL_HEAP, + GraphicsAllocation::AllocationType::KERNEL_ISA, + GraphicsAllocation::AllocationType::LINEAR_STREAM, + GraphicsAllocation::AllocationType::MCS, + GraphicsAllocation::AllocationType::SCRATCH_SURFACE, + GraphicsAllocation::AllocationType::WORK_PARTITION_SURFACE, + GraphicsAllocation::AllocationType::SHARED_CONTEXT_IMAGE, + GraphicsAllocation::AllocationType::SHARED_IMAGE, + GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY, + GraphicsAllocation::AllocationType::SURFACE_STATE_HEAP, + GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER, +}; + +static const GraphicsAllocation::AllocationType allocationHaveNotToBeForcedTo48Bit[] = { + GraphicsAllocation::AllocationType::BUFFER, + GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, + GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, + GraphicsAllocation::AllocationType::CONSTANT_SURFACE, + GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, + GraphicsAllocation::AllocationType::FILL_PATTERN, + GraphicsAllocation::AllocationType::GLOBAL_SURFACE, + GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY, + GraphicsAllocation::AllocationType::MAP_ALLOCATION, + GraphicsAllocation::AllocationType::PIPE, + GraphicsAllocation::AllocationType::PRINTF_SURFACE, + GraphicsAllocation::AllocationType::PRIVATE_SURFACE, + GraphicsAllocation::AllocationType::PROFILING_TAG_BUFFER, + GraphicsAllocation::AllocationType::SHARED_BUFFER, + GraphicsAllocation::AllocationType::SVM_CPU, + GraphicsAllocation::AllocationType::SVM_GPU, + GraphicsAllocation::AllocationType::SVM_ZERO_COPY, + GraphicsAllocation::AllocationType::TAG_BUFFER, + GraphicsAllocation::AllocationType::GLOBAL_FENCE, + GraphicsAllocation::AllocationType::WRITE_COMBINED, + GraphicsAllocation::AllocationType::RING_BUFFER, + GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, + GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA, +}; + +INSTANTIATE_TEST_CASE_P(ForceTo48Bit, + MemoryManagerGetAlloctionDataHaveToBeForcedTo48BitTest, + ::testing::Combine( + ::testing::ValuesIn(allocationHaveToBeForcedTo48Bit), + ::testing::Bool())); + +INSTANTIATE_TEST_CASE_P(NotForceTo48Bit, + MemoryManagerGetAlloctionDataHaveNotToBeForcedTo48BitTest, + ::testing::Combine( + ::testing::ValuesIn(allocationHaveNotToBeForcedTo48Bit), + ::testing::Bool())); diff --git a/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_preferred_pool_tests.inl b/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_preferred_pool_tests.inl deleted file mode 100644 index 0df414a0ac..0000000000 --- a/opencl/test/unit_test/memory_manager/memory_manager_allocate_in_preferred_pool_tests.inl +++ /dev/null @@ -1,1097 +0,0 @@ -/* - * Copyright (C) 2018-2021 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#include "shared/source/debug_settings/debug_settings_manager.h" -#include "shared/source/execution_environment/execution_environment.h" -#include "shared/source/memory_manager/os_agnostic_memory_manager.h" -#include "shared/test/common/helpers/debug_manager_state_restore.h" -#include "shared/test/common/helpers/unit_test_helper.h" -#include "shared/test/common/mocks/mock_execution_environment.h" -#include "shared/test/common/mocks/mock_graphics_allocation.h" - -#include "opencl/test/unit_test/mocks/mock_allocation_properties.h" -#include "opencl/test/unit_test/mocks/mock_memory_manager.h" -#include "opencl/test/unit_test/mocks/mock_os_context.h" -#include "test.h" - -using namespace NEO; -class MemoryManagerGetAlloctionDataTest : public testing::TestWithParam { - public: - void SetUp() override {} - void TearDown() override {} -}; - -using MemoryManagerGetAlloctionDataTests = ::testing::Test; - -TEST_F(MemoryManagerGetAlloctionDataTests, givenHostMemoryAllocationTypeAndAllocateMemoryFlagAndNullptrWhenAllocationDataIsQueriedThenCorrectFlagsAndSizeAreSet) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false, mockDeviceBitfield); - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.useSystemMemory); - EXPECT_EQ(10u, allocData.size); - EXPECT_EQ(nullptr, allocData.hostPtr); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenNonHostMemoryAllocatoinTypeWhenAllocationDataIsQueriedThenUseSystemMemoryFlagsIsNotSet) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_FALSE(allocData.flags.useSystemMemory); - EXPECT_EQ(10u, allocData.size); - EXPECT_EQ(nullptr, allocData.hostPtr); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenForceSystemMemoryFlagWhenAllocationDataIsQueriedThenUseSystemMemoryFlagsIsSet) { - auto firstAllocationIdx = static_cast(GraphicsAllocation::AllocationType::UNKNOWN); - auto lastAllocationIdx = static_cast(GraphicsAllocation::AllocationType::COUNT); - - for (int allocationIdx = firstAllocationIdx + 1; allocationIdx != lastAllocationIdx; allocationIdx++) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, static_cast(allocationIdx), false, mockDeviceBitfield); - properties.flags.forceSystemMemory = true; - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.useSystemMemory); - } -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenMultiRootDeviceIndexAllocationPropertiesWhenAllocationDataIsQueriedThenUseSystemMemoryFlagsIsSet) { - { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); - properties.flags.crossRootDeviceAccess = true; - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.crossRootDeviceAccess); - } - - { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::IMAGE, false, mockDeviceBitfield); - properties.flags.crossRootDeviceAccess = true; - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.crossRootDeviceAccess); - } -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenDisabledCrossRootDeviceAccsessFlagInAllocationPropertiesWhenAllocationDataIsQueriedThenUseSystemMemoryFlagsIsNotSet) { - { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); - properties.flags.crossRootDeviceAccess = false; - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_FALSE(allocData.flags.useSystemMemory); - EXPECT_FALSE(allocData.flags.crossRootDeviceAccess); - } - - { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::IMAGE, false, mockDeviceBitfield); - properties.flags.crossRootDeviceAccess = false; - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_FALSE(allocData.flags.useSystemMemory); - EXPECT_FALSE(allocData.flags.crossRootDeviceAccess); - } -} - -HWTEST_F(MemoryManagerGetAlloctionDataTests, givenCommandBufferAllocationTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::COMMAND_BUFFER, false, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenAllocateMemoryFlagTrueWhenHostPtrIsNotNullThenAllocationDataHasHostPtrNulled) { - AllocationData allocData; - char memory = 0; - AllocationProperties properties(mockRootDeviceIndex, true, sizeof(memory), GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, &memory, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_EQ(sizeof(memory), allocData.size); - EXPECT_EQ(nullptr, allocData.hostPtr); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenBufferTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER, false, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.forcePin); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenBufferHostMemoryTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.forcePin); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenBufferCompressedTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, false, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.forcePin); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenWriteCombinedTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, true, 10, GraphicsAllocation::AllocationType::WRITE_COMBINED, false, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.forcePin); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenDefaultAllocationFlagsWhenAllocationDataIsQueriedThenAllocateMemoryIsFalse) { - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, false, 0, GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, false, mockDeviceBitfield); - char memory; - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, &memory, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_FALSE(allocData.flags.allocateMemory); -} - -TEST_F(MemoryManagerGetAlloctionDataTests, givenDebugModeWhenCertainAllocationTypesAreSelectedThenSystemPlacementIsChoosen) { - DebugManagerStateRestore restorer; - auto allocationType = GraphicsAllocation::AllocationType::BUFFER; - auto mask = 1llu << (static_cast(allocationType) - 1); - DebugManager.flags.ForceSystemMemoryPlacement.set(mask); - - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 0, allocationType, mockDeviceBitfield); - allocData.flags.useSystemMemory = false; - - MockMemoryManager::overrideAllocationData(allocData, properties); - EXPECT_TRUE(allocData.flags.useSystemMemory); - - allocData.flags.useSystemMemory = false; - allocationType = GraphicsAllocation::AllocationType::WRITE_COMBINED; - mask |= 1llu << (static_cast(allocationType) - 1); - DebugManager.flags.ForceSystemMemoryPlacement.set(mask); - - AllocationProperties properties2(mockRootDeviceIndex, 0, allocationType, mockDeviceBitfield); - MockMemoryManager::overrideAllocationData(allocData, properties2); - EXPECT_TRUE(allocData.flags.useSystemMemory); - - allocData.flags.useSystemMemory = false; - - MockMemoryManager::overrideAllocationData(allocData, properties); - EXPECT_TRUE(allocData.flags.useSystemMemory); - - allocData.flags.useSystemMemory = false; - allocationType = GraphicsAllocation::AllocationType::IMAGE; - mask = 1llu << (static_cast(allocationType) - 1); - DebugManager.flags.ForceSystemMemoryPlacement.set(mask); - - MockMemoryManager::overrideAllocationData(allocData, properties); - EXPECT_FALSE(allocData.flags.useSystemMemory); -} - -typedef MemoryManagerGetAlloctionDataTest MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest; - -TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, givenAllocationTypesWith32BitAnd64kbPagesAllowedWhenAllocationDataIsQueriedThenProperFlagsAreSet) { - AllocationData allocData; - auto allocType = GetParam(); - AllocationProperties properties(mockRootDeviceIndex, 0, allocType, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.mockExecutionEnvironment->initGmm(); - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_TRUE(allocData.flags.allow32Bit); - EXPECT_TRUE(allocData.flags.allow64kbPages); - EXPECT_EQ(allocType, allocData.type); -} - -TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, given64kbAllowedAllocationTypeWhenAllocatingThenPreferRenderCompressionOnlyForSpecificTypes) { - auto allocType = GetParam(); - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, allocType, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager(true, false); - mockMemoryManager.mockExecutionEnvironment->initGmm(); - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - bool bufferCompressedType = (allocType == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); - EXPECT_TRUE(allocData.flags.allow64kbPages); - auto allocation = mockMemoryManager.allocateGraphicsMemory(allocData); - - EXPECT_TRUE(mockMemoryManager.allocation64kbPageCreated); - EXPECT_EQ(mockMemoryManager.preferRenderCompressedFlagPassed, bufferCompressedType); - - mockMemoryManager.freeGraphicsMemory(allocation); -} - -typedef MemoryManagerGetAlloctionDataTest MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest; - -TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest, givenAllocationTypesWith32BitAnd64kbPagesDisallowedWhenAllocationDataIsQueriedThenFlagsAreNotSet) { - AllocationData allocData; - auto allocType = GetParam(); - AllocationProperties properties(mockRootDeviceIndex, 0, allocType, mockDeviceBitfield); - - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - - EXPECT_FALSE(allocData.flags.allow32Bit); - EXPECT_FALSE(allocData.flags.allow64kbPages); - EXPECT_EQ(allocType, allocData.type); -} - -static const GraphicsAllocation::AllocationType allocationTypesWith32BitAnd64KbPagesAllowed[] = {GraphicsAllocation::AllocationType::BUFFER, - GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, - GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, - GraphicsAllocation::AllocationType::PIPE, - GraphicsAllocation::AllocationType::SCRATCH_SURFACE, - GraphicsAllocation::AllocationType::WORK_PARTITION_SURFACE, - GraphicsAllocation::AllocationType::PRIVATE_SURFACE, - GraphicsAllocation::AllocationType::PRINTF_SURFACE, - GraphicsAllocation::AllocationType::CONSTANT_SURFACE, - GraphicsAllocation::AllocationType::GLOBAL_SURFACE, - GraphicsAllocation::AllocationType::WRITE_COMBINED}; - -INSTANTIATE_TEST_CASE_P(Allow32BitAnd64kbPagesTypes, - MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, - ::testing::ValuesIn(allocationTypesWith32BitAnd64KbPagesAllowed)); - -static const GraphicsAllocation::AllocationType allocationTypesWith32BitAnd64KbPagesNotAllowed[] = {GraphicsAllocation::AllocationType::COMMAND_BUFFER, - GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER, - GraphicsAllocation::AllocationType::PROFILING_TAG_BUFFER, - GraphicsAllocation::AllocationType::IMAGE, - GraphicsAllocation::AllocationType::INSTRUCTION_HEAP, - GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY}; - -INSTANTIATE_TEST_CASE_P(Disallow32BitAnd64kbPagesTypes, - MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest, - ::testing::ValuesIn(allocationTypesWith32BitAnd64KbPagesNotAllowed)); - -TEST(MemoryManagerTest, givenForced32BitSetWhenGraphicsMemoryFor32BitAllowedTypeIsAllocatedThen32BitAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - memoryManager.setForce32BitAllocations(true); - - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - if constexpr (is64bit) { - EXPECT_TRUE(allocation->is32BitAllocation()); - EXPECT_EQ(MemoryPool::System4KBPagesWith32BitGpuAddressing, allocation->getMemoryPool()); - } else { - EXPECT_FALSE(allocation->is32BitAllocation()); - EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); - } - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenEnabledShareableWhenGraphicsAllocationIsAllocatedThenAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - executionEnvironment.initGmm(); - MockMemoryManager memoryManager(false, false, executionEnvironment); - - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); - properties.flags.shareable = true; - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - EXPECT_EQ(allocData.flags.shareable, 1u); - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenEnabledShareableWhenGraphicsAllocationIsCalledAndSystemMemoryFailsThenNullAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - executionEnvironment.initGmm(); - MockMemoryManager memoryManager(false, false, executionEnvironment); - - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); - properties.flags.shareable = true; - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - EXPECT_EQ(allocData.flags.shareable, 1u); - - memoryManager.failAllocateSystemMemory = true; - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_EQ(nullptr, allocation); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenForced32BitEnabledWhenGraphicsMemoryWihtoutAllow32BitFlagIsAllocatedThenNon32BitAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(executionEnvironment); - memoryManager.setForce32BitAllocations(true); - - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - allocData.flags.allow32Bit = false; - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - EXPECT_FALSE(allocation->is32BitAllocation()); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenForced32BitDisabledWhenGraphicsMemoryWith32BitFlagFor32BitAllowedTypeIsAllocatedThenNon32BitAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(executionEnvironment); - memoryManager.setForce32BitAllocations(false); - - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - EXPECT_FALSE(allocation->is32BitAllocation()); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThen64kbAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - executionEnvironment.initGmm(); - MockMemoryManager memoryManager(true, false, executionEnvironment); - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, mockDeviceBitfield); - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - EXPECT_EQ(0u, reinterpret_cast(allocation->getUnderlyingBuffer()) & MemoryConstants::page64kMask); - EXPECT_EQ(0u, allocation->getGpuAddress() & MemoryConstants::page64kMask); - EXPECT_EQ(0u, allocation->getUnderlyingBufferSize() & MemoryConstants::page64kMask); - EXPECT_EQ(MemoryPool::System64KBPages, allocation->getMemoryPool()); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryWithoutAllow64kbPagesFlagsIsAllocatedThenNon64kbAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(true, false, executionEnvironment); - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - allocData.flags.allow64kbPages = false; - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - EXPECT_FALSE(memoryManager.allocation64kbPageCreated); - EXPECT_TRUE(memoryManager.allocationCreated); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenDisabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThenNon64kbAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, mockDeviceBitfield); - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - EXPECT_FALSE(memoryManager.allocation64kbPageCreated); - EXPECT_TRUE(memoryManager.allocationCreated); - EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenForced32BitAndEnabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThen32BitAllocationOver64kbIsChosen) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - memoryManager.setForce32BitAllocations(true); - - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, mockDeviceBitfield); - - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - if constexpr (is64bit) { - EXPECT_TRUE(allocation->is32BitAllocation()); - } else { - EXPECT_FALSE(allocation->is32BitAllocation()); - } - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryIsAllocatedWithHostPtrForBufferThenExistingMemoryIsUsedForAllocation) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(true, false, executionEnvironment); - AllocationData allocData; - AllocationProperties properties(mockRootDeviceIndex, false, 1, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false, mockDeviceBitfield); - - char memory[1]; - memoryManager.getAllocationData(allocData, properties, &memory, memoryManager.createStorageInfoFromProperties(properties)); - - auto allocation = memoryManager.allocateGraphicsMemory(allocData); - ASSERT_NE(nullptr, allocation); - EXPECT_EQ(1u, allocation->fragmentsStorage.fragmentCount); - EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenMemoryManagerWhenGraphicsMemoryAllocationInDevicePoolFailsThenFallbackAllocationIsReturned) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, true, executionEnvironment); - - memoryManager.failInDevicePool = true; - - auto allocation = memoryManager.allocateGraphicsMemoryWithProperties({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}); - ASSERT_NE(nullptr, allocation); - EXPECT_TRUE(memoryManager.allocationCreated); - EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenMemoryManagerWhenBufferTypeIsPassedThenAllocateGraphicsMemoryInPreferredPoolCanAllocateInDevicePool) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, true, executionEnvironment); - - auto allocation = memoryManager.allocateGraphicsMemoryWithProperties({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}); - EXPECT_NE(nullptr, allocation); - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenMemoryManagerWhenBufferTypeIsPassedAndAllocateInDevicePoolFailsWithErrorThenAllocateGraphicsMemoryInPreferredPoolReturnsNullptr) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, true, executionEnvironment); - - memoryManager.failInDevicePoolWithError = true; - - auto allocation = memoryManager.allocateGraphicsMemoryWithProperties({mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}); - ASSERT_EQ(nullptr, allocation); - EXPECT_FALSE(memoryManager.allocationInDevicePoolCreated); - - memoryManager.freeGraphicsMemory(allocation); -} - -TEST(MemoryManagerTest, givenSvmAllocationTypeWhenGetAllocationDataIsCalledThenAllocatingMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::SVM_ZERO_COPY, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.allocateMemory); -} - -TEST(MemoryManagerTest, givenSvmAllocationTypeWhenGetAllocationDataIsCalledThen64kbPagesAreAllowedAnd32BitAllocationIsDisallowed) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::SVM_ZERO_COPY, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.allow64kbPages); - EXPECT_FALSE(allocData.flags.allow32Bit); -} - -TEST(MemoryManagerTest, givenTagBufferTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::TAG_BUFFER, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenGlobalFenceTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::GLOBAL_FENCE, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenPreemptionTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PREEMPTION, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenPreemptionTypeWhenGetAllocationDataIsCalledThen64kbPagesAllowed) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PREEMPTION, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.allow64kbPages); -} - -TEST(MemoryManagerTest, givenPreemptionTypeWhenGetAllocationDataIsCalledThen48BitResourceIsTrue) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PREEMPTION, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.resource48Bit); -} - -TEST(MemoryManagerTest, givenSharedContextImageTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::SHARED_CONTEXT_IMAGE, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenMCSTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::MCS, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenPipeTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PIPE, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenGlobalSurfaceTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::GLOBAL_SURFACE, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenWriteCombinedTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::WRITE_COMBINED, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenDeviceQueueBufferTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::DEVICE_QUEUE_BUFFER, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenInternalHostMemoryTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenFillPatternTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::FILL_PATTERN, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -using GetAllocationDataTestHw = ::testing::Test; - -HWTEST_F(GetAllocationDataTestHw, givenLinearStreamTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::LINEAR_STREAM, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.requiresCpuAccess); -} - -HWTEST_F(GetAllocationDataTestHw, givenTimestampPacketTagBufferTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequestedAndRequireCpuAccess) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_EQ(UnitTestHelper::requiresTimestampPacketsInSystemMemory(), allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.requiresCpuAccess); -} - -TEST(MemoryManagerTest, givenProfilingTagBufferTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PROFILING_TAG_BUFFER, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); - EXPECT_FALSE(allocData.flags.requiresCpuAccess); -} - -TEST(MemoryManagerTest, givenAllocationPropertiesWithMultiOsContextCapableFlagEnabledWhenAllocateMemoryThenAllocationDataIsMultiOsContextCapable) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - AllocationProperties properties{mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}; - properties.flags.multiOsContextCapable = true; - - AllocationData allocData; - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.multiOsContextCapable); -} - -TEST(MemoryManagerTest, givenAllocationPropertiesWithMultiOsContextCapableFlagDisabledWhenAllocateMemoryThenAllocationDataIsNotMultiOsContextCapable) { - MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); - MockMemoryManager memoryManager(false, false, executionEnvironment); - AllocationProperties properties{mockRootDeviceIndex, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}; - properties.flags.multiOsContextCapable = false; - - AllocationData allocData; - memoryManager.getAllocationData(allocData, properties, nullptr, memoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.multiOsContextCapable); -} - -TEST(MemoryManagerTest, givenConstantSurfaceTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::CONSTANT_SURFACE, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); -} - -HWTEST_F(GetAllocationDataTestHw, givenInternalHeapTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::INTERNAL_HEAP, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.requiresCpuAccess); -} - -HWTEST_F(GetAllocationDataTestHw, givenKernelIsaTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::KERNEL_ISA, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_NE(defaultHwInfo->featureTable.ftrLocalMemory, allocData.flags.useSystemMemory); - - AllocationProperties properties2{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::KERNEL_ISA_INTERNAL, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties2, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_NE(defaultHwInfo->featureTable.ftrLocalMemory, allocData.flags.useSystemMemory); -} - -HWTEST_F(GetAllocationDataTestHw, givenLinearStreamWhenGetAllocationDataIsCalledThenSystemMemoryIsNotRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::LINEAR_STREAM, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.requiresCpuAccess); -} - -HWTEST_F(GetAllocationDataTestHw, givenPrintfAllocationWhenGetAllocationDataIsCalledThenDontUseSystemMemoryAndRequireCpuAccess) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::PRINTF_SURFACE, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.requiresCpuAccess); -} - -TEST(MemoryManagerTest, givenExternalHostMemoryWhenGetAllocationDataIsCalledThenItHasProperFieldsSet) { - AllocationData allocData; - auto hostPtr = reinterpret_cast(0x1234); - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, false, 1, GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, false, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, hostPtr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); - EXPECT_FALSE(allocData.flags.allocateMemory); - EXPECT_FALSE(allocData.flags.allow32Bit); - EXPECT_FALSE(allocData.flags.allow64kbPages); - EXPECT_EQ(allocData.hostPtr, hostPtr); -} - -TEST(MemoryManagerTest, GivenAllocationPropertiesWhenGettingAllocationDataThenSameRootDeviceIndexIsUsed) { - const uint32_t rootDevicesCount = 100u; - - AllocationData allocData; - MockExecutionEnvironment executionEnvironment{defaultHwInfo.get(), true, rootDevicesCount}; - MockMemoryManager mockMemoryManager{executionEnvironment}; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_EQ(allocData.rootDeviceIndex, 0u); - - AllocationProperties properties2{rootDevicesCount - 1, 1, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties2, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_EQ(allocData.rootDeviceIndex, properties2.rootDeviceIndex); -} - -TEST(MemoryManagerTest, givenMapAllocationWhenGetAllocationDataIsCalledThenItHasProperFieldsSet) { - AllocationData allocData; - auto hostPtr = reinterpret_cast(0x1234); - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, false, 1, GraphicsAllocation::AllocationType::MAP_ALLOCATION, false, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, hostPtr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); - EXPECT_FALSE(allocData.flags.allocateMemory); - EXPECT_FALSE(allocData.flags.allow32Bit); - EXPECT_FALSE(allocData.flags.allow64kbPages); - EXPECT_EQ(allocData.hostPtr, hostPtr); -} - -HWTEST_F(GetAllocationDataTestHw, givenRingBufferAllocationWhenGetAllocationDataIsCalledThenItHasProperFieldsSet) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 0x10000u, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.allocateMemory); - EXPECT_FALSE(allocData.flags.allow32Bit); - EXPECT_FALSE(allocData.flags.allow64kbPages); - EXPECT_EQ(0x10000u, allocData.size); - EXPECT_EQ(nullptr, allocData.hostPtr); - EXPECT_TRUE(allocData.flags.requiresCpuAccess); -} - -HWTEST_F(GetAllocationDataTestHw, givenSemaphoreBufferAllocationWhenGetAllocationDataIsCalledThenItHasProperFieldsSet) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 0x1000u, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_FALSE(allocData.flags.useSystemMemory); - EXPECT_TRUE(allocData.flags.allocateMemory); - EXPECT_FALSE(allocData.flags.allow32Bit); - EXPECT_FALSE(allocData.flags.allow64kbPages); - EXPECT_EQ(0x1000u, allocData.size); - EXPECT_EQ(nullptr, allocData.hostPtr); - EXPECT_TRUE(allocData.flags.requiresCpuAccess); -} - -TEST(MemoryManagerTest, givenDirectBufferPlacementSetWhenDefaultIsUsedThenExpectNoFlagsChanged) { - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(0u, allocationData.flags.requiresCpuAccess); - EXPECT_EQ(0u, allocationData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenDirectBufferPlacementSetWhenOverrideToNonSystemThenExpectNonSystemFlags) { - DebugManagerStateRestore restorer; - DebugManager.flags.DirectSubmissionBufferPlacement.set(0); - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(1u, allocationData.flags.requiresCpuAccess); - EXPECT_EQ(0u, allocationData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenDirectBufferPlacementSetWhenOverrideToSystemThenExpectNonFlags) { - DebugManagerStateRestore restorer; - DebugManager.flags.DirectSubmissionBufferPlacement.set(1); - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(0u, allocationData.flags.requiresCpuAccess); - EXPECT_EQ(1u, allocationData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenDirectSemaphorePlacementSetWhenDefaultIsUsedThenExpectNoFlagsChanged) { - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(0u, allocationData.flags.requiresCpuAccess); - EXPECT_EQ(0u, allocationData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenDirectSemaphorePlacementSetWhenOverrideToNonSystemThenExpectNonSystemFlags) { - DebugManagerStateRestore restorer; - DebugManager.flags.DirectSubmissionSemaphorePlacement.set(0); - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(1u, allocationData.flags.requiresCpuAccess); - EXPECT_EQ(0u, allocationData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenDirectSemaphorePlacementSetWhenOverrideToSystemThenExpectNonFlags) { - DebugManagerStateRestore restorer; - DebugManager.flags.DirectSubmissionSemaphorePlacement.set(1); - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(0u, allocationData.flags.requiresCpuAccess); - EXPECT_EQ(1u, allocationData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenDirectBufferAddressingWhenOverrideToNo48BitThenExpect48BitFlagFalse) { - DebugManagerStateRestore restorer; - DebugManager.flags.DirectSubmissionBufferAddressing.set(0); - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); - allocationData.flags.resource48Bit = 1; - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(0u, allocationData.flags.resource48Bit); -} - -TEST(MemoryManagerTest, givenDirectBufferAddressingWhenOverrideTo48BitThenExpect48BitFlagTrue) { - DebugManagerStateRestore restorer; - DebugManager.flags.DirectSubmissionBufferAddressing.set(1); - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); - allocationData.flags.resource48Bit = 0; - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(1u, allocationData.flags.resource48Bit); -} - -TEST(MemoryManagerTest, givenDirectBufferAddressingDefaultWhenNoOverrideThenExpect48BitFlagSame) { - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::RING_BUFFER, mockDeviceBitfield); - allocationData.flags.resource48Bit = 0; - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(0u, allocationData.flags.resource48Bit); - - allocationData.flags.resource48Bit = 1; - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(1u, allocationData.flags.resource48Bit); -} - -TEST(MemoryManagerTest, givenDirectSemaphoreAddressingWhenOverrideToNo48BitThenExpect48BitFlagFalse) { - DebugManagerStateRestore restorer; - DebugManager.flags.DirectSubmissionSemaphoreAddressing.set(0); - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); - allocationData.flags.resource48Bit = 1; - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(0u, allocationData.flags.resource48Bit); -} - -TEST(MemoryManagerTest, givenDirectSemaphoreAddressingWhenOverrideTo48BitThenExpect48BitFlagTrue) { - DebugManagerStateRestore restorer; - DebugManager.flags.DirectSubmissionSemaphoreAddressing.set(1); - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); - allocationData.flags.resource48Bit = 0; - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(1u, allocationData.flags.resource48Bit); -} - -TEST(MemoryManagerTest, givenDirectSemaphoreAddressingDefaultWhenNoOverrideThenExpect48BitFlagSame) { - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, mockDeviceBitfield); - allocationData.flags.resource48Bit = 0; - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(0u, allocationData.flags.resource48Bit); - - allocationData.flags.resource48Bit = 1; - MockMemoryManager::overrideAllocationData(allocationData, properties); - - EXPECT_EQ(1u, allocationData.flags.resource48Bit); -} - -TEST(MemoryManagerTest, givenForceNonSystemMaskWhenAllocationTypeMatchesMaskThenExpectSystemFlagFalse) { - DebugManagerStateRestore restorer; - auto allocationType = GraphicsAllocation::AllocationType::BUFFER; - auto mask = 1llu << (static_cast(allocationType) - 1); - DebugManager.flags.ForceNonSystemMemoryPlacement.set(mask); - - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield); - allocationData.flags.useSystemMemory = 1; - MockMemoryManager::overrideAllocationData(allocationData, properties); - EXPECT_EQ(0u, allocationData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenForceNonSystemMaskWhenAllocationTypeNotMatchesMaskThenExpectSystemFlagTrue) { - DebugManagerStateRestore restorer; - auto allocationType = GraphicsAllocation::AllocationType::BUFFER; - auto mask = 1llu << (static_cast(allocationType) - 1); - DebugManager.flags.ForceNonSystemMemoryPlacement.set(mask); - - AllocationData allocationData; - AllocationProperties properties(mockRootDeviceIndex, 0x1000, GraphicsAllocation::AllocationType::COMMAND_BUFFER, mockDeviceBitfield); - allocationData.flags.useSystemMemory = 1; - MockMemoryManager::overrideAllocationData(allocationData, properties); - EXPECT_EQ(1u, allocationData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenDebugContextSaveAreaTypeWhenGetAllocationDataIsCalledThenSystemMemoryIsRequested) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA, mockDeviceBitfield}; - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocData.flags.useSystemMemory); -} - -TEST(MemoryManagerTest, givenPropertiesWithOsContextWhenGetAllocationDataIsCalledThenOsContextIsSet) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{0, 1, GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA, mockDeviceBitfield}; - - MockOsContext osContext(0u, 1, - HwHelper::get(defaultHwInfo->platform.eRenderCoreFamily).getGpgpuEngineInstances(*defaultHwInfo)[0], - PreemptionMode::Disabled, false); - - properties.osContext = &osContext; - - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_EQ(&osContext, allocData.osContext); -} - -TEST(MemoryManagerTest, givenPropertiesWithGpuAddressWhenGetAllocationDataIsCalledThenGpuAddressIsSet) { - AllocationData allocData; - MockMemoryManager mockMemoryManager; - AllocationProperties properties{mockRootDeviceIndex, 1, GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA, mockDeviceBitfield}; - - properties.gpuAddress = 0x4000; - - mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_EQ(properties.gpuAddress, allocData.gpuAddress); -} - -using MemoryManagerGetAlloctionDataHaveToBeForcedTo48BitTest = testing::TestWithParam>; - -TEST_P(MemoryManagerGetAlloctionDataHaveToBeForcedTo48BitTest, givenAllocationTypesHaveToBeForcedTo48BitThenAllocationDataResource48BitIsSet) { - GraphicsAllocation::AllocationType allocationType; - bool propertiesFlag48Bit; - - std::tie(allocationType, propertiesFlag48Bit) = GetParam(); - - AllocationProperties properties(mockRootDeviceIndex, 0, allocationType, mockDeviceBitfield); - properties.flags.resource48Bit = propertiesFlag48Bit; - - AllocationData allocationData; - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocationData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_TRUE(allocationData.flags.resource48Bit); -} - -using MemoryManagerGetAlloctionDataHaveNotToBeForcedTo48BitTest = testing::TestWithParam>; - -TEST_P(MemoryManagerGetAlloctionDataHaveNotToBeForcedTo48BitTest, givenAllocationTypesHaveNotToBeForcedTo48BitThenAllocationDataResource48BitIsSetProperly) { - GraphicsAllocation::AllocationType allocationType; - bool propertiesFlag48Bit; - - std::tie(allocationType, propertiesFlag48Bit) = GetParam(); - - AllocationProperties properties(mockRootDeviceIndex, 0, allocationType, mockDeviceBitfield); - properties.flags.resource48Bit = propertiesFlag48Bit; - - AllocationData allocationData; - MockMemoryManager mockMemoryManager; - mockMemoryManager.getAllocationData(allocationData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); - EXPECT_EQ(allocationData.flags.resource48Bit, propertiesFlag48Bit); -} - -static const GraphicsAllocation::AllocationType allocationHaveToBeForcedTo48Bit[] = { - GraphicsAllocation::AllocationType::COMMAND_BUFFER, - GraphicsAllocation::AllocationType::DEVICE_QUEUE_BUFFER, - GraphicsAllocation::AllocationType::IMAGE, - GraphicsAllocation::AllocationType::INDIRECT_OBJECT_HEAP, - GraphicsAllocation::AllocationType::INSTRUCTION_HEAP, - GraphicsAllocation::AllocationType::INTERNAL_HEAP, - GraphicsAllocation::AllocationType::KERNEL_ISA, - GraphicsAllocation::AllocationType::LINEAR_STREAM, - GraphicsAllocation::AllocationType::MCS, - GraphicsAllocation::AllocationType::SCRATCH_SURFACE, - GraphicsAllocation::AllocationType::WORK_PARTITION_SURFACE, - GraphicsAllocation::AllocationType::SHARED_CONTEXT_IMAGE, - GraphicsAllocation::AllocationType::SHARED_IMAGE, - GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY, - GraphicsAllocation::AllocationType::SURFACE_STATE_HEAP, - GraphicsAllocation::AllocationType::TIMESTAMP_PACKET_TAG_BUFFER, -}; - -static const GraphicsAllocation::AllocationType allocationHaveNotToBeForcedTo48Bit[] = { - GraphicsAllocation::AllocationType::BUFFER, - GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, - GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, - GraphicsAllocation::AllocationType::CONSTANT_SURFACE, - GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, - GraphicsAllocation::AllocationType::FILL_PATTERN, - GraphicsAllocation::AllocationType::GLOBAL_SURFACE, - GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY, - GraphicsAllocation::AllocationType::MAP_ALLOCATION, - GraphicsAllocation::AllocationType::PIPE, - GraphicsAllocation::AllocationType::PRINTF_SURFACE, - GraphicsAllocation::AllocationType::PRIVATE_SURFACE, - GraphicsAllocation::AllocationType::PROFILING_TAG_BUFFER, - GraphicsAllocation::AllocationType::SHARED_BUFFER, - GraphicsAllocation::AllocationType::SVM_CPU, - GraphicsAllocation::AllocationType::SVM_GPU, - GraphicsAllocation::AllocationType::SVM_ZERO_COPY, - GraphicsAllocation::AllocationType::TAG_BUFFER, - GraphicsAllocation::AllocationType::GLOBAL_FENCE, - GraphicsAllocation::AllocationType::WRITE_COMBINED, - GraphicsAllocation::AllocationType::RING_BUFFER, - GraphicsAllocation::AllocationType::SEMAPHORE_BUFFER, - GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA, -}; - -INSTANTIATE_TEST_CASE_P(ForceTo48Bit, - MemoryManagerGetAlloctionDataHaveToBeForcedTo48BitTest, - ::testing::Combine( - ::testing::ValuesIn(allocationHaveToBeForcedTo48Bit), - ::testing::Bool())); - -INSTANTIATE_TEST_CASE_P(NotForceTo48Bit, - MemoryManagerGetAlloctionDataHaveNotToBeForcedTo48BitTest, - ::testing::Combine( - ::testing::ValuesIn(allocationHaveNotToBeForcedTo48Bit), - ::testing::Bool())); diff --git a/shared/source/memory_manager/CMakeLists.txt b/shared/source/memory_manager/CMakeLists.txt index 79a9ce3814..b126109e0c 100644 --- a/shared/source/memory_manager/CMakeLists.txt +++ b/shared/source/memory_manager/CMakeLists.txt @@ -44,7 +44,6 @@ set(NEO_CORE_MEMORY_MANAGER ${CMAKE_CURRENT_SOURCE_DIR}/multi_graphics_allocation.h ${CMAKE_CURRENT_SOURCE_DIR}/os_agnostic_memory_manager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_agnostic_memory_manager.h - ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/os_agnostic_memory_manager_allocate_in_device_pool.cpp ${CMAKE_CURRENT_SOURCE_DIR}/physical_address_allocator.h ${CMAKE_CURRENT_SOURCE_DIR}/residency.cpp ${CMAKE_CURRENT_SOURCE_DIR}/residency.h diff --git a/shared/source/memory_manager/os_agnostic_memory_manager.cpp b/shared/source/memory_manager/os_agnostic_memory_manager.cpp index fdd6a03d72..8b27864eda 100644 --- a/shared/source/memory_manager/os_agnostic_memory_manager.cpp +++ b/shared/source/memory_manager/os_agnostic_memory_manager.cpp @@ -8,6 +8,7 @@ #include "shared/source/memory_manager/os_agnostic_memory_manager.h" #include "shared/source/aub/aub_center.h" +#include "shared/source/aub/aub_helper.h" #include "shared/source/execution_environment/execution_environment.h" #include "shared/source/execution_environment/root_device_environment.h" #include "shared/source/gmm_helper/gmm.h" @@ -416,4 +417,94 @@ void OsAgnosticMemoryManager::freeGpuAddress(AddressRange addressRange, uint32_t gfxPartition->freeGpuAddressRange(graphicsAddress, addressRange.size); } +GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) { + MemoryAllocation *allocation = nullptr; + status = AllocationStatus::RetryInNonDevicePool; + auto numHandles = allocationData.storageInfo.getNumBanks(); + + if (!this->localMemorySupported[allocationData.rootDeviceIndex]) { + return nullptr; + } + + if (allocationData.flags.useSystemMemory || (allocationData.flags.allow32Bit && this->force32bitAllocations)) { + return nullptr; + } + bool use32Allocator = heapAssigner.use32BitHeap(allocationData.type); + if (use32Allocator) { + auto adjustedAllocationData(allocationData); + adjustedAllocationData.size = alignUp(allocationData.size, MemoryConstants::pageSize64k); + adjustedAllocationData.alignment = MemoryConstants::pageSize64k; + allocation = static_cast(allocate32BitGraphicsMemoryImpl(adjustedAllocationData, true)); + } else if (allocationData.type == GraphicsAllocation::AllocationType::SVM_GPU) { + auto storage = allocateSystemMemory(allocationData.size, allocationData.alignment); + allocation = new MemoryAllocation(allocationData.rootDeviceIndex, numHandles, allocationData.type, storage, storage, reinterpret_cast(allocationData.hostPtr), + allocationData.size, counter, MemoryPool::LocalMemory, false, allocationData.flags.flushL3, maxOsContextCount); + counter++; + } else { + std::unique_ptr gmm; + size_t sizeAligned64k = 0; + if (allocationData.type == GraphicsAllocation::AllocationType::IMAGE || + allocationData.type == GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY) { + allocationData.imgInfo->useLocalMemory = true; + gmm = std::make_unique(executionEnvironment.rootDeviceEnvironments[allocationData.rootDeviceIndex]->getGmmClientContext(), *allocationData.imgInfo, allocationData.storageInfo); + sizeAligned64k = alignUp(allocationData.imgInfo->size, MemoryConstants::pageSize64k); + } else { + sizeAligned64k = alignUp(allocationData.size, MemoryConstants::pageSize64k); + if (DebugManager.flags.RenderCompressedBuffersEnabled.get() && + allocationData.flags.preferRenderCompressed) { + gmm = std::make_unique(executionEnvironment.rootDeviceEnvironments[allocationData.rootDeviceIndex]->getGmmClientContext(), + allocationData.hostPtr, + sizeAligned64k, + MemoryConstants::pageSize64k, + allocationData.flags.uncacheable, + true, + allocationData.flags.useSystemMemory, + allocationData.storageInfo); + } + } + + auto gfxPartition = getGfxPartition(allocationData.rootDeviceIndex); + auto heapIndex = HeapIndex::HEAP_STANDARD64KB; + + if ((gfxPartition->getHeapLimit(HeapIndex::HEAP_EXTENDED) > 0) && !allocationData.flags.resource48Bit) { + heapIndex = HeapIndex::HEAP_EXTENDED; + } + + auto systemMemory = allocateSystemMemory(sizeAligned64k, MemoryConstants::pageSize64k); + if (allocationData.type == GraphicsAllocation::AllocationType::PREEMPTION) { + memset(systemMemory, 0, sizeAligned64k); + } + auto sizeOfHeapChunk = sizeAligned64k; + auto gpuAddress = GmmHelper::canonize(gfxPartition->heapAllocate(heapIndex, sizeOfHeapChunk)); + allocation = new MemoryAllocation(allocationData.rootDeviceIndex, numHandles, allocationData.type, systemMemory, systemMemory, + gpuAddress, sizeAligned64k, counter, + MemoryPool::LocalMemory, false, allocationData.flags.flushL3, maxOsContextCount); + counter++; + allocation->setDefaultGmm(gmm.release()); + allocation->sizeToFree = sizeOfHeapChunk; + } + + if (allocation) { + allocation->overrideMemoryPool(MemoryPool::LocalMemory); + allocation->storageInfo = allocationData.storageInfo; + status = AllocationStatus::Success; + } else { + status = AllocationStatus::Error; + } + + return allocation; +} + +uint64_t OsAgnosticMemoryManager::getLocalMemorySize(uint32_t rootDeviceIndex, uint32_t deviceBitfield) { + return AubHelper::getMemBankSize(executionEnvironment.rootDeviceEnvironments[rootDeviceIndex]->getHardwareInfo()); +} + +void MemoryAllocation::overrideMemoryPool(MemoryPool::Type pool) { + if (DebugManager.flags.AUBDumpForceAllToLocalMemory.get()) { + this->memoryPool = MemoryPool::LocalMemory; + return; + } + this->memoryPool = pool; +} + } // namespace NEO diff --git a/shared/source/memory_manager/os_agnostic_memory_manager_allocate_in_device_pool.cpp b/shared/source/memory_manager/os_agnostic_memory_manager_allocate_in_device_pool.cpp deleted file mode 100644 index 6e3550a3a3..0000000000 --- a/shared/source/memory_manager/os_agnostic_memory_manager_allocate_in_device_pool.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2018-2020 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#include "shared/source/memory_manager/os_agnostic_memory_manager.h" - -namespace NEO { -GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) { - status = AllocationStatus::Error; - switch (allocationData.type) { - case GraphicsAllocation::AllocationType::IMAGE: - case GraphicsAllocation::AllocationType::SHARED_RESOURCE_COPY: - break; - default: - if (!allocationData.flags.useSystemMemory && !(allocationData.flags.allow32Bit && this->force32bitAllocations)) { - GraphicsAllocation *allocation = nullptr; - if (allocationData.type == GraphicsAllocation::AllocationType::SVM_GPU) { - void *cpuAllocation = allocateSystemMemory(allocationData.size, allocationData.alignment); - if (!cpuAllocation) { - return nullptr; - } - uint64_t gpuAddress = reinterpret_cast(allocationData.hostPtr); - allocation = new MemoryAllocation(allocationData.rootDeviceIndex, allocationData.type, cpuAllocation, - cpuAllocation, gpuAddress, allocationData.size, counter++, MemoryPool::LocalMemory, false, false, maxOsContextCount); - } else { - allocation = allocateGraphicsMemory(allocationData); - } - - if (allocation) { - allocation->storageInfo = allocationData.storageInfo; - allocation->setFlushL3Required(allocationData.flags.flushL3); - status = AllocationStatus::Success; - } - return allocation; - } - } - status = AllocationStatus::RetryInNonDevicePool; - return nullptr; -} - -uint64_t OsAgnosticMemoryManager::getLocalMemorySize(uint32_t rootDeviceIndex, uint32_t deviceBitfield) { - return 0 * GB; -} - -void MemoryAllocation::overrideMemoryPool(MemoryPool::Type pool) { - this->memoryPool = pool; -} -} // namespace NEO