65 lines
4.3 KiB
Python
65 lines
4.3 KiB
Python
|
|
"""One-shot watchdog check; administer only the two test processes created here."""
|
||
|
|
import pathlib,json,subprocess,time,urllib.request
|
||
|
|
D='/usr/bin/docker';ROOT=pathlib.Path('/root/llmuser-gpu-priority-20261110')
|
||
|
|
POLICY=pathlib.Path('/var/lib/llmuser-gpu-policy/status.json')
|
||
|
|
UUID='GPU-6f547b70-7a25-70cb-e134-888402be61e2'
|
||
|
|
NAME='llmuser-priority-validation-watchdog'
|
||
|
|
report={'started_at':time.time(),'passed':False};foreign_id=None;own=None
|
||
|
|
def state():return json.loads(POLICY.read_text())
|
||
|
|
def record(phase):
|
||
|
|
report['phase']=phase
|
||
|
|
(ROOT/'watchdog-progress.json').write_text(json.dumps(report,indent=2))
|
||
|
|
print(phase,flush=True)
|
||
|
|
try:
|
||
|
|
record('waiting_for_integration')
|
||
|
|
expected=json.loads((ROOT/'integration-progress.json').read_text())['started_at']
|
||
|
|
deadline=time.monotonic()+500
|
||
|
|
while True:
|
||
|
|
r=json.loads((ROOT/'priority-integration.json').read_text())
|
||
|
|
if r.get('started_at',0)>=expected:
|
||
|
|
assert r['passed'],r.get('error')
|
||
|
|
break
|
||
|
|
assert time.monotonic()<deadline
|
||
|
|
time.sleep(.5)
|
||
|
|
assert not any(g['own_pids'] for g in state()['gpus'])
|
||
|
|
assert not next(g for g in state()['gpus'] if g['uuid']==UUID)['external_pids']
|
||
|
|
assert subprocess.run([D,'inspect',NAME],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL).returncode!=0
|
||
|
|
code="import signal,torch,time;signal.signal(signal.SIGTERM,lambda *a:exit(0));x=torch.ones((256,256),device='cuda');print('EXTERNAL_READY',flush=True);time.sleep(90)"
|
||
|
|
cmd=[D,'run','-d','--name',NAME,'--gpus','device='+UUID,'--env','NVIDIA_DRIVER_CAPABILITIES=compute,utility','--env','CUDA_VISIBLE_DEVICES='+UUID,'--env','TMPDIR=/run/probe','--env','XDG_CACHE_HOME=/run/probe/cache','--env','CUDA_CACHE_PATH=/run/probe/cuda','--tmpfs','/run/probe:rw,nosuid,nodev,size=128m,mode=1777','--network','none','--read-only','--user','1013:1014','--cap-drop','ALL','--security-opt','no-new-privileges:true','--memory','2g','--cpus','2','--mount','type=bind,src=/data/llmusers/llmuser/workspace,dst=/workspace,readonly','--entrypoint','/workspace/envs/vllm/bin/python','llmuser-runtime:multimodal-final-20261110','-c',code]
|
||
|
|
foreign_id=subprocess.check_output(cmd,text=True).strip()
|
||
|
|
deadline=time.monotonic()+40
|
||
|
|
while 'EXTERNAL_READY' not in subprocess.check_output([D,'logs',NAME],text=True,stderr=subprocess.STDOUT):
|
||
|
|
assert time.monotonic()<deadline
|
||
|
|
time.sleep(.25)
|
||
|
|
report['signal_started_at']=time.time();record('foreign_ready_starting_manual_own_process')
|
||
|
|
own_code="import signal,torch,time;signal.signal(signal.SIGTERM,signal.SIG_IGN);x=torch.ones((256,256),device='cuda');print('MANUAL_GPU_READY',flush=True);time.sleep(90)"
|
||
|
|
log=(ROOT/'manual-watchdog-process.log').open('ab',buffering=0)
|
||
|
|
own=subprocess.Popen([D,'exec','--user','1013:1014','--env','CUDA_VISIBLE_DEVICES='+UUID,'llmuser-workspace','/workspace/envs/vllm/bin/python','-c',own_code],stdout=log,stderr=subprocess.STDOUT)
|
||
|
|
report['manual_exit_code']=own.wait(timeout=30);log.close()
|
||
|
|
assert report['manual_exit_code']==137, (ROOT/'manual-watchdog-process.log').read_text()
|
||
|
|
assert 'MANUAL_GPU_READY' in (ROOT/'manual-watchdog-process.log').read_text()
|
||
|
|
assert json.loads(subprocess.check_output([D,'inspect',NAME],text=True))[0]['State']['Running']
|
||
|
|
deadline=time.monotonic()+10
|
||
|
|
while any(g['own_pids'] for g in state()['gpus']):
|
||
|
|
assert time.monotonic()<deadline
|
||
|
|
time.sleep(.25)
|
||
|
|
logs=subprocess.check_output([D,'logs','--since',str(report['signal_started_at']),'llmuser-gpu-guard'],text=True,stderr=subprocess.STDOUT)
|
||
|
|
assert '"own_term"' in logs and '"own_kill"' in logs,logs
|
||
|
|
report.update(passed=True,foreign_not_killed=True,own_gpu_process_released=True,guard_log=logs)
|
||
|
|
record('watchdog_verified')
|
||
|
|
except Exception as exc:
|
||
|
|
report['error']=type(exc).__name__+': '+str(exc);record('failed')
|
||
|
|
finally:
|
||
|
|
if own is not None and own.poll() is None:
|
||
|
|
# This host CLI only belongs to this test; the root guard handles the actual GPU process.
|
||
|
|
own.terminate()
|
||
|
|
try:own.wait(timeout=5)
|
||
|
|
except subprocess.TimeoutExpired:own.kill();own.wait()
|
||
|
|
if foreign_id is not None:
|
||
|
|
x=json.loads(subprocess.check_output([D,'inspect',NAME],text=True))[0]
|
||
|
|
assert x['Id']==foreign_id
|
||
|
|
if x['State']['Running']:subprocess.run([D,'stop','--time','5',NAME],check=True,stdout=subprocess.DEVNULL)
|
||
|
|
subprocess.run([D,'rm',NAME],check=True,stdout=subprocess.DEVNULL)
|
||
|
|
report['finished_at']=time.time()
|
||
|
|
(ROOT/'watchdog-check.json').write_text(json.dumps(report,indent=2))
|