-
Notifications
You must be signed in to change notification settings - Fork 1
/
change_log_fetcher.py
189 lines (149 loc) · 6.26 KB
/
change_log_fetcher.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
from prefetch_modeler.core import ContinueBucket, GlobalCapacityBucket, RateBucket, \
Rate, Duration, ForkBucket, Bucket, GateBucket
from prefetch_modeler.prefetcher_type import ConstantRatePrefetcher
from constant_distance_prefetcher import ConstantDistancePrefetcher
from dataclasses import dataclass
from fractions import Fraction
import itertools
import math
from numpy import mean
@dataclass
class LatencyLogEntry:
tick: int
in_storage: int
latency: float
@dataclass
class WaitLogEntry:
tick: int
in_storage: int
wait: float
@dataclass
class ChangeLogEntry:
tick: int
prefetch_distance: int
change: int
upvote: int
downvote: int
class ChangeLogFetcher(ConstantDistancePrefetcher):
headroom = 20
target_idle_time = 20
def __init__(self, *args, **kwargs):
self.prefetch_distance = self.headroom
# new prefetch distance
self.change_log = [ChangeLogEntry(tick=0,
prefetch_distance=self.prefetch_distance,
change=5, upvote=1, downvote=1)]
self.waited_at = None
self.adjust = True
super().__init__(*args, **kwargs)
def remove(self, io):
# submission time should be for cached too?
if not getattr(io, "cached", False):
io.submission_time = self.tick
if self.waited_at is not None:
io.wait_time = self.tick - self.waited_at
else:
io.wait_time = 0
return super().remove(io)
@property
def in_storage(self):
return len(self.pipeline['minimum_latency']) + \
len(self.pipeline['inflight']) + \
len(self.pipeline['deadline'])
def max_buffers(self):
if self.adjust is False:
return int(self.prefetch_distance)
if self.waited_at is not None:
wait_time = self.tick - self.waited_at
else:
wait_time = 0
idle_time = sum(self.tick - io.completion_time for io in self.pipeline['completed'])
idle_time -= self.headroom * self.target_idle_time
delta = wait_time - idle_time
self.adjust = False
self.info['delta'] = delta
self.info['wait_time'] = wait_time
self.info['idle_time'] = idle_time
if len(self.change_log) < 2:
return int(self.prefetch_distance)
accumulated_change = 0
for entry in list(reversed(self.change_log))[:20]:
if entry.upvote > entry.downvote * 1.2:
accumulated_change += entry.change * 1
elif entry.downvote > entry.upvote * 1.2:
accumulated_change -= entry.change * 1
print("adjust", accumulated_change)
accumulated_change = int(accumulated_change)
if accumulated_change == 0:
accumulated_change = 1 if self.tick % 2 == 0 else -1
new_prefetch_distance = self.prefetch_distance + accumulated_change
if new_prefetch_distance <= 0:
new_prefetch_distance = 1
if new_prefetch_distance != self.prefetch_distance:
self.change_log.append(ChangeLogEntry(tick=self.tick,
prefetch_distance=new_prefetch_distance,
change = accumulated_change,
upvote=1, downvote=1))
print(f'old pfd: {self.prefetch_distance}. new pfd: {new_prefetch_distance}')
self.prefetch_distance = new_prefetch_distance
return int(self.prefetch_distance)
@property
def cnc(self):
return len(self.pipeline['completed'])
def reaction(self):
# Determine current wait time
if self.waited_at is None and self.cnc < self.headroom:
self.waited_at = self.tick
elif self.waited_at is not None and self.cnc >= self.headroom:
self.waited_at = None
completed = self.pipeline['completed']
consumed = self.pipeline['consumed']
# Record idle time on each completed not consumed IO
# do this for cached IOs too?
for io in completed:
io.completion_time = getattr(io, "completion_time", self.tick)
# skip if we don't have a consumption
if not completed.info['to_move']:
return
# skip if we don't have any information
if len(consumed.consumption_log) < 2:
new_prefetch_distance = self.prefetch_distance + 1
self.change_log.append(ChangeLogEntry(tick=self.tick,
prefetch_distance=new_prefetch_distance,
change = 1,
upvote=1, downvote=1))
print(f'old pfd: {self.prefetch_distance}. new pfd: {new_prefetch_distance}')
self.prefetch_distance = new_prefetch_distance
return
new = consumed.consumption_log[-1]
# old = consumed.consumption_log[-2]
old = None
for i, consumed_io in enumerate(reversed(consumed.consumption_log)):
if consumed_io.consumed < new.submitted:
old = consumed_io
break
if old is None:
return
print(new.consumed - new.submitted, old.consumed - old.submitted)
# The time to consume increased. Bad!
if new.consumed - new.submitted >= old.consumed - old.submitted:
responsible_change = None
for change in reversed(self.change_log):
if change.tick < new.submitted:
responsible_change = change
break
if responsible_change is not None:
self.adjust = True
responsible_change.downvote += 1
# The time to consume decreased. Good!
else:
responsible_change = None
for change in reversed(self.change_log):
if change.tick < new.submitted:
responsible_change = change
break
if responsible_change is not None:
self.adjust = True
responsible_change.upvote += 1
def next_action(self):
return self.tick + 1 if self.adjust else super().next_action()