code wiki / (root) / nx_gpu_broker.nx

nx_gpu_broker.nx

buildroot/runtime/nx_gpu_broker.nx

9852 B154 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind tooltopic gpu
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_gpu_broker.nx

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

main gw sys_write sys_mmap gb_new sys_mmap ↻ gb_admit gb_free_bytes gb_free_slot _place _preemptible_vram _preempt_one _enq _enq ↻ gn sys_mmap ↻ sys_write ↻ sys_exit

structs

26struct GpuBroker

consts

10const KIND_MAGIC_16384: i64 = 16384
11const KIND_MAGIC_10240: i64 = 10240
12const KIND_MAGIC_2048: i64 = 2048
13const KIND_MAGIC_12288: i64 = 12288
14const KIND_MAGIC_6144: i64 = 6144
16const BUSY: i64 = 1
17const FREE: i64 = 0
18const KIND_CHAT: i64 = 0
19const KIND_IMAGE: i64 = 1
20const QCAP: i64 = 64
23const R_REJECT: i64 = 0 - 1 // never fits (vram > budget)
24const R_QUEUE: i64 = 0 - 2 // queued (no room, nothing preemptible)

functions

32func gb_new(budget_mb: i64, n_slots: i64) -> *GpuBroker
called by 1: main calls 1: sys_mmap
43func gb_free_bytes(b: *GpuBroker) -> i64 { return b.budget - b.used }
called by 2: gb_admitgb_complete
44func 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 }
called by 2: gb_admitgb_complete
45func 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 }
47func _enq(b: *GpuBroker, id: i64, kind: i64, vram: i64, prio: i64, work: i64) -> i64
52func _place(b: *GpuBroker, slot: i64, id: i64, kind: i64, vram: i64, prio: i64, work: i64) -> i64
called by 2: gb_admitgb_complete
59func _preemptible_vram(b: *GpuBroker, prio: i64) -> i64
called by 1: gb_admit
66func _preempt_one(b: *GpuBroker, prio: i64) -> i64
called by 1: gb_admit calls 1: _enq
79func gb_admit(b: *GpuBroker, id: i64, kind: i64, vram: i64, prio: i64, work: i64, preempted_out: *i64) -> i64
95func gb_complete(b: *GpuBroker, slot: i64) -> i64
112func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
called by 1: main calls 1: sys_write
113func 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 }
called by 1: main calls 2: sys_mmapsys_write
115func main() -> i64