2018-04-05 01:02:07 +08:00
|
|
|
/*
|
2020-01-13 21:47:05 +08:00
|
|
|
* Copyright (C) 2018-2020 Intel Corporation
|
2018-04-05 01:02:07 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2018-04-05 01:02:07 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/memory_manager/internal_allocation_storage.h"
|
|
|
|
#include "shared/source/os_interface/os_context.h"
|
2020-02-24 17:22:30 +08:00
|
|
|
|
2020-03-19 21:26:08 +08:00
|
|
|
#include "opencl/source/memory_manager/os_agnostic_memory_manager.h"
|
2020-02-23 22:20:22 +08:00
|
|
|
#include "opencl/test/unit_test/fixtures/device_fixture.h"
|
|
|
|
#include "opencl/test/unit_test/mocks/mock_kernel.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
#include "test.h"
|
2020-02-23 05:50:57 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
using namespace NEO;
|
2018-04-05 01:02:07 +08:00
|
|
|
|
|
|
|
typedef Test<DeviceFixture> KernelSubstituteTest;
|
|
|
|
|
|
|
|
TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithGreaterSizeThenAllocatesNewKernelAllocation) {
|
2020-01-14 21:32:11 +08:00
|
|
|
MockKernelWithInternals kernel(*pClDevice);
|
2018-04-05 01:02:07 +08:00
|
|
|
auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader);
|
|
|
|
const size_t initialHeapSize = 0x40;
|
|
|
|
pHeader->KernelHeapSize = initialHeapSize;
|
|
|
|
|
|
|
|
EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation);
|
2019-11-07 21:15:04 +08:00
|
|
|
kernel.kernelInfo.createKernelAllocation(pDevice->getRootDeviceIndex(), pDevice->getMemoryManager());
|
2018-04-05 01:02:07 +08:00
|
|
|
auto firstAllocation = kernel.kernelInfo.kernelAllocation;
|
|
|
|
EXPECT_NE(nullptr, firstAllocation);
|
|
|
|
auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize();
|
|
|
|
EXPECT_EQ(initialHeapSize, firstAllocationSize);
|
|
|
|
|
|
|
|
auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id;
|
|
|
|
|
|
|
|
const size_t newHeapSize = initialHeapSize + 1;
|
|
|
|
char newHeap[newHeapSize];
|
|
|
|
|
|
|
|
kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize);
|
|
|
|
auto secondAllocation = kernel.kernelInfo.kernelAllocation;
|
|
|
|
EXPECT_NE(nullptr, secondAllocation);
|
|
|
|
auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize();
|
|
|
|
EXPECT_NE(initialHeapSize, secondAllocationSize);
|
|
|
|
EXPECT_EQ(newHeapSize, secondAllocationSize);
|
|
|
|
auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id;
|
|
|
|
|
|
|
|
EXPECT_NE(firstAllocationId, secondAllocationId);
|
|
|
|
|
|
|
|
pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithSameSizeThenDoesNotAllocateNewKernelAllocation) {
|
2020-01-14 21:32:11 +08:00
|
|
|
MockKernelWithInternals kernel(*pClDevice);
|
2018-04-05 01:02:07 +08:00
|
|
|
auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader);
|
|
|
|
const size_t initialHeapSize = 0x40;
|
|
|
|
pHeader->KernelHeapSize = initialHeapSize;
|
|
|
|
|
|
|
|
EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation);
|
2019-11-07 21:15:04 +08:00
|
|
|
kernel.kernelInfo.createKernelAllocation(pDevice->getRootDeviceIndex(), pDevice->getMemoryManager());
|
2018-04-05 01:02:07 +08:00
|
|
|
auto firstAllocation = kernel.kernelInfo.kernelAllocation;
|
|
|
|
EXPECT_NE(nullptr, firstAllocation);
|
|
|
|
auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize();
|
|
|
|
EXPECT_EQ(initialHeapSize, firstAllocationSize);
|
|
|
|
|
|
|
|
auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id;
|
|
|
|
|
|
|
|
const size_t newHeapSize = initialHeapSize;
|
|
|
|
char newHeap[newHeapSize];
|
|
|
|
|
|
|
|
kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize);
|
|
|
|
auto secondAllocation = kernel.kernelInfo.kernelAllocation;
|
|
|
|
EXPECT_NE(nullptr, secondAllocation);
|
|
|
|
auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize();
|
|
|
|
EXPECT_EQ(initialHeapSize, secondAllocationSize);
|
|
|
|
auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id;
|
|
|
|
|
|
|
|
EXPECT_EQ(firstAllocationId, secondAllocationId);
|
|
|
|
|
|
|
|
pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithSmallerSizeThenDoesNotAllocateNewKernelAllocation) {
|
2020-01-14 21:32:11 +08:00
|
|
|
MockKernelWithInternals kernel(*pClDevice);
|
2018-04-05 01:02:07 +08:00
|
|
|
auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader);
|
|
|
|
const size_t initialHeapSize = 0x40;
|
|
|
|
pHeader->KernelHeapSize = initialHeapSize;
|
|
|
|
|
|
|
|
EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation);
|
2019-11-07 21:15:04 +08:00
|
|
|
kernel.kernelInfo.createKernelAllocation(pDevice->getRootDeviceIndex(), pDevice->getMemoryManager());
|
2018-04-05 01:02:07 +08:00
|
|
|
auto firstAllocation = kernel.kernelInfo.kernelAllocation;
|
|
|
|
EXPECT_NE(nullptr, firstAllocation);
|
|
|
|
auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize();
|
|
|
|
EXPECT_EQ(initialHeapSize, firstAllocationSize);
|
|
|
|
|
|
|
|
auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id;
|
|
|
|
|
|
|
|
const size_t newHeapSize = initialHeapSize - 1;
|
|
|
|
char newHeap[newHeapSize];
|
|
|
|
|
|
|
|
kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize);
|
|
|
|
auto secondAllocation = kernel.kernelInfo.kernelAllocation;
|
|
|
|
EXPECT_NE(nullptr, secondAllocation);
|
|
|
|
auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize();
|
|
|
|
EXPECT_EQ(initialHeapSize, secondAllocationSize);
|
|
|
|
auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id;
|
|
|
|
|
|
|
|
EXPECT_EQ(firstAllocationId, secondAllocationId);
|
|
|
|
|
|
|
|
pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KernelSubstituteTest, givenKernelWithUsedKernelAllocationWhenSubstituteKernelHeapAndAllocateNewMemoryThenStoreOldAllocationOnTemporaryList) {
|
2020-01-14 21:32:11 +08:00
|
|
|
MockKernelWithInternals kernel(*pClDevice);
|
2018-04-05 01:02:07 +08:00
|
|
|
auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader);
|
|
|
|
auto memoryManager = pDevice->getMemoryManager();
|
2019-07-15 20:28:09 +08:00
|
|
|
auto &commandStreamReceiver = pDevice->getGpgpuCommandStreamReceiver();
|
2018-04-05 01:02:07 +08:00
|
|
|
|
|
|
|
const size_t initialHeapSize = 0x40;
|
|
|
|
pHeader->KernelHeapSize = initialHeapSize;
|
|
|
|
|
2019-11-07 21:15:04 +08:00
|
|
|
kernel.kernelInfo.createKernelAllocation(pDevice->getRootDeviceIndex(), memoryManager);
|
2018-04-05 01:02:07 +08:00
|
|
|
auto firstAllocation = kernel.kernelInfo.kernelAllocation;
|
|
|
|
|
2018-11-02 17:01:56 +08:00
|
|
|
uint32_t notReadyTaskCount = *commandStreamReceiver.getTagAddress() + 1u;
|
|
|
|
|
2018-12-03 17:05:36 +08:00
|
|
|
firstAllocation->updateTaskCount(notReadyTaskCount, commandStreamReceiver.getOsContext().getContextId());
|
2018-04-05 01:02:07 +08:00
|
|
|
|
|
|
|
const size_t newHeapSize = initialHeapSize + 1;
|
|
|
|
char newHeap[newHeapSize];
|
|
|
|
|
2018-10-26 19:05:31 +08:00
|
|
|
EXPECT_TRUE(commandStreamReceiver.getTemporaryAllocations().peekIsEmpty());
|
2018-04-05 01:02:07 +08:00
|
|
|
|
|
|
|
kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize);
|
|
|
|
auto secondAllocation = kernel.kernelInfo.kernelAllocation;
|
|
|
|
|
2018-10-26 19:05:31 +08:00
|
|
|
EXPECT_FALSE(commandStreamReceiver.getTemporaryAllocations().peekIsEmpty());
|
|
|
|
EXPECT_EQ(commandStreamReceiver.getTemporaryAllocations().peekHead(), firstAllocation);
|
2018-04-05 01:02:07 +08:00
|
|
|
memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation);
|
2018-11-02 17:01:56 +08:00
|
|
|
commandStreamReceiver.getInternalAllocationStorage()->cleanAllocationList(notReadyTaskCount, TEMPORARY_ALLOCATION);
|
2018-04-05 01:02:07 +08:00
|
|
|
}
|