|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Intel Corporation |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included |
| 12 | + * in all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "unit_tests/fixtures/device_fixture.h" |
| 24 | +#include "unit_tests/mocks/mock_kernel.h" |
| 25 | +#include "test.h" |
| 26 | + |
| 27 | +using namespace OCLRT; |
| 28 | + |
| 29 | +typedef Test<DeviceFixture> KernelSubstituteTest; |
| 30 | + |
| 31 | +TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithGreaterSizeThenAllocatesNewKernelAllocation) { |
| 32 | + MockKernelWithInternals kernel(*pDevice); |
| 33 | + auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader); |
| 34 | + const size_t initialHeapSize = 0x40; |
| 35 | + pHeader->KernelHeapSize = initialHeapSize; |
| 36 | + |
| 37 | + EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation); |
| 38 | + kernel.kernelInfo.createKernelAllocation(pDevice->getMemoryManager()); |
| 39 | + auto firstAllocation = kernel.kernelInfo.kernelAllocation; |
| 40 | + EXPECT_NE(nullptr, firstAllocation); |
| 41 | + auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize(); |
| 42 | + EXPECT_EQ(initialHeapSize, firstAllocationSize); |
| 43 | + |
| 44 | + auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id; |
| 45 | + |
| 46 | + const size_t newHeapSize = initialHeapSize + 1; |
| 47 | + char newHeap[newHeapSize]; |
| 48 | + |
| 49 | + kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize); |
| 50 | + auto secondAllocation = kernel.kernelInfo.kernelAllocation; |
| 51 | + EXPECT_NE(nullptr, secondAllocation); |
| 52 | + auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize(); |
| 53 | + EXPECT_NE(initialHeapSize, secondAllocationSize); |
| 54 | + EXPECT_EQ(newHeapSize, secondAllocationSize); |
| 55 | + auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id; |
| 56 | + |
| 57 | + EXPECT_NE(firstAllocationId, secondAllocationId); |
| 58 | + |
| 59 | + pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation); |
| 60 | +} |
| 61 | + |
| 62 | +TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithSameSizeThenDoesNotAllocateNewKernelAllocation) { |
| 63 | + MockKernelWithInternals kernel(*pDevice); |
| 64 | + auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader); |
| 65 | + const size_t initialHeapSize = 0x40; |
| 66 | + pHeader->KernelHeapSize = initialHeapSize; |
| 67 | + |
| 68 | + EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation); |
| 69 | + kernel.kernelInfo.createKernelAllocation(pDevice->getMemoryManager()); |
| 70 | + auto firstAllocation = kernel.kernelInfo.kernelAllocation; |
| 71 | + EXPECT_NE(nullptr, firstAllocation); |
| 72 | + auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize(); |
| 73 | + EXPECT_EQ(initialHeapSize, firstAllocationSize); |
| 74 | + |
| 75 | + auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id; |
| 76 | + |
| 77 | + const size_t newHeapSize = initialHeapSize; |
| 78 | + char newHeap[newHeapSize]; |
| 79 | + |
| 80 | + kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize); |
| 81 | + auto secondAllocation = kernel.kernelInfo.kernelAllocation; |
| 82 | + EXPECT_NE(nullptr, secondAllocation); |
| 83 | + auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize(); |
| 84 | + EXPECT_EQ(initialHeapSize, secondAllocationSize); |
| 85 | + auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id; |
| 86 | + |
| 87 | + EXPECT_EQ(firstAllocationId, secondAllocationId); |
| 88 | + |
| 89 | + pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation); |
| 90 | +} |
| 91 | + |
| 92 | +TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithSmallerSizeThenDoesNotAllocateNewKernelAllocation) { |
| 93 | + MockKernelWithInternals kernel(*pDevice); |
| 94 | + auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader); |
| 95 | + const size_t initialHeapSize = 0x40; |
| 96 | + pHeader->KernelHeapSize = initialHeapSize; |
| 97 | + |
| 98 | + EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation); |
| 99 | + kernel.kernelInfo.createKernelAllocation(pDevice->getMemoryManager()); |
| 100 | + auto firstAllocation = kernel.kernelInfo.kernelAllocation; |
| 101 | + EXPECT_NE(nullptr, firstAllocation); |
| 102 | + auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize(); |
| 103 | + EXPECT_EQ(initialHeapSize, firstAllocationSize); |
| 104 | + |
| 105 | + auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id; |
| 106 | + |
| 107 | + const size_t newHeapSize = initialHeapSize - 1; |
| 108 | + char newHeap[newHeapSize]; |
| 109 | + |
| 110 | + kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize); |
| 111 | + auto secondAllocation = kernel.kernelInfo.kernelAllocation; |
| 112 | + EXPECT_NE(nullptr, secondAllocation); |
| 113 | + auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize(); |
| 114 | + EXPECT_EQ(initialHeapSize, secondAllocationSize); |
| 115 | + auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id; |
| 116 | + |
| 117 | + EXPECT_EQ(firstAllocationId, secondAllocationId); |
| 118 | + |
| 119 | + pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_F(KernelSubstituteTest, givenKernelWithUsedKernelAllocationWhenSubstituteKernelHeapAndAllocateNewMemoryThenStoreOldAllocationOnTemporaryList) { |
| 123 | + MockKernelWithInternals kernel(*pDevice); |
| 124 | + auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader); |
| 125 | + auto memoryManager = pDevice->getMemoryManager(); |
| 126 | + |
| 127 | + const size_t initialHeapSize = 0x40; |
| 128 | + pHeader->KernelHeapSize = initialHeapSize; |
| 129 | + |
| 130 | + kernel.kernelInfo.createKernelAllocation(memoryManager); |
| 131 | + auto firstAllocation = kernel.kernelInfo.kernelAllocation; |
| 132 | + |
| 133 | + firstAllocation->taskCount = ObjectNotUsed - 1; |
| 134 | + |
| 135 | + const size_t newHeapSize = initialHeapSize + 1; |
| 136 | + char newHeap[newHeapSize]; |
| 137 | + |
| 138 | + EXPECT_TRUE(memoryManager->graphicsAllocations.peekIsEmpty()); |
| 139 | + |
| 140 | + kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize); |
| 141 | + auto secondAllocation = kernel.kernelInfo.kernelAllocation; |
| 142 | + |
| 143 | + EXPECT_FALSE(memoryManager->graphicsAllocations.peekIsEmpty()); |
| 144 | + EXPECT_EQ(memoryManager->graphicsAllocations.peekHead(), firstAllocation); |
| 145 | + memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation); |
| 146 | + memoryManager->cleanAllocationList(firstAllocation->taskCount, TEMPORARY_ALLOCATION); |
| 147 | +} |
0 commit comments