|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import dataclasses |
| 5 | +import json |
| 6 | +import requests |
| 7 | + |
| 8 | +from enum import Enum |
| 9 | +from typing import Any, Dict, List, Final, Set, Union, Optional |
| 10 | + |
| 11 | +MANAGED_OWNER: Final[str] = "kernel-patches" |
| 12 | +MANAGED_REPOS: Final[Set[str]] = { |
| 13 | + f"{MANAGED_OWNER}/bpf", |
| 14 | + f"{MANAGED_OWNER}/vmtest", |
| 15 | +} |
| 16 | + |
| 17 | +DEFAULT_SELF_HOSTED_RUNNER_TAGS: Final[List[str]] = ["self-hosted", "docker-noble-main"] |
| 18 | +DEFAULT_GITHUB_HOSTED_RUNNER: Final[str] = "ubuntu-24.04" |
| 19 | +DEFAULT_GCC_VERSION: Final[int] = 14 |
| 20 | +DEFAULT_LLVM_VERSION: Final[int] = 18 |
| 21 | + |
| 22 | +RUNNERS_BUSY_THRESHOLD: Final[float] = 0.8 |
| 23 | + |
| 24 | + |
| 25 | +class Arch(str, Enum): |
| 26 | + """ |
| 27 | + CPU architecture supported by CI. |
| 28 | + """ |
| 29 | + |
| 30 | + AARCH64 = "aarch64" |
| 31 | + S390X = "s390x" |
| 32 | + X86_64 = "x86_64" |
| 33 | + |
| 34 | + |
| 35 | +class Compiler(str, Enum): |
| 36 | + GCC = "gcc" |
| 37 | + LLVM = "llvm" |
| 38 | + |
| 39 | + |
| 40 | +def query_runners_from_github() -> List[Dict[str, Any]]: |
| 41 | + if "GITHUB_TOKEN" not in os.environ: |
| 42 | + return [] |
| 43 | + token = os.environ["GITHUB_TOKEN"] |
| 44 | + headers = { |
| 45 | + "Authorization": f"token {token}", |
| 46 | + "Accept": "application/vnd.github.v3+json", |
| 47 | + } |
| 48 | + owner = os.environ["GITHUB_REPOSITORY_OWNER"] |
| 49 | + url: Optional[str] = f"https://api.github.com/orgs/{owner}/actions/runners" |
| 50 | + # GitHub returns 30 runners per page, fetch all |
| 51 | + all_runners = [] |
| 52 | + try: |
| 53 | + while url is not None: |
| 54 | + response = requests.get(url, headers=headers) |
| 55 | + if response.status_code != 200: |
| 56 | + print(f"Failed to query runners: {response.status_code}") |
| 57 | + print(f"response: {response.text}") |
| 58 | + return [] |
| 59 | + data = response.json() |
| 60 | + all_runners.extend(data.get("runners", [])) |
| 61 | + # Check for next page URL in Link header |
| 62 | + url = None |
| 63 | + if "Link" in response.headers: |
| 64 | + links = requests.utils.parse_header_links(response.headers["Link"]) |
| 65 | + for link in links: |
| 66 | + if link["rel"] == "next": |
| 67 | + url = link["url"] |
| 68 | + break |
| 69 | + return all_runners |
| 70 | + except Exception as e: |
| 71 | + print(f"Warning: Failed to query runner status due to exception: {e}") |
| 72 | + return [] |
| 73 | + |
| 74 | + |
| 75 | +all_runners_cached: Optional[List[Dict[str, Any]]] = None |
| 76 | + |
| 77 | + |
| 78 | +def all_runners() -> List[Dict[str, Any]]: |
| 79 | + global all_runners_cached |
| 80 | + if all_runners_cached is None: |
| 81 | + print("Querying runners from GitHub...") |
| 82 | + all_runners_cached = query_runners_from_github() |
| 83 | + print(f"Github returned {len(all_runners_cached)} runners") |
| 84 | + counts = count_by_status(all_runners_cached) |
| 85 | + print( |
| 86 | + f"Busy: {counts['busy']}, Idle: {counts['idle']}, Offline: {counts['offline']}" |
| 87 | + ) |
| 88 | + return all_runners_cached |
| 89 | + |
| 90 | + |
| 91 | +def runner_labels(runner: Dict[str, Any]) -> List[str]: |
| 92 | + return [label["name"] for label in runner["labels"]] |
| 93 | + |
| 94 | + |
| 95 | +def is_self_hosted_runner(runner: Dict[str, Any]) -> bool: |
| 96 | + labels = runner_labels(runner) |
| 97 | + for label in DEFAULT_SELF_HOSTED_RUNNER_TAGS: |
| 98 | + if label not in labels: |
| 99 | + return False |
| 100 | + return True |
| 101 | + |
| 102 | + |
| 103 | +def self_hosted_runners() -> List[Dict[str, Any]]: |
| 104 | + runners = all_runners() |
| 105 | + return [r for r in runners if is_self_hosted_runner(r)] |
| 106 | + |
| 107 | + |
| 108 | +def runners_by_arch(arch: Arch) -> List[Dict[str, Any]]: |
| 109 | + runners = self_hosted_runners() |
| 110 | + return [r for r in runners if arch.value in runner_labels(r)] |
| 111 | + |
| 112 | + |
| 113 | +def count_by_status(runners: List[Dict[str, Any]]) -> Dict[str, int]: |
| 114 | + result = {"busy": 0, "idle": 0, "offline": 0} |
| 115 | + for runner in runners: |
| 116 | + if runner["status"] == "online": |
| 117 | + if runner["busy"]: |
| 118 | + result["busy"] += 1 |
| 119 | + else: |
| 120 | + result["idle"] += 1 |
| 121 | + else: |
| 122 | + result["offline"] += 1 |
| 123 | + return result |
| 124 | + |
| 125 | + |
| 126 | +@dataclasses.dataclass |
| 127 | +class BuildConfig: |
| 128 | + arch: Arch |
| 129 | + kernel_compiler: Compiler = Compiler.GCC |
| 130 | + gcc_version: int = DEFAULT_GCC_VERSION |
| 131 | + llvm_version: int = DEFAULT_LLVM_VERSION |
| 132 | + kernel: str = "LATEST" |
| 133 | + run_veristat: bool = False |
| 134 | + parallel_tests: bool = False |
| 135 | + build_release: bool = False |
| 136 | + |
| 137 | + @property |
| 138 | + def runs_on(self) -> List[str]: |
| 139 | + if is_managed_repo(): |
| 140 | + return DEFAULT_SELF_HOSTED_RUNNER_TAGS + [self.arch.value] |
| 141 | + else: |
| 142 | + return [DEFAULT_GITHUB_HOSTED_RUNNER] |
| 143 | + |
| 144 | + @property |
| 145 | + def build_runs_on(self) -> List[str]: |
| 146 | + if not is_managed_repo(): |
| 147 | + return [DEFAULT_GITHUB_HOSTED_RUNNER] |
| 148 | + |
| 149 | + # @Temporary: disable codebuild runners for cross-compilation jobs |
| 150 | + match self.arch: |
| 151 | + case Arch.S390X: |
| 152 | + return DEFAULT_SELF_HOSTED_RUNNER_TAGS + [Arch.X86_64.value] |
| 153 | + case Arch.AARCH64: |
| 154 | + return DEFAULT_SELF_HOSTED_RUNNER_TAGS + [Arch.AARCH64.value] |
| 155 | + |
| 156 | + # For managed repos, check the busyness of relevant self-hosted runners |
| 157 | + # If they are too busy, use codebuild |
| 158 | + runner_arch = self.arch |
| 159 | + # We don't build s390x kernel on s390x runners, because it's too slow |
| 160 | + # Cross-compiling on x86_64 is faster |
| 161 | + if runner_arch == Arch.S390X: |
| 162 | + runner_arch = Arch.X86_64 |
| 163 | + runners = runners_by_arch(runner_arch) |
| 164 | + counts = count_by_status(runners) |
| 165 | + online = counts["idle"] + counts["busy"] |
| 166 | + busy = counts["busy"] |
| 167 | + # if online <= 0, then something is wrong, don't use codebuild |
| 168 | + if online > 0 and busy / online > RUNNERS_BUSY_THRESHOLD: |
| 169 | + return ["codebuild"] |
| 170 | + else: |
| 171 | + return DEFAULT_SELF_HOSTED_RUNNER_TAGS + [runner_arch.value] |
| 172 | + |
| 173 | + @property |
| 174 | + def tests(self) -> Dict[str, Any]: |
| 175 | + tests_list = [ |
| 176 | + "test_progs", |
| 177 | + "test_progs_parallel", |
| 178 | + "test_progs_no_alu32", |
| 179 | + "test_progs_no_alu32_parallel", |
| 180 | + "test_verifier", |
| 181 | + ] |
| 182 | + |
| 183 | + if self.arch.value != "s390x": |
| 184 | + tests_list.append("test_maps") |
| 185 | + |
| 186 | + if self.llvm_version >= 18: |
| 187 | + tests_list.append("test_progs_cpuv4") |
| 188 | + |
| 189 | + # if self.arch in [Arch.X86_64, Arch.AARCH64]: |
| 190 | + # tests_list.append("sched_ext") |
| 191 | + |
| 192 | + # Don't run GCC BPF runner, because too many tests are failing |
| 193 | + # See: https://lore.kernel.org/bpf/[email protected]/ |
| 194 | + # if self.arch == Arch.X86_64: |
| 195 | + # tests_list.append("test_progs-bpf_gcc") |
| 196 | + |
| 197 | + if not self.parallel_tests: |
| 198 | + tests_list = [test for test in tests_list if not test.endswith("parallel")] |
| 199 | + |
| 200 | + return {"include": [generate_test_config(test) for test in tests_list]} |
| 201 | + |
| 202 | + def to_dict(self) -> Dict[str, Any]: |
| 203 | + return { |
| 204 | + "arch": self.arch.value, |
| 205 | + "kernel_compiler": self.kernel_compiler.value, |
| 206 | + "gcc_version": DEFAULT_GCC_VERSION, |
| 207 | + "llvm_version": DEFAULT_LLVM_VERSION, |
| 208 | + "kernel": self.kernel, |
| 209 | + "run_veristat": self.run_veristat, |
| 210 | + "parallel_tests": self.parallel_tests, |
| 211 | + "build_release": self.build_release, |
| 212 | + "runs_on": self.runs_on, |
| 213 | + "tests": self.tests, |
| 214 | + "build_runs_on": self.build_runs_on, |
| 215 | + } |
| 216 | + |
| 217 | + |
| 218 | +def is_managed_repo() -> bool: |
| 219 | + return ( |
| 220 | + os.environ["GITHUB_REPOSITORY_OWNER"] == MANAGED_OWNER |
| 221 | + and os.environ["GITHUB_REPOSITORY"] in MANAGED_REPOS |
| 222 | + ) |
| 223 | + |
| 224 | + |
| 225 | +def set_output(name, value): |
| 226 | + """Write an output variable to the GitHub output file.""" |
| 227 | + with open(os.getenv("GITHUB_OUTPUT"), "a", encoding="utf-8") as file: |
| 228 | + file.write(f"{name}={value}\n") |
| 229 | + |
| 230 | + |
| 231 | +def generate_test_config(test: str) -> Dict[str, Union[str, int]]: |
| 232 | + """Create the configuration for the provided test.""" |
| 233 | + is_parallel = test.endswith("_parallel") |
| 234 | + config = { |
| 235 | + "test": test, |
| 236 | + "continue_on_error": is_parallel, |
| 237 | + # While in experimental mode, parallel jobs may get stuck |
| 238 | + # anywhere, including in user space where the kernel won't detect |
| 239 | + # a problem and panic. We add a second layer of (smaller) timeouts |
| 240 | + # here such that if we get stuck in a parallel run, we hit this |
| 241 | + # timeout and fail without affecting the overall job success (as |
| 242 | + # would be the case if we hit the job-wide timeout). For |
| 243 | + # non-experimental jobs, 360 is the default which will be |
| 244 | + # superseded by the overall workflow timeout (but we need to |
| 245 | + # specify something). |
| 246 | + "timeout_minutes": 30 if is_parallel else 360, |
| 247 | + } |
| 248 | + return config |
| 249 | + |
| 250 | + |
| 251 | +if __name__ == "__main__": |
| 252 | + matrix = [ |
| 253 | + BuildConfig( |
| 254 | + arch=Arch.X86_64, |
| 255 | + run_veristat=True, |
| 256 | + parallel_tests=True, |
| 257 | + ), |
| 258 | + BuildConfig( |
| 259 | + arch=Arch.X86_64, |
| 260 | + kernel_compiler=Compiler.LLVM, |
| 261 | + build_release=True, |
| 262 | + ), |
| 263 | + BuildConfig( |
| 264 | + arch=Arch.AARCH64, |
| 265 | + ), |
| 266 | + BuildConfig( |
| 267 | + arch=Arch.S390X, |
| 268 | + ), |
| 269 | + ] |
| 270 | + |
| 271 | + # Outside of managed repositories only run on x86_64 |
| 272 | + if not is_managed_repo(): |
| 273 | + matrix = [config for config in matrix if config.arch == Arch.X86_64] |
| 274 | + |
| 275 | + json_matrix = json.dumps({"include": [config.to_dict() for config in matrix]}) |
| 276 | + print(json.dumps(json.loads(json_matrix), indent=4)) |
| 277 | + set_output("build_matrix", json_matrix) |
0 commit comments