Debian Codegraph @

opencl/test/unit_test/sharings/gl/linux/gl_arb_sync_event_tests.cpp

raw
1 /*
2 * Copyright (C) 2018-2023 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/source/execution_environment/execution_environment.h"
9 #include "shared/source/memory_manager/os_agnostic_memory_manager.h"
10 #include "shared/source/os_interface/os_interface.h"
11 #include "shared/test/common/mocks/mock_csr.h"
12 #include "shared/test/common/mocks/mock_device.h"
13 #include "shared/test/common/test_macros/test.h"
14
15 #include "opencl/source/context/context.h"
16 #include "opencl/source/event/user_event.h"
17 #include "opencl/source/platform/platform.h"
18 #include "opencl/source/sharings/gl/gl_arb_sync_event.h"
19 #include "opencl/source/sharings/sharing.h"
20 #include "opencl/test/unit_test/mocks/gl/linux/mock_gl_arb_sync_event_linux.h"
21 #include "opencl/test/unit_test/mocks/gl/linux/mock_gl_sharing_linux.h"
22 #include "opencl/test/unit_test/mocks/mock_cl_device.h"
23 #include "opencl/test/unit_test/mocks/mock_command_queue.h"
24 #include "opencl/test/unit_test/mocks/mock_context.h"
25 #include "opencl/test/unit_test/mocks/mock_event.h"
26 #include "opencl/test/unit_test/mocks/mock_platform.h"
27
28 using namespace NEO;
29
30 TEST(GlArbSyncEvent, whenCreateArbSyncEventNameIsCalledMultipleTimesThenEachCallReturnsUniqueName) {
31 char *name1 = NEO::createArbSyncEventName();
32 EXPECT_NE(nullptr, name1);
33 EXPECT_STRNE("", name1);
34
35 char *name2 = NEO::createArbSyncEventName();
36 EXPECT_NE(nullptr, name2);
37 EXPECT_STRNE("", name2);
38
39 char *name3 = NEO::createArbSyncEventName();
40 EXPECT_NE(nullptr, name3);
41 EXPECT_STRNE("", name3);
42
43 EXPECT_STRNE(name1, name2);
44 EXPECT_STRNE(name1, name3);
45 EXPECT_STRNE(name2, name3);
46
47 NEO::destroyArbSyncEventName(name1);
48 NEO::destroyArbSyncEventName(name2);
49 NEO::destroyArbSyncEventName(name3);
50 }
51
52 template <bool signalWaited>
53 inline void glArbSyncObjectWaitServerMock(NEO::OSInterface &osInterface, CL_GL_SYNC_INFO &glSyncInfo) {
54 glSyncInfo.waitCalled = signalWaited;
55 }
56
57 struct MockBaseEvent : Event {
58 using Event::Event;
59 bool wasUpdated = false;
60 void updateExecutionStatus() override {
61 Event::updateExecutionStatus();
62 wasUpdated = true;
63 }
64 };
65
66 struct GlArbSyncEventTest : public ::testing::Test {
67 GlArbSyncEventTest(void) {
68 }
69
70 void SetUp() override {
71 executionEnvironment = platform()->peekExecutionEnvironment();
72 executionEnvironment->memoryManager = std::make_unique<OsAgnosticMemoryManager>(*executionEnvironment);
73 device = std::make_unique<MockClDevice>(MockDevice::create<MockDevice>(executionEnvironment, 0u));
74 auto mockCsr = new MockCommandStreamReceiver(*executionEnvironment, 0, device->getDeviceBitfield());
75 device->resetCommandStreamReceiver(mockCsr);
76 ctx.reset(new MockContext);
77 cmdQ.reset(new MockCommandQueue(ctx.get(), device.get(), nullptr, false));
78 sharing = new GlSharingFunctionsMock();
79 ctx->setSharingFunctions(sharing);
80 sharing->pfnGlArbSyncObjectCleanup = glArbSyncObjectCleanupMockDoNothing;
81 sharing->pfnGlArbSyncObjectSetup = mockGlArbSyncObjectSetup<false>;
82 sharing->pfnGlArbSyncObjectSignal = glArbSyncObjectSignalMockDoNothing;
83 sharing->pfnGlArbSyncObjectWaitServer = glArbSyncObjectWaitServerMock<false>;
84 osInterface = new OSInterface;
85 executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(osInterface);
86 }
87
88 void TearDown() override {
89 if (baseEvent) {
90 triggerEvent->setStatus(-1);
91 baseEvent->release();
92 triggerEvent->release();
93 }
94 }
95
96 template <typename T>
97 T *createArbEventMock() {
98 T *ret = new T(*ctx);
99 ret->osInterface = osInterface;
100
101 ret->baseEvent = getBaseEvent();
102 baseEvent->incRefInternal();
103 baseEvent->addChild(*ret);
104
105 return ret;
106 }
107
108 MockBaseEvent *getBaseEvent() {
109 if (baseEvent == nullptr) {
110 triggerEvent = new UserEvent(ctx.get());
111 baseEvent = new MockBaseEvent(cmdQ.get(), CL_COMMAND_RELEASE_GL_OBJECTS, CompletionStamp::notReady, CompletionStamp::notReady);
112 triggerEvent->addChild(*baseEvent);
113 }
114 return baseEvent;
115 }
116
117 void failSyncObjectCreation() {
118 sharing->pfnGlArbSyncObjectSetup = mockGlArbSyncObjectSetup<true>;
119 }
120
121 void setWaitCalledFlagOnServerWait() {
122 sharing->pfnGlArbSyncObjectWaitServer = glArbSyncObjectWaitServerMock<true>;
123 }
124
125 std::unique_ptr<MockClDevice> device;
126 std::unique_ptr<MockContext> ctx;
127 std::unique_ptr<MockCommandQueue> cmdQ;
128 OSInterface *osInterface = nullptr;
129 Event *triggerEvent = nullptr;
130 MockBaseEvent *baseEvent = nullptr;
131 GlSharingFunctionsMock *sharing = nullptr;
132 ExecutionEnvironment *executionEnvironment = nullptr;
133 };
134
135 TEST_F(GlArbSyncEventTest, whenGlArbEventIsCreatedThenBaseEventObjectIsConstructedWithProperContextAndCommandType) {
136 auto *syncEv = createArbEventMock<DummyArbEvent<false>>();
137
138 EXPECT_EQ(static_cast<unsigned int>(CL_COMMAND_GL_FENCE_SYNC_OBJECT_KHR), syncEv->getCommandType());
139 EXPECT_EQ(ctx.get(), syncEv->getContext());
140 EXPECT_NE(nullptr, syncEv->glSyncInfo);
141
142 syncEv->release();
143 }
144
145 TEST_F(GlArbSyncEventTest, whenGetSyncInfoIsCalledThenEventsSyncInfoIsReturned) {
146 auto *syncEv = createArbEventMock<DummyArbEvent<false>>();
147
148 EXPECT_NE(nullptr, syncEv->glSyncInfo);
149 EXPECT_EQ(syncEv->glSyncInfo.get(), syncEv->getSyncInfo());
150
151 syncEv->release();
152 }
153
154 TEST_F(GlArbSyncEventTest, whenSetBaseEventIsCalledThenProperMembersOfParentEventAreCopiedToSyncEventAndReferenceCountersAreUpdated) {
155 ASSERT_NE(nullptr, getBaseEvent()->getCommandQueue());
156 EXPECT_EQ(2, getBaseEvent()->getRefInternalCount());
157 EXPECT_EQ(2, getBaseEvent()->getCommandQueue()->getRefInternalCount());
158 EXPECT_FALSE(getBaseEvent()->peekHasChildEvents());
159 auto *syncEv = new DummyArbEvent<false>(*ctx);
160
161 EXPECT_EQ(nullptr, syncEv->baseEvent);
162 EXPECT_EQ(nullptr, syncEv->osInterface);
163 EXPECT_EQ(nullptr, syncEv->getCommandQueue());
164
165 syncEv->useBaseSetEvent = true;
166 bool ret = syncEv->setBaseEvent(*getBaseEvent());
167 EXPECT_TRUE(ret);
168
169 EXPECT_TRUE(getBaseEvent()->peekHasChildEvents());
170 EXPECT_EQ(getBaseEvent(), syncEv->baseEvent);
171 EXPECT_EQ(getBaseEvent()->getCommandQueue(), syncEv->getCommandQueue());
172 EXPECT_EQ(syncEv->getCommandQueue()->getGpgpuCommandStreamReceiver().getOSInterface(), syncEv->osInterface);
173
174 EXPECT_EQ(3, getBaseEvent()->getRefInternalCount());
175 EXPECT_EQ(3, getBaseEvent()->getCommandQueue()->getRefInternalCount());
176 EXPECT_TRUE(getBaseEvent()->peekHasChildEvents());
177
178 syncEv->release();
179 }
180
181 TEST_F(GlArbSyncEventTest, whenSetBaseEventIsCalledButGlArbSyncObjectCreationFailsThenOperationIsAborted) {
182 ASSERT_NE(nullptr, getBaseEvent()->getCommandQueue());
183 EXPECT_EQ(2, getBaseEvent()->getRefInternalCount());
184 EXPECT_EQ(2, getBaseEvent()->getCommandQueue()->getRefInternalCount());
185 EXPECT_FALSE(getBaseEvent()->peekHasChildEvents());
186 auto *syncEv = new DummyArbEvent<false>(*ctx);
187
188 EXPECT_EQ(nullptr, syncEv->baseEvent);
189 EXPECT_EQ(nullptr, syncEv->osInterface);
190 EXPECT_EQ(nullptr, syncEv->getCommandQueue());
191
192 syncEv->useBaseSetEvent = true;
193 failSyncObjectCreation();
194 bool ret = syncEv->setBaseEvent(*getBaseEvent());
195 EXPECT_FALSE(ret);
196
197 EXPECT_EQ(2, getBaseEvent()->getRefInternalCount());
198 EXPECT_EQ(2, getBaseEvent()->getCommandQueue()->getRefInternalCount());
199 EXPECT_FALSE(getBaseEvent()->peekHasChildEvents());
200
201 EXPECT_EQ(nullptr, syncEv->baseEvent);
202 EXPECT_EQ(nullptr, syncEv->osInterface);
203 EXPECT_EQ(nullptr, syncEv->getCommandQueue());
204
205 syncEv->osInterface = this->osInterface;
206 syncEv->baseEvent = getBaseEvent();
207 getBaseEvent()->incRefInternal();
208 syncEv->release();
209 }
210
211 TEST_F(GlArbSyncEventTest, whenGlArbSyncEventGetsUnblockedByTerminatedBaseEventThenSyncObjectDoesntGetSignalled) {
212 auto *syncEv = createArbEventMock<DummyArbEvent<false>>();
213 triggerEvent->setStatus(-1);
214 EXPECT_FALSE(syncEv->getSyncInfo()->waitCalled);
215 syncEv->release();
216 }
217
218 TEST_F(GlArbSyncEventTest, whenGlArbSyncEventGetsUnblockedByQueuedBaseEventThenSyncObjectDoesntGetSignalled) {
219 auto *syncEv = createArbEventMock<DummyArbEvent<false>>();
220 syncEv->unblockEventBy(*this->baseEvent, 0, CL_QUEUED);
221 EXPECT_FALSE(syncEv->getSyncInfo()->waitCalled);
222 syncEv->release();
223 }
224
225 TEST_F(GlArbSyncEventTest, whenGlArbSyncEventGetsUnblockedBySubmittedOrCompletedEventThenSyncObjectGetsSignalled) {
226 setWaitCalledFlagOnServerWait();
227
228 auto *syncEv = createArbEventMock<DummyArbEvent<false>>();
229 triggerEvent->setStatus(CL_COMPLETE);
230 EXPECT_TRUE(syncEv->getSyncInfo()->waitCalled);
231 syncEv->release();
232 }
233
234 TEST_F(GlArbSyncEventTest, whenGlArbSyncEventIsCreatedFromBaseEventWithoutValidContextThenCreationFails) {
235 Event *baseEvent = new Event(nullptr, CL_COMMAND_RELEASE_GL_OBJECTS, CompletionStamp::notReady, CompletionStamp::notReady);
236 auto *arbEvent = GlArbSyncEvent::create(*baseEvent);
237 EXPECT_EQ(nullptr, arbEvent);
238 baseEvent->release();
239 }
240
241 TEST_F(GlArbSyncEventTest, whenGlArbSyncEventIsCreatedAndSetEventFailsThenCreationFails) {
242 failSyncObjectCreation();
243 auto *arbEvent = GlArbSyncEvent::create(*this->getBaseEvent());
244 EXPECT_EQ(nullptr, arbEvent);
245 }
246
247 TEST_F(GlArbSyncEventTest, whenGlArbSyncEventIsCreatedThenBaseEventIsProperlySet) {
248 auto *arbEvent = GlArbSyncEvent::create(*this->getBaseEvent());
249 EXPECT_NE(nullptr, arbEvent);
250 EXPECT_TRUE(this->baseEvent->peekHasChildEvents());
251 EXPECT_EQ(arbEvent, this->baseEvent->peekChildEvents()->ref);
252 arbEvent->release();
253 }
254
255 TEST_F(GlArbSyncEventTest, whenClEnqueueMarkerWithSyncObjectINTELIsCalledThenInvalidOperationErrorCodeIsReturned) {
256 cl_command_queue queue = static_cast<cl_command_queue>(this->cmdQ.get());
257 auto ret = clEnqueueMarkerWithSyncObjectINTEL(queue, nullptr, nullptr);
258 EXPECT_EQ(CL_INVALID_OPERATION, ret);
259 }
260
261 TEST_F(GlArbSyncEventTest, whenClGetCLObjectInfoINTELIsCalledThenInvalidOperationErrorCodeIsReturned) {
262 cl_mem mem = {};
263 auto ret = clGetCLObjectInfoINTEL(mem, nullptr);
264 EXPECT_EQ(CL_INVALID_OPERATION, ret);
265 }
266
267 TEST_F(GlArbSyncEventTest, givenNullSynInfoParameterWhenClGetCLEventInfoINTELIsCalledThenInvalidArgValueErrorCodeIsReturned) {
268 cl_event ev = getBaseEvent();
269 cl_context ctxRet = {};
270 auto ret = clGetCLEventInfoINTEL(ev, nullptr, &ctxRet);
271 EXPECT_EQ(CL_INVALID_ARG_VALUE, ret);
272 }
273
274 TEST_F(GlArbSyncEventTest, givenNullContextParameterWhenClGetCLEventInfoINTELIsCalledThenInvalidArgValueErrorCodeIsReturned) {
275 cl_event ev = getBaseEvent();
276 CL_GL_SYNC_INFO *synInfoRet = nullptr;
277 auto ret = clGetCLEventInfoINTEL(ev, &synInfoRet, nullptr);
278 EXPECT_EQ(CL_INVALID_ARG_VALUE, ret);
279 }
280
281 TEST_F(GlArbSyncEventTest, givenUnknownEventWhenclGetCLEventInfoINTELIsCalledThenInvalidEventErrorCodeIsReturned) {
282 auto deadEvent = new MockEvent<Event>(nullptr, 0, 0, 0);
283 deadEvent->magic = Event::deadMagic;
284 cl_event unknownEvent = deadEvent;
285 CL_GL_SYNC_INFO *synInfoRet = nullptr;
286 cl_context ctxRet = {};
287 auto ret = clGetCLEventInfoINTEL(unknownEvent, &synInfoRet, &ctxRet);
288 EXPECT_EQ(CL_INVALID_EVENT, ret);
289 deadEvent->release();
290 }
291
292 TEST_F(GlArbSyncEventTest, givenEventWithCommandDifferentThanReleaseGlObjectsWhenClGetCLEventInfoINTELIsCalledThenValidContextIsReturned) {
293 getBaseEvent();
294 cl_event ev = triggerEvent;
295 CL_GL_SYNC_INFO *synInfoRet = reinterpret_cast<CL_GL_SYNC_INFO *>(static_cast<uintptr_t>(0xFF));
296 cl_context ctxRet = {};
297 auto ret = clGetCLEventInfoINTEL(ev, &synInfoRet, &ctxRet);
298 EXPECT_EQ(CL_SUCCESS, ret);
299 EXPECT_EQ(nullptr, synInfoRet);
300 EXPECT_EQ(ctxRet, ctx.get());
301 }
302
303 TEST_F(GlArbSyncEventTest, givenDisabledSharingWhenClGetCLEventInfoINTELIsCalledThenInvalidOperationErrorCodeIsReturned) {
304 getBaseEvent();
305 cl_event ev = baseEvent;
306 CL_GL_SYNC_INFO *synInfoRet = reinterpret_cast<CL_GL_SYNC_INFO *>(static_cast<uintptr_t>(0xFF));
307 cl_context ctxRet = {};
308 auto sharing = ctx->getSharing<NEO::GLSharingFunctions>();
309 ctx->sharingFunctions[sharing->getId()] = nullptr;
310 auto ret = clGetCLEventInfoINTEL(ev, &synInfoRet, &ctxRet);
311 ctx->setSharingFunctions(new GlSharingFunctionsMock());
312 EXPECT_EQ(CL_INVALID_OPERATION, ret);
313 }
314
315 TEST_F(GlArbSyncEventTest, givenCallToClGetCLEventInfoINTELWhenGetOrCreateGlArbSyncFailsThenOutOfMemoryErrorCodeIsReturned) {
316 getBaseEvent();
317 cl_event ev = this->baseEvent;
318 CL_GL_SYNC_INFO *synInfoRet = reinterpret_cast<CL_GL_SYNC_INFO *>(static_cast<uintptr_t>(0xFF));
319 cl_context ctxRet = {};
320 sharing->pfnGlArbSyncObjectSetup = mockGlArbSyncObjectSetup<true>;
321 auto ret = clGetCLEventInfoINTEL(ev, &synInfoRet, &ctxRet);
322 EXPECT_EQ(CL_OUT_OF_RESOURCES, ret);
323 }
324
325 TEST_F(GlArbSyncEventTest, givenCallToClGetCLEventInfoINTELWhenFunctionSucceedsThenEventsGetUpdatedAndValidContextAndSyncInfoAreReturned) {
326 auto *arbEvent = GlArbSyncEvent::create(*this->getBaseEvent());
327 this->sharing->glArbEventMapping[this->baseEvent] = arbEvent;
328 cl_event ev = this->baseEvent;
329 CL_GL_SYNC_INFO *synInfoRet = reinterpret_cast<CL_GL_SYNC_INFO *>(static_cast<uintptr_t>(0xFF));
330 cl_context ctxRet = {};
331 EXPECT_FALSE(this->baseEvent->wasUpdated);
332 auto ret = clGetCLEventInfoINTEL(ev, &synInfoRet, &ctxRet);
333 EXPECT_TRUE(this->baseEvent->wasUpdated);
334 EXPECT_EQ(CL_SUCCESS, ret);
335 EXPECT_EQ(ctx.get(), ctxRet);
336 EXPECT_EQ(arbEvent->getSyncInfo(), synInfoRet);
337 arbEvent->release();
338 }
339
340 TEST_F(GlArbSyncEventTest, givenUnknownEventWhenClReleaseGlSharedEventINTELIsCalledThenInvalidEventErrorCodeIsReturned) {
341 auto deadEvent = new MockEvent<Event>(nullptr, 0, 0, 0);
342 deadEvent->magic = Event::deadMagic;
343 cl_event unknownEvent = deadEvent;
344 auto ret = clReleaseGlSharedEventINTEL(unknownEvent);
345 EXPECT_EQ(CL_INVALID_EVENT, ret);
346 deadEvent->release();
347 }
348
349 TEST_F(GlArbSyncEventTest, givenEventWithoutArbSyncWhenClReleaseGlSharedEventINTELIsCalledThenThisEventsRefcountIsDecreased) {
350 this->getBaseEvent();
351 triggerEvent->retain();
352 EXPECT_EQ(2, triggerEvent->getRefInternalCount());
353 cl_event ev = triggerEvent;
354 auto ret = clReleaseGlSharedEventINTEL(ev);
355 EXPECT_EQ(CL_SUCCESS, ret);
356 EXPECT_EQ(1, triggerEvent->getRefInternalCount());
357 }
358
359 TEST_F(GlArbSyncEventTest, givenEventWithArbSyncWhenClReleaseGlSharedEventINTELIsCalledThenThisEventsAndArbSyncsRefcountsAreDecreased) {
360 auto *arbEvent = GlArbSyncEvent::create(*this->getBaseEvent());
361 baseEvent->retain();
362 arbEvent->retain();
363 this->sharing->glArbEventMapping[baseEvent] = arbEvent;
364 EXPECT_EQ(4, baseEvent->getRefInternalCount());
365 EXPECT_EQ(3, arbEvent->getRefInternalCount());
366 cl_event ev = baseEvent;
367 auto ret = clReleaseGlSharedEventINTEL(ev);
368 EXPECT_EQ(CL_SUCCESS, ret);
369 EXPECT_EQ(3, baseEvent->getRefInternalCount());
370 EXPECT_EQ(2, arbEvent->getRefInternalCount());
371 arbEvent->release();
372 }