-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
421 lines (416 loc) · 16.1 KB
/
Copy pathmain.cpp
File metadata and controls
421 lines (416 loc) · 16.1 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
#include <iostream>
#include <vector>
#include <utility>
#include <fstream>
#include <sstream>
#include <list>
#include <unordered_map>
#include <queue>
#include <limits>
using namespace std;
class Cache
{
private:
int no_of_sets;
int no_of_blocks;
int capacity_of_blocks;
vector<vector<bool>> valid;
vector<vector<bool>> dirtybit;
vector<vector<int>> tag;
vector<vector<int>> lrutag;
vector<int> fifotag; // queue containg line nos of a particular set
int cache_access_time = 1; // it is preassumed that processor will ask for 1 byte data so independent of data size
int main_memory_access_time_4_bytes = 100;
bool write_allocate;
bool write_through_or_write_back;
bool lru_or_fifo;
vector<pair<char, unsigned int>> trace_file;
int total_cycle = 0;
int load_hit = 0;
int load_miss = 0;
int store_hit = 0;
int store_miss = 0;
int total_load = 0;
int total_store = 0;
int get_set_index(unsigned int address)
{
return (address / this->capacity_of_blocks) % (this->no_of_sets);
}
int get_tag(unsigned int address)
{
return (address / (this->no_of_sets * this->capacity_of_blocks));
}
int check_hit_get_way_index(unsigned int address)
{
int set_index = this->get_set_index(address);
int address_tag = this->get_tag(address);
for (int i = 0; i < this->no_of_blocks; i++)
{
if (this->valid[set_index][i] == 1 && this->tag[set_index][i] == address_tag)
{
return i; // hit
}
}
return -1; // miss
}
int check_compulsory_miss_get_free_line(unsigned int address)
{
int set_index = this->get_set_index(address);
for (int i = 0; i < this->no_of_blocks; i++)
{
if (this->valid[set_index][i] == 0)
{
return i; // empty location was found
}
}
return -1;
}
void update_lru_tags()
{
for (int i = 0; i < this->no_of_sets; i++)
{
for (int j = 0; j < this->no_of_blocks; j++)
{
if (this->lrutag[i][j] != -1)
{
this->lrutag[i][j]++;
}
}
}
}
int get_lru(int set_id)
{
int res = -1;
int max_lrutag = std::numeric_limits<int>::min();
for (int i = 0; i < this->no_of_blocks; i++)
{
if (this->lrutag[set_id][i] > max_lrutag)
{
max_lrutag = this->lrutag[set_id][i];
res = i;
}
}
return res;
}
public:
Cache(int s, int b, int bc, bool wa, bool wt_or_wb, bool l_or_fifo)
{
this->no_of_sets = s;
this->no_of_blocks = b;
this->capacity_of_blocks = bc;
this->write_allocate = wa;
this->write_through_or_write_back = wt_or_wb;
this->lru_or_fifo = l_or_fifo;
vector<bool> u(b, false);
vector<vector<bool>> v(s, u);
this->valid = v;
this->dirtybit = v;
vector<int> l(b, -1);
vector<vector<int>> p(s, l);
this->tag = p;
this->lrutag = p;
vector<int> q(s, 0);
this->fifotag = q;
}
void set_trace_file(vector<pair<char, unsigned int>> t_f)
{
this->trace_file = t_f;
}
void simulate_cache()
{
for (auto p : this->trace_file)
{
if (this->lru_or_fifo)
{
this->update_lru_tags();
}
int curr_set_index = this->get_set_index(p.second);
int curr_line_if_hit = this->check_hit_get_way_index(p.second);
if (curr_line_if_hit != -1)
{ // hit
if (this->lru_or_fifo)
{
this->lrutag[curr_set_index][curr_line_if_hit] = 0;
}
if (p.first == 'l')
{
this->load_hit++;
this->total_load++;
// processor take back data
this->total_cycle += this->cache_access_time;
// done
}
else
{ //'s'
this->store_hit++;
this->total_store++;
// make change in cache
this->total_cycle += this->cache_access_time;
if (this->write_through_or_write_back)
{ // write through
// update the main memory
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4);
// done
}
else
{ // write back
this->dirtybit[curr_set_index][curr_line_if_hit] = true;
// done
}
}
}
else
{ // miss
int free_line = this->check_compulsory_miss_get_free_line(p.second);
if (p.first == 'l')
{
this->load_miss++;
this->total_load++;
if (free_line != -1)
{ // empty space found
if (this->lru_or_fifo)
{
this->lrutag[curr_set_index][free_line] = 0;
}
this->valid[curr_set_index][free_line] = true;
// bring the data frm main memory to cache
this->tag[curr_set_index][free_line] = get_tag(p.second);
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4); // read from main memory
this->total_cycle += this->cache_access_time; // write to cache memory
// processor accesses the loaded data(here we neglect the modelling the exact data fetch using offest that we need during making a cache)
this->total_cycle += this->cache_access_time;
// done
}
else
{ // conflict or capacity miss
int curr_line;
if (this->lru_or_fifo)
{ // lru
curr_line = this->get_lru(curr_set_index);
this->lrutag[curr_set_index][curr_line] = 0;
}
else
{ // fifo
curr_line = this->fifotag[curr_set_index];
this->fifotag[curr_set_index] = (this->fifotag[curr_set_index] + 1) % (this->no_of_blocks);
}
if (!(this->write_through_or_write_back) && this->dirtybit[curr_set_index][curr_line])
{
// update the main memory and send back the data
this->total_cycle += this->cache_access_time; // read from cache
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4); // write to main memory
this->dirtybit[curr_set_index][curr_line] = false; // update dirty bit to zero for new data upcoming
}
// bring data from main memory to cache
this->tag[curr_set_index][curr_line] = get_tag(p.second);
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4); // read from main memory
this->total_cycle += this->cache_access_time; // write to cache
// valid bit remains to be 1
// processor loads this fetched data from cache
this->total_cycle += this->cache_access_time;
// done
}
}
else
{ //'s'
this->store_miss++;
this->total_store++;
if (free_line != -1)
{ // compulsory miss
if (this->write_allocate)
{
if (this->lru_or_fifo)
{
this->lrutag[curr_set_index][free_line] = 0;
}
this->valid[curr_set_index][free_line] = true;
// bring the data from main memory
this->tag[curr_set_index][free_line] = get_tag(p.second);
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4);
this->total_cycle += this->cache_access_time;
this->total_cycle += this->cache_access_time;
if (this->write_through_or_write_back)
{ // write through
// update the main memory
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4);
// done
}
else
{ // write back
this->dirtybit[curr_set_index][free_line] = true;
// done
}
}
else
{ // no write allocate
// directly write to main memory(we will beassuming same 100 cycle thing) cache is left untouched
// update the main memory
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4);
// done
}
}
else
{ // conflict or capacity miss
// valid bit continues to be one
if (this->write_allocate)
{
int curr_line;
if (this->lru_or_fifo)
{ // lru
curr_line = this->get_lru(curr_set_index);
this->lrutag[curr_set_index][curr_line] = 0;
}
else
{ // fifo
curr_line = this->fifotag[curr_set_index];
this->fifotag[curr_set_index] = (this->fifotag[curr_set_index] + 1) % (this->no_of_blocks);
}
if (!(this->write_through_or_write_back) && this->dirtybit[curr_set_index][curr_line])
{
// update the main memory and send back the data
this->total_cycle += this->cache_access_time;
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4);
this->dirtybit[curr_set_index][curr_line] = false; // update dirty bit to zero for new data upcoming
}
// bring the data from main memory
this->tag[curr_set_index][curr_line] = get_tag(p.second);
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4);
this->total_cycle += this->cache_access_time;
this->total_cycle += this->cache_access_time;
if (this->write_through_or_write_back)
{ // write through
// update the main memory
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4);
// done
}
else
{ // write back
this->dirtybit[curr_set_index][curr_line] = true;
// done
}
}
else
{ // no write allocate
// directly write to main memory(we will beassuming same 100 cycle thing) cache is left untouched
// update the main memory
this->total_cycle += (this->main_memory_access_time_4_bytes) * ((this->capacity_of_blocks + 4 - 1) / 4);
// done
}
}
}
}
}
}
int get_total_cycles()
{
return this->total_cycle;
}
int get_total_load_hit()
{
return this->load_hit;
}
int get_total_load_miss()
{
return this->load_miss;
}
int get_total_store_hit()
{
return this->store_hit;
}
int get_total_store_miss()
{
return this->store_miss;
}
int get_total_load()
{
return this->total_load;
}
int get_total_store()
{
return this->total_store;
}
};
int hexStringToInt(const std::string &hexStr)
{
std::stringstream ss;
ss << std::hex << hexStr;
unsigned int result;
ss >> result;
return result;
}
void store_trace_file(string filename, vector<pair<char, unsigned int>> &result)
{
std::ifstream infile(filename);
if (!infile.is_open())
{
std::cerr << "Error opening file." << std::endl;
}
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
char action;
std::string value;
if (iss >> action >> value)
{
if (value.size() >= 2 && value.substr(0, 2) == "0x")
{
value = value.substr(2);
}
unsigned int vl = hexStringToInt(value);
result.push_back({action, vl});
}
}
infile.close();
}
int main(int argc, char *argv[])
{
if (argc != 7)
{
cout << "Incorrect number of parameters" << endl;
}
bool wa = false;
if (std::string(argv[4]) == "write-allocate")
{
wa = true;
}
bool wt_or_wb = true;
if (std::string(argv[5]) == "write-back")
{
wt_or_wb = false;
}
bool lru_or_fifo = false;
if (std::string(argv[6]) == "lru")
{
lru_or_fifo = true;
}
Cache c(stoi(argv[1]), stoi(argv[2]), stoi(argv[3]), wa, wt_or_wb, lru_or_fifo);
vector<pair<char, unsigned int>> v;
// store_trace_file(argv[7], v);
// Read from stdin
string line;
while (getline(cin, line)) {
std::istringstream iss(line);
char action;
std::string value;
if (iss >> action >> value)
{
if (value.size() >= 2 && value.substr(0, 2) == "0x")
{
value = value.substr(2);
}
unsigned int vl = hexStringToInt(value);
v.push_back({action, vl});
}
}
c.set_trace_file(v);
c.simulate_cache();
cout << "Total loads: " << c.get_total_load() << endl;
cout << "Total stores: " << c.get_total_store() << endl;
cout << "Load hits: " << c.get_total_load_hit() << endl;
cout << "Load misses: " << c.get_total_load_miss() << endl;
cout << "Store hits: " << c.get_total_store_hit() << endl;
cout << "Store misses: " << c.get_total_store_miss() << endl;
cout << "Total cycles: " << c.get_total_cycles() << endl;
return 0;
}