blob: 3999537c40e4dfb02d5ae55f3b5c0a8008b7c64f [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
32import multiprocessing
Craig Tiller71735182015-01-15 17:07:13 -080033import os
Craig Tiller5058c692015-04-08 09:42:04 -070034import platform
Craig Tiller5f735a62016-01-20 09:31:15 -080035import re
Craig Tiller336ad502015-02-24 14:46:02 -080036import signal
Nicolas Nobleddef2462015-01-06 18:08:25 -080037import subprocess
38import sys
ctiller3040cb72015-01-07 12:13:17 -080039import tempfile
40import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080041
ctiller3040cb72015-01-07 12:13:17 -080042
Craig Tiller5f735a62016-01-20 09:31:15 -080043# cpu cost measurement
44measure_cpu_costs = False
45
46
ctiller94e5dde2015-01-09 10:41:59 -080047_DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count()
Adele Zhoud01cbe32015-11-02 14:20:43 -080048_MAX_RESULT_SIZE = 8192
Nicolas Nobleddef2462015-01-06 18:08:25 -080049
Masood Malekghassemi768b1db2016-06-06 16:45:19 -070050def sanitized_environment(env):
51 sanitized = {}
52 for key, value in env.items():
53 sanitized[str(key).encode()] = str(value).encode()
54 return sanitized
55
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +010056def platform_string():
57 if platform.system() == 'Windows':
58 return 'windows'
59 elif platform.system()[:7] == 'MSYS_NT':
60 return 'windows'
61 elif platform.system() == 'Darwin':
62 return 'mac'
63 elif platform.system() == 'Linux':
64 return 'linux'
65 else:
66 return 'posix'
67
Nicolas Nobleddef2462015-01-06 18:08:25 -080068
Craig Tiller336ad502015-02-24 14:46:02 -080069# setup a signal handler so that signal.pause registers 'something'
70# when a child finishes
71# not using futures and threading to avoid a dependency on subprocess32
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +010072if platform_string() == 'windows':
Craig Tiller5058c692015-04-08 09:42:04 -070073 pass
74else:
75 have_alarm = False
76 def alarm_handler(unused_signum, unused_frame):
77 global have_alarm
78 have_alarm = False
79
80 signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None)
81 signal.signal(signal.SIGALRM, alarm_handler)
Craig Tiller336ad502015-02-24 14:46:02 -080082
83
ctiller3040cb72015-01-07 12:13:17 -080084_SUCCESS = object()
85_FAILURE = object()
86_RUNNING = object()
87_KILLED = object()
88
89
Craig Tiller3b083062015-01-12 13:51:28 -080090_COLORS = {
Nicolas Noble044db742015-01-14 16:57:24 -080091 'red': [ 31, 0 ],
92 'green': [ 32, 0 ],
93 'yellow': [ 33, 0 ],
94 'lightgray': [ 37, 0],
95 'gray': [ 30, 1 ],
Craig Tillerd7e09c32015-09-25 11:33:39 -070096 'purple': [ 35, 0 ],
Craig Tiller3b083062015-01-12 13:51:28 -080097 }
98
99
100_BEGINNING_OF_LINE = '\x1b[0G'
101_CLEAR_LINE = '\x1b[2K'
102
103
104_TAG_COLOR = {
105 'FAILED': 'red',
Craig Tillerd7e09c32015-09-25 11:33:39 -0700106 'FLAKE': 'purple',
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700107 'TIMEOUT_FLAKE': 'purple',
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700108 'WARNING': 'yellow',
Craig Tillere1d0d1c2015-02-27 08:54:23 -0800109 'TIMEOUT': 'red',
Craig Tiller3b083062015-01-12 13:51:28 -0800110 'PASSED': 'green',
Nicolas Noble044db742015-01-14 16:57:24 -0800111 'START': 'gray',
Craig Tiller3b083062015-01-12 13:51:28 -0800112 'WAITING': 'yellow',
Nicolas Noble044db742015-01-14 16:57:24 -0800113 'SUCCESS': 'green',
114 'IDLE': 'gray',
Craig Tiller3b083062015-01-12 13:51:28 -0800115 }
116
117
Nicolas "Pixel" Noble99768ac2015-05-13 02:34:06 +0200118def message(tag, msg, explanatory_text=None, do_newline=False):
119 if message.old_tag == tag and message.old_msg == msg and not explanatory_text:
120 return
121 message.old_tag = tag
122 message.old_msg = msg
Craig Tiller23d2f3f2015-02-24 15:23:32 -0800123 try:
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +0100124 if platform_string() == 'windows' or not sys.stdout.isatty():
Craig Tiller9f3b2d72015-08-25 11:50:57 -0700125 if explanatory_text:
126 print explanatory_text
127 print '%s: %s' % (tag, msg)
128 return
vjpaia29d2d72015-07-08 10:31:15 -0700129 sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % (
130 _BEGINNING_OF_LINE,
131 _CLEAR_LINE,
132 '\n%s' % explanatory_text if explanatory_text is not None else '',
133 _COLORS[_TAG_COLOR[tag]][1],
134 _COLORS[_TAG_COLOR[tag]][0],
135 tag,
136 msg,
137 '\n' if do_newline or explanatory_text is not None else ''))
Craig Tiller23d2f3f2015-02-24 15:23:32 -0800138 sys.stdout.flush()
139 except:
140 pass
Craig Tiller3b083062015-01-12 13:51:28 -0800141
Adele Zhoue4c35612015-10-16 15:34:23 -0700142message.old_tag = ''
143message.old_msg = ''
Craig Tiller3b083062015-01-12 13:51:28 -0800144
Craig Tiller71735182015-01-15 17:07:13 -0800145def which(filename):
146 if '/' in filename:
147 return filename
148 for path in os.environ['PATH'].split(os.pathsep):
149 if os.path.exists(os.path.join(path, filename)):
150 return os.path.join(path, filename)
151 raise Exception('%s not found' % filename)
152
153
Craig Tiller547db2b2015-01-30 14:08:39 -0800154class JobSpec(object):
155 """Specifies what to run for a job."""
156
Craig Tiller74189cd2016-06-23 15:39:06 -0700157 def __init__(self, cmdline, shortname=None, environ=None,
Craig Tiller95cc07b2015-09-28 13:41:30 -0700158 cwd=None, shell=False, timeout_seconds=5*60, flake_retries=0,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700159 timeout_retries=0, kill_handler=None, cpu_cost=1.0,
160 verbose_success=False):
Craig Tiller547db2b2015-01-30 14:08:39 -0800161 """
162 Arguments:
163 cmdline: a list of arguments to pass as the command line
164 environ: a dictionary of environment variables to set in the child process
Jan Tattermusche2686282015-10-08 16:27:07 -0700165 kill_handler: a handler that will be called whenever job.kill() is invoked
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800166 cpu_cost: number of cores per second this job needs
Craig Tiller547db2b2015-01-30 14:08:39 -0800167 """
murgatroid99132ce6a2015-03-04 17:29:14 -0800168 if environ is None:
169 environ = {}
Craig Tiller547db2b2015-01-30 14:08:39 -0800170 self.cmdline = cmdline
171 self.environ = environ
172 self.shortname = cmdline[0] if shortname is None else shortname
Craig Tiller5058c692015-04-08 09:42:04 -0700173 self.cwd = cwd
Jan Tattermusche8243592015-04-17 14:14:01 -0700174 self.shell = shell
Jan Tattermusch725835a2015-08-01 21:02:35 -0700175 self.timeout_seconds = timeout_seconds
Craig Tiller91318bc2015-09-24 08:58:39 -0700176 self.flake_retries = flake_retries
Craig Tillerbfc8a062015-09-28 14:40:21 -0700177 self.timeout_retries = timeout_retries
Jan Tattermusche2686282015-10-08 16:27:07 -0700178 self.kill_handler = kill_handler
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800179 self.cpu_cost = cpu_cost
Jan Tattermuschb2758442016-03-28 09:32:20 -0700180 self.verbose_success = verbose_success
Craig Tiller547db2b2015-01-30 14:08:39 -0800181
182 def identity(self):
Craig Tiller74189cd2016-06-23 15:39:06 -0700183 return '%r %r' % (self.cmdline, self.environ)
Craig Tiller547db2b2015-01-30 14:08:39 -0800184
185 def __hash__(self):
186 return hash(self.identity())
187
188 def __cmp__(self, other):
189 return self.identity() == other.identity()
Craig Tillerdb218992016-01-05 09:28:46 -0800190
Jan Tattermusch2dd156e2015-12-04 18:26:17 -0800191 def __repr__(self):
192 return 'JobSpec(shortname=%s, cmdline=%s)' % (self.shortname, self.cmdline)
Craig Tiller547db2b2015-01-30 14:08:39 -0800193
194
Adele Zhoue4c35612015-10-16 15:34:23 -0700195class JobResult(object):
196 def __init__(self):
197 self.state = 'UNKNOWN'
198 self.returncode = -1
199 self.elapsed_time = 0
Adele Zhoud5fffa52015-10-23 15:51:42 -0700200 self.num_failures = 0
Adele Zhoue4c35612015-10-16 15:34:23 -0700201 self.retries = 0
202 self.message = ''
Craig Tillerdb218992016-01-05 09:28:46 -0800203
Adele Zhoue4c35612015-10-16 15:34:23 -0700204
ctiller3040cb72015-01-07 12:13:17 -0800205class Job(object):
206 """Manages one job."""
207
Craig Tiller74189cd2016-06-23 15:39:06 -0700208 def __init__(self, spec, newline_on_success, travis, add_env):
Craig Tiller547db2b2015-01-30 14:08:39 -0800209 self._spec = spec
Nicolas Noble044db742015-01-14 16:57:24 -0800210 self._newline_on_success = newline_on_success
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100211 self._travis = travis
Craig Tiller91318bc2015-09-24 08:58:39 -0700212 self._add_env = add_env.copy()
Craig Tiller91318bc2015-09-24 08:58:39 -0700213 self._retries = 0
Craig Tiller95cc07b2015-09-28 13:41:30 -0700214 self._timeout_retries = 0
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700215 self._suppress_failure_message = False
Craig Tillerb84728d2015-02-26 15:40:39 -0800216 message('START', spec.shortname, do_newline=self._travis)
Adele Zhoue4c35612015-10-16 15:34:23 -0700217 self.result = JobResult()
Craig Tiller91318bc2015-09-24 08:58:39 -0700218 self.start()
219
Adele Zhoue4c35612015-10-16 15:34:23 -0700220 def GetSpec(self):
221 return self._spec
222
Craig Tiller91318bc2015-09-24 08:58:39 -0700223 def start(self):
224 self._tempfile = tempfile.TemporaryFile()
225 env = dict(os.environ)
226 env.update(self._spec.environ)
227 env.update(self._add_env)
Masood Malekghassemi768b1db2016-06-06 16:45:19 -0700228 env = sanitized_environment(env)
Craig Tiller91318bc2015-09-24 08:58:39 -0700229 self._start = time.time()
Craig Tiller5f735a62016-01-20 09:31:15 -0800230 cmdline = self._spec.cmdline
231 if measure_cpu_costs:
232 cmdline = ['time', '--portability'] + cmdline
233 try_start = lambda: subprocess.Popen(args=cmdline,
Craig Tiller60078bb2015-11-04 16:39:54 -0800234 stderr=subprocess.STDOUT,
235 stdout=self._tempfile,
236 cwd=self._spec.cwd,
237 shell=self._spec.shell,
238 env=env)
239 delay = 0.3
240 for i in range(0, 4):
241 try:
242 self._process = try_start()
243 break
244 except OSError:
245 message('WARNING', 'Failed to start %s, retrying in %f seconds' % (self._spec.shortname, delay))
246 time.sleep(delay)
247 delay *= 2
248 else:
249 self._process = try_start()
Craig Tiller91318bc2015-09-24 08:58:39 -0700250 self._state = _RUNNING
ctiller3040cb72015-01-07 12:13:17 -0800251
Craig Tiller74189cd2016-06-23 15:39:06 -0700252 def state(self):
ctiller3040cb72015-01-07 12:13:17 -0800253 """Poll current state of the job. Prints messages at completion."""
Craig Tillerdb218992016-01-05 09:28:46 -0800254 def stdout(self=self):
255 self._tempfile.seek(0)
256 stdout = self._tempfile.read()
257 self.result.message = stdout[-_MAX_RESULT_SIZE:]
258 return stdout
ctiller3040cb72015-01-07 12:13:17 -0800259 if self._state == _RUNNING and self._process.poll() is not None:
Craig Tiller9d6139a2015-02-26 15:24:43 -0800260 elapsed = time.time() - self._start
Adele Zhoue4c35612015-10-16 15:34:23 -0700261 self.result.elapsed_time = elapsed
ctiller3040cb72015-01-07 12:13:17 -0800262 if self._process.returncode != 0:
Craig Tiller91318bc2015-09-24 08:58:39 -0700263 if self._retries < self._spec.flake_retries:
264 message('FLAKE', '%s [ret=%d, pid=%d]' % (
Craig Tillerd0ffe142015-05-19 21:51:13 -0700265 self._spec.shortname, self._process.returncode, self._process.pid),
Craig Tillerdb218992016-01-05 09:28:46 -0800266 stdout(), do_newline=True)
Craig Tiller91318bc2015-09-24 08:58:39 -0700267 self._retries += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700268 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700269 self.result.retries = self._timeout_retries + self._retries
Craig Tiller91318bc2015-09-24 08:58:39 -0700270 self.start()
271 else:
272 self._state = _FAILURE
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700273 if not self._suppress_failure_message:
274 message('FAILED', '%s [ret=%d, pid=%d]' % (
275 self._spec.shortname, self._process.returncode, self._process.pid),
Craig Tillerdb218992016-01-05 09:28:46 -0800276 stdout(), do_newline=True)
Adele Zhoue4c35612015-10-16 15:34:23 -0700277 self.result.state = 'FAILED'
Adele Zhoud5fffa52015-10-23 15:51:42 -0700278 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700279 self.result.returncode = self._process.returncode
ctiller3040cb72015-01-07 12:13:17 -0800280 else:
281 self._state = _SUCCESS
Craig Tiller5f735a62016-01-20 09:31:15 -0800282 measurement = ''
283 if measure_cpu_costs:
284 m = re.search(r'real ([0-9.]+)\nuser ([0-9.]+)\nsys ([0-9.]+)', stdout())
285 real = float(m.group(1))
286 user = float(m.group(2))
287 sys = float(m.group(3))
288 if real > 0.5:
289 cores = (user + sys) / real
Craig Tillerbfe69362016-01-20 09:38:21 -0800290 measurement = '; cpu_cost=%.01f; estimated=%.01f' % (cores, self._spec.cpu_cost)
Craig Tiller5f735a62016-01-20 09:31:15 -0800291 message('PASSED', '%s [time=%.1fsec; retries=%d:%d%s]' % (
Jan Tattermuschb2758442016-03-28 09:32:20 -0700292 self._spec.shortname, elapsed, self._retries, self._timeout_retries, measurement),
293 stdout() if self._spec.verbose_success else None,
Craig Tiller95cc07b2015-09-28 13:41:30 -0700294 do_newline=self._newline_on_success or self._travis)
Adele Zhoue4c35612015-10-16 15:34:23 -0700295 self.result.state = 'PASSED'
Craig Tiller5f735a62016-01-20 09:31:15 -0800296 elif (self._state == _RUNNING and
297 self._spec.timeout_seconds is not None and
Craig Tiller590105a2016-01-19 13:03:46 -0800298 time.time() - self._start > self._spec.timeout_seconds):
Craig Tiller95cc07b2015-09-28 13:41:30 -0700299 if self._timeout_retries < self._spec.timeout_retries:
Craig Tillerdb218992016-01-05 09:28:46 -0800300 message('TIMEOUT_FLAKE', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True)
Craig Tiller95cc07b2015-09-28 13:41:30 -0700301 self._timeout_retries += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700302 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700303 self.result.retries = self._timeout_retries + self._retries
Jan Tattermusch39e3cb32015-10-22 18:21:08 -0700304 if self._spec.kill_handler:
305 self._spec.kill_handler(self)
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700306 self._process.terminate()
307 self.start()
308 else:
Craig Tillerdb218992016-01-05 09:28:46 -0800309 message('TIMEOUT', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True)
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700310 self.kill()
Adele Zhoue4c35612015-10-16 15:34:23 -0700311 self.result.state = 'TIMEOUT'
Adele Zhoud5fffa52015-10-23 15:51:42 -0700312 self.result.num_failures += 1
ctiller3040cb72015-01-07 12:13:17 -0800313 return self._state
314
315 def kill(self):
316 if self._state == _RUNNING:
317 self._state = _KILLED
Jan Tattermusche2686282015-10-08 16:27:07 -0700318 if self._spec.kill_handler:
319 self._spec.kill_handler(self)
ctiller3040cb72015-01-07 12:13:17 -0800320 self._process.terminate()
321
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700322 def suppress_failure_message(self):
323 self._suppress_failure_message = True
Craig Tillerdb218992016-01-05 09:28:46 -0800324
ctiller3040cb72015-01-07 12:13:17 -0800325
Nicolas Nobleddef2462015-01-06 18:08:25 -0800326class Jobset(object):
327 """Manages one run of jobs."""
328
Craig Tiller533b1a22015-05-29 08:41:29 -0700329 def __init__(self, check_cancelled, maxjobs, newline_on_success, travis,
Craig Tiller74189cd2016-06-23 15:39:06 -0700330 stop_on_failure, add_env):
ctiller3040cb72015-01-07 12:13:17 -0800331 self._running = set()
332 self._check_cancelled = check_cancelled
333 self._cancelled = False
Nicolas Nobleddef2462015-01-06 18:08:25 -0800334 self._failures = 0
Craig Tiller738c3342015-01-12 14:28:33 -0800335 self._completed = 0
ctiller94e5dde2015-01-09 10:41:59 -0800336 self._maxjobs = maxjobs
Nicolas Noble044db742015-01-14 16:57:24 -0800337 self._newline_on_success = newline_on_success
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100338 self._travis = travis
Craig Tiller533b1a22015-05-29 08:41:29 -0700339 self._stop_on_failure = stop_on_failure
Craig Tillerf53d9c82015-08-04 14:19:43 -0700340 self._add_env = add_env
Adele Zhoue4c35612015-10-16 15:34:23 -0700341 self.resultset = {}
Craig Tiller6364dcb2015-11-24 16:29:06 -0800342 self._remaining = None
Craig Tillered735102016-04-06 12:59:23 -0700343 self._start_time = time.time()
Craig Tiller6364dcb2015-11-24 16:29:06 -0800344
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 Tiller74189cd2016-06-23 15:39:06 -0700366 job = Job(spec,
367 self._newline_on_success,
368 self._travis,
369 self._add_env)
370 self._running.add(job)
371 if not self.resultset.has_key(job.GetSpec().shortname):
372 self.resultset[job.GetSpec().shortname] = []
Craig Tillerddf02c12016-06-24 14:04:01 -0700373 return True
Nicolas Nobleddef2462015-01-06 18:08:25 -0800374
ctiller3040cb72015-01-07 12:13:17 -0800375 def reap(self):
376 """Collect the dead jobs."""
377 while self._running:
378 dead = set()
379 for job in self._running:
Craig Tiller74189cd2016-06-23 15:39:06 -0700380 st = job.state()
ctiller3040cb72015-01-07 12:13:17 -0800381 if st == _RUNNING: continue
Craig Tiller533b1a22015-05-29 08:41:29 -0700382 if st == _FAILURE or st == _KILLED:
383 self._failures += 1
384 if self._stop_on_failure:
385 self._cancelled = True
386 for job in self._running:
387 job.kill()
ctiller3040cb72015-01-07 12:13:17 -0800388 dead.add(job)
Craig Tiller74e770d2015-06-11 09:38:09 -0700389 break
ctiller3040cb72015-01-07 12:13:17 -0800390 for job in dead:
Craig Tiller738c3342015-01-12 14:28:33 -0800391 self._completed += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700392 self.resultset[job.GetSpec().shortname].append(job.result)
ctiller3040cb72015-01-07 12:13:17 -0800393 self._running.remove(job)
Craig Tiller3b083062015-01-12 13:51:28 -0800394 if dead: return
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100395 if (not self._travis):
Craig Tiller2c23ad52015-12-04 06:50:38 -0800396 rstr = '' if self._remaining is None else '%d queued, ' % self._remaining
Craig Tillered735102016-04-06 12:59:23 -0700397 if self._remaining is not None and self._completed > 0:
398 now = time.time()
399 sofar = now - self._start_time
400 remaining = sofar / self._completed * (self._remaining + len(self._running))
401 rstr = 'ETA %.1f sec; %s' % (remaining, rstr)
Craig Tiller2c23ad52015-12-04 06:50:38 -0800402 message('WAITING', '%s%d jobs running, %d complete, %d failed' % (
403 rstr, len(self._running), self._completed, self._failures))
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +0100404 if platform_string() == 'windows':
Craig Tiller5058c692015-04-08 09:42:04 -0700405 time.sleep(0.1)
406 else:
407 global have_alarm
408 if not have_alarm:
409 have_alarm = True
410 signal.alarm(10)
411 signal.pause()
ctiller3040cb72015-01-07 12:13:17 -0800412
413 def cancelled(self):
414 """Poll for cancellation."""
415 if self._cancelled: return True
416 if not self._check_cancelled(): return False
417 for job in self._running:
418 job.kill()
419 self._cancelled = True
420 return True
421
422 def finish(self):
423 while self._running:
424 if self.cancelled(): pass # poll cancellation
425 self.reap()
426 return not self.cancelled() and self._failures == 0
Nicolas Nobleddef2462015-01-06 18:08:25 -0800427
428
ctiller3040cb72015-01-07 12:13:17 -0800429def _never_cancelled():
430 return False
431
432
Craig Tiller6364dcb2015-11-24 16:29:06 -0800433def tag_remaining(xs):
434 staging = []
435 for x in xs:
436 staging.append(x)
Craig Tillered735102016-04-06 12:59:23 -0700437 if len(staging) > 5000:
Craig Tiller6364dcb2015-11-24 16:29:06 -0800438 yield (staging.pop(0), None)
439 n = len(staging)
440 for i, x in enumerate(staging):
441 yield (x, n - i - 1)
442
443
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800444def run(cmdlines,
445 check_cancelled=_never_cancelled,
446 maxjobs=None,
Craig Tiller71735182015-01-15 17:07:13 -0800447 newline_on_success=False,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100448 travis=False,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700449 infinite_runs=False,
Craig Tiller533b1a22015-05-29 08:41:29 -0700450 stop_on_failure=False,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700451 add_env={}):
ctiller94e5dde2015-01-09 10:41:59 -0800452 js = Jobset(check_cancelled,
Nicolas Noble044db742015-01-14 16:57:24 -0800453 maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS,
Craig Tiller74189cd2016-06-23 15:39:06 -0700454 newline_on_success, travis, stop_on_failure, add_env)
Craig Tiller6364dcb2015-11-24 16:29:06 -0800455 for cmdline, remaining in tag_remaining(cmdlines):
ctiller3040cb72015-01-07 12:13:17 -0800456 if not js.start(cmdline):
457 break
Craig Tiller6364dcb2015-11-24 16:29:06 -0800458 if remaining is not None:
459 js.set_remaining(remaining)
460 js.finish()
Adele Zhoue4c35612015-10-16 15:34:23 -0700461 return js.get_num_failures(), js.resultset