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/test-run-arm-lifecycle.py # 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 / test-run-arm-lifecycle.py` 256 lines ยท 11393 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """CPU-only ownership and interruption controls for external benchmark lifecycle.""" 3 from pathlib import Path 4 import shlex 5 import os 6 import signal 7 import time 8 import subprocess 9 import tempfile 10 import unittest 11 HERE=Path(__file__).resolve().parent 12 13 class ContainerOwnershipTests(unittest.TestCase): 14 def test_foreign_container_is_never_stopped(self): 15 source=(HERE/'run_arm.sh').read_text() 16 function=source[source.index('stop_server() {'):source.index('ready() {')] 17 with tempfile.TemporaryDirectory() as tmp: 18 script='''set -u 19 ARM=VLLM CTR=fixture-unowned SPID=`999999999` PAT=fixture-no-match 20 RUN_ID=our-session LOGD=%s 21 board() { :; } 22 docker() { case "$1" in inspect) echo someone-else ;; stop) echo stopped > "$LOGD/foreign-stopped" ;; esac; } 23 kill() { return 1; } 24 sleep() { :; } 25 pgrep() { return 1; } 26 ''' % shlex.quote(tmp) 27 result=subprocess.run(['bash','-c',script+function+'\nstop_server\n'],capture_output=True,text=True,timeout=5) 28 self.assertFalse((Path(tmp)/'foreign-stopped').exists(),result.stderr) 29 self.assertNotEqual(result.returncode,0) 30 31 def test_owned_container_launch_records_label_and_shutdown(self): 32 source=(HERE/'run_arm.sh').read_text() 33 functions=source[source.index('start_server() {'):source.index('ready() {')] 34 with tempfile.TemporaryDirectory() as tmp: 35 script="""set -u 36 source %s 37 LOGD=%s ARM=VLLM CTR=stack-fixture RUN_ID=fixture SPID= 38 MDIR=/fixture VLLM_IMG=fixture-image SRV_CPUS=0-1 VLLM_UTIL=0.50 39 trap 'for p in $(jobs -pr); do kill -TERM "$p" 2>/dev/null || true; done; wait 2>/dev/null || true' EXIT 40 timeout() { shift; "$@"; } 41 docker() { 42 case "$1" in 43 create) printf '%%s\n' "$@" > "$LOGD/create-args"; echo fixture-id ;; 44 start) echo started > "$LOGD/start-called"; exec sleep 60 ;; 45 inspect) echo fixture ;; 46 stop) echo stopped > "$LOGD/stop-called" ;; 47 esac 48 } 49 """ % (shlex.quote(str(HERE/'session-lifecycle.sh')),shlex.quote(tmp)) 50 result=subprocess.run(['bash','-c',script+functions+'\nstart_server && stop_server\n'], 51 capture_output=True,text=True,timeout=5) 52 self.assertEqual(result.returncode,0,result.stderr) 53 args=(Path(tmp)/'create-args').read_text().splitlines() 54 self.assertIn('aien.stack-comparison.run=fixture',args) 55 self.assertIn('never',args) 56 self.assertTrue((Path(tmp)/'server-identity.txt').exists()) 57 self.assertTrue((Path(tmp)/'stop-called').exists()) 58 59 def test_failed_container_creation_never_starts_server(self): 60 source=(HERE/'run_arm.sh').read_text() 61 function=source[source.index('start_server() {'):source.index('stop_server() {')] 62 with tempfile.TemporaryDirectory() as tmp: 63 script="""set -u 64 source %s 65 LOGD=%s ARM=VLLM CTR=stack-fixture RUN_ID=fixture SPID= 66 MDIR=/fixture VLLM_IMG=fixture-image SRV_CPUS=0-1 VLLM_UTIL=0.50 67 timeout() { shift; "$@"; } 68 docker() { if [ "$1" = create ]; then return 7; fi; echo wrongly-started > "$LOGD/start-called"; } 69 """ % (shlex.quote(str(HERE/'session-lifecycle.sh')),shlex.quote(tmp)) 70 result=subprocess.run(['bash','-c',script+function+'\nstart_server\n'],capture_output=True,text=True,timeout=5) 71 self.assertNotEqual(result.returncode,0) 72 self.assertFalse((Path(tmp)/'start-called').exists()) 73 74 class ProcessLifecycleTests(unittest.TestCase): 75 def shell(self, body): 76 prefix="""set -u 77 trap 'for p in $(jobs -pr); do kill -TERM "$p" 2>/dev/null || true; done; wait 2>/dev/null || true' EXIT 78 source %s 79 """ % shlex.quote(str(HERE/'session-lifecycle.sh')) 80 result=subprocess.run(['bash','-c',prefix+body],capture_output=True,text=True,timeout=15) 81 self.assertEqual(result.returncode,0,result.stderr) 82 return result.stdout 83 84 def test_tracked_child_stops_and_untracked_decoy_survives(self): 85 self.shell("""sleep 60 & own=$!; h2h_track "$own" 86 sleep 60 & decoy=$! 87 h2h_stop "$own" 10 || exit 1 88 kill -0 "$decoy" || exit 2 89 kill -TERM "$decoy"; wait "$decoy" 2>/dev/null || true 90 [ -z "${H2H_PID_START[$own]:-}" ] 91 """) 92 93 def test_stale_birth_does_not_signal_process(self): 94 self.shell("""sleep 60 & own=$!; h2h_track "$own" 95 H2H_PID_START[$own]=stale 96 if h2h_stop "$own" 1; then exit 1; fi 97 kill -0 "$own" || exit 2 98 kill -TERM "$own"; wait "$own" 2>/dev/null || true 99 """) 100 101 def test_cleanup_deadline_forces_own_term_ignoring_child(self): 102 with tempfile.TemporaryDirectory() as tmp: 103 ready=Path(tmp)/'ready' 104 body="""bash -c 'trap "" TERM; echo ready > "$1"; exec sleep 60' fixture %s & own=$! 105 h2h_track "$own" 106 for attempt in {1..100}; do [ -e %s ] && break; sleep 0.01; done 107 H2H_STOP_DEADLINE=$SECONDS 108 h2h_stop "$own" || exit 1 109 if kill -0 "$own" 2>/dev/null; then exit 2; fi 110 """ % (shlex.quote(str(ready)),shlex.quote(str(ready))) 111 self.shell(body) 112 113 def test_non_child_and_untracked_process_refused(self): 114 self.shell("""if h2h_track $$; then exit 1; fi 115 sleep 60 & other=$! 116 if h2h_stop "$other" 1; then kill "$other"; exit 2; fi 117 kill -0 "$other" || exit 3 118 kill -TERM "$other"; wait "$other" 2>/dev/null || true 119 """) 120 121 def test_already_exited_child_can_be_reaped(self): 122 self.shell("""true & own=$!; wait "$own" 123 h2h_track "$own" || exit 1 124 h2h_stop "$own" 1 125 """) 126 127 def test_term_ignoring_child_is_killed_after_grace(self): 128 with tempfile.TemporaryDirectory() as tmp: 129 ready=Path(tmp)/'ready' 130 body="""bash -c 'trap "" TERM; echo ready > "$1"; exec sleep 60' fixture %s & own=$! 131 h2h_track "$own" 132 for attempt in {1..100}; do [ -e %s ] && break; sleep 0.01; done 133 h2h_stop "$own" 2 || exit 1 134 if kill -0 "$own" 2>/dev/null; then exit 2; fi 135 """ % (shlex.quote(str(ready)),shlex.quote(str(ready))) 136 self.shell(body) 137 138 def test_descendant_is_stopped_with_root(self): 139 with tempfile.TemporaryDirectory() as tmp: 140 ready=Path(tmp)/'child' 141 body="""( sleep 60 & child=$! 142 trap 'kill -TERM "$child" 2>/dev/null || true; wait "$child" 2>/dev/null || true; exit 0' TERM 143 printf '%%s' "$child" > %s 144 wait "$child" 145 ) & own=$! 146 h2h_track "$own" 147 for attempt in {1..100}; do [ -s %s ] && break; sleep 0.01; done 148 child=$(cat %s) 149 h2h_stop "$own" 10 || exit 1 150 identity=$(h2h_identity "$child") || exit 0 151 read -r state birth parent <<< "$identity" 152 [ "$state" = Z ] || [ "$state" = X ] 153 """ % tuple(shlex.quote(str(ready)) for _ in range(3)) 154 self.shell(body) 155 156 def test_memory_sampler_resolves_published_server_identity(self): 157 source=(HERE/'run_arm.sh').read_text() 158 function=source[source.index('server_pids() {'):source.index('start_server() {')] 159 with tempfile.TemporaryDirectory() as tmp: 160 script='LOGD=%s\nCTR= SPID= RUN_ID=fixture\n' % shlex.quote(tmp) 161 script+=function+""" 162 sleep 60 & actual=$!; h2h_track "$actual" 163 sleep 60 & decoy=$! 164 printf '%s %s\n' "$actual" "${H2H_PID_START[$actual]}" > "$LOGD/server-identity.txt" 165 observed=$(server_pids) 166 [ "$observed" = "$actual" ] || exit 1 167 h2h_stop "$actual" 10 || exit 2 168 kill -TERM "$decoy"; wait "$decoy" 2>/dev/null || true 169 """ 170 self.shell(script) 171 172 def test_cleanup_preserves_parent_lock_and_releases_own_lock(self): 173 source=(HERE/'run_arm.sh').read_text() 174 cleanup=source[source.index('cleanup_session() {'):source.index('# ---- session ----')] 175 for release in [0,1]: 176 with self.subTest(release=release),tempfile.TemporaryDirectory() as tmp: 177 prefix="""set -u 178 LOGD=%s 179 SPID= CPID= CMON_PID= WD= SAMP= VMS= BASE=100 GPU_LOCK_HELD=1 GPU_LOCK_RELEASE=%d 180 ARM=CONTROL ROUND=R0 181 timeout() { shift; "$@"; } 182 nvidia-smi() { case "$*" in *noheader*) : ;; *) echo fixture-empty-inventory-header ;; esac; } 183 memavail() { echo 100; } 184 ktpsr() { echo 99; } 185 board() { :; } 186 stop_server() { :; } 187 """ % (shlex.quote(tmp),release) 188 script=prefix+cleanup+""" 189 export LOGD SPID CPID CMON_PID WD SAMP VMS BASE GPU_LOCK_HELD GPU_LOCK_RELEASE ARM ROUND 190 export -f cleanup_session timeout nvidia-smi memavail ktpsr board stop_server 191 exec 9> "$LOGD/lock" 192 flock -n 9 || exit 1 193 bash -c 'trap cleanup_session EXIT; exit 0' || exit 2 194 if flock -n "$LOGD/lock" true; then acquired=1; else acquired=0; fi 195 [ "$acquired" = "$GPU_LOCK_RELEASE" ] || exit 3 196 flock -u 9 197 flock -n "$LOGD/lock" true 198 """ 199 result=subprocess.run(['bash','-c',script],capture_output=True,text=True,timeout=5) 200 self.assertEqual(result.returncode,0,result.stderr) 201 202 def lifecycle_fixture(self, mode): 203 source=(HERE/'run_arm.sh').read_text() 204 stop=source[source.index('stop_server() {'):source.index('ready() {')] 205 cleanup=source[source.index('start_watchdog() {'):source.index('# ---- session ----')] 206 with tempfile.TemporaryDirectory() as tmp: 207 root=Path(tmp) 208 prefix='''set -u 209 source %s 210 LOGD=%s 211 SPID= CPID= CMON_PID= WD= SAMP= VMS= BASE= GPU_LOCK_HELD=0 CTR= 212 ARM=OURS ROUND=R0 RUN_ID=fixture 213 board() { :; } 214 ''' % (shlex.quote(str(HERE/'session-lifecycle.sh')),shlex.quote(tmp)) 215 body=""" 216 trap cleanup_session EXIT 217 trap 'exit 143' TERM 218 trap 'exit 130' INT 219 sleep 60 & SPID=$!; h2h_track "$SPID" 220 sleep 60 & CPID=$!; h2h_track "$CPID" 221 sleep 60 & CMON_PID=$!; h2h_track "$CMON_PID" 222 sleep 60 & WD=$!; h2h_track "$WD" 223 sleep 60 & SAMP=$!; h2h_track "$SAMP" 224 sleep 60 & VMS=$!; h2h_track "$VMS" 225 touch "$LOGD/DONE" 226 printf '%s\n' "$SPID" "$CPID" "$CMON_PID" "$WD" "$SAMP" "$VMS" > "$LOGD/children" 227 """ 228 if mode=='timeout': 229 body += 'h2h_stop "$WD" 10; WD=; start_watchdog 0.02 0.02; wait "$CPID"\n' 230 else: 231 body += 'wait "$CPID"\n' if mode=='term' else ('exit 7\n' if mode=='fail' else 'exit 0\n') 232 process=subprocess.Popen(['bash','-c',prefix+stop+cleanup+body],stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True) 233 try: 234 if mode=='term': 235 deadline=time.monotonic()+5 236 while not (root/'children').exists() and time.monotonic()