-
Notifications
You must be signed in to change notification settings - Fork 16
/
CacheCore.cpp
527 lines (430 loc) · 13.5 KB
/
CacheCore.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
SESC: Super ESCalar simulator
Copyright (C) 2003 University of Illinois.
Contributed by Jose Renau
Basilio Fraguela
James Tuck
Smruti Sarangi
Luis Ceze
Karin Strauss
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.
*/
#define CACHECORE_CPP
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include "CacheCore.h"
#define k_RANDOM "RANDOM"
#define k_LRU "LRU"
//
// Class CacheGeneric, the combinational logic of Cache
//
template<class State, class Addr_t, bool Energy>
CacheGeneric<State, Addr_t, Energy> *CacheGeneric<State, Addr_t, Energy>::create(int32_t size, int32_t assoc, int32_t bsize, int32_t addrUnit, const char *pStr, bool skew)
{
CacheGeneric *cache;
if (skew) {
I(assoc=1); // Skew cache should be direct map
cache = new CacheDMSkew<State, Addr_t, Energy>(size, bsize, addrUnit, pStr);
}else if (assoc==1) {
// Direct Map cache
cache = new CacheDM<State, Addr_t, Energy>(size, bsize, addrUnit, pStr);
}else if(size == (assoc * bsize)) {
// TODO: Fully assoc can use STL container for speed
cache = new CacheAssoc<State, Addr_t, Energy>(size, assoc, bsize, addrUnit, pStr);
}else{
// Associative Cache
cache = new CacheAssoc<State, Addr_t, Energy>(size, assoc, bsize, addrUnit, pStr);
}
I(cache);
return cache;
}
template<class State, class Addr_t, bool Energy>
void CacheGeneric<State, Addr_t, Energy>::createStats(const char *section, const char *name)
{
}
template<class State, class Addr_t, bool Energy>
CacheGeneric<State, Addr_t, Energy> *CacheGeneric<State, Addr_t, Energy>::create(const char *section, const char *append, const char *format, ...)
{
/*We never use this constructor in MultiCacheSim*/
CacheGeneric *cache=0;
char size[STR_BUF_SIZE];
char bsize[STR_BUF_SIZE];
char addrUnit[STR_BUF_SIZE];
char assoc[STR_BUF_SIZE];
char repl[STR_BUF_SIZE];
char skew[STR_BUF_SIZE];
snprintf(size ,STR_BUF_SIZE,"%sSize" ,append);
snprintf(bsize,STR_BUF_SIZE,"%sBsize",append);
snprintf(addrUnit,STR_BUF_SIZE,"%sAddrUnit",append);
snprintf(assoc,STR_BUF_SIZE,"%sAssoc",append);
snprintf(repl ,STR_BUF_SIZE,"%sReplPolicy",append);
snprintf(skew ,STR_BUF_SIZE,"%sSkew",append);
int32_t s = CSIZE;
int32_t a = CASSOC;
int32_t b = CBLKSZ;
bool sk = false;
int32_t u;
u = 1;
const char *pStr = CREPLPOLICY;
cache = create(s, a, b, u, pStr, sk);
char cacheName[STR_BUF_SIZE];
{
va_list ap;
va_start(ap, format);
vsprintf(cacheName, format, ap);
va_end(ap);
}
cache->createStats(section, cacheName);
return cache;
}
/*********************************************************
* CacheAssoc
*********************************************************/
template<class State, class Addr_t, bool Energy>
CacheAssoc<State, Addr_t, Energy>::CacheAssoc(int32_t size, int32_t assoc, int32_t blksize, int32_t addrUnit, const char *pStr)
: CacheGeneric<State, Addr_t, Energy>(size, assoc, blksize, addrUnit)
{
I(numLines>0);
if (strcasecmp(pStr, k_RANDOM) == 0)
policy = RANDOM;
else if (strcasecmp(pStr, k_LRU) == 0)
policy = LRU;
else {
MSG("Invalid cache policy [%s]",pStr);
exit(0);
}
mem = new Line [numLines + 1];
content = new Line* [numLines + 1];
for(uint32_t i = 0; i < numLines; i++) {
mem[i].initialize(this);
mem[i].invalidate();
content[i] = &mem[i];
}
irand = 0;
}
template<class State, class Addr_t, bool Energy>
typename CacheAssoc<State, Addr_t, Energy>::Line *CacheAssoc<State, Addr_t, Energy>::findLinePrivate(Addr_t addr)
{
Addr_t tag = calcTag(addr);
GI(Energy, goodInterface); // If modeling energy. Do not use this
// interface directly. use readLine and
// writeLine instead. If it is called
// inside debugging only use
// findLineDebug instead
Line **theSet = &content[calcIndex4Tag(tag)];
// Check most typical case
if ((*theSet)->getTag() == tag) {
//this assertion is not true for SMP; it is valid to return invalid line
#if !defined(SESC_SMP) && !defined(SESC_CRIT)
I((*theSet)->isValid());
#endif
return *theSet;
}
Line **lineHit=0;
Line **setEnd = theSet + assoc;
// For sure that position 0 is not (short-cut)
{
Line **l = theSet + 1;
while(l < setEnd) {
if ((*l)->getTag() == tag) {
lineHit = l;
break;
}
l++;
}
}
if (lineHit == 0)
return 0;
I((*lineHit)->isValid());
// No matter what is the policy, move lineHit to the *theSet. This
// increases locality
Line *tmp = *lineHit;
{
Line **l = lineHit;
while(l > theSet) {
Line **prev = l - 1;
*l = *prev;;
l = prev;
}
*theSet = tmp;
}
return tmp;
}
template<class State, class Addr_t, bool Energy>
typename CacheAssoc<State, Addr_t, Energy>::Line
*CacheAssoc<State, Addr_t, Energy>::findInvalidLine2Replace(Addr_t addr, bool ignoreLocked)
{
Addr_t tag = calcTag(addr);
Line **theSet = &content[calcIndex4Tag(tag)];
// Check most typical case
if ((*theSet)->getTag() == tag ) {
//GI(tag,(*theSet)->isValid());
return *theSet;
}
Line **lineHit=0;
Line **lineFree=0; // Order of preference, invalid, locked
Line **setEnd = theSet + assoc;
// Start in reverse order so that get the youngest invalid possible,
// and the oldest isLocked possible (lineFree)
{
Line **l = setEnd -1;
while(l >= theSet) {
if ((*l)->getTag() == tag && !(*l)->isValid()) {
lineHit = l;
break;
}
if (!(*l)->isValid())
lineFree = l;
else if (lineFree == 0 && !(*l)->isLocked())
lineFree = l;
// If line is invalid, isLocked must be false
GI(!(*l)->isValid(), !(*l)->isLocked());
l--;
}
}
GI(lineFree, !(*lineFree)->isValid() || !(*lineFree)->isLocked());
if (lineHit)
return *lineHit;
I(lineHit==0);
if(lineFree == 0 && !ignoreLocked)
return 0;
if (lineFree == 0) {
I(ignoreLocked);
if (policy == RANDOM) {
lineFree = &theSet[irand];
irand = (irand + 1) & maskAssoc;
}else{
I(policy == LRU);
// Get the oldest line possible
lineFree = setEnd-1;
}
}else if(ignoreLocked) {
if (policy == RANDOM && (*lineFree)->isValid()) {
lineFree = &theSet[irand];
irand = (irand + 1) & maskAssoc;
}else{
// I(policy == LRU);
// Do nothing. lineFree is the oldest
}
}
I(lineFree);
GI(!ignoreLocked, !(*lineFree)->isValid() || !(*lineFree)->isLocked());
if (lineFree == theSet)
return *lineFree; // Hit in the first possition
// No matter what is the policy, move lineHit to the *theSet. This
// increases locality
Line *tmp = *lineFree;
{
Line **l = lineFree;
while(l > theSet) {
Line **prev = l - 1;
*l = *prev;;
l = prev;
}
*theSet = tmp;
}
return tmp;
}
template<class State, class Addr_t, bool Energy>
typename CacheAssoc<State, Addr_t, Energy>::Line
*CacheAssoc<State, Addr_t, Energy>::findLine2Replace(Addr_t addr, bool ignoreLocked)
{
Addr_t tag = calcTag(addr);
Line **theSet = &content[calcIndex4Tag(tag)];
// Check most typical case
if ((*theSet)->getTag() == tag) {
GI(tag,(*theSet)->isValid());
return *theSet;
}
Line **lineHit=0;
Line **lineFree=0; // Order of preference, invalid, locked
Line **setEnd = theSet + assoc;
// Start in reverse order so that get the youngest invalid possible,
// and the oldest isLocked possible (lineFree)
{
Line **l = setEnd -1;
while(l >= theSet) {
if ((*l)->getTag() == tag) {
lineHit = l;
break;
}
if (!(*l)->isValid())
lineFree = l;
else if (lineFree == 0 && !(*l)->isLocked())
lineFree = l;
// If line is invalid, isLocked must be false
GI(!(*l)->isValid(), !(*l)->isLocked());
l--;
}
}
GI(lineFree, !(*lineFree)->isValid() || !(*lineFree)->isLocked());
if (lineHit)
return *lineHit;
I(lineHit==0);
if(lineFree == 0 && !ignoreLocked)
return 0;
if (lineFree == 0) {
I(ignoreLocked);
if (policy == RANDOM) {
lineFree = &theSet[irand];
irand = (irand + 1) & maskAssoc;
}else{
I(policy == LRU);
// Get the oldest line possible
lineFree = setEnd-1;
}
}else if(ignoreLocked) {
if (policy == RANDOM && (*lineFree)->isValid()) {
lineFree = &theSet[irand];
irand = (irand + 1) & maskAssoc;
}else{
// I(policy == LRU);
// Do nothing. lineFree is the oldest
}
}
I(lineFree);
GI(!ignoreLocked, !(*lineFree)->isValid() || !(*lineFree)->isLocked());
if (lineFree == theSet)
return *lineFree; // Hit in the first possition
// No matter what is the policy, move lineHit to the *theSet. This
// increases locality
Line *tmp = *lineFree;
{
Line **l = lineFree;
while(l > theSet) {
Line **prev = l - 1;
*l = *prev;;
l = prev;
}
*theSet = tmp;
}
return tmp;
}
/*********************************************************
* CacheDM
*********************************************************/
template<class State, class Addr_t, bool Energy>
CacheDM<State, Addr_t, Energy>::CacheDM(int32_t size, int32_t blksize, int32_t addrUnit, const char *pStr)
: CacheGeneric<State, Addr_t, Energy>(size, 1, blksize, addrUnit)
{
I(numLines>0);
mem = new Line[numLines + 1];
content = new Line* [numLines + 1];
for(uint32_t i = 0; i < numLines; i++) {
mem[i].initialize(this);
mem[i].invalidate();
content[i] = &mem[i];
}
}
template<class State, class Addr_t, bool Energy>
typename CacheDM<State, Addr_t, Energy>::Line *CacheDM<State, Addr_t, Energy>::findLinePrivate(Addr_t addr)
{
Addr_t tag = calcTag(addr);
GI(Energy, goodInterface); // If modeling energy. Do not use this
// interface directly. use readLine and
// writeLine instead. If it is called
// inside debugging only use
// findLineDebug instead
Line *line = content[calcIndex4Tag(tag)];
if (line->getTag() == tag) {
I(line->isValid());
return line;
}
return 0;
}
template<class State, class Addr_t, bool Energy>
typename CacheDM<State, Addr_t, Energy>::Line
*CacheDM<State, Addr_t, Energy>::findLine2Replace(Addr_t addr, bool ignoreLocked)
{
Addr_t tag = calcTag(addr);
Line *line = content[calcIndex4Tag(tag)];
if (ignoreLocked)
return line;
if (line->getTag() == tag) {
GI(tag,line->isValid());
return line;
}
if (line->isLocked())
return 0;
return line;
}
/*********************************************************
* CacheDMSkew
*********************************************************/
template<class State, class Addr_t, bool Energy>
CacheDMSkew<State, Addr_t, Energy>::CacheDMSkew(int32_t size, int32_t blksize, int32_t addrUnit, const char *pStr)
: CacheGeneric<State, Addr_t, Energy>(size, 1, blksize, addrUnit)
{
I(numLines>0);
mem = new Line[numLines + 1];
content = new Line* [numLines + 1];
for(uint32_t i = 0; i < numLines; i++) {
mem[i].initialize(this);
mem[i].invalidate();
content[i] = &mem[i];
}
}
template<class State, class Addr_t, bool Energy>
typename CacheDMSkew<State, Addr_t, Energy>::Line *CacheDMSkew<State, Addr_t, Energy>::findLinePrivate(Addr_t addr)
{
Addr_t tag = calcTag(addr);
GI(Energy, goodInterface); // If modeling energy. Do not use this
// interface directly. use readLine and
// writeLine instead. If it is called
// inside debugging only use
// findLineDebug instead
Line *line = content[calcIndex4Tag(tag)];
if (line->getTag() == tag) {
I(line->isValid());
return line;
}
// BEGIN Skew cache
Addr_t tag2 = calcTag(addr ^ (addr>>7));
line = content[calcIndex4Tag(tag2)];
if (line->getTag() == tag) {
I(line->isValid());
return line;
}
// END Skew cache
return 0;
}
template<class State, class Addr_t, bool Energy>
typename CacheDMSkew<State, Addr_t, Energy>::Line
*CacheDMSkew<State, Addr_t, Energy>::findLine2Replace(Addr_t addr, bool ignoreLocked)
{
Addr_t tag = calcTag(addr);
Line *line = content[calcIndex4Tag(tag)];
if (ignoreLocked)
return line;
if (line->getTag() == tag) {
GI(tag,line->isValid());
return line;
}
if (line->isLocked())
return 0;
// BEGIN Skew cache
Addr_t tag2 = calcTag(addr ^ (addr>>7));
Line *line2 = content[calcIndex4Tag(tag2)];
if (line2->getTag() == tag) {
I(line2->isValid());
return line2;
}
static int32_t rand_number =0;
if (rand_number++ & 1)
return line;
else
return line2;
// END Skew cache
return line;
}