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 / aienux / kernel / core / scheduler.om

389 lines · 7769 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/core/scheduler.om: Pure-Omega Aienux Kernel Scheduler (MADT CPU placement,
3note fair priority + least-served run queues, anti-starvation background reservation, work stealing).
4note Converted from upstream aienux crates/aienos-kernel/src/scheduler.rs.
5note Compose with: si/directives/om2.om aienux/kernel/core/scheduler.om aienux/kernel/core/ipc.om aienux/kernel/core/pagetable.om <test.om>
6
7note Shared kernel core structs (declared before first fn per OM-2 rule)
8struct IpcMsg tag uint sender uint arg0 ulong arg1 ulong end
9
10struct PtFlags attr uchar writable uchar user uchar executable uchar shareable uchar global_flag uchar end
11
12struct PtTrans pa ulong page_size ulong attr uchar writable uchar user uchar executable uchar shareable uchar global_flag uchar end
13
14note Scheduler priorities
15const SCHED_PRIO_BG uchar 0
16const SCHED_PRIO_NORMAL uchar 1
17const SCHED_PRIO_LATENCY uchar 2
18
19note Scheduler return / error codes
20const SCHED_OK int 0
21const SCHED_ERR_NO_CPUS int -1
22const SCHED_ERR_FULL int -2
23const SCHED_ERR_DUP_TASK int -3
24const SCHED_ERR_BAD_CPU int -4
25const SCHED_ERR_UNKNOWN int -5
26
27const SCHED_MAX_CPUS ulong 8
28const SCHED_MAX_TASKS ulong 64
29const SCHED_SLOT_NONE uint 0xffffffff
30
31note Scheduler metadata: 0=ncpus, 1=bg_interval, 2=cursor
32global sched_meta ulong 4
33global sched_classes uchar 8
34global sched_qlen ulong 8
35global sched_bg_wait ulong 8
36note Queues: 8 CPUs * 64 slots = 512 entries
37global sched_queues uint 512
38
39note Task table (64 slots)
40global sched_t_used uchar 64
41global sched_t_id uint 64
42global sched_t_prio uchar 64
43global sched_t_cpu ulong 64
44global sched_t_ticks ulong 64
45
46fn sched_init ncpus ulong bg_interval ulong classes view uchar * to int
47if or eq ncpus 0 or gt ncpus SCHED_MAX_CPUS lt len classes ncpus
48ret SCHED_ERR_NO_CPUS
49end
50bgi = bg_interval
51if eq bgi 0
52bgi = ulong 1
53end
54put sched_meta 0 ncpus
55put sched_meta 1 bgi
56put sched_meta 2 0
57for c ulong 8
58put sched_classes c 0
59put sched_qlen c 0
60put sched_bg_wait c 0
61end
62i = ulong 0
63while lt i ncpus bound 8
64put sched_classes i get classes i
65i = add i 1
66end
67for q ulong 512
68put sched_queues q SCHED_SLOT_NONE
69end
70for t ulong 64
71put sched_t_used t 0
72put sched_t_id t 0
73put sched_t_prio t 0
74put sched_t_cpu t 0
75put sched_t_ticks t 0
76end
77ret SCHED_OK
78end
79
80fn sched_place prio uchar cpu_out edit ulong 1 to int
81ncpus = get sched_meta 0
82if eq ncpus 0
83ret SCHED_ERR_NO_CPUS
84end
85cur = rem get sched_meta 2 ncpus
86if eq prio SCHED_PRIO_NORMAL
87best = cur
88best_len = get sched_qlen best
89off = ulong 1
90while lt off ncpus bound 8
91c = rem add cur off ncpus
92ql = get sched_qlen c
93if lt ql best_len
94best = c
95best_len = ql
96end
97off = add off 1
98end
99put cpu_out 0 best
100ret SCHED_OK
101end
102best_cls = get sched_classes 0
103i = ulong 1
104while lt i ncpus bound 8
105cl = get sched_classes i
106if eq prio SCHED_PRIO_LATENCY
107if gt cl best_cls
108best_cls = cl
109end
110else
111if lt cl best_cls
112best_cls = cl
113end
114end
115i = add i 1
116end
117off = ulong 0
118while lt off ncpus bound 8
119c = rem add cur off ncpus
120if eq get sched_classes c best_cls
121put cpu_out 0 c
122ret SCHED_OK
123end
124off = add off 1
125end
126ret SCHED_ERR_NO_CPUS
127end
128
129fn sched_enqueue tid uint prio uchar cpu_out edit ulong 1 to int
130ncpus = get sched_meta 0
131if eq ncpus 0
132ret SCHED_ERR_NO_CPUS
133end
134for i ulong 64
135if ne get sched_t_used i 0
136if eq get sched_t_id i tid
137ret SCHED_ERR_DUP_TASK
138end
139end
140end
141slot = ulong 64
142for i ulong 64
143if eq get sched_t_used i 0
144slot = i
145break
146end
147end
148if ge slot SCHED_MAX_TASKS
149ret SCHED_ERR_FULL
150end
151cbox = new ulong 1 0
152rc = sched_place prio cbox
153if ne rc SCHED_OK
154ret rc
155end
156cpu = get cbox 0
157put sched_meta 2 rem add cpu 1 ncpus
158ql = get sched_qlen cpu
159put sched_queues add mul cpu 64 ql uint slot
160put sched_qlen cpu add ql 1
161put sched_t_used slot 1
162put sched_t_id slot tid
163put sched_t_prio slot prio
164put sched_t_cpu slot cpu
165put sched_t_ticks slot 0
166put cpu_out 0 cpu
167ret SCHED_OK
168end
169
170fn sched_queue_remove_at c ulong pos ulong to void
171ql = get sched_qlen c
172base = mul c 64
173last = sub ql 1
174k = pos
175while lt k last bound 64
176nxt = get sched_queues add base add k 1
177put sched_queues add base k nxt
178k = add k 1
179end
180put sched_queues add base last SCHED_SLOT_NONE
181put sched_qlen c last
182end
183
184fn sched_remove tid uint to int
185slot = ulong 64
186for i ulong 64
187if ne get sched_t_used i 0
188if eq get sched_t_id i tid
189slot = i
190break
191end
192end
193end
194if ge slot SCHED_MAX_TASKS
195ret SCHED_ERR_UNKNOWN
196end
197ncpus = get sched_meta 0
198c = ulong 0
199while lt c ncpus bound 8
200ql = get sched_qlen c
201base = mul c 64
202found = ulong 64
203j = ulong 0
204while lt j ql bound 64
205if eq get sched_queues add base j uint slot
206found = j
207break
208end
209j = add j 1
210end
211if lt found ql
212sched_queue_remove_at c found
213break
214end
215c = add c 1
216end
217put sched_t_used slot 0
218ret SCHED_OK
219end
220
221fn sched_steal_pick donor ulong to ulong
222base = mul donor 64
223dlen = get sched_qlen donor
224cand_pos = ulong 0
225cand_slot = ulong get sched_queues base
226j = ulong 1
227while lt j dlen bound 64
228sl = ulong get sched_queues add base j
229sp = get sched_t_prio sl
230cp = get sched_t_prio cand_slot
231better = lt sp cp
232if eq sp cp
233if gt get sched_t_ticks sl get sched_t_ticks cand_slot
234better = true
235end
236end
237if better
238cand_pos = j
239cand_slot = sl
240end
241j = add j 1
242end
243ret cand_pos
244end
245
246fn sched_steal idle_cpu ulong stole_out edit uchar 1 to int
247ncpus = get sched_meta 0
248if ge idle_cpu ncpus
249ret SCHED_ERR_BAD_CPU
250end
251put stole_out 0 0
252if ne get sched_qlen idle_cpu 0
253ret SCHED_OK
254end
255donor = ulong 8
256best_len = ulong 1
257c = ulong 0
258while lt c ncpus bound 8
259if ne c idle_cpu
260ql = get sched_qlen c
261if gt ql best_len
262donor = c
263best_len = ql
264end
265end
266c = add c 1
267end
268if ge donor ncpus
269ret SCHED_OK
270end
271cand_pos = sched_steal_pick donor
272cand_slot = ulong get sched_queues add mul donor 64 cand_pos
273sched_queue_remove_at donor cand_pos
274put sched_queues mul idle_cpu 64 uint cand_slot
275put sched_qlen idle_cpu 1
276put sched_t_cpu cand_slot idle_cpu
277put stole_out 0 1
278ret SCHED_OK
279end
280
281fn sched_select_slot cpu ulong force_bg bool to ulong
282base = mul cpu 64
283ql = get sched_qlen cpu
284sel = ulong get sched_queues base
285j = ulong 1
286while lt j ql bound 64
287sl = ulong get sched_queues add base j
288sp = get sched_t_prio sl
289op = get sched_t_prio sel
290wins = false
291if force_bg
292if eq sp SCHED_PRIO_BG
293if ne op SCHED_PRIO_BG
294wins = true
295end
296end
297if eq sp op
298if lt get sched_t_ticks sl get sched_t_ticks sel
299wins = true
300end
301end
302else
303if gt sp op
304wins = true
305end
306if eq sp op
307if lt get sched_t_ticks sl get sched_t_ticks sel
308wins = true
309end
310end
311end
312if wins
313sel = sl
314end
315j = add j 1
316end
317ret sel
318end
319
320note Account one tick on cpu. Sets ran_out[0]=1 and tid_out[0]=task id if a task ran.
321fn sched_tick cpu ulong ran_out edit uchar 1 tid_out edit uint 1 to int
322ncpus = get sched_meta 0
323if ge cpu ncpus
324ret SCHED_ERR_BAD_CPU
325end
326put ran_out 0 0
327put tid_out 0 0
328stole = new uchar 1 0
329if eq get sched_qlen cpu 0
330rc = sched_steal cpu stole
331if ne rc SCHED_OK
332ret rc
333end
334if eq get stole 0 0
335ret SCHED_OK
336end
337end
338base = mul cpu 64
339ql = get sched_qlen cpu
340has_bg = false
341has_hi = false
342j = ulong 0
343while lt j ql bound 64
344sl = ulong get sched_queues add base j
345pr = get sched_t_prio sl
346if eq pr SCHED_PRIO_BG
347has_bg = true
348else
349has_hi = true
350end
351j = add j 1
352end
353force_bg = false
354if and has_bg has_hi
355bw = add get sched_bg_wait cpu 1
356put sched_bg_wait cpu bw
357if ge bw get sched_meta 1
358force_bg = true
359end
360else
361put sched_bg_wait cpu 0
362end
363sel = sched_select_slot cpu force_bg
364if eq get sched_t_prio sel SCHED_PRIO_BG
365put sched_bg_wait cpu 0
366end
367put sched_t_ticks sel add get sched_t_ticks sel 1
368put ran_out 0 1
369put tid_out 0 get sched_t_id sel
370ret SCHED_OK
371end
372
373fn sched_task_ticks tid uint to ulong
374for i ulong 64
375if ne get sched_t_used i 0
376if eq get sched_t_id i tid
377ret get sched_t_ticks i
378end
379end
380end
381ret 0
382end
383
384fn sched_queue_len cpu ulong to ulong
385if ge cpu SCHED_MAX_CPUS
386ret 0
387end
388ret get sched_qlen cpu
389end