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 135 136 137 138 139 140 141 142 143 144 145
|
/*
* Copyright (C) 2020 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/linux/drm_memory_operations_handler_bind.h"
#include "shared/source/command_stream/command_stream_receiver.h"
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/device/device.h"
#include "shared/source/os_interface/linux/drm_allocation.h"
#include "shared/source/os_interface/linux/drm_buffer_object.h"
#include "shared/source/os_interface/linux/drm_memory_manager.h"
#include "shared/source/os_interface/linux/drm_neo.h"
#include "shared/source/os_interface/linux/os_context_linux.h"
#include "shared/source/os_interface/linux/os_interface.h"
namespace NEO {
DrmMemoryOperationsHandlerBind::DrmMemoryOperationsHandlerBind(RootDeviceEnvironment &rootDeviceEnvironment, uint32_t rootDeviceIndex)
: rootDeviceEnvironment(rootDeviceEnvironment),
rootDeviceIndex(rootDeviceIndex){};
DrmMemoryOperationsHandlerBind::~DrmMemoryOperationsHandlerBind() = default;
MemoryOperationsStatus DrmMemoryOperationsHandlerBind::makeResident(Device *device, ArrayRef<GraphicsAllocation *> gfxAllocations) {
auto engines = device->getEngines();
for (const auto &engine : engines) {
this->makeResidentWithinOsContext(engine.osContext, gfxAllocations, false);
}
return MemoryOperationsStatus::SUCCESS;
}
MemoryOperationsStatus DrmMemoryOperationsHandlerBind::makeResidentWithinOsContext(OsContext *osContext, ArrayRef<GraphicsAllocation *> gfxAllocations, bool evictable) {
std::lock_guard<std::mutex> lock(mutex);
for (auto gfxAllocation = gfxAllocations.begin(); gfxAllocation != gfxAllocations.end(); gfxAllocation++) {
auto drmAllocation = static_cast<DrmAllocation *>(*gfxAllocation);
for (auto drmIterator = 0u; drmIterator < osContext->getDeviceBitfield().size(); drmIterator++) {
if (osContext->getDeviceBitfield().test(drmIterator)) {
drmAllocation->makeBOsResident(osContext, drmIterator, nullptr, true);
}
}
if (!evictable) {
drmAllocation->updateResidencyTaskCount(GraphicsAllocation::objectAlwaysResident, osContext->getContextId());
}
}
return MemoryOperationsStatus::SUCCESS;
}
MemoryOperationsStatus DrmMemoryOperationsHandlerBind::evict(Device *device, GraphicsAllocation &gfxAllocation) {
auto engines = device->getEngines();
auto retVal = MemoryOperationsStatus::SUCCESS;
for (const auto &engine : engines) {
retVal = this->evictWithinOsContext(engine.osContext, gfxAllocation);
if (retVal != MemoryOperationsStatus::SUCCESS) {
return retVal;
}
}
return MemoryOperationsStatus::SUCCESS;
}
MemoryOperationsStatus DrmMemoryOperationsHandlerBind::evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) {
std::lock_guard<std::mutex> lock(mutex);
evictImpl(osContext, gfxAllocation, osContext->getDeviceBitfield());
return MemoryOperationsStatus::SUCCESS;
}
void DrmMemoryOperationsHandlerBind::evictImpl(OsContext *osContext, GraphicsAllocation &gfxAllocation, DeviceBitfield deviceBitfield) {
auto drmAllocation = static_cast<DrmAllocation *>(&gfxAllocation);
for (auto drmIterator = 0u; drmIterator < deviceBitfield.size(); drmIterator++) {
if (deviceBitfield.test(drmIterator)) {
drmAllocation->makeBOsResident(osContext, drmIterator, nullptr, false);
}
}
drmAllocation->updateResidencyTaskCount(GraphicsAllocation::objectNotResident, osContext->getContextId());
}
MemoryOperationsStatus DrmMemoryOperationsHandlerBind::isResident(Device *device, GraphicsAllocation &gfxAllocation) {
std::lock_guard<std::mutex> lock(mutex);
bool isResident = true;
auto engines = device->getEngines();
for (const auto &engine : engines) {
isResident &= gfxAllocation.isAlwaysResident(engine.osContext->getContextId());
}
if (isResident) {
return MemoryOperationsStatus::SUCCESS;
}
return MemoryOperationsStatus::MEMORY_NOT_FOUND;
}
void DrmMemoryOperationsHandlerBind::mergeWithResidencyContainer(OsContext *osContext, ResidencyContainer &residencyContainer) {
this->makeResidentWithinOsContext(osContext, ArrayRef<GraphicsAllocation *>(residencyContainer), true);
residencyContainer.clear();
}
std::unique_lock<std::mutex> DrmMemoryOperationsHandlerBind::lockHandlerForExecWA() {
return std::unique_lock<std::mutex>();
}
void DrmMemoryOperationsHandlerBind::evictUnusedAllocations() {
auto memoryManager = static_cast<DrmMemoryManager *>(this->rootDeviceEnvironment.executionEnvironment.memoryManager.get());
std::lock_guard<std::mutex> lock(mutex);
auto allocLock = memoryManager->acquireAllocLock();
this->evictUnusedAllocationsImpl(memoryManager->getSysMemAllocs());
this->evictUnusedAllocationsImpl(memoryManager->getLocalMemAllocs(this->rootDeviceIndex));
}
void DrmMemoryOperationsHandlerBind::evictUnusedAllocationsImpl(std::vector<GraphicsAllocation *> &allocationsForEviction) {
const auto &engines = this->rootDeviceEnvironment.executionEnvironment.memoryManager->getRegisteredEngines();
std::vector<GraphicsAllocation *> evictCandidates;
for (auto subdeviceIndex = 0u; subdeviceIndex < HwHelper::getSubDevicesCount(rootDeviceEnvironment.getHardwareInfo()); subdeviceIndex++) {
for (auto &allocation : allocationsForEviction) {
bool evict = true;
for (const auto &engine : engines) {
if (this->rootDeviceIndex == engine.commandStreamReceiver->getRootDeviceIndex() &&
engine.osContext->getDeviceBitfield().test(subdeviceIndex)) {
evict &= allocation->isResidencyTaskCountBelow(*engine.commandStreamReceiver->getTagAddress(), engine.osContext->getContextId());
}
}
if (evict) {
evictCandidates.push_back(allocation);
}
}
for (auto &allocationToEvict : evictCandidates) {
for (const auto &engine : engines) {
if (this->rootDeviceIndex == engine.commandStreamReceiver->getRootDeviceIndex() &&
engine.osContext->getDeviceBitfield().test(subdeviceIndex)) {
DeviceBitfield deviceBitfield;
deviceBitfield.set(subdeviceIndex);
this->evictImpl(engine.osContext, *allocationToEvict, deviceBitfield);
}
}
}
evictCandidates.clear();
}
}
} // namespace NEO
|