-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.c
More file actions
149 lines (138 loc) · 3.89 KB
/
cache.c
File metadata and controls
149 lines (138 loc) · 3.89 KB
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "cache.h"
static cache_entry_t *cache = NULL;
static int cache_size = 0;
static int clock = 0;
static int num_queries = 0;
static int num_hits = 0;
int cache_create(int num_entries) {
//Parameter Checks
if(num_entries < 2 || num_entries > 4096) {
return -1;
}
//If Cache does not exist then allocate memory to cache
if(cache == NULL) {
cache = calloc(num_entries, sizeof(cache_entry_t));
cache_size = num_entries;
return 1;
}
return -1;
}
int cache_destroy(void) {
//If Cache exists then free memory used by the cache
if(cache != NULL) {
free(cache);
cache = NULL;
cache_size = 0;
clock = 0;
return 1;
}
return -1;
}
int cache_lookup(int disk_num, int block_num, uint8_t *buf) {
//If cache or buffer of invalid size / dont exist
if(cache == NULL || buf == NULL) {
return -1;
}
//Bounds Check
if(block_num < 0 || block_num >= 256) {
return -1;
}
if(disk_num < 0 || disk_num >= 16) {
return -1;
}
num_queries += 1;
int index = 0;
//Lookup the block identified by disk_num and block_num in the cache.
while(index < cache_size) {
//If found in cache copy from cache to to buffer
if(cache[index].disk_num == disk_num && cache[index].block_num == block_num && cache[index].valid) {
memcpy(buf, cache[index].block, 256);
clock += 1;
cache[index].access_time = clock;
num_hits += 1;
return 1;
}
index += 1;
}
return -1;
}
void cache_update(int disk_num, int block_num, const uint8_t *buf) {
//If cacher or buffer of invalid size /Dont exist
if(cache == NULL || buf == NULL) {
return;
}
//Bounds Check
if(block_num < 0 || block_num >= 256) {
return;
}
if(disk_num < 0 || disk_num >= 16) {
return;
}
int index = 0;
//Lookup the block identified by disk_num and block_num in the cache.
while(index < cache_size) {
//If location in cache is found with corresponding block and disk nums, copy from buffer into cache location
if(cache[index].disk_num == disk_num && cache[index].block_num == block_num && cache[index].valid) {
memcpy(cache[index].block, buf, 256);
clock += 1;
cache[index].access_time = clock;
return;
}
index += 1;
}
return;
}
int cache_insert(int disk_num, int block_num, const uint8_t *buf) {
//If cache or buffer of invalid size / Dont exist
if(cache == NULL || buf == NULL) {
return -1;
}
//Bounds Check
if(block_num < 0 || block_num >= 256) {
return -1;
}
if(disk_num < 0 || disk_num >= 16) {
return -1;
}
int index = 0;
int lru_index = 0;
while(index < cache_size) {
//If block entry for block and disk num exist, return -1
if(cache[index].disk_num == disk_num && cache[index].block_num == block_num && cache[index].valid) {
return -1;
}
//else insert entry into cache for that block and disk num
if(cache[index].valid == false) {
memcpy(cache[index].block, buf, 256);
cache[index].block_num = block_num;
cache[index].disk_num = disk_num;
cache[index].valid = true;
clock += 1;
cache[index].access_time = clock;
return 1;
}
//Least Recently Used Algorithim to evict least recently used entry and replace it with new one.
if(cache[index].access_time < cache[lru_index].access_time) {
lru_index = index;
}
index += 1;
}
//Copy contents of buffer into the cache and update the cache entry properties
memcpy(cache[lru_index].block, buf, 256);
cache[lru_index].block_num = block_num;
cache[lru_index].disk_num = disk_num;
cache[lru_index].valid = true;
clock += 1;
cache[lru_index].access_time = clock;
return 1;
}
bool cache_enabled(void) {
//Cache parameters checked in previous code
return (cache != NULL);
}
void cache_print_hit_rate(void) {
fprintf(stderr, "Hit rate: %5.1f%%\n", 100 * (float) num_hits / num_queries);
}