-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_cache.c
624 lines (524 loc) · 19.3 KB
/
lru_cache.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "debug.h"
#include "common.h"
#include "lru_cache.h"
/*
* Assuming the lookup_table_size is multiples of 512
* so that the lookup_table would occupy multiples of
* 4K pages.
*
* lookup_table_size is 8 times smaller than the
* cache_size so that the open chaining would not
* be longer than 8.
*/
uint32_t get_lookup_table_size (uint32_t cache_size)
{
uint32_t lookup_table_size = 512;
uint32_t multiple = (cache_size >> 12) + 1;
return (lookup_table_size * multiple);
}
int lru_cache_init (uint32_t cache_size, LRUCache *cache)
{
cache->cache_size = cache_size;
check (cache_size>=2, "the SSD cache size should at least be 2.");
cache->entry_count = 0;
if (cache_size < 64) {
cache->lookup_table_size = cache_size;
} else {
cache->lookup_table_size = get_lookup_table_size (cache_size);
}
/* allocate space for cache_entry */
cache->cache_entry = (LRUCacheEntry *) calloc (cache->cache_size,
sizeof(LRUCacheEntry));
check(cache->cache_entry!=NULL,
"failed to allocate space for lru_cache->cache_entry.");
/* allocate space for lookup_table */
cache->lookup_table = (LRUCacheEntry **) calloc (
cache->lookup_table_size, sizeof(LRUCacheEntry *));
check(cache->lookup_table!=NULL,
"failed to allocate lru_cache->lookup table.");
cache->lru_head = NULL;
cache->lru_tail = NULL;
return 0;
error:
return -1;
}
/* check if the request could hit in the cache.
*
* returns 0: cache miss
* 1: cache hit plus updating LRU info
*
*/
int lru_cache_lookup (LRUCache *cache, Request *req)
{
/* if ((req->block_num == 3314217) && (req->server_num == 0)
&& (req->volume_num == 0)) {
printf("debug\n");
}*/
int exists = 0;
int table_slot = req->block_num % cache->lookup_table_size;
LRUCacheEntry *cache_entry = cache->lookup_table[table_slot];
if (req->req_type == 0) {
cache->read_lookups += 1;
} else {
cache->write_lookups += 1;
}
while (cache_entry != NULL) {
if ((cache_entry->block_num == req->block_num) &&
(cache_entry->server_num == req->server_num) &&
(cache_entry->volume_num == req->volume_num)) {
exists = 1;
break;
} else {
cache_entry = cache_entry->lookup_next;
}
}
if (exists) {
if (req->req_type == 0) {
cache->read_hits += 1;
} else {
cache->write_hits += 1;
cache_entry->write_access_count += 1;
}
}
if (exists)
{
/* update LRU information */
if ((cache_entry->lru_prev == NULL) && (cache_entry->lru_next == NULL)) {
/* the only entry in the cache, do nothing. */
} else {
if (cache_entry == cache->lru_tail) {
cache->lru_tail = cache_entry->lru_prev;
cache_entry->lru_prev->lru_next = NULL;
cache->lru_head->lru_prev = cache_entry;
cache_entry->lru_next = cache->lru_head;
cache_entry->lru_prev = NULL;
cache->lru_head = cache_entry;
} else {
if (cache_entry == cache->lru_head) {
/* do nothing */
} else {
cache_entry->lru_prev->lru_next = cache_entry->lru_next;
cache_entry->lru_next->lru_prev = cache_entry->lru_prev;
cache_entry->lru_prev = NULL;
cache_entry->lru_next = cache->lru_head;
cache->lru_head->lru_prev = cache_entry;
cache->lru_head = cache_entry;
}
}
}
/* move the entry to the front of lookup chain */
if ((cache_entry->lookup_prev == NULL) && (cache_entry->lookup_next == NULL)) {
/* the only entry in the chain, do nothing */
} else {
if (cache->lookup_table[table_slot] == cache_entry) {
/* it is the first entry already. do nothing. */
} else {
cache_entry->lookup_prev->lookup_next = cache_entry->lookup_next;
if (cache_entry->lookup_next != NULL) {
cache_entry->lookup_next->lookup_prev =
cache_entry->lookup_prev;
}
cache_entry->lookup_prev = NULL;
cache_entry->lookup_next = cache->lookup_table[table_slot];
cache->lookup_table[table_slot]->lookup_prev = cache_entry;
cache->lookup_table[table_slot] = cache_entry;
} // else (the first entry in the chain)
} // if (the only entry in the chain )
} // if (exists)
return exists;
}
int lru_cache_read_lookup (LRUCache *cache, Request *req)
{
int exists = 0;
int table_slot = req->block_num % cache->lookup_table_size;
LRUCacheEntry *cache_entry = cache->lookup_table[table_slot];
int num_lookups = 1;
if (req->req_type == 0) {
cache->read_lookups += 1;
} else {
cache->write_lookups += 1;
}
while (cache_entry != NULL) {
num_lookups += 1;
check (num_lookups <= cache->cache_size, "lookups more than cache size.");
if ((cache_entry->block_num == req->block_num) &&
(cache_entry->server_num == req->server_num) &&
(cache_entry->volume_num == req->volume_num)) {
exists = 1;
break;
} else {
cache_entry = cache_entry->lookup_next;
}
}
if (exists) {
if (req->req_type == 0) {
cache->read_hits += 1;
} else {
cache->write_hits += 1;
}
}
return exists;
error:
return -1;
}
int lru_cache_peek (LRUCache *cache, Request *req)
{
int exists = 0;
int table_slot = req->block_num % cache->lookup_table_size;
LRUCacheEntry *cache_entry = cache->lookup_table[table_slot];
LRUCacheEntry *first_entry = cache_entry;
int num_lookups = 0;
while (cache_entry != NULL) {
num_lookups += 1;
check (num_lookups <= cache->cache_size, "lookups more than cache size.");
if ((cache_entry->block_num == req->block_num) &&
(cache_entry->server_num == req->server_num) &&
(cache_entry->volume_num == req->volume_num)) {
exists = 1;
break;
} else {
cache_entry = cache_entry->lookup_next;
if (cache_entry == first_entry) {
printf("loop detected\n.");
}
}
}
return exists;
error:
return -1;
}
int lru_cache_update (LRUCache *cache, Request *req)
{
/* if ((req->block_num == 3314217) && (req->server_num == 0)
&& (req->volume_num == 0)) {
printf("debug\n");
}*/
int exists = 0;
int table_slot = req->block_num % cache->lookup_table_size;
LRUCacheEntry *cache_entry = cache->lookup_table[table_slot];
while (cache_entry != NULL) {
if ((cache_entry->block_num == req->block_num)
&& (cache_entry->server_num == req->server_num)
&& (cache_entry->volume_num == req->volume_num)) {
exists = 1;
break;
} else {
cache_entry = cache_entry->lookup_next;
}
}
check (exists == 1, "failed to find entry to update.");
cache->num_updates += 1;
// updating LRU information
if (cache_entry != cache->lru_head) {
if (cache_entry->lru_prev != NULL) {
cache_entry->lru_prev->lru_next = cache_entry->lru_next;
}
if (cache_entry->lru_next != NULL) {
cache_entry->lru_next->lru_prev = cache_entry->lru_prev;
}
if (cache_entry == cache->lru_tail) {
cache->lru_tail = cache_entry->lru_prev;
}
cache_entry->lru_prev = NULL;
cache_entry->lru_next = cache->lru_head;
cache->lru_head->lru_prev = cache_entry;
cache->lru_head = cache_entry;
}
/* update lookup info, move it to the front of the chain */
if (cache->lookup_table[table_slot] != cache_entry) {
if (cache_entry->lookup_prev != NULL) {
cache_entry->lookup_prev->lookup_next = cache_entry->lookup_next;
}
if (cache_entry->lookup_next != NULL) {
cache_entry->lookup_next->lookup_prev = cache_entry->lookup_prev;
}
cache_entry->lookup_prev = NULL;
cache_entry->lookup_next = cache->lookup_table[table_slot];
if (cache->lookup_table[table_slot] != NULL) {
cache->lookup_table[table_slot]->lookup_prev = cache_entry;
}
cache->lookup_table[table_slot] = cache_entry;
}
return 0;
error:
return -1;
}
int lru_cache_remove (LRUCache *cache, Request *req)
{
/* if ((req->block_num == 3314217) && (req->server_num == 0)
&& (req->volume_num == 0)) {
printf("debug\n");
}*/
int exists = 0;
int table_slot = req->block_num % cache->lookup_table_size;
LRUCacheEntry *cache_entry = cache->lookup_table[table_slot];
while (cache_entry != NULL) {
if ((cache_entry->block_num == req->block_num)
&& (cache_entry->server_num == req->server_num)
&& (cache_entry->volume_num == req->volume_num)) {
exists = 1;
break;
} else {
cache_entry = cache_entry->lookup_next;
}
}
check (exists == 1, "failed to find entry to remove.");
cache->num_removes += 1;
// updating LRU information
if ((cache_entry == cache->lru_head) && (cache_entry == cache->lru_tail)) {
// the only entry in the cache
check (cache->entry_count == 1, "failed to verify to be the only entry.");
cache->lru_head = NULL;
cache->lru_tail = NULL;
cache->entry_count = 0;
cache->lookup_table[table_slot] = NULL;
return 0;
} else {
if (cache_entry == cache->lru_head) {
if (cache_entry->lru_next != NULL) {
cache_entry->lru_next->lru_prev = NULL;
}
cache->lru_head = cache_entry->lru_next;
} else if (cache_entry == cache->lru_tail) {
if (cache_entry->lru_prev != NULL) {
cache_entry->lru_prev->lru_next = NULL;
}
cache->lru_tail = cache_entry->lru_prev;
} else {
cache_entry->lru_prev->lru_next = cache_entry->lru_next;
cache_entry->lru_next->lru_prev = cache_entry->lru_prev;
}
}
cache_entry->lru_next = NULL;
cache_entry->lru_prev = NULL;
/* update lookup info */
if (cache->lookup_table[table_slot] == cache_entry) {
check(cache_entry->lookup_prev==NULL, "failed to verify lookup head.");
if (cache_entry->lookup_next != NULL) {
cache_entry->lookup_next->lookup_prev = NULL;
}
cache->lookup_table[table_slot] = cache_entry->lookup_next;
} else if (cache_entry->lookup_next == NULL) {
cache_entry->lookup_prev->lookup_next = NULL;
} else {
cache_entry->lookup_prev->lookup_next = cache_entry->lookup_next;
cache_entry->lookup_next->lookup_prev = cache_entry->lookup_prev;
}
cache_entry->lookup_next = NULL;
cache_entry->lookup_prev = NULL;
/* move the last entry to the current position*/
check (cache->entry_count >= 1, "failed to remove entry from empty cache.");
LRUCacheEntry *entry = &(cache->cache_entry[cache->entry_count-1]);
/* if ((entry->block_num == 3314217) && (entry->server_num == 0)
&& (entry->volume_num == 0)) {
printf("debug\n");
}*/
if (cache_entry == entry) {
cache->entry_count -= 1;
return 0;
}
if (entry->lru_prev != NULL) {
entry->lru_prev->lru_next = cache_entry;
}
if (entry->lru_next != NULL) {
entry->lru_next->lru_prev = cache_entry;
}
if (entry->lookup_prev != NULL) {
entry->lookup_prev->lookup_next = cache_entry;
}
if (entry->lookup_next != NULL) {
entry->lookup_next->lookup_prev = cache_entry;
}
table_slot = entry->block_num % cache->lookup_table_size;
if (cache->lookup_table[table_slot] == entry) {
cache->lookup_table[table_slot] = cache_entry;
}
if (cache->lru_head == entry) {
cache->lru_head = cache_entry;
}
if (cache->lru_tail == entry) {
cache->lru_tail = cache_entry;
}
/*copy the content of the last entry to the current position */
cache_entry->block_num = entry->block_num;
cache_entry->server_num = entry->server_num;
cache_entry->volume_num = entry->volume_num;
cache_entry->write_access_count = entry->write_access_count;
cache_entry->lookup_next = entry->lookup_next;
cache_entry->lookup_prev = entry->lookup_prev;
cache_entry->lru_next = entry->lru_next;
cache_entry->lru_prev = entry->lru_prev;
/* move entry to the lru head */
if (cache->lru_head != cache_entry) {
if (cache->lru_tail == cache_entry ){
cache_entry->lru_prev->lru_next = NULL;
cache->lru_tail = cache_entry->lru_prev;
cache_entry->lru_prev = NULL;
cache_entry->lru_next = cache->lru_head;
cache->lru_head->lru_prev = cache_entry;
cache->lru_head = cache_entry;
} else {
cache_entry->lru_prev->lru_next = cache_entry->lru_next;
cache_entry->lru_next->lru_prev = cache_entry->lru_prev;
cache_entry->lru_prev = NULL;
cache_entry->lru_next = cache->lru_head;
cache->lru_head->lru_prev = cache_entry;
cache->lru_head = cache_entry;
}
}
cache->entry_count -= 1;
return 0;
error:
return -1;
}
int lru_cache_insert (LRUCache *cache, uint64_t block_num, uint8_t server_num,
uint8_t volume_num, Request *replaced_req)
{
/* if ((block_num == 3314217) && (server_num == 0)
&& (volume_num == 0)) {
printf("debug\n");
}*/
LRUCacheEntry *cache_entry = NULL;
int table_slot = -1;
int ret = 0;
cache->num_writes += 1;
if (cache->entry_count == cache->cache_size) {
/* replacing an existing entry */
cache_entry = cache->lru_tail;
replaced_req->block_num = cache_entry->block_num;
replaced_req->server_num = cache_entry->server_num;
replaced_req->volume_num = cache_entry->volume_num;
cache->num_replaces += 1;
ret = 1;
/* updating LRU info */
cache->lru_tail = cache_entry->lru_prev;
cache_entry->lru_prev->lru_next = NULL;
cache_entry->lru_prev = NULL;
cache_entry->lru_next = cache->lru_head;
cache->lru_head->lru_prev = cache_entry;
cache->lru_head = cache_entry;
/* update lookup info */
/* remove it from the original lookup chain */
table_slot = cache_entry->block_num % cache->lookup_table_size;
if (cache->lookup_table[table_slot] == cache_entry) {
/*if it is the first one in the chain */
if (cache_entry->lookup_next == NULL) {
/* if it is the only one in the chain */
cache->lookup_table[table_slot] = NULL;
} else {
/* if it is first one but not the only one */
cache->lookup_table[table_slot] = cache_entry->lookup_next;
cache_entry->lookup_next->lookup_prev = NULL;
}
} else {
/* if it is not the first one in the chain */
if (cache_entry->lookup_next == NULL) {
/* if it is the last one in the chain */
cache_entry->lookup_prev->lookup_next = NULL;
} else {
/* if it is neither the first one nor the last one */
cache_entry->lookup_prev->lookup_next = cache_entry->lookup_next;
cache_entry->lookup_next->lookup_prev = cache_entry->lookup_prev;
}
}
/* insert it to the head of the new lookup chain */
table_slot = block_num % cache->lookup_table_size;
cache_entry->lookup_prev = NULL;
cache_entry->lookup_next = cache->lookup_table[table_slot];
if (cache->lookup_table[table_slot] != NULL) {
cache->lookup_table[table_slot]->lookup_prev = cache_entry;
}
cache->lookup_table[table_slot] = cache_entry;
cache_entry->block_num = block_num;
cache_entry->server_num = server_num;
cache_entry->volume_num = volume_num;
cache_entry->write_access_count = 0;
} else {
/* inserting a new entry */
cache_entry = &(cache->cache_entry[cache->entry_count]);
cache_entry->block_num = block_num;
cache_entry->server_num = server_num;
cache_entry->volume_num = volume_num;
cache_entry->write_access_count = 0;
/* update LRU info */
if (cache->lru_head == NULL) {
/* very first entry */
cache->lru_head = cache_entry;
cache->lru_tail = cache_entry;
cache_entry->lru_prev = NULL;
cache_entry->lru_next = NULL;
} else {
cache_entry->lru_prev = NULL;
cache_entry->lru_next = cache->lru_head;
cache->lru_head->lru_prev = cache_entry;
cache->lru_head = cache_entry;
}
/* update lookup info */
table_slot = block_num % cache->lookup_table_size;
cache_entry->lookup_prev = NULL;
cache_entry->lookup_next = cache->lookup_table[table_slot];
if (cache->lookup_table[table_slot] != NULL) {
cache->lookup_table[table_slot]->lookup_prev = cache_entry;
}
cache->lookup_table[table_slot] = cache_entry;
cache->entry_count += 1;
}
return ret;
}
void lru_cache_destroy (LRUCache *cache)
{
if (cache->cache_entry != NULL) {
free (cache->cache_entry);
}
if (cache->lookup_table != NULL) {
free (cache->lookup_table);
}
free (cache);
}
void print_lru_cache (LRUCache *cache)
{
int i;
LRUCacheEntry *cache_entry = NULL;
printf("++++++++++++++++++++++++++++++++++++\n");
for (i = 0; i < cache->lookup_table_size; i++) {
printf("SLOT %d: ", i);
cache_entry = cache->lookup_table[i];
while (cache_entry != NULL) {
printf("%"PRIu64"\t", cache_entry->block_num);
cache_entry = cache_entry->lookup_next;
}
printf("\n");
}
printf("LRU:\t");
cache_entry = cache->lru_head;
while (cache_entry != NULL) {
printf("%"PRIu64"\t", cache_entry->block_num);
cache_entry = cache_entry->lru_next;
}
printf("\n++++++++++++++++++++++++++++++++++++\n\n");
}
/*void lru_cache_test ()
{
int ret = 0;
int i;
int block_array[20] = { 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2,
3, 4, 5 };
Request req, replaced_req;
LRUCache *lru_cache = (LRUCache *) calloc(1, sizeof(LRUCache));
ret = lru_cache_init(16, lru_cache);
for (i = 0; i < 20; i++) {
req.block_num = block_array[i];
req.server_num = 0;
req.volume_num = 0;
ret = lru_cache_lookup(lru_cache, &req);
if (ret == 0) {
lru_cache_insert(lru_cache, req.block_num, req.server_num,
req.volume_num, &replaced_req);
}
print_lru_cache(lru_cache);
}
lru_cache_destroy(lru_cache);
}*/