-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCache.java~
More file actions
401 lines (306 loc) · 11.8 KB
/
Cache.java~
File metadata and controls
401 lines (306 loc) · 11.8 KB
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
// Cache Project (ECE 521)
// Neal O'Hara
// ngohara @ ngohara@ncsu.edu
// 9/19/13
/*---------------------------------------------------------------------------------------------------*/
// A Cache simulator
// Needs to be variably defined in caceheSize,
// associativity: setASSOC
// blockSize
// l1_PREF_N
// l1_PREF_M
// Every cache is N-way set associative
// numSets = #cache blocks / setASSOC = cacheSize/ (blockSize * setASSOC)
// where setASSOC is the associativity assigned to the sets of this cache
// All Addresses are 32 bits,
// Addresses are addressed in hexadecimal format
// Addresses are divided into parts via
// (MSB) Tag | Index | Block offset (LSB)
// num_block_offset = log_2(blockSize)
// num_index_bits = log_2(numSets)
// num_tag_bits = 32 - num_index_bits - num_block_offset
// Caches use WBWA (write-back + write allocate)
// Write allocate means a write miss causes a block to
// be allocated in cache (in addition to read misses)
// Write back means we need dirty bits with each block in set,
// such that when block evicted, it then writes to
// lower cache or memory
// when cache writebacks, set invalid bits in stream buffer for block
// Allocation
// first: make space for requested block
// Use LRU to replace in full cache
// Use Dirty bits for write back
// second: bring in requested block
// Make a cache read call
import java.lang.Math;
public class Cache {
//cache parameters
private int cacheSize;
private int setASSOC;
private int blockSize;
private String cacheName = null;
//Sets Fields for this cache
private int numSets;
public Set[] mySets = new Set[1];
//address dividers
public int num_block_offset;
public int num_index_bits;
public int num_tag_bits;
//cache statistics
public long read_count = 0;
public long read_miss = 0;
public long write_count = 0;
public long write_miss = 0;
public long writeback_count = 0;
public long push_count = 0;
public long pull_count = 0;
//next tier cache
public Cache childCache = null;
public boolean debugValid = true; //set true for debug statements
//initializes the cache
public Cache(int blocksize, int size, int assoc, int prefetchN, int prefetchM, String name ){
//Using master try catch to ensure initialized properly
try{
this.cacheSize = size;
this.setASSOC = assoc;
this.blockSize = blocksize;
this.cacheName = name;
this.numSets = cacheSize/ (blockSize * setASSOC);
this.num_block_offset = log_2(blockSize);
this.num_index_bits = log_2(numSets);
this.num_tag_bits = 32 - num_index_bits - num_block_offset;
this.mySets = new Set[numSets];
for(int k=0;k<numSets;k++){
mySets[k] = new Set(setASSOC, k, num_tag_bits);
}
} catch (Exception e){
//report error
System.out.println("Cache Debug: cache did not initialize properly");
System.out.println(e.toString());
e.printStackTrace();
}
}
/*---------------------------------------------------------------------------------------------------*/
public Boolean cacheContainsBlock(int address){
int index = getSetIndex(address);
Boolean hit = mySets[index].containsBlock(address);
//check PStream here
//if in PStream, invalid block and move to cache
return hit;
}
/*---------------------------------------------------------------------------------------------------*/
public void allocateSpace(int address){
int index = getSetIndex(address);
Boolean full = mySets[index].isFull();
if(full){//Is full, allocate space
Block writeBack = mySets[index].removeLRU();
//handel writeback
Boolean dirtyBit = writeBack.isDirty();
if(dirtyBit){
//actual writeback here
writeback_count ++;
if(childCache == null){
//null, so hard memory, ie throwout block
if(debugValid){//Debug print statement
Block print = new Block(writeBack.getAddress());
int masked = writeBack.getAddress() & (~(int)(Math.pow((double)2,(double)num_block_offset)-1));
Integer tag = print.getTag(num_tag_bits);
//L1 victim: 4214c9d0 (tag 210a64, index 29, dirty)
System.out.println(cacheName +" victim: "+ Integer.toHexString(masked)+" (tag "+Integer.toHexString(tag) + ", index "+index+", dirty)");
}
}
else{
if(debugValid){//Debug print statement
Block print = new Block(writeBack.getAddress());
int masked = writeBack.getAddress() & (~(int)(Math.pow((double)2,(double)num_block_offset)-1));
Integer tag = print.getTag(num_tag_bits);
//L1 victim: 4214c9d0 (tag 210a64, index 29, dirty)
System.out.println(cacheName +" victim: "+ Integer.toHexString(masked)+" (tag "+Integer.toHexString(tag) + ", index "+index+", dirty)");
}
childCache.writeMemory(writeBack.getAddress());
}
}//else ignore clean block
else{
if(debugValid){//Debug print statement
Block print = new Block(writeBack.getAddress());
int masked = writeBack.getAddress() & (~(int)(Math.pow((double)2,(double)num_block_offset)-1));
Integer tag = print.getTag(num_tag_bits);
//L1 victim: 4214c9d0 (tag 210a64, index 29, clean)
System.out.println(cacheName +" victim: "+ Integer.toHexString(masked)+" (tag "+Integer.toHexString(tag) + ", index "+index+", clean)");
}
}
}
else{ //not full, but need to shift for new MRU
mySets[index].incNonFull();
if(debugValid){//Debug print statement
//L1 victim: none
System.out.println(cacheName + " victim: none");
}
}
}
/*---------------------------------------------------------------------------------------------------*/
//either contains block, or space is allocated for block
public Block readMemory(int address){
int index = getSetIndex(address);
read_count++;
if(debugValid){//Debug print statement
Block print = new Block(address);
int masked = address & (~(int)(Math.pow((double)2,(double)num_block_offset)-1));
Integer tag = print.getTag(num_tag_bits);
//L1 read : dfcfa0 (tag 6fe7, index 26)
System.out.println(cacheName +" read : "+ Integer.toHexString(masked)+" (tag "+Integer.toHexString(tag) + ", index "+index+")");
}
Boolean hit = cacheContainsBlock(address);
//check PStream
if(hit){
if(debugValid){//Debug print statement
//L1 hit
System.out.println(cacheName + " hit");
//L1 Update LRU
System.out.println(cacheName + " Update LRU");
}
Block is_read = mySets[index].resetMRU(address);
Block pass = new Block(is_read.getAddress()); //isolate references
pass.setDirty(false); //blocks passed up are not dirty
return pass; //read hit
}
else{
if(debugValid){//Debug print statement
//L1 miss
System.out.println(cacheName + " miss");
}
//allocate space
allocateSpace(address);
read_miss++;
//next read from child
if(childCache ==null){
//hit memory, so create block and pass up
Block is_read = new Block(address);
Block pass = new Block(is_read.getAddress()); //isolate references
pass.setDirty(false); //blocks passed up are not dirty
mySets[index].addMRU( pass.getAddress(),false);//blocks passed up are not dirty
if(debugValid){//Debug print statement
//L1 Update LRU
System.out.println(cacheName + " Update LRU");
}
return pass;
}
else{
//now read from child cache, store, and return
Block is_read = childCache.readMemory(address);
Block pass = new Block(is_read.getAddress()); //isolate references
pass.setDirty(false); //blocks passed up are not dirty
mySets[index].addMRU( pass.getAddress(),false);//blocks passed up are not dirty
if(debugValid){//Debug print statement
//L1 Update LRU
System.out.println(cacheName + " Update LRU");
}
return pass;
}
}
}/// end read mem
/*---------------------------------------------------------------------------------------------------*/
public Boolean writeMemory(int address){
int index = getSetIndex(address);
write_count++;
Boolean hit = cacheContainsBlock(address);
//check PStream
if(debugValid){//Debug print statement
Block print = new Block(address);
int masked = address & (~(int)(Math.pow((double)2,(double)num_block_offset)-1));
Integer tag = print.getTag(num_tag_bits);
//L1 read : dfcfa0 (tag 6fe7, index 26)
System.out.println(cacheName +" Write : "+ Integer.toHexString(masked)+" (tag "+Integer.toHexString(tag) + ", index "+index+")");
}
if(hit){
if(debugValid){//Debug print statement
//L1 hit
System.out.println(cacheName + " hit");
//L1 Update LRU
System.out.println(cacheName + " Update LRU");
//L1 set dirty
System.out.println(cacheName + " set dirty");
}
Block to_write = mySets[index].resetMRU(address);
mySets[index].theseBlocks[0].setDirty(true); //Set now current MRU block as dirty, as it was just written to.
return true; //wrote memory
}
else{
if(debugValid){//Debug print statement
//L1 miss
System.out.println(cacheName + " miss");
}
//allocate space
allocateSpace(address);
write_miss++;
//next read from child
if(childCache ==null){
//hit memory, so create block and write to it
Block to_write = new Block(address);
to_write.setDirty(true);
mySets[index].addMRU( to_write.getAddress(), to_write.isDirty());
if(debugValid){//Debug print statement
//L1 Update LRU
System.out.println(cacheName + " Update LRU");
//L1 set dirty
System.out.println(cacheName + " set dirty");
}
return true; //wrote memory
}
else{
//now read from child cache, store, and return
Block to_write = childCache.readMemory(address);
to_write.setDirty(true);
mySets[index].addMRU( to_write.getAddress(), to_write.isDirty());
if(debugValid){//Debug print statement
//L1 Update LRU
System.out.println(cacheName + " Update LRU");
//L1 set dirty
System.out.println(cacheName + " set dirty");
}
return true; //wrote memory
}
}
} /// end write Memory
/*--------------------------------------------------------------------------------------------------*/
public int getSetIndex(int address){
/*
System.out.println("address " + address);
System.out.println("cacheSize " + cacheSize);
System.out.println("setASSOC " + setASSOC);
System.out.println("blocksize " + blockSize);
System.out.println("num_block_offset " + num_block_offset);
System.out.println("numSets " + numSets);
System.out.println("num_index_bits " + num_index_bits);
*/
int index = 0;
if(num_index_bits == 0){
index=0; //Fully associative, no index bits
}
else{
int mask = (int) Math.pow((double)2, (double) num_index_bits) -1; //to make mask of 1s for desired numer of bits
//System.out.println("mask " + mask);
int temp = address >>> num_block_offset; //shifts out offset bits
//System.out.println("temp " + temp);
index = temp & mask;//masks zeroes tag bits
}
//System.out.println("index " + index);
return index;
}
/*---------------------------------------------------------------------------------------------------*/
private int log_2(int x){
double y = Math.log( (double) x)/Math.log( (double) 2) ; //log(X)/log(2) yeilds log_2(x)
return (int) y;
}
/*---------------------------------------------------------------------------------------------------*/
//example output
//Set 0: 20028d D 20018a
public void printMemory(){
for(int k=0;k<numSets;k++){
System.out.print("Set "+k+": ");
mySets[k].printSet();
System.out.println();
}
}
/*---------------------------------------------------------------------------------------------------*/
} //end Cache class