interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / apps / omega-wattd / watt_sense.om

720 lines · 13455 bytes Raw Blame History
1note apps/omega-wattd/watt_sense.om
2note Pure seeded OM-2 live DGX Spark GB10 thermal, cpufreq, and ACPI CPPC sensor reader.
3note Strictly READ-ONLY: zero nvidia.ko, zero /dev/nvidia*, zero writes to sysfs/UEFI/SPI/PCIe.
4note Compose with si/directives/om2.om first, then watt_sense.om, watt_cbf.om, and wattd_main.om or gate_wattd.om.
5
6note Shared structs MUST precede the first fn in concatenated OM-2 units.
7struct WattThermal t_die long t_sink long t_amb long end
8struct WattPolicy p_budget long p_gpu_budget long t_ceil long tau long end
9struct WattProp p_gpu long p_cpu long p_unc long fan_pct long end
10struct WattCmd p_gpu long p_cpu long p_unc long fan_pct long fail_closed long clamped long end
11struct WattPlan p_gpu_req long p_cpu_req long p_unc_req long fan_req long precool_active long precool_fan long trough_mw long bpj long end
12
13note Output and sysfs scratch globals (never passed as arguments)
14global g_wout uchar 16384
15global g_wout_pos ulong 1
16global g_wpath uchar 256
17global g_wread uchar 256
18
19note Live sensor arrays (DGX Spark: 7 ACPI thermal zones, 20 Grace CPUs)
20global g_tz_mc long 7
21global g_tz_crit long 7
22global g_tz_ok long 7
23global g_cpu_khz long 20
24global g_cpu_ok long 20
25global g_cppc_ref ulong 20
26global g_cppc_del ulong 20
27global g_cppc_ok long 20
28
29note Summary metadata slots:
30note 0=zones_ok 1=cpus_ok 2=cppc_ok 3=a725_avg_khz 4=x925_avg_khz
31note 5=t_gpu_mc 6=t_soc_mc 7=t_unc_mc 8=t_die_max_mc 9=ts_mono_ns
32note 10=a725_del_ratio_pm 11=x925_del_ratio_pm 12=hw_writes 13=live_thermal_ok
33global g_wmeta long 16
34
35note Sysfs path fragments (strictly read-only thermal + cpufreq + acpi_cppc)
36text ws_p_tz_pre "/sys/class/thermal/thermal_zone"
37text ws_p_tz_temp "/temp"
38text ws_p_tz_crit "/trip_point_0_temp"
39text ws_p_cpu_pre "/sys/devices/system/cpu/cpu"
40text ws_p_cpu_freq "/cpufreq/scaling_cur_freq"
41text ws_p_cpu_cppc "/acpi_cppc/feedback_ctrs"
42
43note Sensor formatting labels
44text ws_lbl_hdr "OMEGA-WATTD SENSE driver=ZERO_NVIDIA actuator_mode=DRY_RUN_READONLY hw_writes=0\n"
45text ws_lbl_z0 "zone0[TSOC]"
46text ws_lbl_z1 "zone1[TS0E]"
47text ws_lbl_z2 "zone2[TS0P]"
48text ws_lbl_z3 "zone3[TS1E]"
49text ws_lbl_z4 "zone4[TS1P]"
50text ws_lbl_z5 "zone5[TGPU]"
51text ws_lbl_z6 "zone6[TUNC]"
52text ws_lbl_eq "="
53text ws_lbl_c "C"
54text ws_lbl_crit " crit="
55text ws_lbl_na "N/A"
56text ws_lbl_sp " "
57text ws_lbl_nl "\n"
58text ws_lbl_dot "."
59text ws_lbl_minus "-"
60text ws_lbl_summary1 "SUMMARY zones_ok="
61text ws_lbl_summary2 "/7 cpus_ok="
62text ws_lbl_summary3 "/20 cppc_ok="
63text ws_lbl_summary4 "/20 tgpu_mc="
64text ws_lbl_summary5 " tsoc_mc="
65text ws_lbl_summary6 " tunc_mc="
66text ws_lbl_summary7 " tmax_mc="
67text ws_lbl_grace1 "GRACE a725_avg_khz="
68text ws_lbl_grace2 " x925_avg_khz="
69text ws_lbl_grace3 " a725_cppc_pm="
70text ws_lbl_grace4 " x925_cppc_pm="
71text ws_lbl_grace5 " mono_ns="
72text ws_lbl_ref "ref:"
73text ws_lbl_del "del:"
74
75note Buffered output helpers (fd 1 = stdout, fd 2 = stderr)
76fn wd_flush_fd fd int to void
77used = get g_wout_pos 0
78if eq used 0
79ret
80end
81off = ulong 0
82while lt off used bound 16384
83k = write fd g_wout off sub used off
84if le k 0
85break
86end
87off = add off ulong k
88end
89put g_wout_pos 0 0
90end
91
92fn wd_flush to void
93wd_flush_fd 1
94end
95
96fn wd_putb b uchar to void
97used = get g_wout_pos 0
98if ge used 16384
99wd_flush_fd 1
100used = get g_wout_pos 0
101end
102put g_wout used b
103put g_wout_pos 0 add used 1
104end
105
106fn wd_puts s view uchar * to void
107n = len s
108if gt n 16384
109n = ulong 16384
110end
111for i ulong n bound 16384
112wd_putb get s i
113end
114end
115
116fn wd_put_u64 v ulong to void
117if ge v 10
118wd_put_u64 div v 10
119end
120d = rem v 10
121wd_putb uchar add 48 d
122end
123
124fn wd_put_i64 v long to void
125if lt v 0
126wd_putb 45
127note Avoid signed overflow on INT64_MIN by splitting last digit
128q = div v -10
129r = rem v 10
130if lt r 0
131r = neg r
132end
133if gt q 0
134wd_put_u64 ulong q
135end
136wd_putb uchar add 48 ulong r
137ret
138end
139wd_put_u64 ulong v
140end
141
142fn wd_put_fixed3 v long to void
143note Format milli-units (mC or mW) as signed D.DDD without INT64_MIN overflow
144whole = ulong 0
145frac = ulong 0
146if lt v 0
147wd_putb 45
148q = div v -1000
149r = rem v 1000
150if lt r 0
151r = neg r
152end
153whole = ulong q
154frac = ulong r
155else
156whole = ulong div v 1000
157frac = ulong rem v 1000
158end
159wd_put_u64 whole
160wd_putb 46
161d1 = div frac 100
162d2 = rem div frac 10 10
163d3 = rem frac 10
164wd_putb uchar add 48 d1
165wd_putb uchar add 48 d2
166wd_putb uchar add 48 d3
167end
168
169fn wd_eq_view a view uchar * b view uchar * to bool
170na = len a
171nb = len b
172if ne na nb
173ret false
174end
175if gt na 4096
176ret false
177end
178for i ulong na bound 4096
179if ne get a i get b i
180ret false
181end
182end
183ret true
184end
185
186fn wd_is_ws c uchar to bool
187if eq c 32
188ret true
189end
190if eq c 9
191ret true
192end
193if eq c 10
194ret true
195end
196if eq c 13
197ret true
198end
199ret false
200end
201
202note Parse signed decimal integer from byte slice; returns ok via g_wmeta[15] (1=ok, 0=err)
203fn wd_parse_i64 s view uchar * to long
204n = len s
205if or eq n 0 gt n 64
206put g_wmeta 15 0
207ret 0
208end
209i = ulong 0
210while lt i n bound 64
211c = get s i
212if or eq c 32 eq c 9
213i = add i 1
214else
215break
216end
217end
218if ge i n
219put g_wmeta 15 0
220ret 0
221end
222is_neg = false
223if eq get s i 45
224is_neg = true
225i = add i 1
226else
227if eq get s i 43
228i = add i 1
229end
230end
231if ge i n
232put g_wmeta 15 0
233ret 0
234end
235acc = ulong 0
236digits = ulong 0
237while lt i n bound 64
238c = get s i
239if and ge c 48 le c 57
240if gt acc 900000000000000000
241put g_wmeta 15 0
242ret 0
243end
244acc = add mul acc 10 ulong sub c 48
245digits = add digits 1
246i = add i 1
247else
248if wd_is_ws c
249break
250end
251put g_wmeta 15 0
252ret 0
253end
254end
255while lt i n bound 64
256if not wd_is_ws get s i
257put g_wmeta 15 0
258ret 0
259end
260i = add i 1
261end
262if eq digits 0
263put g_wmeta 15 0
264ret 0
265end
266put g_wmeta 15 1
267if is_neg
268ret neg long acc
269end
270ret long acc
271end
272
273note Path builder helpers into g_wpath
274fn ws_path_append pos ulong s view uchar * to ulong
275n = len s
276w = pos
277if gt add w n 250
278ret w
279end
280for i ulong n bound 256
281put g_wpath w get s i
282w = add w 1
283end
284ret w
285end
286
287fn ws_path_append_u64 pos ulong v ulong to ulong
288w = pos
289if ge v 10
290w = ws_path_append_u64 w div v 10
291end
292if lt w 250
293put g_wpath w uchar add 48 rem v 10
294w = add w 1
295end
296ret w
297end
298
299note Strictly read-only sysfs file read into g_wread; returns byte count or -1
300fn ws_read_gpath plen ulong to long
301if or eq plen 0 gt plen 250
302ret -1
303end
304fdl = fopen g_wpath 0 plen 0 0
305if lt fdl 0
306ret -1
307end
308fd = int fdl
309got = ulong 0
310while lt got 250 bound 16
311k = read fd g_wread got sub 250 got
312if lt k 0
313cl1 = fclose fd
314ret -1
315end
316if eq k 0
317break
318end
319got = add got ulong k
320end
321cl2 = fclose fd
322ret long got
323end
324
325note Parse signed long from g_wread[0..n); sets g_wmeta[15] = 1 on success
326fn ws_parse_gread_i64 n ulong to long
327tmp = new uchar 64 0
328m = n
329if gt m 64
330m = ulong 64
331end
332for i ulong m bound 64
333put tmp i get g_wread i
334end
335ret wd_parse_i64 part tmp 0 m
336end
337
338note Parse unsigned ulong from g_wread starting at off until non-digit; returns end pos in g_wmeta[14]
339fn ws_parse_gread_u64_at off ulong n ulong to ulong
340i = off
341acc = ulong 0
342digits = ulong 0
343while lt i n bound 256
344c = get g_wread i
345if and ge c 48 le c 57
346d = ulong sub c 48
347if gt acc 1844674407370955161
348put g_wmeta 14 long i
349put g_wmeta 15 0
350ret 0
351end
352if and eq acc 1844674407370955161 gt d 5
353put g_wmeta 14 long i
354put g_wmeta 15 0
355ret 0
356end
357acc = add mul acc 10 d
358digits = add digits 1
359i = add i 1
360else
361break
362end
363end
364put g_wmeta 14 long i
365if eq digits 0
366put g_wmeta 15 0
367ret 0
368end
369put g_wmeta 15 1
370ret acc
371end
372
373note Find 4-byte literal prefix (like "ref:" or "del:") in g_wread[0..n); returns index after prefix or n
374fn ws_find_tag4 n ulong tag view uchar * to ulong
375if or lt n 4 ne len tag 4
376ret n
377end
378lim = sub n 3
379for i ulong lim bound 256
380if eq get g_wread i get tag 0
381if eq get g_wread add i 1 get tag 1
382if eq get g_wread add i 2 get tag 2
383if eq get g_wread add i 3 get tag 3
384ret add i 4
385end
386end
387end
388end
389end
390ret n
391end
392
393fn ws_read_one_zone z ulong to void
394put g_tz_ok z 0
395put g_tz_mc z 0
396put g_tz_crit z 104800
397p = ws_path_append 0 ws_p_tz_pre
398p = ws_path_append_u64 p z
399p = ws_path_append p ws_p_tz_temp
400nr = ws_read_gpath p
401if gt nr 0
402val = ws_parse_gread_i64 ulong nr
403if eq get g_wmeta 15 1
404put g_tz_mc z val
405put g_tz_ok z 1
406end
407end
408p2 = ws_path_append 0 ws_p_tz_pre
409p2 = ws_path_append_u64 p2 z
410p2 = ws_path_append p2 ws_p_tz_crit
411nr2 = ws_read_gpath p2
412if gt nr2 0
413cv = ws_parse_gread_i64 ulong nr2
414if eq get g_wmeta 15 1
415put g_tz_crit z cv
416end
417end
418end
419
420fn ws_read_one_cpu c ulong to void
421put g_cpu_ok c 0
422put g_cpu_khz c 0
423p = ws_path_append 0 ws_p_cpu_pre
424p = ws_path_append_u64 p c
425p = ws_path_append p ws_p_cpu_freq
426nr = ws_read_gpath p
427if gt nr 0
428val = ws_parse_gread_i64 ulong nr
429if eq get g_wmeta 15 1
430put g_cpu_khz c val
431put g_cpu_ok c 1
432end
433end
434put g_cppc_ok c 0
435put g_cppc_ref c 0
436put g_cppc_del c 0
437p2 = ws_path_append 0 ws_p_cpu_pre
438p2 = ws_path_append_u64 p2 c
439p2 = ws_path_append p2 ws_p_cpu_cppc
440nr2 = ws_read_gpath p2
441if gt nr2 0
442un = ulong nr2
443ir = ws_find_tag4 un ws_lbl_ref
444id = ws_find_tag4 un ws_lbl_del
445if and lt ir un lt id un
446rv = ws_parse_gread_u64_at ir un
447ok1 = get g_wmeta 15
448dv = ws_parse_gread_u64_at id un
449ok2 = get g_wmeta 15
450if and eq ok1 1 eq ok2 1
451put g_cppc_ref c rv
452put g_cppc_del c dv
453put g_cppc_ok c 1
454end
455end
456end
457end
458
459note Check whether Grace CPU index is A725 E-core (0..4, 10..14) vs X925 P-core (5..9, 15..19)
460fn ws_is_a725 c ulong to bool
461if lt c 5
462ret true
463end
464if and ge c 10 lt c 15
465ret true
466end
467ret false
468end
469
470fn ws_sense_zones to void
471zok = long 0
472tmax = long 0
473has_max = false
474for z ulong 7
475ws_read_one_zone z
476if eq get g_tz_ok z 1
477zok = add zok 1
478tv = get g_tz_mc z
479if not has_max
480tmax = tv
481has_max = true
482else
483if gt tv tmax
484tmax = tv
485end
486end
487end
488end
489put g_wmeta 0 zok
490put g_wmeta 5 get g_tz_mc 5
491put g_wmeta 6 get g_tz_mc 0
492put g_wmeta 7 get g_tz_mc 6
493put g_wmeta 8 tmax
494live_ok = long 0
495if and eq get g_tz_ok 0 1 eq get g_tz_ok 5 1
496live_ok = 1
497end
498put g_wmeta 13 live_ok
499end
500
501fn ws_sense_cpus to void
502cok = long 0
503cpok = long 0
504a_sum = long 0
505a_cnt = long 0
506x_sum = long 0
507x_cnt = long 0
508for c ulong 20
509ws_read_one_cpu c
510if eq get g_cpu_ok c 1
511cok = add cok 1
512fv = get g_cpu_khz c
513if ws_is_a725 c
514a_sum = add a_sum fv
515a_cnt = add a_cnt 1
516else
517x_sum = add x_sum fv
518x_cnt = add x_cnt 1
519end
520end
521if eq get g_cppc_ok c 1
522cpok = add cpok 1
523end
524end
525put g_wmeta 1 cok
526put g_wmeta 2 cpok
527if gt a_cnt 0
528put g_wmeta 3 div a_sum a_cnt
529else
530put g_wmeta 3 0
531end
532if gt x_cnt 0
533put g_wmeta 4 div x_sum x_cnt
534else
535put g_wmeta 4 0
536end
537end
538
539fn ws_sense_all to void
540put g_wmeta 12 0
541put g_wmeta 9 monons
542ws_sense_zones
543ws_sense_cpus
544ws_compute_cppc_ratios
545end
546
547note Overflow-safe CPPC delivered/reference permille helper (handles both tiny and 64-bit counters)
548fn ws_ratio_permille dv ulong rv ulong to long
549if eq rv 0
550ret 0
551end
552ds = dv
553rs = rv
554while or gt ds 10000000000000 gt rs 10000000000000 bound 32
555ds = shr ds 2
556rs = shr rs 2
557end
558if eq rs 0
559ret 1000000
560end
561q = div mul ds 1000 rs
562if gt q 1000000
563ret 1000000
564end
565ret long q
566end
567
568fn ws_compute_cppc_ratios to void
569a_pm = long 0
570a_n = long 0
571x_pm = long 0
572x_n = long 0
573for c ulong 20
574if eq get g_cppc_ok c 1
575rv = get g_cppc_ref c
576dv = get g_cppc_del c
577if gt rv 0
578pm = ws_ratio_permille dv rv
579if ws_is_a725 c
580a_pm = add a_pm pm
581a_n = add a_n 1
582else
583x_pm = add x_pm pm
584x_n = add x_n 1
585end
586end
587end
588end
589if gt a_n 0
590put g_wmeta 10 div a_pm a_n
591else
592put g_wmeta 10 0
593end
594if gt x_n 0
595put g_wmeta 11 div x_pm x_n
596else
597put g_wmeta 11 0
598end
599end
600
601note Derive conservative whole-unit 2-node thermal state from live sensors.
602note Returns true iff critical live sensors (zone0 [TSOC] and zone5 [TGPU]) are valid;
603note otherwise populates fail-closed critical sentinel (104.8 C) and returns false.
604fn ws_fill_thermal_state st edit WattThermal to bool
605if or ne get g_tz_ok 0 1 ne get g_tz_ok 5 1
606put st t_die 104800
607put st t_sink 104800
608put st t_amb 25000
609ret false
610end
611note Whole-unit Node 1 = max across all live GB10 silicon zones (covers BOTH TGPU and Grace TSOC)
612tdie = get g_wmeta 8
613tgpu = get g_tz_mc 5
614if gt tgpu tdie
615tdie = tgpu
616end
617tsoc = get g_tz_mc 0
618if gt tsoc tdie
619tdie = tsoc
620end
621note Node 2 = conservative vapor-chamber proxy (never fabricate cold 25C sink when zone6 TUNC missing)
622tsink = sub tdie 2000
623if eq get g_tz_ok 6 1
624tunc_p = sub get g_tz_mc 6 4000
625if lt tunc_p tsink
626tsink = tunc_p
627end
628end
629tamb = long 25000
630if lt tsink 25000
631if ge tdie 25000
632tsink = 25000
633if gt tsink tdie
634tsink = tdie
635end
636else
637tamb = tsink
638end
639end
640if lt tdie tamb
641tamb = tdie
642end
643put st t_die tdie
644put st t_sink tsink
645put st t_amb tamb
646ret true
647end
648
649fn ws_print_zone_lbl z ulong to void
650if eq z 0
651wd_puts ws_lbl_z0
652ret
653end
654if eq z 1
655wd_puts ws_lbl_z1
656ret
657end
658if eq z 2
659wd_puts ws_lbl_z2
660ret
661end
662if eq z 3
663wd_puts ws_lbl_z3
664ret
665end
666if eq z 4
667wd_puts ws_lbl_z4
668ret
669end
670if eq z 5
671wd_puts ws_lbl_z5
672ret
673end
674wd_puts ws_lbl_z6
675end
676
677fn ws_print_sense to void
678wd_puts ws_lbl_hdr
679for z ulong 7
680ws_print_zone_lbl z
681wd_puts ws_lbl_eq
682if eq get g_tz_ok z 1
683wd_put_fixed3 get g_tz_mc z
684wd_puts ws_lbl_c
685wd_puts ws_lbl_crit
686wd_put_fixed3 get g_tz_crit z
687wd_puts ws_lbl_c
688else
689wd_puts ws_lbl_na
690end
691wd_puts ws_lbl_nl
692end
693wd_puts ws_lbl_summary1
694wd_put_i64 get g_wmeta 0
695wd_puts ws_lbl_summary2
696wd_put_i64 get g_wmeta 1
697wd_puts ws_lbl_summary3
698wd_put_i64 get g_wmeta 2
699wd_puts ws_lbl_summary4
700wd_put_i64 get g_wmeta 5
701wd_puts ws_lbl_summary5
702wd_put_i64 get g_wmeta 6
703wd_puts ws_lbl_summary6
704wd_put_i64 get g_wmeta 7
705wd_puts ws_lbl_summary7
706wd_put_i64 get g_wmeta 8
707wd_puts ws_lbl_nl
708wd_puts ws_lbl_grace1
709wd_put_i64 get g_wmeta 3
710wd_puts ws_lbl_grace2
711wd_put_i64 get g_wmeta 4
712wd_puts ws_lbl_grace3
713wd_put_i64 get g_wmeta 10
714wd_puts ws_lbl_grace4
715wd_put_i64 get g_wmeta 11
716wd_puts ws_lbl_grace5
717wd_put_i64 get g_wmeta 9
718wd_puts ws_lbl_nl
719wd_flush
720end