blob: af4b5e09b54d416f468892afc8ab3a8e35132369 [file] [log] [blame]
Craig Tiller6169d5f2016-03-31 07:46:18 -07001# Copyright 2015, Google Inc.
Craig Tillerc2c79212015-02-16 12:00:01 -08002# 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 Nobleddef2462015-01-06 18:08:25 -080030"""Run a group of subprocesses and then finish."""
31
Craig Tiller71735182015-01-15 17:07:13 -080032import hashlib
Nicolas Nobleddef2462015-01-06 18:08:25 -080033import multiprocessing
Craig Tiller71735182015-01-15 17:07:13 -080034import os
Craig Tiller5058c692015-04-08 09:42:04 -070035import platform
Craig Tiller5f735a62016-01-20 09:31:15 -080036import re
Craig Tiller336ad502015-02-24 14:46:02 -080037import signal
Nicolas Nobleddef2462015-01-06 18:08:25 -080038import subprocess
39import sys
ctiller3040cb72015-01-07 12:13:17 -080040import tempfile
41import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080042
ctiller3040cb72015-01-07 12:13:17 -080043
Craig Tiller5f735a62016-01-20 09:31:15 -080044# cpu cost measurement
45measure_cpu_costs = False
46
47
ctiller94e5dde2015-01-09 10:41:59 -080048_DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count()
Adele Zhoud01cbe32015-11-02 14:20:43 -080049_MAX_RESULT_SIZE = 8192
Nicolas Nobleddef2462015-01-06 18:08:25 -080050
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +010051def platform_string():
52 if platform.system() == 'Windows':
53 return 'windows'
54 elif platform.system()[:7] == 'MSYS_NT':
55 return 'windows'
56 elif platform.system() == 'Darwin':
57 return 'mac'
58 elif platform.system() == 'Linux':
59 return 'linux'
60 else:
61 return 'posix'
62
Nicolas Nobleddef2462015-01-06 18:08:25 -080063
Craig Tiller336ad502015-02-24 14:46:02 -080064# setup a signal handler so that signal.pause registers 'something'
65# when a child finishes
66# not using futures and threading to avoid a dependency on subprocess32
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +010067if platform_string() == 'windows':
Craig Tiller5058c692015-04-08 09:42:04 -070068 pass
69else:
70 have_alarm = False
71 def alarm_handler(unused_signum, unused_frame):
72 global have_alarm
73 have_alarm = False
74
75 signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None)
76 signal.signal(signal.SIGALRM, alarm_handler)
Craig Tiller336ad502015-02-24 14:46:02 -080077
78
ctiller3040cb72015-01-07 12:13:17 -080079_SUCCESS = object()
80_FAILURE = object()
81_RUNNING = object()
82_KILLED = object()
83
84
Craig Tiller3b083062015-01-12 13:51:28 -080085_COLORS = {
Nicolas Noble044db742015-01-14 16:57:24 -080086 'red': [ 31, 0 ],
87 'green': [ 32, 0 ],
88 'yellow': [ 33, 0 ],
89 'lightgray': [ 37, 0],
90 'gray': [ 30, 1 ],
Craig Tillerd7e09c32015-09-25 11:33:39 -070091 'purple': [ 35, 0 ],
Craig Tiller3b083062015-01-12 13:51:28 -080092 }
93
94
95_BEGINNING_OF_LINE = '\x1b[0G'
96_CLEAR_LINE = '\x1b[2K'
97
98
99_TAG_COLOR = {
100 'FAILED': 'red',
Craig Tillerd7e09c32015-09-25 11:33:39 -0700101 'FLAKE': 'purple',
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700102 'TIMEOUT_FLAKE': 'purple',
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700103 'WARNING': 'yellow',
Craig Tillere1d0d1c2015-02-27 08:54:23 -0800104 'TIMEOUT': 'red',
Craig Tiller3b083062015-01-12 13:51:28 -0800105 'PASSED': 'green',
Nicolas Noble044db742015-01-14 16:57:24 -0800106 'START': 'gray',
Craig Tiller3b083062015-01-12 13:51:28 -0800107 'WAITING': 'yellow',
Nicolas Noble044db742015-01-14 16:57:24 -0800108 'SUCCESS': 'green',
109 'IDLE': 'gray',
Craig Tiller3b083062015-01-12 13:51:28 -0800110 }
111
112
Nicolas "Pixel" Noble99768ac2015-05-13 02:34:06 +0200113def message(tag, msg, explanatory_text=None, do_newline=False):
114 if message.old_tag == tag and message.old_msg == msg and not explanatory_text:
115 return
116 message.old_tag = tag
117 message.old_msg = msg
Craig Tiller23d2f3f2015-02-24 15:23:32 -0800118 try:
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +0100119 if platform_string() == 'windows' or not sys.stdout.isatty():
Craig Tiller9f3b2d72015-08-25 11:50:57 -0700120 if explanatory_text:
121 print explanatory_text
122 print '%s: %s' % (tag, msg)
123 return
vjpaia29d2d72015-07-08 10:31:15 -0700124 sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % (
125 _BEGINNING_OF_LINE,
126 _CLEAR_LINE,
127 '\n%s' % explanatory_text if explanatory_text is not None else '',
128 _COLORS[_TAG_COLOR[tag]][1],
129 _COLORS[_TAG_COLOR[tag]][0],
130 tag,
131 msg,
132 '\n' if do_newline or explanatory_text is not None else ''))
Craig Tiller23d2f3f2015-02-24 15:23:32 -0800133 sys.stdout.flush()
134 except:
135 pass
Craig Tiller3b083062015-01-12 13:51:28 -0800136
Adele Zhoue4c35612015-10-16 15:34:23 -0700137message.old_tag = ''
138message.old_msg = ''
Craig Tiller3b083062015-01-12 13:51:28 -0800139
Craig Tiller71735182015-01-15 17:07:13 -0800140def which(filename):
141 if '/' in filename:
142 return filename
143 for path in os.environ['PATH'].split(os.pathsep):
144 if os.path.exists(os.path.join(path, filename)):
145 return os.path.join(path, filename)
146 raise Exception('%s not found' % filename)
147
148
Craig Tiller547db2b2015-01-30 14:08:39 -0800149class JobSpec(object):
150 """Specifies what to run for a job."""
151
Jan Tattermusch725835a2015-08-01 21:02:35 -0700152 def __init__(self, cmdline, shortname=None, environ=None, hash_targets=None,
Craig Tiller95cc07b2015-09-28 13:41:30 -0700153 cwd=None, shell=False, timeout_seconds=5*60, flake_retries=0,
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800154 timeout_retries=0, kill_handler=None, cpu_cost=1.0):
Craig Tiller547db2b2015-01-30 14:08:39 -0800155 """
156 Arguments:
157 cmdline: a list of arguments to pass as the command line
158 environ: a dictionary of environment variables to set in the child process
159 hash_targets: which files to include in the hash representing the jobs version
160 (or empty, indicating the job should not be hashed)
Jan Tattermusche2686282015-10-08 16:27:07 -0700161 kill_handler: a handler that will be called whenever job.kill() is invoked
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800162 cpu_cost: number of cores per second this job needs
Craig Tiller547db2b2015-01-30 14:08:39 -0800163 """
murgatroid99132ce6a2015-03-04 17:29:14 -0800164 if environ is None:
165 environ = {}
166 if hash_targets is None:
167 hash_targets = []
Craig Tiller547db2b2015-01-30 14:08:39 -0800168 self.cmdline = cmdline
169 self.environ = environ
170 self.shortname = cmdline[0] if shortname is None else shortname
171 self.hash_targets = hash_targets or []
Craig Tiller5058c692015-04-08 09:42:04 -0700172 self.cwd = cwd
Jan Tattermusche8243592015-04-17 14:14:01 -0700173 self.shell = shell
Jan Tattermusch725835a2015-08-01 21:02:35 -0700174 self.timeout_seconds = timeout_seconds
Craig Tiller91318bc2015-09-24 08:58:39 -0700175 self.flake_retries = flake_retries
Craig Tillerbfc8a062015-09-28 14:40:21 -0700176 self.timeout_retries = timeout_retries
Jan Tattermusche2686282015-10-08 16:27:07 -0700177 self.kill_handler = kill_handler
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800178 self.cpu_cost = cpu_cost
Craig Tiller547db2b2015-01-30 14:08:39 -0800179
180 def identity(self):
181 return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets)
182
183 def __hash__(self):
184 return hash(self.identity())
185
186 def __cmp__(self, other):
187 return self.identity() == other.identity()
Craig Tillerdb218992016-01-05 09:28:46 -0800188
Jan Tattermusch2dd156e2015-12-04 18:26:17 -0800189 def __repr__(self):
190 return 'JobSpec(shortname=%s, cmdline=%s)' % (self.shortname, self.cmdline)
Craig Tiller547db2b2015-01-30 14:08:39 -0800191
192
Adele Zhoue4c35612015-10-16 15:34:23 -0700193class JobResult(object):
194 def __init__(self):
195 self.state = 'UNKNOWN'
196 self.returncode = -1
197 self.elapsed_time = 0
Adele Zhoud5fffa52015-10-23 15:51:42 -0700198 self.num_failures = 0
Adele Zhoue4c35612015-10-16 15:34:23 -0700199 self.retries = 0
200 self.message = ''
Craig Tillerdb218992016-01-05 09:28:46 -0800201
Adele Zhoue4c35612015-10-16 15:34:23 -0700202
ctiller3040cb72015-01-07 12:13:17 -0800203class Job(object):
204 """Manages one job."""
205
Adele Zhou2271ab52015-10-28 13:59:14 -0700206 def __init__(self, spec, bin_hash, newline_on_success, travis, add_env):
Craig Tiller547db2b2015-01-30 14:08:39 -0800207 self._spec = spec
Craig Tiller71735182015-01-15 17:07:13 -0800208 self._bin_hash = bin_hash
Nicolas Noble044db742015-01-14 16:57:24 -0800209 self._newline_on_success = newline_on_success
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100210 self._travis = travis
Craig Tiller91318bc2015-09-24 08:58:39 -0700211 self._add_env = add_env.copy()
Craig Tiller91318bc2015-09-24 08:58:39 -0700212 self._retries = 0
Craig Tiller95cc07b2015-09-28 13:41:30 -0700213 self._timeout_retries = 0
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700214 self._suppress_failure_message = False
Craig Tillerb84728d2015-02-26 15:40:39 -0800215 message('START', spec.shortname, do_newline=self._travis)
Adele Zhoue4c35612015-10-16 15:34:23 -0700216 self.result = JobResult()
Craig Tiller91318bc2015-09-24 08:58:39 -0700217 self.start()
218
Adele Zhoue4c35612015-10-16 15:34:23 -0700219 def GetSpec(self):
220 return self._spec
221
Craig Tiller91318bc2015-09-24 08:58:39 -0700222 def start(self):
223 self._tempfile = tempfile.TemporaryFile()
224 env = dict(os.environ)
225 env.update(self._spec.environ)
226 env.update(self._add_env)
227 self._start = time.time()
Craig Tiller5f735a62016-01-20 09:31:15 -0800228 cmdline = self._spec.cmdline
229 if measure_cpu_costs:
230 cmdline = ['time', '--portability'] + cmdline
231 try_start = lambda: subprocess.Popen(args=cmdline,
Craig Tiller60078bb2015-11-04 16:39:54 -0800232 stderr=subprocess.STDOUT,
233 stdout=self._tempfile,
234 cwd=self._spec.cwd,
235 shell=self._spec.shell,
236 env=env)
237 delay = 0.3
238 for i in range(0, 4):
239 try:
240 self._process = try_start()
241 break
242 except OSError:
243 message('WARNING', 'Failed to start %s, retrying in %f seconds' % (self._spec.shortname, delay))
244 time.sleep(delay)
245 delay *= 2
246 else:
247 self._process = try_start()
Craig Tiller91318bc2015-09-24 08:58:39 -0700248 self._state = _RUNNING
ctiller3040cb72015-01-07 12:13:17 -0800249
Craig Tiller71735182015-01-15 17:07:13 -0800250 def state(self, update_cache):
ctiller3040cb72015-01-07 12:13:17 -0800251 """Poll current state of the job. Prints messages at completion."""
Craig Tillerdb218992016-01-05 09:28:46 -0800252 def stdout(self=self):
253 self._tempfile.seek(0)
254 stdout = self._tempfile.read()
255 self.result.message = stdout[-_MAX_RESULT_SIZE:]
256 return stdout
ctiller3040cb72015-01-07 12:13:17 -0800257 if self._state == _RUNNING and self._process.poll() is not None:
Craig Tiller9d6139a2015-02-26 15:24:43 -0800258 elapsed = time.time() - self._start
Adele Zhoue4c35612015-10-16 15:34:23 -0700259 self.result.elapsed_time = elapsed
ctiller3040cb72015-01-07 12:13:17 -0800260 if self._process.returncode != 0:
Craig Tiller91318bc2015-09-24 08:58:39 -0700261 if self._retries < self._spec.flake_retries:
262 message('FLAKE', '%s [ret=%d, pid=%d]' % (
Craig Tillerd0ffe142015-05-19 21:51:13 -0700263 self._spec.shortname, self._process.returncode, self._process.pid),
Craig Tillerdb218992016-01-05 09:28:46 -0800264 stdout(), do_newline=True)
Craig Tiller91318bc2015-09-24 08:58:39 -0700265 self._retries += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700266 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700267 self.result.retries = self._timeout_retries + self._retries
Craig Tiller91318bc2015-09-24 08:58:39 -0700268 self.start()
269 else:
270 self._state = _FAILURE
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700271 if not self._suppress_failure_message:
272 message('FAILED', '%s [ret=%d, pid=%d]' % (
273 self._spec.shortname, self._process.returncode, self._process.pid),
Craig Tillerdb218992016-01-05 09:28:46 -0800274 stdout(), do_newline=True)
Adele Zhoue4c35612015-10-16 15:34:23 -0700275 self.result.state = 'FAILED'
Adele Zhoud5fffa52015-10-23 15:51:42 -0700276 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700277 self.result.returncode = self._process.returncode
ctiller3040cb72015-01-07 12:13:17 -0800278 else:
279 self._state = _SUCCESS
Craig Tiller5f735a62016-01-20 09:31:15 -0800280 measurement = ''
281 if measure_cpu_costs:
282 m = re.search(r'real ([0-9.]+)\nuser ([0-9.]+)\nsys ([0-9.]+)', stdout())
283 real = float(m.group(1))
284 user = float(m.group(2))
285 sys = float(m.group(3))
286 if real > 0.5:
287 cores = (user + sys) / real
Craig Tillerbfe69362016-01-20 09:38:21 -0800288 measurement = '; cpu_cost=%.01f; estimated=%.01f' % (cores, self._spec.cpu_cost)
Craig Tiller5f735a62016-01-20 09:31:15 -0800289 message('PASSED', '%s [time=%.1fsec; retries=%d:%d%s]' % (
290 self._spec.shortname, elapsed, self._retries, self._timeout_retries, measurement),
Craig Tiller95cc07b2015-09-28 13:41:30 -0700291 do_newline=self._newline_on_success or self._travis)
Adele Zhoue4c35612015-10-16 15:34:23 -0700292 self.result.state = 'PASSED'
Craig Tiller547db2b2015-01-30 14:08:39 -0800293 if self._bin_hash:
294 update_cache.finished(self._spec.identity(), self._bin_hash)
Craig Tiller5f735a62016-01-20 09:31:15 -0800295 elif (self._state == _RUNNING and
296 self._spec.timeout_seconds is not None and
Craig Tiller590105a2016-01-19 13:03:46 -0800297 time.time() - self._start > self._spec.timeout_seconds):
Craig Tiller95cc07b2015-09-28 13:41:30 -0700298 if self._timeout_retries < self._spec.timeout_retries:
Craig Tillerdb218992016-01-05 09:28:46 -0800299 message('TIMEOUT_FLAKE', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True)
Craig Tiller95cc07b2015-09-28 13:41:30 -0700300 self._timeout_retries += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700301 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700302 self.result.retries = self._timeout_retries + self._retries
Jan Tattermusch39e3cb32015-10-22 18:21:08 -0700303 if self._spec.kill_handler:
304 self._spec.kill_handler(self)
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700305 self._process.terminate()
306 self.start()
307 else:
Craig Tillerdb218992016-01-05 09:28:46 -0800308 message('TIMEOUT', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True)
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700309 self.kill()
Adele Zhoue4c35612015-10-16 15:34:23 -0700310 self.result.state = 'TIMEOUT'
Adele Zhoud5fffa52015-10-23 15:51:42 -0700311 self.result.num_failures += 1
ctiller3040cb72015-01-07 12:13:17 -0800312 return self._state
313
314 def kill(self):
315 if self._state == _RUNNING:
316 self._state = _KILLED
Jan Tattermusche2686282015-10-08 16:27:07 -0700317 if self._spec.kill_handler:
318 self._spec.kill_handler(self)
ctiller3040cb72015-01-07 12:13:17 -0800319 self._process.terminate()
320
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700321 def suppress_failure_message(self):
322 self._suppress_failure_message = True
Craig Tillerdb218992016-01-05 09:28:46 -0800323
ctiller3040cb72015-01-07 12:13:17 -0800324
Nicolas Nobleddef2462015-01-06 18:08:25 -0800325class Jobset(object):
326 """Manages one run of jobs."""
327
Craig Tiller533b1a22015-05-29 08:41:29 -0700328 def __init__(self, check_cancelled, maxjobs, newline_on_success, travis,
Adele Zhou2271ab52015-10-28 13:59:14 -0700329 stop_on_failure, add_env, cache):
ctiller3040cb72015-01-07 12:13:17 -0800330 self._running = set()
331 self._check_cancelled = check_cancelled
332 self._cancelled = False
Nicolas Nobleddef2462015-01-06 18:08:25 -0800333 self._failures = 0
Craig Tiller738c3342015-01-12 14:28:33 -0800334 self._completed = 0
ctiller94e5dde2015-01-09 10:41:59 -0800335 self._maxjobs = maxjobs
Nicolas Noble044db742015-01-14 16:57:24 -0800336 self._newline_on_success = newline_on_success
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100337 self._travis = travis
Craig Tiller71735182015-01-15 17:07:13 -0800338 self._cache = cache
Craig Tiller533b1a22015-05-29 08:41:29 -0700339 self._stop_on_failure = stop_on_failure
Craig Tiller74e770d2015-06-11 09:38:09 -0700340 self._hashes = {}
Craig Tillerf53d9c82015-08-04 14:19:43 -0700341 self._add_env = add_env
Adele Zhoue4c35612015-10-16 15:34:23 -0700342 self.resultset = {}
Craig Tiller6364dcb2015-11-24 16:29:06 -0800343 self._remaining = None
344
345 def set_remaining(self, remaining):
346 self._remaining = remaining
347
Adele Zhoue4c35612015-10-16 15:34:23 -0700348 def get_num_failures(self):
Craig Tiller6364dcb2015-11-24 16:29:06 -0800349 return self._failures
Nicolas Nobleddef2462015-01-06 18:08:25 -0800350
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800351 def cpu_cost(self):
352 c = 0
353 for job in self._running:
354 c += job._spec.cpu_cost
355 return c
356
Craig Tiller547db2b2015-01-30 14:08:39 -0800357 def start(self, spec):
ctiller3040cb72015-01-07 12:13:17 -0800358 """Start a job. Return True on success, False on failure."""
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800359 while True:
ctiller3040cb72015-01-07 12:13:17 -0800360 if self.cancelled(): return False
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800361 current_cpu_cost = self.cpu_cost()
362 if current_cpu_cost == 0: break
Craig Tiller3e301a32016-01-27 15:36:52 -0800363 if current_cpu_cost + spec.cpu_cost <= self._maxjobs: break
ctiller3040cb72015-01-07 12:13:17 -0800364 self.reap()
365 if self.cancelled(): return False
Craig Tiller547db2b2015-01-30 14:08:39 -0800366 if spec.hash_targets:
Craig Tiller74e770d2015-06-11 09:38:09 -0700367 if spec.identity() in self._hashes:
368 bin_hash = self._hashes[spec.identity()]
369 else:
370 bin_hash = hashlib.sha1()
371 for fn in spec.hash_targets:
372 with open(which(fn)) as f:
373 bin_hash.update(f.read())
374 bin_hash = bin_hash.hexdigest()
375 self._hashes[spec.identity()] = bin_hash
Craig Tiller547db2b2015-01-30 14:08:39 -0800376 should_run = self._cache.should_run(spec.identity(), bin_hash)
377 else:
378 bin_hash = None
379 should_run = True
380 if should_run:
Adele Zhoue4c35612015-10-16 15:34:23 -0700381 job = Job(spec,
382 bin_hash,
383 self._newline_on_success,
384 self._travis,
Adele Zhou2271ab52015-10-28 13:59:14 -0700385 self._add_env)
Adele Zhoue4c35612015-10-16 15:34:23 -0700386 self._running.add(job)
Jan Tattermusch494f3122016-03-01 13:55:42 -0800387 if not self.resultset.has_key(job.GetSpec().shortname):
388 self.resultset[job.GetSpec().shortname] = []
ctiller3040cb72015-01-07 12:13:17 -0800389 return True
Nicolas Nobleddef2462015-01-06 18:08:25 -0800390
ctiller3040cb72015-01-07 12:13:17 -0800391 def reap(self):
392 """Collect the dead jobs."""
393 while self._running:
394 dead = set()
395 for job in self._running:
Craig Tiller71735182015-01-15 17:07:13 -0800396 st = job.state(self._cache)
ctiller3040cb72015-01-07 12:13:17 -0800397 if st == _RUNNING: continue
Craig Tiller533b1a22015-05-29 08:41:29 -0700398 if st == _FAILURE or st == _KILLED:
399 self._failures += 1
400 if self._stop_on_failure:
401 self._cancelled = True
402 for job in self._running:
403 job.kill()
ctiller3040cb72015-01-07 12:13:17 -0800404 dead.add(job)
Craig Tiller74e770d2015-06-11 09:38:09 -0700405 break
ctiller3040cb72015-01-07 12:13:17 -0800406 for job in dead:
Craig Tiller738c3342015-01-12 14:28:33 -0800407 self._completed += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700408 self.resultset[job.GetSpec().shortname].append(job.result)
ctiller3040cb72015-01-07 12:13:17 -0800409 self._running.remove(job)
Craig Tiller3b083062015-01-12 13:51:28 -0800410 if dead: return
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100411 if (not self._travis):
Craig Tiller2c23ad52015-12-04 06:50:38 -0800412 rstr = '' if self._remaining is None else '%d queued, ' % self._remaining
413 message('WAITING', '%s%d jobs running, %d complete, %d failed' % (
414 rstr, len(self._running), self._completed, self._failures))
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +0100415 if platform_string() == 'windows':
Craig Tiller5058c692015-04-08 09:42:04 -0700416 time.sleep(0.1)
417 else:
418 global have_alarm
419 if not have_alarm:
420 have_alarm = True
421 signal.alarm(10)
422 signal.pause()
ctiller3040cb72015-01-07 12:13:17 -0800423
424 def cancelled(self):
425 """Poll for cancellation."""
426 if self._cancelled: return True
427 if not self._check_cancelled(): return False
428 for job in self._running:
429 job.kill()
430 self._cancelled = True
431 return True
432
433 def finish(self):
434 while self._running:
435 if self.cancelled(): pass # poll cancellation
436 self.reap()
437 return not self.cancelled() and self._failures == 0
Nicolas Nobleddef2462015-01-06 18:08:25 -0800438
439
ctiller3040cb72015-01-07 12:13:17 -0800440def _never_cancelled():
441 return False
442
443
Craig Tiller71735182015-01-15 17:07:13 -0800444# cache class that caches nothing
445class NoCache(object):
446 def should_run(self, cmdline, bin_hash):
447 return True
448
449 def finished(self, cmdline, bin_hash):
450 pass
451
452
Craig Tiller6364dcb2015-11-24 16:29:06 -0800453def tag_remaining(xs):
454 staging = []
455 for x in xs:
456 staging.append(x)
457 if len(staging) > 1000:
458 yield (staging.pop(0), None)
459 n = len(staging)
460 for i, x in enumerate(staging):
461 yield (x, n - i - 1)
462
463
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800464def run(cmdlines,
465 check_cancelled=_never_cancelled,
466 maxjobs=None,
Craig Tiller71735182015-01-15 17:07:13 -0800467 newline_on_success=False,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100468 travis=False,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700469 infinite_runs=False,
Craig Tiller533b1a22015-05-29 08:41:29 -0700470 stop_on_failure=False,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200471 cache=None,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700472 add_env={}):
ctiller94e5dde2015-01-09 10:41:59 -0800473 js = Jobset(check_cancelled,
Nicolas Noble044db742015-01-14 16:57:24 -0800474 maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700475 newline_on_success, travis, stop_on_failure, add_env,
Adele Zhou2271ab52015-10-28 13:59:14 -0700476 cache if cache is not None else NoCache())
Craig Tiller6364dcb2015-11-24 16:29:06 -0800477 for cmdline, remaining in tag_remaining(cmdlines):
ctiller3040cb72015-01-07 12:13:17 -0800478 if not js.start(cmdline):
479 break
Craig Tiller6364dcb2015-11-24 16:29:06 -0800480 if remaining is not None:
481 js.set_remaining(remaining)
482 js.finish()
Adele Zhoue4c35612015-10-16 15:34:23 -0700483 return js.get_num_failures(), js.resultset