interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / gb10_lock.sh

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