From e8fb931ef141eb3c25366a3091f84d7dc346e22b Mon Sep 17 00:00:00 2001 From: "Hoppe, Mateusz" Date: Thu, 28 Dec 2017 11:25:43 +0100 Subject: [PATCH] Use 64KB pages for SVM allocations when 64KB pages are enabled - clSVMAlloc allocates 64KB pages as memory storage for both fine grain and coarse grain allocation Change-Id: I2068ffb9f5577761f739df47b54bc382e971949c --- runtime/memory_manager/memory_constants.h | 1 + runtime/memory_manager/memory_manager.cpp | 9 ++++-- runtime/memory_manager/memory_manager.h | 2 +- runtime/memory_manager/svm_memory_manager.cpp | 2 +- .../memory_manager/memory_manager_tests.cpp | 28 ++++++++++++++++--- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/runtime/memory_manager/memory_constants.h b/runtime/memory_manager/memory_constants.h index 64dc1b22c3..245fe5d9cb 100644 --- a/runtime/memory_manager/memory_constants.h +++ b/runtime/memory_manager/memory_constants.h @@ -39,6 +39,7 @@ static const size_t allocationAlignment = pageSize; // alignment required to gra static const size_t slmWindowAlignment = 128 * kiloByte; static const size_t slmWindowSize = 64 * kiloByte; static const uintptr_t pageMask = (pageSize - 1); +static const uintptr_t page64kMask = (pageSize64k - 1); static const uint64_t max32BitAppAddress = ((1ULL << 31) - 1); static const uint64_t max64BitAppAddress = ((1ULL << 47) - 1); } diff --git a/runtime/memory_manager/memory_manager.cpp b/runtime/memory_manager/memory_manager.cpp index 8db173a58e..b90ba5903c 100644 --- a/runtime/memory_manager/memory_manager.cpp +++ b/runtime/memory_manager/memory_manager.cpp @@ -77,8 +77,13 @@ void *MemoryManager::allocateSystemMemory(size_t size, size_t alignment) { return alignedMalloc(size, std::max(alignment, minAlignment)); } -GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForSVM(size_t size, size_t alignment, bool coherent) { - auto graphicsAllocation = allocateGraphicsMemory(size, alignment); +GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForSVM(size_t size, bool coherent) { + GraphicsAllocation *graphicsAllocation = nullptr; + if (enable64kbpages) { + graphicsAllocation = allocateGraphicsMemory64kb(size, MemoryConstants::pageSize64k, false); + } else { + graphicsAllocation = allocateGraphicsMemory(size, MemoryConstants::pageSize); + } if (graphicsAllocation) { graphicsAllocation->setCoherent(coherent); } diff --git a/runtime/memory_manager/memory_manager.h b/runtime/memory_manager/memory_manager.h index dd07250e1e..ef66cea3c1 100644 --- a/runtime/memory_manager/memory_manager.h +++ b/runtime/memory_manager/memory_manager.h @@ -97,7 +97,7 @@ class MemoryManager { virtual GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, Gmm *gmm) = 0; - GraphicsAllocation *allocateGraphicsMemoryForSVM(size_t size, size_t alignment, bool coherent); + GraphicsAllocation *allocateGraphicsMemoryForSVM(size_t size, bool coherent); GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) { return createGraphicsAllocationFromSharedHandle(handle, requireSpecificBitness, false); diff --git a/runtime/memory_manager/svm_memory_manager.cpp b/runtime/memory_manager/svm_memory_manager.cpp index 59f8b6e235..4b267d1ebe 100644 --- a/runtime/memory_manager/svm_memory_manager.cpp +++ b/runtime/memory_manager/svm_memory_manager.cpp @@ -69,7 +69,7 @@ void *SVMAllocsManager::createSVMAlloc(size_t size, bool coherent) { return nullptr; std::unique_lock lock(mtx); - GraphicsAllocation *GA = memoryManager->allocateGraphicsMemoryForSVM(size, 4096, coherent); + GraphicsAllocation *GA = memoryManager->allocateGraphicsMemoryForSVM(size, coherent); if (!GA) { return nullptr; } diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index 1e7341dfef..85ee9913f8 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -735,15 +735,35 @@ TEST(OsAgnosticMemoryManager, givenDefaultMemoryManagerWhenAllocateGraphicsMemor memoryManager.freeGraphicsMemory(imageAllocation); } -TEST(OsAgnosticMemoryManager, givenDefaultMemoryManagerWhenAllocateGraphicsMemoryForSVMIsCalledThenGraphicsAllocationIsReturned) { - OsAgnosticMemoryManager memoryManager; +TEST(OsAgnosticMemoryManager, givenMemoryManagerWith64KBPagesDisabledWhenAllocateGraphicsMemoryForSVMIsCalledThen4KBGraphicsAllocationIsReturned) { + OsAgnosticMemoryManager memoryManager(false); auto size = 4096u; - auto allignment = 4096u; auto isCoherent = true; - auto svmAllocation = memoryManager.allocateGraphicsMemoryForSVM(size, allignment, isCoherent); + auto svmAllocation = memoryManager.allocateGraphicsMemoryForSVM(size, isCoherent); EXPECT_NE(nullptr, svmAllocation); EXPECT_TRUE(svmAllocation->isCoherent()); + + EXPECT_EQ(size, svmAllocation->getUnderlyingBufferSize()); + + uintptr_t address = reinterpret_cast(svmAllocation->getUnderlyingBuffer()); + EXPECT_EQ(0u, (address & MemoryConstants::pageMask)); + memoryManager.freeGraphicsMemory(svmAllocation); +} + +TEST(OsAgnosticMemoryManager, givenMemoryManagerWith64KBPagesEnabledWhenAllocateGraphicsMemoryForSVMIsCalledThen64KBGraphicsAllocationIsReturned) { + OsAgnosticMemoryManager memoryManager(true); + auto size = 4096u; + auto isCoherent = true; + + auto svmAllocation = memoryManager.allocateGraphicsMemoryForSVM(size, isCoherent); + EXPECT_NE(nullptr, svmAllocation); + EXPECT_TRUE(svmAllocation->isCoherent()); + + EXPECT_EQ(MemoryConstants::pageSize64k, svmAllocation->getUnderlyingBufferSize()); + + uintptr_t address = reinterpret_cast(svmAllocation->getUnderlyingBuffer()); + EXPECT_EQ(0u, (address & MemoryConstants::page64kMask)); memoryManager.freeGraphicsMemory(svmAllocation); }