-
Notifications
You must be signed in to change notification settings - Fork 16
/
CacheCore.h
403 lines (316 loc) · 10.6 KB
/
CacheCore.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
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
/*
SESC: Super ESCalar simulator
Copyright (C) 2003 University of Illinois.
Contributed by Jose Renau
Basilio Fraguela
James Tuck
Milos Prvulovic
Smruti Sarangi
This file is part of SESC.
SESC is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2, or (at your option) any later version.
SESC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
SESC; see the file COPYING. If not, write to the Free Software Foundation, 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef CACHECORE_H
#define CACHECORE_H
//#include "GEnergy.h" //NEED TO GET RID OF THIS SOMEHOW
#include "nanassert.h"
#include "Snippets.h"
//#include "GStats.h" //NEED TO GET RID OF THIS SOMEHOW
#define CBLKSZ 32
#define CASSOC 8
#define CSIZE 32767
#define CREPLPOLICY "RANDOM"
enum ReplacementPolicy {LRU, RANDOM};
#ifdef SESC_ENERGY
template<class State, class Addr_t = uint32_t, bool Energy=true>
#else
template<class State, class Addr_t = uint32_t, bool Energy=false>
#endif
class CacheGeneric {
private:
static const int32_t STR_BUF_SIZE=1024;
//static PowerGroup getRightStat(const char* type);
protected:
const uint32_t size;
const uint32_t lineSize;
const uint32_t addrUnit; //Addressable unit: for most caches = 1 byte
const uint32_t assoc;
const uint32_t log2Assoc;
const uint64_t log2AddrLs;
const uint64_t maskAssoc;
const uint32_t sets;
const uint32_t maskSets;
const uint32_t numLines;
//GStatsEnergy *rdEnergy[2]; // 0 hit, 1 miss
//GStatsEnergy *wrEnergy[2]; // 0 hit, 1 miss
bool goodInterface;
public:
class CacheLine : public State {
public:
// Pure virtual class defines interface
//
// Tag included in state. Accessed through:
//
// Addr_t getTag() const;
// void setTag(Addr_t a);
// void clearTag();
//
//
// bool isValid() const;
// void invalidate();
//
// bool isLocked() const;
};
// findLine returns a cache line that has tag == addr, NULL otherwise
virtual CacheLine *findLinePrivate(Addr_t addr)=0;
protected:
CacheGeneric(uint32_t s, uint32_t a, uint32_t b, uint32_t u)
: size(s)
,lineSize(b)
,addrUnit(u)
,assoc(a)
,log2Assoc(log2i(a))
,log2AddrLs(log2i(b/u))
,maskAssoc(a-1)
,sets((s/b)/a)
,maskSets(sets-1)
,numLines(s/b)
{
// TODO : assoc and sets must be a power of 2
}
virtual ~CacheGeneric() {}
//GStatsEnergy *getEnergy(const char *section, PowerGroup grp, const char *format, const char *name);
void createStats(const char *section, const char *name);
public:
// Do not use this interface, use other create
static CacheGeneric<State, Addr_t, Energy> *create(int32_t size, int32_t assoc, int32_t blksize, int32_t addrUnit, const char *pStr, bool skew);
static CacheGeneric<State, Addr_t, Energy> *create(const char *section, const char *append, const char *format, ...);
void destroy() {
delete this;
}
// If there are not free lines, it would return an existing cache line unless
// all the possible cache lines are locked State must implement isLocked API
//
// when locked parameter is false, it would try to remove even locked lines
virtual CacheLine *findLine2Replace(Addr_t addr, bool ignoreLocked=false)=0;
virtual CacheLine *findInvalidLine2Replace(Addr_t addr, bool ignoreLocked=false){
return findLine2Replace(addr,ignoreLocked);
}
// TO DELETE if flush from Cache.cpp is cleared. At least it should have a
// cleaner interface so that Cache.cpp does not touch the internals.
//
// Access the line directly without checking TAG
virtual CacheLine *getPLine(uint32_t l) = 0;
//ALL USERS OF THIS CLASS PLEASE READ:
//
//readLine and writeLine MUST have the same functionality as findLine. The only
//difference is that readLine and writeLine update power consumption
//statistics. So, only use these functions when you want to model a physical
//read or write operation.
// Use this is for debug checks. Otherwise, a bad interface can be detected
CacheLine *findLineDebug(Addr_t addr) {
IS(goodInterface=true);
CacheLine *line = findLine(addr);
IS(goodInterface=false);
return line;
}
// Use this when you need to change the line state but
// do not want to account for energy
CacheLine *findLineNoEffect(Addr_t addr) {
IS(goodInterface=true);
CacheLine *line = findLine(addr);
IS(goodInterface=false);
return line;
}
CacheLine *findLine(Addr_t addr) {
return findLinePrivate(addr);
}
CacheLine *readLine(Addr_t addr) {
IS(goodInterface=true);
CacheLine *line = findLine(addr);
IS(goodInterface=false);
return line;
//rdEnergy[line != 0 ? 0 : 1]->inc();
}
CacheLine *writeLine(Addr_t addr) {
IS(goodInterface=true);
CacheLine *line = findLine(addr);
IS(goodInterface=false);
if(!Energy)
return line;
//wrEnergy[line != 0 ? 0 : 1]->inc();
return line;
}
CacheLine *fillLine(Addr_t addr) {
CacheLine *l = findLine2Replace(addr);
if (l==0)
return 0;
l->setTag(calcTag(addr));
return l;
}
CacheLine *fillLine(Addr_t addr, Addr_t &rplcAddr, bool ignoreLocked=false) {
CacheLine *l = findLine2Replace(addr, ignoreLocked);
rplcAddr = 0;
if (l==0)
return 0;
Addr_t newTag = calcTag(addr);
if (l->isValid()) {
Addr_t curTag = l->getTag();
if (curTag != newTag) {
rplcAddr = calcAddr4Tag(curTag);
}
}
l->setTag(newTag);
return l;
}
uint32_t getLineSize() const { return lineSize; }
uint32_t getAssoc() const { return assoc; }
uint32_t getLog2AddrLs() const { return log2AddrLs; }
uint32_t getLog2Assoc() const { return log2Assoc; }
uint32_t getMaskSets() const { return maskSets; }
uint32_t getNumLines() const { return numLines; }
uint32_t getNumSets() const { return sets; }
Addr_t calcTag(Addr_t addr) const { return (addr >> log2AddrLs); }
uint32_t calcSet4Tag(Addr_t tag) const { return (tag & maskSets); }
uint32_t calcSet4Addr(Addr_t addr) const { return calcSet4Tag(calcTag(addr)); }
uint32_t calcIndex4Set(uint32_t set) const { return (set << log2Assoc); }
uint32_t calcIndex4Tag(uint32_t tag) const { return calcIndex4Set(calcSet4Tag(tag)); }
uint32_t calcIndex4Addr(Addr_t addr) const { return calcIndex4Set(calcSet4Addr(addr)); }
Addr_t calcAddr4Tag(Addr_t tag) const { return (tag << log2AddrLs); }
};
#ifdef SESC_ENERGY
template<class State, class Addr_t = uint32_t, bool Energy=true>
#else
template<class State, class Addr_t = uint32_t, bool Energy=false>
#endif
class CacheAssoc : public CacheGeneric<State, Addr_t, Energy> {
using CacheGeneric<State, Addr_t, Energy>::numLines;
using CacheGeneric<State, Addr_t, Energy>::assoc;
using CacheGeneric<State, Addr_t, Energy>::maskAssoc;
using CacheGeneric<State, Addr_t, Energy>::goodInterface;
private:
public:
typedef typename CacheGeneric<State, Addr_t, Energy>::CacheLine Line;
protected:
Line *mem;
Line **content;
ushort irand;
ReplacementPolicy policy;
friend class CacheGeneric<State, Addr_t, Energy>;
CacheAssoc(int32_t size, int32_t assoc, int32_t blksize, int32_t addrUnit, const char *pStr);
Line *findLinePrivate(Addr_t addr);
public:
virtual ~CacheAssoc() {
delete [] content;
delete [] mem;
}
// TODO: do an iterator. not this junk!!
Line *getPLine(uint32_t l) {
// Lines [l..l+assoc] belong to the same set
I(l<numLines);
return content[l];
}
Line *findLine2Replace(Addr_t addr, bool ignoreLocked=false);
Line *findInvalidLine2Replace(Addr_t addr, bool ignoreLocked=false);
};
#ifdef SESC_ENERGY
template<class State, class Addr_t = uint32_t, bool Energy=true>
#else
template<class State, class Addr_t = uint32_t, bool Energy=false>
#endif
class CacheDM : public CacheGeneric<State, Addr_t, Energy> {
using CacheGeneric<State, Addr_t, Energy>::numLines;
using CacheGeneric<State, Addr_t, Energy>::goodInterface;
private:
public:
typedef typename CacheGeneric<State, Addr_t, Energy>::CacheLine Line;
protected:
Line *mem;
Line **content;
friend class CacheGeneric<State, Addr_t, Energy>;
CacheDM(int32_t size, int32_t blksize, int32_t addrUnit, const char *pStr);
Line *findLinePrivate(Addr_t addr);
public:
virtual ~CacheDM() {
delete [] content;
delete [] mem;
};
// TODO: do an iterator. not this junk!!
Line *getPLine(uint32_t l) {
// Lines [l..l+assoc] belong to the same set
I(l<numLines);
return content[l];
}
Line *findLine2Replace(Addr_t addr, bool ignoreLocked=false);
};
#ifdef SESC_ENERGY
template<class State, class Addr_t = uint32_t, bool Energy=true>
#else
template<class State, class Addr_t = uint32_t, bool Energy=false>
#endif
class CacheDMSkew : public CacheGeneric<State, Addr_t, Energy> {
using CacheGeneric<State, Addr_t, Energy>::numLines;
using CacheGeneric<State, Addr_t, Energy>::goodInterface;
private:
public:
typedef typename CacheGeneric<State, Addr_t, Energy>::CacheLine Line;
protected:
Line *mem;
Line **content;
friend class CacheGeneric<State, Addr_t, Energy>;
CacheDMSkew(int32_t size, int32_t blksize, int32_t addrUnit, const char *pStr);
Line *findLinePrivate(Addr_t addr);
public:
virtual ~CacheDMSkew() {
delete [] content;
delete [] mem;
};
// TODO: do an iterator. not this junk!!
Line *getPLine(uint32_t l) {
// Lines [l..l+assoc] belong to the same set
I(l<numLines);
return content[l];
}
Line *findLine2Replace(Addr_t addr, bool ignoreLocked=false);
};
template<class Addr_t=uint32_t>
class StateGeneric {
private:
Addr_t tag;
protected:
unsigned state;
public:
virtual unsigned getState() const {
return state;
}
virtual ~StateGeneric() {
tag = 0;
}
Addr_t getTag() const { return tag; }
void setTag(Addr_t a) {
I(a);
tag = a;
}
void clearTag() { tag = 0; }
void initialize(void *c) {
clearTag();
}
virtual bool isValid() const { return tag; }
virtual void invalidate() { clearTag(); }
virtual bool isLocked() const {
return false;
}
virtual void dump(const char *str) {
}
};
#ifndef CACHECORE_CPP
#include "CacheCore.cpp"
#endif
#endif // CACHECORE_H