-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.py
480 lines (413 loc) · 17.7 KB
/
filter.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env python
from __future__ import division
import argparse
import random
import sys
import math
import time
import traceback
import multiprocessing
import multiprocessing.pool
from collections import defaultdict
from csv import DictWriter
from itertools import product
from math import ceil
from multiprocessing import cpu_count
from numpy import arange
from pprint import pprint
from copy import deepcopy
from timeout_decorator import timeout
from schedcat.model.tasks import SporadicTask, TaskSystem
from schedcat.generator.tasks import exponential, uniform
from schedcat.generator.tasksets import NAMED_PERIODS, NAMED_UTILIZATIONS
from schedcat.util.storage import storage
import schedcat.sched.edf as edf
import schedcat.model.resources as resources
import schedcat.generator.tasks as tasks
import schedcat.locking.bounds as bounds
from schedcat.util.time import ms2us
# NEW
import schedcat.sched.fp as fp
from numpy import random
# FIXME this is not working for some reason
# I have coppied and pasted this class below to resolve this
#from util.stats import BernoulliEstimator
import scipy.stats
#MIN_SAMPLES = 1000
#MAX_SAMPLES = 100000
MIN_SAMPLES = 100
MAX_SAMPLES = 1000
MAX_CI = 0.05
CONFIDENCE = 0.95
# These need to be set from Jay's measurements
# Need milliseconds
#trying microseconds???
#BUFFER_RATE = 20000000000/14687
# FLUSH + CHECK / CLOCK SPEED
BUFFER_DELAY = 8832.513156/20 # (in micro seconds)
INDV_DELAY = (BUFFER_DELAY/512) + 27.5
INDV_DELAY_FILTER = (22197.39621/20)/512 + 27.5
INDV_DELAY_SHERLOC = (9954.009864/20)/512 + 21.19754873
# MEAN = 28.68593064
# STDEV = 22.86616364
# NAMED_CS_LENGTHS = {
# 'short' : uniform(1, 15),
# 'moderate' : uniform(1, 100),
# 'long' : uniform(5, 1280)
# }
# FIXME This is to resolve import error
class BernoulliEstimator(object):
def __init__(self, default_mean = 0, default_count = 0):
self.mean = default_mean
self.count = default_count
def add_sample(self, sample):
self.mean = (self.mean*self.count + sample)/(self.count+1)
self.count+=1
def confidence_interval(self, confidence = 0.95):
if self.count == 0:
return 1
else:
zalpha = scipy.stats.t.ppf((1+confidence)/2.0, self.count-1)
return zalpha * math.sqrt(self.mean*(1-self.mean)/self.count)
#NAMED_CS_LENGTHS = {
# 'small' : exponential(10, 100, 1000),
# 'moderate' : uniform(1, 100),
# 'large' : exponential(100, 1000, 10000),
# 'variant' : uniform(1, 100000)
#}
# the following two classes are take from
# http://stackoverflow.com/questions/6974695/python-process-pool-non-daemonic
# without time, you cannot use timeouts within the threads.
class NoDaemonProcess(multiprocessing.Process):
def _get_daemon(self):
return False
def _set_daemon(self, value):
pass
daemon = property(_get_daemon, _set_daemon)
class MyPool(multiprocessing.pool.Pool):
Process = NoDaemonProcess
class ExperimentManager(object):
def __init__(self, parameters, outfile):
random.seed(12345)
self.parameters = parameters
self.outfile = outfile
self.out = DictWriter(outfile, parameters.keys()+DesignPoint.metrics)
self.out.writeheader()
self.create_design_points()
def valid(self, dp):
return dp.processors >= dp.sys_util
def create_design_points(self, shuffle = True):
self.design_points = filter(self.valid,
[DesignPoint(**dict(zip(self.parameters.keys(),val)))
for val in product(*self.parameters.values())])
if shuffle:
random.shuffle(self.design_points)
def run(self, processors, status = True):
if processors == 1:
self._run_serial(status)
else:
self._run_parallel(processors, status)
self.finish()
return # need to return the results here.
#Useful for debugging purposes.
def _run_serial(self, status = True):
rows = map(lambda dp: dp.run(), self.design_points)
for i, row in enumerate(rows):
if status and (self.outfile != sys.stdout):
sys.stderr.write('\rdone {0:%}'.format(i/len(rows)))
self.out.writerow(row)
def _run_parallel(self, processors, status = True):
#pool = Pool(processes = processors)
pool = MyPool(processes = processors)
try:
for i, row in enumerate(pool.imap_unordered(pickleable_function, self.design_points)):
if status and (self.outfile != sys.stdout):
sys.stderr.write('\rdone {0:%}'.format(i/len(self.design_points)))
self.out.writerow(row)
pool.close()
except Exception as e:
pool.terminate()
print e
raise
def finish(self):
self.outfile.close()
# for whatever reason, imap_unordered will not take a lambda function, because
# it is not pickleable. This seems stupid.
def pickleable_function(dp):
return dp.run()
class DesignPoint(object):
# Update this
metrics = ["NOFILTER","FILTER_90%","FILTER_99%","FILTER_99.9%"] #["OMLP", "FMLPOblivious", "FMLPAware", "NOLOCK"]
# a graph which represents which tests can decide a metric on a given run.
test_graph = {}
def __init__(self, **levels):
self.__dict__.update(levels)
self.levels = levels
self.data = dict([(m, BernoulliEstimator()) for m in DesignPoint.metrics])
def run(self):
while not self.complete():
org_ts = self.create_task_set()
# Give each task it's own (no select) buffer rate
for t in org_ts:
t.buffer_rate = 20/(random.uniform(5,100) * 512)
# Write some logic to run all your tests here. This is often study
# specific, as you can exploit domincance results to accelerate
# things. A classic example, if the task system isn't schedulable
# without any synchronization, it isn't schedulable with it.
if True: #fp.rta.is_schedulable(self.processors, org_ts):
#self.data["RTA_NORM"].add_sample(True)
tests_remaining = [self.no_filter, self.filter10, self.filter1, self.filter0] #[self.omlp, self.fmlp_sob, self.fmlp_saware]
for test in tests_remaining:
ts = org_ts.copy()
test(ts)
else:
for m in DesignPoint.metrics:
self.data[m].add_sample(False)
return dict(self.levels.items() + [(k,v.mean) for (k,v) in self.data.iteritems()])
# New testing functions
def no_filter(self, ts):
def rta(task,higher_prio_tasks):
own_demand = task.__dict__.get('prio_inversion', 0) + task.cost
#hp_jitter = task.__dict__.get('jitter', 0)
# see if we find a point where the demand is satisfied
delta = sum([t.cost for t in higher_prio_tasks]) + own_demand
while delta <= task.deadline:
# Added delay term + 1 here for pessimism
demand = own_demand #+ (((buffer_rate * delta ) + 1) * BUFFER_DELAY)
for t in higher_prio_tasks:
demand += t.cost * int(ceil(delta/ t.period)) * (1 + (t.buffer_rate * INDV_DELAY))
# demand *= (1 + (task.buffer_rate * INDV_DELAY))
demand += (512 * INDV_DELAY) + (task.cost * task.buffer_rate * INDV_DELAY)
if demand == delta:
# yep, demand will be met by time
task.response_time = delta #+ task.__dict__.get('jitter', 0)
return True
else:
# try again
delta = demand
# if we get here, we didn't converge
return False
def test():
#print(ts)
# for t in ts:
# t.buffer_rate = 20/(random.uniform(5,100) * 512)
#print buffer_rate
ts.sort(key=lambda task: task.period)
for i, t in enumerate(ts):
if not rta(t, ts[0:i]):
return False
return True
res = test()
self.data["NOFILTER"].add_sample(res)
return res
def filter10(self, ts):
def rta(task,higher_prio_tasks):
own_demand = task.__dict__.get('prio_inversion', 0) + task.cost
#hp_jitter = task.__dict__.get('jitter', 0)
# see if we find a point where the demand is satisfied
delta = sum([t.cost for t in higher_prio_tasks]) + own_demand
while delta <= task.deadline:
# Added delay term + 1 here for pessimism
demand = own_demand #+ (((buffer_rate * delta ) + 1) * BUFFER_DELAY)
for t in higher_prio_tasks:
demand += t.cost * int(ceil(delta/ t.period)) * (1 + (t.buffer_rate * INDV_DELAY_FILTER))
# demand *= (1 + (task.buffer_rate * INDV_DELAY))
demand += (512 * INDV_DELAY_FILTER) + (task.cost * task.buffer_rate * INDV_DELAY_FILTER)
if demand == delta:
# yep, demand will be met by time
task.response_time = delta #+ task.__dict__.get('jitter', 0)
return True
else:
# try again
delta = demand
# if we get here, we didn't converge
return False
def test():
#print(ts)
for t in ts:
t.buffer_rate = t.buffer_rate/10
#print buffer_rate
ts.sort(key=lambda task: task.period)
for i, t in enumerate(ts):
if not rta(t, ts[0:i]):
return False
return True
res = test()
self.data["FILTER_90%"].add_sample(res)
return res
def filter1(self, ts):
def rta(task,higher_prio_tasks):
own_demand = task.__dict__.get('prio_inversion', 0) + task.cost
#hp_jitter = task.__dict__.get('jitter', 0)
# see if we find a point where the demand is satisfied
delta = sum([t.cost for t in higher_prio_tasks]) + own_demand
while delta <= task.deadline:
# Added delay term + 1 here for pessimism
demand = own_demand #+ (((buffer_rate * delta ) + 1) * BUFFER_DELAY)
for t in higher_prio_tasks:
demand += t.cost * int(ceil(delta/ t.period)) * (1 + (t.buffer_rate * INDV_DELAY_FILTER))
# demand *= (1 + (task.buffer_rate * INDV_DELAY))
demand += (512 * INDV_DELAY_FILTER) + (task.cost * task.buffer_rate * INDV_DELAY_FILTER)
if demand == delta:
# yep, demand will be met by time
task.response_time = delta #+ task.__dict__.get('jitter', 0)
return True
else:
# try again
delta = demand
# if we get here, we didn't converge
return False
def test():
#print(ts)
for t in ts:
t.buffer_rate = t.buffer_rate/100
#print buffer_rate
ts.sort(key=lambda task: task.period)
for i, t in enumerate(ts):
if not rta(t, ts[0:i]):
return False
return True
res = test()
self.data["FILTER_99%"].add_sample(res)
return res
def filter0(self, ts):
def rta(task,higher_prio_tasks):
own_demand = task.__dict__.get('prio_inversion', 0) + task.cost
#hp_jitter = task.__dict__.get('jitter', 0)
# see if we find a point where the demand is satisfied
delta = sum([t.cost for t in higher_prio_tasks]) + own_demand
while delta <= task.deadline:
# Added delay term + 1 here for pessimism
demand = own_demand #+ (((buffer_rate * delta ) + 1) * BUFFER_DELAY)
for t in higher_prio_tasks:
demand += t.cost * int(ceil(delta/ t.period)) * (1 + (t.buffer_rate * INDV_DELAY_FILTER))
# demand *= (1 + (task.buffer_rate * INDV_DELAY))
demand += (512 * INDV_DELAY_FILTER) + (task.cost * task.buffer_rate * INDV_DELAY_FILTER)
if demand == delta:
# yep, demand will be met by time
task.response_time = delta #+ task.__dict__.get('jitter', 0)
return True
else:
# try again
delta = demand
# if we get here, we didn't converge
return False
def test():
#print(ts)
for t in ts:
t.buffer_rate = t.buffer_rate/1000
#print buffer_rate
ts.sort(key=lambda task: task.period)
for i, t in enumerate(ts):
if not rta(t, ts[0:i]):
return False
return True
res = test()
self.data["FILTER_99.9%"].add_sample(res)
return res
# NEW
# ---------------------------------------------------------------
#OLD
def omlp(self, ts):
#@timeout(10, use_signals = False)
def test():
bounds.apply_global_omlp_bounds(ts, self.processors)
return edf.is_schedulable(self.processors, ts,
want_baruah = True,
want_rta = False,
want_ffdbf = False,
want_load = False )#,
#want_la = False)
#try:
res = test()
#except:
# res = False
self.data["OMLP"].add_sample(res)
return res
def fmlp_sob(self, ts):
#@timeout(10, use_signals = False)
def test():
bounds.apply_global_fmlp_sob_bounds(ts)
return edf.is_schedulable(self.processors, ts,
want_baruah = True,
want_rta = False,
want_ffdbf = False,
want_load = False )#,
#want_la = False)
#try:
res = test()
#except:
# res = False
self.data["FMLPOblivious"].add_sample(res)
return res
def fmlp_saware(self, ts):
#@timeout(10, use_signals = False)
def test():
bounds.apply_generalized_fmlp_bounds(ts, self.processors, using_edf=True)
return edf.is_schedulable(self.processors, ts,
want_baruah = False,
want_rta = False,
want_ffdbf = False,
want_load = False )#,
#want_la = True)
#try:
res = test()
#except:
# res = False
self.data["FMLPAware"].add_sample(res)
return res
def complete(self):
for metric, stat in self.data.iteritems():
if stat.count < MIN_SAMPLES:
return False
elif stat.count > MAX_SAMPLES:
return True
elif stat.confidence_interval(CONFIDENCE) > MAX_CI:
return False
return True
def create_task_set(self):
tg = tasks.TaskGenerator(period = NAMED_PERIODS[self.period],
util = NAMED_UTILIZATIONS[self.task_util])
ts = tg.make_task_set(max_util = self.sys_util, squeeze = True, time_conversion=ms2us)
# resources.initialize_resource_model(ts)
# bounds.assign_edf_preemption_levels(ts)
# for t in random.sample(ts, int(self.req_perc * len(ts))):
# t.resmodel[0].add_request(int(NAMED_CS_LENGTHS[self.cs_length]()))
for t in ts:
t.partition = 0
t.response_time = t.deadline
return ts
# Helper function to more easily specify parameters
def myrange(start, end, inc):
return arange(start, end+inc, inc)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-o', "--outfile", type = argparse.FileType('w'),
default = sys.stdout,
help = "File to output csv file to")
parser.add_argument('-p', "--pretend", action='store_true',
help = "Only print design point, do not execute")
parser.add_argument('-m', "--processors", default=cpu_count(), type = int,
help="Number of processors to execute on")
parser.add_argument('-q', "--quiet", action='store_true',
help = "Quash all progress and status updates")
args = parser.parse_args()
params = storage()
params.processors = [1]#[2, 4]
params.task_util = NAMED_UTILIZATIONS #['uni-light', 'uni-medium', 'uni-heavy']
params.period = NAMED_PERIODS #['uni-short', 'uni-moderate', 'uni-long']#, 'uni-broad']
# params.task_util = ['exp-light', 'exp-medium']
# params.period = ['uni-moderate']
params.sys_util = myrange(0.1, 1.1, 0.1)
#params.cs_length = ['small', 'moderate', 'large', 'variant']
# params.cs_length = ['moderate', 'long']
# params.req_perc = [0.1, 1.0]
exp = ExperimentManager(params, args.outfile)
if args.pretend:
print exp.design_points
if not args.quiet:
print "Total design points: ", len(exp.design_points)
if not args.pretend:
exp.run(args.processors, not args.quiet)
if __name__ == '__main__':
main()