nx_gpu_broker.nx
buildroot/runtime/nx_gpu_broker.nx
about
nx_gpu_broker.nx -- THE RESOURCE-SHARING BRAIN (serving-census capstone). Admits/schedules a MIXED workload --
background image-gen (Z-Image DiT, big VRAM, low priority, preemptible) + interactive chat (small VRAM, high
priority, latency-sensitive) -- onto ONE 16GB GPU, using BOTH levers together: (1) VRAM-AWARE admission (never
oversubscribe the card; co-resident jobs whose VRAM sums <= budget run TOGETHER) and (2) QoS PREEMPTION (a
high-priority arrival preempts enough low-priority resident jobs to fit, and the preempted job resumes later
with its work intact). This is the operator's "resource sharing" highlight made a working decision-maker --
it composes the verified nx_vram_estimate (budgets) + nx_sched_qos (preemption) ideas into one broker. Pure
logic, no GPU/model needed; VRAM never oversubscribed BY CONSTRUCTION (checked every op). license_tier: ORIGINAL expect_exit: 0
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 26 | struct GpuBroker |
consts
| 10 | const KIND_MAGIC_16384: i64 = 16384 |
| 11 | const KIND_MAGIC_10240: i64 = 10240 |
| 12 | const KIND_MAGIC_2048: i64 = 2048 |
| 13 | const KIND_MAGIC_12288: i64 = 12288 |
| 14 | const KIND_MAGIC_6144: i64 = 6144 |
| 16 | const BUSY: i64 = 1 |
| 17 | const FREE: i64 = 0 |
| 18 | const KIND_CHAT: i64 = 0 |
| 19 | const KIND_IMAGE: i64 = 1 |
| 20 | const QCAP: i64 = 64 |
| 23 | const R_REJECT: i64 = 0 - 1 // never fits (vram > budget) |
| 24 | const R_QUEUE: i64 = 0 - 2 // queued (no room, nothing preemptible) |
functions
| 32 | func gb_new(budget_mb: i64, n_slots: i64) -> *GpuBroker |
| 43 | func gb_free_bytes(b: *GpuBroker) -> i64 { return b.budget - b.used } |
| 44 | func gb_free_slot(b: *GpuBroker) -> i64 { let st: *i64=b.st; var i: i64=0; while i<b.n_slots { if st[i]==FREE { return i } i=i+1 } return 0-1 } |
| 45 | func gb_resident(b: *GpuBroker) -> i64 { let st: *i64=b.st; var n: i64=0; var i: i64=0; while i<b.n_slots { if st[i]==BUSY { n=n+1 } i=i+1 } return n } |
| 47 | func _enq(b: *GpuBroker, id: i64, kind: i64, vram: i64, prio: i64, work: i64) -> i64 |
| 52 | func _place(b: *GpuBroker, slot: i64, id: i64, kind: i64, vram: i64, prio: i64, work: i64) -> i64 |
| 59 | func _preemptible_vram(b: *GpuBroker, prio: i64) -> i64 called by 1: gb_admit |
| 66 | func _preempt_one(b: *GpuBroker, prio: i64) -> i64 |
| 79 | func gb_admit(b: *GpuBroker, id: i64, kind: i64, vram: i64, prio: i64, work: i64, preempted_out: *i64) -> i64 |
| 95 | func gb_complete(b: *GpuBroker, slot: i64) -> i64 |
| 112 | func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 113 | func gn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 } |
| 115 | func main() -> i64 |