2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2018-09-17 20:03:37 +08:00
|
|
|
* Copyright (C) 2017-2018 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-17 20:03:37 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-01-22 23:43:26 +08:00
|
|
|
#include "runtime/memory_manager/memory_manager.h"
|
2018-07-23 01:27:33 +08:00
|
|
|
#include "runtime/command_stream/command_stream_receiver.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/event/event.h"
|
2018-07-23 01:27:33 +08:00
|
|
|
#include "runtime/event/hw_timestamps.h"
|
|
|
|
#include "runtime/event/perf_counter.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/helpers/aligned_memory.h"
|
|
|
|
#include "runtime/helpers/basic_math.h"
|
2018-09-26 06:44:43 +08:00
|
|
|
#include "runtime/helpers/kernel_commands.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/helpers/options.h"
|
2018-07-23 01:27:33 +08:00
|
|
|
#include "runtime/memory_manager/deferred_deleter.h"
|
2018-10-24 14:46:54 +08:00
|
|
|
#include "runtime/memory_manager/host_ptr_manager.h"
|
2018-10-22 21:17:32 +08:00
|
|
|
#include "runtime/memory_manager/internal_allocation_storage.h"
|
2018-12-14 18:24:45 +08:00
|
|
|
#include "runtime/mem_obj/image.h"
|
2018-09-06 15:01:13 +08:00
|
|
|
#include "runtime/os_interface/os_context.h"
|
2018-11-21 16:57:51 +08:00
|
|
|
#include "runtime/os_interface/os_interface.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/utilities/stackvec.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace OCLRT {
|
2018-10-01 22:10:54 +08:00
|
|
|
MemoryManager::MemoryManager(bool enable64kbpages, bool enableLocalMemory,
|
|
|
|
ExecutionEnvironment &executionEnvironment) : allocator32Bit(nullptr), enable64kbpages(enable64kbpages),
|
|
|
|
localMemorySupported(enableLocalMemory),
|
2018-10-24 14:46:54 +08:00
|
|
|
executionEnvironment(executionEnvironment),
|
2018-11-07 20:04:10 +08:00
|
|
|
hostPtrManager(std::make_unique<HostPtrManager>()) {
|
|
|
|
registeredOsContexts.resize(1);
|
|
|
|
};
|
2018-09-06 16:53:35 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
MemoryManager::~MemoryManager() {
|
2018-09-06 15:01:13 +08:00
|
|
|
for (auto osContext : registeredOsContexts) {
|
2018-11-07 20:04:10 +08:00
|
|
|
if (osContext) {
|
|
|
|
osContext->decRefInternal();
|
|
|
|
}
|
2018-09-06 15:01:13 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *MemoryManager::allocateSystemMemory(size_t size, size_t alignment) {
|
|
|
|
// Establish a minimum alignment of 16bytes.
|
2018-01-22 23:43:26 +08:00
|
|
|
constexpr size_t minAlignment = 16;
|
|
|
|
alignment = std::max(alignment, minAlignment);
|
|
|
|
auto restrictions = getAlignedMallocRestrictions();
|
|
|
|
void *ptr = nullptr;
|
|
|
|
|
|
|
|
ptr = alignedMallocWrapper(size, alignment);
|
|
|
|
if (restrictions == nullptr) {
|
|
|
|
return ptr;
|
|
|
|
} else if (restrictions->minAddress == 0) {
|
|
|
|
return ptr;
|
|
|
|
} else {
|
|
|
|
if (restrictions->minAddress > reinterpret_cast<uintptr_t>(ptr) && ptr != nullptr) {
|
|
|
|
StackVec<void *, 100> invalidMemVector;
|
|
|
|
invalidMemVector.push_back(ptr);
|
|
|
|
do {
|
|
|
|
ptr = alignedMallocWrapper(size, alignment);
|
|
|
|
if (restrictions->minAddress > reinterpret_cast<uintptr_t>(ptr) && ptr != nullptr) {
|
|
|
|
invalidMemVector.push_back(ptr);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
for (auto &it : invalidMemVector) {
|
|
|
|
alignedFreeWrapper(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2018-11-30 18:01:33 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (deferredDeleter) {
|
|
|
|
deferredDeleter->drain(true);
|
|
|
|
}
|
2018-10-23 17:42:40 +08:00
|
|
|
GraphicsAllocation *graphicsAllocation = nullptr;
|
2018-11-30 18:01:33 +08:00
|
|
|
auto osStorage = hostPtrManager->prepareOsStorageForAllocation(*this, allocationData.size, allocationData.hostPtr);
|
2018-10-23 17:42:40 +08:00
|
|
|
if (osStorage.fragmentCount > 0) {
|
2018-11-30 18:01:33 +08:00
|
|
|
graphicsAllocation = createGraphicsAllocation(osStorage, allocationData.size, allocationData.hostPtr);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
return graphicsAllocation;
|
|
|
|
}
|
|
|
|
|
2018-12-14 18:24:45 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImageFromHostPtr(ImageInfo &imgInfo, const void *hostPtr) {
|
|
|
|
bool copyRequired = Image::isCopyRequired(imgInfo, hostPtr);
|
|
|
|
|
|
|
|
if (hostPtr && !copyRequired) {
|
2018-12-17 18:49:17 +08:00
|
|
|
return allocateGraphicsMemory({false, imgInfo.size, GraphicsAllocation::AllocationType::UNDECIDED}, hostPtr);
|
2018-12-14 18:24:45 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
void MemoryManager::cleanGraphicsMemoryCreatedFromHostPtr(GraphicsAllocation *graphicsAllocation) {
|
2018-10-24 14:46:54 +08:00
|
|
|
hostPtrManager->releaseHandleStorage(graphicsAllocation->fragmentsStorage);
|
2017-12-21 07:45:38 +08:00
|
|
|
cleanOsHandles(graphicsAllocation->fragmentsStorage);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *MemoryManager::createGraphicsAllocationWithPadding(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) {
|
|
|
|
if (!paddingAllocation) {
|
2018-12-12 01:56:37 +08:00
|
|
|
paddingAllocation = allocateGraphicsMemoryWithProperties({paddingBufferSize, GraphicsAllocation::AllocationType::UNDECIDED});
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
return createPaddedAllocation(inputGraphicsAllocation, sizeWithPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *MemoryManager::createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) {
|
2018-12-12 01:56:37 +08:00
|
|
|
return allocateGraphicsMemoryWithProperties({sizeWithPadding, GraphicsAllocation::AllocationType::UNDECIDED});
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::freeSystemMemory(void *ptr) {
|
|
|
|
::alignedFree(ptr);
|
|
|
|
}
|
|
|
|
|
2018-02-28 16:27:38 +08:00
|
|
|
void MemoryManager::setForce32BitAllocations(bool newValue) {
|
|
|
|
if (newValue && !this->allocator32Bit) {
|
|
|
|
this->allocator32Bit.reset(new Allocator32bit);
|
|
|
|
}
|
|
|
|
force32bitAllocations = newValue;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
void MemoryManager::applyCommonCleanup() {
|
|
|
|
if (this->paddingAllocation) {
|
|
|
|
this->freeGraphicsMemory(this->paddingAllocation);
|
|
|
|
}
|
2018-08-24 14:48:59 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation) {
|
|
|
|
freeGraphicsMemoryImpl(gfxAllocation);
|
|
|
|
}
|
2018-01-19 17:55:36 +08:00
|
|
|
//if not in use destroy in place
|
|
|
|
//if in use pass to temporary allocation list that is cleaned on blocking calls
|
|
|
|
void MemoryManager::checkGpuUsageAndDestroyGraphicsAllocations(GraphicsAllocation *gfxAllocation) {
|
2018-12-10 20:17:52 +08:00
|
|
|
if (!gfxAllocation->isUsed()) {
|
2018-01-19 17:55:36 +08:00
|
|
|
freeGraphicsMemory(gfxAllocation);
|
2018-12-10 20:17:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &csr : getCommandStreamReceivers()[0]) {
|
|
|
|
if (csr) {
|
|
|
|
auto osContextId = csr->getOsContext().getContextId();
|
|
|
|
auto allocationTaskCount = gfxAllocation->getTaskCount(osContextId);
|
|
|
|
|
|
|
|
if (gfxAllocation->isUsedByContext(osContextId) &&
|
|
|
|
allocationTaskCount > *csr->getTagAddress()) {
|
|
|
|
csr->getInternalAllocationStorage()->storeAllocation(std::unique_ptr<GraphicsAllocation>(gfxAllocation), TEMPORARY_ALLOCATION);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 17:55:36 +08:00
|
|
|
}
|
2018-12-10 20:17:52 +08:00
|
|
|
freeGraphicsMemory(gfxAllocation);
|
2018-01-19 17:55:36 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
void MemoryManager::waitForDeletions() {
|
|
|
|
if (deferredDeleter) {
|
|
|
|
deferredDeleter->drain(false);
|
|
|
|
}
|
|
|
|
deferredDeleter.reset(nullptr);
|
|
|
|
}
|
|
|
|
bool MemoryManager::isAsyncDeleterEnabled() const {
|
|
|
|
return asyncDeleterEnabled;
|
|
|
|
}
|
|
|
|
|
2018-10-12 21:20:02 +08:00
|
|
|
bool MemoryManager::isLocalMemorySupported() const {
|
|
|
|
return localMemorySupported;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
bool MemoryManager::isMemoryBudgetExhausted() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:30:39 +08:00
|
|
|
OsContext *MemoryManager::createAndRegisterOsContext(EngineInstanceT engineType, PreemptionMode preemptionMode) {
|
2018-11-21 16:57:51 +08:00
|
|
|
auto contextId = ++latestContextId;
|
2018-09-12 13:47:55 +08:00
|
|
|
if (contextId + 1 > registeredOsContexts.size()) {
|
|
|
|
registeredOsContexts.resize(contextId + 1);
|
|
|
|
}
|
2018-12-10 17:30:39 +08:00
|
|
|
auto osContext = new OsContext(executionEnvironment.osInterface.get(), contextId, engineType, preemptionMode);
|
2018-11-21 16:57:51 +08:00
|
|
|
osContext->incRefInternal();
|
|
|
|
registeredOsContexts[contextId] = osContext;
|
|
|
|
|
|
|
|
return osContext;
|
2018-09-06 15:01:13 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 22:03:06 +08:00
|
|
|
bool MemoryManager::getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const DevicesBitfield devicesBitfield,
|
2018-12-12 01:56:37 +08:00
|
|
|
const void *hostPtr) {
|
2018-12-06 22:03:06 +08:00
|
|
|
UNRECOVERABLE_IF(hostPtr == nullptr && !properties.flags.allocateMemory);
|
2018-07-09 20:12:32 +08:00
|
|
|
|
|
|
|
bool allow64KbPages = false;
|
|
|
|
bool allow32Bit = false;
|
2018-12-06 22:03:06 +08:00
|
|
|
bool forcePin = properties.flags.forcePin;
|
|
|
|
bool uncacheable = properties.flags.uncacheable;
|
2018-07-25 00:36:26 +08:00
|
|
|
bool mustBeZeroCopy = false;
|
2018-12-20 15:43:57 +08:00
|
|
|
bool shareable = properties.flags.shareable;
|
2018-07-09 20:12:32 +08:00
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
switch (properties.allocationType) {
|
2018-07-09 20:12:32 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER:
|
2018-07-25 00:36:26 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY:
|
2018-07-23 01:27:33 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_COMPRESSED:
|
2018-07-09 20:12:32 +08:00
|
|
|
case GraphicsAllocation::AllocationType::PIPE:
|
|
|
|
case GraphicsAllocation::AllocationType::SCRATCH_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::PRIVATE_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::PRINTF_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::CONSTANT_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::GLOBAL_SURFACE:
|
2018-12-06 22:03:06 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SVM:
|
2018-07-09 20:12:32 +08:00
|
|
|
allow64KbPages = true;
|
|
|
|
allow32Bit = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
switch (properties.allocationType) {
|
2018-07-25 00:36:26 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER:
|
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY:
|
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_COMPRESSED:
|
|
|
|
forcePin = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
switch (properties.allocationType) {
|
2018-07-25 00:36:26 +08:00
|
|
|
case GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY:
|
|
|
|
case GraphicsAllocation::AllocationType::PIPE:
|
|
|
|
case GraphicsAllocation::AllocationType::PRINTF_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::CONSTANT_SURFACE:
|
|
|
|
case GraphicsAllocation::AllocationType::GLOBAL_SURFACE:
|
2018-12-13 17:18:34 +08:00
|
|
|
case GraphicsAllocation::AllocationType::SVM:
|
2018-07-25 00:36:26 +08:00
|
|
|
mustBeZeroCopy = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
switch (properties.allocationType) {
|
|
|
|
case GraphicsAllocation::AllocationType::UNDECIDED:
|
|
|
|
case GraphicsAllocation::AllocationType::LINEAR_STREAM:
|
|
|
|
case GraphicsAllocation::AllocationType::FILL_PATTERN:
|
2018-12-18 15:54:44 +08:00
|
|
|
case GraphicsAllocation::AllocationType::TIMESTAMP_TAG_BUFFER:
|
2018-12-12 01:56:37 +08:00
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:12:32 +08:00
|
|
|
allocationData.flags.mustBeZeroCopy = mustBeZeroCopy;
|
2018-12-06 22:03:06 +08:00
|
|
|
allocationData.flags.allocateMemory = properties.flags.allocateMemory;
|
2018-07-09 20:12:32 +08:00
|
|
|
allocationData.flags.allow32Bit = allow32Bit;
|
|
|
|
allocationData.flags.allow64kbPages = allow64KbPages;
|
|
|
|
allocationData.flags.forcePin = forcePin;
|
|
|
|
allocationData.flags.uncacheable = uncacheable;
|
2018-12-06 22:03:06 +08:00
|
|
|
allocationData.flags.flushL3 = properties.flags.flushL3RequiredForRead | properties.flags.flushL3RequiredForWrite;
|
2018-12-12 01:56:37 +08:00
|
|
|
allocationData.flags.preferRenderCompressed = GraphicsAllocation::AllocationType::BUFFER_COMPRESSED == properties.allocationType;
|
2018-12-20 15:43:57 +08:00
|
|
|
allocationData.flags.shareable = shareable;
|
2018-07-09 20:12:32 +08:00
|
|
|
|
|
|
|
if (allocationData.flags.mustBeZeroCopy) {
|
|
|
|
allocationData.flags.useSystemMemory = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
allocationData.hostPtr = hostPtr;
|
2018-12-06 22:03:06 +08:00
|
|
|
allocationData.size = properties.size;
|
2018-12-12 01:56:37 +08:00
|
|
|
allocationData.type = properties.allocationType;
|
2018-09-23 21:47:27 +08:00
|
|
|
allocationData.devicesBitfield = devicesBitfield;
|
2018-12-06 22:03:06 +08:00
|
|
|
allocationData.alignment = properties.alignment ? properties.alignment : MemoryConstants::preferredAlignment;
|
2018-07-09 20:12:32 +08:00
|
|
|
|
|
|
|
if (allocationData.flags.allocateMemory) {
|
|
|
|
allocationData.hostPtr = nullptr;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryInPreferredPool(AllocationProperties properties, DevicesBitfield devicesBitfield, const void *hostPtr) {
|
2018-07-09 20:12:32 +08:00
|
|
|
AllocationData allocationData;
|
2018-07-18 15:48:21 +08:00
|
|
|
AllocationStatus status = AllocationStatus::Error;
|
|
|
|
|
2018-12-12 01:56:37 +08:00
|
|
|
getAllocationData(allocationData, properties, devicesBitfield, hostPtr);
|
2018-07-09 20:12:32 +08:00
|
|
|
UNRECOVERABLE_IF(allocationData.type == GraphicsAllocation::AllocationType::IMAGE || allocationData.type == GraphicsAllocation::AllocationType::SHARED_RESOURCE);
|
2018-07-18 15:48:21 +08:00
|
|
|
GraphicsAllocation *allocation = nullptr;
|
2018-07-09 20:12:32 +08:00
|
|
|
|
2018-07-18 15:48:21 +08:00
|
|
|
allocation = allocateGraphicsMemoryInDevicePool(allocationData, status);
|
|
|
|
if (!allocation && status == AllocationStatus::RetryInNonDevicePool) {
|
|
|
|
allocation = allocateGraphicsMemory(allocationData);
|
|
|
|
}
|
2018-12-12 01:56:37 +08:00
|
|
|
if (allocation) {
|
|
|
|
allocation->setAllocationType(properties.allocationType);
|
|
|
|
}
|
2018-07-18 15:48:21 +08:00
|
|
|
return allocation;
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *MemoryManager::allocateGraphicsMemory(const AllocationData &allocationData) {
|
|
|
|
if (force32bitAllocations && allocationData.flags.allow32Bit && is64bit) {
|
|
|
|
return allocate32BitGraphicsMemory(allocationData.size, allocationData.hostPtr, AllocationOrigin::EXTERNAL_ALLOCATION);
|
|
|
|
}
|
|
|
|
if (allocationData.hostPtr) {
|
2018-11-30 18:01:33 +08:00
|
|
|
return allocateGraphicsMemoryWithHostPtr(allocationData);
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
2018-07-11 15:45:20 +08:00
|
|
|
if (peek64kbPagesEnabled() && allocationData.flags.allow64kbPages) {
|
2018-12-06 22:03:06 +08:00
|
|
|
return allocateGraphicsMemory64kb(allocationData);
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
2018-11-30 18:01:33 +08:00
|
|
|
return allocateGraphicsMemoryWithAlignment(allocationData);
|
2018-07-09 20:12:32 +08:00
|
|
|
}
|
2018-11-29 18:39:10 +08:00
|
|
|
|
|
|
|
const CsrContainer &MemoryManager::getCommandStreamReceivers() const {
|
|
|
|
return executionEnvironment.commandStreamReceivers;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandStreamReceiver *MemoryManager::getDefaultCommandStreamReceiver(uint32_t deviceId) const {
|
|
|
|
return executionEnvironment.commandStreamReceivers[deviceId][defaultEngineIndex].get();
|
2018-10-01 22:10:54 +08:00
|
|
|
}
|
2018-07-09 20:12:32 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
} // namespace OCLRT
|