-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathqcache-mesi.h
237 lines (201 loc) · 6.06 KB
/
qcache-mesi.h
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
#if !__QCACHE_MESI_H || __QCACHE_DEF_MSI || __QCACHE_DEF_MOESI
#define __QCACHE_MESI_H 1
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <stdint.h>
#include <pthread.h>
#include <limits.h>
#include <stdlib.h>
#include "qcache.h"
#include "qcache-dir.h"
namespace Qcache {
// Directory MESI coherence protocol.
template <int L2LINESZ, typename CACHE> class
#ifdef __QCACHE_DEF_MSI
CPDirMsi
#else
#ifdef __QCACHE_DEF_MOESI
CPDirMoesi
#else
CPDirMesi
#endif
#endif
{
public:
#ifdef __QCACHE_DEF_MSI
CPDirMsi(std::vector<CACHE>&caches):
#else
#ifdef __QCACHE_DEF_MOESI
CPDirMoesi(std::vector<CACHE> &caches):
#else
CPDirMesi(std::vector<CACHE> &caches):
#endif
#endif
caches(caches) {}
enum State {
STATE_I = 0x00,
STATE_X = 0x01, // Initial state
STATE_M = 0x02,
STATE_O = 0x03,
STATE_E = 0x04,
STATE_S = 0x05
};
void lockAddr(addr_t addr, int id) { dir.lockAddr(addr, id); }
void unlockAddr(addr_t addr, int id) { dir.unlockAddr(addr, id); }
void addAddr(addr_t addr, int id) { dir.addAddr(addr, id); }
void remAddr(addr_t addr, int id) { dir.remAddr(addr, id); }
bool isExclusive(addr_t addr, int id) {
std::set<int>::iterator it;
for (it = dir.idsBegin(addr, id); it != dir.idsEnd(addr, id); ++it) {
if (*it == id) continue;
return false;
}
return true;
}
bool hitAddr(int id, addr_t addr, bool locked,
spinlock_t *setLock, uint64_t *line, bool wr)
{
if (getState(line) == STATE_M || (getState(line) == STATE_E)) {
if (getState(line) == STATE_E && wr) {
setState(line, STATE_M);
}
if (setLock) spin_unlock(setLock);
return true;
} else if (getState(line)) {
if (setLock) spin_unlock(setLock);
if (!wr) return true;
// If I don't hold the lock for this address, get it.
if (!locked) {
lockAddr(addr, id);
}
// Since we have to serialize on the address lock, it is still possible
// for a miss to occur
if (!dir.hasId(addr, id)) {
if (locked) {
pthread_mutex_lock(&errLock);
std::cerr << "Hit 0x" << std::hex << addr << " on " << id
<< " missing from dir. Dir entry:";
for (std::set<int>::iterator it = dir.idsBegin(addr, id);
it != dir.idsEnd(addr, id); ++it)
{
std::cout << ' ' << *it;
}
std::cerr << '\n';
std::cerr << "Cache set:"; caches[id].dumpSet(addr);
pthread_mutex_unlock(&errLock);
ASSERT(false);
}
unlockAddr(addr, id);
return false;
}
// Invalidate all of the remote lines.
for (std::set<int>::iterator it = dir.idsBegin(addr, id);
it != dir.idsEnd(addr, id); ++it)
{
if (*it == id) continue;
spinlock_t *l;
uint64_t *invLine = caches[*it].cprotLookup(addr, l, true);
setState(invLine, STATE_I);
spin_unlock(l);
}
dir.clearIds(addr, id);
setState(line, STATE_M);
ASSERT(dir.hasId(addr, id));
if (!locked) {
unlockAddr(addr, id);
}
return true;
} else {
std::cerr << "Invalid state: " << getState(line) << '\n';
ASSERT(false); // Invalid state.
return false;
}
}
bool missAddr(int id, addr_t addr, uint64_t *line, bool wr, bool dirty) {
if (dir.hasId(addr, id)) {
// It's in a lower-level private cache on the same core.
hitAddr(id, addr, true, NULL, line, wr);
return false;
}
State st;
addAddr(addr, id);
bool forwarded = false;
// Change state of remote lines.
bool shared(false);
for (std::set<int>::iterator it = dir.idsBegin(addr, id);
it != dir.idsEnd(addr, id); ++it)
{
if (*it == id) continue;
shared = true;
forwarded = true;
spinlock_t *l;
uint64_t *remLine = caches[*it].cprotLookup(addr, l, wr);
if (!remLine) continue;
if (wr && dirty) {
setState(remLine, STATE_I);
while (remLine = caches[*it].cprotLookup(addr, l, wr, false)) {
setState(remLine, STATE_I);
}
} else {
#ifdef __QCACHE_DEF_MOESI
// MOESI doesn't have to do a writeback yet.
if (getState(remLine) == STATE_M || getState(remLine) == STATE_E ||
getState(remLine) == STATE_O)
{
setState(remLine, STATE_O);
} else {
setState(remLine, STATE_S);
}
#else
// This line has to be written back.
if (getState(remLine) == STATE_M && caches[*it].lowerLevel) {
caches[*it].lowerLevel->access(addr, true, *it, /*WRITEBACK_D*/3);
}
setState(remLine, STATE_S);
#endif
}
spin_unlock(l);
}
if (wr && dirty) dir.clearIds(addr, id);
if (wr && dirty) st = STATE_M;
#ifdef __QCACHE_DEF_MSI
else st = STATE_S;
#else
else if (shared) st = STATE_S;
else st = STATE_E;
#endif
setState(line, st);
ASSERT(dir.hasId(addr, id));
return forwarded;
}
void evAddr(int id, addr_t addr) {
remAddr(addr, id);
}
bool dirty(int state) {
return state == STATE_M || state == STATE_O;
}
private:
CoherenceDir<L2LINESZ> dir;
void setState(uint64_t *line, State state) {
#ifdef DEBUG
pthread_mutex_lock(&errLock);
addr_t addr = *line & ~((1<<L2LINESZ)-1);
int curSt = *line & ((1<<L2LINESZ)-1), nextSt = int(state);
const char STATES[] = {'I', 'X', 'M', 'O', 'E', 'S'};
std::cout << "0x" << std::hex << addr << ' '
<< STATES[curSt] << "->" << STATES[nextSt] << '\n';
pthread_mutex_unlock(&errLock);
#endif
*line = *line & ~(uint64_t)((1<<L2LINESZ)-1) | state;
ASSERT(getState(line) == state);
}
State getState(uint64_t *line) {
return State(*line & ((1<<L2LINESZ)-1));
}
std::vector<CACHE> &caches;
};
};
#endif