code wiki / _hdl_build / nx_nomad_schedule.nx
nx_nomad_schedule.nx source
↩ module page · 46 lines · 2173 B
1// nx_nomad_schedule.nx -- sovereign CLUSTER SCHEDULER core (HashiCorp Nomad-class; NOMAD-001, new frontier).
2// Place a job (cpu+mem request) onto the BEST-FIT node via BIN-PACKING: among nodes that have enough free
3// cpu AND mem (feasibility), pick the one with the LEAST leftover (tightest fit) so the cluster packs dense
4// and leaves the largest contiguous holes for big jobs. Placing decrements that node's free capacity. A job
5// that fits NOWHERE is REJECTED (no overcommit -- the safety property). Pure logic over node arrays; this is
6// the allocator the rest of Nomad (eval queue, reschedule-on-failure, constraints) builds on. ORIGINAL.
7import "nx_syscalls.nx"
8
9const NOMAD_UNPLACEABLE: i64 = 0 - 1
10
11// does node i have room for (cpu,mem)?
12func ns_feasible(fcpu: *i64, fmem: *i64, i: i64, cpu: i64, mem: i64) -> i64 {
13 if fcpu[i] >= cpu { if fmem[i] >= mem { return 1 } }
14 return 0
15}
16
17// bin-pack score = leftover after placing (cpu+mem). LOWER = tighter fit = preferred. only valid if feasible.
18func ns_leftover(fcpu: *i64, fmem: *i64, i: i64, cpu: i64, mem: i64) -> i64 {
19 return (fcpu[i] - cpu) + (fmem[i] - mem)
20}
21
22// choose the best-fit node index for (cpu,mem), or NOMAD_UNPLACEABLE if none feasible. tightest leftover wins;
23// ties -> lowest index (deterministic).
24func ns_place(fcpu: *i64, fmem: *i64, n: i64, cpu: i64, mem: i64) -> i64 {
25 var best: i64 = NOMAD_UNPLACEABLE
26 var best_score: i64 = 0
27 var i: i64 = 0
28 while i < n {
29 if ns_feasible(fcpu, fmem, i, cpu, mem) == 1 {
30 let sc: i64 = ns_leftover(fcpu, fmem, i, cpu, mem)
31 if best == NOMAD_UNPLACEABLE { best = i; best_score = sc }
32 else { if sc < best_score { best = i; best_score = sc } }
33 }
34 i = i + 1
35 }
36 return best
37}
38
39// commit a placement: decrement the chosen node's free capacity. returns the node, or UNPLACEABLE (no change).
40func ns_alloc(fcpu: *i64, fmem: *i64, n: i64, cpu: i64, mem: i64) -> i64 {
41 let node: i64 = ns_place(fcpu, fmem, n, cpu, mem)
42 if node == NOMAD_UNPLACEABLE { return NOMAD_UNPLACEABLE }
43 fcpu[node] = fcpu[node] - cpu
44 fmem[node] = fmem[node] - mem
45 return node
46}