diff --git a/libCacheSim/cache/eviction/SLRU.c b/libCacheSim/cache/eviction/SLRU.c index 6dbd2c42..82c48111 100644 --- a/libCacheSim/cache/eviction/SLRU.c +++ b/libCacheSim/cache/eviction/SLRU.c @@ -7,6 +7,8 @@ // // +#include + #include "dataStructure/hashtable/hashtable.h" #include "libCacheSim/evictionAlgo.h" @@ -49,6 +51,7 @@ static void SLRU_promote_to_next_seg(cache_t *cache, const request_t *req, cache_obj_t *obj); static void SLRU_cool(cache_t *cache, const request_t *req, const int id); bool SLRU_can_insert(cache_t *cache, const request_t *req); +static int SLRU_seg_pct(const SLRU_params_t *params, const int seg); static void SLRU_parse_params(cache_t *cache, const char *cache_specific_params); @@ -162,7 +165,7 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params, // update slru cache name bool same_size = true; for (int i = 1; i < params->n_seg; i++) { - if (params->lru_max_n_bytes[i] != params->lru_max_n_bytes[i - 1]) { + if (params->seg_ratio[i] != params->seg_ratio[i - 1]) { same_size = false; break; } @@ -171,22 +174,22 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params, if (same_size) { n = snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "S%dLRU(%d", params->n_seg, (int)(100 / params->n_seg)); - for (int i = 1; i < params->n_seg; i++) { + for (int i = 1; i < params->n_seg && n < CACHE_NAME_ARRAY_LEN; i++) { n += snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ":%d", (int)(100 / params->n_seg)); } } else { n = snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "S%dLRU(%d", - params->n_seg, - (int)(params->lru_max_n_bytes[0] * 100 / cache->cache_size)); + params->n_seg, SLRU_seg_pct(params, 0)); - for (int i = 1; i < params->n_seg; i++) { - n += - snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ":%d", - (int)(params->lru_max_n_bytes[i] * 100 / cache->cache_size)); + for (int i = 1; i < params->n_seg && n < CACHE_NAME_ARRAY_LEN; i++) { + n += snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ":%d", + SLRU_seg_pct(params, i)); } } - snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ")"); + if (n < CACHE_NAME_ARRAY_LEN) { + snprintf(cache->cache_name + n, CACHE_NAME_ARRAY_LEN - n, ")"); + } #ifdef USE_BELADY char *tmp = strdup(cache->cache_name); @@ -310,8 +313,9 @@ static cache_obj_t *SLRU_insert(cache_t *cache, const request_t *req) { if (nth_seg == -1) { // No space for insertion - while (cache->occupied_byte + req->obj_size + cache->obj_md_size > - cache->cache_size) { + int64_t sz = req->obj_size + cache->obj_md_size; + while (params->lru_n_bytes[0] + sz > params->lru_max_n_bytes[0] || + cache->occupied_byte + sz > cache->cache_size) { cache->evict(cache, req); } nth_seg = 0; @@ -420,28 +424,24 @@ static bool SLRU_remove(cache_t *cache, obj_id_t obj_id) { // **** parameter set up functions **** // **** **** // *********************************************************************** -/* share of the cache given to one segment, in percent. lru_max_n_bytes is only - * allocated after the parameters are parsed, so it is still NULL when the user - * asks for the parameters with `-e print`; until seg-size says otherwise the - * segments are evenly sized */ -static int SLRU_seg_pct(const cache_t *cache, const SLRU_params_t *params, - const int seg) { - if (params->lru_max_n_bytes == NULL) { +/* share of one segment in percent; even until seg_ratio is set */ +static int SLRU_seg_pct(const SLRU_params_t *params, const int seg) { + if (params->seg_ratio[seg] == 0) { return 100 / params->n_seg; } - return (int)(params->lru_max_n_bytes[seg] * 100 / cache->cache_size); + return (int)lround(params->seg_ratio[seg] * 100); } -static const char *SLRU_current_params(cache_t *cache, SLRU_params_t *params) { +static const char *SLRU_current_params(SLRU_params_t *params) { static __thread char params_str[128]; int n = snprintf(params_str, sizeof(params_str), "n-seg=%d,seg-size=%d", - params->n_seg, SLRU_seg_pct(cache, params, 0)); + params->n_seg, SLRU_seg_pct(params, 0)); for (int i = 1; i < params->n_seg && n > 0 && n < (int)sizeof(params_str); i++) { n += snprintf(params_str + n, sizeof(params_str) - n, ":%d", - SLRU_seg_pct(cache, params, i)); + SLRU_seg_pct(params, i)); } return params_str; @@ -502,9 +502,12 @@ static void SLRU_parse_params(cache_t *cache, (long long)params->lru_max_n_bytes[i]); } } + // give the rounding remainder to the last segment + SLRU_resize(cache, cache->cache_size); } else if (strcasecmp(key, "print") == 0) { - printf("current parameters: %s\n", SLRU_current_params(cache, params)); + printf("current parameters: %s\n", SLRU_current_params(params)); free(old_params_str); + SLRU_free(cache); exit(0); } else { ERROR("%s does not have parameter %s\n", cache->cache_name, key); @@ -562,8 +565,8 @@ static void SLRU_cool(cache_t *cache, const request_t *req, const int id) { prepend_obj_to_head(¶ms->lru_heads[id - 1], ¶ms->lru_tails[id - 1], obj); obj->SLRU.lru_id = id - 1; - params->lru_n_bytes[id] -= obj->obj_size; - params->lru_n_bytes[id - 1] += obj->obj_size; + params->lru_n_bytes[id] -= obj->obj_size + cache->obj_md_size; + params->lru_n_bytes[id - 1] += obj->obj_size + cache->obj_md_size; params->lru_n_objs[id]--; params->lru_n_objs[id - 1]++; @@ -603,7 +606,7 @@ static void _SLRU_verify_lru_size(cache_t *cache) { cache_obj_t *obj = params->lru_heads[i]; while (obj != NULL) { n_objs += 1; - n_bytes += obj->obj_size; + n_bytes += obj->obj_size + cache->obj_md_size; obj = obj->queue.next; } assert(n_objs == params->lru_n_objs[i]); diff --git a/libCacheSim/cache/eviction/WTinyLFU.c b/libCacheSim/cache/eviction/WTinyLFU.c index 2ad583c3..e5e55a5a 100644 --- a/libCacheSim/cache/eviction/WTinyLFU.c +++ b/libCacheSim/cache/eviction/WTinyLFU.c @@ -50,6 +50,7 @@ typedef struct WTinyLFU_params { char main_cache_type[32]; request_t *req_local; + bool main_full; // once full, every admission is compared bool hill_climb; int window_step; @@ -257,6 +258,9 @@ static void WTinyLFU_resize_window(cache_t *cache, const request_t *req) { 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 (main_size > main_cache->cache_size) { + params->main_full = false; + } if (strcasecmp(params->main_cache_type, "SLRU") == 0) { SLRU_resize(main_cache, main_size); } else { @@ -361,6 +365,12 @@ static cache_obj_t *WTinyLFU_to_evict(cache_t *cache, const request_t *req) { return NULL; } +static bool WTinyLFU_main_fits(cache_t *main_cache, const request_t *req) { + return main_cache->get_occupied_byte(main_cache) + req->obj_size + + main_cache->obj_md_size <= + main_cache->cache_size; +} + static void WTinyLFU_evict(cache_t *cache, const request_t *req) { WTinyLFU_params_t *params = (WTinyLFU_params_t *)(cache->eviction_params); @@ -384,9 +394,8 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { * caller asking the composite what it reserves is not told less than it * really does; using it here would bill a FIFO or Clock main cache for * the window's 16 bytes and call it full early. */ - if (main_cache->get_occupied_byte(main_cache) + - params->req_local->obj_size + main_cache->obj_md_size <= - main_cache->cache_size) { + if (!params->main_full && + WTinyLFU_main_fits(main_cache, params->req_local)) { main_cache->insert(main_cache, params->req_local); #if defined(TRACK_DEMOTION) @@ -399,6 +408,7 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { } else { // compare the frequency of window_victim and main_cache_victim + params->main_full = true; cache_obj_t *main_cache_victim = main_cache->to_evict(main_cache, req); DEBUG_ASSERT(main_cache_victim != NULL); // if window_victim is more frequent, insert it into main_cache @@ -413,7 +423,9 @@ static void WTinyLFU_evict(cache_t *cache, const request_t *req) { window_victim->next_access_vtime); #endif - main_cache->evict(main_cache, req); + do { + main_cache->evict(main_cache, req); + } while (!WTinyLFU_main_fits(main_cache, params->req_local)); bool ret = window->remove(window, window_victim->obj_id); DEBUG_ASSERT(ret); diff --git a/test/test_cli.sh b/test/test_cli.sh index 2d084c94..22f622e8 100755 --- a/test/test_cli.sh +++ b/test/test_cli.sh @@ -375,8 +375,11 @@ expect_output "slru -e print" "n-seg=4,seg-size=25:25:25:25" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e print expect_output "slru -e n-seg=8,print" "n-seg=8" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e "n-seg=8,print" -expect_output "slru -e seg-size=1:2:3:4,print" "seg-size=9:19:29:39" \ +expect_output "slru -e seg-size=1:2:3:4,print" "seg-size=10:20:30:40" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 1gb -e "seg-size=1:2:3:4,print" +expect_output "slru seg-size=1:1 name on an odd size" "S2LRU\\(50:50\\)" \ + "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru 101 --ignore-obj-size=1 \ + -e "seg-size=1:1" expect_output "slru -e print with auto sizing" "n-seg=" \ "${BIN_DIR}/cachesim" "${TRACE}" vscsi slru auto -e print expect_output "qdlp -e print" "fifo-size-ratio=" \ diff --git a/test/test_evictionAlgo.c b/test/test_evictionAlgo.c index 36f87b56..dbe3bc76 100644 --- a/test/test_evictionAlgo.c +++ b/test/test_evictionAlgo.c @@ -202,9 +202,9 @@ static const cache_test_data_t test_data_truth[] = { .hashpower = 20, .req_cnt_true = 113872, .req_byte_true = 4368040448, - .miss_cnt_true = {89624, 86725, 82781, 80203, 75388, 65645, 59035, 56063}, - .miss_byte_true = {4123085312, 3915534848, 3690704896, 3493027840, - 3174708736, 2661464064, 2507604992, 2439981056}}, + .miss_cnt_true = {89862, 86685, 83365, 80309, 75447, 66000, 59092, 56111}, + .miss_byte_true = {4125897728, 3909879808, 3697543168, 3489533952, + 3159642624, 2695841792, 2506203648, 2440419840}}, {.cache_name = "SR_LRU", .hashpower = 20, .req_cnt_true = 113872, @@ -723,6 +723,82 @@ static void test_SLRU_resize_keeps_large_object(void) { cache->cache_free(cache); } +// seg-size=1:4 on 6 objects must give segments of 1 and 5, not 1 and 4 +static void test_SLRU_seg_size_fills_cache(void) { + common_cache_params_t cc_params = {.cache_size = 6, .hashpower = 8}; + cache_t *cache = SLRU_init(cc_params, "seg-size=1:4"); + request_t *req = new_request(); + req->obj_size = 1; + + for (obj_id_t id = 1; id <= 6; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + g_assert_cmpint(cache->get_n_obj(cache), ==, 6); + + req->obj_id = 1; + g_assert_true(cache->get(cache, req)); + g_assert_cmpint(cache->get_n_obj(cache), ==, 6); + + free_request(req); + cache->cache_free(cache); +} + +// a cold window victim must not take a free slot in a full main cache +static void test_WTinyLFU_full_main_checks_admission(void) { + common_cache_params_t cc_params = {.cache_size = 13, .hashpower = 8}; + cache_t *cache = WTinyLFU_init(cc_params, "window-size=0.2"); + request_t *req = new_request(); + req->obj_size = 1; + + for (obj_id_t id = 1; id <= 14; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + + // promoting 1 used to leave a free slot (segments of 2 and 8) + req->obj_id = 1; + g_assert_true(cache->get(cache, req)); + + for (obj_id_t id = 15; id <= 16; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + req->obj_id = 13; + g_assert_null(cache->find(cache, req, false)); + + free_request(req); + cache->cache_free(cache); +} + +// the same in byte mode, with a small object +static void test_WTinyLFU_full_main_checks_small_object(void) { + common_cache_params_t cc_params = {.cache_size = 1300, .hashpower = 8}; + cache_t *cache = WTinyLFU_init(cc_params, "main-cache=LRU,window-size=0.2"); + request_t *req = new_request(); + + req->obj_size = 100; + for (obj_id_t id = 1; id <= 14; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + + // main holds 1000 of 1040 bytes, so 15 fits + req->obj_id = 15; + req->obj_size = 30; + g_assert_false(cache->get(cache, req)); + req->obj_size = 100; + for (obj_id_t id = 16; id <= 18; id++) { + req->obj_id = id; + g_assert_false(cache->get(cache, req)); + } + req->obj_id = 15; + g_assert_null(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 @@ -830,6 +906,13 @@ 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_seg_size_fills_cache", + test_SLRU_seg_size_fills_cache); + g_test_add_func("/libCacheSim/cacheAlgo_WTinyLFU_full_main_checks_admission", + test_WTinyLFU_full_main_checks_admission); + g_test_add_func( + "/libCacheSim/cacheAlgo_WTinyLFU_full_main_checks_small_object", + test_WTinyLFU_full_main_checks_small_object); g_test_add_func("/libCacheSim/cacheAlgo_SLRU_resize_keeps_large_object", test_SLRU_resize_keeps_large_object);