From e056082710289cf934d55d5be91958977efcf57d Mon Sep 17 00:00:00 2001 From: John Falkowski Date: Thu, 6 Apr 2023 22:35:11 +0000 Subject: [PATCH] refactor graphics allocation structure elements for sub-allocation properties Resolves: LOCI-3772 Signed-off-by: John Falkowski --- .../core/source/context/context_imp.cpp | 6 +- .../unit_tests/sources/memory/test_memory.cpp | 75 ++++++++++--------- .../memory_manager/graphics_allocation.h | 11 ++- .../os_interface/linux/drm_allocation.h | 11 ++- .../os_interface/linux/drm_memory_manager.cpp | 3 - .../common/mocks/mock_graphics_allocation.h | 3 - .../graphics_allocation_tests.cpp | 9 ++- .../linux/drm_memory_manager_tests.cpp | 5 +- 8 files changed, 70 insertions(+), 53 deletions(-) diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index bd2ec8a1f7..00287b59b2 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -679,7 +679,7 @@ ze_result_t ContextImp::handleAllocationExtensions(NEO::GraphicsAllocation *allo } exportStructure->handle = reinterpret_cast(handle); } else if (extendedProperties->stype == ZE_STRUCTURE_TYPE_MEMORY_SUB_ALLOCATIONS_EXP_PROPERTIES) { - if (alloc->isSubAllocSet) { + if (alloc->getNumHandles()) { ze_memory_sub_allocations_exp_properties_t *extendedSubAllocProperties = reinterpret_cast(extendedProperties); if (extendedSubAllocProperties->pCount) { @@ -690,8 +690,8 @@ ze_result_t ContextImp::handleAllocationExtensions(NEO::GraphicsAllocation *allo } if (extendedSubAllocProperties->pSubAllocations) { for (uint32_t i = 0; i < *extendedSubAllocProperties->pCount; i++) { - extendedSubAllocProperties->pSubAllocations[i].base = reinterpret_cast(alloc->subAllocBase[i]); - extendedSubAllocProperties->pSubAllocations[i].size = alloc->subAllocSize[i]; + extendedSubAllocProperties->pSubAllocations[i].base = reinterpret_cast(alloc->getHandleAddressBase(i)); + extendedSubAllocProperties->pSubAllocations[i].size = alloc->getHandleSize(i); } // If pSubAllocations nullptr, then user getting Count first and calling second time } diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp index 25d3190e7e..2693c7e725 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp @@ -2606,7 +2606,16 @@ struct SingleSubAllocMockGraphicsAllocation : public NEO::MockGraphicsAllocation uint32_t getNumHandles() override { return 1; } + + uint64_t getHandleAddressBase(uint32_t handleIndex) override { + return 0lu; + } + + size_t getHandleSize(uint32_t handleIndex) override { + return 1000lu; + } }; + TEST_F(MemoryExportImportTest, givenCallToMemAllocPropertiesWithExtendedExportPropertiesAndGetSingleSubAllocation) { @@ -2619,10 +2628,6 @@ TEST_F(MemoryExportImportTest, subAllocationDesc.stype = ZE_STRUCTURE_TYPE_MEMORY_SUB_ALLOCATIONS_EXP_PROPERTIES; subAllocationDesc.pCount = &numberOfSubAllocations; memoryProperties.pNext = &subAllocationDesc; - - alloc->subAllocSize[0] = MemoryConstants::pageSize64k; - alloc->subAllocBase[0] = 0; - alloc->isSubAllocSet = true; ze_memory_type_t type = ZE_MEMORY_TYPE_DEVICE; ze_result_t result = context->handleAllocationExtensions(alloc, type, &subAllocationDesc, driverHandle.get()); @@ -2633,8 +2638,8 @@ TEST_F(MemoryExportImportTest, result = context->handleAllocationExtensions(alloc, type, &subAllocationDesc, driverHandle.get()); EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(1u, numberOfSubAllocations); - EXPECT_EQ(alloc->subAllocSize[0], subAllocationMemAllocProperties[0].size); - EXPECT_EQ(alloc->subAllocBase[0], reinterpret_cast(subAllocationMemAllocProperties[0].base)); + EXPECT_EQ(1000lu, subAllocationMemAllocProperties[0].size); + EXPECT_EQ(0lu, reinterpret_cast(subAllocationMemAllocProperties[0].base)); delete alloc; } @@ -2644,7 +2649,16 @@ struct SubAllocMockGraphicsAllocation : public NEO::MockGraphicsAllocation { uint32_t getNumHandles() override { return 4; } + + uint64_t getHandleAddressBase(uint32_t handleIndex) override { + return (handleIndex * MemoryConstants::pageSize64k); + } + + size_t getHandleSize(uint32_t handleIndex) override { + return MemoryConstants::pageSize64k; + } }; + TEST_F(MemoryExportImportTest, givenCallToMemAllocPropertiesWithExtendedExportPropertiesAndGetSubAllocations) { @@ -2658,15 +2672,6 @@ TEST_F(MemoryExportImportTest, subAllocationDesc.pCount = &numberOfSubAllocations; memoryProperties.pNext = &subAllocationDesc; - uint64_t baseAddress = 0; - size_t size = MemoryConstants::pageSize64k; - - for (uint32_t i = 0; i < alloc->getNumHandles(); i++) { - alloc->subAllocSize[i] = size; - alloc->subAllocBase[i] = baseAddress; - baseAddress += size; - } - alloc->isSubAllocSet = true; ze_memory_type_t type = ZE_MEMORY_TYPE_DEVICE; ze_result_t result = context->handleAllocationExtensions(alloc, type, &subAllocationDesc, driverHandle.get()); @@ -2679,17 +2684,33 @@ TEST_F(MemoryExportImportTest, EXPECT_EQ(4u, numberOfSubAllocations); for (uint32_t i = 0; i < numberOfSubAllocations; i++) { - EXPECT_EQ(alloc->subAllocSize[i], subAllocationMemAllocProperties[i].size); - EXPECT_EQ(alloc->subAllocBase[i], reinterpret_cast(subAllocationMemAllocProperties[i].base)); + EXPECT_EQ(MemoryConstants::pageSize64k, subAllocationMemAllocProperties[i].size); + EXPECT_EQ(i * MemoryConstants::pageSize64k, reinterpret_cast(subAllocationMemAllocProperties[i].base)); } delete alloc; } +struct NoSubAllocMockGraphicsAllocation : public NEO::MockGraphicsAllocation { + using NEO::MockGraphicsAllocation::MockGraphicsAllocation; + + uint32_t getNumHandles() override { + return 0; + } + + uint64_t getHandleAddressBase(uint32_t handleIndex) override { + return 0lu; + } + + size_t getHandleSize(uint32_t handleIndex) override { + return 0lu; + } +}; + TEST_F(MemoryExportImportTest, givenCallToMemAllocPropertiesWithExtendedExportPropertiesAndUnsetFlagAndGetReturnError) { char buffer[256]; - auto alloc = new SubAllocMockGraphicsAllocation(&buffer, sizeof(buffer)); + auto alloc = new NoSubAllocMockGraphicsAllocation(&buffer, sizeof(buffer)); ze_memory_allocation_properties_t memoryProperties = {}; ze_memory_sub_allocations_exp_properties_t subAllocationDesc{}; @@ -2698,15 +2719,6 @@ TEST_F(MemoryExportImportTest, subAllocationDesc.pCount = &numberOfSubAllocations; memoryProperties.pNext = &subAllocationDesc; - uint64_t baseAddress = 0; - size_t size = MemoryConstants::pageSize64k; - - for (uint32_t i = 0; i < alloc->getNumHandles(); i++) { - alloc->subAllocSize[i] = size; - alloc->subAllocBase[i] = baseAddress; - baseAddress += size; - } - alloc->isSubAllocSet = false; ze_memory_type_t type = ZE_MEMORY_TYPE_DEVICE; ze_result_t result = context->handleAllocationExtensions(alloc, type, &subAllocationDesc, driverHandle.get()); @@ -2725,15 +2737,6 @@ TEST_F(MemoryExportImportTest, subAllocationDesc.stype = ZE_STRUCTURE_TYPE_MEMORY_SUB_ALLOCATIONS_EXP_PROPERTIES; memoryProperties.pNext = &subAllocationDesc; - uint64_t baseAddress = 0; - size_t size = MemoryConstants::pageSize64k; - - for (uint32_t i = 0; i < alloc->getNumHandles(); i++) { - alloc->subAllocSize[i] = size; - alloc->subAllocBase[i] = baseAddress; - baseAddress += size; - } - alloc->isSubAllocSet = true; ze_memory_type_t type = ZE_MEMORY_TYPE_DEVICE; ze_result_t result = context->handleAllocationExtensions(alloc, type, &subAllocationDesc, driverHandle.get()); diff --git a/shared/source/memory_manager/graphics_allocation.h b/shared/source/memory_manager/graphics_allocation.h index 35d77a097d..0c71032322 100644 --- a/shared/source/memory_manager/graphics_allocation.h +++ b/shared/source/memory_manager/graphics_allocation.h @@ -182,6 +182,14 @@ class GraphicsAllocation : public IDNode { virtual void setNumHandles(uint32_t numHandles) { } + virtual uint64_t getHandleAddressBase(uint32_t handleIndex) { + return 0lu; + } + + virtual size_t getHandleSize(uint32_t handleIndex) { + return 0lu; + } + static bool isCpuAccessRequired(AllocationType allocationType) { return allocationType == AllocationType::COMMAND_BUFFER || allocationType == AllocationType::CONSTANT_SURFACE || @@ -280,9 +288,6 @@ class GraphicsAllocation : public IDNode { constexpr static TaskCountType objectAlwaysResident = std::numeric_limits::max() - 1; std::atomic hostPtrTaskCountAssignment{0}; bool isShareableHostMemory = false; - bool isSubAllocSet = false; - StackVec subAllocSize; - StackVec subAllocBase; protected: struct UsageInfo { diff --git a/shared/source/os_interface/linux/drm_allocation.h b/shared/source/os_interface/linux/drm_allocation.h index 25dd4a96fd..a39565b3d4 100644 --- a/shared/source/os_interface/linux/drm_allocation.h +++ b/shared/source/os_interface/linux/drm_allocation.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 Intel Corporation + * Copyright (C) 2018-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -9,6 +9,7 @@ #include "shared/source/memory_manager/graphics_allocation.h" #include "shared/source/memory_manager/memadvise_flags.h" #include "shared/source/memory_manager/memory_manager.h" +#include "shared/source/os_interface/linux/drm_buffer_object.h" namespace NEO { class BufferObject; @@ -90,6 +91,14 @@ class DrmAllocation : public GraphicsAllocation { this->numHandles = numHandles; } + uint64_t getHandleAddressBase(uint32_t handleIndex) override { + return bufferObjects[handleIndex]->peekAddress(); + } + + size_t getHandleSize(uint32_t handleIndex) override { + return bufferObjects[handleIndex]->peekSize(); + } + int peekInternalHandle(MemoryManager *memoryManager, uint64_t &handle) override; int peekInternalHandle(MemoryManager *memoryManager, uint32_t handleId, uint64_t &handle) override; diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index fb73eff1b7..a2983b28fc 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -1861,9 +1861,6 @@ bool DrmMemoryManager::createDrmAllocation(Drm *drm, DrmAllocation *allocation, return false; } allocation->getBufferObjectToModify(currentBank + iterationOffset) = bos[handleId]; - allocation->isSubAllocSet = true; - allocation->subAllocSize[handleId] = static_cast(boSize); - allocation->subAllocBase[handleId] = static_cast(boAddress); if (storageInfo.multiStorage) { boAddress += boSize; } diff --git a/shared/test/common/mocks/mock_graphics_allocation.h b/shared/test/common/mocks/mock_graphics_allocation.h index 00100d67ba..0f5a2c16a0 100644 --- a/shared/test/common/mocks/mock_graphics_allocation.h +++ b/shared/test/common/mocks/mock_graphics_allocation.h @@ -23,14 +23,11 @@ class MockGraphicsAllocation : public MemoryAllocation { using MemoryAllocation::aubInfo; using MemoryAllocation::cpuPtr; using MemoryAllocation::gpuAddress; - using MemoryAllocation::isSubAllocSet; using MemoryAllocation::MemoryAllocation; using MemoryAllocation::memoryPool; using MemoryAllocation::objectNotResident; using MemoryAllocation::objectNotUsed; using MemoryAllocation::size; - using MemoryAllocation::subAllocBase; - using MemoryAllocation::subAllocSize; using MemoryAllocation::usageInfos; MockGraphicsAllocation() diff --git a/shared/test/unit_test/memory_manager/graphics_allocation_tests.cpp b/shared/test/unit_test/memory_manager/graphics_allocation_tests.cpp index c2dbc8deb6..bcf87ae86d 100644 --- a/shared/test/unit_test/memory_manager/graphics_allocation_tests.cpp +++ b/shared/test/unit_test/memory_manager/graphics_allocation_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 Intel Corporation + * Copyright (C) 2018-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -38,6 +38,13 @@ TEST(GraphicsAllocationTest, givenGraphicsAllocationWhenUpdatedTaskCountThenAllo graphicsAllocation.updateTaskCount(0u, 0u); EXPECT_TRUE(graphicsAllocation.isUsed()); } + +TEST(GraphicsAllocationTest, givenGraphicsAllocationGetDefaultHandleAddressAndHandleSize) { + MockGraphicsAllocation graphicsAllocation; + EXPECT_EQ(0lu, graphicsAllocation.getHandleAddressBase(0)); + EXPECT_EQ(0lu, graphicsAllocation.getHandleSize(0)); +} + TEST(GraphicsAllocationTest, givenGraphicsAllocationWhenUpdatedTaskCountThenOnlyOneTaskCountIsUpdated) { MockGraphicsAllocation graphicsAllocation; graphicsAllocation.updateTaskCount(1u, 0u); diff --git a/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp b/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp index 603864edf2..810d2b369d 100644 --- a/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp @@ -5270,14 +5270,13 @@ TEST_F(DrmMemoryManagerWithLocalMemoryAndExplicitExpectationsTest, givenMultiple auto drmAllocation = static_cast(allocation); auto &bos = drmAllocation->getBOs(); - EXPECT_TRUE(allocation->isSubAllocSet); auto boAddress = drmAllocation->getGpuAddress(); for (auto handleId = 0u; handleId < EngineLimits::maxHandleCount; handleId++) { auto bo = bos[handleId]; ASSERT_NE(nullptr, bo); auto boSize = allocation->getGmm(handleId)->gmmResourceInfo->getSizeAllocation(); - EXPECT_EQ(boAddress, allocation->subAllocBase[handleId]); - EXPECT_EQ(boSize, allocation->subAllocSize[handleId]); + EXPECT_EQ(boAddress, allocation->getHandleAddressBase(handleId)); + EXPECT_EQ(boSize, allocation->getHandleSize(handleId)); boAddress += boSize; } memoryManager->freeGraphicsMemory(allocation);