File: linear_stream_tests.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (230 lines) | stat: -rw-r--r-- 10,593 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2018-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/command_stream/linear_stream.h"
#include "shared/source/gmm_helper/gmm_helper.h"
#include "shared/source/helpers/gfx_core_helper.h"
#include "shared/source/indirect_heap/heap_size.h"
#include "shared/source/memory_manager/graphics_allocation.h"
#include "shared/test/common/fixtures/device_fixture.h"
#include "shared/test/common/fixtures/linear_stream_fixture.h"
#include "shared/test/common/mocks/mock_device.h"
#include "shared/test/common/mocks/mock_execution_environment.h"
#include "shared/test/common/mocks/mock_graphics_allocation.h"

using namespace NEO;

TEST(LinearStreamCtorTest, WhenConstructingLinearStreamThenInitialValuesValuesAreSet) {
    LinearStream linearStream;
    EXPECT_EQ(nullptr, linearStream.getCpuBase());
    EXPECT_EQ(0u, linearStream.getMaxAvailableSpace());
}

TEST(LinearStreamCtorTest, whenProvidedAllArgumentsThenExpectSameValuesSet) {
    GraphicsAllocation *gfxAllocation = reinterpret_cast<GraphicsAllocation *>(0x1234);
    void *buffer = reinterpret_cast<void *>(0x2000);
    size_t bufferSize = 0x1000u;
    LinearStream linearStream(gfxAllocation, buffer, bufferSize);
    EXPECT_EQ(buffer, linearStream.getCpuBase());
    EXPECT_EQ(bufferSize, linearStream.getMaxAvailableSpace());
    EXPECT_EQ(gfxAllocation, linearStream.getGraphicsAllocation());
}

TEST(LinearStreamSimpleTest, givenLinearStreamWithoutGraphicsAllocationWhenGettingGpuBaseThenValueSetAsGpuBaseIsReturned) {
    uint32_t pCmdBuffer[1024]{};
    LinearStream linearStream(pCmdBuffer, 1000);

    EXPECT_EQ(0u, linearStream.getGpuBase());

    linearStream.setGpuBase(0x1234000);
    EXPECT_EQ(0x1234000u, linearStream.getGpuBase());
}

TEST(LinearStreamSimpleTest, givenLinearStreamWithGraphicsAllocationWhenGettingGpuBaseThenGpuAddressFromGraphicsAllocationIsReturned) {
    MockGraphicsAllocation gfxAllocation;
    auto executionEnvironment = MockExecutionEnvironment{};
    auto gmmHelper = executionEnvironment.rootDeviceEnvironments[0]->getGmmHelper();
    auto canonizedGpuAddress = gmmHelper->canonize(0x5555000);

    gfxAllocation.setCpuPtrAndGpuAddress(nullptr, canonizedGpuAddress);
    uint32_t pCmdBuffer[1024]{};
    LinearStream linearStream(&gfxAllocation, pCmdBuffer, 1000);

    EXPECT_EQ(canonizedGpuAddress, linearStream.getGpuBase());
}

TEST_F(LinearStreamTest, GivenSizeZeroWhenGettingSpaceUsedThenNonNullPointerIsReturned) {
    EXPECT_NE(nullptr, linearStream.getSpace(0));
}

TEST_F(LinearStreamTest, GivenSizeUint32WhenGettingSpaceUsedThenNonNullPointerIsReturned) {
    EXPECT_NE(nullptr, linearStream.getSpace(sizeof(uint32_t)));
}

TEST_F(LinearStreamTest, GivenNullBufferWhenGettingSpaceThenAssert) {
    linearStream.replaceBuffer(nullptr, 100);
    EXPECT_THROW(linearStream.getSpace(1), std::exception);
}

TEST_F(LinearStreamTest, GivenBadBufferPtrWhenGettingSpaceThenAssert) {
    int64_t ptr = 0;
    linearStream.replaceBuffer(reinterpret_cast<void *>(ptr), 100);
    EXPECT_THROW(linearStream.getSpace(1), std::exception);
}

TEST_F(LinearStreamTest, WhenAllocatingMultipleTimesThenPointersIncrementedCorrectly) {
    size_t allocSize = 1;
    auto ptr1 = linearStream.getSpace(allocSize);
    ASSERT_NE(nullptr, ptr1);

    auto ptr2 = linearStream.getSpace(2);
    ASSERT_NE(nullptr, ptr2);

    EXPECT_EQ(allocSize, (uintptr_t)ptr2 - (uintptr_t)ptr1);
}

TEST_F(LinearStreamTest, WhenGettingSpaceThenPointerIsWriteable) {
    uint32_t cmd = 0xbaddf00d;
    auto pCmd = linearStream.getSpace(sizeof(cmd));
    ASSERT_NE(nullptr, pCmd);
    *(uint32_t *)pCmd = cmd;
}

TEST_F(LinearStreamTest, WhenRequestingMutltipleAllocationsThenDifferentPointersAreReturnedForEachRequest) {
    auto pCmd = linearStream.getSpace(sizeof(uint32_t));
    ASSERT_NE(nullptr, pCmd);
    auto pCmd2 = linearStream.getSpace(sizeof(uint32_t));
    ASSERT_NE(pCmd2, pCmd);
}

TEST_F(LinearStreamTest, GivenNoAllocationsWhenGettingSpaceThenAvailableSpaceIsEqualMaximumSpace) {
    ASSERT_EQ(linearStream.getMaxAvailableSpace(), linearStream.getAvailableSpace());
}

TEST_F(LinearStreamTest, GivenNoAllocationsWhenGettingSpaceThenAvailableSpaceIsGreaterThanZero) {
    EXPECT_NE(0u, linearStream.getAvailableSpace());
}

TEST_F(LinearStreamTest, GivenAllocationWhenGettingSpaceThenAvailableSpaceIsReduced) {
    auto originalAvailable = linearStream.getAvailableSpace();
    linearStream.getSpace(sizeof(uint32_t));

    EXPECT_LT(linearStream.getAvailableSpace(), originalAvailable);
}

TEST_F(LinearStreamTest, GivenOneAllocationsWhenGettingSpaceThenSpaceUsedIsEqualToAllocationSize) {
    size_t sizeToAllocate = 2 * sizeof(uint32_t);
    ASSERT_NE(nullptr, linearStream.getSpace(sizeToAllocate));

    EXPECT_EQ(sizeToAllocate, linearStream.getUsed());
}

TEST_F(LinearStreamTest, givenLinearStreamWhenGetCpuBaseIsCalledThenCpuBaseAddressIsReturned) {
    ASSERT_EQ(pCmdBuffer, linearStream.getCpuBase());
}

TEST_F(LinearStreamTest, givenNotEnoughSpaceWhenGetSpaceIsCalledThenThrowException) {
    linearStream.getSpace(linearStream.getMaxAvailableSpace());
    EXPECT_THROW(linearStream.getSpace(1), std::exception);
}

TEST_F(LinearStreamTest, WhenReplacingBufferThenAvailableSizeIsEqualToBufferSizeAndAllSpaceIsAvailable) {
    char buffer[256];
    linearStream.replaceBuffer(buffer, sizeof(buffer));
    EXPECT_EQ(buffer, linearStream.getCpuBase());
    EXPECT_EQ(sizeof(buffer), linearStream.getAvailableSpace());
    EXPECT_EQ(0u, linearStream.getUsed());
}

TEST_F(LinearStreamTest, givenNewGraphicsAllocationWhenReplaceIsCalledThenLinearStreamContainsNewGraphicsAllocation) {
    auto graphicsAllocation = linearStream.getGraphicsAllocation();
    EXPECT_NE(nullptr, graphicsAllocation);
    auto address = (void *)0x100000;
    MockGraphicsAllocation newGraphicsAllocation(address, 4096);
    EXPECT_NE(&newGraphicsAllocation, graphicsAllocation);
    linearStream.replaceGraphicsAllocation(&newGraphicsAllocation);
    EXPECT_EQ(&newGraphicsAllocation, linearStream.getGraphicsAllocation());
}

class MyLinearStreamMock : public LinearStream {
  public:
    using LinearStream::sizeUsed;
};

TEST_F(LinearStreamTest, givenLinearStreamWithoutCmdContainerWhenOneByteLeftInStreamThenGetSpaceDontThrowAbort) {
    reinterpret_cast<MyLinearStreamMock *>(&linearStream)->sizeUsed = linearStream.getMaxAvailableSpace() - 1;
    EXPECT_NO_THROW(linearStream.getSpace(1));
}
using CommandContainerLinearStreamTest = Test<DeviceFixture>;
TEST_F(CommandContainerLinearStreamTest, givenLinearStreamWithCmdContainerWhenOneByteLeftInStreamThenGetSpaceThrowAbort) {
    CommandContainer cmdContainer;
    cmdContainer.initialize(pDevice, nullptr, HeapSize::defaultHeapSize, true, false);
    auto stream = reinterpret_cast<MyLinearStreamMock *>(cmdContainer.getCommandStream());
    stream->sizeUsed = stream->getMaxAvailableSpace() - 1;
    EXPECT_THROW(stream->getSpace(1), std::exception);
}

