Cap Rust's build jobs by memory; no page-wait kills in the VM

The Rust build in the 12 GB VM lost two llvm-tblgen 2.8 hours in:
FreeBSD kills a process that has waited 30 seconds for a page, and the
emulated VM's pageout took that long with the swap unused. setup turns
those kills off (vm.pfault_oom_attempts=-1), and build caps lang/rust,
which builds its own LLVM with a job per CPU, at one job per 768 MB
through tools' make.conf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jeremy Karst 2026-11-20 17:46:16 -05:00
parent fc2481ecac
commit 98b6a457ad
3 changed files with 81 additions and 4 deletions

View file

@ -177,6 +177,13 @@ release/vm.sh sync 14.3 # this repository (with .git) -> /usr/deps
not on ZFS, where swap can deadlock when memory runs out, and past the
host's page cache (`cache=none`). Builds that need more than the VM has
(Rust, LLVM) swap inside the VM instead of pressing on the host.
FreeBSD kills a process that has waited 30 seconds for a free page, and
the emulated VM's pageout can take that long: later that day the Rust
build lost two `llvm-tblgen` that way, 2.8 hours in, with the swap
unused. `setup` turns those kills off (`vm.pfault_oom_attempts=-1`).
On every build host, `build` caps `lang/rust`, which builds its own LLVM
with a job per CPU, at one job per 768 MB of memory (15 in the 12 GB VM,
where 24 did not fit), through tools' `make.conf`.
- **Restarts:** the VM does not start with the build host. After a reboot,
`vm.sh start` it and run `build` again: finished sets are reused. The
ports step writes its set only when it ends (below); `build` first adds

View file

