Skip to content

Commit

Permalink
Revert "[1/n] Use GfxPartition for 32-bit allocations - WddmMemoryMan…
Browse files Browse the repository at this point in the history
…ager"

This reverts commit 2bb451e76d922861673e052f5f889658ac7db15f.

Change-Id: I1deada59a291a96ef88c8b9b4f2b28861ad27347
Signed-off-by: Venevtsev, Igor <[email protected]>
  • Loading branch information
ivvenevt authored and Compute-Runtime-Automation committed Apr 12, 2019
1 parent 54c4678 commit 3a008fa
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 47 deletions.
4 changes: 0 additions & 4 deletions runtime/memory_manager/gfx_partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HeapIndex, 4> heap32Names;
Expand Down
8 changes: 1 addition & 7 deletions runtime/memory_manager/memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
10 changes: 9 additions & 1 deletion runtime/os_interface/windows/wddm/wddm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(heap)].Base,
outGfxPartition.heapInit(heap, gfxPartition.Heap32[static_cast<uint32_t>(heap)].Base + MemoryConstants::pageSize,
gfxPartition.Heap32[static_cast<uint32_t>(heap)].Limit - gfxPartition.Heap32[static_cast<uint32_t>(heap)].Base + 1);
}
}
Expand All @@ -791,6 +791,14 @@ PFND3DKMT_ESCAPE Wddm::getEscapeHandle() const {
return gdi->escape;
}

uint64_t Wddm::getExternalHeapBase() const {
return alignUp(gfxPartition.Heap32[static_cast<uint32_t>(HeapIndex::HEAP_EXTERNAL)].Base, MemoryConstants::pageSize);
}

uint64_t Wddm::getExternalHeapSize() const {
return alignDown(gfxPartition.Heap32[static_cast<uint32_t>(HeapIndex::HEAP_EXTERNAL)].Limit - gfxPartition.Heap32[static_cast<uint32_t>(HeapIndex::HEAP_EXTERNAL)].Base, MemoryConstants::pageSize);
}

VOID *Wddm::registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, WddmResidencyController &residencyController) {
if (DebugManager.flags.DoNotRegisterTrimCallback.get()) {
return nullptr;
Expand Down
3 changes: 3 additions & 0 deletions runtime/os_interface/windows/wddm/wddm.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class Wddm {
return static_cast<uint32_t>(hwContextId);
}

uint64_t getExternalHeapBase() const;
uint64_t getExternalHeapSize() const;

std::unique_ptr<SettingsReader> registryReader;

GmmPageTableMngr *getPageTableManager() const { return pageTableManager.get(); }
Expand Down
15 changes: 10 additions & 5 deletions runtime/os_interface/windows/wddm_memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ WddmMemoryManager::WddmMemoryManager(ExecutionEnvironment &executionEnvironment)
wddm(executionEnvironment.osInterface->get()->getWddm()) {
DEBUG_BREAK_IF(wddm == nullptr);

allocator32Bit = std::unique_ptr<Allocator32bit>(new Allocator32bit(wddm->getExternalHeapBase(), wddm->getExternalHeapSize()));
asyncDeleterEnabled = DebugManager.flags.EnableDeferredDeleter.get();
if (asyncDeleterEnabled)
deferredDeleter = createDeferredDeleter();
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -482,6 +483,10 @@ uint64_t WddmMemoryManager::getMaxApplicationAddress() {
return wddm->getMaxApplicationAddress();
}

uint64_t WddmMemoryManager::getInternalHeapBaseAddress() {
return wddm->getGfxPartition().Heap32[static_cast<uint32_t>(internalHeapIndex)].Base;
}

bool WddmMemoryManager::mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) {
return wddm->updateAuxTable(graphicsAllocation->getGpuAddress(), graphicsAllocation->getDefaultGmm(), true);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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());
}
}

Expand Down
1 change: 1 addition & 0 deletions runtime/os_interface/windows/wddm_memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 0 additions & 3 deletions unit_tests/memory_manager/gfx_partition_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions unit_tests/memory_manager/memory_manager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion unit_tests/os_interface/windows/mock_wddm_memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class MockWddmMemoryManager : public MemoryManagerCreate<WddmMemoryManager> {
using BaseClass::allocateGraphicsMemoryWithProperties;
using BaseClass::createGraphicsAllocation;
using BaseClass::createWddmAllocation;
using BaseClass::gfxPartition;
using BaseClass::localMemorySupported;
using MemoryManagerCreate<WddmMemoryManager>::MemoryManagerCreate;

Expand Down
38 changes: 20 additions & 18 deletions unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<WddmMock> wddm(static_cast<WddmMock *>(Wddm::createWddm()));
Expand All @@ -116,7 +116,9 @@ TEST(WddmMemoryManagerExternalHeapTest, externalHeapIsCreatedWithCorrectBase) {

std::unique_ptr<WddmMemoryManager> memoryManager = std::unique_ptr<WddmMemoryManager>(new WddmMemoryManager(*executionEnvironment));

EXPECT_EQ(base, memoryManager->getExternalHeapBaseAddress());
ASSERT_NE(nullptr, memoryManager->allocator32Bit.get());

EXPECT_EQ(base, memoryManager->allocator32Bit->getBase());
}

TEST(WddmMemoryManagerWithDeferredDeleterTest, givenWMMWhenAsyncDeleterIsEnabledAndWaitForDeletionsIsCalledThenDeleterInWddmIsSetToNullptr) {
Expand Down Expand Up @@ -275,7 +277,7 @@ TEST_F(WddmMemoryManagerSimpleTest,
givenAllocateGraphicsMemoryForNonSvmHostPtrIsCalledWhenNotAlignedPtrIsPassedThenAlignedGraphicsAllocationIsCreated) {
memoryManager.reset(new MockWddmMemoryManager(false, false, *executionEnvironment));
auto size = 13u;
auto hostPtr = reinterpret_cast<const void *>(0x10001);
auto hostPtr = reinterpret_cast<const void *>(0x5001);

AllocationData allocationData;
allocationData.size = size;
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -975,7 +977,7 @@ TEST_F(WddmMemoryManagerTest, givenManagerWithDisabledDeferredDeleterWhenMapGpuV
}

TEST_F(WddmMemoryManagerTest, givenManagerWithEnabledDeferredDeleterWhenFirstMapGpuVaFailSecondAfterDrainSuccessThenCreateAllocation) {
void *ptr = reinterpret_cast<void *>(0x10000);
void *ptr = reinterpret_cast<void *>(0x1000);
size_t size = 0x1000;
std::unique_ptr<Gmm> gmm(new Gmm(ptr, size, false));

Expand Down Expand Up @@ -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<uint32_t>(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());
Expand All @@ -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<uint32_t>(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());
Expand Down

0 comments on commit 3a008fa

Please sign in to comment.