|
5 | 5 | * |
6 | 6 | */ |
7 | 7 |
|
8 | | -#include "runtime/memory_manager/os_agnostic_memory_manager.h" |
9 | | -#include "unit_tests/mocks/mock_memory_manager.h" |
10 | | - |
11 | | -#include "test.h" |
12 | | -#include "gtest/gtest.h" |
13 | | - |
14 | | -using namespace OCLRT; |
15 | | -class MemoryManagerGetAlloctionDataTest : public testing::TestWithParam<GraphicsAllocation::AllocationType> { |
16 | | - public: |
17 | | - void SetUp() override {} |
18 | | - void TearDown() override {} |
19 | | -}; |
20 | | - |
21 | | -TEST(MemoryManagerGetAlloctionDataTest, givenHostMemoryAllocationTypeAndAllocateMemoryFlagAndNullptrWhenAllocationDataIsQueriedThenCorrectFlagsAndSizeAreSet) { |
22 | | - AllocationData allocData; |
23 | | - AllocationFlags flags(true); |
24 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); |
25 | | - |
26 | | - EXPECT_TRUE(allocData.flags.mustBeZeroCopy); |
27 | | - EXPECT_TRUE(allocData.flags.useSystemMemory); |
28 | | - EXPECT_EQ(10u, allocData.size); |
29 | | - EXPECT_EQ(nullptr, allocData.hostPtr); |
30 | | -} |
31 | | - |
32 | | -TEST(MemoryManagerGetAlloctionDataTest, givenNonHostMemoryAllocatoinTypeWhenAllocationDataIsQueriedThenMustBeZeroCopyAndUseSystemMemoryFlagsAreNotSet) { |
33 | | - AllocationData allocData; |
34 | | - AllocationFlags flags(true); |
35 | | - |
36 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER); |
37 | | - |
38 | | - EXPECT_FALSE(allocData.flags.mustBeZeroCopy); |
39 | | - EXPECT_FALSE(allocData.flags.useSystemMemory); |
40 | | - EXPECT_EQ(10u, allocData.size); |
41 | | - EXPECT_EQ(nullptr, allocData.hostPtr); |
42 | | -} |
43 | | - |
44 | | -TEST(MemoryManagerGetAlloctionDataTest, givenAllocateMemoryFlagTrueWhenHostPtrIsNotNullThenAllocationDataHasHostPtrNulled) { |
45 | | - AllocationData allocData; |
46 | | - char memory = 0; |
47 | | - AllocationFlags flags(true); |
48 | | - |
49 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, &memory, sizeof(memory), GraphicsAllocation::AllocationType::BUFFER); |
50 | | - |
51 | | - EXPECT_EQ(sizeof(memory), allocData.size); |
52 | | - EXPECT_EQ(nullptr, allocData.hostPtr); |
53 | | -} |
54 | | - |
55 | | -TEST(MemoryManagerGetAlloctionDataTest, givenBufferTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { |
56 | | - AllocationData allocData; |
57 | | - AllocationFlags flags(true); |
58 | | - |
59 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER); |
60 | | - |
61 | | - EXPECT_TRUE(allocData.flags.forcePin); |
62 | | -} |
63 | | - |
64 | | -TEST(MemoryManagerGetAlloctionDataTest, givenBufferHostMemoryTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { |
65 | | - AllocationData allocData; |
66 | | - AllocationFlags flags(true); |
67 | | - |
68 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); |
69 | | - |
70 | | - EXPECT_TRUE(allocData.flags.forcePin); |
71 | | -} |
72 | | - |
73 | | -TEST(MemoryManagerGetAlloctionDataTest, givenBufferCompressedTypeWhenAllocationDataIsQueriedThenForcePinFlagIsSet) { |
74 | | - AllocationData allocData; |
75 | | - AllocationFlags flags(true); |
76 | | - |
77 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); |
78 | | - |
79 | | - EXPECT_TRUE(allocData.flags.forcePin); |
80 | | -} |
81 | | - |
82 | | -TEST(MemoryManagerGetAlloctionDataTest, givenDefaultAllocationFlagsWhenAllocationDataIsQueriedThenAllocateMemoryIsFalse) { |
83 | | - AllocationData allocData; |
84 | | - AllocationFlags flags; |
85 | | - char memory; |
86 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, &memory, sizeof(memory), GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); |
87 | | - |
88 | | - EXPECT_FALSE(allocData.flags.allocateMemory); |
89 | | -} |
90 | | - |
91 | | -TEST(MemoryManagerGetAlloctionDataTest, givenSpecificDeviceWhenAllocationDataIsQueriedThenDeviceIsPropagatedToAllocationData) { |
92 | | - AllocationData allocData; |
93 | | - AllocationFlags flags(true); |
94 | | - MockMemoryManager::getAllocationData(allocData, flags, 3u, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); |
95 | | - |
96 | | - EXPECT_EQ(3u, allocData.devicesBitfield); |
97 | | -} |
98 | | - |
99 | | -typedef MemoryManagerGetAlloctionDataTest MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest; |
100 | | - |
101 | | -TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, givenAllocationTypesWith32BitAnd64kbPagesAllowedWhenAllocationDataIsQueriedThenProperFlagsAreSet) { |
102 | | - AllocationData allocData; |
103 | | - AllocationFlags flags(true); |
104 | | - |
105 | | - auto allocType = GetParam(); |
106 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, allocType); |
107 | | - |
108 | | - EXPECT_TRUE(allocData.flags.allow32Bit); |
109 | | - EXPECT_TRUE(allocData.flags.allow64kbPages); |
110 | | - EXPECT_EQ(allocType, allocData.type); |
111 | | -} |
112 | | - |
113 | | -TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, given64kbAllowedAllocationTypeWhenAllocatingThenPreferRenderCompressionOnlyForSpecificTypes) { |
114 | | - auto allocType = GetParam(); |
115 | | - AllocationData allocData; |
116 | | - AllocationFlags flags(true); |
117 | | - |
118 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, allocType); |
119 | | - bool bufferCompressedType = (allocType == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); |
120 | | - EXPECT_TRUE(allocData.flags.allow64kbPages); |
121 | | - |
122 | | - MockMemoryManager mockMemoryManager(true); |
123 | | - auto allocation = mockMemoryManager.allocateGraphicsMemory(allocData); |
124 | | - |
125 | | - EXPECT_TRUE(mockMemoryManager.allocation64kbPageCreated); |
126 | | - EXPECT_EQ(mockMemoryManager.preferRenderCompressedFlagPassed, bufferCompressedType); |
127 | | - |
128 | | - mockMemoryManager.freeGraphicsMemory(allocation); |
129 | | -} |
130 | | - |
131 | | -typedef MemoryManagerGetAlloctionDataTest MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest; |
132 | | - |
133 | | -TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest, givenAllocationTypesWith32BitAnd64kbPagesDisallowedWhenAllocationDataIsQueriedThenFlagsAreNotSet) { |
134 | | - AllocationData allocData; |
135 | | - AllocationFlags flags(true); |
136 | | - |
137 | | - auto allocType = GetParam(); |
138 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, allocType); |
139 | | - |
140 | | - EXPECT_FALSE(allocData.flags.allow32Bit); |
141 | | - EXPECT_FALSE(allocData.flags.allow64kbPages); |
142 | | - EXPECT_EQ(allocType, allocData.type); |
143 | | -} |
144 | | - |
145 | | -static const GraphicsAllocation::AllocationType allocationTypesWith32BitAnd64KbPagesAllowed[] = {GraphicsAllocation::AllocationType::BUFFER, |
146 | | - GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, |
147 | | - GraphicsAllocation::AllocationType::BUFFER_COMPRESSED, |
148 | | - GraphicsAllocation::AllocationType::PIPE, |
149 | | - GraphicsAllocation::AllocationType::SCRATCH_SURFACE, |
150 | | - GraphicsAllocation::AllocationType::PRIVATE_SURFACE, |
151 | | - GraphicsAllocation::AllocationType::PRINTF_SURFACE, |
152 | | - GraphicsAllocation::AllocationType::CONSTANT_SURFACE, |
153 | | - GraphicsAllocation::AllocationType::GLOBAL_SURFACE}; |
154 | | - |
155 | | -INSTANTIATE_TEST_CASE_P(Allow32BitAnd64kbPagesTypes, |
156 | | - MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, |
157 | | - ::testing::ValuesIn(allocationTypesWith32BitAnd64KbPagesAllowed)); |
158 | | - |
159 | | -static const GraphicsAllocation::AllocationType allocationTypesWith32BitAnd64KbPagesNotAllowed[] = {GraphicsAllocation::AllocationType::COMMAND_BUFFER, |
160 | | - GraphicsAllocation::AllocationType::DYNAMIC_STATE_HEAP, |
161 | | - GraphicsAllocation::AllocationType::EVENT_TAG_BUFFER, |
162 | | - GraphicsAllocation::AllocationType::IMAGE, |
163 | | - GraphicsAllocation::AllocationType::INSTRUCTION_HEAP, |
164 | | - GraphicsAllocation::AllocationType::SHARED_RESOURCE}; |
165 | | - |
166 | | -INSTANTIATE_TEST_CASE_P(Disallow32BitAnd64kbPagesTypes, |
167 | | - MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest, |
168 | | - ::testing::ValuesIn(allocationTypesWith32BitAnd64KbPagesNotAllowed)); |
169 | | - |
170 | | -TEST(MemoryManagerTest, givenForced32BitSetWhenGraphicsMemoryFor32BitAllowedTypeIsAllocatedThen32BitAllocationIsReturned) { |
171 | | - OsAgnosticMemoryManager memoryManager; |
172 | | - memoryManager.setForce32BitAllocations(true); |
173 | | - |
174 | | - AllocationData allocData; |
175 | | - AllocationFlags flags(true); |
176 | | - |
177 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER); |
178 | | - |
179 | | - auto allocation = memoryManager.allocateGraphicsMemory(allocData); |
180 | | - ASSERT_NE(nullptr, allocation); |
181 | | - if (is64bit) { |
182 | | - EXPECT_TRUE(allocation->is32BitAllocation); |
183 | | - EXPECT_EQ(MemoryPool::System4KBPagesWith32BitGpuAddressing, allocation->getMemoryPool()); |
184 | | - } else { |
185 | | - EXPECT_FALSE(allocation->is32BitAllocation); |
186 | | - EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); |
187 | | - } |
188 | | - |
189 | | - memoryManager.freeGraphicsMemory(allocation); |
190 | | -} |
191 | | - |
192 | | -TEST(MemoryManagerTest, givenForced32BitEnabledWhenGraphicsMemoryWihtoutAllow32BitFlagIsAllocatedThenNon32BitAllocationIsReturned) { |
193 | | - OsAgnosticMemoryManager memoryManager; |
194 | | - memoryManager.setForce32BitAllocations(true); |
195 | | - |
196 | | - AllocationData allocData; |
197 | | - AllocationFlags flags(true); |
198 | | - |
199 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER); |
200 | | - allocData.flags.allow32Bit = false; |
201 | | - |
202 | | - auto allocation = memoryManager.allocateGraphicsMemory(allocData); |
203 | | - ASSERT_NE(nullptr, allocation); |
204 | | - EXPECT_FALSE(allocation->is32BitAllocation); |
205 | | - |
206 | | - memoryManager.freeGraphicsMemory(allocation); |
207 | | -} |
208 | | - |
209 | | -TEST(MemoryManagerTest, givenForced32BitDisabledWhenGraphicsMemoryWith32BitFlagFor32BitAllowedTypeIsAllocatedThenNon32BitAllocationIsReturned) { |
210 | | - OsAgnosticMemoryManager memoryManager; |
211 | | - memoryManager.setForce32BitAllocations(false); |
212 | | - |
213 | | - AllocationData allocData; |
214 | | - AllocationFlags flags(true); |
215 | | - |
216 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER); |
217 | | - |
218 | | - auto allocation = memoryManager.allocateGraphicsMemory(allocData); |
219 | | - ASSERT_NE(nullptr, allocation); |
220 | | - EXPECT_FALSE(allocation->is32BitAllocation); |
221 | | - |
222 | | - memoryManager.freeGraphicsMemory(allocation); |
223 | | -} |
224 | | - |
225 | | -TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThen64kbAllocationIsReturned) { |
226 | | - OsAgnosticMemoryManager memoryManager(true, false); |
227 | | - AllocationData allocData; |
228 | | - AllocationFlags flags(true); |
229 | | - |
230 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); |
231 | | - |
232 | | - auto allocation = memoryManager.allocateGraphicsMemory(allocData); |
233 | | - ASSERT_NE(nullptr, allocation); |
234 | | - EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(allocation->getUnderlyingBuffer()) & MemoryConstants::page64kMask); |
235 | | - EXPECT_EQ(0u, allocation->getGpuAddress() & MemoryConstants::page64kMask); |
236 | | - EXPECT_EQ(0u, allocation->getUnderlyingBufferSize() & MemoryConstants::page64kMask); |
237 | | - EXPECT_EQ(MemoryPool::System64KBPages, allocation->getMemoryPool()); |
238 | | - |
239 | | - memoryManager.freeGraphicsMemory(allocation); |
240 | | -} |
241 | | - |
242 | | -TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryWithoutAllow64kbPagesFlagsIsAllocatedThenNon64kbAllocationIsReturned) { |
243 | | - MockMemoryManager memoryManager(true); |
244 | | - AllocationData allocData; |
245 | | - AllocationFlags flags(true); |
246 | | - |
247 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER); |
248 | | - allocData.flags.allow64kbPages = false; |
249 | | - |
250 | | - auto allocation = memoryManager.allocateGraphicsMemory(allocData); |
251 | | - ASSERT_NE(nullptr, allocation); |
252 | | - EXPECT_FALSE(memoryManager.allocation64kbPageCreated); |
253 | | - EXPECT_TRUE(memoryManager.allocationCreated); |
254 | | - |
255 | | - memoryManager.freeGraphicsMemory(allocation); |
256 | | -} |
257 | | - |
258 | | -TEST(MemoryManagerTest, givenDisabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThenNon64kbAllocationIsReturned) { |
259 | | - MockMemoryManager memoryManager(false); |
260 | | - AllocationData allocData; |
261 | | - AllocationFlags flags(true); |
262 | | - |
263 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); |
264 | | - |
265 | | - auto allocation = memoryManager.allocateGraphicsMemory(allocData); |
266 | | - ASSERT_NE(nullptr, allocation); |
267 | | - EXPECT_FALSE(memoryManager.allocation64kbPageCreated); |
268 | | - EXPECT_TRUE(memoryManager.allocationCreated); |
269 | | - EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); |
270 | | - |
271 | | - memoryManager.freeGraphicsMemory(allocation); |
272 | | -} |
273 | | - |
274 | | -TEST(MemoryManagerTest, givenForced32BitAndEnabled64kbPagesWhenGraphicsMemoryMustBeHostMemoryAndIsAllocatedWithNullptrForBufferThen32BitAllocationOver64kbIsChosen) { |
275 | | - OsAgnosticMemoryManager memoryManager; |
276 | | - memoryManager.setForce32BitAllocations(true); |
277 | | - |
278 | | - AllocationData allocData; |
279 | | - AllocationFlags flags(true); |
280 | | - |
281 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); |
282 | | - |
283 | | - auto allocation = memoryManager.allocateGraphicsMemory(allocData); |
284 | | - ASSERT_NE(nullptr, allocation); |
285 | | - if (is64bit) { |
286 | | - EXPECT_TRUE(allocation->is32BitAllocation); |
287 | | - } else { |
288 | | - EXPECT_FALSE(allocation->is32BitAllocation); |
289 | | - } |
290 | | - |
291 | | - memoryManager.freeGraphicsMemory(allocation); |
292 | | -} |
293 | | - |
294 | | -TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryIsAllocatedWithHostPtrForBufferThenExistingMemoryIsUsedForAllocation) { |
295 | | - OsAgnosticMemoryManager memoryManager(true, false); |
296 | | - AllocationData allocData; |
297 | | - AllocationFlags flags(false); |
298 | | - |
299 | | - char memory[1]; |
300 | | - MockMemoryManager::getAllocationData(allocData, flags, 0, &memory, 1, GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); |
301 | | - |
302 | | - auto allocation = memoryManager.allocateGraphicsMemory(allocData); |
303 | | - ASSERT_NE(nullptr, allocation); |
304 | | - EXPECT_EQ(1u, allocation->fragmentsStorage.fragmentCount); |
305 | | - EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); |
306 | | - |
307 | | - memoryManager.freeGraphicsMemory(allocation); |
308 | | -} |
309 | | - |
310 | | -TEST(MemoryManagerTest, givenMemoryManagerWhenGraphicsMemoryAllocationInDevicePoolFailsThenFallbackAllocationIsReturned) { |
311 | | - MockMemoryManager memoryManager(false); |
312 | | - |
313 | | - memoryManager.failInDevicePool = true; |
314 | | - |
315 | | - auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool(AllocationFlags(true), 0, nullptr, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER); |
316 | | - ASSERT_NE(nullptr, allocation); |
317 | | - EXPECT_TRUE(memoryManager.allocationCreated); |
318 | | - EXPECT_EQ(MemoryPool::System4KBPages, allocation->getMemoryPool()); |
319 | | - |
320 | | - memoryManager.freeGraphicsMemory(allocation); |
321 | | -} |
322 | | - |
323 | | -TEST(MemoryManagerTest, givenMemoryManagerWhenBufferTypeIsPassedThenAllocateGraphicsMemoryInPreferredPoolCanAllocateInDevicePool) { |
324 | | - MockMemoryManager memoryManager(false); |
325 | | - |
326 | | - auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool(AllocationFlags(true), 0, nullptr, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER); |
327 | | - EXPECT_NE(nullptr, allocation); |
328 | | - memoryManager.freeGraphicsMemory(allocation); |
329 | | -} |
330 | | - |
331 | | -TEST(MemoryManagerTest, givenMemoryManagerWhenBufferTypeIsPassedAndAllocateInDevicePoolFailsWithErrorThenAllocateGraphicsMemoryInPreferredPoolReturnsNullptr) { |
332 | | - MockMemoryManager memoryManager(false); |
333 | | - |
334 | | - memoryManager.failInDevicePoolWithError = true; |
335 | | - |
336 | | - auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool(AllocationFlags(true), 0, nullptr, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER); |
337 | | - ASSERT_EQ(nullptr, allocation); |
338 | | - EXPECT_FALSE(memoryManager.allocationInDevicePoolCreated); |
339 | | - |
340 | | - memoryManager.freeGraphicsMemory(allocation); |
341 | | -} |
| 8 | +#include "unit_tests/memory_manager/memory_manager_allocate_in_preferred_pool_tests.inl" |
0 commit comments