Cap the memory of each container a heavy command makes under run-heavy at its slot's share, and have stability-reviewer size a mutation run's concurrency to fit it

This commit is contained in:
2026-10-24 01:06:56 +02:00
parent eb7b50698c
commit 4c1ab3f185
16 changed files with 149 additions and 27 deletions
+23 -6
View File
@@ -35,20 +35,37 @@ def unlimited(arguments: list[str]) -> bool:
The container's own arguments, past its image, are read too: telling them apart needs every value-taking option of docker run. So a -m 4g there reads as a limit, while python -m pytest does not, since pytest is no size.
"""
subcommand = docker_subcommand(arguments)
limited = given(arguments, ("-m", "--memory"), SIZE) and given(arguments, ("--memory-swap",), SWAP)
limited = option_value(arguments, ("-m", "--memory"), SIZE) and option_value(arguments, ("--memory-swap",), SWAP)
return subcommand in STARTS_CONTAINER and not limited and not helped(arguments, subcommand)
def given(arguments: list[str], options: tuple[str, ...], pattern: str) -> bool:
"""Whether the arguments give one of the options a value pattern matches, as its next word or attached, as in -m4g or --memory=4g."""
attached = re.compile("|".join(re.escape(option) + ("" if len(option) == 2 else "=") + f"(?:{pattern})" for option in options))
return any(attached.fullmatch(word) or (word in options and re.fullmatch(pattern, following)) for word, following in zip(arguments, [*arguments[1:], ""]))
def capped(arguments: list[str], share: str) -> list[str]:
"""docker's arguments with the limits a container they make lacks, put right after the subcommand: share for each, except a swap limit beside the container's own memory limit, which takes that limit, as docker refuses less swap than memory."""
if not unlimited(arguments):
return arguments
memory = option_value(arguments, ("-m", "--memory"), SIZE)
after = arguments.index(docker_subcommand(arguments)[-1]) + 1
limits = ["--memory-swap", memory] if memory else ["--memory", share, "--memory-swap", share]
return [*arguments[:after], *limits, *arguments[after:]]
def option_value(arguments: list[str], options: tuple[str, ...], pattern: str) -> str | None:
"""The value the arguments give one of the options, where pattern matches it, as its next word or attached, as in -m4g or --memory=4g; None where they give none."""
for word, following in zip(arguments, [*arguments[1:], ""]):
for option in options:
attached = word.removeprefix(option if len(option) == 2 else option + "=")
if attached != word and re.fullmatch(pattern, attached):
return attached
if word == option and re.fullmatch(pattern, following):
return following
return None
def unlimited_container(words: list[str]) -> bool:
"""Whether the simple command, directly or through a launcher, starts a container that lacks a memory limit or a swap limit."""
docker = launched(words, {"docker"})
return docker is not None and unlimited(docker.arguments)
# Under run-heavy, its docker adds the limits a container lacks.
return docker is not None and "run-heavy" not in docker.launchers and unlimited(docker.arguments)
def helped(arguments: list[str], subcommand: list[str]) -> bool: