From 5c39056d42e98d54923a9717690f818d7f802c99 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sat, 14 Nov 2026 01:19:28 +0100 Subject: [PATCH 1/2] fix(SLRU, WTinyLFU): crashes after remove and after the main cache shrinks The insert loop from the seg-size fix waited for the lowest segment to fit. After SLRU_resize() shrinks that segment below an object W-TinyLFU inserts directly, it never fits, so the loop evicted everything and crashed on a NULL victim. The hill climber does this resize, so cachesim data/cloudPhysicsIO.vscsi vscsi wtinylfu 1mb -e hill-climb=1 crashed (it ran before the seg-size fix). Go back to evicting until the whole cache fits. SLRU_remove() unlinked the object but left lru_n_bytes and lru_n_objs as they were, so the freed space was never reused and, with the loop above, removing probation's last object also crashed the next insert. Update the counts as SLRU_evict() does. W-TinyLFU: the loop that evicts room for an admitted object also stops when the main cache is empty, and removing an object from the main cache clears main_full so the freed space is filled again. Count mode results are unchanged. In byte mode SLRU goes back to its results from before the seg-size fix, and W-TinyLFU, whose main cache is SLRU, moves slightly with it. --- libCacheSim/cache/eviction/SLRU.c | 7 ++++--- libCacheSim/cache/eviction/WTinyLFU.c | 4 +++- test/test_evictionAlgo.c | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index 82c48111..3f83cfdd 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -313,9 +313,8 @@ static cache_obj_t *SLRU_insert(cache_t *cache, const request_t *req) { if (nth_seg == -1) { // No space for insertion - int64_t sz = req->obj_size + cache->obj_md_size; - while (params->lru_n_bytes[0] + sz > params->lru_max_n_bytes[0] || - cache->occupied_byte + sz > cache->cache_size) { + while (cache->occupied_byte + req->obj_size + cache->obj_md_size > + cache->cache_size) { cache->evict(cache, req); } nth_seg = 0; @@ -412,6 +411,8 @@ static bool SLRU_remove(cache_t *cache, obj_id_t obj_id) { return false; } + params->lru_n_bytes[obj->SLRU.lru_id] -= obj->obj_size + cache->obj_md_size; + params->lru_n_objs[obj->SLRU.lru_id]--; remove_obj_from_list(&(params->lru_heads[obj->SLRU.lru_id]), &(params->lru_tails[obj->SLRU.lru_id]), obj); cache_remove_obj_base(cache, obj, true); diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index e5e55a5a..5e4d335c 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -425,7 +425,8 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { do { main_cache->evict(main_cache, req); - } while (!WTinyLFU_main_fits(main_cache, params->req_local)); + } while (!WTinyLFU_main_fits(main_cache, params->req_local) && + main_cache->get_n_obj(main_cache) > 0); bool ret = window->remove(window, window_victim->obj_id); DEBUG_ASSERT(ret); @@ -462,6 +463,7 @@ static bool WTinyLFU_remove(cache_t *cache, obj_id_t obj_id) { return true; } if (params->main_cache->remove(params->main_cache, obj_id)) { + params->main_full = false; return true; } return false; diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index dbe3bc76..48d38506 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -202,9 +202,9 @@ static const cache_test_data_t test_data_truth[] = { .hashpower = 20, .req_cnt_true = 113872, .req_byte_true = 4368040448, - .miss_cnt_true = {89862, 86685, 83365, 80309, 75447, 66000, 59092, 56111}, - .miss_byte_true = {4125897728, 3909879808, 3697543168, 3489533952, - 3159642624, 2695841792, 2506203648, 2440419840}}, + .miss_cnt_true = {89624, 86725, 82781, 80203, 75388, 65645, 59035, 56063}, + .miss_byte_true = {4123085312, 3915534848, 3690704896, 3493027840, + 3174708736, 2661464064, 2507604992, 2439981056}}, {.cache_name = "SR_LRU", .hashpower = 20, .req_cnt_true = 113872, From fad814750c6335752d514dc2776e86462b8e9bb2 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sat, 14 Nov 2026 15:44:02 +0100 Subject: [PATCH 2/2] fix(WTinyLFU): drop window objects larger than the main cache With an LRU main cache, the hill climber can shrink the main cache below an object waiting in the window. Admitting it emptied the main cache and then crashed. Such an object is now dropped from the window, and an empty main cache is filled again without a comparison. SLRU_evict stops with an error instead of crashing when there is nothing to evict. Add a CLI test that crashes on develop. --- libCacheSim/cache/eviction/SLRU.c | 3 +++ libCacheSim/cache/eviction/WTinyLFU.c | 13 +++++++++---- test/test_cli.sh | 3 +++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index 3f83cfdd..8455aa60 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -381,6 +381,9 @@ static void SLRU_evict(cache_t *cache, const request_t *req) { SLRU_params_t *params = (SLRU_params_t *)(cache->eviction_params); cache_obj_t *obj = SLRU_to_evict(cache, req); + if (obj == NULL) { + ERROR("SLRU: no object to evict\n"); + } params->lru_n_bytes[obj->SLRU.lru_id] -= obj->obj_size + cache->obj_md_size; params->lru_n_objs[obj->SLRU.lru_id]--; diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 5e4d335c..4af74046 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -394,8 +394,14 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { * caller asking the composite what it reserves is not told less than it * really does; using it here would bill a FIFO or Clock main cache for * the window's 16 bytes and call it full early. */ - if (!params->main_full && - WTinyLFU_main_fits(main_cache, params->req_local)) { + if (params->req_local->obj_size + main_cache->obj_md_size > + main_cache->cache_size) { + // the hill climber can shrink the main cache below this object + window->evict(window, req); + evicted = true; + } else if ((!params->main_full || + main_cache->get_n_obj(main_cache) == 0) && + WTinyLFU_main_fits(main_cache, params->req_local)) { main_cache->insert(main_cache, params->req_local); #if defined(TRACK_DEMOTION) @@ -425,8 +431,7 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { do { main_cache->evict(main_cache, req); - } while (!WTinyLFU_main_fits(main_cache, params->req_local) && - main_cache->get_n_obj(main_cache) > 0); + } while (!WTinyLFU_main_fits(main_cache, params->req_local)); bool ret = window->remove(window, window_victim->obj_id); DEBUG_ASSERT(ret); diff --git a/test/test_cli.sh b/test/test_cli.sh index 22f622e8..e92a67ff 100755 --- a/test/test_cli.sh +++ b/test/test_cli.sh @@ -341,6 +341,9 @@ expect_output "WTinyLFU reports hill-climb=1" "hill-climb=1" \ expect_clean_error "WTinyLFU hill-climb rejects a FIFO main cache" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi wtinyLFU 1mb \ --num-req=10 --hashpower=8 -e "main-cache=FIFO,hill-climb=1" +expect_ok "WTinyLFU hill-climb runs with a small LRU main cache" \ + "${BIN_DIR}/cachesim" "${TRACE}" vscsi wtinyLFU 256kb \ + --hashpower=8 -e "main-cache=LRU,hill-climb=1" echo "running admission and prefetch missing-value tests"