Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 1 | """Run a group of subprocesses and then finish.""" |
| 2 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 3 | import hashlib |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 4 | import multiprocessing |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 5 | import os |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 6 | import random |
| 7 | import subprocess |
| 8 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 9 | import tempfile |
| 10 | import time |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 11 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 12 | |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 13 | _DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count() |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def shuffle_iteratable(it): |
| 17 | """Return an iterable that randomly walks it""" |
| 18 | # take a random sampling from the passed in iterable |
| 19 | # we take an element with probablity 1/p and rapidly increase |
| 20 | # p as we take elements - this gives us a somewhat random set of values before |
| 21 | # we've seen all the values, but starts producing values without having to |
| 22 | # compute ALL of them at once, allowing tests to start a little earlier |
| 23 | nextit = [] |
| 24 | p = 1 |
| 25 | for val in it: |
| 26 | if random.randint(0, p) == 0: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 27 | p = min(p*2, 100) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 28 | yield val |
| 29 | else: |
| 30 | nextit.append(val) |
| 31 | # after taking a random sampling, we shuffle the rest of the elements and |
| 32 | # yield them |
| 33 | random.shuffle(nextit) |
| 34 | for val in nextit: |
| 35 | yield val |
| 36 | |
| 37 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 38 | _SUCCESS = object() |
| 39 | _FAILURE = object() |
| 40 | _RUNNING = object() |
| 41 | _KILLED = object() |
| 42 | |
| 43 | |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 44 | _COLORS = { |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 45 | 'red': [ 31, 0 ], |
| 46 | 'green': [ 32, 0 ], |
| 47 | 'yellow': [ 33, 0 ], |
| 48 | 'lightgray': [ 37, 0], |
| 49 | 'gray': [ 30, 1 ], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
| 53 | _BEGINNING_OF_LINE = '\x1b[0G' |
| 54 | _CLEAR_LINE = '\x1b[2K' |
| 55 | |
| 56 | |
| 57 | _TAG_COLOR = { |
| 58 | 'FAILED': 'red', |
| 59 | 'PASSED': 'green', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 60 | 'START': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 61 | 'WAITING': 'yellow', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 62 | 'SUCCESS': 'green', |
| 63 | 'IDLE': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 67 | def message(tag, message, explanatory_text=None, do_newline=False): |
| 68 | sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % ( |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 69 | _BEGINNING_OF_LINE, |
| 70 | _CLEAR_LINE, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 71 | '\n%s' % explanatory_text if explanatory_text is not None else '', |
| 72 | _COLORS[_TAG_COLOR[tag]][1], |
| 73 | _COLORS[_TAG_COLOR[tag]][0], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 74 | tag, |
| 75 | message, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 76 | '\n' if do_newline or explanatory_text is not None else '')) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 77 | sys.stdout.flush() |
| 78 | |
| 79 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 80 | def which(filename): |
| 81 | if '/' in filename: |
| 82 | return filename |
| 83 | for path in os.environ['PATH'].split(os.pathsep): |
| 84 | if os.path.exists(os.path.join(path, filename)): |
| 85 | return os.path.join(path, filename) |
| 86 | raise Exception('%s not found' % filename) |
| 87 | |
| 88 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 89 | class Job(object): |
| 90 | """Manages one job.""" |
| 91 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 92 | def __init__(self, cmdline, bin_hash, newline_on_success): |
| 93 | self._cmdline = cmdline |
| 94 | self._bin_hash = bin_hash |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 95 | self._tempfile = tempfile.TemporaryFile() |
| 96 | self._process = subprocess.Popen(args=cmdline, |
| 97 | stderr=subprocess.STDOUT, |
| 98 | stdout=self._tempfile) |
| 99 | self._state = _RUNNING |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 100 | self._newline_on_success = newline_on_success |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 101 | message('START', ' '.join(self._cmdline)) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 102 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 103 | def state(self, update_cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 104 | """Poll current state of the job. Prints messages at completion.""" |
| 105 | if self._state == _RUNNING and self._process.poll() is not None: |
| 106 | if self._process.returncode != 0: |
| 107 | self._state = _FAILURE |
| 108 | self._tempfile.seek(0) |
| 109 | stdout = self._tempfile.read() |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 110 | message('FAILED', '%s [ret=%d]' % ( |
| 111 | ' '.join(self._cmdline), self._process.returncode), stdout) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 112 | else: |
| 113 | self._state = _SUCCESS |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 114 | message('PASSED', '%s' % ' '.join(self._cmdline), |
| 115 | do_newline=self._newline_on_success) |
| 116 | update_cache.finished(self._cmdline, self._bin_hash) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 117 | return self._state |
| 118 | |
| 119 | def kill(self): |
| 120 | if self._state == _RUNNING: |
| 121 | self._state = _KILLED |
| 122 | self._process.terminate() |
| 123 | |
| 124 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 125 | class Jobset(object): |
| 126 | """Manages one run of jobs.""" |
| 127 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 128 | def __init__(self, check_cancelled, maxjobs, newline_on_success, cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 129 | self._running = set() |
| 130 | self._check_cancelled = check_cancelled |
| 131 | self._cancelled = False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 132 | self._failures = 0 |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 133 | self._completed = 0 |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 134 | self._maxjobs = maxjobs |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 135 | self._newline_on_success = newline_on_success |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 136 | self._cache = cache |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 137 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 138 | def start(self, cmdline): |
| 139 | """Start a job. Return True on success, False on failure.""" |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 140 | while len(self._running) >= self._maxjobs: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 141 | if self.cancelled(): return False |
| 142 | self.reap() |
| 143 | if self.cancelled(): return False |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 144 | with open(which(cmdline[0])) as f: |
| 145 | bin_hash = hashlib.sha1(f.read()).hexdigest() |
| 146 | if self._cache.should_run(cmdline, bin_hash): |
| 147 | self._running.add(Job(cmdline, bin_hash, self._newline_on_success)) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 148 | return True |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 149 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 150 | def reap(self): |
| 151 | """Collect the dead jobs.""" |
| 152 | while self._running: |
| 153 | dead = set() |
| 154 | for job in self._running: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 155 | st = job.state(self._cache) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 156 | if st == _RUNNING: continue |
| 157 | if st == _FAILURE: self._failures += 1 |
| 158 | dead.add(job) |
| 159 | for job in dead: |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 160 | self._completed += 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 161 | self._running.remove(job) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 162 | if dead: return |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 163 | message('WAITING', '%d jobs running, %d complete' % ( |
| 164 | len(self._running), self._completed)) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 165 | time.sleep(0.1) |
| 166 | |
| 167 | def cancelled(self): |
| 168 | """Poll for cancellation.""" |
| 169 | if self._cancelled: return True |
| 170 | if not self._check_cancelled(): return False |
| 171 | for job in self._running: |
| 172 | job.kill() |
| 173 | self._cancelled = True |
| 174 | return True |
| 175 | |
| 176 | def finish(self): |
| 177 | while self._running: |
| 178 | if self.cancelled(): pass # poll cancellation |
| 179 | self.reap() |
| 180 | return not self.cancelled() and self._failures == 0 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 181 | |
| 182 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 183 | def _never_cancelled(): |
| 184 | return False |
| 185 | |
| 186 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 187 | # cache class that caches nothing |
| 188 | class NoCache(object): |
| 189 | def should_run(self, cmdline, bin_hash): |
| 190 | return True |
| 191 | |
| 192 | def finished(self, cmdline, bin_hash): |
| 193 | pass |
| 194 | |
| 195 | |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 196 | def run(cmdlines, |
| 197 | check_cancelled=_never_cancelled, |
| 198 | maxjobs=None, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 199 | newline_on_success=False, |
| 200 | cache=None): |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 201 | js = Jobset(check_cancelled, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 202 | maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame^] | 203 | newline_on_success, |
| 204 | cache if cache is not None else NoCache()) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 205 | for cmdline in shuffle_iteratable(cmdlines): |
| 206 | if not js.start(cmdline): |
| 207 | break |
| 208 | return js.finish() |