diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 0c91b6d4..0418aa58 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -8,6 +8,14 @@ // // part of the details borrowed from https://github.com/mandreyel/w-tinylfu // +// the frequency sketch follows Caffeine: counters stop at 15 (4 bits), and +// once the access count reaches 10x the cache size, the counters and the +// access count are halved. Differences from the TinyLFU paper: +// 1. no doorkeeper +// 2. the sketch is sized and aged in bytes, not objects +// 3. an add increments all counters of an object, not only the smallest +// 4. counters stop at 15 instead of W/C (= 10) +// // WTinyLFU.c // libCacheSim // @@ -169,8 +177,7 @@ cache_t *WTinyLFU_init(const common_cache_params_t ccache_params, params->req_local = new_request(); params->n_admit_bytes = 0; - params->max_request_num = - 32 * params->main_cache->cache_size; // sample size is 32 + params->max_request_num = 10 * cache->cache_size; // sample size is 10x params->CBF = (struct minimalIncrementCBF *)malloc(sizeof(struct minimalIncrementCBF)); @@ -183,6 +190,7 @@ cache_t *WTinyLFU_init(const common_cache_params_t ccache_params, if (ret != 0) { ERROR("CBF init failed\n"); } + params->CBF->max_count = 15; // 4-bit counters #ifdef DEBUG_MODE minimalIncrementCBF_print(params->CBF); @@ -234,7 +242,7 @@ static void WTinyLFU_record_access(WTinyLFU_params_t *params, obj_id_t obj_id) { params->request_counter++; if (params->request_counter >= params->max_request_num) { - params->request_counter = 0; + params->request_counter /= 2; minimalIncrementCBF_decay(params->CBF); } } @@ -358,8 +366,6 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { } } // TODO @ Ziyue: add doorkeeper - minimalIncrementCBF_add(params->CBF, (void *)(¶ms->req_local->obj_id), - sizeof(obj_id_t)); } else { DEBUG_ASSERT(window->get_occupied_byte(window) == 0); main_cache->evict(main_cache, req); @@ -394,8 +400,6 @@ static void WTinyLFU_parse_params(cache_t *cache, const char *cache_specific_params) { WTinyLFU_params_t *params = (WTinyLFU_params_t *)cache->eviction_params; - // params->max_request_num = 32 * cache->cache_size; // 32 * cache_size - char *params_str = strdup(cache_specific_params); char *old_params_str = params_str; while (params_str != NULL && params_str[0] != '\0') { diff --git a/libCacheSim/dataStructure/minimalIncrementCBF.c b/libCacheSim/dataStructure/minimalIncrementCBF.c index b8667eb8..97c2389f 100644 --- a/libCacheSim/dataStructure/minimalIncrementCBF.c +++ b/libCacheSim/dataStructure/minimalIncrementCBF.c @@ -68,6 +68,7 @@ int minimalIncrementCBF_init(struct minimalIncrementCBF *CBF, int entries, // #endif exit(1); } + CBF->max_count = UINT_MAX; CBF->ready = 1; return 0; } @@ -116,7 +117,7 @@ static int minimalIncrementCBF_check_add(struct minimalIncrementCBF *CBF, } } - if (add == 1 && min_count < UINT_MAX) { + if (add == 1 && min_count < CBF->max_count) { for (i = 0; i < CBF->hashes; i++) { x = (a + i * b) % CBF->counter_num; if (CBF->bf[x] == min_count) { @@ -125,7 +126,9 @@ static int minimalIncrementCBF_check_add(struct minimalIncrementCBF *CBF, CBF->bf[x], min_count); #endif } - CBF->bf[x]++; + if (CBF->bf[x] < CBF->max_count) { + CBF->bf[x]++; + } } min_count++; } diff --git a/libCacheSim/dataStructure/minimalIncrementCBF.h b/libCacheSim/dataStructure/minimalIncrementCBF.h index af73f716..2a2801b5 100644 --- a/libCacheSim/dataStructure/minimalIncrementCBF.h +++ b/libCacheSim/dataStructure/minimalIncrementCBF.h @@ -27,6 +27,7 @@ struct minimalIncrementCBF { // on these. double bpe; unsigned int* bf; + unsigned int max_count; int ready; }; diff --git a/test/test_dataStructure.c b/test/test_dataStructure.c index 7b298e36..41b3a3ac 100644 --- a/test/test_dataStructure.c +++ b/test/test_dataStructure.c @@ -5,6 +5,7 @@ #include "common.h" #include "dataStructure/hashtable/chainedHashTableV2.h" #include "dataStructure/hashtable/hashtable.h" +#include "dataStructure/minimalIncrementCBF.h" void test_chained_hashtable_v2(gconstpointer user_data) { set_rand_seed(rand()); @@ -33,11 +34,27 @@ void test_chained_hashtable_v2(gconstpointer user_data) { free_chained_hashtable_v2(hashtable); } +void test_minimalIncrementCBF_max_count(gconstpointer user_data) { + struct minimalIncrementCBF cbf = {.ready = 0}; + minimalIncrementCBF_init(&cbf, 1000, 0.001); + cbf.max_count = 15; + + obj_id_t id = 42; + for (int i = 0; i < 20; i++) { + minimalIncrementCBF_add(&cbf, &id, sizeof(id)); + } + g_assert_cmpint(minimalIncrementCBF_estimate(&cbf, &id, sizeof(id)), ==, 15); + + minimalIncrementCBF_free(&cbf); +} + int main(int argc, char *argv[]) { g_test_init(&argc, &argv, NULL); g_test_add_data_func("/libCacheSim/test_chained_hashtable_v2", NULL, test_chained_hashtable_v2); + g_test_add_data_func("/libCacheSim/test_minimalIncrementCBF_max_count", NULL, + test_minimalIncrementCBF_max_count); return g_test_run(); } diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index db3500fe..adab1db7 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -582,9 +582,10 @@ static void test_3LCache(gconstpointer user_data) { #endif /* ENABLE_3L_CACHE */ // W-TinyLFU counts every access, so a hit in the window must raise an object's -// frequency just as a hit in the main cache does. Object 102 is read six times -// while it sits in the window; when it reaches the window's tail it must beat -// the main cache's victim, counted twice, and be admitted. +// frequency just as a hit in the main cache does, while leaving the window is +// not an access. Object 102 is read twice while it sits in the window; when it +// reaches the window's tail it must beat the main cache's victim, read once, +// and be admitted. static void test_WTinyLFU_counts_window_hits(void) { // 1000-byte objects: 100 fit, half in the window and half in the main cache. // The sketch is sized from the main cache's bytes, so with this few objects @@ -603,9 +604,7 @@ static void test_WTinyLFU_counts_window_hits(void) { g_assert_false(cache->get(cache, req)); } req->obj_id = 102; - for (int i = 0; i < 5; i++) { - g_assert_true(cache->get(cache, req)); - } + g_assert_true(cache->get(cache, req)); // 49 new objects bring 102 to the window's tail, and the 50th evicts it. for (obj_id_t id = 103; id <= 152; id++) {