-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_comparison.py
More file actions
279 lines (226 loc) · 9.96 KB
/
cache_comparison.py
File metadata and controls
279 lines (226 loc) · 9.96 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
import numpy as np
import json
import argparse
from queue import Queue
import math
import time
from tqdm import tqdm
import mmap
import operator
DEBUG = 0
def get_num_lines(file_path):
fp = open(file_path, "r+")
buf = mmap.mmap(fp.fileno(), 0)
lines = 0
while buf.readline():
lines += 1
return lines
class Cache():
"""
Cache class that takes constructor parameters to set cache size, associativity, replacement policy and write policy.
"""
def __init__(self, cache_size, associativity, replacement_policy, write_policy):
"""
Constructor for Cache class.
"""
self.cache_rows = int(cache_size/(64 * associativity))
self.associativity = associativity
self.offset = 6
self.set_bits = int(math.ceil(math.log2(self.cache_rows)))
self.dirty = np.zeros((self.cache_rows, self.associativity), dtype=np.int8)
self.vacant = np.zeros((self.cache_rows, self.associativity), dtype=np.int8)
self.tags = np.zeros((self.cache_rows, self.associativity), dtype=np.int64)
self.replacement = [Queue() for x in range(self.cache_rows)]
#1 for FIFO, 0 for LRU
if replacement_policy == 1:
self.replacement_policy = replacement_policy
self.replacement = [Queue() for x in range(self.cache_rows)]
else:
self.replacement_policy = replacement_policy
LRUs = []
for i in range(self.cache_rows):
temp = {}
for i in range(self.associativity):
temp[i] = 0
LRUs.append(temp)
self.replacement = [LRUs[x] for x in range(self.cache_rows)]
# 0 is WT, 1 is WB
self.write_policy = write_policy
self.hits = 0
self.misses = 0
self.reads = 0
self.writes = 0
def hex_to_binary(self, hex_value):
temp = ""
for i in range(17-len(hex_value)):
temp += "0"
hex_value = temp + hex_value
return str(bin(int(hex_value, 16)))
def hex_to_int(self, hex_value):
return int(hex_value, 16)
def get_set_tag(self, address):
set_bits = (address/64) % self.cache_rows
tag = address/64
return int(tag), int(set_bits)
def breakdown_address(self, address):
address = address[0:-self.offset]
set_bits = address[-self.set_bits:]
tag = address[0:-self.set_bits]
tag = int(tag, 2)
set_bits = int(set_bits, 2)
return tag, set_bits
def cache_read(self, address):
address = self.hex_to_binary(address)
tag, set_bits = self.breakdown_address(address)
if DEBUG:
print("Tag: {} Set: {}".format(tag, set_bits))
if tag in self.tags[set_bits]: # Already in Cache
self.hits += 1
if DEBUG:
print("Read - Hit")
if self.replacement_policy == 0:
index = list(self.tags[set_bits]).index(tag)
for key in self.replacement[set_bits]:
if (self.vacant[set_bits][key] == 1):
self.replacement[set_bits][key] += 1
self.replacement[set_bits][index] = 1
elif (0 in self.vacant[set_bits]): # Not in Cache, but space
self.reads += 1
self.misses += 1
if DEBUG:
print("Read - Miss")
if self.replacement_policy == 1:
index = list(self.vacant[set_bits]).index(0)
self.replacement[set_bits].put(index)
self.vacant[set_bits][index] = 1
self.tags[set_bits][index] = tag
else:
index = list(self.vacant[set_bits]).index(0)
self.vacant[set_bits][index] = 1
for key in self.replacement[set_bits]:
if self.vacant[set_bits][key] == 1:
self.replacement[set_bits][key] += 1
self.tags[set_bits][index] = tag
self.replacement[set_bits][index] = 1
else: # Not in Cache, Need to Replace
self.reads += 1
self.misses += 1
if DEBUG:
print("Read - Miss")
if self.replacement_policy == 1:
index = self.replacement[set_bits].get()
self.tags[set_bits][index] = tag
self.replacement[set_bits].put(index)
if self.dirty[set_bits][index] == 1 and self.write_policy == 1:
self.writes += 1
else:
index = max(self.replacement[set_bits].items(), key=operator.itemgetter(1))[0]
for key in self.replacement[set_bits]:
self.replacement[set_bits][key] += 1
self.tags[set_bits][index] = tag
self.replacement[set_bits][index] = 1
if self.dirty[set_bits][index] == 1 and self.write_policy == 1:
self.writes += 1
self.dirty[set_bits][index] = 0
if (DEBUG):
print("Index {}".format(index))
if self.replacement_policy == 1:
print("Replacement: {}".format(list(self.replacement[set_bits].queue)))
else:
print("Replacement: {}".format([self.replacement[set_bits][i] for i in self.replacement[set_bits]]))
print("Vacant: {}".format(self.vacant[set_bits]))
print()
def cache_write(self, address):
address = self.hex_to_binary(address)
tag, set_bits = self.breakdown_address(address)
if DEBUG:
print("Tag: {} Set: {}".format(tag, set_bits))
if tag in self.tags[set_bits]: # Already in Cache
self.hits += 1
if self.write_policy == 0:
self.writes += 1
if DEBUG:
print("Write - Hit")
if self.replacement_policy == 0:
index = list(self.tags[set_bits]).index(tag)
for key in self.replacement[set_bits]:
if (self.vacant[set_bits][key] == 1):
self.replacement[set_bits][key] += 1
self.replacement[set_bits][index] = 1
self.dirty[set_bits][index] = 1
elif 0 in self.vacant[set_bits]: # Not in Cache, but space
self.misses += 1
self.reads += 1
if DEBUG:
print("Write - Miss")
if self.replacement_policy == 1:
index = list(self.vacant[set_bits]).index(0)
self.replacement[set_bits].put(index)
self.vacant[set_bits][index] = 1
self.tags[set_bits][index] = tag
else:
index = list(self.vacant[set_bits]).index(0)
self.vacant[set_bits][index] = 1
for key in self.replacement[set_bits]:
if self.vacant[set_bits][key] == 1:
self.replacement[set_bits][key] += 1
self.tags[set_bits][index] = tag
self.replacement[set_bits][index] = 1
if self.write_policy == 0:
self.writes += 1
else: # Not in Cache, Need to Replace
self.misses += 1
self.reads += 1
if DEBUG:
print("Write - Miss")
if self.replacement_policy == 1:
index = self.replacement[set_bits].get()
self.tags[set_bits][index] = tag
self.replacement[set_bits].put(index)
else:
index = max(self.replacement[set_bits].items(), key=operator.itemgetter(1))[0]
for key in self.replacement[set_bits]:
self.replacement[set_bits][key] += 1
self.tags[set_bits][index] = tag
self.replacement[set_bits][index] = 1
if self.write_policy == 0 or (self.write_policy == 1 and self.dirty[set_bits][index] == 1):
self.writes += 1
self.dirty[set_bits][index] = 0
if (DEBUG):#0 in self.vacant[set_bits]):
print("Index {}".format(index))
if self.replacement_policy == 1:
print("Replacement: {}".format(list(self.replacement[set_bits].queue)))
else:
print("Replacement: {}".format([self.replacement[set_bits][i] for i in self.replacement[set_bits]]))
print("Vacant: {}".format(self.vacant[set_bits]))
print()
def execute_trace(cache, trace_file):
f = open(trace_file, 'r')
for line in tqdm(f, total=get_num_lines(trace_file)):
if line[0] == 'W':
cache.cache_write(line[4:])
else:
cache.cache_read(line[4:])
return cache
def main():
parser = argparse.ArgumentParser()
parser.add_argument("cache_size", type=int,
help="the total size of the cache")
parser.add_argument("associtativity", type=int,
help="the associativity of the cache")
parser.add_argument("replacement_policy", type=int,
help="the replacement policy of the cache")
parser.add_argument("write_policy", type=int,
help="the write policy of the cache")
parser.add_argument("tracefile", type=str,
help="the trace file to run the cache on")
args = parser.parse_args()
cache = Cache(args.cache_size, args.associtativity, args.replacement_policy, args.write_policy)
trace = args.tracefile
results = execute_trace(cache, trace)
print("Hit Ratio: {}, Writes: {}, Reads: {}".format((results.misses / (results.hits + results.misses)), results.writes, results.reads))
if __name__ == "__main__":
main()
#results = Cache(32768, 8, 0, 1)
#results = execute_trace(results, 'XSBENCH.t')
#print("Hit Ratio: {}, Writes: {}, Reads: {}, Total Lines: {}".format((results.misses / (results.hits + results.misses)), results.writes, results.reads, (results.hits + results.misses)))