From e25fcd88efe8fc65cd124944986793176489b7c2 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sun, 8 Nov 2026 01:29:40 +0100 Subject: [PATCH 1/6] feat(WTinyLFU): adapt the window size with a hill climber (#21) Add hill-climb=1, the window climber (WC-W-TinyLFU) of Adaptive Software Cache Management (Middleware'18). After every 10 x cache size bytes of requests it compares the hit ratio with the previous period and moves the window 5% in the same direction if it improved, the other way if not, between 1% and 80%. The window already holds whatever the main cache does not use, so a resize only sets the main cache's size: the main cache evicts its own victims when the window grows, and fills with window victims when it shrinks. SLRU_resize() keeps each SLRU segment's share of the cache. Off by default, and results without hill-climb=1 are unchanged. Only LRU and SLRU main caches are supported. --- libCacheSim/cache/eviction/SLRU.c | 22 +++++ libCacheSim/cache/eviction/WTinyLFU.c | 98 ++++++++++++++++++- .../include/libCacheSim/evictionAlgo.h | 3 + test/test_cli.sh | 13 ++- test/test_evictionAlgo.c | 66 +++++++++++++ 5 files changed, 196 insertions(+), 6 deletions(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index 875f09e4..33223b17 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -26,6 +26,7 @@ typedef struct SLRU_params { int64_t *lru_n_objs; int64_t *lru_max_n_bytes; int n_seg; + double seg_ratio[SLRU_MAX_N_SEG]; // share of the cache, used by SLRU_resize } SLRU_params_t; // *********************************************************************** @@ -143,6 +144,11 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params, } } + for (int i = 0; i < params->n_seg; i++) { + params->seg_ratio[i] = + (double)params->lru_max_n_bytes[i] / ccache_params.cache_size; + } + params->lru_heads = (cache_obj_t **)malloc(sizeof(cache_obj_t *) * params->n_seg); params->lru_tails = @@ -514,6 +520,22 @@ static void SLRU_parse_params(cache_t *cache, // **** internal functions **** // **** **** // *********************************************************************** +/** + * @brief change the cache size, each segment keeps its share of the cache. + * No object is moved or evicted here: a segment over its new size shrinks on + * the next promotion, and the cache on the next eviction + */ +void SLRU_resize(cache_t *cache, int64_t new_size) { + SLRU_params_t *params = (SLRU_params_t *)(cache->eviction_params); + int64_t n_assigned = 0; + for (int i = 0; i < params->n_seg - 1; i++) { + params->lru_max_n_bytes[i] = (int64_t)(params->seg_ratio[i] * new_size); + n_assigned += params->lru_max_n_bytes[i]; + } + params->lru_max_n_bytes[params->n_seg - 1] = new_size - n_assigned; + cache->cache_size = new_size; +} + /* SLRU cannot insert an object larger than segment size */ bool SLRU_can_insert(cache_t *cache, const request_t *req) { SLRU_params_t *params = (SLRU_params_t *)cache->eviction_params; diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 0c91b6d4..c96849d8 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -4,6 +4,19 @@ // combined with a windowed LRU structure (W-TinyLFU) // to resist burst traffic of temporal hot objects // +// hill-climb=1 adapts the window size with the hill climber of +// Adaptive Software Cache Management (Middleware'18, WC-W-TinyLFU): +// after every 10 x cache size of requests, compare the hit ratio with the +// previous period, keep moving the window in the same direction (5% steps +// between 1% and 80%) if it improved, turn around if not. +// Differences from the paper: the window does not go down to 0% (every +// object enters through the window, so 0% misses on every request), and a +// period is 10 x cache size bytes of requests, not 10 x cache size requests +// (the same when objects have unit size). Objects are not moved when the +// window changes: when the window grows, the main cache evicts its own +// victims down to its new size; when it shrinks, the main cache fills up +// with window victims +// // NOTE: it is NOT thread-safe // // part of the details borrowed from https://github.com/mandreyel/w-tinylfu @@ -41,10 +54,24 @@ typedef struct WTinyLFU_params { char main_cache_type[32]; request_t *req_local; + + bool hill_climb; + int window_step; // see WTinyLFU_window_ratio + int climb_direction; // 1 grows the window, -1 shrinks it + int64_t n_climb_req; // requests, hits and bytes in this period + int64_t n_climb_hit; + int64_t n_climb_byte; + double prev_hit_ratio; } WTinyLFU_params_t; static const char *DEFAULT_PARAMS = "main-cache=SLRU,window-size=0.01"; +/* the window sizes the hill climber moves between: 1%, 5%, 10%, ..., 80% */ +#define N_WINDOW_STEP 16 +static double WTinyLFU_window_ratio(int step) { + return step == 0 ? 0.01 : 0.05 * step; +} + // *********************************************************************** // **** **** // **** function declarations **** @@ -110,6 +137,18 @@ cache_t *WTinyLFU_init(const common_cache_params_t ccache_params, WTinyLFU_parse_params(cache, cache_specific_params); } + if (params->hill_climb) { + if (strcasecmp(params->main_cache_type, "LRU") != 0 && + strcasecmp(params->main_cache_type, "SLRU") != 0) { + ERROR("WTinyLFU hill-climb only supports LRU and SLRU main caches\n"); + } + // start from the step closest to window-size + params->window_step = (int)(params->window_size / 0.05 + 0.5); + params->window_step = MIN(params->window_step, N_WINDOW_STEP); + params->window_size = WTinyLFU_window_ratio(params->window_step); + params->climb_direction = 1; + } + common_cache_params_t ccache_params_local = ccache_params; ccache_params_local.cache_size *= params->window_size; @@ -163,8 +202,9 @@ cache_t *WTinyLFU_init(const common_cache_params_t ccache_params, cache->obj_md_size = params->LRU->obj_md_size; } - snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "WTinyLFU-w%.2lf-%s", - params->window_size, params->main_cache_type); + snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "WTinyLFU-w%.2lf-%s%s", + params->window_size, params->main_cache_type, + params->hill_climb ? "-climb" : ""); params->req_local = new_request(); params->n_admit_bytes = 0; @@ -216,12 +256,56 @@ static void WTinyLFU_free(cache_t *cache) { cache_struct_free(cache); } +/* move the window to a new size, the main cache gets the rest */ +static void WTinyLFU_resize_window(cache_t *cache, const request_t *req, + double window_ratio) { + WTinyLFU_params_t *params = (WTinyLFU_params_t *)(cache->eviction_params); + params->LRU->cache_size = (int64_t)(cache->cache_size * window_ratio); + int64_t main_size = cache->cache_size - params->LRU->cache_size; + if (strcasecmp(params->main_cache_type, "SLRU") == 0) { + SLRU_resize(params->main_cache, main_size); + } else { + params->main_cache->cache_size = main_size; + } + // the window has grown, shrink the main cache to its new size now + cache_t *main_cache = params->main_cache; + while (main_cache->get_occupied_byte(main_cache) > main_cache->cache_size) { + main_cache->evict(main_cache, req); + } +} + +static void WTinyLFU_climb(cache_t *cache, const request_t *req, bool hit) { + WTinyLFU_params_t *params = (WTinyLFU_params_t *)(cache->eviction_params); + params->n_climb_req += 1; + params->n_climb_hit += hit; + params->n_climb_byte += req->obj_size; + if (params->n_climb_byte < 10 * cache->cache_size) { + return; + } + + double hit_ratio = (double)params->n_climb_hit / params->n_climb_req; + if (hit_ratio <= params->prev_hit_ratio) { + params->climb_direction = -params->climb_direction; + } + params->prev_hit_ratio = hit_ratio; + params->n_climb_req = params->n_climb_hit = params->n_climb_byte = 0; + + params->window_step += params->climb_direction; + params->window_step = MAX(0, MIN(N_WINDOW_STEP, params->window_step)); + WTinyLFU_resize_window(cache, req, + WTinyLFU_window_ratio(params->window_step)); +} + static bool WTinyLFU_get(cache_t *cache, const request_t *req) { /* because this field cannot be updated in time since segment LRUs are * updated, so we should not use this field */ DEBUG_ASSERT(cache->occupied_byte == 0); + WTinyLFU_params_t *params = (WTinyLFU_params_t *)(cache->eviction_params); bool ck = cache_get_base(cache, req); + if (params->hill_climb) { + WTinyLFU_climb(cache, req, ck); + } return ck; } @@ -383,10 +467,10 @@ static bool WTinyLFU_remove(cache_t *cache, obj_id_t obj_id) { * configured type, which is what `-e print` runs against */ static const char *WTinyLFU_current_params(WTinyLFU_params_t *params) { static __thread char params_str[128]; - snprintf(params_str, 128, "main-cache=%s,window-size=%.4lf", + snprintf(params_str, 128, "main-cache=%s,window-size=%.4lf,hill-climb=%d", params->main_cache == NULL ? params->main_cache_type : params->main_cache->cache_name, - params->window_size); + params->window_size, params->hill_climb); return params_str; } @@ -418,6 +502,12 @@ static void WTinyLFU_parse_params(cache_t *cache, error = "expected a nonempty value"; } else if (strcasecmp(key, "main-cache") == 0) { strncpy(params->main_cache_type, value, 30); + } else if (strcasecmp(key, "hill-climb") == 0) { + if (strcmp(value, "0") != 0 && strcmp(value, "1") != 0) { + error = "expected 0 or 1"; + } else { + params->hill_climb = value[0] == '1'; + } } else if (strcasecmp(key, "window-size") == 0) { char *end; errno = 0; diff --git a/libCacheSim/include/libCacheSim/evictionAlgo.h b/libCacheSim/include/libCacheSim/evictionAlgo.h index 811cbe20..5b9e0385 100644 --- a/libCacheSim/include/libCacheSim/evictionAlgo.h +++ b/libCacheSim/include/libCacheSim/evictionAlgo.h @@ -224,6 +224,9 @@ cache_t *Size_init(const common_cache_params_t ccache_params, cache_t *SLRU_init(const common_cache_params_t ccache_params, const char *cache_specific_params); +/* change the size of an SLRU cache, used by WTinyLFU's hill climber */ +void SLRU_resize(cache_t *cache, int64_t new_size); + cache_t *SLRUv0_init(const common_cache_params_t ccache_params, const char *cache_specific_params); diff --git a/test/test_cli.sh b/test/test_cli.sh index 93c59b03..8cea69bd 100755 --- a/test/test_cli.sh +++ b/test/test_cli.sh @@ -307,7 +307,8 @@ for params in main-cache main-cache= main-cache,print \ window-size=abc,print window-size=0.1junk,print window-size=nan \ window-size=nan,print window-size=inf,print window-size=-inf,print \ window-size=-0.1,print window-size=1,print window-size=1e999,print \ - window-size=1e-999,print unknown=1; do + window-size=1e-999,print hill-climb hill-climb= hill-climb=2,print \ + hill-climb=yes,print unknown=1; do out=$("${BIN_DIR}/cachesim" "${TRACE}" vscsi wtinyLFU 1mb \ --num-req=10 --hashpower=8 -e "${params}" 2>&1) rc=$? @@ -320,7 +321,8 @@ for params in main-cache main-cache= main-cache,print \ fi done for params in '' window-size=0.01 window-size=1e-2 window-size=0.01, \ - 'main-cache=LRU,window-size=0.01' 'main-cache=FIFO, window-size=0.01'; do + 'main-cache=LRU,window-size=0.01' 'main-cache=FIFO, window-size=0.01' \ + hill-climb=0 hill-climb=1 'main-cache=LRU,hill-climb=1'; do expect_output "WTinyLFU accepts '${params}'" '10 req' \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi tinyLFU 1mb \ --num-req=10 --hashpower=8 -e "${params}" @@ -333,6 +335,13 @@ for spec in 0:0.0000 0.01:0.0100 1e-2:0.0100 +0.01:0.0100 \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi wtinyLFU 1mb \ --hashpower=8 -e "window-size=${value},print" done +expect_output "WTinyLFU reports hill-climb=1" "hill-climb=1" \ + "${BIN_DIR}/cachesim" "${TRACE}" vscsi wtinyLFU 1mb \ + --hashpower=8 -e "hill-climb=1,print" +# the hill climber resizes the main cache, which only LRU and SLRU support +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" echo "running admission and prefetch missing-value tests" diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index db3500fe..5dbfc4a1 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -623,6 +623,69 @@ static void test_WTinyLFU_counts_window_hits(void) { cache->cache_free(cache); } +// The hill climber decides after every 10 x cache size of requests. The first +// decision always grows the window, from 1% to 5% here, and the main cache +// evicts its own victims down to its new size right away. +static void test_WTinyLFU_hill_climb_grows_window(void) { + common_cache_params_t cc_params = {.cache_size = 100, .hashpower = 8}; + cache_t *cache = WTinyLFU_init(cc_params, "main-cache=LRU,hill-climb=1"); + request_t *req = new_request(); + req->obj_size = 1; + + // 1 to 100 fill the cache, and 101 moves 1 to 99 into the main cache + for (obj_id_t id = 1; id <= 101; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + g_assert_cmpint(cache->get_n_obj(cache), ==, 100); + + // hits fill up the first period of 1000 requests + for (int i = 0; i < 899; i++) { + req->obj_id = 2 + i % 98; + g_assert_true(cache->get(cache, req)); + } + + // the window is now 5 objects, the main cache 95 instead of 99 + g_assert_cmpint(cache->get_n_obj(cache), ==, 96); + + // the next four objects fit without an eviction + for (obj_id_t id = 102; id <= 105; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + g_assert_cmpint(cache->get_n_obj(cache), ==, 100); + + free_request(req); + cache->cache_free(cache); +} + +// SLRU_resize keeps each segment's share of the cache: with seg-size=1:4 the +// lowest segment, which caps the object size, is 20% of the cache. Resizing +// to 333 rounds it down to 66; going back must give 200 again, not 198 as +// scaling the rounded size would. +static void test_SLRU_resize(void) { + common_cache_params_t cc_params = {.cache_size = 1000, .hashpower = 8}; + cache_t *cache = SLRU_init(cc_params, "seg-size=1:4"); + request_t *req = new_request(); + req->obj_id = 1; + + SLRU_resize(cache, 333); + g_assert_cmpint(cache->cache_size, ==, 333); + req->obj_size = 66; + g_assert_true(cache->can_insert(cache, req)); + req->obj_size = 67; + g_assert_false(cache->can_insert(cache, req)); + + SLRU_resize(cache, 1000); + req->obj_size = 200; + g_assert_true(cache->can_insert(cache, req)); + req->obj_size = 201; + g_assert_false(cache->can_insert(cache, req)); + + 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 @@ -725,6 +788,9 @@ int main(int argc, char *argv[]) { test_Clock2QPlus); g_test_add_func("/libCacheSim/cacheAlgo_WTinyLFU_counts_window_hits", test_WTinyLFU_counts_window_hits); + g_test_add_func("/libCacheSim/cacheAlgo_WTinyLFU_hill_climb_grows_window", + test_WTinyLFU_hill_climb_grows_window); + g_test_add_func("/libCacheSim/cacheAlgo_SLRU_resize", test_SLRU_resize); #if defined(ENABLE_3L_CACHE) && ENABLE_3L_CACHE == 1 g_test_add_data_func("/libCacheSim/cacheAlgo_3LCache", reader, test_3LCache); From 8c331154f4fc74ee08996133988bea3041d8ba29 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sun, 8 Nov 2026 01:38:31 +0100 Subject: [PATCH 2/6] refactor(WTinyLFU): shorten the hill climber comments Drop comments that repeat the code, keep the paper deviations in the file header, and let WTinyLFU_resize_window() read the step itself. No change in results. --- libCacheSim/cache/eviction/SLRU.c | 8 +-- libCacheSim/cache/eviction/WTinyLFU.c | 50 ++++++++----------- .../include/libCacheSim/evictionAlgo.h | 1 - test/test_cli.sh | 1 - test/test_evictionAlgo.c | 19 +++---- 5 files changed, 30 insertions(+), 49 deletions(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index 33223b17..f8e028b9 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -26,7 +26,7 @@ typedef struct SLRU_params { int64_t *lru_n_objs; int64_t *lru_max_n_bytes; int n_seg; - double seg_ratio[SLRU_MAX_N_SEG]; // share of the cache, used by SLRU_resize + double seg_ratio[SLRU_MAX_N_SEG]; } SLRU_params_t; // *********************************************************************** @@ -520,11 +520,7 @@ static void SLRU_parse_params(cache_t *cache, // **** internal functions **** // **** **** // *********************************************************************** -/** - * @brief change the cache size, each segment keeps its share of the cache. - * No object is moved or evicted here: a segment over its new size shrinks on - * the next promotion, and the cache on the next eviction - */ +/* change the cache size and keep the share of each segment, no eviction */ void SLRU_resize(cache_t *cache, int64_t new_size) { SLRU_params_t *params = (SLRU_params_t *)(cache->eviction_params); int64_t n_assigned = 0; diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index c96849d8..8032e8eb 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -4,18 +4,12 @@ // combined with a windowed LRU structure (W-TinyLFU) // to resist burst traffic of temporal hot objects // -// hill-climb=1 adapts the window size with the hill climber of -// Adaptive Software Cache Management (Middleware'18, WC-W-TinyLFU): -// after every 10 x cache size of requests, compare the hit ratio with the -// previous period, keep moving the window in the same direction (5% steps -// between 1% and 80%) if it improved, turn around if not. -// Differences from the paper: the window does not go down to 0% (every -// object enters through the window, so 0% misses on every request), and a -// period is 10 x cache size bytes of requests, not 10 x cache size requests -// (the same when objects have unit size). Objects are not moved when the -// window changes: when the window grows, the main cache evicts its own -// victims down to its new size; when it shrinks, the main cache fills up -// with window victims +// hill-climb=1 adapts the window size with the hill climber in Adaptive +// Software Cache Management (Middleware'18): every 10 x cache size bytes of +// requests, move the window by 5% (between 1% and 80%), in the same +// direction if the hit ratio improved, otherwise in the other direction. +// Different from the paper, the window does not go down to 0% (every request +// would miss), and the period is in bytes (the same for unit-size objects) // // NOTE: it is NOT thread-safe // @@ -56,9 +50,9 @@ typedef struct WTinyLFU_params { request_t *req_local; bool hill_climb; - int window_step; // see WTinyLFU_window_ratio - int climb_direction; // 1 grows the window, -1 shrinks it - int64_t n_climb_req; // requests, hits and bytes in this period + int window_step; + int climb_direction; + int64_t n_climb_req; int64_t n_climb_hit; int64_t n_climb_byte; double prev_hit_ratio; @@ -66,8 +60,7 @@ typedef struct WTinyLFU_params { static const char *DEFAULT_PARAMS = "main-cache=SLRU,window-size=0.01"; -/* the window sizes the hill climber moves between: 1%, 5%, 10%, ..., 80% */ -#define N_WINDOW_STEP 16 +#define MAX_WINDOW_STEP 16 static double WTinyLFU_window_ratio(int step) { return step == 0 ? 0.01 : 0.05 * step; } @@ -142,9 +135,8 @@ cache_t *WTinyLFU_init(const common_cache_params_t ccache_params, strcasecmp(params->main_cache_type, "SLRU") != 0) { ERROR("WTinyLFU hill-climb only supports LRU and SLRU main caches\n"); } - // start from the step closest to window-size - params->window_step = (int)(params->window_size / 0.05 + 0.5); - params->window_step = MIN(params->window_step, N_WINDOW_STEP); + params->window_step = + (int)MIN(lround(params->window_size / 0.05), MAX_WINDOW_STEP); params->window_size = WTinyLFU_window_ratio(params->window_step); params->climb_direction = 1; } @@ -256,19 +248,18 @@ static void WTinyLFU_free(cache_t *cache) { cache_struct_free(cache); } -/* move the window to a new size, the main cache gets the rest */ -static void WTinyLFU_resize_window(cache_t *cache, const request_t *req, - double window_ratio) { +static void WTinyLFU_resize_window(cache_t *cache, const request_t *req) { WTinyLFU_params_t *params = (WTinyLFU_params_t *)(cache->eviction_params); + cache_t *main_cache = params->main_cache; + 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 (strcasecmp(params->main_cache_type, "SLRU") == 0) { - SLRU_resize(params->main_cache, main_size); + SLRU_resize(main_cache, main_size); } else { - params->main_cache->cache_size = main_size; + main_cache->cache_size = main_size; } - // the window has grown, shrink the main cache to its new size now - cache_t *main_cache = params->main_cache; + // if the window has grown, the main cache evicts down to its new size while (main_cache->get_occupied_byte(main_cache) > main_cache->cache_size) { main_cache->evict(main_cache, req); } @@ -291,9 +282,8 @@ static void WTinyLFU_climb(cache_t *cache, const request_t *req, bool hit) { params->n_climb_req = params->n_climb_hit = params->n_climb_byte = 0; params->window_step += params->climb_direction; - params->window_step = MAX(0, MIN(N_WINDOW_STEP, params->window_step)); - WTinyLFU_resize_window(cache, req, - WTinyLFU_window_ratio(params->window_step)); + params->window_step = MAX(0, MIN(MAX_WINDOW_STEP, params->window_step)); + WTinyLFU_resize_window(cache, req); } static bool WTinyLFU_get(cache_t *cache, const request_t *req) { diff --git a/libCacheSim/include/libCacheSim/evictionAlgo.h b/libCacheSim/include/libCacheSim/evictionAlgo.h index 5b9e0385..80b9f520 100644 --- a/libCacheSim/include/libCacheSim/evictionAlgo.h +++ b/libCacheSim/include/libCacheSim/evictionAlgo.h @@ -224,7 +224,6 @@ cache_t *Size_init(const common_cache_params_t ccache_params, cache_t *SLRU_init(const common_cache_params_t ccache_params, const char *cache_specific_params); -/* change the size of an SLRU cache, used by WTinyLFU's hill climber */ void SLRU_resize(cache_t *cache, int64_t new_size); cache_t *SLRUv0_init(const common_cache_params_t ccache_params, diff --git a/test/test_cli.sh b/test/test_cli.sh index 8cea69bd..2d084c94 100755 --- a/test/test_cli.sh +++ b/test/test_cli.sh @@ -338,7 +338,6 @@ done expect_output "WTinyLFU reports hill-climb=1" "hill-climb=1" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi wtinyLFU 1mb \ --hashpower=8 -e "hill-climb=1,print" -# the hill climber resizes the main cache, which only LRU and SLRU support 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" diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 5dbfc4a1..3d7ceee8 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -623,32 +623,31 @@ static void test_WTinyLFU_counts_window_hits(void) { cache->cache_free(cache); } -// The hill climber decides after every 10 x cache size of requests. The first -// decision always grows the window, from 1% to 5% here, and the main cache -// evicts its own victims down to its new size right away. +// The first hill climbing decision, after 10 x cache size requests, grows the +// window from 1% to 5%, and the main cache evicts down to its new size. static void test_WTinyLFU_hill_climb_grows_window(void) { common_cache_params_t cc_params = {.cache_size = 100, .hashpower = 8}; cache_t *cache = WTinyLFU_init(cc_params, "main-cache=LRU,hill-climb=1"); request_t *req = new_request(); req->obj_size = 1; - // 1 to 100 fill the cache, and 101 moves 1 to 99 into the main cache + // 101 moves 1 to 99 into the main cache for (obj_id_t id = 1; id <= 101; id++) { req->obj_id = id; g_assert_false(cache->get(cache, req)); } g_assert_cmpint(cache->get_n_obj(cache), ==, 100); - // hits fill up the first period of 1000 requests + // hits on the main cache until the 1000th request for (int i = 0; i < 899; i++) { req->obj_id = 2 + i % 98; g_assert_true(cache->get(cache, req)); } - // the window is now 5 objects, the main cache 95 instead of 99 + // the main cache is now 95 objects instead of 99 g_assert_cmpint(cache->get_n_obj(cache), ==, 96); - // the next four objects fit without an eviction + // the window grows into the free space for (obj_id_t id = 102; id <= 105; id++) { req->obj_id = id; g_assert_false(cache->get(cache, req)); @@ -659,10 +658,8 @@ static void test_WTinyLFU_hill_climb_grows_window(void) { cache->cache_free(cache); } -// SLRU_resize keeps each segment's share of the cache: with seg-size=1:4 the -// lowest segment, which caps the object size, is 20% of the cache. Resizing -// to 333 rounds it down to 66; going back must give 200 again, not 198 as -// scaling the rounded size would. +// SLRU_resize keeps the 1:4 shares. The first segment, which caps the object +// size, is 66 at size 333 and 200 again (not 198) back at size 1000. static void test_SLRU_resize(void) { common_cache_params_t cc_params = {.cache_size = 1000, .hashpower = 8}; cache_t *cache = SLRU_init(cc_params, "seg-size=1:4"); From 9dfb1a4653ea5a0d3a77b19e1c532456bb176e3e Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sun, 8 Nov 2026 02:08:12 +0100 Subject: [PATCH 3/6] fix(SLRU): keep the configured segment shares when resizing SLRU_resize() derived each segment's share from its truncated initial size, so a resize back to the original size could leave the first segment one byte smaller (e.g. 13 instead of 14 at size 71, for 2,868 of the sizes up to 200,000 with seg-size=1:4). Store the configured share where the segments are first sized, and size them from it in both places. --- libCacheSim/cache/eviction/SLRU.c | 10 +++------- test/test_evictionAlgo.c | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index f8e028b9..37410af3 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -141,14 +141,10 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params, params->lru_max_n_bytes[i] = (int64_t)(ccache_params.cache_size / params->n_seg) + (i >= params->n_seg - remainder); + params->seg_ratio[i] = 1.0 / params->n_seg; } } - for (int i = 0; i < params->n_seg; i++) { - params->seg_ratio[i] = - (double)params->lru_max_n_bytes[i] / ccache_params.cache_size; - } - params->lru_heads = (cache_obj_t **)malloc(sizeof(cache_obj_t *) * params->n_seg); params->lru_tails = @@ -495,9 +491,9 @@ static void SLRU_parse_params(cache_t *cache, params->n_seg = n_seg; params->lru_max_n_bytes = calloc(params->n_seg, sizeof(int64_t)); for (int i = 0; i < n_seg; i++) { + params->seg_ratio[i] = (double)seg_size_array[i] / seg_size_sum; params->lru_max_n_bytes[i] = - (int64_t)((double)seg_size_array[i] / seg_size_sum * - cache->cache_size); + (int64_t)(params->seg_ratio[i] * cache->cache_size); if (params->lru_max_n_bytes[i] <= 0) { ERROR("Invalid segment size for segment %d: %lld bytes\n", i, (long long)params->lru_max_n_bytes[i]); diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 3d7ceee8..3db7813c 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -658,25 +658,26 @@ static void test_WTinyLFU_hill_climb_grows_window(void) { cache->cache_free(cache); } -// SLRU_resize keeps the 1:4 shares. The first segment, which caps the object -// size, is 66 at size 333 and 200 again (not 198) back at size 1000. +// SLRU_resize keeps the configured share of each segment. With seg-size=1:4 +// the first segment, which caps the object size, is 14 at size 71, 6 at size +// 30, and 14 again back at size 71. static void test_SLRU_resize(void) { - common_cache_params_t cc_params = {.cache_size = 1000, .hashpower = 8}; + common_cache_params_t cc_params = {.cache_size = 71, .hashpower = 8}; cache_t *cache = SLRU_init(cc_params, "seg-size=1:4"); request_t *req = new_request(); req->obj_id = 1; - SLRU_resize(cache, 333); - g_assert_cmpint(cache->cache_size, ==, 333); - req->obj_size = 66; + SLRU_resize(cache, 30); + g_assert_cmpint(cache->cache_size, ==, 30); + req->obj_size = 6; g_assert_true(cache->can_insert(cache, req)); - req->obj_size = 67; + req->obj_size = 7; g_assert_false(cache->can_insert(cache, req)); - SLRU_resize(cache, 1000); - req->obj_size = 200; + SLRU_resize(cache, 71); + req->obj_size = 14; g_assert_true(cache->can_insert(cache, req)); - req->obj_size = 201; + req->obj_size = 15; g_assert_false(cache->can_insert(cache, req)); free_request(req); From 695263450f25b811ae32d3696f4ee7922506fcae Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sun, 8 Nov 2026 02:08:12 +0100 Subject: [PATCH 4/6] fix(WTinyLFU): keep the climbing direction on a tie Turning around on a tie traps the climber when no object fits in the 1% and 5% windows: every period has no hit, so it switches between them forever and the cache never gets a hit (e.g. 10 and 19 objects, or small caches with object metadata). Keep the direction unless the hit ratio dropped, as Caffeine does. --- libCacheSim/cache/eviction/WTinyLFU.c | 8 +++++--- test/test_evictionAlgo.c | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 8032e8eb..2344ef2b 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -7,9 +7,11 @@ // hill-climb=1 adapts the window size with the hill climber in Adaptive // Software Cache Management (Middleware'18): every 10 x cache size bytes of // requests, move the window by 5% (between 1% and 80%), in the same -// direction if the hit ratio improved, otherwise in the other direction. +// direction unless the hit ratio dropped, otherwise in the other direction. // Different from the paper, the window does not go down to 0% (every request -// would miss), and the period is in bytes (the same for unit-size objects) +// would miss), a tie keeps the direction (as in Caffeine, otherwise a cache +// without hits switches between 1% and 5% forever), and the period is in +// bytes (the same for unit-size objects) // // NOTE: it is NOT thread-safe // @@ -275,7 +277,7 @@ static void WTinyLFU_climb(cache_t *cache, const request_t *req, bool hit) { } double hit_ratio = (double)params->n_climb_hit / params->n_climb_req; - if (hit_ratio <= params->prev_hit_ratio) { + if (hit_ratio < params->prev_hit_ratio) { params->climb_direction = -params->climb_direction; } params->prev_hit_ratio = hit_ratio; diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 3db7813c..264a5db1 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -658,6 +658,28 @@ static void test_WTinyLFU_hill_climb_grows_window(void) { cache->cache_free(cache); } +// A period without hits must not trap the climber: with 19 objects the 1% and +// 5% windows hold no object, so it has to keep growing the window to 10%. +static void test_WTinyLFU_hill_climb_no_hits(void) { + common_cache_params_t cc_params = {.cache_size = 19, .hashpower = 8}; + cache_t *cache = WTinyLFU_init(cc_params, "main-cache=LRU,hill-climb=1"); + request_t *req = new_request(); + req->obj_size = 1; + + int64_t n_hit = 0; + for (int i = 0; i < 1000; i++) { + req->obj_id = 1 + i % 5; + bool hit = cache->get(cache, req); + // the first two periods of 190 requests run with an empty window + if (i < 380) g_assert_false(hit); + n_hit += hit; + } + g_assert_cmpint(n_hit, >, 0); + + free_request(req); + cache->cache_free(cache); +} + // SLRU_resize keeps the configured share of each segment. With seg-size=1:4 // the first segment, which caps the object size, is 14 at size 71, 6 at size // 30, and 14 again back at size 71. @@ -788,6 +810,8 @@ int main(int argc, char *argv[]) { test_WTinyLFU_counts_window_hits); g_test_add_func("/libCacheSim/cacheAlgo_WTinyLFU_hill_climb_grows_window", test_WTinyLFU_hill_climb_grows_window); + 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); #if defined(ENABLE_3L_CACHE) && ENABLE_3L_CACHE == 1 From 7fb111ef51327f6ec10a0f6faf993171e48850cd Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sun, 8 Nov 2026 02:25:15 +0100 Subject: [PATCH 5/6] fix(SLRU): keep an object that does not fit the next segment in place After SLRU_resize() a resident object can be larger than the next segment. On a hit, SLRU_find() promoted it, cooled it back down and evicted it, then read the freed object (heap-use-after-free under ASan, e.g. a 20-byte object after resizing a 100-byte 1:4 SLRU to 20 bytes). Move it to the head of its own segment instead. Objects that fit the next segment, which is every object in the default layouts, are promoted as before. --- libCacheSim/cache/eviction/SLRU.c | 8 +++++--- test/test_evictionAlgo.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index 37410af3..64d8212a 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -263,9 +263,11 @@ static cache_obj_t *SLRU_find(cache_t *cache, const request_t *req, } #endif - if (obj->SLRU.lru_id == params->n_seg - 1) { - move_obj_to_head(¶ms->lru_heads[params->n_seg - 1], - ¶ms->lru_tails[params->n_seg - 1], obj); + int id = obj->SLRU.lru_id; + if (id == params->n_seg - 1 || + obj->obj_size + cache->obj_md_size > params->lru_max_n_bytes[id + 1]) { + // an object larger than the next segment (after SLRU_resize) stays here + move_obj_to_head(¶ms->lru_heads[id], ¶ms->lru_tails[id], obj); } else { SLRU_promote_to_next_seg(cache, req, obj); diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 264a5db1..96bae928 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -706,6 +706,25 @@ static void test_SLRU_resize(void) { cache->cache_free(cache); } +// After SLRU_resize a resident object can be larger than the next segment. +// A hit must keep it in place: promoting it would cool it back down and +// evict it while SLRU_find still uses it. +static void test_SLRU_resize_keeps_large_object(void) { + common_cache_params_t cc_params = {.cache_size = 100, .hashpower = 8}; + cache_t *cache = SLRU_init(cc_params, "seg-size=1:4"); + request_t *req = new_request(); + req->obj_id = 1; + req->obj_size = 20; + + g_assert_false(cache->get(cache, req)); + SLRU_resize(cache, 20); // segments of 4 and 16 bytes + g_assert_true(cache->get(cache, req)); + g_assert_nonnull(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 @@ -813,6 +832,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_resize_keeps_large_object", + test_SLRU_resize_keeps_large_object); #if defined(ENABLE_3L_CACHE) && ENABLE_3L_CACHE == 1 g_test_add_data_func("/libCacheSim/cacheAlgo_3LCache", reader, test_3LCache); From 631a3d2e8690fff1311c0e82d2fae0a979c98f74 Mon Sep 17 00:00:00 2001 From: Marc Frances Date: Sun, 8 Nov 2026 02:28:55 +0100 Subject: [PATCH 6/6] refactor(WTinyLFU): shorten the hill climber comments again Shorten the header note on the paper deviations, drop a comment that repeats the eviction loop, and trim two test headers. No code change. --- libCacheSim/cache/eviction/WTinyLFU.c | 11 ++++------- test/test_evictionAlgo.c | 10 ++++------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 2344ef2b..c58954a0 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -6,12 +6,10 @@ // // hill-climb=1 adapts the window size with the hill climber in Adaptive // Software Cache Management (Middleware'18): every 10 x cache size bytes of -// requests, move the window by 5% (between 1% and 80%), in the same -// direction unless the hit ratio dropped, otherwise in the other direction. -// Different from the paper, the window does not go down to 0% (every request -// would miss), a tie keeps the direction (as in Caffeine, otherwise a cache -// without hits switches between 1% and 5% forever), and the period is in -// bytes (the same for unit-size objects) +// requests, move the window by 5% (1% to 80%), and turn around if the hit +// ratio dropped. Different from the paper, the window does not go to 0% +// (every request would miss), a tie does not turn around (as in Caffeine), +// and the period is in bytes (the same for unit-size objects) // // NOTE: it is NOT thread-safe // @@ -261,7 +259,6 @@ static void WTinyLFU_resize_window(cache_t *cache, const request_t *req) { } else { main_cache->cache_size = main_size; } - // if the window has grown, the main cache evicts down to its new size while (main_cache->get_occupied_byte(main_cache) > main_cache->cache_size) { main_cache->evict(main_cache, req); } diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 96bae928..5223bbb5 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -680,9 +680,8 @@ static void test_WTinyLFU_hill_climb_no_hits(void) { cache->cache_free(cache); } -// SLRU_resize keeps the configured share of each segment. With seg-size=1:4 -// the first segment, which caps the object size, is 14 at size 71, 6 at size -// 30, and 14 again back at size 71. +// SLRU_resize keeps the 1:4 shares: the first segment, which caps the object +// size, is 14 at size 71, 6 at size 30, and 14 again back at size 71. static void test_SLRU_resize(void) { common_cache_params_t cc_params = {.cache_size = 71, .hashpower = 8}; cache_t *cache = SLRU_init(cc_params, "seg-size=1:4"); @@ -706,9 +705,8 @@ static void test_SLRU_resize(void) { cache->cache_free(cache); } -// After SLRU_resize a resident object can be larger than the next segment. -// A hit must keep it in place: promoting it would cool it back down and -// evict it while SLRU_find still uses it. +// After SLRU_resize an object can be larger than the next segment. A hit must +// keep it, not promote it, cool it back down and evict it. static void test_SLRU_resize_keeps_large_object(void) { common_cache_params_t cc_params = {.cache_size = 100, .hashpower = 8}; cache_t *cache = SLRU_init(cc_params, "seg-size=1:4");