Answer principles round 1: 0 🔴, 5 🟡; enforce run-heavy in rule-hook, take slots through one tested function, exit 125 on lock errors, fake meminfo in black-box tests
This commit is contained in:
+25
-22
@@ -3,7 +3,7 @@
|
||||
usage: run-heavy <command> [<arg>...]
|
||||
|
||||
A heavy command is one that loads the machine for minutes: a full gate, mutation tests, an e2e run,
|
||||
a docker build. The machine has one heavy slot per 4 CPUs, and at least one, shared by every
|
||||
a docker build. The machine has one heavy slot per 4 CPUs it may run on, and at least one, shared by every
|
||||
session that runs run-heavy. Each slot is a file locked with flock, under
|
||||
$XDG_STATE_HOME/run-heavy (default ~/.local/state/run-heavy). While every slot is held, run-heavy
|
||||
says so once on stderr and waits. It then runs the command in its place, which holds the slot until
|
||||
@@ -13,14 +13,14 @@ A command it runs carries the slot's number in RUN_HEAVY_SLOT, and run-heavy cal
|
||||
runs its command at once, so a gate that wraps its own steps cannot wait on itself.
|
||||
|
||||
As GNU env, nice and timeout do, it exits 125, saying why, when no command is given or the slot
|
||||
folder cannot be made; 126 when the command is found but cannot run; and 127 when it is not found.
|
||||
folder or a slot file cannot be made or locked; 126 when the command is found but cannot run; and 127 when it is not found.
|
||||
"""
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from machine_load.slots import slot_count, slot_folder, taken_slot
|
||||
from machine_load.slots import slot_count, slot_folder, waited_slot
|
||||
|
||||
# Exit codes for a command run-heavy never ran, apart from any the command exits with, as GNU env, nice and timeout use them.
|
||||
OWN_FAILURE = 125
|
||||
@@ -28,12 +28,10 @@ CANNOT_RUN = 126
|
||||
NOT_FOUND = 127
|
||||
# Names the slot a command runs in, so a heavy command it runs does not wait for a second slot.
|
||||
HELD = "RUN_HEAVY_SLOT"
|
||||
# Seconds between tries while every slot is held; a slot frees as its command exits.
|
||||
POLL_SECONDS = 1
|
||||
USAGE = "usage: run-heavy <command> [<arg>...]"
|
||||
|
||||
|
||||
def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
|
||||
def main(argv: list[str], env: typing.Mapping[str, str], cpus: int | None) -> int:
|
||||
if argv[:1] in (["-h"], ["--help"]):
|
||||
print(__doc__.strip())
|
||||
return 0
|
||||
@@ -41,23 +39,28 @@ def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
|
||||
print(f"run-heavy: no command given; {USAGE}", file=sys.stderr)
|
||||
return OWN_FAILURE
|
||||
if HELD not in env:
|
||||
folder = slot_folder(env)
|
||||
try:
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
except OSError as failure:
|
||||
print(f"run-heavy: cannot make the slot folder {folder}: {failure.strerror}", file=sys.stderr)
|
||||
slot = held_slot(slot_folder(env), slot_count(cpus))
|
||||
if isinstance(slot, str):
|
||||
print(f"run-heavy: {slot}", file=sys.stderr)
|
||||
return OWN_FAILURE
|
||||
slots = slot_count(os.cpu_count())
|
||||
said = False
|
||||
while (taken := taken_slot(folder, slots)) is None:
|
||||
if not said:
|
||||
print(f"run-heavy: waiting for one of the machine's {slots} heavy slots", file=sys.stderr, flush=True)
|
||||
said = True
|
||||
time.sleep(POLL_SECONDS)
|
||||
slot, fd = taken
|
||||
# The command inherits the locked file, so the slot stays held until it exits.
|
||||
os.set_inheritable(fd, True)
|
||||
env = {**env, HELD: str(slot)}
|
||||
return run(argv, env)
|
||||
|
||||
|
||||
def held_slot(folder: pathlib.Path, slots: int) -> int | str:
|
||||
"""The number of the slot this call holds, its lock left for the command to inherit, or why no slot could be taken."""
|
||||
try:
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
slot, fd = waited_slot(folder, slots, lambda: print(f"run-heavy: waiting for one of the machine's {slots} heavy slots", file=sys.stderr, flush=True))
|
||||
except OSError as failure:
|
||||
return f"cannot take a heavy slot in {folder}: {failure.strerror}"
|
||||
# The command inherits the locked file, so the slot stays held until it exits.
|
||||
os.set_inheritable(fd, True)
|
||||
return slot
|
||||
|
||||
|
||||
def run(argv: list[str], env: typing.Mapping[str, str]) -> int:
|
||||
"""Runs the command in run-heavy's place; returns only where it cannot start."""
|
||||
try:
|
||||
os.execvpe(argv[0], argv, env)
|
||||
except FileNotFoundError:
|
||||
|
||||
@@ -5,13 +5,12 @@ import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from machine_load.slots import slot_count
|
||||
from run_heavy import main
|
||||
from machine_load.slots import slot_count, usable_cpus
|
||||
|
||||
TOOL = pathlib.Path(__file__).resolve().parents[1] / "run-heavy"
|
||||
|
||||
@@ -36,14 +35,14 @@ class RunHeavy(unittest.TestCase):
|
||||
def every_slot_held(self):
|
||||
self.slots.mkdir(parents=True, exist_ok=True)
|
||||
with contextlib.ExitStack() as held:
|
||||
for slot in range(slot_count(os.cpu_count())):
|
||||
for slot in range(slot_count(usable_cpus())):
|
||||
file = held.enter_context(open(self.slots / f"slot-{slot}", "a"))
|
||||
fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
yield
|
||||
|
||||
def free_slots(self) -> int:
|
||||
free = 0
|
||||
for slot in range(slot_count(os.cpu_count())):
|
||||
for slot in range(slot_count(usable_cpus())):
|
||||
with open(self.slots / f"slot-{slot}", "a") as file:
|
||||
try:
|
||||
fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
@@ -59,18 +58,9 @@ class RunHeavy(unittest.TestCase):
|
||||
def test_holds_a_slot_while_the_command_runs_and_frees_it_after(self):
|
||||
tool = self.start_tool("sh", "-c", "echo started; read line")
|
||||
self.assertEqual(tool.stdout.readline(), "started\n")
|
||||
self.assertEqual(self.free_slots(), slot_count(os.cpu_count()) - 1)
|
||||
self.assertEqual(self.free_slots(), slot_count(usable_cpus()) - 1)
|
||||
tool.communicate("\n", timeout=30)
|
||||
self.assertEqual(self.free_slots(), slot_count(os.cpu_count()))
|
||||
|
||||
def test_waits_while_every_slot_is_held_and_says_so_once(self):
|
||||
with self.every_slot_held():
|
||||
tool = self.start_tool("echo", "ran")
|
||||
time.sleep(3 * main.POLL_SECONDS)
|
||||
self.assertIsNone(tool.poll())
|
||||
out, err = tool.communicate(timeout=30)
|
||||
self.assertEqual((tool.returncode, out), (0, "ran\n"))
|
||||
self.assertEqual(err.count("waiting"), 1, err)
|
||||
self.assertEqual(self.free_slots(), slot_count(usable_cpus()))
|
||||
|
||||
def test_a_command_inside_a_held_slot_runs_at_once(self):
|
||||
with self.every_slot_held():
|
||||
|
||||
Reference in New Issue
Block a user