-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.py
163 lines (147 loc) · 6.08 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
__author__ = 'cesar17'
from HashTable import *
from DoubleLinkedList import *
class Cache(object):
def __init__(self, N):
self.N = N #Tamano de cache
self.M = 13
# El tamanio de la tabla Hash es seleccionado de acuerdo al numero de entradas ingresado... Y este tamanio seleccionado
# es primo para disminuir la cantidad de colisiones
if self.N <= 155:
self.M = 31
elif self.N <= 1255:
self.M = 251
elif self.N <= 10195:
self.M = 2039
elif self.N <= 163745:
self.M = 65521
elif self.N <= 655355:
self.M = 262139
else:
self.M = 524287
#Implementacion de politica de reemplazo CLOCK por ***Cesar San Lucas***
def politicaClock(self, file):
pageFaults = 0
clockHand = 0
location = []
hashTable = HashTable(self.M)
totalLineas = 0
i = 0
while i < self.N:
location.append(PageTableEntry())
i += 1
for line in file:
totalLineas += 1
line = line.rstrip('\n')
if location[clockHand].getValidBit() == 1:
sMH = StringMH(line)
value = hashTable.getValue(sMH)
if value is None:
pageFaults = pageFaults + 1
while location[clockHand].getRefBit() == 1:
location[clockHand].setRefBit(0)
clockHand = clockHand + 1
clockHand = clockHand % self.N
key = location[clockHand].getStringMH()
hashTable.deleteKey(key)
hashTable.putKeyValue(sMH,clockHand)
location[clockHand].setStringMH(sMH)
location[clockHand].setRefBit(0)
clockHand = clockHand + 1
clockHand = clockHand % self.N
else:
if location[value].getRefBit() == 1:
location[value].setRefBit(0)
else:
location[value].setRefBit(1)
else:
sMH = StringMH(line)
value = hashTable.getValue(sMH)
if value is None:
pageFaults = pageFaults + 1
hashTable.putKeyValue(sMH,clockHand)
location[clockHand].setStringMH(sMH)
location[clockHand].setValidBit(1)
location[clockHand].setRefBit(0)
clockHand = clockHand + 1
clockHand = clockHand % self.N
else:
if location[value].getRefBit() == 1:
location[value].setRefBit(0)
else:
location[value].setRefBit(1)
missRate = 100*(float(pageFaults)/totalLineas)
missWarm = 100*(float(pageFaults - self.N)/(totalLineas-self.N))
mW = pageFaults - self.N
print "Evaluando una cache CLOCK con " + str(self.N) + " entradas"
print "Resultados:"
print "Miss rate: %0.2f" %missRate + "%% (%d" %pageFaults + " misses out of %d" %totalLineas + " references)"
print "Miss rate (warm cache): %0.2f" %missWarm + "%% (%d" %mW + " misses out of %d" %totalLineas + "-%d" %self.N + " references)"
#print "Page Faults: " + " " + str(pageFaults)
# Implementacion de politica de reemplazo LRU por ***HERNAN ULLON***
def politicaLRU(self, file):
location = DoubleLinkedList()
pageFaults = 0
indice = 0
llena = False
hashTable = HashTable(self.M)
totalLineas = 0
for line in file:
totalLineas += 1
line = line.rstrip('\n')
if llena:
sMH = StringMH(line)
value = hashTable.getValue(sMH)
if value is None:
pageFaults = pageFaults + 1
firstNodeData = location.removeFirstNode()
hashTable.deleteKey(firstNodeData)
refNode = location.append(sMH)
hashTable.putKeyValue(sMH, refNode)
else:
location.remove(value)
refNode = location.append(sMH)
hashTable.updateValue(sMH, refNode)
else:
sMH = StringMH(line)
value = hashTable.getValue(sMH)
if value is None:
pageFaults = pageFaults + 1
refNode = location.append(sMH)
hashTable.putKeyValue(sMH, refNode)
indice = indice + 1
if indice >= self.N:
llena = True
else:
location.remove(value)
refNode = location.append(sMH)
hashTable.updateValue(sMH, refNode)
missRate = 100*(float(pageFaults)/totalLineas)
missWarm = 100*(float(pageFaults - self.N)/(totalLineas-self.N))
mW = pageFaults - self.N
print "Evaluando una cache LRU con " + str(self.N) + " entradas"
print "Resultados:"
print "Miss rate: %0.2f" %missRate + "%% (%d" %pageFaults + " misses out of %d" %totalLineas + " references)"
print "Miss rate (warm cache): %0.2f" %missWarm + "%% (%d" %mW + " misses out of %d" %totalLineas + "-%d" %self.N + " references)"
#print "Page Faults: " + " " + str(pageFaults)
class PageTableEntry(object):
def __init__(self):
self.stringMH = None
self.refbit = 0
self.validbit = 0
def __eq__(self, other):
if type(other) is type(self):
return self.stringMH == other.stringMH
return False
def getStringMH(self):
return self.stringMH
def getRefBit(self):
return self.refbit
def getValidBit(self):
return self.validbit
def setStringMH(self, sMH):
self.stringMH = sMH
def setRefBit(self, value):
self.refbit = value
def setValidBit(self, value):
self.validbit = value