diff --git a/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp b/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp index 39edd96533..655836b868 100644 --- a/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp +++ b/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp @@ -2568,3 +2568,10 @@ TEST(MemoryManagerTest, givenDebugModuleAreaAllocationTypeWhenCallingGetAllocati mockMemoryManager.getAllocationData(allocData, properties, nullptr, mockMemoryManager.createStorageInfoFromProperties(properties)); EXPECT_EQ(1u, allocData.flags.use32BitFrontWindow); } + +TEST(MemoryManagerTest, WhenCallingIsAllocationTypeToCaptureThenScratchAndPrivateTypesReturnTrue) { + MockMemoryManager mockMemoryManager; + + EXPECT_TRUE(mockMemoryManager.isAllocationTypeToCapture(GraphicsAllocation::AllocationType::SCRATCH_SURFACE)); + EXPECT_TRUE(mockMemoryManager.isAllocationTypeToCapture(GraphicsAllocation::AllocationType::PRIVATE_SURFACE)); +} diff --git a/opencl/test/unit_test/mocks/linux/mock_drm_allocation.h b/opencl/test/unit_test/mocks/linux/mock_drm_allocation.h index a3fa4835ab..741034896c 100644 --- a/opencl/test/unit_test/mocks/linux/mock_drm_allocation.h +++ b/opencl/test/unit_test/mocks/linux/mock_drm_allocation.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2020 Intel Corporation + * Copyright (C) 2019-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -36,7 +36,13 @@ class MockDrmAllocation : public DrmAllocation { DrmAllocation::registerBOBindExtHandle(drm); } + void markForCapture() override { + markedForCapture = true; + DrmAllocation::markForCapture(); + } + bool registerBOBindExtHandleCalled = false; + bool markedForCapture = false; }; } // namespace NEO diff --git a/opencl/test/unit_test/mocks/mock_memory_manager.h b/opencl/test/unit_test/mocks/mock_memory_manager.h index 8d49ae0ee1..6c43cd0c5a 100644 --- a/opencl/test/unit_test/mocks/mock_memory_manager.h +++ b/opencl/test/unit_test/mocks/mock_memory_manager.h @@ -49,6 +49,7 @@ class MockMemoryManager : public MemoryManagerCreate { using OsAgnosticMemoryManager::allocateGraphicsMemoryForImageFromHostPtr; using MemoryManagerCreate::MemoryManagerCreate; using MemoryManager::enable64kbpages; + using MemoryManager::isAllocationTypeToCapture; using MemoryManager::isCopyRequired; using MemoryManager::localMemorySupported; using MemoryManager::reservedMemory; diff --git a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp index c68329b0c7..24ba9165b2 100644 --- a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp +++ b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp @@ -4081,6 +4081,32 @@ TEST(DrmMemoryManager, givenTrackedAllocationTypeAndDisabledRegistrationInDrmWhe EXPECT_EQ(Drm::ResourceClass::MaxSize, mockDrm->registeredClass); } +TEST(DrmMemoryManager, givenResourceRegistrationEnabledAndAllocTypeToCaptureWhenRegisteringAllocationInOsThenItIsMarkedForCapture) { + auto executionEnvironment = std::make_unique(); + executionEnvironment->prepareRootDeviceEnvironments(1u); + executionEnvironment->rootDeviceEnvironments[0]->setHwInfo(defaultHwInfo.get()); + auto memoryManager = std::make_unique(false, false, false, *executionEnvironment); + auto mockDrm = new DrmMockResources(*executionEnvironment->rootDeviceEnvironments[0]); + executionEnvironment->rootDeviceEnvironments[0]->osInterface = std::make_unique(); + executionEnvironment->rootDeviceEnvironments[0]->osInterface->get()->setDrm(mockDrm); + + // mock resource registration enabling by storing class handles + mockDrm->classHandles.push_back(1); + + MockBufferObject bo(mockDrm, 0, 0, 1); + MockDrmAllocation allocation(GraphicsAllocation::AllocationType::SCRATCH_SURFACE, MemoryPool::System4KBPages); + allocation.bufferObjects[0] = &bo; + memoryManager->registerAllocationInOs(&allocation); + + EXPECT_TRUE(allocation.markedForCapture); + + MockDrmAllocation allocation2(GraphicsAllocation::AllocationType::BUFFER, MemoryPool::System4KBPages); + allocation2.bufferObjects[0] = &bo; + memoryManager->registerAllocationInOs(&allocation2); + + EXPECT_FALSE(allocation2.markedForCapture); +} + TEST(DrmMemoryManager, givenTrackedAllocationTypeWhenAllocatingThenAllocationIsRegistered) { auto executionEnvironment = std::make_unique(); executionEnvironment->prepareRootDeviceEnvironments(1u); @@ -4279,6 +4305,22 @@ TEST(DrmAllocationTest, givenDrmAllocationWhenCacheRegionIsSetSuccessfullyThenSe } } +TEST(DrmAllocationTest, givenBoWhenMarkingForCaptureThenBosAreMarked) { + auto executionEnvironment = std::make_unique(); + executionEnvironment->prepareRootDeviceEnvironments(1); + + DrmMock drm(*executionEnvironment->rootDeviceEnvironments[0]); + + MockBufferObject bo(&drm, 0, 0, 1); + MockDrmAllocation allocation(GraphicsAllocation::AllocationType::SCRATCH_SURFACE, MemoryPool::System4KBPages); + allocation.markForCapture(); + + allocation.bufferObjects[0] = &bo; + allocation.markForCapture(); + + EXPECT_TRUE(bo.isMarkedForCapture()); +} + TEST_F(DrmMemoryManagerTest, givenDrmAllocationWithHostPtrWhenItIsCreatedWithCacheRegionThenSetRegionInBufferObject) { mock->ioctl_expected.total = -1; auto drm = static_cast(executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->osInterface->get()->getDrm()); diff --git a/shared/source/memory_manager/memory_manager.cpp b/shared/source/memory_manager/memory_manager.cpp index 21dd578a29..814b4fab0a 100644 --- a/shared/source/memory_manager/memory_manager.cpp +++ b/shared/source/memory_manager/memory_manager.cpp @@ -748,6 +748,17 @@ void MemoryManager::overrideAllocationData(AllocationData &allocationData, const } } +bool MemoryManager::isAllocationTypeToCapture(GraphicsAllocation::AllocationType type) const { + switch (type) { + case GraphicsAllocation::AllocationType::SCRATCH_SURFACE: + case GraphicsAllocation::AllocationType::PRIVATE_SURFACE: + return true; + default: + break; + } + return false; +} + bool MemoryTransferHelper::transferMemoryToAllocation(bool useBlitter, const Device &device, GraphicsAllocation *dstAllocation, size_t dstOffset, const void *srcMemory, size_t srcSize) { if (useBlitter) { return (BlitHelperFunctions::blitMemoryToAllocation(device, dstAllocation, dstOffset, srcMemory, {srcSize, 1, 1}) == BlitOperationResult::Success); diff --git a/shared/source/memory_manager/memory_manager.h b/shared/source/memory_manager/memory_manager.h index 8b7da52d52..0e890ec437 100644 --- a/shared/source/memory_manager/memory_manager.h +++ b/shared/source/memory_manager/memory_manager.h @@ -223,6 +223,7 @@ class MemoryManager { virtual void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0; virtual void freeAssociatedResourceImpl(GraphicsAllocation &graphicsAllocation) { return unlockResourceImpl(graphicsAllocation); }; virtual void registerAllocationInOs(GraphicsAllocation *allocation) {} + bool isAllocationTypeToCapture(GraphicsAllocation::AllocationType type) const; bool initialized = false; bool forceNonSvmForExternalHostPtr = false; diff --git a/shared/source/os_interface/linux/drm_allocation.cpp b/shared/source/os_interface/linux/drm_allocation.cpp index 4d2e9a2c32..ad41d5a44c 100644 --- a/shared/source/os_interface/linux/drm_allocation.cpp +++ b/shared/source/os_interface/linux/drm_allocation.cpp @@ -147,4 +147,13 @@ void DrmAllocation::freeRegisteredBOBindExtHandles(Drm *drm) { drm->unregisterResource(i); } } + +void DrmAllocation::markForCapture() { + auto &bos = getBOs(); + for (auto bo : bos) { + if (bo) { + bo->markForCapture(); + } + } +} } // namespace NEO diff --git a/shared/source/os_interface/linux/drm_allocation.h b/shared/source/os_interface/linux/drm_allocation.h index bf9f366d0a..1020d196ef 100644 --- a/shared/source/os_interface/linux/drm_allocation.h +++ b/shared/source/os_interface/linux/drm_allocation.h @@ -78,6 +78,7 @@ class DrmAllocation : public GraphicsAllocation { MOCKABLE_VIRTUAL void registerBOBindExtHandle(Drm *drm); void freeRegisteredBOBindExtHandles(Drm *drm); void linkWithRegisteredHandle(uint32_t handle); + MOCKABLE_VIRTUAL void markForCapture(); protected: BufferObjects bufferObjects{}; diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index 1c85b62875..b7e6d5ca64 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -995,6 +995,10 @@ void DrmMemoryManager::registerAllocationInOs(GraphicsAllocation *allocation) { if (allocation && getDrm(allocation->getRootDeviceIndex()).resourceRegistrationEnabled()) { auto drmAllocation = static_cast(allocation); drmAllocation->registerBOBindExtHandle(&getDrm(drmAllocation->getRootDeviceIndex())); + + if (isAllocationTypeToCapture(drmAllocation->getAllocationType())) { + drmAllocation->markForCapture(); + } } } } // namespace NEO