Skip to content

Commit

Permalink
add random two and remove myclock (it has renamed to sieve now)
Browse files Browse the repository at this point in the history
  • Loading branch information
1a1a11a committed Jan 7, 2024
1 parent a98824e commit 834da03
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 381 deletions.
13 changes: 8 additions & 5 deletions libCacheSim/bin/cachesim/cache_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ static inline cache_t *create_cache(const char *trace_path,
cache = ARCv0_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "lhd") == 0) {
cache = LHD_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "random") == 0) {
cache = Random_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "randomTwo") == 0) {
cache = RandomTwo_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "lfu") == 0) {
cache = LFU_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "gdsf") == 0) {
Expand Down Expand Up @@ -102,8 +106,8 @@ static inline cache_t *create_cache(const char *trace_path,
} else if (strcasecmp(eviction_algo, "fifomerge") == 0 ||
strcasecmp(eviction_algo, "fifo-merge") == 0) {
cache = FIFO_Merge_init(cc_params, eviction_params);
// } else if (strcasecmp(eviction_algo, "fifo-reinsertion") == 0) {
// cache = FIFO_Reinsertion_init(cc_params, eviction_params);
// } else if (strcasecmp(eviction_algo, "fifo-reinsertion") == 0) {
// cache = FIFO_Reinsertion_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "flashProb") == 0) {
// used to measure application level write amp
cache = flashProb_init(cc_params, eviction_params);
Expand All @@ -121,7 +125,8 @@ static inline cache_t *create_cache(const char *trace_path,
cache = Sieve_Belady_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "s3lru") == 0) {
cache = S3LRU_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "s3fifo") == 0 || strcasecmp(eviction_algo, "s3-fifo") == 0) {
} else if (strcasecmp(eviction_algo, "s3fifo") == 0 ||
strcasecmp(eviction_algo, "s3-fifo") == 0) {
cache = S3FIFO_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "s3fifod") == 0) {
cache = S3FIFOd_init(cc_params, eviction_params);
Expand All @@ -139,8 +144,6 @@ static inline cache_t *create_cache(const char *trace_path,
cache = LRB_init(cc_params, eviction_params);
#endif
#ifdef INCLUDE_PRIV
} else if (strcasecmp(eviction_algo, "myclock") == 0) {
cache = MyClock_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "mclock") == 0) {
cache = MClock_init(cc_params, eviction_params);
} else if (strcasecmp(eviction_algo, "lp-sfifo") == 0) {
Expand Down
8 changes: 0 additions & 8 deletions libCacheSim/cache/eviction/ARCv0.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ extern "C" {
// #define LAZY_PROMOTION
// #define QUICK_DEMOTION

// #define USE_MYCLOCK

typedef struct ARCv0_params {
// L1_data is T1 in the paper, L1_ghost is B1 in the paper
cache_t *T1;
Expand Down Expand Up @@ -141,12 +139,6 @@ cache_t *ARCv0_init(const common_cache_params_t ccache_params,
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "ARCv0-QD");
#endif

#ifdef USE_MYCLOCK
params->T2->cache_free(params->T2);
params->T2 = MyClock_init(ccache_params_local, NULL);
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "ARCv0-myclock");
#endif

return cache;
}

Expand Down
2 changes: 1 addition & 1 deletion libCacheSim/cache/eviction/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(sourceC
MRU.c
Random.c
RandomTwo.c
LFU.c
LFUDA.c
ARC.c
Expand Down Expand Up @@ -43,7 +44,6 @@ set(sourceC
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/priv")
set(sourceC ${sourceC}

priv/MyClock.c
priv/QDLPv0.c


Expand Down
8 changes: 0 additions & 8 deletions libCacheSim/cache/eviction/LeCaRv0.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
extern "C" {
#endif

// #define USE_MYCLOCK

typedef struct LeCaRv0_params {
cache_t *LRU; // LRU
cache_t *LRU_g; // eviction history of LRU
Expand Down Expand Up @@ -118,12 +116,6 @@ cache_t *LeCaRv0_init(const common_cache_params_t ccache_params,
params->LRU_g = LRU_init(ccache_params_g, NULL);
params->LFU_g = LRU_init(ccache_params_g, NULL);

#ifdef USE_MYCLOCK
params->LRU->cache_free(params->LRU);
params->LRU = MyClock_init(ccache_params, NULL);
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "LeCaRv0-myclock");
#endif

return cache;
}

Expand Down
204 changes: 204 additions & 0 deletions libCacheSim/cache/eviction/RandomTwo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//
// RandomTwo.c
// libCacheSim
//
// Picks two objects at random and evicts the one that is the least recently
// used RandomTwo eviction
//
// Created by Juncheng on 8/2/16.
// Copyright © 2016 Juncheng. All rights reserved.
//

#include "../../dataStructure/hashtable/hashtable.h"
#include "../../include/libCacheSim/evictionAlgo.h"
#include "../../include/libCacheSim/macro.h"

#ifdef __cplusplus
extern "C" {
#endif

// ***********************************************************************
// **** ****
// **** function declarations ****
// **** ****
// ***********************************************************************

static void RandomTwo_free(cache_t *cache);
static bool RandomTwo_get(cache_t *cache, const request_t *req);
static cache_obj_t *RandomTwo_find(cache_t *cache, const request_t *req,
const bool update_cache);
static cache_obj_t *RandomTwo_insert(cache_t *cache, const request_t *req);
static cache_obj_t *RandomTwo_to_evict(cache_t *cache, const request_t *req);
static void RandomTwo_evict(cache_t *cache, const request_t *req);
static bool RandomTwo_remove(cache_t *cache, const obj_id_t obj_id);

// ***********************************************************************
// **** ****
// **** end user facing functions ****
// **** ****
// **** init, free, get ****
// ***********************************************************************
/**
* @brief initialize a RandomTwo cache
*
* @param ccache_params some common cache parameters
* @param cache_specific_params RandomTwo specific parameters, should be NULL
*/
cache_t *RandomTwo_init(const common_cache_params_t ccache_params,
const char *cache_specific_params) {
common_cache_params_t ccache_params_copy = ccache_params;
ccache_params_copy.hashpower = MAX(12, ccache_params_copy.hashpower - 8);

cache_t *cache =
cache_struct_init("RandomTwo", ccache_params, cache_specific_params);
cache->cache_init = RandomTwo_init;
cache->cache_free = RandomTwo_free;
cache->get = RandomTwo_get;
cache->find = RandomTwo_find;
cache->insert = RandomTwo_insert;
cache->to_evict = RandomTwo_to_evict;
cache->evict = RandomTwo_evict;
cache->remove = RandomTwo_remove;

return cache;
}

/**
* free resources used by this cache
*
* @param cache
*/
static void RandomTwo_free(cache_t *cache) { cache_struct_free(cache); }

/**
* @brief this function is the user facing API
* it performs the following logic
*
* ```
* if obj in cache:
* update_metadata
* return true
* else:
* if cache does not have enough space:
* evict until it has space to insert
* insert the object
* return false
* ```
*
* @param cache
* @param req
* @return true if cache hit, false if cache miss
*/
static bool RandomTwo_get(cache_t *cache, const request_t *req) {
return cache_get_base(cache, req);
}

// ***********************************************************************
// **** ****
// **** developer facing APIs (used by cache developer) ****
// **** ****
// ***********************************************************************

/**
* @brief check whether an object is in the cache
*
* @param cache
* @param req
* @param update_cache whether to update the cache,
* if true, the object is promoted
* and if the object is expired, it is removed from the cache
* @return true on hit, false on miss
*/
static cache_obj_t *RandomTwo_find(cache_t *cache, const request_t *req,
const bool update_cache) {
cache_obj_t *obj = cache_find_base(cache, req, update_cache);
if (obj != NULL && update_cache) {
obj->RandomTwo.last_access_vtime = cache->n_req;
}

return obj;
}

/**
* @brief insert an object into the cache,
* update the hash table and cache metadata
* this function assumes the cache has enough space
* and eviction is not part of this function
*
* @param cache
* @param req
* @return the inserted object
*/
static cache_obj_t *RandomTwo_insert(cache_t *cache, const request_t *req) {
cache_obj_t *obj = cache_insert_base(cache, req);
obj->RandomTwo.last_access_vtime = cache->n_req;

return obj;
}

/**
* @brief find the object to be evicted
* this function does not actually evict the object or update metadata
* not all eviction algorithms support this function
* because the eviction logic cannot be decoupled from finding eviction
* candidate, so use assert(false) if you cannot support this function
*
* @param cache the cache
* @return the object to be evicted
*/
static cache_obj_t *RandomTwo_to_evict(cache_t *cache, const request_t *req) {
cache_obj_t *obj_to_evict1 = hashtable_rand_obj(cache->hashtable);
cache_obj_t *obj_to_evict2 = hashtable_rand_obj(cache->hashtable);
DEBUG_ASSERT(obj_to_evict->obj_size != 0);
if (obj_to_evict1->RandomTwo.last_access_vtime <
obj_to_evict2->RandomTwo.last_access_vtime)
return obj_to_evict1;
else
return obj_to_evict2;
}

/**
* @brief evict an object from the cache
* it needs to call cache_evict_base before returning
* which updates some metadata such as n_obj, occupied size, and hash table
*
* @param cache
* @param req not used
*/
static void RandomTwo_evict(cache_t *cache, const request_t *req) {
cache_obj_t *obj_to_evict1 = hashtable_rand_obj(cache->hashtable);
cache_obj_t *obj_to_evict2 = hashtable_rand_obj(cache->hashtable);
DEBUG_ASSERT(obj_to_evict->obj_size != 0);
if (obj_to_evict1->RandomTwo.last_access_vtime <
obj_to_evict2->RandomTwo.last_access_vtime)
cache_evict_base(cache, obj_to_evict1, true);
else
cache_evict_base(cache, obj_to_evict2, true);
}

/**
* @brief remove an object from the cache
* this is different from cache_evict because it is used to for user trigger
* remove, and eviction is used by the cache to make space for new objects
*
* it needs to call cache_remove_obj_base before returning
* which updates some metadata such as n_obj, occupied size, and hash table
*
* @param cache
* @param obj_id
* @return true if the object is removed, false if the object is not in the
* cache
*/
static bool RandomTwo_remove(cache_t *cache, const obj_id_t obj_id) {
cache_obj_t *obj = hashtable_find_obj_id(cache->hashtable, obj_id);
if (obj == NULL) {
return false;
}
cache_remove_obj_base(cache, obj, true);

return true;
}

#ifdef __cplusplus
}
#endif
7 changes: 0 additions & 7 deletions libCacheSim/cache/eviction/TwoQ.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
extern "C" {
#endif

// #define USE_MYCLOCK

typedef struct {
cache_t *Ain;
cache_t *Aout;
Expand Down Expand Up @@ -110,11 +108,6 @@ cache_t *TwoQ_init(const common_cache_params_t ccache_params,

ccache_params_local.cache_size = params->Am_cache_size;
params->Am = LRU_init(ccache_params_local, NULL);
#ifdef USE_MYCLOCK
params->Am->cache_free(params->Am);
params->Am = MyClock_init(ccache_params_local, NULL);
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "TwoQ-myclock");
#endif

return cache;
}
Expand Down
18 changes: 9 additions & 9 deletions libCacheSim/cache/eviction/belady/Sieve_Belady.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static cache_obj_t *Sieve_Belady_find(cache_t *cache, const request_t *req,
const bool update_cache) {
cache_obj_t *cache_obj = cache_find_base(cache, req, update_cache);
if (cache_obj != NULL && update_cache) {
cache_obj->myclock.freq = 1;
cache_obj->sieve.freq = 1;
}

return cache_obj;
Expand All @@ -199,14 +199,14 @@ static cache_obj_t *Sieve_Belady_insert(cache_t *cache, const request_t *req) {
if (should_insert(cache, req->next_access_vtime)) {
obj = cache_insert_base(cache, req);
prepend_obj_to_head(&params->q_head, &params->q_tail, obj);
obj->myclock.freq = 0;
obj->myclock.new_obj = true;
obj->sieve.freq = 0;
// obj->sieve.new_obj = true;
}
#else
obj = cache_insert_base(cache, req);
prepend_obj_to_head(&params->q_head, &params->q_tail, obj);
obj->myclock.freq = 0;
obj->myclock.new_obj = true;
obj->sieve.freq = 0;
// obj->sieve.new_obj = true;
#endif

return obj;
Expand All @@ -232,17 +232,17 @@ static void Sieve_Belady_evict(cache_t *cache, const request_t *req) {

/* find the first untouched */
while (obj != NULL && should_insert(cache, obj->misc.next_access_vtime)) {
// while (obj != NULL && obj->myclock.freq > 0) {
obj->myclock.freq -= 1;
// while (obj != NULL && obj->sieve.freq > 0) {
obj->sieve.freq -= 1;
obj = obj->queue.prev;
}

/* if we have finished one around, start from the tail */
if (obj == NULL) {
obj = params->q_tail;
while (obj != NULL && should_insert(cache, obj->misc.next_access_vtime)) {
// while (obj != NULL && obj->myclock.freq > 0) {
obj->myclock.freq -= 1;
// while (obj != NULL && obj->sieve.freq > 0) {
obj->sieve.freq -= 1;
obj = obj->queue.prev;
}
}
Expand Down
Loading

0 comments on commit 834da03

Please sign in to comment.