-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqueezer.py
220 lines (158 loc) · 5.5 KB
/
squeezer.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
#
# Copyright (c) 2018 Nutanix Inc. All rights reserved.
#
# Author: David Vrabel <david.vrabel@nutanix.com>
# Jonathan Davies <jonathan.davies@nutanix.com>
#
import numpy as np
import matplotlib.pyplot as plot
MAJOR_FAULT_LIMIT = 2
MINOR_FAULT_LIMIT = 5
class Squeezer(object):
def __init__(self):
self.last_work = 0
self.last_major_faults = 0
self.last_minor_faults = 0
self.ticks = []
self.limit = []
self.work = []
self.major_faults = []
self.minor_faults = []
self.longterm_estimates = []
self.longterm_estimate = None
def squeeze(self, host):
pass
def count_faults(self, host):
major_faults = host.mm.major_faults - self.last_major_faults
self.last_major_faults = host.mm.major_faults
minor_faults = host.mm.minor_faults - self.last_minor_faults
self.last_minor_faults = host.mm.minor_faults
if host.tick > 0:
self.major_faults.append(major_faults)
self.minor_faults.append(minor_faults)
return (major_faults, minor_faults)
def _calc_stats(self, host):
if host.tick == 0:
return
self.limit.append(host.mm.limit)
self.work.append(self._work_done(host))
self.ticks.append(host.tick)
if self.longterm_estimate:
self.longterm_estimates.append(self.longterm_estimate)
def _work_done(self, host):
if len(self.ticks) > 0:
ticks = host.tick - self.ticks[-1]
else:
ticks = host.tick
work = float(host.vcpu.work_done - self.last_work) / ticks
self.last_work = host.vcpu.work_done
return work
def plot(self, host):
x = map(lambda x: x/1000, self.ticks)
plot.subplot(221)
plot.xlabel("Time/ktick")
plot.ylabel("Limit/pages")
plot.ylim((0.0, host.mm.total))
plot.plot(x, self.limit, label="Short-term")
if self.longterm_estimate:
plot.plot(x, self.longterm_estimates, label="Long-term")
plot.legend()
plot.grid(True)
plot.subplot(223)
plot.xlabel("Time/ktick")
plot.ylabel("Work done")
plot.ylim((0.0, 1.0))
plot.plot(x, self.work)
plot.grid(True)
plot.subplot(222)
host.mm.plot_accesses()
plot.subplot(224)
plot.xlabel("Time/ktick")
plot.ylabel("Faults")
plot.plot(x, self.major_faults, label="Major")
#plot.plot(x, self.minor_faults, label="Minor")
plot.legend()
plot.grid(True)
class NullSqueezer(Squeezer):
pass
class StaticSqueezer(Squeezer):
def __init__(self, limit):
Squeezer.__init__(self)
self.estimate = limit
def squeeze(self, host):
major_faults, _ = self.count_faults(host)
host.mm.limit = self.estimate
self._calc_stats(host)
class SimpleSqueezer(Squeezer):
def __init__(self):
Squeezer.__init__(self)
def squeeze(self, host):
major_faults, _ = self.count_faults(host)
# Squeeze?
if major_faults < MAJOR_FAULT_LIMIT and host.mm.limit > 1:
host.mm.limit -= 1
# Desqueeze?
elif major_faults > MAJOR_FAULT_LIMIT:
host.mm.limit += 1
self._calc_stats(host)
class Mk1Squeezer(Squeezer):
def __init__(self):
Squeezer.__init__(self)
def squeeze(self, host):
major_faults, minor_faults = self.count_faults(host)
limit = host.mm.limit
if major_faults < MAJOR_FAULT_LIMIT:
limit -= 2
elif major_faults > MAJOR_FAULT_LIMIT:
limit += 2
elif minor_faults < MINOR_FAULT_LIMIT:
limit -= 1
elif minor_faults > MINOR_FAULT_LIMIT:
limit += 1
host.mm.limit = max(limit, 1)
self._calc_stats(host)
class Mk2Squeezer(Squeezer):
def __init__(self):
Squeezer.__init__(self)
self.estimate = []
def squeeze(self, host):
major_faults, _ = self.count_faults(host)
limit = host.mm.limit
if major_faults < MAJOR_FAULT_LIMIT:
limit -= (MAJOR_FAULT_LIMIT - major_faults)
elif major_faults > MAJOR_FAULT_LIMIT:
limit += major_faults
limit = max(limit, 1)
self.estimate.append(limit)
if len(self.estimate) > 10:
self.estimate = self.estimate[1:]
host.mm.limit = sum(self.estimate) / len(self.estimate)
self._calc_stats(host)
class Mk3Squeezer(Squeezer):
def __init__(self):
Squeezer.__init__(self)
def squeeze(self, host):
major_faults, minor_faults = self.count_faults(host)
limit = host.mm.limit
if major_faults < MAJOR_FAULT_LIMIT:
limit -= (MAJOR_FAULT_LIMIT - major_faults)
elif major_faults > MAJOR_FAULT_LIMIT:
limit += major_faults
host.mm.limit = max(limit, 1)
self._calc_stats(host)
class Mk4Squeezer(Squeezer):
def __init__(self):
Squeezer.__init__(self)
self.faults = 0
A = 0.6
def squeeze(self, host):
major_faults, _ = self.count_faults(host)
self.faults = self.A * major_faults + (1 - self.A) * self.faults
limit = host.mm.limit
if self.faults < MAJOR_FAULT_LIMIT:
limit -= (MAJOR_FAULT_LIMIT - self.faults / 2)
elif self.faults > MAJOR_FAULT_LIMIT:
limit += self.faults / 2
host.mm.limit = max(limit, 1)
print limit
self._calc_stats(host)