/deps/v8/tools/testrunner/testproc/execution.py

https://github.com/isaacs/node · Python · 95 lines · 61 code · 25 blank · 9 comment · 6 complexity · 1b2a9fb4d6e814ff2b5a359be1079ee8 MD5 · raw file

  1. # Copyright 2018 the V8 project authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import collections
  5. import traceback
  6. from . import base
  7. from ..local import pool
  8. # Global function for multiprocessing, because pickling a static method doesn't
  9. # work on Windows.
  10. def run_job(job, process_context):
  11. return job.run(process_context)
  12. def create_process_context(result_reduction):
  13. return ProcessContext(result_reduction)
  14. JobResult = collections.namedtuple('JobResult', ['id', 'result'])
  15. ProcessContext = collections.namedtuple('ProcessContext', ['result_reduction'])
  16. class Job(object):
  17. def __init__(self, test_id, cmd, outproc, keep_output):
  18. self.test_id = test_id
  19. self.cmd = cmd
  20. self.outproc = outproc
  21. self.keep_output = keep_output
  22. def run(self, process_ctx):
  23. output = self.cmd.execute()
  24. reduction = process_ctx.result_reduction if not self.keep_output else None
  25. result = self.outproc.process(output, reduction)
  26. return JobResult(self.test_id, result)
  27. class ExecutionProc(base.TestProc):
  28. """Last processor in the chain. Instead of passing tests further it creates
  29. commands and output processors, executes them in multiple worker processes and
  30. sends results to the previous processor.
  31. """
  32. def __init__(self, jobs, outproc_factory=None):
  33. super(ExecutionProc, self).__init__()
  34. self._pool = pool.Pool(jobs, notify_fun=self.notify_previous)
  35. self._outproc_factory = outproc_factory or (lambda t: t.output_proc)
  36. self._tests = {}
  37. def connect_to(self, next_proc):
  38. assert False, 'ExecutionProc cannot be connected to anything'
  39. def run(self):
  40. it = self._pool.imap_unordered(
  41. fn=run_job,
  42. gen=[],
  43. process_context_fn=create_process_context,
  44. process_context_args=[self._prev_requirement],
  45. )
  46. for pool_result in it:
  47. self._unpack_result(pool_result)
  48. def next_test(self, test):
  49. if self.is_stopped:
  50. return False
  51. test_id = test.procid
  52. cmd = test.get_command()
  53. self._tests[test_id] = test, cmd
  54. outproc = self._outproc_factory(test)
  55. self._pool.add([Job(test_id, cmd, outproc, test.keep_output)])
  56. return True
  57. def result_for(self, test, result):
  58. assert False, 'ExecutionProc cannot receive results'
  59. def stop(self):
  60. super(ExecutionProc, self).stop()
  61. self._pool.abort()
  62. def _unpack_result(self, pool_result):
  63. if pool_result.heartbeat:
  64. self.heartbeat()
  65. return
  66. job_result = pool_result.value
  67. test_id, result = job_result
  68. test, result.cmd = self._tests[test_id]
  69. del self._tests[test_id]
  70. self._send_result(test, result)