fix: track usm reuse usage in multiple contexts

Add tracking of memory used for usm reuse mechanism when multiple cl
contexts are used.
Tracking for device added to NEO::Device, for host added to
NEO::MemoryManager.

This fixes usm reuse using x% of memory per each context instead of
globally.

Related-To: NEO-13308

Signed-off-by: Dominik Dabek <dominik.dabek@intel.com>
This commit is contained in:
Dominik Dabek
2024-11-26 13:02:19 +00:00
committed by Compute-Runtime-Automation
parent 250171688f
commit e55aa958b7
5 changed files with 255 additions and 45 deletions

View File

@@ -252,56 +252,128 @@ TEST_F(SvmDeviceAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingDevic
{
auto allocation = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation, nullptr);
auto allocation2 = svmManager->createUnifiedMemoryAllocation(1u, unifiedMemoryProperties);
auto allocation2 = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation2, nullptr);
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(allocation);
EXPECT_EQ(1u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(allocationSize, device->getAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(allocation2);
EXPECT_EQ(1u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(allocationSize, device->getAllocationsSavedForReuseSize());
auto recycledAllocation = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
EXPECT_EQ(recycledAllocation, allocation);
EXPECT_EQ(svmManager->usmDeviceAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(recycledAllocation);
svmManager->trimUSMDeviceAllocCache();
EXPECT_EQ(svmManager->usmDeviceAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
}
{
auto allocation = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation, nullptr);
auto allocation2 = svmManager->createUnifiedMemoryAllocation(1u, unifiedMemoryProperties);
auto allocation2 = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation2, nullptr);
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
svmManager->freeSVMAllocDefer(allocation);
EXPECT_EQ(1u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(allocationSize, device->getAllocationsSavedForReuseSize());
svmManager->freeSVMAllocDefer(allocation2);
EXPECT_EQ(1u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(allocationSize, device->getAllocationsSavedForReuseSize());
auto recycledAllocation = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
EXPECT_EQ(recycledAllocation, allocation);
EXPECT_EQ(svmManager->usmDeviceAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
svmManager->freeSVMAllocDefer(recycledAllocation);
svmManager->trimUSMDeviceAllocCache();
EXPECT_EQ(svmManager->usmDeviceAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.totalSize);
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
}
}
TEST_F(SvmDeviceAllocationCacheTest, givenAllocationCacheEnabledAndMultipleSVMManagersWhenFreeingDeviceAllocationThenItIsPutIntoCacheOnlyIfMaxSizeWillNotBeExceeded) {
std::unique_ptr<UltDeviceFactory> deviceFactory(new UltDeviceFactory(1, 1));
RootDeviceIndicesContainer rootDeviceIndices = {mockRootDeviceIndex};
std::map<uint32_t, DeviceBitfield> deviceBitfields{{mockRootDeviceIndex, mockDeviceBitfield}};
DebugManagerStateRestore restore;
debugManager.flags.ExperimentalEnableDeviceAllocationCache.set(1);
auto device = deviceFactory->rootDevices[0];
auto svmManager = std::make_unique<MockSVMAllocsManager>(device->getMemoryManager(), false);
auto secondSvmManager = std::make_unique<MockSVMAllocsManager>(device->getMemoryManager(), false);
svmManager->initUsmAllocationsCaches(*device);
secondSvmManager->initUsmAllocationsCaches(*device);
ASSERT_TRUE(svmManager->usmDeviceAllocationsCacheEnabled);
ASSERT_TRUE(secondSvmManager->usmDeviceAllocationsCacheEnabled);
constexpr auto allocationSize = MemoryConstants::pageSize64k;
svmManager->usmDeviceAllocationsCache.maxSize = allocationSize;
secondSvmManager->usmDeviceAllocationsCache.maxSize = allocationSize;
SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::deviceUnifiedMemory, 1, rootDeviceIndices, deviceBitfields);
unifiedMemoryProperties.device = device;
{
auto allocation = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation, nullptr);
auto allocation2 = secondSvmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation2, nullptr);
svmManager->freeSVMAlloc(allocation);
EXPECT_EQ(1u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, device->getAllocationsSavedForReuseSize());
secondSvmManager->freeSVMAlloc(allocation2);
EXPECT_EQ(0u, secondSvmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, device->getAllocationsSavedForReuseSize());
auto recycledAllocation = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
EXPECT_EQ(recycledAllocation, allocation);
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(recycledAllocation);
svmManager->trimUSMDeviceAllocCache();
EXPECT_EQ(svmManager->usmDeviceAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
}
{
auto allocation = svmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation, nullptr);
auto allocation2 = secondSvmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation2, nullptr);
secondSvmManager->freeSVMAlloc(allocation2);
EXPECT_EQ(1u, secondSvmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, device->getAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(allocation);
EXPECT_EQ(0u, svmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, device->getAllocationsSavedForReuseSize());
auto recycledAllocation = secondSvmManager->createUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
EXPECT_EQ(recycledAllocation, allocation2);
EXPECT_EQ(0u, secondSvmManager->usmDeviceAllocationsCache.allocations.size());
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
secondSvmManager->freeSVMAlloc(recycledAllocation);
secondSvmManager->trimUSMDeviceAllocCache();
EXPECT_EQ(secondSvmManager->usmDeviceAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, device->getAllocationsSavedForReuseSize());
}
}
@@ -699,7 +771,7 @@ struct SvmHostAllocationCacheSimpleTestDataType {
void *allocation;
};
TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingDeviceAllocationThenItIsPutIntoCache) {
TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingHostAllocationThenItIsPutIntoCache) {
std::unique_ptr<UltDeviceFactory> deviceFactory(new UltDeviceFactory(1, 1));
RootDeviceIndicesContainer rootDeviceIndices = {mockRootDeviceIndex};
std::map<uint32_t, DeviceBitfield> deviceBitfields{{mockRootDeviceIndex, mockDeviceBitfield}};
@@ -759,14 +831,15 @@ TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledWhenInitializedThe
EXPECT_EQ(expectedMaxSize, svmManager->usmHostAllocationsCache.maxSize);
}
TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingDeviceAllocationThenItIsPutIntoCacheOnlyIfMaxSizeWillNotBeExceeded) {
TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingHostAllocationThenItIsPutIntoCacheOnlyIfMaxSizeWillNotBeExceeded) {
std::unique_ptr<UltDeviceFactory> deviceFactory(new UltDeviceFactory(1, 1));
RootDeviceIndicesContainer rootDeviceIndices = {mockRootDeviceIndex};
std::map<uint32_t, DeviceBitfield> deviceBitfields{{mockRootDeviceIndex, mockDeviceBitfield}};
DebugManagerStateRestore restore;
debugManager.flags.ExperimentalEnableHostAllocationCache.set(1);
auto device = deviceFactory->rootDevices[0];
auto svmManager = std::make_unique<MockSVMAllocsManager>(device->getMemoryManager(), false);
auto memoryManager = device->getMemoryManager();
auto svmManager = std::make_unique<MockSVMAllocsManager>(memoryManager, false);
svmManager->initUsmAllocationsCaches(*device);
ASSERT_TRUE(svmManager->usmHostAllocationsCacheEnabled);
@@ -780,26 +853,26 @@ TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingDeviceA
auto allocation2 = svmManager->createHostUnifiedMemoryAllocation(1u, unifiedMemoryProperties);
ASSERT_NE(allocation2, nullptr);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(allocation);
EXPECT_EQ(1u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(allocationSize, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(allocation2);
EXPECT_EQ(1u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(allocationSize, memoryManager->getHostAllocationsSavedForReuseSize());
auto recycledAllocation = svmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
EXPECT_EQ(recycledAllocation, allocation);
EXPECT_EQ(svmManager->usmHostAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(recycledAllocation);
svmManager->trimUSMHostAllocCache();
EXPECT_EQ(svmManager->usmHostAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
}
{
auto allocation = svmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
@@ -807,26 +880,104 @@ TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingDeviceA
auto allocation2 = svmManager->createHostUnifiedMemoryAllocation(1u, unifiedMemoryProperties);
ASSERT_NE(allocation2, nullptr);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAllocDefer(allocation);
EXPECT_EQ(1u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(allocationSize, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAllocDefer(allocation2);
EXPECT_EQ(1u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(allocationSize, memoryManager->getHostAllocationsSavedForReuseSize());
auto recycledAllocation = svmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
EXPECT_EQ(recycledAllocation, allocation);
EXPECT_EQ(svmManager->usmHostAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAllocDefer(recycledAllocation);
svmManager->trimUSMHostAllocCache();
EXPECT_EQ(svmManager->usmHostAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.totalSize);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
}
}
TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledAndMultipleSVMManagersWhenFreeingHostAllocationThenItIsPutIntoCacheOnlyIfMaxSizeWillNotBeExceeded) {
std::unique_ptr<UltDeviceFactory> deviceFactory(new UltDeviceFactory(1, 1));
RootDeviceIndicesContainer rootDeviceIndices = {mockRootDeviceIndex};
std::map<uint32_t, DeviceBitfield> deviceBitfields{{mockRootDeviceIndex, mockDeviceBitfield}};
DebugManagerStateRestore restore;
debugManager.flags.ExperimentalEnableHostAllocationCache.set(1);
auto device = deviceFactory->rootDevices[0];
auto memoryManager = device->getMemoryManager();
auto svmManager = std::make_unique<MockSVMAllocsManager>(memoryManager, false);
auto secondSvmManager = std::make_unique<MockSVMAllocsManager>(memoryManager, false);
svmManager->initUsmAllocationsCaches(*device);
secondSvmManager->initUsmAllocationsCaches(*device);
ASSERT_TRUE(svmManager->usmHostAllocationsCacheEnabled);
ASSERT_TRUE(secondSvmManager->usmHostAllocationsCacheEnabled);
constexpr auto allocationSize = MemoryConstants::pageSize64k;
svmManager->usmHostAllocationsCache.maxSize = allocationSize;
secondSvmManager->usmHostAllocationsCache.maxSize = allocationSize;
SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::hostUnifiedMemory, 1, rootDeviceIndices, deviceBitfields);
{
auto allocation = svmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation, nullptr);
auto allocation2 = secondSvmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation2, nullptr);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(0u, secondSvmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(allocation);
EXPECT_EQ(1u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, memoryManager->getHostAllocationsSavedForReuseSize());
secondSvmManager->freeSVMAlloc(allocation2);
EXPECT_EQ(0u, secondSvmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, memoryManager->getHostAllocationsSavedForReuseSize());
auto recycledAllocation = svmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
EXPECT_EQ(recycledAllocation, allocation);
EXPECT_EQ(svmManager->usmHostAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(recycledAllocation);
svmManager->trimUSMHostAllocCache();
EXPECT_EQ(svmManager->usmHostAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
}
{
auto allocation = svmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation, nullptr);
auto allocation2 = secondSvmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
ASSERT_NE(allocation2, nullptr);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(0u, secondSvmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
secondSvmManager->freeSVMAlloc(allocation2);
EXPECT_EQ(1u, secondSvmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, memoryManager->getHostAllocationsSavedForReuseSize());
svmManager->freeSVMAlloc(allocation);
EXPECT_EQ(0u, svmManager->usmHostAllocationsCache.allocations.size());
EXPECT_EQ(allocationSize, memoryManager->getHostAllocationsSavedForReuseSize());
auto recycledAllocation = secondSvmManager->createHostUnifiedMemoryAllocation(allocationSize, unifiedMemoryProperties);
EXPECT_EQ(recycledAllocation, allocation2);
EXPECT_EQ(secondSvmManager->usmHostAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
secondSvmManager->freeSVMAlloc(recycledAllocation);
secondSvmManager->trimUSMHostAllocCache();
EXPECT_EQ(secondSvmManager->usmHostAllocationsCache.allocations.size(), 0u);
EXPECT_EQ(0u, memoryManager->getHostAllocationsSavedForReuseSize());
}
}