-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsim.c
325 lines (251 loc) · 6.45 KB
/
csim.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
//NAME : Sanghyeon Park
//ID : tkdgus2916
#include "cachelab.h"
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <argp.h>
typedef unsigned long addr_size;
typedef struct {
int hits;
int misses;
int evictions;
}result;
typedef struct Elem {
bool valid;
addr_size tag_val;
struct Elem* prev;
struct Elem* next;
} Elem;
typedef struct Cache {
int capacity;
int size;
Elem* head;
Elem* tail;
} Cache;
bool verbose_on = false;
int set_index_num;
int associativity;
int block_num;
char *file_name;
result res = {0, 0, 0};
/* Argument Pasrser */
void print_options(){
printf(
"Usage: ./csim [-hv] -s <num> -E <num> -b <num> -t <file>\n"
"Options:\n"
" -h Print this help message.\n"
" -v Optional verbose flag.\n"
" -s <num> Number of set index bits.\n"
" -E <num> Number of lines per set.\n"
" -b <num> Number of block offset bits.\n"
" -t <file> Trace file.\n\n"
"Examples:\n"
" linux> ./csim -s 4 -E 1 -b 4 -t traces/yi.trace\n"
" linux> ./csim -v -s 8 -E 2 -b 4 -t traces/yi.trace\n");
}
void arg_parser(int argc, char *argv[]){
if(argc < 2){
printf("%s : Missing required command line argument\n", argv[0]);
print_options();
exit(EXIT_FAILURE);
}
int arg_count = 0;
size_t optind;
for (optind = 1; optind < argc && argv[optind][0] == '-'; optind++) {
switch (argv[optind][1]) {
case 'h': print_options(); exit(EXIT_FAILURE);
case 'v': verbose_on = true; break;
case 's': set_index_num = atoi(argv[++optind]); arg_count++; break;
case 'E': associativity = atoi(argv[++optind]); arg_count++; break;
case 'b': block_num = atoi(argv[++optind]); arg_count++; break;
case 't': file_name = argv[++optind]; arg_count++; break;
default:
printf("%s : invalid option -- '%c'\n", argv[0], argv[optind][1]);
print_options();
exit(EXIT_FAILURE);
}
}
if(arg_count != 4){
printf("%s : Missing required command line argument\n", argv[0]);
print_options();
exit(EXIT_FAILURE);
}
argv += optind;
}
/*Bit API*/
addr_size get_mask_bit(int num){
addr_size mask = 0xFFFFFFFFFFFFFFFF;
return ~(mask << num);
}
addr_size get_set_bit(addr_size addr){
addr >>= block_num;
return addr & get_mask_bit(set_index_num);
}
unsigned get_tag_bit(addr_size addr){
unsigned tag_idx = block_num + set_index_num;
addr >>= tag_idx;
return addr & get_mask_bit(64 - tag_idx);
}
// Cache line & elem API
Elem* create_elem(addr_size tag_val) {
Elem* newelem = (Elem*)malloc(sizeof(Elem));
newelem->tag_val = tag_val;
newelem->prev = NULL;
newelem->next = NULL;
return newelem;
}
Cache* init_cache_line(int capacity) {
Cache* cache = (Cache*)malloc(sizeof(Cache));
cache->capacity = capacity;
cache->size = 0;
cache->head = NULL;
cache->tail = NULL;
return cache;
}
bool find_elem(Cache* cache, addr_size tag_val) {
//Find from the first element
Elem *current = cache->head;
// Search the matching elem in list
while (current != NULL) {
if (current->tag_val == tag_val) {
// When find the matching elem, then move to front
if (current != cache->head) {
if (current == cache->tail) {
cache->tail = current->prev;
cache->tail->next = NULL;
} else {
current->prev->next = current->next;
current->next->prev = current->prev;
}
current->prev = NULL;
current->next = cache->head;
cache->head->prev = current;
cache->head = current;
}
if(verbose_on)
printf(" hit");
res.hits++;
return true;
}
current = current->next;
}
if(verbose_on)
printf(" miss");
res.misses++;
return false;
}
void add_elem(Cache* cache, addr_size tag_val) {
if (cache == NULL) exit(1);
if(find_elem(cache, tag_val))
return;
Elem* newelem = create_elem(tag_val);
newelem->next = cache->head;
if (cache->head != NULL) {
cache->head->prev = newelem;
}
cache->head = newelem;
if (cache->tail == NULL) {
cache->tail = newelem;
}
// When cache is full, evict the old cache element
if (cache->size == cache->capacity) {
if(verbose_on)
printf(" eviction");
res.evictions++;
Elem* temp = cache->tail;
cache->tail = cache->tail->prev;
cache->tail->next = NULL;
free(temp);
cache->size--;
}
cache->size++;
}
void printCache(Cache* cache) {
printf("LRU Cache: ");
Elem* current = cache->head;
while (current != NULL) {
printf("(%lx) ", current->tag_val);
current = current->next;
}
printf("\n");
}
void free_cache_line(Cache* cache) {
Elem* current = cache->head;
while (current != NULL) {
Elem* temp = current;
current = current->next;
free(temp);
}
free(cache);
}
/* Cache API*/
Cache **init_cache(){
Cache **cache = (Cache **)malloc((1<<set_index_num) * sizeof(Cache *));
if(cache == NULL)
return NULL;
for (int i = 0; i < (1<<set_index_num); i++) {
cache[i] = init_cache_line(associativity);
}
return cache;
}
void free_cache(Cache **cache){
for (int i = 0; i < set_index_num; ++i) {
free_cache_line(cache[i]);
}
free(cache);
}
void cache_store(Cache **cache, addr_size addr, int size){
addr_size set_bit = get_set_bit(addr);
addr_size tag_bit = get_tag_bit(addr);
add_elem(cache[set_bit], tag_bit);
}
void cache_load(Cache **cache, addr_size addr, int size){
addr_size set_bit = get_set_bit(addr);
addr_size tag_bit = get_tag_bit(addr);
add_elem(cache[set_bit], tag_bit);
}
int main(int argc, char *argv[])
{
Cache **cache;
//Parsed data
char type;
long addr;
int size;
//Get the argument
arg_parser(argc, argv);
//Check NULL pointer
if(!(cache = init_cache())){
printf("Can not allocate memory\n");
return 1;
}
//Open file
FILE *file = fopen(file_name, "r");
if (file == NULL) {
printf("File does not exist\n");
return 1;
}
//Read line
while (fscanf(file, " %c", &type) == 1) {
fscanf(file, " %lx,%d", &addr, &size);
if(verbose_on)
printf("%c %lx,%d", type, addr, size);
if(type == 'I')
continue;
else if(type == 'S')
cache_store(cache, addr, size);
else if(type == 'L')
cache_load(cache, addr, size);
else{
cache_load(cache, addr, size);
cache_store(cache, addr, size);
}
if(verbose_on)
printf("\n");
}
fclose(file); // 파일 닫기
printSummary(res.hits, res.misses, res.evictions);
free_cache(cache);
return 0;
}