diff --git a/tests/compile_budget_test.py b/tests/compile_budget_test.py new file mode 100644 index 00000000..932813cd --- /dev/null +++ b/tests/compile_budget_test.py @@ -0,0 +1,37 @@ +"""tools/build_game.py compile_budget: the compile jobs fit the memory that is free, not only the RAM. + + python tests/compile_budget_test.py +""" +import sys +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools')) +from build_game import GiB, JOB_MEM, MEM_BUDGET, compile_budget # noqa: E402 + + +def jobs(budget, cpus=16): + return max(1, min(cpus, budget // JOB_MEM)) # as build_game.main + + +class CompileBudgetTest(unittest.TestCase): + def test_free_memory_limits_a_pc_whose_ram_other_programs_hold(self): + self.assertEqual(jobs(compile_budget(8 * GiB, None)), 8) # the RAM alone: 6.4 GiB, 8 jobs + self.assertEqual(jobs(compile_budget(8 * GiB, 3 * GiB)), 3) # 3 GiB free: 2.4 GiB, 3 jobs + self.assertEqual(compile_budget(8 * GiB, 3 * GiB), int(3 * GiB * 0.8)) + + def test_ram_still_limits_when_more_is_reported_free(self): + self.assertEqual(compile_budget(4 * GiB, 64 * GiB), int(4 * GiB * 0.8)) + + def test_unknown_free_memory_keeps_the_old_budget(self): + self.assertEqual(compile_budget(4 * GiB, None), int(4 * GiB * 0.8)) + self.assertEqual(compile_budget(128 * GiB, None), MEM_BUDGET) + + def test_no_free_memory_still_runs_one_job(self): + self.assertEqual(compile_budget(4 * GiB, 0), 0) + self.assertEqual(compile_budget(4 * GiB, -1), 0) + self.assertEqual(jobs(compile_budget(4 * GiB, 0)), 1) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/run_all.py b/tests/run_all.py index 7b4ee957..2b6b4910 100644 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -49,6 +49,7 @@ TESTS = { 'flags_check': ('.venv', None, [], ''), 'excl_mt': ('.venv', None, [], ''), 'check_isa_test': ('.venv', None, [], ''), + 'compile_budget_test': ('.venv', None, [], ''), 'pack_shader_cache_test': ('.venv', None, [], ''), 'spin_waits_test': ('.venv', None, [], ''), 'worktree_test': ('.venv', None, [], ''), diff --git a/tools/build_game.py b/tools/build_game.py index e8d2aaf3..d660f4cd 100644 --- a/tools/build_game.py +++ b/tools/build_game.py @@ -42,8 +42,9 @@ objects Ninja just rebuilt (new mtime). A mismatch deletes the object, Ninja rec summary line. A fork checks the hardlinked generated objects against build//obj/objects.sha256 (read-only; a bad shared object stops the fork and asks for a main build run, which recompiles it and breaks the shared inode). -Compile jobs share a memory budget (MEM_BUDGET, default the smaller of 50 GiB and 80% of RAM) at JOB_MEM per job, so a -big machine does not run out of memory on the largest chunks. Each phase reports its wall time and the peak memory +Compile jobs share a memory budget (compile_budget: the smaller of MEM_BUDGET = 50 GiB, 80% of RAM and, on Windows, +80% of the memory free at the start) at JOB_MEM per job, so a big machine does not run out of memory on the largest +chunks, nor a small one already running a browser and the setup. Each phase reports its wall time and the peak memory of its processes (Windows job object accounting: committed private bytes). --ninja PATH names the Ninja to run; without MSYS2 the bin/ninja.exe of the release folder (tools/make_release.py) is used. @@ -77,6 +78,7 @@ import urllib.request from pathlib import Path import check_isa +import hostmem import romfs_index from build_shaders import stale as stale_shaders from check_content import GENERATED_MARKER @@ -220,6 +222,18 @@ GiB = 1 << 30 # (295k-entry tables). 768 MiB leaves headroom for the largest file. JOB_MEM = 768 << 20 MEM_BUDGET = 50 * GiB # total for all compile jobs +MEM_SHARE = 0.8 # of the RAM, and of the free memory, the compile jobs may take + + +def compile_budget(total, free): + """Bytes the compile jobs may take together: MEM_BUDGET, MEM_SHARE of the RAM (total) and MEM_SHARE of the memory + free now (free: hostmem.available_ram, None when unknown). The total alone counted the RAM other programs hold: on + an 8 GiB PC with 3 GiB free it allowed 8 jobs of JOB_MEM (6 GiB), so the compile paged or a job ran out of memory. + recomp/gen.py sizes the lift workers from the free memory the same way.""" + budget = min(MEM_BUDGET, int(total * MEM_SHARE)) + if free is not None: + budget = min(budget, int(max(0, free) * MEM_SHARE)) + return budget def below_root(p): @@ -1179,8 +1193,12 @@ def main(): mods = {} if args.make_runtime_lib else {m: chunk_files(gen, m) for m in linked} cpus = os.cpu_count() or 1 - budget = min(MEM_BUDGET, int(total_ram() * 0.8)) + free = hostmem.available_ram() + budget = compile_budget(total_ram(), free) jobs = args.jobs or max(1, min(cpus, budget // JOB_MEM)) + if not args.jobs: + print(f'build_game: {cpus} CPUs, {mib(budget)} for compile jobs' + f'{"" if free is None else f" ({mib(free)} free memory)"}', flush=True) plugins = read_plugin_files(gen) missing = [n for n in names if n not in hashes and n not in plugins] if missing: