performance: usm reuse, avoid looking up svmData

Save svmData on putting into reuse, instead of searching each time.
Change UNRECOVERABLE_IF to DEBUG_BREAK_IF.

Related-To: NEO-6893

Signed-off-by: Dominik Dabek <dominik.dabek@intel.com>
This commit is contained in:
Dominik Dabek
2025-04-03 13:33:12 +00:00
committed by Compute-Runtime-Automation
parent c010d17842
commit be27367020
3 changed files with 31 additions and 28 deletions

View File

@@ -85,7 +85,7 @@ bool SVMAllocsManager::SvmAllocationCache::insert(size_t size, void *ptr, SvmAll
} }
} }
if (isSuccess) { if (isSuccess) {
allocations.emplace(std::lower_bound(allocations.begin(), allocations.end(), size), size, ptr); allocations.emplace(std::lower_bound(allocations.begin(), allocations.end(), size), size, ptr, svmData);
} }
if (enablePerformanceLogging) { if (enablePerformanceLogging) {
logCacheOperation({.allocationSize = size, logCacheOperation({.allocationSize = size,
@@ -129,15 +129,14 @@ void *SVMAllocsManager::SvmAllocationCache::get(size_t size, const UnifiedMemory
break; break;
} }
void *allocationPtr = allocationIter->allocation; void *allocationPtr = allocationIter->allocation;
SvmAllocationData *svmData = svmAllocsManager->getSVMAlloc(allocationPtr); DEBUG_BREAK_IF(nullptr == allocationIter->svmData);
UNRECOVERABLE_IF(nullptr == svmData); if (allocationIter->svmData->device == unifiedMemoryProperties.device &&
if (svmData->device == unifiedMemoryProperties.device && allocationIter->svmData->allocationFlagsProperty.allFlags == unifiedMemoryProperties.allocationFlags.allFlags &&
svmData->allocationFlagsProperty.allFlags == unifiedMemoryProperties.allocationFlags.allFlags && allocationIter->svmData->allocationFlagsProperty.allAllocFlags == unifiedMemoryProperties.allocationFlags.allAllocFlags &&
svmData->allocationFlagsProperty.allAllocFlags == unifiedMemoryProperties.allocationFlags.allAllocFlags && false == isInUse(allocationIter->svmData)) {
false == isInUse(svmData)) { if (allocationIter->svmData->device) {
if (svmData->device) { auto lock = allocationIter->svmData->device->usmReuseInfo.obtainAllocationsReuseLock();
auto lock = svmData->device->usmReuseInfo.obtainAllocationsReuseLock(); allocationIter->svmData->device->usmReuseInfo.recordAllocationGetFromReuse(allocationIter->allocationSize);
svmData->device->usmReuseInfo.recordAllocationGetFromReuse(allocationIter->allocationSize);
} else { } else {
auto lock = memoryManager->usmReuseInfo.obtainAllocationsReuseLock(); auto lock = memoryManager->usmReuseInfo.obtainAllocationsReuseLock();
memoryManager->usmReuseInfo.recordAllocationGetFromReuse(allocationIter->allocationSize); memoryManager->usmReuseInfo.recordAllocationGetFromReuse(allocationIter->allocationSize);
@@ -145,12 +144,12 @@ void *SVMAllocsManager::SvmAllocationCache::get(size_t size, const UnifiedMemory
if (enablePerformanceLogging) { if (enablePerformanceLogging) {
logCacheOperation({.allocationSize = allocationIter->allocationSize, logCacheOperation({.allocationSize = allocationIter->allocationSize,
.timePoint = std::chrono::high_resolution_clock::now(), .timePoint = std::chrono::high_resolution_clock::now(),
.allocationType = svmData->memoryType, .allocationType = allocationIter->svmData->memoryType,
.operationType = CacheOperationType::get, .operationType = CacheOperationType::get,
.isSuccess = true}); .isSuccess = true});
} }
allocationIter->svmData->size = size;
allocations.erase(allocationIter); allocations.erase(allocationIter);
svmData->size = size;
return allocationPtr; return allocationPtr;
} }
} }
@@ -167,11 +166,10 @@ void *SVMAllocsManager::SvmAllocationCache::get(size_t size, const UnifiedMemory
void SVMAllocsManager::SvmAllocationCache::trim() { void SVMAllocsManager::SvmAllocationCache::trim() {
std::lock_guard<std::mutex> lock(this->mtx); std::lock_guard<std::mutex> lock(this->mtx);
for (auto &cachedAllocationInfo : this->allocations) { for (auto &cachedAllocationInfo : this->allocations) {
SvmAllocationData *svmData = svmAllocsManager->getSVMAlloc(cachedAllocationInfo.allocation); DEBUG_BREAK_IF(nullptr == cachedAllocationInfo.svmData);
UNRECOVERABLE_IF(nullptr == svmData); if (cachedAllocationInfo.svmData->device) {
if (svmData->device) { auto lock = cachedAllocationInfo.svmData->device->usmReuseInfo.obtainAllocationsReuseLock();
auto lock = svmData->device->usmReuseInfo.obtainAllocationsReuseLock(); cachedAllocationInfo.svmData->device->usmReuseInfo.recordAllocationGetFromReuse(cachedAllocationInfo.allocationSize);
svmData->device->usmReuseInfo.recordAllocationGetFromReuse(cachedAllocationInfo.allocationSize);
} else { } else {
auto lock = memoryManager->usmReuseInfo.obtainAllocationsReuseLock(); auto lock = memoryManager->usmReuseInfo.obtainAllocationsReuseLock();
memoryManager->usmReuseInfo.recordAllocationGetFromReuse(cachedAllocationInfo.allocationSize); memoryManager->usmReuseInfo.recordAllocationGetFromReuse(cachedAllocationInfo.allocationSize);
@@ -179,11 +177,11 @@ void SVMAllocsManager::SvmAllocationCache::trim() {
if (enablePerformanceLogging) { if (enablePerformanceLogging) {
logCacheOperation({.allocationSize = cachedAllocationInfo.allocationSize, logCacheOperation({.allocationSize = cachedAllocationInfo.allocationSize,
.timePoint = std::chrono::high_resolution_clock::now(), .timePoint = std::chrono::high_resolution_clock::now(),
.allocationType = svmData->memoryType, .allocationType = cachedAllocationInfo.svmData->memoryType,
.operationType = CacheOperationType::trim, .operationType = CacheOperationType::trim,
.isSuccess = true}); .isSuccess = true});
} }
svmAllocsManager->freeSVMAllocImpl(cachedAllocationInfo.allocation, FreePolicyType::none, svmData); svmAllocsManager->freeSVMAllocImpl(cachedAllocationInfo.allocation, FreePolicyType::none, cachedAllocationInfo.svmData);
} }
this->allocations.clear(); this->allocations.clear();
} }
@@ -247,12 +245,10 @@ void SVMAllocsManager::SvmAllocationCache::trimOldAllocs(std::chrono::high_resol
++allocCleanCandidate; ++allocCleanCandidate;
continue; continue;
} }
void *allocationPtr = allocCleanCandidate->allocation; DEBUG_BREAK_IF(nullptr == allocCleanCandidate->svmData);
SvmAllocationData *svmData = svmAllocsManager->getSVMAlloc(allocationPtr); if (allocCleanCandidate->svmData->device) {
UNRECOVERABLE_IF(nullptr == svmData); auto lock = allocCleanCandidate->svmData->device->usmReuseInfo.obtainAllocationsReuseLock();
if (svmData->device) { allocCleanCandidate->svmData->device->usmReuseInfo.recordAllocationGetFromReuse(allocCleanCandidate->allocationSize);
auto lock = svmData->device->usmReuseInfo.obtainAllocationsReuseLock();
svmData->device->usmReuseInfo.recordAllocationGetFromReuse(allocCleanCandidate->allocationSize);
} else { } else {
auto lock = memoryManager->usmReuseInfo.obtainAllocationsReuseLock(); auto lock = memoryManager->usmReuseInfo.obtainAllocationsReuseLock();
memoryManager->usmReuseInfo.recordAllocationGetFromReuse(allocCleanCandidate->allocationSize); memoryManager->usmReuseInfo.recordAllocationGetFromReuse(allocCleanCandidate->allocationSize);
@@ -260,11 +256,11 @@ void SVMAllocsManager::SvmAllocationCache::trimOldAllocs(std::chrono::high_resol
if (enablePerformanceLogging) { if (enablePerformanceLogging) {
logCacheOperation({.allocationSize = allocCleanCandidate->allocationSize, logCacheOperation({.allocationSize = allocCleanCandidate->allocationSize,
.timePoint = std::chrono::high_resolution_clock::now(), .timePoint = std::chrono::high_resolution_clock::now(),
.allocationType = svmData->memoryType, .allocationType = allocCleanCandidate->svmData->memoryType,
.operationType = CacheOperationType::trimOld, .operationType = CacheOperationType::trimOld,
.isSuccess = true}); .isSuccess = true});
} }
svmAllocsManager->freeSVMAllocImpl(allocCleanCandidate->allocation, FreePolicyType::defer, svmData); svmAllocsManager->freeSVMAllocImpl(allocCleanCandidate->allocation, FreePolicyType::defer, allocCleanCandidate->svmData);
if (shouldLimitReuse) { if (shouldLimitReuse) {
allocCleanCandidate = allocations.erase(allocCleanCandidate); allocCleanCandidate = allocations.erase(allocCleanCandidate);
} else { } else {

View File

@@ -154,8 +154,9 @@ class SVMAllocsManager {
struct SvmCacheAllocationInfo { struct SvmCacheAllocationInfo {
size_t allocationSize; size_t allocationSize;
void *allocation; void *allocation;
SvmAllocationData *svmData;
std::chrono::high_resolution_clock::time_point saveTime; std::chrono::high_resolution_clock::time_point saveTime;
SvmCacheAllocationInfo(size_t allocationSize, void *allocation) : allocationSize(allocationSize), allocation(allocation) { SvmCacheAllocationInfo(size_t allocationSize, void *allocation, SvmAllocationData *svmData) : allocationSize(allocationSize), allocation(allocation), svmData(svmData) {
saveTime = std::chrono::high_resolution_clock::now(); saveTime = std::chrono::high_resolution_clock::now();
} }
bool operator<(SvmCacheAllocationInfo const &other) const { bool operator<(SvmCacheAllocationInfo const &other) const {

View File

@@ -304,6 +304,9 @@ TEST_F(SvmDeviceAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingDevic
for (auto i = 0u; i < svmManager->usmDeviceAllocationsCache->allocations.size(); ++i) { for (auto i = 0u; i < svmManager->usmDeviceAllocationsCache->allocations.size(); ++i) {
if (svmManager->usmDeviceAllocationsCache->allocations[i].allocation == testData.allocation) { if (svmManager->usmDeviceAllocationsCache->allocations[i].allocation == testData.allocation) {
foundInCache = true; foundInCache = true;
auto svmData = svmManager->getSVMAlloc(testData.allocation);
EXPECT_NE(nullptr, svmData);
EXPECT_EQ(svmData, svmManager->usmDeviceAllocationsCache->allocations[i].svmData);
break; break;
} }
} }
@@ -1153,6 +1156,9 @@ TEST_F(SvmHostAllocationCacheTest, givenAllocationCacheEnabledWhenFreeingHostAll
for (auto i = 0u; i < svmManager->usmHostAllocationsCache->allocations.size(); ++i) { for (auto i = 0u; i < svmManager->usmHostAllocationsCache->allocations.size(); ++i) {
if (svmManager->usmHostAllocationsCache->allocations[i].allocation == testData.allocation) { if (svmManager->usmHostAllocationsCache->allocations[i].allocation == testData.allocation) {
foundInCache = true; foundInCache = true;
auto svmData = svmManager->getSVMAlloc(testData.allocation);
EXPECT_NE(nullptr, svmData);
EXPECT_EQ(svmData, svmManager->usmHostAllocationsCache->allocations[i].svmData);
break; break;
} }
} }