-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulator.cpp
132 lines (123 loc) · 4.42 KB
/
simulator.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
/*
* Copyright (c) 2019, Aditya Rohan
* Copyright (c) 2019, Aniket Pandey
*
* Submitted to:
* CS622A: 2019-20 Fall Semester. Assignment 1
*/
#include <experimental/filesystem>
#include <iostream>
#include <fstream>
#include <sstream>
#include "cache.h"
using namespace std;
namespace fs = std::experimental::filesystem;
void inclusive(shared_ptr<Cache> l2, shared_ptr<Cache> l3, int_t addr) {
int_t evicted;
if (l2->check_hit_or_miss(addr)) {
(l2->hits)++;
l2->update_on_hit(addr);
} else {
(l2->misses)++;
if (l3->check_hit_or_miss(addr)) {
(l3->hits)++;
l3->update_on_hit(addr);
l2->add_block(addr);
} else {
(l3->misses)++;
evicted = l3->add_block(addr);
if (evicted && l2->check_hit_or_miss(evicted))
l2->invalidate_block(evicted);
l2->add_block(addr);
}
}
}
void exclusive(shared_ptr<Cache> l2, shared_ptr<Cache> l3, int_t addr) {
int_t evicted;
if (l2->check_hit_or_miss(addr)) {
(l2->hits)++;
l2->update_on_hit(addr);
} else {
(l2->misses)++;
if (l3->check_hit_or_miss(addr)) {
(l3->hits)++;
l3->invalidate_block(addr);
evicted = l2->add_block(addr);
if(evicted > 0)
l3->add_block(evicted);
} else {
(l3->misses)++;
evicted = l2->add_block(addr);
if (evicted > 0)
l3->add_block(evicted);
}
}
}
void nine(shared_ptr<Cache> l2, shared_ptr<Cache> l3, int_t addr) {
if (l2->check_hit_or_miss(addr)) {
(l2->hits)++;
l2->update_on_hit(addr);
} else {
(l2->misses)++;
if (l3->check_hit_or_miss(addr)) {
(l3->hits)++;
l3->update_on_hit(addr);
l2->add_block(addr);
} else {
(l3->misses)++;
l3->add_block(addr);
l2->add_block(addr);
}
}
}
int main() {
int_t type, address;
string line, folder = "output";
for (const auto& tracefile : fs::directory_iterator(folder)) {
ifstream tracestrm (tracefile.path());
if (!tracestrm.is_open())
cerr << "Tracefile " << tracefile.path().filename().string()
<< " could not be opened\n";
// Create shared pointers to the cache hierarchy for all
// different inclusion/exclusion policies.
Cache::Ptr l2Incl = make_shared<Cache>(L2Cache, L2_SET, L2_WAY);
Cache::Ptr l3Incl = make_shared<Cache>(L3Cache, L3_SET, L3_WAY);
Cache::Ptr l2Excl = make_shared<Cache>(L2Cache, L2_SET, L2_WAY);
Cache::Ptr l3Excl = make_shared<Cache>(L3Cache, L3_SET, L3_WAY);
Cache::Ptr l2Nine = make_shared<Cache>(L2Cache, L2_SET, L2_WAY);
Cache::Ptr l3Nine = make_shared<Cache>(L3Cache, L3_SET, L3_WAY);
// Read through the traces and simlulate above declared caches
// through the corresponding trace.
cout << "Processing file " << tracefile.path().filename().string()
<< endl;
while (getline(tracestrm, line)) {
stringstream line_(line);
line_ >> type >> address;
// L1 cache missed, forward it to lower levels
address = address >> BLOCK_OFFSET;
if (type) {
inclusive(l2Incl, l3Incl, address);
exclusive(l2Excl, l3Excl, address);
nine(l2Nine, l3Nine, address);
}
}
cout << "------------ " << tracefile.path().filename().string()
<< " ------------";
cout << "\n INCLUSIVE | L2 Hits: " << l2Incl->hits;
cout << "\n INCLUSIVE | L2 Misses: " << l2Incl->misses;
cout << "\n INCLUSIVE | L3 Hits: " << l3Incl->hits;
cout << "\n INCLUSIVE | L3 Misses: " << l3Incl->misses;
cout << "\n";
cout << "\n EXCLUSIVE | L2 Hits: " << l2Excl->hits;
cout << "\n EXCLUSIVE | L2 Misses: " << l2Excl->misses;
cout << "\n EXCLUSIVE | L3 Hits: " << l3Excl->hits;
cout << "\n EXCLUSIVE | L3 Misses: " << l3Excl->misses;
cout << "\n";
cout << "\n Non-In/Ex | L2 Hits: " << l2Nine->hits;
cout << "\n Non-In/Ex | L2 Misses: " << l2Nine->misses;
cout << "\n Non-In/Ex | L3 Hits: " << l3Nine->hits;
cout << "\n Non-In/Ex | L3 Misses: " << l3Nine->misses;
cout << "\n\n";
}
return 0;
}