diff --git a/skills/bounding-heavy-commands/SKILL.md b/skills/bounding-heavy-commands/SKILL.md index 3b2387c..26f8998 100644 --- a/skills/bounding-heavy-commands/SKILL.md +++ b/skills/bounding-heavy-commands/SKILL.md @@ -51,10 +51,10 @@ python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_ru The wrapper does four things in sequence. It waits for its turn in the queue. It waits until the memory of its budget is free. It runs the command. It prints the budget and the measured peak on the error stream. For the wait, the free memory is the memory that the machine has free now, less the part of their budgets that the running bounded commands do not use yet, less a headroom for the programs outside the queue. The headroom is a tenth of the memory of the machine, and at least 1 GiB. The budget and the headroom together never need more than the memory of the queue, so a budget of the whole queue does not wait for a headroom. The wait has no time limit: the command does not start before its memory is free, and the wrapper prints a line at intervals while it waits. Each line that the wrapper prints starts with `bounded-run:`. A measurement also prints one row line, in a fixed form: ``` -bounded-run: row label= cpus= budget=M peak=M exact= exit= +bounded-run: row label= cpus= budget=M peak=M held=M|- exact= exit= ``` -`budget` is the suggested budget. `exact=no` means that the peak was sampled, so the margin is larger. A `-` in place of the label or the `--cpus` means that the call did not give the option. The wrapper cannot know if the command did its full work: the exit code and the output of the command tell you (§Discovery, step 5). +`budget` is the suggested budget. `peak` is the peak of all memory of the command, with the file cache that the kernel can take back from it. `held` is the peak of the memory that the kernel cannot take back at once. It needs the hard cap: without it, the row line shows `held=-`. With `held`, the suggested budget is `held` plus a margin, but at least a quarter of `peak` plus a margin, so that the files the command reads stay in the cache. It is never more than `peak` plus a margin. Without `held`, it is `peak` plus a margin. `exact=no` means that the peak was sampled, so the margin is larger. `held` is always sampled. A `-` in place of the label or the `--cpus` means that the call did not give the option. The wrapper cannot know if the command did its full work: the exit code and the output of the command tell you (§Discovery, step 5). A check prints its lines in a fixed form too: @@ -107,7 +107,7 @@ Then, before each heavy command, use the first case that applies: 4. **Your harness has no project memory, or it is off.** You cannot save a record, so do not try. Run the discovery in this session, keep the result in your context, and use the wrapper's default budget for anything you could not measure. 5. **Project memory is on, and the record does not exist.** Run the discovery for every heavy command of the project, not only for the command that you are about to run, and measure them all with one command line that the user runs (§Measure the whole project). While you wait for the answer of the user and for the line, run a heavy command that you need through the wrapper, as its bounded command line at `--cpus` of half the machine's processors, with no `--memory` option. -A budget is a measurement, not a preference. Change a row only when the wrapper's output at the `--cpus` of that row shows that the peak moved, and write the new peak and date in it. +A budget is a measurement, not a preference. Change a row only when the wrapper's output at the `--cpus` of that row shows that the suggested budget moved, and write the new budget, peak, and date in it. Under the hard cap the budget follows `held`, so it can move while `peak` stays the same. To make a run start sooner, use a row at a smaller `--cpus`. That command runs fewer things at once, so its peak and its budget are smaller. It gets a slot sooner only when its budget needs fewer slots of the queue than the budget of the default row. It is a different measured command, not a lower budget for the same command. This works only for a command whose `self-bound` reads `BOUNDED_RUN_CPUS`. For any other command, a smaller `--cpus` does not make the command run fewer things at once: keep the run in the queue, and measure no smaller row. When the entry has no row at a smaller `--cpus`, the current run cannot start sooner: keep it in the queue. Then measure the command with `--measure`, with `--cpus` at half the smallest `--cpus` of the table, rounded down, and with `--memory` at the budget of the smallest row, in place of the two values of the command line, and add the row, so that the next run can use it. A command that runs fewer things at once seldom needs more than that budget, and with `--memory` the measurement does not take the whole queue. When the hard cap stops it, measure it again with two times that budget, as step 5 of §Discovery says. At `--cpus 1` no smaller command exists. Keep the default row as it is, unless the user wants the smaller `--cpus` for each run. @@ -131,7 +131,7 @@ A part of a recorded command (one test file, one target) uses the bounded comman python3 "${CLAUDE_PLUGIN_ROOT}/skills/bounding-heavy-commands/scripts/bounded_run.py" --measure --cpus -- ``` -5. Record the suggested budget that the wrapper prints, with the peak and the date, as the row at the `--cpus` of the measurement. Record it only from a run in which the command did its full work. The suggested budget is the peak plus a margin, and the margin is larger where the measurement is approximate. A run that stopped early gives a peak that is too small: a suite that stops at its first failure, a command that failed at its start, a run that a signal or the hard cap stopped. Do not record the budget of such a run, and save no record for the command yet. Run the command with `--measure` again each time that you run it, until one run does its full work. A measurement with no `--memory` does not know the peak of the command. Where the hard cap is available, it runs with the default budget, and the cap stops it when it needs more. Where there is no hard cap, it takes the whole queue: it waits until no other bounded command runs, and no other starts while it runs. While it is at the head of the line, the commands behind it wait too (§Rules for the wait). When it waits for the whole queue, tell the user that a first measurement waits for the other bounded commands to end. If the hard cap stopped a measurement, measure again with `--memory` at two times the budget that the wrapper printed. For a measurement with no `--memory`, that is the default budget. The wrapper lowers a budget that is larger than the queue to the whole queue. If the cap stopped a measurement at the whole queue, the command needs more memory than the queue has: tell the user, with the budget that the wrapper printed. +5. Record the suggested budget that the wrapper prints, with the peak and the date, as the row at the `--cpus` of the measurement. Record it only from a run in which the command did its full work. The suggested budget is the peak plus a margin, and the margin is larger where the measurement is approximate. Under the hard cap, the peak that counts is the memory that the kernel cannot take back at once, with a floor for the file cache (§The wrapper). A run that stopped early gives a peak that is too small: a suite that stops at its first failure, a command that failed at its start, a run that a signal or the hard cap stopped. Do not record the budget of such a run, and save no record for the command yet. Run the command with `--measure` again each time that you run it, until one run does its full work. A measurement with no `--memory` does not know the peak of the command. Where the hard cap is available, it runs with the default budget, and the cap stops it when it needs more. Where there is no hard cap, it takes the whole queue: it waits until no other bounded command runs, and no other starts while it runs. While it is at the head of the line, the commands behind it wait too (§Rules for the wait). When it waits for the whole queue, tell the user that a first measurement waits for the other bounded commands to end. If the hard cap stopped a measurement, measure again with `--memory` at two times the budget of the cap: the budget in the line `budget MiB, peak ...` that the wrapper printed, not the suggested budget. For a measurement with no `--memory`, that is the default budget. The wrapper lowers a budget that is larger than the queue to the whole queue. If the cap stopped a measurement at the whole queue, the command needs more memory than the queue has: tell the user, with the budget that the wrapper printed. 6. Save the record. The measured run is a real run of the command. Its result counts, also when the run stopped early, so do not run the command a second time to get the result. @@ -212,7 +212,7 @@ When Python is missing or too old, and when the fault is a fault of the machine, | "I'll run just those few tests now, that fits in the free memory" | A part of a heavy command goes through the wrapper too. "It fits" is your estimate; the queue is the count. | | "I'll run the plain command once to check quickly" | One command outside the queue is all a freeze needs. | | "Running both in parallel is what makes the deadline possible" | Through the wrapper a parallel start is safe, but the queue decides if the two run together. Do not promise a time that depends on it. | -| "The queue has waited five minutes, I'll lower `--memory` to get a slot" | The budget is the measured peak plus a margin. Lowering it moves the failure inside the command. | +| "The queue has waited five minutes, I'll lower `--memory` to get a slot" | The budget is a measurement of the command plus a margin. Lowering it moves the failure inside the command. | | "A smaller slot size made it start at once last time" | The variable changes what the queue counts, not what the command uses. Set no variable whose name starts with `BOUNDED_RUN_`. | | "A crashed job left a stale lock, I'll remove only that file" | A stale lock cannot exist: the kernel releases a lock when its holder stops. The slots are held by commands that run. Leave the lock files alone. | | "The user told me to run it directly, but the skill says no exception" | An explicit instruction from the user comes first. Say one time what the wrapper protects, then obey. | diff --git a/skills/bounding-heavy-commands/scripts/bounded_run.py b/skills/bounding-heavy-commands/scripts/bounded_run.py index e71d9e8..9e59c79 100755 --- a/skills/bounding-heavy-commands/scripts/bounded_run.py +++ b/skills/bounding-heavy-commands/scripts/bounded_run.py @@ -10,9 +10,11 @@ A measurement prints one row line on the error stream, in a fixed form: - bounded-run: row label=NAME cpus=N budget=M peak=M exact=yes|no exit=CODE + bounded-run: row label=NAME cpus=N budget=M peak=M held=M|- exact=yes|no exit=CODE -budget is the suggested budget. A '-' stands for an option that the call did +budget is the suggested budget. held is the sampled peak of the memory that +the kernel cannot take back at once under the hard cap, or '-' without it. +A '-' in place of the label or of cpus stands for an option that the call did not give. --measure-rows measures the command at each processor limit of the list, the largest first. The largest runs with --memory. Without it, the largest runs with the default budget where the hard cap is available, and @@ -93,6 +95,7 @@ SCOPE_WAIT_SECONDS = 5.0 MARGIN_EXACT = 0.25 MARGIN_APPROXIMATE = 0.5 +CACHE_FLOOR_DIVISOR = 4 BUDGET_STEP_MIB = 256 HELD_COMMAND_CHARACTERS = 200 PR_SET_PDEATHSIG = 1 @@ -841,6 +844,33 @@ def cgroup_peak_mib(directory: str, names: Sequence[str] = ("memory.peak", "memo return None +# The kernel cannot take back these pages at once to keep a cgroup under its limit: dirty and writeback file pages +# must reach the disk first. An older kernel has no 'kernel' line, and there its parts stand in for it. +HELD_FIELDS = ("anon", "shmem", "file_dirty", "file_writeback") +KERNEL_PARTS = ("kernel_stack", "pagetables", "sec_pagetables", "slab_unreclaimable", "sock", "percpu", "vmalloc") + + +def cgroup_held_mib(directory: str) -> Optional[int]: + """Returns the memory of the cgroup that the kernel cannot take back at once, from its memory.stat. + + Returns None when the file cannot be read or has none of the lines of that memory. + """ + text = _read_text(os.path.join(directory, "memory.stat")) + if text is None: + return None + fields = {} + for line in text.splitlines(): + parts = line.split() + if len(parts) == 2 and parts[1].isdigit(): + fields[parts[0]] = int(parts[1]) + if "kernel" in fields: + # The 'kernel' line also counts the reclaimable slab, the cache of file names and inodes. + fields["kernel"] = max(0, fields["kernel"] - fields.get("slab_reclaimable", 0)) + names = HELD_FIELDS + (("kernel",) if "kernel" in fields else KERNEL_PARTS) + found = [fields[name] for name in names if name in fields] + return math.ceil(sum(found) / (1024 * 1024)) if found else None + + def parse_group_rss_mib(text: str, group: int) -> Optional[int]: total = 0 found = False @@ -868,17 +898,51 @@ def rusage_peak_mib(maxrss: int, platform: str = sys.platform) -> int: return math.ceil(maxrss / 1024) -def suggested_budget_mib(peak_mib: int, approximate: bool) -> int: +def _with_margin(mib: int, approximate: bool) -> int: margin = MARGIN_APPROXIMATE if approximate else MARGIN_EXACT - return max(1, math.ceil(peak_mib * (1 + margin) / BUDGET_STEP_MIB)) * BUDGET_STEP_MIB + return max(1, math.ceil(mib * (1 + margin) / BUDGET_STEP_MIB)) * BUDGET_STEP_MIB + + +def suggested_budget_mib(peak_mib: int, approximate: bool, held_mib: Optional[int] = None) -> int: + """Returns the suggested budget in MiB for a command with the peak. An approximate peak gets the larger margin. + + held_mib is the sampled peak of the memory that the kernel cannot take back at once. With it, the budget + covers that memory and a quarter of the peak, and it is never more than the budget from the peak alone. + """ + from_peak = _with_margin(peak_mib, approximate) + if held_mib is None: + return from_peak + # A command that reads the same files again and again slows down when the cap leaves no room for their cache. + cache_floor = _with_margin(math.ceil(peak_mib / CACHE_FLOOR_DIVISOR), approximate) + return min(from_peak, max(_with_margin(held_mib, True), cache_floor)) + + +def _larger(current: Optional[int], read: Callable[[], Optional[int]]) -> Optional[int]: + try: + value = read() + except Exception: + value = None + if value is not None and (current is None or value > current): + return value + return current class PeakTracker(threading.Thread): - def __init__(self, read_sample: Callable[[], Optional[int]], interval: float) -> None: + """Keeps the largest sample of read_sample in peak, and of read_held in held, until finish. + + held stays None without read_held or when read_held gives no value. + """ + + def __init__( + self, read_sample: Callable[[], Optional[int]], interval: float, + read_held: Optional[Callable[[], Optional[int]]] = None, + ) -> None: super().__init__(daemon=True) self.read_sample = read_sample + self.read_held = read_held self.interval = interval self.peak: Optional[int] = None + self.held: Optional[int] = None self.done = threading.Event() def run(self) -> None: @@ -888,12 +952,9 @@ def run(self) -> None: return def sample(self) -> None: - try: - value = self.read_sample() - except Exception: - value = None - if value is not None and (self.peak is None or value > self.peak): - self.peak = value + self.peak = _larger(self.peak, self.read_sample) + if self.read_held is not None: + self.held = _larger(self.held, self.read_held) def finish(self) -> Optional[int]: self.done.set() @@ -1263,12 +1324,17 @@ def start_tracker(pid: int) -> None: def read_sample() -> Optional[int]: found = cgroup_directory(pid, unit) return cgroup_peak_mib(found) if found is not None else None + + def read_held() -> Optional[int]: + found = cgroup_directory(pid, unit) + return cgroup_held_mib(found) if found is not None else None interval = min(settings.sample_seconds, 0.5) else: def read_sample() -> Optional[int]: return group_rss_mib(pid) + read_held = None interval = settings.sample_seconds - tracker = PeakTracker(read_sample, interval) + tracker = PeakTracker(read_sample, interval, read_held) tracker.start() trackers.append(tracker) @@ -1303,6 +1369,7 @@ def on_start(pid: int) -> None: ) code, maxrss = run_command(command, child_environ, on_start) tracked = trackers[0].finish() if trackers else None + held = trackers[0].held if trackers else None finally: unlock_memory() if capped: @@ -1311,14 +1378,16 @@ def on_start(pid: int) -> None: exact = reported is not None peak = max(reported or tracked or 0, rusage_peak_mib(maxrss)) - log("budget %d MiB, peak %d MiB (%s), exit %d" % (budget, peak, "cgroup" if exact else "sampled, approximate", code)) + log("budget %d MiB, peak %d MiB (%s), held %s, exit %d" % ( + budget, peak, "cgroup" if exact else "sampled, approximate", "-" if held is None else "%d MiB (sampled)" % held, code)) if capped and code == 137: log("the command was killed; if the peak is near the budget, the hard cap stopped it") - suggested = suggested_budget_mib(peak, not exact) + suggested = suggested_budget_mib(peak, not exact, held) if options.measure: log("suggested budget %d MiB" % suggested) - log("row label=%s cpus=%s budget=%dM peak=%dM exact=%s exit=%d" % ( - options.label or "-", options.cpus if options.cpus is not None else "-", suggested, peak, "yes" if exact else "no", code)) + log("row label=%s cpus=%s budget=%dM peak=%dM held=%s exact=%s exit=%d" % ( + options.label or "-", options.cpus if options.cpus is not None else "-", suggested, peak, + "-" if held is None else "%dM" % held, "yes" if exact else "no", code)) return code, suggested diff --git a/skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py b/skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py index 37340ac..453bb76 100644 --- a/skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py +++ b/skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py @@ -1306,6 +1306,66 @@ def test_the_suggested_budget_has_a_margin(self): self.assertEqual(bounded_run.suggested_budget_mib(4000, approximate=False), 5120) self.assertEqual(bounded_run.suggested_budget_mib(4000, approximate=True), 6144) + def test_a_peak_that_is_mostly_file_cache_gets_a_budget_near_its_held_memory_or_the_cache_floor(self): + # 300 MiB held gives 512 MiB with the margin of a sample; a quarter of the 8000 MiB peak gives 2560 MiB. + self.assertEqual(bounded_run.suggested_budget_mib(8000, approximate=False, held_mib=300), 2560) + + def test_held_memory_that_grows_gets_a_budget_that_covers_it(self): + self.assertEqual(bounded_run.suggested_budget_mib(8000, approximate=False, held_mib=5000), 7680) + + def test_the_budget_from_held_memory_is_never_more_than_the_budget_from_the_peak(self): + self.assertEqual(bounded_run.suggested_budget_mib(4000, approximate=False, held_mib=3900), 5120) + + def test_no_held_memory_gives_the_budget_from_the_peak(self): + self.assertEqual(bounded_run.suggested_budget_mib(4000, approximate=False, held_mib=None), 5120) + + def write_stat(self, directory, **fields): + Path(directory, "memory.stat").write_text("".join("%s %d\n" % (name, value * 1024 * 1024) for name, value in fields.items())) + + def test_held_memory_is_what_the_kernel_cannot_take_back_at_once(self): + with tempfile.TemporaryDirectory() as directory: + self.write_stat(directory, anon=100, file=5000, kernel=20, kernel_stack=1, slab_unreclaimable=2, shmem=30, + file_dirty=40, file_writeback=50, inactive_file=4000, active_file=900, slab_reclaimable=10) + self.assertEqual(bounded_run.cgroup_held_mib(directory), 230) + + def test_held_memory_without_the_kernel_line_adds_the_parts_of_kernel_memory(self): + with tempfile.TemporaryDirectory() as directory: + self.write_stat(directory, anon=100, kernel_stack=1, slab_unreclaimable=2, sock=3, percpu=4, pagetables=50, + sec_pagetables=6, vmalloc=7, slab_reclaimable=60) + self.assertEqual(bounded_run.cgroup_held_mib(directory), 173) + + def test_held_memory_counts_only_the_lines_that_exist(self): + with tempfile.TemporaryDirectory() as directory: + self.write_stat(directory, anon=100, file=5000, inactive_file=5000) + self.assertEqual(bounded_run.cgroup_held_mib(directory), 100) + + def test_dirty_file_pages_are_held(self): + with tempfile.TemporaryDirectory() as directory: + self.write_stat(directory, anon=10, file=3000, file_dirty=1500, file_writeback=500) + self.assertEqual(bounded_run.cgroup_held_mib(directory), 2010) + + def test_a_cgroup_without_a_readable_stat_file_has_no_held_memory(self): + with tempfile.TemporaryDirectory() as directory: + self.assertIsNone(bounded_run.cgroup_held_mib(directory)) + Path(directory, "memory.stat").write_text("file 100\ninactive_file 100\n") + self.assertIsNone(bounded_run.cgroup_held_mib(directory)) + + def test_the_tracker_keeps_the_largest_held_sample(self): + samples = [100, None, 900, 300] + tracker = bounded_run.PeakTracker(lambda: 1000, 0.01, read_held=lambda: samples.pop(0) if samples else 300) + tracker.start() + deadline = time.time() + 5 + while samples and time.time() < deadline: + time.sleep(0.01) + self.assertEqual(tracker.finish(), 1000) + self.assertEqual(tracker.held, 900) + + def test_a_tracker_without_a_held_reader_has_no_held_memory(self): + tracker = bounded_run.PeakTracker(lambda: 1000, 0.01) + tracker.start() + tracker.finish() + self.assertIsNone(tracker.held) + def test_the_tracker_keeps_the_largest_sample(self): samples = [100, None, 900, 300] tracker = bounded_run.PeakTracker(lambda: samples.pop(0) if samples else 300, 0.01) @@ -1876,11 +1936,44 @@ def row_lines(self): def test_a_measurement_prints_one_row_line(self): self.assertEqual(self.run_bounded(["--measure", "--cpus", "4", "--label", "unit-tests"], [(0, 1000)]), 0) - self.assertEqual(self.row_lines(), ["row label=unit-tests cpus=4 budget=1536M peak=1000M exact=no exit=0"]) + self.assertEqual(self.row_lines(), ["row label=unit-tests cpus=4 budget=1536M peak=1000M held=- exact=no exit=0"]) def test_a_row_line_marks_the_options_that_were_not_given(self): self.run_bounded(["--measure"], [(3, 100)]) - self.assertEqual(self.row_lines(), ["row label=- cpus=- budget=256M peak=100M exact=no exit=3"]) + self.assertEqual(self.row_lines(), ["row label=- cpus=- budget=256M peak=100M held=- exact=no exit=3"]) + + def run_capped(self, arguments, peak_mib, stat): + """Runs bounded() as if under the hard cap, with a cgroup of fake files whose memory.stat holds stat in MiB.""" + cgroup = os.path.join(self.base.name, "cgroup") + os.makedirs(cgroup, exist_ok=True) + Path(cgroup, "memory.peak").write_text("%d\n" % (peak_mib * 1024 * 1024)) + Path(cgroup, "memory.stat").write_text("".join("%s %d\n" % (name, value * 1024 * 1024) for name, value in stat.items())) + environ = dict(self.environ) + del environ["BOUNDED_RUN_NO_CAP"] + + def fake_run_command(command, environ, on_start=None): + on_start(os.getpid()) + return 0, 0 + + options = bounded_run.parse_arguments(arguments + ["--", sys.executable, "-c", "pass"]) + with mock.patch.object(bounded_run, "run_command", fake_run_command), \ + mock.patch.object(bounded_run, "choose_cap_prefix", return_value=(["true"], "")), \ + mock.patch.object(bounded_run, "cgroup_directory", return_value=cgroup), \ + mock.patch.object(bounded_run, "read_peak_file", return_value=peak_mib): + return bounded_run.bounded(options, environ) + + def test_a_capped_command_whose_peak_is_mostly_file_cache_gets_a_budget_from_its_held_memory(self): + self.assertEqual(self.run_capped(["--measure", "--memory", "8G"], 8000, {"anon": 300, "file": 7600, "inactive_file": 7000}), 0) + self.assertEqual(self.row_lines(), ["row label=- cpus=- budget=2560M peak=8000M held=300M exact=yes exit=0"]) + self.assertIn("budget 8192 MiB, peak 8000 MiB (cgroup), held 300 MiB (sampled), exit 0", self.lines) + + def test_a_capped_command_whose_held_memory_is_large_gets_a_budget_that_covers_it(self): + self.run_capped(["--measure", "--memory", "8G"], 8000, {"anon": 4000, "file_dirty": 1000, "file": 3500}) + self.assertEqual(self.row_lines(), ["row label=- cpus=- budget=7680M peak=8000M held=5000M exact=yes exit=0"]) + + def test_a_capped_command_with_no_held_memory_in_its_stat_file_gets_the_budget_from_its_peak(self): + self.run_capped(["--measure", "--memory", "8G"], 8000, {}) + self.assertEqual(self.row_lines()[0], "row label=- cpus=- budget=10240M peak=8000M held=- exact=yes exit=0") def test_a_run_that_does_not_measure_prints_no_row_line(self): self.run_bounded(["--memory", "2G"], [(0, 100)]) @@ -1892,9 +1985,9 @@ def test_rows_run_largest_first_and_each_smaller_row_gets_the_budget_of_the_row_ # The first row has no budget yet and no cap, so it holds the whole queue of 8192 MiB. self.assertEqual(self.runs, [(8, 8192), (4, 1536), (2, 768)]) self.assertEqual(self.row_lines(), [ - "row label=build cpus=8 budget=1536M peak=1000M exact=no exit=0", - "row label=build cpus=4 budget=768M peak=500M exact=no exit=0", - "row label=build cpus=2 budget=512M peak=200M exact=no exit=0", + "row label=build cpus=8 budget=1536M peak=1000M held=- exact=no exit=0", + "row label=build cpus=4 budget=768M peak=500M held=- exact=no exit=0", + "row label=build cpus=2 budget=512M peak=200M held=- exact=no exit=0", ]) def test_the_largest_row_runs_with_the_given_memory(self): @@ -2685,7 +2778,7 @@ def test_an_exclusive_name_in_a_removed_working_directory_gives_125(self): def test_each_run_reports_the_budget_and_the_peak(self): process = self.wrapper(["--memory", "2G"], [sys.executable, "-c", "pass"]) output, errors = process.communicate(timeout=60) - self.assertRegex(errors, r"bounded-run: budget 2048 MiB, peak \d+ MiB \(sampled, approximate\), exit 0\n") + self.assertRegex(errors, r"bounded-run: budget 2048 MiB, peak \d+ MiB \(sampled, approximate\), held -, exit 0\n") self.assertNotIn("suggested budget", errors) def test_the_default_budget_on_a_small_machine_is_not_more_than_its_memory(self): @@ -2781,7 +2874,7 @@ def test_measured_rows_print_their_row_lines_in_order(self): output, errors = process.communicate(timeout=60) self.assertEqual(process.returncode, 0, errors) self.assertEqual(output.split(), ["2", "1"]) - rows = re.findall(r"^bounded-run: row label=lint cpus=(\d+) budget=\d+M peak=\d+M exact=no exit=0$", errors, re.MULTILINE) + rows = re.findall(r"^bounded-run: row label=lint cpus=(\d+) budget=\d+M peak=\d+M held=- exact=no exit=0$", errors, re.MULTILINE) self.assertEqual(rows, ["2", "1"], errors) def test_an_interrupt_in_the_queue_gives_130_and_no_traceback(self): @@ -2927,7 +3020,7 @@ def test_a_short_command_gets_its_peak_from_the_cgroup(self): process = self.wrapper(["--memory", "1G", "--cpus", "2"], [sys.executable, "-c", "print(1)"]) output, errors = process.communicate(timeout=60) self.assertEqual((process.returncode, output), (0, "1\n"), errors) - self.assertRegex(errors, r"bounded-run: budget 1024 MiB, peak \d+ MiB \(cgroup\), exit 0\n") + self.assertRegex(errors, r"bounded-run: budget 1024 MiB, peak \d+ MiB \(cgroup\), held (\d+ MiB \(sampled\)|-), exit 0\n") locks = os.listdir(self.environ["BOUNDED_RUN_LOCK_DIR"]) self.assertEqual([name for name in locks if name.startswith("peak-")], []) @@ -2962,6 +3055,9 @@ def test_the_peak_comes_from_the_cgroup(self): peak = [line for line in errors.splitlines() if " peak " in line][0] self.assertIn("(cgroup)", peak) self.assertGreaterEqual(int(peak.split(" peak ")[1].split()[0]), 140) + held = re.search(r"^bounded-run: row .* held=(\d+)M exact=yes exit=0$", errors, re.MULTILINE) + self.assertIsNotNone(held, errors) + self.assertGreaterEqual(int(held.group(1)), 140) def test_no_peak_file_is_left_behind_after_a_failing_command(self): process = self.wrapper(["--memory", "1G"], [sys.executable, "-c", "import sys; sys.exit(9)"])