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
|
/*
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/memory_manager/prefetch_manager.h"
#include "shared/source/unified_memory/usm_memory_support.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/memory_manager/mock_prefetch_manager.h"
#include "shared/test/common/mocks/mock_command_stream_receiver.h"
#include "shared/test/common/mocks/mock_device.h"
#include "shared/test/common/mocks/mock_svm_manager.h"
#include "shared/test/common/mocks/ult_device_factory.h"
#include "gtest/gtest.h"
using namespace NEO;
TEST(PrefetchManagerTests, givenPrefetchManagerWhenCallingInterfaceFunctionsThenUpdateAllocationsInPrefetchContext) {
std::unique_ptr<UltDeviceFactory> deviceFactory(new UltDeviceFactory(1, 1));
RootDeviceIndicesContainer rootDeviceIndices = {mockRootDeviceIndex};
std::map<uint32_t, DeviceBitfield> deviceBitfields{{mockRootDeviceIndex, mockDeviceBitfield}};
auto device = deviceFactory->rootDevices[0];
auto csr = std::make_unique<MockCommandStreamReceiver>(*device->getExecutionEnvironment(), device->getRootDeviceIndex(), device->getDeviceBitfield());
auto svmManager = std::make_unique<MockSVMAllocsManager>(device->getMemoryManager());
auto prefetchManager = std::make_unique<MockPrefetchManager>();
PrefetchContext prefetchContext;
DebugManagerStateRestore restore;
SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::sharedUnifiedMemory, 1, rootDeviceIndices, deviceBitfields);
auto ptr = svmManager->createSharedUnifiedMemoryAllocation(4096u, unifiedMemoryProperties, nullptr);
ASSERT_NE(nullptr, ptr);
auto &hwInfo = *device->getRootDeviceEnvironment().getMutableHardwareInfo();
VariableBackup<uint64_t> sharedSystemMemCapabilities{&hwInfo.capabilityTable.sharedSystemMemCapabilities};
sharedSystemMemCapabilities = (UnifiedSharedMemoryFlags::access | UnifiedSharedMemoryFlags::atomicAccess | UnifiedSharedMemoryFlags::concurrentAccess | UnifiedSharedMemoryFlags::concurrentAtomicAccess);
auto svmData = svmManager->getSVMAlloc(ptr);
ASSERT_NE(nullptr, svmData);
auto ptr2 = malloc(1024);
debugManager.flags.EnableSharedSystemUsmSupport.set(1);
EXPECT_EQ(0u, prefetchContext.allocations.size());
prefetchManager->insertAllocation(prefetchContext, *svmManager.get(), *device, ptr, 4096);
EXPECT_EQ(1u, prefetchContext.allocations.size());
debugManager.flags.EnableRecoverablePageFaults.set(0);
prefetchManager->insertAllocation(prefetchContext, *svmManager.get(), *device, ptr2, 1024);
EXPECT_EQ(1u, prefetchContext.allocations.size());
debugManager.flags.EnableRecoverablePageFaults.set(1);
prefetchManager->insertAllocation(prefetchContext, *svmManager.get(), *device, ptr2, 1024);
EXPECT_EQ(2u, prefetchContext.allocations.size());
prefetchManager->migrateAllocationsToGpu(prefetchContext, *svmManager.get(), *device, *csr.get());
EXPECT_EQ(2u, prefetchContext.allocations.size());
prefetchManager->removeAllocations(prefetchContext);
EXPECT_EQ(0u, prefetchContext.allocations.size());
svmManager->freeSVMAlloc(ptr);
free(ptr2);
}
TEST(PrefetchManagerTests, givenPrefetchManagerWhenCallingInterfaceFunctionsThenNoUpdateAllocationsInPrefetchContextForInvalidAllocations) {
std::unique_ptr<UltDeviceFactory> deviceFactory(new UltDeviceFactory(1, 1));
RootDeviceIndicesContainer rootDeviceIndices = {mockRootDeviceIndex};
std::map<uint32_t, DeviceBitfield> deviceBitfields{{mockRootDeviceIndex, mockDeviceBitfield}};
auto device = deviceFactory->rootDevices[0];
auto csr = std::make_unique<MockCommandStreamReceiver>(*device->getExecutionEnvironment(), device->getRootDeviceIndex(), device->getDeviceBitfield());
auto svmManager = std::make_unique<MockSVMAllocsManager>(device->getMemoryManager());
auto prefetchManager = std::make_unique<MockPrefetchManager>();
PrefetchContext prefetchContext;
DebugManagerStateRestore restore;
SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::sharedUnifiedMemory, 1, rootDeviceIndices, deviceBitfields);
auto ptr = svmManager->createSharedUnifiedMemoryAllocation(4096u, unifiedMemoryProperties, nullptr);
ASSERT_NE(nullptr, ptr);
auto svmData = svmManager->getSVMAlloc(ptr);
svmData->memoryType = InternalMemoryType::deviceUnifiedMemory;
ASSERT_NE(nullptr, svmData);
EXPECT_EQ(0u, prefetchContext.allocations.size());
prefetchManager->insertAllocation(prefetchContext, *svmManager.get(), *device, ptr, 4096);
EXPECT_EQ(0u, prefetchContext.allocations.size());
prefetchManager->migrateAllocationsToGpu(prefetchContext, *svmManager.get(), *device, *csr.get());
EXPECT_EQ(0u, prefetchContext.allocations.size());
svmManager->freeSVMAlloc(ptr);
}
TEST(PrefetchManagerTests, givenPrefetchManagerWhenCallingMigrateAllocationsToGpuThenPrefetchMemoryForValidSVMAllocPtrs) {
DebugManagerStateRestore restore;
debugManager.flags.UseKmdMigration.set(1);
std::unique_ptr<UltDeviceFactory> deviceFactory(new UltDeviceFactory(1, 1));
RootDeviceIndicesContainer rootDeviceIndices = {mockRootDeviceIndex};
std::map<uint32_t, DeviceBitfield> deviceBitfields{{mockRootDeviceIndex, mockDeviceBitfield}};
auto device = deviceFactory->rootDevices[0];
auto csr = std::make_unique<MockCommandStreamReceiver>(*device->getExecutionEnvironment(), device->getRootDeviceIndex(), device->getDeviceBitfield());
auto svmManager = std::make_unique<MockSVMAllocsManager>(device->getMemoryManager());
auto prefetchManager = std::make_unique<MockPrefetchManager>();
PrefetchContext prefetchContext;
SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::sharedUnifiedMemory, 1, rootDeviceIndices, deviceBitfields);
auto ptr = svmManager->createSharedUnifiedMemoryAllocation(4096u, unifiedMemoryProperties, nullptr);
ASSERT_NE(nullptr, ptr);
auto svmData = svmManager->getSVMAlloc(ptr);
ASSERT_NE(nullptr, svmData);
prefetchManager->insertAllocation(prefetchContext, *svmManager.get(), *device, ptr, 4096);
EXPECT_FALSE(prefetchManager->migrateAllocationsToGpuCalled);
EXPECT_FALSE(svmManager->prefetchMemoryCalled);
prefetchManager->migrateAllocationsToGpu(prefetchContext, *svmManager.get(), *device, *csr.get());
EXPECT_TRUE(prefetchManager->migrateAllocationsToGpuCalled);
EXPECT_TRUE(svmManager->prefetchMemoryCalled);
svmManager->freeSVMAlloc(ptr);
}
|