diff --git a/libCacheSim/cache/eviction/S3FIFO.c b/libCacheSim/cache/eviction/S3FIFO.c index 8c226bfe7..cd05bfbfa 100644 --- a/libCacheSim/cache/eviction/S3FIFO.c +++ b/libCacheSim/cache/eviction/S3FIFO.c @@ -354,7 +354,14 @@ static void S3FIFO_evict_small(cache_t *cache, const request_t *req) { copy_cache_obj_to_request(params->req_local, obj_to_evict); if (obj_to_evict->S3FIFO.freq >= params->move_to_main_threshold) { +#ifdef SUPPORT_TTL + cache_obj_t *new_obj = main_fifo->insert(main_fifo, params->req_local); + // req_local carries no TTL, so insert() gave the object a default + // expiry; keep its own expiry across the move + new_obj->exp_time = obj_to_evict->exp_time; +#else main_fifo->insert(main_fifo, params->req_local); +#endif } else { // insert to ghost if (ghost_fifo != NULL) { @@ -378,6 +385,9 @@ static void S3FIFO_evict_main(cache_t *cache, const request_t *req) { cache_obj_t *obj_to_evict = main_fifo->to_evict(main_fifo, req); DEBUG_ASSERT(obj_to_evict != NULL); int64_t freq = obj_to_evict->S3FIFO.freq; +#ifdef SUPPORT_TTL + uint32_t exp_time = obj_to_evict->exp_time; +#endif copy_cache_obj_to_request(params->req_local, obj_to_evict); if (freq >= 1) { // we need to evict first because the object to insert has the same obj_id @@ -387,6 +397,9 @@ static void S3FIFO_evict_main(cache_t *cache, const request_t *req) { cache_obj_t *new_obj = main_fifo->insert(main_fifo, params->req_local); // clock with 2-bit counter new_obj->S3FIFO.freq = MIN(freq, 3) - 1; +#ifdef SUPPORT_TTL + new_obj->exp_time = exp_time; // see S3FIFO_evict_small +#endif } else { bool removed = main_fifo->remove(main_fifo, obj_to_evict->obj_id); diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 45a30b9e8..01a55de11 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -445,6 +445,54 @@ static void test_S3FIFO_main_freq_beyond_int32(void) { cache->cache_free(cache); } +#ifdef SUPPORT_TTL +// S3FIFO moves an object between its queues by re-inserting it through a +// local request that carries no TTL. Both moves, promotion from the small FIFO +// and reinsertion at the main tail, must keep the object's own expiry. +static void test_S3FIFO_keeps_expiry_across_moves(void) { + common_cache_params_t cc_params = {.cache_size = 100, .hashpower = 8}; + cache_t *cache = S3FIFO_init(cc_params, NULL); + request_t *req = new_request(); + req->obj_size = 1; + req->clock_time = 0; + + // As in test_S3FIFO_main_freq_beyond_int32: objects 1 to 10 fill the small + // FIFO and 11 to 100 go straight to main. Objects 1 and 11 expire at 1000. + for (obj_id_t id = 1; id <= 100; id++) { + req->obj_id = id; + req->ttl = (id == 1 || id == 11) ? 1000 : 0; + cache->get(cache, req); + } + req->ttl = 0; + // Two reads each will promote 1 to 10; one read lets 11, the oldest object + // in main, survive the main tail once. + for (int round = 0; round < 2; round++) { + for (obj_id_t id = 1; id <= 10; id++) { + req->obj_id = id; + cache->get(cache, req); + } + } + req->obj_id = 11; + cache->get(cache, req); + + // The next miss promotes 1 to 10 and makes main reinsert 11. + req->obj_id = 101; + cache->get(cache, req); + + req->obj_id = 1; + cache_obj_t *obj = cache->find(cache, req, false); + g_assert_nonnull(obj); + g_assert_cmpuint(obj->exp_time, ==, 1000); + req->obj_id = 11; + obj = cache->find(cache, req, false); + g_assert_nonnull(obj); + g_assert_cmpuint(obj->exp_time, ==, 1000); + + free_request(req); + cache->cache_free(cache); +} +#endif + static void test_S3FIFOv0(gconstpointer user_data) { test_cache_algorithm(user_data, &test_data_truth[22]); } @@ -530,6 +578,10 @@ int main(int argc, char *argv[]) { g_test_add_data_func("/libCacheSim/cacheAlgo_S3FIFO", reader, test_S3FIFO); g_test_add_func("/libCacheSim/cacheAlgo_S3FIFO_main_freq_beyond_int32", test_S3FIFO_main_freq_beyond_int32); +#ifdef SUPPORT_TTL + g_test_add_func("/libCacheSim/cacheAlgo_S3FIFO_keeps_expiry_across_moves", + test_S3FIFO_keeps_expiry_across_moves); +#endif g_test_add_data_func("/libCacheSim/cacheAlgo_S3FIFOv0", reader, test_S3FIFOv0); #if defined(ENABLE_S4FIFO) && ENABLE_S4FIFO == 1