refactor: simplify tracking CacheRegion reservations

Leverage features of the mechanism to simplify implementation:
- The maximum number of possible cache-region reservations is a small
value known at compile-time
- Each reservation is unique (described by `CacheRegion`) so can have
a dedicated entry with either zero (free) or non-zero (reserved) value

So, there is no need for a dynamic collection (unordered_map here) to
keep track of reservations. A simple array is enough for that purpose.

Also, add some helper-code to enable array-indexing with the values of
`CacheRegion` enum.

Related-To: NEO-12837
Signed-off-by: Maciej Bielski <maciej.bielski@intel.com>
This commit is contained in:
Maciej Bielski
2024-12-05 14:31:28 +00:00
committed by Compute-Runtime-Automation
parent 9c109dc0c6
commit c9726dbb10
5 changed files with 48 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Intel Corporation
* Copyright (C) 2022-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -14,10 +14,16 @@
namespace NEO {
CacheInfo::~CacheInfo() {
for (auto const &cacheRegion : cacheRegionsReserved) {
cacheReserve.freeCache(CacheLevel::level3, cacheRegion.first);
// skip the defaultRegion
constexpr auto regionStart{toUnderlying(CacheRegion::region1)};
constexpr auto regionEnd{toUnderlying(CacheRegion::count)};
for (auto regionIndex{regionStart}; regionIndex < regionEnd; ++regionIndex) {
if (reservedCacheRegionsSize[regionIndex]) {
cacheReserve.freeCache(CacheLevel::level3, toCacheRegion(regionIndex));
reservedCacheRegionsSize[regionIndex] = 0u;
}
}
cacheRegionsReserved.clear();
}
CacheRegion CacheInfo::reserveRegion(size_t cacheReservationSize) {
@@ -30,28 +36,28 @@ CacheRegion CacheInfo::reserveRegion(size_t cacheReservationSize) {
if (regionIndex == CacheRegion::none) {
return CacheRegion::none;
}
cacheRegionsReserved.insert({regionIndex, cacheReservationSize});
DEBUG_BREAK_IF(regionIndex == CacheRegion::defaultRegion);
DEBUG_BREAK_IF(regionIndex >= CacheRegion::count);
reservedCacheRegionsSize[toUnderlying(regionIndex)] = cacheReservationSize;
return regionIndex;
}
CacheRegion CacheInfo::freeRegion(CacheRegion regionIndex) {
auto search = cacheRegionsReserved.find(regionIndex);
if (search != cacheRegionsReserved.end()) {
cacheRegionsReserved.erase(search);
if (regionIndex < CacheRegion::count && reservedCacheRegionsSize[toUnderlying(regionIndex)] > 0u) {
reservedCacheRegionsSize[toUnderlying(regionIndex)] = 0u;
return cacheReserve.freeCache(CacheLevel::level3, regionIndex);
}
return CacheRegion::none;
}
bool CacheInfo::isRegionReserved(CacheRegion regionIndex, [[maybe_unused]] size_t regionSize) const {
auto search = cacheRegionsReserved.find(regionIndex);
if (search != cacheRegionsReserved.end()) {
bool CacheInfo::isRegionReserved(CacheRegion regionIndex, [[maybe_unused]] size_t expectedRegionSize) const {
if (regionIndex < CacheRegion::count && reservedCacheRegionsSize[toUnderlying(regionIndex)]) {
if (debugManager.flags.ClosNumCacheWays.get() != -1) {
auto numWays = debugManager.flags.ClosNumCacheWays.get();
regionSize = (numWays * maxReservationCacheSize) / maxReservationNumWays;
expectedRegionSize = (numWays * maxReservationCacheSize) / maxReservationNumWays;
}
DEBUG_BREAK_IF(search->second != regionSize);
DEBUG_BREAK_IF(expectedRegionSize != reservedCacheRegionsSize[toUnderlying(regionIndex)]);
return true;
}
return false;