2019-04-25 23:08:53 -03:00
|
|
|
import collections
|
2016-03-22 22:04:32 -03:00
|
|
|
import faulthandler
|
2015-09-29 18:15:38 -03:00
|
|
|
import json
|
|
|
|
import os
|
2015-09-29 22:05:43 -03:00
|
|
|
import queue
|
2019-04-26 03:40:25 -03:00
|
|
|
import subprocess
|
2015-09-29 18:15:38 -03:00
|
|
|
import sys
|
2017-09-07 13:56:24 -03:00
|
|
|
import threading
|
2015-09-29 19:33:29 -03:00
|
|
|
import time
|
2019-04-26 03:40:25 -03:00
|
|
|
import traceback
|
2015-09-29 20:32:39 -03:00
|
|
|
import types
|
2015-09-29 18:15:38 -03:00
|
|
|
from test import support
|
|
|
|
|
2016-03-23 08:14:10 -03:00
|
|
|
from test.libregrtest.runtest import (
|
2016-05-20 08:37:40 -03:00
|
|
|
runtest, INTERRUPTED, CHILD_ERROR, PROGRESS_MIN_TIME,
|
2019-05-13 14:17:54 -03:00
|
|
|
format_test_result, TestResult, is_failed)
|
2015-09-29 21:17:28 -03:00
|
|
|
from test.libregrtest.setup import setup_tests
|
2018-06-01 06:04:45 -03:00
|
|
|
from test.libregrtest.utils import format_duration
|
2015-09-29 18:15:38 -03:00
|
|
|
|
|
|
|
|
2015-09-29 22:05:43 -03:00
|
|
|
# Display the running tests if nothing happened last N seconds
|
2015-11-04 04:03:53 -04:00
|
|
|
PROGRESS_UPDATE = 30.0 # seconds
|
2015-09-29 22:05:43 -03:00
|
|
|
|
2019-05-13 22:47:32 -03:00
|
|
|
# Time to wait until a worker completes: should be immediate
|
|
|
|
JOIN_TIMEOUT = 30.0 # seconds
|
|
|
|
|
2016-03-24 08:04:15 -03:00
|
|
|
|
2019-05-13 14:17:54 -03:00
|
|
|
def must_stop(result, ns):
|
|
|
|
if result.result == INTERRUPTED:
|
|
|
|
return True
|
|
|
|
if ns.failfast and is_failed(result, ns):
|
|
|
|
return True
|
|
|
|
return False
|
2015-09-29 19:33:29 -03:00
|
|
|
|
|
|
|
|
2019-04-26 03:40:25 -03:00
|
|
|
def run_test_in_subprocess(testname, ns):
|
2015-09-29 20:32:39 -03:00
|
|
|
ns_dict = vars(ns)
|
2018-09-07 12:20:42 -03:00
|
|
|
worker_args = (ns_dict, testname)
|
|
|
|
worker_args = json.dumps(worker_args)
|
2015-09-29 19:33:29 -03:00
|
|
|
|
|
|
|
cmd = [sys.executable, *support.args_from_interpreter_flags(),
|
2016-09-21 12:12:50 -03:00
|
|
|
'-u', # Unbuffered stdout and stderr
|
2015-09-29 19:33:29 -03:00
|
|
|
'-m', 'test.regrtest',
|
2018-09-07 12:20:42 -03:00
|
|
|
'--worker-args', worker_args]
|
2015-10-02 20:20:49 -03:00
|
|
|
if ns.pgo:
|
|
|
|
cmd += ['--pgo']
|
2015-09-29 19:33:29 -03:00
|
|
|
|
2015-09-29 18:15:38 -03:00
|
|
|
# Running the child from the same working directory as regrtest's original
|
|
|
|
# invocation ensures that TEMPDIR for the child is the same when
|
|
|
|
# sysconfig.is_python_build() is true. See issue 15300.
|
2019-04-26 03:40:25 -03:00
|
|
|
return subprocess.Popen(cmd,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
universal_newlines=True,
|
|
|
|
close_fds=(os.name != 'nt'),
|
|
|
|
cwd=support.SAVEDCWD)
|
2015-09-29 18:15:38 -03:00
|
|
|
|
|
|
|
|
2018-09-07 12:20:42 -03:00
|
|
|
def run_tests_worker(worker_args):
|
|
|
|
ns_dict, testname = json.loads(worker_args)
|
2015-09-29 20:32:39 -03:00
|
|
|
ns = types.SimpleNamespace(**ns_dict)
|
|
|
|
|
2015-09-29 21:17:28 -03:00
|
|
|
setup_tests(ns)
|
2015-09-29 20:32:39 -03:00
|
|
|
|
2019-04-25 23:08:53 -03:00
|
|
|
result = runtest(ns, testname)
|
2015-09-29 18:15:38 -03:00
|
|
|
print() # Force a newline (just in case)
|
2015-09-29 19:48:27 -03:00
|
|
|
print(json.dumps(result), flush=True)
|
2015-09-29 18:15:38 -03:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
# We do not use a generator so multiple threads can call next().
|
|
|
|
class MultiprocessIterator:
|
|
|
|
|
|
|
|
"""A thread-safe iterator over tests for multiprocess mode."""
|
|
|
|
|
2019-05-13 14:17:54 -03:00
|
|
|
def __init__(self, tests_iter):
|
2015-09-29 18:15:38 -03:00
|
|
|
self.lock = threading.Lock()
|
2019-05-13 14:17:54 -03:00
|
|
|
self.tests_iter = tests_iter
|
2015-09-29 18:15:38 -03:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
with self.lock:
|
2019-05-13 14:17:54 -03:00
|
|
|
if self.tests_iter is None:
|
|
|
|
raise StopIteration
|
|
|
|
return next(self.tests_iter)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
with self.lock:
|
|
|
|
self.tests_iter = None
|
2015-09-29 18:15:38 -03:00
|
|
|
|
|
|
|
|
2019-04-25 23:08:53 -03:00
|
|
|
MultiprocessResult = collections.namedtuple('MultiprocessResult',
|
|
|
|
'result stdout stderr error_msg')
|
|
|
|
|
2019-05-13 22:47:32 -03:00
|
|
|
class ExitThread(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-09-29 18:15:38 -03:00
|
|
|
class MultiprocessThread(threading.Thread):
|
|
|
|
def __init__(self, pending, output, ns):
|
|
|
|
super().__init__()
|
|
|
|
self.pending = pending
|
|
|
|
self.output = output
|
|
|
|
self.ns = ns
|
2019-04-25 23:08:53 -03:00
|
|
|
self.current_test_name = None
|
2015-09-29 19:33:29 -03:00
|
|
|
self.start_time = None
|
2019-04-26 03:40:25 -03:00
|
|
|
self._popen = None
|
2019-05-13 22:47:32 -03:00
|
|
|
self._killed = False
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
info = ['MultiprocessThread']
|
|
|
|
test = self.current_test_name
|
|
|
|
if self.is_alive():
|
|
|
|
info.append('alive')
|
|
|
|
if test:
|
|
|
|
info.append(f'test={test}')
|
|
|
|
popen = self._popen
|
|
|
|
if popen:
|
|
|
|
info.append(f'pid={popen.pid}')
|
|
|
|
return '<%s>' % ' '.join(info)
|
2015-09-29 19:33:29 -03:00
|
|
|
|
2019-04-26 03:40:25 -03:00
|
|
|
def kill(self):
|
2019-05-13 22:47:32 -03:00
|
|
|
self._killed = True
|
|
|
|
|
2019-05-13 14:17:54 -03:00
|
|
|
popen = self._popen
|
|
|
|
if popen is None:
|
2019-04-26 03:40:25 -03:00
|
|
|
return
|
2019-05-13 14:17:54 -03:00
|
|
|
popen.kill()
|
2019-05-13 22:47:32 -03:00
|
|
|
# stdout and stderr must be closed to ensure that communicate()
|
|
|
|
# does not hang
|
|
|
|
popen.stdout.close()
|
|
|
|
popen.stderr.close()
|
2015-09-29 19:33:29 -03:00
|
|
|
|
2019-04-26 03:40:25 -03:00
|
|
|
def _runtest(self, test_name):
|
2015-09-29 19:33:29 -03:00
|
|
|
try:
|
|
|
|
self.start_time = time.monotonic()
|
2019-04-25 23:08:53 -03:00
|
|
|
self.current_test_name = test_name
|
2015-09-29 19:33:29 -03:00
|
|
|
|
2019-05-13 14:17:54 -03:00
|
|
|
self._popen = run_test_in_subprocess(test_name, self.ns)
|
|
|
|
popen = self._popen
|
2019-04-26 03:40:25 -03:00
|
|
|
with popen:
|
|
|
|
try:
|
2019-05-13 22:47:32 -03:00
|
|
|
if self._killed:
|
|
|
|
# If kill() has been called before self._popen is set,
|
|
|
|
# self._popen is still running. Call again kill()
|
|
|
|
# to ensure that the process is killed.
|
|
|
|
self.kill()
|
|
|
|
raise ExitThread
|
|
|
|
|
|
|
|
try:
|
|
|
|
stdout, stderr = popen.communicate()
|
|
|
|
except OSError:
|
|
|
|
if self._killed:
|
|
|
|
# kill() has been called: communicate() fails
|
|
|
|
# on reading closed stdout/stderr
|
|
|
|
raise ExitThread
|
|
|
|
raise
|
2019-04-26 03:40:25 -03:00
|
|
|
except:
|
2019-05-13 14:17:54 -03:00
|
|
|
self.kill()
|
2019-04-26 03:40:25 -03:00
|
|
|
popen.wait()
|
|
|
|
raise
|
|
|
|
|
|
|
|
retcode = popen.wait()
|
2015-09-29 19:33:29 -03:00
|
|
|
finally:
|
2019-04-25 23:08:53 -03:00
|
|
|
self.current_test_name = None
|
2019-04-26 03:40:25 -03:00
|
|
|
self._popen = None
|
|
|
|
|
|
|
|
stdout = stdout.strip()
|
|
|
|
stderr = stderr.rstrip()
|
2015-09-29 19:33:29 -03:00
|
|
|
|
2019-04-26 03:40:25 -03:00
|
|
|
err_msg = None
|
2015-09-29 19:33:29 -03:00
|
|
|
if retcode != 0:
|
2019-04-26 03:40:25 -03:00
|
|
|
err_msg = "Exit code %s" % retcode
|
|
|
|
else:
|
|
|
|
stdout, _, result = stdout.rpartition("\n")
|
|
|
|
stdout = stdout.rstrip()
|
|
|
|
if not result:
|
|
|
|
err_msg = "Failed to parse worker stdout"
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
# deserialize run_tests_worker() output
|
|
|
|
result = json.loads(result)
|
|
|
|
result = TestResult(*result)
|
|
|
|
except Exception as exc:
|
|
|
|
err_msg = "Failed to parse worker JSON: %s" % exc
|
|
|
|
|
|
|
|
if err_msg is not None:
|
2019-04-25 23:08:53 -03:00
|
|
|
test_time = time.monotonic() - self.start_time
|
|
|
|
result = TestResult(test_name, CHILD_ERROR, test_time, None)
|
2015-09-29 19:33:29 -03:00
|
|
|
|
2019-04-26 03:40:25 -03:00
|
|
|
return MultiprocessResult(result, stdout, stderr, err_msg)
|
2015-09-29 18:15:38 -03:00
|
|
|
|
|
|
|
def run(self):
|
2019-05-13 22:47:32 -03:00
|
|
|
while not self._killed:
|
2019-04-26 03:40:25 -03:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
test_name = next(self.pending)
|
|
|
|
except StopIteration:
|
|
|
|
break
|
2015-09-29 18:15:38 -03:00
|
|
|
|
2019-04-26 03:40:25 -03:00
|
|
|
mp_result = self._runtest(test_name)
|
|
|
|
self.output.put((False, mp_result))
|
2015-09-29 18:15:38 -03:00
|
|
|
|
2019-05-13 14:17:54 -03:00
|
|
|
if must_stop(mp_result.result, self.ns):
|
2019-04-26 03:40:25 -03:00
|
|
|
break
|
2019-05-13 22:47:32 -03:00
|
|
|
except ExitThread:
|
|
|
|
break
|
2019-04-26 03:40:25 -03:00
|
|
|
except BaseException:
|
|
|
|
self.output.put((True, traceback.format_exc()))
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
def get_running(workers):
|
|
|
|
running = []
|
2015-09-29 18:15:38 -03:00
|
|
|
for worker in workers:
|
2019-04-26 03:40:25 -03:00
|
|
|
current_test_name = worker.current_test_name
|
|
|
|
if not current_test_name:
|
|
|
|
continue
|
|
|
|
dt = time.monotonic() - worker.start_time
|
|
|
|
if dt >= PROGRESS_MIN_TIME:
|
|
|
|
text = '%s (%s)' % (current_test_name, format_duration(dt))
|
|
|
|
running.append(text)
|
|
|
|
return running
|
|
|
|
|
|
|
|
|
|
|
|
class MultiprocessRunner:
|
|
|
|
def __init__(self, regrtest):
|
|
|
|
self.regrtest = regrtest
|
|
|
|
self.ns = regrtest.ns
|
|
|
|
self.output = queue.Queue()
|
|
|
|
self.pending = MultiprocessIterator(self.regrtest.tests)
|
|
|
|
if self.ns.timeout is not None:
|
|
|
|
self.test_timeout = self.ns.timeout * 1.5
|
|
|
|
else:
|
|
|
|
self.test_timeout = None
|
|
|
|
self.workers = None
|
|
|
|
|
|
|
|
def start_workers(self):
|
|
|
|
self.workers = [MultiprocessThread(self.pending, self.output, self.ns)
|
|
|
|
for _ in range(self.ns.use_mp)]
|
|
|
|
print("Run tests in parallel using %s child processes"
|
|
|
|
% len(self.workers))
|
|
|
|
for worker in self.workers:
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
def wait_workers(self):
|
2019-05-13 22:47:32 -03:00
|
|
|
start_time = time.monotonic()
|
2019-04-26 03:40:25 -03:00
|
|
|
for worker in self.workers:
|
|
|
|
worker.kill()
|
|
|
|
for worker in self.workers:
|
2019-05-13 22:47:32 -03:00
|
|
|
while True:
|
|
|
|
worker.join(1.0)
|
|
|
|
if not worker.is_alive():
|
|
|
|
break
|
|
|
|
dt = time.monotonic() - start_time
|
|
|
|
print("Wait for regrtest worker %r for %.1f sec" % (worker, dt))
|
|
|
|
if dt > JOIN_TIMEOUT:
|
|
|
|
print("Warning -- failed to join a regrtest worker %s"
|
|
|
|
% worker)
|
|
|
|
break
|
2019-04-26 03:40:25 -03:00
|
|
|
|
|
|
|
def _get_result(self):
|
|
|
|
if not any(worker.is_alive() for worker in self.workers):
|
|
|
|
# all worker threads are done: consume pending results
|
|
|
|
try:
|
|
|
|
return self.output.get(timeout=0)
|
|
|
|
except queue.Empty:
|
|
|
|
return None
|
|
|
|
|
|
|
|
while True:
|
|
|
|
if self.test_timeout is not None:
|
|
|
|
faulthandler.dump_traceback_later(self.test_timeout, exit=True)
|
2016-03-22 22:04:32 -03:00
|
|
|
|
2019-04-26 03:40:25 -03:00
|
|
|
# wait for a thread
|
|
|
|
timeout = max(PROGRESS_UPDATE, PROGRESS_MIN_TIME)
|
2015-09-29 22:05:43 -03:00
|
|
|
try:
|
2019-04-26 03:40:25 -03:00
|
|
|
return self.output.get(timeout=timeout)
|
2015-09-29 22:05:43 -03:00
|
|
|
except queue.Empty:
|
2019-04-26 03:40:25 -03:00
|
|
|
pass
|
|
|
|
|
|
|
|
# display progress
|
|
|
|
running = get_running(self.workers)
|
|
|
|
if running and not self.ns.pgo:
|
|
|
|
print('running: %s' % ', '.join(running), flush=True)
|
|
|
|
|
|
|
|
def display_result(self, mp_result):
|
|
|
|
result = mp_result.result
|
|
|
|
|
|
|
|
text = format_test_result(result)
|
|
|
|
if mp_result.error_msg is not None:
|
|
|
|
# CHILD_ERROR
|
|
|
|
text += ' (%s)' % mp_result.error_msg
|
|
|
|
elif (result.test_time >= PROGRESS_MIN_TIME and not self.ns.pgo):
|
|
|
|
text += ' (%s)' % format_duration(result.test_time)
|
|
|
|
running = get_running(self.workers)
|
|
|
|
if running and not self.ns.pgo:
|
|
|
|
text += ' -- running: %s' % ', '.join(running)
|
|
|
|
self.regrtest.display_progress(self.test_index, text)
|
|
|
|
|
|
|
|
def _process_result(self, item):
|
|
|
|
if item[0]:
|
|
|
|
# Thread got an exception
|
|
|
|
format_exc = item[1]
|
|
|
|
print(f"regrtest worker thread failed: {format_exc}",
|
|
|
|
file=sys.stderr, flush=True)
|
|
|
|
return True
|
|
|
|
|
|
|
|
self.test_index += 1
|
|
|
|
mp_result = item[1]
|
|
|
|
self.regrtest.accumulate_result(mp_result.result)
|
|
|
|
self.display_result(mp_result)
|
|
|
|
|
|
|
|
if mp_result.stdout:
|
|
|
|
print(mp_result.stdout, flush=True)
|
|
|
|
if mp_result.stderr and not self.ns.pgo:
|
|
|
|
print(mp_result.stderr, file=sys.stderr, flush=True)
|
|
|
|
|
2019-05-13 14:17:54 -03:00
|
|
|
if must_stop(mp_result.result, self.ns):
|
2019-04-26 03:40:25 -03:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def run_tests(self):
|
|
|
|
self.start_workers()
|
|
|
|
|
|
|
|
self.test_index = 0
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
item = self._get_result()
|
|
|
|
if item is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
stop = self._process_result(item)
|
|
|
|
if stop:
|
|
|
|
break
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
|
|
|
self.regrtest.interrupted = True
|
|
|
|
finally:
|
|
|
|
if self.test_timeout is not None:
|
|
|
|
faulthandler.cancel_dump_traceback_later()
|
|
|
|
|
2019-05-13 14:17:54 -03:00
|
|
|
# a test failed (and --failfast is set) or all tests completed
|
|
|
|
self.pending.stop()
|
2019-04-26 03:40:25 -03:00
|
|
|
self.wait_workers()
|
|
|
|
|
|
|
|
|
|
|
|
def run_tests_multiprocess(regrtest):
|
|
|
|
MultiprocessRunner(regrtest).run_tests()
|