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