Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 2 | # Copyright 2021 The Chromium Authors |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Creates an server to offload non-critical-path GN targets.""" |
| 6 | |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 7 | from __future__ import annotations |
| 8 | |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 9 | import argparse |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 10 | import collections |
| 11 | import contextlib |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 12 | import dataclasses |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 13 | import datetime |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 14 | import os |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 15 | import pathlib |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 16 | import re |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 17 | import signal |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 18 | import shlex |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 19 | import shutil |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 20 | import socket |
| 21 | import subprocess |
| 22 | import sys |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 23 | import threading |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 24 | import traceback |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 25 | import time |
| 26 | from typing import Callable, Dict, List, Optional, Tuple, IO |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 27 | |
| 28 | sys.path.append(os.path.join(os.path.dirname(__file__), 'gyp')) |
| 29 | from util import server_utils |
| 30 | |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 31 | _SOCKET_TIMEOUT = 60 # seconds |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 32 | |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 33 | _LOGFILE_NAME = 'buildserver.log' |
| 34 | _MAX_LOGFILES = 6 |
| 35 | |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 36 | FIRST_LOG_LINE = """\ |
| 37 | #### Start of log for build: {build_id} |
| 38 | #### CWD: {outdir} |
| 39 | """ |
| 40 | BUILD_ID_RE = re.compile(r'^#### Start of log for build: (?P<build_id>.+)') |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 41 | |
| 42 | |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 43 | def server_log(msg: str): |
| 44 | if OptionsManager.is_quiet(): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 45 | return |
| 46 | # Ensure we start our message on a new line. |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 47 | print('\n' + msg) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 48 | |
| 49 | |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 50 | def print_status(prefix: str, msg: str): |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 51 | # No need to also output to the terminal if quiet. |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 52 | if OptionsManager.is_quiet(): |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 53 | return |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 54 | # Shrink the message (leaving a 2-char prefix and use the rest of the room |
| 55 | # for the suffix) according to terminal size so it is always one line. |
| 56 | width = shutil.get_terminal_size().columns |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 57 | max_msg_width = width - len(prefix) |
| 58 | if len(msg) > max_msg_width: |
| 59 | length_to_show = max_msg_width - 5 # Account for ellipsis and header. |
| 60 | msg = f'{msg[:2]}...{msg[-length_to_show:]}' |
| 61 | # \r to return the carriage to the beginning of line. |
| 62 | # \033[K to replace the normal \n to erase until the end of the line. |
| 63 | # Avoid the default line ending so the next \r overwrites the same line just |
| 64 | # like ninja's output. |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 65 | print(f'\r{prefix}{msg}\033[K', end='', flush=True) |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 66 | |
| 67 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 68 | def _exception_hook(exctype: type, exc: Exception, tb): |
Mohamed Heikal | d764eca | 2025-01-31 01:06:35 | [diff] [blame] | 69 | # Let KeyboardInterrupt through. |
| 70 | if issubclass(exctype, KeyboardInterrupt): |
| 71 | sys.__excepthook__(exctype, exc, tb) |
| 72 | return |
| 73 | stacktrace = ''.join(traceback.format_exception(exctype, exc, tb)) |
| 74 | stacktrace_lines = [f'\n⛔{line}' for line in stacktrace.splitlines()] |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 75 | # Output uncaught exceptions to all live terminals |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 76 | # Extra newline since siso's output often erases the current line. |
| 77 | BuildManager.broadcast(''.join(stacktrace_lines) + '\n') |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 78 | # Cancel all pending tasks cleanly (i.e. delete stamp files if necessary). |
| 79 | TaskManager.deactivate() |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 80 | # Reset all remote terminal titles. |
| 81 | BuildManager.update_remote_titles('') |
| 82 | |
| 83 | |
| 84 | # Stores global options so as to not keep passing along and storing options |
| 85 | # everywhere. |
| 86 | class OptionsManager: |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 87 | _quiet = None |
| 88 | _should_remote_print = None |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 89 | |
| 90 | @classmethod |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 91 | def set_options(cls, *, quiet, should_remote_print): |
| 92 | cls._quiet = quiet |
| 93 | cls._should_remote_print = should_remote_print |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 94 | |
| 95 | @classmethod |
| 96 | def is_quiet(cls): |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 97 | assert cls._quiet is not None |
| 98 | return cls._quiet |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 99 | |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 100 | @classmethod |
| 101 | def should_remote_print(cls): |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 102 | assert cls._should_remote_print is not None |
| 103 | return cls._should_remote_print |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 104 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 105 | |
Mohamed Heikal | 08b467e0 | 2025-01-27 20:54:25 | [diff] [blame] | 106 | class LogfileManager: |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 107 | _logfiles: dict[str, IO[str]] = {} |
| 108 | _lock = threading.RLock() |
Mohamed Heikal | 08b467e0 | 2025-01-27 20:54:25 | [diff] [blame] | 109 | |
| 110 | @classmethod |
Mohamed Heikal | 08b467e0 | 2025-01-27 20:54:25 | [diff] [blame] | 111 | def create_logfile(cls, build_id, outdir): |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 112 | with cls._lock: |
| 113 | if logfile := cls._logfiles.get(build_id, None): |
| 114 | return logfile |
| 115 | |
| 116 | outdir = pathlib.Path(outdir) |
| 117 | latest_logfile = outdir / f'{_LOGFILE_NAME}.0' |
| 118 | |
| 119 | if latest_logfile.exists(): |
| 120 | with latest_logfile.open('rt') as f: |
| 121 | first_line = f.readline() |
| 122 | if log_build_id := BUILD_ID_RE.search(first_line): |
| 123 | # If the newest logfile on disk is referencing the same build we are |
| 124 | # currently processing, we probably crashed previously and we should |
| 125 | # pick up where we left off in the same logfile. |
| 126 | if log_build_id.group('build_id') == build_id: |
| 127 | cls._logfiles[build_id] = latest_logfile.open('at') |
| 128 | return cls._logfiles[build_id] |
| 129 | |
| 130 | # Do the logfile name shift. |
| 131 | filenames = os.listdir(outdir) |
| 132 | logfiles = {f for f in filenames if f.startswith(_LOGFILE_NAME)} |
| 133 | for idx in reversed(range(_MAX_LOGFILES)): |
| 134 | current_name = f'{_LOGFILE_NAME}.{idx}' |
| 135 | next_name = f'{_LOGFILE_NAME}.{idx+1}' |
| 136 | if current_name in logfiles: |
| 137 | shutil.move(os.path.join(outdir, current_name), |
| 138 | os.path.join(outdir, next_name)) |
| 139 | |
| 140 | # Create a new 0th logfile. |
| 141 | logfile = latest_logfile.open('wt') |
| 142 | logfile.write(FIRST_LOG_LINE.format(build_id=build_id, outdir=outdir)) |
| 143 | logfile.flush() |
| 144 | cls._logfiles[build_id] = logfile |
Mohamed Heikal | 08b467e0 | 2025-01-27 20:54:25 | [diff] [blame] | 145 | return logfile |
| 146 | |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 147 | |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 148 | class TaskStats: |
| 149 | """Class to keep track of aggregate stats for all tasks across threads.""" |
| 150 | _num_processes = 0 |
| 151 | _completed_tasks = 0 |
| 152 | _total_tasks = 0 |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 153 | _lock = threading.RLock() |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 154 | |
| 155 | @classmethod |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 156 | def no_running_processes(cls): |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 157 | with cls._lock: |
| 158 | return cls._num_processes == 0 |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 159 | |
| 160 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 161 | def add_task(cls): |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 162 | with cls._lock: |
| 163 | cls._total_tasks += 1 |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 164 | |
| 165 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 166 | def add_process(cls): |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 167 | with cls._lock: |
| 168 | cls._num_processes += 1 |
| 169 | |
| 170 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 171 | def remove_process(cls): |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 172 | with cls._lock: |
| 173 | cls._num_processes -= 1 |
| 174 | |
| 175 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 176 | def complete_task(cls): |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 177 | with cls._lock: |
| 178 | cls._completed_tasks += 1 |
| 179 | |
| 180 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 181 | def num_pending_tasks(cls): |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 182 | with cls._lock: |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 183 | return cls._total_tasks - cls._completed_tasks |
| 184 | |
| 185 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 186 | def num_completed_tasks(cls): |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 187 | with cls._lock: |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 188 | return cls._completed_tasks |
| 189 | |
| 190 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 191 | def total_tasks(cls): |
Andrew Grieve | 6c764fff | 2025-01-30 21:02:03 | [diff] [blame] | 192 | with cls._lock: |
Andrew Grieve | 6c764fff | 2025-01-30 21:02:03 | [diff] [blame] | 193 | return cls._total_tasks |
| 194 | |
| 195 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 196 | def get_title_message(cls): |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 197 | with cls._lock: |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 198 | return f'Analysis Steps: {cls._completed_tasks}/{cls._total_tasks}' |
| 199 | |
| 200 | @classmethod |
| 201 | def query_build(cls, query_build_id: str = None): |
| 202 | builds = [] |
| 203 | if query_build_id: |
| 204 | if build := BuildManager.get_build(query_build_id): |
| 205 | builds.append(build) |
| 206 | else: |
| 207 | builds = BuildManager.get_all_builds() |
| 208 | build_infos = [] |
| 209 | for build in builds: |
| 210 | build_infos.append(build.query_build_info()) |
| 211 | return { |
| 212 | 'pid': os.getpid(), |
| 213 | 'builds': build_infos, |
| 214 | } |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 215 | |
| 216 | @classmethod |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 217 | def prefix(cls, build_id: str = None): |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 218 | # Ninja's prefix is: [205 processes, 6/734 @ 6.5/s : 0.922s ] |
| 219 | # Time taken and task completion rate are not important for the build server |
| 220 | # since it is always running in the background and uses idle priority for |
| 221 | # its tasks. |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 222 | with cls._lock: |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 223 | if build_id: |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 224 | build = BuildManager.get_build(build_id) |
| 225 | _num_processes = build.process_count() |
| 226 | _completed_tasks = build.completed_task_count() |
| 227 | _total_tasks = build.total_task_count() |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 228 | else: |
| 229 | _num_processes = cls._num_processes |
| 230 | _completed_tasks = cls._completed_tasks |
| 231 | _total_tasks = cls._total_tasks |
| 232 | word = 'process' if _num_processes == 1 else 'processes' |
| 233 | return (f'{_num_processes} {word}, ' |
| 234 | f'{_completed_tasks}/{_total_tasks}') |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 235 | |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 236 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 237 | def check_pid_alive(pid: int): |
| 238 | try: |
| 239 | os.kill(pid, 0) |
| 240 | except OSError: |
| 241 | return False |
| 242 | return True |
| 243 | |
| 244 | |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 245 | @dataclasses.dataclass |
| 246 | class Build: |
| 247 | id: str |
| 248 | pid: int |
| 249 | env: dict |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 250 | stdout: IO[str] |
| 251 | cwd: Optional[str] = None |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 252 | _logfile: Optional[IO[str]] = None |
| 253 | _is_ninja_alive: bool = True |
| 254 | _tasks: List[Task] = dataclasses.field(default_factory=list) |
| 255 | _completed_task_count = 0 |
| 256 | _active_process_count = 0 |
| 257 | _lock: threading.RLock = dataclasses.field(default_factory=threading.RLock, |
| 258 | repr=False, |
| 259 | init=False) |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 260 | |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 261 | def __hash__(self): |
| 262 | return hash((self.id, self.pid, self.cwd)) |
| 263 | |
| 264 | def add_task(self, task: Task): |
| 265 | self._status_update(f'QUEUED {task.name}') |
| 266 | with self._lock: |
| 267 | self._tasks.append(task) |
| 268 | TaskStats.add_task() |
| 269 | TaskManager.add_task(task) |
| 270 | |
| 271 | def add_process(self, task: Task): |
| 272 | self._status_update(f'STARTING {task.name}') |
| 273 | with self._lock: |
| 274 | self._active_process_count += 1 |
| 275 | TaskStats.add_process() |
| 276 | |
| 277 | def task_done(self, task: Task, status_string: str): |
| 278 | self._status_update(f'{status_string} {task.name}') |
| 279 | TaskStats.complete_task() |
| 280 | TaskManager.task_done(task) |
| 281 | with self._lock: |
| 282 | self._completed_task_count += 1 |
| 283 | |
| 284 | # We synchronize all terminal title info rather than having it per build |
| 285 | # since if two builds are happening in the same terminal concurrently, both |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 286 | # builds will be overriding each other's titles continuously. Usually we |
| 287 | # only have the one build anyways so it should equivalent in most cases. |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 288 | BuildManager.update_remote_titles() |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 289 | with self._lock: |
| 290 | if not self.is_active(): |
| 291 | self._logfile.close() |
| 292 | # Reset in case its the last build. |
| 293 | BuildManager.update_remote_titles('') |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 294 | |
| 295 | def process_complete(self): |
| 296 | with self._lock: |
| 297 | self._active_process_count -= 1 |
| 298 | TaskStats.remove_process() |
| 299 | |
| 300 | def ensure_logfile(self): |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 301 | with self._lock: |
| 302 | if not self._logfile: |
| 303 | assert self.cwd is not None |
| 304 | self._logfile = LogfileManager.create_logfile(self.id, self.cwd) |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 305 | |
| 306 | def log(self, message: str): |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 307 | with self._lock: |
| 308 | self.ensure_logfile() |
| 309 | if self._logfile.closed: |
| 310 | # BuildManager#broadcast can call log after the build is done and the |
| 311 | # log is closed. Might make sense to separate out that flow so we can |
| 312 | # raise an exception here otherwise. |
| 313 | return |
| 314 | print(message, file=self._logfile, flush=True) |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 315 | |
| 316 | def _status_update(self, status_message): |
| 317 | prefix = f'[{TaskStats.prefix(self.id)}] ' |
| 318 | self.log(f'{prefix}{status_message}') |
| 319 | print_status(prefix, status_message) |
| 320 | |
| 321 | def total_task_count(self): |
| 322 | with self._lock: |
| 323 | return len(self._tasks) |
| 324 | |
| 325 | def completed_task_count(self): |
| 326 | with self._lock: |
| 327 | return self._completed_task_count |
| 328 | |
| 329 | def pending_task_count(self): |
| 330 | with self._lock: |
| 331 | return self.total_task_count() - self.completed_task_count() |
| 332 | |
| 333 | def process_count(self): |
| 334 | with self._lock: |
| 335 | return self._active_process_count |
| 336 | |
| 337 | def is_active(self): |
| 338 | if self.pending_task_count() > 0: |
| 339 | return True |
| 340 | # Ninja is not coming back to life so only check on it if last we checked it |
| 341 | # was still alive. |
| 342 | if self._is_ninja_alive: |
| 343 | self._is_ninja_alive = check_pid_alive(self.pid) |
| 344 | return self._is_ninja_alive |
| 345 | |
| 346 | def query_build_info(self): |
| 347 | current_tasks = TaskManager.get_current_tasks(self.id) |
| 348 | return { |
| 349 | 'build_id': self.id, |
| 350 | 'is_active': self.is_active(), |
| 351 | 'completed_tasks': self.completed_task_count(), |
| 352 | 'pending_tasks': self.pending_task_count(), |
| 353 | 'active_tasks': [t.cmd for t in current_tasks], |
| 354 | 'outdir': self.cwd, |
| 355 | } |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 356 | |
| 357 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 358 | class BuildManager: |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 359 | _builds_by_id: dict[str, Build] = dict() |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 360 | _cached_ttys: dict[(int, int), tuple[IO[str], bool]] = dict() |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 361 | _lock = threading.RLock() |
| 362 | |
| 363 | @classmethod |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 364 | def register_builder(cls, env, pid, cwd): |
| 365 | build_id = env['AUTONINJA_BUILD_ID'] |
| 366 | stdout = cls.open_tty(env['AUTONINJA_STDOUT_NAME']) |
| 367 | # Tells the script not to re-delegate to build server. |
| 368 | env[server_utils.BUILD_SERVER_ENV_VARIABLE] = '1' |
| 369 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 370 | with cls._lock: |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 371 | build = Build(id=build_id, |
| 372 | pid=pid, |
| 373 | cwd=cwd, |
| 374 | env=env, |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 375 | stdout=stdout) |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 376 | cls.maybe_init_cwd(build, cwd) |
| 377 | cls._builds_by_id[build_id] = build |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 378 | cls.update_remote_titles() |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 379 | |
| 380 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 381 | def maybe_init_cwd(cls, build: Build, cwd: str): |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 382 | if cwd is not None: |
| 383 | with cls._lock: |
| 384 | if build.cwd is None: |
| 385 | build.cwd = cwd |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 386 | else: |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 387 | assert pathlib.Path(cwd).samefile( |
| 388 | build.cwd), f'{repr(cwd)} != {repr(build.cwd)}' |
Mohamed Heikal | eb1a1dc | 2025-02-27 17:16:16 | [diff] [blame] | 389 | build.ensure_logfile() |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 390 | |
| 391 | @classmethod |
| 392 | def get_build(cls, build_id): |
| 393 | with cls._lock: |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 394 | return cls._builds_by_id.get(build_id, None) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 395 | |
| 396 | @classmethod |
Mohamed Heikal | 08b467e0 | 2025-01-27 20:54:25 | [diff] [blame] | 397 | def open_tty(cls, tty_path): |
| 398 | # Do not open the same tty multiple times. Use st_ino and st_dev to compare |
| 399 | # file descriptors. |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 400 | tty = open(tty_path, 'at') |
Mohamed Heikal | db4fd9c | 2025-01-29 20:56:27 | [diff] [blame] | 401 | st = os.stat(tty.fileno()) |
Mohamed Heikal | 08b467e0 | 2025-01-27 20:54:25 | [diff] [blame] | 402 | tty_key = (st.st_ino, st.st_dev) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 403 | with cls._lock: |
Mohamed Heikal | 08b467e0 | 2025-01-27 20:54:25 | [diff] [blame] | 404 | # Dedupes ttys |
| 405 | if tty_key not in cls._cached_ttys: |
| 406 | # TTYs are kept open for the lifetime of the server so that broadcast |
| 407 | # messages (e.g. uncaught exceptions) can be sent to them even if they |
| 408 | # are not currently building anything. |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 409 | cls._cached_ttys[tty_key] = (tty, tty.isatty()) |
Mohamed Heikal | db4fd9c | 2025-01-29 20:56:27 | [diff] [blame] | 410 | else: |
| 411 | tty.close() |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 412 | return cls._cached_ttys[tty_key][0] |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 413 | |
| 414 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 415 | def get_active_builds(cls) -> List[Build]: |
| 416 | builds = cls.get_all_builds() |
| 417 | return list(build for build in builds if build.is_active()) |
| 418 | |
| 419 | @classmethod |
| 420 | def get_all_builds(cls) -> List[Build]: |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 421 | with cls._lock: |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 422 | return list(cls._builds_by_id.values()) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 423 | |
| 424 | @classmethod |
| 425 | def broadcast(cls, msg: str): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 426 | with cls._lock: |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 427 | ttys = list(cls._cached_ttys.values()) |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 428 | builds = list(cls._builds_by_id.values()) |
| 429 | if OptionsManager.should_remote_print(): |
| 430 | for tty, _unused in ttys: |
| 431 | try: |
| 432 | tty.write(msg + '\n') |
| 433 | tty.flush() |
| 434 | except BrokenPipeError: |
| 435 | pass |
| 436 | for build in builds: |
| 437 | build.log(msg) |
Mohamed Heikal | d764eca | 2025-01-31 01:06:35 | [diff] [blame] | 438 | # Write to the current terminal if we have not written to it yet. |
| 439 | st = os.stat(sys.stderr.fileno()) |
| 440 | stderr_key = (st.st_ino, st.st_dev) |
| 441 | if stderr_key not in cls._cached_ttys: |
| 442 | print(msg, file=sys.stderr) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 443 | |
| 444 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 445 | def update_remote_titles(cls, new_title=None): |
| 446 | if new_title is None: |
| 447 | if not cls.has_active_builds() and TaskStats.num_pending_tasks() == 0: |
| 448 | # Setting an empty title causes most terminals to go back to the |
| 449 | # default title (and at least prevents the tab title from being |
| 450 | # "Analysis Steps: N/N" forevermore. |
| 451 | new_title = '' |
| 452 | else: |
| 453 | new_title = TaskStats.get_title_message() |
| 454 | |
| 455 | with cls._lock: |
| 456 | ttys = list(cls._cached_ttys.values()) |
| 457 | for tty, isatty in ttys: |
| 458 | if isatty: |
| 459 | try: |
| 460 | tty.write(f'\033]2;{new_title}\007') |
| 461 | tty.flush() |
| 462 | except BrokenPipeError: |
| 463 | pass |
| 464 | |
| 465 | @classmethod |
| 466 | def has_active_builds(cls): |
| 467 | return bool(cls.get_active_builds()) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 468 | |
| 469 | |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 470 | class TaskManager: |
| 471 | """Class to encapsulate a threadsafe queue and handle deactivating it.""" |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 472 | _queue: collections.deque[Task] = collections.deque() |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 473 | _current_tasks: set[Task] = set() |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 474 | _deactivated = False |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 475 | _lock = threading.RLock() |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 476 | |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 477 | @classmethod |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 478 | def add_task(cls, task: Task): |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 479 | assert not cls._deactivated |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 480 | with cls._lock: |
| 481 | cls._queue.appendleft(task) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 482 | cls._maybe_start_tasks() |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 483 | |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 484 | @classmethod |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 485 | def task_done(cls, task: Task): |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 486 | with cls._lock: |
Mohamed Heikal | 651c992 | 2025-01-16 19:12:21 | [diff] [blame] | 487 | cls._current_tasks.discard(task) |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 488 | |
| 489 | @classmethod |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 490 | def get_current_tasks(cls, build_id): |
| 491 | with cls._lock: |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 492 | return [t for t in cls._current_tasks if t.build.id == build_id] |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 493 | |
| 494 | @classmethod |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 495 | def deactivate(cls): |
| 496 | cls._deactivated = True |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 497 | tasks_to_terminate: list[Task] = [] |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 498 | with cls._lock: |
| 499 | while cls._queue: |
| 500 | task = cls._queue.pop() |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 501 | tasks_to_terminate.append(task) |
| 502 | # Cancel possibly running tasks. |
| 503 | tasks_to_terminate.extend(cls._current_tasks) |
| 504 | # Terminate outside lock since task threads need the lock to finish |
| 505 | # terminating. |
| 506 | for task in tasks_to_terminate: |
| 507 | task.terminate() |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 508 | |
| 509 | @classmethod |
| 510 | def cancel_build(cls, build_id): |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 511 | terminated_pending_tasks: list[Task] = [] |
| 512 | terminated_current_tasks: list[Task] = [] |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 513 | with cls._lock: |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 514 | # Cancel pending tasks. |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 515 | for task in cls._queue: |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 516 | if task.build.id == build_id: |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 517 | terminated_pending_tasks.append(task) |
| 518 | for task in terminated_pending_tasks: |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 519 | cls._queue.remove(task) |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 520 | # Cancel running tasks. |
| 521 | for task in cls._current_tasks: |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 522 | if task.build.id == build_id: |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 523 | terminated_current_tasks.append(task) |
| 524 | # Terminate tasks outside lock since task threads need the lock to finish |
| 525 | # terminating. |
| 526 | for task in terminated_pending_tasks: |
| 527 | task.terminate() |
| 528 | for task in terminated_current_tasks: |
| 529 | task.terminate() |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 530 | |
| 531 | @staticmethod |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 532 | # pylint: disable=inconsistent-return-statements |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 533 | def _num_running_processes(): |
| 534 | with open('/proc/stat') as f: |
| 535 | for line in f: |
| 536 | if line.startswith('procs_running'): |
| 537 | return int(line.rstrip().split()[1]) |
| 538 | assert False, 'Could not read /proc/stat' |
| 539 | |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 540 | @classmethod |
| 541 | def _maybe_start_tasks(cls): |
| 542 | if cls._deactivated: |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 543 | return |
| 544 | # Include load avg so that a small dip in the number of currently running |
| 545 | # processes will not cause new tasks to be started while the overall load is |
| 546 | # heavy. |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 547 | cur_load = max(cls._num_running_processes(), os.getloadavg()[0]) |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 548 | num_started = 0 |
| 549 | # Always start a task if we don't have any running, so that all tasks are |
| 550 | # eventually finished. Try starting up tasks when the overall load is light. |
| 551 | # Limit to at most 2 new tasks to prevent ramping up too fast. There is a |
| 552 | # chance where multiple threads call _maybe_start_tasks and each gets to |
| 553 | # spawn up to 2 new tasks, but since the only downside is some build tasks |
| 554 | # get worked on earlier rather than later, it is not worth mitigating. |
| 555 | while num_started < 2 and (TaskStats.no_running_processes() |
| 556 | or num_started + cur_load < os.cpu_count()): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 557 | with cls._lock: |
| 558 | try: |
| 559 | next_task = cls._queue.pop() |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 560 | cls._current_tasks.add(next_task) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 561 | except IndexError: |
| 562 | return |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 563 | num_started += next_task.start(cls._maybe_start_tasks) |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 564 | |
| 565 | |
| 566 | # TODO(wnwen): Break this into Request (encapsulating what ninja sends) and Task |
| 567 | # when a Request starts to be run. This would eliminate ambiguity |
| 568 | # about when and whether _proc/_thread are initialized. |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 569 | class Task: |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 570 | """Class to represent one task and operations on it.""" |
| 571 | |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 572 | def __init__(self, name: str, build: Build, cmd: List[str], |
| 573 | stamp_file: Optional[str]): |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 574 | self.name = name |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 575 | self.build = build |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 576 | self.cmd = cmd |
| 577 | self.stamp_file = stamp_file |
| 578 | self._terminated = False |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 579 | self._replaced = False |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 580 | self._lock = threading.RLock() |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 581 | self._proc: Optional[subprocess.Popen] = None |
| 582 | self._thread: Optional[threading.Thread] = None |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 583 | self._delete_stamp_thread: Optional[threading.Thread] = None |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 584 | self._return_code: Optional[int] = None |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 585 | |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 586 | @property |
| 587 | def key(self): |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 588 | return (self.build.cwd, self.name) |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 589 | |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 590 | def __hash__(self): |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 591 | return hash((self.key, self.build.id)) |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 592 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 593 | def __eq__(self, other): |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 594 | return self.key == other.key and self.build is other.build |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 595 | |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 596 | def start(self, on_complete_callback: Callable[[], None]) -> int: |
| 597 | """Starts the task if it has not already been terminated. |
| 598 | |
| 599 | Returns the number of processes that have been started. This is called at |
| 600 | most once when the task is popped off the task queue.""" |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 601 | with self._lock: |
| 602 | if self._terminated: |
| 603 | return 0 |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 604 | |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 605 | # Use os.nice(19) to ensure the lowest priority (idle) for these analysis |
| 606 | # tasks since we want to avoid slowing down the actual build. |
| 607 | # TODO(wnwen): Use ionice to reduce resource consumption. |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 608 | self.build.add_process(self) |
Peter Wen | 1cdf05d8 | 2022-04-05 17:31:23 | [diff] [blame] | 609 | # This use of preexec_fn is sufficiently simple, just one os.nice call. |
| 610 | # pylint: disable=subprocess-popen-preexec-fn |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 611 | self._proc = subprocess.Popen( |
| 612 | self.cmd, |
| 613 | stdout=subprocess.PIPE, |
| 614 | stderr=subprocess.STDOUT, |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 615 | cwd=self.build.cwd, |
| 616 | env=self.build.env, |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 617 | text=True, |
| 618 | preexec_fn=lambda: os.nice(19), |
| 619 | ) |
| 620 | self._thread = threading.Thread( |
| 621 | target=self._complete_when_process_finishes, |
| 622 | args=(on_complete_callback, )) |
| 623 | self._thread.start() |
| 624 | return 1 |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 625 | |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 626 | def terminate(self, replaced=False): |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 627 | """Can be called multiple times to cancel and ignore the task's output.""" |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 628 | with self._lock: |
| 629 | if self._terminated: |
| 630 | return |
| 631 | self._terminated = True |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 632 | self._replaced = replaced |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 633 | |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 634 | # It is safe to access _proc and _thread outside of _lock since they are |
| 635 | # only changed by self.start holding _lock when self._terminate is false. |
| 636 | # Since we have just set self._terminate to true inside of _lock, we know |
| 637 | # that neither _proc nor _thread will be changed from this point onwards. |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 638 | if self._proc: |
| 639 | self._proc.terminate() |
| 640 | self._proc.wait() |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 641 | # Ensure that self._complete is called either by the thread or by us. |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 642 | if self._thread: |
| 643 | self._thread.join() |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 644 | else: |
| 645 | self._complete() |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 646 | |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 647 | def _complete_when_process_finishes(self, |
| 648 | on_complete_callback: Callable[[], None]): |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 649 | assert self._proc |
| 650 | # We know Popen.communicate will return a str and not a byte since it is |
| 651 | # constructed with text=True. |
| 652 | stdout: str = self._proc.communicate()[0] |
| 653 | self._return_code = self._proc.returncode |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 654 | self.build.process_complete() |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 655 | self._complete(stdout) |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 656 | on_complete_callback() |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 657 | |
Peter Wen | cd460ff5 | 2021-02-23 22:40:05 | [diff] [blame] | 658 | def _complete(self, stdout: str = ''): |
| 659 | """Update the user and ninja after the task has run or been terminated. |
| 660 | |
| 661 | This method should only be run once per task. Avoid modifying the task so |
| 662 | that this method does not need locking.""" |
| 663 | |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 664 | delete_stamp = False |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 665 | status_string = 'FINISHED' |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 666 | if self._terminated: |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 667 | status_string = 'TERMINATED' |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 668 | # When tasks are replaced, avoid deleting the stamp file, context: |
| 669 | # https://issuetracker.google.com/301961827. |
| 670 | if not self._replaced: |
| 671 | delete_stamp = True |
| 672 | elif stdout or self._return_code != 0: |
| 673 | status_string = 'FAILED' |
| 674 | delete_stamp = True |
| 675 | preamble = [ |
| 676 | f'FAILED: {self.name}', |
| 677 | f'Return code: {self._return_code}', |
Andrew Grieve | 38c8046 | 2024-12-17 21:33:27 | [diff] [blame] | 678 | 'CMD: ' + shlex.join(self.cmd), |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 679 | 'STDOUT:', |
| 680 | ] |
| 681 | |
| 682 | message = '\n'.join(preamble + [stdout]) |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 683 | self.build.log(message) |
| 684 | server_log(message) |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 685 | |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 686 | if OptionsManager.should_remote_print(): |
| 687 | # Add emoji to show that output is from the build server. |
| 688 | preamble = [f'⏩ {line}' for line in preamble] |
| 689 | remote_message = '\n'.join(preamble + [stdout]) |
| 690 | # Add a new line at start of message to clearly delineate from previous |
| 691 | # output/text already on the remote tty we are printing to. |
| 692 | self.build.stdout.write(f'\n{remote_message}') |
| 693 | self.build.stdout.flush() |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 694 | if delete_stamp and self.stamp_file: |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 695 | # Force siso to consider failed targets as dirty. |
| 696 | try: |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 697 | os.unlink(os.path.join(self.build.cwd, self.stamp_file)) |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 698 | except FileNotFoundError: |
| 699 | pass |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 700 | self.build.task_done(self, status_string) |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 701 | |
| 702 | |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 703 | def _handle_add_task(data, current_tasks: Dict[Tuple[str, str], Task]): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 704 | """Handle messages of type ADD_TASK.""" |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 705 | build_id = data['build_id'] |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 706 | build = BuildManager.get_build(build_id) |
| 707 | BuildManager.maybe_init_cwd(build, data.get('cwd')) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 708 | |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 709 | cmd = data['cmd'] |
| 710 | name = data.get('name') or shlex.join(cmd) |
| 711 | new_task = Task(name=name, |
| 712 | cmd=cmd, |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 713 | build=build, |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 714 | stamp_file=data['stamp_file']) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 715 | existing_task = current_tasks.get(new_task.key) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 716 | if existing_task: |
Mohamed Heikal | 9984e43 | 2024-12-03 18:21:40 | [diff] [blame] | 717 | existing_task.terminate(replaced=True) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 718 | current_tasks[new_task.key] = new_task |
| 719 | |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 720 | build.add_task(new_task) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 721 | |
| 722 | |
| 723 | def _handle_query_build(data, connection: socket.socket): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 724 | """Handle messages of type QUERY_BUILD.""" |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 725 | build_id = data['build_id'] |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 726 | response = TaskStats.query_build(build_id) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 727 | try: |
| 728 | with connection: |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 729 | server_utils.SendMessage(connection, response) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 730 | except BrokenPipeError: |
| 731 | # We should not die because the client died. |
| 732 | pass |
| 733 | |
| 734 | |
| 735 | def _handle_heartbeat(connection: socket.socket): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 736 | """Handle messages of type POLL_HEARTBEAT.""" |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 737 | try: |
| 738 | with connection: |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 739 | server_utils.SendMessage(connection, { |
| 740 | 'status': 'OK', |
| 741 | 'pid': os.getpid(), |
| 742 | }) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 743 | except BrokenPipeError: |
| 744 | # We should not die because the client died. |
| 745 | pass |
| 746 | |
| 747 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 748 | def _handle_register_builder(data): |
| 749 | """Handle messages of type REGISTER_BUILDER.""" |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 750 | env = data['env'] |
| 751 | pid = int(data['builder_pid']) |
| 752 | cwd = data['cwd'] |
| 753 | |
| 754 | BuildManager.register_builder(env, pid, cwd) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 755 | |
| 756 | |
| 757 | def _handle_cancel_build(data): |
| 758 | """Handle messages of type CANCEL_BUILD.""" |
| 759 | build_id = data['build_id'] |
| 760 | TaskManager.cancel_build(build_id) |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 761 | BuildManager.update_remote_titles('') |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 762 | |
| 763 | |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 764 | def _handle_stop_server(): |
| 765 | """Handle messages of type STOP_SERVER.""" |
| 766 | server_log('STOPPING SERVER...') |
| 767 | TaskManager.deactivate() |
| 768 | server_log('STOPPED') |
| 769 | sys.exit(0) |
| 770 | |
| 771 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 772 | def _listen_for_request_data(sock: socket.socket): |
| 773 | """Helper to encapsulate getting a new message.""" |
| 774 | while True: |
| 775 | conn = sock.accept()[0] |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 776 | message = server_utils.ReceiveMessage(conn) |
| 777 | if message: |
| 778 | yield message, conn |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 779 | |
| 780 | |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 781 | def _register_cleanup_signal_handlers(): |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 782 | original_sigint_handler = signal.getsignal(signal.SIGINT) |
| 783 | original_sigterm_handler = signal.getsignal(signal.SIGTERM) |
| 784 | |
| 785 | def _cleanup(signum, frame): |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 786 | server_log('STOPPING SERVER...') |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 787 | # Gracefully shut down the task manager, terminating all queued tasks. |
| 788 | TaskManager.deactivate() |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 789 | server_log('STOPPED') |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 790 | if signum == signal.SIGINT: |
| 791 | if callable(original_sigint_handler): |
| 792 | original_sigint_handler(signum, frame) |
| 793 | else: |
| 794 | raise KeyboardInterrupt() |
| 795 | if signum == signal.SIGTERM: |
| 796 | # Sometimes sigterm handler is not a callable. |
| 797 | if callable(original_sigterm_handler): |
| 798 | original_sigterm_handler(signum, frame) |
| 799 | else: |
| 800 | sys.exit(1) |
| 801 | |
| 802 | signal.signal(signal.SIGINT, _cleanup) |
| 803 | signal.signal(signal.SIGTERM, _cleanup) |
| 804 | |
| 805 | |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 806 | def _process_requests(sock: socket.socket, exit_on_idle: bool): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 807 | """Main loop for build server receiving request messages.""" |
Peter Wen | 6e7e52b | 2021-02-13 02:39:28 | [diff] [blame] | 808 | # Since dicts in python can contain anything, explicitly type tasks to help |
| 809 | # make static type checking more useful. |
| 810 | tasks: Dict[Tuple[str, str], Task] = {} |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 811 | server_log(f'Server started. PID={os.getpid()}') |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 812 | _register_cleanup_signal_handlers() |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 813 | # pylint: disable=too-many-nested-blocks |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 814 | while True: |
| 815 | try: |
| 816 | for data, connection in _listen_for_request_data(sock): |
| 817 | message_type = data.get('message_type', server_utils.ADD_TASK) |
| 818 | if message_type == server_utils.POLL_HEARTBEAT: |
| 819 | _handle_heartbeat(connection) |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 820 | elif message_type == server_utils.ADD_TASK: |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 821 | connection.close() |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 822 | _handle_add_task(data, tasks) |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 823 | elif message_type == server_utils.QUERY_BUILD: |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 824 | _handle_query_build(data, connection) |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 825 | elif message_type == server_utils.REGISTER_BUILDER: |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 826 | connection.close() |
| 827 | _handle_register_builder(data) |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 828 | elif message_type == server_utils.CANCEL_BUILD: |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 829 | connection.close() |
| 830 | _handle_cancel_build(data) |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 831 | elif message_type == server_utils.STOP_SERVER: |
| 832 | connection.close() |
| 833 | _handle_stop_server() |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 834 | else: |
| 835 | connection.close() |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 836 | except TimeoutError: |
| 837 | # If we have not received a new task in a while and do not have any |
| 838 | # pending tasks or running builds, then exit. Otherwise keep waiting. |
| 839 | if (TaskStats.num_pending_tasks() == 0 |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 840 | and not BuildManager.has_active_builds() and exit_on_idle): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 841 | break |
Mohamed Heikal | abf646e | 2024-12-12 16:06:05 | [diff] [blame] | 842 | except KeyboardInterrupt: |
| 843 | break |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 844 | BuildManager.update_remote_titles('') |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 845 | |
| 846 | |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 847 | def query_build_info(build_id=None): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 848 | """Communicates with the main server to query build info.""" |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 849 | return _send_message_with_response({ |
| 850 | 'message_type': server_utils.QUERY_BUILD, |
| 851 | 'build_id': build_id, |
| 852 | }) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 853 | |
| 854 | |
| 855 | def _wait_for_build(build_id): |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 856 | """Comunicates with the main server waiting for a build to complete.""" |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 857 | start_time = datetime.datetime.now() |
| 858 | while True: |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 859 | try: |
| 860 | build_info = query_build_info(build_id)['builds'][0] |
| 861 | except ConnectionRefusedError: |
| 862 | print('No server running. It likely finished all tasks.') |
| 863 | print('You can check $OUTDIR/buildserver.log.0 to be sure.') |
| 864 | return 0 |
| 865 | |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 866 | pending_tasks = build_info['pending_tasks'] |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 867 | |
| 868 | if pending_tasks == 0: |
| 869 | print(f'\nAll tasks completed for build_id: {build_id}.') |
| 870 | return 0 |
| 871 | |
| 872 | current_time = datetime.datetime.now() |
| 873 | duration = current_time - start_time |
| 874 | print(f'\rWaiting for {pending_tasks} tasks [{str(duration)}]\033[K', |
| 875 | end='', |
| 876 | flush=True) |
| 877 | time.sleep(1) |
| 878 | |
| 879 | |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 880 | def _wait_for_idle(): |
| 881 | """Communicates with the main server waiting for all builds to complete.""" |
| 882 | start_time = datetime.datetime.now() |
| 883 | while True: |
| 884 | try: |
| 885 | builds = query_build_info()['builds'] |
| 886 | except ConnectionRefusedError: |
| 887 | print('No server running. It likely finished all tasks.') |
| 888 | print('You can check $OUTDIR/buildserver.log.0 to be sure.') |
| 889 | return 0 |
| 890 | |
| 891 | all_pending_tasks = 0 |
| 892 | all_completed_tasks = 0 |
| 893 | for build_info in builds: |
| 894 | pending_tasks = build_info['pending_tasks'] |
| 895 | completed_tasks = build_info['completed_tasks'] |
| 896 | active = build_info['is_active'] |
| 897 | # Ignore completed builds. |
| 898 | if active or pending_tasks: |
| 899 | all_pending_tasks += pending_tasks |
| 900 | all_completed_tasks += completed_tasks |
| 901 | total_tasks = all_pending_tasks + all_completed_tasks |
| 902 | |
| 903 | if all_pending_tasks == 0: |
| 904 | print('\nServer Idle, All tasks complete.') |
| 905 | return 0 |
| 906 | |
| 907 | current_time = datetime.datetime.now() |
| 908 | duration = current_time - start_time |
| 909 | print( |
| 910 | f'\rWaiting for {all_pending_tasks} remaining tasks. ' |
| 911 | f'({all_completed_tasks}/{total_tasks} tasks complete) ' |
| 912 | f'[{str(duration)}]\033[K', |
| 913 | end='', |
| 914 | flush=True) |
| 915 | time.sleep(0.5) |
| 916 | |
| 917 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 918 | def _send_message_and_close(message_dict): |
| 919 | with contextlib.closing(socket.socket(socket.AF_UNIX)) as sock: |
| 920 | sock.connect(server_utils.SOCKET_ADDRESS) |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 921 | sock.settimeout(1) |
| 922 | server_utils.SendMessage(sock, message_dict) |
| 923 | |
| 924 | |
| 925 | def _send_message_with_response(message_dict): |
| 926 | with contextlib.closing(socket.socket(socket.AF_UNIX)) as sock: |
| 927 | sock.connect(server_utils.SOCKET_ADDRESS) |
| 928 | sock.settimeout(1) |
| 929 | server_utils.SendMessage(sock, message_dict) |
| 930 | return server_utils.ReceiveMessage(sock) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 931 | |
| 932 | |
| 933 | def _send_cancel_build(build_id): |
| 934 | _send_message_and_close({ |
| 935 | 'message_type': server_utils.CANCEL_BUILD, |
| 936 | 'build_id': build_id, |
| 937 | }) |
| 938 | return 0 |
| 939 | |
| 940 | |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 941 | def _send_stop_server(): |
| 942 | try: |
| 943 | _send_message_and_close({ |
| 944 | 'message_type': server_utils.STOP_SERVER, |
| 945 | }) |
| 946 | except socket.error as e: |
| 947 | if e.errno == 111: |
| 948 | sys.stderr.write('No running build server found.\n') |
| 949 | return 1 |
| 950 | raise |
| 951 | return 0 |
| 952 | |
| 953 | |
| 954 | def _register_build(builder_pid, output_directory): |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 955 | if output_directory is not None: |
| 956 | output_directory = str(pathlib.Path(output_directory).absolute()) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 957 | for _attempt in range(3): |
| 958 | try: |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 959 | # Ensure environment variables that the server expects to be there are |
| 960 | # present. |
| 961 | server_utils.AssertEnvironmentVariables() |
| 962 | |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 963 | _send_message_and_close({ |
| 964 | 'message_type': server_utils.REGISTER_BUILDER, |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 965 | 'env': dict(os.environ), |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 966 | 'builder_pid': builder_pid, |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 967 | 'cwd': output_directory, |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 968 | }) |
| 969 | return 0 |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 970 | except OSError: |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 971 | time.sleep(0.05) |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 972 | print('Failed to register build. No server running?') |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 973 | return 1 |
| 974 | |
| 975 | |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 976 | def poll_server(retries=3): |
| 977 | """Communicates with the main server to query build info.""" |
| 978 | for _attempt in range(retries): |
| 979 | try: |
| 980 | response = _send_message_with_response( |
| 981 | {'message_type': server_utils.POLL_HEARTBEAT}) |
| 982 | if response: |
| 983 | break |
| 984 | except OSError: |
| 985 | time.sleep(0.05) |
| 986 | else: |
| 987 | return None |
| 988 | return response['pid'] |
| 989 | |
| 990 | |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 991 | def _print_build_status_all(): |
| 992 | try: |
| 993 | query_data = query_build_info(None) |
| 994 | except ConnectionRefusedError: |
| 995 | print('No server running. Consult $OUTDIR/buildserver.log.0') |
| 996 | return 0 |
| 997 | builds = query_data['builds'] |
| 998 | pid = query_data['pid'] |
| 999 | all_active_tasks = [] |
| 1000 | print(f'Build server (PID={pid}) has {len(builds)} registered builds') |
| 1001 | for build_info in builds: |
| 1002 | build_id = build_info['build_id'] |
| 1003 | pending_tasks = build_info['pending_tasks'] |
| 1004 | completed_tasks = build_info['completed_tasks'] |
| 1005 | active_tasks = build_info['active_tasks'] |
| 1006 | out_dir = build_info['outdir'] |
| 1007 | active = build_info['is_active'] |
| 1008 | total_tasks = pending_tasks + completed_tasks |
| 1009 | all_active_tasks += active_tasks |
| 1010 | if total_tasks == 0 and not active: |
| 1011 | status = 'Finished without any jobs' |
| 1012 | else: |
| 1013 | if active: |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 1014 | status = 'Main build is still running' |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1015 | else: |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 1016 | status = 'Main build completed' |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1017 | if out_dir: |
| 1018 | status += f' in {out_dir}' |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 1019 | status += f'. Tasks completed: {completed_tasks}/{total_tasks}' |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1020 | if completed_tasks < total_tasks: |
Andrew Grieve | 2f123a0 | 2025-03-20 18:13:06 | [diff] [blame] | 1021 | status += f' {len(active_tasks)} task(s) currently executing' |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1022 | print(f'{build_id}: {status}') |
| 1023 | if all_active_tasks: |
| 1024 | total = len(all_active_tasks) |
| 1025 | to_show = min(4, total) |
| 1026 | print(f'Currently executing (showing {to_show} of {total}):') |
| 1027 | for cmd in sorted(all_active_tasks)[:to_show]: |
| 1028 | truncated = shlex.join(cmd) |
| 1029 | if len(truncated) > 200: |
| 1030 | truncated = truncated[:200] + '...' |
| 1031 | print(truncated) |
| 1032 | return 0 |
| 1033 | |
| 1034 | |
Mohamed Heikal | 6b56cf6 | 2024-12-10 23:14:55 | [diff] [blame] | 1035 | def _print_build_status(build_id): |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 1036 | server_path = os.path.relpath(str(server_utils.SERVER_SCRIPT)) |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1037 | try: |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 1038 | builds = query_build_info(build_id)['builds'] |
| 1039 | if not builds: |
Mohamed Heikal | d6809655 | 2025-03-18 19:54:51 | [diff] [blame] | 1040 | print(f'⚠️ No build found with id ({build_id})') |
| 1041 | print('⚠️ To see the status of all builds:', |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 1042 | shlex.join([server_path, '--print-status-all'])) |
| 1043 | return 1 |
| 1044 | build_info = builds[0] |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1045 | except ConnectionRefusedError: |
Mohamed Heikal | d6809655 | 2025-03-18 19:54:51 | [diff] [blame] | 1046 | print('⚠️ No server running. Consult $OUTDIR/buildserver.log.0') |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1047 | return 0 |
Mohamed Heikal | 6b56cf6 | 2024-12-10 23:14:55 | [diff] [blame] | 1048 | pending_tasks = build_info['pending_tasks'] |
Mohamed Heikal | 6b56cf6 | 2024-12-10 23:14:55 | [diff] [blame] | 1049 | |
Andrew Grieve | 2f123a0 | 2025-03-20 18:13:06 | [diff] [blame] | 1050 | # Print nothing unless there are still pending tasks |
| 1051 | if pending_tasks: |
| 1052 | is_str = 'is' if pending_tasks == 1 else 'are' |
| 1053 | job_str = 'job' if pending_tasks == 1 else 'jobs' |
| 1054 | print(f'⏩ There {is_str} still {pending_tasks} static analysis {job_str}' |
| 1055 | ' running in the background.') |
| 1056 | print('⏩ To wait for them:', shlex.join([server_path, '--wait-for-idle'])) |
Mohamed Heikal | 6b56cf6 | 2024-12-10 23:14:55 | [diff] [blame] | 1057 | return 0 |
| 1058 | |
| 1059 | |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 1060 | def _start_server(exit_on_idle): |
| 1061 | sys.excepthook = _exception_hook |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 1062 | with socket.socket(socket.AF_UNIX) as sock: |
| 1063 | sock.settimeout(_SOCKET_TIMEOUT) |
| 1064 | try: |
| 1065 | sock.bind(server_utils.SOCKET_ADDRESS) |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 1066 | except OSError as e: |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 1067 | # errno 98 is Address already in use |
| 1068 | if e.errno == 98: |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 1069 | if not OptionsManager.is_quiet(): |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 1070 | pid = poll_server() |
| 1071 | print(f'Another instance is already running (pid: {pid}).', |
| 1072 | file=sys.stderr) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 1073 | return 1 |
| 1074 | raise |
| 1075 | sock.listen() |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 1076 | _process_requests(sock, exit_on_idle) |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 1077 | return 0 |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 1078 | |
| 1079 | |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 1080 | def _add_task(cmd): |
| 1081 | build_id = f'default-{time.time()}' |
| 1082 | os.environ['AUTONINJA_BUILD_ID'] = build_id |
| 1083 | tty = os.readlink('/proc/self/fd/1') |
| 1084 | if os.path.exists(tty): |
| 1085 | os.environ['AUTONINJA_STDOUT_NAME'] = tty |
| 1086 | else: |
| 1087 | os.environ['AUTONINJA_STDOUT_NAME'] = '/dev/null' |
| 1088 | |
| 1089 | if code := _register_build(os.getpid(), os.getcwd()): |
| 1090 | return code |
| 1091 | |
| 1092 | try: |
| 1093 | _send_message_and_close({ |
| 1094 | 'name': None, |
| 1095 | 'message_type': server_utils.ADD_TASK, |
| 1096 | 'cmd': cmd, |
| 1097 | 'cwd': os.getcwd(), |
| 1098 | 'build_id': build_id, |
| 1099 | 'stamp_file': None, |
| 1100 | }) |
| 1101 | return 0 |
| 1102 | except socket.error as e: |
| 1103 | if e.errno == 111: |
| 1104 | sys.stderr.write('No running build server found.\n') |
| 1105 | return 1 |
| 1106 | raise |
| 1107 | |
| 1108 | |
| 1109 | def _main_old(): |
Peter Wen | f409c0c | 2021-02-09 19:33:02 | [diff] [blame] | 1110 | parser = argparse.ArgumentParser(description=__doc__) |
Peter Wen | d70f486 | 2022-02-02 16:00:16 | [diff] [blame] | 1111 | parser.add_argument( |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 1112 | '--exit-on-idle', |
| 1113 | action='store_true', |
| 1114 | help='Server started on demand. Exit when all tasks run out.') |
| 1115 | parser.add_argument('--quiet', |
| 1116 | action='store_true', |
| 1117 | help='Do not output status updates.') |
Mohamed Heikal | f73b717a | 2025-02-12 15:53:07 | [diff] [blame] | 1118 | parser.add_argument('--no-remote-print', |
| 1119 | action='store_true', |
| 1120 | help='Do not output errors to remote terminals.') |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 1121 | parser.add_argument('--wait-for-build', |
| 1122 | metavar='BUILD_ID', |
| 1123 | help='Wait for build server to finish with all tasks ' |
| 1124 | 'for BUILD_ID and output any pending messages.') |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 1125 | parser.add_argument('--wait-for-idle', |
| 1126 | action='store_true', |
| 1127 | help='Wait for build server to finish with all ' |
| 1128 | 'pending tasks.') |
Mohamed Heikal | 6b56cf6 | 2024-12-10 23:14:55 | [diff] [blame] | 1129 | parser.add_argument('--print-status', |
| 1130 | metavar='BUILD_ID', |
| 1131 | help='Print the current state of a build.') |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1132 | parser.add_argument('--print-status-all', |
| 1133 | action='store_true', |
| 1134 | help='Print the current state of all active builds.') |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 1135 | parser.add_argument( |
| 1136 | '--register-build-id', |
| 1137 | metavar='BUILD_ID', |
| 1138 | help='Inform the build server that a new build has started.') |
Andrew Grieve | 0d6e8a75 | 2025-02-05 21:20:50 | [diff] [blame] | 1139 | parser.add_argument('--output-directory', |
| 1140 | help='Build directory (use with --register-build-id)') |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 1141 | parser.add_argument('--builder-pid', |
| 1142 | help='Builder process\'s pid for build BUILD_ID.') |
| 1143 | parser.add_argument('--cancel-build', |
| 1144 | metavar='BUILD_ID', |
| 1145 | help='Cancel all pending and running tasks for BUILD_ID.') |
Peter Wen | d70f486 | 2022-02-02 16:00:16 | [diff] [blame] | 1146 | args = parser.parse_args() |
Mohamed Heikal | 3b8c955 | 2025-02-11 22:33:40 | [diff] [blame] | 1147 | |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 1148 | OptionsManager.set_options(quiet=args.quiet, |
| 1149 | should_remote_print=not args.no_remote_print) |
| 1150 | |
Mohamed Heikal | f746b57f | 2024-11-13 21:20:17 | [diff] [blame] | 1151 | if args.wait_for_build: |
| 1152 | return _wait_for_build(args.wait_for_build) |
Mohamed Heikal | f11b6f3 | 2025-01-30 19:44:29 | [diff] [blame] | 1153 | if args.wait_for_idle: |
| 1154 | return _wait_for_idle() |
Mohamed Heikal | 6b56cf6 | 2024-12-10 23:14:55 | [diff] [blame] | 1155 | if args.print_status: |
| 1156 | return _print_build_status(args.print_status) |
Andrew Grieve | d863d0f | 2024-12-13 20:13:01 | [diff] [blame] | 1157 | if args.print_status_all: |
| 1158 | return _print_build_status_all() |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 1159 | if args.register_build_id: |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 1160 | return _register_build(args.builder_pid, args.output_directory) |
Mohamed Heikal | b752b77 | 2024-11-25 23:05:44 | [diff] [blame] | 1161 | if args.cancel_build: |
| 1162 | return _send_cancel_build(args.cancel_build) |
Andrew Grieve | f12da2c6 | 2025-06-24 14:07:51 | [diff] [blame] | 1163 | return _start_server(args.exit_on_idle) |
| 1164 | |
| 1165 | |
| 1166 | def _main_new(): |
| 1167 | parser = argparse.ArgumentParser(description=__doc__) |
| 1168 | sub_parsers = parser.add_subparsers(dest='command') |
| 1169 | |
| 1170 | sub_parser = sub_parsers.add_parser('start', help='Start the server') |
| 1171 | sub_parser.add_argument('--quiet', |
| 1172 | action='store_true', |
| 1173 | help='Do not output status updates.') |
| 1174 | sub_parser.add_argument('--no-remote-print', |
| 1175 | action='store_true', |
| 1176 | help='Do not output errors to remote terminals.') |
| 1177 | sub_parser.add_argument( |
| 1178 | '--exit-on-idle', |
| 1179 | action='store_true', |
| 1180 | help='Server started on demand. Exit when all tasks run out.') |
| 1181 | |
| 1182 | sub_parser = sub_parsers.add_parser('stop', |
| 1183 | help='Stops the server if it is running') |
| 1184 | |
| 1185 | sub_parser = sub_parsers.add_parser( |
| 1186 | 'register-build', help='Tell a running server about a new ninja session') |
| 1187 | sub_parser.add_argument('--output-directory', |
| 1188 | required=True, |
| 1189 | help='CWD for the build') |
| 1190 | sub_parser.add_argument('--builder-pid', |
| 1191 | required=True, |
| 1192 | help='Builder process\'s PID.') |
| 1193 | |
| 1194 | sub_parser = sub_parsers.add_parser( |
| 1195 | 'unregister-build', |
| 1196 | help='Tell a running server a ninja session has finished') |
| 1197 | sub_parser.add_argument('--build-id', |
| 1198 | required=True, |
| 1199 | help='The AUTONINJA_BUILD_ID') |
| 1200 | sub_parser.add_argument('--verbose', |
| 1201 | action='store_true', |
| 1202 | help='Print status if jobs exist.') |
| 1203 | sub_parser.add_argument('--cancel-jobs', |
| 1204 | action='store_true', |
| 1205 | help='Cancel pending jobs') |
| 1206 | |
| 1207 | sub_parser = sub_parsers.add_parser('status', help='Print status and exit') |
| 1208 | sub_parser.add_argument('--build-id', |
| 1209 | help='The AUTONINJA_BUILD_ID of the session to query ' |
| 1210 | '(otherwise prints all sessions).') |
| 1211 | |
| 1212 | sub_parser = sub_parsers.add_parser('wait', help='Wait for jobs to complete') |
| 1213 | sub_parser.add_argument( |
| 1214 | '--build-id', |
| 1215 | help='The AUTONINJA_BUILD_ID of the session to wait for ' |
| 1216 | '(otherwise waits for all sessions).') |
| 1217 | |
| 1218 | sub_parser = sub_parsers.add_parser('run', help='Adds a task.') |
| 1219 | sub_parser.add_argument('cmd', nargs='+', help='The command to run') |
| 1220 | |
| 1221 | args = parser.parse_args() |
| 1222 | |
| 1223 | ret = 0 |
| 1224 | if args.command == 'start': |
| 1225 | OptionsManager.set_options(quiet=args.quiet, |
| 1226 | should_remote_print=not args.no_remote_print) |
| 1227 | ret = _start_server(args.exit_on_idle) |
| 1228 | elif args.command == 'stop': |
| 1229 | ret = _send_stop_server() |
| 1230 | elif args.command == 'register-build': |
| 1231 | ret = _register_build(args.builder_pid, args.output_directory) |
| 1232 | elif args.command == 'unregister-build': |
| 1233 | if args.verbose: |
| 1234 | ret = _print_build_status(args.build_id) |
| 1235 | if args.cancel_jobs: |
| 1236 | ret = _send_cancel_build(args.cancel_build) |
| 1237 | elif args.command == 'status': |
| 1238 | if args.build_id: |
| 1239 | ret = _print_build_status(args.build_id) |
| 1240 | else: |
| 1241 | ret = _print_build_status_all() |
| 1242 | elif args.command == 'wait': |
| 1243 | if args.build_id: |
| 1244 | ret = _wait_for_build(args.build_id) |
| 1245 | else: |
| 1246 | ret = _wait_for_idle() |
| 1247 | elif args.command == 'run': |
| 1248 | ret = _add_task(args.cmd) |
| 1249 | else: |
| 1250 | parser.print_help() |
| 1251 | return 1 |
| 1252 | return ret |
| 1253 | |
| 1254 | |
| 1255 | def main(): |
| 1256 | if len(sys.argv) <= 1 or not sys.argv[1].startswith('-'): |
| 1257 | return _main_new() |
| 1258 | return _main_old() |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 1259 | |
| 1260 | |
| 1261 | if __name__ == '__main__': |
Peter Wen | b1f3b1d | 2021-02-02 21:30:20 | [diff] [blame] | 1262 | sys.exit(main()) |