aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / sched.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* sched.c -- see sched.h. Same decisions as scheduler.rs, step for step. */ |
| 18 | #include "sched.h" |
| 19 | |
| 20 | int ck_sched_init(struct ck_sched *s, unsigned cpus, const uint8_t *classes, unsigned tasks, |
| 21 | unsigned bg_interval) |
| 22 | { |
| 23 | if (cpus > CK_SCHED_MAX_CPUS || tasks > CK_SCHED_MAX_TASKS) |
| 24 | return -1; |
| 25 | s->cpus = cpus; |
| 26 | s->ntasks = tasks; |
| 27 | for (unsigned c = 0; c < CK_SCHED_MAX_CPUS; c++) { |
| 28 | s->classes[c] = c < cpus ? classes[c] : 0; |
| 29 | s->queue_len[c] = 0; |
| 30 | s->bg_wait[c] = 0; |
| 31 | for (unsigned t = 0; t < CK_SCHED_MAX_TASKS; t++) |
| 32 | s->queues[c][t] = -1; |
| 33 | } |
| 34 | for (unsigned t = 0; t < CK_SCHED_MAX_TASKS; t++) { |
| 35 | s->used[t] = 0; |
| 36 | s->tasks[t] = (struct ck_task){ 0 }; |
| 37 | } |
| 38 | s->bg_interval = bg_interval ? bg_interval : 1; |
| 39 | s->cursor = 0; |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | int ck_sched_place(const struct ck_sched *s, enum ck_prio prio) |
| 44 | { |
| 45 | if (s->cpus == 0) |
| 46 | return CK_SCHED_NO_CPUS; |
| 47 | unsigned start = s->cursor % s->cpus; |
| 48 | if (prio == CK_PRIO_NORMAL) { |
| 49 | unsigned best = start; |
| 50 | for (unsigned off = 1; off < s->cpus; off++) { |
| 51 | unsigned cpu = (start + off) % s->cpus; |
| 52 | if (s->queue_len[cpu] < s->queue_len[best]) |
| 53 | best = cpu; |
| 54 | } |
| 55 | return (int)best; |
| 56 | } |
| 57 | uint8_t want = s->classes[0]; |
| 58 | for (unsigned c = 1; c < s->cpus; c++) { |
| 59 | if (prio == CK_PRIO_LATENCY ? s->classes[c] > want : s->classes[c] < want) |
| 60 | want = s->classes[c]; |
| 61 | } |
| 62 | for (unsigned off = 0; off < s->cpus; off++) { |
| 63 | unsigned cpu = (start + off) % s->cpus; |
| 64 | if (s->classes[cpu] == want) |
| 65 | return (int)cpu; |
| 66 | } |
| 67 | return CK_SCHED_NO_CPUS; /* unreachable */ |
| 68 | } |
| 69 | |
| 70 | static int slot_of(const struct ck_sched *s, uint32_t id) |
| 71 | { |
| 72 | for (unsigned t = 0; t < s->ntasks; t++) |
| 73 | if (s->used[t] && s->tasks[t].id == id) |
| 74 | return (int)t; |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | int ck_sched_enqueue(struct ck_sched *s, uint32_t id, enum ck_prio prio) |
| 79 | { |
| 80 | if (slot_of(s, id) >= 0) |
| 81 | return CK_SCHED_DUPLICATE; |
| 82 | int slot = -1; |
| 83 | for (unsigned t = 0; t < s->ntasks; t++) |
| 84 | if (!s->used[t]) { |
| 85 | slot = (int)t; |
| 86 | break; |
| 87 | } |
| 88 | if (slot < 0) |
| 89 | return CK_SCHED_FULL; |
| 90 | int cpu = ck_sched_place(s, prio); |
| 91 | if (cpu < 0) |
| 92 | return cpu; |
| 93 | s->cursor = ((unsigned)cpu + 1) % s->cpus; |
| 94 | s->queues[cpu][s->queue_len[cpu]++] = slot; |
| 95 | s->used[slot] = 1; |
| 96 | s->tasks[slot] = (struct ck_task){ .id = id, .prio = prio, .cpu = (unsigned)cpu, .ticks = 0 }; |
| 97 | return cpu; |
| 98 | } |
| 99 | |
| 100 | static void queue_drop(struct ck_sched *s, unsigned cpu, unsigned at) |
| 101 | { |
| 102 | unsigned len = s->queue_len[cpu]; |
| 103 | for (unsigned i = at; i + 1 < len; i++) |
| 104 | s->queues[cpu][i] = s->queues[cpu][i + 1]; |
| 105 | s->queues[cpu][len - 1] = -1; |
| 106 | s->queue_len[cpu] = len - 1; |
| 107 | } |
| 108 | |
| 109 | int ck_sched_remove(struct ck_sched *s, uint32_t id) |
| 110 | { |
| 111 | int slot = slot_of(s, id); |
| 112 | if (slot < 0) |
| 113 | return CK_SCHED_UNKNOWN; |
| 114 | for (unsigned cpu = 0; cpu < s->cpus; cpu++) { |
| 115 | for (unsigned i = 0; i < s->queue_len[cpu]; i++) { |
| 116 | if (s->queues[cpu][i] == slot) { |
| 117 | queue_drop(s, cpu, i); |
| 118 | goto done; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | done: |
| 123 | s->used[slot] = 0; |
| 124 | return CK_SCHED_OK; |
| 125 | } |
| 126 | |
| 127 | int ck_sched_steal(struct ck_sched *s, unsigned idle) |
| 128 | { |
| 129 | if (idle >= s->cpus) |
| 130 | return CK_SCHED_INVALID_CPU; |
| 131 | if (s->queue_len[idle] != 0) |
| 132 | return 0; |
| 133 | int donor = -1; |
| 134 | for (unsigned cpu = 0; cpu < s->cpus; cpu++) |
| 135 | if (cpu != idle && s->queue_len[cpu] > 1 && |
| 136 | (donor < 0 || s->queue_len[cpu] > s->queue_len[donor])) |
| 137 | donor = (int)cpu; |
| 138 | if (donor < 0) |
| 139 | return 0; |
| 140 | /* Candidate: lowest priority, then most ticks (first wins ties). */ |
| 141 | unsigned pick = 0; |
| 142 | for (unsigned i = 1; i < s->queue_len[donor]; i++) { |
| 143 | const struct ck_task *t = &s->tasks[s->queues[donor][i]]; |
| 144 | const struct ck_task *p = &s->tasks[s->queues[donor][pick]]; |
| 145 | if (t->prio < p->prio || (t->prio == p->prio && t->ticks > p->ticks)) |
| 146 | pick = i; |
| 147 | } |
| 148 | int slot = s->queues[donor][pick]; |
| 149 | queue_drop(s, (unsigned)donor, pick); |
| 150 | s->queues[idle][0] = slot; |
| 151 | s->queue_len[idle] = 1; |
| 152 | s->tasks[slot].cpu = idle; |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | int ck_sched_tick(struct ck_sched *s, unsigned cpu, uint32_t *id) |
| 157 | { |
| 158 | if (cpu >= s->cpus) |
| 159 | return CK_SCHED_INVALID_CPU; |
| 160 | if (s->queue_len[cpu] == 0) { |
| 161 | int st = ck_sched_steal(s, cpu); |
| 162 | if (st < 0) |
| 163 | return st; |
| 164 | if (!st) |
| 165 | return 0; |
| 166 | } |
| 167 | unsigned len = s->queue_len[cpu]; |
| 168 | int has_bg = 0, has_higher = 0; |
| 169 | for (unsigned i = 0; i < len; i++) { |
| 170 | enum ck_prio p = s->tasks[s->queues[cpu][i]].prio; |
| 171 | if (p == CK_PRIO_BACKGROUND) |
| 172 | has_bg = 1; |
| 173 | else |
| 174 | has_higher = 1; |
| 175 | } |
| 176 | int force_bg = 0; |
| 177 | if (has_bg && has_higher) { |
| 178 | s->bg_wait[cpu]++; |
| 179 | force_bg = s->bg_wait[cpu] >= s->bg_interval; |
| 180 | } else { |
| 181 | s->bg_wait[cpu] = 0; |
| 182 | } |
| 183 | int sel = s->queues[cpu][0]; |
| 184 | for (unsigned i = 1; i < len; i++) { |
| 185 | int slot = s->queues[cpu][i]; |
| 186 | const struct ck_task *t = &s->tasks[slot], *p = &s->tasks[sel]; |
| 187 | int wins; |
| 188 | if (force_bg) |
| 189 | wins = (t->prio == CK_PRIO_BACKGROUND && p->prio != CK_PRIO_BACKGROUND) || |
| 190 | (t->prio == p->prio && t->ticks < p->ticks); |
| 191 | else |
| 192 | wins = t->prio > p->prio || (t->prio == p->prio && t->ticks < p->ticks); |
| 193 | if (wins) |
| 194 | sel = slot; |
| 195 | } |
| 196 | if (s->tasks[sel].prio == CK_PRIO_BACKGROUND) |
| 197 | s->bg_wait[cpu] = 0; |
| 198 | s->tasks[sel].ticks++; |
| 199 | *id = s->tasks[sel].id; |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | const struct ck_task *ck_sched_task(const struct ck_sched *s, uint32_t id) |
| 204 | { |
| 205 | int slot = slot_of(s, id); |
| 206 | return slot < 0 ? 0 : &s->tasks[slot]; |
| 207 | } |