-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
132 lines (111 loc) · 3.89 KB
/
main.cpp
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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "./Cache.h"
#include "./Memory.h"
#include "./UI.h"
using namespace std;
enum {
CACHE_READ, CACHE_WRITE
};
int main(int argc, char *argv[ ]) {
unsigned int cmd = 0;
unsigned int address = 0;
unsigned int data = 0;
int capacity = 0;
int blocksize = 0;
int associativity = 0;
char replacement[10] = "Default";
bool verbose = false;
ifstream fin;
ofstream fwriteMiss;
ofstream fwriteOccupancy;
char filename[100];
UI ui;
if (!ui.parseParams(argc, argv, capacity, blocksize, associativity, replacement, verbose, filename)) {
exit(2);
}
fin.open(filename);
if (!fin) {
fprintf(stderr, "%s cannot find file %s\n", argv[0], filename);
exit(2);
}
Cache myCache(capacity, blocksize, associativity, replacement);
if (!myCache.checkConfiguration()) {
exit(2);
}
double readMissRate;
double writeMissRate;
double totalMissRate;
double totalTime;
double occupancy;
double readTime = 0;
double writeTime = 0;
cout << "Cache Capacity: " << myCache.capacity << endl;
cout << "Cache BlockSize: " << myCache.blocksize << endl;
cout << "Cache Associativity: " << myCache.associativity << endl;
cout << "Number of Sets: " << myCache.numberOfSets << endl;
cout << "Replacement Policy: " << myCache.replacement << endl;
// Each iteration of this loop reads in and operates upon one transaction to memory.
fwriteMiss.open("MissRate.dat");
fwriteOccupancy.open("Occupancy.dat");
while (ui.readTraceEntry(cmd, address, data, fin)) {
if (cmd == CACHE_WRITE) {
// Process a cache write.
writeTime += myCache.write(address, data);
} else {
// Process a cache read.
readTime += myCache.read(address);
}
totalMissRate = (double) (myCache.readMiss + myCache.writeMiss) / (myCache.reads + myCache.writes);
occupancy = (double) (myCache.validCount) / (myCache.associativity*myCache.numberOfSets);
readMissRate = (double) (myCache.readMiss) / (myCache.reads);
writeMissRate = (double) (myCache.writeMiss) / (myCache.writes);
totalTime = readTime + writeTime;
fwriteMiss << setw(8) << totalTime << "\t"
<< setw(8) << totalMissRate << "\t"
<< setw(8) << readMissRate << "\t"
<< setw(8) << writeMissRate << "\t"
<< endl;
fwriteOccupancy << setw(8) << totalTime << "\t"
<< setw(8) << occupancy
<< endl;
}
fin.close();
fwriteMiss.close();
fwriteOccupancy.close();
if (verbose) {
cout << "\nCACHE CONTENTS" << endl;
myCache.printCache();
cout << "\nMAIN MEMORY" << endl;
myCache.myMemory.printMemory();
}
cout << "\nSTATISTICS" << endl;
cout << "Total Misses: "
<< setw(8) << myCache.readMiss + myCache.writeMiss
<< "\tRead Misses: "
<< setw(8) << myCache.readMiss
<< "\tWrite Misses: "
<< setw(8) << myCache.writeMiss
<< endl;
cout << "Total Miss Rate: "
<< setw(8) << totalMissRate
<< "\tRead Miss Rate: "
<< setw(8) << readMissRate
<< "\tWrite Miss Rate: "
<< setw(8) << writeMissRate
<< endl;
cout << "Read Time: "
<< setw(8) << readTime
<< "\t\tWrite Time: "
<< setw(8) << writeTime
<< "\t\tTotal Time: "
<< setw(8) << totalTime
<< endl;
cout << "Occupancy: "
<< setw(8) << occupancy
<< endl;
cout << "Number Of Dirty Blocks Evicted From The Cache: " << myCache.numDirtyBlocksEvicted << endl;
}