interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / tests / gb10_lock.sh

58 lines · 3329 bytes Raw Blame History
1#!/bin/bash
2# tests/gb10_lock.sh -- the GB10 lock for GPU gate scripts (sourced, not run).
3#
4# gb10_lock_acquire exclusive flock(2) on $GB10_LOCK through fd 9, waiting for any other holder;
5# or nothing, when the runner says it holds it (AIEN_GB10_LOCK_HELD=1) or a process
6# above this shell already holds it
7# gb10_lock_release closes fd 9 (the kernel drops the lock with the last copy of the descriptor)
8#
9# Why the parent check: the Spark judge (aien-test worker) takes /tmp/aien-gb10.lock for a whole gb10
10# job and then runs the gate in its sandbox. The gate opening the file again gets a separate open file
11# description, and man 2 flock: "An attempt to lock the file using one of these file descriptors may be
12# denied by a lock that the calling process has already placed via another file descriptor." The gate
13# then waits on its own parent forever (2026-10-18: physics #64, forge-gate34, 83 minutes, every chip
14# job on the Spark queued behind it). A holder that is not an ancestor is waited for, as before.
15#
16# The file is opened read-only when it exists: fs.protected_regular=2 refuses an O_CREAT open of another
17# user's file in sticky /tmp, and flock(2) locks whatever the open mode (sc#627).
18# The gb10 sandbox profile shares the PID namespace and /proc (aien-run-exec: unshare -m only), so the
19# ancestors and /proc/locks are visible from inside. Shell + coreutils + awk. No Python.
20
21GB10_LOCK=${GB10_LOCK:-/tmp/aien-gb10.lock}
22
23# gb10_lock_held_by_ancestor FILE: 0 when this shell or one of its ancestors holds a flock on FILE.
24gb10_lock_held_by_ancestor() {
25 local id holders pid p
26 # /proc/locks names the file as MAJOR:MINOR:INODE (major and minor in hex); waiters are "N: -> FLOCK ...".
27 id=$(stat -L -c '%Hd %Ld %i' "$1" 2>/dev/null) || return 1
28 id=$(printf '%02x:%02x:%s' $id)
29 holders=$(awk -v id="$id" '$2 == "FLOCK" && $6 == id { print $5 }' /proc/locks 2>/dev/null)
30 [ -n "$holders" ] || return 1
31 p=$$
32 while [ -n "$p" ] && [ "$p" -gt 0 ]; do
33 for pid in $holders; do [ "$p" = "$pid" ] && return 0; done
34 [ "$p" -eq 1 ] && break
35 p=$(awk '/^PPid:/ { print $2 }' "/proc/$p/status" 2>/dev/null)
36 done
37 return 1
38}
39
40gb10_lock_acquire() {
41 # The aien-test runner sets AIEN_GB10_LOCK_HELD=1 only for a gb10 gate while it holds the lock, and strips an
42 # inherited copy from every other gate (aien-sovereign-core #580). Trust it first; it still works if the
43 # runner and the gate ever stop sharing a PID namespace, where the ancestor check below would see nothing.
44 if [ "${AIEN_GB10_LOCK_HELD:-0}" = 1 ]; then
45 echo "[gb10-lock] $GB10_LOCK is held by the caller (AIEN_GB10_LOCK_HELD=1): not taking it again"
46 return 0
47 fi
48 # Same user, no marker: e.g. an operator running the gate as `flock /tmp/aien-gb10.lock tests/run_...sh`.
49 if gb10_lock_held_by_ancestor "$GB10_LOCK"; then
50 echo "[gb10-lock] $GB10_LOCK is held by a parent process (the judge's gb10 job): not taking it again"
51 return 0
52 fi
53 if [ -e "$GB10_LOCK" ]; then exec 9< "$GB10_LOCK"; else exec 9>> "$GB10_LOCK"; fi \
54 || { echo "cannot open $GB10_LOCK" >&2; return 1; }
55 flock -x 9 || { echo "cannot take $GB10_LOCK" >&2; return 1; }
56}
57
58gb10_lock_release() { exec 9>&-; }