2014-05-14 13:30:57 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2015-04-08 09:53:35 +00:00
|
|
|
from Queue import Empty
|
2018-01-31 10:21:44 +00:00
|
|
|
from contextlib import contextmanager
|
2018-02-20 17:23:37 +00:00
|
|
|
from multiprocessing import Process, Queue
|
|
|
|
import os
|
2018-01-31 10:21:44 +00:00
|
|
|
import signal
|
2018-02-20 17:23:37 +00:00
|
|
|
import time
|
2015-11-27 12:51:43 +00:00
|
|
|
import traceback
|
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
from . import command
|
|
|
|
|
2014-05-14 13:30:57 +00:00
|
|
|
|
2017-12-30 13:37:49 +00:00
|
|
|
def setup_testing():
|
|
|
|
"""For testing only: Use threading under the hood instead of multiprocessing
|
|
|
|
to make coverage work.
|
|
|
|
"""
|
|
|
|
global Queue
|
|
|
|
global Process
|
|
|
|
del Queue
|
|
|
|
del Process
|
|
|
|
from Queue import Queue
|
|
|
|
from threading import Thread as Process
|
2018-02-20 17:23:37 +00:00
|
|
|
# Monkeypatch threading Queue to look like multiprocessing Queue.
|
|
|
|
Queue.cancel_join_thread = lambda self: None
|
2018-10-02 13:31:32 +00:00
|
|
|
# Monkeypatch os.kill and add fake pid property on Thread.
|
|
|
|
os.kill = lambda *args: None
|
|
|
|
Process.pid = property(lambda self: None)
|
2017-12-30 13:37:49 +00:00
|
|
|
|
|
|
|
|
2014-05-14 13:30:57 +00:00
|
|
|
class NormalResult():
|
|
|
|
def __init__(self, result):
|
|
|
|
self.result = result
|
2018-01-31 11:04:33 +00:00
|
|
|
self.exception = None
|
2014-05-14 13:30:57 +00:00
|
|
|
|
|
|
|
class ExceptionResult():
|
2018-01-31 11:04:33 +00:00
|
|
|
def __init__(self, exception):
|
|
|
|
self.exception = exception
|
2014-05-14 13:30:57 +00:00
|
|
|
|
|
|
|
|
2015-04-08 09:53:35 +00:00
|
|
|
class MaybeResult():
|
|
|
|
def __init__(self, heartbeat, value):
|
|
|
|
self.heartbeat = heartbeat
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_heartbeat():
|
|
|
|
return MaybeResult(True, None)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_result(value):
|
|
|
|
return MaybeResult(False, value)
|
|
|
|
|
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
def Worker(fn, work_queue, done_queue,
|
2015-11-27 12:51:43 +00:00
|
|
|
process_context_fn=None, process_context_args=None):
|
2014-05-14 13:30:57 +00:00
|
|
|
"""Worker to be run in a child process.
|
2018-01-31 10:21:44 +00:00
|
|
|
The worker stops when the poison pill "STOP" is reached.
|
|
|
|
"""
|
2014-05-14 13:30:57 +00:00
|
|
|
try:
|
2015-11-27 12:51:43 +00:00
|
|
|
kwargs = {}
|
|
|
|
if process_context_fn and process_context_args is not None:
|
|
|
|
kwargs.update(process_context=process_context_fn(*process_context_args))
|
2014-05-14 13:30:57 +00:00
|
|
|
for args in iter(work_queue.get, "STOP"):
|
|
|
|
try:
|
2015-11-27 12:51:43 +00:00
|
|
|
done_queue.put(NormalResult(fn(*args, **kwargs)))
|
2018-02-20 17:23:37 +00:00
|
|
|
except command.AbortException:
|
|
|
|
# SIGINT, SIGTERM or internal hard timeout.
|
|
|
|
break
|
2014-05-14 13:30:57 +00:00
|
|
|
except Exception, e:
|
2015-11-27 12:51:43 +00:00
|
|
|
traceback.print_exc()
|
2014-05-14 13:30:57 +00:00
|
|
|
print(">>> EXCEPTION: %s" % e)
|
2018-01-31 11:04:33 +00:00
|
|
|
done_queue.put(ExceptionResult(e))
|
2018-02-20 17:23:37 +00:00
|
|
|
# When we reach here on normal tear down, all items have been pulled from
|
|
|
|
# the done_queue before and this should have no effect. On fast abort, it's
|
|
|
|
# possible that a fast worker left items on the done_queue in memory, which
|
|
|
|
# will never be pulled. This call purges those to avoid a deadlock.
|
|
|
|
done_queue.cancel_join_thread()
|
2014-05-14 13:30:57 +00:00
|
|
|
except KeyboardInterrupt:
|
2018-01-31 10:21:44 +00:00
|
|
|
assert False, 'Unreachable'
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2018-02-20 17:23:37 +00:00
|
|
|
def without_sig():
|
|
|
|
int_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
|
|
term_handler = signal.signal(signal.SIGTERM, signal.SIG_IGN)
|
|
|
|
try:
|
2018-01-31 10:21:44 +00:00
|
|
|
yield
|
2018-02-20 17:23:37 +00:00
|
|
|
finally:
|
|
|
|
signal.signal(signal.SIGINT, int_handler)
|
|
|
|
signal.signal(signal.SIGTERM, term_handler)
|
2014-05-14 13:30:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Pool():
|
|
|
|
"""Distributes tasks to a number of worker processes.
|
|
|
|
New tasks can be added dynamically even after the workers have been started.
|
|
|
|
Requirement: Tasks can only be added from the parent process, e.g. while
|
|
|
|
consuming the results generator."""
|
|
|
|
|
|
|
|
# Factor to calculate the maximum number of items in the work/done queue.
|
|
|
|
# Necessary to not overflow the queue's pipe if a keyboard interrupt happens.
|
|
|
|
BUFFER_FACTOR = 4
|
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
def __init__(self, num_workers, heartbeat_timeout=1):
|
2014-05-14 13:30:57 +00:00
|
|
|
self.num_workers = num_workers
|
|
|
|
self.processes = []
|
|
|
|
self.terminated = False
|
2018-02-20 17:23:37 +00:00
|
|
|
self.abort_now = False
|
2014-05-14 13:30:57 +00:00
|
|
|
|
2018-01-31 10:21:44 +00:00
|
|
|
# Invariant: processing_count >= #work_queue + #done_queue. It is greater
|
|
|
|
# when a worker takes an item from the work_queue and before the result is
|
2014-05-14 13:30:57 +00:00
|
|
|
# submitted to the done_queue. It is equal when no worker is working,
|
|
|
|
# e.g. when all workers have finished, and when no results are processed.
|
|
|
|
# Count is only accessed by the parent process. Only the parent process is
|
|
|
|
# allowed to remove items from the done_queue and to add items to the
|
|
|
|
# work_queue.
|
2018-01-31 10:21:44 +00:00
|
|
|
self.processing_count = 0
|
2015-04-08 09:53:35 +00:00
|
|
|
self.heartbeat_timeout = heartbeat_timeout
|
2014-05-14 13:30:57 +00:00
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
# Disable sigint and sigterm to prevent subprocesses from capturing the
|
|
|
|
# signals.
|
|
|
|
with without_sig():
|
2018-01-31 10:21:44 +00:00
|
|
|
self.work_queue = Queue()
|
|
|
|
self.done_queue = Queue()
|
|
|
|
|
2015-11-27 12:51:43 +00:00
|
|
|
def imap_unordered(self, fn, gen,
|
|
|
|
process_context_fn=None, process_context_args=None):
|
2014-05-14 13:30:57 +00:00
|
|
|
"""Maps function "fn" to items in generator "gen" on the worker processes
|
|
|
|
in an arbitrary order. The items are expected to be lists of arguments to
|
2015-04-08 09:53:35 +00:00
|
|
|
the function. Returns a results iterator. A result value of type
|
|
|
|
MaybeResult either indicates a heartbeat of the runner, i.e. indicating
|
|
|
|
that the runner is still waiting for the result to be computed, or it wraps
|
2015-11-27 12:51:43 +00:00
|
|
|
the real result.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
process_context_fn: Function executed once by each worker. Expected to
|
|
|
|
return a process-context object. If present, this object is passed
|
|
|
|
as additional argument to each call to fn.
|
|
|
|
process_context_args: List of arguments for the invocation of
|
|
|
|
process_context_fn. All arguments will be pickled and sent beyond the
|
|
|
|
process boundary.
|
|
|
|
"""
|
2018-01-31 11:04:33 +00:00
|
|
|
if self.terminated:
|
|
|
|
return
|
2014-05-14 13:30:57 +00:00
|
|
|
try:
|
2016-02-02 13:58:32 +00:00
|
|
|
internal_error = False
|
2014-05-14 13:30:57 +00:00
|
|
|
gen = iter(gen)
|
|
|
|
self.advance = self._advance_more
|
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
# Disable sigint and sigterm to prevent subprocesses from capturing the
|
|
|
|
# signals.
|
|
|
|
with without_sig():
|
2018-01-31 10:21:44 +00:00
|
|
|
for w in xrange(self.num_workers):
|
|
|
|
p = Process(target=Worker, args=(fn,
|
|
|
|
self.work_queue,
|
|
|
|
self.done_queue,
|
|
|
|
process_context_fn,
|
|
|
|
process_context_args))
|
|
|
|
p.start()
|
|
|
|
self.processes.append(p)
|
2014-05-14 13:30:57 +00:00
|
|
|
|
|
|
|
self.advance(gen)
|
2018-01-31 10:21:44 +00:00
|
|
|
while self.processing_count > 0:
|
2015-04-08 09:53:35 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2018-02-20 17:23:37 +00:00
|
|
|
# Read from result queue in a responsive fashion. If available,
|
|
|
|
# this will return a normal result immediately or a heartbeat on
|
|
|
|
# heartbeat timeout (default 1 second).
|
2018-01-31 11:04:33 +00:00
|
|
|
result = self._get_result_from_queue()
|
|
|
|
except:
|
|
|
|
# TODO(machenbach): Handle a few known types of internal errors
|
|
|
|
# gracefully, e.g. missing test files.
|
|
|
|
internal_error = True
|
|
|
|
continue
|
2018-02-20 17:23:37 +00:00
|
|
|
|
|
|
|
if self.abort_now:
|
|
|
|
# SIGINT, SIGTERM or internal hard timeout.
|
|
|
|
return
|
|
|
|
|
2018-01-31 11:04:33 +00:00
|
|
|
yield result
|
|
|
|
break
|
|
|
|
|
2014-05-14 13:30:57 +00:00
|
|
|
self.advance(gen)
|
2017-11-30 12:57:45 +00:00
|
|
|
except KeyboardInterrupt:
|
2018-02-20 17:23:37 +00:00
|
|
|
assert False, 'Unreachable'
|
2017-11-30 12:57:45 +00:00
|
|
|
except Exception as e:
|
|
|
|
traceback.print_exc()
|
|
|
|
print(">>> EXCEPTION: %s" % e)
|
2014-05-14 13:30:57 +00:00
|
|
|
finally:
|
2018-02-20 17:23:37 +00:00
|
|
|
self._terminate()
|
2018-01-31 10:21:44 +00:00
|
|
|
|
2016-02-02 13:58:32 +00:00
|
|
|
if internal_error:
|
|
|
|
raise Exception("Internal error in a worker process.")
|
2014-05-14 13:30:57 +00:00
|
|
|
|
|
|
|
def _advance_more(self, gen):
|
2018-01-31 10:21:44 +00:00
|
|
|
while self.processing_count < self.num_workers * self.BUFFER_FACTOR:
|
2014-05-14 13:30:57 +00:00
|
|
|
try:
|
|
|
|
self.work_queue.put(gen.next())
|
2018-01-31 10:21:44 +00:00
|
|
|
self.processing_count += 1
|
2014-05-14 13:30:57 +00:00
|
|
|
except StopIteration:
|
|
|
|
self.advance = self._advance_empty
|
|
|
|
break
|
|
|
|
|
|
|
|
def _advance_empty(self, gen):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def add(self, args):
|
|
|
|
"""Adds an item to the work queue. Can be called dynamically while
|
|
|
|
processing the results from imap_unordered."""
|
2018-01-31 11:04:33 +00:00
|
|
|
assert not self.terminated
|
|
|
|
|
2014-05-14 13:30:57 +00:00
|
|
|
self.work_queue.put(args)
|
2018-01-31 10:21:44 +00:00
|
|
|
self.processing_count += 1
|
2014-05-14 13:30:57 +00:00
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
def abort(self):
|
|
|
|
"""Schedules abort on next queue read.
|
2018-01-31 11:04:33 +00:00
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
This is safe to call when handling SIGINT, SIGTERM or when an internal
|
|
|
|
hard timeout is reached.
|
|
|
|
"""
|
|
|
|
self.abort_now = True
|
|
|
|
|
|
|
|
def _terminate(self):
|
|
|
|
"""Terminates execution and cleans up the queues.
|
|
|
|
|
|
|
|
If abort() was called before termination, this also terminates the
|
|
|
|
subprocesses and doesn't wait for ongoing tests.
|
2018-01-31 11:04:33 +00:00
|
|
|
"""
|
2014-05-14 13:30:57 +00:00
|
|
|
if self.terminated:
|
|
|
|
return
|
|
|
|
self.terminated = True
|
|
|
|
|
2018-01-31 10:21:44 +00:00
|
|
|
# Drain out work queue from tests
|
|
|
|
try:
|
2018-02-20 17:23:37 +00:00
|
|
|
while True:
|
|
|
|
self.work_queue.get(True, 0.1)
|
2018-01-31 10:21:44 +00:00
|
|
|
except Empty:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Make sure all processes stop
|
2018-01-31 11:04:33 +00:00
|
|
|
for _ in self.processes:
|
2014-05-14 13:30:57 +00:00
|
|
|
# During normal tear down the workers block on get(). Feed a poison pill
|
|
|
|
# per worker to make them stop.
|
|
|
|
self.work_queue.put("STOP")
|
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
if self.abort_now:
|
|
|
|
for p in self.processes:
|
|
|
|
os.kill(p.pid, signal.SIGTERM)
|
2018-01-31 10:21:44 +00:00
|
|
|
|
2014-05-14 13:30:57 +00:00
|
|
|
for p in self.processes:
|
|
|
|
p.join()
|
2014-05-15 15:01:27 +00:00
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
# Drain the queues to prevent stderr chatter when queues are garbage
|
|
|
|
# collected.
|
2018-01-31 11:04:33 +00:00
|
|
|
try:
|
2018-02-20 17:23:37 +00:00
|
|
|
while True: self.work_queue.get(False)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
while True: self.done_queue.get(False)
|
|
|
|
except:
|
|
|
|
pass
|
2018-01-31 11:04:33 +00:00
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
def _get_result_from_queue(self):
|
|
|
|
"""Attempts to get the next result from the queue.
|
2018-01-31 11:04:33 +00:00
|
|
|
|
2018-02-20 17:23:37 +00:00
|
|
|
Returns: A wrapped result if one was available within heartbeat timeout,
|
|
|
|
a heartbeat result otherwise.
|
|
|
|
Raises:
|
|
|
|
Exception: If an exception occured when processing the task on the
|
|
|
|
worker side, it is reraised here.
|
|
|
|
"""
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
result = self.done_queue.get(timeout=self.heartbeat_timeout)
|
|
|
|
self.processing_count -= 1
|
|
|
|
if result.exception:
|
|
|
|
raise result.exception
|
|
|
|
return MaybeResult.create_result(result.result)
|
|
|
|
except Empty:
|
|
|
|
return MaybeResult.create_heartbeat()
|