-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMSI_SMPCache.cpp
300 lines (212 loc) · 8.27 KB
/
MSI_SMPCache.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
#include "MSI_SMPCache.h"
MSI_SMPCache::MSI_SMPCache(int cpuid,
std::vector<SMPCache * > * cacheVector,
int csize,
int cassoc,
int cbsize,
int caddressable,
const char * repPol,
bool cskew) :
SMPCache(cpuid,cacheVector){
fprintf(stderr,"Making a MSI cache with cpuid %d\n",cpuid);
CacheGeneric<MSI_SMPCacheState> *c =
CacheGeneric<MSI_SMPCacheState>::create(csize,
cassoc,
cbsize,
caddressable,
repPol,
cskew);
cache = (CacheGeneric<StateGeneric<> >*)c;
}
void MSI_SMPCache::fillLine(uint32_t addr, uint32_t msi_state){
//this gets the state of whatever line this address maps to
MSI_SMPCacheState *st = (MSI_SMPCacheState *)cache->findLine2Replace(addr);
if(st==0){
/*No state*/
return;
}
/*Set the tags to the tags for the newly cached block*/
st->setTag(cache->calcTag(addr));
/*Set the state of the block to the msi_state passed in*/
st->changeStateTo((MSIState_t)msi_state);
return;
}
MSI_SMPCache::RemoteReadService MSI_SMPCache::readRemoteAction(uint32_t addr){
/*This method implements snoop behavior on all the other
*caches that this cache might be interacting with*/
/*Loop over the other caches in the simulation*/
std::vector<SMPCache * >::iterator cacheIter;
std::vector<SMPCache * >::iterator lastCacheIter;
for(cacheIter = this->getCacheVector()->begin(),
lastCacheIter = this->getCacheVector()->end();
cacheIter != lastCacheIter;
cacheIter++){
/*Get a pointer to the other cache*/
MSI_SMPCache *otherCache = (MSI_SMPCache*)*cacheIter;
if(otherCache->getCPUId() == this->getCPUId()){
/*We don't want to snoop our own access*/
continue;
}
/*Get the state of the block this addr maps to in the other cache*/
MSI_SMPCacheState* otherState =
(MSI_SMPCacheState *)otherCache->cache->findLine(addr);
/*If otherState == NULL here, the tags didn't match, so the
*other cache didn't have this line cached*/
if(otherState){
/*The tags matched -- need to do snoop actions*/
/*Other cache has recently written the line*/
if(otherState->getState() == MSI_MODIFIED){
/*Modified transitions to Shared on a remote Read*/
otherState->changeStateTo(MSI_SHARED);
/*Return a Remote Read Service indicating that
*1)The line was not shared (the false param)
*2)The line was provided by otherCache, as only it had it cached
*/
return MSI_SMPCache::RemoteReadService(false,true);
/*Other cache has recently read the line*/
}else if(otherState->getState() == MSI_SHARED){
/*Return a Remote Read Service indicating that
*1)The line was shared (the true param)
*2)The line was provided by otherCache
*/
return MSI_SMPCache::RemoteReadService(true,true);
/*Line was cached, but invalid*/
}else if(otherState->getState() == MSI_INVALID){
/*Do Nothing*/
}
}/*Else: Tag didn't match. Nothing to do for this cache*/
}/*Done with other caches*/
/*If all other caches were MSI_INVALID*/
return MSI_SMPCache::RemoteReadService(false,false);
}
void MSI_SMPCache::readLine(uint32_t rdPC, uint32_t addr){
/*
*This method implements actions taken on a read access to address addr
*at instruction rdPC
*/
/*Get the state of the line to which this address maps*/
MSI_SMPCacheState *st =
(MSI_SMPCacheState *)cache->findLine(addr);
/*Read Miss - tags didn't match, or line is invalid*/
if(!st || (st && !(st->isValid())) ){
/*Update event counter for read misses*/
numReadMisses++;
if(st){
/*Tag matched, but state was invalid*/
numReadOnInvalidMisses++;
}
/*Make the other caches snoop this access
*and get a remote read service object describing what happened.
*This is effectively putting the access on the bus.
*/
MSI_SMPCache::RemoteReadService rrs = readRemoteAction(addr);
numReadRequestsSent++;
if(rrs.providedData){
/*If it was shared or modified elsewhere,
*the line was provided by another cache.
*Update these counters to reflect that
*/
numReadMissesServicedByOthers++;
if(rrs.isShared){
numReadMissesServicedByShared++;
}else{
numReadMissesServicedByModified++;
}
}
/*Fill the line*/
fillLine(addr,MSI_SHARED);
}else{
/*Read Hit - any state but Invalid*/
numReadHits++;
return;
}
}
MSI_SMPCache::InvalidateReply MSI_SMPCache::writeRemoteAction(uint32_t addr){
/*This method implements snoop behavior on all the other
*caches that this cache might be interacting with*/
bool empty = true;
/*Loop over all other caches*/
std::vector<SMPCache * >::iterator cacheIter;
std::vector<SMPCache * >::iterator lastCacheIter;
for(cacheIter = this->getCacheVector()->begin(),
lastCacheIter = this->getCacheVector()->end();
cacheIter != lastCacheIter;
cacheIter++){
MSI_SMPCache *otherCache = (MSI_SMPCache*)*cacheIter;
if(otherCache->getCPUId() == this->getCPUId()){
/*We don't snoop ourselves*/
continue;
}
/*Get the line from the current other cache*/
MSI_SMPCacheState* otherState =
(MSI_SMPCacheState *)otherCache->cache->findLine(addr);
/*if it is cached by otherCache*/
if(otherState && otherState->isValid()){
/*Invalidate the line, because we're writing*/
otherState->invalidate();
/*The reply contains data, so "empty" is false*/
empty = false;
}
}/*done with other caches*/
/*Empty=true indicates that no other cache
*had the line or there were no other caches
*
*This data in this object is not used as is,
*but it might be useful if you plan to extend
*this simulator, so i left it in.
*/
return MSI_SMPCache::InvalidateReply(empty);
}
void MSI_SMPCache::writeLine(uint32_t wrPC, uint32_t addr){
/*This method implements actions taken when instruction wrPC
*writes to memory location addr*/
/*Find the line to which this address maps*/
MSI_SMPCacheState * st = (MSI_SMPCacheState *)cache->findLine(addr);
/*
*If the tags didn't match, or the line was invalid, it is a
*write miss
*/
if(!st || (st && !(st->isValid())) ){
numWriteMisses++;
if(st){
/*We're writing to an invalid line*/
numWriteOnInvalidMisses++;
}
/*
* Let the other caches snoop this write access and update their
* state accordingly. This action is effectively putting the write
* on the bus.
*/
MSI_SMPCache::InvalidateReply inv_ack = writeRemoteAction(addr);
numInvalidatesSent++;
/*Fill the line with the new written block*/
fillLine(addr,MSI_MODIFIED);
return;
}else if(st->getState() == MSI_SHARED){
/*If the block is shared and we're writing, we've incurred a coherence
*miss. We need to upgrade to Modified to write, and all other
*copies must be invalidated
*/
numWriteMisses++;
/*Write-on-shared Coherence Misses*/
numWriteOnSharedMisses++;
/*Let the other sharers snoop this write, and invalidate themselves*/
MSI_SMPCache::InvalidateReply inv_ack = writeRemoteAction(addr);
numInvalidatesSent++;
/*Change the state of the line to Modified to reflect the write*/
st->changeStateTo(MSI_MODIFIED);
return;
}else{ //Write Hit
/*Already have it writable: No coherence action required!*/
numWriteHits++;
return;
}
}
char *MSI_SMPCache::Identify(){
return (char *)"MSI Cache Coherence";
}
MSI_SMPCache::~MSI_SMPCache(){
}
extern "C" SMPCache *Create(int num, std::vector<SMPCache*> *cvec, int csize, int casso, int bs, int addrble, const char *repl, bool skw){
return new MSI_SMPCache(num,cvec,csize,casso,bs,addrble,repl,skw);
}