| 1 | /* |
| 2 | * Copyright (C) 2019-2025 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "shared/test/common/helpers/variable_backup.h" |
| 9 | #include "shared/test/common/mocks/mock_device.h" |
| 10 | #include "shared/test/common/mocks/mock_memory_manager.h" |
| 11 | #include "shared/test/common/test_macros/test.h" |
| 12 | |
| 13 | #include "opencl/source/mem_obj/buffer.h" |
| 14 | #include "opencl/source/sharings/unified/enable_unified.h" |
| 15 | #include "opencl/source/sharings/unified/unified_buffer.h" |
| 16 | #include "opencl/source/sharings/unified/unified_sharing.h" |
| 17 | #include "opencl/test/unit_test/mocks/mock_cl_device.h" |
| 18 | #include "opencl/test/unit_test/mocks/mock_context.h" |
| 19 | #include "opencl/test/unit_test/sharings/unified/unified_sharing_fixtures.h" |
| 20 | |
| 21 | namespace NEO { |
| 22 | class MemoryManager; |
| 23 | } // namespace NEO |
| 24 | |
| 25 | using namespace NEO; |
| 26 | |
| 27 | TEST(UnifiedSharingTests, givenContextCreatedWithExternalDeviceHandlePropertyWhenGettingUnifiedSharingThenReturnIt) { |
| 28 | MockClDevice device{MockDevice::createWithNewExecutionEnvironment<MockDevice>(defaultHwInfo.get())}; |
| 29 | cl_device_id deviceId = &device; |
| 30 | ClDeviceVector allDevs(&deviceId, 1); |
| 31 | cl_int retVal{}; |
| 32 | |
| 33 | const cl_context_properties contextProps[] = { |
| 34 | static_cast<cl_context_properties>(UnifiedSharingContextType::deviceHandle), 0, |
| 35 | CL_CONTEXT_INTEROP_USER_SYNC, 1, |
| 36 | 0}; |
| 37 | |
| 38 | auto context = std::unique_ptr<MockContext>(Context::create<MockContext>(contextProps, allDevs, |
| 39 | nullptr, nullptr, retVal)); |
| 40 | auto sharingFunctions = context->getSharing<UnifiedSharingFunctions>(); |
| 41 | EXPECT_NE(nullptr, sharingFunctions); |
| 42 | } |
| 43 | |
| 44 | struct MockUnifiedSharingContextBuilder : UnifiedSharingContextBuilder { |
| 45 | using UnifiedSharingContextBuilder::contextData; |
| 46 | }; |
| 47 | |
| 48 | TEST(UnifiedSharingTests, givenExternalDeviceHandleWhenProcessingBySharingContextBuilderThenResultIsTrue) { |
| 49 | MockUnifiedSharingContextBuilder builder{}; |
| 50 | cl_context_properties propertyType = static_cast<cl_context_properties>(UnifiedSharingContextType::deviceHandle); |
| 51 | cl_context_properties propertyValue = 0x1234; |
| 52 | bool result = builder.processProperties(propertyType, propertyValue); |
| 53 | EXPECT_TRUE(result); |
| 54 | EXPECT_NE(nullptr, builder.contextData); |
| 55 | } |
| 56 | |
| 57 | TEST(UnifiedSharingTests, givenExternalDeviceGroupHandleWhenProcessingBySharingContextBuilderThenResultIsTrue) { |
| 58 | MockUnifiedSharingContextBuilder builder{}; |
| 59 | cl_context_properties propertyType = static_cast<cl_context_properties>(UnifiedSharingContextType::deviceGroup); |
| 60 | cl_context_properties propertyValue = 0x1234; |
| 61 | bool result = builder.processProperties(propertyType, propertyValue); |
| 62 | EXPECT_TRUE(result); |
| 63 | EXPECT_NE(nullptr, builder.contextData); |
| 64 | } |
| 65 | |
| 66 | TEST(UnifiedSharingTests, givenExternalDeviceGroupHandleWhenProcessingBySharingContextBuilderThenReturnSuccess) { |
| 67 | MockUnifiedSharingContextBuilder builder{}; |
| 68 | cl_context_properties propertyType = CL_CONTEXT_PLATFORM; |
| 69 | cl_context_properties propertyValue = 0x1234; |
| 70 | bool result = builder.processProperties(propertyType, propertyValue); |
| 71 | EXPECT_FALSE(result); |
| 72 | EXPECT_EQ(nullptr, builder.contextData); |
| 73 | } |
| 74 | |
| 75 | TEST(UnifiedSharingTests, givenContextWithUserSyncWhenFinalizingPropertiesBySharingContextBuilderThenRegisterSharingInContextAndClearContextData) { |
| 76 | MockUnifiedSharingContextBuilder builder{}; |
| 77 | builder.contextData = std::make_unique<UnifiedCreateContextProperties>(); |
| 78 | |
| 79 | MockContext context{}; |
| 80 | context.setInteropUserSyncEnabled(true); |
| 81 | cl_int retVal{}; |
| 82 | bool result = builder.finalizeProperties(context, retVal); |
| 83 | EXPECT_TRUE(result); |
| 84 | EXPECT_EQ(CL_SUCCESS, retVal); |
| 85 | EXPECT_NE(nullptr, context.sharingFunctions[UnifiedSharingFunctions::sharingId]); |
| 86 | EXPECT_EQ(nullptr, builder.contextData); |
| 87 | } |
| 88 | |
| 89 | TEST(UnifiedSharingTests, givenContextWithoutUserSyncWhenFinalizingPropertiesBySharingContextBuilderThenDoNotRegisterSharingInContextAndClearContextData) { |
| 90 | MockUnifiedSharingContextBuilder builder{}; |
| 91 | builder.contextData = std::make_unique<UnifiedCreateContextProperties>(); |
| 92 | |
| 93 | MockContext context{}; |
| 94 | context.setInteropUserSyncEnabled(false); |
| 95 | cl_int retVal{}; |
| 96 | bool result = builder.finalizeProperties(context, retVal); |
| 97 | EXPECT_TRUE(result); |
| 98 | EXPECT_EQ(CL_SUCCESS, retVal); |
| 99 | EXPECT_EQ(nullptr, context.sharingFunctions[UnifiedSharingFunctions::sharingId]); |
| 100 | EXPECT_EQ(nullptr, builder.contextData); |
| 101 | } |
| 102 | |
| 103 | TEST(UnifiedSharingTests, givenBuilderWithoutContextDataWhenFinalizingPropertiesBySharingContextBuilderThenDoNotRegisterSharingInContext) { |
| 104 | MockUnifiedSharingContextBuilder builder{}; |
| 105 | |
| 106 | MockContext context{}; |
| 107 | cl_int retVal{}; |
| 108 | bool result = builder.finalizeProperties(context, retVal); |
| 109 | EXPECT_TRUE(result); |
| 110 | EXPECT_EQ(CL_SUCCESS, retVal); |
| 111 | EXPECT_EQ(nullptr, context.sharingFunctions[UnifiedSharingFunctions::sharingId]); |
| 112 | EXPECT_EQ(nullptr, builder.contextData); |
| 113 | } |
| 114 | |
| 115 | TEST(UnifiedSharingTests, givenSharingHandlerThenItReturnsCorrectValues) { |
| 116 | UnifiedSharingFunctions sharingFunctions; |
| 117 | EXPECT_EQ(UnifiedSharingFunctions::sharingId, sharingFunctions.getId()); |
| 118 | UnifiedSharing sharingHandler{&sharingFunctions, UnifiedSharingHandleType::linuxFd}; |
| 119 | EXPECT_EQ(&sharingFunctions, sharingHandler.peekFunctionsHandler()); |
| 120 | EXPECT_EQ(UnifiedSharingHandleType::linuxFd, sharingHandler.getExternalMemoryType()); |
| 121 | } |
| 122 | |
| 123 | using UnifiedSharingTestsWithMemoryManager = UnifiedSharingFixture<true, true>; |
| 124 | |
| 125 | TEST_F(UnifiedSharingTestsWithMemoryManager, givenUnifiedSharingHandlerWhenAcquiringAndReleasingThenMethodsAreCalledAppropriately) { |
| 126 | struct MockSharingHandler : UnifiedSharing { |
| 127 | using UnifiedSharing::UnifiedSharing; |
| 128 | unsigned int synchronizeObjectCalled = 0u; |
| 129 | unsigned int releaseResourceCalled = 0u; |
| 130 | void synchronizeObject(UpdateData &updateData) override { |
| 131 | UnifiedSharing::synchronizeObject(updateData); |
| 132 | synchronizeObjectCalled++; |
| 133 | } |
| 134 | void releaseResource(MemObj *memObject, uint32_t rootDeviceIndex) override { |
| 135 | UnifiedSharing::releaseResource(memObject, rootDeviceIndex); |
| 136 | releaseResourceCalled++; |
| 137 | }; |
| 138 | }; |
| 139 | |
| 140 | cl_mem_flags flags{}; |
| 141 | UnifiedSharingMemoryDescription desc{}; |
| 142 | desc.handle = reinterpret_cast<void *>(0x1234); |
| 143 | desc.type = UnifiedSharingHandleType::win32Nt; |
| 144 | cl_int retVal{}; |
| 145 | auto buffer = std::unique_ptr<Buffer>(UnifiedBuffer::createSharedUnifiedBuffer(context.get(), flags, desc, &retVal)); |
| 146 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 147 | |
| 148 | UnifiedSharingFunctions sharingFunctions; |
| 149 | MockSharingHandler *sharingHandler = new MockSharingHandler(&sharingFunctions, desc.type); |
| 150 | buffer->setSharingHandler(sharingHandler); |
| 151 | |
| 152 | ASSERT_EQ(0u, sharingHandler->synchronizeObjectCalled); |
| 153 | |
| 154 | ASSERT_EQ(CL_SUCCESS, sharingHandler->acquire(buffer.get(), context->getDevice(0)->getRootDeviceIndex())); |
| 155 | EXPECT_EQ(1u, sharingHandler->synchronizeObjectCalled); |
| 156 | |
| 157 | ASSERT_EQ(CL_SUCCESS, sharingHandler->acquire(buffer.get(), context->getDevice(0)->getRootDeviceIndex())); |
| 158 | EXPECT_EQ(1u, sharingHandler->synchronizeObjectCalled); |
| 159 | |
| 160 | ASSERT_EQ(0u, sharingHandler->releaseResourceCalled); |
| 161 | sharingHandler->release(buffer.get(), context->getDevice(0)->getRootDeviceIndex()); |
| 162 | EXPECT_EQ(0u, sharingHandler->releaseResourceCalled); |
| 163 | sharingHandler->release(buffer.get(), context->getDevice(0)->getRootDeviceIndex()); |
| 164 | EXPECT_EQ(1u, sharingHandler->releaseResourceCalled); |
| 165 | } |
| 166 | |
| 167 | struct UnifiedSharingCreateAllocationTests : UnifiedSharingTestsWithMemoryManager { |
| 168 | struct MemoryManagerCheckingAllocationMethod : MockMemoryManager { |
| 169 | using MockMemoryManager::MockMemoryManager; |
| 170 | |
| 171 | GraphicsAllocation *createGraphicsAllocationFromSharedHandle(const OsHandleData &osHandleData, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) override { |
| 172 | this->createFromSharedHandleCalled = true; |
| 173 | this->properties = std::make_unique<AllocationProperties>(properties); |
| 174 | this->handle = osHandleData.handle; |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
| 178 | bool createFromSharedHandleCalled = false; |
| 179 | osHandle handle; |
| 180 | std::unique_ptr<AllocationProperties> properties; |
| 181 | }; |
| 182 | |
| 183 | struct MockSharingHandler : UnifiedSharing { |
| 184 | using UnifiedSharing::createMultiGraphicsAllocation; |
| 185 | }; |
| 186 | |
| 187 | void SetUp() override { |
| 188 | UnifiedSharingTestsWithMemoryManager::SetUp(); |
| 189 | this->memoryManager = std::make_unique<MemoryManagerCheckingAllocationMethod>(); |
| 190 | this->memoryManagerBackup = std::make_unique<VariableBackup<MemoryManager *>>(&this->context->memoryManager, this->memoryManager.get()); |
| 191 | } |
| 192 | |
| 193 | std::unique_ptr<MemoryManagerCheckingAllocationMethod> memoryManager; |
| 194 | std::unique_ptr<VariableBackup<MemoryManager *>> memoryManagerBackup; |
| 195 | }; |
| 196 | |
| 197 | TEST_F(UnifiedSharingCreateAllocationTests, givenWindowsNtHandleWhenCreateGraphicsAllocationIsCalledThenUseSharedHandleMethod) { |
| 198 | UnifiedSharingMemoryDescription desc{}; |
| 199 | desc.handle = reinterpret_cast<void *>(0x1234); |
| 200 | desc.type = UnifiedSharingHandleType::win32Nt; |
| 201 | AllocationType allocationType = AllocationType::sharedImage; |
| 202 | MockSharingHandler::createMultiGraphicsAllocation(this->context.get(), desc, nullptr, allocationType, nullptr); |
| 203 | |
| 204 | EXPECT_TRUE(memoryManager->createFromSharedHandleCalled); |
| 205 | EXPECT_EQ(toOsHandle(desc.handle), memoryManager->handle); |
| 206 | } |
| 207 | |
| 208 | TEST_F(UnifiedSharingCreateAllocationTests, givenWindowsSharedHandleWhenCreateGraphicsAllocationIsCalledThenUseSharedHandleMethod) { |
| 209 | UnifiedSharingMemoryDescription desc{}; |
| 210 | desc.handle = reinterpret_cast<void *>(0x1234); |
| 211 | desc.type = UnifiedSharingHandleType::win32Shared; |
| 212 | AllocationType allocationType = AllocationType::sharedImage; |
| 213 | MockSharingHandler::createMultiGraphicsAllocation(this->context.get(), desc, nullptr, allocationType, nullptr); |
| 214 | |
| 215 | EXPECT_TRUE(memoryManager->createFromSharedHandleCalled); |
| 216 | EXPECT_EQ(toOsHandle(desc.handle), memoryManager->handle); |
| 217 | const AllocationProperties expectedProperties{0u, false, 0u, allocationType, false, {}}; |
| 218 | EXPECT_EQ(expectedProperties.allFlags, memoryManager->properties->allFlags); |
| 219 | } |
| 220 | |
| 221 | TEST_F(UnifiedSharingCreateAllocationTests, givenLinuxSharedHandleWhenCreateGraphicsAllocationIsCalledThenUseSharedHandleMethod) { |
| 222 | UnifiedSharingMemoryDescription desc{}; |
| 223 | desc.handle = reinterpret_cast<void *>(0x1234); |
| 224 | desc.type = UnifiedSharingHandleType::linuxFd; |
| 225 | AllocationType allocationType = AllocationType::sharedImage; |
| 226 | MockSharingHandler::createMultiGraphicsAllocation(this->context.get(), desc, nullptr, allocationType, nullptr); |
| 227 | |
| 228 | EXPECT_TRUE(memoryManager->createFromSharedHandleCalled); |
| 229 | EXPECT_EQ(toOsHandle(desc.handle), memoryManager->handle); |
| 230 | const AllocationProperties expectedProperties{0u, false, 0u, allocationType, false, {}}; |
| 231 | EXPECT_EQ(expectedProperties.allFlags, memoryManager->properties->allFlags); |
| 232 | } |