Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/gpu-head-to-head/blob/main/bench/session-lifecycle.sh # gpu-head-to-head aien-dev/gpu-head-to-head Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `gpu-head-to-head / bench / session-lifecycle.sh` 135 lines ยท 4840 bytes Raw Blame History 1 #!/usr/bin/env bash 2 # External benchmark process ownership. Source from a Bash runner; no model/GPU access here. 3 # Linux proc_pid_stat(5): after comm, state is index0, ppid index1 and starttime index19. 4 declare -A H2H_PID_START=() 5 6 h2h_identity() { 7 local pid=$1 line rest 8 local -a fields 9 [[ "$pid" =~ ^[1-9][0-9]*$ ]] || return 1 10 { IFS= read -r line < "/proc/$pid/stat"; } 2>/dev/null || return 1 11 rest=${line##*) } 12 read -r -a fields <<< "$rest" 13 [ "${#fields[@]}" -ge 20 ] || return 1 14 printf '%s %s %s\n' "${fields[0]}" "${fields[19]}" "${fields[1]}" 15 } 16 17 h2h_track() { 18 local pid=$1 owner=$BASHPID identity state birth parent 19 identity=$(h2h_identity "$pid") || { H2H_PID_START[$pid]=gone; wait "$pid" 2>/dev/null || true; return 0; } 20 read -r state birth parent <<< "$identity" 21 [ "$parent" = "$owner" ] || { echo "refusing to track non-child process $pid" >&2; return 1; } 22 H2H_PID_START[$pid]=$birth 23 } 24 25 h2h_matches() { 26 local pid=$1 identity state birth parent 27 [ -n "${H2H_PID_START[$pid]:-}" ] || return 1 28 identity=$(h2h_identity "$pid") || return 1 29 read -r state birth parent <<< "$identity" 30 [ "$birth" = "${H2H_PID_START[$pid]}" ] && [ "$state" != Z ] && [ "$state" != X ] 31 } 32 33 h2h_tree_snapshot() { 34 local root=$1 pid identity state birth parent changed=1 35 local -A parents=() births=() keep=() 36 keep[$root]=1 37 while read -r pid parent; do parents[$pid]=$parent; done < <(ps -e -o pid=,ppid=) 38 while [ "$changed" = 1 ]; do 39 changed=0 40 for pid in "${!parents[@]}"; do 41 if [ -n "${keep[${parents[$pid]}]:-}" ] && [ -z "${keep[$pid]:-}" ]; then 42 keep[$pid]=1; changed=1 43 fi 44 done 45 done 46 h2h_matches "$root" || return 0 47 for pid in "${!keep[@]}"; do 48 identity=$(h2h_identity "$pid") || continue 49 read -r state birth parent <<< "$identity" 50 if [ "$pid" = "$root" ]; then 51 [ "$birth" = "${H2H_PID_START[$root]}" ] || return 0 52 else 53 [ "$parent" = "${parents[$pid]}" ] || continue 54 fi 55 births[$pid]=$birth 56 done 57 h2h_matches "$root" || return 0 58 for pid in "${!births[@]}"; do printf '%s %s\n' "$pid" "${births[$pid]}"; done 59 } 60 61 h2h_descendant_pids() { 62 local root=$1 63 [[ "$root" =~ ^[1-9][0-9]*$ ]] || return 1 64 ps -e -o pid=,ppid= | awk -v root="$root" ' 65 {parent[$1]=$2; present[$1]=1} 66 END { 67 if (!present[root]) exit 68 keep[root]=1; changed=1 69 while (changed) {changed=0; for (p in parent) if (keep[parent[p]] && !keep[p]) {keep[p]=1;changed=1}} 70 for (p in keep) if (keep[p]) print p 71 }' 72 } 73 74 # Reap an exited child of this shell without blocking on a pid that is already gone. 75 # A pid with no /proc entry was reaped by this shell already; Bash 5.2 `wait` on it 76 # from an EXIT trap that interrupted another `wait` (the watchdog's TERM) can block 77 # forever. A pid that is not this shell's child cannot be waited for either. 78 h2h_reap() { 79 local identity state birth parent 80 identity=$(h2h_identity "$1") || return 0 81 read -r state birth parent <<< "$identity" 82 [ "$parent" = "$BASHPID" ] || return 0 83 wait "$1" 2>/dev/null || true 84 } 85 86 h2h_stop() { 87 local pid=$1 attempts=${2:-600} child birth attempt alive 88 local -a tree=() 89 [ -n "${H2H_PID_START[$pid]:-}" ] || { echo "refusing to stop untracked process $pid" >&2; return 1; } 90 if ! h2h_matches "$pid"; then 91 local identity state current parent 92 if identity=$(h2h_identity "$pid"); then 93 read -r state current parent <<< "$identity" 94 if [ "$state" != Z ] && [ "$state" != X ]; then 95 echo "refusing stale process identity $pid" >&2 96 return 1 97 fi 98 fi 99 h2h_reap "$pid" 100 unset 'H2H_PID_START[$pid]' 101 return 0 102 fi 103 if h2h_matches "$pid"; then 104 while read -r child birth; do 105 H2H_PID_START[$child]=$birth 106 tree+=("$child") 107 done < <(h2h_tree_snapshot "$pid") 108 for child in "${tree[@]}"; do 109 h2h_matches "$child" && kill -TERM "$child" 2>/dev/null || true 110 done 111 for ((attempt=0; attempt/dev/null || true 120 done 121 fi 122 h2h_reap "$pid" 123 for ((attempt=0; attempt<20; attempt++)); do 124 if [ -n "${H2H_STOP_DEADLINE:-}" ] && [ "$SECONDS" -ge "$H2H_STOP_DEADLINE" ]; then break; fi 125 alive=0 126 for child in "${tree[@]}"; do h2h_matches "$child" && alive=1; done 127 [ "$alive" = 0 ] && break 128 sleep 0.1 129 done 130 for child in "${tree[@]}"; do 131 if h2h_matches "$child"; then echo "owned process $child still live after shutdown" >&2; return 1; fi 132 unset 'H2H_PID_START[$child]' 133 done 134 unset 'H2H_PID_START[$pid]' 135 }