[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:
Varun Sundar Rabindranath
2026-06-18 05:34:59 +03:00
committed by GitHub
co-authored by Varun Sundar Rabindranath
parent 731fb3323d
commit ed938ad7db
2 changed files with 114 additions and 0 deletions
+93
View File
@@ -689,3 +689,96 @@ def test_filter_reused_manager():
assert prepare_store_output.keys_to_store == []
manager.complete_store(to_keys([1]), _EMPTY_REQ_CTX)
def test_evictable_cache_block_count():
"""
Verifies _num_evictable_cache_blocks is maintained correctly through the
full store/load lifecycle, eviction, failed stores, concurrent loads,
reset_cache, and the early-exit fast path in prepare_store.
"""
manager = make_cpu_manager(num_blocks=4, cache_policy="lru")
# Initially no blocks allocated.
assert manager._num_evictable_cache_blocks == 0
# Initial cache state [x, x, x, x]
# We get 3 blocks from the cache.
manager.prepare_store(to_keys([1, 2, 3]), _EMPTY_REQ_CTX)
# cache state [1', 2', 3', x] <- 1', 2', 3' are actively being used.
assert manager._num_evictable_cache_blocks == 0
# Completing stores makes them idle.
manager.complete_store(to_keys([1, 2, 3]), _EMPTY_REQ_CTX)
# cache state [1, 2, 3, x] <- 1, 2, 3 blocks are idle.
assert manager._num_evictable_cache_blocks == 3
# prepare_load pins a block: idle count decrements once even if the
# same block is loaded by two concurrent callers.
manager.prepare_load(to_keys([1]), _EMPTY_REQ_CTX)
# cache state [1', 2, 3, x] <- 2, 3 blocks are idle.
assert manager._num_evictable_cache_blocks == 2
manager.prepare_load(to_keys([1]), _EMPTY_REQ_CTX) # 2nd concurrent load
# cache state [1', 2, 3, x] <- 2, 3 blocks are idle.
assert manager._num_evictable_cache_blocks == 2 # no double-decrement
# First complete_load does not restore idle (ref_cnt still 1).
manager.complete_load(to_keys([1]), _EMPTY_REQ_CTX)
# cache state [1', 2, 3, x] <- 2, 3 blocks are idle.
assert manager._num_evictable_cache_blocks == 2
# Second complete_load drops ref_cnt to 0 -> block becomes idle again.
manager.complete_load(to_keys([1]), _EMPTY_REQ_CTX)
# cache state [1, 2, 3, x] <- 1, 2, 3 blocks are idle.
assert manager._num_evictable_cache_blocks == 3
# Eviction decrements idle count.
# Cache has 3 stored blocks and 1 free slot. Storing 3 new keys needs 2 eviction.
manager.prepare_store(to_keys([4, 5, 6]), _EMPTY_REQ_CTX)
# cache state [1, 4', 5', 6'] <- block 1 is idle
assert manager._num_evictable_cache_blocks == 1
# Failed store does not increment idle count (block discarded from cache).
manager.complete_store(to_keys([4, 5, 6]), _EMPTY_REQ_CTX, success=False)
# cache state [1, x, x, x] <- block 1 is idle. Other returned to cache.
assert manager._num_evictable_cache_blocks == 1
# reset_cache zeroes the count unconditionally.
manager.reset_cache()
# cache state [x, x, x, x]
assert manager._num_evictable_cache_blocks == 0
# setup 3 blocks with loads so idle count drops to 0.
manager.prepare_store(to_keys([10, 11, 12]), _EMPTY_REQ_CTX)
manager.complete_store(to_keys([10, 11, 12]), _EMPTY_REQ_CTX)
manager.prepare_load(to_keys([10, 11, 12]), _EMPTY_REQ_CTX)
# cache state [10', 11', 12', x]
assert manager._num_evictable_cache_blocks == 0
# prepare_store requiring eviction must return None immediately (fast exit).
# Spy on policy.evict to confirm the fast path short-circuits before calling it.
evict_called = False
original_evict = manager._policy.evict
def spy_evict(*args, **kwargs):
nonlocal evict_called
evict_called = True
return original_evict(*args, **kwargs)
manager._policy.evict = spy_evict # type: ignore[method-assign]
# cache state [10', 11', 12', x] <- cannot evict anything
assert manager.prepare_store(to_keys([14, 15]), _EMPTY_REQ_CTX) is None
assert not evict_called, (
"_num_evictable_cache_blocks==0 should short-circuit before evict()"
)
# After releasing the loads, eviction becomes possible again.
manager.complete_load(to_keys([10, 11, 12]), _EMPTY_REQ_CTX)
# cache state [10, 11, 12, x] <- 10, 11, 12 are idle
assert manager._num_evictable_cache_blocks == 3
assert manager.prepare_store(to_keys([14, 15]), _EMPTY_REQ_CTX) is not None
# cache state [10, 11, 14', 15'] <- 10, 11 are idle
assert manager._num_evictable_cache_blocks == 2
manager.complete_store(to_keys([14, 15]), _EMPTY_REQ_CTX)
# cache state [10, 11, 14, 15] <- all blocks idle
assert manager._num_evictable_cache_blocks == 4