armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame^] | 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 | |
| 27 | import multiprocessing |
| 28 | import re |
| 29 | import signal |
| 30 | import subprocess |
| 31 | import sys |
| 32 | import time |
| 33 | |
| 34 | import printer |
| 35 | import util |
| 36 | |
| 37 | # Catch SIGINT to gracefully exit when ctrl+C is pressed. |
| 38 | def SigIntHandler(signal, frame): |
| 39 | sys.exit(1) |
| 40 | |
| 41 | signal.signal(signal.SIGINT, SigIntHandler) |
| 42 | |
| 43 | |
| 44 | # Scan matching tests and return a test manifest. |
| 45 | def 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. |
| 63 | n_tests_passed = multiprocessing.Value('i', 0) |
| 64 | n_tests_failed = multiprocessing.Value('i', 0) |
| 65 | # Read-only for workers. |
| 66 | test_runner = None |
| 67 | test_runner_runtime_options = None |
| 68 | n_tests = None |
| 69 | start_time = None |
| 70 | progress_prefix = None |
| 71 | |
| 72 | |
| 73 | def RunTest(test): |
| 74 | command = [test_runner, test] + test_runner_runtime_options |
| 75 | |
| 76 | p = subprocess.Popen(command, |
| 77 | stdout=subprocess.PIPE, |
| 78 | stderr=subprocess.STDOUT) |
| 79 | p_out, p_err = p.communicate() |
| 80 | rc = p.poll() |
| 81 | |
| 82 | if rc == 0: |
| 83 | with n_tests_passed.get_lock(): n_tests_passed.value += 1 |
| 84 | else: |
| 85 | with n_tests_failed.get_lock(): n_tests_failed.value += 1 |
| 86 | |
| 87 | printer.__print_lock__.acquire() |
| 88 | |
| 89 | printer.UpdateProgress(start_time, |
| 90 | n_tests_passed.value, |
| 91 | n_tests_failed.value, |
| 92 | n_tests, |
| 93 | test, |
| 94 | prevent_next_overwrite = (rc != 0), |
| 95 | has_lock = True, |
| 96 | prefix = progress_prefix) |
| 97 | |
| 98 | if rc != 0: |
| 99 | printer.Print('FAILED: ' + test, has_lock = True) |
| 100 | printer.Print(printer.COLOUR_RED + ' '.join(command) + printer.NO_COLOUR, |
| 101 | has_lock = True) |
| 102 | printer.Print(p_out, has_lock = True) |
| 103 | |
| 104 | printer.__print_lock__.release() |
| 105 | |
| 106 | |
| 107 | # Run the specified tests. |
| 108 | # This function won't run in parallel due to constraints from the |
| 109 | # multiprocessing module. |
| 110 | __run_tests_lock__ = multiprocessing.Lock() |
| 111 | def RunTests(test_runner_command, filters, runtime_options, |
| 112 | jobs = 1, prefix = ''): |
| 113 | global test_runner |
| 114 | global test_runner_runtime_options |
| 115 | global n_tests |
| 116 | global start_time |
| 117 | global progress_prefix |
| 118 | |
| 119 | tests = GetTests(test_runner_command, filters) |
| 120 | |
| 121 | if n_tests == 0: |
| 122 | printer.Print('No tests to run.') |
| 123 | return 0 |
| 124 | |
| 125 | with __run_tests_lock__: |
| 126 | |
| 127 | # Initialisation. |
| 128 | start_time = time.time() |
| 129 | test_runner = test_runner_command |
| 130 | test_runner_runtime_options = runtime_options |
| 131 | n_tests = len(tests) |
| 132 | n_tests_passed.value = 0 |
| 133 | n_tests_failed.value = 0 |
| 134 | progress_prefix = prefix |
| 135 | |
| 136 | pool = multiprocessing.Pool(jobs) |
| 137 | # The '.get(9999999)' is a workaround to allow killing the test script with |
| 138 | # ctrl+C from the shell. This bug is documented at |
| 139 | # http://bugs.python.org/issue8296. |
| 140 | work = pool.map_async(RunTest, tests).get(9999999) |
| 141 | pool.close() |
| 142 | pool.join() |
| 143 | |
| 144 | printer.UpdateProgress(start_time, |
| 145 | n_tests_passed.value, |
| 146 | n_tests_failed.value, |
| 147 | n_tests, |
| 148 | '== Done ==', |
| 149 | prevent_next_overwrite = True, |
| 150 | prefix = progress_prefix) |
| 151 | |
| 152 | # `0` indicates success |
| 153 | return n_tests_failed.value |