This repository has been archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
641 lines (445 loc) · 19.7 KB
/
main.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//Emre Kumaş
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
typedef struct line{
time_t time;
char validBit;
int tag;
char *data;
}line;
typedef struct set{
line *lines;
}set;
void setGlobalVariables(char *argv[]);
void createCaches(set *L1I, set *L1D, set *L2);
void setValidBitsToZero(set *L1I, set *L1D, set *L2);
void traceFile(set *L1I, set *L1D, set *L2);
void loadInstructionOrData(bool loadInstruction, char *cacheName, set *L1I, set *L1D, set *L2, char *address, int size);
void dataStore(set *L1D, set *L2, char *address, int size, char *data);
void dataModify(set *L1I, set *L1D, set *L2, char *address, int size, char *data);
int returnLineNumberIfStored(set *L, int lineNumber, int setNum, int tag);
int copyInstructionBetweenCaches(set *source, set *dest, int sourceLineNumber, int sourceSetNum, int destSetNum, int destTag, int destTotalLine, int destBlockSize);
int copyInstructionFromRamToL2Cache(int int_address, set *L2, int setNumL2, int tagL2);
void copyDataToRAM(int int_address, int size, char *data);
void printSummary(char *cacheName, int hit, int miss, int eviction);
void cleanUp(set *L1I, set *L1D, set *L2);
//Global variables.
int L1s, L1E, L1b, L2s, L2E, L2b, L1S, L1B, L2S, L2B;
char *fileName;
//Hit, miss, eviction counters.
int L1I_hit = 0, L1I_miss = 0, L1I_eviction = 0;
int L1D_hit = 0, L1D_miss = 0, L1D_eviction = 0;
int L2_hit = 0, L2_miss = 0, L2_eviction = 0;
//Loop iterators.
int i, j;
int main(int argc, char *argv[]){
//Firstly, checking if the user entered correct number of command-line arguments.
if(argc == 1){
printf("You didn't enter any command-line arguments. Please enter them.\n");
return 0;
}else if(argc < 15){
printf("You entered %d number of command-line arguments, but you need to enter 14 values.\n", (argc-1));
return 0;
}
//Setting the global variables as the user entered.
setGlobalVariables(argv);
/////////////////////////////////////////////////////////////////////////////////////////////
//Lets create caches.
set L1I[L1S];
set L1D[L1S];
set L2[L2S];
createCaches(L1I, L1D, L2);
//After creating the caches, we need to make sure their valid bit is 0 initially.
setValidBitsToZero(L1I, L1D, L2);
/////////////////////////////////////////////////////////////////////////////////////////////
//Now, we need to read the trace file.
traceFile(L1I, L1D, L2);
/////////////////////////////////////////////////////////////////////////////////////////////
//Printing the results.
printf("\n");
printSummary("L1I", L1I_hit, L1I_miss, L1I_eviction);
printSummary("L1d", L1D_hit, L1D_miss, L1D_eviction);
printSummary("L2", L2_hit, L2_miss, L2_eviction);
printf("\n");
cleanUp(L1I, L1D, L2);
return 0;
}
void setGlobalVariables(char *argv[]){
L1s = (int) strtol(argv[2], NULL, 10);
L1E = (int) strtol(argv[4], NULL, 10);
L1b = (int) strtol(argv[6], NULL, 10);
L2s = (int) strtol(argv[8], NULL, 10);
L2E = (int) strtol(argv[10], NULL, 10);
L2b = (int) strtol(argv[12], NULL, 10);
fileName = argv[14];
L1S = (int) pow(2, L1s);
L2S = (int) pow(2, L2s);
L1B = (int) pow(2, L1b);
L2B = (int) pow(2, L2b);
}
void createCaches(set *L1I, set *L1D, set *L2){
int blockSizeL1 = (int) pow(2, L1b);
int blockSizeL2 = (int) pow(2, L2b);
//Allocating space for lines and datas.
for(i = 0; i < L1S; i++){
L1I[i].lines = malloc(L1E * sizeof(line));
L1D[i].lines = malloc(L1E * sizeof(line));
//Setting the block sizes.
for(j = 0; j < L1E; j++){
L1I[i].lines[j].data = malloc(blockSizeL1 * sizeof(char));
L1D[i].lines[j].data = malloc(blockSizeL1 * sizeof(char));
}
}
//Same thing for L2 cache.
for(i = 0; i < L2S; i++){
L2[i].lines = malloc(L2E * sizeof(line));
//Setting the block sizes.
for(j = 0; j < L2E; j++)
L2[i].lines[j].data = malloc(blockSizeL2 * sizeof(char));
}
}
void setValidBitsToZero(set *L1I, set *L1D, set *L2){
for(i = 0; i < L1S; i++){
for(j = 0; j < L1E; j++){
L1I[i].lines[j].validBit = 0;
L1D[i].lines[j].validBit = 0;
}
}
for(i = 0; i < L2S; i++){
for(j = 0; j < L2E; j++){
L2[i].lines[j].validBit = 0;
}
}
}
void traceFile(set *L1I, set *L1D, set *L2){
FILE *fp = NULL;
//If the file exists, we read it. If not, we exit the function with an error.
if(access(fileName, F_OK) != -1){
fp = fopen(fileName, "r");
}else{
printf("%s does not exist. Please make sure it exists before starting this program.\n", fileName);
return;
}
//Variables.
char operation;
int size;
char address[16], data[16];
//After reading the file, its time to read its content.
//While not reaching to the end of file, we will read it.
while(!feof(fp)) { // NOLINT
if(fscanf(fp, "%c", &operation) == 1){
//Now, we will look at the "operation" to understand which operation to execute.
switch(operation){
case 'I':
if(fscanf(fp, " %[^,] , %d\n", address, &size) == 2){ // NOLINT
printf("\nI %s, %d", address, size);
loadInstructionOrData(true, "L1I", L1I, L1D, L2, address, size);
}
break;
case 'L':
if(fscanf(fp, " %[^,] , %d\n", address, &size) == 2){ // NOLINT
printf("\nL %s, %d", address, size);
loadInstructionOrData(false, "L1D", L1I, L1D, L2, address, size);
}
break;
case 'S':
if(fscanf(fp, " %[^,] , %d , %s\n", address, &size, data) == 3){ // NOLINT
printf("\nS %s, %d, %s", address, size, data);
dataStore(L1D, L2, address, size, data);
}
break;
case 'M':
if(fscanf(fp, " %[^,] , %d , %s\n", address, &size, data) == 3){ // NOLINT
printf("\nM %s, %d, %s", address, size, data);
dataModify(L1I, L1D, L2, address, size, data);
}
break;
default:
//If we encounter any weird symbol it means this file is corrupted. We return from the function.
printf("\nThe program encountered an invalid operation type, exiting...\n");
return;
}
}
}
fclose(fp);
}
void loadInstructionOrData(bool loadInstruction, char *cacheName, set *L1I, set *L1D, set *L2, char *address, int size){
//At the start of the function, we need to tell program which set to use for the rest of the function.
set *setToUse;
if(loadInstruction)
setToUse = L1I;
else
setToUse = L1D;
//First, we need to convert the address into an appropriate format and also convert hex to decimal.
int int_address = (int) strtol(address, NULL, 16);
//Getting necessary information.
int blockNumL1 = int_address & ((int) pow(2, L1b) - 1); // NOLINT
int setNumL1 = (int_address >> L1b) & ((int) pow(2, L1s) - 1); // NOLINT
int tagSizeL1 = 32 - (L1s + L1b);
int tagL1 = (int_address >> (L1s + L1b)) & ((int) pow(2, tagSizeL1) - 1); // NOLINT
int blockNumL2 = int_address & ((int) pow(2, L2b) - 1); // NOLINT
int setNumL2 = (int_address >> L1b) & ((int) pow(2, L2s) - 1); // NOLINT
int tagSizeL2 = 32 - (L2s + L2b);
int tagL2 = (int_address >> (L2s + L2b)) & ((int) pow(2, tagSizeL2) - 1); // NOLINT
//Checking if the data can fit into cache block.
if(blockNumL1 + size > L1B || blockNumL2 + size > L2B){
//Means the data can't fit. We need to return.
printf("\nCan't fit the data for the size of wanted block. Please edit your trace file...\n");
exit(0);
}
printf("\n ");
//Then, we need to check if the instruction is in caches.
int L1_location = returnLineNumberIfStored(setToUse, L1E, setNumL1, tagL1);
int L2_location = returnLineNumberIfStored(L2, L2E, setNumL2, tagL2);
if(L1_location != -1){
////////////////////////////////// HIT IN L1
printf("%s hit", cacheName);
(loadInstruction == true) ? L1I_hit++ : L1D_hit++;
//Lets update the time variable.
setToUse[setNumL1].lines[L1_location].time = time(NULL);
//Now, lets check if the instruction is in L2 cache.
if(L2_location != -1){
////////////////////////////////// HIT IN L1 AND L2
printf(", L2 hit\n");
L2_hit++;
//Lets update the time variable.
L2[setNumL2].lines[L2_location].time = time(NULL);
}else{
////////////////////////////////// HIT IN L1 BUT MISS IN L2
//Means the instruction is in L1 cache but not stored in L2 cache. So we need to copy it from L1.
printf(", L2 miss\n");
L2_miss++;
L2_eviction += copyInstructionBetweenCaches(setToUse, L2, L1_location, setNumL1, setNumL2, tagL2, L2E, L2B);
}
}else{
////////////////////////////////// MISS IN L1
printf("%s miss", cacheName);
(loadInstruction == true) ? L1I_miss++ : L1D_miss++;
//After that, we need to check if the instruction is stored in L2 cache.
if(L2_location != -1) {
////////////////////////////////// MISS IN L1 BUT HIT IN L2
//The instruction is in L2 cache.
printf(", L2 hit\n");
L2_hit++;
//Lets update the time variable.
L2[setNumL2].lines[L2_location].time = time(NULL);
//Now, we need to load it into L1 cache.
int eviction_counter = copyInstructionBetweenCaches(L2, setToUse, L2_location, setNumL2, setNumL1, tagL1, L1E, L1B);
if(loadInstruction) L1I_eviction += eviction_counter;
else L1D_eviction += eviction_counter;
}else{
////////////////////////////////// MISS IN BOTH L1 AND L2
printf(", L2 miss\n");
L2_miss++;
//The instruction is not in L1 neither in L2. So we need to load it from RAM.
L2_location = copyInstructionFromRamToL2Cache(int_address, L2, setNumL2, tagL2);
int eviction_counter = copyInstructionBetweenCaches(L2, setToUse, L2_location, setNumL2, setNumL1, tagL1, L1E, L1B);
if(loadInstruction) L1I_eviction += eviction_counter;
else L1D_eviction += eviction_counter;
}
}
printf(" Place in L2 set %d, %s set %d\n", setNumL2, cacheName, setNumL1);
}
void dataStore(set *L1D, set *L2, char *address, int size, char *data){
//First, we need to convert the address into an appropriate format and also convert hex to decimal.
int int_address = (int) strtol(address, NULL, 16);
//Also, we need to convert the data to a an appropriate format.
char char_data[size], subString[2];
for(int a = 0; a < size; a++){
strncpy(subString, (data + a * 2), 2);
char_data[a] = (char) strtol(subString, NULL, 16);
}
//Getting necessary information.
int blockNumL1 = int_address & ((int) pow(2, L1b) - 1); // NOLINT
int setNumL1 = (int_address >> L1b) & ((int) pow(2, L1s) - 1); // NOLINT
int tagSizeL1 = 32 - (L1s + L1b);
int tagL1 = (int_address >> (L1s + L1b)) & ((int) pow(2, tagSizeL1) - 1); // NOLINT
int blockNumL2 = int_address & ((int) pow(2, L2b) - 1); // NOLINT
int setNumL2 = (int_address >> L1b) & ((int) pow(2, L2s) - 1); // NOLINT
int tagSizeL2 = 32 - (L2s + L2b);
int tagL2 = (int_address >> (L2s + L2b)) & ((int) pow(2, tagSizeL2) - 1); // NOLINT
//Checking if the data can fit into cache block.
if(blockNumL1 + size > L1B || blockNumL2 + size > L2B){
//Means the data can't fit. We need to return.
printf("\nCan't fit the data for the size of wanted block. Please edit your trace file...\n");
exit(0);
}
printf("\n ");
//Then, we need to check if the data is in caches.
int L1D_location = returnLineNumberIfStored(L1D, L1E, setNumL1, tagL1);
int L2_location = returnLineNumberIfStored(L2, L2E, setNumL2, tagL2);
if(L1D_location != -1) {
////////////////////////////////// HIT IN L1D
printf("L1D hit");
L1D_hit++;
//Now, we need to store our data in L1D.
L1D[setNumL1].lines[L1D_location].time = time(NULL);
memcpy(L1D[setNumL1].lines[L1D_location].data, char_data, (size_t) size);
//Now, lets check if the data is in L2 cache.
if(L2_location != -1) {
////////////////////////////////// HIT IN L1D AND L2
printf(", L2 hit\n");
L2_hit++;
//Lets store this data in L2 too.
L2[setNumL2].lines[L2_location].time = time(NULL);
memcpy(L2[setNumL2].lines[L2_location].data, char_data, (size_t) size);
}else{
////////////////////////////////// HIT IN L1D BUT MISS IN L2
//Means the data is in L1D cache but not stored in L2 cache. So we need to copy it from L1D.
printf(", L2 miss\n");
L2_miss++;
L2_eviction += copyInstructionBetweenCaches(L1D, L2, L1D_location, setNumL1, setNumL2, tagL2, L2E, L2B);
}
}else{
////////////////////////////////// MISS IN L1D
printf("L1D miss");
L1D_miss++;
//After that, we need to check if the data is stored in L2 cache.
if(L2_location != -1) {
////////////////////////////////// MISS IN L1D BUT HIT IN L2
//The data is in L2 cache.
printf(", L2 hit\n");
L2_hit++;
//Now, we need to load this data into L1D cache.
L1D_eviction += copyInstructionBetweenCaches(L2, L1D, L2_location, setNumL2, setNumL1, tagL1, L1E, L1B);
}else{
////////////////////////////////// MISS IN BOTH L1D AND L2
printf(", L2 miss\n");
L2_miss++;
//The instruction is not in L1D neither in L2. So we need to load it from RAM.
L2_location = copyInstructionFromRamToL2Cache(int_address, L2, setNumL2, tagL2);
L1D_eviction = copyInstructionBetweenCaches(L2, L1D, L2_location, setNumL2, setNumL1, tagL1, L1E, L1B);
}
//Now, we caught all misses and evictions and datas are in both L1D and L2 caches.
//Make the changes. For this, we need the line number.
L1D_location = returnLineNumberIfStored(L1D, L1E, setNumL1, tagL1);
L1D[setNumL1].lines[L1D_location].time = time(NULL);
memcpy(L1D[setNumL1].lines[L1D_location].data, char_data, (size_t) size);
//Also, we need to copy this data from L1D back to L2 cache.
copyInstructionBetweenCaches(L1D, L2, L1D_location, setNumL1, setNumL2, tagL2, L2E, L2B);
}
//We have updated the data in caches now. All we need to do is copy the data from L2 cache back to RAM.
copyDataToRAM(int_address, size, char_data);
printf(" Store in L1D, L2, RAM\n");
}
void dataModify(set *L1I, set *L1D, set *L2, char *address, int size, char *data){
//Data modify is simply a data load followed by a data store.
loadInstructionOrData(false, "L1D", L1I, L1D, L2, address, size);
dataStore(L1D, L2, address, size, data);
}
int returnLineNumberIfStored(set *L, int lineNumber, int setNum, int tag){
int i;
//First, we need to check if the corresponding line is valid and is it contains the data.
for(i = 0; i < lineNumber; i++){
//We will look if the valid bit is 1 and also the tag matches.
if(L[setNum].lines[i].validBit == 1 && L[setNum].lines[i].tag == tag)
return i;
}
return -1;
}
int copyInstructionBetweenCaches(set *source, set *dest, int sourceLineNumber, int sourceSetNum, int destSetNum, int destTag, int destTotalLine, int destBlockSize){
//Return value. 1 if there is an eviction, 0 if there is no eviction.
int returnValue = 0;
bool freeSlot = false;
int lineToPlace = 0;
for(i = 0; i < destTotalLine; i++){
if(dest[destSetNum].lines[i].validBit == 0){
freeSlot = true;
lineToPlace = i;
break;
}
}
//If freeSlot is false, it means all the lines are valid so we have an eviction.
if(freeSlot == false){
returnValue++;
//Now, we need to determine which line to use. For this we need to look at time_t values.
time_t current = time(NULL);
for(i = 0; i < destTotalLine; i++){
if(current > dest[destSetNum].lines[i].time){
current = dest[destSetNum].lines[i].time;
lineToPlace = i;
}
}
}
//Now, we have determined which line to store our instruction.
dest[destSetNum].lines[lineToPlace].validBit = 1;
dest[destSetNum].lines[lineToPlace].tag = destTag;
dest[destSetNum].lines[lineToPlace].time = time(NULL);
memcpy(dest[destSetNum].lines[lineToPlace].data, source[sourceSetNum].lines[sourceLineNumber].data, (size_t) destBlockSize);
return returnValue;
}
int copyInstructionFromRamToL2Cache(int int_address, set *L2, int setNumL2, int tagL2){
FILE *RAM = fopen("RAM.dat", "r");
//We need to make sure to pull the right chunk of data.
int address = int_address & (0xFFFFFFFF ^ (L2B - 1)); // NOLINT
fseek(RAM, address, SEEK_SET);
char readData[L2B];
fread(readData, (size_t) L2B, 1, RAM);
//We have gathered the data from the RAM.
fclose(RAM);
//Now, we need to store this data into the L2 cache.
bool freeSlot = false;
int lineToPlace = 0;
for(i = 0; i < L2E; i++){
if(L2[setNumL2].lines[i].validBit == 0){
freeSlot = true;
lineToPlace = i;
break;
}
}
//If freeSlot is false, it means all the lines are valid so we have an eviction.
if(freeSlot == false){
L2_eviction++;
//Now, we need to determine which line to use. For this we need to look at time_t values.
time_t current = time(NULL);
for(i = 0; i < L2E; i++){
if(current > L2[setNumL2].lines[i].time){
current = L2[setNumL2].lines[i].time;
lineToPlace = i;
}
}
}
//Now, we have determined which line to store our instruction.
L2[setNumL2].lines[lineToPlace].validBit = 1;
L2[setNumL2].lines[lineToPlace].tag = tagL2;
L2[setNumL2].lines[lineToPlace].time = time(NULL);
memcpy(L2[setNumL2].lines[lineToPlace].data, readData, (size_t) L2B);
return lineToPlace;
}
void copyDataToRAM(int int_address, int size, char *data){
FILE *RAM = fopen("RAM.dat", "r+");
fseek(RAM, int_address, SEEK_SET);
//Write the data here.
fwrite(data, 1, (size_t) size, RAM);
//We have wrote the data to the RAM.
fclose(RAM);
}
void printSummary(char *cacheName, int hit, int miss, int eviction){
printf("\n%s-hits: %d %s-misses: %d %s-evictions: %d", cacheName, hit, cacheName, miss, cacheName, eviction);
}
void cleanUp(set *L1I, set *L1D, set *L2){
//Freeing L1 cache.
for(i = 0; i < L1S; i++){
//Firstly, we need to free data pointers.
for(j = 0; j < L1E; j++){
free(L1I[i].lines[j].data);
free(L1D[i].lines[j].data);
}
//Now, we can free lines.
free(L1I[i].lines);
free(L1D[i].lines);
}
//Same things for L2 cache.
for(i = 0; i < L2S; i++){
//Firstly, we need to free data pointers.
for(j = 0; j < L2E; j++)
free(L2[i].lines[j].data);
//Now, we can free lines.
free(L2[i].lines);
}
}