2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2020-01-13 16:40:03 +08:00
|
|
|
* Copyright (C) 2017-2020 Intel Corporation
|
2018-09-18 15:11:08 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
#pragma once
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/execution_environment/execution_environment.h"
|
2020-02-24 08:01:38 +08:00
|
|
|
#include "shared/test/unit_test/helpers/default_hw_info.h"
|
2020-02-24 17:22:30 +08:00
|
|
|
|
2020-02-23 05:50:57 +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/mocks/mock_execution_environment.h"
|
|
|
|
#include "opencl/test/unit_test/mocks/mock_host_ptr_manager.h"
|
2018-10-24 14:46:54 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-03-15 17:22:35 +08:00
|
|
|
template <class T>
|
|
|
|
class MemoryManagerCreate : public T {
|
|
|
|
public:
|
|
|
|
using T::T;
|
|
|
|
|
|
|
|
template <class... U>
|
|
|
|
MemoryManagerCreate(bool enable64kbPages, bool enableLocalMemory, U &&... args) : T(std::forward<U>(args)...) {
|
2020-02-12 15:29:26 +08:00
|
|
|
std::fill(this->enable64kbpages.begin(), this->enable64kbpages.end(), enable64kbPages);
|
|
|
|
std::fill(this->localMemorySupported.begin(), this->localMemorySupported.end(), enableLocalMemory);
|
2019-03-15 17:22:35 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockMemoryManager : public MemoryManagerCreate<OsAgnosticMemoryManager> {
|
2017-12-21 07:45:38 +08:00
|
|
|
public:
|
2019-03-12 19:00:41 +08:00
|
|
|
using MemoryManager::allocateGraphicsMemoryForNonSvmHostPtr;
|
2018-07-25 00:36:26 +08:00
|
|
|
using MemoryManager::allocateGraphicsMemoryInPreferredPool;
|
2019-08-23 19:09:09 +08:00
|
|
|
using MemoryManager::allocateGraphicsMemoryWithAlignment;
|
2019-03-12 19:00:41 +08:00
|
|
|
using MemoryManager::allocateGraphicsMemoryWithProperties;
|
2018-12-06 22:03:06 +08:00
|
|
|
using MemoryManager::AllocationData;
|
2019-02-25 21:11:34 +08:00
|
|
|
using MemoryManager::createGraphicsAllocation;
|
2019-05-06 16:35:49 +08:00
|
|
|
using MemoryManager::createStorageInfoFromProperties;
|
2018-07-24 22:42:35 +08:00
|
|
|
using MemoryManager::getAllocationData;
|
2019-10-22 16:26:23 +08:00
|
|
|
using MemoryManager::gfxPartitions;
|
2019-05-10 17:30:07 +08:00
|
|
|
using MemoryManager::localMemoryUsageBankSelector;
|
2019-01-03 21:48:24 +08:00
|
|
|
using MemoryManager::multiContextResourceDestructor;
|
2020-01-16 00:02:47 +08:00
|
|
|
using MemoryManager::overrideAllocationData;
|
2019-07-04 18:17:42 +08:00
|
|
|
using MemoryManager::pageFaultManager;
|
2019-02-18 20:59:16 +08:00
|
|
|
using MemoryManager::registeredEngines;
|
2019-06-06 14:45:14 +08:00
|
|
|
using MemoryManager::supportsMultiStorageResources;
|
2019-02-21 23:29:05 +08:00
|
|
|
using MemoryManager::useInternal32BitAllocator;
|
2019-12-19 00:16:49 +08:00
|
|
|
using MemoryManager::useNonSvmHostPtrAlloc;
|
2018-12-20 23:58:15 +08:00
|
|
|
using OsAgnosticMemoryManager::allocateGraphicsMemoryForImageFromHostPtr;
|
2019-03-15 17:22:35 +08:00
|
|
|
using MemoryManagerCreate<OsAgnosticMemoryManager>::MemoryManagerCreate;
|
2020-01-13 16:40:03 +08:00
|
|
|
using MemoryManager::isCopyRequired;
|
2019-08-27 21:26:30 +08:00
|
|
|
using MemoryManager::reservedMemory;
|
2018-12-20 23:58:15 +08:00
|
|
|
|
|
|
|
MockMemoryManager(ExecutionEnvironment &executionEnvironment) : MockMemoryManager(false, executionEnvironment) {}
|
|
|
|
|
2019-03-15 17:22:35 +08:00
|
|
|
MockMemoryManager(bool enableLocalMemory, ExecutionEnvironment &executionEnvironment) : MemoryManagerCreate(false, enableLocalMemory, executionEnvironment) {
|
2018-10-24 14:46:54 +08:00
|
|
|
hostPtrManager.reset(new MockHostPtrManager);
|
|
|
|
};
|
2018-12-20 23:58:15 +08:00
|
|
|
|
2020-03-25 00:04:08 +08:00
|
|
|
MockMemoryManager() : MockMemoryManager(*(new MockExecutionEnvironment(defaultHwInfo.get()))) {
|
2020-02-25 23:38:47 +08:00
|
|
|
mockExecutionEnvironment.reset(static_cast<MockExecutionEnvironment *>(&executionEnvironment));
|
2018-10-01 22:10:54 +08:00
|
|
|
};
|
2020-03-25 00:04:08 +08:00
|
|
|
MockMemoryManager(bool enable64pages, bool enableLocalMemory) : MemoryManagerCreate(enable64pages, enableLocalMemory, *(new MockExecutionEnvironment(defaultHwInfo.get()))) {
|
2020-02-25 23:38:47 +08:00
|
|
|
mockExecutionEnvironment.reset(static_cast<MockExecutionEnvironment *>(&executionEnvironment));
|
2018-10-01 22:10:54 +08:00
|
|
|
}
|
2019-02-28 21:12:13 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemory64kb(const AllocationData &allocationData) override;
|
2017-12-21 07:45:38 +08:00
|
|
|
void setDeferredDeleter(DeferredDeleter *deleter);
|
|
|
|
void overrideAsyncDeleterFlag(bool newValue);
|
2019-01-22 19:40:17 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryForImage(const AllocationData &allocationData) override;
|
2019-11-14 17:08:59 +08:00
|
|
|
GraphicsAllocation *allocateShareableMemory(const AllocationData &allocationData) override;
|
2017-12-21 07:45:38 +08:00
|
|
|
int redundancyRatio = 1;
|
2018-07-24 22:42:35 +08:00
|
|
|
|
|
|
|
GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) override;
|
2018-11-30 18:01:33 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override;
|
2018-12-14 18:24:45 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties) override;
|
|
|
|
|
|
|
|
void *allocateSystemMemory(size_t size, size_t alignment) override;
|
2018-07-24 22:42:35 +08:00
|
|
|
|
2018-11-28 18:09:56 +08:00
|
|
|
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override {
|
|
|
|
freeGraphicsMemoryCalled++;
|
|
|
|
OsAgnosticMemoryManager::freeGraphicsMemoryImpl(gfxAllocation);
|
|
|
|
};
|
|
|
|
|
2019-01-24 22:16:12 +08:00
|
|
|
void *lockResourceImpl(GraphicsAllocation &gfxAllocation) override {
|
|
|
|
lockResourceCalled++;
|
|
|
|
return OsAgnosticMemoryManager::lockResourceImpl(gfxAllocation);
|
|
|
|
}
|
|
|
|
|
2019-01-24 18:51:33 +08:00
|
|
|
void unlockResourceImpl(GraphicsAllocation &gfxAllocation) override {
|
2018-12-12 21:36:45 +08:00
|
|
|
unlockResourceCalled++;
|
2019-01-24 18:51:33 +08:00
|
|
|
OsAgnosticMemoryManager::unlockResourceImpl(gfxAllocation);
|
2018-12-12 21:36:45 +08:00
|
|
|
}
|
2019-03-04 21:50:26 +08:00
|
|
|
|
|
|
|
void handleFenceCompletion(GraphicsAllocation *graphicsAllocation) override {
|
|
|
|
handleFenceCompletionCalled++;
|
|
|
|
OsAgnosticMemoryManager::handleFenceCompletion(graphicsAllocation);
|
|
|
|
}
|
|
|
|
|
2020-01-07 14:42:40 +08:00
|
|
|
void *reserveCpuAddressRange(size_t size, uint32_t rootDeviceIndex) override {
|
2019-03-06 23:35:21 +08:00
|
|
|
if (failReserveAddress) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-01-07 14:42:40 +08:00
|
|
|
return OsAgnosticMemoryManager::reserveCpuAddressRange(size, rootDeviceIndex);
|
2019-03-06 23:35:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 16:59:18 +08:00
|
|
|
bool isCpuCopyRequired(const void *ptr) override { return cpuCopyRequired; }
|
|
|
|
|
2019-02-21 23:29:05 +08:00
|
|
|
GraphicsAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, GraphicsAllocation::AllocationType allocationType);
|
2019-07-26 21:28:06 +08:00
|
|
|
GraphicsAllocation *allocate32BitGraphicsMemoryImpl(const AllocationData &allocationData) override;
|
2018-12-12 21:36:45 +08:00
|
|
|
|
2020-01-30 00:48:36 +08:00
|
|
|
void forceLimitedRangeAllocator(uint32_t rootDeviceIndex, uint64_t range) { getGfxPartition(rootDeviceIndex)->init(range, 0, 0, gfxPartitions.size()); }
|
2019-08-23 19:09:09 +08:00
|
|
|
|
2018-11-28 18:09:56 +08:00
|
|
|
uint32_t freeGraphicsMemoryCalled = 0u;
|
2018-12-12 21:36:45 +08:00
|
|
|
uint32_t unlockResourceCalled = 0u;
|
2019-01-24 22:16:12 +08:00
|
|
|
uint32_t lockResourceCalled = 0u;
|
2019-03-04 21:50:26 +08:00
|
|
|
uint32_t handleFenceCompletionCalled = 0u;
|
2018-07-24 22:42:35 +08:00
|
|
|
bool allocationCreated = false;
|
|
|
|
bool allocation64kbPageCreated = false;
|
|
|
|
bool allocationInDevicePoolCreated = false;
|
|
|
|
bool failInDevicePool = false;
|
|
|
|
bool failInDevicePoolWithError = false;
|
2018-07-25 15:38:00 +08:00
|
|
|
bool failInAllocateWithSizeAndAlignment = false;
|
2018-07-24 22:42:35 +08:00
|
|
|
bool preferRenderCompressedFlagPassed = false;
|
2019-01-22 19:40:17 +08:00
|
|
|
bool allocateForImageCalled = false;
|
2019-11-14 17:08:59 +08:00
|
|
|
bool allocateForShareableCalled = false;
|
2019-03-06 23:35:21 +08:00
|
|
|
bool failReserveAddress = false;
|
|
|
|
bool failAllocateSystemMemory = false;
|
2019-07-26 21:28:06 +08:00
|
|
|
bool failAllocate32Bit = false;
|
2020-02-28 16:59:18 +08:00
|
|
|
bool cpuCopyRequired = false;
|
2020-02-25 23:38:47 +08:00
|
|
|
std::unique_ptr<MockExecutionEnvironment> mockExecutionEnvironment;
|
2017-12-21 07:45:38 +08:00
|
|
|
};
|
|
|
|
|
2018-12-06 22:03:06 +08:00
|
|
|
using AllocationData = MockMemoryManager::AllocationData;
|
|
|
|
|
2018-02-28 19:09:48 +08:00
|
|
|
class GMockMemoryManager : public MockMemoryManager {
|
2017-12-21 07:45:38 +08:00
|
|
|
public:
|
2018-10-01 22:10:54 +08:00
|
|
|
GMockMemoryManager(const ExecutionEnvironment &executionEnvironment) : MockMemoryManager(const_cast<ExecutionEnvironment &>(executionEnvironment)){};
|
2020-01-07 14:42:40 +08:00
|
|
|
MOCK_METHOD2(populateOsHandles, MemoryManager::AllocationStatus(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex));
|
2019-03-12 19:00:41 +08:00
|
|
|
MOCK_METHOD1(allocateGraphicsMemoryForNonSvmHostPtr, GraphicsAllocation *(const AllocationData &));
|
2018-08-24 21:23:45 +08:00
|
|
|
|
2020-01-07 14:42:40 +08:00
|
|
|
MemoryManager::AllocationStatus MemoryManagerPopulateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) { return OsAgnosticMemoryManager::populateOsHandles(handleStorage, rootDeviceIndex); }
|
2017-12-21 07:45:38 +08:00
|
|
|
};
|
2018-01-26 23:53:18 +08:00
|
|
|
|
|
|
|
class MockAllocSysMemAgnosticMemoryManager : public OsAgnosticMemoryManager {
|
|
|
|
public:
|
2019-03-15 17:22:35 +08:00
|
|
|
MockAllocSysMemAgnosticMemoryManager(ExecutionEnvironment &executionEnvironment) : OsAgnosticMemoryManager(executionEnvironment) {
|
2018-01-26 23:53:18 +08:00
|
|
|
ptrRestrictions = nullptr;
|
|
|
|
testRestrictions.minAddress = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AlignedMallocRestrictions *getAlignedMallocRestrictions() override {
|
|
|
|
return ptrRestrictions;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *allocateSystemMemory(size_t size, size_t alignment) override {
|
|
|
|
constexpr size_t minAlignment = 16;
|
|
|
|
alignment = std::max(alignment, minAlignment);
|
|
|
|
return alignedMalloc(size, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
AlignedMallocRestrictions testRestrictions;
|
|
|
|
AlignedMallocRestrictions *ptrRestrictions;
|
|
|
|
};
|
2018-10-19 19:41:42 +08:00
|
|
|
|
|
|
|
class FailMemoryManager : public MockMemoryManager {
|
|
|
|
public:
|
2019-03-12 19:00:41 +08:00
|
|
|
using MemoryManager::allocateGraphicsMemoryWithProperties;
|
2018-10-19 19:41:42 +08:00
|
|
|
using MockMemoryManager::MockMemoryManager;
|
2019-03-28 22:42:23 +08:00
|
|
|
FailMemoryManager(int32_t failedAllocationsCount, ExecutionEnvironment &executionEnvironment);
|
2019-03-06 23:35:21 +08:00
|
|
|
FailMemoryManager(int32_t failedAllocationsCount, ExecutionEnvironment &executionEnvironment, bool localMemory);
|
2018-12-06 22:03:06 +08:00
|
|
|
|
2018-11-30 18:01:33 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override {
|
2018-12-06 22:03:06 +08:00
|
|
|
if (failedAllocationsCount <= 0) {
|
2018-10-19 19:41:42 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-06 22:03:06 +08:00
|
|
|
failedAllocationsCount--;
|
|
|
|
return OsAgnosticMemoryManager::allocateGraphicsMemoryWithAlignment(allocationData);
|
2019-03-12 19:00:41 +08:00
|
|
|
}
|
|
|
|
GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(const AllocationData &allocationData) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-02-28 21:12:13 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemory64kb(const AllocationData &allocationData) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return nullptr;
|
2019-03-12 19:00:41 +08:00
|
|
|
}
|
|
|
|
GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties, const void *ptr) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return nullptr;
|
2019-03-12 19:00:41 +08:00
|
|
|
}
|
2018-12-21 17:16:27 +08:00
|
|
|
GraphicsAllocation *allocate32BitGraphicsMemoryImpl(const AllocationData &allocationData) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return nullptr;
|
2019-03-12 19:00:41 +08:00
|
|
|
}
|
2019-04-01 20:04:50 +08:00
|
|
|
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return nullptr;
|
2019-03-12 19:00:41 +08:00
|
|
|
}
|
2019-10-22 16:26:23 +08:00
|
|
|
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return nullptr;
|
2019-03-12 19:00:41 +08:00
|
|
|
}
|
2018-12-14 18:24:45 +08:00
|
|
|
|
2019-01-24 18:51:33 +08:00
|
|
|
void *lockResourceImpl(GraphicsAllocation &gfxAllocation) override { return nullptr; };
|
|
|
|
void unlockResourceImpl(GraphicsAllocation &gfxAllocation) override{};
|
2018-10-19 19:41:42 +08:00
|
|
|
|
2020-01-07 14:42:40 +08:00
|
|
|
MemoryManager::AllocationStatus populateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return AllocationStatus::Error;
|
|
|
|
};
|
2019-11-15 16:59:48 +08:00
|
|
|
void cleanOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override{};
|
2018-10-19 19:41:42 +08:00
|
|
|
|
2020-01-07 14:42:40 +08:00
|
|
|
uint64_t getSystemSharedMemory(uint32_t rootDeviceIndex) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2019-02-25 21:11:34 +08:00
|
|
|
GraphicsAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, const AllocationData &allocationData) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return nullptr;
|
|
|
|
};
|
2019-01-22 19:40:17 +08:00
|
|
|
GraphicsAllocation *allocateGraphicsMemoryForImage(const AllocationData &allocationData) override {
|
2018-10-19 19:41:42 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-14 17:08:59 +08:00
|
|
|
GraphicsAllocation *allocateShareableMemory(const AllocationData &allocationData) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-06 22:03:06 +08:00
|
|
|
int32_t failedAllocationsCount = 0;
|
2018-10-19 19:41:42 +08:00
|
|
|
};
|
2018-12-20 23:58:15 +08:00
|
|
|
|
|
|
|
class GMockMemoryManagerFailFirstAllocation : public MockMemoryManager {
|
|
|
|
public:
|
|
|
|
GMockMemoryManagerFailFirstAllocation(bool enableLocalMemory, const ExecutionEnvironment &executionEnvironment) : MockMemoryManager(enableLocalMemory, const_cast<ExecutionEnvironment &>(executionEnvironment)){};
|
|
|
|
GMockMemoryManagerFailFirstAllocation(const ExecutionEnvironment &executionEnvironment) : GMockMemoryManagerFailFirstAllocation(false, executionEnvironment){};
|
|
|
|
|
|
|
|
MOCK_METHOD2(allocateGraphicsMemoryInDevicePool, GraphicsAllocation *(const AllocationData &, AllocationStatus &));
|
|
|
|
GraphicsAllocation *baseAllocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) {
|
|
|
|
return OsAgnosticMemoryManager::allocateGraphicsMemoryInDevicePool(allocationData, status);
|
|
|
|
}
|
|
|
|
GraphicsAllocation *allocateNonSystemGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) {
|
|
|
|
auto allocation = baseAllocateGraphicsMemoryInDevicePool(allocationData, status);
|
|
|
|
if (!allocation) {
|
|
|
|
allocation = allocateGraphicsMemory(allocationData);
|
|
|
|
}
|
|
|
|
static_cast<MemoryAllocation *>(allocation)->overrideMemoryPool(MemoryPool::SystemCpuInaccessible);
|
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|