Skip to content

Commit 79fad65

Browse files
committed
add oracle to CLOCK and SIEVE
1 parent 9451cb8 commit 79fad65

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

libCacheSim/cache/eviction/Clock.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
extern "C" {
1818
#endif
1919

20-
// #define USE_BELADY
21-
#undef USE_BELADY
20+
#define USE_BELADY
21+
// #undef USE_BELADY
2222

2323
static const char *DEFAULT_PARAMS = "init-freq=0,n-bit-counter=1";
2424

@@ -123,7 +123,14 @@ static void Clock_free(cache_t *cache) {
123123
* @return true if cache hit, false if cache miss
124124
*/
125125
static bool Clock_get(cache_t *cache, const request_t *req) {
126-
return cache_get_base(cache, req);
126+
bool ck_hit = cache_get_base(cache, req);
127+
#ifdef USE_BELADY
128+
if (!ck_hit) {
129+
Clock_params_t *params = (Clock_params_t *)cache->eviction_params;
130+
params->n_miss++;
131+
}
132+
#endif
133+
return ck_hit;
127134
}
128135

129136
// ***********************************************************************
@@ -221,11 +228,36 @@ static cache_obj_t *Clock_to_evict(cache_t *cache, const request_t *req) {
221228
* @param req not used
222229
* @param evicted_obj if not NULL, return the evicted object to caller
223230
*/
231+
#ifdef USE_BELADY
232+
static inline bool Clock_should_retain(cache_t *cache, cache_obj_t *obj) {
233+
Clock_params_t *params = (Clock_params_t *)cache->eviction_params;
234+
235+
// if (obj->clock.freq == 0) return false;
236+
237+
if (obj->next_access_vtime == -1 || obj->next_access_vtime == INT64_MAX) {
238+
return false;
239+
}
240+
241+
/* oracle check: if next access is too far away, don't retain */
242+
double miss_ratio = (double)params->n_miss / (double)cache->n_req;
243+
int64_t next_access_dist = obj->next_access_vtime - cache->n_req;
244+
int64_t thresh = (int64_t)((double)cache->cache_size / miss_ratio);
245+
if (next_access_dist > thresh) {
246+
return false;
247+
}
248+
return true;
249+
}
250+
#endif
251+
224252
static void Clock_evict(cache_t *cache, const request_t *req) {
225253
Clock_params_t *params = (Clock_params_t *)cache->eviction_params;
226254

227255
cache_obj_t *obj_to_evict = params->q_tail;
256+
#ifdef USE_BELADY
257+
while (Clock_should_retain(cache, obj_to_evict)) {
258+
#else
228259
while (obj_to_evict->clock.freq >= 1) {
260+
#endif
229261
obj_to_evict->clock.freq -= 1;
230262
params->n_obj_rewritten += 1;
231263
params->n_byte_rewritten += obj_to_evict->obj_size;

libCacheSim/cache/eviction/Sieve.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
extern "C" {
88
#endif
99

10+
#define USE_BELADY
11+
// #undef USE_BELADY
12+
1013
typedef struct {
1114
cache_obj_t *q_head;
1215
cache_obj_t *q_tail;
1316

1417
cache_obj_t *pointer;
18+
19+
#ifdef USE_BELADY
20+
int64_t n_miss;
21+
#endif
1522
} Sieve_params_t;
1623

1724
// ***********************************************************************
@@ -68,6 +75,10 @@ cache_t *Sieve_init(const common_cache_params_t ccache_params,
6875
params->q_head = NULL;
6976
params->q_tail = NULL;
7077

78+
#ifdef USE_BELADY
79+
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "Sieve_Belady");
80+
#endif
81+
7182
return cache;
7283
}
7384

@@ -103,6 +114,12 @@ static void Sieve_free(cache_t *cache) {
103114

104115
static bool Sieve_get(cache_t *cache, const request_t *req) {
105116
bool ck_hit = cache_get_base(cache, req);
117+
#ifdef USE_BELADY
118+
if (!ck_hit) {
119+
Sieve_params_t *params = cache->eviction_params;
120+
params->n_miss++;
121+
}
122+
#endif
106123
return ck_hit;
107124
}
108125

@@ -215,13 +232,37 @@ static cache_obj_t *Sieve_to_evict(cache_t *cache, const request_t *req) {
215232
* @param req not used
216233
* @param evicted_obj if not NULL, return the evicted object to caller
217234
*/
235+
#ifdef USE_BELADY
236+
static inline bool Sieve_should_retain(cache_t *cache, cache_obj_t *obj) {
237+
Sieve_params_t *params = cache->eviction_params;
238+
239+
// if (obj->sieve.freq == 0) return false;
240+
241+
if (obj->next_access_vtime == -1 || obj->next_access_vtime == INT64_MAX) {
242+
return false;
243+
}
244+
245+
double miss_ratio = (double)params->n_miss / (double)cache->n_req;
246+
int64_t next_access_dist = obj->next_access_vtime - cache->n_req;
247+
int64_t thresh = (int64_t)((double)cache->cache_size / miss_ratio);
248+
if (next_access_dist > thresh) {
249+
return false;
250+
}
251+
return true;
252+
}
253+
#endif
254+
218255
static void Sieve_evict(cache_t *cache, const request_t *req) {
219256
Sieve_params_t *params = cache->eviction_params;
220257

221258
/* if we have run one full around or first eviction */
222259
cache_obj_t *obj = params->pointer == NULL ? params->q_tail : params->pointer;
223260

261+
#ifdef USE_BELADY
262+
while (Sieve_should_retain(cache, obj)) {
263+
#else
224264
while (obj->sieve.freq > 0) {
265+
#endif
225266
obj->sieve.freq -= 1;
226267
obj = obj->queue.prev == NULL ? params->q_tail : obj->queue.prev;
227268
}

0 commit comments

Comments
 (0)