|
| 1 | +/* |
| 2 | + * Copyright (C) 2018-2021 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "shared/source/memory_manager/surface.h" |
| 9 | +#include "shared/test/common/fixtures/device_fixture.h" |
| 10 | +#include "shared/test/common/mocks/mock_csr.h" |
| 11 | + |
| 12 | +#include "opencl/source/mem_obj/buffer.h" |
| 13 | +#include "opencl/test/unit_test/fixtures/multi_root_device_fixture.h" |
| 14 | +#include "opencl/test/unit_test/helpers/raii_hw_helper.h" |
| 15 | +#include "opencl/test/unit_test/mocks/mock_context.h" |
| 16 | +#include "opencl/test/unit_test/mocks/mock_hw_helper.h" |
| 17 | +#include "test.h" |
| 18 | + |
| 19 | +#include "gmock/gmock.h" |
| 20 | + |
| 21 | +using namespace NEO; |
| 22 | + |
| 23 | +TEST(ClCommandStreamReceiverTest, WhenMakingResidentThenBufferResidencyFlagIsSet) { |
| 24 | + MockContext context; |
| 25 | + auto commandStreamReceiver = context.getDevice(0)->getDefaultEngine().commandStreamReceiver; |
| 26 | + float srcMemory[] = {1.0f}; |
| 27 | + |
| 28 | + auto retVal = CL_INVALID_VALUE; |
| 29 | + auto buffer = Buffer::create( |
| 30 | + &context, |
| 31 | + CL_MEM_USE_HOST_PTR, |
| 32 | + sizeof(srcMemory), |
| 33 | + srcMemory, |
| 34 | + retVal); |
| 35 | + ASSERT_NE(nullptr, buffer); |
| 36 | + |
| 37 | + auto graphicsAllocation = buffer->getGraphicsAllocation(context.getDevice(0)->getRootDeviceIndex()); |
| 38 | + EXPECT_FALSE(graphicsAllocation->isResident(commandStreamReceiver->getOsContext().getContextId())); |
| 39 | + |
| 40 | + commandStreamReceiver->makeResident(*graphicsAllocation); |
| 41 | + |
| 42 | + EXPECT_TRUE(graphicsAllocation->isResident(commandStreamReceiver->getOsContext().getContextId())); |
| 43 | + |
| 44 | + delete buffer; |
| 45 | +} |
| 46 | + |
| 47 | +using ClCommandStreamReceiverTests = Test<DeviceFixture>; |
| 48 | + |
| 49 | +HWTEST_F(ClCommandStreamReceiverTests, givenCommandStreamReceiverWhenFenceAllocationIsRequiredAndCreateGlobalFenceAllocationIsCalledThenFenceAllocationIsAllocated) { |
| 50 | + RAIIHwHelperFactory<MockHwHelperWithFenceAllocation<FamilyType>> hwHelperBackup{pDevice->getHardwareInfo().platform.eRenderCoreFamily}; |
| 51 | + |
| 52 | + MockCsrHw<FamilyType> csr(*pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()); |
| 53 | + csr.setupContext(*pDevice->getDefaultEngine().osContext); |
| 54 | + EXPECT_EQ(nullptr, csr.globalFenceAllocation); |
| 55 | + |
| 56 | + EXPECT_TRUE(csr.createGlobalFenceAllocation()); |
| 57 | + |
| 58 | + ASSERT_NE(nullptr, csr.globalFenceAllocation); |
| 59 | + EXPECT_EQ(GraphicsAllocation::AllocationType::GLOBAL_FENCE, csr.globalFenceAllocation->getAllocationType()); |
| 60 | +} |
| 61 | + |
| 62 | +HWTEST_F(ClCommandStreamReceiverTests, givenCommandStreamReceiverWhenGettingFenceAllocationThenCorrectFenceAllocationIsReturned) { |
| 63 | + RAIIHwHelperFactory<MockHwHelperWithFenceAllocation<FamilyType>> hwHelperBackup{pDevice->getHardwareInfo().platform.eRenderCoreFamily}; |
| 64 | + |
| 65 | + CommandStreamReceiverHw<FamilyType> csr(*pDevice->executionEnvironment, pDevice->getRootDeviceIndex(), pDevice->getDeviceBitfield()); |
| 66 | + csr.setupContext(*pDevice->getDefaultEngine().osContext); |
| 67 | + EXPECT_EQ(nullptr, csr.getGlobalFenceAllocation()); |
| 68 | + |
| 69 | + EXPECT_TRUE(csr.createGlobalFenceAllocation()); |
| 70 | + |
| 71 | + ASSERT_NE(nullptr, csr.getGlobalFenceAllocation()); |
| 72 | + EXPECT_EQ(GraphicsAllocation::AllocationType::GLOBAL_FENCE, csr.getGlobalFenceAllocation()->getAllocationType()); |
| 73 | +} |
| 74 | + |
| 75 | +using CommandStreamReceiverMultiRootDeviceTest = MultiRootDeviceFixture; |
| 76 | + |
| 77 | +TEST_F(CommandStreamReceiverMultiRootDeviceTest, WhenCreatingCommandStreamGraphicsAllocationsThenTheyHaveCorrectRootDeviceIndex) { |
| 78 | + auto commandStreamReceiver = &device1->getGpgpuCommandStreamReceiver(); |
| 79 | + |
| 80 | + ASSERT_NE(nullptr, commandStreamReceiver); |
| 81 | + EXPECT_EQ(expectedRootDeviceIndex, commandStreamReceiver->getRootDeviceIndex()); |
| 82 | + |
| 83 | + // Linear stream / Command buffer |
| 84 | + GraphicsAllocation *allocation = mockMemoryManager->allocateGraphicsMemoryWithProperties({expectedRootDeviceIndex, 128u, GraphicsAllocation::AllocationType::COMMAND_BUFFER, device1->getDeviceBitfield()}); |
| 85 | + LinearStream commandStream{allocation}; |
| 86 | + |
| 87 | + commandStreamReceiver->ensureCommandBufferAllocation(commandStream, 100u, 0u); |
| 88 | + EXPECT_EQ(allocation, commandStream.getGraphicsAllocation()); |
| 89 | + EXPECT_EQ(128u, commandStream.getMaxAvailableSpace()); |
| 90 | + EXPECT_EQ(expectedRootDeviceIndex, commandStream.getGraphicsAllocation()->getRootDeviceIndex()); |
| 91 | + |
| 92 | + commandStreamReceiver->ensureCommandBufferAllocation(commandStream, 1024u, 0u); |
| 93 | + EXPECT_NE(allocation, commandStream.getGraphicsAllocation()); |
| 94 | + EXPECT_EQ(0u, commandStream.getMaxAvailableSpace() % MemoryConstants::pageSize64k); |
| 95 | + EXPECT_EQ(expectedRootDeviceIndex, commandStream.getGraphicsAllocation()->getRootDeviceIndex()); |
| 96 | + mockMemoryManager->freeGraphicsMemory(commandStream.getGraphicsAllocation()); |
| 97 | + |
| 98 | + // Debug surface |
| 99 | + auto debugSurface = commandStreamReceiver->allocateDebugSurface(MemoryConstants::pageSize); |
| 100 | + ASSERT_NE(nullptr, debugSurface); |
| 101 | + EXPECT_EQ(expectedRootDeviceIndex, debugSurface->getRootDeviceIndex()); |
| 102 | + |
| 103 | + // Indirect heaps |
| 104 | + IndirectHeap::Type heapTypes[]{IndirectHeap::DYNAMIC_STATE, IndirectHeap::INDIRECT_OBJECT, IndirectHeap::SURFACE_STATE}; |
| 105 | + for (auto heapType : heapTypes) { |
| 106 | + IndirectHeap *heap = nullptr; |
| 107 | + commandStreamReceiver->allocateHeapMemory(heapType, MemoryConstants::pageSize, heap); |
| 108 | + ASSERT_NE(nullptr, heap); |
| 109 | + ASSERT_NE(nullptr, heap->getGraphicsAllocation()); |
| 110 | + EXPECT_EQ(expectedRootDeviceIndex, heap->getGraphicsAllocation()->getRootDeviceIndex()); |
| 111 | + mockMemoryManager->freeGraphicsMemory(heap->getGraphicsAllocation()); |
| 112 | + delete heap; |
| 113 | + } |
| 114 | + |
| 115 | + // Tag allocation |
| 116 | + ASSERT_NE(nullptr, commandStreamReceiver->getTagAllocation()); |
| 117 | + EXPECT_EQ(expectedRootDeviceIndex, commandStreamReceiver->getTagAllocation()->getRootDeviceIndex()); |
| 118 | + |
| 119 | + // Preemption allocation |
| 120 | + if (nullptr == commandStreamReceiver->getPreemptionAllocation()) { |
| 121 | + commandStreamReceiver->createPreemptionAllocation(); |
| 122 | + } |
| 123 | + EXPECT_EQ(expectedRootDeviceIndex, commandStreamReceiver->getPreemptionAllocation()->getRootDeviceIndex()); |
| 124 | + |
| 125 | + // HostPtr surface |
| 126 | + char memory[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 127 | + HostPtrSurface surface(memory, sizeof(memory), true); |
| 128 | + EXPECT_TRUE(commandStreamReceiver->createAllocationForHostSurface(surface, false)); |
| 129 | + ASSERT_NE(nullptr, surface.getAllocation()); |
| 130 | + EXPECT_EQ(expectedRootDeviceIndex, surface.getAllocation()->getRootDeviceIndex()); |
| 131 | +} |
0 commit comments