-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplacement_state.cpp
609 lines (488 loc) · 23.7 KB
/
replacement_state.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/mman.h>
#include <map>
#include <iostream>
using namespace std;
#include "replacement_state.h"
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// //
// This file is distributed as part of the Cache Replacement Championship //
// workshop held in conjunction with ISCA'2010. //
// //
// //
// Everyone is granted permission to copy, modify, and/or re-distribute //
// this software. //
// //
// Please contact Aamer Jaleel <[email protected]> should you have any //
// questions //
// //
////////////////////////////////////////////////////////////////////////////////
/*
** This file implements the cache replacement state. Users can enhance the code
** below to develop their cache replacement ideas.
**
*/
static Addr_t primes[TOTAL_PREDICT_TABLES][3] =
{
{997,359,57},
{521,131,73},
{337,677,17}
}; // a table of primes for hashing function
////////////////////////////////////////////////////////////////////////////////
// The replacement state constructor: //
// Inputs: number of sets, associativity, and replacement policy to use //
// Outputs: None //
// //
// DO NOT CHANGE THE CONSTRUCTOR PROTOTYPE //
// //
////////////////////////////////////////////////////////////////////////////////
CACHE_REPLACEMENT_STATE::CACHE_REPLACEMENT_STATE( UINT32 _sets, UINT32 _assoc, UINT32 _pol )
{
numsets = _sets;
assoc = _assoc;
replPolicy = _pol;
mytimer = 0;
InitReplacementState();
}
////////////////////////////////////////////////////////////////////////////////
// //
// The function prints the statistics for the cache //
// //
////////////////////////////////////////////////////////////////////////////////
ostream & CACHE_REPLACEMENT_STATE::PrintStats(ostream &out)
{
out<<"=========================================================="<<endl;
out<<"=========== Replacement Policy Statistics ================"<<endl;
out<<"=========================================================="<<endl;
// CONTESTANTS: Insert your statistics printing here
return out;
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function initializes the replacement policy hardware by creating //
// storage for the replacement state on a per-line/per-cache basis. //
// //
////////////////////////////////////////////////////////////////////////////////
void CACHE_REPLACEMENT_STATE::InitReplacementState()
{
// Create the state for sets, then create the state for the ways
repl = new LINE_REPLACEMENT_STATE* [ numsets ];
// ensure that we were able to create replacement state:
assert(repl);
// Create the state for the sets
for(UINT32 setIndex=0; setIndex<numsets; setIndex++)
{
repl[ setIndex ] = new LINE_REPLACEMENT_STATE[ assoc ];
for(UINT32 way=0; way<assoc; way++)
{
// initialize stack position (for true LRU)
repl[ setIndex ][ way ].LRUstackposition = way;
// initalize dead indication bit (for dead block sampler)
repl[ setIndex ][ way ].dead = false;
}
}
if (replPolicy != CRC_REPL_CONTESTANT) return;
// Set the baseline replacement policy for LLC, 0 -- LRU, 1 -- random
blreplPolicy = 0;
// Set the previous pc:
prev_PC = 0;
// Create the sampler, which is basically a cache
sampler = new SAMPLER();
// ensure that we were able to create replacement state:
assert(sampler);
// Create the sets for the samplers:
sampler->sets = new m_set[TOTAL_SAMPLE_SETS];
sampler->nsets = TOTAL_SAMPLE_SETS;
sampler->assoc = SAMPLER_ASSOC;
sampler->replacement_policy = SAMPLER_REPLACEMENT_POLICY;
// Create the predictor, which is basically a 2-bit saturated counter table:
predictor = new PREDICTOR* [TOTAL_PREDICT_TABLES];
assert(predictor);
// Initialize the counters in the table as zero:
for(UINT32 table = 0; table < TOTAL_PREDICT_TABLES; ++table){
predictor[ table ] = new PREDICTOR[ numsets ];
for(UINT32 row = 0; row < numsets; ++row)
predictor[ table ][ row ].cnt = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function is called by the cache on every cache miss. The input //
// argument is the set index. The return value is the physical way //
// index for the line being replaced. //
// //
////////////////////////////////////////////////////////////////////////////////
INT32 CACHE_REPLACEMENT_STATE::GetVictimInSet( UINT32 tid, UINT32 setIndex, const LINE_STATE *vicSet, UINT32 assoc, Addr_t PC, Addr_t paddr, UINT32 accessType ) {
// If no invalid lines, then replace based on replacement policy
if( replPolicy == CRC_REPL_LRU )
{
return Get_LRU_Victim( setIndex );
}
else if( replPolicy == CRC_REPL_RANDOM )
{
return Get_Random_Victim( setIndex );
}
else if( replPolicy == CRC_REPL_CONTESTANT )
{
// Contestants: ADD YOUR VICTIM SELECTION FUNCTION HERE
return Get_My_Victim (setIndex, vicSet, assoc, PC, paddr, accessType);
}
// We should never here here
assert(0);
return -1;
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function is called by the cache after every cache hit/miss //
// The arguments are: the set index, the physical way of the cache, //
// the pointer to the physical line (should contestants need access //
// to information of the line filled or hit upon), the thread id //
// of the request, the PC of the request, the accesstype, and finall //
// whether the line was a cachehit or not (cacheHit=true implies hit) //
// //
////////////////////////////////////////////////////////////////////////////////
void CACHE_REPLACEMENT_STATE::UpdateReplacementState(
UINT32 setIndex, INT32 updateWayID, const LINE_STATE *currLine,
UINT32 tid, Addr_t PC, UINT32 accessType, bool cacheHit )
{
// What replacement policy?
if( replPolicy == CRC_REPL_LRU )
{
UpdateLRU( setIndex, updateWayID );
}
else if( replPolicy == CRC_REPL_RANDOM )
{
// Random replacement requires no replacement state update
}
else if( replPolicy == CRC_REPL_CONTESTANT )
{
// Contestants: ADD YOUR UPDATE REPLACEMENT STATE FUNCTION HERE
// Update my policy according to the paper:
UpdateMyPolicy( setIndex, updateWayID, currLine, PC, accessType, cacheHit );
}
}
////////////////////////////////////////////////////////////////////////////////
// //
//////// HELPER FUNCTIONS FOR REPLACEMENT UPDATE AND VICTIM SELECTION //////////
// //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// //
// This function finds the LRU victim in the cache set by returning the //
// cache block at the bottom of the LRU stack. Top of LRU stack is '0' //
// while bottom of LRU stack is 'assoc-1' //
// //
////////////////////////////////////////////////////////////////////////////////
INT32 CACHE_REPLACEMENT_STATE::Get_LRU_Victim( UINT32 setIndex )
{
// Get pointer to replacement state of current set
LINE_REPLACEMENT_STATE *replSet = repl[ setIndex ];
INT32 lruWay = 0;
// Search for victim whose stack position is assoc-1
for(UINT32 way=0; way<assoc; way++) {
if (replSet[way].LRUstackposition == (assoc-1)) {
lruWay = way;
break;
}
}
// return lru way
return lruWay;
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function finds a random victim in the cache set //
// //
////////////////////////////////////////////////////////////////////////////////
INT32 CACHE_REPLACEMENT_STATE::Get_Random_Victim( UINT32 setIndex )
{
INT32 way = (rand() % assoc);
return way;
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function implements the LRU update routine for the traditional //
// LRU replacement policy. The arguments to the function are the physical //
// way and set index. //
// //
////////////////////////////////////////////////////////////////////////////////
void CACHE_REPLACEMENT_STATE::UpdateLRU( UINT32 setIndex, INT32 updateWayID )
{
// Determine current LRU stack position
UINT32 currLRUstackposition = repl[ setIndex ][ updateWayID ].LRUstackposition;
// Update the stack position of all lines before the current line
// Update implies incremeting their stack positions by one
for(UINT32 way=0; way<assoc; way++) {
if( repl[setIndex][way].LRUstackposition < currLRUstackposition ) {
repl[setIndex][way].LRUstackposition++;
}
}
// Set the LRU stack position of new line to be zero
repl[ setIndex ][ updateWayID ].LRUstackposition = 0;
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function implements the dead block sampling replacement policy //
// The arguments to the function are set index, set tag, associtivity, //
// PC, physical memory address and the accessType //
// way and set index. //
// //
////////////////////////////////////////////////////////////////////////////////
INT32 CACHE_REPLACEMENT_STATE::Get_My_Victim(
UINT32 setIndex, const LINE_STATE *vicSet,
UINT32 assoc, Addr_t PC, Addr_t paddr, UINT32 accessType )
{
// Implement the bypassing policy here, if predicted dead, simply ignore!
// Try to use the different threshold for bypassing!
// Also ignore the writeback because of its irregular activity:
if(GetPrediction(PC, 8) || accessType == ACCESS_WRITEBACK)
return -1;
// Get pointer to the replacement state of the current set
// to find out whether or not there is block in LLC is predicted to be dead
LINE_REPLACEMENT_STATE *replSet = repl[ setIndex ];
// Find out if there is any dead blocks in LLC:
UINT32 llc_way;
for(llc_way = 0; llc_way < assoc; ++llc_way){
// always pickup the first predicted dead block
if(replSet[llc_way].dead)
break;
}
// assume no dead block is detected:
dead_block_detection = false;
// no block is predicted dead, use lru as default for LLC
if(llc_way == assoc){
return Get_LRU_Victim( setIndex );
}
else{
// if dead block is detected, return its way
dead_block_detection = true;
return (INT32)llc_way;
}
}
void CACHE_REPLACEMENT_STATE::UpdateMyPolicy(
UINT32 setIndex, INT32 updateWayID, const LINE_STATE *currLine,
Addr_t PC, UINT32 accessType, bool cacheHit)
{
// ignore the writeback because of its irregular activity:
if(accessType == ACCESS_WRITEBACK)
return;
if(accessType == ACCESS_PREFETCH){
// previous PC is needed bc all prefetch share
// the same PC!
//prev_PC = PC;
}
// Get pointer to replacement state of the current set
// in order to update the death indication bit in the LLC
LINE_REPLACEMENT_STATE *replSet = repl[ setIndex ];
// Update the death indication bit for LLC according to the prediction table
// Use the 15-bit partial PC to index the prediction table
if(cacheHit && accessType != ACCESS_PREFETCH)
replSet[updateWayID].dead = GetPrediction(PC);
else if(!cacheHit)
replSet[updateWayID].dead = false;
// remember to update the lru stack position for LLC under LRU policy
// there are two cases:
// 1) hit at LLC
// 2) misses at LLC and the victim block is selected by LRU
if(blreplPolicy == 0){
// case 1) if hit, update the lru stack position given the updateWayID
if(cacheHit)
UpdateLRU( setIndex, updateWayID );
else{
// case 2) if no dead block is detected in LLC during miss
// then update LRU
//if(!dead_block_detection){
UpdateLRU( setIndex, updateWayID );
//}
}
}
// Need to update its prediction bit of the sampler set if accessed
// Also need to update the prediction table
// Note that I ignore the prefetch because all prefetches have the same PC!
if(setIndex % SAMPLE_GAP == 0){
AccessSampler(setIndex/SAMPLE_GAP,currLine[updateWayID].tag,PC,accessType);
}
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function is to access the sampler and determine hit or miss //
// If hit, decrease the prediction table entry index by partial PC //
// this PC can be the incoming PC or PC stored in the sampler block //
// If capacity/conflict miss, increase the prediction table entry in a //
// similar way. //
// The arguments to the functions are sampler set index, tag of the //
// incoming block and PC made this access //
// //
////////////////////////////////////////////////////////////////////////////////
void CACHE_REPLACEMENT_STATE::AccessSampler(UINT32 sampleIndex, Addr_t tag, Addr_t PC, UINT32 accessType)
{
// Get the pointer to the set in the sampler:
assert(sampleIndex < sampler-> nsets && sampleIndex >= 0);
m_set & samplerSet = sampler->sets[sampleIndex];
INT32 s_assoc = sampler->assoc;
// mask to extract the 15-bit tag coming from the LLC and PC made this access:
Addr_t mask = (1<<PARTIAL_BITS) - 1;
Addr_t fullPC = PC;
tag &= mask;
PC &= mask;
// Access the set to find out whether or not the tag is matched
INT32 i = 0;
for(i = 0; i < s_assoc; ++i){
// if hit, get the prediction for the block and decrease the prediction table
if(samplerSet.blocks[i].valid && samplerSet.blocks[i].partial_tag == tag){
// Use the previous PC to do the update:
// if the access type is prefetch, please skip the update of the tab!
if(accessType != ACCESS_PREFETCH){
UpdatePredictionTable(samplerSet.blocks[i].partial_PC, false);
samplerSet.blocks[i].prediction = GetPrediction(PC);
samplerSet.blocks[i].partial_PC = PC;
samplerSet.blocks[i].PC = fullPC;
}
// please remember to update its LRU position as well if needed!
if(sampler->replacement_policy == 0)
MoveToMRU(samplerSet, i);
// NOTE: you can choose PC/ PC of the last instruct record in this block
//UpdatePredictionTable(PC, false);
return;
}
}
// if miss
bool set_valid = samplerSet.set_valid;
if(!samplerSet.set_valid){
// is there any invalid block left ?
for(i = 0; i < s_assoc; ++i)
if(samplerSet.blocks[i].valid == false) break;
if(i == s_assoc){
// mark the sampler set as having only valid blocks so
// we don't search it again!
samplerSet.set_valid = true;
set_valid = true;
}
// at this point i indicates an invalid block
// or assoc if there is no invalid block
}
// find out the dead block in the sampler, if none is found, i should be assoc:
if(set_valid){
INT32 j;
for(j = 0; j < s_assoc; ++j){
// always find the first dead block
if(samplerSet.blocks[j].prediction){
break;
}
}
i = j;
}
if(i == s_assoc){
// this is the case when no dead block is found
assert(set_valid);
// lru as the default replacement policy for sampler
if(sampler->replacement_policy == 0){
i = s_assoc - 1;
assert(i>= 0 && i < s_assoc);
// use the evicted block signature (partial PC) to increase the table
UpdatePredictionTable(samplerSet.blocks[i].partial_PC, true);
MoveToMRU(samplerSet, i);
// place the partial tag and partial PC into the sampler
// set the valid bit as true and prediction
//if(accessType != ACCESS_PREFETCH)
PlaceSamplerSet(samplerSet, 0, tag, PC);
// if you place a faked PC, indicate that the block does not need to be updated
samplerSet.blocks[0].PC = fullPC;
}
else{ // random policy as the default replacement policy for sampler
i = rand() % s_assoc;
// use the evicted block signature (parital PC) to increase the table
UpdatePredictionTable(samplerSet.blocks[i].partial_PC, true);
PlaceSamplerSet(samplerSet, i, tag, PC);
}
}
else{
// this is the case 1) when dead block is found or 2) the block is invalid!
assert((samplerSet.blocks[i].prediction == true && samplerSet.blocks[i].valid == true)
|| (samplerSet.blocks[i].prediction == false && samplerSet.blocks[i].valid == false));
// need to update the prediction table if the block is found dead!
if(samplerSet.blocks[i].valid){
UpdatePredictionTable(samplerSet.blocks[i].partial_PC, true);
}
//MoveToMRU(samplerSet, i);
PlaceSamplerSet(samplerSet, i, tag, PC);
samplerSet.blocks[0].PC = fullPC;
}
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function is to move a block of the sampler to the MRU position //
// The arguments to the function are sampler set and //
// index of the block to be moved //
// samplerSet will be changed after this function //
// //
////////////////////////////////////////////////////////////////////////////////
void CACHE_REPLACEMENT_STATE::MoveToMRU(m_set & samplerSet, UINT32 way)
{
`
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function is to update the prediction table indexed by the hashed PC //
// The arguments to the function are partial PC and //
// a flag to indicate whether increase/decrease the table //
// //
////////////////////////////////////////////////////////////////////////////////
void CACHE_REPLACEMENT_STATE::UpdatePredictionTable(Addr_t PC, bool increase)
{
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function is to generate a prediction based on the prediction table //
// The argument to the function is the partial PC //
// The function returns the prediction results true->dead, false->not dead //
// //
////////////////////////////////////////////////////////////////////////////////
bool CACHE_REPLACEMENT_STATE::GetPrediction(Addr_t PC, const INT32 const_thresh)
{
INT32 sum = 0;
for(UINT32 table = 0; table < TOTAL_PREDICT_TABLES; ++table){
UINT32 entry = hash(PC, primes[table][0], primes[table][1], primes[table][2]);
assert(entry >=0 && entry < numsets);
assert(predictor[table][entry].cnt >=0 && predictor[table][entry].cnt < 4);
sum += predictor[table][entry].cnt;
}
return sum >= const_thresh ? true : false;
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function is to place a block into the sampler //
// The arguments to the function are sampler set and //
// index of the updated way, partial tag, and partial PC //
// samplerSet will be changed after this function //
// //
////////////////////////////////////////////////////////////////////////////////
void CACHE_REPLACEMENT_STATE::PlaceSamplerSet(m_set & samplerSet, UINT32 i, Addr_t tag, Addr_t PC)
{
assert(i >= 0 && i < SAMPLER_ASSOC);
// set the valid bit as true and prediction as false
assert( tag < 1<<PARTIAL_BITS && tag >= 0 && PC < 1<<PARTIAL_BITS && PC >= 0);
samplerSet.blocks[i].partial_tag = tag;
samplerSet.blocks[i].partial_PC = PC;
samplerSet.blocks[i].valid = true;
samplerSet.blocks[i].prediction = GetPrediction(PC);
}
////////////////////////////////////////////////////////////////////////////////
// //
// This function is to use three prime number to do a hashing //
// The arguments to the function are the partial PC as the key //
// and three prime numbers //
// The function returns a table entry (0 ~ numsets) //
// //
////////////////////////////////////////////////////////////////////////////////
inline
UINT32 CACHE_REPLACEMENT_STATE::hash(
Addr_t key, Addr_t a_prime, Addr_t b_prime, Addr_t p_prime)
{
}
CACHE_REPLACEMENT_STATE::~CACHE_REPLACEMENT_STATE (void) {
}