-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmr3429-handin.tar
664 lines (591 loc) · 30 KB
/
rmr3429-handin.tar
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
csim.c 0000600 0107025 0000025 00000033377 13273004741 011200 0 ustar rmr3429 under // Ryan Resma rmr3429
// libraries included for implementation of this cache
#include "cachelab.h"
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <stdint.h>
#include <ctype.h>
// error identifiers
#define MISSING_FLAG 1
#define MISSING_ARGUMENT 2
#define MISSING_FILENAME 3
#define FILE_NAME_NOT_FOUND 4
// flags
#define FLAGS_h 0
#define FLAGS_v 1
#define FLAGS_s 2
#define FLAGS_E 3
#define FLAGS_b 4
#define FLAGS_t 5
#define FLAGS_COUNT 6
// size identifiers
#define VALID_BIT_SIZE 1
#define POINTER_SIZE 8
#define MEMORY_ADDRESS_SIZE 64
// bases used
#define HEXADECIMAL 16
#define DECIMAL 10
// types of cache accesses
#define MISS_EMPTY 0
#define MISS_FULL 1
#define HIT 2
// INTEGER.MAX_VALUE
#define MAX_INT 2147483647
// Cache struct, contains sets
typedef struct Cache {
struct Set **sets; // arr of sets in cache
int numSets;
int clock;
} Cache;
typedef struct Set {
struct Line **lines; // arr of lines in set
int numLines;
} Set;
typedef struct Line {
char *full; // full line
bool valid;
int tag;
int totSize;
int tagSize;
int blockSize;
int lastTimeUsed; // used for replacement policy
} Line;
void formatHelp(char *op);
void initiateFlags(bool flags[], int argc, char *argv[]);
bool collectInput(int *s, int *E, int *b, int argc, char *argv[]);
bool collectFileName(char **fileName, int argc, char *argv[]);
void makeCache(Cache *cache, int s, int E, int b);
void flushCache(Cache *cache);
void parseFile(Cache *cache, char *fileName, bool vbool, int *hits, int *misses, int *evictions);
int useCache(Cache *cache, long address);
void cutLeadingAddressZeros(char str[]);
void trimEnd(char str[]);
void getBin(int64_t num, char *str);
void reverseStr(char *p);
int main(int argc, char *argv[])
{
int STATUS = 0;
int hits = 0, misses = 0, evictions = 0;
int s = -1, E = -1, b = -1; // error checking to make sure set
char *fileName;
// Makes sure arguments exist in order: h, v, s, E, b, t
bool flags[FLAGS_COUNT] = {false, false, false, false, false, false};
initiateFlags(flags, argc, argv);
if(flags[FLAGS_h]) { // help screen is called
formatHelp(argv[0]);
return 0;
} else if(!flags[FLAGS_s] || !flags[FLAGS_E] || !flags[FLAGS_b] || !flags[FLAGS_t]
|| !collectInput(&s, &E, &b, argc, argv)) { // missing flags or missing vals
fprintf(stderr, "%s: Error: missing command-line arg\n", argv[0]);
formatHelp(argv[0]);
STATUS = MISSING_FLAG;
} else if(!collectFileName(&fileName, argc, argv)) { // if no file is specified
fprintf(stderr, "%s: Error: flag needs argument -- 't'\n", argv[0]);
formatHelp(argv[0]);
STATUS = MISSING_FILENAME;
} else if(access(fileName, R_OK) == -1) {
fprintf(stderr, "%s: Error: No such file or directory", fileName);
STATUS = FILE_NAME_NOT_FOUND;
} else { // everything is ok, simulate
// create cache
Cache *cache = malloc(sizeof(Cache));
makeCache(cache, s, E, b);
// simulate cache
parseFile(cache, fileName, flags[FLAGS_v], &hits, &misses, &evictions);
// print results
printSummary(hits, misses, evictions);
//
flushCache(cache);
}
return STATUS;
}
// helper function to set boolean flags
void initiateFlags(bool flags[], int argc, char *argv[]) {
int idx; // counter
char temp; // temporary flag
// parse through the arguments
for (idx = 0; idx < argc; idx++) {
// check if a given character flag is found
if (argv[idx][0] == '-' && argv[idx][2] == '\0') {
temp = argv[idx][1]; // assign flag
// assign boolean values to flags arr
if (temp == 'h') {
flags[FLAGS_h] = true;
} else if (temp == 'v') {
flags[FLAGS_v] = true;
} else if (temp == 's') {
flags[FLAGS_s] = true;
} else if (temp == 'E') {
flags[FLAGS_E] = true;
} else if (temp == 'b') {
flags[FLAGS_b] = true;
} else if (temp == 't') {
flags[FLAGS_t] = true;
}
}
}
}
// helper function to return true or false based on successful v unsuccessful
// input data collection from the command line
bool collectInput(int *s, int *E, int *b, int argc, char *argv[]) {
int idx; // counter
char temp; // temporary flag
char **eof = '\0'; // strtol arg, used to be next char after numerical value
// parse the command line but not over last arg (cant be flag)
for (idx = 1; idx < argc -1; idx ++) {
// check if a given character flag is found
if (argv[idx][0] == '-' && argv[idx][2] == '\0') {
temp = argv[idx][1]; // assign flag
if (temp == 's') {
*s = strtol(argv[idx + 1], eof, DECIMAL); // convert string to long
if (*s <= 0) { // check if 0 or negative long, if so, NaN or invalid
return false; // break out of loop
}
} else if (temp == 'E') {
*E = strtol(argv[idx + 1], eof, DECIMAL); // convert string to long
if (*E <= 0) { // check if 0 or negative long, if so, NaN or invalid
return false; // break out of loop
}
} else if (temp == 'b') {
*b = strtol(argv[idx + 1], eof, DECIMAL); // convert string to long
if (*b <= 0) { // check if 0 or negative long, if so, NaN or invalid
return false; // break out of loop
}
}
}
}
return true; // successful input collectiokn
}
bool collectFileName(char **fileName, int argc, char *argv[]) {
int idx; // arg counter
char temp; // temporary flag
// parse the command line, searching for the -t flag to signal for trace file name
// skip over last arg for it cannot be a flag
for (idx = 1; idx < argc - 1; idx++) {
// check if a given character flag is found
if (argv[idx][0] == '-' && argv[idx][2] == '\0') {
temp = argv[idx][1]; // assign flag
// -t flag signals trace file name
if (temp == 't') {
*fileName = argv[idx + 1]; // assign fileName char array to the next argument
return true; // break out, successful in finding loop
}
}
}
return false; // unsuccessful in finding file name
}
// simulation of cache
void parseFile(Cache *cache, char *fileName, bool vbool, int *hits, int *misses, int *evictions) {
int state; // state of cache
char **endptr = '\0'; // endptr arg needed for strtol
int64_t address; // address being read
FILE *file; // file being read
char buff[100]; // number of characters being read in at a time is 100
char verbose[20]; // verbose mode
char temp; // temporary var
bool toPrint; // boolean to print
file = fopen(fileName, "r"); // open the file passed in read mode
// parse entire file until eof char is reached
while (1) {
fgets(buff, 100, file); // read in 100 characters
if (feof(file)) {
break;
}
toPrint = false;
// check if first character is space, for all our mem accesses starts w/ [space] *type of access*
if (buff[0] == ' ') {
toPrint = true; // valid print
address = strtol(&buff[3], endptr, 16); // address starts at the 4th space
temp = buff[1]; // type of memory access at 2nd space
// same operations for both store and load
if (temp == 'S' || temp == 'L') {
state = useCache(cache, address); // use the cache
if(state == MISS_EMPTY) {
(*misses)++;
strcpy(verbose, "miss");
} else if(state == MISS_FULL) {
(*misses)++;
(*evictions)++;
strcpy(verbose, "miss eviction");
} else {
(*hits)++;
strcpy(verbose, "hit");
}
} else if (temp == 'M') { // data modify
state = useCache(cache, address);
if(state == MISS_EMPTY) {
(*misses)++;
(*hits)++;
strcpy(verbose, "miss hit");
} else if(state == MISS_FULL) {
(*misses)++;
(*evictions)++;
(*hits)++;
strcpy(verbose, "miss eviction hit");
} else {
(*hits) += 2;
strcpy(verbose, "hit hit");
}
}
}
if(vbool && toPrint) {
cutLeadingAddressZeros(buff);
trimEnd(buff);
trimEnd(verbose);
printf("%s %s \n", &buff[1], verbose); // 1 is to get rid of the starting space that's always before a L, S, or M instruction in trace files
}
}
}
// function to access the cache
int useCache(Cache *cache, int64_t address) {
int tagBits, indexbits, i, numOfLines, LRUIndex, LRUTime; // local vars
long tag, index; // vars to store the tag and the index
char binary[66]; // addresses are 64 bits long + 1 EOL char
char tagS[64];
char indexS[64];
char **endptr = '\0'; // eof character needed for strtol arg
Set *set;
Line **lines, *line;
bool empty = false; // cache is not empty right now
int status = -1; // status to be returned at the end of the method
LRUIndex = 0; // vars for Least Recently Used replacement policy
LRUTime = MAX_INT;
tagBits = cache->sets[0]->lines[0]->tagSize; // init tag bits
indexbits = cache->numSets; // init index bits
indexbits = (int) log2(indexbits);
getBin(address, binary);
binary[65] = '\0';
reverseStr(binary);
binary[64] = '\0';
strncpy(tagS, &binary[0], tagBits);
tagS[tagBits] = '\0';
strncpy(indexS, &binary[tagBits], indexbits); // starts after tagBits
indexS[indexbits] = '\0';
tag = strtol(tagS, endptr, 2);
index = strtol(indexS, endptr, 2);
set = cache->sets[index];
lines = set->lines;
numOfLines = set->numLines;
cache->clock++;
// parse through the lines of the given set
for(i = 0; i < numOfLines; i++) {
line = lines[i];
if(!(line->valid)) { // if line is not valid
empty = true; // full line
LRUIndex = i;
} else if(line->tag == tag) { // HIT
status = HIT;
line->lastTimeUsed = cache->clock;
break;
}
if(!empty && line->lastTimeUsed < LRUTime) { // look oldest index if full
LRUIndex = i;
LRUTime = line->lastTimeUsed;
}
}
if(status != HIT && !empty) { // MISS_FULL
lines[LRUIndex]->tag = tag;
lines[LRUIndex]->lastTimeUsed = cache->clock;
status = MISS_FULL;
} else if(status != HIT && empty) { // MISS_EMPTY
lines[LRUIndex]->tag = tag;
lines[LRUIndex]->lastTimeUsed = cache->clock;
lines[LRUIndex]->valid = true;
status = MISS_EMPTY;
}
if(status < 0) {
fprintf(stderr, "ERROR! STATUS = %d\n", status);
}
return status;
}
// helper function to create cache
void makeCache(Cache *cache, int s, int E, int b) {
int setIndex, lineIndex, numOfSets;
Set **sets, *set;
Line *line;
cache->numSets = (int) pow(2, s); // number of sets = 2^s
numOfSets = cache->numSets; // removes dereference penalty in loop
cache->sets = malloc(numOfSets * POINTER_SIZE);
sets = cache->sets;
cache->clock = 0;
for(setIndex = 0; setIndex < numOfSets; setIndex++) { // init each set
sets[setIndex] = malloc(sizeof(Set));
set = sets[setIndex];
set->lines = malloc(E * POINTER_SIZE);
set->numLines = E;
for(lineIndex = 0; lineIndex < E; lineIndex++) { // init each line
set->lines[lineIndex] = malloc(sizeof(Line));
line = set->lines[lineIndex];
line->valid = false;
line->lastTimeUsed = -1;
line->tagSize = MEMORY_ADDRESS_SIZE - (s + b); // formula from book
line->blockSize = b;
line->totSize = VALID_BIT_SIZE + line->tagSize + line->blockSize;
line->full = malloc(line->totSize * sizeof(char));
line->tag = -1;
}
}
}
// helper function to reset a cache
void flushCache(Cache *cache) {
int setIndex, lineIndex; // temps
Set **sets, *set; // temp set ptr
Line *line; // temp line ptr
sets = cache->sets; // assign sets from given cache
// parse through all sets
for(setIndex = 0; setIndex < cache->numSets; setIndex++) { // free each set
set = sets[setIndex]; // temp set
// parse through all lines in given set
for(lineIndex = 0; lineIndex < set->numLines; lineIndex++) { // free each line
line = set->lines[lineIndex];
free(line->full);
free(line);
}
free(set->lines);
free(set);
}
free(sets);
free(cache); // freed all data storages
}
void getBin(int64_t num, char *str) {
int64_t mask = 0x1;
*str++ = 'e';
while(mask != 0) {
*str++ = !!(mask & num) + '0';
mask <<= 1;
}
}
// helper function to reverse a string
void reverseStr(char s[]) {
// local var for length
int length = strlen(s) ;
int temp, i, j; // temps
// sandwich the middle of the array of chars, swapping ith and jth elements
for (i = 0, j = length - 1; i < j; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
void cutLeadingAddressZeros(char str[]) {
int i = 0, firstDigitIndex;
char buff[30];
// as long as input is valid, first digit will never be num
while(!isdigit(str[++i])) { // find the first digit
i++;
}
firstDigitIndex = i;
if(str[firstDigitIndex] == '0') {
while(str[++i] == '0') { // find first nonzero digit
i++;
}
strcpy(buff, &str[i]); // move to buffer to prevent mem errors
strcpy(&str[firstDigitIndex], buff);
}
}
void trimEnd(char str[]) {
int i = strlen(str); // get end of str
while(str[--i] <= 32) { // while str is not a printable char
i++;
}
// after loop i is the last non-space character in str
str[++i] = '\0';
}
// helper function to help print the instructions for the given flag modes
void formatHelp(char* op) {
printf("Usage: %s [-hv] -s <num> -E <num> -b <num> -t <file>\n", op);
printf("Options:\n");
printf(" -h Print this help message.\n");
printf(" -v Optional verbose flag.\n");
printf(" -s <num> Number of set index bits.\n");
printf(" -E <num> Number of lines per set.\n");
printf(" -b <num> Number of block offset bits.\n");
printf(" -t <file> Trace file.\n\n");
printf("Examples:\n");
printf(" linux> %s -s 4 -E 1 -b 4 -t traces/yi.trace\n", op);
printf(" linux> %s -v -s 8 -E 2 -b 4 -t traces/yi.trace\n", op);
}