1 #!/bin/sh
2 # moxbox - run a command inside the 4 GB memory box.
3 #
4 # Every compiler build, every test, every soak, and every binary the compiler
5 # produced runs through this. The box is a transient systemd scope with
6 # MemoryMax=4G, MemoryHigh=3500M and MemorySwapMax=0.
7 #
8 # Swap is disabled on purpose. With swap the cgroup reclaims to stay under Max:
9 # the run stays inside 4 GB by paging, the fan runs all day, and nothing fails.
10 # That bounds the symptom instead of catching the bug. With MemorySwapMax=0 the
11 # limit is an immediate OOM kill - a fast, loud failure you can read.
12 #
13 # MemoryHigh sits just below Max so the run is throttled before it is killed.
14 # That window is where you attach gdb or read the arena accounting while the
15 # process is still alive; Max is the backstop for when nobody is watching.
16 #
17 # 4G, not 6G: a 64-bit build that cannot fit in 32 bits of address space is
18 # broken, and whatever pushed it past 4G is the defect to find.
19 #
20 # scripts/moxbox.sh <command...> run a command in the box
21 # scripts/moxbox.sh interactive shell already inside the box
22 #
23 # Exit codes, which decide how you read the result:
24 # 137 SIGKILL - the cgroup OOM killer, or `timeout -s KILL`
25 # 139 SIGSEGV - a fault in the program
26 # 134 SIGABRT - a panic or an assertion
27 #
28 # Before concluding the compiler has a leak, check WHICH process died. LLVM's
29 # `opt` and `ld.lld` take 1-2 GB on a large module, and the kernel names
30 # whichever process was biggest. The OOM message is in the kernel journal, not
31 # the user one: journalctl -k | grep -i oom, or sudo dmesg | tail.
32 #
33 # The unit is named so it can be polled while it runs:
34 # systemctl --user show mxbuild.scope -p MemoryCurrent,MemoryPeak
35 # A transient scope keeps its name for a moment after it exits, so a new run
36 # destroys the old unit first rather than failing to start.
37 #
38 # CPUs: the box is pinned to TWO hardware threads with a cpuset. That is the
39 # resource boundary, and it is deliberately not a job-count knob - `-j`, the
40 # worker pool and TasksMax all describe how the work is scheduled, and the whole
41 # point of the box is that it must fit in what it is given. The scheduler is
42 # then free to put those two threads wherever it likes on the two CPUs and
43 # nothing else may use them.
44 #
45 # MXBOX_CPUS names the set (systemd cpuset syntax). Default 0-1.
46 #
47 # The pin is applied TWICE on purpose: AllowedCPUs on the scope sets the cgroup
48 # cpuset, and `taskset -c` sets the affinity mask of the process systemd spawns,
49 # which every descendant inherits. Measured: AllowedCPUs alone left
50 # Cpus_allowed_list at 0-7 for the command running inside the scope, and
51 # `taskset -c 0-1` inside it reports 0-1. Both are kept so the cgroup and the
52 # process tree agree.
53 #
54 # This also sizes the compiler's pool without a flag: it asks nproc, and a
55 # process pinned to two CPUs answers 2, so its heuristic (nproc-1) selects one
56 # worker. The work fits what it is given - which is the point of the box.
57 #
58 # TasksMax is not set: at 2 even `sh -c 'echo $(ls /proc/self/task)'` fails with
59 # "fork: Resource temporarily unavailable" because the limit counts every task,
60 # and at 8 LLVM's linker dies with std::system_error. It is not the lever.
61 set -u
62 MEM=4G
63 HIGH=3500M
64 CPUS=${MXBOX_CPUS:-0-1}
65 systemctl --user stop mxbuild.scope 2>/dev/null
66 if [ $# -eq 0 ]; then
67 exec systemd-run --user --scope --quiet --collect --unit=mxbuild \
68 -p MemoryMax=$MEM -p MemoryHigh=$HIGH -p MemorySwapMax=0 \
69 -p AllowedCPUs=$CPUS -- taskset -c $CPUS "${SHELL:-/bin/sh}"
70 fi
71 exec systemd-run --user --scope --quiet --collect --unit=mxbuild \
72 -p MemoryMax=$MEM -p MemoryHigh=$HIGH -p MemorySwapMax=0 \
73 -p AllowedCPUs=$CPUS -- taskset -c $CPUS "$@"
74