From eddb26f104fad9c69a8839a87e8fb7b67e706c80 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:13:18 +0100 Subject: [PATCH] refactor: one load threshold for every probe --- scripts/core_probe.py | 7 ++++++- scripts/probes/host.py | 2 +- scripts/probes/packprobe.py | 8 ++++---- scripts/probes/standalonerecord.py | 3 +-- tests/test_pack_probe.py | 20 ++++++++++++++++++++ 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/scripts/core_probe.py b/scripts/core_probe.py index c02297acf..309881fe5 100644 --- a/scripts/core_probe.py +++ b/scripts/core_probe.py @@ -1587,8 +1587,13 @@ def probe(args: argparse.Namespace) -> int: return _probe(args) +def default_max_load(cpus: int | None = None) -> float: + """The load above which a run records nothing without --max-load: a quarter of the CPUs.""" + return (cpus if cpus is not None else os.cpu_count() or 1) / 4 + + def _probe(args: argparse.Namespace) -> int: - max_load = args.max_load if args.max_load is not None else (os.cpu_count() or 1) / 4 + max_load = args.max_load if args.max_load is not None else default_max_load() load_before = load_average() if args.record and load_before and load_before[0] > max_load: print(f"ERROR: load average {load_before[0]} exceeds {max_load}: the machine is busy, " diff --git a/scripts/probes/host.py b/scripts/probes/host.py index ff2c99e9d..5f30fb36a 100644 --- a/scripts/probes/host.py +++ b/scripts/probes/host.py @@ -125,7 +125,7 @@ class Host: @property def max_load(self) -> float: - return self.cpu_count / 4 + return core_probe.default_max_load(self.cpu_count) def parse_budget(text: str) -> float: diff --git a/scripts/probes/packprobe.py b/scripts/probes/packprobe.py index 2386833da..58ec91319 100644 --- a/scripts/probes/packprobe.py +++ b/scripts/probes/packprobe.py @@ -13,8 +13,8 @@ from collections.abc import Iterable, Sequence from dataclasses import dataclass from pathlib import Path -from core_probe import (EXIT_BUSY, PROBE_LOCK_ENV, PROBE_LOCK_HELD_ENV, VERDICTS, head_commit, host_probe_lock, - probe_lock_path) +from core_probe import (EXIT_BUSY, PROBE_LOCK_ENV, PROBE_LOCK_HELD_ENV, VERDICTS, default_max_load, head_commit, + host_probe_lock, probe_lock_path) from core.paths import REPO_ROOT from core.statefile import now_iso from model import measurements as mr @@ -256,9 +256,9 @@ def content_folder(content: Content, fetch: bool) -> Path: # -------------------------------------------------------------------------- def wait_idle(limit: float = IDLE_WAIT, poll: float = 30.0) -> None: - """Wait for the load core_probe records under, a quarter of the CPUs. + """Wait for the load core_probe records under (core_probe.default_max_load). Extracting a pack loads the host itself for a minute or two.""" - threshold = (os.cpu_count() or 1) / 4 + threshold = default_max_load() waited = 0.0 while os.getloadavg()[0] > threshold: if waited >= limit: diff --git a/scripts/probes/standalonerecord.py b/scripts/probes/standalonerecord.py index e5a2bfdae..059428423 100644 --- a/scripts/probes/standalonerecord.py +++ b/scripts/probes/standalonerecord.py @@ -3,7 +3,6 @@ from __future__ import annotations import argparse -import os import re import shutil import subprocess @@ -101,7 +100,7 @@ def measure(args: argparse.Namespace, emu: Emulator) -> dict: meta, tree = installed(emu, args.sha256) except (cp.ProbeError, OSError, subprocess.SubprocessError) as exc: raise RunFailed(exc) from exc - max_load = args.max_load if args.max_load is not None else (os.cpu_count() or 1) / 4 + max_load = args.max_load if args.max_load is not None else cp.default_max_load() load_before = cp.load_average() if args.record and load_before and load_before[0] > max_load: raise Busy(f"load average {load_before[0]} exceeds {max_load}: the machine is busy, " diff --git a/tests/test_pack_probe.py b/tests/test_pack_probe.py index dabfd077d..5d60a8759 100644 --- a/tests/test_pack_probe.py +++ b/tests/test_pack_probe.py @@ -355,6 +355,26 @@ class Lock(unittest.TestCase): self.assertFalse(try_lock()) +class LoadThreshold(unittest.TestCase): + """pack_probe waits for the load core_probe records under, and the campaign + passes the same value: all three read core_probe.default_max_load.""" + + def test_wait_and_campaign_follow_core_probe(self) -> None: + from unittest import mock + + import core_probe + from probes import host + + with mock.patch.object(os, "cpu_count", return_value=12): + threshold = core_probe.default_max_load() + self.assertEqual(host.Host(cpu_count=12).max_load, threshold) + with mock.patch.object(os, "getloadavg", return_value=(threshold, 0.0, 0.0)): + packprobe.wait_idle(limit=0, poll=0) + with mock.patch.object(os, "getloadavg", return_value=(threshold + 0.5, 0.0, 0.0)), \ + self.assertRaisesRegex(packprobe.ProbeFailure, f"above {threshold} "): + packprobe.wait_idle(limit=0, poll=0) + + class RecordsNameNoLocalPath(unittest.TestCase): """A record is read on other machines: the probe's scratch folders are written as the options that named them."""