From c9e667d601c2e61a2a1a0844d0639b02fc6dd035 Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Mon, 17 Dec 2018 11:49:17 +0100 Subject: [PATCH] Simplify Memory Manager API [4/4] - fill AllocationData in one place - remove allocateGraphicsMemoryForSVM function - refactor SVM manager tests Change-Id: I6f4ecd70503da8031cced50ea98a54162fd8e5d3 Signed-off-by: Mateusz Jablonski --- runtime/mem_obj/image.cpp | 2 +- runtime/memory_manager/memory_manager.cpp | 11 +- runtime/memory_manager/memory_manager.h | 14 +- runtime/memory_manager/svm_memory_manager.cpp | 3 +- .../os_interface/linux/drm_memory_manager.h | 6 - .../windows/wddm_memory_manager.cpp | 8 +- .../windows/wddm_memory_manager.h | 2 +- .../aub_command_stream_fixture.h | 3 +- unit_tests/aub_tests/fixtures/aub_fixture.cpp | 2 +- .../command_stream_receiver_tests.cpp | 7 +- .../tbx_command_stream_tests.cpp | 4 +- unit_tests/event/event_tests.cpp | 2 +- unit_tests/memory_manager/CMakeLists.txt | 2 +- .../memory_manager/host_ptr_manager_tests.cpp | 15 +- .../internal_allocation_storage_tests.cpp | 20 +- .../memory_manager/memory_manager_tests.cpp | 71 ++----- .../memory_manager/svm_memory_manager.cpp | 178 ------------------ .../svm_memory_manager_tests.cpp | 130 +++++++++++++ unit_tests/mocks/CMakeLists.txt | 1 + unit_tests/mocks/mock_memory_manager.h | 2 +- unit_tests/mocks/mock_svm_manager.h | 17 ++ .../linux/drm_command_stream_tests.cpp | 22 +-- .../linux/drm_memory_manager_tests.cpp | 38 +--- .../windows/device_command_stream_tests.cpp | 8 +- .../windows/wddm_memory_manager_tests.cpp | 24 +-- .../wddm_residency_controller_tests.cpp | 11 +- 26 files changed, 249 insertions(+), 354 deletions(-) delete mode 100644 unit_tests/memory_manager/svm_memory_manager.cpp create mode 100644 unit_tests/memory_manager/svm_memory_manager_tests.cpp create mode 100644 unit_tests/mocks/mock_svm_manager.h diff --git a/runtime/mem_obj/image.cpp b/runtime/mem_obj/image.cpp index 9b50bb370c..7956482d1e 100644 --- a/runtime/mem_obj/image.cpp +++ b/runtime/mem_obj/image.cpp @@ -221,7 +221,7 @@ Image *Image::create(Context *context, } } else { gmm = new Gmm(imgInfo); - memory = memoryManager->allocateGraphicsMemory(imgInfo.size, hostPtr); + memory = memoryManager->allocateGraphicsMemory({false, imgInfo.size, GraphicsAllocation::AllocationType::UNDECIDED}, hostPtr); memory->gmm = gmm; zeroCopy = true; } diff --git a/runtime/memory_manager/memory_manager.cpp b/runtime/memory_manager/memory_manager.cpp index ccabf0e670..f0a87df6ba 100644 --- a/runtime/memory_manager/memory_manager.cpp +++ b/runtime/memory_manager/memory_manager.cpp @@ -74,15 +74,6 @@ void *MemoryManager::allocateSystemMemory(size_t size, size_t alignment) { return ptr; } -GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForSVM(size_t size, bool coherent) { - GraphicsAllocation *graphicsAllocation = nullptr; - graphicsAllocation = allocateGraphicsMemoryWithProperties({size, GraphicsAllocation::AllocationType::SVM}); - if (graphicsAllocation) { - graphicsAllocation->setCoherent(coherent); - } - return graphicsAllocation; -} - GraphicsAllocation *MemoryManager::allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) { if (deferredDeleter) { deferredDeleter->drain(true); @@ -99,7 +90,7 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImageFromHostPtr(Ima bool copyRequired = Image::isCopyRequired(imgInfo, hostPtr); if (hostPtr && !copyRequired) { - return allocateGraphicsMemory(imgInfo.size, hostPtr); + return allocateGraphicsMemory({false, imgInfo.size, GraphicsAllocation::AllocationType::UNDECIDED}, hostPtr); } return nullptr; } diff --git a/runtime/memory_manager/memory_manager.h b/runtime/memory_manager/memory_manager.h index 5f19b148cf..ac82790e78 100644 --- a/runtime/memory_manager/memory_manager.h +++ b/runtime/memory_manager/memory_manager.h @@ -93,18 +93,13 @@ class MemoryManager { return allocateGraphicsMemoryInPreferredPool(properties, 0u, nullptr); } - virtual GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) = 0; - - virtual GraphicsAllocation *allocateGraphicsMemory(size_t size, const void *ptr) { - AllocationData allocationData; - allocationData.hostPtr = ptr; - allocationData.size = size; - return MemoryManager::allocateGraphicsMemoryWithHostPtr(allocationData); + virtual GraphicsAllocation *allocateGraphicsMemory(const AllocationProperties &properties, const void *ptr) { + return allocateGraphicsMemoryInPreferredPool(properties, 0u, ptr); } GraphicsAllocation *allocateGraphicsMemoryForHostPtr(size_t size, void *ptr, bool fullRangeSvm, bool requiresL3Flush) { if (fullRangeSvm) { - return allocateGraphicsMemory(size, ptr); + return allocateGraphicsMemory({false, size, GraphicsAllocation::AllocationType::UNDECIDED}, ptr); } else { auto allocation = allocateGraphicsMemoryForNonSvmHostPtr(size, ptr); if (allocation) { @@ -122,8 +117,6 @@ class MemoryManager { GraphicsAllocation *allocateGraphicsMemoryInPreferredPool(AllocationProperties properties, DevicesBitfield devicesBitfield, const void *hostPtr); - GraphicsAllocation *allocateGraphicsMemoryForSVM(size_t size, bool coherent); - virtual GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) = 0; virtual GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle) = 0; @@ -223,6 +216,7 @@ class MemoryManager { static bool getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const DevicesBitfield devicesBitfield, const void *hostPtr); + virtual GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) = 0; GraphicsAllocation *allocateGraphicsMemory(const AllocationData &allocationData); virtual GraphicsAllocation *allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData); virtual GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) = 0; diff --git a/runtime/memory_manager/svm_memory_manager.cpp b/runtime/memory_manager/svm_memory_manager.cpp index 8e27d47aee..51366e9ef9 100644 --- a/runtime/memory_manager/svm_memory_manager.cpp +++ b/runtime/memory_manager/svm_memory_manager.cpp @@ -54,11 +54,12 @@ void *SVMAllocsManager::createSVMAlloc(size_t size, bool coherent, bool readOnly return nullptr; std::unique_lock lock(mtx); - GraphicsAllocation *GA = memoryManager->allocateGraphicsMemoryForSVM(size, coherent); + GraphicsAllocation *GA = memoryManager->allocateGraphicsMemoryWithProperties({size, GraphicsAllocation::AllocationType::SVM}); if (!GA) { return nullptr; } GA->setMemObjectsAllocationWithWritableFlags(!readOnly); + GA->setCoherent(coherent); this->SVMAllocs.insert(*GA); return GA->getUnderlyingBuffer(); diff --git a/runtime/os_interface/linux/drm_memory_manager.h b/runtime/os_interface/linux/drm_memory_manager.h index 80608ea988..a534648f19 100644 --- a/runtime/os_interface/linux/drm_memory_manager.h +++ b/runtime/os_interface/linux/drm_memory_manager.h @@ -32,12 +32,6 @@ class DrmMemoryManager : public MemoryManager { void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override; void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override; DrmAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) override; - DrmAllocation *allocateGraphicsMemory(size_t size, const void *ptr) override { - AllocationData allocationData; - allocationData.hostPtr = ptr; - allocationData.size = size; - return allocateGraphicsMemoryWithHostPtr(allocationData); - } GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, const void *hostPtr) override; DrmAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override; GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) override; diff --git a/runtime/os_interface/windows/wddm_memory_manager.cpp b/runtime/os_interface/windows/wddm_memory_manager.cpp index a27829df5b..08f4d09988 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.cpp +++ b/runtime/os_interface/windows/wddm_memory_manager.cpp @@ -132,7 +132,7 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(si return wddmAllocation.release(); } -GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, const void *ptrArg) { +GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(const AllocationProperties &properties, const void *ptrArg) { void *ptr = const_cast(ptrArg); if (ptr == nullptr) { @@ -143,14 +143,14 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, const if (mallocRestrictions.minAddress > reinterpret_cast(ptrArg)) { void *reserve = nullptr; void *ptrAligned = alignDown(ptr, MemoryConstants::allocationAlignment); - size_t sizeAligned = alignSizeWholePage(ptr, size); + size_t sizeAligned = alignSizeWholePage(ptr, properties.size); size_t offset = ptrDiff(ptr, ptrAligned); if (!wddm->reserveValidAddressRange(sizeAligned, reserve)) { return nullptr; } - auto allocation = new WddmAllocation(ptr, size, reserve, MemoryPool::System4KBPages, getOsContextCount(), false); + auto allocation = new WddmAllocation(ptr, properties.size, reserve, MemoryPool::System4KBPages, getOsContextCount(), false); allocation->allocationOffset = offset; Gmm *gmm = new Gmm(ptrAligned, sizeAligned, false); @@ -162,7 +162,7 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, const return nullptr; } - return MemoryManager::allocateGraphicsMemory(size, ptr); + return MemoryManager::allocateGraphicsMemory(properties, ptr); } GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) { diff --git a/runtime/os_interface/windows/wddm_memory_manager.h b/runtime/os_interface/windows/wddm_memory_manager.h index f9ba4f0454..be8d3e51f6 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.h +++ b/runtime/os_interface/windows/wddm_memory_manager.h @@ -34,7 +34,7 @@ class WddmMemoryManager : public MemoryManager { WddmMemoryManager &operator=(const WddmMemoryManager &) = delete; void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override; - GraphicsAllocation *allocateGraphicsMemory(size_t size, const void *ptr) override; + GraphicsAllocation *allocateGraphicsMemory(const AllocationProperties &properties, const void *ptr) override; GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) override; GraphicsAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override; GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) override; diff --git a/unit_tests/aub_tests/command_stream/aub_command_stream_fixture.h b/unit_tests/aub_tests/command_stream/aub_command_stream_fixture.h index 725274d277..095d5577d4 100644 --- a/unit_tests/aub_tests/command_stream/aub_command_stream_fixture.h +++ b/unit_tests/aub_tests/command_stream/aub_command_stream_fixture.h @@ -14,6 +14,7 @@ #include "runtime/memory_manager/internal_allocation_storage.h" #include "runtime/memory_manager/memory_banks.h" #include "unit_tests/command_stream/command_stream_fixture.h" +#include "unit_tests/mocks/mock_allocation_properties.h" #include "unit_tests/tests_configuration.h" #include @@ -64,7 +65,7 @@ class AUBCommandStreamFixture : public CommandStreamFixture { } GraphicsAllocation *createResidentAllocationAndStoreItInCsr(const void *address, size_t size) { - GraphicsAllocation *graphicsAllocation = pCommandStreamReceiver->getMemoryManager()->allocateGraphicsMemory(size, address); + GraphicsAllocation *graphicsAllocation = pCommandStreamReceiver->getMemoryManager()->allocateGraphicsMemory(MockAllocationProperties{false, size}, address); pCommandStreamReceiver->makeResidentHostPtrAllocation(graphicsAllocation); pCommandStreamReceiver->getInternalAllocationStorage()->storeAllocation(std::unique_ptr(graphicsAllocation), TEMPORARY_ALLOCATION); return graphicsAllocation; diff --git a/unit_tests/aub_tests/fixtures/aub_fixture.cpp b/unit_tests/aub_tests/fixtures/aub_fixture.cpp index 4a788f639f..aeb92ca2c1 100644 --- a/unit_tests/aub_tests/fixtures/aub_fixture.cpp +++ b/unit_tests/aub_tests/fixtures/aub_fixture.cpp @@ -13,7 +13,7 @@ namespace OCLRT { GraphicsAllocation *AUBFixture::createHostPtrAllocationFromSvmPtr(void *svmPtr, size_t size) { - GraphicsAllocation *allocation = csr->getMemoryManager()->allocateGraphicsMemory(size, svmPtr); + GraphicsAllocation *allocation = csr->getMemoryManager()->allocateGraphicsMemory(MockAllocationProperties{false, size}, svmPtr); csr->makeResidentHostPtrAllocation(allocation); csr->getInternalAllocationStorage()->storeAllocation(std::unique_ptr(allocation), TEMPORARY_ALLOCATION); allocation->setAllocationType(GraphicsAllocation::AllocationType::BUFFER); diff --git a/unit_tests/command_stream/command_stream_receiver_tests.cpp b/unit_tests/command_stream/command_stream_receiver_tests.cpp index 8246385855..b781562405 100644 --- a/unit_tests/command_stream/command_stream_receiver_tests.cpp +++ b/unit_tests/command_stream/command_stream_receiver_tests.cpp @@ -143,7 +143,7 @@ HWTEST_F(CommandStreamReceiverTest, givenPtrAndSizeThatMeetL3CriteriaWhenMakeRes auto size = 0x2000u; auto memoryManager = commandStreamReceiver->getMemoryManager(); - GraphicsAllocation *graphicsAllocation = memoryManager->allocateGraphicsMemory(size, hostPtr); + GraphicsAllocation *graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, hostPtr); ASSERT_NE(nullptr, graphicsAllocation); commandStreamReceiver->makeResidentHostPtrAllocation(graphicsAllocation); @@ -158,7 +158,7 @@ HWTEST_F(CommandStreamReceiverTest, givenPtrAndSizeThatDoNotMeetL3CriteriaWhenMa auto size = 0x2001u; auto memoryManager = commandStreamReceiver->getMemoryManager(); - GraphicsAllocation *graphicsAllocation = memoryManager->allocateGraphicsMemory(size, hostPtr); + GraphicsAllocation *graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, hostPtr); ASSERT_NE(nullptr, graphicsAllocation); commandStreamReceiver->makeResidentHostPtrAllocation(graphicsAllocation); @@ -176,8 +176,7 @@ TEST_F(CommandStreamReceiverTest, memoryManagerHasAccessToCSR) { HWTEST_F(CommandStreamReceiverTest, whenStoreAllocationThenStoredAllocationHasTaskCountFromCsr) { auto &csr = pDevice->getUltCommandStreamReceiver(); auto *memoryManager = csr.getMemoryManager(); - void *host_ptr = (void *)0x1234; - auto allocation = memoryManager->allocateGraphicsMemory(1, host_ptr); + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); EXPECT_FALSE(allocation->isUsed()); diff --git a/unit_tests/command_stream/tbx_command_stream_tests.cpp b/unit_tests/command_stream/tbx_command_stream_tests.cpp index 67230a063b..03bcd4d3d3 100644 --- a/unit_tests/command_stream/tbx_command_stream_tests.cpp +++ b/unit_tests/command_stream/tbx_command_stream_tests.cpp @@ -74,7 +74,7 @@ TEST_F(TbxCommandStreamTests, DISABLED_makeResident) { uint8_t buffer[0x10000]; size_t size = sizeof(buffer); - GraphicsAllocation *graphicsAllocation = mmTbx->allocateGraphicsMemory(size, buffer); + GraphicsAllocation *graphicsAllocation = mmTbx->allocateGraphicsMemory(MockAllocationProperties{false, size}, buffer); pCommandStreamReceiver->makeResident(*graphicsAllocation); pCommandStreamReceiver->makeNonResident(*graphicsAllocation); mmTbx->freeGraphicsMemory(graphicsAllocation); @@ -310,7 +310,7 @@ HWTEST_F(TbxCommandSteamSimpleTest, givenTbxCsrWhenWaitBeforeMakeNonResidentWhen uint32_t tag = 0; MockTbxCsr tbxCsr(*platformDevices[0], *pDevice->executionEnvironment); - tbxCsr.setTagAllocation(pDevice->getMemoryManager()->allocateGraphicsMemory(sizeof(tag), &tag)); + tbxCsr.setTagAllocation(pDevice->getMemoryManager()->allocateGraphicsMemory(MockAllocationProperties{false, sizeof(tag)}, &tag)); EXPECT_FALSE(tbxCsr.makeCoherentCalled); diff --git a/unit_tests/event/event_tests.cpp b/unit_tests/event/event_tests.cpp index 695512533e..860a50c6c3 100644 --- a/unit_tests/event/event_tests.cpp +++ b/unit_tests/event/event_tests.cpp @@ -410,7 +410,7 @@ struct UpdateEventTest : public ::testing::Test { TEST_F(UpdateEventTest, givenEventContainingCommandQueueWhenItsStatusIsUpdatedToCompletedThenTemporaryAllocationsAreDeleted) { void *ptr = (void *)0x1000; size_t size = 4096; - auto temporary = memoryManager->allocateGraphicsMemory(size, ptr); + auto temporary = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); temporary->updateTaskCount(3, commandQueue->getCommandStreamReceiver().getOsContext().getContextId()); commandQueue->getCommandStreamReceiver().getInternalAllocationStorage()->storeAllocation(std::unique_ptr(temporary), TEMPORARY_ALLOCATION); Event event(commandQueue.get(), CL_COMMAND_NDRANGE_KERNEL, 3, 3); diff --git a/unit_tests/memory_manager/CMakeLists.txt b/unit_tests/memory_manager/CMakeLists.txt index 4a51731b7f..77163a465c 100644 --- a/unit_tests/memory_manager/CMakeLists.txt +++ b/unit_tests/memory_manager/CMakeLists.txt @@ -21,7 +21,7 @@ set(IGDRCL_SRCS_tests_memory_manager ${CMAKE_CURRENT_SOURCE_DIR}/page_table_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/physical_address_allocator_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/surface_tests.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/svm_memory_manager.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/svm_memory_manager_tests.cpp ) target_sources(igdrcl_tests PRIVATE ${IGDRCL_SRCS_tests_memory_manager}) add_subdirectories() diff --git a/unit_tests/memory_manager/host_ptr_manager_tests.cpp b/unit_tests/memory_manager/host_ptr_manager_tests.cpp index f6bcb01f6c..42c5a0cd11 100644 --- a/unit_tests/memory_manager/host_ptr_manager_tests.cpp +++ b/unit_tests/memory_manager/host_ptr_manager_tests.cpp @@ -9,6 +9,7 @@ #include "runtime/helpers/ptr_math.h" #include "runtime/memory_manager/memory_constants.h" #include "unit_tests/fixtures/memory_manager_fixture.h" +#include "unit_tests/mocks/mock_allocation_properties.h" #include "unit_tests/mocks/mock_csr.h" #include "unit_tests/mocks/mock_host_ptr_manager.h" #include "unit_tests/mocks/mock_internal_allocation_storage.h" @@ -785,10 +786,10 @@ TEST_F(HostPtrAllocationTest, givenTwoAllocationsThatSharesOneFragmentWhenOneIsD void *cpuPtr2 = ptrOffset(cpuPtr1, MemoryConstants::pageSize); auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); - auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(2 * MemoryConstants::pageSize - 1, cpuPtr1); + auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 2 * MemoryConstants::pageSize - 1}, cpuPtr1); EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); - auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr2); + auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, cpuPtr2); EXPECT_EQ(3u, hostPtrManager->getFragmentCount()); memoryManager->freeGraphicsMemory(graphicsAllocation1); EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); @@ -817,7 +818,7 @@ TEST_F(HostPtrAllocationTest, whenOverlappedFragmentIsBiggerThenStoredAndStoredF void *cpuPtr1 = (void *)0x100004; auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); - auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); + auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, cpuPtr1); EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); @@ -865,7 +866,7 @@ TEST_F(HostPtrAllocationTest, whenOverlappedFragmentIsBiggerThenStoredAndStoredF void *cpuPtr1 = (void *)0x100004; auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); - auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); + auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, cpuPtr1); EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); @@ -912,10 +913,10 @@ TEST_F(HostPtrAllocationTest, checkAllocationsForOverlappingWithoutBiggerOverlap void *cpuPtr2 = (void *)0x101008; auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); - auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); + auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, cpuPtr1); EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); - auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize * 3, cpuPtr2); + auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize * 3}, cpuPtr2); EXPECT_EQ(4u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); @@ -967,7 +968,7 @@ TEST_F(HostPtrAllocationTest, checkAllocationsForOverlappingWithBiggerOverlapUnt void *cpuPtr1 = (void *)0x100004; - auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); + auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, cpuPtr1); auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); diff --git a/unit_tests/memory_manager/internal_allocation_storage_tests.cpp b/unit_tests/memory_manager/internal_allocation_storage_tests.cpp index 77912b7ef9..71d25b54fc 100644 --- a/unit_tests/memory_manager/internal_allocation_storage_tests.cpp +++ b/unit_tests/memory_manager/internal_allocation_storage_tests.cpp @@ -27,9 +27,8 @@ struct InternalAllocationStorageTest : public MemoryAllocatorFixture, TEST_F(InternalAllocationStorageTest, givenDebugFlagThatDisablesAllocationReuseWhenStoreReusableAllocationIsCalledThenAllocationIsReleased) { DebugManagerStateRestore stateRestorer; DebugManager.flags.DisableResourceRecycling.set(true); - void *host_ptr = (void *)0x1234; - auto allocation = memoryManager->allocateGraphicsMemory(1, host_ptr); + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); storage->storeAllocation(std::unique_ptr(allocation), REUSABLE_ALLOCATION); EXPECT_NE(allocation, csr->getAllocationsForReuse().peekHead()); @@ -37,11 +36,10 @@ TEST_F(InternalAllocationStorageTest, givenDebugFlagThatDisablesAllocationReuseW } TEST_F(InternalAllocationStorageTest, whenCleanAllocationListThenRemoveOnlyCompletedAllocations) { - void *host_ptr = (void *)0x1234; - auto allocation = memoryManager->allocateGraphicsMemory(1, host_ptr); - auto allocation2 = memoryManager->allocateGraphicsMemory(1, host_ptr); - auto allocation3 = memoryManager->allocateGraphicsMemory(1, host_ptr); + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); + auto allocation2 = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); + auto allocation3 = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); allocation->updateTaskCount(10, csr->getOsContext().getContextId()); allocation2->updateTaskCount(5, csr->getOsContext().getContextId()); @@ -76,8 +74,7 @@ TEST_F(InternalAllocationStorageTest, whenCleanAllocationListThenRemoveOnlyCompl } TEST_F(InternalAllocationStorageTest, whenAllocationIsStoredAsReusableButIsStillUsedThenCannotBeObtained) { - void *host_ptr = (void *)0x1234; - auto allocation = memoryManager->allocateGraphicsMemory(1, host_ptr); + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); storage->storeAllocationWithTaskCount(std::unique_ptr(allocation), REUSABLE_ALLOCATION, 2u); @@ -95,8 +92,7 @@ TEST_F(InternalAllocationStorageTest, whenObtainAllocationFromEmptyReuseListThen } TEST_F(InternalAllocationStorageTest, whenCompletedAllocationIsStoredAsReusableAndThenCanBeObtained) { - void *host_ptr = (void *)0x1234; - auto allocation = memoryManager->allocateGraphicsMemory(1, host_ptr); + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); EXPECT_NE(nullptr, allocation); storage->storeAllocationWithTaskCount(std::unique_ptr(allocation), REUSABLE_ALLOCATION, 2u); @@ -113,8 +109,8 @@ TEST_F(InternalAllocationStorageTest, whenCompletedAllocationIsStoredAsReusableA } TEST_F(InternalAllocationStorageTest, whenNotUsedAllocationIsStoredAsReusableAndThenCanBeObtained) { - void *host_ptr = (void *)0x1234; - auto allocation = memoryManager->allocateGraphicsMemory(1, host_ptr); + + auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{MemoryConstants::pageSize}); EXPECT_NE(nullptr, allocation); EXPECT_FALSE(allocation->isUsed()); EXPECT_EQ(0u, csr->peekTaskCount()); diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index 15666c3b1a..3b9f113460 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -225,7 +225,7 @@ TEST_F(MemoryAllocatorTest, AlignedHostPtrWithAlignedSizeWhenAskedForGraphicsAll auto ptr = (void *)0x1000; MockMemoryManager mockMemoryManager(*executionEnvironment); auto hostPtrManager = static_cast(mockMemoryManager.getHostPtrManager()); - auto graphicsAllocation = mockMemoryManager.allocateGraphicsMemory(4096, ptr); + auto graphicsAllocation = mockMemoryManager.allocateGraphicsMemory(MockAllocationProperties{false, 4096}, ptr); EXPECT_NE(nullptr, graphicsAllocation); EXPECT_EQ(1u, hostPtrManager->getFragmentCount()); @@ -242,7 +242,7 @@ TEST_F(MemoryAllocatorTest, GivenAlignedHostPtrAndCacheAlignedSizeWhenAskedForL3 auto ptr = (void *)0x1000; auto alignedSize = MemoryConstants::cacheLineSize; - auto graphicsAllocation = memoryManager->allocateGraphicsMemory(alignedSize, ptr); + auto graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, alignedSize}, ptr); EXPECT_TRUE(graphicsAllocation->isL3Capable()); @@ -253,7 +253,7 @@ TEST_F(MemoryAllocatorTest, GivenAlignedHostPtrAndNotCacheAlignedSizeWhenAskedFo auto ptr = (void *)0x1000; auto alignedSize = MemoryConstants::cacheLineSize - 1; - auto graphicsAllocation = memoryManager->allocateGraphicsMemory(alignedSize, ptr); + auto graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, alignedSize}, ptr); EXPECT_FALSE(graphicsAllocation->isL3Capable()); @@ -264,7 +264,7 @@ TEST_F(MemoryAllocatorTest, GivenMisAlignedHostPtrAndNotCacheAlignedSizeWhenAske auto ptr = (void *)0x1001; auto alignedSize = MemoryConstants::cacheLineSize - 1; - auto graphicsAllocation = memoryManager->allocateGraphicsMemory(alignedSize, ptr); + auto graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, alignedSize}, ptr); EXPECT_FALSE(graphicsAllocation->isL3Capable()); @@ -275,7 +275,7 @@ TEST_F(MemoryAllocatorTest, GivenHostPtrAlignedToCacheLineWhenAskedForL3Allowanc auto ptr = (void *)0x1040; auto alignedSize = MemoryConstants::cacheLineSize; - auto graphicsAllocation = memoryManager->allocateGraphicsMemory(alignedSize, ptr); + auto graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, alignedSize}, ptr); EXPECT_TRUE(graphicsAllocation->isL3Capable()); @@ -283,7 +283,6 @@ TEST_F(MemoryAllocatorTest, GivenHostPtrAlignedToCacheLineWhenAskedForL3Allowanc } TEST_F(MemoryAllocatorTest, NullOsHandleStorageAskedForPopulationReturnsFilledPointer) { - OsHandleStorage storage; storage.fragmentStorageData[0].cpuPtr = (void *)0x1000; memoryManager->populateOsHandles(storage); @@ -304,7 +303,7 @@ TEST_F(MemoryAllocatorTest, GivenEmptyMemoryManagerAndMisalingedHostPtrWithHugeS ASSERT_EQ(3u, reqs.requiredFragmentsCount); - auto graphicsAllocation = mockMemoryManager.allocateGraphicsMemory(size, cpuPtr); + auto graphicsAllocation = mockMemoryManager.allocateGraphicsMemory(MockAllocationProperties{false, size}, cpuPtr); for (int i = 0; i < maxFragmentsCount; i++) { EXPECT_NE(nullptr, graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage); EXPECT_EQ(reqs.AllocationFragments[i].allocationPtr, graphicsAllocation->fragmentsStorage.fragmentStorageData[i].cpuPtr); @@ -317,7 +316,6 @@ TEST_F(MemoryAllocatorTest, GivenEmptyMemoryManagerAndMisalingedHostPtrWithHugeS } TEST_F(MemoryAllocatorTest, GivenPointerAndSizeWhenAskedToCreateGrahicsAllocationThenGraphicsAllocationIsCreated) { - OsHandleStorage handleStorage; auto ptr = (void *)0x1000; auto ptr2 = (void *)0x1001; @@ -696,7 +694,7 @@ TEST(OsAgnosticMemoryManager, givenMemoryManagerWhenAllocateGraphicsMemoryWithPt void *ptr = reinterpret_cast(0x1001); auto size = MemoryConstants::pageSize; - auto allocation = memoryManager.allocateGraphicsMemory(size, ptr); + auto allocation = memoryManager.allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); ASSERT_NE(nullptr, allocation); EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); @@ -731,32 +729,20 @@ TEST(OsAgnosticMemoryManager, givenMemoryManagerWhenAllocate32BitGraphicsMemoryW memoryManager.freeGraphicsMemory(allocation); } -TEST(OsAgnosticMemoryManager, givenMemoryManagerWith64KBPagesEnabledWhenAllocateGraphicsMemoryForSVMIsCalledThenMemoryPoolIsSystem64KBPages) { +TEST(OsAgnosticMemoryManager, givenMemoryManagerWith64KBPagesEnabledWhenAllocateGraphicsMemoryThenMemoryPoolIsSystem64KBPages) { ExecutionEnvironment executionEnvironment; OsAgnosticMemoryManager memoryManager(true, false, executionEnvironment); - auto size = 4096u; - - auto svmAllocation = memoryManager.allocateGraphicsMemoryForSVM(size, false); + auto svmAllocation = memoryManager.allocateGraphicsMemoryWithProperties({MemoryConstants::pageSize, GraphicsAllocation::AllocationType::SVM}); EXPECT_NE(nullptr, svmAllocation); EXPECT_EQ(MemoryPool::System64KBPages, svmAllocation->getMemoryPool()); memoryManager.freeGraphicsMemory(svmAllocation); } -TEST(OsAgnosticMemoryManager, givenMemoryManagerWith64KBPagesDisabledWhenAllocateGraphicsMemoryForSVMIsCalledThen4KBGraphicsAllocationIsReturned) { +TEST(OsAgnosticMemoryManager, givenMemoryManagerWith64KBPagesDisabledWhenAllocateGraphicsMemoryThen4KBGraphicsAllocationIsReturned) { ExecutionEnvironment executionEnvironment; OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment); - auto size = 4096u; - auto isCoherent = true; - - auto svmAllocation = memoryManager.allocateGraphicsMemoryForSVM(size, isCoherent); - EXPECT_NE(nullptr, svmAllocation); - EXPECT_TRUE(svmAllocation->isCoherent()); + auto svmAllocation = memoryManager.allocateGraphicsMemoryWithProperties({MemoryConstants::pageSize, GraphicsAllocation::AllocationType::SVM}); EXPECT_EQ(MemoryPool::System4KBPages, svmAllocation->getMemoryPool()); - - EXPECT_EQ(size, svmAllocation->getUnderlyingBufferSize()); - - uintptr_t address = reinterpret_cast(svmAllocation->getUnderlyingBuffer()); - EXPECT_EQ(0u, (address & MemoryConstants::pageMask)); memoryManager.freeGraphicsMemory(svmAllocation); } @@ -780,24 +766,6 @@ TEST(OsAgnosticMemoryManager, givenDeviceWith64kbPagesDisbledWhenCreatingMemoryM EXPECT_FALSE(device->getMemoryManager()->peek64kbPagesEnabled()); } -TEST(OsAgnosticMemoryManager, givenMemoryManagerWith64KBPagesEnabledWhenAllocateGraphicsMemoryForSVMIsCalledThen64KBGraphicsAllocationIsReturned) { - ExecutionEnvironment executionEnvironment; - OsAgnosticMemoryManager memoryManager(true, false, executionEnvironment); - auto size = 4096u; - auto isCoherent = true; - - auto svmAllocation = memoryManager.allocateGraphicsMemoryForSVM(size, isCoherent); - EXPECT_NE(nullptr, svmAllocation); - EXPECT_TRUE(svmAllocation->isCoherent()); - EXPECT_EQ(MemoryPool::System64KBPages, svmAllocation->getMemoryPool()); - - EXPECT_EQ(MemoryConstants::pageSize64k, svmAllocation->getUnderlyingBufferSize()); - - uintptr_t address = reinterpret_cast(svmAllocation->getUnderlyingBuffer()); - EXPECT_EQ(0u, (address & MemoryConstants::page64kMask)); - memoryManager.freeGraphicsMemory(svmAllocation); -} - TEST(OsAgnosticMemoryManager, givenDefaultMemoryManagerWhenCreateGraphicsAllocationFromSharedObjectIsCalledThenGraphicsAllocationIsReturned) { ExecutionEnvironment executionEnvironment; OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment); @@ -965,7 +933,7 @@ TEST_F(MemoryManagerWithAsyncDeleterTest, givenMemoryManagerWhenAllocateGraphics char ptr[128]; EXPECT_EQ(0, deleter->drainCalled); deleter->expectDrainBlockingValue(true); - auto allocation = memoryManager.MemoryManager::allocateGraphicsMemory(sizeof(char), (void *)&ptr); + auto allocation = memoryManager.MemoryManager::allocateGraphicsMemory(MockAllocationProperties{false, sizeof(char)}, (void *)&ptr); EXPECT_TRUE(deleter->isQueueEmpty()); memoryManager.freeGraphicsMemoryImpl(allocation); } @@ -974,7 +942,7 @@ TEST_F(MemoryManagerWithAsyncDeleterTest, givenMemoryManagerWhenAllocateGraphics memoryManager.setDeferredDeleter(nullptr); EXPECT_EQ(nullptr, memoryManager.getDeferredDeleter()); char ptr[128]; - auto allocation = memoryManager.MemoryManager::allocateGraphicsMemory(sizeof(char), (void *)&ptr); + auto allocation = memoryManager.MemoryManager::allocateGraphicsMemory(MockAllocationProperties{false, sizeof(char)}, (void *)&ptr); memoryManager.freeGraphicsMemoryImpl(allocation); } @@ -1151,23 +1119,22 @@ TEST_F(MemoryAllocatorTest, GivenSizeWhenGmmIsCreatedThenSuccess) { typedef Test MemoryManagerWithCsrTest; TEST_F(MemoryManagerWithCsrTest, GivenAllocationsInHostPtrManagerWhenBiggerOverllapingAllcoationIsCreatedAndNothingToCleanThenAbortExecution) { - void *cpuPtr1 = (void *)0x100004; void *cpuPtr2 = (void *)0x101008; void *cpuPtr3 = (void *)0x100000; auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); - auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); + auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, cpuPtr1); EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); - auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize * 3, cpuPtr2); + auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize * 3}, cpuPtr2); EXPECT_EQ(4u, hostPtrManager->getFragmentCount()); GraphicsAllocation *graphicsAllocation3 = nullptr; bool catchMe = false; try { - graphicsAllocation3 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize * 10, cpuPtr3); + graphicsAllocation3 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize * 10}, cpuPtr3); } catch (...) { catchMe = true; } @@ -1194,10 +1161,10 @@ TEST_F(MemoryManagerWithCsrTest, GivenAllocationsInHostPtrManagerReadyForCleanin void *cpuPtr3 = (void *)0x100000; auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); - auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); + auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, cpuPtr1); EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); - auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize * 3, cpuPtr2); + auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize * 3}, cpuPtr2); EXPECT_EQ(4u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); @@ -1223,7 +1190,7 @@ TEST_F(MemoryManagerWithCsrTest, GivenAllocationsInHostPtrManagerReadyForCleanin taskCount = taskCountReady; csr->latestSentTaskCount = taskCountReady; - auto graphicsAllocation3 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize * 10, cpuPtr3); + auto graphicsAllocation3 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize * 10}, cpuPtr3); EXPECT_NE(nullptr, graphicsAllocation3); diff --git a/unit_tests/memory_manager/svm_memory_manager.cpp b/unit_tests/memory_manager/svm_memory_manager.cpp deleted file mode 100644 index 823368842c..0000000000 --- a/unit_tests/memory_manager/svm_memory_manager.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (C) 2017-2018 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#include "runtime/memory_manager/svm_memory_manager.h" -#include "runtime/utilities/tag_allocator.h" -#include "test.h" -#include "unit_tests/fixtures/memory_allocator_fixture.h" -#include "unit_tests/helpers/memory_management.h" -#include "unit_tests/mocks/mock_context.h" -#include "unit_tests/utilities/containers_tests_helpers.h" -#include "gtest/gtest.h" - -#include - -using namespace OCLRT; - -typedef Test SVMMemoryAllocatorTest; - -TEST_F(SVMMemoryAllocatorTest, allocateSystem) { - auto ptr = memoryManager->allocateSystemMemory(sizeof(char), 0); - EXPECT_NE(nullptr, ptr); - memoryManager->freeSystemMemory(ptr); -} - -TEST_F(SVMMemoryAllocatorTest, SVMAllocCreateNullFreeNull) { - ExecutionEnvironment executionEnvironment; - OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment); - { - SVMAllocsManager svmM(&memoryManager); - char *Ptr1 = (char *)svmM.createSVMAlloc(0, false, false); - EXPECT_EQ(Ptr1, nullptr); - svmM.freeSVMAlloc(nullptr); - } -} - -TEST_F(SVMMemoryAllocatorTest, SVMAllocCreateFree) { - ExecutionEnvironment executionEnvironment; - OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment); - { - SVMAllocsManager svmM(&memoryManager); - char *Ptr1 = (char *)svmM.createSVMAlloc(4096, false, false); - EXPECT_NE(Ptr1, nullptr); - - svmM.freeSVMAlloc(Ptr1); - GraphicsAllocation *GA2 = svmM.getSVMAlloc(Ptr1); - EXPECT_EQ(GA2, nullptr); - } -} - -TEST_F(SVMMemoryAllocatorTest, SVMAllocGetNoSVMAdrees) { - char array[100]; - ExecutionEnvironment executionEnvironment; - OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment); - { - SVMAllocsManager svmM(&memoryManager); - - char *Ptr1 = (char *)100; - GraphicsAllocation *GA1 = svmM.getSVMAlloc(Ptr1); - EXPECT_EQ(GA1, nullptr); - - GraphicsAllocation *GA2 = svmM.getSVMAlloc(array); - EXPECT_EQ(GA2, nullptr); - } -} - -TEST_F(SVMMemoryAllocatorTest, SVMAllocGetBeforeAndInside) { - ExecutionEnvironment executionEnvironment; - OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment); - { - SVMAllocsManager svmM(&memoryManager); - char *Ptr1 = (char *)svmM.createSVMAlloc(4096, false, false); - EXPECT_NE(Ptr1, nullptr); - - char *Ptr2 = Ptr1 - 4; - GraphicsAllocation *GA2 = svmM.getSVMAlloc(Ptr2); - EXPECT_EQ(GA2, nullptr); - - char *Ptr3 = Ptr1 + 4; - GraphicsAllocation *GA3 = svmM.getSVMAlloc(Ptr3); - EXPECT_NE(GA3, nullptr); - EXPECT_EQ(GA3->getUnderlyingBuffer(), Ptr1); - - svmM.freeSVMAlloc(Ptr1); - } -} - -TEST_F(SVMMemoryAllocatorTest, SVMAllocgetAfterSVM) { - ExecutionEnvironment executionEnvironment; - OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment); - { - SVMAllocsManager svmM(&memoryManager); - char *Ptr1 = (char *)svmM.createSVMAlloc(4096, false, false); - EXPECT_NE(Ptr1, nullptr); - - char *Ptr2 = Ptr1 + 4096 + 100; - GraphicsAllocation *GA2 = svmM.getSVMAlloc(Ptr2); - EXPECT_EQ(GA2, nullptr); - - svmM.freeSVMAlloc(Ptr1); - } -} - -TEST_F(SVMMemoryAllocatorTest, WhenCouldNotAllocateInMemoryManagerThenReturnsNullAndDoesNotChangeAllocsMap) { - struct MockMemManager : public OsAgnosticMemoryManager { - using OsAgnosticMemoryManager::allocateGraphicsMemory; - - public: - MockMemManager(ExecutionEnvironment &executionEnvironment) : OsAgnosticMemoryManager(false, false, executionEnvironment){}; - GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override { - return nullptr; - } - }; - - struct MockSVMAllocsManager : SVMAllocsManager { - - MockSVMAllocsManager(MemoryManager *m) - : SVMAllocsManager(m) { - } - - MapBasedAllocationTracker &GetSVMAllocs() { - return SVMAllocs; - } - }; - ExecutionEnvironment executionEnvironment; - MockMemManager memoryManager(executionEnvironment); - { - MockSVMAllocsManager svmM{&memoryManager}; - void *svmPtr = svmM.createSVMAlloc(512, false, false); - EXPECT_EQ(nullptr, svmPtr); - - EXPECT_EQ(0U, svmM.GetSVMAllocs().getNumAllocs()); - } -} - -TEST_F(SVMMemoryAllocatorTest, given64kbAllowedwhenAllocatingSvmMemoryThenDontPreferRenderCompression) { - class MyMemoryManager : public OsAgnosticMemoryManager { - public: - MyMemoryManager(ExecutionEnvironment &executionEnvironment) : OsAgnosticMemoryManager(false, false, executionEnvironment) { enable64kbpages = true; } - GraphicsAllocation *allocateGraphicsMemory64kb(AllocationData allocationData) override { - preferRenderCompressedFlag = allocationData.flags.preferRenderCompressed; - return nullptr; - } - bool preferRenderCompressedFlag = true; - }; - ExecutionEnvironment executionEnvironment; - MyMemoryManager myMemoryManager(executionEnvironment); - myMemoryManager.allocateGraphicsMemoryForSVM(1, false); - EXPECT_FALSE(myMemoryManager.preferRenderCompressedFlag); -} - -TEST_F(SVMMemoryAllocatorTest, whenReadOnlyFlagIsPresentThenReturnTrue) { - EXPECT_TRUE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_READ_ONLY)); - EXPECT_TRUE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_HOST_READ_ONLY)); - EXPECT_TRUE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_READ_ONLY)); -} - -TEST_F(SVMMemoryAllocatorTest, whenNoReadOnlyFlagIsPresentThenReturnFalse) { - EXPECT_FALSE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_READ_WRITE)); - EXPECT_FALSE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_WRITE_ONLY)); -} - -TEST_F(SVMMemoryAllocatorTest, whenReadOnlySvmAllocationCreatedThenGraphicsAllocationHasWriteableFlagFalse) { - ExecutionEnvironment executionEnvironment; - OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment); - SVMAllocsManager svmM(&memoryManager); - void *svm = svmM.createSVMAlloc(4096, false, true); - EXPECT_NE(nullptr, svm); - - GraphicsAllocation *svmAllocation = svmM.getSVMAlloc(svm); - EXPECT_NE(nullptr, svmAllocation); - EXPECT_FALSE(svmAllocation->isMemObjectsAllocationWithWritableFlags()); - - svmM.freeSVMAlloc(svm); -} diff --git a/unit_tests/memory_manager/svm_memory_manager_tests.cpp b/unit_tests/memory_manager/svm_memory_manager_tests.cpp new file mode 100644 index 0000000000..b84382f891 --- /dev/null +++ b/unit_tests/memory_manager/svm_memory_manager_tests.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2017-2018 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "unit_tests/mocks/mock_memory_manager.h" +#include "unit_tests/mocks/mock_svm_manager.h" +#include "gtest/gtest.h" + +using namespace OCLRT; + +struct SVMMemoryAllocatorTest : ::testing::Test { + SVMMemoryAllocatorTest() : memoryManager(false, false, executionEnvironment), svmManager(&memoryManager) {} + ExecutionEnvironment executionEnvironment; + MockMemoryManager memoryManager; + MockSVMAllocsManager svmManager; +}; + +TEST_F(SVMMemoryAllocatorTest, whenCreateZeroSizedSVMAllocationThenReturnNullptr) { + auto ptr = svmManager.createSVMAlloc(0, false, false); + + EXPECT_EQ(0u, svmManager.SVMAllocs.getNumAllocs()); + EXPECT_EQ(ptr, nullptr); +} + +TEST_F(SVMMemoryAllocatorTest, whenSVMAllocationIsFreedThenCannotBeGotAgain) { + auto ptr = svmManager.createSVMAlloc(MemoryConstants::pageSize, false, false); + EXPECT_NE(nullptr, ptr); + EXPECT_NE(nullptr, svmManager.getSVMAlloc(ptr)); + EXPECT_NE(nullptr, svmManager.getSVMAlloc(ptr)); + EXPECT_EQ(1u, svmManager.SVMAllocs.getNumAllocs()); + EXPECT_FALSE(svmManager.getSVMAlloc(ptr)->isCoherent()); + + svmManager.freeSVMAlloc(ptr); + EXPECT_EQ(nullptr, svmManager.getSVMAlloc(ptr)); + EXPECT_EQ(0u, svmManager.SVMAllocs.getNumAllocs()); +} + +TEST_F(SVMMemoryAllocatorTest, whenGetSVMAllocationFromReturnedPointerAreaThenReturnSameAllocation) { + auto ptr = svmManager.createSVMAlloc(MemoryConstants::pageSize, false, false); + EXPECT_NE(ptr, nullptr); + GraphicsAllocation *graphicsAllocation = svmManager.getSVMAlloc(ptr); + EXPECT_NE(nullptr, graphicsAllocation); + + auto ptrInRange = ptrOffset(ptr, MemoryConstants::pageSize - 4); + GraphicsAllocation *graphicsAllocationInRange = svmManager.getSVMAlloc(ptrInRange); + EXPECT_NE(nullptr, graphicsAllocationInRange); + + EXPECT_EQ(graphicsAllocation, graphicsAllocationInRange); + + svmManager.freeSVMAlloc(ptr); +} + +TEST_F(SVMMemoryAllocatorTest, whenGetSVMAllocationFromOutsideOfReturnedPointerAreaThenDontReturnThisAllocation) { + auto ptr = svmManager.createSVMAlloc(MemoryConstants::pageSize, false, false); + EXPECT_NE(ptr, nullptr); + GraphicsAllocation *graphicsAllocation = svmManager.getSVMAlloc(ptr); + EXPECT_NE(nullptr, graphicsAllocation); + + auto ptrBefore = ptrOffset(ptr, -4); + GraphicsAllocation *graphicsAllocationBefore = svmManager.getSVMAlloc(ptrBefore); + EXPECT_EQ(nullptr, graphicsAllocationBefore); + + auto ptrAfter = ptrOffset(ptr, MemoryConstants::pageSize); + GraphicsAllocation *graphicsAllocationAfter = svmManager.getSVMAlloc(ptrAfter); + EXPECT_EQ(nullptr, graphicsAllocationAfter); + + svmManager.freeSVMAlloc(ptr); +} + +TEST_F(SVMMemoryAllocatorTest, whenCouldNotAllocateInMemoryManagerThenReturnsNullAndDoesNotChangeAllocsMap) { + FailMemoryManager failMemoryManager(executionEnvironment); + svmManager.memoryManager = &failMemoryManager; + auto ptr = svmManager.createSVMAlloc(MemoryConstants::pageSize, false, false); + EXPECT_EQ(nullptr, ptr); + EXPECT_EQ(0u, svmManager.SVMAllocs.getNumAllocs()); + svmManager.freeSVMAlloc(ptr); +} + +TEST_F(SVMMemoryAllocatorTest, given64kbAllowedWhenAllocatingSvmMemoryThenDontPreferRenderCompression) { + MockMemoryManager memoryManager64Kb(true, false, executionEnvironment); + svmManager.memoryManager = &memoryManager64Kb; + auto ptr = svmManager.createSVMAlloc(MemoryConstants::pageSize, false, false); + EXPECT_FALSE(memoryManager64Kb.preferRenderCompressedFlagPassed); + svmManager.freeSVMAlloc(ptr); +} + +TEST_F(SVMMemoryAllocatorTest, given64kbAllowedwhenAllocatingSvmMemoryThenAllocationIsIn64kbPagePool) { + MockMemoryManager memoryManager64Kb(true, false, executionEnvironment); + svmManager.memoryManager = &memoryManager64Kb; + auto ptr = svmManager.createSVMAlloc(MemoryConstants::pageSize, false, false); + EXPECT_EQ(MemoryPool::System64KBPages, svmManager.getSVMAlloc(ptr)->getMemoryPool()); + svmManager.freeSVMAlloc(ptr); +} + +TEST_F(SVMMemoryAllocatorTest, given64kbDisallowedWhenAllocatingSvmMemoryThenAllocationIsIn4kbPagePool) { + auto ptr = svmManager.createSVMAlloc(MemoryConstants::pageSize, false, false); + EXPECT_EQ(MemoryPool::System4KBPages, svmManager.getSVMAlloc(ptr)->getMemoryPool()); + svmManager.freeSVMAlloc(ptr); +} + +TEST_F(SVMMemoryAllocatorTest, whenCoherentFlagIsPassedThenAllocationIsCoherent) { + auto ptr = svmManager.createSVMAlloc(MemoryConstants::pageSize, true, false); + EXPECT_TRUE(svmManager.getSVMAlloc(ptr)->isCoherent()); + svmManager.freeSVMAlloc(ptr); +} + +TEST_F(SVMMemoryAllocatorTest, whenReadOnlyFlagIsPresentThenReturnTrue) { + EXPECT_TRUE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_READ_ONLY)); + EXPECT_TRUE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_HOST_READ_ONLY)); + EXPECT_TRUE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_READ_ONLY)); +} + +TEST_F(SVMMemoryAllocatorTest, whenNoReadOnlyFlagIsPresentThenReturnFalse) { + EXPECT_FALSE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_READ_WRITE)); + EXPECT_FALSE(SVMAllocsManager::memFlagIsReadOnly(CL_MEM_WRITE_ONLY)); +} + +TEST_F(SVMMemoryAllocatorTest, whenReadOnlySvmAllocationCreatedThenGraphicsAllocationHasWriteableFlagFalse) { + void *svm = svmManager.createSVMAlloc(4096, false, true); + EXPECT_NE(nullptr, svm); + + GraphicsAllocation *svmAllocation = svmManager.getSVMAlloc(svm); + EXPECT_NE(nullptr, svmAllocation); + EXPECT_FALSE(svmAllocation->isMemObjectsAllocationWithWritableFlags()); + + svmManager.freeSVMAlloc(svm); +} diff --git a/unit_tests/mocks/CMakeLists.txt b/unit_tests/mocks/CMakeLists.txt index 57e940ea2e..ce4acc17f1 100644 --- a/unit_tests/mocks/CMakeLists.txt +++ b/unit_tests/mocks/CMakeLists.txt @@ -63,6 +63,7 @@ set(IGDRCL_SRCS_tests_mocks ${CMAKE_CURRENT_SOURCE_DIR}/mock_sip.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_source_level_debugger.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_submissions_aggregator.h + ${CMAKE_CURRENT_SOURCE_DIR}/mock_svm_manager.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_tbx_stream.h ) diff --git a/unit_tests/mocks/mock_memory_manager.h b/unit_tests/mocks/mock_memory_manager.h index b8a89dae54..ddcd1dd3b8 100644 --- a/unit_tests/mocks/mock_memory_manager.h +++ b/unit_tests/mocks/mock_memory_manager.h @@ -115,7 +115,7 @@ class FailMemoryManager : public MockMemoryManager { GraphicsAllocation *allocateGraphicsMemory64kb(AllocationData allocationData) override { return nullptr; }; - GraphicsAllocation *allocateGraphicsMemory(size_t size, const void *ptr) override { + GraphicsAllocation *allocateGraphicsMemory(const AllocationProperties &properties, const void *ptr) override { return nullptr; }; GraphicsAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override { diff --git a/unit_tests/mocks/mock_svm_manager.h b/unit_tests/mocks/mock_svm_manager.h new file mode 100644 index 0000000000..62351bf3ac --- /dev/null +++ b/unit_tests/mocks/mock_svm_manager.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) 2018 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "runtime/memory_manager/svm_memory_manager.h" +namespace OCLRT { +struct MockSVMAllocsManager : SVMAllocsManager { + + using SVMAllocsManager::memoryManager; + using SVMAllocsManager::SVMAllocs; + using SVMAllocsManager::SVMAllocsManager; +}; +} // namespace OCLRT \ No newline at end of file diff --git a/unit_tests/os_interface/linux/drm_command_stream_tests.cpp b/unit_tests/os_interface/linux/drm_command_stream_tests.cpp index bfc88ec342..bca6d8712d 100644 --- a/unit_tests/os_interface/linux/drm_command_stream_tests.cpp +++ b/unit_tests/os_interface/linux/drm_command_stream_tests.cpp @@ -1022,7 +1022,7 @@ TEST_F(DrmCommandStreamLeaksTest, makeResidentTwiceWhenFragmentStorage) { auto ptr = (void *)0x1001; auto size = MemoryConstants::pageSize * 10; auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); - auto allocation = mm->allocateGraphicsMemory(size, ptr); + auto allocation = mm->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); ASSERT_EQ(3u, allocation->fragmentsStorage.fragmentCount); @@ -1056,12 +1056,12 @@ TEST_F(DrmCommandStreamLeaksTest, givenFragmentedAllocationsWithResuedFragmentsW //3 fragments auto ptr = (void *)0x1001; auto size = MemoryConstants::pageSize * 10; - auto graphicsAllocation = mm->allocateGraphicsMemory(size, ptr); + auto graphicsAllocation = mm->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); auto offsetedPtr = (void *)((uintptr_t)ptr + size); auto size2 = MemoryConstants::pageSize - 1; - auto graphicsAllocation2 = mm->allocateGraphicsMemory(size2, offsetedPtr); + auto graphicsAllocation2 = mm->allocateGraphicsMemory(MockAllocationProperties{false, size2}, offsetedPtr); //graphicsAllocation2 reuses one fragment from graphicsAllocation EXPECT_EQ(graphicsAllocation->fragmentsStorage.fragmentStorageData[2].residency, graphicsAllocation2->fragmentsStorage.fragmentStorageData[0].residency); @@ -1123,7 +1123,7 @@ TEST_F(DrmCommandStreamLeaksTest, GivenAllocationCreatedFromThreeFragmentsWhenMa auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); - auto allocation = mm->allocateGraphicsMemory(size, ptr); + auto allocation = mm->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); ASSERT_EQ(3u, allocation->fragmentsStorage.fragmentCount); @@ -1153,11 +1153,11 @@ TEST_F(DrmCommandStreamLeaksTest, GivenAllocationCreatedFromThreeFragmentsWhenMa TEST_F(DrmCommandStreamLeaksTest, GivenAllocationsContainingDifferentCountOfFragmentsWhenAllocationIsMadeResidentThenAllFragmentsAreMadeResident) { auto ptr = (void *)0x1001; auto size = MemoryConstants::pageSize; - auto size2 = 100; + auto size2 = 100u; auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); - auto allocation = mm->allocateGraphicsMemory(size, ptr); + auto allocation = mm->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); ASSERT_EQ(2u, allocation->fragmentsStorage.fragmentCount); ASSERT_EQ(2u, reqs.requiredFragmentsCount); @@ -1185,7 +1185,7 @@ TEST_F(DrmCommandStreamLeaksTest, GivenAllocationsContainingDifferentCountOfFrag mm->freeGraphicsMemory(allocation); csr->getResidencyAllocations().clear(); - auto allocation2 = mm->allocateGraphicsMemory(size2, ptr); + auto allocation2 = mm->allocateGraphicsMemory(MockAllocationProperties{false, size2}, ptr); reqs = MockHostPtrManager::getAllocationRequirements(ptr, size2); ASSERT_EQ(1u, allocation2->fragmentsStorage.fragmentCount); @@ -1219,8 +1219,8 @@ TEST_F(DrmCommandStreamLeaksTest, GivenTwoAllocationsWhenBackingStorageIsTheSame auto size = MemoryConstants::pageSize; auto ptr2 = (void *)0x1000; - auto allocation = mm->allocateGraphicsMemory(size, ptr); - auto allocation2 = mm->allocateGraphicsMemory(size, ptr2); + auto allocation = mm->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); + auto allocation2 = mm->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr2); csr->makeResident(*allocation); csr->makeResident(*allocation2); @@ -1242,8 +1242,8 @@ TEST_F(DrmCommandStreamLeaksTest, GivenTwoAllocationsWhenBackingStorageIsDiffere auto size = MemoryConstants::pageSize; auto ptr2 = (void *)0x3000; - auto allocation = mm->allocateGraphicsMemory(size, ptr); - auto allocation2 = mm->allocateGraphicsMemory(size, ptr2); + auto allocation = mm->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); + auto allocation2 = mm->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr2); csr->makeResident(*allocation); csr->makeResident(*allocation2); diff --git a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp index bae13a087e..1453e1c6cb 100644 --- a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp @@ -440,7 +440,7 @@ TEST_F(DrmMemoryManagerTest, Allocate_HostPtr) { void *ptr = ::alignedMalloc(1024, 4096); ASSERT_NE(nullptr, ptr); - auto alloc = memoryManager->allocateGraphicsMemory(1024, ptr); + auto alloc = static_cast(memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 1024}, ptr)); ASSERT_NE(nullptr, alloc); EXPECT_NE(nullptr, alloc->getUnderlyingBuffer()); EXPECT_EQ(ptr, alloc->getUnderlyingBuffer()); @@ -461,7 +461,9 @@ TEST_F(DrmMemoryManagerTest, Allocate_HostPtr_Nullptr) { void *ptr = nullptr; - auto alloc = memoryManager->allocateGraphicsMemory(1024, ptr); + allocationData.hostPtr = nullptr; + allocationData.size = MemoryConstants::pageSize; + auto alloc = static_cast(memoryManager->allocateGraphicsMemoryWithHostPtr(allocationData)); ASSERT_NE(nullptr, alloc); EXPECT_EQ(ptr, alloc->getUnderlyingBuffer()); @@ -484,7 +486,7 @@ TEST_F(DrmMemoryManagerTest, Allocate_HostPtr_MisAligned) { void *ptr = ptrOffset(ptrT, 128); - auto alloc = memoryManager->allocateGraphicsMemory(1024, ptr); + auto alloc = static_cast(memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 1024}, ptr)); ASSERT_NE(nullptr, alloc); EXPECT_NE(nullptr, alloc->getUnderlyingBuffer()); EXPECT_EQ(ptr, alloc->getUnderlyingBuffer()); @@ -505,7 +507,7 @@ TEST_F(DrmMemoryManagerTest, Allocate_HostPtr_UserptrFail) { void *ptrT = ::alignedMalloc(1024, 4096); ASSERT_NE(nullptr, ptrT); - auto alloc = memoryManager->allocateGraphicsMemory(1024, ptrT); + auto alloc = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 1024}, ptrT); EXPECT_EQ(nullptr, alloc); ::alignedFree(ptrT); @@ -694,7 +696,7 @@ TEST_F(DrmMemoryManagerTest, GivenMisalignedHostPtrAndMultiplePagesSizeWhenAsked auto ptr = reinterpret_cast(0x1001); auto size = MemoryConstants::pageSize * 10; - auto graphicsAllocation = memoryManager->allocateGraphicsMemory(size, ptr); + auto graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); @@ -2441,7 +2443,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenMemoryManagerWhenAlloc void *ptr = reinterpret_cast(0x1001); auto size = 4096u; - auto allocation = memoryManager->allocateGraphicsMemory(size, ptr); + auto allocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); ASSERT_NE(nullptr, allocation); EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); memoryManager->freeGraphicsMemory(allocation); @@ -2464,17 +2466,6 @@ TEST(DrmMemoryManager, givenMemoryManagerWhenAllocate32BitGraphicsMemoryWithPtrI memoryManager->freeGraphicsMemory(allocation); } -TEST(DrmMemoryManager, givenMemoryManagerWith64KBPagesDisabledWhenAllocateGraphicsMemoryForSVMIsCalledThen4KBGraphicsAllocationIsReturned) { - ExecutionEnvironment executionEnvironment; - std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(Drm::get(0), false, true, executionEnvironment)); - auto size = MemoryConstants::pageSize; - - auto svmAllocation = memoryManager->allocateGraphicsMemoryForSVM(size, false); - EXPECT_NE(nullptr, svmAllocation); - EXPECT_EQ(MemoryPool::System4KBPages, svmAllocation->getMemoryPool()); - memoryManager->freeGraphicsMemory(svmAllocation); -} - TEST(DrmMemoryManager, givenMemoryManagerWhenCreateAllocationFromHandleIsCalledThenMemoryPoolIsSystemCpuInaccessible) { ExecutionEnvironment executionEnvironment; std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(Drm::get(0), false, true, executionEnvironment)); @@ -2496,17 +2487,6 @@ TEST(DrmMemoryManager, DISABLED_givenMemoryManagerWith64KBPagesEnabledWhenAlloca memoryManager->freeGraphicsMemory(allocation); } -TEST(DrmMemoryManager, DISABLED_givenMemoryManagerWith64KBPagesEnabledWhenAllocateGraphicsMemoryForSVMIsCalledThenMemoryPoolIsSystem64KBPages) { - ExecutionEnvironment executionEnvironment; - std::unique_ptr memoryManager(new (std::nothrow) TestedDrmMemoryManager(Drm::get(0), false, true, executionEnvironment)); - auto size = MemoryConstants::pageSize; - - auto svmAllocation = memoryManager->allocateGraphicsMemoryForSVM(size, false); - EXPECT_NE(nullptr, svmAllocation); - EXPECT_EQ(MemoryPool::System64KBPages, svmAllocation->getMemoryPool()); - memoryManager->freeGraphicsMemory(svmAllocation); -} - TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDisabledForcePinAndEnabledValidateHostMemoryWhenPinBBAllocationFailsThenUnrecoverableIsCalled) { this->mock->ioctl_res = -1; this->mock->ioctl_expected.gemUserptr = 1; @@ -2617,7 +2597,7 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenValidateHostPtrMemoryE size_t size = 10 * 1024 * 1024; void *ptr = ::alignedMalloc(size, 4096); - auto alloc = memoryManager->allocateGraphicsMemory(size, ptr); + auto alloc = static_cast(memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr)); ASSERT_NE(nullptr, alloc); EXPECT_NE(nullptr, alloc->getBO()); diff --git a/unit_tests/os_interface/windows/device_command_stream_tests.cpp b/unit_tests/os_interface/windows/device_command_stream_tests.cpp index f295d2cf2b..d056fd7216 100644 --- a/unit_tests/os_interface/windows/device_command_stream_tests.cpp +++ b/unit_tests/os_interface/windows/device_command_stream_tests.cpp @@ -580,7 +580,7 @@ TEST_F(WddmCommandStreamTest, givenGraphicsAllocationWhenMakeResidentThenAllocat void *hostPtr = reinterpret_cast(wddm->virtualAllocAddress + 0x1234); auto size = 1234u; - auto gfxAllocation = memoryManager->allocateGraphicsMemory(size, hostPtr); + auto gfxAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, hostPtr); ASSERT_NE(nullptr, gfxAllocation); @@ -596,7 +596,7 @@ TEST_F(WddmCommandStreamTest, givenHostPtrWhenPtrBelowRestrictionThenCreateAlloc void *hostPtr = reinterpret_cast(memoryManager->getAlignedMallocRestrictions()->minAddress - 0x1000); auto size = 0x2000u; - auto gfxAllocation = static_cast(memoryManager->allocateGraphicsMemory(size, hostPtr)); + auto gfxAllocation = static_cast(memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, hostPtr)); void *expectedReserve = reinterpret_cast(wddm->virtualAllocAddress); @@ -616,8 +616,8 @@ TEST_F(WddmCommandStreamTest, givenTwoTemporaryAllocationsWhenCleanTemporaryAllo void *host_ptr2 = (void *)0x2212341; auto size = 17262u; - GraphicsAllocation *graphicsAllocation = memoryManager->allocateGraphicsMemory(size, host_ptr); - GraphicsAllocation *graphicsAllocation2 = memoryManager->allocateGraphicsMemory(size, host_ptr2); + GraphicsAllocation *graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, host_ptr); + GraphicsAllocation *graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, host_ptr2); csr->getInternalAllocationStorage()->storeAllocation(std::unique_ptr(graphicsAllocation), TEMPORARY_ALLOCATION); csr->getInternalAllocationStorage()->storeAllocation(std::unique_ptr(graphicsAllocation2), TEMPORARY_ALLOCATION); diff --git a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp index 7073f0b651..7c0ebedbc2 100644 --- a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp @@ -137,7 +137,7 @@ TEST_F(WddmMemoryManagerSimpleTest, givenMemoryManagerWhenAllocateGraphicsMemory memoryManager.reset(new MockWddmMemoryManager(false, false, wddm, executionEnvironment)); void *ptr = reinterpret_cast(0x1001); auto size = 4096u; - auto allocation = memoryManager->allocateGraphicsMemory(size, ptr); + auto allocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); ASSERT_NE(nullptr, allocation); EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); for (size_t i = 0; i < allocation->fragmentsStorage.fragmentCount; i++) { @@ -160,22 +160,22 @@ TEST_F(WddmMemoryManagerSimpleTest, givenMemoryManagerWhenAllocate32BitGraphicsM memoryManager->freeGraphicsMemory(allocation); } -TEST_F(WddmMemoryManagerSimpleTest, givenMemoryManagerWith64KBPagesDisabledWhenAllocateGraphicsMemoryForSVMIsCalledThen4KBGraphicsAllocationIsReturned) { +TEST_F(WddmMemoryManagerSimpleTest, givenMemoryManagerWith64KBPagesDisabledWhenAllocateGraphicsMemoryForSVMThen4KBGraphicsAllocationIsReturned) { memoryManager.reset(new MockWddmMemoryManager(false, false, wddm, executionEnvironment)); auto size = MemoryConstants::pageSize; - auto svmAllocation = memoryManager->allocateGraphicsMemoryForSVM(size, false); + auto svmAllocation = memoryManager->allocateGraphicsMemoryWithProperties({size, GraphicsAllocation::AllocationType::SVM}); EXPECT_NE(nullptr, svmAllocation); EXPECT_EQ(MemoryPool::System4KBPages, svmAllocation->getMemoryPool()); EXPECT_TRUE(svmAllocation->gmm->useSystemMemoryPool); memoryManager->freeGraphicsMemory(svmAllocation); } -TEST_F(WddmMemoryManagerSimpleTest, givenMemoryManagerWith64KBPagesEnabledWhenAllocateGraphicsMemoryForSVMIsCalledThenMemoryPoolIsSystem64KBPages) { +TEST_F(WddmMemoryManagerSimpleTest, givenMemoryManagerWith64KBPagesEnabledWhenAllocateGraphicsMemoryForSVMThenMemoryPoolIsSystem64KBPages) { memoryManager.reset(new MockWddmMemoryManager(true, false, wddm, executionEnvironment)); auto size = MemoryConstants::pageSize; - auto svmAllocation = memoryManager->allocateGraphicsMemoryForSVM(size, false); + auto svmAllocation = memoryManager->allocateGraphicsMemoryWithProperties({size, GraphicsAllocation::AllocationType::SVM}); EXPECT_NE(nullptr, svmAllocation); EXPECT_EQ(MemoryPool::System64KBPages, svmAllocation->getMemoryPool()); memoryManager->freeGraphicsMemory(svmAllocation); @@ -272,7 +272,7 @@ TEST_F(WddmMemoryManagerTest, AllocateGpuMemHostPtr) { void *ptr = alignedMalloc(3 * 4096, 4096); ASSERT_NE(nullptr, ptr); - auto *gpuAllocation = memoryManager->allocateGraphicsMemory(0x1000, ptr); + auto *gpuAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, ptr); // Should be same cpu ptr and gpu ptr EXPECT_EQ(ptr, gpuAllocation->getUnderlyingBuffer()); @@ -583,7 +583,7 @@ TEST_F(WddmMemoryManagerTest, AllocateGpuMemHostPtrOffseted) { size_t baseOffset = 1024; // misalligned buffer spanning accross 3 pages - auto *gpuAllocation = memoryManager->allocateGraphicsMemory(2 * 4096, (char *)ptr + baseOffset); + auto *gpuAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 2 * MemoryConstants::pageSize}, (char *)ptr + baseOffset); // Should be same cpu ptr and gpu ptr EXPECT_EQ((char *)ptr + baseOffset, gpuAllocation->getUnderlyingBuffer()); @@ -601,7 +601,7 @@ TEST_F(WddmMemoryManagerTest, AllocateGpuMemHostPtrOffseted) { // offseted by one page, still in boundary void *offsetedPtr = reinterpret_cast(reinterpret_cast(ptr) + 4096); - auto *gpuAllocation2 = memoryManager->allocateGraphicsMemory(0x1000, offsetedPtr); + auto *gpuAllocation2 = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, offsetedPtr); // Should be same cpu ptr and gpu ptr EXPECT_EQ(offsetedPtr, gpuAllocation2->getUnderlyingBuffer()); @@ -633,7 +633,7 @@ TEST_F(WddmMemoryManagerTest, AllocateGpuMemCheckGmm) { bool success = false; // three pages void *ptr = alignedMalloc(3 * 4096, 4096); - auto *gpuAllocation = memoryManager->allocateGraphicsMemory(3 * 4096, ptr); + auto *gpuAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 3 * MemoryConstants::pageSize}, ptr); // Should be same cpu ptr and gpu ptr ASSERT_NE(nullptr, gpuAllocation); EXPECT_EQ(ptr, gpuAllocation->getUnderlyingBuffer()); @@ -789,7 +789,7 @@ TEST_F(WddmMemoryManagerTest, givenWddmMemoryManagerWhenCpuMemNotMeetRestriction void *cpuPtr = reinterpret_cast(memoryManager->getAlignedMallocRestrictions()->minAddress - 0x1000); size_t size = 0x1000; - WddmAllocation *allocation = static_cast(memoryManager->allocateGraphicsMemory(size, cpuPtr)); + WddmAllocation *allocation = static_cast(memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, cpuPtr)); void *expectReserve = reinterpret_cast(wddm->virtualAllocAddress); @@ -921,7 +921,7 @@ TEST_F(BufferWithWddmMemory, NullOsHandleStorageAskedForPopulationReturnsFilledP TEST_F(BufferWithWddmMemory, GivenMisalignedHostPtrAndMultiplePagesSizeWhenAskedForGraphicsAllcoationThenItContainsAllFragmentsWithProperGpuAdrresses) { auto ptr = reinterpret_cast(wddm->virtualAllocAddress + 0x1001); auto size = MemoryConstants::pageSize * 10; - auto graphicsAllocation = memoryManager->allocateGraphicsMemory(size, ptr); + auto graphicsAllocation = memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, size}, ptr); auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); @@ -1129,7 +1129,7 @@ TEST_F(MockWddmMemoryManagerTest, givenValidateAllocationFunctionWhenItIsCalledW EXPECT_TRUE(wddm->init(PreemptionHelper::getDefaultPreemptionMode(*platformDevices[0]))); MockWddmMemoryManager memoryManager(wddm.get(), executionEnvironment); - auto wddmAlloc = (WddmAllocation *)memoryManager.allocateGraphicsMemory(4096u, reinterpret_cast(0x1000)); + auto wddmAlloc = (WddmAllocation *)memoryManager.allocateGraphicsMemory(MockAllocationProperties{false, MemoryConstants::pageSize}, reinterpret_cast(0x1000)); EXPECT_TRUE(memoryManager.validateAllocationMock(wddmAlloc)); diff --git a/unit_tests/os_interface/windows/wddm_residency_controller_tests.cpp b/unit_tests/os_interface/windows/wddm_residency_controller_tests.cpp index 7022bcb484..3aecfc7918 100644 --- a/unit_tests/os_interface/windows/wddm_residency_controller_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_residency_controller_tests.cpp @@ -12,6 +12,7 @@ #include "runtime/os_interface/os_interface.h" #include "runtime/os_interface/windows/os_interface.h" #include "runtime/os_interface/windows/wddm_residency_controller.h" +#include "unit_tests/mocks/mock_allocation_properties.h" #include "unit_tests/mocks/mock_wddm.h" #include "unit_tests/os_interface/windows/mock_gdi_interface.h" #include "unit_tests/os_interface/windows/mock_wddm_allocation.h" @@ -498,7 +499,7 @@ TEST_F(WddmResidencyControllerWithGdiAndMemoryManagerTest, givenTripleAllocation // 3-fragment Allocation void *ptr = reinterpret_cast(wddm->virtualAllocAddress + 0x1500); - auto allocationTriple = static_cast(memoryManager->allocateGraphicsMemory(8196, ptr)); + auto allocationTriple = static_cast(memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 2 * MemoryConstants::pageSize}, ptr)); // whole allocation unused since previous trim allocationTriple->getResidencyData().updateCompletionData(0, osContextId); @@ -737,7 +738,7 @@ TEST_F(WddmResidencyControllerWithGdiAndMemoryManagerTest, trimToBudgetEvictsDon allocation2.getResidencyData().resident[osContextId] = true; void *ptrTriple = reinterpret_cast(reinterpret_cast(ptr) + 0x500); - WddmAllocation *allocationTriple = static_cast(memoryManager->allocateGraphicsMemory(8196, ptrTriple)); + WddmAllocation *allocationTriple = static_cast(memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 2 * MemoryConstants::pageSize}, ptrTriple)); allocationTriple->getResidencyData().updateCompletionData(1, osContextId); allocationTriple->getResidencyData().resident[osContextId] = true; @@ -877,7 +878,7 @@ TEST_F(WddmResidencyControllerWithGdiAndMemoryManagerTest, makeResidentResidency MockWddmAllocation allocation1, allocation2; void *ptr = reinterpret_cast(wddm->virtualAllocAddress + 0x1500); - WddmAllocation *allocationTriple = (WddmAllocation *)memoryManager->allocateGraphicsMemory(8196, ptr); + WddmAllocation *allocationTriple = (WddmAllocation *)memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 2 * MemoryConstants::pageSize}, ptr); ResidencyContainer residencyPack{&allocation1, allocationTriple, &allocation2}; residencyController->makeResidentResidencyAllocations(residencyPack); @@ -894,7 +895,7 @@ TEST_F(WddmResidencyControllerWithGdiAndMemoryManagerTest, makeResidentResidency TEST_F(WddmResidencyControllerWithGdiAndMemoryManagerTest, makeResidentResidencyAllocationsSetsLastFencePLusOneForTripleAllocations) { MockWddmAllocation allocation1, allocation2; - WddmAllocation *allocationTriple = (WddmAllocation *)memoryManager->allocateGraphicsMemory(8196, reinterpret_cast(0x1500)); + WddmAllocation *allocationTriple = (WddmAllocation *)memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 2 * MemoryConstants::pageSize}, reinterpret_cast(0x1500)); residencyController->getMonitoredFence().currentFenceValue = 20; @@ -930,7 +931,7 @@ TEST_F(WddmResidencyControllerWithMockWddmTest, givenMakeResidentFailsWhenCallin TEST_F(WddmResidencyControllerWithMockWddmTest, givenMakeResidentFailsWhenCallingMakeResidentResidencyAllocationsThenDontMarkTripleAllocationsAsResident) { MockWddmAllocation allocation1, allocation2; void *ptr = reinterpret_cast(wddm->getWddmMinAddress() + 0x1500); - WddmAllocation *allocationTriple = static_cast(memoryManager->allocateGraphicsMemory(8196, ptr)); + WddmAllocation *allocationTriple = static_cast(memoryManager->allocateGraphicsMemory(MockAllocationProperties{false, 2 * MemoryConstants::pageSize}, ptr)); ASSERT_NE(nullptr, allocationTriple); auto makeResidentWithOutBytesToTrim = [](D3DKMT_HANDLE *handles, uint32_t count, bool cantTrimFurther, uint64_t *numberOfBytesToTrim) -> bool { *numberOfBytesToTrim = 4 * 4096; return false; };