Debian Codegraph @

opencl/test/unit_test/sharings/unified/unified_sharing_tests.cpp

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