-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.c
More file actions
179 lines (141 loc) · 3.9 KB
/
cache.c
File metadata and controls
179 lines (141 loc) · 3.9 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
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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "cache.h"
#include "jbod.h"
static cache_entry_t *cache = NULL;
static int cache_size = 0;
static int num_queries = 0;
static int num_hits = 0;
int insert_flag = 0;
int remaining;
cache_entry_t *c;
int cache_create(int num_entries) {
if(num_entries < 2 || num_entries > 4096){ // cache has to be greater than 2 and less than 4096
return -1;
}
else if (cache != NULL){
return -1;
}
//allocating memory in heap and making changes to all the parameters
else{
cache = calloc(num_entries, sizeof(cache_entry_t));
c = cache;
cache_size = num_entries;
remaining = num_entries;
return 1;
}
return -1;
}
int cache_destroy(void) {
if(cache == NULL){
return -1;
}
// freeing memory and making necessary changes to the parameters
else{
free(cache);
cache = NULL;
cache_size = 0;
insert_flag = 0;
return 1;
}
return -1;
}
int cache_lookup(int disk_num, int block_num, uint8_t *buf) {
if (insert_flag == 0){
return -1;
}
else if(cache == NULL || buf == NULL){
return -1;
}
else if(block_num < 0 || block_num >= 256){
return -1;
}
else if(disk_num < 0 || disk_num >= 16){
return -1;
}
else{
num_queries += 1;
for(int j = 0; j <cache_size; j++){ // iterating through cache to see if it already exists
if(cache[j].block_num == block_num && cache[j].disk_num == disk_num){
memcpy(buf, cache[j].block, JBOD_BLOCK_SIZE); // copying contents and making necessary changes to parameters
cache[j].num_accesses += 1;
num_hits += 1;
return 1;
}
}
}
return -1;
}
void cache_update(int disk_num, int block_num, const uint8_t *buf) {
for(cache_entry_t *i = cache; i < cache + cache_size; i++){ //iterating through cache to find the memory block
if(i->block_num == block_num && i->disk_num == disk_num){
memcpy(i->block, buf, JBOD_BLOCK_SIZE); // updating the content in cach everytime we write something on the disk
i->num_accesses += 1;
return;
}
}
return;
}
int cache_insert(int disk_num, int block_num, const uint8_t *buf) {
// printf("here\n");
insert_flag = 1;
if(buf == NULL){
return -1;
}
else if(block_num < 0 || block_num >= 256){
return -1;
}
else if(disk_num < 0 || disk_num >= 16){
return -1;
}
else if(cache == NULL){
return -1;
}
else{
int x = 0;
int min = 0;
int a = 0;
for(int i = 0; i < cache_size; i ++){
if(cache[i].valid == true && block_num == cache[i].block_num && disk_num == cache[i].disk_num){
return -1;
}
}
for(int i = 0; i < cache_size; i++){
// inserting block in cache if cache has empty space
if(cache[i].valid != true){
cache[i].disk_num = disk_num;
cache[i].block_num = block_num;
memcpy(cache[i].block, buf, JBOD_BLOCK_SIZE);
cache[i].num_accesses = 1;
a++;
cache[i].valid = true;
return 1;
}
// finding the block in cache with minimum accesses for LFU
else{
if(cache[i].num_accesses < min){
min = cache[i].num_accesses;
x = i;
}
}
}
// replacing block selected by LFU with new block
cache[x].disk_num = disk_num;
cache[x].block_num = block_num;
memcpy(cache[x].block, buf, JBOD_BLOCK_SIZE);
cache[x].num_accesses = 1;
cache[x].valid = true;
return 1;
}
return -1;
}
bool cache_enabled(void) {
return cache != NULL && cache_size > 0;
}
void cache_print_hit_rate(void) {
fprintf(stderr, "num_hits: %d, num_queries: %d\n", num_hits, num_queries);
fprintf(stderr, "Hit rate: %5.1f%%\n", 100 * (float) num_hits / num_queries);
}