From 3a008fafc68bb37a3d27166ff68a1e24b19f3dba Mon Sep 17 00:00:00 2001 From: "Venevtsev, Igor" Date: Fri, 12 Apr 2019 15:21:42 +0200 Subject: [PATCH] Revert "[1/n] Use GfxPartition for 32-bit allocations - WddmMemoryManager" This reverts commit 2bb451e76d922861673e052f5f889658ac7db15f. Change-Id: I1deada59a291a96ef88c8b9b4f2b28861ad27347 Signed-off-by: Venevtsev, Igor --- runtime/memory_manager/gfx_partition.h | 4 -- runtime/memory_manager/memory_manager.h | 8 +--- runtime/os_interface/windows/wddm/wddm.cpp | 10 ++++- runtime/os_interface/windows/wddm/wddm.h | 3 ++ .../windows/wddm_memory_manager.cpp | 15 +++++--- .../windows/wddm_memory_manager.h | 1 + .../memory_manager/gfx_partition_tests.cpp | 3 -- .../memory_manager/memory_manager_tests.cpp | 8 ---- .../windows/mock_wddm_memory_manager.h | 1 - .../windows/wddm_memory_manager_tests.cpp | 38 ++++++++++--------- 10 files changed, 44 insertions(+), 47 deletions(-) diff --git a/runtime/memory_manager/gfx_partition.h b/runtime/memory_manager/gfx_partition.h index 3695836e50..f10d0e7709 100644 --- a/runtime/memory_manager/gfx_partition.h +++ b/runtime/memory_manager/gfx_partition.h @@ -54,10 +54,6 @@ class GfxPartition { return getHeap(heapIndex).getBase() + getHeap(heapIndex).getSize() - 1; } - uint64_t getHeapMinimalAddress(HeapIndex heapIndex) { - return getHeapBase(heapIndex) + heapGranularity; - } - static const uint64_t heapGranularity = MemoryConstants::pageSize64k; static const std::array heap32Names; diff --git a/runtime/memory_manager/memory_manager.h b/runtime/memory_manager/memory_manager.h index 645c564040..ab650c577b 100644 --- a/runtime/memory_manager/memory_manager.h +++ b/runtime/memory_manager/memory_manager.h @@ -106,13 +106,7 @@ class MemoryManager { virtual uint64_t getMaxApplicationAddress() = 0; - virtual uint64_t getInternalHeapBaseAddress() { - return gfxPartition.getHeapBase(internalHeapIndex); - } - - uint64_t getExternalHeapBaseAddress() { - return gfxPartition.getHeapBase(HeapIndex::HEAP_EXTERNAL); - } + virtual uint64_t getInternalHeapBaseAddress() = 0; bool peek64kbPagesEnabled() const { return enable64kbpages; } bool peekForce32BitAllocations() const { return force32bitAllocations; } diff --git a/runtime/os_interface/windows/wddm/wddm.cpp b/runtime/os_interface/windows/wddm/wddm.cpp index ad178ca804..7429a2d9e5 100644 --- a/runtime/os_interface/windows/wddm/wddm.cpp +++ b/runtime/os_interface/windows/wddm/wddm.cpp @@ -769,7 +769,7 @@ void Wddm::initGfxPartition(GfxPartition &outGfxPartition) const { outGfxPartition.heapInit(HeapIndex::HEAP_STANDARD64KB, gfxPartition.Standard64KB.Base, gfxPartition.Standard64KB.Limit - gfxPartition.Standard64KB.Base + 1); for (auto heap : GfxPartition::heap32Names) { - outGfxPartition.heapInit(heap, gfxPartition.Heap32[static_cast(heap)].Base, + outGfxPartition.heapInit(heap, gfxPartition.Heap32[static_cast(heap)].Base + MemoryConstants::pageSize, gfxPartition.Heap32[static_cast(heap)].Limit - gfxPartition.Heap32[static_cast(heap)].Base + 1); } } @@ -791,6 +791,14 @@ PFND3DKMT_ESCAPE Wddm::getEscapeHandle() const { return gdi->escape; } +uint64_t Wddm::getExternalHeapBase() const { + return alignUp(gfxPartition.Heap32[static_cast(HeapIndex::HEAP_EXTERNAL)].Base, MemoryConstants::pageSize); +} + +uint64_t Wddm::getExternalHeapSize() const { + return alignDown(gfxPartition.Heap32[static_cast(HeapIndex::HEAP_EXTERNAL)].Limit - gfxPartition.Heap32[static_cast(HeapIndex::HEAP_EXTERNAL)].Base, MemoryConstants::pageSize); +} + VOID *Wddm::registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, WddmResidencyController &residencyController) { if (DebugManager.flags.DoNotRegisterTrimCallback.get()) { return nullptr; diff --git a/runtime/os_interface/windows/wddm/wddm.h b/runtime/os_interface/windows/wddm/wddm.h index eb0d98af0d..1fe071043e 100644 --- a/runtime/os_interface/windows/wddm/wddm.h +++ b/runtime/os_interface/windows/wddm/wddm.h @@ -129,6 +129,9 @@ class Wddm { return static_cast(hwContextId); } + uint64_t getExternalHeapBase() const; + uint64_t getExternalHeapSize() const; + std::unique_ptr registryReader; GmmPageTableMngr *getPageTableManager() const { return pageTableManager.get(); } diff --git a/runtime/os_interface/windows/wddm_memory_manager.cpp b/runtime/os_interface/windows/wddm_memory_manager.cpp index 6b9ddbb01a..2fdfc69b75 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.cpp +++ b/runtime/os_interface/windows/wddm_memory_manager.cpp @@ -39,6 +39,7 @@ WddmMemoryManager::WddmMemoryManager(ExecutionEnvironment &executionEnvironment) wddm(executionEnvironment.osInterface->get()->getWddm()) { DEBUG_BREAK_IF(wddm == nullptr); + allocator32Bit = std::unique_ptr(new Allocator32bit(wddm->getExternalHeapBase(), wddm->getExternalHeapSize())); asyncDeleterEnabled = DebugManager.flags.EnableDeferredDeleter.get(); if (asyncDeleterEnabled) deferredDeleter = createDeferredDeleter(); @@ -211,7 +212,7 @@ GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemoryImpl(const All return nullptr; } - auto baseAddress = useInternal32BitAllocator(allocationData.type) ? getInternalHeapBaseAddress() : getExternalHeapBaseAddress(); + auto baseAddress = useInternal32BitAllocator(allocationData.type) ? getInternalHeapBaseAddress() : allocator32Bit->getBase(); wddmAllocation->setGpuBaseAddress(GmmHelper::canonize(baseAddress)); DebugManager.logAllocation(wddmAllocation.get()); @@ -240,7 +241,7 @@ GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handl allocation->setReservedAddressRange(ptr, size); } else if (requireSpecificBitness && this->force32bitAllocations) { allocation->set32BitAllocation(true); - allocation->setGpuBaseAddress(GmmHelper::canonize(getExternalHeapBaseAddress())); + allocation->setGpuBaseAddress(GmmHelper::canonize(allocator32Bit->getBase())); } status = mapGpuVirtualAddressWithRetry(allocation.get(), allocation->getReservedAddressPtr()); DEBUG_BREAK_IF(!status); @@ -482,6 +483,10 @@ uint64_t WddmMemoryManager::getMaxApplicationAddress() { return wddm->getMaxApplicationAddress(); } +uint64_t WddmMemoryManager::getInternalHeapBaseAddress() { + return wddm->getGfxPartition().Heap32[static_cast(internalHeapIndex)].Base; +} + bool WddmMemoryManager::mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) { return wddm->updateAuxTable(graphicsAllocation->getGpuAddress(), graphicsAllocation->getDefaultGmm(), true); } @@ -544,7 +549,7 @@ uint32_t WddmMemoryManager::mapGpuVirtualAddress(WddmAllocation *graphicsAllocat for (auto handleId = startingIndex; handleId < graphicsAllocation->getNumHandles(); handleId++) { if (!wddm->mapGpuVirtualAddress(graphicsAllocation->getGmm(handleId), graphicsAllocation->getHandles()[handleId], - gfxPartition.getHeapMinimalAddress(heapIndex), gfxPartition.getHeapLimit(heapIndex), + gfxPartition.getHeapBase(heapIndex), gfxPartition.getHeapLimit(heapIndex), addressToMap, graphicsAllocation->getGpuAddressToModify())) { return numMappedAllocations; } @@ -556,8 +561,8 @@ uint32_t WddmMemoryManager::mapGpuVirtualAddress(WddmAllocation *graphicsAllocat void WddmMemoryManager::obtainGpuAddressIfNeeded(WddmAllocation *allocation) { if (allocation->getNumHandles() > 1u) { auto heapIndex = selectHeap(allocation, false, executionEnvironment.isFullRangeSvm()); - allocation->preferredGpuAddress = wddm->reserveGpuVirtualAddress(gfxPartition.getHeapMinimalAddress(heapIndex), - gfxPartition.getHeapLimit(heapIndex), allocation->getAlignedSize()); + allocation->preferredGpuAddress = wddm->reserveGpuVirtualAddress(gfxPartition.getHeapBase(heapIndex), gfxPartition.getHeapLimit(heapIndex), + allocation->getAlignedSize()); } } diff --git a/runtime/os_interface/windows/wddm_memory_manager.h b/runtime/os_interface/windows/wddm_memory_manager.h index ba3b566b25..fbb4f610da 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.h +++ b/runtime/os_interface/windows/wddm_memory_manager.h @@ -49,6 +49,7 @@ class WddmMemoryManager : public MemoryManager { uint64_t getSystemSharedMemory() override; uint64_t getMaxApplicationAddress() override; + uint64_t getInternalHeapBaseAddress() override; bool tryDeferDeletions(const D3DKMT_HANDLE *handles, uint32_t allocationCount, D3DKMT_HANDLE resourceHandle); diff --git a/unit_tests/memory_manager/gfx_partition_tests.cpp b/unit_tests/memory_manager/gfx_partition_tests.cpp index e9229077d1..1aaa8b47b6 100644 --- a/unit_tests/memory_manager/gfx_partition_tests.cpp +++ b/unit_tests/memory_manager/gfx_partition_tests.cpp @@ -69,9 +69,6 @@ void testGfxPartition(uint64_t gpuAddressSpace) { continue; } - EXPECT_GT(gfxPartition.getHeapMinimalAddress(heap), gfxPartition.getHeapBase(heap)); - EXPECT_EQ(gfxPartition.getHeapMinimalAddress(heap), gfxPartition.getHeapBase(heap) + GfxPartition::heapGranularity); - auto ptrBig = gfxPartition.heapAllocate(heap, sizeBig); EXPECT_NE(ptrBig, 0ull); EXPECT_LT(gfxPartition.getHeapBase(heap), ptrBig); diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index 70123d4aed..82e21e227d 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -336,14 +336,6 @@ TEST_F(MemoryAllocatorTest, GivenPointerAndSizeWhenAskedToCreateGrahicsAllocatio EXPECT_NE(&allocation->fragmentsStorage, &handleStorage); } -TEST_F(MemoryAllocatorTest, defaultInternalHeapBaseIsInitialized) { - EXPECT_LE(0ull, memoryManager->MemoryManager::getInternalHeapBaseAddress()); -} - -TEST_F(MemoryAllocatorTest, defaultExternalHeapBaseIsNotNull) { - EXPECT_LT(0ull, memoryManager->getExternalHeapBaseAddress()); -} - TEST_F(MemoryAllocatorTest, givenMemoryManagerWhensetForce32BitAllocationsIsCalledWithTrueMutlipleTimesThenAllocatorIsReused) { memoryManager->setForce32BitAllocations(true); EXPECT_NE(nullptr, memoryManager->allocator32Bit.get()); 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 ee6ad5223c..69b41b00f7 100644 --- a/unit_tests/os_interface/windows/mock_wddm_memory_manager.h +++ b/unit_tests/os_interface/windows/mock_wddm_memory_manager.h @@ -22,7 +22,6 @@ class MockWddmMemoryManager : public MemoryManagerCreate { using BaseClass::allocateGraphicsMemoryWithProperties; using BaseClass::createGraphicsAllocation; using BaseClass::createWddmAllocation; - using BaseClass::gfxPartition; using BaseClass::localMemorySupported; using MemoryManagerCreate::MemoryManagerCreate; 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 576ad3fae7..fd9312a13c 100644 --- a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp @@ -105,7 +105,7 @@ TEST(WddmAllocationTest, givenMemoryPoolWhenPassedToWddmAllocationConstructorThe EXPECT_EQ(MemoryPool::SystemCpuInaccessible, allocation2.getMemoryPool()); } -TEST(WddmMemoryManagerExternalHeapTest, externalHeapIsCreatedWithCorrectBase) { +TEST(WddmMemoryManagerAllocator32BitTest, allocator32BitIsCreatedWithCorrectBase) { HardwareInfo *hwInfo; auto executionEnvironment = getExecutionEnvironmentImpl(hwInfo); std::unique_ptr wddm(static_cast(Wddm::createWddm())); @@ -116,7 +116,9 @@ TEST(WddmMemoryManagerExternalHeapTest, externalHeapIsCreatedWithCorrectBase) { std::unique_ptr memoryManager = std::unique_ptr(new WddmMemoryManager(*executionEnvironment)); - EXPECT_EQ(base, memoryManager->getExternalHeapBaseAddress()); + ASSERT_NE(nullptr, memoryManager->allocator32Bit.get()); + + EXPECT_EQ(base, memoryManager->allocator32Bit->getBase()); } TEST(WddmMemoryManagerWithDeferredDeleterTest, givenWMMWhenAsyncDeleterIsEnabledAndWaitForDeletionsIsCalledThenDeleterInWddmIsSetToNullptr) { @@ -275,7 +277,7 @@ TEST_F(WddmMemoryManagerSimpleTest, givenAllocateGraphicsMemoryForNonSvmHostPtrIsCalledWhenNotAlignedPtrIsPassedThenAlignedGraphicsAllocationIsCreated) { memoryManager.reset(new MockWddmMemoryManager(false, false, *executionEnvironment)); auto size = 13u; - auto hostPtr = reinterpret_cast(0x10001); + auto hostPtr = reinterpret_cast(0x5001); AllocationData allocationData; allocationData.size = size; @@ -517,7 +519,7 @@ TEST_F(WddmMemoryManagerTest, createAllocationFromSharedHandleReturns32BitAllocW if (is64bit) { EXPECT_TRUE(gpuAllocation->is32BitAllocation()); - uint64_t base = memoryManager->getExternalHeapBaseAddress(); + uint64_t base = memoryManager->allocator32Bit->getBase(); EXPECT_EQ(GmmHelper::canonize(base), gpuAllocation->getGpuBaseAddress()); } @@ -848,8 +850,8 @@ TEST_F(WddmMemoryManagerTest, Allocate32BitMemoryWithNullptr) { auto *gpuAllocation = memoryManager->allocate32BitGraphicsMemory(3 * MemoryConstants::pageSize, nullptr, GraphicsAllocation::AllocationType::BUFFER); ASSERT_NE(nullptr, gpuAllocation); - EXPECT_LT(GmmHelper::canonize(memoryManager->gfxPartition.getHeapBase(HeapIndex::HEAP_EXTERNAL)), gpuAllocation->getGpuAddress()); - EXPECT_GT(GmmHelper::canonize(memoryManager->gfxPartition.getHeapLimit(HeapIndex::HEAP_EXTERNAL)), gpuAllocation->getGpuAddress() + gpuAllocation->getUnderlyingBufferSize()); + EXPECT_LE(GmmHelper::canonize(wddm->getExternalHeapBase()), gpuAllocation->getGpuAddress()); + EXPECT_GT(GmmHelper::canonize(wddm->getExternalHeapBase()) + wddm->getExternalHeapSize() - 1, gpuAllocation->getGpuAddress()); EXPECT_EQ(0u, gpuAllocation->fragmentsStorage.fragmentCount); memoryManager->freeGraphicsMemory(gpuAllocation); @@ -860,8 +862,8 @@ TEST_F(WddmMemoryManagerTest, given32BitAllocationWhenItIsCreatedThenItHasNonZer ASSERT_NE(nullptr, gpuAllocation); EXPECT_NE(0llu, gpuAllocation->getGpuAddressToPatch()); - EXPECT_LT(GmmHelper::canonize(memoryManager->gfxPartition.getHeapBase(HeapIndex::HEAP_EXTERNAL)), gpuAllocation->getGpuAddress()); - EXPECT_GT(GmmHelper::canonize(memoryManager->gfxPartition.getHeapLimit(HeapIndex::HEAP_EXTERNAL)), gpuAllocation->getGpuAddress() + gpuAllocation->getUnderlyingBufferSize()); + EXPECT_LE(GmmHelper::canonize(wddm->getExternalHeapBase()), gpuAllocation->getGpuAddress()); + EXPECT_GT(GmmHelper::canonize(wddm->getExternalHeapBase()) + wddm->getExternalHeapSize() - 1, gpuAllocation->getGpuAddress()); memoryManager->freeGraphicsMemory(gpuAllocation); } @@ -875,8 +877,8 @@ TEST_F(WddmMemoryManagerTest, Allocate32BitMemoryWithMisalignedHostPtrDoesNotDoT EXPECT_EQ(alignSizeWholePage(misalignedPtr, misalignedSize), gpuAllocation->getUnderlyingBufferSize()); - EXPECT_LT(GmmHelper::canonize(memoryManager->gfxPartition.getHeapBase(HeapIndex::HEAP_EXTERNAL)), gpuAllocation->getGpuAddress()); - EXPECT_GT(GmmHelper::canonize(memoryManager->gfxPartition.getHeapLimit(HeapIndex::HEAP_EXTERNAL)), gpuAllocation->getGpuAddress() + gpuAllocation->getUnderlyingBufferSize()); + EXPECT_LE(GmmHelper::canonize(wddm->getExternalHeapBase()), gpuAllocation->getGpuAddress()); + EXPECT_GT(GmmHelper::canonize(wddm->getExternalHeapBase()) + wddm->getExternalHeapSize() - 1, gpuAllocation->getGpuAddress()); EXPECT_EQ(0u, gpuAllocation->fragmentsStorage.fragmentCount); @@ -892,7 +894,7 @@ TEST_F(WddmMemoryManagerTest, Allocate32BitMemorySetsCannonizedGpuBaseAddress) { ASSERT_NE(nullptr, gpuAllocation); - uint64_t cannonizedAddress = GmmHelper::canonize(memoryManager->gfxPartition.getHeapBase(HeapIndex::HEAP_EXTERNAL)); + uint64_t cannonizedAddress = GmmHelper::canonize(wddm->getExternalHeapBase()); EXPECT_EQ(cannonizedAddress, gpuAllocation->getGpuBaseAddress()); memoryManager->freeGraphicsMemory(gpuAllocation); @@ -975,7 +977,7 @@ TEST_F(WddmMemoryManagerTest, givenManagerWithDisabledDeferredDeleterWhenMapGpuV } TEST_F(WddmMemoryManagerTest, givenManagerWithEnabledDeferredDeleterWhenFirstMapGpuVaFailSecondAfterDrainSuccessThenCreateAllocation) { - void *ptr = reinterpret_cast(0x10000); + void *ptr = reinterpret_cast(0x1000); size_t size = 0x1000; std::unique_ptr gmm(new Gmm(ptr, size, false)); @@ -1014,10 +1016,10 @@ TEST_F(WddmMemoryManagerTest, givenNullPtrAndSizePassedToCreateInternalAllocatio EXPECT_EQ(4096u, wddmAllocation->getUnderlyingBufferSize()); EXPECT_NE((uint64_t)wddmAllocation->getUnderlyingBuffer(), wddmAllocation->getGpuAddress()); auto cannonizedHeapBase = GmmHelper::canonize(memoryManager->getInternalHeapBaseAddress()); - auto cannonizedHeapEnd = GmmHelper::canonize(memoryManager->gfxPartition.getHeapLimit(internalHeapIndex)); + auto cannonizedHeapEnd = GmmHelper::canonize(this->wddm->getGfxPartition().Heap32[static_cast(internalHeapIndex)].Limit); - EXPECT_GT(wddmAllocation->getGpuAddress(), cannonizedHeapBase); - EXPECT_LT(wddmAllocation->getGpuAddress() + wddmAllocation->getUnderlyingBufferSize(), cannonizedHeapEnd); + EXPECT_GE(wddmAllocation->getGpuAddress(), cannonizedHeapBase); + EXPECT_LE(wddmAllocation->getGpuAddress(), cannonizedHeapEnd); EXPECT_NE(nullptr, wddmAllocation->getDriverAllocatedCpuPtr()); EXPECT_TRUE(wddmAllocation->is32BitAllocation()); @@ -1033,10 +1035,10 @@ TEST_F(WddmMemoryManagerTest, givenPtrAndSizePassedToCreateInternalAllocationWhe EXPECT_EQ(4096u, wddmAllocation->getUnderlyingBufferSize()); EXPECT_NE((uint64_t)wddmAllocation->getUnderlyingBuffer(), wddmAllocation->getGpuAddress()); auto cannonizedHeapBase = GmmHelper::canonize(memoryManager->getInternalHeapBaseAddress()); - auto cannonizedHeapEnd = GmmHelper::canonize(memoryManager->gfxPartition.getHeapLimit(internalHeapIndex)); + auto cannonizedHeapEnd = GmmHelper::canonize(wddm->getGfxPartition().Heap32[static_cast(internalHeapIndex)].Limit); - EXPECT_GT(wddmAllocation->getGpuAddress(), cannonizedHeapBase); - EXPECT_LT(wddmAllocation->getGpuAddress() + wddmAllocation->getUnderlyingBufferSize(), cannonizedHeapEnd); + EXPECT_GE(wddmAllocation->getGpuAddress(), cannonizedHeapBase); + EXPECT_LE(wddmAllocation->getGpuAddress(), cannonizedHeapEnd); EXPECT_EQ(nullptr, wddmAllocation->getDriverAllocatedCpuPtr()); EXPECT_TRUE(wddmAllocation->is32BitAllocation());