Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 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 |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 30 | """Run a group of subprocesses and then finish.""" |
| 31 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 32 | from __future__ import print_function |
| 33 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 34 | import multiprocessing |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 35 | import os |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 36 | import platform |
Craig Tiller | 5f735a6 | 2016-01-20 09:31:15 -0800 | [diff] [blame] | 37 | import re |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 38 | import signal |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 39 | import subprocess |
| 40 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 41 | import tempfile |
| 42 | import time |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 43 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 44 | |
Craig Tiller | 5f735a6 | 2016-01-20 09:31:15 -0800 | [diff] [blame] | 45 | # cpu cost measurement |
| 46 | measure_cpu_costs = False |
| 47 | |
| 48 | |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 49 | _DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count() |
Adele Zhou | d01cbe3 | 2015-11-02 14:20:43 -0800 | [diff] [blame] | 50 | _MAX_RESULT_SIZE = 8192 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 51 | |
Masood Malekghassemi | 768b1db | 2016-06-06 16:45:19 -0700 | [diff] [blame] | 52 | def sanitized_environment(env): |
| 53 | sanitized = {} |
| 54 | for key, value in env.items(): |
| 55 | sanitized[str(key).encode()] = str(value).encode() |
| 56 | return sanitized |
| 57 | |
Nicolas "Pixel" Noble | f72d7b5 | 2015-12-03 03:07:43 +0100 | [diff] [blame] | 58 | def platform_string(): |
| 59 | if platform.system() == 'Windows': |
| 60 | return 'windows' |
| 61 | elif platform.system()[:7] == 'MSYS_NT': |
| 62 | return 'windows' |
| 63 | elif platform.system() == 'Darwin': |
| 64 | return 'mac' |
| 65 | elif platform.system() == 'Linux': |
| 66 | return 'linux' |
| 67 | else: |
| 68 | return 'posix' |
| 69 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 70 | |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 71 | # setup a signal handler so that signal.pause registers 'something' |
| 72 | # when a child finishes |
| 73 | # not using futures and threading to avoid a dependency on subprocess32 |
Nicolas "Pixel" Noble | f72d7b5 | 2015-12-03 03:07:43 +0100 | [diff] [blame] | 74 | if platform_string() == 'windows': |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 75 | pass |
| 76 | else: |
| 77 | have_alarm = False |
| 78 | def alarm_handler(unused_signum, unused_frame): |
| 79 | global have_alarm |
| 80 | have_alarm = False |
| 81 | |
| 82 | signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None) |
| 83 | signal.signal(signal.SIGALRM, alarm_handler) |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 84 | |
| 85 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 86 | _SUCCESS = object() |
| 87 | _FAILURE = object() |
| 88 | _RUNNING = object() |
| 89 | _KILLED = object() |
| 90 | |
| 91 | |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 92 | _COLORS = { |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 93 | 'red': [ 31, 0 ], |
| 94 | 'green': [ 32, 0 ], |
| 95 | 'yellow': [ 33, 0 ], |
| 96 | 'lightgray': [ 37, 0], |
| 97 | 'gray': [ 30, 1 ], |
Craig Tiller | d7e09c3 | 2015-09-25 11:33:39 -0700 | [diff] [blame] | 98 | 'purple': [ 35, 0 ], |
Matt Kwong | 5c691c6 | 2016-10-20 17:11:18 -0700 | [diff] [blame] | 99 | 'cyan': [ 36, 0 ] |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | _BEGINNING_OF_LINE = '\x1b[0G' |
| 104 | _CLEAR_LINE = '\x1b[2K' |
| 105 | |
| 106 | |
| 107 | _TAG_COLOR = { |
| 108 | 'FAILED': 'red', |
Craig Tiller | d7e09c3 | 2015-09-25 11:33:39 -0700 | [diff] [blame] | 109 | 'FLAKE': 'purple', |
Craig Tiller | 3dc1e4f | 2015-09-25 11:46:56 -0700 | [diff] [blame] | 110 | 'TIMEOUT_FLAKE': 'purple', |
Masood Malekghassemi | e5f7002 | 2015-06-29 09:20:26 -0700 | [diff] [blame] | 111 | 'WARNING': 'yellow', |
Craig Tiller | e1d0d1c | 2015-02-27 08:54:23 -0800 | [diff] [blame] | 112 | 'TIMEOUT': 'red', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 113 | 'PASSED': 'green', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 114 | 'START': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 115 | 'WAITING': 'yellow', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 116 | 'SUCCESS': 'green', |
| 117 | 'IDLE': 'gray', |
Matt Kwong | 5c691c6 | 2016-10-20 17:11:18 -0700 | [diff] [blame] | 118 | 'SKIPPED': 'cyan' |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 122 | def message(tag, msg, explanatory_text=None, do_newline=False): |
| 123 | if message.old_tag == tag and message.old_msg == msg and not explanatory_text: |
| 124 | return |
| 125 | message.old_tag = tag |
| 126 | message.old_msg = msg |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 127 | try: |
Nicolas "Pixel" Noble | f72d7b5 | 2015-12-03 03:07:43 +0100 | [diff] [blame] | 128 | if platform_string() == 'windows' or not sys.stdout.isatty(): |
Craig Tiller | 9f3b2d7 | 2015-08-25 11:50:57 -0700 | [diff] [blame] | 129 | if explanatory_text: |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 130 | print(explanatory_text) |
| 131 | print('%s: %s' % (tag, msg)) |
Craig Tiller | 9f3b2d7 | 2015-08-25 11:50:57 -0700 | [diff] [blame] | 132 | return |
vjpai | a29d2d7 | 2015-07-08 10:31:15 -0700 | [diff] [blame] | 133 | sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % ( |
| 134 | _BEGINNING_OF_LINE, |
| 135 | _CLEAR_LINE, |
| 136 | '\n%s' % explanatory_text if explanatory_text is not None else '', |
| 137 | _COLORS[_TAG_COLOR[tag]][1], |
| 138 | _COLORS[_TAG_COLOR[tag]][0], |
| 139 | tag, |
| 140 | msg, |
| 141 | '\n' if do_newline or explanatory_text is not None else '')) |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 142 | sys.stdout.flush() |
| 143 | except: |
| 144 | pass |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 145 | |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 146 | message.old_tag = '' |
| 147 | message.old_msg = '' |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 148 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 149 | def which(filename): |
| 150 | if '/' in filename: |
| 151 | return filename |
| 152 | for path in os.environ['PATH'].split(os.pathsep): |
| 153 | if os.path.exists(os.path.join(path, filename)): |
| 154 | return os.path.join(path, filename) |
| 155 | raise Exception('%s not found' % filename) |
| 156 | |
| 157 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 158 | class JobSpec(object): |
| 159 | """Specifies what to run for a job.""" |
| 160 | |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 161 | def __init__(self, cmdline, shortname=None, environ=None, |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 162 | cwd=None, shell=False, timeout_seconds=5*60, flake_retries=0, |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 163 | timeout_retries=0, kill_handler=None, cpu_cost=1.0, |
| 164 | verbose_success=False): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 165 | """ |
| 166 | Arguments: |
| 167 | cmdline: a list of arguments to pass as the command line |
| 168 | environ: a dictionary of environment variables to set in the child process |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 169 | kill_handler: a handler that will be called whenever job.kill() is invoked |
Craig Tiller | 56c6b6a | 2016-01-20 08:27:37 -0800 | [diff] [blame] | 170 | cpu_cost: number of cores per second this job needs |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 171 | """ |
murgatroid99 | 132ce6a | 2015-03-04 17:29:14 -0800 | [diff] [blame] | 172 | if environ is None: |
| 173 | environ = {} |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 174 | self.cmdline = cmdline |
| 175 | self.environ = environ |
| 176 | self.shortname = cmdline[0] if shortname is None else shortname |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 177 | self.cwd = cwd |
Jan Tattermusch | e824359 | 2015-04-17 14:14:01 -0700 | [diff] [blame] | 178 | self.shell = shell |
Jan Tattermusch | 725835a | 2015-08-01 21:02:35 -0700 | [diff] [blame] | 179 | self.timeout_seconds = timeout_seconds |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 180 | self.flake_retries = flake_retries |
Craig Tiller | bfc8a06 | 2015-09-28 14:40:21 -0700 | [diff] [blame] | 181 | self.timeout_retries = timeout_retries |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 182 | self.kill_handler = kill_handler |
Craig Tiller | 56c6b6a | 2016-01-20 08:27:37 -0800 | [diff] [blame] | 183 | self.cpu_cost = cpu_cost |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 184 | self.verbose_success = verbose_success |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 185 | |
| 186 | def identity(self): |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 187 | return '%r %r' % (self.cmdline, self.environ) |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 188 | |
| 189 | def __hash__(self): |
| 190 | return hash(self.identity()) |
| 191 | |
| 192 | def __cmp__(self, other): |
| 193 | return self.identity() == other.identity() |
Craig Tiller | db21899 | 2016-01-05 09:28:46 -0800 | [diff] [blame] | 194 | |
Jan Tattermusch | 2dd156e | 2015-12-04 18:26:17 -0800 | [diff] [blame] | 195 | def __repr__(self): |
| 196 | return 'JobSpec(shortname=%s, cmdline=%s)' % (self.shortname, self.cmdline) |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 197 | |
| 198 | |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 199 | class JobResult(object): |
| 200 | def __init__(self): |
| 201 | self.state = 'UNKNOWN' |
| 202 | self.returncode = -1 |
| 203 | self.elapsed_time = 0 |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 204 | self.num_failures = 0 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 205 | self.retries = 0 |
| 206 | self.message = '' |
Craig Tiller | db21899 | 2016-01-05 09:28:46 -0800 | [diff] [blame] | 207 | |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 208 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 209 | class Job(object): |
| 210 | """Manages one job.""" |
| 211 | |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 212 | def __init__(self, spec, newline_on_success, travis, add_env): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 213 | self._spec = spec |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 214 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 215 | self._travis = travis |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 216 | self._add_env = add_env.copy() |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 217 | self._retries = 0 |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 218 | self._timeout_retries = 0 |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 219 | self._suppress_failure_message = False |
Craig Tiller | b84728d | 2015-02-26 15:40:39 -0800 | [diff] [blame] | 220 | message('START', spec.shortname, do_newline=self._travis) |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 221 | self.result = JobResult() |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 222 | self.start() |
| 223 | |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 224 | def GetSpec(self): |
| 225 | return self._spec |
| 226 | |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 227 | def start(self): |
| 228 | self._tempfile = tempfile.TemporaryFile() |
| 229 | env = dict(os.environ) |
| 230 | env.update(self._spec.environ) |
| 231 | env.update(self._add_env) |
Masood Malekghassemi | 768b1db | 2016-06-06 16:45:19 -0700 | [diff] [blame] | 232 | env = sanitized_environment(env) |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 233 | self._start = time.time() |
Craig Tiller | 5f735a6 | 2016-01-20 09:31:15 -0800 | [diff] [blame] | 234 | cmdline = self._spec.cmdline |
| 235 | if measure_cpu_costs: |
| 236 | cmdline = ['time', '--portability'] + cmdline |
| 237 | try_start = lambda: subprocess.Popen(args=cmdline, |
Craig Tiller | 60078bb | 2015-11-04 16:39:54 -0800 | [diff] [blame] | 238 | stderr=subprocess.STDOUT, |
| 239 | stdout=self._tempfile, |
| 240 | cwd=self._spec.cwd, |
| 241 | shell=self._spec.shell, |
| 242 | env=env) |
| 243 | delay = 0.3 |
| 244 | for i in range(0, 4): |
| 245 | try: |
| 246 | self._process = try_start() |
| 247 | break |
| 248 | except OSError: |
| 249 | message('WARNING', 'Failed to start %s, retrying in %f seconds' % (self._spec.shortname, delay)) |
| 250 | time.sleep(delay) |
| 251 | delay *= 2 |
| 252 | else: |
| 253 | self._process = try_start() |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 254 | self._state = _RUNNING |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 255 | |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 256 | def state(self): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 257 | """Poll current state of the job. Prints messages at completion.""" |
Craig Tiller | db21899 | 2016-01-05 09:28:46 -0800 | [diff] [blame] | 258 | def stdout(self=self): |
| 259 | self._tempfile.seek(0) |
| 260 | stdout = self._tempfile.read() |
| 261 | self.result.message = stdout[-_MAX_RESULT_SIZE:] |
| 262 | return stdout |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 263 | if self._state == _RUNNING and self._process.poll() is not None: |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame] | 264 | elapsed = time.time() - self._start |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 265 | self.result.elapsed_time = elapsed |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 266 | if self._process.returncode != 0: |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 267 | if self._retries < self._spec.flake_retries: |
| 268 | message('FLAKE', '%s [ret=%d, pid=%d]' % ( |
Craig Tiller | d0ffe14 | 2015-05-19 21:51:13 -0700 | [diff] [blame] | 269 | self._spec.shortname, self._process.returncode, self._process.pid), |
Craig Tiller | db21899 | 2016-01-05 09:28:46 -0800 | [diff] [blame] | 270 | stdout(), do_newline=True) |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 271 | self._retries += 1 |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 272 | self.result.num_failures += 1 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 273 | self.result.retries = self._timeout_retries + self._retries |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 274 | self.start() |
| 275 | else: |
| 276 | self._state = _FAILURE |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 277 | if not self._suppress_failure_message: |
| 278 | message('FAILED', '%s [ret=%d, pid=%d]' % ( |
| 279 | self._spec.shortname, self._process.returncode, self._process.pid), |
Craig Tiller | db21899 | 2016-01-05 09:28:46 -0800 | [diff] [blame] | 280 | stdout(), do_newline=True) |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 281 | self.result.state = 'FAILED' |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 282 | self.result.num_failures += 1 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 283 | self.result.returncode = self._process.returncode |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 284 | else: |
| 285 | self._state = _SUCCESS |
Craig Tiller | 5f735a6 | 2016-01-20 09:31:15 -0800 | [diff] [blame] | 286 | measurement = '' |
| 287 | if measure_cpu_costs: |
| 288 | m = re.search(r'real ([0-9.]+)\nuser ([0-9.]+)\nsys ([0-9.]+)', stdout()) |
| 289 | real = float(m.group(1)) |
| 290 | user = float(m.group(2)) |
| 291 | sys = float(m.group(3)) |
| 292 | if real > 0.5: |
| 293 | cores = (user + sys) / real |
Craig Tiller | bfe6936 | 2016-01-20 09:38:21 -0800 | [diff] [blame] | 294 | measurement = '; cpu_cost=%.01f; estimated=%.01f' % (cores, self._spec.cpu_cost) |
Craig Tiller | 5f735a6 | 2016-01-20 09:31:15 -0800 | [diff] [blame] | 295 | message('PASSED', '%s [time=%.1fsec; retries=%d:%d%s]' % ( |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 296 | self._spec.shortname, elapsed, self._retries, self._timeout_retries, measurement), |
| 297 | stdout() if self._spec.verbose_success else None, |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 298 | do_newline=self._newline_on_success or self._travis) |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 299 | self.result.state = 'PASSED' |
Craig Tiller | 5f735a6 | 2016-01-20 09:31:15 -0800 | [diff] [blame] | 300 | elif (self._state == _RUNNING and |
| 301 | self._spec.timeout_seconds is not None and |
Craig Tiller | 590105a | 2016-01-19 13:03:46 -0800 | [diff] [blame] | 302 | time.time() - self._start > self._spec.timeout_seconds): |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 303 | if self._timeout_retries < self._spec.timeout_retries: |
Craig Tiller | db21899 | 2016-01-05 09:28:46 -0800 | [diff] [blame] | 304 | message('TIMEOUT_FLAKE', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True) |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 305 | self._timeout_retries += 1 |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 306 | self.result.num_failures += 1 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 307 | self.result.retries = self._timeout_retries + self._retries |
Jan Tattermusch | 39e3cb3 | 2015-10-22 18:21:08 -0700 | [diff] [blame] | 308 | if self._spec.kill_handler: |
| 309 | self._spec.kill_handler(self) |
Craig Tiller | 3dc1e4f | 2015-09-25 11:46:56 -0700 | [diff] [blame] | 310 | self._process.terminate() |
| 311 | self.start() |
| 312 | else: |
Craig Tiller | db21899 | 2016-01-05 09:28:46 -0800 | [diff] [blame] | 313 | message('TIMEOUT', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True) |
Craig Tiller | 3dc1e4f | 2015-09-25 11:46:56 -0700 | [diff] [blame] | 314 | self.kill() |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 315 | self.result.state = 'TIMEOUT' |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 316 | self.result.num_failures += 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 317 | return self._state |
| 318 | |
| 319 | def kill(self): |
| 320 | if self._state == _RUNNING: |
| 321 | self._state = _KILLED |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 322 | if self._spec.kill_handler: |
| 323 | self._spec.kill_handler(self) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 324 | self._process.terminate() |
| 325 | |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 326 | def suppress_failure_message(self): |
| 327 | self._suppress_failure_message = True |
Craig Tiller | db21899 | 2016-01-05 09:28:46 -0800 | [diff] [blame] | 328 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 329 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 330 | class Jobset(object): |
| 331 | """Manages one run of jobs.""" |
| 332 | |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 333 | def __init__(self, check_cancelled, maxjobs, newline_on_success, travis, |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 334 | stop_on_failure, add_env): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 335 | self._running = set() |
| 336 | self._check_cancelled = check_cancelled |
| 337 | self._cancelled = False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 338 | self._failures = 0 |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 339 | self._completed = 0 |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 340 | self._maxjobs = maxjobs |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 341 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 342 | self._travis = travis |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 343 | self._stop_on_failure = stop_on_failure |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 344 | self._add_env = add_env |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 345 | self.resultset = {} |
Craig Tiller | 6364dcb | 2015-11-24 16:29:06 -0800 | [diff] [blame] | 346 | self._remaining = None |
Craig Tiller | ed73510 | 2016-04-06 12:59:23 -0700 | [diff] [blame] | 347 | self._start_time = time.time() |
Craig Tiller | 6364dcb | 2015-11-24 16:29:06 -0800 | [diff] [blame] | 348 | |
| 349 | def set_remaining(self, remaining): |
| 350 | self._remaining = remaining |
| 351 | |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 352 | def get_num_failures(self): |
Craig Tiller | 6364dcb | 2015-11-24 16:29:06 -0800 | [diff] [blame] | 353 | return self._failures |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 354 | |
Craig Tiller | 56c6b6a | 2016-01-20 08:27:37 -0800 | [diff] [blame] | 355 | def cpu_cost(self): |
| 356 | c = 0 |
| 357 | for job in self._running: |
| 358 | c += job._spec.cpu_cost |
| 359 | return c |
| 360 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 361 | def start(self, spec): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 362 | """Start a job. Return True on success, False on failure.""" |
Craig Tiller | 56c6b6a | 2016-01-20 08:27:37 -0800 | [diff] [blame] | 363 | while True: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 364 | if self.cancelled(): return False |
Craig Tiller | 56c6b6a | 2016-01-20 08:27:37 -0800 | [diff] [blame] | 365 | current_cpu_cost = self.cpu_cost() |
| 366 | if current_cpu_cost == 0: break |
Craig Tiller | 3e301a3 | 2016-01-27 15:36:52 -0800 | [diff] [blame] | 367 | if current_cpu_cost + spec.cpu_cost <= self._maxjobs: break |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 368 | self.reap() |
| 369 | if self.cancelled(): return False |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 370 | job = Job(spec, |
| 371 | self._newline_on_success, |
| 372 | self._travis, |
| 373 | self._add_env) |
| 374 | self._running.add(job) |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 375 | if job.GetSpec().shortname not in self.resultset: |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 376 | self.resultset[job.GetSpec().shortname] = [] |
Craig Tiller | ddf02c1 | 2016-06-24 14:04:01 -0700 | [diff] [blame] | 377 | return True |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 378 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 379 | def reap(self): |
| 380 | """Collect the dead jobs.""" |
| 381 | while self._running: |
| 382 | dead = set() |
| 383 | for job in self._running: |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 384 | st = job.state() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 385 | if st == _RUNNING: continue |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 386 | if st == _FAILURE or st == _KILLED: |
| 387 | self._failures += 1 |
| 388 | if self._stop_on_failure: |
| 389 | self._cancelled = True |
| 390 | for job in self._running: |
| 391 | job.kill() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 392 | dead.add(job) |
Craig Tiller | 74e770d | 2015-06-11 09:38:09 -0700 | [diff] [blame] | 393 | break |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 394 | for job in dead: |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 395 | self._completed += 1 |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 396 | self.resultset[job.GetSpec().shortname].append(job.result) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 397 | self._running.remove(job) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 398 | if dead: return |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 399 | if (not self._travis): |
Craig Tiller | 2c23ad5 | 2015-12-04 06:50:38 -0800 | [diff] [blame] | 400 | rstr = '' if self._remaining is None else '%d queued, ' % self._remaining |
Craig Tiller | ed73510 | 2016-04-06 12:59:23 -0700 | [diff] [blame] | 401 | if self._remaining is not None and self._completed > 0: |
| 402 | now = time.time() |
| 403 | sofar = now - self._start_time |
| 404 | remaining = sofar / self._completed * (self._remaining + len(self._running)) |
| 405 | rstr = 'ETA %.1f sec; %s' % (remaining, rstr) |
Craig Tiller | 2c23ad5 | 2015-12-04 06:50:38 -0800 | [diff] [blame] | 406 | message('WAITING', '%s%d jobs running, %d complete, %d failed' % ( |
| 407 | rstr, len(self._running), self._completed, self._failures)) |
Nicolas "Pixel" Noble | f72d7b5 | 2015-12-03 03:07:43 +0100 | [diff] [blame] | 408 | if platform_string() == 'windows': |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 409 | time.sleep(0.1) |
| 410 | else: |
| 411 | global have_alarm |
| 412 | if not have_alarm: |
| 413 | have_alarm = True |
| 414 | signal.alarm(10) |
| 415 | signal.pause() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 416 | |
| 417 | def cancelled(self): |
| 418 | """Poll for cancellation.""" |
| 419 | if self._cancelled: return True |
| 420 | if not self._check_cancelled(): return False |
| 421 | for job in self._running: |
| 422 | job.kill() |
| 423 | self._cancelled = True |
| 424 | return True |
| 425 | |
| 426 | def finish(self): |
| 427 | while self._running: |
| 428 | if self.cancelled(): pass # poll cancellation |
| 429 | self.reap() |
| 430 | return not self.cancelled() and self._failures == 0 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 431 | |
| 432 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 433 | def _never_cancelled(): |
| 434 | return False |
| 435 | |
| 436 | |
Craig Tiller | 6364dcb | 2015-11-24 16:29:06 -0800 | [diff] [blame] | 437 | def tag_remaining(xs): |
| 438 | staging = [] |
| 439 | for x in xs: |
| 440 | staging.append(x) |
Craig Tiller | ed73510 | 2016-04-06 12:59:23 -0700 | [diff] [blame] | 441 | if len(staging) > 5000: |
Craig Tiller | 6364dcb | 2015-11-24 16:29:06 -0800 | [diff] [blame] | 442 | yield (staging.pop(0), None) |
| 443 | n = len(staging) |
| 444 | for i, x in enumerate(staging): |
| 445 | yield (x, n - i - 1) |
| 446 | |
| 447 | |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 448 | def run(cmdlines, |
| 449 | check_cancelled=_never_cancelled, |
| 450 | maxjobs=None, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 451 | newline_on_success=False, |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 452 | travis=False, |
David Garcia Quintas | e90cd37 | 2015-05-31 18:15:26 -0700 | [diff] [blame] | 453 | infinite_runs=False, |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 454 | stop_on_failure=False, |
Matt Kwong | 5c691c6 | 2016-10-20 17:11:18 -0700 | [diff] [blame] | 455 | add_env={}, |
| 456 | skip_jobs=False): |
| 457 | if skip_jobs: |
| 458 | results = {} |
| 459 | skipped_job_result = JobResult() |
| 460 | skipped_job_result.state = 'SKIPPED' |
| 461 | for job in cmdlines: |
| 462 | message('SKIPPED', job.shortname, do_newline=True) |
| 463 | results[job.shortname] = [skipped_job_result] |
| 464 | return results |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 465 | js = Jobset(check_cancelled, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 466 | maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
Craig Tiller | 74189cd | 2016-06-23 15:39:06 -0700 | [diff] [blame] | 467 | newline_on_success, travis, stop_on_failure, add_env) |
Craig Tiller | 6364dcb | 2015-11-24 16:29:06 -0800 | [diff] [blame] | 468 | for cmdline, remaining in tag_remaining(cmdlines): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 469 | if not js.start(cmdline): |
| 470 | break |
Craig Tiller | 6364dcb | 2015-11-24 16:29:06 -0800 | [diff] [blame] | 471 | if remaining is not None: |
| 472 | js.set_remaining(remaining) |
| 473 | js.finish() |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 474 | return js.get_num_failures(), js.resultset |