2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2021-05-17 02:51:16 +08:00
|
|
|
* Copyright (C) 2018-2021 Intel Corporation
|
2018-09-18 15:11:08 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2020-02-23 22:20:22 +08:00
|
|
|
#include "opencl/test/unit_test/mocks/mock_memory_manager.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/command_stream/command_stream_receiver.h"
|
|
|
|
#include "shared/source/gmm_helper/gmm.h"
|
|
|
|
#include "shared/source/memory_manager/deferred_deleter.h"
|
2020-02-24 17:22:30 +08:00
|
|
|
|
2020-02-23 05:50:57 +08:00
|
|
|
#include "opencl/source/helpers/surface_formats.h"
|
2020-02-23 22:20:22 +08:00
|
|
|
#include "opencl/test/unit_test/mocks/mock_allocation_properties.h"
|
2020-02-22 16:28:27 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <cstring>
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
void MockMemoryManager::setDeferredDeleter(DeferredDeleter *deleter) {
|
|
|
|
deferredDeleter.reset(deleter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MockMemoryManager::overrideAsyncDeleterFlag(bool newValue) {
|
|
|
|
asyncDeleterEnabled = newValue;
|
|
|
|
if (asyncDeleterEnabled && deferredDeleter == nullptr) {
|
|
|
|
deferredDeleter = createDeferredDeleter();
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 18:24:45 +08:00
|
|
|
|
|
|
|
void *MockMemoryManager::allocateSystemMemory(size_t size, size_t alignment) {
|
2019-03-06 23:35:21 +08:00
|
|
|
if (failAllocateSystemMemory) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-08-11 18:36:04 +08:00
|
|
|
|
|
|
|
if (fakeBigAllocations && size > bigAllocation) {
|
|
|
|
size = MemoryConstants::pageSize64k;
|
|
|
|
}
|
|
|
|
|
2018-12-14 18:24:45 +08:00
|
|
|
return OsAgnosticMemoryManager::allocateSystemMemory(redundancyRatio * size, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemoryWithProperties(const AllocationProperties &properties) {
|
2021-02-25 16:38:48 +08:00
|
|
|
if (isMockHostMemoryManager) {
|
|
|
|
allocateGraphicsMemoryWithPropertiesCount++;
|
|
|
|
if (forceFailureInPrimaryAllocation) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return NEO::MemoryManager::allocateGraphicsMemoryWithProperties(properties);
|
|
|
|
}
|
|
|
|
|
2020-04-16 23:24:38 +08:00
|
|
|
recentlyPassedDeviceBitfield = properties.subDevicesBitfield;
|
2018-12-20 15:43:57 +08:00
|
|
|
AllocationProperties adjustedProperties(properties);
|
|
|
|
adjustedProperties.size = redundancyRatio * properties.size;
|
|
|
|
return OsAgnosticMemoryManager::allocateGraphicsMemoryWithProperties(adjustedProperties);
|
2018-12-14 18:24:45 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:38:48 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemoryWithProperties(const AllocationProperties &properties, const void *ptr) {
|
2021-06-16 22:41:26 +08:00
|
|
|
if (returnFakeAllocation) {
|
|
|
|
return new GraphicsAllocation(properties.rootDeviceIndex, properties.allocationType, reinterpret_cast<void *>(dummyAddress), reinterpret_cast<uint64_t>(ptr), properties.size, 0, MemoryPool::System4KBPages, maxOsContextCount);
|
|
|
|
}
|
2021-02-25 16:38:48 +08:00
|
|
|
if (isMockHostMemoryManager) {
|
|
|
|
allocateGraphicsMemoryWithPropertiesCount++;
|
|
|
|
if (forceFailureInAllocationWithHostPointer) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return NEO::MemoryManager::allocateGraphicsMemoryWithProperties(properties, ptr);
|
|
|
|
}
|
|
|
|
return OsAgnosticMemoryManager::allocateGraphicsMemoryWithProperties(properties, ptr);
|
|
|
|
}
|
|
|
|
|
2019-01-22 19:40:17 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemoryForImage(const AllocationData &allocationData) {
|
|
|
|
allocateForImageCalled = true;
|
|
|
|
auto *allocation = MemoryManager::allocateGraphicsMemoryForImage(allocationData);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (redundancyRatio != 1) {
|
2019-01-22 19:40:17 +08:00
|
|
|
memset((unsigned char *)allocation->getUnderlyingBuffer(), 0, allocationData.imgInfo->size * redundancyRatio);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
|
2021-08-11 18:36:04 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocateMemoryByKMD(const AllocationData &allocationData) {
|
2019-11-14 17:08:59 +08:00
|
|
|
allocateForShareableCalled = true;
|
2021-08-11 18:36:04 +08:00
|
|
|
return OsAgnosticMemoryManager::allocateMemoryByKMD(allocationData);
|
2019-11-14 17:08:59 +08:00
|
|
|
}
|
|
|
|
|
2019-02-28 21:12:13 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemory64kb(const AllocationData &allocationData) {
|
2018-07-24 22:42:35 +08:00
|
|
|
allocation64kbPageCreated = true;
|
2020-04-02 16:52:36 +08:00
|
|
|
preferRenderCompressedFlagPassed = forceRenderCompressed ? true : allocationData.flags.preferRenderCompressed;
|
2018-07-24 22:42:35 +08:00
|
|
|
|
2018-12-06 22:03:06 +08:00
|
|
|
auto allocation = OsAgnosticMemoryManager::allocateGraphicsMemory64kb(allocationData);
|
2018-07-24 15:06:33 +08:00
|
|
|
if (allocation) {
|
2021-06-09 00:37:54 +08:00
|
|
|
allocation->getDefaultGmm()->isCompressionEnabled = preferRenderCompressedFlagPassed;
|
2018-07-24 15:06:33 +08:00
|
|
|
}
|
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:42:35 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) {
|
|
|
|
if (failInDevicePool) {
|
|
|
|
status = AllocationStatus::RetryInNonDevicePool;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (failInDevicePoolWithError) {
|
|
|
|
status = AllocationStatus::Error;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-02-25 16:38:48 +08:00
|
|
|
if (successAllocatedGraphicsMemoryIndex >= maxSuccessAllocatedGraphicsMemoryIndex) {
|
|
|
|
return nullptr;
|
2018-07-24 22:42:35 +08:00
|
|
|
|
2021-02-25 16:38:48 +08:00
|
|
|
} else {
|
|
|
|
auto allocation = OsAgnosticMemoryManager::allocateGraphicsMemoryInDevicePool(allocationData, status);
|
|
|
|
if (allocation) {
|
|
|
|
allocationInDevicePoolCreated = true;
|
|
|
|
if (localMemorySupported[allocation->getRootDeviceIndex()]) {
|
|
|
|
static_cast<MemoryAllocation *>(allocation)->overrideMemoryPool(MemoryPool::LocalMemory);
|
|
|
|
}
|
2019-04-16 21:42:23 +08:00
|
|
|
}
|
2021-02-25 16:38:48 +08:00
|
|
|
successAllocatedGraphicsMemoryIndex++;
|
|
|
|
return allocation;
|
2018-07-24 22:42:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 18:01:33 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) {
|
2018-07-25 15:38:00 +08:00
|
|
|
if (failInAllocateWithSizeAndAlignment) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-24 22:42:35 +08:00
|
|
|
allocationCreated = true;
|
2021-02-19 19:45:53 +08:00
|
|
|
alignAllocationData = allocationData;
|
2018-11-30 18:01:33 +08:00
|
|
|
return OsAgnosticMemoryManager::allocateGraphicsMemoryWithAlignment(allocationData);
|
2018-07-24 22:42:35 +08:00
|
|
|
}
|
2019-02-21 23:29:05 +08:00
|
|
|
|
2020-03-25 22:15:03 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocate32BitGraphicsMemory(uint32_t rootDeviceIndex, size_t size, const void *ptr, GraphicsAllocation::AllocationType allocationType) {
|
2018-12-21 17:16:27 +08:00
|
|
|
bool allocateMemory = ptr == nullptr;
|
2020-03-25 22:15:03 +08:00
|
|
|
AllocationData allocationData{};
|
|
|
|
MockAllocationProperties properties(rootDeviceIndex, allocateMemory, size, allocationType);
|
2019-05-10 17:30:07 +08:00
|
|
|
getAllocationData(allocationData, properties, ptr, createStorageInfoFromProperties(properties));
|
2020-07-01 20:03:46 +08:00
|
|
|
bool useLocalMemory = !allocationData.flags.useSystemMemory && this->localMemorySupported[rootDeviceIndex];
|
|
|
|
return allocate32BitGraphicsMemoryImpl(allocationData, useLocalMemory);
|
2018-12-21 17:16:27 +08:00
|
|
|
}
|
2018-07-24 22:42:35 +08:00
|
|
|
|
2021-06-02 21:57:28 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(const AllocationData &allocationData) {
|
|
|
|
allocateGraphicsMemoryForNonSvmHostPtrCalled = true;
|
|
|
|
return OsAgnosticMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(allocationData);
|
|
|
|
}
|
|
|
|
|
2020-07-01 20:03:46 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::allocate32BitGraphicsMemoryImpl(const AllocationData &allocationData, bool useLocalMemory) {
|
2021-05-06 22:15:19 +08:00
|
|
|
allocate32BitGraphicsMemoryImplCalled = true;
|
2019-07-26 21:28:06 +08:00
|
|
|
if (failAllocate32Bit) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-07-01 20:03:46 +08:00
|
|
|
return OsAgnosticMemoryManager::allocate32BitGraphicsMemoryImpl(allocationData, useLocalMemory);
|
2019-07-26 21:28:06 +08:00
|
|
|
}
|
|
|
|
|
2021-04-23 01:01:19 +08:00
|
|
|
GraphicsAllocation *MockMemoryManager::createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) {
|
|
|
|
auto allocation = OsAgnosticMemoryManager::createGraphicsAllocationFromExistingStorage(properties, ptr, multiGraphicsAllocation);
|
|
|
|
createGraphicsAllocationFromExistingStorageCalled++;
|
|
|
|
allocationsFromExistingStorage.push_back(allocation);
|
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:42:23 +08:00
|
|
|
FailMemoryManager::FailMemoryManager(int32_t failedAllocationsCount, ExecutionEnvironment &executionEnvironment) : MockMemoryManager(executionEnvironment) {
|
2018-12-06 22:03:06 +08:00
|
|
|
this->failedAllocationsCount = failedAllocationsCount;
|
2018-10-19 19:41:42 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 23:35:21 +08:00
|
|
|
FailMemoryManager::FailMemoryManager(int32_t failedAllocationsCount, ExecutionEnvironment &executionEnvironment, bool enableLocalMemory)
|
|
|
|
: MockMemoryManager(enableLocalMemory, executionEnvironment) {
|
|
|
|
this->failedAllocationsCount = failedAllocationsCount;
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|