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 |
| 36 | import subprocess |
| 37 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 38 | import tempfile |
| 39 | import time |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 40 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 41 | |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 42 | _DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count() |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def shuffle_iteratable(it): |
| 46 | """Return an iterable that randomly walks it""" |
| 47 | # take a random sampling from the passed in iterable |
| 48 | # we take an element with probablity 1/p and rapidly increase |
| 49 | # p as we take elements - this gives us a somewhat random set of values before |
| 50 | # we've seen all the values, but starts producing values without having to |
| 51 | # compute ALL of them at once, allowing tests to start a little earlier |
| 52 | nextit = [] |
| 53 | p = 1 |
| 54 | for val in it: |
| 55 | if random.randint(0, p) == 0: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 56 | p = min(p*2, 100) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 57 | yield val |
| 58 | else: |
| 59 | nextit.append(val) |
| 60 | # after taking a random sampling, we shuffle the rest of the elements and |
| 61 | # yield them |
| 62 | random.shuffle(nextit) |
| 63 | for val in nextit: |
| 64 | yield val |
| 65 | |
| 66 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 67 | _SUCCESS = object() |
| 68 | _FAILURE = object() |
| 69 | _RUNNING = object() |
| 70 | _KILLED = object() |
| 71 | |
| 72 | |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 73 | _COLORS = { |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 74 | 'red': [ 31, 0 ], |
| 75 | 'green': [ 32, 0 ], |
| 76 | 'yellow': [ 33, 0 ], |
| 77 | 'lightgray': [ 37, 0], |
| 78 | 'gray': [ 30, 1 ], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | _BEGINNING_OF_LINE = '\x1b[0G' |
| 83 | _CLEAR_LINE = '\x1b[2K' |
| 84 | |
| 85 | |
| 86 | _TAG_COLOR = { |
| 87 | 'FAILED': 'red', |
| 88 | 'PASSED': 'green', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 89 | 'START': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 90 | 'WAITING': 'yellow', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 91 | 'SUCCESS': 'green', |
| 92 | 'IDLE': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 96 | def message(tag, message, explanatory_text=None, do_newline=False): |
| 97 | 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] | 98 | _BEGINNING_OF_LINE, |
| 99 | _CLEAR_LINE, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 100 | '\n%s' % explanatory_text if explanatory_text is not None else '', |
| 101 | _COLORS[_TAG_COLOR[tag]][1], |
| 102 | _COLORS[_TAG_COLOR[tag]][0], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 103 | tag, |
| 104 | message, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 105 | '\n' if do_newline or explanatory_text is not None else '')) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 106 | sys.stdout.flush() |
| 107 | |
| 108 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 109 | def which(filename): |
| 110 | if '/' in filename: |
| 111 | return filename |
| 112 | for path in os.environ['PATH'].split(os.pathsep): |
| 113 | if os.path.exists(os.path.join(path, filename)): |
| 114 | return os.path.join(path, filename) |
| 115 | raise Exception('%s not found' % filename) |
| 116 | |
| 117 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 118 | class JobSpec(object): |
| 119 | """Specifies what to run for a job.""" |
| 120 | |
| 121 | def __init__(self, cmdline, shortname=None, environ={}, hash_targets=[]): |
| 122 | """ |
| 123 | Arguments: |
| 124 | cmdline: a list of arguments to pass as the command line |
| 125 | environ: a dictionary of environment variables to set in the child process |
| 126 | hash_targets: which files to include in the hash representing the jobs version |
| 127 | (or empty, indicating the job should not be hashed) |
| 128 | """ |
| 129 | self.cmdline = cmdline |
| 130 | self.environ = environ |
| 131 | self.shortname = cmdline[0] if shortname is None else shortname |
| 132 | self.hash_targets = hash_targets or [] |
| 133 | |
| 134 | def identity(self): |
| 135 | return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets) |
| 136 | |
| 137 | def __hash__(self): |
| 138 | return hash(self.identity()) |
| 139 | |
| 140 | def __cmp__(self, other): |
| 141 | return self.identity() == other.identity() |
| 142 | |
| 143 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 144 | class Job(object): |
| 145 | """Manages one job.""" |
| 146 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 147 | def __init__(self, spec, bin_hash, newline_on_success): |
| 148 | self._spec = spec |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 149 | self._bin_hash = bin_hash |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 150 | self._tempfile = tempfile.TemporaryFile() |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 151 | env = os.environ.copy() |
| 152 | for k, v in spec.environ.iteritems(): |
| 153 | env[k] = v |
| 154 | self._process = subprocess.Popen(args=spec.cmdline, |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 155 | stderr=subprocess.STDOUT, |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 156 | stdout=self._tempfile, |
| 157 | env=env) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 158 | self._state = _RUNNING |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 159 | self._newline_on_success = newline_on_success |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 160 | message('START', spec.shortname) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 161 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 162 | def state(self, update_cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 163 | """Poll current state of the job. Prints messages at completion.""" |
| 164 | if self._state == _RUNNING and self._process.poll() is not None: |
| 165 | if self._process.returncode != 0: |
| 166 | self._state = _FAILURE |
| 167 | self._tempfile.seek(0) |
| 168 | stdout = self._tempfile.read() |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 169 | message('FAILED', '%s [ret=%d]' % ( |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 170 | self._spec.shortname, self._process.returncode), stdout) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 171 | else: |
| 172 | self._state = _SUCCESS |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 173 | message('PASSED', self._spec.shortname, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 174 | do_newline=self._newline_on_success) |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 175 | if self._bin_hash: |
| 176 | update_cache.finished(self._spec.identity(), self._bin_hash) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 177 | return self._state |
| 178 | |
| 179 | def kill(self): |
| 180 | if self._state == _RUNNING: |
| 181 | self._state = _KILLED |
| 182 | self._process.terminate() |
| 183 | |
| 184 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 185 | class Jobset(object): |
| 186 | """Manages one run of jobs.""" |
| 187 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 188 | def __init__(self, check_cancelled, maxjobs, newline_on_success, cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 189 | self._running = set() |
| 190 | self._check_cancelled = check_cancelled |
| 191 | self._cancelled = False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 192 | self._failures = 0 |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 193 | self._completed = 0 |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 194 | self._maxjobs = maxjobs |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 195 | self._newline_on_success = newline_on_success |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 196 | self._cache = cache |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 197 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 198 | def start(self, spec): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 199 | """Start a job. Return True on success, False on failure.""" |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 200 | while len(self._running) >= self._maxjobs: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 201 | if self.cancelled(): return False |
| 202 | self.reap() |
| 203 | if self.cancelled(): return False |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 204 | if spec.hash_targets: |
| 205 | bin_hash = hashlib.sha1() |
| 206 | for fn in spec.hash_targets: |
| 207 | with open(which(fn)) as f: |
| 208 | bin_hash.update(f.read()) |
| 209 | bin_hash = bin_hash.hexdigest() |
| 210 | should_run = self._cache.should_run(spec.identity(), bin_hash) |
| 211 | else: |
| 212 | bin_hash = None |
| 213 | should_run = True |
| 214 | if should_run: |
| 215 | self._running.add(Job(spec, |
| 216 | bin_hash, |
| 217 | self._newline_on_success)) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 218 | return True |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 219 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 220 | def reap(self): |
| 221 | """Collect the dead jobs.""" |
| 222 | while self._running: |
| 223 | dead = set() |
| 224 | for job in self._running: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 225 | st = job.state(self._cache) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 226 | if st == _RUNNING: continue |
| 227 | if st == _FAILURE: self._failures += 1 |
| 228 | dead.add(job) |
| 229 | for job in dead: |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 230 | self._completed += 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 231 | self._running.remove(job) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 232 | if dead: return |
Craig Tiller | 6f5e2c4 | 2015-01-21 18:05:31 -0800 | [diff] [blame] | 233 | message('WAITING', '%d jobs running, %d complete, %d failed' % ( |
| 234 | len(self._running), self._completed, self._failures)) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 235 | time.sleep(0.1) |
| 236 | |
| 237 | def cancelled(self): |
| 238 | """Poll for cancellation.""" |
| 239 | if self._cancelled: return True |
| 240 | if not self._check_cancelled(): return False |
| 241 | for job in self._running: |
| 242 | job.kill() |
| 243 | self._cancelled = True |
| 244 | return True |
| 245 | |
| 246 | def finish(self): |
| 247 | while self._running: |
| 248 | if self.cancelled(): pass # poll cancellation |
| 249 | self.reap() |
| 250 | return not self.cancelled() and self._failures == 0 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 251 | |
| 252 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 253 | def _never_cancelled(): |
| 254 | return False |
| 255 | |
| 256 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 257 | # cache class that caches nothing |
| 258 | class NoCache(object): |
| 259 | def should_run(self, cmdline, bin_hash): |
| 260 | return True |
| 261 | |
| 262 | def finished(self, cmdline, bin_hash): |
| 263 | pass |
| 264 | |
| 265 | |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 266 | def run(cmdlines, |
| 267 | check_cancelled=_never_cancelled, |
| 268 | maxjobs=None, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 269 | newline_on_success=False, |
| 270 | cache=None): |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 271 | js = Jobset(check_cancelled, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 272 | maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 273 | newline_on_success, |
| 274 | cache if cache is not None else NoCache()) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 275 | for cmdline in shuffle_iteratable(cmdlines): |
| 276 | if not js.start(cmdline): |
| 277 | break |
| 278 | return js.finish() |