diff options
author | Mac Mollison <mollison@cs.unc.edu> | 2009-03-03 01:52:57 -0500 |
---|---|---|
committer | Mac Mollison <mollison@cs.unc.edu> | 2009-03-03 01:52:57 -0500 |
commit | 99a1ada5620902ad8607c598260e995aa96fb7aa (patch) | |
tree | c5f82ffe72eff4554a786fe3e8d193f1c86c3a23 | |
parent | 59aeaf7b558b930dfa625456d3e52c80a1f7f0bc (diff) |
Cleaned up EDF code
-rwxr-xr-x | sta.py | 103 |
1 files changed, 51 insertions, 52 deletions
@@ -121,6 +121,8 @@ class Trace: | |||
121 | print("") | 121 | print("") |
122 | 122 | ||
123 | def getStr(record,omitTime=False): | 123 | def getStr(record,omitTime=False): |
124 | """Return the representation of a record in the format | ||
125 | time: (task, job)""" | ||
124 | time = None | 126 | time = None |
125 | if 'when' in record: | 127 | if 'when' in record: |
126 | time = record['when'] | 128 | time = record['when'] |
@@ -144,13 +146,25 @@ class Trace: | |||
144 | ################## | 146 | ################## |
145 | 147 | ||
146 | class EDF: | 148 | class EDF: |
147 | """Object representing an EDF test""" | 149 | """Object representing an EDF test |
148 | 150 | ||
149 | def __init__(self,trace_files, tick_size=1000000, tolerance=1000000): | 151 | The strategy is to create a model of runnable and running tasks, which is |
150 | """Create an EDF test""" | 152 | updated and checked against an invariant every tick_size time units |
153 | |||
154 | The invariant is that every running task much have an earlier deadline | ||
155 | than every runnable task; an error message is not generated until the | ||
156 | invariant is false for more than a specified number of ticks (given by | ||
157 | 'tolerance')""" | ||
158 | |||
159 | def __init__(self,trace_files, tick_size=1000000, tolerance=1): | ||
160 | """Create an EDF test. | ||
161 | |||
162 | tick_size = check the invariant every tick_size units of time | ||
163 | |||
164 | tolerance = number of tick_size time units which can pass before | ||
165 | a false invariant triggers an error message""" | ||
151 | self.trace_files = trace_files | 166 | self.trace_files = trace_files |
152 | self.errors = 0 | 167 | self.errors = 0 |
153 | self.corrects = 0 | ||
154 | self.runnables = [] | 168 | self.runnables = [] |
155 | self.runnings = [] | 169 | self.runnings = [] |
156 | self.time = None | 170 | self.time = None |
@@ -158,6 +172,7 @@ class EDF: | |||
158 | self.tolerance = tolerance | 172 | self.tolerance = tolerance |
159 | 173 | ||
160 | def run_test(self): | 174 | def run_test(self): |
175 | """Run an EDF test""" | ||
161 | 176 | ||
162 | #Make the trace, in order | 177 | #Make the trace, in order |
163 | trace = Trace(self.trace_files) | 178 | trace = Trace(self.trace_files) |
@@ -172,12 +187,11 @@ class EDF: | |||
172 | print("Starting time: " + str(self.time)) | 187 | print("Starting time: " + str(self.time)) |
173 | break | 188 | break |
174 | 189 | ||
175 | #Initialize last_record | 190 | #Initialize variables needed for the algorithm |
176 | last_record = trace.iter.__next__() | 191 | last_record = trace.iter.__next__() |
177 | |||
178 | #Initialize finished flag | ||
179 | finished = False | 192 | finished = False |
180 | 193 | ||
194 | #Increment the time, update the model, and check the invariant | ||
181 | while not finished: | 195 | while not finished: |
182 | 196 | ||
183 | #Increment the time | 197 | #Increment the time |
@@ -193,37 +207,41 @@ class EDF: | |||
193 | except StopIteration: | 207 | except StopIteration: |
194 | finished = True | 208 | finished = True |
195 | break | 209 | break |
196 | 210 | ||
197 | #Check to see if our invariant is met | 211 | #Check the invariant |
198 | #1) Find the max deadline in runnings | 212 | self.check_invariant() |
199 | #2) Check against the deadlines in runnables | ||
200 | |||
201 | #1) Find the max deadline in runnings | ||
202 | max_deadline = None | ||
203 | for record in self.runnings: | ||
204 | if max_deadline==None: | ||
205 | max_deadline = record['deadline'] | ||
206 | continue | ||
207 | elif record['deadline'] > max_deadline: | ||
208 | max_deadline = record['deadline'] | ||
209 | |||
210 | #2) Check against the deadlines in runnables | ||
211 | if max_deadline: | ||
212 | for record in self.runnables: | ||
213 | if record['deadline'] < max_deadline: | ||
214 | record['overtime'] += self.tick_size | ||
215 | if record['overtime'] > self.tolerance: | ||
216 | print("INVALID!") | ||
217 | self.print_runnables() | ||
218 | self.print_runnings() | ||
219 | record['overtime'] = -99999999999999999 | ||
220 | self.errors += 1 | ||
221 | |||
222 | 213 | ||
223 | print('Errors : {0}'.format(self.errors)) | 214 | print('Errors : {0}'.format(self.errors)) |
224 | 215 | ||
225 | 216 | ||
217 | def check_invariant(self): | ||
218 | """Check the invariant: the earliest deadline in runnables should be | ||
219 | later than the latest deadline in runnings""" | ||
220 | |||
221 | #Find the max deadline in runnings | ||
222 | max_deadline = None | ||
223 | for record in self.runnings: | ||
224 | if max_deadline==None: | ||
225 | max_deadline = record['deadline'] | ||
226 | continue | ||
227 | elif record['deadline'] > max_deadline: | ||
228 | max_deadline = record['deadline'] | ||
229 | |||
230 | #Check against the deadlines in runnables | ||
231 | if max_deadline: | ||
232 | for record in self.runnables: | ||
233 | if record['deadline'] < max_deadline: | ||
234 | record['overtime'] += 1 | ||
235 | if record['overtime'] > self.tolerance: | ||
236 | print("INVALID!") | ||
237 | self.print_runnables() | ||
238 | self.print_runnings() | ||
239 | record['overtime'] = -99999999999999999 | ||
240 | self.errors += 1 | ||
241 | |||
226 | def update_model(self, record): | 242 | def update_model(self, record): |
243 | """Update our model of the system: running tasks ('self.runnings') and | ||
244 | runnable tasks ('self.runnable')""" | ||
227 | 245 | ||
228 | #Releases | 246 | #Releases |
229 | if record['type'] == 3: | 247 | if record['type'] == 3: |
@@ -237,24 +255,6 @@ class EDF: | |||
237 | release_record = EDF.pop_job(record, self.runnables) | 255 | release_record = EDF.pop_job(record, self.runnables) |
238 | self.runnings.append(release_record) | 256 | self.runnings.append(release_record) |
239 | print("{0} became running".format(Trace.getStr(record))) | 257 | print("{0} became running".format(Trace.getStr(record))) |
240 | #check_tuple = self.check_deadline(release_record) | ||
241 | #if check_tuple[0] == True: | ||
242 | # print("{0} became running (VALID)" | ||
243 | # .format(Trace.getStr(record))) | ||
244 | # self.corrects += 1 | ||
245 | #else: | ||
246 | # print('='*50) | ||
247 | # print("{0} became running (INVALID)" | ||
248 | # .format(Trace.getStr(record))) | ||
249 | # print('Deadline of {0} greater than deadline of {1}' + | ||
250 | # 'in the following record:' | ||
251 | # .format(release_record['deadline'], | ||
252 | # check_tuple[1]['deadline'])) | ||
253 | # print(check_tuple[1]) | ||
254 | # self.print_runnables() | ||
255 | # self.print_runnings() | ||
256 | # print('='*50) | ||
257 | # self.errors += 1 | ||
258 | 258 | ||
259 | #Switch Aways | 259 | #Switch Aways |
260 | elif record['type'] == 6: | 260 | elif record['type'] == 6: |
@@ -272,7 +272,6 @@ class EDF: | |||
272 | print('{0} completed' | 272 | print('{0} completed' |
273 | .format(Trace.getStr(record))) | 273 | .format(Trace.getStr(record))) |
274 | 274 | ||
275 | |||
276 | def pop_job(record, record_list): | 275 | def pop_job(record, record_list): |
277 | """Pop a job from a list of jobs""" | 276 | """Pop a job from a list of jobs""" |
278 | job = record['job'] | 277 | job = record['job'] |