forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB2602aecTahinli6 days ago3958 commits

remote-run.sh276 lines, 10.5 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# remote-run.sh: run heavy cargo commands on the lane servers instead of this machine.
3#
4# Usage:
5# scripts/remote-run.sh <lane> [--host auto|a|b|c] [--with-edith] [--clean] -- <command...>
6# scripts/remote-run.sh <lane> --clean
7#
8# --host auto|a|b|c auto (default) probes a, b and c each round (one ssh each: busy slots and
9# MemAvailable) and tries the least loaded first: fewest busy slots, then most
10# MemAvailable. Unreachable hosts are skipped. It retries for up to 10 minutes
11# (20 s between rounds), then builds locally.
12# --with-edith also sync the edith tree ($EDITH_TREE, default
13# $HOME/Documents/Code/Rust/edith) to ~/lanes/<lane>/edith and run
14# the command inside it. Its path deps ../../../edith_codecs resolve to
15# ~/lanes/<lane>/edith_codecs.
16# --clean remove ~/lanes/<lane>, ~/.cache/tgt-<lane> and ~/tmp/<lane> on every host,
17# and ~/.cache/tgt-<lane>, ~/.cache/tmp-<lane> locally.
18#
19# The source tree is the git toplevel of the current directory (worktrees work). It is rsynced
20# to ~/lanes/<lane>/edith_codecs on the host, without target/, fixtures/ and .git. The host gets
21# fixtures -> ~/fixtures as a symlink. The command runs under the host's slot lock (~/.remote-slot),
22# so one heavy job at a time per server.
23#
24# Three job slots per host (~/.remote-slot.0, .1 and .2), each with CARGO_BUILD_JOBS=2.
25# Remote env: CARGO_TARGET_DIR=~/.cache/tgt-<lane>, TMPDIR=~/tmp/<lane>, CARGO_BUILD_JOBS=2,
26# EC_MEMGUARD_MAX=3500M (the memory cap read by scripts/memguard-runner.sh). Every EC_* variable
27# from the local environment is forwarded and wins over the defaults.
28#
29# Exit codes: the remote command's code. 75 means the slot stayed busy (with --host a or b, after
30# 10 minutes). A command that itself exits 75 looks the same and is retried on the next host.
31# 255 means the chosen host was unreachable. stderr shows "remote-run: host=a|b|c|local" when a run starts.
32
33set -uo pipefail
34
35[ -f "$HOME/.config/edith_codecs/remote-hosts" ] && . "$HOME/.config/edith_codecs/remote-hosts"
36HOST_A=${EC_HOST_A:-}
37HOST_B=${EC_HOST_B:-}
38HOST_C=${EC_HOST_C:-}
39EDITH_TREE=${EDITH_TREE:-$HOME/Documents/Code/Rust/edith}
40EDITH_ASSETS=${EDITH_ASSETS:-$HOME/Documents/Code/Rust/edith/assets}
41SSH=(ssh -o BatchMode=yes -o ConnectTimeout=20)
42RSYNC_RSH="ssh -o BatchMode=yes -o ConnectTimeout=20"
43BUSY=75
44UNREACH=254
45RETRY_SECS=20
46RETRY_TOTAL=600
47
48die() { echo "remote-run: $*" >&2; exit 2; }
49
50[ $# -ge 1 ] || { sed -n '2,25p' "$0" >&2; exit 2; }
51case $1 in
52 -h|--help) sed -n '2,25p' "$0" >&2; exit 0 ;;
53esac
54lane=$1
55shift
56
57host=auto
58with_edith=0
59clean=0
60cmd=()
61while [ $# -gt 0 ]; do
62 case $1 in
63 --host) [ $# -ge 2 ] || die "--host needs a value"; host=$2; shift 2 ;;
64 --host=*) host=${1#--host=}; shift ;;
65 --with-edith) with_edith=1; shift ;;
66 --clean) clean=1; shift ;;
67 --) shift; cmd=("$@"); break ;;
68 *) die "unknown argument: $1 (put the command after --)" ;;
69 esac
70done
71
72[[ $lane =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] || die "bad lane name: $lane"
73case $host in
74 auto|a|b|c) ;;
75 *) die "--host must be auto, a, b or c" ;;
76esac
77
78host_addr() { case $1 in a) echo "$HOST_A" ;; b) echo "$HOST_B" ;; c) echo "$HOST_C" ;; esac; }
79
80# rsh <ssh-target> <script>: run a bash script on the host. The script goes through stdin, so no
81# quoting reaches fish, the remote login shell.
82rsh() { printf '%s\n' "$2" | "${SSH[@]}" "$1" 'bash -c "$(cat)"'; }
83
84# Literal "$HOME/lanes/<lane>/<rel>" text, meant to be expanded by the remote bash.
85remote_path() { printf '%s' "\$HOME/lanes/$lane/$1"; }
86
87forwarded_env() {
88 local name
89 while IFS= read -r name; do
90 case $name in
91 EC_*) printf 'export %s=%q\n' "$name" "${!name}" ;;
92 esac
93 done < <(compgen -e)
94}
95
96# sync_tree <addr> <local-dir> <remote-rel> [rsync filters...]
97sync_tree() {
98 local addr=$1 src=$2 rel=$3 lock try
99 shift 3
100 # Parallel runs of one lane sync into the same remote tree; a concurrent --delete
101 # removes the other's temp files (rsync code 23), which used to read as "unreachable".
102 # Serialise syncs per lane and host, and retry a partial transfer once.
103 # --checksum --no-times: a file whose content changed lands with the host's current
104 # mtime. Kept source mtimes can be older than the host's last build (a merge of a
105 # lane edited yesterday), and cargo then reuses a stale artifact of the old content.
106 lock="${XDG_CACHE_HOME:-$HOME/.cache}/remote-sync-$lane-${addr##*@}.lock"
107 for try in 1 2; do
108 if flock "$lock" rsync -a --checksum --no-times --delete -e "$RSYNC_RSH" --exclude=__pycache__/ "$@" \
109 "$src/" "$addr:lanes/$lane/$rel/"; then
110 return 0
111 fi
112 done
113 return 1
114}
115
116# run_payload <host-letter>: the remote script that runs under the slot lock.
117run_payload() {
118 local letter=$1 cmd_q run_rel=edith_codecs
119 (( with_edith )) && run_rel=edith
120 printf -v cmd_q '%q ' "${cmd[@]}"
121 forwarded_env
122 cat <<EOF
123export PATH="\$HOME/.cargo/bin:\$PATH"
124export CARGO_TARGET_DIR="\$HOME/.cache/tgt-$lane" TMPDIR="\$HOME/tmp/$lane" CARGO_BUILD_JOBS=2
125export EC_MEMGUARD_MAX="\${EC_MEMGUARD_MAX:-3500M}"
126mkdir -p "\$TMPDIR" "\$CARGO_TARGET_DIR" || exit 1
127cd "\$HOME/lanes/$lane/edith_codecs" || exit 1
128[ -e .git ] || git init -q || exit 1
129ln -sfn "\$HOME/fixtures" fixtures # replace a local worktree link that would dangle here
130EOF
131 if (( with_edith )); then
132 cat <<EOF
133(cd "\$HOME/lanes/$lane/edith" && { [ -e .git ] || git init -q; }) || exit 1
134# libspa-sys runs bindgen; with clang-libs but no clang binary, libclang misses its own
135# builtin headers (stdbool.h), so name the newest resource include dir explicitly.
136inc=\$(ls -d /usr/lib/clang/*/include 2>/dev/null | sort -V | tail -1)
137[ -n "\$inc" ] && export BINDGEN_EXTRA_CLANG_ARGS="\${BINDGEN_EXTRA_CLANG_ARGS:+\$BINDGEN_EXTRA_CLANG_ARGS }-I\$inc"
138EOF
139 fi
140 cat <<EOF
141got=0
142for slot in 0 1 2; do
143 exec 9>"\$HOME/.remote-slot.\$slot" || exit 1
144 if flock -n 9; then got=1; break; fi
145 exec 9>&-
146done
147[ "\$got" = 1 ] || exit $BUSY
148echo "remote-run: host=$letter" >&2
149cd "\$HOME/lanes/$lane/$run_rel" || exit 1
150$cmd_q
151EOF
152}
153
154if (( clean )); then
155 for h in a b c; do
156 echo "remote-run: cleaning host=$h" >&2
157 rsh "$(host_addr "$h")" "rm -rf \"\$HOME/lanes/$lane\" \"\$HOME/.cache/tgt-$lane\" \"\$HOME/tmp/$lane\"" \
158 || echo "remote-run: clean failed on host=$h (rc $?)" >&2
159 done
160 rm -rf "$HOME/.cache/tgt-$lane" "$HOME/.cache/tmp-$lane"
161 echo "remote-run: clean done" >&2
162 exit 0
163fi
164
165(( ${#cmd[@]} > 0 )) || die "no command given (usage: $0 $lane [opts] -- <command...>)"
166ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || die "not inside a git checkout"
167if (( with_edith )) && [ ! -d "$EDITH_TREE" ]; then
168 die "edith tree not found: $EDITH_TREE"
169fi
170
171# try_host <letter>: sync, then run. Returns the remote exit code, $BUSY, or $UNREACH.
172try_host() {
173 local letter=$1 addr setup rc
174 addr=$(host_addr "$letter")
175 setup="mkdir -p \"$(remote_path edith_codecs)\""
176 (( with_edith )) && setup+=" \"$(remote_path edith)\""
177 # A worktree's assets/ may be a symlink to the main checkout; never leave one on the host.
178 (( with_edith )) && setup+="; if [ -L \"$(remote_path edith/assets)\" ]; then rm \"$(remote_path edith/assets)\"; fi"
179 rsh "$addr" "$setup" || { echo "remote-run: host=$letter setup failed" >&2; return $UNREACH; }
180 sync_tree "$addr" "$ROOT" edith_codecs --exclude=target/ --exclude=/fixtures --exclude=/.git --exclude=/graphify-out --exclude=fuzz/corpus/ --exclude=fuzz/artifacts/ --exclude=/scripts/aac-tables/sbr-cache.json --exclude=/lanes/i64/ --exclude=/lanes/b64b/ \
181 || { echo "remote-run: host=$letter rsync of edith_codecs failed" >&2; return $UNREACH; }
182 if (( with_edith )); then
183 sync_tree "$addr" "$EDITH_TREE" edith --exclude=target/ --exclude=/assets --exclude=/.git --exclude=/graphify-out \
184 || { echo "remote-run: host=$letter rsync of edith failed" >&2; return $UNREACH; }
185 # assets/ is gitignored, so worktrees lack it; the tests read it. Take the main checkout's copy (~28 MB).
186 if [[ -d "$EDITH_ASSETS" ]]; then
187 sync_tree "$addr" "$EDITH_ASSETS" edith/assets \
188 || { echo "remote-run: host=$letter rsync of edith assets failed" >&2; return $UNREACH; }
189 fi
190 fi
191 rsh "$addr" "$(run_payload "$letter")"
192 rc=$?
193 (( rc == 255 )) && return $UNREACH
194 return $rc
195}
196
197run_local() {
198 local dir=$ROOT tgt=$HOME/.cache/tgt-$lane tmp=$HOME/.cache/tmp-$lane
199 (( with_edith )) && dir=$EDITH_TREE
200 mkdir -p "$tgt" "$tmp" || exit 1
201 echo "remote-run: host=local" >&2
202 cd "$dir" || exit 1
203 CARGO_TARGET_DIR=$tgt TMPDIR=$tmp CARGO_BUILD_JOBS=4 "${cmd[@]}"
204 exit $?
205}
206
207# probe_host <ssh-target>: print "<busy slots> <MemAvailable kB>" for one host. Each slot lock is
208# taken with flock -n and dropped at once, so a running job is never disturbed.
209probe_host() {
210 local script
211 script=$(cat <<'EOF'
212busy=0
213for slot in 0 1 2; do
214 exec 9>"$HOME/.remote-slot.$slot" || exit 1
215 flock -n 9 || busy=$((busy + 1))
216 exec 9>&-
217done
218echo "$busy $(awk '/^MemAvailable:/ {print $2}' /proc/meminfo)"
219EOF
220)
221 printf '%s\n' "$script" | ssh -o BatchMode=yes -o ConnectTimeout=5 "$1" 'bash -c "$(cat)"'
222}
223
224# auto_order: print the host letters, least loaded first. Unreachable hosts are left out. If none
225# answers, print the old order c a b so try_host reports each host as unreachable as before.
226auto_order() {
227 local letter out busy mem rows=()
228 for letter in a b c; do
229 out=$(probe_host "$(host_addr "$letter")" 2>/dev/null)
230 read -r busy mem <<<"$out"
231 if [[ $busy =~ ^[0-9]+$ && $mem =~ ^[0-9]+$ ]]; then
232 rows+=("$busy $mem $letter")
233 else
234 echo "remote-run: host=$letter probe failed, skipped" >&2
235 fi
236 done
237 if (( ${#rows[@]} == 0 )); then
238 printf '%s\n' c a b
239 return
240 fi
241 printf '%s\n' "${rows[@]}" | sort -k1,1n -k2,2nr | awk '{print $3}'
242}
243
244candidates=("$host")
245deadline=$((SECONDS + RETRY_TOTAL))
246busy=0
247while :; do
248 busy=0
249 if [ "$host" = auto ]; then
250 mapfile -t candidates < <(auto_order)
251 echo "remote-run: auto order: ${candidates[*]}" >&2
252 fi
253 for letter in "${candidates[@]}"; do
254 try_host "$letter"
255 rc=$?
256 case $rc in
257 "$UNREACH") echo "remote-run: host=$letter unreachable" >&2 ;;
258 "$BUSY") busy=1; echo "remote-run: host=$letter slot busy" >&2 ;;
259 *) exit "$rc" ;;
260 esac
261 done
262 (( busy )) || break
263 (( SECONDS < deadline )) || break
264 sleep "$RETRY_SECS"
265done
266
267if [ "$host" != auto ]; then
268 if (( busy )); then
269 echo "remote-run: host=$host still busy after ${RETRY_TOTAL}s" >&2
270 exit "$BUSY"
271 fi
272 echo "remote-run: host=$host unreachable" >&2
273 exit 255
274fi
275echo "remote-run: no server slot free or reachable, running locally" >&2
276run_local