From a76e97e422cd1985ca5613a6949d020802d8e803 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Sun, 18 Oct 2026 11:30:18 -0700 Subject: [PATCH 1/2] fix(cache): do not touch an expired object after cache_find_base removes it Signed-off-by: Sai Asish Y --- libCacheSim/cache/cache.c | 2 +- test/test_evictionAlgo.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/libCacheSim/cache/cache.c b/libCacheSim/cache/cache.c index e782625d..0013aab9 100644 --- a/libCacheSim/cache/cache.c +++ b/libCacheSim/cache/cache.c @@ -262,7 +262,7 @@ cache_obj_t *cache_find_base(cache_t *cache, const request_t *req, cache->remove(cache, cache_obj->obj_id); } - cache_obj = NULL; + return NULL; } #endif diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 36f87b56..bfcc5d54 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -406,6 +406,29 @@ static void test_LRUv0(gconstpointer user_data) { test_cache_algorithm(user_data, &test_data); } +#ifdef SUPPORT_TTL +// A hit on an expired object removes it from the cache. The lookup must then +// report a miss instead of touching the object it just freed. +static void test_LRU_expired_object(void) { + common_cache_params_t cc_params = {.cache_size = 100, .hashpower = 8}; + cache_t *cache = LRU_init(cc_params, NULL); + request_t *req = new_request(); + req->obj_id = 1; + req->obj_size = 1; + req->ttl = 10; + + req->clock_time = 0; + g_assert_false(cache->get(cache, req)); + + req->clock_time = 20; + g_assert_false(cache->get(cache, req)); + g_assert_cmpint(cache->get_n_obj(cache), ==, 1); + + free_request(req); + cache->cache_free(cache); +} +#endif + static void test_LRU_K(gconstpointer user_data) { test_cache_algorithm(user_data, &test_data_truth[17]); } @@ -798,6 +821,10 @@ int main(int argc, char *argv[]) { g_test_add_data_func("/libCacheSim/cacheAlgo_LRU", reader, test_LRU); g_test_add_data_func("/libCacheSim/cacheAlgo_LRUv0", reader, test_LRUv0); g_test_add_data_func("/libCacheSim/cacheAlgo_LRU_K", reader, test_LRU_K); +#ifdef SUPPORT_TTL + g_test_add_func("/libCacheSim/cacheAlgo_LRU_expired_object", + test_LRU_expired_object); +#endif g_test_add_data_func("/libCacheSim/cacheAlgo_Merlin", reader, test_Merlin); g_test_add_data_func("/libCacheSim/cacheAlgo_MQ", reader, test_MQ); g_test_add_data_func("/libCacheSim/cacheAlgo_MRU", reader, test_MRU); From 3df441affe9b490014371631b6684e48dd2505d8 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Mon, 19 Oct 2026 01:51:23 -0700 Subject: [PATCH 2/2] test: exercise TTL expiry across eviction policies Signed-off-by: Sai Asish Y --- .github/workflows/build.yml | 9 +++++ test/test_evictionAlgo.c | 71 +++++++++++++++++++++++++++++++------ 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fbb95b99..75b7b972 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,7 +28,15 @@ jobs: run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure --parallel ubuntu: + name: ${{ matrix.name }} runs-on: ubuntu-latest + strategy: + matrix: + include: + - ttl: "OFF" + name: ubuntu + - ttl: "ON" + name: ubuntu / TTL ON steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -39,6 +47,7 @@ jobs: run: | cmake -G Ninja -B ${{github.workspace}}/build \ -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \ + -DSUPPORT_TTL=${{ matrix.ttl }} \ -DCMAKE_C_FLAGS="-fsanitize=leak" \ -DCMAKE_CXX_FLAGS="-fsanitize=leak" - name: Build diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index bfcc5d54..fff42c14 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -407,22 +407,71 @@ static void test_LRUv0(gconstpointer user_data) { } #ifdef SUPPORT_TTL -// A hit on an expired object removes it from the cache. The lookup must then -// report a miss instead of touching the object it just freed. -static void test_LRU_expired_object(void) { +typedef struct { + const char *test_path; + cache_init_func_ptr init; + int warmup_objects; +} expired_object_test_t; + +static const expired_object_test_t expired_object_tests[] = { + {"/libCacheSim/cacheAlgo_FIFO_expired_object", FIFO_init, 0}, + {"/libCacheSim/cacheAlgo_LRU_expired_object", LRU_init, 0}, + {"/libCacheSim/cacheAlgo_Sieve_expired_object", Sieve_init, 0}, + {"/libCacheSim/cacheAlgo_S3FIFO_expired_object", S3FIFO_init, 0}, + {"/libCacheSim/cacheAlgo_S3FIFO_main_expired_object", S3FIFO_init, 10}, +}; + +// Expired non-updating lookups leave the object resident. Updating lookups +// remove it and report a miss without touching the object they just freed. +static void test_expired_object(gconstpointer user_data) { + const expired_object_test_t *test = user_data; common_cache_params_t cc_params = {.cache_size = 100, .hashpower = 8}; - cache_t *cache = LRU_init(cc_params, NULL); + cache_t *cache = test->init(cc_params, NULL); request_t *req = new_request(); - req->obj_id = 1; req->obj_size = 1; - req->ttl = 10; - req->clock_time = 0; + // Filling S3FIFO's ten-byte small queue puts the next object in main. + for (int i = 0; i < test->warmup_objects; i++) { + req->obj_id = i + 1; + g_assert_false(cache->get(cache, req)); + } + req->obj_id = test->warmup_objects + 1; + req->ttl = 10; + req->next_access_vtime = 42; g_assert_false(cache->get(cache, req)); + cache_obj_t *obj = cache->find(cache, req, false); + g_assert_nonnull(obj); + const int64_t n_obj = cache->get_n_obj(cache); + const int64_t occupied_byte = cache->get_occupied_byte(cache); + const int32_t freq = obj->freq; + + req->clock_time = 20; + req->next_access_vtime = 84; + g_assert_null(cache->find(cache, req, false)); + g_assert_cmpint(cache->get_n_obj(cache), ==, n_obj); + g_assert_cmpint(cache->get_occupied_byte(cache), ==, occupied_byte); + + // Observe at the expiry boundary without updating: the same object and + // metadata remain, and expiry is strictly earlier than the request time. + req->clock_time = 10; + g_assert_true(cache->find(cache, req, false) == obj); + g_assert_cmpint(obj->freq, ==, freq); + g_assert_cmpint(obj->next_access_vtime, ==, 42); req->clock_time = 20; + g_assert_null(cache->find(cache, req, true)); + g_assert_cmpint(cache->get_n_obj(cache), ==, n_obj - 1); + g_assert_cmpint(cache->get_occupied_byte(cache), ==, occupied_byte - 1); g_assert_false(cache->get(cache, req)); - g_assert_cmpint(cache->get_n_obj(cache), ==, 1); + g_assert_cmpint(cache->get_n_obj(cache), ==, n_obj); + g_assert_cmpint(cache->get_occupied_byte(cache), ==, occupied_byte); + g_assert_true(cache->get(cache, req)); + + // Exercise expiration through get(), which removes and reinserts the item. + req->clock_time = 40; + g_assert_false(cache->get(cache, req)); + g_assert_cmpint(cache->get_n_obj(cache), ==, n_obj); + g_assert_cmpint(cache->get_occupied_byte(cache), ==, occupied_byte); free_request(req); cache->cache_free(cache); @@ -822,8 +871,10 @@ int main(int argc, char *argv[]) { g_test_add_data_func("/libCacheSim/cacheAlgo_LRUv0", reader, test_LRUv0); g_test_add_data_func("/libCacheSim/cacheAlgo_LRU_K", reader, test_LRU_K); #ifdef SUPPORT_TTL - g_test_add_func("/libCacheSim/cacheAlgo_LRU_expired_object", - test_LRU_expired_object); + for (gsize i = 0; i < G_N_ELEMENTS(expired_object_tests); i++) { + g_test_add_data_func(expired_object_tests[i].test_path, + &expired_object_tests[i], test_expired_object); + } #endif g_test_add_data_func("/libCacheSim/cacheAlgo_Merlin", reader, test_Merlin); g_test_add_data_func("/libCacheSim/cacheAlgo_MQ", reader, test_MQ);