From 2a04d238413e5d2df59e90206be4051d748c78cd Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Fri, 13 Nov 2026 20:51:30 +0100 Subject: [PATCH 1/4] fix(SLRU): give the seg-size rounding remainder to the last segment SLRU_parse_params() rounds each seg-size segment down on its own, so the segments add up to less than the cache unless the cache size divides evenly. #343 fixed this for the default layout but kept explicit seg-size layouts as they were. With W-TinyLFU's seg-size=1:4 main cache this is one object short unless the main size is a multiple of 5. Once every segment is full, a hit in probation evicts inside find(), and W-TinyLFU then admits its next window victim without the frequency check. Call SLRU_resize() after parsing, which gives the remainder to the last segment. The seg-size=1:2:3:4 print in test_cli.sh changes from 9:19:29:39 to 9:19:29:40. --- libCacheSim/cache/eviction/SLRU.c | 2 ++ test/test_cli.sh | 2 +- test/test_evictionAlgo.c | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index 6dbd2c42..bce331ed 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -502,6 +502,8 @@ static void SLRU_parse_params(cache_t *cache, (long long)params->lru_max_n_bytes[i]); } } + // give the rounding remainder to the last segment + SLRU_resize(cache, cache->cache_size); } else if (strcasecmp(key, "print") == 0) { printf("current parameters: %s\n", SLRU_current_params(cache, params)); free(old_params_str); diff --git a/test/test_cli.sh b/test/test_cli.sh index 2d084c94..6a28c3a0 100755 --- a/test/test_cli.sh +++ b/test/test_cli.sh @@ -375,7 +375,7 @@ expect_output "slru -e print" "n-seg=4,seg-size=25:25:25:25" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e print expect_output "slru -e n-seg=8,print" "n-seg=8" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e "n-seg=8,print" -expect_output "slru -e seg-size=1:2:3:4,print" "seg-size=9:19:29:39" \ +expect_output "slru -e seg-size=1:2:3:4,print" "seg-size=9:19:29:40" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e "seg-size=1:2:3:4,print" expect_output "slru -e print with auto sizing" "n-seg=" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru auto -e print diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 36f87b56..d874114f 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -723,6 +723,27 @@ static void test_SLRU_resize_keeps_large_object(void) { cache->cache_free(cache); } +// seg-size=1:4 on 6 objects must give segments of 1 and 5, not 1 and 4 +static void test_SLRU_seg_size_fills_cache(void) { + common_cache_params_t cc_params = {.cache_size = 6, .hashpower = 8}; + cache_t *cache = SLRU_init(cc_params, "seg-size=1:4"); + request_t *req = new_request(); + req->obj_size = 1; + + for (obj_id_t id = 1; id <= 6; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + g_assert_cmpint(cache->get_n_obj(cache), ==, 6); + + req->obj_id = 1; + g_assert_true(cache->get(cache, req)); + g_assert_cmpint(cache->get_n_obj(cache), ==, 6); + + free_request(req); + cache->cache_free(cache); +} + /* The MINISIM profiler only knows an algorithm by its name and reaches it * through create_cache_using_plugin(). That used to go straight to dlsym(), * which cannot see a built-in constructor in a statically linked build — the @@ -830,6 +851,8 @@ int main(int argc, char *argv[]) { g_test_add_func("/libCacheSim/cacheAlgo_WTinyLFU_hill_climb_no_hits", test_WTinyLFU_hill_climb_no_hits); g_test_add_func("/libCacheSim/cacheAlgo_SLRU_resize", test_SLRU_resize); + g_test_add_func("/libCacheSim/cacheAlgo_SLRU_seg_size_fills_cache", + test_SLRU_seg_size_fills_cache); g_test_add_func("/libCacheSim/cacheAlgo_SLRU_resize_keeps_large_object", test_SLRU_resize_keeps_large_object); From 3829e7a09b02d518af37828a943466034e174209 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Fri, 13 Nov 2026 23:42:03 +0100 Subject: [PATCH 2/4] fix(SLRU): exact segment byte counts and stable names SLRU_cool() moved obj_size between segments while promote, insert and evict moved obj_size + obj_md_size, so with --consider-obj-metadata each promote-then-demote left the metadata on the upper segment's count. When no segment had room, SLRU_insert() evicted until the whole cache fit, which could push the lowest segment past its share when objects differ in size; it now evicts until that segment fits too. The cache name and -e print now show the shares asked for (seg_ratio) instead of the byte limits, so seg-size=1:1 is S2LRU(50:50) at every cache size and seg-size=1:2:3:4 prints 10:20:30:40. Count mode (--ignore-obj-size=1) is unchanged. --- libCacheSim/cache/eviction/SLRU.c | 44 +++++++++++++++---------------- test/test_cli.sh | 5 +++- test/test_evictionAlgo.c | 6 ++--- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index bce331ed..7cbaa8ef 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -7,6 +7,8 @@ // // +#include + #include "dataStructure/hashtable/hashtable.h" #include "libCacheSim/evictionAlgo.h" @@ -49,6 +51,7 @@ static void SLRU_promote_to_next_seg(cache_t *cache, const request_t *req, cache_obj_t *obj); static void SLRU_cool(cache_t *cache, const request_t *req, const int id); bool SLRU_can_insert(cache_t *cache, const request_t *req); +static int SLRU_seg_pct(const SLRU_params_t *params, const int seg); static void SLRU_parse_params(cache_t *cache, const char *cache_specific_params); @@ -162,7 +165,7 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params, // update slru cache name bool same_size = true; for (int i = 1; i < params->n_seg; i++) { - if (params->lru_max_n_bytes[i] != params->lru_max_n_bytes[i - 1]) { + if (params->seg_ratio[i] != params->seg_ratio[i - 1]) { same_size = false; break; } @@ -177,13 +180,11 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params, } } else { n = snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "S%dLRU(%d", - params->n_seg, - (int)(params->lru_max_n_bytes[0] * 100 / cache->cache_size)); + params->n_seg, SLRU_seg_pct(params, 0)); for (int i = 1; i < params->n_seg; i++) { - n += - snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ":%d", - (int)(params->lru_max_n_bytes[i] * 100 / cache->cache_size)); + n += snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ":%d", + SLRU_seg_pct(params, i)); } } snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ")"); @@ -310,8 +311,9 @@ static cache_obj_t *SLRU_insert(cache_t *cache, const request_t *req) { if (nth_seg == -1) { // No space for insertion - while (cache->occupied_byte + req->obj_size + cache->obj_md_size > - cache->cache_size) { + 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) { cache->evict(cache, req); } nth_seg = 0; @@ -420,28 +422,24 @@ static bool SLRU_remove(cache_t *cache, obj_id_t obj_id) { // **** parameter set up functions **** // **** **** // *********************************************************************** -/* share of the cache given to one segment, in percent. lru_max_n_bytes is only - * allocated after the parameters are parsed, so it is still NULL when the user - * asks for the parameters with `-e print`; until seg-size says otherwise the - * segments are evenly sized */ -static int SLRU_seg_pct(const cache_t *cache, const SLRU_params_t *params, - const int seg) { - if (params->lru_max_n_bytes == NULL) { +/* share of one segment in percent; even until seg_ratio is set */ +static int SLRU_seg_pct(const SLRU_params_t *params, const int seg) { + if (params->seg_ratio[seg] == 0) { return 100 / params->n_seg; } - return (int)(params->lru_max_n_bytes[seg] * 100 / cache->cache_size); + return (int)lround(params->seg_ratio[seg] * 100); } -static const char *SLRU_current_params(cache_t *cache, SLRU_params_t *params) { +static const char *SLRU_current_params(SLRU_params_t *params) { static __thread char params_str[128]; int n = snprintf(params_str, sizeof(params_str), "n-seg=%d,seg-size=%d", - params->n_seg, SLRU_seg_pct(cache, params, 0)); + params->n_seg, SLRU_seg_pct(params, 0)); for (int i = 1; i < params->n_seg && n > 0 && n < (int)sizeof(params_str); i++) { n += snprintf(params_str + n, sizeof(params_str) - n, ":%d", - SLRU_seg_pct(cache, params, i)); + SLRU_seg_pct(params, i)); } return params_str; @@ -505,7 +503,7 @@ static void SLRU_parse_params(cache_t *cache, // give the rounding remainder to the last segment SLRU_resize(cache, cache->cache_size); } else if (strcasecmp(key, "print") == 0) { - printf("current parameters: %s\n", SLRU_current_params(cache, params)); + printf("current parameters: %s\n", SLRU_current_params(params)); free(old_params_str); exit(0); } else { @@ -564,8 +562,8 @@ static void SLRU_cool(cache_t *cache, const request_t *req, const int id) { prepend_obj_to_head(¶ms->lru_heads[id - 1], ¶ms->lru_tails[id - 1], obj); obj->SLRU.lru_id = id - 1; - params->lru_n_bytes[id] -= obj->obj_size; - params->lru_n_bytes[id - 1] += obj->obj_size; + params->lru_n_bytes[id] -= obj->obj_size + cache->obj_md_size; + params->lru_n_bytes[id - 1] += obj->obj_size + cache->obj_md_size; params->lru_n_objs[id]--; params->lru_n_objs[id - 1]++; @@ -605,7 +603,7 @@ static void _SLRU_verify_lru_size(cache_t *cache) { cache_obj_t *obj = params->lru_heads[i]; while (obj != NULL) { n_objs += 1; - n_bytes += obj->obj_size; + n_bytes += obj->obj_size + cache->obj_md_size; obj = obj->queue.next; } assert(n_objs == params->lru_n_objs[i]); diff --git a/test/test_cli.sh b/test/test_cli.sh index 6a28c3a0..22f622e8 100755 --- a/test/test_cli.sh +++ b/test/test_cli.sh @@ -375,8 +375,11 @@ expect_output "slru -e print" "n-seg=4,seg-size=25:25:25:25" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e print expect_output "slru -e n-seg=8,print" "n-seg=8" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e "n-seg=8,print" -expect_output "slru -e seg-size=1:2:3:4,print" "seg-size=9:19:29:40" \ +expect_output "slru -e seg-size=1:2:3:4,print" "seg-size=10:20:30:40" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e "seg-size=1:2:3:4,print" +expect_output "slru seg-size=1:1 name on an odd size" "S2LRU\\(50:50\\)" \ + "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 101 --ignore-obj-size=1 \ + -e "seg-size=1:1" expect_output "slru -e print with auto sizing" "n-seg=" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru auto -e print expect_output "qdlp -e print" "fifo-size-ratio=" \ diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index d874114f..7caf73cf 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 = {89624, 86725, 82781, 80203, 75388, 65645, 59035, 56063}, - .miss_byte_true = {4123085312, 3915534848, 3690704896, 3493027840, - 3174708736, 2661464064, 2507604992, 2439981056}}, + .miss_cnt_true = {89862, 86685, 83365, 80309, 75447, 66000, 59092, 56111}, + .miss_byte_true = {4125897728, 3909879808, 3697543168, 3489533952, + 3159642624, 2695841792, 2506203648, 2440419840}}, {.cache_name = "SR_LRU", .hashpower = 20, .req_cnt_true = 113872, From dcf876354c29728ba59a55028f25d6d3b20154b3 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Fri, 13 Nov 2026 23:42:03 +0100 Subject: [PATCH 3/4] fix(WTinyLFU): compare every admission once the main cache is full In byte mode the main cache evicts whole objects, so it can sit a little below its size. A window victim that fit that space was admitted without the frequency check, even long after the main cache had filled. Remember that the main cache has filled, and compare from then on; the hill climber clears this when it grows the main cache. A winning window victim larger than the main victim now evicts until it fits. Two tests push a cold object out of the window into a full main cache with free space: from SLRU segments that did not add up (count mode; fails on develop 4e3521a9) and from evicting whole objects (byte mode; fails without this fix). Count mode (--ignore-obj-size=1) is unchanged. --- libCacheSim/cache/eviction/WTinyLFU.c | 20 +++++++-- test/test_evictionAlgo.c | 60 +++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 2ad583c3..e5e55a5a 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -50,6 +50,7 @@ typedef struct WTinyLFU_params { char main_cache_type[32]; request_t *req_local; + bool main_full; // once full, every admission is compared bool hill_climb; int window_step; @@ -257,6 +258,9 @@ static void WTinyLFU_resize_window(cache_t *cache, const request_t *req) { double window_ratio = WTinyLFU_window_ratio(params->window_step); params->LRU->cache_size = (int64_t)(cache->cache_size * window_ratio); int64_t main_size = cache->cache_size - params->LRU->cache_size; + if (main_size > main_cache->cache_size) { + params->main_full = false; + } if (strcasecmp(params->main_cache_type, "SLRU") == 0) { SLRU_resize(main_cache, main_size); } else { @@ -361,6 +365,12 @@ static cache_obj_t *WTinyLFU_to_evict(cache_t *cache, const request_t *req) { return NULL; } +static bool WTinyLFU_main_fits(cache_t *main_cache, const request_t *req) { + return main_cache->get_occupied_byte(main_cache) + req->obj_size + + main_cache->obj_md_size <= + main_cache->cache_size; +} + static void WTinyLFU_evict(cache_t *cache, const request_t *req) { WTinyLFU_params_t *params = (WTinyLFU_params_t *)(cache->eviction_params); @@ -384,9 +394,8 @@ 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 (main_cache->get_occupied_byte(main_cache) + - params->req_local->obj_size + main_cache->obj_md_size <= - main_cache->cache_size) { + if (!params->main_full && + WTinyLFU_main_fits(main_cache, params->req_local)) { main_cache->insert(main_cache, params->req_local); #if defined(TRACK_DEMOTION) @@ -399,6 +408,7 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { } else { // compare the frequency of window_victim and main_cache_victim + params->main_full = true; cache_obj_t *main_cache_victim = main_cache->to_evict(main_cache, req); DEBUG_ASSERT(main_cache_victim != NULL); // if window_victim is more frequent, insert it into main_cache @@ -413,7 +423,9 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { window_victim->next_access_vtime); #endif - main_cache->evict(main_cache, req); + do { + main_cache->evict(main_cache, req); + } 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_evictionAlgo.c b/test/test_evictionAlgo.c index 7caf73cf..dbe3bc76 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -744,6 +744,61 @@ static void test_SLRU_seg_size_fills_cache(void) { cache->cache_free(cache); } +// a cold window victim must not take a free slot in a full main cache +static void test_WTinyLFU_full_main_checks_admission(void) { + common_cache_params_t cc_params = {.cache_size = 13, .hashpower = 8}; + cache_t *cache = WTinyLFU_init(cc_params, "window-size=0.2"); + request_t *req = new_request(); + req->obj_size = 1; + + for (obj_id_t id = 1; id <= 14; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + + // promoting 1 used to leave a free slot (segments of 2 and 8) + req->obj_id = 1; + g_assert_true(cache->get(cache, req)); + + for (obj_id_t id = 15; id <= 16; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + req->obj_id = 13; + g_assert_null(cache->find(cache, req, false)); + + free_request(req); + cache->cache_free(cache); +} + +// the same in byte mode, with a small object +static void test_WTinyLFU_full_main_checks_small_object(void) { + common_cache_params_t cc_params = {.cache_size = 1300, .hashpower = 8}; + cache_t *cache = WTinyLFU_init(cc_params, "main-cache=LRU,window-size=0.2"); + request_t *req = new_request(); + + req->obj_size = 100; + for (obj_id_t id = 1; id <= 14; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + + // main holds 1000 of 1040 bytes, so 15 fits + req->obj_id = 15; + req->obj_size = 30; + g_assert_false(cache->get(cache, req)); + req->obj_size = 100; + for (obj_id_t id = 16; id <= 18; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + req->obj_id = 15; + g_assert_null(cache->find(cache, req, false)); + + free_request(req); + cache->cache_free(cache); +} + /* The MINISIM profiler only knows an algorithm by its name and reaches it * through create_cache_using_plugin(). That used to go straight to dlsym(), * which cannot see a built-in constructor in a statically linked build — the @@ -853,6 +908,11 @@ int main(int argc, char *argv[]) { g_test_add_func("/libCacheSim/cacheAlgo_SLRU_resize", test_SLRU_resize); g_test_add_func("/libCacheSim/cacheAlgo_SLRU_seg_size_fills_cache", test_SLRU_seg_size_fills_cache); + g_test_add_func("/libCacheSim/cacheAlgo_WTinyLFU_full_main_checks_admission", + test_WTinyLFU_full_main_checks_admission); + g_test_add_func( + "/libCacheSim/cacheAlgo_WTinyLFU_full_main_checks_small_object", + test_WTinyLFU_full_main_checks_small_object); g_test_add_func("/libCacheSim/cacheAlgo_SLRU_resize_keeps_large_object", test_SLRU_resize_keeps_large_object); From 497a50e662f773f2afcae2009a41fdd4426fd695 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sat, 14 Nov 2026 00:34:08 +0100 Subject: [PATCH 4/4] fix(SLRU): free the cache on -e print and bound the name loops -e print exits from SLRU_parse_params() without freeing the cache. LeakSanitizer stayed quiet only while the cache pointer was still live in the print call; since the label change no longer passes it, the Ubuntu job reports the whole cache as leaked. Free it before exiting. The cache name loops now stop once the name fills the buffer, as the -e print loop already does. --- libCacheSim/cache/eviction/SLRU.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index 7cbaa8ef..82c48111 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -174,7 +174,7 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params, if (same_size) { n = snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "S%dLRU(%d", params->n_seg, (int)(100 / params->n_seg)); - for (int i = 1; i < params->n_seg; i++) { + for (int i = 1; i < params->n_seg && n < CACHE_NAME_ARRAY_LEN; i++) { n += snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ":%d", (int)(100 / params->n_seg)); } @@ -182,12 +182,14 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params, n = snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "S%dLRU(%d", params->n_seg, SLRU_seg_pct(params, 0)); - for (int i = 1; i < params->n_seg; i++) { + for (int i = 1; i < params->n_seg && n < CACHE_NAME_ARRAY_LEN; i++) { n += snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ":%d", SLRU_seg_pct(params, i)); } } - snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ")"); + if (n < CACHE_NAME_ARRAY_LEN) { + snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ")"); + } #ifdef USE_BELADY char *tmp = strdup(cache->cache_name); @@ -505,6 +507,7 @@ static void SLRU_parse_params(cache_t *cache, } else if (strcasecmp(key, "print") == 0) { printf("current parameters: %s\n", SLRU_current_params(params)); free(old_params_str); + SLRU_free(cache); exit(0); } else { ERROR("%s does not have parameter %s\n", cache->cache_name, key);