-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.java
More file actions
1040 lines (884 loc) · 38.2 KB
/
driver.java
File metadata and controls
1040 lines (884 loc) · 38.2 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
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.io.BufferedReader;
import java.io.File;
import java.util.*;
import java.text.DecimalFormat;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
public class driver {
public static void main (String[] args) {
// check for arguments
if(args.length == 0){
System.out.println("Not enough arguments, try again");
System.exit(0);
}
// cache global variables
int cacheSize = 0;
int blockSize = 0;
int assoc = 0;
int physMem = 0;
int memUsed = 0;
int timeSlice = 0;
String policy = new String();
ArrayList<File> fileList = new ArrayList<>();
ArrayList<Tracefile> traceFileList = new ArrayList<>();
ArrayList<int[]> pageTableList = new ArrayList<>();
// stores arguments into respective variables
for (int i = 0; i < args.length; i++) {
switch(args[i]){
case "-s":
cacheSize = Integer.parseInt(args[++i]);
break;
case "-b":
blockSize = Integer.parseInt(args[++i]);
break;
case "-a":
assoc = Integer.parseInt(args[++i]);
break;
case "-r":
policy = (args[++i].equalsIgnoreCase("RR")) ? "Round Robin" : "Random";
break;
case "-p":
physMem = Integer.parseInt(args[++i]);
break;
case "-u":
memUsed = Integer.parseInt(args[++i]);
break;
case "-n":
timeSlice = Integer.parseInt(args[++i]);
break;
case "-f":
File f = new File(args[++i]);
fileList.add(f);
traceFileList.add(new Tracefile(f));
pageTableList.add(new int[524288]);
//traceFileList.add(new Tracefile(f.getName()));
break;
default:
continue;
}
}
//////////////////////////////////
// BEGIN MILESTONE #1 //
//////////////////////////////////
//print all to information to the screen and write to and output file
System.out.println("Cache Simulator - CS 3853 - Group #05\n");
printTraceFiles(fileList);
printInputParams(cacheSize, blockSize, assoc, physMem, memUsed, timeSlice, policy);
printCacheCalcs(cacheSize, blockSize, assoc, physMem, memUsed, timeSlice, policy);
printPhysicalMemoryCalcs(physMem, memUsed, fileList.size());
/*try (PrintStream out = new PrintStream(new FileOutputStream("Team_05_Sim_n_M#1.txt"))){
System.setOut(out);
System.out.println("Cache Simulator - CS 3853 - Group #05\n");
printTraceFiles(fileList);
printInputParams(cacheSize, blockSize, assoc, physMem, memUsed, timeSlice, policy);
printCacheCalcs(cacheSize, blockSize, assoc, physMem, memUsed, timeSlice, policy);
printPhysicalMemoryCalcs(physMem, memUsed, fileList.size());
} catch (IOException e) {
e.printStackTrace();
}*/
//////////////////////////////////
// END MILESTONE #1 //
//////////////////////////////////
//////////////////////////////////
// BEGIN MILESTONE #2 //
//////////////////////////////////
////
//
// GLOBAL VARIABLES needed for Milestone#2
//
////
int addressSpace = 32;
int blockOffset = CalculateLogBase2(blockSize); //the number of bits for the block offset
cacheSize *= 1024; //this needs to be fixed later for our print to file output!!
int numOfIndices = CalculateNumOfIndices(cacheSize, blockSize, assoc);
int numOfSets = CalculateNumOfSets(cacheSize,blockSize,assoc);
int tagBits = CalculateTagBits(addressSpace,blockOffset,CalculateLogBase2(numOfIndices)); //number of tag bits
int indexBits = CalculateLogBase2(numOfIndices); //number of index bits
int sumCacheHits = 0;
int sumCompulsoryMisses = 0;
int sumConflictMisses = 0;
int timeSliceLinesToRead = timeSlice * 3;
////
//
// GLOBAL VARIABLES needed for Milestone #3
//
////
int pageSize = 4096;
long numOfPhysPages = (physMem * 1024 * 1024) / pageSize;
long numOfPagesForSystem = (long) (numOfPhysPages * (memUsed / 100.0));
int pagesAvailableToUser = (int)numOfPhysPages - (int)numOfPagesForSystem;
int totalMappedVirtualPages = 0;
int totalPageTableHits = 0;
int totalPagesFromFree = 0;
int totalPageFaults = 0;
initializePTList(pageTableList);
//Linked List of all available pages that aren't being used by system
LinkedList<Integer> freeUserPagesList = new LinkedList<>();
for (int p = 0; p < pagesAvailableToUser; p++) {
freeUserPagesList.add(p);
}
//Linked List of all used pages from list of User Pages
LinkedList<Integer> usedUserPagesList = new LinkedList<>();
//setup the cache
//each block set to -1 to indicate valid is not set/no tag written
int[][] arrCache = new int[CalculateNumOfSets(cacheSize,blockSize,assoc)][assoc];
for (int row = 0; row < arrCache.length; row++){
for (int col = 0; col < arrCache[row].length; col++){
arrCache[row][col] = -1;
}
}
//keeps track of rows in the cache
//this is used exclusively for the Round Robin replacement algorithm
int[] replacementIndex = new int[numOfSets];
//process each trace file, save info to variables and cache
int totalAddressesRead = 0; //+1 EIP address & +1src & +1dst (if src/dst read occurred)
int sumInstructionBytes = 0; //sum of all the numbers in (_) after EIP
int sumDstSrcBytes = 0; //if src/dst read occurred, add 4 for either one where read occurred
int totalCacheAccesses = 0; //total # of cache accesses
int totalCacheMisses = 0; //total # of cache misses
int totalCycles = 0; // total # cycles
int numReads = (int) Math.ceil((double) blockSize / 4); //number of memory reads to populate cache block
int totalInstructions = 0;
/* NEW FOR LOOP CODE TO READ FILES MORE EFFICIENTLY*/
List<BufferedReader> readers = new ArrayList<>();
int doneCount = 0;
try {
//creates list of buffered readers to keep each trace file open
for (File filePath : fileList) {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
readers.add(reader);
}
String line = null;
char[] charArray = new char[100];
//reads from all trace files until all trace files are done reading
//index i is being used by both traceFileList, readers list, and pageTableList
for (int i = 0; doneCount < traceFileList.size(); i++){
//loop back to beginning of trace files array
if(i == traceFileList.size()) {
i = 0;
}
Tracefile currentTraceFile = traceFileList.get(i);
if(currentTraceFile.isDoneReading) {
continue;
}
int numOfLinesRead = 0;
//reads lines in the file
while (!currentTraceFile.isDoneReading)
{
line = readers.get(i).readLine();
numOfLinesRead++;
//Break out if we hit the final line to read
if (numOfLinesRead == timeSliceLinesToRead && traceFileList.size() != 1){
break;
}
if (line == null){
currentTraceFile.isDoneReading = true;
//once file is done reading, we must "free" all assigned User Page
int[] temp = pageTableList.get(i);
for (int t : temp ){
if (t != -1){
freeUserPagesList.add(t);
}
}
doneCount++;
break;
}
//convert line to a char array
charArray = line.toCharArray();
if(charArray.length != 0)
{
switch(charArray[0])
{
//process the INSTRUCTION ADDRESS, hex address is [10-17]
case 'E':
String eipNum = new StringBuilder().append(charArray[5]).append(charArray[6]).toString();
if (Integer.parseInt(eipNum) == 0) continue; //check for eip not reading any bytes
totalAddressesRead++;
sumInstructionBytes += Integer.parseInt(eipNum);
StringBuilder sbEipAddress = new StringBuilder();
for (int j = 10; j <= 17; j++)
{
sbEipAddress.append(charArray[j]);
}
String eipAddress = sbEipAddress.toString(); //a hex string
/// MILESTONE 3 STUFF HERE
/// break address into two parts
/// PAGE NUMBER = top 20 bits
/// PAGE OFFSET = bottom 12 bits
int eipPageNumber = getPageNumBits(eipAddress);
int eipPageOffset = getPageOffsetBits(eipAddress);
if (eipPageNumber >= 1048575){
continue;
}
/// check the current trace files Page Table: pageTabeList[pageNumber] == ?
int[] eipPageTable = pageTableList.get(i);
if (eipPageTable[eipPageNumber] != -1){ //page table hit
totalPageTableHits++;
} else if (eipPageTable[eipPageNumber] == -1){ //page table miss
if(freeUserPagesList.isEmpty()){
totalPageFaults++;
// pick from another random file first, will choose self if only trace file
int checker = 0;
for (int j = i; j <= traceFileList.size(); j++){
if (j == traceFileList.size()){
j = 0;
}
Tracefile tempTrace = traceFileList.get(j);
if (checker == traceFileList.size()){
eipPageTable[eipPageNumber] = FindFirstFreePage(pageTableList.get(i));
break;
}
if (j == i){
checker++;
continue;
}
if (tempTrace.isDoneReading){
checker++;
continue;
}
//pull from trace file j
eipPageTable[eipPageNumber] = FindFirstFreePage(pageTableList.get(j));
break;
}
} else{
eipPageTable[eipPageNumber] = freeUserPagesList.getFirst();
freeUserPagesList.removeFirst();
totalPagesFromFree++;
}
}
/// create physical address from User Page and Page Offset
/// ex] 0x12345678
/// 0x12345 is the USER PAGE pulled from freeUserPagesList
/// 0x678 is the PAGE OFFSET(bottom 12 bits)
long longEIPAddress = (eipPageTable[eipPageNumber] * 4096) + eipPageOffset;
eipAddress = String.format("%08X", longEIPAddress);
/// run this new Physical Address through Milestone 2 caching algorithm
/// MILESTONE 3 STUFF ENDS HERE!!
//perform eip cache check and update cache if necessary
int eipTag = parseTagBitsToInt(toBinaryString(eipAddress), tagBits);
int eipIndex = parseIndexBitsToInt(toBinaryString(eipAddress), tagBits, indexBits);
int eipBlock = parseBlockBitsToInt(toBinaryString(eipAddress), tagBits, indexBits, blockOffset);
int eipAddBlock = addBlockRows(eipBlock, Integer.parseInt(eipNum), blockSize);
for (int row = eipIndex; row < (eipIndex + eipAddBlock + 1); row++){
if (row >= arrCache.length) break; //OOB check
int conflictCheckCount = 0;
for (int col = 0; col < assoc; col++){
if (col >= arrCache[row].length) break; //OOB check
//arrCache[row][col] = eipTag;
//every col is checked before deciding if hit/miss
//checks for hit/matching tag
if (arrCache[row][col] == eipTag){
sumCacheHits++;
totalCycles += 1; // cache hit; add 1 to total cycles
break;
}
//checks for compulsory miss
else if (arrCache[row][col] < 0){
arrCache[row][col] = eipTag;
sumCompulsoryMisses++;
totalCycles += (4 * numReads); // cache miss; 4 * num reads to populate block
break;
}
//checks for conflict miss
else if (arrCache[row][col] != eipTag){
conflictCheckCount++;
if (conflictCheckCount == assoc){
sumConflictMisses++;
totalCycles += (4 * numReads); // cache miss; 4 * num reads to populate block
//run replacement policy algorithm
if (policy.equals("Random")){
Random rand = new Random();
//random number between [0 - (associativity-1)]
int n = rand.nextInt(assoc);
arrCache[row][n] = eipTag;
} else {
//Round Robin
int replaceCol = replacementIndex[row];
arrCache[row][replaceCol] = eipTag;
replacementIndex[row] = (replaceCol + 1) % assoc; //update replacement index
}
break;
}
continue;
} else {
System.out.println("Error in EIP cache check");
System.exit(0);
}
}
totalCycles += 2; // +2 cycles to execute instruction
totalInstructions += 1; // adds to total number instructions for cache
}
break;
//process both dst[6-13][15-22] and src[33-40][42-49] addresses
//always 4 bytes read if non-zero address for both dst and src
case 'd':
//dstM:
String dstAddress = null;
StringBuilder sbDst = new StringBuilder();
for (int j = 6; j <= 13; j++)
{
sbDst.append(charArray[j]);
}
dstAddress = sbDst.toString();
if (!dstAddress.equals("00000000") || charArray[15] != '-') //check if dst is actually not reading bytes
{
/// MILESTONE 3 STUFF HERE
/// break address into two parts
/// PAGE NUMBER = top 20 bits
/// PAGE OFFSET = bottom 12 bits
int dstPageNumber = getPageNumBits(dstAddress);
int dstPageOffset = getPageOffsetBits(dstAddress);
if (dstPageNumber >= 1048575){
continue;
}
/// check the current trace files Page Table: pageTabeList[pageNumber] == ?
int[] dstpageTable = pageTableList.get(i);
if (dstpageTable[dstPageNumber] != -1){ //page table hit
totalPageTableHits++;
} else if (dstpageTable[dstPageNumber] == -1){ //page table miss
if(freeUserPagesList.isEmpty()){
totalPageFaults++;
// pick from another random file first, will choose self if only trace file
int checker = 0;
for (int j = i; j <= traceFileList.size(); j++){
if (j == traceFileList.size()){
j = 0;
}
Tracefile tempTrace = traceFileList.get(j);
if (checker == traceFileList.size()){
dstpageTable[dstPageNumber] = FindFirstFreePage(pageTableList.get(i));
break;
}
if (j == i){
checker++;
continue;
}
if (tempTrace.isDoneReading){
checker++;
continue;
}
//pull from trace file j
dstpageTable[dstPageNumber] = FindFirstFreePage(pageTableList.get(j));
break;
}
} else{
dstpageTable[dstPageNumber] = freeUserPagesList.getFirst();
freeUserPagesList.removeFirst();
totalPagesFromFree++;
}
}
/// create physical address from User Page and Page Offset
/// ex] 0x12345678
/// 0x12345 is the USER PAGE pulled from freeUserPagesList
/// 0x678 is the PAGE OFFSET(bottom 12 bits)
long longDSTAddress = (dstpageTable[dstPageNumber] * 4096) + dstPageOffset;
dstAddress = String.format("%08X", longDSTAddress);
/// run this new Physical Address through Milestone 2 caching algorithm
/// MILESTONE 3 STUFF ENDS HERE!!
sumDstSrcBytes += 4;
totalAddressesRead++;
//dst cache check
int dstTag = parseTagBitsToInt(toBinaryString(dstAddress), tagBits);
int dstIndex = parseIndexBitsToInt(toBinaryString(dstAddress), tagBits, indexBits);
int dstBlock = parseBlockBitsToInt(toBinaryString(dstAddress), tagBits, indexBits, blockOffset);
int dstAddBlock = addBlockRows(dstBlock, 4, blockSize);
for (int row = dstIndex; row < (dstIndex + dstAddBlock + 1); row++){
if (row >= arrCache.length) break; //OOB check
int conflictCheckCount = 0;
for (int col = 0; col < assoc; col++){
if (col >= arrCache[row].length) break; //OOB check
//arrCache[row][col] = dstTag;
//every col is checked before deciding if hit/miss
//checks for hit/matching tag
if (arrCache[row][col] == dstTag){
sumCacheHits++;
totalCycles += 1; // cache hit; +1 cycle
break;
}
//checks for compulsory miss
else if (arrCache[row][col] < 0){
arrCache[row][col] = dstTag;
sumCompulsoryMisses++;
totalCycles += (4 * numReads); // cache miss; 4 * num reads to populate block
break;
}
//checks for conflict miss
else if (arrCache[row][col] != dstTag){
conflictCheckCount++;
if (conflictCheckCount == assoc){
sumConflictMisses++;
totalCycles += (4 * numReads); // cache miss; 4 * num reads to populate block
//run replacement policy algorithm
if (policy.equals("Random")){
Random rand = new Random();
//random number between [0 - (associativity-1)]
int n = rand.nextInt(assoc);
arrCache[row][n] = dstTag;
} else {
//Round Robin
int replaceCol = replacementIndex[row];
arrCache[row][replaceCol] = dstTag;
replacementIndex[row] = (replaceCol + 1) % assoc; //update replacement index
}
break;
}
continue;
} else {
System.out.println("Error in dstM cache check");
System.exit(0);
}
}
totalCycles += 1; //calculate effective address; +1 cycle
}
}
//srcM:
String srcAddress = null;
StringBuilder sbSrc = new StringBuilder();
for (int j = 33; j <= 40; j++)
{
sbSrc.append(charArray[j]);
}
srcAddress = sbSrc.toString();
if (!srcAddress.equals("00000000") || charArray[42] != '-') //check if src is actually not reading bytes
{
/// MILESTONE 3 STUFF HERE
/// break address into two parts
/// PAGE NUMBER = top 20 bits
/// PAGE OFFSET = bottom 12 bits
int srcPageNumber = getPageNumBits(srcAddress);
int srcPageOffset = getPageOffsetBits(srcAddress);
if (srcPageNumber >= 1048575){
continue;
}
/// check the current trace files Page Table: pageTabeList[pageNumber] == ?
int[] srcPageTable = pageTableList.get(i);
if (srcPageTable[srcPageNumber] != -1){ //page table hit
totalPageTableHits++;
} else if (srcPageTable[srcPageNumber] == -1){ //page table miss
if(freeUserPagesList.isEmpty()){
totalPageFaults++;
// pick from another random file first, will choose self if only trace file
int checker = 0;
for (int j = i; j <= traceFileList.size(); j++){
if (j == traceFileList.size()){
j = 0;
}
Tracefile tempTrace = traceFileList.get(j);
if (checker == traceFileList.size()){
srcPageTable[srcPageNumber] = FindFirstFreePage(pageTableList.get(i));
break;
}
if (j == i){
checker++;
continue;
}
if (tempTrace.isDoneReading){
checker++;
continue;
}
//pull from trace file j
srcPageTable[srcPageNumber] = FindFirstFreePage(pageTableList.get(j));
break;
}
} else{
srcPageTable[srcPageNumber] = freeUserPagesList.getFirst();
freeUserPagesList.removeFirst();
totalPagesFromFree++;
}
}
/// create physical address from User Page and Page Offset
/// ex] 0x12345678
/// 0x12345 is the USER PAGE pulled from freeUserPagesList
/// 0x678 is the PAGE OFFSET(bottom 12 bits)
long longSRCAddress = (srcPageTable[srcPageNumber] * 4096) + srcPageOffset;
srcAddress = String.format("%08X", longSRCAddress);
/// run this new Physical Address through Milestone 2 caching algorithm
/// MILESTONE 3 STUFF ENDS HERE!!
sumDstSrcBytes += 4;
totalAddressesRead++;
//src cache check
int srcTag = parseTagBitsToInt(toBinaryString(srcAddress), tagBits);
int srcIndex = parseIndexBitsToInt(toBinaryString(srcAddress), tagBits, indexBits);
int srcBlock = parseBlockBitsToInt(toBinaryString(srcAddress), tagBits, indexBits, blockOffset);
int srcAddBlock = addBlockRows(srcBlock, 4, blockSize);
for (int row = srcIndex; row < (srcIndex + srcAddBlock + 1); row++){
if (row >= arrCache.length) break; //OOB check
int conflictCheckCount = 0;
for (int col = 0; col < assoc; col++){
if (col >= arrCache[row].length) break; //OOB check
//arrCache[row][col] = srcTag;
//every col is checked before deciding if hit/miss
//checks for hit/matching tag
if (arrCache[row][col] == srcTag){
sumCacheHits++;
totalCycles += 1; // cache hit; +1 cycle
break;
}
//checks for compulsory miss
else if (arrCache[row][col] < 0){
arrCache[row][col] = srcTag;
sumCompulsoryMisses++;
totalCycles += (4 * numReads); // cache miss; 4 * num reads to populate block
break;
}
//checks for conflict miss
else if (arrCache[row][col] != srcTag){
conflictCheckCount++;
if (conflictCheckCount == assoc){
sumConflictMisses++;
totalCycles += (4 * numReads); // cache miss; 4 * num reads to populate block
//run replacement policy algorithm
if (policy.equals("Random")){
Random rand = new Random();
//random number between [0 - (associativity-1)]
int n = rand.nextInt(assoc);
arrCache[row][n] = srcTag;
} else {
//Round Robin
int replaceCol = replacementIndex[row];
arrCache[row][replaceCol] = srcTag;
replacementIndex[row] = (replaceCol + 1) % assoc; //update replacement index
}
break;
}
continue;
} else {
System.out.println("Error in dstM cache check");
System.exit(0);
}
}
totalCycles += 1; //calculate effective address; +1 cycle
}
}
default:
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
totalCacheAccesses = sumCacheHits + sumCompulsoryMisses + sumConflictMisses;
totalCacheMisses = sumCompulsoryMisses + sumConflictMisses;
//print cache simulation results
System.out.println("\n*****CACHE SIMULATION RESULTS*****\n");
System.out.println("Total Cache Accesses: \t\t" + totalCacheAccesses + "\t(" + totalAddressesRead + " addresses)");
System.out.print("Instruction Bytes: \t\t" + sumInstructionBytes);
System.out.println("\tSrcDst Bytes: " + sumDstSrcBytes);
System.out.println("Cache Hits: \t\t\t" + sumCacheHits);
System.out.println("Cache Misses: \t\t\t" + totalCacheMisses);
System.out.println("--- Compulsory Misses: \t\t" + sumCompulsoryMisses);
System.out.println("--- Conflict Misses: \t\t" + sumConflictMisses);
int totalBlocks = cacheSize / blockSize;
double impSizeKB = (totalBlocks*blockSize + CalculateCacheTax(assoc,tagBits,numOfIndices)) / Math.pow(2,10);
int overheadSize = CalculateCacheTax(assoc,tagBits,numOfIndices);
double hitRate = (double)(sumCacheHits * 100) / totalCacheAccesses;
double missRate = (double)100 - hitRate;
double CPI = (double) totalCycles / totalInstructions;
double unusedKB = ((double) (((totalBlocks - sumCompulsoryMisses) * blockSize) + overheadSize) / 1024);
//System.out.println("******** " + (cacheSize) + " *********");
double waste = 0.15 * unusedKB;
double percentUnused = (unusedKB / impSizeKB) * 100;
System.out.println("\n***** ***** CACHE HIT & MISS RATE: ***** *****\n");
System.out.println("Hit Rate: " + String.format("%.2f",hitRate) + "%");
System.out.println("Miss Rate: " + String.format("%.2f",missRate) + "%");
System.out.println("CPI: " + String.format("%.2f",CPI) + " Cycles/Instruction (" + totalInstructions + ")");
System.out.printf("Unused Cache Space: %.2f KB / %.2f KB = %.2f%% Waste: $%.2f\n", unusedKB, impSizeKB, percentUnused, waste);
System.out.println("Unused Cache Blocks: " + (totalBlocks - sumCompulsoryMisses) + " / " + totalBlocks);
System.out.println("\n***** ***** PHYSICAL MEMORY SIMULATION RESULTS: ***** *****\n");
System.out.println("Physical Pages Used By SYSTEM: " + numOfPagesForSystem);
System.out.println("Pages Available to User: " + pagesAvailableToUser);
System.out.println();
totalMappedVirtualPages = totalPageTableHits + totalPagesFromFree + totalPageFaults;
System.out.println("Virtual Pages Mapped: " + totalMappedVirtualPages);
System.out.println(" ----------------------");
System.out.println(" Page Table Hits: " + totalPageTableHits);
System.out.println();
System.out.println(" Pages from Free: " + totalPagesFromFree);
System.out.println();
System.out.println(" Total Page Faults " + totalPageFaults);
System.out.println();
System.out.println();
long PTOverhead = (524288L * 4 * fileList.size()) / 8;
printPageTableUsage(fileList, pageTableList, PTOverhead);
//write Milestone results to a text file
try (PrintStream out = new PrintStream(new FileOutputStream("Team_05_Sim_n_M#3.txt"))){
cacheSize /= 1024;
System.setOut(out);
System.out.println("Cache Simulator - CS 3853 - Group #05\n");
printTraceFiles(fileList);
printInputParams(cacheSize, blockSize, assoc, physMem, memUsed, timeSlice, policy);
printCacheCalcs(cacheSize, blockSize, assoc, physMem, memUsed, timeSlice, policy);
printPhysicalMemoryCalcs(physMem, memUsed, fileList.size());
System.out.println("\n*****CACHE SIMULATION RESULTS*****\n");
System.out.println("Total Cache Accesses: \t\t" + totalCacheAccesses + "\t(" + totalAddressesRead + " addresses)");
System.out.print("Instruction Bytes: \t\t" + sumInstructionBytes);
System.out.println("\tSrcDst Bytes: " + sumDstSrcBytes);
System.out.println("Cache Hits: \t\t\t" + sumCacheHits);
System.out.println("Cache Misses: \t\t\t" + totalCacheMisses);
System.out.println("--- Compulsory Misses: \t\t" + sumCompulsoryMisses);
System.out.println("--- Conflict Misses: \t\t" + sumConflictMisses);
System.out.println("\n***** ***** CACHE HIT & MISS RATE: ***** *****\n");
System.out.println("Hit Rate: " + String.format("%.2f",hitRate) + "%");
System.out.println("Miss Rate: " + String.format("%.2f",missRate) + "%");
System.out.println("CPI: " + String.format("%.2f",CPI) + " Cycles/Instruction (" + totalInstructions + ")");
System.out.printf("Unused Cache Space: %.2f KB / %.2f KB = %.2f%% Waste: $%.2f\n", unusedKB, impSizeKB, percentUnused, waste);
System.out.println("Unused Cache Blocks: " + (totalBlocks - sumCompulsoryMisses) + " / " + totalBlocks);
System.out.println("\n***** ***** PHYSICAL MEMORY SIMULATION RESULTS: ***** *****\n");
System.out.println("Physical Pages Used By SYSTEM: " + numOfPagesForSystem);
System.out.println("Pages Available to User: " + pagesAvailableToUser);
System.out.println();
System.out.println("Virtual Pages Mapped: " + totalMappedVirtualPages);
System.out.println(" ----------------------");
System.out.println(" Page Table Hits: " + totalPageTableHits);
System.out.println();
System.out.println(" Pages from Free: " + totalPagesFromFree);
System.out.println();
System.out.println(" Total Page Faults " + totalPageFaults);
System.out.println();
System.out.println();
printPageTableUsage(fileList, pageTableList, PTOverhead);
} catch (IOException e) {
e.printStackTrace();
}
}
// prints the input parameters
public static void printInputParams(int cacheSize, int blockSize, int assoc, int physMem, int memUsed, int timeSlice, String policy){
DecimalFormat df = new DecimalFormat("0.0");
System.out.println("***** Cache Input Parameters *****\n");
System.out.println("Cache size: " + cacheSize + " KB");
System.out.println("Block size: " + blockSize + " bytes");
System.out.println("Associativity: " + assoc);
System.out.println("Replacement Policy: " + policy);
System.out.println("Physical Memory: " + physMem);
System.out.println("Percent Memory Used by System: " + df.format(memUsed) + "%");
System.out.println("Instructions / Time Slice: " + timeSlice);
}
// prints trace files
public static void printTraceFiles(ArrayList<File> fileList) {
System.out.println("Trace File(s):");
for (File f : fileList){
System.out.println("\t" + f.getName());
}
System.out.println();
}
// prints the cache calculations
public static void printCacheCalcs(int cacheSize, int blockSize, int assoc, int physMem, int memUsed, int timeSlice, String policy){
int addressSpace = 32;
int blockOffset = CalculateLogBase2(blockSize);
DecimalFormat df = new DecimalFormat("0.00");
cacheSize *= 1024; //converting to KB
System.out.println("\n***** Cache Calculated Values *****");
System.out.println("");
int numOfBlocks = (cacheSize / blockSize);
System.out.println("Total # Blocks:\t\t\t" + numOfBlocks);
int numOfIndices = CalculateNumOfIndices(cacheSize, blockSize, assoc);
int tagBits = CalculateTagBits(addressSpace,blockOffset,CalculateLogBase2(numOfIndices));
System.out.println("Tag Size:\t\t\t" + tagBits + " bits");
System.out.println("Index Size:\t\t\t" + CalculateLogBase2(numOfIndices) + " bits");
int numOfSets = CalculateNumOfSets(cacheSize,blockSize,assoc);
System.out.println("Total # Rows:\t\t\t" + numOfSets);
int cacheTax = CalculateCacheTax(assoc,tagBits,numOfIndices);
System.out.println("Overhead Size:\t\t\t" + cacheTax + " bytes");
double impSizeKB = (numOfBlocks*blockSize + CalculateCacheTax(assoc,tagBits,numOfIndices)) / Math.pow(2,10);
int impSize = (numOfBlocks*blockSize + CalculateCacheTax(assoc,tagBits,numOfIndices));
System.out.println("Implementation Memory Size:\t" + df.format(impSizeKB) + " KB (" + impSize + " bytes)");
double cost = (((numOfBlocks*blockSize + CalculateCacheTax(assoc,tagBits,numOfIndices)) / Math.pow(2,10))*0.15);
System.out.println("Cost:\t\t\t\t$" + df.format(cost) + " @ $0.15 per KB");
}
// prints physical memory calculations
public static void printPhysicalMemoryCalcs(long physMem, int memUsed, int numTraceFiles) {
int pageSize = 4096;
long numOfPhysPages = (physMem * 1024 * 1024) / pageSize;
long numOfPagesForSystem = (long) (numOfPhysPages * (memUsed / 100.0));
long sizeOfPageTableEntry = 0;
try{
sizeOfPageTableEntry = CalculateLogBase2(numOfPhysPages) + 1; //bits needed for number of physical pages + 1 valid bit
} catch(Exception e)
{
System.out.println("Error in Size of Page Table Calculation");
System.exit(-1);
}
int numEntries = 512 * 1024; //all trace file addresses < 0x7FFFFFFF, so 512KB hard limit
long totalRAMForPageTables = (numEntries * sizeOfPageTableEntry * numTraceFiles) / 8;
System.out.println("\n***** Physical Memory Calculated Values *****");
System.out.println("");
System.out.println("Number of Physical Pages:\t" + numOfPhysPages);
System.out.println("Number of Pages for System:\t" + numOfPagesForSystem);
System.out.println("Size of Page Table Entry:\t" + sizeOfPageTableEntry + " bits");
System.out.println("Total RAM for Page Table(s):\t" + totalRAMForPageTables + " bytes");
}
// these methods needed for cache calculations
// may consider putting these is a private class to make things cleaner
public static int CalculateLogBase2(int arg){
int base = 2;
return (int) (Math.log(arg) / Math.log(base));
}
public static long CalculateLogBase2(long arg){
long base = 2;
return (long) (Math.log(arg) / Math.log(base));
}
public static int CalculateNumOfIndices(int cacheSize, int blockSize, int assoc){
return cacheSize / (blockSize * assoc);
}
public static int CalculateTagBits(int addressSpace, int byteOffset, int indexBits){
return addressSpace - byteOffset - indexBits;
}
public static int CalculateCacheTax(int assoc, int tagBits, int numOfIndices){
return assoc * (1 + tagBits) * numOfIndices / 8; //last 8 is for converting to Bytes
}
public static int CalculateNumOfSets(int cacheSize, int blockSize, int assoc){
return cacheSize / (blockSize * assoc);
}
//Tracefile Class
public static class Tracefile{
public File filePath = null;
public String name = null;
public int fileLineReadPos;
public boolean isDoneReading;
public Tracefile(File filePath){
this.filePath = filePath;
fileLineReadPos = 1;
isDoneReading = false;
}
/*public Tracefile(String name){
name = this.name;
filePath = new File(name);
currReadPos = 0;
isDoneReading = false;
}*/
public String toString() {
return "Filepath " + filePath.getName()
+ "Name " + name
+ "fileLineReadPos " + fileLineReadPos
+ "Done Status " + Boolean.toString(isDoneReading);
}
}
//convert 32-bit hex address string to binary string
public static String toBinaryString(String str){
long lHex = Long.parseLong(str, 16);
String bHex = String.format("%32s", Long.toBinaryString(lHex)).replace(' ', '0');
return bHex;
}
//parse tag bits from 32-bit binary address string, return as int
public static int parseTagBitsToInt(String str, int tagBits){
char[] charArray = new char[100];
charArray = str.toCharArray();
String strTag = null;
StringBuilder sbTag = new StringBuilder();
//tag [0 -> (tag bits-1)]
for (int j = 0; j <= (tagBits - 1); j++)
{
sbTag.append(charArray[j]);
}
strTag = sbTag.toString();
int iTag = Integer.parseInt(strTag, 2); //binary string to int
return iTag;
}
//parse index bits from 32-bit binary address string, return as int
public static int parseIndexBitsToInt(String str, int tagBits, int indexBits){
char[] charArray = new char[100];
charArray = str.toCharArray();
String strIndex = null;
StringBuilder sbIndex = new StringBuilder();
//index [tag bits -> (tag bits + index bits) - 1 ]
for (int j = tagBits; j <= (tagBits + indexBits - 1); j++)
{
sbIndex.append(charArray[j]);
}
strIndex = sbIndex.toString();
int iIndex = Integer.parseInt(strIndex, 2); //binary string to int
return iIndex;
}
//parse block offset bits from 32-bit binary address string, return as int
public static int parseBlockBitsToInt(String str, int tagBits, int indexBits, int blockOffset){
char[] charArray = new char[100];
charArray = str.toCharArray();
String strBlock = null;
StringBuilder sbBlock = new StringBuilder();
//block [(tag bits + index bits) -> (tag bits + index bits + block bits)-1
for (int j = (tagBits + indexBits); j <= (tagBits + indexBits + blockOffset - 1); j++)
{
sbBlock.append(charArray[j]);
}
strBlock = sbBlock.toString();
int iBlock = Integer.parseInt(strBlock, 2); //binary string to int
return iBlock;
}
//calculate how many additional blocks into the cache we need to access
public static int addBlockRows(int block, int bytesToRead, int blockSize){
return (int) Math.floor( (block + bytesToRead - 1) / blockSize );
}
public static int getPageNumBits(String str) {
String binaryAddress = toBinaryString(str);
binaryAddress = String.format("%32s", binaryAddress).replace(' ', '0');
StringBuilder sbBits = new StringBuilder();
// Loop through the first 20 bits of the binary string
for (int i = 0; i < 20; i++) {
sbBits.append(binaryAddress.charAt(i));
}
// Convert the binary string to an integer
String strBits = sbBits.toString();
return Integer.parseInt(strBits, 2);
}
public static int getPageOffsetBits(String str) {
// Convert the address to a binary string
String binaryAddress = toBinaryString(str);
binaryAddress = String.format("%32s", binaryAddress).replace(' ', '0');
StringBuilder sbBits = new StringBuilder();
// Loop through the bits from position 20 to 31
for (int i = 20; i < 32; i++) {
sbBits.append(binaryAddress.charAt(i));
}
// Convert the binary string to an integer
String strBits = sbBits.toString();
return Integer.parseInt(strBits, 2);
}
public static void initializePTList(ArrayList<int[]> pageTableList){
for (int[] currPageTable : pageTableList) {
Arrays.fill(currPageTable, -1);
}
}
public static int countUsedPTEntries(int[] pageTable){
int cnt = 0;
for (int entry : pageTable) {
if (entry != -1) cnt++;
}
return cnt;
}