-
Notifications
You must be signed in to change notification settings - Fork 0
/
round-robin-sim.py
executable file
·383 lines (348 loc) · 15.9 KB
/
round-robin-sim.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
#!/sw/bin/python2.7
import simpy
from collections import deque
import os, sys, math, random
"""
Simple round-robin batch queue simulator. Each process runs for a given
time quanta and is then preempted out for the next process in the queue.
"""
RANDOM_SEED = 42
PT_MEAN = 1000.0 # Avg. processing time in minutes
PT_SIGMA = 100.0 # Sigma of processing time
MTBF = 300.0 # Mean time to failure in minutes
BREAK_MEAN = 1 / MTBF # Param. for expovariate distribution
NUM_PROCESSES = 1 # Number of processes
MAX_PARALLEL_PROCESSES = 1
MAX_CIRC_Q_LEN = 5
CKPT_THRESH = 10
enableBqLogs = True
enableProcLogs = True
def time_per_process():
"""Return a randomly generated compute time."""
return int(random.normalvariate(PT_MEAN, PT_SIGMA))
def time_to_failure():
"""Return time until next failure for a machine."""
return int(random.expovariate(BREAK_MEAN))
#return MTBF
def time_to_checkpoint():
return 10
def time_to_preempt():
return 250
class BatchQueue(object):
def __init__(self, myenv, max_circ_length, nodes):
self.env = myenv
self.maxLength = max_circ_length
self.circQ = deque([], self.maxLength)
self.allJobs = []
self.numPreempts = 0
self.process = None
self.switchingJobs = False
self.numFailures = 0
self.machine = nodes
self.currentProc = None
def BqLog(self, msg):
if enableBqLogs:
print("[%d]: BQ(%d): %s" %(self.env.now, len(self.circQ), msg))
def addToBq(self, p):
self.allJobs.append(p)
p.submitToQueue();
if len(self.circQ) < self.maxLength - 1:
self.circQ.append(p)
def runBq(self):
self.process = self.env.process(self.runBqHelper())
self.env.process(self.inject_failure())
yield self.process
def inject_failure(self):
"""Break the machine every now and then."""
while len(self.circQ):
yield self.env.timeout(time_to_failure())
if len(self.circQ) > 0 and \
not self.currentProc.broken and \
self.currentProc.workLeft > 0:
# Only break the machine if it is currently computing,
# and if current proc is not restarting
# TODO: Allow errors to be thrown while restarting
self.BqLog("Injecting a failure in %s" % (self.currentProc.name))
self.numFailures += 1
self.process.interrupt(cause="failure")
def runBqHelper(self):
preemtionTime = time_to_preempt()
while len(self.circQ) > 0:
with self.machine.request() as req:
yield req
try:
# Run the head of the queue for a while
p = self.circQ.popleft()
if p.workLeft == 0:
self.BqLog("Done with the queue")
continue
# Run, or Restart (if the process has at least one checkpoint)
self.currentProc = p
if not p.startAfresh:
start = self.env.now
preemtionTime = time_to_preempt()
self.BqLog("Starting %s" %(p.name))
p.process = self.env.process(p.runJob(p.startAfresh))
elif p.broken:
start = self.env.now
self.BqLog("%s recovering from failure... nothing to do" %(p.name))
else:
self.BqLog("Resuming %s" % (p.name))
preemtionTime = time_to_preempt()
p.waitForBq.succeed()
p.waitForBq = self.env.event()
yield p.resumeCompleted
self.BqLog("Restarted %s" % (p.name))
start = self.env.now
self.BqLog("Will preempt %s after %d" %(p.name, preemtionTime))
yield p.waitForComputeToEnd | self.env.timeout(preemtionTime)
if p.workLeft == 0:
self.BqLog("Done with %s." % (p.name))
if len(self.circQ) == 0:
self.env.exit()
continue
# Then, preempt it after its quanta is over
self.switchingJobs = True
if p.inTheMiddle:
self.BqLog("%s currently ckpting." % (p.name))
yield p.isCkpting
self.BqLog("Non-regular Preempt %s" % (p.name))
p.process.interrupt(cause="preempt")
else:
self.BqLog("Regular Preempt %s" % (p.name))
p.process.interrupt(cause="preempt")
if self.env.now - p.lastCkptInstant > CKPT_THRESH:
yield self.env.process(p.do_ckpt())
else:
self.BqLog("Skip ckpting for %s. Last Ckpt was at %d" % (p.name, p.lastCkptInstant))
self.BqLog("%s completed ckpting" % (p.name))
# Finally, add it back to the tail of the queue
self.circQ.append(p)
self.BqLog("Added %s back to queue" % (p.name))
self.switchingJobs = False
except simpy.Interrupt as e:
if e.cause == "failure":
preemtionTime -= self.env.now - start
p.broken = True
if preemtionTime < CKPT_THRESH:
self.circQ.append(p)
p.process.interrupt(cause="failureNoRestart")
self.BqLog("Not enough time %s to recover" % (p.name))
else:
# Add the job back for execution
self.BqLog("Adding %s back for execution" % (p.name))
self.circQ.appendleft(p)
p.process.interrupt(cause="failure")
else:
self.BqLog("Unknown failure type. Exiting...")
exit(-1)
class Process(object):
"""A process computes, checkpoints, and occasionaly incurs a failure.
If it fails, it restarts from the latest checkpoint.
"""
def __init__(self, myenv, name, ckptTime, nodes):
self.env = myenv
self.name = name
self.broken = False
self.totalComputeTime = time_per_process()
self.lastCheckpointTime = 0
self.numCkpts = 0
self.numFailures = 0
self.workLeft = self.totalComputeTime
self.endTime = 0
self.actualRunTime = 0
self.lostWork = 0
self.ckptTime = ckptTime
self.ckptFailures = 0
self.bq = nodes
self.startTime = 0
self.submissionTime = 0
self.process = None
self.startAfresh = False # True once you have taken the first ckpt
self.isCkpting = myenv.event() # True during checkpointing
self.waitForBq = myenv.event()
self.waitForComputeToEnd = myenv.event()
self.resumeCompleted = myenv.event()
self.numOfPreempts = 0
self.lastComputeStartTime = 0
self.lastCkptInstant = 0
self.inTheMiddle = False
def submitToQueue(self):
self.submissionTime = self.env.now
def ProcLog(self, msg):
if enableProcLogs:
print("[%d]: %s: %s" % (self.env.now, self.name, msg))
def runJob(self, shouldRestart=False):
"""Simulate compute for the given amount of total work.
"""
self.inTheMiddle = False
while self.workLeft:
try:
delta = self.ckptTime
oci = int(math.sqrt(2*MTBF*delta))
computeTime = min(oci, self.workLeft)
if computeTime <= 0:
self.endTime = self.env.now
self.actualRunTime = self.endTime - self.startTime
self.env.exit()
if shouldRestart:
yield self.env.timeout(delta) # simulate restart when requested by the bq
self.resumeCompleted.succeed()
self.resumeCompleted = self.env.event()
# Start computing
start = self.env.now
self.lastComputeStartTime = start
self.ProcLog("Computing for %d, workleft %d" % (computeTime, self.workLeft))
yield self.env.timeout(computeTime)
if self.workLeft < oci:
self.workLeft = 0
self.endTime = self.env.now
self.actualRunTime = self.endTime - self.startTime
self.waitForComputeToEnd.succeed()
self.waitForComputeToEnd = self.env.event()
self.env.exit()
self.ProcLog("Ckpting, workleft %d" % (self.workLeft))
ckptStartTime = self.env.now
self.inTheMiddle = True
yield self.env.timeout(delta)
self.lastCkptInstant = self.env.now
# Done with ckpting, now
# first, save the progress made since the last interruption, and
timeSinceLastInterruption = ckptStartTime - start
self.workLeft -= timeSinceLastInterruption
# second, update the latest ckpt time
self.lastCheckpointTime += timeSinceLastInterruption
# ... and increment the number of ckpts
self.startAfresh = True
self.numCkpts += 1
self.inTheMiddle = False
self.ProcLog("Done ckpting, work left %d, ckpts %d, lastCkpt %d" % (self.workLeft, self.numCkpts, self.lastCheckpointTime))
except simpy.Interrupt as e:
if e.cause == "failure":
# fallback to the last checkpoint
if self.inTheMiddle:
self.inTheMiddle = False
self.ckptFailures += 1
#self.ProcLog("Checkpointing failure, lastCkpt %d, workLeft %d" % (self.lastCheckpointTime, self.workLeft))
self.broken = True
#self.ProcLog("Incurred a failure, work left %d" % (self.workLeft))
self.numFailures += 1
restarting = self.env.process(self.do_restart(self.env.now - start))
yield restarting
#self.ProcLog("Resumed after failure, work left %d, lost work %d" % (self.workLeft, self.lostWork))
self.broken = False
elif e.cause == "failureNoRestart":
if self.inTheMiddle:
self.inTheMiddle = False
self.ckptFailures += 1
#self.ProcLog("Checkpointing failure, lastCkpt %d, workLeft %d" % (self.lastCheckpointTime, self.workLeft))
self.broken = False
self.numFailures += 1
restarting = self.env.process(self.do_restart(self.env.now - start, True))
yield restarting
yield self.waitForBq
self.ProcLog("Resumed after failureNoRestart")
shouldRestart = True
elif e.cause == "preempt":
self.ProcLog("Preempted, workLeft %d" %(self.workLeft))
self.numOfPreempts += 1
yield self.waitForBq
self.ProcLog("Resumed after preemption")
shouldRestart = True
else:
print("Unexpected interrupt in the middle of computing")
exit(-1)
self.workLeft = 0
self.endTime = self.env.now
self.actualRunTime = self.endTime - self.startTime
def do_ckpt(self):
self.inTheMiddle = False
try:
delta = self.ckptTime
self.ProcLog("Forced ckpting, workleft %d" % (self.workLeft))
self.inTheMiddle = True
ckptStartTime = self.env.now
yield self.env.timeout(delta)
timeSinceLastInterruption = ckptStartTime - self.lastComputeStartTime;
# Done with ckpting, now
# first, save the progress made since the last interruption, and
self.workLeft -= timeSinceLastInterruption
# second, update the latest ckpt time
self.lastCheckpointTime += timeSinceLastInterruption
# ... and increment the number of ckpts
self.startAfresh = True
self.numCkpts += 1
self.isCkpting.succeed()
self.isCkpting = self.env.event()
self.inTheMiddle = False
self.ProcLog("Done forced ckpting, work left %d, ckpts %d, lastCkpt %d" % (self.workLeft, self.numCkpts, self.lastCheckpointTime))
except simpy.Interrupt as e:
if e.cause == "failure":
self.ckptFailures += 1
self.isCkpting.fail()
self.isCkpting = self.env.event()
#self.ProcLog ("Checkpointing failure, lastCkpt %d, workLeft %d" % (self.lastCheckpointTime, self.workLeft))
def do_restart(self, timeSinceLastInterruption, noRestart=False):
"""Restart the process after a failure."""
delta = self.ckptTime
try:
self.ProcLog("Attempting to restart from ckpt #%d, taken at %d" % (self.numCkpts, self.lastCheckpointTime))
self.lostWork += timeSinceLastInterruption
if not noRestart:
assert self.broken == True
yield self.env.timeout(delta)
# Done with restart without errors
self.ProcLog("Restart successful... going back to compute")
except simpy.Interrupt as e:
if (e.cause == "failure"):
# TODO: Handle failures during a restart
print("Failure in the middle of a restart... will attempt restart again")
exit(-1)
#self.do_restart()
def __str__(self):
# FIXME: Fix actual runtime
return "%s, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d" %\
(self.name, self.numCkpts, self.numFailures, self.ckptFailures, self.numOfPreempts,
self.totalComputeTime, self.ckptTime, self.lostWork, self.submissionTime,
self.startTime, self.endTime, self.actualRunTime)
def simulateArrivalOfJobs(env, processes, batchQ):
"""Simulate random arrival of jobs"""
for p in processes:
batchQ.addToBq(p)
# # Submit four initial jobs
# for p in processes[:4]:
# env.process(p.submitToQueue())
#
# # Create more processes while the simulation is running
# for p in processes[4:]:
# yield env.timeout(random.randint(5, 7))
# env.process(p.submitToQueue())
def main(argc, argv):
"""Set up and start the simulation."""
print('Process checkpoint-restart simulator')
random.seed(RANDOM_SEED) # constant seed for reproducibility
# Create an environment and start the setup process
env = simpy.Environment()
# Create a batch queue
mymachine = simpy.Resource(env, MAX_PARALLEL_PROCESSES)
batchQ = BatchQueue(env, MAX_CIRC_Q_LEN, mymachine)
testProcesses = [Process(env, 'Process %d' % i, time_to_checkpoint(), mymachine)
for i in range(NUM_PROCESSES)]
simulateArrivalOfJobs(env, testProcesses, batchQ)
env.process(batchQ.runBq())
# Execute
env.run()
# Analyis/results
print("******************************************************")
print("******************FINAL DATA**************************")
print("******************************************************")
print("Process #, # Ckpts, # Total Failures, # Failure During Ckpt, # Preempts,"\
" Compute Time, Ckpt Time, Lost Work, Submission Time, Start Time,"\
" End Time, Actual Run Time")
for p in testProcesses:
if int((p.numCkpts + p.numFailures) * p.ckptTime +\
p.lostWork + p.totalComputeTime) != int(p.actualRunTime):
print("Warning")
print(p)
if __name__ == "__main__":
main(len(sys.argv), sys.argv)