• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015, ARM Limited
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7#   * Redistributions of source code must retain the above copyright notice,
8#     this list of conditions and the following disclaimer.
9#   * Redistributions in binary form must reproduce the above copyright notice,
10#     this list of conditions and the following disclaimer in the documentation
11#     and/or other materials provided with the distribution.
12#   * Neither the name of ARM Limited nor the names of its contributors may be
13#     used to endorse or promote products derived from this software without
14#     specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27import multiprocessing
28import re
29import signal
30import subprocess
31import sys
32import time
33
34import printer
35import util
36
37# Catch SIGINT to gracefully exit when ctrl+C is pressed.
38def SigIntHandler(signal, frame):
39  sys.exit(1)
40
41signal.signal(signal.SIGINT, SigIntHandler)
42
43
44# Scan matching tests and return a test manifest.
45def GetTests(runner, filters = []):
46  rc, output = util.getstatusoutput(runner +  ' --list')
47  if rc != 0: util.abort('Failed to list all tests')
48
49  tests = output.split()
50  for f in filters:
51    print f
52    tests = filter(re.compile(f).search, tests)
53
54  return tests
55
56
57# Shared state for multiprocessing. Ideally the context should be passed with
58# arguments, but constraints from the multiprocessing module prevent us from
59# doing so: the shared variables (multiprocessing.Value) must be global, or no
60# work is started. So we abstract some additional state into global variables to
61# simplify the implementation.
62# Read-write variables for the workers.
63n_tests_passed = multiprocessing.Value('i', 0)
64n_tests_failed = multiprocessing.Value('i', 0)
65# Read-only for workers.
66test_runner = None
67test_runner_runtime_options = None
68test_runner_under_valgrind = False
69n_tests = None
70start_time = None
71progress_prefix = None
72
73
74def RunTest(test):
75  command = [test_runner, test] + test_runner_runtime_options
76  if test_runner_under_valgrind:
77    command = ['valgrind'] + command
78
79  p = subprocess.Popen(command,
80                       stdout=subprocess.PIPE,
81                       stderr=subprocess.STDOUT)
82  p_out, p_err = p.communicate()
83  rc = p.poll()
84
85  if rc == 0:
86    with n_tests_passed.get_lock(): n_tests_passed.value += 1
87  else:
88    with n_tests_failed.get_lock(): n_tests_failed.value += 1
89
90  printer.__print_lock__.acquire()
91
92  printer.UpdateProgress(start_time,
93                         n_tests_passed.value,
94                         n_tests_failed.value,
95                         n_tests,
96                         test,
97                         prevent_next_overwrite = (rc != 0),
98                         has_lock = True,
99                         prefix = progress_prefix)
100
101  if rc != 0:
102    printer.Print('FAILED: ' + test, has_lock = True)
103    printer.Print(printer.COLOUR_RED + ' '.join(command) + printer.NO_COLOUR,
104                  has_lock = True)
105    printer.Print(p_out, has_lock = True)
106
107  printer.__print_lock__.release()
108
109
110# Run the specified tests.
111# This function won't run in parallel due to constraints from the
112# multiprocessing module.
113__run_tests_lock__ = multiprocessing.Lock()
114def RunTests(test_runner_command, filters, runtime_options,
115             under_valgrind = False,
116             jobs = 1, prefix = ''):
117  global test_runner
118  global test_runner_runtime_options
119  global test_runner_under_valgrind
120  global n_tests
121  global start_time
122  global progress_prefix
123
124  tests = GetTests(test_runner_command, filters)
125
126  if n_tests == 0:
127    printer.Print('No tests to run.')
128    return 0
129
130  with __run_tests_lock__:
131
132    # Initialisation.
133    start_time = time.time()
134    test_runner = test_runner_command
135    test_runner_runtime_options = runtime_options
136    test_runner_under_valgrind = under_valgrind
137    n_tests = len(tests)
138    n_tests_passed.value = 0
139    n_tests_failed.value = 0
140    progress_prefix = prefix
141
142    pool = multiprocessing.Pool(jobs)
143    # The '.get(9999999)' is a workaround to allow killing the test script with
144    # ctrl+C from the shell. This bug is documented at
145    # http://bugs.python.org/issue8296.
146    work = pool.map_async(RunTest, tests).get(9999999)
147    pool.close()
148    pool.join()
149
150    printer.UpdateProgress(start_time,
151                           n_tests_passed.value,
152                           n_tests_failed.value,
153                           n_tests,
154                           '== Done ==',
155                           prevent_next_overwrite = True,
156                           prefix = progress_prefix)
157
158  # `0` indicates success
159  return n_tests_failed.value
160