-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartitioning.py
executable file
·279 lines (253 loc) · 8.6 KB
/
partitioning.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
#!/usr/bin/env python3
# ------------------------------------------
# partitioning.py : Partitioning strategies:
# Best fist, First fit, Next fit
# Author: Ragesh RAMACHANDRAN
# -----------------------------------------
import json
import copy
import operator
import random
from sys import *
from math import gcd
import math
import numpy as np
# Data structure to store the task sets
class task:
def __init__(self,task_id=None, period=None, WCET=None, U=None):
self.task_id = task_id
self.period = period
self.WCET = WCET
self.U = U
# Data structure to store the processor data
class core:
def __init__(self,core_id=None,core_U=None,core_rem_U=None, task_ID=None,task_U=None):
self.core_id = core_id
self.core_U = core_U
self.core_rem_U = core_rem_U
self.task_ID = task_ID
self.task_U = task_U
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
def hyperperiod():
temp = []
for i in range(n):
temp.append(tasks[i].period)
HP = temp[0]
for i in temp[1:]:
HP = HP*i//gcd(HP, i)
print ("\n\tHyperperiod:",HP)
return HP
def random_data():
global n # Number of tasks to be partitioned
global tasks # List that stores the instances of tasks
tasks = []
random.seed() #Random seeding
n = random.randrange(2,7) #random in range of 2 and 7
print("number of tasks",n)
for i in range(n):
task_id = i
period = random.randrange(9,20) #random period
WCET = random.randrange(4,8) #random WCET
u = WCET/period
# Limit U by 2 decimal places without rounding off
U = truncate(u,2)
tasks.append(task(task_id,period,WCET,U))
# Tasks are sorted based on their period and displayed
tasks = sorted(tasks, key=lambda tasks:tasks.period)
for i in range(n):
print("-----------------")
print("TASK %d"%(i+1))
print("-----------------")
print("Period ",tasks[i].period)
print("WCET ",tasks[i].WCET)
print("Utilization",tasks[i].U)
print("-----------------")
print("\n\n")
def read_data():
global n # Number of tasks to be partitioned
global tasks # List that stores the instances of tasks
tasks = []
n = int(input("\n \t\tEnter number of tasks:"))
# Read data from user
for i in range(n):
task_id = i
print("\nEnter Period of task T",i,":")
period = int(input())
print("Enter the WCET of task C",i,":")
WCET = int(input())
u = WCET/period
# Limit U by 2 decimal places without rounding off
U = truncate(u,2)
tasks.append(task(task_id,period,WCET,U))
# Tasks are sorted based on their period and displayed
tasks = sorted(tasks, key=lambda tasks:tasks.period)
for i in range(n):
print("-----------------")
print("TASK %d"%(i+1))
print("-----------------")
print("Period ",tasks[i].period)
print("WCET ",tasks[i].WCET)
print("Utilization",tasks[i].U)
print("-----------------")
print("\n\n")
def schedulability():
global sched_factor
# Schedulability test for RM scheduling
sched_fac = n*(2**(1/n)-1)
# to limit only 2 decimal places without rounding off
sched_factor = truncate(sched_fac, 2)
print("\tsched_factor", sched_factor)
def NEXT_FIT():
# 'n' tasks and 'm' processors
m = 1 #core counter
c = 1 #initial core count
cores = [] #instance list of core class
core_rem = sched_factor #Store remaining U factor of core
for i in range(n):
# if a task cannot fit into same core
if(tasks[i].U > core_rem): #task does not fit into same core
c = c + 1 #task i is added to a new core
core_rem = sched_factor - tasks[i].U #calculate rem capacity
core_rem = truncate(core_rem,2)
cores.append(core(c, sched_factor, core_rem,i,tasks[i].U))
# If a task fit into a same core
else:
core_rem = core_rem - tasks[i].U #task run on same core
core_rem = truncate(core_rem,2)
cores.append(core(c, sched_factor, core_rem,i,tasks[i].U))
# if there are no new cores added then default value = 1
if(c>m):
m = c
# sorting the processors based on core_id
cores = sorted(cores, key=lambda cores:cores.core_id )
for i in range(len(cores)):
print("--------------------------------")
print("\nCORE %d"%cores[i].core_id)
print("\tcore number ",cores[i].core_id)
print("\tcore load capacity",cores[i].core_U)
print("\tcore rem capacity ",cores[i].core_rem_U)
print("\tTask in core ",cores[i].task_ID)
print("\tTask Utilization ",cores[i].task_U)
print("--------------------------------")
print("\n\tNumber of processors used for NEXT FIT",m)
dispaly_metrics(cores)
def FIRST_FIT():
# 'n' tasks and 'res' processors
global need
res = 0
core_remain = [0]*n # array to store remaining space in cores
cores = [] #instance list of core class
core_rem = sched_factor #Store remaining U factor of core
for i in range(n):
need = 0
# Find the first core that can schedule the task
for j in range(res):
if core_remain[j] >= tasks[i].U:
core_remain[j] = tasks[i].U - core_remain[j]
cores.append(core(res,
sched_factor,
truncate(tasks[i].U - core_remain[j] ,2),
i,
tasks[i].U))
break
else:
need = need +1
# If no core can schedule the task add new core
if need == res:
core_remain[res] = sched_factor - tasks[i].U
res = res +1 #task i is added to a new core
cores.append(core(res,
sched_factor,
truncate(sched_factor - tasks[i].U,2),
i,
tasks[i].U))
for i in range(len(cores)):
print("--------------------------------")
print("\nCORE %d"%cores[i].core_id)
print("\tcore number ",cores[i].core_id)
print("\tcore load capacity",cores[i].core_U)
print("\tcore rem capacity ",cores[i].core_rem_U)
print("\tTask in core ",cores[i].task_ID)
print("\tTask Utilization ",cores[i].task_U)
print("--------------------------------")
print("\n\tNumber of processors used for FIRST FIT",res)
dispaly_metrics(cores)
def BEST_FIT():
# 'n' tasks and 'm' processors
res = 0
core_remain = [0]*n # array to store remaining space in cores
cores = [] #instance list of core class
core_rem = sched_factor #Store remaining U factor of core
count = sched_factor
for i in range(n):
min = count +1
for j in range(res):
if (core_remain[j] >= tasks[i].U
and core_remain[j]-tasks[i].U < min):
min = core_remain[j] - tasks[i].U
c = j
break
if min == count+1:
core_remain[res] = sched_factor - tasks[i].U
res = res + 1
cores.append(core(res,
sched_factor,
truncate(sched_factor - tasks[i].U,2),
i,
tasks[i].U))
else:
core_remain[c] = tasks[i].U - core_remain[c]
cores.append(core(res,
sched_factor,
truncate(tasks[i].U - core_remain[c],2),
i,
tasks[i].U))
for i in range(len(cores)):
print("--------------------------------")
print("\nCORE %d"%cores[i].core_id)
print("\tcore number ",cores[i].core_id)
print("\tcore load capacity",cores[i].core_U)
print("\tcore rem capacity ",cores[i].core_rem_U)
print("\tTask in core ",cores[i].task_ID)
print("\tTask Utilization ",cores[i].task_U)
print("--------------------------------")
print("\n\tNumber of processors used for BEST FIT",res)
dispaly_metrics(cores)
def dispaly_metrics(cores):
core_buff = [] #Store the utilization of cores
merged_cores = [] #Store the non-duplicate cores
id_list = [] #Stores id list of cores
id_merged = [] #Stores id list of merged cores
# The cores are sorted based on the core_id
cores = sorted(cores, key=lambda cores:cores.core_id )
for i in range(len(cores)):
id_list.append(cores[i].core_id)
# Metrics are calculated here
# Inorder to remove the multiple instances of core
# with same core_id they are merged
id_merged = [i for i, x in enumerate(id_list)
if i == len(id_list) - 1 or x != id_list[i + 1]]
# A merged_core array is created to store the core_id
for i in id_merged:
merged_cores.append(cores[i])
# Display of main metrics
for i in range(len(merged_cores)): #truncate the utilization value
core_buff.append(truncate(merged_cores[i].core_U
- merged_cores[i].core_rem_U,2))
core_buff.sort() #sorting the U list for finding max and min values
print("Utilization factor of cores ", core_buff)
print("Maximum Utilization factor of cores", core_buff[-1])
print("Minimum Utilization factor of cores", core_buff[0])
if __name__ == '__main__':
random_data() #Random task set generation
# read_data() #Reads taskset from user
# hp = hyperperiod() #Calculates hyperperiod
schedulability() #Check for feasibility
print("\n\n\n\n")
NEXT_FIT() #Next fit partitioning
print("\n\n\n\n")
FIRST_FIT() #First fit partitioning
print("\n\n\n\n")
BEST_FIT() #Best fit partitioning
print("\n\n\n\n")