1# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6#     * Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#     * Redistributions in binary form must reproduce the above
9#       copyright notice, this list of conditions and the following
10#       disclaimer in the documentation and/or other materials provided
11#       with the distribution.
12#     * Neither the name of Google Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived
14#       from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29import multiprocessing
30import os
31import Queue
32import threading
33import time
34
35from ..local import execution
36from ..local import progress
37from ..local import testsuite
38from ..local import utils
39from ..server import compression
40
41
42class EndpointProgress(progress.ProgressIndicator):
43  def __init__(self, sock, server, ctx):
44    super(EndpointProgress, self).__init__()
45    self.sock = sock
46    self.server = server
47    self.context = ctx
48    self.results_queue = []  # Accessors must synchronize themselves.
49    self.sender_lock = threading.Lock()
50    self.senderthread = threading.Thread(target=self._SenderThread)
51    self.senderthread.start()
52
53  def HasRun(self, test, has_unexpected_output):
54    # The runners that call this have a lock anyway, so this is safe.
55    self.results_queue.append(test)
56
57  def _SenderThread(self):
58    keep_running = True
59    tests = []
60    self.sender_lock.acquire()
61    while keep_running:
62      time.sleep(0.1)
63      # This should be "atomic enough" without locking :-)
64      # (We don't care which list any new elements get appended to, as long
65      # as we don't lose any and the last one comes last.)
66      current = self.results_queue
67      self.results_queue = []
68      for c in current:
69        if c is None:
70          keep_running = False
71        else:
72          tests.append(c)
73      if keep_running and len(tests) < 1:
74        continue  # Wait for more results.
75      if len(tests) < 1: break  # We're done here.
76      result = []
77      for t in tests:
78        result.append(t.PackResult())
79      try:
80        compression.Send(result, self.sock)
81      except:
82        self.runner.terminate = True
83      for t in tests:
84        self.server.CompareOwnPerf(t, self.context.arch, self.context.mode)
85      tests = []
86    self.sender_lock.release()
87
88
89def Execute(workspace, ctx, tests, sock, server):
90  suite_paths = utils.GetSuitePaths(os.path.join(workspace, "test"))
91  suites = []
92  for root in suite_paths:
93    suite = testsuite.TestSuite.LoadTestSuite(
94        os.path.join(workspace, "test", root))
95    if suite:
96      suites.append(suite)
97
98  suites_dict = {}
99  for s in suites:
100    suites_dict[s.name] = s
101    s.tests = []
102  for t in tests:
103    suite = suites_dict[t.suite]
104    t.suite = suite
105    suite.tests.append(t)
106
107  suites = [ s for s in suites if len(s.tests) > 0 ]
108  for s in suites:
109    s.DownloadData()
110
111  progress_indicator = EndpointProgress(sock, server, ctx)
112  runner = execution.Runner(suites, progress_indicator, ctx)
113  try:
114    runner.Run(server.jobs)
115  except IOError, e:
116    if e.errno == 2:
117      message = ("File not found: %s, maybe you forgot to 'git add' it?" %
118                 e.filename)
119    else:
120      message = "%s" % e
121    compression.Send([[-1, message]], sock)
122  progress_indicator.HasRun(None, None)  # Sentinel to signal the end.
123  progress_indicator.sender_lock.acquire()  # Released when sending is done.
124  progress_indicator.sender_lock.release()
125