forked from khchenTW/MissRateSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeadline_miss_probability.py
More file actions
527 lines (500 loc) · 19.9 KB
/
deadline_miss_probability.py
File metadata and controls
527 lines (500 loc) · 19.9 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# further developed by Jannik Drögemüller, Mats Haring, Franziska Schmidt and Simon Koschel
''' Implementation of the novel multinomial-based approach as detailed in Section 5.
For the actual implementation the binomial case is considered.
Furthermore, since the multinomial-based approach relies on convolution to merge
multinomial representations of tasks the convolution based approach is implemented
in this file as well. '''
from __future__ import division
import random
import math
import numpy as np
from operator import itemgetter, attrgetter
from pkg_resources import get_distribution
from decimal import Decimal
import TDA
''' Calculates the probability of deadline miss as detailed in Section 5.
All job releases of higher priority tasks are considered.
'tasks represents' the given task set,
'prob_abnormal' the probability of abnormal execution, i.e., higher WCET.
'probabilities' tracks the calculated probabilities for each time point
'states' tracks the number of states considered for each time point '''
'''
def calculate(tasks, prob_abnormal, probabilties, states):
tasks = sort(tasks, 'deadline', False)
deadline = tasks[len(tasks)-1]['deadline']
min_time = TDA.min_time(tasks, 'execution')
tasks = sort(tasks, 'execution', True)
all_times = all_releases(tasks, deadline)
times = []
for i in all_times:
if i > min_time:
times.append(i)
times.sort()
for time in times:
prob = calculate_probabiltiy(tasks, time, prob_abnormal, states)
probabilties.append(prob)
probability = 1
for i in range(0, len(times),1):
if (probabilties[i]<probability):
probability = probabilties[i]
return probability
'''
''' KHCHEN: extension for consecutive deadlines, which calculates the probability of deadline miss as detailed in Section 5.
All job releases of higher priority tasks are considered.
The state pruning introduced in Section 6.1 is used
'tasks represents' the given task set,
'prob_abnormal' the probability of abnormal execution, i.e., higher WCET.
'probabilities' tracks the calculated probabilities for each time point
'states' tracks the number of states for each time point
'pruned' tracks the number of states for each time point after pruning '''
def calculate_pruneCON(tasks, prob_abnormal, probabilties, states, pruned, numD):
tasks = sort(tasks, 'deadline', False)
deadline = tasks[len(tasks)-1]['deadline']
min_time = TDA.min_time(tasks, 'execution', numD)
tasks = sort(tasks, 'execution', True)
all_times = all_releases(tasks, deadline*numD)
times = []
for i in all_times:
if i > min_time:
times.append(i)
times.sort()
for time in times:
prob = calculate_probabiltiy_prune(tasks, time, prob_abnormal, states, pruned)
probabilties.append(prob)
probability = 1
for i in range(0, len(times),1):
if (probabilties[i]<probability):
probability = probabilties[i]
return probability
''' Calculates the probability of deadline miss as detailed in Section 5.
All job releases of higher priority tasks are considered.
The state pruning introduced in Section 6.1 is used
'tasks represents' the given task set,
'prob_abnormal' the probability of abnormal execution, i.e., higher WCET.
'probabilities' tracks the calculated probabilities for each time point
'states' tracks the number of states for each time point
'pruned' tracks the number of states for each time point after pruning '''
'''
def calculate_prune(tasks, prob_abnormal, probabilties, states, pruned):
tasks = sort(tasks, 'deadline', False)
deadline = tasks[len(tasks)-1]['deadline']
min_time = TDA.min_time(tasks, 'execution')
tasks = sort(tasks, 'execution', True)
all_times = all_releases(tasks, deadline)
times = []
for i in all_times:
if i > min_time:
times.append(i)
times.sort()
for time in times:
prob = calculate_probabiltiy_prune(tasks, time, prob_abnormal, states, pruned)
probabilties.append(prob)
probability = 1
for i in range(0, len(times),1):
if (probabilties[i]<probability):
probability = probabilties[i]
return probability
'''
''' Calculates the probability of deadline miss as detailed in Section 5.
All job releases of higher priority tasks are considered.
The state pruning introduced in Section 6.1 is used
Furthermore, the reduction of the binomial representation in Section 6.2
'max_error_allowed' describes the maximum total error that is allowed after state
reduction for all binomial representations.
'tasks represents' the given task set,
'prob_abnormal' the probability of abnormal execution, i.e., higher WCET.
'probabilities' tracks the calculated probabilities for each time point
'states' tracks the number of states for each time point
'pruned' tracks the number of states for each time point after pruning
'max_error' tracks the maximum possible error that can occur after the reduction '''
'''
def calculate_prune_reduct(tasks, prob_abnormal, probabilties, states, pruned, max_error, max_error_allowed):
tasks = sort(tasks, 'deadline', False)
deadline = tasks[len(tasks)-1]['deadline']
min_time = TDA.min_time(tasks, 'execution')
tasks = sort(tasks, 'execution', True)
all_times = all_releases(tasks, deadline)
times = []
for i in all_times:
if i > min_time:
times.append(i)
times.sort()
error = []
for time in times:
prob = calculate_probabiltiy_prune_reduct(tasks, time, prob_abnormal, states, pruned, error, max_error_allowed)
probabilties.append(prob)
max_error.append(np.max(error))
probability = 1
for i in range(0, len(times),1):
if (probabilties[i]<probability):
probability = probabilties[i]
return probability
'''
''' Approximates the probability of deadline miss as detailed in Section 5
by only considering the last release of all higher priority tasks.
The state pruning introduced in Section 6.1 is used
'tasks represents' the given task set,
'prob_abnormal' the probability of abnormal execution, i.e., higher WCET.
'probabilities' tracks the calculated probabilities for each time point
'states' tracks the number of states for each time point
'pruned' tracks the number of states for each time point after pruning '''
'''
def approximate_prune(tasks, prob_abnormal, probabilties, states, pruned):
tasks = sort(tasks, 'deadline', False)
deadline = tasks[len(tasks)-1]['deadline']
tasks = sort(tasks, 'execution', True)
times = last_release(tasks, deadline)
times.sort()
for time in times:
prob = calculate_probabiltiy_prune(tasks, time, prob_abnormal, states, pruned)
probabilties.append(prob)
probability = 1
for i in range(0, len(times),1):
if (probabilties[i]<probability):
probability = probabilties[i]
return probability
'''
''' The convolution based approach by Maxim and Cucu-Grosjean [17]'''
'''
def convolution(tasks, prob_abnormal, probabilties, states):
tasks = sort(tasks, 'deadline', False)
deadline = tasks[len(tasks)-1]['deadline']
releases = []
times = []
calculate_releases(tasks, deadline, releases, prob_abnormal)
releases = sorted(releases, key=lambda release:release[0]['time'])
min_time = TDA.min_time(tasks, 'execution')
tasks = sort(tasks, 'execution', True)
all_times = all_releases(tasks, deadline)
distri = empty_distri()
t = 0.0
print('releases: ' + repr(len(releases)))
print('states: ' + repr(math.pow(2,len(releases))))
while (t < deadline):
i = 0
print(len(distri))
job = releases[0]
while(job[0]['time']== t):
distri = convolute(distri, job)
del releases[0]
if len(releases) > 0:
job = releases[0]
else:
break
i = i + 1
if len(releases) > 0:
t = job[0]['time']
else:
t = deadline
prob = calculate_miss_prob(distri, t)
probabilties.append(prob)
times.append(t)
states.append(len(distri))
probability = 1
for i in range(0, len(probabilties),1):
if (probabilties[i]<probability):
probability = probabilties[i]
return probability
'''
''' The convolution based approach by Maxim and Cucu-Grosjean [17] with state merging'''
'''
def convolution_merge(tasks, prob_abnormal, probabilties, states, pruned):
tasks = sort(tasks, 'deadline', False)
deadline = tasks[len(tasks)-1]['deadline']
releases = []
times = []
calculate_releases(tasks, deadline, releases, prob_abnormal)
releases = sorted(releases, key=lambda release:release[0]['time'])
states.append(int(math.pow(2,len(releases))))
min_time = TDA.min_time(tasks, 'execution')
tasks = sort(tasks, 'execution', True)
all_times = all_releases(tasks, deadline)
distri = empty_distri()
t = 0.0
while (t < deadline):
i = 0
job = releases[0]
while(job[0]['time']== t):
distri = convolute(distri, job)
distri = collapse(distri)
del releases[0]
if len(releases) > 0:
job = releases[0]
else:
break
i = i + 1
if len(releases) > 0:
t = job[0]['time']
else:
t = deadline
prob = calculate_miss_prob(distri, t)
probabilties.append(prob)
pruned.append(len(distri))
times.append(t)
probability = 1
for i in range(0, len(probabilties),1):
if (probabilties[i]<probability):
probability = probabilties[i]
return probability
'''
''' Calculates the deadline miss probability for a given point in time'''
def calculate_probabiltiy(tasks, time, prob_abnormal, states):
order = sort(tasks, 'execution', True)
distributions = []
# Generates the binomial distribution of the tasks
for task in order:
distributions.append(get_distribution(task, time, prob_abnormal))
# creates an empty distribution as starting point for the convolution
distri = empty_distri()
# successively convolutes the starting distribution with the
for i in range(0,len(distributions),1):
distri = convolute(distri, distributions[i])
prob = calculate_miss_prob(distri, time)
states.append(len(distri))
return prob
# probability calculation for one time point with pruning
def calculate_probabiltiy_prune(tasks, time, prob_abnormal, states, pruned_states):
order = sort(tasks, 'execution', True)
distributions = []
for task in order:
distributions.append(get_distribution(task, time, prob_abnormal))
min = []
max = []
num_states = []
pruned = []
prob_cut = []
prob = 0.0
minimum = 0.0
maximum = 0.0
for distri in distributions:
min.append(distri[0]['execution'])
minimum = minimum + distri[0]['execution']
max.append(distri[len(distri)-1]['execution'])
maximum = maximum + distri[len(distri)-1]['execution']
max_states = 1
if (minimum > time):
return 1.0
elif (maximum < time):
return 0.0
else:
distri = empty_distri()
for i in range(0,len(distributions),1):
max_states = max_states * len(distributions[i])
minimum = minimum - min[i]
maximum = maximum - max[i]
distri = convolute_prune(distri, distributions[i], minimum, maximum, num_states, pruned, prob_cut, time)
if (len(distri)>100):
distri = collapse(distri)
for i in prob_cut:
prob = prob + i
prob = prob + calculate_miss_prob(distri, time)
max_num_states = np.amax(num_states)
pruned_states.append(max_num_states)
states.append(max_states)
return prob
# probability calculation for one time point with pruning and reduction
def calculate_probabiltiy_prune_reduct(tasks, time, prob_abnormal, states, pruned_states, total_error, max_error_allowed):
order = sort(tasks, 'execution', True)
individual_max_error = max_error_allowed / (len(tasks)-1)
distributions = []
max_error = []
i = 0
for task in order:
i = i + 1
error = []
distri = get_distribution_reduct(task, time, prob_abnormal, error, individual_max_error)
err = np.sum(error)
max_error.append(err)
distributions.append(distri)
tot_err = np.sum(max_error)
total_error.append(tot_err)
min = []
max = []
num_states = []
pruned = []
prob_cut = []
prob = 0.0
minimum = 0.0
maximum = 0.0
for distri in distributions:
min.append(distri[0]['execution'])
minimum = minimum + distri[0]['execution']
max.append(distri[len(distri)-1]['execution'])
maximum = maximum + distri[len(distri)-1]['execution']
max_states = 1
if (minimum > time):
return 1.0
elif (maximum < time):
return 0.0
else:
distri = empty_distri()
for i in range(0,len(distributions),1):
max_states = max_states * len(distributions[i])
minimum = minimum - min[i]
maximum = maximum - max[i]
distri = convolute_prune(distri, distributions[i], minimum, maximum, num_states, pruned, prob_cut, time)
if (len(distri)>100):
distri = collapse(distri)
for i in prob_cut:
prob = prob + i
prob = prob + calculate_miss_prob(distri, time)
max_num_states = np.amax(num_states)
pruned_states.append(max_num_states)
states.append(max_states)
return prob
# calculates the binomial distribution for a given task, time, and probability of abnormal execution
def get_distribution(task, time, prob_abnormal):
distribution = []
n = math.ceil(time/task['deadline'])
for k in range(0, int(n) + 1, 1):
pair={}
pair['misses']=k
pair['prob']= (Decimal(math.factorial(n)) / (Decimal(math.factorial(k))*Decimal(math.factorial(n-k))))*Decimal(math.pow(prob_abnormal, k))*Decimal(math.pow((1-prob_abnormal),(n-k)))
pair['execution']=k*task['abnormal_exe']+(n-k)*task['execution']
distribution.append(pair)
return distribution
# calculating the reducted distribution for a given task
def get_distribution_reduct(task, time, prob_abnormal, max_error, task_error):
distribution = []
n = math.ceil(time/task['deadline'])
for k in range(0, int(n) + 1, 1):
pair={}
pair['misses']=k
pair['prob']= (math.factorial(n)/(math.factorial(k)*math.factorial(n-k)))*math.pow(prob_abnormal, k)*math.pow((1-prob_abnormal),(n-k))
pair['execution']=k*task['abnormal_exe']+(n-k)*task['execution']
distribution.append(pair)
# if the original distribution has 6 states or less (5 jobs or less) it is directly kept
if (len(distribution)<= 6):
return distribution
else:
# otherwise all states are kept (starting from the ones with highest probability)
# until the total probability of the remaining states is less than the 'task_error'
total_prob = 0.0
reducted_distribution = []
i = 0
while (i < 6):
reducted_distribution.append(distribution[0])
total_prob = total_prob + distribution[0]['prob']
del distribution[0]
i = i + 1
i = 0
while (len(distribution)> 0 and (total_prob <= 1 - task_error)):
reducted_distribution.append(distribution[0])
total_prob = total_prob + distribution[0]['prob']
del distribution[0]
if (len(distribution) > 0):
pair={}
pair['prob']= 0.0
while(len(distribution) > 0):
pair['prob']=pair['prob'] + distribution[0]['prob']
pair['execution']=distribution[0]['execution']
if (len(distribution)>1):
max_error.append(distribution[0]['prob'])
del distribution[0]
reducted_distribution.append(pair)
return reducted_distribution
# direct convolution of two distributions
def convolute(dist1, dist2):
dist = []
for state1 in dist1:
for state2 in dist2:
pair={}
pair['prob']=state1['prob']*state2['prob']
pair['execution']=state1['execution']+state2['execution']
dist.append(pair)
return dist
''' direct convolution of two distributions. The pruning techniques presented in
Section 6.2 are used to prune away unnecessary states. '''
def convolute_prune(dist1, dist2, minimum, maximum, num_states, pruned, prob_cut, time):
prob = 0.0
dist = []
prune = 0
states = 0
for state1 in dist1:
for state2 in dist2:
states = states + 1
pair={}
pair['prob']=state1['prob']*state2['prob']
pair['execution']=state1['execution']+state2['execution']
# if a new state will always result in a deadline miss it can be pruned
# probability of the state is added to the miss probabiltiy
if ((pair['execution'] + minimum) > time):
prune = prune + 1
prob = prob + pair['prob']
# if a new state will never result in a deadline miss it can be pruned
elif((pair['execution'] + maximum) < time):
prune = prune + 1
# otherwise, it has to be considered further
else:
dist.append(pair)
prob_cut.append(prob)
pruned.append(prune)
num_states.append(states)
return dist
# collapsing the state space by merging states with same workload
def collapse(distri):
distri = sorted(distri, key=lambda r:r['execution'])
collapsed = []
current={}
current['prob']= 0.0
current['execution']=distri[0]['execution']
for state in distri:
if (current['execution'] == state['execution']):
current['prob'] = current['prob'] + state['prob']
else:
collapsed.append(current)
current = state
collapsed.append(state)
return collapsed
# Calculates the deadline miss probability for a given distribution and the time,
# i.e., tests if the workload is larger than the execution time and sums up the
# related probabilities.
def calculate_miss_prob(distribution, time):
prob = np.longdouble(0.0)
for dist in distribution:
if (dist['execution']>time):
prob = prob + dist['prob']
return prob
# calculates the time for the last releases of all tasks before the deadline (and adds the deadline)
# (Binomial based approach)
def last_release(tasks, deadline):
times = []
for task in tasks:
times.append(math.floor(deadline/task['deadline'])*task['deadline'])
return times
# calculates the time for all releases of all tasks before the deadline (and adds the deadline)
# (Binomial based approach)
def all_releases(tasks, deadline):
times = []
times.append(deadline)
for task in tasks:
count = task['period']
while(count < deadline):
times.append(count)
count = count + task['period']
return times
# creates the jobs that have to be convoluted in the convolution based approach
def calculate_releases(tasks, deadline, releases, prob_abnormal):
for task in tasks:
time = 0.0
while(time < deadline):
distribution = []
for k in range(0, 2, 1):
pair={}
pair['time']=time
pair['prob']= math.pow(prob_abnormal, k)*math.pow((1-prob_abnormal),(1-k))
pair['execution']=k*task['abnormal_exe']+(1-k)*task['execution']
distribution.append(pair)
releases.append(distribution)
time = time + task['period']
def sort(tasks, criteria, reverse_order):
return sorted(tasks, key=lambda item:item[criteria], reverse=reverse_order)
# initializes an empty distribution (workload 0 with probability 1)
def empty_distri():
distri = []
pair={}
pair['misses']=''
pair['prob']=np.longdouble(1.0)
pair['execution']=0.0
distri.append(pair)
return distri