2018-10-22 21:17:32 +08:00
|
|
|
/*
|
2022-02-04 21:59:01 +08:00
|
|
|
* Copyright (C) 2018-2022 Intel Corporation
|
2018-10-22 21:17:32 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/memory_manager/internal_allocation_storage.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/memory_manager/host_ptr_manager.h"
|
|
|
|
#include "shared/source/os_interface/os_context.h"
|
2018-10-22 21:17:32 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2020-06-10 17:49:34 +08:00
|
|
|
|
|
|
|
InternalAllocationStorage::InternalAllocationStorage(CommandStreamReceiver &commandStreamReceiver)
|
|
|
|
: commandStreamReceiver(commandStreamReceiver),
|
|
|
|
temporaryAllocations(TEMPORARY_ALLOCATION),
|
|
|
|
allocationsForReuse(REUSABLE_ALLOCATION){};
|
|
|
|
|
2021-10-21 20:37:31 +08:00
|
|
|
void InternalAllocationStorage::storeAllocation(std::unique_ptr<GraphicsAllocation> &&gfxAllocation, uint32_t allocationUsage) {
|
2018-12-03 17:05:36 +08:00
|
|
|
uint32_t taskCount = gfxAllocation->getTaskCount(commandStreamReceiver.getOsContext().getContextId());
|
2018-10-22 21:17:32 +08:00
|
|
|
|
|
|
|
if (allocationUsage == REUSABLE_ALLOCATION) {
|
|
|
|
taskCount = commandStreamReceiver.peekTaskCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
storeAllocationWithTaskCount(std::move(gfxAllocation), allocationUsage, taskCount);
|
|
|
|
}
|
2021-10-21 20:37:31 +08:00
|
|
|
void InternalAllocationStorage::storeAllocationWithTaskCount(std::unique_ptr<GraphicsAllocation> &&gfxAllocation, uint32_t allocationUsage, uint32_t taskCount) {
|
2018-10-24 20:25:04 +08:00
|
|
|
if (allocationUsage == REUSABLE_ALLOCATION) {
|
|
|
|
if (DebugManager.flags.DisableResourceRecycling.get()) {
|
2018-10-22 21:17:32 +08:00
|
|
|
commandStreamReceiver.getMemoryManager()->freeGraphicsMemory(gfxAllocation.release());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-10-29 20:26:58 +08:00
|
|
|
auto &allocationsList = (allocationUsage == TEMPORARY_ALLOCATION) ? temporaryAllocations : allocationsForReuse;
|
2018-12-03 17:05:36 +08:00
|
|
|
gfxAllocation->updateTaskCount(taskCount, commandStreamReceiver.getOsContext().getContextId());
|
2018-10-22 21:17:32 +08:00
|
|
|
allocationsList.pushTailOne(*gfxAllocation.release());
|
|
|
|
}
|
|
|
|
|
2018-10-24 20:25:04 +08:00
|
|
|
void InternalAllocationStorage::cleanAllocationList(uint32_t waitTaskCount, uint32_t allocationUsage) {
|
2018-10-29 20:26:58 +08:00
|
|
|
freeAllocationsList(waitTaskCount, (allocationUsage == TEMPORARY_ALLOCATION) ? temporaryAllocations : allocationsForReuse);
|
2018-10-22 21:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InternalAllocationStorage::freeAllocationsList(uint32_t waitTaskCount, AllocationsList &allocationsList) {
|
|
|
|
auto memoryManager = commandStreamReceiver.getMemoryManager();
|
2019-09-25 20:09:32 +08:00
|
|
|
auto lock = memoryManager->getHostPtrManager()->obtainOwnership();
|
|
|
|
|
2018-10-22 21:17:32 +08:00
|
|
|
GraphicsAllocation *curr = allocationsList.detachNodes();
|
|
|
|
|
|
|
|
IDList<GraphicsAllocation, false, true> allocationsLeft;
|
|
|
|
while (curr != nullptr) {
|
|
|
|
auto *next = curr->next;
|
2018-12-03 17:05:36 +08:00
|
|
|
if (curr->getTaskCount(commandStreamReceiver.getOsContext().getContextId()) <= waitTaskCount) {
|
2018-10-22 21:17:32 +08:00
|
|
|
memoryManager->freeGraphicsMemory(curr);
|
|
|
|
} else {
|
|
|
|
allocationsLeft.pushTailOne(*curr);
|
|
|
|
}
|
|
|
|
curr = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allocationsLeft.peekIsEmpty() == false) {
|
|
|
|
allocationsList.splice(*allocationsLeft.detachNodes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 21:59:01 +08:00
|
|
|
std::unique_ptr<GraphicsAllocation> InternalAllocationStorage::obtainReusableAllocation(size_t requiredSize, AllocationType allocationType) {
|
2021-08-16 23:31:09 +08:00
|
|
|
auto allocation = allocationsForReuse.detachAllocation(requiredSize, nullptr, &commandStreamReceiver, allocationType);
|
2020-04-15 19:39:09 +08:00
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
|
2022-02-04 21:59:01 +08:00
|
|
|
std::unique_ptr<GraphicsAllocation> InternalAllocationStorage::obtainTemporaryAllocationWithPtr(size_t requiredSize, const void *requiredPtr, AllocationType allocationType) {
|
2021-08-16 23:31:09 +08:00
|
|
|
auto allocation = temporaryAllocations.detachAllocation(requiredSize, requiredPtr, &commandStreamReceiver, allocationType);
|
2018-10-22 21:17:32 +08:00
|
|
|
return allocation;
|
|
|
|
}
|
|
|
|
|
2020-06-23 17:30:01 +08:00
|
|
|
DeviceBitfield InternalAllocationStorage::getDeviceBitfield() const {
|
|
|
|
return commandStreamReceiver.getOsContext().getDeviceBitfield();
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|