Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions libCacheSim/cache/eviction/WTinyLFU.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -358,8 +366,6 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) {
}
}
// TODO @ Ziyue: add doorkeeper
minimalIncrementCBF_add(params->CBF, (void *)(&params->req_local->obj_id),
sizeof(obj_id_t));
} else {
DEBUG_ASSERT(window->get_occupied_byte(window) == 0);
main_cache->evict(main_cache, req);
Expand Down Expand Up @@ -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') {
Expand Down
7 changes: 5 additions & 2 deletions libCacheSim/dataStructure/minimalIncrementCBF.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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++;
}
Expand Down
1 change: 1 addition & 0 deletions libCacheSim/dataStructure/minimalIncrementCBF.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct minimalIncrementCBF {
// on these.
double bpe;
unsigned int* bf;
unsigned int max_count;
int ready;
};

Expand Down
17 changes: 17 additions & 0 deletions test/test_dataStructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
}
11 changes: 5 additions & 6 deletions test/test_evictionAlgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++) {
Expand Down
Loading