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): |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 104 | try: |
| 105 | sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % ( |
| 106 | _BEGINNING_OF_LINE, |
| 107 | _CLEAR_LINE, |
| 108 | '\n%s' % explanatory_text if explanatory_text is not None else '', |
| 109 | _COLORS[_TAG_COLOR[tag]][1], |
| 110 | _COLORS[_TAG_COLOR[tag]][0], |
| 111 | tag, |
| 112 | message, |
| 113 | '\n' if do_newline or explanatory_text is not None else '')) |
| 114 | sys.stdout.flush() |
| 115 | except: |
| 116 | pass |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 117 | |
| 118 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 119 | def which(filename): |
| 120 | if '/' in filename: |
| 121 | return filename |
| 122 | for path in os.environ['PATH'].split(os.pathsep): |
| 123 | if os.path.exists(os.path.join(path, filename)): |
| 124 | return os.path.join(path, filename) |
| 125 | raise Exception('%s not found' % filename) |
| 126 | |
| 127 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 128 | class JobSpec(object): |
| 129 | """Specifies what to run for a job.""" |
| 130 | |
| 131 | def __init__(self, cmdline, shortname=None, environ={}, hash_targets=[]): |
| 132 | """ |
| 133 | Arguments: |
| 134 | cmdline: a list of arguments to pass as the command line |
| 135 | environ: a dictionary of environment variables to set in the child process |
| 136 | hash_targets: which files to include in the hash representing the jobs version |
| 137 | (or empty, indicating the job should not be hashed) |
| 138 | """ |
| 139 | self.cmdline = cmdline |
| 140 | self.environ = environ |
| 141 | self.shortname = cmdline[0] if shortname is None else shortname |
| 142 | self.hash_targets = hash_targets or [] |
| 143 | |
| 144 | def identity(self): |
| 145 | return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets) |
| 146 | |
| 147 | def __hash__(self): |
| 148 | return hash(self.identity()) |
| 149 | |
| 150 | def __cmp__(self, other): |
| 151 | return self.identity() == other.identity() |
| 152 | |
| 153 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 154 | class Job(object): |
| 155 | """Manages one job.""" |
| 156 | |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 157 | def __init__(self, spec, bin_hash, newline_on_success, travis): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 158 | self._spec = spec |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 159 | self._bin_hash = bin_hash |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 160 | self._tempfile = tempfile.TemporaryFile() |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 161 | env = os.environ.copy() |
| 162 | for k, v in spec.environ.iteritems(): |
| 163 | env[k] = v |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame^] | 164 | self._start = time.time() |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 165 | self._process = subprocess.Popen(args=spec.cmdline, |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 166 | stderr=subprocess.STDOUT, |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 167 | stdout=self._tempfile, |
| 168 | env=env) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 169 | self._state = _RUNNING |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 170 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 171 | self._travis = travis |
| 172 | if not travis: |
| 173 | message('START', spec.shortname) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 174 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 175 | def state(self, update_cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 176 | """Poll current state of the job. Prints messages at completion.""" |
| 177 | if self._state == _RUNNING and self._process.poll() is not None: |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame^] | 178 | elapsed = time.time() - self._start |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 179 | if self._process.returncode != 0: |
| 180 | self._state = _FAILURE |
| 181 | self._tempfile.seek(0) |
| 182 | stdout = self._tempfile.read() |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 183 | message('FAILED', '%s [ret=%d]' % ( |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 184 | self._spec.shortname, self._process.returncode), stdout) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 185 | else: |
| 186 | self._state = _SUCCESS |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame^] | 187 | message('PASSED', '%s [time=%.1fsec]' % (self._spec.shortname, elapsed), |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 188 | do_newline=self._newline_on_success or self._travis) |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 189 | if self._bin_hash: |
| 190 | update_cache.finished(self._spec.identity(), self._bin_hash) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 191 | return self._state |
| 192 | |
| 193 | def kill(self): |
| 194 | if self._state == _RUNNING: |
| 195 | self._state = _KILLED |
| 196 | self._process.terminate() |
| 197 | |
| 198 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 199 | class Jobset(object): |
| 200 | """Manages one run of jobs.""" |
| 201 | |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 202 | def __init__(self, check_cancelled, maxjobs, newline_on_success, travis, cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 203 | self._running = set() |
| 204 | self._check_cancelled = check_cancelled |
| 205 | self._cancelled = False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 206 | self._failures = 0 |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 207 | self._completed = 0 |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 208 | self._maxjobs = maxjobs |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 209 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 210 | self._travis = travis |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 211 | self._cache = cache |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 212 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 213 | def start(self, spec): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 214 | """Start a job. Return True on success, False on failure.""" |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 215 | while len(self._running) >= self._maxjobs: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 216 | if self.cancelled(): return False |
| 217 | self.reap() |
| 218 | if self.cancelled(): return False |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 219 | if spec.hash_targets: |
| 220 | bin_hash = hashlib.sha1() |
| 221 | for fn in spec.hash_targets: |
| 222 | with open(which(fn)) as f: |
| 223 | bin_hash.update(f.read()) |
| 224 | bin_hash = bin_hash.hexdigest() |
| 225 | should_run = self._cache.should_run(spec.identity(), bin_hash) |
| 226 | else: |
| 227 | bin_hash = None |
| 228 | should_run = True |
| 229 | if should_run: |
| 230 | self._running.add(Job(spec, |
| 231 | bin_hash, |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 232 | self._newline_on_success, |
| 233 | self._travis)) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 234 | return True |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 235 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 236 | def reap(self): |
| 237 | """Collect the dead jobs.""" |
| 238 | while self._running: |
| 239 | dead = set() |
| 240 | for job in self._running: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 241 | st = job.state(self._cache) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 242 | if st == _RUNNING: continue |
| 243 | if st == _FAILURE: self._failures += 1 |
| 244 | dead.add(job) |
| 245 | for job in dead: |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 246 | self._completed += 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 247 | self._running.remove(job) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 248 | if dead: return |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 249 | if (not self._travis): |
| 250 | message('WAITING', '%d jobs running, %d complete, %d failed' % ( |
| 251 | len(self._running), self._completed, self._failures)) |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 252 | signal.pause() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 253 | |
| 254 | def cancelled(self): |
| 255 | """Poll for cancellation.""" |
| 256 | if self._cancelled: return True |
| 257 | if not self._check_cancelled(): return False |
| 258 | for job in self._running: |
| 259 | job.kill() |
| 260 | self._cancelled = True |
| 261 | return True |
| 262 | |
| 263 | def finish(self): |
| 264 | while self._running: |
| 265 | if self.cancelled(): pass # poll cancellation |
| 266 | self.reap() |
| 267 | return not self.cancelled() and self._failures == 0 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 268 | |
| 269 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 270 | def _never_cancelled(): |
| 271 | return False |
| 272 | |
| 273 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 274 | # cache class that caches nothing |
| 275 | class NoCache(object): |
| 276 | def should_run(self, cmdline, bin_hash): |
| 277 | return True |
| 278 | |
| 279 | def finished(self, cmdline, bin_hash): |
| 280 | pass |
| 281 | |
| 282 | |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 283 | def run(cmdlines, |
| 284 | check_cancelled=_never_cancelled, |
| 285 | maxjobs=None, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 286 | newline_on_success=False, |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 287 | travis=False, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 288 | cache=None): |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 289 | js = Jobset(check_cancelled, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 290 | maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 291 | newline_on_success, travis, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 292 | cache if cache is not None else NoCache()) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 293 | for cmdline in shuffle_iteratable(cmdlines): |
| 294 | if not js.start(cmdline): |
| 295 | break |
| 296 | return js.finish() |