Answer principles round 1: 0 🔴, 4 🟡 (0 misread); exempt only a bare docker under run-heavy with no PATH of its own, drop the stale item citation, unit-test the slot share in MiB, place the limits by the subcommand's index; also a named Option, exit 126, clearer names and the launcher's import order
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""The docker a heavy command finds first on its PATH: it gives a container the command makes the limits it lacks, the slot's memory share in RUN_HEAVY_MEMORY, then runs the real docker."""
|
||||
"""The docker that run-heavy puts first on its command's PATH. It adds the memory limits a container lacks, from the slot's share, then runs the real docker."""
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
@@ -7,17 +7,29 @@ import typing
|
||||
|
||||
from docker_command import capped
|
||||
|
||||
FOLDER = pathlib.Path(__file__).resolve().parent / "path_entry"
|
||||
SHARE = "RUN_HEAVY_MEMORY"
|
||||
# The folder run-heavy puts first on its command's PATH, holding the docker that runs main.
|
||||
PATH_ENTRY = pathlib.Path(__file__).resolve().parent / "path_entry"
|
||||
# The environment variable that hands this docker the slot's memory share, in MiB.
|
||||
SHARE_VARIABLE = "RUN_HEAVY_MEMORY"
|
||||
CANNOT_RUN = 126
|
||||
NOT_FOUND = 127
|
||||
|
||||
|
||||
def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
|
||||
"""Replaces this process with the real docker; returns only where none is found."""
|
||||
path = os.pathsep.join(entry for entry in env.get("PATH", "").split(os.pathsep) if pathlib.Path(entry or ".").resolve() != FOLDER)
|
||||
"""Replaces this process with the real docker; returns only where none is found or it cannot run."""
|
||||
path = without_path_entry(env.get("PATH", ""))
|
||||
docker = shutil.which("docker", path=path)
|
||||
if docker is None:
|
||||
print("run-heavy: docker: command not found", file=sys.stderr)
|
||||
return NOT_FOUND
|
||||
share = env.get(SHARE)
|
||||
os.execve(docker, [docker, *(capped(argv, share) if share else argv)], {**env, "PATH": path})
|
||||
share = env.get(SHARE_VARIABLE)
|
||||
try:
|
||||
os.execve(docker, [docker, *(capped(argv, f"{share}m") if share else argv)], {**env, "PATH": path})
|
||||
except OSError as failure:
|
||||
print(f"run-heavy: cannot run {docker}: {failure.strerror}", file=sys.stderr)
|
||||
return CANNOT_RUN
|
||||
|
||||
|
||||
def without_path_entry(path: str) -> str:
|
||||
"""path less PATH_ENTRY, so the real docker is the one found."""
|
||||
return os.pathsep.join(entry for entry in path.split(os.pathsep) if pathlib.Path(entry or ".").resolve() != PATH_ENTRY)
|
||||
|
||||
+17
-10
@@ -9,12 +9,17 @@ $XDG_STATE_HOME/run-heavy (default ~/.local/state/run-heavy). While every slot i
|
||||
says so once on stderr and waits. It then replaces itself with the command. The slot's lock stays held
|
||||
until the command, and every process that inherited the lock, exits. Its exit code is the command's.
|
||||
|
||||
Each container the command makes with docker run or docker create gets a memory limit and a swap limit,
|
||||
both the slot's share, where it sets neither: three quarters of the machine's memory, split across the
|
||||
slots. A docker first on the command's PATH adds them, and passes every other call on unchanged. A
|
||||
container that sets one of the two limits keeps it, and gets the other as large; an unlimited
|
||||
swap, --memory-swap -1, gives way. The share is in RUN_HEAVY_MEMORY. A docker compose service,
|
||||
or a docker named by its full path, gets no limit.
|
||||
On Linux, run-heavy also caps the memory of each container the command makes with docker run or
|
||||
docker create. The cap is the slot's share: three quarters of the machine's memory, split evenly
|
||||
across the slots, handed to the command in RUN_HEAVY_MEMORY, in MiB. run-heavy puts its own docker
|
||||
first on the command's PATH. That docker adds the limits a container lacks, then runs the real one:
|
||||
|
||||
- A container that sets neither limit gets a memory limit and a swap limit, both the share.
|
||||
- A container that sets one of them keeps it, and gets the other at the same size.
|
||||
- A --memory-swap of -1, unlimited swap, is dropped and replaced.
|
||||
|
||||
Every other docker call goes through unchanged. A docker compose service, and a docker named by its
|
||||
full path, get no cap: give those containers their own limits.
|
||||
|
||||
A command it runs carries the slot's number in RUN_HEAVY_SLOT, and run-heavy called with that set
|
||||
runs its command at once, so a gate that wraps its own steps cannot wait on itself.
|
||||
@@ -24,12 +29,13 @@ folder or a slot file cannot be made or locked; 126 when the command is found bu
|
||||
"""
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import sys
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
from run_heavy.capped_docker import FOLDER, SHARE
|
||||
from machine_load.memory import Memory
|
||||
from run_heavy.capped_docker import PATH_ENTRY, SHARE_VARIABLE, without_path_entry
|
||||
from machine_load.slots import slot_count, slot_folder, waited_slot
|
||||
|
||||
OWN_FAILURE = 125
|
||||
@@ -56,10 +62,11 @@ def main(argv: list[str], env: typing.Mapping[str, str], cpus: int | None, memor
|
||||
|
||||
|
||||
def capping(memory: Memory | None, slots: int, env: typing.Mapping[str, str]) -> dict[str, str]:
|
||||
"""What the command's environment needs for its docker to cap each container it makes at the slot's share of memory; nothing where the memory is unknown, as off Linux."""
|
||||
if memory is None:
|
||||
"""What the command's environment needs for its docker to cap each container it makes at the slot's share of memory; nothing where the memory is unknown, as off Linux, or where PATH finds no docker to cap."""
|
||||
path = env.get("PATH", "")
|
||||
if memory is None or shutil.which("docker", path=without_path_entry(path)) is None:
|
||||
return {}
|
||||
return {SHARE: memory.slot_share(slots), "PATH": os.pathsep.join([str(FOLDER), env.get("PATH", "")])}
|
||||
return {SHARE_VARIABLE: str(memory.slot_share(slots)), "PATH": os.pathsep.join([str(PATH_ENTRY), path])}
|
||||
|
||||
|
||||
def held_slot(folder: pathlib.Path, slots: int) -> int | Failure:
|
||||
|
||||
@@ -94,7 +94,8 @@ class RunHeavy(unittest.TestCase):
|
||||
|
||||
def test_a_docker_run_without_limits_gets_three_quarters_of_the_memory_split_across_the_slots(self):
|
||||
memory = read_memory()
|
||||
assert memory is not None
|
||||
if memory is None:
|
||||
self.skipTest("no /proc/meminfo to read the share from")
|
||||
share = f"{memory.total * 3 // 4 // slot_count(usable_cpus()) // 1024}m"
|
||||
result = self.run_tool("docker", "run", "--rm", "img", "true", env=self.fake_docker())
|
||||
self.assertEqual((result.returncode, result.stdout), (0, f"run --memory {share} --memory-swap {share} --rm img true\n"))
|
||||
@@ -106,6 +107,14 @@ class RunHeavy(unittest.TestCase):
|
||||
result = self.run_tool("docker", *args, env=env)
|
||||
self.assertEqual((result.returncode, result.stdout), (0, " ".join(args) + "\n"))
|
||||
|
||||
def test_a_command_on_a_machine_without_docker_finds_none(self):
|
||||
folder = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(folder.cleanup)
|
||||
(pathlib.Path(folder.name) / "python3").symlink_to(sys.executable)
|
||||
(pathlib.Path(folder.name) / "sh").symlink_to("/bin/sh")
|
||||
result = self.run_tool("sh", "-c", "command -v docker || echo none", env={**self.env, "PATH": folder.name})
|
||||
self.assertEqual((result.returncode, result.stdout), (0, "none\n"))
|
||||
|
||||
def test_a_capped_docker_that_finds_no_docker_exits_127_saying_so(self):
|
||||
folder = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(folder.cleanup)
|
||||
|
||||
Reference in New Issue
Block a user