blob: d3a46b63e14ee705c82dbc8d284bb49a97cf582c [file] [log] [blame]
Nicolas Nobleddef2462015-01-06 18:08:25 -08001"""Run a group of subprocesses and then finish."""
2
3import multiprocessing
4import random
5import subprocess
6import sys
ctiller3040cb72015-01-07 12:13:17 -08007import tempfile
8import time
Nicolas Nobleddef2462015-01-06 18:08:25 -08009
ctiller3040cb72015-01-07 12:13:17 -080010
11_MAX_JOBS = 16 * multiprocessing.cpu_count()
Nicolas Nobleddef2462015-01-06 18:08:25 -080012
13
14def shuffle_iteratable(it):
15 """Return an iterable that randomly walks it"""
16 # take a random sampling from the passed in iterable
17 # we take an element with probablity 1/p and rapidly increase
18 # p as we take elements - this gives us a somewhat random set of values before
19 # we've seen all the values, but starts producing values without having to
20 # compute ALL of them at once, allowing tests to start a little earlier
21 nextit = []
22 p = 1
23 for val in it:
24 if random.randint(0, p) == 0:
ctiller3040cb72015-01-07 12:13:17 -080025 p = min(p*2, 100)
Nicolas Nobleddef2462015-01-06 18:08:25 -080026 yield val
27 else:
28 nextit.append(val)
29 # after taking a random sampling, we shuffle the rest of the elements and
30 # yield them
31 random.shuffle(nextit)
32 for val in nextit:
33 yield val
34
35
ctiller3040cb72015-01-07 12:13:17 -080036_SUCCESS = object()
37_FAILURE = object()
38_RUNNING = object()
39_KILLED = object()
40
41
42class Job(object):
43 """Manages one job."""
44
45 def __init__(self, cmdline):
46 self._cmdline = ' '.join(cmdline)
47 self._tempfile = tempfile.TemporaryFile()
48 self._process = subprocess.Popen(args=cmdline,
49 stderr=subprocess.STDOUT,
50 stdout=self._tempfile)
51 self._state = _RUNNING
52 sys.stdout.write('\x1b[0G\x1b[2K\x1b[33mSTART\x1b[0m: %s' %
53 self._cmdline)
54 sys.stdout.flush()
55
56 def state(self):
57 """Poll current state of the job. Prints messages at completion."""
58 if self._state == _RUNNING and self._process.poll() is not None:
59 if self._process.returncode != 0:
60 self._state = _FAILURE
61 self._tempfile.seek(0)
62 stdout = self._tempfile.read()
63 sys.stdout.write('\x1b[0G\x1b[2K\x1b[31mFAILED\x1b[0m: %s'
64 ' [ret=%d]\n'
65 '%s\n' % (
66 self._cmdline, self._process.returncode, stdout))
67 sys.stdout.flush()
68 else:
69 self._state = _SUCCESS
70 sys.stdout.write('\x1b[0G\x1b[2K\x1b[32mPASSED\x1b[0m: %s' %
71 self._cmdline)
72 sys.stdout.flush()
73 return self._state
74
75 def kill(self):
76 if self._state == _RUNNING:
77 self._state = _KILLED
78 self._process.terminate()
79
80
Nicolas Nobleddef2462015-01-06 18:08:25 -080081class Jobset(object):
82 """Manages one run of jobs."""
83
ctiller3040cb72015-01-07 12:13:17 -080084 def __init__(self, check_cancelled):
85 self._running = set()
86 self._check_cancelled = check_cancelled
87 self._cancelled = False
Nicolas Nobleddef2462015-01-06 18:08:25 -080088 self._failures = 0
89
ctiller3040cb72015-01-07 12:13:17 -080090 def start(self, cmdline):
91 """Start a job. Return True on success, False on failure."""
92 while len(self._running) >= _MAX_JOBS:
93 if self.cancelled(): return False
94 self.reap()
95 if self.cancelled(): return False
96 self._running.add(Job(cmdline))
97 return True
Nicolas Nobleddef2462015-01-06 18:08:25 -080098
ctiller3040cb72015-01-07 12:13:17 -080099 def reap(self):
100 """Collect the dead jobs."""
101 while self._running:
102 dead = set()
103 for job in self._running:
104 st = job.state()
105 if st == _RUNNING: continue
106 if st == _FAILURE: self._failures += 1
107 dead.add(job)
108 for job in dead:
109 self._running.remove(job)
110 if not dead: return
111 time.sleep(0.1)
112
113 def cancelled(self):
114 """Poll for cancellation."""
115 if self._cancelled: return True
116 if not self._check_cancelled(): return False
117 for job in self._running:
118 job.kill()
119 self._cancelled = True
120 return True
121
122 def finish(self):
123 while self._running:
124 if self.cancelled(): pass # poll cancellation
125 self.reap()
126 return not self.cancelled() and self._failures == 0
Nicolas Nobleddef2462015-01-06 18:08:25 -0800127
128
ctiller3040cb72015-01-07 12:13:17 -0800129def _never_cancelled():
130 return False
131
132
133def run(cmdlines, check_cancelled=_never_cancelled):
134 js = Jobset(check_cancelled)
135 for cmdline in shuffle_iteratable(cmdlines):
136 if not js.start(cmdline):
137 break
138 return js.finish()
Nicolas Nobleddef2462015-01-06 18:08:25 -0800139