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