-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
477 lines (412 loc) · 14.7 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
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
/**
* @author Amay Patel
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <cmath>
#include <list>
#include <queue>
using namespace std;
/**
* @brief This function will read in the file the user inputs and store the addresses into a vector
* @param directory the file the user inputs
* @param addresses the vector that will be populated with the addresses in the file
*/
void readFile(string& directory, vector<string>& addresses) {
ifstream file(directory);
string line;
while (getline(file, line)) {
istringstream iss(line);
string currAddress;
getline(iss, currAddress, ' ');
getline(iss, currAddress, ' ');
addresses.push_back(currAddress);
}
}
/**
* @brief This function will take in a hex address and convert it to a binary address
* @param hexAddress the hex address that will be converted to binary
* @return string binary address
*/
string convertToBin(const string& hexAddress) {
string binAddress;
for(int i = 2; i < hexAddress.length(); i++) {
if(hexAddress.at(i) == '0') {
binAddress += "0000";
}
else if(hexAddress.at(i) == '1') {
binAddress += "0001";
}
else if(hexAddress.at(i) == '2') {
binAddress += "0010";
}
else if(hexAddress.at(i) == '3') {
binAddress += "0011";
}
else if(hexAddress.at(i) == '4') {
binAddress += "0100";
}
else if(hexAddress.at(i) == '5') {
binAddress += "0101";
}
else if(hexAddress.at(i) == '6') {
binAddress += "0110";
}
else if(hexAddress.at(i) == '7') {
binAddress += "0111";
}
else if(hexAddress.at(i) == '8') {
binAddress += "1000";
}
else if(hexAddress.at(i) == '9') {
binAddress += "1001";
}
else if(hexAddress.at(i) == 'A' || hexAddress.at(i) == 'a') {
binAddress += "1010";
}
else if(hexAddress.at(i) == 'B' || hexAddress.at(i) == 'b') {
binAddress += "1011";
}
else if(hexAddress.at(i) == 'C' || hexAddress.at(i) == 'c') {
binAddress += "1100";
}
else if(hexAddress.at(i) == 'D' || hexAddress.at(i) == 'd') {
binAddress += "1101";
}
else if(hexAddress.at(i) == 'E' || hexAddress.at(i) == 'e') {
binAddress += "1110";
}
else if(hexAddress.at(i) == 'F' || hexAddress.at(i) == 'f') {
binAddress += "1111";
}
}
while(binAddress.length() < 32) {
binAddress = "0" + binAddress;
}
return binAddress;
}
/**
* @brief This function will calculate the hit rate for a fully associative cache
* @param addresses the vector of addresses that will be used to calculate the hit rate
* @param bytesPerBlock the amount of bytes per block in cache
* @param cacheSize the size of the cache
* @param replacementPolicy the replacement policy the user wants to use (FIFO or LRU)
*/
void fullyAssociative(vector<string>& addresses, int bytesPerBlock, int cacheSize, string& replacementPolicy) {
int hits = 0;
int misses = 0;
map<string, string> treeMap;
queue<string> tagQueue;
list<string> tagList;
for(int i = 0; i < addresses.size(); i++) {
int numLines = cacheSize / bytesPerBlock;
int offsetWidth = log2(bytesPerBlock);
int tagWidth = addresses.at(i).length() - offsetWidth;
string tag = addresses.at(i).substr(0, tagWidth);
string offset = addresses.at(i).substr(tagWidth, offsetWidth);
if(treeMap.find(tag) == treeMap.end()) {
misses++;
if(treeMap.size() < numLines) {
treeMap.insert(make_pair(tag, offset));
if(replacementPolicy == "FIFO") {
tagQueue.push(tag);
}
else if(replacementPolicy == "LRU") {
tagList.push_back(tag);
}
}
else if(treeMap.size() == numLines) {
if(replacementPolicy == "FIFO") {
treeMap.erase(tagQueue.front());
tagQueue.pop();
tagQueue.push(tag);
}
else if(replacementPolicy == "LRU") {
treeMap.erase(tagList.front());
tagList.pop_front();
tagList.push_back(tag);
}
treeMap.insert(make_pair(tag, offset));
}
}
else {
hits++;
if(replacementPolicy == "LRU") {
tagList.remove(tag);
tagList.push_back(tag);
}
}
}
cout << "Hit Rate: " << (double)hits/(double)(hits + misses) << endl;
}
/**
* @brief This function will calculate the hit rate for a direct mapped cache
* @param addresses the vector of addresses that will be used to calculate the hit rate
* @param bytesPerBlock the amount of bytes per block in cache
* @param cacheSize the size of the cache
*/
void directMap(vector<string>& addresses, int bytesPerBlock, int cacheSize) {
int hits = 0;
int misses = 0;
map<string, string> treeMap;
for(int i = 0; i < addresses.size(); i++) {
int numLines = cacheSize/bytesPerBlock;
int lineWidth = log2(numLines);
int offsetWidth = log2(bytesPerBlock);
int tagWidth = addresses.at(i).length() - lineWidth - offsetWidth;
string tag = addresses.at(i).substr(0, tagWidth);
string line = addresses.at(i).substr(tagWidth, lineWidth);
if(treeMap.find(line) == treeMap.end()) {
misses++;
treeMap.insert(make_pair(line, tag));
}
else {
if(treeMap.at(line) == tag) {
hits++;
}
else {
misses++;
treeMap.at(line) = tag;
}
}
}
cout << "Hit Rate: " << (double)hits/(double)(hits + misses) << endl;
}
/**
* @brief This function will calculate the hit rate for a n-way set associative cache
* @param addresses the vector of addresses that will be used to calculate the hit rate
* @param bytesPerBlock the amount of bytes per block in cache
* @param nSetWay the set associativity of the cache
* @param cacheSize the size of the cache
* @param replacementPolicy the replacement policy the user wants to use (FIFO or LRU)
*/
void setAssociative(vector<string>& addresses, int bytesPerBlock, int nSetWay, int cacheSize, string& replacementPolicy) {
int hits = 0;
int misses = 0;
map<string, map<string, string>> treeMap;
map<string, queue<string>> tagQueueMap;
map<string, list<string>> tagListMap;
for(int i = 0; i < addresses.size(); i++) {
int numOfSets = (cacheSize / bytesPerBlock) / nSetWay;
int setWidth = log2(numOfSets);
int offsetWidth = log2(bytesPerBlock);
int tagWidth = addresses.at(i).length() - setWidth - offsetWidth;
string tag = addresses.at(i).substr(0, tagWidth);
string set = addresses.at(i).substr(tagWidth, setWidth);
string offset = addresses.at(i).substr(tagWidth + setWidth, offsetWidth);
if(treeMap.find(set) == treeMap.end()) {
misses++;
map<string, string> tempMap;
tempMap.insert(make_pair(tag, offset));
treeMap.insert(make_pair(set, tempMap));
if(replacementPolicy == "FIFO") {
queue<string> q;
q.push(tag);
tagQueueMap.insert(make_pair(set, q));
}
else if(replacementPolicy == "LRU") {
list<string> l;
l.push_back(tag);
tagListMap.insert(make_pair(set, l));
}
}
else {
if(treeMap.at(set).find(tag) == treeMap.at(set).end()) {
misses++;
if(treeMap.at(set).size() < nSetWay) {
treeMap.at(set).insert(make_pair(tag, offset));
if(replacementPolicy == "FIFO") {
tagQueueMap.at(set).push(tag);
}
else if(replacementPolicy == "LRU") {
tagListMap.at(set).push_back(tag);
}
}
else if(treeMap.at(set).size() == nSetWay) {
if(replacementPolicy == "FIFO") {
treeMap.at(set).erase(tagQueueMap.at(set).front());
tagQueueMap.at(set).pop();
tagQueueMap.at(set).push(tag);
}
else if(replacementPolicy == "LRU") {
treeMap.at(set).erase(tagListMap.at(set).front());
tagListMap.at(set).pop_front();
tagListMap.at(set).push_back(tag);
}
treeMap.at(set).insert(make_pair(tag, offset));
}
}
else {
hits++;
if(replacementPolicy == "LRU") {
tagListMap.at(set).remove(tag);
tagListMap.at(set).push_back(tag);
}
}
}
}
cout << "Hit Rate: " << (double)hits/(double)(hits + misses) << endl;
}
/**
* @brief this function was used to calculate the hit rates for the three different cache designs
* @param addresses - the vector of addresses that will be used to calculate the hit rate
* @attention commented in main() method. Results from method call were saved into a Excel file to create line graph
*/
void simulateValues(vector<string>& addresses) {
int start = pow(2, 9);
int end = pow(2, 16);
string fifo = "FIFO";
string lru = "LRU";
cout << "Fully Associative (FIFO)" << endl;
for(int i = start; i <= end; i *= 2) {
fullyAssociative(addresses, 64, i, fifo);
}
cout << endl;
cout << "Fully Associative (LRU)" << endl;
for(int i = start; i <= end; i *= 2) {
fullyAssociative(addresses, 64, i, lru);
}
cout << endl;
cout << "Direct Mapped" << endl;
for(int i = start; i <= end; i *= 2) {
directMap(addresses, 64, i);
}
cout << endl;
for(int i = 2; i <= 8; i *= 2) {
cout << "Set Associative " << i << "-way (FIFO)";
cout << endl;
for(int j = start; j <= end; j *= 2) {
setAssociative(addresses, 64, i, j, fifo);
}
}
for(int i = 2; i <= 8; i *= 2) {
cout << "Set Associative " << i << "-way (LRU)";
cout << endl;
for(int j = start; j <= end; j *= 2) {
setAssociative(addresses, 64, i, j, lru);
}
}
}
int main() {
string filename;
cout << "Enter filename: " << endl;
cin >> filename;
string currDirectory = "Trace files/" + filename + ".trace";
vector<string> addresses;
readFile(currDirectory, addresses);
for(string& address : addresses) {
address = convertToBin(address);
}
//simulateValues(addresses);
string cacheType;
cout << "Enter cache type: " << endl;
cin >> ws;
getline(cin, cacheType);
if(cacheType == "Fully Associative") {
string replaceStrat;
cout << "Enter replacement strategy: " << endl;
cin >> replaceStrat;
if(replaceStrat == "FIFO" || replaceStrat == "LRU") {
int bytesPerBlock = 0;
int cacheSize = 0;
while(true) {
cout << "Enter bytes per block: " << endl;
cin >> bytesPerBlock;
if(bytesPerBlock >= 4 && (bytesPerBlock & bytesPerBlock - 1) == 0) {
break;
}
else {
cout << "Invalid number of bytes in a block" << endl;
cout << endl;
}
}
while(true) {
cout << "Enter the cache size: " << endl;
cin >> cacheSize;
if(cacheSize <= 0 && (cacheSize & cacheSize - 1) == 0) {
cout << "Cache size must be a power of 2" << endl;
cout << endl;
}
else {
break;
}
}
fullyAssociative(addresses, bytesPerBlock, cacheSize, replaceStrat);
}
else {
cout << "Invalid replacement strategy" << endl;
}
}
else if(cacheType == "Direct Mapped") {
int bytesPerBlock = 0;
int cacheSize = 0;
while(true) {
cout << "Enter bytes per block: " << endl;
cin >> bytesPerBlock;
if(bytesPerBlock >= 4 && (bytesPerBlock & bytesPerBlock - 1) == 0) {
break;
}
else {
cout << "Invalid number of bytes in a block" << endl;
cout << endl;
}
}
while(true) {
cout << "Enter the cache size: " << endl;
cin >> cacheSize;
if (cacheSize <= 0 && (cacheSize & cacheSize - 1) == 0) {
cout << "Cache size must be a power of 2" << endl;
cout << endl;
} else {
break;
}
}
directMap(addresses, bytesPerBlock, cacheSize);
}
else if(cacheType == "Set Associative") {
string replaceStrat;
cout << "Enter replacement strategy: " << endl;
cin >> replaceStrat;
if(replaceStrat == "FIFO" || replaceStrat == "LRU") {
int bytesPerBlock = 0;
int cacheSize = 0;
int nSetWay = 0;
while(true) {
cout << "Enter bytes per block: " << endl;
cin >> bytesPerBlock;
if(bytesPerBlock >= 4 && (bytesPerBlock & bytesPerBlock - 1) == 0) {
break;
}
else {
cout << "Invalid number of bytes in a block" << endl;
cout << endl;
}
}
while(true) {
cout << "Enter the cache size: " << endl;
cin >> cacheSize;
if(cacheSize <= 0 && (cacheSize & cacheSize - 1) == 0) {
cout << "Cache size must be a power of 2" << endl;
cout << endl;
}
else {
break;
}
}
cout << "Enter number in to represent n-set way: " << endl;
cin >> nSetWay;
setAssociative(addresses, bytesPerBlock, nSetWay, cacheSize, replaceStrat);
}
else {
cout << "Invalid replacement strategy" << endl;
}
}
else {
cout << "Invalid cache type" << endl;
}
return 0;
}