-
Notifications
You must be signed in to change notification settings - Fork 2
/
policies.py
239 lines (222 loc) · 8.61 KB
/
policies.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
import pyximport; pyximport.install()
from enum import Enum, auto
from cms import CMS
from collections import Counter
from math import log
from scipy import stats
class Policy(object):
def __init__(self, maximum_size):
self.maximum_size = maximum_size
self.misses = 0
self.hits = 0
pass
def record(self, key, size=1):
pass
def get_stats(self):
return { 'name' : self.__class__.__name__, 'hits' : self.hits, 'misses' : self.misses, 'hit ratio' : self.hits / (self.hits + self.misses) }
class LRU(Policy):
def __init__(self, maximum_size):
super().__init__(maximum_size)
self.current_size = 0
self.data = {}
self.sentinel = Node()
def record(self, key, size=1):
node = self.data.get(key)
if node:
self.hits += 1
node.remove()
node.append_to_tail(self.sentinel)
else:
self.misses += 1
if size > self.maximum_size:
return
self.current_size += size
while (self.current_size > self.maximum_size):
del self.data[self.sentinel.next_node.data]
self.current_size -= self.sentinel.next_node.size
self.sentinel.next_node.remove()
new_node = Node(key, size=size)
new_node.append_to_tail(self.sentinel)
self.data[key] = new_node
class WTinyLFU(Policy):
def __init__(self, maximum_size, window_percentage=1):
super().__init__(maximum_size)
self.data = {}
self.cms = CMS(maximum_size)
self.sentinel_window = Node() # LRU
self.sentinel_probation = Node() # SLRU
self.sentinel_protected = Node() # SLRU
self.max_window_size = (self.maximum_size * window_percentage) // 100
max_main = self.maximum_size - self.max_window_size
self.max_protected = max_main * 4 // 5
self.size_window = 0
self.size_protected = 0
def record(self, key, size=1):
self.cms.increment(key)
node = self.data.get(key)
if not node:
self.misses += 1
new_node = Node(key, Node.Status.Window)
new_node.append_to_tail(self.sentinel_window)
self.data[key] = new_node
self.size_window += 1
if self.size_window > self.max_window_size:
self.evict()
return False
else:
self.hits += 1
node.remove()
if node.status == Node.Status.Window:
node.append_to_tail(self.sentinel_window)
elif node.status == Node.Status.Probation:
node.status = Node.Status.Protected
node.append_to_tail(self.sentinel_protected)
self.size_protected += 1
self.demote_protected()
elif node.status == Node.Status.Protected:
node.append_to_tail(self.sentinel_protected)
return True
def demote_protected(self):
if self.size_protected > self.max_protected:
demote = self.sentinel_protected.next_node
demote.remove()
demote.status = Node.Status.Probation
demote.append_to_tail(self.sentinel_probation)
self.size_protected -= 1
def evict(self):
candidate = self.sentinel_window.next_node
candidate.remove()
self.size_window -= 1
candidate.status = Node.Status.Probation
candidate.append_to_tail(self.sentinel_probation)
if len(self.data) > self.maximum_size:
victim = self.sentinel_probation.next_node
evicted = victim if self.cms.frequancy(candidate.data) > self.cms.frequancy(victim.data) else candidate
del self.data[evicted.data]
evicted.remove()
class AdaptiveWTinyLFU(WTinyLFU):
def adjust(self, wanted_window):
if len(self.data) < self.maximum_size:
return
if wanted_window > self.max_window_size:
self.increase_window(wanted_window - self.max_window_size)
elif wanted_window < self.max_window_size:
self.decrease_window(self.max_window_size - wanted_window)
def increase_window(self, amount):
steps = min(amount, self.max_protected)
for _ in range(steps):
self.max_window_size += 1
self.size_window += 1
self.max_protected -= 1
self.demote_protected()
candidate = self.sentinel_probation.next_node
candidate.remove()
candidate.status = Node.Status.Window
candidate.append_to_tail(self.sentinel_window)
def decrease_window(self, amount):
steps = min(amount, self.max_window_size)
for _ in range(steps):
assert amount > 0
self.max_window_size -= 1
self.size_window -= 1
self.max_protected += 1
candidate = self.sentinel_window.next_node
candidate.remove()
candidate.status = Node.Status.Probation
candidate.append_to_head(self.sentinel_probation)
# Hill Climbing aprroach
class WC_WTinyLFU(AdaptiveWTinyLFU):
def __init__(self, maximum_size, window_percentage=1, sample_multiplier=10, pivot=0.05):
super().__init__(maximum_size, window_percentage)
self.hits_in_sample = 0
self.hits_in_prev = 0
self.sample = 0
self.sample_size = sample_multiplier * self.maximum_size
self.pivot = int(pivot * self.maximum_size)
self.increase_direction = False
def record(self, key, size=1):
hit = super().record(key)
if len(self.data) >= self.maximum_size:
self.climb(hit)
def climb(self, hit):
if hit:
self.hits_in_sample += 1
self.sample += 1
if self.sample >= self.sample_size:
if self.hits_in_prev > 0:
if (self.hits_in_prev + self.sample * 0.01) > self.hits_in_sample:
self.increase_direction = not self.increase_direction
if self.increase_direction:
self.increase_window(self.pivot)
else:
self.decrease_window(self.pivot)
self.hits_in_prev = self.hits_in_sample
self.hits_in_sample = 0
self.sample = 0
# Indicator approach
class WI_WTinyLFU(AdaptiveWTinyLFU):
def __init__(self, maximum_size, window_percentage=1):
super().__init__(maximum_size, window_percentage)
self.sample = 0
self.sample_size = 50000
self.indicator = Indicator()
def record(self, key, size=1):
super().record(key)
if len(self.data) >= self.maximum_size:
self.climb(key)
def climb(self, key):
self.indicator.record(key)
self.sample += 1
if self.sample >= self.sample_size:
ind = self.indicator.get_indicator()*80.0/100.0
self.adjust(int(ind*self.maximum_size))
self.indicator.reset()
self.sample = 0
class Indicator(object):
def __init__(self):
self.cms = CMS(5000)
self.hinter_sum = 0
self.hinter_count = 0
self.freqs = Counter() # Alternatively use SpaceSaving
def record(self, key):
hint = self.cms.frequancy(key)
self.hinter_sum += hint
self.hinter_count += 1
self.cms.increment(key)
self.freqs[key] += 1
def get_hint(self):
return self.hinter_sum / self.hinter_count
def est_skew(self):
top_k = [ (i, log(k[1])) for i, k in zip(range(1,71), self.freqs.most_common(70)) ]
return -stats.linregress(top_k)[0]
def get_indicator(self):
skew = self.est_skew()
return (self.get_hint() * ((1 - skew**3) if skew < 1 else 0)) / 15.0
def reset(self):
self.hinter_sum = 0
self.hinter_count = 0
self.freqs.clear()
class Node(object):
def __init__(self, data=None, size=1, status=None):
self.data = data
self.next_node = self
self.prev_node = self
self.status = status
self.size = size
def remove(self):
self.prev_node.next_node = self.next_node
self.next_node.prev_node = self.prev_node
def append_to_tail(self, sentinel):
self.prev_node = sentinel.prev_node
self.next_node = sentinel
self.prev_node.next_node = self
self.next_node.prev_node = self
def append_to_head(self, sentinel):
self.next_node = sentinel.next_node
self.prev_node = sentinel
self.prev_node.next_node = self
self.next_node.prev_node = self
class Status(Enum):
Window = auto()
Probation = auto()
Protected = auto()