[CPUOffloading] Guard CPU eviction check (#45757)
Signed-off-by: Varun Sundar Rabindranath <varun-sundar-rabindranath@h100-01.nemg-001.lab.rdu2.dc.redhat.com> Co-authored-by: Varun Sundar Rabindranath <varun-sundar-rabindranath@h100-01.nemg-001.lab.rdu2.dc.redhat.com>
This commit is contained in:
co-authored by
Varun Sundar Rabindranath
parent
731fb3323d
commit
ed938ad7db
@@ -59,6 +59,9 @@ class CPUOffloadingManager(OffloadingManager):
|
||||
f"Supported: {list(_CACHE_POLICIES)}"
|
||||
)
|
||||
self._policy: CachePolicy = policy_cls(cache_capacity=num_blocks)
|
||||
# Track the number of blocks in the cache that are evictable. i.e. ref_cnt 0.
|
||||
self._num_evictable_cache_blocks: int = 0
|
||||
|
||||
self.store_threshold: int = store_threshold
|
||||
self.max_tracker_size: int = max_tracker_size
|
||||
self.stores_skipped_in_current_batch: int = 0
|
||||
@@ -133,6 +136,9 @@ class CPUOffloadingManager(OffloadingManager):
|
||||
block = self._policy.get(key)
|
||||
assert block is not None, f"Block {key!r} not found in cache"
|
||||
assert block.is_ready, f"Block {key!r} is not ready for reading"
|
||||
if block.ref_cnt == 0:
|
||||
self._num_evictable_cache_blocks -= 1 # ref_cnt 0 -> 1
|
||||
assert self._num_evictable_cache_blocks >= 0
|
||||
block.ref_cnt += 1
|
||||
blocks.append(block)
|
||||
return self._get_load_store_spec(keys, blocks)
|
||||
@@ -150,6 +156,8 @@ class CPUOffloadingManager(OffloadingManager):
|
||||
assert block is not None, f"Block {key!r} not found"
|
||||
assert block.ref_cnt > 0, f"Block {key!r} ref_cnt is already 0"
|
||||
block.ref_cnt -= 1
|
||||
if block.ref_cnt == 0:
|
||||
self._num_evictable_cache_blocks += 1 # ref_cnt 1 -> 0
|
||||
|
||||
@override
|
||||
def prepare_store(
|
||||
@@ -175,12 +183,23 @@ class CPUOffloadingManager(OffloadingManager):
|
||||
|
||||
to_evict: list[OffloadKey] = []
|
||||
if num_blocks_to_evict > 0:
|
||||
if num_blocks_to_evict > self._num_evictable_cache_blocks:
|
||||
# Eviction will fail.
|
||||
return None
|
||||
# There is a still a chance for eviction failure as some of the
|
||||
# idle blocks might be in the protected list.
|
||||
|
||||
# Blocks from the original input are excluded from eviction candidates:
|
||||
# a block that was already stored must remain in the cache after this call.
|
||||
protected = set(keys)
|
||||
evicted = self._policy.evict(num_blocks_to_evict, protected)
|
||||
if evicted is None:
|
||||
return None
|
||||
|
||||
# cache-policy removes only idle blocks.
|
||||
self._num_evictable_cache_blocks -= len(evicted)
|
||||
assert self._num_evictable_cache_blocks >= 0
|
||||
|
||||
for key, block in evicted:
|
||||
self._free_block(block)
|
||||
to_evict.append(key)
|
||||
@@ -225,6 +244,7 @@ class CPUOffloadingManager(OffloadingManager):
|
||||
block = self._policy.get(key)
|
||||
if block is not None and not block.is_ready:
|
||||
block.ref_cnt = 0
|
||||
self._num_evictable_cache_blocks += 1
|
||||
stored_keys.append(key)
|
||||
else:
|
||||
for key in keys:
|
||||
@@ -250,6 +270,7 @@ class CPUOffloadingManager(OffloadingManager):
|
||||
# flushes in-flight load job IDs to the workers before any new stores
|
||||
# can begin, preventing a cross-direction data race on reused offload block IDs.
|
||||
self._policy.clear()
|
||||
self._num_evictable_cache_blocks = 0
|
||||
|
||||
self._free_list.clear()
|
||||
self._num_allocated_blocks = 0
|
||||
|
||||
Reference in New Issue
Block a user