106 lines
5.9 KiB
Python
106 lines
5.9 KiB
Python
"""Pure discrete-event contract model. NO subprocesses, data reads or real jobs.
|
|
|
|
Virtual process-tree inputs test accounting and barriers, not Linux enforcement.
|
|
A production adapter must supply sampled + final tree accounting, kill/reap,
|
|
locks and persisted recovery; none is claimed here.
|
|
"""
|
|
from dataclasses import dataclass, asdict
|
|
import math
|
|
|
|
@dataclass(frozen=True)
|
|
class Task:
|
|
pair_id: str
|
|
rank: int
|
|
duration: float = 1.
|
|
tree_cpu_rates: tuple = (1.,)
|
|
tree_rss_bytes: tuple = (1024,)
|
|
disk_rate: float = 0.
|
|
fails: bool = False
|
|
|
|
@dataclass(frozen=True)
|
|
class Limits:
|
|
workers: int = 16
|
|
worker_cpu: float = 80*3600
|
|
wall: float = 8*3600
|
|
disk: float = 20*1024**3
|
|
pair_wall: float = 1800.
|
|
pair_rss: int = 1342177280
|
|
checkpoint: int = 100
|
|
|
|
|
|
def simulate(tasks, limits=Limits(), *, supervisor_cpu_rate=.1):
|
|
"""Virtual seconds/bytes only. Each unique pair is attempted at most once."""
|
|
tasks=sorted(tasks,key=lambda t:(t.rank,t.pair_id))
|
|
if len({t.pair_id for t in tasks})!=len(tasks):raise ValueError('Duplicate pair cannot expand denominator')
|
|
if not 1<=limits.workers<=16 or limits.checkpoint!=100:raise ValueError('Contract worker/checkpoint bound')
|
|
if any(not math.isfinite(v) or v<=0 for v in asdict(limits).values()):raise ValueError('Invalid limit')
|
|
if not math.isfinite(supervisor_cpu_rate) or supervisor_cpu_rate<0:raise ValueError('Invalid supervisor rate')
|
|
for t in tasks:
|
|
values=(t.duration,*t.tree_cpu_rates,*t.tree_rss_bytes,t.disk_rate)
|
|
if not t.pair_id or not math.isfinite(t.rank) or any(not math.isfinite(v) or v<0 for v in values) or t.duration<=0 or not t.tree_cpu_rates or not t.tree_rss_bytes:
|
|
raise ValueError('Invalid virtual tree/task')
|
|
active={};records=[];events=[];checkpoints=[];index=0;end=min(100,len(tasks))
|
|
elapsed=worker_cpu=supervisor_cpu=disk=0.;peak_workers=peak_tree_processes=0
|
|
reason=None
|
|
def finish(pid,why=None):
|
|
a=active.pop(pid);t=a['task']
|
|
records.append({'pair_id':pid,'rank':t.rank,'status':'FAIL' if why or t.fails else 'PASS',
|
|
'reason':why or ('worker_exit' if t.fails else None),'worker_tree_cpu':a['cpu'],
|
|
'tree_rss':sum(t.tree_rss_bytes),'start':a['start'],'finish':elapsed})
|
|
events.append({'event':'finish','pair_id':pid,'time':elapsed,'reason':records[-1]['reason']})
|
|
def global_limit():
|
|
if worker_cpu>=limits.worker_cpu:return 'worker_cpu_cap'
|
|
if elapsed>=limits.wall:return 'global_wall_cap'
|
|
if disk>=limits.disk:return 'global_disk_cap'
|
|
while index<len(tasks) or active:
|
|
reason=global_limit()
|
|
if reason:break
|
|
# Barrier: never launch pair 101 while any of first 100 are in flight.
|
|
while index<end and len(active)<limits.workers:
|
|
t=tasks[index];index+=1
|
|
active[t.pair_id]={'task':t,'start':elapsed,'cpu':0.}
|
|
events.append({'event':'start','pair_id':t.pair_id,'time':elapsed,'attempt':index})
|
|
peak_workers=max(peak_workers,len(active))
|
|
peak_tree_processes=max(peak_tree_processes,sum(len(a['task'].tree_rss_bytes) for a in active.values()))
|
|
if sum(t.tree_rss_bytes)>limits.pair_rss:finish(t.pair_id,'pair_tree_rss_cap')
|
|
if not active and index==end:
|
|
failed=sum(r['status']=='FAIL' for r in records)
|
|
assert len(records)==index
|
|
checkpoint={'attempted':index,'terminal':len(records),'failed':failed,'time':elapsed,'stop':failed*50>index}
|
|
checkpoints.append(checkpoint);events.append({'event':'checkpoint',**checkpoint})
|
|
if checkpoint['stop']:reason='failure_fraction_gt_2_percent';break
|
|
if index==len(tasks):break
|
|
end=min(end+100,len(tasks));continue
|
|
rate=sum(sum(a['task'].tree_cpu_rates) for a in active.values())
|
|
disk_rate=sum(a['task'].disk_rate for a in active.values())
|
|
remaining=[min(a['task'].duration,limits.pair_wall)-(elapsed-a['start']) for a in active.values()]
|
|
remaining += [limits.wall-elapsed]
|
|
if rate:remaining.append((limits.worker_cpu-worker_cpu)/rate)
|
|
if disk_rate:remaining.append((limits.disk-disk)/disk_rate)
|
|
dt=max(0.,min(remaining))
|
|
if dt==0:raise RuntimeError('Zero-time event without stop/terminal transition')
|
|
elapsed+=dt;supervisor_cpu+=supervisor_cpu_rate*dt;disk+=disk_rate*dt
|
|
for a in active.values():
|
|
c=sum(a['task'].tree_cpu_rates)*dt;a['cpu']+=c;worker_cpu+=c
|
|
for pid,a in list(active.items()):
|
|
t=a['task'];age=elapsed-a['start']
|
|
if age>=min(t.duration,limits.pair_wall):
|
|
finish(pid,'pair_wall_cap' if t.duration>limits.pair_wall else None)
|
|
reason=reason or global_limit()
|
|
if reason:
|
|
for pid in list(active):finish(pid,reason)
|
|
# Stop/global cap still records an end checkpoint against every attempted pair.
|
|
if records and (not checkpoints or checkpoints[-1]['attempted']!=index):
|
|
failed=sum(r['status']=='FAIL' for r in records)
|
|
checkpoints.append({'attempted':index,'terminal':len(records),'failed':failed,'time':elapsed,'stop':failed*50>index,'trigger':'end/global stop'})
|
|
if reason is None and checkpoints and checkpoints[-1]['stop']:
|
|
reason='failure_fraction_gt_2_percent'
|
|
assert len(records)==index and abs(sum(r['worker_tree_cpu'] for r in records)-worker_cpu)<1e-6
|
|
return {'kind':'PURE_VIRTUAL_CONTRACT_SIMULATION_NOT_REAL_PROCESS_SUPERVISION',
|
|
'status':'STOPPED' if reason else 'COMPLETE','stop_reason':reason,'attempted':index,
|
|
'not_attempted':len(tasks)-index,'records':records,'events':events,'checkpoints':checkpoints,
|
|
'virtual_worker_cpu':worker_cpu,'virtual_supervisor_cpu':supervisor_cpu,
|
|
'virtual_wall':elapsed,'virtual_disk':disk,'peak_worker_trees':peak_workers,
|
|
'peak_virtual_process_count_including_descendants':peak_tree_processes,
|
|
'real_workers_started':0,'retry':False}
|