diff --git a/runtime/mem_obj/buffer.cpp b/runtime/mem_obj/buffer.cpp index 6c75eae4e4..246f61a6a2 100644 --- a/runtime/mem_obj/buffer.cpp +++ b/runtime/mem_obj/buffer.cpp @@ -17,6 +17,7 @@ #include "runtime/helpers/ptr_math.h" #include "runtime/helpers/string.h" #include "runtime/helpers/validators.h" +#include "runtime/memory_manager/host_ptr_manager.h" #include "runtime/memory_manager/memory_manager.h" #include "runtime/memory_manager/svm_memory_manager.h" #include "runtime/os_interface/debug_settings_manager.h" @@ -238,19 +239,19 @@ void Buffer::checkMemory(cl_mem_flags flags, cl_int &errcodeRet, bool &alignementSatisfied, bool ©MemoryFromHostPtr, - MemoryManager *memMngr) { + MemoryManager *memoryManager) { errcodeRet = CL_SUCCESS; alignementSatisfied = true; copyMemoryFromHostPtr = false; uintptr_t minAddress = 0; - auto memRestrictions = memMngr->getAlignedMallocRestrictions(); + auto memRestrictions = memoryManager->getAlignedMallocRestrictions(); if (memRestrictions) { minAddress = memRestrictions->minAddress; } if (flags & CL_MEM_USE_HOST_PTR) { if (hostPtr) { - auto fragment = memMngr->hostPtrManager.getFragment(hostPtr); + auto fragment = memoryManager->getHostPtrManager()->getFragment(hostPtr); if (fragment && fragment->driverAllocation) { errcodeRet = CL_INVALID_HOST_PTR; return; diff --git a/runtime/memory_manager/host_ptr_manager.cpp b/runtime/memory_manager/host_ptr_manager.cpp index 66bec33b64..65aecedcc2 100644 --- a/runtime/memory_manager/host_ptr_manager.cpp +++ b/runtime/memory_manager/host_ptr_manager.cpp @@ -6,13 +6,12 @@ */ #include "runtime/command_stream/command_stream_receiver.h" -#include "runtime/helpers/ptr_math.h" -#include "runtime/helpers/abort.h" +#include "runtime/memory_manager/host_ptr_manager.h" #include "runtime/memory_manager/memory_manager.h" using namespace OCLRT; -std::map::iterator HostPtrManager::findElement(const void *ptr) { +HostPtrFragmentsContainer::iterator HostPtrManager::findElement(const void *ptr) { auto nextElement = partialAllocations.lower_bound(ptr); auto element = nextElement; if (element != partialAllocations.end()) { diff --git a/runtime/memory_manager/host_ptr_manager.h b/runtime/memory_manager/host_ptr_manager.h index 7afd0461a7..85bdd3c445 100644 --- a/runtime/memory_manager/host_ptr_manager.h +++ b/runtime/memory_manager/host_ptr_manager.h @@ -7,33 +7,29 @@ #pragma once #include -#include "runtime/helpers/aligned_memory.h" -#include "runtime/memory_manager/graphics_allocation.h" +#include #include "runtime/memory_manager/host_ptr_defines.h" namespace OCLRT { -typedef std::map HostPtrFragmentsContainer; +using HostPtrFragmentsContainer = std::map; class MemoryManager; class HostPtrManager { public: - static AllocationRequirements getAllocationRequirements(const void *inputPtr, size_t size); - OsHandleStorage populateAlreadyAllocatedFragments(AllocationRequirements &requirements, CheckedFragments *checkedFragments); - void storeFragment(FragmentStorage &fragment); - void storeFragment(AllocationStorageData &storageData); - + FragmentStorage *getFragment(const void *inputPtr); + OsHandleStorage prepareOsStorageForAllocation(MemoryManager &memoryManager, size_t size, const void *ptr); void releaseHandleStorage(OsHandleStorage &fragments); bool releaseHostPtr(const void *ptr); + void storeFragment(AllocationStorageData &storageData); + void storeFragment(FragmentStorage &fragment); - FragmentStorage *getFragment(const void *inputPtr); - size_t getFragmentCount() { return partialAllocations.size(); } + protected: + static AllocationRequirements getAllocationRequirements(const void *inputPtr, size_t size); + OsHandleStorage populateAlreadyAllocatedFragments(AllocationRequirements &requirements, CheckedFragments *checkedFragments); FragmentStorage *getFragmentAndCheckForOverlaps(const void *inputPtr, size_t size, OverlapStatus &overlappingStatus); - OsHandleStorage prepareOsStorageForAllocation(MemoryManager &memoryManager, size_t size, const void *ptr); RequirementsStatus checkAllocationsForOverlapping(MemoryManager &memoryManager, AllocationRequirements *requirements, CheckedFragments *checkedFragments); - private: - std::map::iterator findElement(const void *ptr); - + HostPtrFragmentsContainer::iterator findElement(const void *ptr); HostPtrFragmentsContainer partialAllocations; std::recursive_mutex allocationsMutex; }; diff --git a/runtime/memory_manager/memory_manager.cpp b/runtime/memory_manager/memory_manager.cpp index 76ee16d3ba..e64de55eab 100644 --- a/runtime/memory_manager/memory_manager.cpp +++ b/runtime/memory_manager/memory_manager.cpp @@ -16,6 +16,7 @@ #include "runtime/helpers/options.h" #include "runtime/helpers/timestamp_packet.h" #include "runtime/memory_manager/deferred_deleter.h" +#include "runtime/memory_manager/host_ptr_manager.h" #include "runtime/memory_manager/internal_allocation_storage.h" #include "runtime/os_interface/os_context.h" #include "runtime/utilities/stackvec.h" @@ -60,7 +61,8 @@ GraphicsAllocation *AllocationsList::detachAllocationImpl(GraphicsAllocation *, MemoryManager::MemoryManager(bool enable64kbpages, bool enableLocalMemory, ExecutionEnvironment &executionEnvironment) : allocator32Bit(nullptr), enable64kbpages(enable64kbpages), localMemorySupported(enableLocalMemory), - executionEnvironment(executionEnvironment){}; + executionEnvironment(executionEnvironment), + hostPtrManager(std::make_unique()){}; MemoryManager::~MemoryManager() { for (auto osContext : registeredOsContexts) { @@ -119,7 +121,7 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemory(size_t size, const voi deferredDeleter->drain(true); } GraphicsAllocation *graphicsAllocation = nullptr; - auto osStorage = hostPtrManager.prepareOsStorageForAllocation(*this, size, ptr); + auto osStorage = hostPtrManager->prepareOsStorageForAllocation(*this, size, ptr); if (osStorage.fragmentCount > 0) { graphicsAllocation = createGraphicsAllocation(osStorage, size, ptr); } @@ -127,7 +129,7 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemory(size_t size, const voi } void MemoryManager::cleanGraphicsMemoryCreatedFromHostPtr(GraphicsAllocation *graphicsAllocation) { - hostPtrManager.releaseHandleStorage(graphicsAllocation->fragmentsStorage); + hostPtrManager->releaseHandleStorage(graphicsAllocation->fragmentsStorage); cleanOsHandles(graphicsAllocation->fragmentsStorage); } diff --git a/runtime/memory_manager/memory_manager.h b/runtime/memory_manager/memory_manager.h index 0897713129..b1afeaacaf 100644 --- a/runtime/memory_manager/memory_manager.h +++ b/runtime/memory_manager/memory_manager.h @@ -9,7 +9,6 @@ #include "runtime/helpers/aligned_memory.h" #include "runtime/memory_manager/graphics_allocation.h" #include "runtime/memory_manager/host_ptr_defines.h" -#include "runtime/memory_manager/host_ptr_manager.h" #include "runtime/os_interface/32bit_memory.h" #include @@ -22,6 +21,7 @@ class Device; class DeferredDeleter; class ExecutionEnvironment; class GraphicsAllocation; +class HostPtrManager; class CommandStreamReceiver; class OsContext; class TimestampPacket; @@ -213,8 +213,6 @@ class MemoryManager { MOCKABLE_VIRTUAL std::unique_ptr obtainReusableAllocation(size_t requiredSize, bool isInternalAllocationRequired); - HostPtrManager hostPtrManager; - virtual GraphicsAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, size_t hostPtrSize, const void *hostPtr) = 0; bool peek64kbPagesEnabled() const { return enable64kbpages; } @@ -252,6 +250,7 @@ class MemoryManager { void registerOsContext(OsContext *contextToRegister); size_t getOsContextCount() { return registeredOsContexts.size(); } CommandStreamReceiver *getCommandStreamReceiver(uint32_t contextId); + HostPtrManager *getHostPtrManager() const { return hostPtrManager.get(); } protected: static bool getAllocationData(AllocationData &allocationData, const AllocationFlags &flags, const DevicesBitfield devicesBitfield, @@ -271,6 +270,7 @@ class MemoryManager { bool localMemorySupported = false; ExecutionEnvironment &executionEnvironment; std::vector registeredOsContexts; + std::unique_ptr hostPtrManager; }; std::unique_ptr createDeferredDeleter(); diff --git a/runtime/memory_manager/os_agnostic_memory_manager.cpp b/runtime/memory_manager/os_agnostic_memory_manager.cpp index c914b4027b..d8c26ce76a 100644 --- a/runtime/memory_manager/os_agnostic_memory_manager.cpp +++ b/runtime/memory_manager/os_agnostic_memory_manager.cpp @@ -14,6 +14,7 @@ #include "runtime/helpers/options.h" #include "runtime/helpers/ptr_math.h" #include "runtime/helpers/surface_formats.h" +#include "runtime/memory_manager/host_ptr_manager.h" #include namespace OCLRT { @@ -132,16 +133,16 @@ void OsAgnosticMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation * fragment.fragmentSize = alignUp(gfxAllocation->getUnderlyingBufferSize(), MemoryConstants::pageSize); fragment.osInternalStorage = new OsHandle(); fragment.residency = new ResidencyData(); - hostPtrManager.storeFragment(fragment); + hostPtrManager->storeFragment(fragment); } void OsAgnosticMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) { auto buffer = gfxAllocation->getUnderlyingBuffer(); - auto fragment = hostPtrManager.getFragment(buffer); + auto fragment = hostPtrManager->getFragment(buffer); if (fragment && fragment->driverAllocation) { OsHandle *osStorageToRelease = fragment->osInternalStorage; ResidencyData *residencyDataToRelease = fragment->residency; - if (hostPtrManager.releaseHostPtr(buffer)) { + if (hostPtrManager->releaseHostPtr(buffer)) { delete osStorageToRelease; delete residencyDataToRelease; } @@ -210,7 +211,7 @@ MemoryManager::AllocationStatus OsAgnosticMemoryManager::populateOsHandles(OsHan newFragment.fragmentSize = handleStorage.fragmentStorageData[i].fragmentSize; newFragment.osInternalStorage = handleStorage.fragmentStorageData[i].osHandleStorage; newFragment.residency = handleStorage.fragmentStorageData[i].residency; - hostPtrManager.storeFragment(newFragment); + hostPtrManager->storeFragment(newFragment); } } return AllocationStatus::Success; diff --git a/runtime/os_interface/linux/drm_memory_manager.cpp b/runtime/os_interface/linux/drm_memory_manager.cpp index c35bcf0720..6db3e94844 100644 --- a/runtime/os_interface/linux/drm_memory_manager.cpp +++ b/runtime/os_interface/linux/drm_memory_manager.cpp @@ -8,6 +8,7 @@ #include "runtime/device/device.h" #include "runtime/helpers/ptr_math.h" #include "runtime/helpers/options.h" +#include "runtime/memory_manager/host_ptr_manager.h" #include "runtime/os_interface/32bit_memory.h" #include "runtime/os_interface/linux/drm_allocation.h" #include "runtime/os_interface/linux/drm_buffer_object.h" @@ -406,16 +407,16 @@ void DrmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAllo fragment.osInternalStorage = new OsHandle(); fragment.residency = new ResidencyData(); fragment.osInternalStorage->bo = drmMemory->getBO(); - hostPtrManager.storeFragment(fragment); + hostPtrManager->storeFragment(fragment); } void DrmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) { auto buffer = gfxAllocation->getUnderlyingBuffer(); - auto fragment = hostPtrManager.getFragment(buffer); + auto fragment = hostPtrManager->getFragment(buffer); if (fragment && fragment->driverAllocation) { OsHandle *osStorageToRelease = fragment->osInternalStorage; ResidencyData *residencyDataToRelease = fragment->residency; - if (hostPtrManager.releaseHostPtr(buffer)) { + if (hostPtrManager->releaseHostPtr(buffer)) { delete osStorageToRelease; delete residencyDataToRelease; } @@ -509,7 +510,7 @@ MemoryManager::AllocationStatus DrmMemoryManager::populateOsHandles(OsHandleStor } for (uint32_t i = 0; i < numberOfBosAllocated; i++) { - hostPtrManager.storeFragment(handleStorage.fragmentStorageData[indexesOfAllocatedBos[i]]); + hostPtrManager->storeFragment(handleStorage.fragmentStorageData[indexesOfAllocatedBos[i]]); } return AllocationStatus::Success; } diff --git a/runtime/os_interface/windows/wddm_memory_manager.cpp b/runtime/os_interface/windows/wddm_memory_manager.cpp index 7d71fe39b8..70318e24ee 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.cpp +++ b/runtime/os_interface/windows/wddm_memory_manager.cpp @@ -16,6 +16,7 @@ #include "runtime/helpers/surface_formats.h" #include "runtime/memory_manager/deferrable_deletion.h" #include "runtime/memory_manager/deferred_deleter.h" +#include "runtime/memory_manager/host_ptr_manager.h" #include "runtime/os_interface/windows/wddm/wddm.h" #include "runtime/os_interface/windows/wddm_allocation.h" #include "runtime/os_interface/windows/wddm_residency_controller.h" @@ -285,15 +286,15 @@ void WddmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAll fragment.osInternalStorage->handle = wddmMemory->handle; fragment.osInternalStorage->gmm = gfxAllocation->gmm; fragment.residency = &wddmMemory->getResidencyData(); - hostPtrManager.storeFragment(fragment); + hostPtrManager->storeFragment(fragment); } void WddmMemoryManager::removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) { auto buffer = gfxAllocation->getUnderlyingBuffer(); - auto fragment = hostPtrManager.getFragment(buffer); + auto fragment = hostPtrManager->getFragment(buffer); if (fragment && fragment->driverAllocation) { OsHandle *osStorageToRelease = fragment->osInternalStorage; - if (hostPtrManager.releaseHostPtr(buffer)) { + if (hostPtrManager->releaseHostPtr(buffer)) { delete osStorageToRelease; } } @@ -406,7 +407,7 @@ MemoryManager::AllocationStatus WddmMemoryManager::populateOsHandles(OsHandleSto } for (uint32_t i = 0; i < allocatedFragmentsCounter; i++) { - hostPtrManager.storeFragment(handleStorage.fragmentStorageData[allocatedFragmentIndexes[i]]); + hostPtrManager->storeFragment(handleStorage.fragmentStorageData[allocatedFragmentIndexes[i]]); } return AllocationStatus::Success; @@ -442,7 +443,7 @@ void WddmMemoryManager::cleanOsHandles(OsHandleStorage &handleStorage) { void WddmMemoryManager::obtainGpuAddresFromFragments(WddmAllocation *allocation, OsHandleStorage &handleStorage) { if (this->force32bitAllocations && (handleStorage.fragmentCount > 0)) { auto hostPtr = allocation->getUnderlyingBuffer(); - auto fragment = hostPtrManager.getFragment(hostPtr); + auto fragment = hostPtrManager->getFragment(hostPtr); if (fragment && fragment->driverAllocation) { auto gpuPtr = handleStorage.fragmentStorageData[0].osHandleStorage->gpuPtr; for (uint32_t i = 1; i < handleStorage.fragmentCount; i++) { diff --git a/unit_tests/event/event_tests.cpp b/unit_tests/event/event_tests.cpp index f29d1976d6..9a348a54d1 100644 --- a/unit_tests/event/event_tests.cpp +++ b/unit_tests/event/event_tests.cpp @@ -23,6 +23,7 @@ #include "unit_tests/mocks/mock_event.h" #include "unit_tests/mocks/mock_kernel.h" #include "unit_tests/mocks/mock_mdi.h" +#include "unit_tests/mocks/mock_memory_manager.h" #include "unit_tests/mocks/mock_program.h" #include "unit_tests/os_interface/mock_performance_counters.h" #include @@ -382,22 +383,41 @@ TEST_F(EventTest, Event_Wait_NonBlocking) { EXPECT_FALSE(result); } -TEST_F(EventTest, givenEventContainingCommandQueueWhenItsStatusIsUpdatedToCompletedThenTemporaryAllocationsAreDeleted) { +struct UpdateEventTest : public ::testing::Test { - auto memoryManager = pCmdQ->getDevice().getMemoryManager(); + void SetUp() override { + executionEnvironment = new ExecutionEnvironment; + memoryManager = new MockMemoryManager(*executionEnvironment); + hostPtrManager = static_cast(memoryManager->getHostPtrManager()); + executionEnvironment->memoryManager.reset(memoryManager); + device.reset(Device::create(*platformDevices, executionEnvironment, 0u)); + context = std::make_unique(device.get()); + cl_int retVal = CL_OUT_OF_RESOURCES; + commandQueue.reset(CommandQueue::create(context.get(), device.get(), nullptr, retVal)); + EXPECT_EQ(CL_SUCCESS, retVal); + } + ExecutionEnvironment *executionEnvironment; + MockMemoryManager *memoryManager; + MockHostPtrManager *hostPtrManager; + std::unique_ptr device; + std::unique_ptr context; + std::unique_ptr commandQueue; +}; + +TEST_F(UpdateEventTest, givenEventContainingCommandQueueWhenItsStatusIsUpdatedToCompletedThenTemporaryAllocationsAreDeleted) { void *ptr = (void *)0x1000; size_t size = 4096; auto temporary = memoryManager->allocateGraphicsMemory(size, ptr); temporary->taskCount = 3; memoryManager->storeAllocation(std::unique_ptr(temporary), TEMPORARY_ALLOCATION); - Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, 3); + Event event(commandQueue.get(), CL_COMMAND_NDRANGE_KERNEL, 3, 3); - EXPECT_EQ(1u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(1u, hostPtrManager->getFragmentCount()); event.updateExecutionStatus(); - EXPECT_EQ(0u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(0u, hostPtrManager->getFragmentCount()); } class SurfaceMock : public Surface { diff --git a/unit_tests/mem_obj/buffer_tests.cpp b/unit_tests/mem_obj/buffer_tests.cpp index 0a0c8d2adf..bd4d24b600 100644 --- a/unit_tests/mem_obj/buffer_tests.cpp +++ b/unit_tests/mem_obj/buffer_tests.cpp @@ -245,11 +245,12 @@ TEST(Buffer, givenNullptrPassedToBufferCreateWhenAllocationIsNotSystemMemoryPool cl_int retVal = 0; cl_mem_flags flags = CL_MEM_READ_WRITE; - auto hostPtrAllocationCountBefore = memoryManager->hostPtrManager.getFragmentCount(); + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); + auto hostPtrAllocationCountBefore = hostPtrManager->getFragmentCount(); std::unique_ptr buffer(Buffer::create(&ctx, flags, MemoryConstants::pageSize, nullptr, retVal)); ASSERT_NE(nullptr, buffer.get()); - auto hostPtrAllocationCountAfter = memoryManager->hostPtrManager.getFragmentCount(); + auto hostPtrAllocationCountAfter = hostPtrManager->getFragmentCount(); EXPECT_EQ(hostPtrAllocationCountBefore, hostPtrAllocationCountAfter); } diff --git a/unit_tests/memory_manager/host_ptr_manager_tests.cpp b/unit_tests/memory_manager/host_ptr_manager_tests.cpp index c3780a43fa..845f83c2af 100644 --- a/unit_tests/memory_manager/host_ptr_manager_tests.cpp +++ b/unit_tests/memory_manager/host_ptr_manager_tests.cpp @@ -5,9 +5,12 @@ * */ -#include "gtest/gtest.h" -#include "runtime/memory_manager/host_ptr_manager.h" +#include "runtime/helpers/aligned_memory.h" #include "runtime/helpers/ptr_math.h" +#include "runtime/memory_manager/memory_constants.h" +#include "unit_tests/fixtures/memory_manager_fixture.h" +#include "unit_tests/gen_common/test.h" +#include "unit_tests/mocks/mock_host_ptr_manager.h" using namespace OCLRT; @@ -15,7 +18,7 @@ TEST(HostPtrManager, AlignedPointerAndAlignedSizeAskedForAllocationCountReturnsO auto size = MemoryConstants::pageSize * 10; void *ptr = (void *)0x1000; - AllocationRequirements reqs = HostPtrManager::getAllocationRequirements(ptr, size); + AllocationRequirements reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); EXPECT_EQ(1u, reqs.requiredFragmentsCount); EXPECT_EQ(reqs.AllocationFragments[0].fragmentPosition, FragmentPosition::MIDDLE); @@ -35,7 +38,7 @@ TEST(HostPtrManager, AlignedPointerAndNotAlignedSizeAskedForAllocationCountRetur auto size = MemoryConstants::pageSize * 10 - 1; void *ptr = (void *)0x1000; - AllocationRequirements reqs = HostPtrManager::getAllocationRequirements(ptr, size); + AllocationRequirements reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); EXPECT_EQ(2u, reqs.requiredFragmentsCount); EXPECT_EQ(reqs.AllocationFragments[0].fragmentPosition, FragmentPosition::MIDDLE); @@ -58,7 +61,7 @@ TEST(HostPtrManager, NotAlignedPointerAndNotAlignedSizeAskedForAllocationCountRe auto size = MemoryConstants::pageSize * 10 - 1; void *ptr = (void *)0x1045; - AllocationRequirements reqs = HostPtrManager::getAllocationRequirements(ptr, size); + AllocationRequirements reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); EXPECT_EQ(3u, reqs.requiredFragmentsCount); EXPECT_EQ(reqs.AllocationFragments[0].fragmentPosition, FragmentPosition::LEADING); @@ -85,7 +88,7 @@ TEST(HostPtrManager, NotAlignedPointerAndNotAlignedSizeWithinOnePageAskedForAllo auto size = 200; void *ptr = (void *)0x1045; - AllocationRequirements reqs = HostPtrManager::getAllocationRequirements(ptr, size); + AllocationRequirements reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); EXPECT_EQ(1u, reqs.requiredFragmentsCount); EXPECT_EQ(reqs.AllocationFragments[0].fragmentPosition, FragmentPosition::LEADING); @@ -110,7 +113,7 @@ TEST(HostPtrManager, NotAlignedPointerAndNotAlignedSizeWithinTwoPagesAskedForAll auto size = MemoryConstants::pageSize; void *ptr = (void *)0x1045; - AllocationRequirements reqs = HostPtrManager::getAllocationRequirements(ptr, size); + AllocationRequirements reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); EXPECT_EQ(2u, reqs.requiredFragmentsCount); EXPECT_EQ(reqs.AllocationFragments[0].fragmentPosition, FragmentPosition::LEADING); @@ -136,7 +139,7 @@ TEST(HostPtrManager, AlignedPointerAndAlignedSizeOfOnePageAskedForAllocationCoun auto size = MemoryConstants::pageSize * 10; void *ptr = (void *)0x1000; - AllocationRequirements reqs = HostPtrManager::getAllocationRequirements(ptr, size); + AllocationRequirements reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); EXPECT_EQ(1u, reqs.requiredFragmentsCount); EXPECT_EQ(reqs.AllocationFragments[0].fragmentPosition, FragmentPosition::MIDDLE); @@ -161,7 +164,7 @@ TEST(HostPtrManager, NotAlignedPointerAndSizeThatFitsToPageAskedForAllocationCou auto size = MemoryConstants::pageSize * 10 - 1; void *ptr = (void *)0x1001; - AllocationRequirements reqs = HostPtrManager::getAllocationRequirements(ptr, size); + AllocationRequirements reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); EXPECT_EQ(2u, reqs.requiredFragmentsCount); EXPECT_EQ(reqs.AllocationFragments[0].fragmentPosition, FragmentPosition::LEADING); @@ -187,7 +190,7 @@ TEST(HostPtrManager, AlignedPointerAndPageSizeAskedForAllocationCountRetrunsMidd auto size = MemoryConstants::pageSize; void *ptr = (void *)0x1000; - AllocationRequirements reqs = HostPtrManager::getAllocationRequirements(ptr, size); + AllocationRequirements reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); EXPECT_EQ(1u, reqs.requiredFragmentsCount); EXPECT_EQ(reqs.AllocationFragments[0].fragmentPosition, FragmentPosition::MIDDLE); @@ -211,8 +214,8 @@ TEST(HostPtrManager, AlignedPointerAndPageSizeAskedForAllocationCountRetrunsMidd TEST(HostPtrManager, AllocationRequirementsForMiddleAllocationThatIsNotStoredInManagerAskedForGraphicsAllocationReturnsNotAvailable) { auto size = MemoryConstants::pageSize; void *ptr = (void *)0x1000; - auto reqs = HostPtrManager::getAllocationRequirements(ptr, size); - HostPtrManager hostPtrManager; + auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); + MockHostPtrManager hostPtrManager; auto gpuAllocationFragments = hostPtrManager.populateAlreadyAllocatedFragments(reqs, nullptr); EXPECT_EQ(nullptr, gpuAllocationFragments.fragmentStorageData[0].osHandleStorage); @@ -225,7 +228,7 @@ TEST(HostPtrManager, AllocationRequirementsForMiddleAllocationThatIsNotStoredInM TEST(HostPtrManager, AllocationRequirementsForMiddleAllocationThatIsStoredInManagerAskedForGraphicsAllocationReturnsProperAllocationAndIncreasesRefCount) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; FragmentStorage allocationFragment; auto cpuPtr = (void *)0x1000; auto ptrSize = MemoryConstants::pageSize; @@ -236,7 +239,7 @@ TEST(HostPtrManager, AllocationRequirementsForMiddleAllocationThatIsStoredInMana hostPtrManager.storeFragment(allocationFragment); - auto reqs = HostPtrManager::getAllocationRequirements(cpuPtr, ptrSize); + auto reqs = MockHostPtrManager::getAllocationRequirements(cpuPtr, ptrSize); auto gpuAllocationFragments = hostPtrManager.populateAlreadyAllocatedFragments(reqs, nullptr); @@ -253,7 +256,7 @@ TEST(HostPtrManager, AllocationRequirementsForMiddleAllocationThatIsStoredInMana TEST(HostPtrManager, AllocationRequirementsForAllocationWithinSizeOfStoredAllocationInManagerAskedForGraphicsAllocationReturnsProperAllocation) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; FragmentStorage allocationFragment; auto cpuPtr = (void *)0x1000; auto ptrSize = MemoryConstants::pageSize * 10; @@ -264,7 +267,7 @@ TEST(HostPtrManager, AllocationRequirementsForAllocationWithinSizeOfStoredAlloca hostPtrManager.storeFragment(allocationFragment); - auto reqs = HostPtrManager::getAllocationRequirements(cpuPtr, MemoryConstants::pageSize); + auto reqs = MockHostPtrManager::getAllocationRequirements(cpuPtr, MemoryConstants::pageSize); auto gpuAllocationFragments = hostPtrManager.populateAlreadyAllocatedFragments(reqs, nullptr); @@ -280,7 +283,7 @@ TEST(HostPtrManager, AllocationRequirementsForAllocationWithinSizeOfStoredAlloca } TEST(HostPtrManager, HostPtrAndSizeStoredToHostPtrManagerIncreasesTheContainerCount) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; FragmentStorage allocationFragment; EXPECT_EQ(allocationFragment.fragmentCpuPointer, nullptr); @@ -293,7 +296,7 @@ TEST(HostPtrManager, HostPtrAndSizeStoredToHostPtrManagerIncreasesTheContainerCo } TEST(HostPtrManager, HostPtrAndSizeStoredToHostPtrManagerTwiceReturnsOneAsFragmentCount) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; FragmentStorage allocationFragment; @@ -304,14 +307,14 @@ TEST(HostPtrManager, HostPtrAndSizeStoredToHostPtrManagerTwiceReturnsOneAsFragme } TEST(HostPtrManager, EmptyHostPtrManagerAskedForFragmentReturnsNullptr) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; auto fragment = hostPtrManager.getFragment((void *)0x10121); EXPECT_EQ(nullptr, fragment); EXPECT_EQ(0u, hostPtrManager.getFragmentCount()); } TEST(HostPtrManager, NonEmptyHostPtrManagerAskedForFragmentReturnsProperFragmentWithRefCountOne) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; FragmentStorage fragment; void *cpuPtr = (void *)0x10121; auto fragmentSize = 101u; @@ -330,7 +333,7 @@ TEST(HostPtrManager, NonEmptyHostPtrManagerAskedForFragmentReturnsProperFragment } TEST(HostPtrManager, HostPtrManagerFilledTwiceWithTheSamePointerWhenAskedForFragmentReturnsItWithRefCountSetToTwo) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; FragmentStorage fragment; void *cpuPtr = (void *)0x10121; auto fragmentSize = 101u; @@ -350,7 +353,7 @@ TEST(HostPtrManager, HostPtrManagerFilledTwiceWithTheSamePointerWhenAskedForFrag } TEST(HostPtrManager, GivenHostPtrManagerFilledWithFragmentsWhenFragmentIsBeingReleasedThenManagerMaintainsProperRefferenceCount) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; FragmentStorage fragment; void *cpuPtr = (void *)0x1000; auto fragmentSize = MemoryConstants::pageSize; @@ -392,7 +395,7 @@ TEST(HostPtrManager, GivenOsHandleStorageWhenAskedToStoreTheFragmentThenFragment storage.fragmentStorageData[1].cpuPtr = cpu2; storage.fragmentStorageData[1].fragmentSize = size2; - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; EXPECT_EQ(0u, hostPtrManager.getFragmentCount()); @@ -410,7 +413,7 @@ TEST(HostPtrManager, GivenHostPtrFilledWith3TripleFragmentsWhenAskedForPopulatio void *cpuPtr = (void *)0x1001; auto fragmentSize = MemoryConstants::pageSize * 10; - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; auto reqs = hostPtrManager.getAllocationRequirements(cpuPtr, fragmentSize); ASSERT_EQ(3u, reqs.requiredFragmentsCount); @@ -497,7 +500,7 @@ TEST(HostPtrManager, FragmentFindWhenFragmentSizeIsZero) { } TEST(HostPtrManager, FragmentFindWhenFragmentSizeIsNotZero) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; auto size1 = MemoryConstants::pageSize; @@ -544,7 +547,7 @@ TEST(HostPtrManager, FragmentFindWhenFragmentSizeIsNotZero) { } TEST(HostPtrManager, FragmentCheck) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; auto size1 = MemoryConstants::pageSize; @@ -621,7 +624,7 @@ TEST(HostPtrManager, GivenHostPtrManagerFilledWithBigFragmentWhenAskedForFragmne FragmentStorage fragment; fragment.fragmentCpuPointer = bigPtr; fragment.fragmentSize = bigSize; - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; hostPtrManager.storeFragment(fragment); EXPECT_EQ(1u, hostPtrManager.getFragmentCount()); @@ -663,7 +666,7 @@ TEST(HostPtrManager, GivenHostPtrManagerFilledWithFragmentsWhenCheckedForOverlap FragmentStorage fragment; fragment.fragmentCpuPointer = bigPtr; fragment.fragmentSize = bigSize; - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; hostPtrManager.storeFragment(fragment); EXPECT_EQ(1u, hostPtrManager.getFragmentCount()); @@ -688,7 +691,7 @@ TEST(HostPtrManager, GivenHostPtrManagerFilledWithFragmentsWhenCheckedForOverlap EXPECT_EQ(nullptr, fragment4); } TEST(HostPtrManager, GivenEmptyHostPtrManagerWhenAskedForOverlapingThenNoOverlappingIsReturned) { - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; auto bigPtr = (void *)0x04000; auto bigSize = 10 * MemoryConstants::pageSize; @@ -705,7 +708,7 @@ TEST(HostPtrManager, GivenHostPtrManagerFilledWithFragmentsWhenAskedForOverlpain FragmentStorage fragment; fragment.fragmentCpuPointer = bigPtr1; fragment.fragmentSize = bigSize; - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; hostPtrManager.storeFragment(fragment); fragment.fragmentCpuPointer = bigPtr2; hostPtrManager.storeFragment(fragment); @@ -743,7 +746,7 @@ TEST(HostPtrManager, GivenHostPtrManagerFilledWithFragmentsWhenAskedForOverlapin FragmentStorage fragment; fragment.fragmentCpuPointer = bigPtr1; fragment.fragmentSize = bigSize1; - HostPtrManager hostPtrManager; + MockHostPtrManager hostPtrManager; hostPtrManager.storeFragment(fragment); fragment.fragmentCpuPointer = bigPtr2; fragment.fragmentSize = bigSize2; @@ -770,3 +773,38 @@ TEST(HostPtrManager, GivenHostPtrManagerFilledWithFragmentsWhenAskedForOverlapin EXPECT_EQ(OverlapStatus::FRAGMENT_WITHIN_STORED_FRAGMENT, overlapStatus); EXPECT_NE(nullptr, fragment3); } + +using HostPtrAllocationTest = Test; + +TEST_F(HostPtrAllocationTest, givenTwoAllocationsThatSharesOneFragmentWhenOneIsDestroyedThenFragmentRemains) { + + void *cpuPtr1 = reinterpret_cast(0x100001); + void *cpuPtr2 = ptrOffset(cpuPtr1, MemoryConstants::pageSize); + + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); + auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(2 * MemoryConstants::pageSize - 1, cpuPtr1); + EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); + + auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr2); + EXPECT_EQ(3u, hostPtrManager->getFragmentCount()); + memoryManager->freeGraphicsMemory(graphicsAllocation1); + EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); + memoryManager->freeGraphicsMemory(graphicsAllocation2); + EXPECT_EQ(0u, hostPtrManager->getFragmentCount()); +} + +TEST_F(HostPtrAllocationTest, whenPrepareOsHandlesForAllocationThenPopulateAsManyFragmentsAsRequired) { + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); + void *cpuPtr = reinterpret_cast(0x100001); + size_t allocationSize = MemoryConstants::pageSize / 2; + for (uint32_t expectedFragmentCount = 1; expectedFragmentCount <= 3; expectedFragmentCount++, allocationSize += MemoryConstants::pageSize) { + auto requirements = hostPtrManager->getAllocationRequirements(cpuPtr, allocationSize); + EXPECT_EQ(expectedFragmentCount, requirements.requiredFragmentsCount); + auto osStorage = hostPtrManager->prepareOsStorageForAllocation(*memoryManager, allocationSize, cpuPtr); + EXPECT_EQ(expectedFragmentCount, osStorage.fragmentCount); + EXPECT_EQ(expectedFragmentCount, hostPtrManager->getFragmentCount()); + hostPtrManager->releaseHandleStorage(osStorage); + memoryManager->cleanOsHandles(osStorage); + EXPECT_EQ(0u, hostPtrManager->getFragmentCount()); + } +} \ No newline at end of file diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index 9eede837a5..6d2cd39b9b 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -154,7 +154,7 @@ TEST_F(MemoryAllocatorTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationToH GraphicsAllocation gfxAllocation(cpuPtr, size); memoryManager->addAllocationToHostPtrManager(&gfxAllocation); - auto fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + auto fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_NE(fragment, nullptr); EXPECT_TRUE(fragment->driverAllocation); EXPECT_EQ(fragment->refCount, 1); @@ -165,22 +165,22 @@ TEST_F(MemoryAllocatorTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationToH FragmentStorage fragmentStorage = {}; fragmentStorage.fragmentCpuPointer = cpuPtr; - memoryManager->hostPtrManager.storeFragment(fragmentStorage); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + memoryManager->getHostPtrManager()->storeFragment(fragmentStorage); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 2); fragment->driverAllocation = false; memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 2); fragment->driverAllocation = true; memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 1); memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment, nullptr); } @@ -467,18 +467,19 @@ TEST_F(MemoryAllocatorTest, givenInternalAllocationWhenItIsPutOnReusableListWhen TEST_F(MemoryAllocatorTest, AlignedHostPtrWithAlignedSizeWhenAskedForGraphicsAllocationReturnsNullStorageFromHostPtrManager) { auto ptr = (void *)0x1000; - auto graphicsAllocation = memoryManager->allocateGraphicsMemory(4096, ptr); + MockMemoryManager mockMemoryManager(*executionEnvironment); + auto hostPtrManager = static_cast(mockMemoryManager.getHostPtrManager()); + auto graphicsAllocation = mockMemoryManager.allocateGraphicsMemory(4096, ptr); EXPECT_NE(nullptr, graphicsAllocation); - auto &hostPtrManager = memoryManager->hostPtrManager; - EXPECT_EQ(1u, hostPtrManager.getFragmentCount()); - auto fragmentData = hostPtrManager.getFragment(ptr); + EXPECT_EQ(1u, hostPtrManager->getFragmentCount()); + auto fragmentData = hostPtrManager->getFragment(ptr); ASSERT_NE(nullptr, fragmentData); EXPECT_NE(nullptr, fragmentData->osInternalStorage); - memoryManager->freeGraphicsMemory(graphicsAllocation); + mockMemoryManager.freeGraphicsMemory(graphicsAllocation); } TEST_F(MemoryAllocatorTest, GivenAlignedHostPtrAndCacheAlignedSizeWhenAskedForL3AllowanceThenTrueIsReturned) { @@ -533,7 +534,7 @@ TEST_F(MemoryAllocatorTest, NullOsHandleStorageAskedForPopulationReturnsFilledPo EXPECT_NE(nullptr, storage.fragmentStorageData[0].osHandleStorage); EXPECT_EQ(nullptr, storage.fragmentStorageData[1].osHandleStorage); EXPECT_EQ(nullptr, storage.fragmentStorageData[2].osHandleStorage); - memoryManager->hostPtrManager.releaseHandleStorage(storage); + memoryManager->getHostPtrManager()->releaseHandleStorage(storage); memoryManager->cleanOsHandles(storage); } @@ -541,20 +542,22 @@ TEST_F(MemoryAllocatorTest, GivenEmptyMemoryManagerAndMisalingedHostPtrWithHugeS void *cpuPtr = (void *)0x1005; auto size = MemoryConstants::pageSize * 10 - 1; - auto reqs = HostPtrManager::getAllocationRequirements(cpuPtr, size); + MockMemoryManager mockMemoryManager(*executionEnvironment); + auto hostPtrManager = static_cast(mockMemoryManager.getHostPtrManager()); + auto reqs = MockHostPtrManager::getAllocationRequirements(cpuPtr, size); ASSERT_EQ(3u, reqs.requiredFragmentsCount); - auto graphicsAllocation = memoryManager->allocateGraphicsMemory(size, cpuPtr); + auto graphicsAllocation = mockMemoryManager.allocateGraphicsMemory(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); EXPECT_EQ(reqs.AllocationFragments[i].allocationSize, graphicsAllocation->fragmentsStorage.fragmentStorageData[i].fragmentSize); } - EXPECT_EQ(3u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(3u, hostPtrManager->getFragmentCount()); EXPECT_EQ(Sharing::nonSharedResource, graphicsAllocation->peekSharedHandle()); - memoryManager->freeGraphicsMemory(graphicsAllocation); + mockMemoryManager.freeGraphicsMemory(graphicsAllocation); } TEST_F(MemoryAllocatorTest, GivenPointerAndSizeWhenAskedToCreateGrahicsAllocationThenGraphicsAllocationIsCreated) { @@ -1350,11 +1353,12 @@ TEST_F(MemoryManagerWithCsrTest, GivenAllocationsInHostPtrManagerWhenBiggerOverl void *cpuPtr2 = (void *)0x101008; void *cpuPtr3 = (void *)0x100000; + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); - EXPECT_EQ(2u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize * 3, cpuPtr2); - EXPECT_EQ(4u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(4u, hostPtrManager->getFragmentCount()); GraphicsAllocation *graphicsAllocation3 = nullptr; @@ -1386,29 +1390,30 @@ TEST_F(MemoryManagerWithCsrTest, GivenAllocationsInHostPtrManagerReadyForCleanin void *cpuPtr2 = (void *)0x101008; void *cpuPtr3 = (void *)0x100000; + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); - EXPECT_EQ(2u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize * 3, cpuPtr2); - EXPECT_EQ(4u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(4u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); EXPECT_NE(nullptr, graphicsAllocation2); - auto fragment1 = memoryManager->hostPtrManager.getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); + auto fragment1 = hostPtrManager->getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment1); - auto fragment2 = memoryManager->hostPtrManager.getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); + auto fragment2 = hostPtrManager->getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment2); - auto fragment3 = memoryManager->hostPtrManager.getFragment(alignDown(cpuPtr2, MemoryConstants::pageSize)); + auto fragment3 = hostPtrManager->getFragment(alignDown(cpuPtr2, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment3); - auto fragment4 = memoryManager->hostPtrManager.getFragment(alignUp(cpuPtr2, MemoryConstants::pageSize)); + auto fragment4 = hostPtrManager->getFragment(alignUp(cpuPtr2, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment4); uint32_t taskCountReady = 1; memoryManager->storeAllocation(std::unique_ptr(graphicsAllocation1), TEMPORARY_ALLOCATION, taskCountReady); memoryManager->storeAllocation(std::unique_ptr(graphicsAllocation2), TEMPORARY_ALLOCATION, taskCountReady); - EXPECT_EQ(4u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(4u, hostPtrManager->getFragmentCount()); // All fragments ready for release taskCount = taskCountReady; @@ -1430,22 +1435,23 @@ TEST_F(MemoryManagerWithCsrTest, checkAllocationsForOverlappingWithoutBiggerOver void *cpuPtr1 = (void *)0x100004; void *cpuPtr2 = (void *)0x101008; + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); - EXPECT_EQ(2u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); auto graphicsAllocation2 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize * 3, cpuPtr2); - EXPECT_EQ(4u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(4u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); EXPECT_NE(nullptr, graphicsAllocation2); - auto fragment1 = memoryManager->hostPtrManager.getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); + auto fragment1 = hostPtrManager->getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment1); - auto fragment2 = memoryManager->hostPtrManager.getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); + auto fragment2 = hostPtrManager->getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment2); - auto fragment3 = memoryManager->hostPtrManager.getFragment(alignDown(cpuPtr2, MemoryConstants::pageSize)); + auto fragment3 = hostPtrManager->getFragment(alignDown(cpuPtr2, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment3); - auto fragment4 = memoryManager->hostPtrManager.getFragment(alignUp(cpuPtr2, MemoryConstants::pageSize)); + auto fragment4 = hostPtrManager->getFragment(alignUp(cpuPtr2, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment4); AllocationRequirements requirements; @@ -1462,7 +1468,7 @@ TEST_F(MemoryManagerWithCsrTest, checkAllocationsForOverlappingWithoutBiggerOver requirements.AllocationFragments[1].allocationSize = MemoryConstants::pageSize; requirements.AllocationFragments[1].fragmentPosition = FragmentPosition::TRAILING; - RequirementsStatus status = memoryManager->hostPtrManager.checkAllocationsForOverlapping(*memoryManager, &requirements, &checkedFragments); + RequirementsStatus status = hostPtrManager->checkAllocationsForOverlapping(*memoryManager, &requirements, &checkedFragments); EXPECT_EQ(RequirementsStatus::SUCCESS, status); EXPECT_EQ(2u, checkedFragments.count); @@ -1486,13 +1492,14 @@ TEST_F(MemoryManagerWithCsrTest, checkAllocationsForOverlappingWithBiggerOverlap void *cpuPtr1 = (void *)0x100004; auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); - EXPECT_EQ(2u, memoryManager->hostPtrManager.getFragmentCount()); + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); + EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); - auto fragment1 = memoryManager->hostPtrManager.getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); + auto fragment1 = hostPtrManager->getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment1); - auto fragment2 = memoryManager->hostPtrManager.getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); + auto fragment2 = hostPtrManager->getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment2); uint32_t taskCountReady = 1; @@ -1512,7 +1519,7 @@ TEST_F(MemoryManagerWithCsrTest, checkAllocationsForOverlappingWithBiggerOverlap requirements.AllocationFragments[0].allocationSize = MemoryConstants::pageSize * 10; requirements.AllocationFragments[0].fragmentPosition = FragmentPosition::NONE; - RequirementsStatus status = memoryManager->hostPtrManager.checkAllocationsForOverlapping(*memoryManager, &requirements, &checkedFragments); + RequirementsStatus status = hostPtrManager->checkAllocationsForOverlapping(*memoryManager, &requirements, &checkedFragments); EXPECT_EQ(RequirementsStatus::SUCCESS, status); EXPECT_EQ(1u, checkedFragments.count); @@ -1529,14 +1536,15 @@ TEST_F(MemoryManagerWithCsrTest, checkAllocationsForOverlappingWithBiggerOverlap void *cpuPtr1 = (void *)0x100004; + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); - EXPECT_EQ(2u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); - auto fragment1 = memoryManager->hostPtrManager.getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); + auto fragment1 = hostPtrManager->getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment1); - auto fragment2 = memoryManager->hostPtrManager.getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); + auto fragment2 = hostPtrManager->getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment2); uint32_t taskCountReady = 2; @@ -1568,7 +1576,7 @@ TEST_F(MemoryManagerWithCsrTest, checkAllocationsForOverlappingWithBiggerOverlap EXPECT_CALL(*gmockMemoryManager, cleanAllocationList(::testing::_, ::testing::_)).Times(2).WillOnce(::testing::Invoke(cleanAllocations)).WillOnce(::testing::Invoke(cleanAllocationsWithTaskCount)); - RequirementsStatus status = memoryManager->hostPtrManager.checkAllocationsForOverlapping(*memoryManager, &requirements, &checkedFragments); + RequirementsStatus status = hostPtrManager->checkAllocationsForOverlapping(*memoryManager, &requirements, &checkedFragments); EXPECT_EQ(RequirementsStatus::SUCCESS, status); EXPECT_EQ(1u, checkedFragments.count); @@ -1585,14 +1593,15 @@ TEST_F(MemoryManagerWithCsrTest, checkAllocationsForOverlappingWithBiggerOverlap void *cpuPtr1 = (void *)0x100004; + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); auto graphicsAllocation1 = memoryManager->allocateGraphicsMemory(MemoryConstants::pageSize, cpuPtr1); - EXPECT_EQ(2u, memoryManager->hostPtrManager.getFragmentCount()); + EXPECT_EQ(2u, hostPtrManager->getFragmentCount()); EXPECT_NE(nullptr, graphicsAllocation1); - auto fragment1 = memoryManager->hostPtrManager.getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); + auto fragment1 = hostPtrManager->getFragment(alignDown(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment1); - auto fragment2 = memoryManager->hostPtrManager.getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); + auto fragment2 = hostPtrManager->getFragment(alignUp(cpuPtr1, MemoryConstants::pageSize)); EXPECT_NE(nullptr, fragment2); uint32_t taskCountReady = 2; @@ -1620,7 +1629,7 @@ TEST_F(MemoryManagerWithCsrTest, checkAllocationsForOverlappingWithBiggerOverlap EXPECT_CALL(*gmockMemoryManager, cleanAllocationList(::testing::_, ::testing::_)).Times(2).WillRepeatedly(::testing::Invoke(cleanAllocations)); - RequirementsStatus status = memoryManager->hostPtrManager.checkAllocationsForOverlapping(*memoryManager, &requirements, &checkedFragments); + RequirementsStatus status = hostPtrManager->checkAllocationsForOverlapping(*memoryManager, &requirements, &checkedFragments); EXPECT_EQ(RequirementsStatus::FATAL, status); EXPECT_EQ(1u, checkedFragments.count); diff --git a/unit_tests/mocks/CMakeLists.txt b/unit_tests/mocks/CMakeLists.txt index f3782f023c..3939eeddf5 100644 --- a/unit_tests/mocks/CMakeLists.txt +++ b/unit_tests/mocks/CMakeLists.txt @@ -42,6 +42,7 @@ set(IGDRCL_SRCS_tests_mocks ${CMAKE_CURRENT_SOURCE_DIR}${IGDRCL__INSTRUMENTATION_DIR_SUFFIX}/mock_instrumentation.cpp ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/mock_gmm_client_context.cpp ${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/mock_gmm_client_context.h + ${CMAKE_CURRENT_SOURCE_DIR}/mock_host_ptr_manager.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_image.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_kernel.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_kernel.h diff --git a/unit_tests/mocks/linux/mock_drm_memory_manager.h b/unit_tests/mocks/linux/mock_drm_memory_manager.h index 1ec231d020..7bffa83c48 100644 --- a/unit_tests/mocks/linux/mock_drm_memory_manager.h +++ b/unit_tests/mocks/linux/mock_drm_memory_manager.h @@ -6,6 +6,7 @@ */ #include "runtime/os_interface/linux/drm_memory_manager.h" +#include "unit_tests/mocks/mock_host_ptr_manager.h" namespace OCLRT { static off_t lseekReturn = 4096u; @@ -47,6 +48,7 @@ class TestedDrmMemoryManager : public DrmMemoryManager { lseekCalledCount = 0; mmapMockCallCount = 0; munmapMockCallCount = 0; + hostPtrManager.reset(new MockHostPtrManager); }; TestedDrmMemoryManager(Drm *drm, bool allowForcePin, bool validateHostPtrMemory, ExecutionEnvironment &executionEnvironment) : DrmMemoryManager(drm, gemCloseWorkerMode::gemCloseWorkerInactive, allowForcePin, validateHostPtrMemory, executionEnvironment) { this->lseekFunction = &lseekMock; diff --git a/unit_tests/mocks/mock_host_ptr_manager.h b/unit_tests/mocks/mock_host_ptr_manager.h new file mode 100644 index 0000000000..0f5c925049 --- /dev/null +++ b/unit_tests/mocks/mock_host_ptr_manager.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2017-2018 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "runtime/memory_manager/host_ptr_manager.h" + +namespace OCLRT { +class MockHostPtrManager : public HostPtrManager { + public: + using HostPtrManager::checkAllocationsForOverlapping; + using HostPtrManager::getAllocationRequirements; + using HostPtrManager::getFragmentAndCheckForOverlaps; + using HostPtrManager::populateAlreadyAllocatedFragments; + size_t getFragmentCount() { return partialAllocations.size(); } +}; +} // namespace OCLRT diff --git a/unit_tests/mocks/mock_memory_manager.h b/unit_tests/mocks/mock_memory_manager.h index 8c74887a31..db581c7595 100644 --- a/unit_tests/mocks/mock_memory_manager.h +++ b/unit_tests/mocks/mock_memory_manager.h @@ -9,6 +9,8 @@ #include "runtime/execution_environment/execution_environment.h" #include "runtime/memory_manager/os_agnostic_memory_manager.h" +#include "unit_tests/mocks/mock_host_ptr_manager.h" + #include "gmock/gmock.h" namespace OCLRT { @@ -20,7 +22,9 @@ class MockMemoryManager : public OsAgnosticMemoryManager { using MemoryManager::getAllocationData; using MemoryManager::timestampPacketAllocator; using OsAgnosticMemoryManager::OsAgnosticMemoryManager; - MockMemoryManager(ExecutionEnvironment &executionEnvironment) : OsAgnosticMemoryManager(false, false, executionEnvironment){}; + MockMemoryManager(ExecutionEnvironment &executionEnvironment) : OsAgnosticMemoryManager(false, false, executionEnvironment) { + hostPtrManager.reset(new MockHostPtrManager); + }; MockMemoryManager() : MockMemoryManager(*(new ExecutionEnvironment)) { mockExecutionEnvironment.reset(&executionEnvironment); }; 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 5c7bd48347..3f158b9bfb 100644 --- a/unit_tests/os_interface/linux/drm_command_stream_tests.cpp +++ b/unit_tests/os_interface/linux/drm_command_stream_tests.cpp @@ -23,6 +23,7 @@ #include "unit_tests/gen_common/gen_cmd_parse.h" #include "unit_tests/helpers/hw_parse.h" #include "unit_tests/mocks/mock_program.h" +#include "unit_tests/mocks/mock_host_ptr_manager.h" #include "unit_tests/mocks/mock_submissions_aggregator.h" #include "unit_tests/os_interface/linux/device_command_stream_fixture.h" #include "test.h" @@ -1194,11 +1195,9 @@ TEST_F(DrmCommandStreamLeaksTest, makeResidentTwice) { TEST_F(DrmCommandStreamLeaksTest, makeResidentTwiceWhenFragmentStorage) { auto ptr = (void *)0x1001; auto size = MemoryConstants::pageSize * 10; - auto reqs = HostPtrManager::getAllocationRequirements(ptr, size); + auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); auto allocation = mm->allocateGraphicsMemory(size, ptr); - auto &hostPtrManager = mm->hostPtrManager; - EXPECT_EQ(3u, hostPtrManager.getFragmentCount()); ASSERT_EQ(3u, allocation->fragmentsStorage.fragmentCount); csr->makeResident(*allocation); @@ -1238,9 +1237,6 @@ TEST_F(DrmCommandStreamLeaksTest, givenFragmentedAllocationsWithResuedFragmentsW auto graphicsAllocation2 = mm->allocateGraphicsMemory(size2, offsetedPtr); - auto &hostPtrManager = mm->hostPtrManager; - ASSERT_EQ(3u, hostPtrManager.getFragmentCount()); - //graphicsAllocation2 reuses one fragment from graphicsAllocation EXPECT_EQ(graphicsAllocation->fragmentsStorage.fragmentStorageData[2].residency, graphicsAllocation2->fragmentsStorage.fragmentStorageData[0].residency); @@ -1297,13 +1293,10 @@ TEST_F(DrmCommandStreamLeaksTest, GivenAllocationCreatedFromThreeFragmentsWhenMa auto ptr = (void *)0x1001; auto size = MemoryConstants::pageSize * 10; - auto reqs = HostPtrManager::getAllocationRequirements(ptr, size); + auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); auto allocation = mm->allocateGraphicsMemory(size, ptr); - auto &hostPtrManager = mm->hostPtrManager; - - EXPECT_EQ(3u, hostPtrManager.getFragmentCount()); ASSERT_EQ(3u, allocation->fragmentsStorage.fragmentCount); csr->makeResident(*allocation); @@ -1334,13 +1327,10 @@ TEST_F(DrmCommandStreamLeaksTest, GivenAllocationsContainingDifferentCountOfFrag auto size = MemoryConstants::pageSize; auto size2 = 100; - auto reqs = HostPtrManager::getAllocationRequirements(ptr, size); + auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); auto allocation = mm->allocateGraphicsMemory(size, ptr); - auto &hostPtrManager = mm->hostPtrManager; - - EXPECT_EQ(2u, hostPtrManager.getFragmentCount()); ASSERT_EQ(2u, allocation->fragmentsStorage.fragmentCount); ASSERT_EQ(2u, reqs.requiredFragmentsCount); @@ -1367,12 +1357,9 @@ TEST_F(DrmCommandStreamLeaksTest, GivenAllocationsContainingDifferentCountOfFrag mm->freeGraphicsMemory(allocation); csr->getResidencyAllocations().clear(); - EXPECT_EQ(0u, hostPtrManager.getFragmentCount()); - auto allocation2 = mm->allocateGraphicsMemory(size2, ptr); - reqs = HostPtrManager::getAllocationRequirements(ptr, size2); + reqs = MockHostPtrManager::getAllocationRequirements(ptr, size2); - EXPECT_EQ(1u, hostPtrManager.getFragmentCount()); ASSERT_EQ(1u, allocation2->fragmentsStorage.fragmentCount); ASSERT_EQ(1u, reqs.requiredFragmentsCount); @@ -1397,7 +1384,6 @@ TEST_F(DrmCommandStreamLeaksTest, GivenAllocationsContainingDifferentCountOfFrag EXPECT_EQ(1u, allocation2->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo->getRefCount()); } mm->freeGraphicsMemory(allocation2); - EXPECT_EQ(0u, hostPtrManager.getFragmentCount()); } TEST_F(DrmCommandStreamLeaksTest, GivenTwoAllocationsWhenBackingStorageIsTheSameThenMakeResidentShouldAddOnlyOneLocation) { 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 4a5530a50e..cf4340d28b 100644 --- a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp @@ -14,6 +14,7 @@ #include "runtime/helpers/timestamp_packet.h" #include "runtime/mem_obj/buffer.h" #include "runtime/mem_obj/image.h" +#include "runtime/memory_manager/host_ptr_manager.h" #include "runtime/os_interface/linux/allocator_helper.h" #include "runtime/os_interface/linux/drm_allocation.h" #include "runtime/os_interface/linux/drm_buffer_object.h" @@ -116,7 +117,7 @@ TEST_F(DrmMemoryManagerTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationTo DrmAllocation gfxAllocation(nullptr, cpuPtr, size, MemoryPool::MemoryNull); memoryManager->addAllocationToHostPtrManager(&gfxAllocation); - auto fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + auto fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_NE(fragment, nullptr); EXPECT_TRUE(fragment->driverAllocation); EXPECT_EQ(fragment->refCount, 1); @@ -129,22 +130,22 @@ TEST_F(DrmMemoryManagerTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationTo FragmentStorage fragmentStorage = {}; fragmentStorage.fragmentCpuPointer = cpuPtr; - memoryManager->hostPtrManager.storeFragment(fragmentStorage); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + memoryManager->getHostPtrManager()->storeFragment(fragmentStorage); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 2); fragment->driverAllocation = false; memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 2); fragment->driverAllocation = true; memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 1); memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment, nullptr); } @@ -658,11 +659,11 @@ TEST_F(DrmMemoryManagerTest, GivenMisalignedHostPtrAndMultiplePagesSizeWhenAsked auto size = MemoryConstants::pageSize * 10; auto graphicsAllocation = memoryManager->allocateGraphicsMemory(size, ptr); - auto &hostPtrManager = memoryManager->hostPtrManager; + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); - ASSERT_EQ(3u, hostPtrManager.getFragmentCount()); + ASSERT_EQ(3u, hostPtrManager->getFragmentCount()); - auto reqs = HostPtrManager::getAllocationRequirements(ptr, size); + auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); for (int i = 0; i < maxFragmentsCount; i++) { ASSERT_NE(nullptr, graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo); @@ -671,7 +672,7 @@ TEST_F(DrmMemoryManagerTest, GivenMisalignedHostPtrAndMultiplePagesSizeWhenAsked EXPECT_FALSE(graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo->peekIsAllocated()); } memoryManager->freeGraphicsMemory(graphicsAllocation); - EXPECT_EQ(0u, hostPtrManager.getFragmentCount()); + EXPECT_EQ(0u, hostPtrManager->getFragmentCount()); } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, testProfilingAllocatorCleanup) { @@ -2655,9 +2656,11 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMem mock->testIoctls(); - EXPECT_EQ(0u, testedMemoryManager->hostPtrManager.getFragmentCount()); - EXPECT_EQ(nullptr, testedMemoryManager->hostPtrManager.getFragment(handleStorage.fragmentStorageData[1].cpuPtr)); - EXPECT_EQ(nullptr, testedMemoryManager->hostPtrManager.getFragment(handleStorage.fragmentStorageData[2].cpuPtr)); + auto hostPtrManager = static_cast(testedMemoryManager->getHostPtrManager()); + + EXPECT_EQ(0u, hostPtrManager->getFragmentCount()); + EXPECT_EQ(nullptr, hostPtrManager->getFragment(handleStorage.fragmentStorageData[1].cpuPtr)); + EXPECT_EQ(nullptr, hostPtrManager->getFragment(handleStorage.fragmentStorageData[2].cpuPtr)); handleStorage.fragmentStorageData[0].freeTheFragment = false; handleStorage.fragmentStorageData[1].freeTheFragment = true; @@ -2684,8 +2687,9 @@ TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenEnabledValidateHostMem mock->testIoctls(); - EXPECT_EQ(1u, testedMemoryManager->hostPtrManager.getFragmentCount()); - EXPECT_NE(nullptr, testedMemoryManager->hostPtrManager.getFragment(handleStorage.fragmentStorageData[0].cpuPtr)); + auto hostPtrManager = static_cast(testedMemoryManager->getHostPtrManager()); + EXPECT_EQ(1u, hostPtrManager->getFragmentCount()); + EXPECT_NE(nullptr, hostPtrManager->getFragment(handleStorage.fragmentStorageData[0].cpuPtr)); handleStorage.fragmentStorageData[0].freeTheFragment = true; testedMemoryManager->cleanOsHandles(handleStorage); 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 c1ac3190f8..1c49c30ff9 100644 --- a/unit_tests/os_interface/windows/device_command_stream_tests.cpp +++ b/unit_tests/os_interface/windows/device_command_stream_tests.cpp @@ -644,17 +644,17 @@ TEST_F(WddmCommandStreamTest, givenTwoTemporaryAllocationsWhenCleanTemporaryAllo // graphicsAllocation2 still lives EXPECT_EQ(host_ptr2, graphicsAllocation2->getUnderlyingBuffer()); - auto &hostPtrManager = memoryManager->hostPtrManager; + auto hostPtrManager = memoryManager->getHostPtrManager(); auto alignedPtr = alignDown(host_ptr, MemoryConstants::pageSize); auto alignedPtr2 = alignDown(host_ptr2, MemoryConstants::pageSize); - auto fragment = hostPtrManager.getFragment(alignedPtr2); + auto fragment = hostPtrManager->getFragment(alignedPtr2); ASSERT_NE(nullptr, fragment); EXPECT_EQ(alignedPtr2, fragment->fragmentCpuPointer); - auto fragment2 = hostPtrManager.getFragment(alignedPtr); + auto fragment2 = hostPtrManager->getFragment(alignedPtr); EXPECT_EQ(nullptr, fragment2); // destroy remaining allocation csr->waitForTaskCountAndCleanAllocationList(100, TEMPORARY_ALLOCATION); diff --git a/unit_tests/os_interface/windows/mock_wddm_memory_manager.h b/unit_tests/os_interface/windows/mock_wddm_memory_manager.h index 9c53584ffa..f75ad55e38 100644 --- a/unit_tests/os_interface/windows/mock_wddm_memory_manager.h +++ b/unit_tests/os_interface/windows/mock_wddm_memory_manager.h @@ -8,6 +8,7 @@ #pragma once #include "runtime/memory_manager/deferred_deleter.h" #include "runtime/os_interface/windows/wddm_memory_manager.h" +#include "unit_tests/mocks/mock_host_ptr_manager.h" namespace OCLRT { class MockWddmMemoryManager : public WddmMemoryManager { @@ -20,7 +21,9 @@ class MockWddmMemoryManager : public WddmMemoryManager { using BaseClass::trimResidencyToBudget; using BaseClass::WddmMemoryManager; - MockWddmMemoryManager(Wddm *wddm, ExecutionEnvironment &executionEnvironment) : WddmMemoryManager(false, false, wddm, executionEnvironment){}; + MockWddmMemoryManager(Wddm *wddm, ExecutionEnvironment &executionEnvironment) : WddmMemoryManager(false, false, wddm, executionEnvironment) { + hostPtrManager.reset(new MockHostPtrManager); + }; void setDeferredDeleter(DeferredDeleter *deleter) { this->deferredDeleter.reset(deleter); } 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 ec48fb92c4..c247e53840 100644 --- a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp @@ -237,7 +237,7 @@ TEST_F(WddmMemoryManagerTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationT WddmAllocation gfxAllocation(cpuPtr, size, nullptr, MemoryPool::MemoryNull, memoryManager->getOsContextCount()); memoryManager->addAllocationToHostPtrManager(&gfxAllocation); - auto fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + auto fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_NE(fragment, nullptr); EXPECT_TRUE(fragment->driverAllocation); EXPECT_EQ(fragment->refCount, 1); @@ -251,22 +251,22 @@ TEST_F(WddmMemoryManagerTest, GivenGraphicsAllocationWhenAddAndRemoveAllocationT FragmentStorage fragmentStorage = {}; fragmentStorage.fragmentCpuPointer = cpuPtr; - memoryManager->hostPtrManager.storeFragment(fragmentStorage); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + memoryManager->getHostPtrManager()->storeFragment(fragmentStorage); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 2); fragment->driverAllocation = false; memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 2); fragment->driverAllocation = true; memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment->refCount, 1); memoryManager->removeAllocationFromHostPtrManager(&gfxAllocation); - fragment = memoryManager->hostPtrManager.getFragment(gfxAllocation.getUnderlyingBuffer()); + fragment = memoryManager->getHostPtrManager()->getFragment(gfxAllocation.getUnderlyingBuffer()); EXPECT_EQ(fragment, nullptr); } @@ -590,15 +590,15 @@ TEST_F(WddmMemoryManagerTest, AllocateGpuMemHostPtrOffseted) { // Should be same cpu ptr and gpu ptr EXPECT_EQ((char *)ptr + baseOffset, gpuAllocation->getUnderlyingBuffer()); - auto &hostPtrManager = memoryManager->hostPtrManager; + auto hostPtrManager = memoryManager->getHostPtrManager(); - auto fragment = hostPtrManager.getFragment(ptr); + auto fragment = hostPtrManager->getFragment(ptr); ASSERT_NE(nullptr, fragment); EXPECT_TRUE(fragment->refCount == 1); EXPECT_NE(fragment->osInternalStorage, nullptr); // offseted by 3 pages, not in boundary - auto fragment2 = hostPtrManager.getFragment((char *)ptr + 3 * 4096); + auto fragment2 = hostPtrManager->getFragment((char *)ptr + 3 * 4096); EXPECT_EQ(nullptr, fragment2); @@ -608,7 +608,7 @@ TEST_F(WddmMemoryManagerTest, AllocateGpuMemHostPtrOffseted) { // Should be same cpu ptr and gpu ptr EXPECT_EQ(offsetedPtr, gpuAllocation2->getUnderlyingBuffer()); - auto fragment3 = hostPtrManager.getFragment(offsetedPtr); + auto fragment3 = hostPtrManager->getFragment(offsetedPtr); ASSERT_NE(nullptr, fragment3); EXPECT_TRUE(fragment3->refCount == 2); @@ -618,14 +618,14 @@ TEST_F(WddmMemoryManagerTest, AllocateGpuMemHostPtrOffseted) { memoryManager->freeGraphicsMemory(gpuAllocation2); - auto fragment4 = hostPtrManager.getFragment(ptr); + auto fragment4 = hostPtrManager->getFragment(ptr); ASSERT_NE(nullptr, fragment4); EXPECT_TRUE(fragment4->refCount == 1); memoryManager->freeGraphicsMemory(gpuAllocation); - fragment4 = hostPtrManager.getFragment(ptr); + fragment4 = hostPtrManager->getFragment(ptr); EXPECT_EQ(nullptr, fragment4); alignedFree(ptr); @@ -641,9 +641,7 @@ TEST_F(WddmMemoryManagerTest, AllocateGpuMemCheckGmm) { ASSERT_NE(nullptr, gpuAllocation); EXPECT_EQ(ptr, gpuAllocation->getUnderlyingBuffer()); - auto &hostPtrManager = memoryManager->hostPtrManager; - - auto fragment = hostPtrManager.getFragment(ptr); + auto fragment = memoryManager->getHostPtrManager()->getFragment(ptr); ASSERT_NE(nullptr, fragment); EXPECT_TRUE(fragment->refCount == 1); EXPECT_NE(fragment->osInternalStorage->handle, 0); @@ -1396,11 +1394,11 @@ TEST_F(BufferWithWddmMemory, GivenMisalignedHostPtrAndMultiplePagesSizeWhenAsked auto size = MemoryConstants::pageSize * 10; auto graphicsAllocation = memoryManager->allocateGraphicsMemory(size, ptr); - auto &hostPtrManager = memoryManager->hostPtrManager; + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); - ASSERT_EQ(3u, hostPtrManager.getFragmentCount()); + ASSERT_EQ(3u, hostPtrManager->getFragmentCount()); - auto reqs = HostPtrManager::getAllocationRequirements(ptr, size); + auto reqs = MockHostPtrManager::getAllocationRequirements(ptr, size); for (int i = 0; i < maxFragmentsCount; i++) { @@ -1415,7 +1413,7 @@ TEST_F(BufferWithWddmMemory, GivenMisalignedHostPtrAndMultiplePagesSizeWhenAsked graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->gmm->resourceParams.BaseWidth); } memoryManager->freeGraphicsMemory(graphicsAllocation); - EXPECT_EQ(0u, hostPtrManager.getFragmentCount()); + EXPECT_EQ(0u, hostPtrManager->getFragmentCount()); } TEST_F(BufferWithWddmMemory, GivenPointerAndSizeWhenAskedToCreateGrahicsAllocationThenGraphicsAllocationIsCreated) { @@ -1471,7 +1469,7 @@ TEST_F(BufferWithWddmMemory, givenFragmentsThatAreNotInOrderWhenGraphicsAllocati fragment.fragmentSize = size; fragment.osInternalStorage = handleStorage.fragmentStorageData[0].osHandleStorage; fragment.osInternalStorage->gpuPtr = gpuAdress; - memoryManager->hostPtrManager.storeFragment(fragment); + memoryManager->getHostPtrManager()->storeFragment(fragment); auto allocation = memoryManager->createGraphicsAllocation(handleStorage, size, ptr); EXPECT_EQ(ptr, allocation->getUnderlyingBuffer()); @@ -1502,7 +1500,7 @@ TEST_F(BufferWithWddmMemory, givenFragmentsThatAreNotInOrderWhenGraphicsAllocati fragment.fragmentSize = size; fragment.osInternalStorage = handleStorage.fragmentStorageData[0].osHandleStorage; fragment.osInternalStorage->gpuPtr = gpuAdress; - memoryManager->hostPtrManager.storeFragment(fragment); + memoryManager->getHostPtrManager()->storeFragment(fragment); auto offset = 80; auto allocationPtr = reinterpret_cast(reinterpret_cast(ptr) + offset); @@ -2006,11 +2004,12 @@ TEST_F(WddmMemoryManagerTest2, givenReadOnlyMemoryPassedToPopulateOsHandlesWhenC EXPECT_CALL(*wddm, createAllocationsAndMapGpuVa(::testing::_)).WillOnce(::testing::Return(STATUS_GRAPHICS_NO_VIDEO_MEMORY)); auto result = memoryManager->populateOsHandles(handleStorage); + auto hostPtrManager = static_cast(memoryManager->getHostPtrManager()); EXPECT_EQ(MemoryManager::AllocationStatus::InvalidHostPointer, result); - auto numberOfStoredFragments = memoryManager->hostPtrManager.getFragmentCount(); + auto numberOfStoredFragments = hostPtrManager->getFragmentCount(); EXPECT_EQ(0u, numberOfStoredFragments); - EXPECT_EQ(nullptr, memoryManager->hostPtrManager.getFragment(handleStorage.fragmentStorageData[1].cpuPtr)); + EXPECT_EQ(nullptr, hostPtrManager->getFragment(handleStorage.fragmentStorageData[1].cpuPtr)); handleStorage.fragmentStorageData[1].freeTheFragment = true; memoryManager->cleanOsHandles(handleStorage);