@ -116,9 +116,20 @@ checkout_all()
git ${COMMITTER} -C /usr/core am -q --committer-date-is-author-date ${PATCHES}
echo " /usr/core: $(git -C /usr/core describe --tags) with $(echo "${PATCHES}" | wc -l | tr -d ' ') patches"
cp "${DEPS}/config/GATEWAY.conf" /usr/tools/device/
ports_make_conf "/usr/tools/config/${SERIES}/make.conf"
echo " /usr/deps: $(git -C "${DEPS}" describe --tags --always --dirty)"
}
# ports_make_conf FILE: settings for the ports builds, added to tools' make.conf
# (checkout resets it). lang/rust builds its own LLVM with a job per CPU; 24 jobs
# outgrew the 12 GB VM (2026-11-20). One job per 768 MB of memory fits.
ports_make_conf()
{
_jobs=$(($(sysctl -n hw.physmem) / 805306368))
[ "${_jobs}" -ge 1 ] || _jobs=1
printf 'lang_rust_VARS=\tMAKE_JOBS_NUMBER_LIMIT=%s\n' "${_jobs}" >> "${1}"
}
# tools insists on a host pkg of the same major.minor as the ports tree's. Its
# distfiles and work directory stay out of /usr/ports: tools mounts the tree
# into its chroots with unionfs and replaces distfiles/ there, which fails on
@ -365,7 +376,10 @@ setup()
# vm_memory: in the build VM (it has the disk with serial gwswap that vmhost.sh
# attaches), swap on that disk and a ZFS cache of at most 2 GB. The build host backs
# every page of the VM's memory, and a cache grown into all of it leaves the host
# short; swap on ZFS itself can deadlock when memory runs out. Elsewhere, nothing.
# short; swap on ZFS itself can deadlock when memory runs out. A process waiting for
# a page is not killed: FreeBSD gives up after 30 seconds, which the emulated VM's
# pageout can take (2026-11-20: two llvm-tblgen, with 16 GB of swap unused).
# Elsewhere, nothing.
vm_memory()
{
for _d in $(sysctl -n kern.disks); do
@ -381,6 +395,9 @@ vm_memory()
grep -qx 'vfs.zfs.arc_max="2147483648"' /boot/loader.conf ||
echo 'vfs.zfs.arc_max="2147483648"' >> /boot/loader.conf
sysctl vfs.zfs.arc_max=2147483648
grep -qx 'vm.pfault_oom_attempts=-1' /etc/sysctl.conf ||
echo 'vm.pfault_oom_attempts=-1' >> /etc/sysctl.conf
sysctl vm.pfault_oom_attempts=-1
swapctl -lh
return 0
done

View file

@ -877,8 +877,12 @@ class VmMemoryTest(unittest.TestCase):
self.loader = os.path.join(self.tmp, "loader.conf")
with open(self.loader, "w") as f:
f.write('zfs_load="YES"\n')
self.sysctl_conf = os.path.join(self.tmp, "sysctl.conf")
with open(self.sysctl_conf, "w") as f:
f.write("# sysctl.conf\n")
self.func = build_function("vm_memory", [("/etc/fstab", self.fstab), ("/dev/gpt/", self.gpt + "/"),
("/boot/loader.conf", self.loader)])
("/boot/loader.conf", self.loader),
("/etc/sysctl.conf", self.sysctl_conf)])
fakes = {
"sysctl": 'case "$*" in "-n kern.disks") echo "${FAKE_DISKS}";; esac',
"diskinfo": 'eval "echo \\${FAKE_IDENT_${2#/dev/}:-}"',
@ -906,19 +910,26 @@ class VmMemoryTest(unittest.TestCase):
with open(self.loader) as f:
return f.read().splitlines()
def sysctl_lines(self):
with open(self.sysctl_conf) as f:
return f.read().splitlines()
def test_not_in_the_vm(self):
self.assertEqual(self.vm_memory("vtbd0 vtbd1", vtbd0="BHYVE-1234", vtbd1=""), [])
self.assertEqual(self.vm_memory(""), [])
self.assertEqual(len(self.fstab_lines()), 1)
self.assertEqual(self.loader_lines(), ['zfs_load="YES"'])
self.assertEqual(self.sysctl_lines(), ["# sysctl.conf"])
def test_sets_up_swap_and_cache(self):
# the swap disk need not be the last one (it came up as vtbd0 in the VM)
calls = self.vm_memory("vtbd0 vtbd1 vtbd2", vtbd0="", vtbd2="gwswap")
self.assertEqual(calls, ["gpart create -s gpt vtbd2", "gpart add -t freebsd-swap -l gwswap vtbd2",
f"swapon {self.gpt}/gwswap", "sysctl vfs.zfs.arc_max=2147483648"])
f"swapon {self.gpt}/gwswap", "sysctl vfs.zfs.arc_max=2147483648",
"sysctl vm.pfault_oom_attempts=-1"])
self.assertEqual(self.fstab_lines()[1], f"{self.gpt}/gwswap\tnone\tswap\tsw\t0\t0")
self.assertEqual(self.loader_lines(), ['zfs_load="YES"', 'vfs.zfs.arc_max="2147483648"'])
self.assertEqual(self.sysctl_lines(), ["# sysctl.conf", "vm.pfault_oom_attempts=-1"])
def test_overrides_an_earlier_cache_setting(self):
with open(self.loader, "a") as f:
@ -929,9 +940,16 @@ class VmMemoryTest(unittest.TestCase):
def test_again_changes_nothing_on_the_disk(self):
self.vm_memory("vtbd0 vtbd2", vtbd2="gwswap")
calls = self.vm_memory("vtbd0 vtbd2", vtbd2="gwswap")
self.assertEqual(calls, ["sysctl vfs.zfs.arc_max=2147483648"])
self.assertEqual(calls, ["sysctl vfs.zfs.arc_max=2147483648", "sysctl vm.pfault_oom_attempts=-1"])
self.assertEqual(len(self.fstab_lines()), 2)
self.assertEqual(len(self.loader_lines()), 2)
self.assertEqual(len(self.sysctl_lines()), 2)
def test_overrides_an_earlier_page_wait_setting(self):
with open(self.sysctl_conf, "a") as f:
f.write("vm.pfault_oom_attempts=3\n")
self.vm_memory("vtbd0", vtbd0="gwswap")
self.assertEqual(self.sysctl_lines()[-1], "vm.pfault_oom_attempts=-1")
def test_after_a_reboot_swap_is_turned_on_again(self):
self.vm_memory("vtbd2", vtbd2="gwswap")
@ -940,5 +958,40 @@ class VmMemoryTest(unittest.TestCase):
self.assertEqual(len(self.fstab_lines()), 2)
class PortsMakeConfTest(unittest.TestCase):
"""build.sh's ports_make_conf: lang/rust's jobs capped at one per 768 MB."""
def setUp(self):
self.tmp = tempfile.mkdtemp(prefix="gw-mkconf-")
self.addCleanup(shutil.rmtree, self.tmp)
self.fx = Fixture(self.tmp)
self.fx.make_fake("sysctl", '[ "$*" = "-n hw.physmem" ] && echo "${FAKE_PHYSMEM}"')
self.conf = os.path.join(self.tmp, "make.conf")
with open(self.conf, "w") as f:
f.write("BATCH=\t\t\tyes\n")
self.func = build_function("ports_make_conf")
def appended(self, physmem):
env = {"PATH": f"{self.fx.bin}:/usr/bin:/bin", "FAKE_LOG": self.fx.log, "FAKE_PHYSMEM": str(physmem)}
r = subprocess.run(["sh", "-c", self.func + '\nports_make_conf "$1"', "sh", self.conf],
env=env, capture_output=True, text=True)
self.assertEqual(r.returncode, 0, r.stderr)
with open(self.conf) as f:
lines = f.read().splitlines()
self.assertEqual(lines[0], "BATCH=\t\t\tyes")
return lines[-1]
def test_build_vm(self):
# the 12 GB VM's hw.physmem
self.assertEqual(self.appended(12851638272), "lang_rust_VARS=\tMAKE_JOBS_NUMBER_LIMIT=15")
def test_large_host(self):
self.assertEqual(self.appended(64 << 30), "lang_rust_VARS=\tMAKE_JOBS_NUMBER_LIMIT=85")
def test_at_least_one_job(self):
self.assertEqual(self.appended(805306367), "lang_rust_VARS=\tMAKE_JOBS_NUMBER_LIMIT=1")
self.assertEqual(self.appended(805306368), "lang_rust_VARS=\tMAKE_JOBS_NUMBER_LIMIT=1")
if __name__ == "__main__":
unittest.main()