|
17 | 17 | extern "C" { |
18 | 18 | #endif |
19 | 19 |
|
20 | | -// #define USE_BELADY |
21 | | -#undef USE_BELADY |
| 20 | +#define USE_BELADY |
| 21 | +// #undef USE_BELADY |
22 | 22 |
|
23 | 23 | static const char *DEFAULT_PARAMS = "init-freq=0,n-bit-counter=1"; |
24 | 24 |
|
@@ -123,7 +123,14 @@ static void Clock_free(cache_t *cache) { |
123 | 123 | * @return true if cache hit, false if cache miss |
124 | 124 | */ |
125 | 125 | 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; |
127 | 134 | } |
128 | 135 |
|
129 | 136 | // *********************************************************************** |
@@ -221,11 +228,36 @@ static cache_obj_t *Clock_to_evict(cache_t *cache, const request_t *req) { |
221 | 228 | * @param req not used |
222 | 229 | * @param evicted_obj if not NULL, return the evicted object to caller |
223 | 230 | */ |
| 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 | + |
224 | 252 | static void Clock_evict(cache_t *cache, const request_t *req) { |
225 | 253 | Clock_params_t *params = (Clock_params_t *)cache->eviction_params; |
226 | 254 |
|
227 | 255 | cache_obj_t *obj_to_evict = params->q_tail; |
| 256 | +#ifdef USE_BELADY |
| 257 | + while (Clock_should_retain(cache, obj_to_evict)) { |
| 258 | +#else |
228 | 259 | while (obj_to_evict->clock.freq >= 1) { |
| 260 | +#endif |
229 | 261 | obj_to_evict->clock.freq -= 1; |
230 | 262 | params->n_obj_rewritten += 1; |
231 | 263 | params->n_byte_rewritten += obj_to_evict->obj_size; |
|
0 commit comments