-
Notifications
You must be signed in to change notification settings - Fork 37
/
GDSF.cpp
231 lines (199 loc) · 7.17 KB
/
GDSF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* GDSF: greedy dual frequency size */
#include <cassert>
#include "abstractRank.hpp"
namespace eviction {
class GDSF : public abstractRank {
public:
GDSF() = default;
double pri_last_evict = 0.0;
};
} // namespace eviction
#ifdef __cplusplus
extern "C" {
#endif
// ***********************************************************************
// **** ****
// **** function declarations ****
// **** ****
// ***********************************************************************
cache_t *GDSF_init(const common_cache_params_t ccache_params,
const char *cache_specific_params);
static void GDSF_free(cache_t *cache);
static bool GDSF_get(cache_t *cache, const request_t *req);
static cache_obj_t *GDSF_find(cache_t *cache, const request_t *req,
const bool update_cache);
static cache_obj_t *GDSF_insert(cache_t *cache, const request_t *req);
static cache_obj_t *GDSF_to_evict(cache_t *cache, const request_t *req);
static void GDSF_evict(cache_t *cache, const request_t *req);
static bool GDSF_remove(cache_t *cache, const obj_id_t obj_id);
// ***********************************************************************
// **** ****
// **** end user facing functions ****
// **** ****
// **** init, free, get ****
// ***********************************************************************
/**
* @brief initialize the cache
*
* @param ccache_params some common cache parameters
* @param cache_specific_params cache specific parameters, see parse_params
* function or use -e "print" with the cachesim binary
*/
cache_t *GDSF_init(const common_cache_params_t ccache_params,
const char *cache_specific_params) {
cache_t *cache = cache_struct_init("GDSF", ccache_params, cache_specific_params);
cache->eviction_params = reinterpret_cast<void *>(new eviction::GDSF);
cache->cache_init = GDSF_init;
cache->cache_free = GDSF_free;
cache->get = GDSF_get;
cache->find = GDSF_find;
cache->insert = GDSF_insert;
cache->evict = GDSF_evict;
cache->to_evict = NULL;
cache->remove = GDSF_remove;
if (ccache_params.consider_obj_metadata) {
// freq + priority
cache->obj_md_size = 8;
} else {
cache->obj_md_size = 0;
}
return cache;
}
/**
* free resources used by this cache
*
* @param cache
*/
static void GDSF_free(cache_t *cache) {
delete reinterpret_cast<eviction::GDSF *>(cache->eviction_params);
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 GDSF_get(cache_t *cache, const request_t *req) {
return cache_get_base(cache, req);
}
// ***********************************************************************
// **** ****
// **** developer facing APIs (used by cache developer) ****
// **** ****
// ***********************************************************************
/**
* @brief find an object 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 the object or NULL if not found
*/
static cache_obj_t *GDSF_find(cache_t *cache, const request_t *req,
const bool update_cache) {
auto *gdsf = reinterpret_cast<eviction::GDSF *>(cache->eviction_params);
cache_obj_t *obj = cache_find_base(cache, req, update_cache);
/* this does not consider object size change */
if (obj != nullptr && update_cache) {
/* update frequency */
obj->lfu.freq += 1;
auto itr = gdsf->itr_map[obj];
gdsf->pq.erase(itr);
double pri =
gdsf->pri_last_evict + (double)(obj->lfu.freq) * 1.0e6 / obj->obj_size;
itr = gdsf->pq.emplace(obj, pri, cache->n_req).first;
gdsf->itr_map[obj] = itr;
}
return obj;
}
/**
* @brief insert an object into the cache,
* update the hash table and cache metadata
* this function assumes the cache has enough space
* eviction should be
* performed before calling this function
*
* @param cache
* @param req
* @return the inserted object
*/
static cache_obj_t *GDSF_insert(cache_t *cache, const request_t *req) {
auto *gdsf = reinterpret_cast<eviction::GDSF *>(cache->eviction_params);
cache_obj_t *obj = cache_insert_base(cache, req);
obj->lfu.freq = 1;
double pri = gdsf->pri_last_evict + 1.0e6 / obj->obj_size;
auto itr = gdsf->pq.emplace(obj, pri, cache->n_req).first;
gdsf->itr_map[obj] = itr;
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 *GDSF_to_evict(cache_t *cache, const request_t *req) {
auto *gdsf = reinterpret_cast<eviction::GDSF *>(cache->eviction_params);
eviction::pq_node_type p = gdsf->peek_lowest_score();
return p.obj;
}
/**
* @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
* @param evicted_obj if not NULL, return the evicted object to caller
*/
static void GDSF_evict(cache_t *cache, const request_t *req) {
auto *gdsf = reinterpret_cast<eviction::GDSF *>(cache->eviction_params);
eviction::pq_node_type p = gdsf->pop_lowest_score();
cache_obj_t *obj = p.obj;
gdsf->pri_last_evict = p.priority;
cache_remove_obj_base(cache, obj, true);
}
static void GDSF_remove_obj(cache_t *cache, cache_obj_t *obj) {
auto *gdsf = reinterpret_cast<eviction::GDSF *>(cache->eviction_params);
gdsf->remove_obj(cache, obj);
}
/**
* @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 GDSF_remove(cache_t *cache, const obj_id_t obj_id) {
auto *gdsf = reinterpret_cast<eviction::GDSF *>(cache->eviction_params);
return gdsf->remove(cache, obj_id);
}
#ifdef __cplusplus
}
#endif