diff --git a/runtime/device/device.cpp b/runtime/device/device.cpp index 1d7a69d810..1e14b8e50a 100644 --- a/runtime/device/device.cpp +++ b/runtime/device/device.cpp @@ -120,9 +120,6 @@ Device::~Device() { alignedFree(this->slmWindowStartAddress); } - if (osContext) { - osContext->decRefInternal(); - } executionEnvironment->decRefInternal(); } @@ -132,10 +129,11 @@ bool Device::createDeviceImpl(const HardwareInfo *pHwInfo, Device &outDevice) { if (!executionEnvironment->initializeCommandStreamReceiver(pHwInfo)) { return false; } - outDevice.osContext = new OsContext(executionEnvironment->osInterface.get()); - outDevice.osContext->incRefInternal(); executionEnvironment->initializeMemoryManager(outDevice.getEnabled64kbPages()); + outDevice.osContext = new OsContext(executionEnvironment->osInterface.get()); + executionEnvironment->memoryManager->registerOsContext(outDevice.osContext); + CommandStreamReceiver *commandStreamReceiver = executionEnvironment->commandStreamReceiver.get(); if (!commandStreamReceiver->initializeTagAllocation()) { return false; diff --git a/runtime/memory_manager/memory_manager.cpp b/runtime/memory_manager/memory_manager.cpp index e833acf1d3..393aad4570 100644 --- a/runtime/memory_manager/memory_manager.cpp +++ b/runtime/memory_manager/memory_manager.cpp @@ -32,6 +32,7 @@ #include "runtime/helpers/options.h" #include "runtime/helpers/timestamp_packet.h" #include "runtime/memory_manager/deferred_deleter.h" +#include "runtime/os_interface/os_context.h" #include "runtime/utilities/stackvec.h" #include "runtime/utilities/tag_allocator.h" @@ -76,6 +77,9 @@ MemoryManager::MemoryManager(bool enable64kbpages) : allocator32Bit(nullptr), en MemoryManager::~MemoryManager() { freeAllocationsList(-1, graphicsAllocations); freeAllocationsList(-1, allocationsForReuse); + for (auto osContext : registeredOsContexts) { + osContext->decRefInternal(); + } } void *MemoryManager::allocateSystemMemory(size_t size, size_t alignment) { @@ -379,6 +383,11 @@ RequirementsStatus MemoryManager::checkAllocationsForOverlapping(AllocationRequi return status; } +void MemoryManager::registerOsContext(OsContext *contextToRegister) { + contextToRegister->incRefInternal(); + registeredOsContexts.push_back(contextToRegister); +} + bool MemoryManager::getAllocationData(AllocationData &allocationData, bool allocateMemory, const void *hostPtr, size_t size, GraphicsAllocation::AllocationType type) { UNRECOVERABLE_IF(hostPtr == nullptr && !allocateMemory); diff --git a/runtime/memory_manager/memory_manager.h b/runtime/memory_manager/memory_manager.h index 49e4f57af9..abbc611bc9 100644 --- a/runtime/memory_manager/memory_manager.h +++ b/runtime/memory_manager/memory_manager.h @@ -251,6 +251,9 @@ class MemoryManager { ::alignedFree(ptr); } + void registerOsContext(OsContext *contextToRegister); + size_t getOsContextCount() { return registeredOsContexts.size(); } + protected: static bool getAllocationData(AllocationData &allocationData, bool allocateMemory, const void *hostPtr, size_t size, GraphicsAllocation::AllocationType type); @@ -268,6 +271,7 @@ class MemoryManager { std::unique_ptr deferredDeleter; bool asyncDeleterEnabled = false; bool enable64kbpages = false; + std::vector registeredOsContexts; }; std::unique_ptr createDeferredDeleter(); diff --git a/unit_tests/device/device_tests.cpp b/unit_tests/device/device_tests.cpp index 5bfd35e0a4..07c56209bf 100644 --- a/unit_tests/device/device_tests.cpp +++ b/unit_tests/device/device_tests.cpp @@ -175,6 +175,12 @@ TEST(DeviceCreation, givenDefaultHwCsrInDebugVarsWhenDeviceIsCreatedThenIsSimula EXPECT_FALSE(device->isSimulation()); } +TEST(DeviceCreation, givenDeviceWhenItIsCreatedThenOsContextIsRegistredInMemoryManager) { + auto device = std::unique_ptr(MockDevice::createWithNewExecutionEnvironment(nullptr)); + auto memoryManager = device->getMemoryManager(); + EXPECT_EQ(1u, memoryManager->getOsContextCount()); +} + TEST(DeviceCreation, givenFtrSimulationModeFlagTrueWhenNoOtherSimulationFlagsArePresentThenIsSimulationReturnsTrue) { FeatureTable skuTable = *platformDevices[0]->pSkuTable; skuTable.ftrSimulationMode = true; diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index 34b3f4a6e5..c71043c0d4 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -1824,3 +1824,11 @@ TEST(ResidencyDataTest, givenResidencyDataWhenAddTheSameOsContextTwiceThenIncrem residencyData.addOsContext(osContext); EXPECT_EQ(1, osContext->getRefInternalCount()); } + +TEST(ResidencyDataTest, givenOsContextWhenItIsRegisteredToMemoryManagerThenRefCountIncreases) { + auto osContext = new OsContext(nullptr); + OsAgnosticMemoryManager memoryManager; + memoryManager.registerOsContext(osContext); + EXPECT_EQ(1u, memoryManager.getOsContextCount()); + EXPECT_EQ(1, osContext->getRefInternalCount()); +}