|
|
|
@ -11,7 +11,6 @@ |
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
|
|
# See the License for the specific language governing permissions and |
|
|
|
|
# limitations under the License. |
|
|
|
|
|
|
|
|
|
"""Run a group of subprocesses and then finish.""" |
|
|
|
|
|
|
|
|
|
from __future__ import print_function |
|
|
|
@ -28,11 +27,9 @@ import tempfile |
|
|
|
|
import time |
|
|
|
|
import errno |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# cpu cost measurement |
|
|
|
|
measure_cpu_costs = False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count() |
|
|
|
|
_MAX_RESULT_SIZE = 8192 |
|
|
|
|
|
|
|
|
@ -71,34 +68,31 @@ def platform_string(): |
|
|
|
|
if platform_string() == 'windows': |
|
|
|
|
pass |
|
|
|
|
else: |
|
|
|
|
|
|
|
|
|
def alarm_handler(unused_signum, unused_frame): |
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None) |
|
|
|
|
signal.signal(signal.SIGALRM, alarm_handler) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_SUCCESS = object() |
|
|
|
|
_FAILURE = object() |
|
|
|
|
_RUNNING = object() |
|
|
|
|
_KILLED = object() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_COLORS = { |
|
|
|
|
'red': [ 31, 0 ], |
|
|
|
|
'green': [ 32, 0 ], |
|
|
|
|
'yellow': [ 33, 0 ], |
|
|
|
|
'lightgray': [ 37, 0], |
|
|
|
|
'gray': [ 30, 1 ], |
|
|
|
|
'purple': [ 35, 0 ], |
|
|
|
|
'cyan': [ 36, 0 ] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
'red': [31, 0], |
|
|
|
|
'green': [32, 0], |
|
|
|
|
'yellow': [33, 0], |
|
|
|
|
'lightgray': [37, 0], |
|
|
|
|
'gray': [30, 1], |
|
|
|
|
'purple': [35, 0], |
|
|
|
|
'cyan': [36, 0] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_BEGINNING_OF_LINE = '\x1b[0G' |
|
|
|
|
_CLEAR_LINE = '\x1b[2K' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_TAG_COLOR = { |
|
|
|
|
'FAILED': 'red', |
|
|
|
|
'FLAKE': 'purple', |
|
|
|
@ -111,7 +105,7 @@ _TAG_COLOR = { |
|
|
|
|
'SUCCESS': 'green', |
|
|
|
|
'IDLE': 'gray', |
|
|
|
|
'SKIPPED': 'cyan' |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_FORMAT = '%(asctime)-15s %(message)s' |
|
|
|
|
logging.basicConfig(level=logging.INFO, format=_FORMAT) |
|
|
|
@ -127,7 +121,6 @@ def eintr_be_gone(fn): |
|
|
|
|
raise |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def message(tag, msg, explanatory_text=None, do_newline=False): |
|
|
|
|
if message.old_tag == tag and message.old_msg == msg and not explanatory_text: |
|
|
|
|
return |
|
|
|
@ -141,23 +134,22 @@ def message(tag, msg, explanatory_text=None, do_newline=False): |
|
|
|
|
logging.info('%s: %s', tag, msg) |
|
|
|
|
else: |
|
|
|
|
sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % ( |
|
|
|
|
_BEGINNING_OF_LINE, |
|
|
|
|
_CLEAR_LINE, |
|
|
|
|
'\n%s' % explanatory_text if explanatory_text is not None else '', |
|
|
|
|
_COLORS[_TAG_COLOR[tag]][1], |
|
|
|
|
_COLORS[_TAG_COLOR[tag]][0], |
|
|
|
|
tag, |
|
|
|
|
msg, |
|
|
|
|
'\n' if do_newline or explanatory_text is not None else '')) |
|
|
|
|
_BEGINNING_OF_LINE, _CLEAR_LINE, '\n%s' % explanatory_text |
|
|
|
|
if explanatory_text is not None else '', |
|
|
|
|
_COLORS[_TAG_COLOR[tag]][1], _COLORS[_TAG_COLOR[tag]][0], |
|
|
|
|
tag, msg, '\n' |
|
|
|
|
if do_newline or explanatory_text is not None else '')) |
|
|
|
|
sys.stdout.flush() |
|
|
|
|
return |
|
|
|
|
except IOError, e: |
|
|
|
|
if e.errno != errno.EINTR: |
|
|
|
|
raise |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
message.old_tag = '' |
|
|
|
|
message.old_msg = '' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def which(filename): |
|
|
|
|
if '/' in filename: |
|
|
|
|
return filename |
|
|
|
@ -170,9 +162,17 @@ def which(filename): |
|
|
|
|
class JobSpec(object): |
|
|
|
|
"""Specifies what to run for a job.""" |
|
|
|
|
|
|
|
|
|
def __init__(self, cmdline, shortname=None, environ=None, |
|
|
|
|
cwd=None, shell=False, timeout_seconds=5*60, flake_retries=0, |
|
|
|
|
timeout_retries=0, kill_handler=None, cpu_cost=1.0, |
|
|
|
|
def __init__(self, |
|
|
|
|
cmdline, |
|
|
|
|
shortname=None, |
|
|
|
|
environ=None, |
|
|
|
|
cwd=None, |
|
|
|
|
shell=False, |
|
|
|
|
timeout_seconds=5 * 60, |
|
|
|
|
flake_retries=0, |
|
|
|
|
timeout_retries=0, |
|
|
|
|
kill_handler=None, |
|
|
|
|
cpu_cost=1.0, |
|
|
|
|
verbose_success=False): |
|
|
|
|
""" |
|
|
|
|
Arguments: |
|
|
|
@ -205,15 +205,18 @@ class JobSpec(object): |
|
|
|
|
return self.identity() == other.identity() |
|
|
|
|
|
|
|
|
|
def __repr__(self): |
|
|
|
|
return 'JobSpec(shortname=%s, cmdline=%s)' % (self.shortname, self.cmdline) |
|
|
|
|
return 'JobSpec(shortname=%s, cmdline=%s)' % (self.shortname, |
|
|
|
|
self.cmdline) |
|
|
|
|
|
|
|
|
|
def __str__(self): |
|
|
|
|
return '%s: %s %s' % (self.shortname, |
|
|
|
|
' '.join('%s=%s' % kv for kv in self.environ.items()), |
|
|
|
|
' '.join('%s=%s' % kv |
|
|
|
|
for kv in self.environ.items()), |
|
|
|
|
' '.join(self.cmdline)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class JobResult(object): |
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
|
self.state = 'UNKNOWN' |
|
|
|
|
self.returncode = -1 |
|
|
|
@ -233,7 +236,11 @@ def read_from_start(f): |
|
|
|
|
class Job(object): |
|
|
|
|
"""Manages one job.""" |
|
|
|
|
|
|
|
|
|
def __init__(self, spec, newline_on_success, travis, add_env, |
|
|
|
|
def __init__(self, |
|
|
|
|
spec, |
|
|
|
|
newline_on_success, |
|
|
|
|
travis, |
|
|
|
|
add_env, |
|
|
|
|
quiet_success=False): |
|
|
|
|
self._spec = spec |
|
|
|
|
self._newline_on_success = newline_on_success |
|
|
|
@ -278,7 +285,8 @@ class Job(object): |
|
|
|
|
self._process = try_start() |
|
|
|
|
break |
|
|
|
|
except OSError: |
|
|
|
|
message('WARNING', 'Failed to start %s, retrying in %f seconds' % (self._spec.shortname, delay)) |
|
|
|
|
message('WARNING', 'Failed to start %s, retrying in %f seconds' |
|
|
|
|
% (self._spec.shortname, delay)) |
|
|
|
|
time.sleep(delay) |
|
|
|
|
delay *= 2 |
|
|
|
|
else: |
|
|
|
@ -287,18 +295,24 @@ class Job(object): |
|
|
|
|
|
|
|
|
|
def state(self): |
|
|
|
|
"""Poll current state of the job. Prints messages at completion.""" |
|
|
|
|
|
|
|
|
|
def stdout(self=self): |
|
|
|
|
stdout = read_from_start(self._tempfile) |
|
|
|
|
self.result.message = stdout[-_MAX_RESULT_SIZE:] |
|
|
|
|
return stdout |
|
|
|
|
|
|
|
|
|
if self._state == _RUNNING and self._process.poll() is not None: |
|
|
|
|
elapsed = time.time() - self._start |
|
|
|
|
self.result.elapsed_time = elapsed |
|
|
|
|
if self._process.returncode != 0: |
|
|
|
|
if self._retries < self._spec.flake_retries: |
|
|
|
|
message('FLAKE', '%s [ret=%d, pid=%d]' % ( |
|
|
|
|
self._spec.shortname, self._process.returncode, self._process.pid), |
|
|
|
|
stdout(), do_newline=True) |
|
|
|
|
message( |
|
|
|
|
'FLAKE', |
|
|
|
|
'%s [ret=%d, pid=%d]' % |
|
|
|
|
(self._spec.shortname, self._process.returncode, |
|
|
|
|
self._process.pid), |
|
|
|
|
stdout(), |
|
|
|
|
do_newline=True) |
|
|
|
|
self._retries += 1 |
|
|
|
|
self.result.num_failures += 1 |
|
|
|
|
self.result.retries = self._timeout_retries + self._retries |
|
|
|
@ -307,9 +321,13 @@ class Job(object): |
|
|
|
|
else: |
|
|
|
|
self._state = _FAILURE |
|
|
|
|
if not self._suppress_failure_message: |
|
|
|
|
message('FAILED', '%s [ret=%d, pid=%d, time=%.1fsec]' % ( |
|
|
|
|
self._spec.shortname, self._process.returncode, self._process.pid, elapsed), |
|
|
|
|
stdout(), do_newline=True) |
|
|
|
|
message( |
|
|
|
|
'FAILED', |
|
|
|
|
'%s [ret=%d, pid=%d, time=%.1fsec]' % |
|
|
|
|
(self._spec.shortname, self._process.returncode, |
|
|
|
|
self._process.pid, elapsed), |
|
|
|
|
stdout(), |
|
|
|
|
do_newline=True) |
|
|
|
|
self.result.state = 'FAILED' |
|
|
|
|
self.result.num_failures += 1 |
|
|
|
|
self.result.returncode = self._process.returncode |
|
|
|
@ -317,18 +335,25 @@ class Job(object): |
|
|
|
|
self._state = _SUCCESS |
|
|
|
|
measurement = '' |
|
|
|
|
if measure_cpu_costs: |
|
|
|
|
m = re.search(r'real\s+([0-9.]+)\nuser\s+([0-9.]+)\nsys\s+([0-9.]+)', stdout()) |
|
|
|
|
m = re.search( |
|
|
|
|
r'real\s+([0-9.]+)\nuser\s+([0-9.]+)\nsys\s+([0-9.]+)', |
|
|
|
|
stdout()) |
|
|
|
|
real = float(m.group(1)) |
|
|
|
|
user = float(m.group(2)) |
|
|
|
|
sys = float(m.group(3)) |
|
|
|
|
if real > 0.5: |
|
|
|
|
cores = (user + sys) / real |
|
|
|
|
self.result.cpu_measured = float('%.01f' % cores) |
|
|
|
|
self.result.cpu_estimated = float('%.01f' % self._spec.cpu_cost) |
|
|
|
|
measurement = '; cpu_cost=%.01f; estimated=%.01f' % (self.result.cpu_measured, self.result.cpu_estimated) |
|
|
|
|
self.result.cpu_estimated = float('%.01f' % |
|
|
|
|
self._spec.cpu_cost) |
|
|
|
|
measurement = '; cpu_cost=%.01f; estimated=%.01f' % ( |
|
|
|
|
self.result.cpu_measured, self.result.cpu_estimated) |
|
|
|
|
if not self._quiet_success: |
|
|
|
|
message('PASSED', '%s [time=%.1fsec, retries=%d:%d%s]' % ( |
|
|
|
|
self._spec.shortname, elapsed, self._retries, self._timeout_retries, measurement), |
|
|
|
|
message( |
|
|
|
|
'PASSED', |
|
|
|
|
'%s [time=%.1fsec, retries=%d:%d%s]' % |
|
|
|
|
(self._spec.shortname, elapsed, self._retries, |
|
|
|
|
self._timeout_retries, measurement), |
|
|
|
|
stdout() if self._spec.verbose_success else None, |
|
|
|
|
do_newline=self._newline_on_success or self._travis) |
|
|
|
|
self.result.state = 'PASSED' |
|
|
|
@ -338,7 +363,11 @@ class Job(object): |
|
|
|
|
elapsed = time.time() - self._start |
|
|
|
|
self.result.elapsed_time = elapsed |
|
|
|
|
if self._timeout_retries < self._spec.timeout_retries: |
|
|
|
|
message('TIMEOUT_FLAKE', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True) |
|
|
|
|
message( |
|
|
|
|
'TIMEOUT_FLAKE', |
|
|
|
|
'%s [pid=%d]' % (self._spec.shortname, self._process.pid), |
|
|
|
|
stdout(), |
|
|
|
|
do_newline=True) |
|
|
|
|
self._timeout_retries += 1 |
|
|
|
|
self.result.num_failures += 1 |
|
|
|
|
self.result.retries = self._timeout_retries + self._retries |
|
|
|
@ -348,7 +377,12 @@ class Job(object): |
|
|
|
|
# NOTE: job is restarted regardless of jobset's max_time setting |
|
|
|
|
self.start() |
|
|
|
|
else: |
|
|
|
|
message('TIMEOUT', '%s [pid=%d, time=%.1fsec]' % (self._spec.shortname, self._process.pid, elapsed), stdout(), do_newline=True) |
|
|
|
|
message( |
|
|
|
|
'TIMEOUT', |
|
|
|
|
'%s [pid=%d, time=%.1fsec]' % |
|
|
|
|
(self._spec.shortname, self._process.pid, elapsed), |
|
|
|
|
stdout(), |
|
|
|
|
do_newline=True) |
|
|
|
|
self.kill() |
|
|
|
|
self.result.state = 'TIMEOUT' |
|
|
|
|
self.result.num_failures += 1 |
|
|
|
@ -368,8 +402,9 @@ class Job(object): |
|
|
|
|
class Jobset(object): |
|
|
|
|
"""Manages one run of jobs.""" |
|
|
|
|
|
|
|
|
|
def __init__(self, check_cancelled, maxjobs, maxjobs_cpu_agnostic, newline_on_success, travis, |
|
|
|
|
stop_on_failure, add_env, quiet_success, max_time): |
|
|
|
|
def __init__(self, check_cancelled, maxjobs, maxjobs_cpu_agnostic, |
|
|
|
|
newline_on_success, travis, stop_on_failure, add_env, |
|
|
|
|
quiet_success, max_time): |
|
|
|
|
self._running = set() |
|
|
|
|
self._check_cancelled = check_cancelled |
|
|
|
|
self._cancelled = False |
|
|
|
@ -402,7 +437,8 @@ class Jobset(object): |
|
|
|
|
def start(self, spec): |
|
|
|
|
"""Start a job. Return True on success, False on failure.""" |
|
|
|
|
while True: |
|
|
|
|
if self._max_time > 0 and time.time() - self._start_time > self._max_time: |
|
|
|
|
if self._max_time > 0 and time.time( |
|
|
|
|
) - self._start_time > self._max_time: |
|
|
|
|
skipped_job_result = JobResult() |
|
|
|
|
skipped_job_result.state = 'SKIPPED' |
|
|
|
|
message('SKIPPED', spec.shortname, do_newline=True) |
|
|
|
@ -416,10 +452,7 @@ class Jobset(object): |
|
|
|
|
break |
|
|
|
|
self.reap(spec.shortname, spec.cpu_cost) |
|
|
|
|
if self.cancelled(): return False |
|
|
|
|
job = Job(spec, |
|
|
|
|
self._newline_on_success, |
|
|
|
|
self._travis, |
|
|
|
|
self._add_env, |
|
|
|
|
job = Job(spec, self._newline_on_success, self._travis, self._add_env, |
|
|
|
|
self._quiet_success) |
|
|
|
|
self._running.add(job) |
|
|
|
|
if job.GetSpec().shortname not in self.resultset: |
|
|
|
@ -452,14 +485,19 @@ class Jobset(object): |
|
|
|
|
if self._remaining is not None and self._completed > 0: |
|
|
|
|
now = time.time() |
|
|
|
|
sofar = now - self._start_time |
|
|
|
|
remaining = sofar / self._completed * (self._remaining + len(self._running)) |
|
|
|
|
remaining = sofar / self._completed * ( |
|
|
|
|
self._remaining + len(self._running)) |
|
|
|
|
rstr = 'ETA %.1f sec; %s' % (remaining, rstr) |
|
|
|
|
if waiting_for is not None: |
|
|
|
|
wstr = ' next: %s @ %.2f cpu' % (waiting_for, waiting_for_cost) |
|
|
|
|
wstr = ' next: %s @ %.2f cpu' % (waiting_for, |
|
|
|
|
waiting_for_cost) |
|
|
|
|
else: |
|
|
|
|
wstr = '' |
|
|
|
|
message('WAITING', '%s%d jobs running, %d complete, %d failed (load %.2f)%s' % ( |
|
|
|
|
rstr, len(self._running), self._completed, self._failures, self.cpu_cost(), wstr)) |
|
|
|
|
message( |
|
|
|
|
'WAITING', |
|
|
|
|
'%s%d jobs running, %d complete, %d failed (load %.2f)%s' % |
|
|
|
|
(rstr, len(self._running), self._completed, self._failures, |
|
|
|
|
self.cpu_cost(), wstr)) |
|
|
|
|
if platform_string() == 'windows': |
|
|
|
|
time.sleep(0.1) |
|
|
|
|
else: |
|
|
|
@ -519,9 +557,9 @@ def run(cmdlines, |
|
|
|
|
message('SKIPPED', job.shortname, do_newline=True) |
|
|
|
|
resultset[job.shortname] = [skipped_job_result] |
|
|
|
|
return 0, resultset |
|
|
|
|
js = Jobset(check_cancelled, |
|
|
|
|
maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
|
|
|
|
maxjobs_cpu_agnostic if maxjobs_cpu_agnostic is not None else _DEFAULT_MAX_JOBS, |
|
|
|
|
js = Jobset(check_cancelled, maxjobs if maxjobs is not None else |
|
|
|
|
_DEFAULT_MAX_JOBS, maxjobs_cpu_agnostic |
|
|
|
|
if maxjobs_cpu_agnostic is not None else _DEFAULT_MAX_JOBS, |
|
|
|
|
newline_on_success, travis, stop_on_failure, add_env, |
|
|
|
|
quiet_success, max_time) |
|
|
|
|
for cmdline, remaining in tag_remaining(cmdlines): |
|
|
|
|