1 /*
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #pragma once
9 
10 #include "shared/source/helpers/aligned_memory.h"
11 #include "shared/source/helpers/ptr_math.h"
12 #include "shared/test/unit_test/utilities/base_object_utils.h"
13 
14 #include "opencl/source/command_queue/command_queue.h"
15 #include "opencl/source/event/user_event.h"
16 #include "opencl/test/unit_test/command_queue/command_queue_fixture.h"
17 #include "opencl/test/unit_test/command_stream/command_stream_fixture.h"
18 #include "opencl/test/unit_test/fixtures/cl_device_fixture.h"
19 #include "opencl/test/unit_test/fixtures/hello_world_fixture.h"
20 #include "opencl/test/unit_test/mocks/mock_buffer.h"
21 #include "opencl/test/unit_test/mocks/mock_context.h"
22 
23 #include "gtest/gtest.h"
24 
25 using namespace NEO;
26 
27 struct EventTest
28     : public ClDeviceFixture,
29       public CommandQueueFixture,
30       public CommandStreamFixture,
31       public ::testing::Test {
32 
33     using CommandQueueFixture::SetUp;
34 
SetUpEventTest35     void SetUp() override {
36         ClDeviceFixture::SetUp();
37         CommandQueueFixture::SetUp(&mockContext, pClDevice, 0);
38         CommandStreamFixture::SetUp(pCmdQ);
39     }
40 
TearDownEventTest41     void TearDown() override {
42         CommandStreamFixture::TearDown();
43         CommandQueueFixture::TearDown();
44         ClDeviceFixture::TearDown();
45     }
46     MockContext mockContext;
47 };
48 
49 struct InternalsEventTest
50     : public ClDeviceFixture,
51       public ::testing::Test {
52 
InternalsEventTestInternalsEventTest53     InternalsEventTest() {
54     }
55 
SetUpInternalsEventTest56     void SetUp() override {
57         ClDeviceFixture::SetUp();
58         mockContext = new MockContext(pClDevice);
59     }
60 
TearDownInternalsEventTest61     void TearDown() override {
62         delete mockContext;
63         ClDeviceFixture::TearDown();
64     }
65 
66     MockContext *mockContext = nullptr;
67 };
68 
69 struct MyUserEvent : public VirtualEvent {
waitMyUserEvent70     bool wait(bool blocking, bool quickKmdSleep) override {
71         return VirtualEvent::wait(blocking, quickKmdSleep);
72     };
getTaskLevelMyUserEvent73     uint32_t getTaskLevel() override {
74         return VirtualEvent::getTaskLevel();
75     };
76 };
77 
78 struct MyEvent : public Event {
MyEventMyEvent79     MyEvent(CommandQueue *cmdQueue, cl_command_type cmdType, uint32_t taskLevel, uint32_t taskCount)
80         : Event(cmdQueue, cmdType, taskLevel, taskCount) {
81     }
getQueueTimeStampMyEvent82     TimeStampData getQueueTimeStamp() {
83         return this->queueTimeStamp;
84     };
85 
getSubmitTimeStampMyEvent86     TimeStampData getSubmitTimeStamp() {
87         return this->submitTimeStamp;
88     };
89 
getStartTimeStampMyEvent90     uint64_t getStartTimeStamp() {
91         return this->startTimeStamp;
92     };
93 
getEndTimeStampMyEvent94     uint64_t getEndTimeStamp() {
95         return this->endTimeStamp;
96     };
97 
getCompleteTimeStampMyEvent98     uint64_t getCompleteTimeStamp() {
99         return this->completeTimeStamp;
100     }
101 
getGlobalStartTimestampMyEvent102     uint64_t getGlobalStartTimestamp() const {
103         return this->globalStartTimestamp;
104     }
105 
getDataCalcStatusMyEvent106     bool getDataCalcStatus() const {
107         return this->dataCalculated;
108     }
109 
calculateProfilingDataInternalMyEvent110     void calculateProfilingDataInternal(uint64_t contextStartTS, uint64_t contextEndTS, uint64_t *contextCompleteTS, uint64_t globalStartTS) override {
111         if (DebugManager.flags.ReturnRawGpuTimestamps.get()) {
112             globalStartTimestamp = globalStartTS;
113         }
114         Event::calculateProfilingDataInternal(contextStartTS, contextEndTS, contextCompleteTS, globalStartTS);
115     }
116 
117     uint64_t globalStartTimestamp;
118 };
119 
120 class MockEventTests : public HelloWorldTest<HelloWorldFixtureFactory> {
121   public:
TearDown()122     void TearDown() override {
123         if (uEvent) {
124             uEvent->setStatus(-1);
125             uEvent.reset();
126         }
127         HelloWorldFixture::TearDown();
128     }
129 
130   protected:
131     ReleaseableObjectPtr<UserEvent> uEvent = nullptr;
132 };
133