-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache Simulation.cpp
341 lines (322 loc) · 11.9 KB
/
Cache Simulation.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
//https://cplusplus.com/reference/bitset/bitset/
//https://cplusplus.com/reference/string/stoi/
//https://cplusplus.com/reference/cmath/log2/
//https://www.geeksforgeeks.org/returning-multiple-values-from-a-function-using-tuple-and-pair-in-c/
#include <iostream>
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <math.h>
#include <queue>
#include <bitset>
#include <map>
#include <vector>
using namespace std;
void printQ(queue<int> q) {
while (!q.empty()) {
cout << q.front() << endl;
q.pop();
}
}
int findIndexOf(int value, map<int, int> m) {
for (auto it : m) {
if (value == it.second) {
return it.first;
}
}
return -1;
}
pair<int, int> findIndexOfSet(int value, map<int, map<int, int>> m) {
for (auto it : m) {
for (auto it2 : it.second) {
if (it2.second == value) {
return pair<int, int>(it.first, it2.first);
}
}
}
return pair<int, int>(-1, -1);
}
queue<int> removeFromQueue(int value, queue<int> q) {
vector<int> v;
int size = q.size();
bool check = true;
for (int i = 0; i < size; i++) {
if (q.front() == value && check == true) {
q.pop();
check == false;
}
else {
v.push_back(q.front());
q.pop();
}
}
for (int i = 0; i < v.size(); i++) {
q.push(v[i]);
}
return q;
}
queue<int> replaceInQueueQ1(queue<int> q1, map<int, int> m1) {
for (auto it : m1) {
q1.push(it.second);
}
return q1;
}
unsigned int hexToDecimal(string num) {
string tempS = num.substr(2, num.length() - 1);
unsigned int temp = stoul(tempS, nullptr, 16);
return temp;
}
string decimalToBinary(unsigned int num) {
bitset<32> newNum(num);
string binaryNum = newNum.to_string();
return binaryNum;
}
int binaryToDecimal(string binary) {
int temp = stoi(binary, nullptr, 2);
return temp;
}
pair<map<int, int>, int> cacheSimulationDirect(unsigned long long system, unsigned long long cacheSize, unsigned long long blockSize, string binary, map<int, int> m, int hits) {
unsigned int lines = cacheSize / blockSize;
int bitsWide = log2(system) ;
binary = binary.substr(binary.length() - bitsWide);
int offsetField = log2(blockSize);
int lineField = log2(lines);
int tagField = bitsWide - offsetField - lineField;
//cout << "Binary: " << binary << endl;
string offset = binary.substr(binary.length() - offsetField);
binary = binary.substr(0, binary.length() - offsetField);
string line = binary.substr(binary.length() - lineField);
binary = binary.substr(0, binary.length() - lineField);
string tag = binary.substr(binary.length() - tagField);
binary = binary.substr(0, binary.length() - tagField);
//cout << "Offset: " << offset << endl;
//cout << "Line: " << line << endl;
//cout << "Tag: " << tag << endl;
int index = binaryToDecimal(line);
int tagMapped = binaryToDecimal(tag);
if (findIndexOf(tagMapped, m) == index) {
hits++;
}
else {
m[index] = tagMapped;
}
return pair<map<int, int>, int>(m, hits);
}
tuple<map<int, int>, int, queue<int>> cacheSimulationFully(unsigned long long system, unsigned long long cacheSize, unsigned long long blockSize, string binary, map<int, int> m, int j, int hits, queue<int> q, string replacement) {
unsigned int lines = cacheSize / blockSize;
int bitsWide = log2(system);
binary = binary.substr(binary.length() - bitsWide);
int offsetField = log2(blockSize);
int tagField = bitsWide - offsetField;
//cout << "Binary: " << binary << endl;
string offset = binary.substr(binary.length() - offsetField);
binary = binary.substr(0, binary.length() - offsetField);
string tag = binary.substr(binary.length() - tagField);
binary = binary.substr(0, binary.length() - tagField);
//cout << "Offset: " << offset << endl;
//cout << "Tag: " << tag << endl;
int tagMapped = binaryToDecimal(tag);
if (findIndexOf(tagMapped, m) != -1) {
if (replacement == "lru") {
q = removeFromQueue(tagMapped, q);
q.push(tagMapped);
}
hits++;
}
else if (m.size() >= cacheSize) {
for (auto it : m) {
if (it.second == q.front()) {
m[it.first] = tagMapped;
}
}
q.pop();
q.push(tagMapped);
}
else {
q.push(tagMapped);
m[j] = tagMapped;
}
return tuple<map<int, int>, int, queue<int>>(m, hits, q);
}
tuple<map<int, map<int, int>>, int, queue<int>> cacheSimulationSet(unsigned long long system, unsigned long long cacheSize, unsigned long long blockSize, string binary, map<int, map<int, int>> m, int set, int hits, string replacement, queue<int> q) {
unsigned int sets = cacheSize / (blockSize * set);
int bitsWide = log2(system);
binary = binary.substr(binary.length() - bitsWide);
int offsetField = log2(blockSize);
int setField = log2(sets);
int tagField = bitsWide - offsetField - setField;
//cout << "Binary: " << binary << endl;
string offset = binary.substr(binary.length() - offsetField);
binary = binary.substr(0, binary.length() - offsetField);
string setsString = binary.substr(binary.length() - setField);
binary = binary.substr(0, binary.length() - setField);
string tag = binary.substr(binary.length() - tagField);
binary = binary.substr(0, binary.length() - tagField);
//cout << "Offset: " << offset << endl;
//cout << "Set: " << setsString << endl;
// cout << "Tag: " << tag << endl;
int j = 0;
int setInt = binaryToDecimal(setsString);
int tagMapped = binaryToDecimal(tag);
queue<int> q1;
map<int, int> m1;
//cout << "Tag Decimal: " << tagMapped << endl;
for (int i = 0; i < bitsWide; i++) {
if (i != 0 && (i % set) == 0) {
j++;
}
if (setInt == j) {
if (m.find(i) != m.end()) {
pair<int, int> result = findIndexOfSet(tagMapped, m);
if (result.first != -1) {
if (replacement == "lru") {
q = removeFromQueue(tagMapped, q);
q.push(tagMapped);
}
hits++;
break;
}
else if ((i + 1) % set == 0) {
for (int k = (i - (i % set)); k <= i; k++) {
queue<int> tempQ = q;
int z = 0;
while (!tempQ.empty()) {
if (tempQ.front() == m[k].at(j)) {
m1[z] = m[k].at(j);
}
z++;
tempQ.pop();
}
}
q1 = replaceInQueueQ1(q1, m1);
pair<int, int> p = findIndexOfSet(q1.front(), m);
//cout << "Replaced " << q1.front() << " with " << tagMapped << endl;
q = removeFromQueue(q1.front(), q);
m[p.first][j] = tagMapped;
q.push(tagMapped);
break;
}
}
else {
q.push(tagMapped);
m[i].insert(pair<int, int>(j, tagMapped));
break;
}
}
}
return tuple<map<int, map<int, int>>, int, queue<int>>(m, hits, q);
}
int main()
{
fstream file;
string filename;
string fullFile = "";
int set;
string associativity;
int tempsystemSize;
cout << "Javier Duran" << endl;
cout << "What is the system size (Enter as power of 2, Ex: If 2^32 -> enter '32'): ";
cin >> tempsystemSize;
int tempcacheSize;
cout << "What is the cache size (Enter as power of 2, Ex: If 2^9 -> enter '9') ";
cin >> tempcacheSize;
int tempblockSize;
cout << "What is the block/line size (Enter as power of 2, Ex: If 2^4 -> enter '4'): ";
cin >> tempblockSize;
unsigned long long systemSize = pow(2, tempsystemSize);
unsigned long long cacheSize = pow(2, tempcacheSize);
unsigned long long blockSize = pow(2, tempblockSize);
cout << "Type the associativity you want to use (Direct, Fully, or Set): ";
cin >> associativity;
string tempAs;
for (int i = 0; i < associativity.length(); i++) {
tempAs += tolower(associativity[i]);
}
associativity = tempAs;
cout << "If using Set Associativity, type the set number (if not, then type '0'): ";
cin >> set;
string tempReplacement;
string replacement;
cout << "Select what replacement method to use (LRU or FIFO) (If associativity is direct, type '0'): ";
cin >> tempReplacement;
for (int i = 0; i < tempReplacement.length(); i++) {
replacement += tolower(tempReplacement[i]);
}
cout << "Type the name of the file to be read: ";
cin >> filename;
string temp = "trace/";
fullFile = temp + filename;
file.open(fullFile);
string cacheNum;
int j = 0;
int hits = 0;
int total = 0;
map<int, int> directMap;
map<int, int> fullyMap;
map<int, map<int, int>> setMap;
queue<int> q;
while (getline(file, cacheNum)) {
cacheNum = cacheNum.substr(2, cacheNum.length() - 4);
//cout << "Hex: " << cacheNum << endl;
//cout << "Decimal: " << hexToDecimal(cacheNum) << endl;
if (associativity == "direct") {
pair<map<int, int>, int> m = cacheSimulationDirect(systemSize, cacheSize, blockSize, decimalToBinary(hexToDecimal(cacheNum)), directMap, hits);
directMap = m.first;
hits = m.second;
}
else if (associativity == "fully") {
map<int, int> m1;
int h1;
queue<int> q1;
int temp = hits;
tie(m1, h1, q1) = cacheSimulationFully(systemSize, cacheSize, blockSize, decimalToBinary(hexToDecimal(cacheNum)), fullyMap, j, hits, q, replacement);
fullyMap = m1;
hits = h1;
q = q1;
if (temp == hits) {
j++;
}
}
else if (associativity == "set") {
map<int, map<int, int>> m1;
int h1;
queue<int> q1;
int temp = hits;
tie(m1, h1, q1) = cacheSimulationSet(systemSize, cacheSize, blockSize, decimalToBinary(hexToDecimal(cacheNum)), setMap, set, hits, replacement, q);
setMap = m1;
hits = h1;
q = q1;
}
total++;
//cout << endl;
}
/*cout << "Checking Map: " << endl;
if (associativity == "direct") {
for (auto it : directMap) {
cout << "Index: " << it.first << " Tag: " << it.second << endl;
}
cout << "Total Hits: " << hits << endl;
}
else if (associativity == "fully") {
for (auto it : fullyMap) {
cout << "Index: " << it.first << " Tag: " << it.second << endl;
}
cout << "Total Hits: " << hits << endl;
}
else if (associativity == "set") {
for (auto it : setMap) {
cout << "Index: " << it.first;
for (auto it2 : it.second) {
cout << " Set: " << it2.first << " Tag: " << it2.second << endl;
}
}
cout << "Total Hits: " << hits << endl;
}
*/
double hitRate = (double)hits / (double)total;
cout << "Total: " << total << endl;
cout << "Misses: " << (total - hits) << endl;
cout << "Hit Rate (hits/total): " << hitRate << endl;
file.close();
}