-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.C
256 lines (204 loc) · 6.04 KB
/
cache.C
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
#include "cache.h"
#include <stdlib.h>
#include <math.h>
Cache::Cache(u_int32_t numSets, u_int32_t assoc, u_int32_t blockSize, bool randRep, bool writeAlloc, bool writeThrough) {
_numSets = numSets;
_blockSize = blockSize;
_assoc = assoc;
_randReplacement = randRep;
_writeAlloc = writeAlloc;
_writeThrough = writeThrough;
//printf("numsets %u, blocksize %u, assoc %u\n",_numSets,_blockSize,_assoc);
tags = (int**)malloc(_numSets * sizeof(int *));
valid = (bool**)malloc(_numSets * sizeof(bool *));
dirty = (bool**)malloc(_numSets * sizeof(bool *));
lru = (int**)malloc(_numSets * sizeof(int *));
//mine
pref_bit = (bool**)malloc(_numSets * sizeof(bool *));
for(int i = 0; i < _numSets; ++i) {
tags[i] = (int*)malloc(_assoc * sizeof(int));
valid[i] = (bool*)malloc(_assoc * sizeof(bool));
dirty[i] = (bool*)malloc(_assoc * sizeof(bool));
lru[i] = (int*)malloc(_assoc * sizeof(int));
//mine
pref_bit[i] = (bool*)malloc(_assoc * sizeof(bool));
}
// init data
reset();
}
Cache::~Cache() {
for(int i = 0; i < _numSets; ++i) {
free(tags[i]);
free(valid[i]);
free(dirty[i]);
free(lru[i]);
//mine
free(pref_bit[i]);
}
free(tags);
free(valid);
free(dirty);
free(lru);
//mine
free(pref_bit);
}
void Cache::reset() {
for(int i = 0; i < _numSets; ++i) {
for(int j = 0; j < _assoc; ++j) {
tags[i][j] = lru[i][j] = 0;
dirty[i][j] = false;
valid[i][j] = false;
//mine
pref_bit[i][j] = false;
}
}
// reseed the random generator
srand(100);
}
// check to see if it is in the cache (doesn't modify anything)
bool Cache::check(u_int32_t addr, bool load) {
bool isHit = false;
int b = (int)log2((double)_blockSize);
int s = (int)log2((double)_numSets);
int t = 32 - b - s;
int index = ((addr >> b) << (32 - s)) >> (32 - s);
int tag = addr >> (b + s);
for(int i = 0; i < _assoc; ++i) {
if(tag == tags[index][i] && valid[index][i]) { // found it
isHit = true;
break; // no need to search more
}
}
return isHit;
}
//mine
// check to see if the cache hit is due to prefetch request? (doesn't modify anything)
bool Cache::is_prefetch(u_int32_t addr) {
bool pf_bit = false;
int b = (int)log2((double)_blockSize);
int s = (int)log2((double)_numSets);
int t = 32 - b - s;
int index = ((addr >> b) << (32 - s)) >> (32 - s);
int tag = addr >> (b + s);
for(int i = 0; i < _assoc; ++i) {
if(tag == tags[index][i] && valid[index][i]) { // found it
pf_bit = pref_bit[index][i];
break; // no need to search more
}
}
return pf_bit;
}
u_int32_t Cache::getTag(u_int32_t addr) {
int b = (int)log2((double)_blockSize);
int s = (int)log2((double)_numSets);
int t = 32 - b - s;
u_int32_t tag = addr >> (b + s);
return tag;
}
u_int32_t Cache::getIndex(u_int32_t addr) {
int b = (int)log2((double)_blockSize);
int s = (int)log2((double)_numSets);
u_int32_t index = ((addr >> b) << (32 - s)) >> (32 - s);
return index;
}
// accesses cache, returns true if we have a hit, false otherwise, don't change any state if modify == false
bool Cache::access(u_int32_t addr, bool load, bool pf_bit) {
bool isHit = false;
int b = (int)log2((double)_blockSize);
int s = (int)log2((double)_numSets);
int t = 32 - b - s;
//printf("b %d, s %d, t %d\n",b,s,t);
int index = ((addr >> b) << (32 - s)) >> (32 - s);
int tag = addr >> (b + s);
if(_numSets == 1) index = 0;
//printf("index %d\n",index);
bool inCache = false;
bool foundEmpty;
// first check to see if the data is already in there
for(int i = 0; i < _assoc; ++i) {
if(tag == tags[index][i] && valid[index][i]) { // found it
isHit = true;
//mine
pref_bit[index][i]=!pf_bit;
if(!load)
dirty[index][i] = true; // its dirty now that we stored to it
// update LRU information (no need to do this for DM)
if(_assoc > 1 && !_randReplacement) {
// for all that had smaller LRU numbers, add 1 to them
for(int j = 0; j < _assoc; ++j) {
if(lru[index][j] < lru[index][i] && valid[index][j])
lru[index][j]++;
}
lru[index][i] = 0;
}
inCache = true;
break; // no need to search more
}
}
// if it wasn't in cache, bring it in if it was a load or if we are doing write allocate
if(!inCache && (load || _writeAlloc)) {
foundEmpty = false;
// first search to see if there is an open spot at that index
for(int i = 0; i < _assoc; ++i) {
if(!valid[index][i]) { // found an open spot... all is good with the world
tags[index][i] = tag;
valid[index][i] = true;
//mine
pref_bit[index][i]=!pf_bit;
if(!load) dirty[index][i] = true;
// need to update LRU bits if we are using that policy
if(_assoc > 1 && !_randReplacement) {
// for all that had smaller LRU numbers, add 1 to them
for(int j = 0; j < _assoc; ++j) {
if(valid[index][j]) // since we found an open slot, we can just increase all the others without worrying
lru[index][j]++;
}
lru[index][i] = 0;
}
foundEmpty = true;
break;
}
}
// if we couldn't find an empty spot, we need to kick out something old
if(!foundEmpty) {
// if DM, just put it in the only slot we have...
if(_assoc == 1) {
tags[index][0] = tag;
valid[index][0] = true;
//mine
pref_bit[index][0]=!pf_bit;
//if(dirty[index][0]) evicted = true;
if(load) dirty[index][0] = false;
else dirty[index][0] = true;
}
// just kick out a random guy if we specified random replacement policy
else if(_randReplacement) {
int temp = rand() % _assoc;
tags[index][temp] = tag;
valid[index][temp] = true;
//mine
pref_bit[index][temp]=!pf_bit;
//if(dirty[index][temp]) evicted = true;
if(load) dirty[index][temp] = false;
else dirty[index][temp] = true;
}
// find LRU and kick him out
else {
for(int i = 0; i < _assoc; ++i) {
lru[index][i]++;
if(lru[index][i] == _assoc) { // this is the LRU
tags[index][i] = tag;
valid[index][i] = true;
//mine
pref_bit[index][i]=!pf_bit;
//if(dirty[index][i]) evicted = true;
if(load) dirty[index][i] = false;
else dirty[index][i] = true;
lru[index][i] = 0;
}
}
}
}
}
return isHit;
}