-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
347 lines (288 loc) · 10.2 KB
/
cache.py
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
import simu
from math import *
global CACHE_MISS
CACHE_MISS = 0
cache1_size = 8
cache2_size = 16
block1_size = 4
block2_size = 4
# latency1 = 10
# latency2 = 20
assoc1 = 1
assoc2 = 2
# to be changed later...
stalls1 = 2
stalls2 = 4
stalls3 = 10 # for memory penalty
set1 = int(cache1_size / (block1_size * assoc1))
set2 = int(cache2_size / (block2_size * assoc2))
block1 = int(block1_size / 4)
block2 = int(block2_size / 4)
# no_of_blocks_in_cache1 = cache1_size/block1_size
# no_of_sets = no_of_blocks_in_cache1/assoc1
def update_settings(cache1, cache2, block1, block2, assco1, assco2, stall1, stall2, stall3):
global cache1_size, cache2_size, block1_size, block2_size, assoc1, assoc2, stalls1, stalls2, stalls3
cache1_size = int(cache1 or cache1_size)
cache2_size = int(cache2 or cache2_size)
block1_size = int(block1 or block1_size)
block2_size = int(block2 or block2_size)
assoc1 = int(assco1 or assoc1)
assoc2 = int(assco2 or assoc2)
stalls1 = int(stall1 or stalls1)
stalls2 = int(stall2 or stalls2)
stalls3 = int(stall3 or stalls3)
print("Updated", cache1_size, cache2_size, block1_size, block2_size, assoc1, assoc2, stalls1, stalls2, stalls3)
CacheHit.change_caches()
class CacheHit:
global cache1, cache2, counter1, counter2
cache1 = []
cache2 = []
counter1 = 0
counter2 = 0
for _ in range(set1):
cachetemp1 = []
for _ in range(assoc1):
cachet1 = []
for _ in range(block1):
cachet1.append([-1, -1, counter1])
cachetemp1.append(cachet1)
cache1.append(cachetemp1)
# indices -> tag, data, counter(for LRU)
for _ in range(set2):
cachetemp2 = []
for _ in range(assoc2):
cachet2 = []
for _ in range(block2):
cachet2.append([-1, -1, counter2])
cachetemp2.append(cachet2)
cache2.append(cachetemp2)
# indices -> tag, data, counter(for LRU)
print("=" * 100)
print(cache1)
print(cache2)
print("=" * 100)
def change_caches():
global cache1, cache2, set1, set2, block1, block2
set1 = int(cache1_size / (block1_size * assoc1))
set2 = int(cache2_size / (block2_size * assoc2))
block1 = int(block1_size / 4)
block2 = int(block2_size / 4)
cache1 = []
cache2 = []
for _ in range(set1):
cache1.append([[[-1, -1, counter1]] * block1] * assoc1)
# indices -> tag, data, counter(for LRU)
for _ in range(set2):
cache2.append([[[-1, -1, counter2]] * block2] * assoc2)
# indices -> tag, data, counter(for LRU)
print("=" * 100)
print(cache1)
print(cache2)
print("=" * 100)
# Cache 1. Checking if the data is present or not
def cache_hit_1(self, adrs):
global cachehit1
cachehit1 = False
adr = bin(adrs)
l = len(adr)
adr = adr[0:2] + '0' * (10 - l) + adr[2:]
# set_id = adrs % set1
block_bit = int(log(block1, 2))
set_bit = int(log(set1, 2))
if block_bit == 0 and set_bit == 0:
block_id = 0
set_id = 0
elif block_bit == 0:
block_id = 0
set_id = int(adr[-set_bit:])
elif set_bit == 0:
block_id = int(adr[-block_bit:])
set_id = 0
else:
block_id = int(adr[-block_bit:])
set_id = int(adr[-set_bit-block_bit:-block_bit])
for j in range(assoc1):
if adr[2:] == cache1[set_id][j][block_id][0]:
cachehit1 = True
global counter1
cache1[set_id][j][block_id] = [adr[2:], simu.RAM[adrs], counter1]
counter1 += 1
return cache1[set_id][j][block_id][1], stalls1
if not cachehit1:
temp1 = self.cache_hit_2(adrs)
self.insert_cache1(adrs)
return temp1
# Cache 2. Checking if the data is present or not if it is not present in Cache 1
def cache_hit_2(self, adrs):
global cachehit2
cachehit2 = False
adr = bin(adrs)
l = len(adr)
adr = adr[0:2] + '0' * (10 - l) + adr[2:]
# set_id = adrs % set1
block_bit = int(log(block1, 2))
set_bit = int(log(set1, 2))
if block_bit == 0 and set_bit == 0:
block_id = 0
set_id = 0
elif block_bit == 0:
block_id = 0
set_id = int(adr[-set_bit:])
elif set_bit == 0:
block_id = int(adr[-block_bit:])
set_id = 0
else:
block_id = int(adr[-block_bit:])
set_id = int(adr[-set_bit - block_bit:-block_bit])
for j in range(assoc2):
if adr == cache2[set_id][j][block_id][0]:
cachehit2 = True
global counter2
cache2[set_id][j][block_id] = [adr, simu.RAM[adrs], counter2]
counter2 += 1
return cache2[set_id][j][block_id][1], stalls1 + stalls2
if not cachehit2:
temp2 = self.memory_operation(adrs)
self.insert_cache2(adrs)
return temp2
# Checking in memory if the data is not present in both the Caches
def memory_operation(self, adrs):
print("Cache Miss!")
global CACHE_MISS
CACHE_MISS += 1
return simu.RAM[adrs], stalls1 + stalls2 + stalls3
# Inserting Data in Cache1 if not present
def insert_cache1(self, adrs):
cache1inserted = False
adr = bin(adrs)
l = len(adr)
adr = adr[0:2] + '0' * (10 - l) + adr[2:]
print(adr)
# set_id = adrs % set1
block_bit = int(log(block1, 2))
set_bit = int(log(set1, 2))
if block_bit == 0 and set_bit == 0:
block_id = 0
set_id = 0
elif block_bit == 0:
block_id = 0
set_id = int(adr[-set_bit:])
elif set_bit == 0:
block_id = int(adr[-block_bit:])
set_id = 0
else:
block_id = int(adr[-block_bit:])
set_id = int(adr[-set_bit - block_bit:-block_bit])
for j in range(assoc1):
if cache1[set_id][j][block_id][0] == -1:
global counter1
for k in range(block1):
cache1[set_id][j][k] = [adr, simu.RAM[adrs+k], counter1]
print(cache1)
# cache1[set_id][j][block_id] = [adr, simu.RAM[adrs], counter1]
counter1 += 1
cache1inserted = True
break
if not cache1inserted:
self.replace_in_cache1(adrs)
# Inserting Data in Cache2 if not present
def insert_cache2(self, adrs):
cache2inserted = False
adr = bin(adrs)
l = len(adr)
adr = adr[0:2] + '0' * (10 - l) + adr[2:]
print(adr)
# set_id = adrs % set1
block_bit = int(log(block1, 2))
set_bit = int(log(set1, 2))
if block_bit == 0 and set_bit == 0:
block_id = 0
set_id = 0
elif block_bit == 0:
block_id = 0
set_id = int(adr[-set_bit:])
elif set_bit == 0:
block_id = int(adr[-block_bit:])
set_id = 0
else:
block_id = int(adr[-block_bit:])
set_id = int(adr[-set_bit - block_bit:-block_bit])
for j in range(assoc2):
if cache2[set_id][j][block_id][0] == -1:
global counter2
for k in range(block2):
cache2[set_id][j][k] = [adr, simu.RAM[adrs+k], counter2]
print(cache2)
# cache2[set_id][j][block_id] = [adr, simu.RAM[adrs], counter2]
counter2 += 1
cache2inserted = True
break
if not cache2inserted:
self.replace_in_cache2(adrs)
# If Cache1 is full, then replacing the existing data with new one acc to LRU policy
def replace_in_cache1(self, adrs):
ilru = 0
jlru = 0
adr = bin(adrs)
l = len(adr)
adr = adr[0:2] + '0' * (10 - l) + adr[2:]
print(adr)
# set_id = adrs % set1
block_bit = int(log(block1, 2))
set_bit = int(log(set1, 2))
if block_bit == 0 and set_bit == 0:
block_id = 0
set_id = 0
elif block_bit == 0:
block_id = 0
set_id = int(adr[-set_bit:])
elif set_bit == 0:
block_id = int(adr[-block_bit:])
set_id = 0
else:
block_id = int(adr[-block_bit:])
set_id = int(adr[-set_bit - block_bit:-block_bit])
for j in range(assoc1):
if cache1[set_id][j][block_id][2] < cache1[ilru][jlru][block_id][2]:
ilru = set_id
jlru = j
global counter1
for k in range(block1):
cache1[ilru][jlru][k] = [adr, simu.RAM[adrs + k], counter1].copy()
print(cache1)
# cache1[ilru][jlru][block_id] = [adr, simu.RAM[adrs], counter1]
counter1 += 1
# If Cache2 is full, then replacing the existing data with new one acc to LRU policy
def replace_in_cache2(self, adrs):
ilru = 0
jlru = 0
adr = bin(adrs)
l = len(adr)
adr = adr[0:2] + '0' * (10 - l) + adr[2:]
print(adr)
# set_id = adrs % set1
block_bit = int(log(block1, 2))
set_bit = int(log(set1, 2))
if block_bit == 0 and set_bit == 0:
block_id = 0
set_id = 0
elif block_bit == 0:
block_id = 0
set_id = int(adr[-set_bit:])
elif set_bit == 0:
block_id = int(adr[-block_bit:])
set_id = 0
else:
block_id = int(adr[-block_bit:])
set_id = int(adr[-set_bit - block_bit:-block_bit])
for j in range(assoc2):
if cache2[set_id][j][block_id][2] < cache2[ilru][jlru][block_id][2]:
ilru = set_id
jlru = j
global counter2
for k in range(block2):
cache2[ilru][jlru][k] = [adr, simu.RAM[adrs + k], counter2].copy()
print(cache2)
# cache2[ilru][jlru][block_id] = [adr, simu.RAM[adrs], counter2]
counter2 += 1
CacheOP = CacheHit()