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
|
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
extern "C" {
void printSummary(int hits, int misses, int evictions);
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
}
using namespace std;
long long s, E, b,
hitCnt, missCnt, eviCnt;
size_t timeCnt;
string traceFile;
bool DEBUG = false;
class ov4Cache
{
public:
bool valid = false;
size_t tag = 0;
size_t time = 0;
};
vector<vector<ov4Cache>> cache;
void findFreeSlot(const size_t Tag, const size_t Set) // update eviCnt
{
// if we can find free slot!
auto findFree = find_if(cache[Set].begin(), cache[Set].end(),
[&](const ov4Cache &c)
{
return !c.valid;
});
if (findFree != cache[Set].end())
{
findFree->valid = true;
findFree->time = timeCnt; // very important to update time
findFree->tag = Tag;
return;
}
// no slot is free, and we have to find the oldest
eviCnt++;
auto findOld = min_element(cache[Set].begin(), cache[Set].end(),
[](const ov4Cache &x, const ov4Cache &y)
{
return x.time < y.time;
});
findOld->valid = true;
findOld->time = timeCnt;
findOld->tag = Tag;
if (DEBUG) cout << "eviction ";
}
void solve(const size_t addr) // update hitCnt, missCnt
{
timeCnt++;
size_t Tag, Set;
Tag = addr >> (s+b);
Set = ((addr << (64-s-b)) >> (64-s-b))>>b;
// determine hit/miss
auto res = find_if(cache[Set].begin(), cache[Set].end(),
[&](const ov4Cache &c)
{
return c.valid && c.tag == Tag;
});
if (res != cache[Set].end())
{
hitCnt++;
res->time = timeCnt; // update the time, because we use it again
if (DEBUG) cout << "hit ";
return;
}
missCnt++;
if (DEBUG) cout << "miss ";
findFreeSlot(Tag, Set);
}
int main(int argc, char *argv[])
{
// get arguments
int opt = '?';
while ((opt = getopt(argc, argv, "s:E:b:t:vh")) != -1)
{
switch (opt)
{
case 's':
s = atol(optarg);
break;
case 'E':
E = atol(optarg);
break;
case 'b':
b = atol(optarg);
break;
case 't':
traceFile = optarg;
break;
case 'v':
DEBUG = true;
break;
case 'h':
cout << "\
Usage: ./csim [-hv] -s <num> -E <num> -b <num> -t <file>\n\
Options:\n\
-h Print this help message.\n\
-v Optional verbose flag.\n\
-s <num> Number of set index bits.\n\
-E <num> Number of lines per set.\n\
-b <num> Number of block offset bits.\n\
-t <file> Trace file.\n\
\n\
Examples:\n\
linux> ./csim -s 4 -E 1 -b 4 -t traces/yi.trace\n\
linux> ./csim -v -s 8 -E 2 -b 4 -t traces/yi.trace" << endl;
return 0;
break;
case '?':
cerr << "Unknow arg" << endl;
break;
default:
break;
}
}
// update size of cache
cache.resize((1<<s), vector<ov4Cache>(E));
ifstream file(traceFile);
if (!file.is_open()) {
cerr << "LOL" << endl;
return -1;
}
// handle every line
// NB. `L` `S` are acutally same operations in terms of counting
// for `M`, it can be treated as `L` followed by `S`, and consequently twice `solve()`
string line;
while (getline(file, line))
{
if (line.size()==0 || line[0] == 'I') continue; // ignore I
char op;
size_t addr;
int useless;
sscanf(line.c_str(), " %c %zx,%d", &op, &addr, &useless);
if (DEBUG) cout << op << " " << hex << addr << "," << useless << " ";
if (op == 'S' || op == 'L') solve(addr);
else {solve(addr); solve(addr); }
cout << endl;
}
printSummary(hitCnt, missCnt, eviCnt);
file.close();
return 0;
}
|