TEST_F(CommandContainerLinearStreamTest, givenLinearStreamWithCmdContainerWhenThereIsNoSpaceForCommandAndBBEndThenNewCmdBufferAllocated) {
    CommandContainer cmdContainer;
    cmdContainer.initialize(pDevice, nullptr, HeapSize::defaultHeapSize, true, false);
    auto &gfxCoreHelper = pDevice->getGfxCoreHelper();
    auto stream = reinterpret_cast<MyLinearStreamMock *>(cmdContainer.getCommandStream());
    size_t dummyCommandSize = 2;
    stream->sizeUsed = stream->getMaxAvailableSpace() - gfxCoreHelper.getBatchBufferEndSize() - (dummyCommandSize - 1);
    EXPECT_EQ(cmdContainer.getCmdBufferAllocations().size(), 1u);
    stream->getSpace(dummyCommandSize);
    EXPECT_EQ(cmdContainer.getCmdBufferAllocations().size(), 2u);
}

TEST_F(CommandContainerLinearStreamTest, givenLinearStreamWithCmdContainerWhenThereIsNoSpaceForCommandAndBBEndThenLinearStreamHasNewAllocation) {
    CommandContainer cmdContainer;
    cmdContainer.initialize(pDevice, nullptr, HeapSize::defaultHeapSize, true, false);
    auto &gfxCoreHelper = pDevice->getGfxCoreHelper();
    auto stream = reinterpret_cast<MyLinearStreamMock *>(cmdContainer.getCommandStream());
    size_t dummyCommandSize = 2;
    stream->sizeUsed = stream->getMaxAvailableSpace() - gfxCoreHelper.getBatchBufferEndSize() - (dummyCommandSize - 1);
    auto oldBuffer = stream->getCpuBase();
    stream->getSpace(dummyCommandSize);
    auto newBuffer = stream->getCpuBase();
    EXPECT_NE(newBuffer, oldBuffer);
}

TEST_F(CommandContainerLinearStreamTest, givenLinearStreamWithCmdContainerWhenThereIsNoSpaceForCommandAndBBEndThenGetSpaceReturnPtrFromNewAllocation) {
    CommandContainer cmdContainer;
    cmdContainer.initialize(pDevice, nullptr, HeapSize::defaultHeapSize, true, false);
    auto &gfxCoreHelper = pDevice->getGfxCoreHelper();
    auto stream = reinterpret_cast<MyLinearStreamMock *>(cmdContainer.getCommandStream());
    size_t dummyCommandSize = 2;
    stream->sizeUsed = stream->getMaxAvailableSpace() - gfxCoreHelper.getBatchBufferEndSize() - (dummyCommandSize - 1);
    auto ptr = stream->getSpace(dummyCommandSize);
    auto buffer = stream->getCpuBase();
    EXPECT_EQ(buffer, ptr);
}

TEST_F(CommandContainerLinearStreamTest, givenLinearStreamWithCmdContainerWhenThereIsSpaceForCommandAndBBEndThenNewCmdBufferIsNotAllocated) {
    CommandContainer cmdContainer;
    cmdContainer.initialize(pDevice, nullptr, HeapSize::defaultHeapSize, true, false);
    auto &gfxCoreHelper = pDevice->getGfxCoreHelper();
    auto stream = reinterpret_cast<MyLinearStreamMock *>(cmdContainer.getCommandStream());
    size_t dummyCommandSize = 2;
    stream->sizeUsed = stream->getMaxAvailableSpace() - gfxCoreHelper.getBatchBufferEndSize() - (dummyCommandSize);
    EXPECT_EQ(cmdContainer.getCmdBufferAllocations().size(), 1u);
    stream->getSpace(dummyCommandSize);
    EXPECT_EQ(cmdContainer.getCmdBufferAllocations().size(), 1u);
}

TEST_F(CommandContainerLinearStreamTest, givenLinearStreamWithCmdContainerWhenThereIsNoSpaceForCommandAndBBEndThenBBEndAddedAtEndOfStream) {
    CommandContainer cmdContainer;
    cmdContainer.initialize(pDevice, nullptr, HeapSize::defaultHeapSize, true, false);
    auto &gfxCoreHelper = pDevice->getGfxCoreHelper();
    auto stream = reinterpret_cast<MyLinearStreamMock *>(cmdContainer.getCommandStream());
    size_t dummyCommandSize = 2;
    stream->sizeUsed = stream->getMaxAvailableSpace() - gfxCoreHelper.getBatchBufferEndSize() - (dummyCommandSize - 1);
    auto ptr = stream->getSpace(0u);
    stream->getSpace(dummyCommandSize);
    EXPECT_EQ(memcmp(ptr, gfxCoreHelper.getBatchBufferEndReference(), gfxCoreHelper.getBatchBufferEndSize()), 0);
}