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