-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.py
315 lines (275 loc) · 11.1 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
#Amirhossein Rajabpour 9731085
import math
dirty_bits = set()
#getting base information about the cache
info = input().rstrip('\n')
info2 = info.split(' - ')
block_size = int(info2[0])
is_separated = info2[1]
associativity = int(info2[2])
write_policy = info2[3]
write_miss = info2[4]
def set_finder(my_index, how_many_sets):
my_index = my_index[::-1]
my_set = int(my_index) % how_many_sets
return my_set
def index_finder(address,block_size, num_blocks):
power1 = math.log(block_size, 2)
sets = int(num_blocks / associativity)
power2 = math.log(sets, 2)
index = address[:: -1]
index = index[int(power1) : int(power1) + int(power2)]
return index
def tag_finder(address,num_blocks):
power1 = math.log(block_size, 2)
sets = int(num_blocks / associativity)
power2 = math.log(sets, 2)
address = address[:: -1]
tag = address[int(power2) + int(power1):]
return tag[::-1]
def tag_checking(address, queue, num_blocks):
exist = False
for i in queue:
if tag_finder(address,num_blocks) == tag_finder(i,num_blocks):
queue.remove(i)
exist = True
return exist
#initializing cache structure with a list of empty queues
if is_separated == '0':
cache_size = input().rstrip('\n')
number_of_blocks = int(int(cache_size) / block_size)
cache = []
for q in range(int(number_of_blocks/associativity)):
queue = []
cache.append(queue)
else:
cache_size = input().rstrip('\n').split(' - ')
#initializing instruction cache
instruction_size = cache_size[0]
number_of_inst_blocks = int(int(instruction_size) / block_size)
i_cache = []
for q in range(int(number_of_inst_blocks/associativity)):
queue = []
i_cache.append(queue)
# initializing data cache
data_size = cache_size[1]
number_of_data_blocks = int(int(data_size) / block_size)
d_cache = []
for q in range(int(number_of_data_blocks/associativity)):
queue = []
d_cache.append(queue)
#factors that should be calculated
d_accesses = 0
d_misses = 0
d_replace = 0
i_accesses = 0
i_misses = 0
i_replace = 0
fetch = 0
copies_back = 0
#if the structure is von-nuemann
if is_separated == '0':
lines = []
read = True
while read:
line = input()
if line == '':
read = False
break
else:
lines.append(line)
for d in lines:
tmp = d.split(' ')
tmp = tmp[:2]
# Code to convert hex to binary
res = "{0:032b}".format(int(tmp[1], 16))
res = str(res)
index = index_finder(res, block_size, number_of_blocks)
tmp_set = set_finder(index, int(number_of_blocks / associativity))
if tmp[0] == '0' or tmp[0] == '2':
if tmp[0] == '0': d_accesses += 1
else: i_accesses += 1
if tag_checking(res,cache[tmp_set],number_of_blocks): cache[tmp_set].append(res)
else:
fetch += (block_size / 4)
if len(cache[tmp_set]) == associativity:
evicted = cache[tmp_set].pop(0)
if evicted[:-int(math.log(block_size, 2))] in dirty_bits:
copies_back += (block_size / 4)
dirty_bits.remove(evicted[:-int(math.log(block_size, 2))])
cache[tmp_set].append(res)
if tmp[0] == '0':
d_misses += 1
d_replace += 1
else:
i_misses += 1
i_replace += 1
else:
cache[tmp_set].append(res)
if tmp[0] == '0': d_misses += 1
else: i_misses += 1
elif tmp[0] == '1':
d_accesses += 1
if write_policy == 'wt' and write_miss == 'wa':
copies_back += 1
if tag_checking(res,cache[tmp_set],number_of_blocks): cache[tmp_set].append(res)
else:
fetch += (block_size / 4)
d_misses += 1
if len(cache[tmp_set]) == associativity:
cache[tmp_set].pop(0)
cache[tmp_set].append(res)
d_replace += 1
else: cache[tmp_set].append(res)
elif write_policy == 'wt' and write_miss == 'nw':
copies_back += 1
if tag_checking(res,cache[tmp_set],number_of_blocks): cache[tmp_set].append(res)
else: d_misses += 1
elif write_policy == 'wb' and write_miss == 'wa':
dirty_bits.add(res[:-int(math.log(block_size, 2))])
if tag_checking(res, cache[tmp_set], number_of_blocks): cache[tmp_set].append(res)
else:
d_misses += 1
fetch += (block_size / 4)
if len(cache[tmp_set]) == associativity:
evicted = cache[tmp_set].pop(0)
if evicted[:-int(math.log(block_size, 2))] in dirty_bits:
copies_back += (block_size / 4)
dirty_bits.remove(evicted[:-int(math.log(block_size, 2))])
cache[tmp_set].append(res)
d_replace += 1
else: cache[tmp_set].append(res)
elif write_policy == 'wb' and write_miss == 'nw':
if tag_checking(res, cache[tmp_set], number_of_blocks):
cache[tmp_set].append(res)
dirty_bits.add(res[:-int(math.log(block_size, 2))])
else:
d_misses += 1
copies_back += 1
#if the structure is harvard
else:
lines = []
read = True
while read:
line = input()
if line == '':
read = False
break
else:
lines.append(line)
for d in lines:
tmp = d.split(' ')
tmp = tmp[:2]
# Code to convert hex to binary
res = "{0:032b}".format(int(tmp[1], 16))
res = str(res)
#reading data
if tmp[0] == '0':
d_accesses += 1
index = index_finder(res, block_size,number_of_data_blocks )
tmp_set = set_finder(index, int(number_of_data_blocks / associativity))
if tag_checking(res,d_cache[tmp_set],number_of_data_blocks): d_cache[tmp_set].append(res)
else:
fetch += (block_size / 4)
d_misses += 1
if len(d_cache[tmp_set]) == associativity:
evicted = d_cache[tmp_set].pop(0)
if evicted[:-int(math.log(block_size, 2))] in dirty_bits:
copies_back += (block_size / 4)
dirty_bits.remove(evicted[:-int(math.log(block_size, 2))])
d_cache[tmp_set].append(res)
d_replace += 1
else: d_cache[tmp_set].append(res)
#reading instruction
elif tmp[0] == '2':
i_accesses += 1
index = index_finder(res, block_size,number_of_inst_blocks)
tmp_set = set_finder(index, int(number_of_inst_blocks / associativity))
if tag_checking(res,i_cache[tmp_set],number_of_inst_blocks): i_cache[tmp_set].append(res)
else:
fetch += (block_size / 4)
i_misses += 1
if len(i_cache[tmp_set]) == associativity:
evicted = i_cache[tmp_set].pop(0)
i_cache[tmp_set].append(res)
i_replace += 1
else: i_cache[tmp_set].append(res)
#writing data
elif tmp[0] == '1':
index = index_finder(res, block_size,number_of_data_blocks )
tmp_set = set_finder(index, int(number_of_data_blocks / associativity))
d_accesses += 1
if write_policy == 'wt' and write_miss == 'wa':
copies_back += 1
if tag_checking(res,d_cache[tmp_set],number_of_data_blocks): d_cache[tmp_set].append(res)
else:
fetch += (block_size / 4)
d_misses += 1
if len(d_cache[tmp_set]) == associativity:
d_cache[tmp_set].pop(0)
d_cache[tmp_set].append(res)
d_replace += 1
else: d_cache[tmp_set].append(res)
elif write_policy == 'wt' and write_miss == 'nw':
copies_back += 1
if tag_checking(res,d_cache[tmp_set],number_of_data_blocks): d_cache[tmp_set].append(res)
else: d_misses += 1
elif write_policy == 'wb' and write_miss == 'wa':
dirty_bits.add(res[:-4])
if tag_checking(res, d_cache[tmp_set], number_of_data_blocks): d_cache[tmp_set].append(res)
else:
fetch += (block_size / 4)
d_misses += 1
if len(d_cache[tmp_set]) == associativity:
evicted = d_cache[tmp_set].pop(0)
if evicted[:-int(math.log(block_size, 2))] in dirty_bits:
copies_back += (block_size / 4)
dirty_bits.remove(evicted[:-int(math.log(block_size, 2))])
d_cache[tmp_set].append(res)
d_replace += 1
else: d_cache[tmp_set].append(res)
elif write_policy == 'wb' and write_miss == 'nw':
if tag_checking(res, d_cache[tmp_set], number_of_data_blocks):
d_cache[tmp_set].append(res)
dirty_bits.add(res[:-int(math.log(block_size, 2))])
else:
copies_back += 1
d_misses += 1
copies_back += (len(dirty_bits) * int(block_size/4) )
print('***CACHE SETTINGS***')
if is_separated == '0':
print('Unified I- D-cache')
print('Size:',cache_size)
else:
print('Split I- D-cache')
print('I-cache size:', instruction_size)
print('D-cache size:', data_size)
print('Associativity:',associativity)
print('Block size:',block_size)
if write_policy == 'wb': print('Write policy: WRITE BACK')
else: print('Write policy: WRITE THROUGH')
if write_miss == 'wa': print('Allocation policy: WRITE ALLOCATE')
else: print('Allocation policy: WRITE NO ALLOCATE')
print()
print('***CACHE STATISTICS***')
print('INSTRUCTIONS')
print('accesses:',i_accesses)
print('misses:',i_misses)
if i_accesses != 0:
i_miss_rate = i_misses / i_accesses
i_hit_rate = 1 - i_miss_rate
print('miss rate:', "{:.4f}".format(i_miss_rate),'(hit rate', "{:.4f}".format(i_hit_rate) +')')
else: print('miss rate: 0.0000 (hit rate 0.0000)')
print('replace:',i_replace)
print('DATA')
print('accesses:',d_accesses)
print('misses:',d_misses)
if d_accesses != 0:
d_miss_rate = d_misses / d_accesses
d_hit_rate = 1 - d_miss_rate
print('miss rate:', "{:.4f}".format(d_miss_rate) ,'(hit rate',"{:.4f}".format(d_hit_rate) +')')
else: print('miss rate: 0.0000 (hit rate 0.0000)')
print('replace:',d_replace)
print('TRAFFIC (in words)')
print('demand fetch:',int(fetch))
print('copies back:',int(copies_back))