-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplacementPolicy.cpp
More file actions
256 lines (213 loc) · 5.28 KB
/
Copy pathReplacementPolicy.cpp
File metadata and controls
256 lines (213 loc) · 5.28 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
#include "memorySimulator.h"
struct Node* newNode(bitset<1> value)
{
Node* n = new Node;
n->key = value;
n->left = NULL;
n->right = NULL;
return n;
}
struct Node* insertValue(struct Node* root, bitset<1> value,queue<Node*>& q)
{
Node* node = newNode(value);
if (root == NULL)
root = node;
// The left child of the current Node is
// used if it is available.
else if (q.front()->left == NULL)
q.front()->left = node;
// The right child of the current Node is used
// if it is available. Since the left child of this
// node has already been used, the Node is popped
// from the queue after using its right child.
else {
q.front()->right = node;
q.pop();
}
// Whenever a new Node is added to the tree, its
// address is pushed into the queue.
// So that its children Nodes can be used later.
q.push(node);
return root;
}
// This function mainly calls insertValue() for
// all array elements. All calls use same queue.
Node* createTree(vector<bitset<1>> arr, int n)
{
Node* root = NULL;
queue<Node*> q;
for (int i = 0; i < n; i++)
root = insertValue(root, arr[i], q);
return root;
}
// âââ
struct Node* UpDateValue(int n, int x, struct Node* root, vector<bitset<1>>& arr1)
{
if (root->key == false) {
root->key = true;
arr1[n] = true;
n++;
if (root->left == NULL)
{
return root;
}
UpDateValue(n, x, root->right, arr1);
return root;
}
if (root->key == true) {
root->key = false;
arr1[n] = false;
n++;
if (root->left == NULL)
{
return root;
}
UpDateValue(n, x, root->left, arr1);
return root;
}
}
// âââ
Node* UpDatePLRUArray(struct Node* root, vector<bitset<1>>& arr1, int Way)
{
root = UpDateValue(0, Way - 1, root, arr1);
return root;
}
// âââ
void UpDateLRUArray(vector< vector<unsigned long> >& CounterLRU, int num, int Way,
long& addressIndexL, unsigned long& Row_LCache) {
vector< long> temp, array;
int index = (int)addressIndexL / Row_LCache;
int X0 = 0;
int X1 = 1;
temp.resize(Way);
array.resize(Way);
for (int i = 0; i < Way; i++)
{
temp[i] = CounterLRU[index][i];
array[i] = CounterLRU[index][i];
if (CounterLRU[index][i] == 0)
{
X0 = i;
}
if (CounterLRU[index][i] == 1)
{
X1 = i;
}
}
for (int j = 0; j < Way; j++)
{
array[j] = array[j] - 1;
}
array[num] = Way - 1;
if (array[X0] == -1 && array[X1] == 0)
{
array[X0] = array[X0] + 1;
array[X1] = array[X1] + 1;
}
if (array[X1] == (Way - 1))
{
array[X0] = array[X0] + 1;
}
for (int i = 0; i < Way; i++)
{
CounterLRU[index][i] = array[i];
}
}
int checkReplacementPolicy(unsigned long& Row_LCache, long& addressIndexL, unsigned long& Way,
unsigned long& addressTag_L, vector< vector<bitset<1>> >& ValidBit_L, ReplacementPolicy Policy,
vector< vector< unsigned long> >& CounterLRU_L, vector < bitset<1>> arr) {
int TagInRow = (int)addressIndexL / Row_LCache;
int x = 0;
int counter = 0;
int power = log2(Way) - 1;
if (Policy == ReplacementPolicy::Random) {
int randomNumber = rand() % Way;
// randomNumber in the range 0 to Way num
return randomNumber;
}
if (Policy == ReplacementPolicy::LRU) {
for (int i = 0; i < Way; i++)
{
if (CounterLRU_L[TagInRow][i] == 0) {
return i;
}
}
}
if (Policy == ReplacementPolicy::PseudoLRU) {
for (int i = 0; i < log2(Way); i++)
{
if (arr[i] == true)
{
x = pow(2, power);
counter = counter + x;
}
power--;
}
return counter;
}
}
void firstDateLRU(vector< vector<unsigned long> >& CounterLRU_L1, vector< vector<unsigned long> >& CounterLRU_L2,
vector< vector<unsigned long> >& CounterLRU_L3,
unsigned long& Way1, unsigned long& Way2, unsigned long& Way3,
unsigned long Row_L1Cache, unsigned long Row_L2Cache, unsigned long Row_L3Cache)
{
for (int i = 0; i < Row_L1Cache; i++)
{
for (int j = 0; j < Way1; j++)
{
CounterLRU_L1[i][j] = j;
}
}
for (int i = 0; i < Row_L2Cache; i++)
{
for (int j = 0; j < Way2; j++)
{
CounterLRU_L2[i][j] = j;
}
}
for (int i = 0; i < Row_L3Cache; i++)
{
for (int j = 0; j < Way3; j++)
{
CounterLRU_L3[i][j] = j;
}
}
}
//âââ
void createTreePLRU(vector<struct Node*>& root1, vector<struct Node*>& root2, vector<struct Node*>& root3,
unsigned long Row_L1Cache, unsigned long Row_L2Cache, unsigned long Row_L3Cache, long way1, long way2, long way3)
{
root1.resize(Row_L1Cache);
root2.resize(Row_L2Cache);
root3.resize(Row_L3Cache);
vector<bitset<1>> arr1, arr2, arr3;
arr1.resize(way1 - 1);
arr2.resize(way2 - 1);
arr3.resize(way3 - 1);
for (int i = 0; i < Row_L1Cache; i++) {
root1[i] = createTree(arr1, way1 - 1);
}
for (int i = 0; i < Row_L2Cache; i++) {
root2[i] = createTree(arr2, way2 - 1);
}
for (int i = 0; i < Row_L3Cache; i++) {
root3[i] = createTree(arr3, way3 - 1);
}
}
//âââ
void getCounterPLRU_L(vector< vector<bitset<1>> >& CounterPLRU_L1, long way, unsigned long Row_L1Cache,
vector< vector<bitset<1>> >& CounterPLRU_L2, unsigned long Row_L2Cache,
vector< vector<bitset<1>> >& CounterPLRU_L3, unsigned long Row_L3Cache) {
CounterPLRU_L1.resize(Row_L1Cache);
CounterPLRU_L2.resize(Row_L2Cache);
CounterPLRU_L3.resize(Row_L3Cache);
for (int i = 0; i < Row_L1Cache; i++) {
CounterPLRU_L1[i].resize(way - 1);
}
for (int i = 0; i < Row_L2Cache; i++) {
CounterPLRU_L2[i].resize(way - 1);
}
for (int i = 0; i < Row_L3Cache; i++) {
CounterPLRU_L3[i].resize(way - 1);
}
}