From ac7be4a20444fea74e5b64371d6f2ace1b193ada Mon Sep 17 00:00:00 2001 From: acamilo Date: Thu, 1 Oct 2026 15:57:42 +0000 Subject: [PATCH] infra: fly-cpu-confine also confines /.lxc, check ignores its own chain (PERF-02 review R1-R3) apply/release/status now cover the cgroup pct exec lands in (/.lxc, only when it already exists, warn-only when unwritable). check no longer counts its own process or its parents. Runbook: rollback via an older 05-deploy needs fly-cpu-confine release; the exact post-first-deploy status/check commands. --- infra/README.md | 2 +- infra/bin/fly-cpu-confine | 57 +++++++++++++++++++++++++++++++++------ infra/docs/runbook.md | 42 ++++++++++++++++++++++------- infra/tests/lint.sh | 57 ++++++++++++++++++++++++++++++++++----- 4 files changed, 133 insertions(+), 25 deletions(-) diff --git a/infra/README.md b/infra/README.md index b5a0fc4..30f4d91 100644 --- a/infra/README.md +++ b/infra/README.md @@ -269,7 +269,7 @@ refuses to write the drop-ins until the conf actually carries The sim's CPUs are also kept exclusive to it (PERF-02 review N1, `infra/docs/runbook.md` "The sim's CPUs are exclusive to the sim"): the sim units run in `flysim.slice`, and `bin/fly-cpu-confine` -confines `system.slice`, `user.slice` and `init.scope` to the other CPUs when the sim starts. +confines `system.slice`, `user.slice`, `init.scope` and `/.lxc` (`pct exec`) to the other CPUs when the sim starts. Without a partition (`CPUSET` unset) none of this applies and the deploy turns the session runtime's worker pinning off (`FLY_SESSION_PIN=0`). diff --git a/infra/bin/fly-cpu-confine b/infra/bin/fly-cpu-confine index 93a8f84..4bbb726 100755 --- a/infra/bin/fly-cpu-confine +++ b/infra/bin/fly-cpu-confine @@ -2,11 +2,13 @@ # infra/bin/fly-cpu-confine — keep every process that is not the sim off the sim's CPUs. # # Usage (INSIDE the container, as root): -# fly-cpu-confine apply confine system.slice, user.slice and init.scope to the non-sim CPUs -# fly-cpu-confine release undo `apply` (those three go back to the container's whole cpuset) +# fly-cpu-confine apply confine system.slice, user.slice, init.scope and /.lxc (where `pct exec` +# lands) to the non-sim CPUs +# fly-cpu-confine release undo `apply` (all of them go back to the container's whole cpuset) # fly-cpu-confine status what each of them is confined to, and where the sim runs # fly-cpu-confine check list every process that is not in flysim.slice but may run on a -# sim CPU (exit 1 if there is one) +# sim CPU (exit 1 if there is one); this command's own process and its +# parents (the shell it was started from) are noted, not failed # # Why. The session runtime pins its sweep workers one per CPU (PERF-02). A pinned worker cannot # leave its CPU, so any sustained CPU-bound process that may run there stalls every barrier: @@ -30,8 +32,11 @@ # sim that has not been restarted into flysim.slice would silently make it float over the page # CPUs. `apply` refuses (exit 3, nothing changed) if a sim unit is outside flysim.slice. # -# Not reachable from here: a process the host starts into the container with `pct exec` -# (lxc-attach). Check where it lands with `check`; run long operator jobs with +# `pct exec` / `pct enter` (lxc-attach) put their processes in the cgroup `/.lxc` (or `/.lxc-N`), +# a sibling of system.slice that systemd does not manage. It is written directly +# (cpuset.cpus), only if it already exists (a cgroup lxc owns is never created here), and a +# failure is a warning, never an error: it must not stop the sim from starting. If it cannot be +# written, `check` lists what is running there; run long operator jobs with # `systemd-run --scope --slice=system.slice -- ` (or `... -- bash`). set -euo pipefail @@ -43,6 +48,26 @@ set -euo pipefail : "${SIM_UNITS:=flysim.service flysim-session.service}" CONFINED_UNITS="system.slice user.slice init.scope" +# existing lxc attach cgroups (/.lxc, /.lxc-N) that have a cpuset +lxc_cgroups() { + local d + for d in "$CGROUP_ROOT"/.lxc "$CGROUP_ROOT"/.lxc-*; do + [ -d "$d" ] && [ -e "$d/cpuset.cpus" ] && echo "$d" + done + return 0 +} + +# set_lxc CPUS — write CPUS into every lxc attach cgroup; warn, never fail. +set_lxc() { + local d + for d in $(lxc_cgroups); do + if ! echo "$1" > "$d/cpuset.cpus" 2>/dev/null; then + log "WARNING: cannot write ${d}/cpuset.cpus; processes started with pct exec stay unconfined (run long jobs with systemd-run --scope --slice=system.slice)" + fi + done + return 0 +} + log() { echo "fly-cpu-confine: $*" >&2; } # read_env — set SIM_CPUS and OTHER_CPUS from ${CPUSET_ENV}, or return 1 when there is no partition. @@ -92,7 +117,8 @@ case "$cmd" in for u in $CONFINED_UNITS; do timeout 20 "$SYSTEMCTL" set-property --runtime "$u" "AllowedCPUs=${OTHER_CPUS}" done - log "system.slice, user.slice and init.scope confined to ${OTHER_CPUS}; ${SIM_SLICE} keeps ${SIM_CPUS}" + set_lxc "$OTHER_CPUS" + log "system.slice, user.slice, init.scope and /.lxc confined to ${OTHER_CPUS}; ${SIM_SLICE} keeps ${SIM_CPUS}" ;; release) # Not `AllowedCPUs=` (empty): systemd leaves the cgroup's cpuset.cpus at its last value while @@ -102,12 +128,16 @@ case "$cmd" in for u in $CONFINED_UNITS; do timeout 20 "$SYSTEMCTL" set-property --runtime "$u" "AllowedCPUs=${all_cpus}" done - log "system.slice, user.slice and init.scope released to the container's whole cpuset (${all_cpus:-unknown})" + set_lxc "$all_cpus" + log "system.slice, user.slice, init.scope and /.lxc released to the container's whole cpuset (${all_cpus:-unknown})" ;; status) for u in $CONFINED_UNITS $SIM_SLICE; do echo "$u AllowedCPUs=$("$SYSTEMCTL" show -p AllowedCPUs --value "$u" 2>/dev/null || true)" done + for d in $(lxc_cgroups); do + echo "${d#"$CGROUP_ROOT"} cpuset.cpus=$(cat "$d/cpuset.cpus" 2>/dev/null || true)" + done for u in $SIM_UNITS; do echo "$u ControlGroup=$("$SYSTEMCTL" show -p ControlGroup --value "$u" 2>/dev/null || true)" done @@ -119,6 +149,13 @@ case "$cmd" in fi sim="$(expand "$SIM_CPUS" | sort -n | tr '\n' ' ')" bad=0 + # this process and its parents (the pct exec shell chain) are not a CPU hog + self=" " + p="${SELF_PID:-$$}" + while [ -n "$p" ] && [ "$p" -gt 1 ] 2>/dev/null && [ "${#self}" -lt 400 ]; do + self="$self$p " + p="$(sed -n 's/^PPid:[[:space:]]*//p' "$PROC_ROOT/$p/status" 2>/dev/null || true)" + done for d in "$PROC_ROOT"/[0-9]*; do pid="${d##*/}" [ -r "$d/status" ] || continue @@ -133,6 +170,10 @@ case "$cmd" in for s in $sim; do [ "$c" = "$s" ] && hit="$hit$c,"; done done if [ -n "$hit" ]; then + case "$self" in *" $pid "*) + log "note: ${pid} ($(cat "$d/comm" 2>/dev/null)) is this check's own process chain in ${cg:-?} and may run on sim cpu ${hit%,}; not counted" + continue ;; + esac echo "process $pid ($(cat "$d/comm" 2>/dev/null)) in ${cg:-?} may run on sim cpu ${hit%,}" bad=1 fi @@ -141,7 +182,7 @@ case "$cmd" in exit "$bad" ;; *) - sed -n '2,12p' "$0" >&2 + sed -n '2,11p' "$0" >&2 exit 2 ;; esac diff --git a/infra/docs/runbook.md b/infra/docs/runbook.md index 43f87e4..ad0d9cc 100644 --- a/infra/docs/runbook.md +++ b/infra/docs/runbook.md @@ -862,7 +862,9 @@ the partition above is completed by making the sim's CPUs exclusive: whose `AllowedCPUs=` is the sim's set (`flysim.slice.d/cpuset.conf`, written by `05-deploy.sh` from `lib/common.sh`'s `cpuset_dropin_plan`, the same call that writes every other drop-in). - `bin/fly-cpu-confine apply` sets `AllowedCPUs=` the **non-sim** CPUs on `system.slice` (every - other service, cron, apt, sshd), `user.slice` (every login session) and `init.scope`. It runs as + other service, cron, apt, sshd), `user.slice` (every login session) and `init.scope`, and writes + the same list to `/sys/fs/cgroup/.lxc/cpuset.cpus` (where `pct exec` / `pct enter` processes land, + see below). It runs as both sim units' `ExecStartPre` (root, failure never blocks the start), so it is re-applied after every boot and every restart. The lists come from `/etc/fly/cpuset.env`, which the deploy writes. - The slice is named without a dash on purpose: systemd nests `a-b.slice` under `a.slice`, and a @@ -881,18 +883,38 @@ the partition above is completed by making the sim's CPUs exclusive: is no confinement, so `05-deploy.sh` writes `FLY_SESSION_PIN=0` to `/etc/fly/fly.env` and the session runtime's threads float, as before PERF-02. With the partition in force it writes `FLY_SESSION_PIN=1`. (The runtime treats exactly `0` as off.) -- **Not reachable from inside:** a process the host starts into the container with `pct exec` - (`lxc-attach`) is not placed by systemd, so these two slices may not cover it (which cgroup it - lands in has not been verified on the release container). `fly-cpu-confine check` lists every - process outside `flysim.slice` that may run on a sim CPU (exit 1 if there is one): run it after - a deploy and after anything unusual. Run long operator jobs through - `pct exec -- systemd-run --scope --slice=system.slice -- `; the deploy itself - already pins its heavy steps to the page CPUs. +- **`pct exec` / `pct enter`:** `lxc-attach` puts its processes in the cgroup `/.lxc` (or + `/.lxc-N`), a sibling of `system.slice` that systemd does not manage. `apply` writes the non-sim + list into the `cpuset.cpus` of every such cgroup that already exists (it never creates one; lxc + owns it). Where that file is not writable it logs a warning and carries on: `apply` never fails + the sim's start. In that case run long operator jobs through + `pct exec -- systemd-run --scope --slice=system.slice -- `. A `.lxc-N` created + after `apply` is not covered until the next sim start. `fly-cpu-confine check` lists every + process outside `flysim.slice` that may run on a sim CPU (exit 1 if there is one); its own + process and the shell chain it was started from are printed as a note and not counted, so it + exits 0 on a healthy container when run through `pct exec`. +- **After the first deploy (once):** restart the sim so the confinement is applied, then read it. + `apply` is refused while flysim still runs outside `flysim.slice`, so nothing changes until then. + ``` + pct exec -- systemctl restart flysim.service + pct exec -- /opt/fly/bin/fly-cpu-confine status # system.slice, user.slice, init.scope and /.lxc = the non-sim cpus; flysim.slice = the sim's; flysim ControlGroup under /flysim.slice/ + pct exec -- /opt/fly/bin/fly-cpu-confine check # exit 0: "ok: only flysim.slice can reach ..." + pct exec -- systemctl show -p AllowedCPUs flycast.service xvfb.service # still the drop-in values + ``` + Then watch the probation RTF. A `check` that names a process is a hog to move or stop. - **If lag grows on the session runtime**: `fly-cpu-confine check`, then `top -H` for a stray hog on the sim's CPUs. `FLY_SESSION_PIN=0` (a drop-in) restores floating. - `fly-cpu-confine release` undoes the runtime confinement (until the next sim start); it sets - the three back to the container's whole cpuset explicitly, because systemd keeps the old mask for - an empty `AllowedCPUs=` while the cpuset controller stays on for another unit. + `system.slice`, `user.slice`, `init.scope` and `/.lxc` back to the container's whole cpuset + explicitly, because systemd keeps the old mask for an empty `AllowedCPUs=` while the cpuset + controller stays on for another unit. (Tested in `tests/lint.sh` against a fake cgroup tree.) +- **Rollback by re-running an older infra tree's `05-deploy.sh`** (rather than the symlink flip in + "Roll back a release", which is fine): an infra tree older than this change has no + `Slice=flysim.slice`, so flysim restarts into `system.slice`, which is still confined to the + non-sim CPUs. Its `AllowedCPUs=` then does not intersect that set and it floats over the page and + encoder CPUs while the sim's own CPUs sit idle (the slow shape again, not a black stream). After + such a rollback run `pct exec -- /opt/fly/bin/fly-cpu-confine release` (or reboot), then + restart flysim. Verified on a real-systemd box (4 CPUs, the sim as a unit in `flysim.slice` on 3 of them, 3 sweep threads, paced at 0.55x): confined or not, with no hog the session holds 0.55; five busy loops (three diff --git a/infra/tests/lint.sh b/infra/tests/lint.sh index 40121c7..b8ad3d5 100755 --- a/infra/tests/lint.sh +++ b/infra/tests/lint.sh @@ -1572,7 +1572,8 @@ grep -qE 'FLY_SESSION_PIN=0' "$INFRA_DIR/05-deploy.sh" && grep -qE 'PARTITION_AC # bin/fly-cpu-confine against a stub systemctl and a fake /proc cc="$INFRA_DIR/bin/fly-cpu-confine" cc_dir="$(mktemp -d "${TMPDIR:-/tmp}/fly-lint-confine.XXXXXX")" -mkdir -p "$cc_dir/bin" "$cc_dir/proc" +mkdir -p "$cc_dir/bin" "$cc_dir/proc" "$cc_dir/cg/.lxc" +echo 0-15 > "$cc_dir/cg/.lxc/cpuset.cpus" cat > "$cc_dir/bin/systemctl" <<'STUB' #!/usr/bin/env bash echo "systemctl $*" >> "$STUB_LOG" @@ -1591,7 +1592,7 @@ STUB chmod +x "$cc_dir/bin/systemctl" "$cc_dir/bin/timeout" printf 'FLY_SIM_CPUS=1,3,5,7\nFLY_OTHER_CPUS=9,11,13-15\n' > "$cc_dir/cpuset.env" cc_run() { - PATH="$cc_dir/bin:$PATH" CPUSET_ENV="${CPUSET_ENV:-$cc_dir/cpuset.env}" PROC_ROOT="$cc_dir/proc" STUB_LOG="$cc_dir/log" "$@" + PATH="$cc_dir/bin:$PATH" CPUSET_ENV="${CPUSET_ENV:-$cc_dir/cpuset.env}" PROC_ROOT="$cc_dir/proc" CGROUP_ROOT="${CGROUP_ROOT:-$cc_dir/cg}" STUB_LOG="$cc_dir/log" "$@" } : > "$cc_dir/log" out="$(cc_run "$cc" apply 2>&1)" && rc=0 || rc=$? @@ -1602,6 +1603,28 @@ if [ "$rc" -eq 0 ] && [ "$(grep -c 'set-property --runtime .* AllowedCPUs=9,11,1 else fail "fly-cpu-confine apply (rc=$rc): $out / $(cat "$cc_dir/log")" fi +[ "$(cat "$cc_dir/cg/.lxc/cpuset.cpus")" = "9,11,13-15" ] \ + && pass "fly-cpu-confine apply also confines /.lxc (where pct exec lands) to the non-sim cpus" \ + || fail "fly-cpu-confine apply must write the non-sim cpus into /.lxc/cpuset.cpus (got $(cat "$cc_dir/cg/.lxc/cpuset.cpus"))" +out="$(cc_run "$cc" status 2>&1)" +grep -q '^/.lxc cpuset.cpus=9,11,13-15$' <<< "$out" \ + && pass "fly-cpu-confine status shows /.lxc" \ + || fail "fly-cpu-confine status must show /.lxc: $out" +# unwritable /.lxc: warn, still exit 0, slices still confined; absent /.lxc: nothing created +: > "$cc_dir/log" +mkdir -p "$cc_dir/cg2/.lxc" +mkdir "$cc_dir/cg2/.lxc/cpuset.cpus" # a directory: the write fails, like a read-only cgroup file +out="$(CGROUP_ROOT="$cc_dir/cg2" cc_run "$cc" apply 2>&1)" && rc=0 || rc=$? +if [ "$rc" -eq 0 ] && grep -q 'WARNING: cannot write' <<< "$out" && [ "$(grep -c set-property "$cc_dir/log")" = 3 ]; then + pass "fly-cpu-confine apply warns, never fails, when /.lxc cannot be written (slices still confined)" +else + fail "fly-cpu-confine apply with an unwritable /.lxc (rc=$rc): $out" +fi +mkdir -p "$cc_dir/cg3" +CGROUP_ROOT="$cc_dir/cg3" cc_run "$cc" apply >/dev/null 2>&1 +[ -z "$(ls -A "$cc_dir/cg3")" ] \ + && pass "fly-cpu-confine apply does not create /.lxc (lxc owns it)" \ + || fail "fly-cpu-confine apply must not create cgroups under the root: $(ls -A "$cc_dir/cg3")" : > "$cc_dir/log" out="$(CG_flysim=/system.slice/flysim.service cc_run "$cc" apply 2>&1)" && rc=0 || rc=$? if [ "$rc" -eq 3 ] && ! grep -q set-property "$cc_dir/log"; then @@ -1622,11 +1645,12 @@ else fail "fly-cpu-confine apply without cpuset.env must change nothing (rc=$rc): $out $(cat "$cc_dir/log")" fi : > "$cc_dir/log" -echo 0-15 > "$cc_dir/cpuset.cpus.effective" -CGROUP_ROOT="$cc_dir" cc_run "$cc" release >/dev/null 2>&1 +echo 0-15 > "$cc_dir/cg/cpuset.cpus.effective" +CGROUP_ROOT="$cc_dir/cg" cc_run "$cc" release >/dev/null 2>&1 [ "$(grep -c 'set-property --runtime .* AllowedCPUs=0-15$' "$cc_dir/log")" = 3 ] \ - && pass "fly-cpu-confine release sets all three back to the container's whole cpuset (an empty AllowedCPUs= leaves the old mask in place)" \ - || fail "fly-cpu-confine release: $(cat "$cc_dir/log")" + && [ "$(cat "$cc_dir/cg/.lxc/cpuset.cpus")" = "0-15" ] \ + && pass "fly-cpu-confine release sets all three and /.lxc back to the container's whole cpuset (an empty AllowedCPUs= leaves the old mask in place)" \ + || fail "fly-cpu-confine release: $(cat "$cc_dir/log") / lxc=$(cat "$cc_dir/cg/.lxc/cpuset.cpus")" # check: a fake /proc with a sim process in the slice, a clean neighbour, a kernel thread, then a hog mkproc() { # pid comm cgroup allowed mkdir -p "$cc_dir/proc/$1" @@ -1651,6 +1675,27 @@ if [ "$rc" -eq 1 ] && grep -q 'process 13 (hog).*sim cpu 1,3,5,7' <<< "$out"; th else fail "fly-cpu-confine check must flag the hog (rc=$rc): $out" fi +# the check's own process chain (a pct exec shell) is noted, not failed; a stranger in /.lxc still fails +rm -rf "$cc_dir/proc/13" +mkproc 20 pctexec /.lxc 0-15 +mkproc 21 bash /.lxc 0-15 +mkproc 22 fly-cpu-confine /.lxc 0-15 +printf 'Name:\tx\nPPid:\t21\nCpus_allowed_list:\t0-15\n' > "$cc_dir/proc/22/status" +printf 'Name:\tx\nPPid:\t20\nCpus_allowed_list:\t0-15\n' > "$cc_dir/proc/21/status" +printf 'Name:\tx\nPPid:\t1\nCpus_allowed_list:\t0-15\n' > "$cc_dir/proc/20/status" +out="$(SELF_PID=22 cc_run "$cc" check 2>&1)" && rc=0 || rc=$? +if [ "$rc" -eq 0 ] && grep -q 'own process chain' <<< "$out" && ! grep -q '^process' <<< "$out"; then + pass "fly-cpu-confine check exits 0 when the only process on a sim cpu is its own pct exec chain" +else + fail "fly-cpu-confine check must not count its own process or ancestors (rc=$rc): $out" +fi +mkproc 23 tar /.lxc 0-15 +out="$(SELF_PID=22 cc_run "$cc" check 2>&1)" && rc=0 || rc=$? +if [ "$rc" -eq 1 ] && grep -q '^process 23 (tar)' <<< "$out" && ! grep -q '^process 2[012] ' <<< "$out"; then + pass "fly-cpu-confine check still flags a different process in /.lxc" +else + fail "fly-cpu-confine check must flag a stranger in /.lxc (rc=$rc): $out" +fi rm -rf "$cc_dir" # ---------------------------------------------------------------------------