Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 30 | """Run a group of subprocesses and then finish.""" |
| 31 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 32 | import hashlib |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 33 | import multiprocessing |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 34 | import os |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 35 | import random |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame^] | 36 | import signal |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 37 | import subprocess |
| 38 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 39 | import tempfile |
| 40 | import time |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 41 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 42 | |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 43 | _DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count() |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 44 | |
| 45 | |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame^] | 46 | # setup a signal handler so that signal.pause registers 'something' |
| 47 | # when a child finishes |
| 48 | # not using futures and threading to avoid a dependency on subprocess32 |
| 49 | signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None) |
| 50 | |
| 51 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 52 | def shuffle_iteratable(it): |
| 53 | """Return an iterable that randomly walks it""" |
| 54 | # take a random sampling from the passed in iterable |
| 55 | # we take an element with probablity 1/p and rapidly increase |
| 56 | # p as we take elements - this gives us a somewhat random set of values before |
| 57 | # we've seen all the values, but starts producing values without having to |
| 58 | # compute ALL of them at once, allowing tests to start a little earlier |
| 59 | nextit = [] |
| 60 | p = 1 |
| 61 | for val in it: |
| 62 | if random.randint(0, p) == 0: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 63 | p = min(p*2, 100) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 64 | yield val |
| 65 | else: |
| 66 | nextit.append(val) |
| 67 | # after taking a random sampling, we shuffle the rest of the elements and |
| 68 | # yield them |
| 69 | random.shuffle(nextit) |
| 70 | for val in nextit: |
| 71 | yield val |
| 72 | |
| 73 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 74 | _SUCCESS = object() |
| 75 | _FAILURE = object() |
| 76 | _RUNNING = object() |
| 77 | _KILLED = object() |
| 78 | |
| 79 | |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 80 | _COLORS = { |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 81 | 'red': [ 31, 0 ], |
| 82 | 'green': [ 32, 0 ], |
| 83 | 'yellow': [ 33, 0 ], |
| 84 | 'lightgray': [ 37, 0], |
| 85 | 'gray': [ 30, 1 ], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | _BEGINNING_OF_LINE = '\x1b[0G' |
| 90 | _CLEAR_LINE = '\x1b[2K' |
| 91 | |
| 92 | |
| 93 | _TAG_COLOR = { |
| 94 | 'FAILED': 'red', |
| 95 | 'PASSED': 'green', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 96 | 'START': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 97 | 'WAITING': 'yellow', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 98 | 'SUCCESS': 'green', |
| 99 | 'IDLE': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 103 | def message(tag, message, explanatory_text=None, do_newline=False): |
| 104 | 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] | 105 | _BEGINNING_OF_LINE, |
| 106 | _CLEAR_LINE, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 107 | '\n%s' % explanatory_text if explanatory_text is not None else '', |
| 108 | _COLORS[_TAG_COLOR[tag]][1], |
| 109 | _COLORS[_TAG_COLOR[tag]][0], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 110 | tag, |
| 111 | message, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 112 | '\n' if do_newline or explanatory_text is not None else '')) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 113 | sys.stdout.flush() |
| 114 | |
| 115 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 116 | def which(filename): |
| 117 | if '/' in filename: |
| 118 | return filename |
| 119 | for path in os.environ['PATH'].split(os.pathsep): |
| 120 | if os.path.exists(os.path.join(path, filename)): |
| 121 | return os.path.join(path, filename) |
| 122 | raise Exception('%s not found' % filename) |
| 123 | |
| 124 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 125 | class JobSpec(object): |
| 126 | """Specifies what to run for a job.""" |
| 127 | |
| 128 | def __init__(self, cmdline, shortname=None, environ={}, hash_targets=[]): |
| 129 | """ |
| 130 | Arguments: |
| 131 | cmdline: a list of arguments to pass as the command line |
| 132 | environ: a dictionary of environment variables to set in the child process |
| 133 | hash_targets: which files to include in the hash representing the jobs version |
| 134 | (or empty, indicating the job should not be hashed) |
| 135 | """ |
| 136 | self.cmdline = cmdline |
| 137 | self.environ = environ |
| 138 | self.shortname = cmdline[0] if shortname is None else shortname |
| 139 | self.hash_targets = hash_targets or [] |
| 140 | |
| 141 | def identity(self): |
| 142 | return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets) |
| 143 | |
| 144 | def __hash__(self): |
| 145 | return hash(self.identity()) |
| 146 | |
| 147 | def __cmp__(self, other): |
| 148 | return self.identity() == other.identity() |
| 149 | |
| 150 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 151 | class Job(object): |
| 152 | """Manages one job.""" |
| 153 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 154 | def __init__(self, spec, bin_hash, newline_on_success): |
| 155 | self._spec = spec |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 156 | self._bin_hash = bin_hash |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 157 | self._tempfile = tempfile.TemporaryFile() |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 158 | env = os.environ.copy() |
| 159 | for k, v in spec.environ.iteritems(): |
| 160 | env[k] = v |
| 161 | self._process = subprocess.Popen(args=spec.cmdline, |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 162 | stderr=subprocess.STDOUT, |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 163 | stdout=self._tempfile, |
| 164 | env=env) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 165 | self._state = _RUNNING |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 166 | self._newline_on_success = newline_on_success |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 167 | message('START', spec.shortname) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 168 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 169 | def state(self, update_cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 170 | """Poll current state of the job. Prints messages at completion.""" |
| 171 | if self._state == _RUNNING and self._process.poll() is not None: |
| 172 | if self._process.returncode != 0: |
| 173 | self._state = _FAILURE |
| 174 | self._tempfile.seek(0) |
| 175 | stdout = self._tempfile.read() |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 176 | message('FAILED', '%s [ret=%d]' % ( |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 177 | self._spec.shortname, self._process.returncode), stdout) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 178 | else: |
| 179 | self._state = _SUCCESS |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 180 | message('PASSED', self._spec.shortname, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 181 | do_newline=self._newline_on_success) |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 182 | if self._bin_hash: |
| 183 | update_cache.finished(self._spec.identity(), self._bin_hash) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 184 | return self._state |
| 185 | |
| 186 | def kill(self): |
| 187 | if self._state == _RUNNING: |
| 188 | self._state = _KILLED |
| 189 | self._process.terminate() |
| 190 | |
| 191 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 192 | class Jobset(object): |
| 193 | """Manages one run of jobs.""" |
| 194 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 195 | def __init__(self, check_cancelled, maxjobs, newline_on_success, cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 196 | self._running = set() |
| 197 | self._check_cancelled = check_cancelled |
| 198 | self._cancelled = False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 199 | self._failures = 0 |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 200 | self._completed = 0 |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 201 | self._maxjobs = maxjobs |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 202 | self._newline_on_success = newline_on_success |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 203 | self._cache = cache |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 204 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 205 | def start(self, spec): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 206 | """Start a job. Return True on success, False on failure.""" |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 207 | while len(self._running) >= self._maxjobs: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 208 | if self.cancelled(): return False |
| 209 | self.reap() |
| 210 | if self.cancelled(): return False |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 211 | if spec.hash_targets: |
| 212 | bin_hash = hashlib.sha1() |
| 213 | for fn in spec.hash_targets: |
| 214 | with open(which(fn)) as f: |
| 215 | bin_hash.update(f.read()) |
| 216 | bin_hash = bin_hash.hexdigest() |
| 217 | should_run = self._cache.should_run(spec.identity(), bin_hash) |
| 218 | else: |
| 219 | bin_hash = None |
| 220 | should_run = True |
| 221 | if should_run: |
| 222 | self._running.add(Job(spec, |
| 223 | bin_hash, |
| 224 | self._newline_on_success)) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 225 | return True |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 226 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 227 | def reap(self): |
| 228 | """Collect the dead jobs.""" |
| 229 | while self._running: |
| 230 | dead = set() |
| 231 | for job in self._running: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 232 | st = job.state(self._cache) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 233 | if st == _RUNNING: continue |
| 234 | if st == _FAILURE: self._failures += 1 |
| 235 | dead.add(job) |
| 236 | for job in dead: |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 237 | self._completed += 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 238 | self._running.remove(job) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 239 | if dead: return |
Craig Tiller | 6f5e2c4 | 2015-01-21 18:05:31 -0800 | [diff] [blame] | 240 | message('WAITING', '%d jobs running, %d complete, %d failed' % ( |
| 241 | len(self._running), self._completed, self._failures)) |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame^] | 242 | signal.pause() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 243 | |
| 244 | def cancelled(self): |
| 245 | """Poll for cancellation.""" |
| 246 | if self._cancelled: return True |
| 247 | if not self._check_cancelled(): return False |
| 248 | for job in self._running: |
| 249 | job.kill() |
| 250 | self._cancelled = True |
| 251 | return True |
| 252 | |
| 253 | def finish(self): |
| 254 | while self._running: |
| 255 | if self.cancelled(): pass # poll cancellation |
| 256 | self.reap() |
| 257 | return not self.cancelled() and self._failures == 0 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 258 | |
| 259 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 260 | def _never_cancelled(): |
| 261 | return False |
| 262 | |
| 263 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 264 | # cache class that caches nothing |
| 265 | class NoCache(object): |
| 266 | def should_run(self, cmdline, bin_hash): |
| 267 | return True |
| 268 | |
| 269 | def finished(self, cmdline, bin_hash): |
| 270 | pass |
| 271 | |
| 272 | |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 273 | def run(cmdlines, |
| 274 | check_cancelled=_never_cancelled, |
| 275 | maxjobs=None, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 276 | newline_on_success=False, |
| 277 | cache=None): |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 278 | js = Jobset(check_cancelled, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 279 | maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 280 | newline_on_success, |
| 281 | cache if cache is not None else NoCache()) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 282 | for cmdline in shuffle_iteratable(cmdlines): |
| 283 | if not js.start(cmdline): |
| 284 | break |
| 285 | return js.finish() |