-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.cpp
344 lines (295 loc) · 7.09 KB
/
cache.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/************************** LRU **************************/
static void init_lru(void)
{
}
static void add_to_list_lru(cache_entry* entry) //Add nodes to head
{
node_t *node = (node_t*) malloc(sizeof(node_t));
node->entry = entry;
node->prev = NULL;
if (!head) {
node->next = NULL;
head = tail = node;
return;
}
node->next = head;
head->prev = node;
head = node;
}
static node_t *pop_lru(void) //Pop nodes from tail
{
node_t* tmp;
if (!tail)
return NULL;
tmp = tail;
tail = tail->prev;
if (tail)
tail->next = NULL;
else
head = NULL;
return tmp;
}
static int run_lru(size_t new_item_size)
{
printf("%s\n", "running LRU.");
size_t poped_size = 0;
node_t *poped;
do {
printf("%s\n", "popping");
poped = pop_lru();
if (!poped)
return -1;
printf("%s\n", "popped valid node");
std::cout<<"poped key: "<<poped->entry->key<< " poped value: "<< poped->entry->data<< " poped bytes: "<<poped->entry->bytes;
poped_size += poped->entry->bytes;
printf("poped size: %lu\n", poped_size);
free(poped->entry->data);
memory_counter -= poped_size;
map->erase(poped->entry->key);
printf("%s %s\n", "erased key:", poped->entry->key.c_str());
memory_counter -= sizeof(cache_entry);
free(poped);
process_stats->evictions++;
printf("%s: %u\n", "counter after erase", memory_counter);
} while (poped_size < new_item_size);
return 0;
}
/******************** LRU block ends **********************/
/************************* Random *************************/
static void init_random(void)
{
}
static void add_to_list_random(cache_entry* entry)
{
int rand_index;
node_t *tmp;
node_t *node = (node_t*) malloc(sizeof(node_t));
node->entry = entry;
if (!head) {
node->prev = node->next = NULL;
head = tail = node;
++list_size;
return;
}
rand_index = rand() % list_size;
tmp = head;
while (rand_index--) {
tmp = tmp->next;
}
node->prev = tmp;
node->next = tmp->next;
if (tmp->next)
tmp->next->prev = node;
tmp->next = node;
if (tmp == tail)
tail = node;
}
static node_t *pop_random(void)
{
int rand_index;
node_t* tmp;
if (!tail)
return NULL;
rand_index = rand() % list_size;
tmp = head;
while (rand_index--) {
tmp = tmp->next;
}
if (tmp->prev)
tmp->prev->next = tmp->next;
if (tmp->next)
tmp->next->prev = tmp->prev;
if (tmp == tail)
tail = tmp->prev;
if (tmp == head)
head = tmp->next;
return tmp;
}
static int run_random(size_t new_item_size)
{
printf("%s\n", "running RANDOM.");
size_t poped_size = 0;
node_t *poped;
do {
printf("%s\n", "popping");
poped = pop_random();
if (!poped)
return -1;
printf("%s\n", "popped valid node");
poped_size += poped->entry->bytes;
printf("poped size: %lu\n", poped_size);
free(poped->entry->data);
memory_counter -= poped_size;
map->erase(poped->entry->key);
printf("%s %s\n", "erased key:", poped->entry->key.c_str());
memory_counter -= sizeof(cache_entry);
free(poped);
process_stats->evictions++;
printf("%s: %u\n", "counter after erase", memory_counter);
} while (poped_size < new_item_size);
return 0;
}
/******************* Random block ends ********************/
static float get_new_delta()
{
//return the new minimum from cost map
float min = FLT_MAX;
min = (float)head->cost/head->entry->bytes;
return min;
}
static void init_landlord(void)
{
}
static int set_cost(cache_entry *entry)
{
int res = 0;
if((*cache_miss_map).count(entry->key) > 0){
res = std::time(NULL) - (*cache_miss_map)[entry->key];
cache_miss_map->erase(entry->key);
}
//printf("entry cost = %d\n", res);
return res;
}
static void add_to_list_landlord(cache_entry* entry)
{
node_t *node = (node_t*) malloc(sizeof(node_t));
node->entry = entry;
node->next = NULL;
std::cout<<"Node added\n";
if (!head) {
node->prev = NULL;
head = tail = node;
node->cost = entry->bytes;
node->cost = set_cost(entry);
return;
}
node_t *temp = head;
node_t *prev = NULL;
while(temp != NULL && entry->bytes <= temp->cost){
prev = temp;
temp = temp->next;
}
node->next = temp;
node->prev = prev;
if(temp == NULL)
tail = node;
else
temp->prev = node;
if(prev == NULL)
head = node;
else
prev->next = node;
node->cost = set_cost(entry);
/*node->prev = tail;
tail->next = node;
tail = node;
node->cost = entry->bytes;*/
}
//very naive solution. Needs optimization. Not tested.
static int run_landlord(size_t new_item_size)
{
size_t space_cleared = 0;
//std::lock_guard<std::mutex> guard(list_mutex);
while( space_cleared < new_item_size )
{
node_t *temp = head;
std::cout<<"Running landlord\n";
delta = get_new_delta();
while(temp != NULL)
{
//std::cout<<"3\n";
temp->cost -= delta*temp->entry->bytes;
//std::cout<<"31\n";
node_t *next_entry = temp->next;
//std::cout<<"32\n";
if(temp->cost == 0.0)
{
//std::cout<<"33\n";
space_cleared += temp->entry->bytes+sizeof(cache_entry);
//std::cout<<"34\n";
if(temp->prev )
temp->prev->next = temp->next;
else
head = temp->next;
//std::cout<<"35\n";
if(temp->next)
temp->next->prev = temp->prev;
else
tail = temp->prev;
//std::cout<<"36\n";
memory_counter -= temp->entry->bytes+sizeof(cache_entry);
//std::cout<<"37\n";
map->erase(temp->entry->key);
//std::cout<<"38\n";
free(temp);
process_stats->curr_items--;
process_stats->evictions++;
//std::cout<<"39\n";
}
temp = next_entry;
}
}
return 0;
}
/*
* Current replacement policy
*/
policy_t get_replacement_policy(void)
{
return policy;
}
void init_replacement(void)
{
policy_t curr_policy = get_replacement_policy();
if (curr_policy == LRU)
init_lru();
if (curr_policy == RANDOM)
init_random();
if (curr_policy == LANDLORD)
init_landlord();
}
void add_to_list(cache_entry* entry)
{
policy_t curr_policy = get_replacement_policy();
if (curr_policy == LRU)
add_to_list_lru(entry);
if (curr_policy == RANDOM)
add_to_list_random(entry);
if (curr_policy == LANDLORD)
add_to_list_landlord(entry);
}
void remove_from_list(cache_entry* entry)
{
node_t *tmp = head;
while (tmp) {
if (tmp->entry->key == entry->key) {
if (tmp->prev)
tmp->prev->next = tmp->next;
if (tmp->next)
tmp->next->prev = tmp->prev;
if (tmp == head)
head = tmp->next;
if (tmp == tail)
tail = tmp->prev;
free(tmp);
return;
}
tmp = tmp->next;
}
}
/* Runs a replacement algo. Success returns 0 */
int run_replacement(size_t new_item_size)
{
printf("%s\n", "Running cache replacement algo.");
policy_t curr_policy = get_replacement_policy();
if (curr_policy == LRU)
return run_lru(new_item_size);
if (curr_policy == RANDOM)
return run_random(new_item_size);
if (curr_policy == LANDLORD)
return run_landlord(new_item_size);
return -1;
}
void track_misses(std::string key){
(*cache_miss_map)[key] = std::time(NULL);
}