1 /*
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/source/command_stream/command_stream_receiver.h"
9 #include "shared/source/helpers/hw_info.h"
10 #include "shared/source/memory_manager/internal_allocation_storage.h"
11 #include "shared/source/os_interface/os_interface.h"
12 #include "shared/source/utilities/perf_counter.h"
13 #include "shared/source/utilities/tag_allocator.h"
14 #include "shared/test/common/helpers/debug_manager_state_restore.h"
15 #include "shared/test/common/mocks/mock_allocation_properties.h"
16 #include "shared/test/common/mocks/mock_csr.h"
17 #include "shared/test/common/mocks/mock_device.h"
18 #include "shared/test/common/mocks/mock_memory_manager.h"
19 #include "shared/test/common/mocks/mock_ostime.h"
20 #include "shared/test/common/test_macros/test.h"
21 #include "shared/test/common/test_macros/test_checks_shared.h"
22 
23 #include "opencl/source/command_queue/command_queue_hw.h"
24 #include "opencl/source/helpers/task_information.h"
25 #include "opencl/source/memory_manager/mem_obj_surface.h"
26 #include "opencl/test/unit_test/fixtures/image_fixture.h"
27 #include "opencl/test/unit_test/mocks/mock_command_queue.h"
28 #include "opencl/test/unit_test/mocks/mock_context.h"
29 #include "opencl/test/unit_test/mocks/mock_event.h"
30 #include "opencl/test/unit_test/mocks/mock_kernel.h"
31 #include "opencl/test/unit_test/mocks/mock_mdi.h"
32 #include "opencl/test/unit_test/mocks/mock_platform.h"
33 #include "opencl/test/unit_test/mocks/mock_program.h"
34 #include "opencl/test/unit_test/os_interface/mock_performance_counters.h"
35 
36 #include "event_fixture.h"
37 
38 #include <memory>
39 #include <type_traits>
40 
41 using namespace NEO;
42 
TEST(Event,GivenEventWhenCheckingTraitThenEventIsNotCopyable)43 TEST(Event, GivenEventWhenCheckingTraitThenEventIsNotCopyable) {
44     EXPECT_FALSE(std::is_move_constructible<Event>::value);
45     EXPECT_FALSE(std::is_copy_constructible<Event>::value);
46 }
47 
TEST(Event,GivenEventWhenCheckingTraitThenEventIsNotAssignable)48 TEST(Event, GivenEventWhenCheckingTraitThenEventIsNotAssignable) {
49     EXPECT_FALSE(std::is_move_assignable<Event>::value);
50     EXPECT_FALSE(std::is_copy_assignable<Event>::value);
51 }
52 
TEST(Event,WhenPeekIsCalledThenExecutionIsNotUpdated)53 TEST(Event, WhenPeekIsCalledThenExecutionIsNotUpdated) {
54     auto mockDevice = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
55     MockContext ctx;
56     MockCommandQueue cmdQ(&ctx, mockDevice.get(), 0, false);
57     Event event(&cmdQ, CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, 0);
58 
59     EXPECT_FALSE(event.peekIsBlocked());
60     EXPECT_EQ(CL_QUEUED, event.peekExecutionStatus());
61     event.updateExecutionStatus();
62     EXPECT_EQ(CL_QUEUED, event.peekExecutionStatus());
63 }
64 
TEST(Event,givenEventThatStatusChangeWhenPeekIsCalledThenEventIsNotUpdated)65 TEST(Event, givenEventThatStatusChangeWhenPeekIsCalledThenEventIsNotUpdated) {
66     auto mockDevice = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
67     MockContext ctx;
68     MockCommandQueue cmdQ(&ctx, mockDevice.get(), 0, false);
69 
70     struct mockEvent : public Event {
71         using Event::Event;
72         void updateExecutionStatus() override {
73             callCount++;
74         }
75         uint32_t callCount = 0u;
76     };
77 
78     mockEvent event(&cmdQ, CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, 0);
79     EXPECT_EQ(0u, event.callCount);
80     event.peekExecutionStatus();
81     EXPECT_EQ(0u, event.callCount);
82     event.updateEventAndReturnCurrentStatus();
83     EXPECT_EQ(1u, event.callCount);
84     event.updateEventAndReturnCurrentStatus();
85     EXPECT_EQ(2u, event.callCount);
86 }
87 
TEST(Event,givenEventWithHigherTaskCountWhenLowerTaskCountIsBeingSetThenTaskCountRemainsUnmodifed)88 TEST(Event, givenEventWithHigherTaskCountWhenLowerTaskCountIsBeingSetThenTaskCountRemainsUnmodifed) {
89     Event *event = new Event(nullptr, CL_COMMAND_NDRANGE_KERNEL, 4, 10);
90 
91     EXPECT_EQ(10u, event->peekTaskCount());
92     event->updateTaskCount(8, 0);
93     EXPECT_EQ(10u, event->peekTaskCount());
94     delete event;
95 }
96 
TEST(Event,WhenGettingTaskLevelThenCorrectTaskLevelIsReturned)97 TEST(Event, WhenGettingTaskLevelThenCorrectTaskLevelIsReturned) {
98     class TempEvent : public Event {
99       public:
100         TempEvent() : Event(nullptr, CL_COMMAND_NDRANGE_KERNEL, 5, 7){};
101 
102         uint32_t getTaskLevel() override {
103             return Event::getTaskLevel();
104         }
105     };
106     TempEvent event;
107     // taskLevel and getTaskLevel() should give the same result
108     EXPECT_EQ(5u, event.taskLevel);
109     EXPECT_EQ(5u, event.getTaskLevel());
110 }
111 
TEST(Event,WhenGettingTaskCountThenCorrectValueIsReturned)112 TEST(Event, WhenGettingTaskCountThenCorrectValueIsReturned) {
113     Event event(nullptr, CL_COMMAND_NDRANGE_KERNEL, 5, 7);
114     EXPECT_EQ(7u, event.getCompletionStamp());
115 }
116 
TEST(Event,WhenGettingEventInfoThenCqIsReturned)117 TEST(Event, WhenGettingEventInfoThenCqIsReturned) {
118     auto mockDevice = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
119     auto ctx = std::unique_ptr<Context>(new MockContext());
120     auto cmdQ = std::unique_ptr<MockCommandQueue>(new MockCommandQueue(ctx.get(), mockDevice.get(), 0, false));
121     Event *event = new Event(cmdQ.get(), CL_COMMAND_NDRANGE_KERNEL, 1, 5);
122     cl_event clEvent = event;
123     cl_command_queue cmdQResult = nullptr;
124     size_t sizeReturned = 0;
125 
126     auto result = clGetEventInfo(clEvent, CL_EVENT_COMMAND_QUEUE, 0, nullptr, &sizeReturned);
127     EXPECT_EQ(CL_SUCCESS, result);
128     EXPECT_EQ(sizeof(cl_command_queue), sizeReturned);
129 
130     result = clGetEventInfo(clEvent, CL_EVENT_COMMAND_QUEUE, sizeof(cmdQResult), &cmdQResult, &sizeReturned);
131     ASSERT_EQ(CL_SUCCESS, result);
132     EXPECT_EQ(cmdQ.get(), cmdQResult);
133     EXPECT_EQ(sizeReturned, sizeof(cmdQResult));
134     delete event;
135 }
136 
TEST(Event,givenBcsCsrSetInEventWhenPeekingBcsTaskCountThenReturnCorrectTaskCount)137 TEST(Event, givenBcsCsrSetInEventWhenPeekingBcsTaskCountThenReturnCorrectTaskCount) {
138     HardwareInfo hwInfo = *defaultHwInfo;
139     hwInfo.capabilityTable.blitterOperationsSupported = true;
140     REQUIRE_FULL_BLITTER_OR_SKIP(&hwInfo);
141 
142     auto device = ReleaseableObjectPtr<MockClDevice>{
143         new MockClDevice{MockDevice::createWithNewExecutionEnvironment<MockAlignedMallocManagerDevice>(&hwInfo)}};
144     MockContext context{device.get()};
145     MockCommandQueue queue{context};
146     queue.updateBcsTaskCount(queue.bcsEngines[0]->getEngineType(), 19);
147     Event event{&queue, CL_COMMAND_READ_BUFFER, 0, 0};
148 
149     EXPECT_EQ(0u, event.peekBcsTaskCountFromCommandQueue());
150 
151     event.setupBcs(queue.bcsEngines[0]->getEngineType());
152     EXPECT_EQ(19u, event.peekBcsTaskCountFromCommandQueue());
153 }
154 
TEST(Event,givenCommandQueueWhenEventIsCreatedWithCommandQueueThenCommandQueueInternalRefCountIsIncremented)155 TEST(Event, givenCommandQueueWhenEventIsCreatedWithCommandQueueThenCommandQueueInternalRefCountIsIncremented) {
156     auto mockDevice = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
157     MockContext ctx;
158     MockCommandQueue cmdQ(&ctx, mockDevice.get(), 0, false);
159     auto intitialRefCount = cmdQ.getRefInternalCount();
160 
161     Event *event = new Event(&cmdQ, CL_COMMAND_NDRANGE_KERNEL, 4, 10);
162     auto newRefCount = cmdQ.getRefInternalCount();
163 
164     EXPECT_EQ(intitialRefCount + 1, newRefCount);
165     delete event;
166 
167     auto finalRefCount = cmdQ.getRefInternalCount();
168     EXPECT_EQ(intitialRefCount, finalRefCount);
169 }
170 
TEST(Event,givenCommandQueueWhenEventIsCreatedWithoutCommandQueueThenCommandQueueInternalRefCountIsNotModified)171 TEST(Event, givenCommandQueueWhenEventIsCreatedWithoutCommandQueueThenCommandQueueInternalRefCountIsNotModified) {
172     MockContext ctx;
173     MockCommandQueue cmdQ(&ctx, nullptr, 0, false);
174     auto intitialRefCount = cmdQ.getRefInternalCount();
175 
176     Event *event = new Event(nullptr, CL_COMMAND_NDRANGE_KERNEL, 4, 10);
177     auto newRefCount = cmdQ.getRefInternalCount();
178 
179     EXPECT_EQ(intitialRefCount, newRefCount);
180     delete event;
181 
182     auto finalRefCount = cmdQ.getRefInternalCount();
183     EXPECT_EQ(intitialRefCount, finalRefCount);
184 }
185 
TEST(Event,WhenWaitingForEventsThenAllQueuesAreFlushed)186 TEST(Event, WhenWaitingForEventsThenAllQueuesAreFlushed) {
187     class MockCommandQueueWithFlushCheck : public MockCommandQueue {
188       public:
189         MockCommandQueueWithFlushCheck() = delete;
190         MockCommandQueueWithFlushCheck(MockCommandQueueWithFlushCheck &) = delete;
191         MockCommandQueueWithFlushCheck(Context &context, ClDevice *device) : MockCommandQueue(&context, device, nullptr, false) {
192         }
193         cl_int flush() override {
194             flushCounter++;
195             return CL_SUCCESS;
196         }
197         uint32_t flushCounter = 0;
198     };
199 
200     auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
201     MockContext context;
202 
203     std::unique_ptr<MockCommandQueueWithFlushCheck> cmdQ1(new MockCommandQueueWithFlushCheck(context, device.get()));
204     std::unique_ptr<Event> event1(new Event(cmdQ1.get(), CL_COMMAND_NDRANGE_KERNEL, 4, 10));
205 
206     std::unique_ptr<MockCommandQueueWithFlushCheck> cmdQ2(new MockCommandQueueWithFlushCheck(context, device.get()));
207     std::unique_ptr<Event> event2(new Event(cmdQ2.get(), CL_COMMAND_NDRANGE_KERNEL, 5, 20));
208 
209     cl_event eventWaitlist[] = {event1.get(), event2.get()};
210 
211     Event::waitForEvents(2, eventWaitlist);
212 
213     EXPECT_EQ(1u, cmdQ1->flushCounter);
214     EXPECT_EQ(1u, cmdQ2->flushCounter);
215 }
216 
TEST(Event,GivenNotReadyEventWhenWaitingForEventsThenQueueIsNotFlushed)217 TEST(Event, GivenNotReadyEventWhenWaitingForEventsThenQueueIsNotFlushed) {
218     class MockCommandQueueWithFlushCheck : public MockCommandQueue {
219       public:
220         MockCommandQueueWithFlushCheck() = delete;
221         MockCommandQueueWithFlushCheck(MockCommandQueueWithFlushCheck &) = delete;
222         MockCommandQueueWithFlushCheck(Context &context, ClDevice *device) : MockCommandQueue(&context, device, nullptr, false) {
223         }
224         cl_int flush() override {
225             flushCounter++;
226             return CL_SUCCESS;
227         }
228         uint32_t flushCounter = 0;
229     };
230 
231     auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
232     MockContext context;
233 
234     std::unique_ptr<MockCommandQueueWithFlushCheck> cmdQ1(new MockCommandQueueWithFlushCheck(context, device.get()));
235     std::unique_ptr<Event> event1(new Event(cmdQ1.get(), CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, 0));
236     cl_event eventWaitlist[] = {event1.get()};
237 
238     Event::waitForEvents(1, eventWaitlist);
239 
240     EXPECT_EQ(0u, cmdQ1->flushCounter);
241 }
242 
TEST(Event,givenNotReadyEventOnWaitlistWhenCheckingUserEventDependeciesThenTrueIsReturned)243 TEST(Event, givenNotReadyEventOnWaitlistWhenCheckingUserEventDependeciesThenTrueIsReturned) {
244     auto event1 = std::make_unique<Event>(nullptr, CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, 0);
245     cl_event eventWaitlist[] = {event1.get()};
246 
247     bool userEventDependencies = Event::checkUserEventDependencies(1, eventWaitlist);
248     EXPECT_TRUE(userEventDependencies);
249 }
250 
TEST(Event,givenReadyEventsOnWaitlistWhenCheckingUserEventDependeciesThenFalseIsReturned)251 TEST(Event, givenReadyEventsOnWaitlistWhenCheckingUserEventDependeciesThenFalseIsReturned) {
252     auto event1 = std::make_unique<Event>(nullptr, CL_COMMAND_NDRANGE_KERNEL, 5, 0);
253     cl_event eventWaitlist[] = {event1.get()};
254 
255     bool userEventDependencies = Event::checkUserEventDependencies(1, eventWaitlist);
256     EXPECT_FALSE(userEventDependencies);
257 }
258 
TEST_F(EventTest,WhenGettingClEventCommandExecutionStatusThenCorrectSizeIsReturned)259 TEST_F(EventTest, WhenGettingClEventCommandExecutionStatusThenCorrectSizeIsReturned) {
260     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 1, 5);
261     cl_int eventStatus = -1;
262     size_t sizeReturned = 0;
263 
264     auto result = clGetEventInfo(&event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(eventStatus), &eventStatus, &sizeReturned);
265     ASSERT_EQ(CL_SUCCESS, result);
266     EXPECT_EQ(sizeReturned, sizeof(eventStatus));
267 }
268 
TEST_F(EventTest,GivenTagCsLessThanTaskCountWhenGettingClEventCommandExecutionStatusThenClSubmittedIsReturned)269 TEST_F(EventTest, GivenTagCsLessThanTaskCountWhenGettingClEventCommandExecutionStatusThenClSubmittedIsReturned) {
270     uint32_t tagHW = 4;
271     uint32_t taskCount = 5;
272     *pTagMemory = tagHW;
273 
274     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, taskCount);
275     cl_int eventStatus = -1;
276     size_t sizeReturned = 0;
277 
278     auto result = clGetEventInfo(&event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(eventStatus), &eventStatus, &sizeReturned);
279     ASSERT_EQ(CL_SUCCESS, result);
280 
281     // If tagCS < taskCount, we always return submitted (ie. no buffering!)
282     EXPECT_EQ(CL_SUBMITTED, eventStatus);
283 }
284 
TEST_F(EventTest,GivenTagCsEqualTaskCountWhenGettingClEventCommandExecutionStatusThenClCompleteIsReturned)285 TEST_F(EventTest, GivenTagCsEqualTaskCountWhenGettingClEventCommandExecutionStatusThenClCompleteIsReturned) {
286     uint32_t tagHW = 5;
287     uint32_t taskCount = 5;
288     *pTagMemory = tagHW;
289 
290     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, taskCount);
291     cl_int eventStatus = -1;
292     size_t sizeReturned = 0;
293 
294     auto result = clGetEventInfo(&event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(eventStatus), &eventStatus, &sizeReturned);
295     ASSERT_EQ(CL_SUCCESS, result);
296 
297     // If tagCS == event.taskCount, the event is completed.
298     EXPECT_EQ(CL_COMPLETE, eventStatus);
299 }
300 
TEST_F(EventTest,GivenTagCsGreaterThanTaskCountWhenGettingClEventCommandExecutionStatusThenClCompleteIsReturned)301 TEST_F(EventTest, GivenTagCsGreaterThanTaskCountWhenGettingClEventCommandExecutionStatusThenClCompleteIsReturned) {
302     uint32_t tagHW = 6;
303     uint32_t taskCount = 5;
304     *pTagMemory = tagHW;
305 
306     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, taskCount);
307     cl_int eventStatus = -1;
308     size_t sizeReturned = 0;
309 
310     auto result = clGetEventInfo(&event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(eventStatus), &eventStatus, &sizeReturned);
311     ASSERT_EQ(CL_SUCCESS, result);
312 
313     EXPECT_EQ(CL_COMPLETE, eventStatus);
314 }
315 
TEST_F(EventTest,WhenGettingClEventCommandExecutionStatusThenEventStatusIsReturned)316 TEST_F(EventTest, WhenGettingClEventCommandExecutionStatusThenEventStatusIsReturned) {
317     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, CompletionStamp::notReady);
318     cl_int eventStatus = -1;
319 
320     event.setStatus(-1);
321     auto result = clGetEventInfo(&event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(eventStatus), &eventStatus, 0);
322     EXPECT_EQ(CL_SUCCESS, result);
323     EXPECT_EQ(-1, eventStatus);
324 }
325 
TEST_F(EventTest,GivenNewEventWhenGettingClEventReferenceCountThenOneIsReturned)326 TEST_F(EventTest, GivenNewEventWhenGettingClEventReferenceCountThenOneIsReturned) {
327     uint32_t tagEvent = 5;
328 
329     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, tagEvent);
330     cl_uint refCount = 0;
331     size_t sizeReturned = 0;
332 
333     auto result = clGetEventInfo(&event, CL_EVENT_REFERENCE_COUNT, sizeof(refCount), &refCount, &sizeReturned);
334     ASSERT_EQ(CL_SUCCESS, result);
335     EXPECT_EQ(sizeof(refCount), sizeReturned);
336 
337     EXPECT_EQ(1u, refCount);
338 }
339 
TEST_F(EventTest,GivenRetainedEventWhenGettingClEventReferenceCountThenTwoIsReturned)340 TEST_F(EventTest, GivenRetainedEventWhenGettingClEventReferenceCountThenTwoIsReturned) {
341     uint32_t tagEvent = 5;
342 
343     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, tagEvent);
344     event.retain();
345 
346     cl_uint refCount = 0;
347     size_t sizeReturned = 0;
348 
349     auto result = clGetEventInfo(&event, CL_EVENT_REFERENCE_COUNT, sizeof(refCount), &refCount, &sizeReturned);
350     ASSERT_EQ(CL_SUCCESS, result);
351     EXPECT_EQ(sizeof(refCount), sizeReturned);
352 
353     EXPECT_EQ(2u, refCount);
354     event.release();
355 }
356 
TEST_F(EventTest,GivenRetainAndReleaseEventWhenGettingClEventReferenceCountThenOneIsReturned)357 TEST_F(EventTest, GivenRetainAndReleaseEventWhenGettingClEventReferenceCountThenOneIsReturned) {
358     uint32_t tagEvent = 5;
359 
360     Event *pEvent = new Event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, tagEvent);
361     ASSERT_NE(nullptr, pEvent);
362 
363     pEvent->retain();
364     auto retVal = pEvent->getReference();
365     EXPECT_EQ(2, retVal);
366 
367     cl_uint refCount = 0;
368     size_t sizeReturned = 0;
369     auto result = clGetEventInfo(pEvent, CL_EVENT_REFERENCE_COUNT, sizeof(refCount), &refCount, &sizeReturned);
370     ASSERT_EQ(CL_SUCCESS, result);
371     EXPECT_EQ(sizeof(refCount), sizeReturned);
372     EXPECT_EQ(2u, refCount);
373 
374     pEvent->release();
375     retVal = pEvent->getReference();
376     EXPECT_EQ(1, retVal);
377 
378     delete pEvent;
379 }
380 
TEST_F(EventTest,WhenGettingClEventContextThenCorrectValueIsReturned)381 TEST_F(EventTest, WhenGettingClEventContextThenCorrectValueIsReturned) {
382     uint32_t tagEvent = 5;
383 
384     Event *pEvent = new Event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, tagEvent);
385     ASSERT_NE(nullptr, pEvent);
386 
387     cl_context context;
388     size_t sizeReturned = 0;
389     auto result = clGetEventInfo(pEvent, CL_EVENT_CONTEXT, sizeof(context), &context, &sizeReturned);
390     ASSERT_EQ(CL_SUCCESS, result);
391     EXPECT_EQ(sizeof(context), sizeReturned);
392 
393     cl_context qCtx = (cl_context)&mockContext;
394     EXPECT_EQ(qCtx, context);
395 
396     delete pEvent;
397 }
398 
TEST_F(EventTest,GivenInvalidEventWhenGettingEventInfoThenInvalidValueErrorIsReturned)399 TEST_F(EventTest, GivenInvalidEventWhenGettingEventInfoThenInvalidValueErrorIsReturned) {
400     uint32_t tagEvent = 5;
401 
402     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, tagEvent);
403     cl_int eventStatus = -1;
404 
405     auto result = clGetEventInfo(&event, -1, sizeof(eventStatus), &eventStatus, nullptr);
406     EXPECT_EQ(CL_INVALID_VALUE, result);
407 }
408 
TEST_F(EventTest,GivenNonBlockingEventWhenWaitingThenFalseIsReturned)409 TEST_F(EventTest, GivenNonBlockingEventWhenWaitingThenFalseIsReturned) {
410     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 3, CompletionStamp::notReady);
411     auto result = event.wait(false, false);
412     EXPECT_FALSE(result);
413 }
414 
415 struct UpdateEventTest : public ::testing::Test {
416 
SetUpUpdateEventTest417     void SetUp() override {
418         executionEnvironment = platform()->peekExecutionEnvironment();
419         memoryManager = new MockMemoryManager(*executionEnvironment);
420         hostPtrManager = static_cast<MockHostPtrManager *>(memoryManager->getHostPtrManager());
421         executionEnvironment->memoryManager.reset(memoryManager);
422         device.reset(new ClDevice{*Device::create<RootDevice>(executionEnvironment, 0u), platform()});
423         context = std::make_unique<MockContext>(device.get());
424         cl_int retVal = CL_OUT_OF_RESOURCES;
425         commandQueue.reset(CommandQueue::create(context.get(), device.get(), nullptr, false, retVal));
426         EXPECT_EQ(CL_SUCCESS, retVal);
427     }
428 
429     ExecutionEnvironment *executionEnvironment;
430     MockMemoryManager *memoryManager;
431     MockHostPtrManager *hostPtrManager;
432     std::unique_ptr<ClDevice> device;
433     std::unique_ptr<Context> context;
434     std::unique_ptr<CommandQueue> commandQueue;
435 };
436 
TEST_F(UpdateEventTest,givenEventContainingCommandQueueWhenItsStatusIsUpdatedToCompletedThenTemporaryAllocationsAreDeleted)437 TEST_F(UpdateEventTest, givenEventContainingCommandQueueWhenItsStatusIsUpdatedToCompletedThenTemporaryAllocationsAreDeleted) {
438     void *ptr = (void *)0x1000;
439     size_t size = 4096;
440     auto temporary = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{device->getRootDeviceIndex(), false, size, device->getDeviceBitfield()}, ptr);
441     temporary->updateTaskCount(3, commandQueue->getGpgpuCommandStreamReceiver().getOsContext().getContextId());
442     commandQueue->getGpgpuCommandStreamReceiver().getInternalAllocationStorage()->storeAllocation(std::unique_ptr<GraphicsAllocation>(temporary), TEMPORARY_ALLOCATION);
443     Event event(commandQueue.get(), CL_COMMAND_NDRANGE_KERNEL, 3, 3);
444 
445     EXPECT_EQ(1u, hostPtrManager->getFragmentCount());
446 
447     event.updateExecutionStatus();
448 
449     EXPECT_EQ(0u, hostPtrManager->getFragmentCount());
450 }
451 
452 class SurfaceMock : public Surface {
453   public:
SurfaceMock()454     SurfaceMock() {
455         resident = nonResident = 0;
456     };
~SurfaceMock()457     ~SurfaceMock() override{};
458 
makeResident(CommandStreamReceiver & csr)459     void makeResident(CommandStreamReceiver &csr) override {
460         if (parent) {
461             parent->resident++;
462         } else {
463             resident++;
464         }
465         if (this->graphicsAllocation) {
466             csr.makeResident(*graphicsAllocation);
467         }
468     };
duplicate()469     Surface *duplicate() override {
470         return new SurfaceMock(this);
471     };
472 
473     SurfaceMock *parent = nullptr;
474     std::atomic<uint32_t> resident;
475     std::atomic<uint32_t> nonResident;
476 
477     GraphicsAllocation *graphicsAllocation = nullptr;
478 
479   protected:
SurfaceMock(SurfaceMock * parent)480     SurfaceMock(SurfaceMock *parent) : parent(parent){};
481 };
482 
TEST_F(InternalsEventTest,GivenSubmitCommandFalseWhenSubmittingCommandsThenRefApiCountAndRefInternalGetHandledCorrectly)483 TEST_F(InternalsEventTest, GivenSubmitCommandFalseWhenSubmittingCommandsThenRefApiCountAndRefInternalGetHandledCorrectly) {
484     MockCommandQueue cmdQ(mockContext, pClDevice, nullptr, false);
485     MockEvent<Event> event(&cmdQ, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
486 
487     auto cmdStream = new LinearStream(pDevice->getMemoryManager()->allocateGraphicsMemoryWithProperties({pDevice->getRootDeviceIndex(), true, 4096, GraphicsAllocation::AllocationType::COMMAND_BUFFER, false, pDevice->getDeviceBitfield()}));
488     IndirectHeap *dsh = nullptr, *ioh = nullptr, *ssh = nullptr;
489     cmdQ.allocateHeapMemory(IndirectHeap::DYNAMIC_STATE, 4096u, dsh);
490     cmdQ.allocateHeapMemory(IndirectHeap::INDIRECT_OBJECT, 4096u, ioh);
491     cmdQ.allocateHeapMemory(IndirectHeap::SURFACE_STATE, 4096u, ssh);
492 
493     auto blockedCommandsData = std::make_unique<KernelOperation>(cmdStream, *cmdQ.getGpgpuCommandStreamReceiver().getInternalAllocationStorage());
494     blockedCommandsData->setHeaps(dsh, ioh, ssh);
495 
496     MockKernelWithInternals mockKernelWithInternals(*pClDevice);
497     auto pKernel = mockKernelWithInternals.mockKernel;
498 
499     auto &csr = cmdQ.getGpgpuCommandStreamReceiver();
500     std::vector<Surface *> v;
501     MockBuffer buffer;
502     buffer.retain();
503     auto initialRefCount = buffer.getRefApiCount();
504     auto initialInternalCount = buffer.getRefInternalCount();
505 
506     auto bufferSurf = new MemObjSurface(&buffer);
507 
508     EXPECT_EQ(initialInternalCount + 1, buffer.getRefInternalCount());
509     EXPECT_EQ(initialRefCount, buffer.getRefApiCount());
510 
511     PreemptionMode preemptionMode = pDevice->getPreemptionMode();
512     v.push_back(bufferSurf);
513     auto cmd = new CommandComputeKernel(cmdQ, blockedCommandsData, v, false, false, false, nullptr, preemptionMode, pKernel, 1);
514     event.setCommand(std::unique_ptr<Command>(cmd));
515 
516     auto taskLevelBefore = csr.peekTaskLevel();
517 
518     auto refCount = buffer.getRefApiCount();
519     auto refInternal = buffer.getRefInternalCount();
520 
521     event.submitCommand(false);
522 
523     EXPECT_EQ(refCount, buffer.getRefApiCount());
524     EXPECT_EQ(refInternal - 1, buffer.getRefInternalCount());
525 
526     auto taskLevelAfter = csr.peekTaskLevel();
527 
528     EXPECT_EQ(taskLevelBefore + 1, taskLevelAfter);
529 
530     auto graphicsAllocation = buffer.getGraphicsAllocation(pClDevice->getRootDeviceIndex());
531 
532     EXPECT_FALSE(graphicsAllocation->isResident(csr.getOsContext().getContextId()));
533 }
534 
TEST_F(InternalsEventTest,GivenSubmitCommandTrueWhenSubmittingCommandsThenRefApiCountAndRefInternalGetHandledCorrectly)535 TEST_F(InternalsEventTest, GivenSubmitCommandTrueWhenSubmittingCommandsThenRefApiCountAndRefInternalGetHandledCorrectly) {
536     MockCommandQueue cmdQ(mockContext, pClDevice, nullptr, false);
537     MockEvent<Event> event(&cmdQ, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
538 
539     auto cmdStream = new LinearStream(pDevice->getMemoryManager()->allocateGraphicsMemoryWithProperties({pDevice->getRootDeviceIndex(), 4096, GraphicsAllocation::AllocationType::COMMAND_BUFFER, pDevice->getDeviceBitfield()}));
540     IndirectHeap *dsh = nullptr, *ioh = nullptr, *ssh = nullptr;
541     cmdQ.allocateHeapMemory(IndirectHeap::DYNAMIC_STATE, 4096u, dsh);
542     cmdQ.allocateHeapMemory(IndirectHeap::INDIRECT_OBJECT, 4096u, ioh);
543     cmdQ.allocateHeapMemory(IndirectHeap::SURFACE_STATE, 4096u, ssh);
544 
545     auto blockedCommandsData = std::make_unique<KernelOperation>(cmdStream, *cmdQ.getGpgpuCommandStreamReceiver().getInternalAllocationStorage());
546     blockedCommandsData->setHeaps(dsh, ioh, ssh);
547 
548     MockKernelWithInternals mockKernelWithInternals(*pClDevice);
549     auto pKernel = mockKernelWithInternals.mockKernel;
550 
551     auto &csr = cmdQ.getGpgpuCommandStreamReceiver();
552     std::vector<Surface *> v;
553     NullSurface *surface = new NullSurface;
554     v.push_back(surface);
555     PreemptionMode preemptionMode = pDevice->getPreemptionMode();
556     auto cmd = new CommandComputeKernel(cmdQ, blockedCommandsData, v, false, false, false, nullptr, preemptionMode, pKernel, 1);
557     event.setCommand(std::unique_ptr<Command>(cmd));
558 
559     auto taskLevelBefore = csr.peekTaskLevel();
560 
561     event.submitCommand(true);
562 
563     auto taskLevelAfter = csr.peekTaskLevel();
564 
565     EXPECT_EQ(taskLevelBefore, taskLevelAfter);
566 }
567 
TEST_F(InternalsEventTest,givenBlockedKernelWithPrintfWhenSubmittedThenPrintOutput)568 TEST_F(InternalsEventTest, givenBlockedKernelWithPrintfWhenSubmittedThenPrintOutput) {
569     MockCommandQueue mockCmdQueue(mockContext, pClDevice, nullptr, false);
570 
571     testing::internal::CaptureStdout();
572     MockEvent<Event> event(&mockCmdQueue, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
573 
574     auto cmdStream = new LinearStream(pDevice->getMemoryManager()->allocateGraphicsMemoryWithProperties({pDevice->getRootDeviceIndex(), 4096, GraphicsAllocation::AllocationType::COMMAND_BUFFER, pDevice->getDeviceBitfield()}));
575     IndirectHeap *dsh = nullptr, *ioh = nullptr, *ssh = nullptr;
576     mockCmdQueue.allocateHeapMemory(IndirectHeap::DYNAMIC_STATE, 4096u, dsh);
577     mockCmdQueue.allocateHeapMemory(IndirectHeap::INDIRECT_OBJECT, 4096u, ioh);
578     mockCmdQueue.allocateHeapMemory(IndirectHeap::SURFACE_STATE, 4096u, ssh);
579 
580     auto blockedCommandsData = std::make_unique<KernelOperation>(cmdStream, *mockCmdQueue.getGpgpuCommandStreamReceiver().getInternalAllocationStorage());
581     blockedCommandsData->setHeaps(dsh, ioh, ssh);
582 
583     std::string testString = "test";
584 
585     MockKernelWithInternals mockKernelWithInternals(*pClDevice);
586     auto pKernel = mockKernelWithInternals.mockKernel;
587 
588     auto &kernelInfo = mockKernelWithInternals.kernelInfo;
589     kernelInfo.setPrintfSurface(sizeof(uintptr_t), 0);
590     kernelInfo.addToPrintfStringsMap(0, testString);
591 
592     uint64_t crossThread[10];
593     pKernel->setCrossThreadData(&crossThread, sizeof(uint64_t) * 8);
594 
595     MockMultiDispatchInfo multiDispatchInfo(pClDevice, pKernel);
596     std::unique_ptr<PrintfHandler> printfHandler(PrintfHandler::create(multiDispatchInfo, *pClDevice));
597     printfHandler.get()->prepareDispatch(multiDispatchInfo);
598     auto surface = printfHandler.get()->getSurface();
599 
600     auto printfSurface = reinterpret_cast<uint32_t *>(surface->getUnderlyingBuffer());
601     printfSurface[0] = 8;
602     printfSurface[1] = 0;
603 
604     std::vector<Surface *> v;
605     PreemptionMode preemptionMode = pDevice->getPreemptionMode();
606     auto cmd = new CommandComputeKernel(mockCmdQueue, blockedCommandsData, v, false, false, false, std::move(printfHandler), preemptionMode, pKernel, 1);
607     event.setCommand(std::unique_ptr<Command>(cmd));
608 
609     event.submitCommand(false);
610 
611     EXPECT_EQ(1u, mockCmdQueue.latestTaskCountWaited);
612 
613     std::string output = testing::internal::GetCapturedStdout();
614     EXPECT_STREQ("test", output.c_str());
615     EXPECT_FALSE(surface->isResident(pDevice->getDefaultEngine().osContext->getContextId()));
616 }
617 
TEST_F(InternalsEventTest,GivenMapOperationWhenSubmittingCommandsThenTaskLevelIsIncremented)618 TEST_F(InternalsEventTest, GivenMapOperationWhenSubmittingCommandsThenTaskLevelIsIncremented) {
619     auto pCmdQ = make_releaseable<MockCommandQueue>(mockContext, pClDevice, nullptr, false);
620     MockEvent<Event> event(pCmdQ.get(), CL_COMMAND_NDRANGE_KERNEL, 0, 0);
621 
622     auto &csr = pCmdQ->getGpgpuCommandStreamReceiver();
623     auto buffer = new MockBuffer;
624 
625     MemObjSizeArray size = {{1, 1, 1}};
626     MemObjOffsetArray offset = {{0, 0, 0}};
627     event.setCommand(std::unique_ptr<Command>(new CommandMapUnmap(MAP, *buffer, size, offset, false, *pCmdQ)));
628 
629     auto taskLevelBefore = csr.peekTaskLevel();
630 
631     event.submitCommand(false);
632 
633     auto taskLevelAfter = csr.peekTaskLevel();
634 
635     EXPECT_EQ(taskLevelBefore + 1, taskLevelAfter);
636     buffer->decRefInternal();
637 }
638 
TEST_F(InternalsEventTest,GivenMapOperationNonZeroCopyBufferWhenSubmittingCommandsThenTaskLevelIsIncremented)639 TEST_F(InternalsEventTest, GivenMapOperationNonZeroCopyBufferWhenSubmittingCommandsThenTaskLevelIsIncremented) {
640     auto pCmdQ = make_releaseable<MockCommandQueue>(mockContext, pClDevice, nullptr, false);
641     MockEvent<Event> event(pCmdQ.get(), CL_COMMAND_NDRANGE_KERNEL, 0, 0);
642 
643     auto &csr = pCmdQ->getGpgpuCommandStreamReceiver();
644     auto buffer = new UnalignedBuffer;
645 
646     MemObjSizeArray size = {{1, 1, 1}};
647     MemObjOffsetArray offset = {{0, 0, 0}};
648     event.setCommand(std::unique_ptr<Command>(new CommandMapUnmap(MAP, *buffer, size, offset, false, *pCmdQ)));
649 
650     auto taskLevelBefore = csr.peekTaskLevel();
651 
652     event.submitCommand(false);
653 
654     auto taskLevelAfter = csr.peekTaskLevel();
655 
656     EXPECT_EQ(taskLevelBefore + 1, taskLevelAfter);
657     buffer->decRefInternal();
658 }
659 
660 uint32_t commands[] = {
661     CL_COMMAND_NDRANGE_KERNEL,
662     CL_COMMAND_TASK,
663     CL_COMMAND_NATIVE_KERNEL,
664     CL_COMMAND_READ_BUFFER,
665     CL_COMMAND_WRITE_BUFFER,
666     CL_COMMAND_COPY_BUFFER,
667     CL_COMMAND_READ_IMAGE,
668     CL_COMMAND_WRITE_IMAGE,
669     CL_COMMAND_COPY_IMAGE,
670     CL_COMMAND_COPY_IMAGE_TO_BUFFER,
671     CL_COMMAND_COPY_BUFFER_TO_IMAGE,
672     CL_COMMAND_MAP_BUFFER,
673     CL_COMMAND_MAP_IMAGE,
674     CL_COMMAND_UNMAP_MEM_OBJECT,
675     CL_COMMAND_MARKER,
676     CL_COMMAND_ACQUIRE_GL_OBJECTS,
677     CL_COMMAND_RELEASE_GL_OBJECTS,
678     CL_COMMAND_READ_BUFFER_RECT,
679     CL_COMMAND_WRITE_BUFFER_RECT,
680     CL_COMMAND_COPY_BUFFER_RECT,
681     CL_COMMAND_BARRIER,
682     CL_COMMAND_MIGRATE_MEM_OBJECTS,
683     CL_COMMAND_FILL_BUFFER,
684     CL_COMMAND_FILL_IMAGE,
685     CL_COMMAND_SVM_FREE,
686     CL_COMMAND_SVM_MEMCPY,
687     CL_COMMAND_SVM_MEMFILL,
688     CL_COMMAND_SVM_MAP,
689     CL_COMMAND_SVM_UNMAP,
690 };
691 
692 class InternalsEventProfilingTest : public InternalsEventTest,
693                                     public ::testing::WithParamInterface<uint32_t> {
SetUp()694     void SetUp() override {
695         InternalsEventTest::SetUp();
696     }
697 
TearDown()698     void TearDown() override {
699         InternalsEventTest::TearDown();
700     }
701 };
702 
TEST_P(InternalsEventProfilingTest,GivenProfilingWhenEventCreatedThenProfilingSet)703 TEST_P(InternalsEventProfilingTest, GivenProfilingWhenEventCreatedThenProfilingSet) {
704     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
705     std::unique_ptr<MockCommandQueue> pCmdQ(new MockCommandQueue(mockContext, pClDevice, props, false));
706 
707     std::unique_ptr<MockEvent<Event>> event(new MockEvent<Event>(pCmdQ.get(), GetParam(), 0, 0));
708     EXPECT_TRUE(event.get()->isProfilingEnabled());
709 }
710 
711 INSTANTIATE_TEST_CASE_P(InternalsEventProfilingTest,
712                         InternalsEventProfilingTest,
713                         ::testing::ValuesIn(commands));
714 
TEST_F(InternalsEventTest,GivenProfilingWhenUserEventCreatedThenProfilingNotSet)715 TEST_F(InternalsEventTest, GivenProfilingWhenUserEventCreatedThenProfilingNotSet) {
716     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
717     std::unique_ptr<MockCommandQueue> pCmdQ(new MockCommandQueue(mockContext, pClDevice, props, false));
718 
719     std::unique_ptr<MockEvent<Event>> event(new MockEvent<Event>(pCmdQ.get(), CL_COMMAND_USER, 0, 0));
720     EXPECT_FALSE(event.get()->isProfilingEnabled());
721 }
722 
TEST_F(InternalsEventTest,givenDeviceTimestampBaseNotEnabledWhenGetEventProfilingInfoThenCpuTimestampIsReturned)723 TEST_F(InternalsEventTest, givenDeviceTimestampBaseNotEnabledWhenGetEventProfilingInfoThenCpuTimestampIsReturned) {
724     pClDevice->setOSTime(new MockOSTimeWithConstTimestamp());
725     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
726     MockCommandQueue cmdQ(mockContext, pClDevice, props, false);
727     MockEvent<Event> event(&cmdQ, CL_COMMAND_MARKER, 0, 0);
728 
729     event.setCommand(std::unique_ptr<Command>(new CommandWithoutKernel(cmdQ)));
730 
731     event.submitCommand(false);
732     uint64_t submitTime = 0ULL;
733     event.getEventProfilingInfo(CL_PROFILING_COMMAND_SUBMIT, sizeof(uint64_t), &submitTime, 0);
734 
735     EXPECT_EQ(submitTime, MockDeviceTimeWithConstTimestamp::CPU_TIME_IN_NS);
736 }
737 
TEST_F(InternalsEventTest,givenDeviceTimestampBaseEnabledWhenGetEventProfilingInfoThenGpuTimestampIsReturned)738 TEST_F(InternalsEventTest, givenDeviceTimestampBaseEnabledWhenGetEventProfilingInfoThenGpuTimestampIsReturned) {
739     DebugManagerStateRestore dbgRestorer;
740     DebugManager.flags.EnableDeviceBasedTimestamps.set(true);
741 
742     pClDevice->setOSTime(new MockOSTimeWithConstTimestamp());
743     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
744     MockCommandQueue cmdQ(mockContext, pClDevice, props, false);
745     MockEvent<Event> event(&cmdQ, CL_COMMAND_MARKER, 0, 0);
746     event.queueTimeStamp.GPUTimeStamp = MockDeviceTimeWithConstTimestamp::GPU_TIMESTAMP;
747 
748     event.setCommand(std::unique_ptr<Command>(new CommandWithoutKernel(cmdQ)));
749     event.submitCommand(false);
750     uint64_t submitTime = 0ULL;
751     event.getEventProfilingInfo(CL_PROFILING_COMMAND_SUBMIT, sizeof(uint64_t), &submitTime, 0);
752 
753     auto resolution = pClDevice->getDevice().getDeviceInfo().profilingTimerResolution;
754     EXPECT_EQ(submitTime, static_cast<uint64_t>(MockDeviceTimeWithConstTimestamp::GPU_TIMESTAMP * resolution));
755 }
756 
TEST_F(InternalsEventTest,givenDeviceTimestampBaseEnabledWhenCalculateStartTimestampThenCorrectTimeIsReturned)757 TEST_F(InternalsEventTest, givenDeviceTimestampBaseEnabledWhenCalculateStartTimestampThenCorrectTimeIsReturned) {
758     DebugManagerStateRestore dbgRestorer;
759     DebugManager.flags.EnableDeviceBasedTimestamps.set(true);
760 
761     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
762     MockCommandQueue cmdQ(mockContext, pClDevice, props, false);
763     MockEvent<Event> event(&cmdQ, CL_COMPLETE, 0, 0);
764 
765     HwTimeStamps timestamp{};
766     timestamp.GlobalStartTS = 2;
767     event.queueTimeStamp.GPUTimeStamp = 1;
768     TagNode<HwTimeStamps> timestampNode{};
769     timestampNode.tagForCpuAccess = &timestamp;
770     event.timeStampNode = &timestampNode;
771 
772     uint64_t start;
773     event.getEventProfilingInfo(CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, nullptr);
774 
775     auto resolution = pClDevice->getDevice().getDeviceInfo().profilingTimerResolution;
776     EXPECT_EQ(start, static_cast<uint64_t>(timestamp.GlobalStartTS * resolution));
777 
778     event.timeStampNode = nullptr;
779 }
780 
TEST_F(InternalsEventTest,givenDeviceTimestampBaseEnabledAndGlobalStartTSSmallerThanQueueTSWhenCalculateStartTimestampThenCorrectTimeIsReturned)781 TEST_F(InternalsEventTest, givenDeviceTimestampBaseEnabledAndGlobalStartTSSmallerThanQueueTSWhenCalculateStartTimestampThenCorrectTimeIsReturned) {
782     DebugManagerStateRestore dbgRestorer;
783     DebugManager.flags.EnableDeviceBasedTimestamps.set(true);
784 
785     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
786     MockCommandQueue cmdQ(mockContext, pClDevice, props, false);
787     MockEvent<Event> event(&cmdQ, CL_COMPLETE, 0, 0);
788 
789     HwTimeStamps timestamp{};
790     timestamp.GlobalStartTS = 1;
791     event.queueTimeStamp.GPUTimeStamp = 2;
792     TagNode<HwTimeStamps> timestampNode{};
793     timestampNode.tagForCpuAccess = &timestamp;
794     event.timeStampNode = &timestampNode;
795 
796     uint64_t start = 0u;
797     event.getEventProfilingInfo(CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, nullptr);
798 
799     auto &hwHelper = HwHelper::get(pClDevice->getHardwareInfo().platform.eRenderCoreFamily);
800     auto resolution = pClDevice->getDevice().getDeviceInfo().profilingTimerResolution;
801     auto refStartTime = static_cast<uint64_t>(timestamp.GlobalStartTS * resolution + (1ULL << hwHelper.getGlobalTimeStampBits()) * resolution);
802     EXPECT_EQ(start, refStartTime);
803 
804     event.timeStampNode = nullptr;
805 }
806 
TEST_F(InternalsEventTest,GivenProfilingWHENMapOperationTHENTimesSet)807 TEST_F(InternalsEventTest, GivenProfilingWHENMapOperationTHENTimesSet) {
808     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
809     MockCommandQueue *pCmdQ = new MockCommandQueue(mockContext, pClDevice, props, false);
810 
811     MockEvent<Event> *event = new MockEvent<Event>(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
812 
813     auto &csr = pCmdQ->getGpgpuCommandStreamReceiver();
814     UnalignedBuffer buffer;
815 
816     MemObjSizeArray size = {{1, 1, 1}};
817     MemObjOffsetArray offset = {{0, 0, 0}};
818     event->setCommand(std::unique_ptr<Command>(new CommandMapUnmap(MAP, buffer, size, offset, false, *pCmdQ)));
819 
820     auto taskLevelBefore = csr.peekTaskLevel();
821 
822     event->submitCommand(false);
823     uint64_t submitTime = 0ULL;
824     event->getEventProfilingInfo(CL_PROFILING_COMMAND_SUBMIT, sizeof(uint64_t), &submitTime, 0);
825     EXPECT_NE(0ULL, submitTime);
826     auto taskLevelAfter = csr.peekTaskLevel();
827 
828     delete event;
829 
830     EXPECT_EQ(taskLevelBefore + 1, taskLevelAfter);
831     delete pCmdQ;
832 }
833 
TEST_F(InternalsEventTest,GivenUnMapOperationWhenSubmittingCommandsThenTaskLevelIsIncremented)834 TEST_F(InternalsEventTest, GivenUnMapOperationWhenSubmittingCommandsThenTaskLevelIsIncremented) {
835     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, 0, 0};
836     auto pCmdQ = make_releaseable<MockCommandQueue>(mockContext, pClDevice, props, false);
837     MockEvent<Event> event(pCmdQ.get(), CL_COMMAND_NDRANGE_KERNEL, 0, 0);
838 
839     auto &csr = pCmdQ->getGpgpuCommandStreamReceiver();
840     auto buffer = new UnalignedBuffer;
841 
842     MemObjSizeArray size = {{1, 1, 1}};
843     MemObjOffsetArray offset = {{0, 0, 0}};
844     event.setCommand(std::unique_ptr<Command>(new CommandMapUnmap(UNMAP, *buffer, size, offset, false, *pCmdQ)));
845 
846     auto taskLevelBefore = csr.peekTaskLevel();
847 
848     event.submitCommand(false);
849 
850     auto taskLevelAfter = csr.peekTaskLevel();
851 
852     EXPECT_EQ(taskLevelBefore + 1, taskLevelAfter);
853     buffer->decRefInternal();
854 }
855 
TEST_F(InternalsEventTest,givenBlockedMapCommandWhenSubmitIsCalledThenItReleasesMemObjectReference)856 TEST_F(InternalsEventTest, givenBlockedMapCommandWhenSubmitIsCalledThenItReleasesMemObjectReference) {
857     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, 0, 0};
858     auto pCmdQ = std::make_unique<MockCommandQueue>(mockContext, pClDevice, props, false);
859     MockEvent<Event> event(pCmdQ.get(), CL_COMMAND_NDRANGE_KERNEL, 0, 0);
860 
861     auto buffer = new UnalignedBuffer;
862 
863     auto currentBufferRefInternal = buffer->getRefInternalCount();
864 
865     MemObjSizeArray size = {{1, 1, 1}};
866     MemObjOffsetArray offset = {{0, 0, 0}};
867     event.setCommand(std::unique_ptr<Command>(new CommandMapUnmap(UNMAP, *buffer, size, offset, false, *pCmdQ)));
868     EXPECT_EQ(currentBufferRefInternal + 1, buffer->getRefInternalCount());
869 
870     event.submitCommand(false);
871 
872     EXPECT_EQ(currentBufferRefInternal, buffer->getRefInternalCount());
873     buffer->decRefInternal();
874 }
TEST_F(InternalsEventTest,GivenUnMapOperationNonZeroCopyBufferWhenSubmittingCommandsThenTaskLevelIsIncremented)875 TEST_F(InternalsEventTest, GivenUnMapOperationNonZeroCopyBufferWhenSubmittingCommandsThenTaskLevelIsIncremented) {
876     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, 0, 0};
877     auto pCmdQ = std::make_unique<MockCommandQueue>(mockContext, pClDevice, props, false);
878     MockEvent<Event> event(pCmdQ.get(), CL_COMMAND_NDRANGE_KERNEL, 0, 0);
879 
880     auto &csr = pCmdQ->getGpgpuCommandStreamReceiver();
881     auto buffer = new UnalignedBuffer;
882 
883     MemObjSizeArray size = {{1, 1, 1}};
884     MemObjOffsetArray offset = {{0, 0, 0}};
885     event.setCommand(std::unique_ptr<Command>(new CommandMapUnmap(UNMAP, *buffer, size, offset, false, *pCmdQ)));
886 
887     auto taskLevelBefore = csr.peekTaskLevel();
888 
889     event.submitCommand(false);
890 
891     auto taskLevelAfter = csr.peekTaskLevel();
892 
893     EXPECT_EQ(taskLevelBefore + 1, taskLevelAfter);
894     buffer->decRefInternal();
895 }
896 
HWTEST_F(InternalsEventTest,givenCpuProfilingPathWhenEnqueuedMarkerThenDontUseTimeStampNode)897 HWTEST_F(InternalsEventTest, givenCpuProfilingPathWhenEnqueuedMarkerThenDontUseTimeStampNode) {
898     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
899     MockCommandQueue *pCmdQ = new MockCommandQueue(mockContext, pClDevice, props, false);
900     MockEvent<Event> *event = new MockEvent<Event>(pCmdQ, CL_COMMAND_MARKER, 0, 0);
901     event->setCPUProfilingPath(true);
902 
903     event->setCommand(std::unique_ptr<Command>(new CommandWithoutKernel(*pCmdQ)));
904 
905     event->submitCommand(false);
906 
907     uint64_t submit, start, end;
908     event->getEventProfilingInfo(CL_PROFILING_COMMAND_SUBMIT, sizeof(uint64_t), &submit, 0);
909     event->getEventProfilingInfo(CL_PROFILING_COMMAND_START, sizeof(uint64_t), &start, 0);
910     event->getEventProfilingInfo(CL_PROFILING_COMMAND_END, sizeof(uint64_t), &end, 0);
911 
912     EXPECT_LT(0u, submit);
913     EXPECT_LT(submit, start);
914     EXPECT_LT(start, end);
915     delete event;
916     delete pCmdQ;
917 }
918 
919 struct InternalsEventWithPerfCountersTest
920     : public InternalsEventTest,
921       public PerformanceCountersFixture {
SetUpInternalsEventWithPerfCountersTest922     void SetUp() override {
923         PerformanceCountersFixture::SetUp();
924         InternalsEventTest::SetUp();
925         createPerfCounters();
926         pDevice->setPerfCounters(performanceCountersBase.get());
927     }
928 
TearDownInternalsEventWithPerfCountersTest929     void TearDown() override {
930         performanceCountersBase.release();
931         InternalsEventTest::TearDown();
932         PerformanceCountersFixture::TearDown();
933     }
934 };
HWTEST_F(InternalsEventWithPerfCountersTest,givenCpuProfilingPerfCountersPathWhenEnqueuedMarkerThenDontUseTimeStampNodePerfCounterNode)935 HWTEST_F(InternalsEventWithPerfCountersTest, givenCpuProfilingPerfCountersPathWhenEnqueuedMarkerThenDontUseTimeStampNodePerfCounterNode) {
936     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
937     MockCommandQueue *pCmdQ = new MockCommandQueue(mockContext, pClDevice, props, false);
938     bool ret = false;
939     ret = pCmdQ->setPerfCountersEnabled();
940     EXPECT_TRUE(ret);
941 
942     MockEvent<Event> *event = new MockEvent<Event>(pCmdQ, CL_COMMAND_MARKER, 0, 0);
943     event->setCPUProfilingPath(true);
944 
945     event->setCommand(std::unique_ptr<Command>(new CommandWithoutKernel(*pCmdQ)));
946 
947     event->submitCommand(false);
948 
949     uint64_t submit, start, end;
950     event->getEventProfilingInfo(CL_PROFILING_COMMAND_SUBMIT, sizeof(uint64_t), &submit, 0);
951     event->getEventProfilingInfo(CL_PROFILING_COMMAND_START, sizeof(uint64_t), &start, 0);
952     event->getEventProfilingInfo(CL_PROFILING_COMMAND_END, sizeof(uint64_t), &end, 0);
953 
954     EXPECT_LT(0u, submit);
955     EXPECT_LT(submit, start);
956     EXPECT_LT(start, end);
957     delete event;
958     delete pCmdQ;
959 }
960 
HWTEST_F(InternalsEventWithPerfCountersTest,givenCpuProfilingPerfCountersPathWhenEnqueuedMarkerThenUseTimeStampNodePerfCounterNode)961 HWTEST_F(InternalsEventWithPerfCountersTest, givenCpuProfilingPerfCountersPathWhenEnqueuedMarkerThenUseTimeStampNodePerfCounterNode) {
962     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
963     MockCommandQueue *pCmdQ = new MockCommandQueue(mockContext, pClDevice, props, false);
964     pCmdQ->setPerfCountersEnabled();
965     MockEvent<Event> *event = new MockEvent<Event>(pCmdQ, CL_COMMAND_MARKER, 0, 0);
966     event->setCPUProfilingPath(true);
967     HwPerfCounter *perfCounter = static_cast<TagNode<HwPerfCounter> *>(event->getHwPerfCounterNode())->tagForCpuAccess;
968     ASSERT_NE(nullptr, perfCounter);
969 
970     auto hwTimeStampNode = static_cast<TagNode<HwTimeStamps> *>(event->getHwTimeStampNode());
971     if (pCmdQ->getTimestampPacketContainer()) {
972         EXPECT_EQ(nullptr, hwTimeStampNode);
973     } else {
974         ASSERT_NE(nullptr, hwTimeStampNode->tagForCpuAccess);
975     }
976 
977     event->setCommand(std::unique_ptr<Command>(new CommandWithoutKernel(*pCmdQ)));
978 
979     event->submitCommand(false);
980 
981     uint64_t submit, start, end;
982     event->getEventProfilingInfo(CL_PROFILING_COMMAND_SUBMIT, sizeof(uint64_t), &submit, 0);
983     event->getEventProfilingInfo(CL_PROFILING_COMMAND_START, sizeof(uint64_t), &start, 0);
984     event->getEventProfilingInfo(CL_PROFILING_COMMAND_END, sizeof(uint64_t), &end, 0);
985 
986     EXPECT_LT(0u, submit);
987     EXPECT_LT(submit, start);
988     EXPECT_LT(start, end);
989     delete event;
990     delete pCmdQ;
991 }
992 
TEST_F(InternalsEventWithPerfCountersTest,GivenPerfCountersEnabledWhenEventIsCreatedThenProfilingEnabledAndPerfCountersEnabledAreTrue)993 TEST_F(InternalsEventWithPerfCountersTest, GivenPerfCountersEnabledWhenEventIsCreatedThenProfilingEnabledAndPerfCountersEnabledAreTrue) {
994     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
995     MockCommandQueue *pCmdQ = new MockCommandQueue(mockContext, pClDevice, props, false);
996     pCmdQ->setPerfCountersEnabled();
997     Event *ev = new Event(pCmdQ, CL_COMMAND_COPY_BUFFER, 3, 0);
998     EXPECT_TRUE(ev->isProfilingEnabled());
999     EXPECT_TRUE(ev->isPerfCountersEnabled());
1000     delete ev;
1001     delete pCmdQ;
1002 }
1003 
TEST(Event,WhenReleasingEventThenEventIsNull)1004 TEST(Event, WhenReleasingEventThenEventIsNull) {
1005     UserEvent *ue = new UserEvent();
1006     auto autoptr = ue->release();
1007     ASSERT_TRUE(autoptr.isUnused());
1008 }
1009 
HWTEST_F(EventTest,givenVirtualEventWhenCommandSubmittedThenLockCsrOccurs)1010 HWTEST_F(EventTest, givenVirtualEventWhenCommandSubmittedThenLockCsrOccurs) {
1011     class MockCommandComputeKernel : public CommandComputeKernel {
1012       public:
1013         using CommandComputeKernel::eventsWaitlist;
1014         MockCommandComputeKernel(CommandQueue &commandQueue, std::unique_ptr<KernelOperation> &kernelOperation, std::vector<Surface *> &surfaces, Kernel *kernel)
1015             : CommandComputeKernel(commandQueue, kernelOperation, surfaces, false, false, false, nullptr, PreemptionMode::Disabled, kernel, 0) {}
1016     };
1017     class MockEvent : public Event {
1018       public:
1019         using Event::submitCommand;
1020         MockEvent(CommandQueue *cmdQueue, cl_command_type cmdType,
1021                   uint32_t taskLevel, uint32_t taskCount) : Event(cmdQueue, cmdType,
1022                                                                   taskLevel, taskCount) {}
1023     };
1024 
1025     MockKernelWithInternals kernel(*pClDevice);
1026 
1027     IndirectHeap *ih1 = nullptr, *ih2 = nullptr, *ih3 = nullptr;
1028     pCmdQ->allocateHeapMemory(IndirectHeap::DYNAMIC_STATE, 1, ih1);
1029     pCmdQ->allocateHeapMemory(IndirectHeap::INDIRECT_OBJECT, 1, ih2);
1030     pCmdQ->allocateHeapMemory(IndirectHeap::SURFACE_STATE, 1, ih3);
1031     auto cmdStream = new LinearStream(pDevice->getMemoryManager()->allocateGraphicsMemoryWithProperties({pDevice->getRootDeviceIndex(), 4096, GraphicsAllocation::AllocationType::COMMAND_BUFFER, pDevice->getDeviceBitfield()}));
1032 
1033     std::vector<Surface *> surfaces;
1034     auto kernelOperation = std::make_unique<KernelOperation>(cmdStream, *pDevice->getDefaultEngine().commandStreamReceiver->getInternalAllocationStorage());
1035     kernelOperation->setHeaps(ih1, ih2, ih3);
1036 
1037     std::unique_ptr<MockCommandComputeKernel> command = std::make_unique<MockCommandComputeKernel>(*pCmdQ, kernelOperation, surfaces, kernel);
1038 
1039     auto virtualEvent = make_releaseable<MockEvent>(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, CompletionStamp::notReady);
1040 
1041     virtualEvent->setCommand(std::move(command));
1042 
1043     virtualEvent->submitCommand(false);
1044 
1045     uint32_t expectedLockCounter = pDevice->getDefaultEngine().commandStreamReceiver->getClearColorAllocation() ? 3u : 2u;
1046 
1047     EXPECT_EQ(expectedLockCounter, pDevice->getUltCommandStreamReceiver<FamilyType>().recursiveLockCounter);
1048 }
1049 
HWTEST_F(EventTest,givenVirtualEventWhenSubmitCommandEventNotReadyAndEventWithoutCommandThenOneLockCsrNeeded)1050 HWTEST_F(EventTest, givenVirtualEventWhenSubmitCommandEventNotReadyAndEventWithoutCommandThenOneLockCsrNeeded) {
1051     class MockEvent : public Event {
1052       public:
1053         using Event::submitCommand;
1054         MockEvent(CommandQueue *cmdQueue, cl_command_type cmdType,
1055                   uint32_t taskLevel, uint32_t taskCount) : Event(cmdQueue, cmdType,
1056                                                                   taskLevel, taskCount) {}
1057     };
1058 
1059     auto virtualEvent = make_releaseable<MockEvent>(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, CompletionStamp::notReady);
1060 
1061     virtualEvent->submitCommand(false);
1062 
1063     EXPECT_EQ(pDevice->getUltCommandStreamReceiver<FamilyType>().recursiveLockCounter, 1u);
1064 }
1065 
HWTEST_F(InternalsEventTest,GivenBufferWithoutZeroCopyWhenMappingOrUnmappingThenFlushPreviousTasksBeforeMappingOrUnmapping)1066 HWTEST_F(InternalsEventTest, GivenBufferWithoutZeroCopyWhenMappingOrUnmappingThenFlushPreviousTasksBeforeMappingOrUnmapping) {
1067     struct MockNonZeroCopyBuff : UnalignedBuffer {
1068         MockNonZeroCopyBuff(int32_t &executionStamp)
1069             : executionStamp(executionStamp), dataTransferedStamp(-1) {
1070             hostPtr = &dataTransferedStamp;
1071             memoryStorage = &executionStamp;
1072             size = sizeof(executionStamp);
1073             hostPtrMinSize = size;
1074         }
1075         void setIsZeroCopy() {
1076             isZeroCopy = false;
1077         }
1078 
1079         void swapCopyDirection() {
1080             std::swap(hostPtr, memoryStorage);
1081         }
1082 
1083         int32_t &executionStamp;
1084         int32_t dataTransferedStamp;
1085     };
1086 
1087     int32_t executionStamp = 0;
1088     auto csr = new MockCsr<FamilyType>(executionStamp, *pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield());
1089     pDevice->resetCommandStreamReceiver(csr);
1090 
1091     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, 0, 0};
1092     auto pCmdQ = make_releaseable<MockCommandQueue>(mockContext, pClDevice, props, false);
1093 
1094     MockNonZeroCopyBuff buffer(executionStamp);
1095 
1096     MemObjSizeArray size = {{4, 1, 1}};
1097     MemObjOffsetArray offset = {{0, 0, 0}};
1098     auto commandMap = std::unique_ptr<Command>(new CommandMapUnmap(MAP, buffer, size, offset, false, *pCmdQ));
1099     EXPECT_EQ(0, executionStamp);
1100     EXPECT_EQ(-1, csr->flushTaskStamp);
1101     EXPECT_EQ(-1, buffer.dataTransferedStamp);
1102 
1103     auto latestSentFlushTaskCount = csr->peekLatestSentTaskCount();
1104 
1105     commandMap->submit(0, false);
1106     EXPECT_EQ(1, executionStamp);
1107     EXPECT_EQ(0, csr->flushTaskStamp);
1108     EXPECT_EQ(1, buffer.dataTransferedStamp);
1109     auto latestSentFlushTaskCountAfterSubmit = csr->peekLatestSentTaskCount();
1110     EXPECT_GT(latestSentFlushTaskCountAfterSubmit, latestSentFlushTaskCount);
1111 
1112     executionStamp = 0;
1113     csr->flushTaskStamp = -1;
1114     buffer.dataTransferedStamp = -1;
1115     buffer.swapCopyDirection();
1116 
1117     auto commandUnMap = std::unique_ptr<Command>(new CommandMapUnmap(UNMAP, buffer, size, offset, false, *pCmdQ));
1118     EXPECT_EQ(0, executionStamp);
1119     EXPECT_EQ(-1, csr->flushTaskStamp);
1120     EXPECT_EQ(-1, buffer.dataTransferedStamp);
1121     commandUnMap->submit(0, false);
1122     EXPECT_EQ(1, executionStamp);
1123     EXPECT_EQ(0, csr->flushTaskStamp);
1124     EXPECT_EQ(1, buffer.dataTransferedStamp);
1125     EXPECT_EQ(nullptr, commandUnMap->getCommandStream());
1126 }
1127 
TEST(EventCallback,WhenOverridingStatusThenEventUsesNewStatus)1128 TEST(EventCallback, WhenOverridingStatusThenEventUsesNewStatus) {
1129     struct ClbFuncTempStruct {
1130         static void CL_CALLBACK ClbFuncT(cl_event e, cl_int status, void *retStatus) {
1131             *((cl_int *)retStatus) = status;
1132         }
1133     };
1134 
1135     cl_int retStatus = 7;
1136     Event::Callback clb(nullptr, ClbFuncTempStruct::ClbFuncT, CL_COMPLETE, &retStatus);
1137     EXPECT_EQ(CL_COMPLETE, clb.getCallbackExecutionStatusTarget());
1138     clb.execute();
1139     EXPECT_EQ(CL_COMPLETE, retStatus);
1140 
1141     retStatus = 7;
1142     clb.overrideCallbackExecutionStatusTarget(-1);
1143     EXPECT_EQ(-1, clb.getCallbackExecutionStatusTarget());
1144     clb.execute();
1145     EXPECT_EQ(-1, retStatus);
1146 }
1147 
TEST_F(EventTest,WhenSettingCpuTimeStampThenCorrectTimeIsSet)1148 TEST_F(EventTest, WhenSettingCpuTimeStampThenCorrectTimeIsSet) {
1149     MyEvent ev(this->pCmdQ, CL_COMMAND_COPY_BUFFER, 3, 0);
1150 
1151     ev.setProfilingEnabled(true);
1152     ev.setQueueTimeStamp();
1153     TimeStampData outtimeStamp = {0, 0};
1154     outtimeStamp = ev.getQueueTimeStamp();
1155     EXPECT_NE(0ULL, outtimeStamp.CPUTimeinNS);
1156     EXPECT_EQ(0ULL, outtimeStamp.GPUTimeStamp);
1157 
1158     ev.setSubmitTimeStamp();
1159     outtimeStamp = ev.getSubmitTimeStamp();
1160     EXPECT_NE(0ULL, outtimeStamp.CPUTimeinNS);
1161     EXPECT_EQ(0ULL, outtimeStamp.GPUTimeStamp);
1162 
1163     ev.setStartTimeStamp();
1164     uint64_t outCPUtimeStamp = ev.getStartTimeStamp();
1165     EXPECT_NE(0ULL, outCPUtimeStamp);
1166 
1167     ev.setEndTimeStamp();
1168     outCPUtimeStamp = ev.getEndTimeStamp();
1169     EXPECT_NE(0ULL, outCPUtimeStamp);
1170 
1171     outCPUtimeStamp = ev.getCompleteTimeStamp();
1172     EXPECT_NE(0ULL, outCPUtimeStamp);
1173 }
1174 
TEST_F(EventTest,GivenNoQueueWhenSettingCpuTimeStampThenTimesIsNotSet)1175 TEST_F(EventTest, GivenNoQueueWhenSettingCpuTimeStampThenTimesIsNotSet) {
1176     MyEvent ev(nullptr, CL_COMMAND_COPY_BUFFER, 3, 0);
1177 
1178     ev.setQueueTimeStamp();
1179     TimeStampData outtimeStamp = {0, 0};
1180     outtimeStamp = ev.getQueueTimeStamp();
1181     EXPECT_EQ(0ULL, outtimeStamp.CPUTimeinNS);
1182     EXPECT_EQ(0ULL, outtimeStamp.GPUTimeStamp);
1183 
1184     ev.setSubmitTimeStamp();
1185     outtimeStamp = ev.getSubmitTimeStamp();
1186     EXPECT_EQ(0ULL, outtimeStamp.CPUTimeinNS);
1187     EXPECT_EQ(0ULL, outtimeStamp.GPUTimeStamp);
1188 
1189     ev.setStartTimeStamp();
1190     uint64_t outCPUtimeStamp = ev.getStartTimeStamp();
1191     EXPECT_EQ(0ULL, outCPUtimeStamp);
1192 
1193     ev.setEndTimeStamp();
1194     outCPUtimeStamp = ev.getEndTimeStamp();
1195     EXPECT_EQ(0ULL, outCPUtimeStamp);
1196 
1197     outCPUtimeStamp = ev.getCompleteTimeStamp();
1198     EXPECT_EQ(0ULL, outCPUtimeStamp);
1199 }
1200 
HWTEST_F(EventTest,WhenGettingHwTimeStampsThenValidPointerIsReturned)1201 HWTEST_F(EventTest, WhenGettingHwTimeStampsThenValidPointerIsReturned) {
1202     pDevice->getUltCommandStreamReceiver<FamilyType>().timestampPacketWriteEnabled = false;
1203 
1204     auto myCmdQ = std::make_unique<MockCommandQueueHw<FamilyType>>(pCmdQ->getContextPtr(), pClDevice, nullptr);
1205 
1206     std::unique_ptr<Event> event(new Event(myCmdQ.get(), CL_COMMAND_COPY_BUFFER, 0, 0));
1207     ASSERT_NE(nullptr, event);
1208 
1209     HwTimeStamps *timeStamps = static_cast<TagNode<HwTimeStamps> *>(event->getHwTimeStampNode())->tagForCpuAccess;
1210     ASSERT_NE(nullptr, timeStamps);
1211 
1212     //this should not cause any heap corruptions
1213     ASSERT_EQ(0ULL, timeStamps->GlobalStartTS);
1214     ASSERT_EQ(0ULL, timeStamps->ContextStartTS);
1215     ASSERT_EQ(0ULL, timeStamps->GlobalEndTS);
1216     ASSERT_EQ(0ULL, timeStamps->ContextEndTS);
1217     ASSERT_EQ(0ULL, timeStamps->GlobalCompleteTS);
1218     ASSERT_EQ(0ULL, timeStamps->ContextCompleteTS);
1219 
1220     HwTimeStamps *timeStamps2 = static_cast<TagNode<HwTimeStamps> *>(event->getHwTimeStampNode())->tagForCpuAccess;
1221     ASSERT_EQ(timeStamps, timeStamps2);
1222 }
1223 
HWTEST_F(EventTest,WhenGetHwTimeStampsAllocationThenValidPointerIsReturned)1224 HWTEST_F(EventTest, WhenGetHwTimeStampsAllocationThenValidPointerIsReturned) {
1225     pDevice->getUltCommandStreamReceiver<FamilyType>().timestampPacketWriteEnabled = false;
1226 
1227     auto myCmdQ = std::make_unique<MockCommandQueueHw<FamilyType>>(pCmdQ->getContextPtr(), pClDevice, nullptr);
1228 
1229     std::unique_ptr<Event> event(new Event(myCmdQ.get(), CL_COMMAND_COPY_BUFFER, 0, 0));
1230     ASSERT_NE(nullptr, event);
1231 
1232     GraphicsAllocation *allocation = event->getHwTimeStampNode()->getBaseGraphicsAllocation()->getDefaultGraphicsAllocation();
1233     ASSERT_NE(nullptr, allocation);
1234 
1235     void *memoryStorage = allocation->getUnderlyingBuffer();
1236     size_t memoryStorageSize = allocation->getUnderlyingBufferSize();
1237 
1238     EXPECT_NE(nullptr, memoryStorage);
1239     EXPECT_GT(memoryStorageSize, 0u);
1240 }
1241 
HWTEST_F(EventTest,WhenEventIsCreatedThenHwTimeStampsMemoryIsPlacedInGraphicsAllocation)1242 HWTEST_F(EventTest, WhenEventIsCreatedThenHwTimeStampsMemoryIsPlacedInGraphicsAllocation) {
1243     pDevice->getUltCommandStreamReceiver<FamilyType>().timestampPacketWriteEnabled = false;
1244 
1245     auto myCmdQ = std::make_unique<MockCommandQueueHw<FamilyType>>(pCmdQ->getContextPtr(), pClDevice, nullptr);
1246 
1247     std::unique_ptr<Event> event(new Event(myCmdQ.get(), CL_COMMAND_COPY_BUFFER, 0, 0));
1248     ASSERT_NE(nullptr, event);
1249 
1250     HwTimeStamps *timeStamps = static_cast<TagNode<HwTimeStamps> *>(event->getHwTimeStampNode())->tagForCpuAccess;
1251     ASSERT_NE(nullptr, timeStamps);
1252 
1253     GraphicsAllocation *allocation = event->getHwTimeStampNode()->getBaseGraphicsAllocation()->getDefaultGraphicsAllocation();
1254     ASSERT_NE(nullptr, allocation);
1255 
1256     void *memoryStorage = allocation->getUnderlyingBuffer();
1257     size_t graphicsAllocationSize = allocation->getUnderlyingBufferSize();
1258 
1259     EXPECT_GE(timeStamps, memoryStorage);
1260     EXPECT_LE(timeStamps + 1, ptrOffset(memoryStorage, graphicsAllocationSize));
1261 }
1262 
TEST_F(EventTest,GivenNullQueueWhenEventIsCreatedThenProfilingAndPerfCountersAreDisabled)1263 TEST_F(EventTest, GivenNullQueueWhenEventIsCreatedThenProfilingAndPerfCountersAreDisabled) {
1264     Event ev(nullptr, CL_COMMAND_COPY_BUFFER, 3, 0);
1265     EXPECT_FALSE(ev.isProfilingEnabled());
1266     EXPECT_FALSE(ev.isPerfCountersEnabled());
1267 }
1268 
TEST_F(EventTest,GivenProfilingDisabledWhenEventIsCreatedThenPerfCountersAreDisabled)1269 TEST_F(EventTest, GivenProfilingDisabledWhenEventIsCreatedThenPerfCountersAreDisabled) {
1270     Event ev(pCmdQ, CL_COMMAND_COPY_BUFFER, 3, 0);
1271     EXPECT_FALSE(ev.isProfilingEnabled());
1272     EXPECT_FALSE(ev.isPerfCountersEnabled());
1273 }
1274 
TEST_F(InternalsEventTest,GivenOnlyProfilingEnabledWhenEventIsCreatedThenPerfCountersAreDisabled)1275 TEST_F(InternalsEventTest, GivenOnlyProfilingEnabledWhenEventIsCreatedThenPerfCountersAreDisabled) {
1276     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
1277     MockCommandQueue *pCmdQ = new MockCommandQueue(mockContext, pClDevice, props, false);
1278 
1279     Event *ev = new Event(pCmdQ, CL_COMMAND_COPY_BUFFER, 3, 0);
1280     EXPECT_TRUE(ev->isProfilingEnabled());
1281     EXPECT_FALSE(ev->isPerfCountersEnabled());
1282 
1283     delete ev;
1284     delete pCmdQ;
1285 }
1286 
TEST_F(EventTest,GivenClSubmittedWhenpeekIsSubmittedThenTrueIsReturned)1287 TEST_F(EventTest, GivenClSubmittedWhenpeekIsSubmittedThenTrueIsReturned) {
1288     Event ev(this->pCmdQ, CL_COMMAND_COPY_BUFFER, 3, 0);
1289     int32_t executionStatusSnapshot = CL_SUBMITTED;
1290     bool executionStatus = ev.peekIsSubmitted(executionStatusSnapshot);
1291     EXPECT_EQ(true, executionStatus);
1292 }
1293 
TEST_F(EventTest,GivenCompletedEventWhenQueryingExecutionStatusAfterFlushThenCsrIsNotFlushed)1294 TEST_F(EventTest, GivenCompletedEventWhenQueryingExecutionStatusAfterFlushThenCsrIsNotFlushed) {
1295     cl_int ret;
1296     Event ev(this->pCmdQ, CL_COMMAND_COPY_BUFFER, 3, 3);
1297     auto &csr = this->pCmdQ->getGpgpuCommandStreamReceiver();
1298     *csr.getTagAddress() = 3;
1299     auto previousTaskLevel = csr.peekTaskLevel();
1300     EXPECT_GT(3u, previousTaskLevel);
1301     ret = clFlush(this->pCmdQ);
1302     ASSERT_EQ(CL_SUCCESS, ret);
1303     cl_int execState;
1304     ret = clGetEventInfo(&ev, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(execState), &execState, nullptr);
1305     ASSERT_EQ(CL_SUCCESS, ret);
1306     EXPECT_EQ(previousTaskLevel, csr.peekTaskLevel());
1307 }
1308 
HWTEST_F(EventTest,GivenEventCreatedOnMapBufferWithoutCommandWhenSubmittingCommandThenTaskCountIsNotUpdated)1309 HWTEST_F(EventTest, GivenEventCreatedOnMapBufferWithoutCommandWhenSubmittingCommandThenTaskCountIsNotUpdated) {
1310     MockEvent<Event> ev(this->pCmdQ, CL_COMMAND_MAP_BUFFER, CompletionStamp::notReady, CompletionStamp::notReady);
1311 
1312     EXPECT_EQ(CompletionStamp::notReady, ev.peekTaskCount());
1313     ev.submitCommand(false);
1314     EXPECT_EQ(0u, ev.peekTaskCount());
1315 }
1316 
HWTEST_F(EventTest,GivenEventCreatedOnMapImageWithoutCommandWhenSubmittingCommandThenTaskCountIsNotUpdated)1317 HWTEST_F(EventTest, GivenEventCreatedOnMapImageWithoutCommandWhenSubmittingCommandThenTaskCountIsNotUpdated) {
1318     MockEvent<Event> ev(this->pCmdQ, CL_COMMAND_MAP_IMAGE, CompletionStamp::notReady, CompletionStamp::notReady);
1319 
1320     EXPECT_EQ(CompletionStamp::notReady, ev.peekTaskCount());
1321     ev.submitCommand(false);
1322     EXPECT_EQ(0u, ev.peekTaskCount());
1323 }
1324 
TEST_F(EventTest,givenCmdQueueWithoutProfilingWhenIsCpuProfilingIsCalledThenFalseIsReturned)1325 TEST_F(EventTest, givenCmdQueueWithoutProfilingWhenIsCpuProfilingIsCalledThenFalseIsReturned) {
1326     MockEvent<Event> ev(this->pCmdQ, CL_COMMAND_MAP_IMAGE, CompletionStamp::notReady, CompletionStamp::notReady);
1327     bool cpuProfiling = ev.isCPUProfilingPath() != 0;
1328     EXPECT_FALSE(cpuProfiling);
1329 }
1330 
TEST_F(EventTest,givenOutEventWhenBlockingEnqueueHandledOnCpuThenUpdateTaskCountAndFlushStampFromCmdQ)1331 TEST_F(EventTest, givenOutEventWhenBlockingEnqueueHandledOnCpuThenUpdateTaskCountAndFlushStampFromCmdQ) {
1332     std::unique_ptr<Image> image(ImageHelper<Image1dDefaults>::create(&mockContext));
1333     EXPECT_TRUE(image->mappingOnCpuAllowed());
1334 
1335     pCmdQ->flushStamp->setStamp(10);
1336     pCmdQ->taskCount = 11;
1337 
1338     size_t origin[3] = {0, 0, 0};
1339     size_t region[3] = {1, 1, 1};
1340 
1341     cl_int retVal;
1342     cl_event clEvent;
1343     pCmdQ->enqueueMapImage(image.get(), CL_TRUE, CL_MAP_READ, origin, region, nullptr, nullptr, 0, nullptr, &clEvent, retVal);
1344 
1345     auto eventObj = castToObject<Event>(clEvent);
1346     EXPECT_EQ(pCmdQ->taskCount, eventObj->peekTaskCount());
1347     EXPECT_EQ(pCmdQ->flushStamp->peekStamp(), eventObj->flushStamp->peekStamp());
1348     eventObj->release();
1349 }
1350 
TEST_F(EventTest,givenCmdQueueWithProfilingWhenIsCpuProfilingIsCalledThenTrueIsReturned)1351 TEST_F(EventTest, givenCmdQueueWithProfilingWhenIsCpuProfilingIsCalledThenTrueIsReturned) {
1352     const cl_queue_properties props[3] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0};
1353     std::unique_ptr<MockCommandQueue> pCmdQ(new MockCommandQueue(&mockContext, pClDevice, props, false));
1354 
1355     MockEvent<Event> ev(pCmdQ.get(), CL_COMMAND_MAP_IMAGE, CompletionStamp::notReady, CompletionStamp::notReady);
1356     bool cpuProfiling = ev.isCPUProfilingPath() != 0;
1357     EXPECT_TRUE(cpuProfiling);
1358 }
1359 
TEST(EventCallback,GivenEventWithCallbacksOnWhenPeekingHasCallbacksThenReturnTrue)1360 TEST(EventCallback, GivenEventWithCallbacksOnWhenPeekingHasCallbacksThenReturnTrue) {
1361     DebugManagerStateRestore dbgRestore;
1362     DebugManager.flags.EnableAsyncEventsHandler.set(false);
1363     struct ClbFuncTempStruct {
1364         static void CL_CALLBACK ClbFuncT(cl_event, cl_int, void *) {
1365         }
1366     };
1367 
1368     struct SmallMockEvent : Event {
1369         SmallMockEvent()
1370             : Event(nullptr, CL_COMMAND_COPY_BUFFER, 0, 0) {
1371             this->parentCount = 1; // block event
1372         }
1373     };
1374 
1375     {
1376         SmallMockEvent ev;
1377         EXPECT_FALSE(ev.peekHasCallbacks());
1378     }
1379 
1380     {
1381         SmallMockEvent ev;
1382         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_SUBMITTED, nullptr);
1383         EXPECT_TRUE(ev.peekHasCallbacks());
1384         ev.decRefInternal();
1385     }
1386 
1387     {
1388         SmallMockEvent ev;
1389         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_RUNNING, nullptr);
1390         EXPECT_TRUE(ev.peekHasCallbacks());
1391         ev.decRefInternal();
1392     }
1393 
1394     {
1395         SmallMockEvent ev;
1396         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_COMPLETE, nullptr);
1397         EXPECT_TRUE(ev.peekHasCallbacks());
1398         ev.decRefInternal();
1399     }
1400 
1401     {
1402         SmallMockEvent ev;
1403         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_SUBMITTED, nullptr);
1404         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_COMPLETE, nullptr);
1405         EXPECT_TRUE(ev.peekHasCallbacks());
1406         ev.decRefInternal();
1407         ev.decRefInternal();
1408     }
1409 
1410     {
1411         SmallMockEvent ev;
1412         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_RUNNING, nullptr);
1413         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_COMPLETE, nullptr);
1414         EXPECT_TRUE(ev.peekHasCallbacks());
1415         ev.decRefInternal();
1416         ev.decRefInternal();
1417     }
1418 
1419     {
1420         SmallMockEvent ev;
1421         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_SUBMITTED, nullptr);
1422         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_RUNNING, nullptr);
1423         ev.addCallback(ClbFuncTempStruct::ClbFuncT, CL_COMPLETE, nullptr);
1424         EXPECT_TRUE(ev.peekHasCallbacks());
1425         ev.decRefInternal();
1426         ev.decRefInternal();
1427         ev.decRefInternal();
1428     }
1429 }
1430 
TEST_F(EventTest,GivenNotCompletedEventWhenAddingChildThenNumEventsBlockingThisIsGreaterThanZero)1431 TEST_F(EventTest, GivenNotCompletedEventWhenAddingChildThenNumEventsBlockingThisIsGreaterThanZero) {
1432     VirtualEvent virtualEvent(pCmdQ, &mockContext);
1433     {
1434         Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
1435         event.addChild(virtualEvent);
1436         EXPECT_NE(0U, virtualEvent.peekNumEventsBlockingThis());
1437     }
1438 }
1439 
TEST(Event,whenCreatingRegularEventsThenExternalSynchronizationIsNotRequired)1440 TEST(Event, whenCreatingRegularEventsThenExternalSynchronizationIsNotRequired) {
1441     Event *event = new Event(nullptr, 0, 0, 0);
1442     EXPECT_FALSE(event->isExternallySynchronized());
1443     event->release();
1444 
1445     UserEvent *userEvent = new UserEvent();
1446     EXPECT_FALSE(userEvent->isExternallySynchronized());
1447     userEvent->release();
1448 
1449     VirtualEvent *virtualEvent = new VirtualEvent();
1450     EXPECT_FALSE(virtualEvent->isExternallySynchronized());
1451     virtualEvent->release();
1452 }
1453 
HWTEST_F(EventTest,givenEventWithNotReadyTaskLevelWhenUnblockedThenGetTaskLevelFromCsrIfGreaterThanParent)1454 HWTEST_F(EventTest, givenEventWithNotReadyTaskLevelWhenUnblockedThenGetTaskLevelFromCsrIfGreaterThanParent) {
1455     uint32_t initialTaskLevel = 10;
1456     Event parentEventWithGreaterTaskLevel(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, initialTaskLevel + 5, 0);
1457     Event parentEventWithLowerTaskLevel(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, initialTaskLevel - 5, 0);
1458 
1459     Event childEvent0(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, CompletionStamp::notReady);
1460     Event childEvent1(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, CompletionStamp::notReady, CompletionStamp::notReady);
1461 
1462     auto &csr = reinterpret_cast<UltCommandStreamReceiver<FamilyType> &>(pCmdQ->getGpgpuCommandStreamReceiver());
1463     csr.taskLevel = initialTaskLevel;
1464 
1465     parentEventWithGreaterTaskLevel.addChild(childEvent0);
1466     parentEventWithLowerTaskLevel.addChild(childEvent1);
1467 
1468     parentEventWithGreaterTaskLevel.setStatus(CL_COMPLETE);
1469     parentEventWithLowerTaskLevel.setStatus(CL_COMPLETE);
1470 
1471     EXPECT_EQ(parentEventWithGreaterTaskLevel.getTaskLevel() + 1, childEvent0.getTaskLevel());
1472     EXPECT_EQ(csr.taskLevel, childEvent1.getTaskLevel());
1473 }
1474 
TEST_F(EventTest,GivenCompletedEventWhenAddingChildThenNumEventsBlockingThisIsZero)1475 TEST_F(EventTest, GivenCompletedEventWhenAddingChildThenNumEventsBlockingThisIsZero) {
1476     VirtualEvent virtualEvent(pCmdQ, &mockContext);
1477     {
1478         Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
1479         event.setStatus(CL_COMPLETE);
1480         event.addChild(virtualEvent);
1481         EXPECT_EQ(0U, virtualEvent.peekNumEventsBlockingThis());
1482     }
1483 }
1484 
1485 template <typename GfxFamily>
1486 struct TestEventCsr : public UltCommandStreamReceiver<GfxFamily> {
TestEventCsrTestEventCsr1487     TestEventCsr(const ExecutionEnvironment &executionEnvironment, const DeviceBitfield deviceBitfield)
1488         : UltCommandStreamReceiver<GfxFamily>(const_cast<ExecutionEnvironment &>(executionEnvironment), 0, deviceBitfield) {}
1489     MOCK_METHOD3(waitForCompletionWithTimeout, bool(bool enableTimeout, int64_t timeoutMs, uint32_t taskCountToWait));
1490 };
1491 
HWTEST_F(EventTest,givenQuickKmdSleepRequestWhenWaitIsCalledThenPassRequestToWaitingFunction)1492 HWTEST_F(EventTest, givenQuickKmdSleepRequestWhenWaitIsCalledThenPassRequestToWaitingFunction) {
1493     HardwareInfo localHwInfo = pDevice->getHardwareInfo();
1494     localHwInfo.capabilityTable.kmdNotifyProperties.enableKmdNotify = true;
1495     localHwInfo.capabilityTable.kmdNotifyProperties.enableQuickKmdSleep = true;
1496     localHwInfo.capabilityTable.kmdNotifyProperties.delayQuickKmdSleepMicroseconds = 1;
1497     localHwInfo.capabilityTable.kmdNotifyProperties.delayKmdNotifyMicroseconds = 2;
1498 
1499     pDevice->executionEnvironment->rootDeviceEnvironments[pDevice->getRootDeviceIndex()]->setHwInfo(&localHwInfo);
1500 
1501     auto csr = new ::testing::NiceMock<TestEventCsr<FamilyType>>(*pDevice->executionEnvironment, pDevice->getDeviceBitfield());
1502     pDevice->resetCommandStreamReceiver(csr);
1503 
1504     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
1505     event.updateCompletionStamp(1u, 0, 1u, 1u);
1506 
1507     EXPECT_CALL(*csr, waitForCompletionWithTimeout(::testing::_,
1508                                                    localHwInfo.capabilityTable.kmdNotifyProperties.delayQuickKmdSleepMicroseconds, ::testing::_))
1509         .Times(1)
1510         .WillOnce(::testing::Return(true));
1511 
1512     event.wait(true, true);
1513 }
1514 
HWTEST_F(EventTest,givenNonQuickKmdSleepRequestWhenWaitIsCalledThenPassRequestToWaitingFunction)1515 HWTEST_F(EventTest, givenNonQuickKmdSleepRequestWhenWaitIsCalledThenPassRequestToWaitingFunction) {
1516     HardwareInfo localHwInfo = pDevice->getHardwareInfo();
1517     localHwInfo.capabilityTable.kmdNotifyProperties.enableKmdNotify = true;
1518     localHwInfo.capabilityTable.kmdNotifyProperties.enableQuickKmdSleep = true;
1519     localHwInfo.capabilityTable.kmdNotifyProperties.enableQuickKmdSleepForSporadicWaits = false;
1520     localHwInfo.capabilityTable.kmdNotifyProperties.delayQuickKmdSleepMicroseconds = 1;
1521     localHwInfo.capabilityTable.kmdNotifyProperties.delayKmdNotifyMicroseconds = 2;
1522 
1523     pDevice->executionEnvironment->rootDeviceEnvironments[pDevice->getRootDeviceIndex()]->setHwInfo(&localHwInfo);
1524 
1525     auto csr = new ::testing::NiceMock<TestEventCsr<FamilyType>>(*pDevice->executionEnvironment, pDevice->getDeviceBitfield());
1526     pDevice->resetCommandStreamReceiver(csr);
1527 
1528     Event event(pCmdQ, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
1529     event.updateCompletionStamp(1u, 0, 1u, 1u);
1530 
1531     EXPECT_CALL(*csr, waitForCompletionWithTimeout(::testing::_,
1532                                                    localHwInfo.capabilityTable.kmdNotifyProperties.delayKmdNotifyMicroseconds, ::testing::_))
1533         .Times(1)
1534         .WillOnce(::testing::Return(true));
1535 
1536     event.wait(true, false);
1537 }
1538 
HWTEST_F(InternalsEventTest,givenCommandWhenSubmitCalledThenUpdateFlushStamp)1539 HWTEST_F(InternalsEventTest, givenCommandWhenSubmitCalledThenUpdateFlushStamp) {
1540     auto pCmdQ = std::unique_ptr<MockCommandQueue>(new MockCommandQueue(mockContext, pClDevice, 0, false));
1541     MockEvent<Event> *event = new MockEvent<Event>(pCmdQ.get(), CL_COMMAND_MARKER, 0, 0);
1542     auto &csr = pDevice->getUltCommandStreamReceiver<FamilyType>();
1543     csr.flushStamp->setStamp(5);
1544 
1545     FlushStamp expectedFlushStamp = 0;
1546     EXPECT_EQ(expectedFlushStamp, event->flushStamp->peekStamp());
1547     event->setCommand(std::unique_ptr<Command>(new CommandWithoutKernel(*pCmdQ)));
1548     event->submitCommand(false);
1549     EXPECT_EQ(csr.flushStamp->peekStamp(), event->flushStamp->peekStamp());
1550     delete event;
1551 }
1552 
HWTEST_F(InternalsEventTest,givenAbortedCommandWhenSubmitCalledThenDontUpdateFlushStamp)1553 HWTEST_F(InternalsEventTest, givenAbortedCommandWhenSubmitCalledThenDontUpdateFlushStamp) {
1554     auto pCmdQ = std::unique_ptr<MockCommandQueue>(new MockCommandQueue(mockContext, pClDevice, 0, false));
1555     MockEvent<Event> *event = new MockEvent<Event>(pCmdQ.get(), CL_COMMAND_MARKER, 0, 0);
1556     auto &csr = pDevice->getUltCommandStreamReceiver<FamilyType>();
1557     csr.flushStamp->setStamp(5);
1558 
1559     MockKernelWithInternals mockKernelWithInternals(*pClDevice);
1560     auto pKernel = mockKernelWithInternals.mockKernel;
1561 
1562     auto cmdStream = new LinearStream(pDevice->getMemoryManager()->allocateGraphicsMemoryWithProperties({pDevice->getRootDeviceIndex(), 4096, GraphicsAllocation::AllocationType::COMMAND_BUFFER, pDevice->getDeviceBitfield()}));
1563     IndirectHeap *dsh = nullptr, *ioh = nullptr, *ssh = nullptr;
1564     pCmdQ->allocateHeapMemory(IndirectHeap::DYNAMIC_STATE, 4096u, dsh);
1565     pCmdQ->allocateHeapMemory(IndirectHeap::INDIRECT_OBJECT, 4096u, ioh);
1566     pCmdQ->allocateHeapMemory(IndirectHeap::SURFACE_STATE, 4096u, ssh);
1567     auto blockedCommandsData = std::make_unique<KernelOperation>(cmdStream, *pCmdQ->getGpgpuCommandStreamReceiver().getInternalAllocationStorage());
1568     blockedCommandsData->setHeaps(dsh, ioh, ssh);
1569     PreemptionMode preemptionMode = pDevice->getPreemptionMode();
1570     std::vector<Surface *> v;
1571     auto cmd = new CommandComputeKernel(*pCmdQ, blockedCommandsData, v, false, false, false, nullptr, preemptionMode, pKernel, 1);
1572     event->setCommand(std::unique_ptr<Command>(cmd));
1573 
1574     FlushStamp expectedFlushStamp = 0;
1575 
1576     EXPECT_EQ(expectedFlushStamp, event->flushStamp->peekStamp());
1577     event->submitCommand(true);
1578 
1579     EXPECT_EQ(expectedFlushStamp, event->flushStamp->peekStamp());
1580     delete event;
1581 }
1582 
TEST(EventLockerTests,givenEventWhenEventLockerIsUsedThenOwnershipIsAutomaticallyReleased)1583 TEST(EventLockerTests, givenEventWhenEventLockerIsUsedThenOwnershipIsAutomaticallyReleased) {
1584     Event ev(nullptr, CL_COMMAND_COPY_BUFFER, 3, 0);
1585     {
1586         TakeOwnershipWrapper<Event> locker(ev);
1587         EXPECT_TRUE(ev.hasOwnership());
1588     }
1589     EXPECT_FALSE(ev.hasOwnership());
1590 }
1591 
TEST(EventLockerTests,givenEventWhenEventLockerIsUsedAndUnlockedThenOwnershipIsReleased)1592 TEST(EventLockerTests, givenEventWhenEventLockerIsUsedAndUnlockedThenOwnershipIsReleased) {
1593     Event ev(nullptr, CL_COMMAND_COPY_BUFFER, 3, 0);
1594     {
1595         TakeOwnershipWrapper<Event> locker(ev);
1596         locker.unlock();
1597         EXPECT_FALSE(ev.hasOwnership());
1598     }
1599     EXPECT_FALSE(ev.hasOwnership());
1600 }
1601 
TEST(EventLockerTests,givenEventWhenEventLockerIsUsedAndlockedThenOwnershipIsAcquiredAgain)1602 TEST(EventLockerTests, givenEventWhenEventLockerIsUsedAndlockedThenOwnershipIsAcquiredAgain) {
1603     Event ev(nullptr, CL_COMMAND_COPY_BUFFER, 3, 0);
1604     {
1605         TakeOwnershipWrapper<Event> locker(ev);
1606         locker.unlock();
1607         locker.lock();
1608         EXPECT_TRUE(ev.hasOwnership());
1609     }
1610     EXPECT_FALSE(ev.hasOwnership());
1611 }
TEST(EventLockerTests,givenEventWhenEventLockerIsLockedTwiceThenOwnershipIsReleaseAfterLeavingTheScope)1612 TEST(EventLockerTests, givenEventWhenEventLockerIsLockedTwiceThenOwnershipIsReleaseAfterLeavingTheScope) {
1613     Event ev(nullptr, CL_COMMAND_COPY_BUFFER, 3, 0);
1614     {
1615         TakeOwnershipWrapper<Event> locker(ev);
1616         locker.lock();
1617         EXPECT_TRUE(ev.hasOwnership());
1618     }
1619     EXPECT_FALSE(ev.hasOwnership());
1620 }
1621 
TEST(EventsDebug,givenEventWhenTrackingOfParentsIsOnThenTrackParents)1622 TEST(EventsDebug, givenEventWhenTrackingOfParentsIsOnThenTrackParents) {
1623     DebugManagerStateRestore stateRestore;
1624     DebugManager.flags.TrackParentEvents.set(true);
1625     Event event(nullptr, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
1626     Event event2(nullptr, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
1627 
1628     auto &parentEvents = event.getParentEvents();
1629     auto &parentEvents2 = event2.getParentEvents();
1630 
1631     EXPECT_EQ(0u, parentEvents.size());
1632     EXPECT_EQ(0u, parentEvents2.size());
1633 
1634     event.addChild(event2);
1635     EXPECT_EQ(0u, parentEvents.size());
1636     EXPECT_EQ(1u, parentEvents2.size());
1637     EXPECT_EQ(&event, parentEvents2.at(0));
1638     event.setStatus(CL_COMPLETE);
1639 }
1640 
TEST(EventsDebug,givenEventWhenTrackingOfParentsIsOffThenDoNotTrackParents)1641 TEST(EventsDebug, givenEventWhenTrackingOfParentsIsOffThenDoNotTrackParents) {
1642     DebugManagerStateRestore stateRestore;
1643     DebugManager.flags.TrackParentEvents.set(false);
1644     Event event(nullptr, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
1645     Event event2(nullptr, CL_COMMAND_NDRANGE_KERNEL, 0, 0);
1646 
1647     auto &parentEvents = event.getParentEvents();
1648     auto &parentEvents2 = event2.getParentEvents();
1649 
1650     EXPECT_EQ(0u, parentEvents.size());
1651     EXPECT_EQ(0u, parentEvents2.size());
1652 
1653     event.addChild(event2);
1654     EXPECT_EQ(0u, parentEvents.size());
1655     EXPECT_EQ(0u, parentEvents2.size());
1656     event.setStatus(CL_COMPLETE);
1657 }
1658 
TEST(CommandQueue,givenTimestampPacketWritesDisabledAndQueueHasTimestampPacketContainerThenCreateTheContainerForEvent)1659 TEST(CommandQueue, givenTimestampPacketWritesDisabledAndQueueHasTimestampPacketContainerThenCreateTheContainerForEvent) {
1660     DebugManagerStateRestore stateRestore;
1661     DebugManager.flags.EnableTimestampPacket.set(0);
1662 
1663     MockContext context{};
1664     MockCommandQueue queue{&context, context.getDevice(0), nullptr, false};
1665     ASSERT_FALSE(queue.getGpgpuCommandStreamReceiver().peekTimestampPacketWriteEnabled());
1666     ASSERT_EQ(nullptr, queue.timestampPacketContainer.get());
1667     queue.timestampPacketContainer = std::make_unique<TimestampPacketContainer>();
1668 
1669     MockEvent<Event> event{&queue, CL_COMMAND_MARKER, 0, 0};
1670     EXPECT_NE(nullptr, event.timestampPacketContainer);
1671 }
1672