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 / tests / test_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 | /* test_sched.c -- host tests for core/sched.c (the scheduler.rs test list, |
| 18 | * plus the cooperative-thread order the M3 boot check expects). */ |
| 19 | #include "ck_test.h" |
| 20 | #include "sched.h" |
| 21 | |
| 22 | static uint32_t tick(struct ck_sched *s, unsigned cpu) |
| 23 | { |
| 24 | uint32_t id = 0; |
| 25 | int r = ck_sched_tick(s, cpu, &id); |
| 26 | return r == 1 ? id : (r == 0 ? 0 : 0xffffffffu); |
| 27 | } |
| 28 | |
| 29 | int main(void) |
| 30 | { |
| 31 | struct ck_sched s; |
| 32 | uint8_t c0011[4] = { 0, 0, 1, 1 }, c0[1] = { 0 }, c01[2] = { 0, 1 }, c00[2] = { 0, 0 }; |
| 33 | |
| 34 | /* placement: latency on fast cores, background on efficient ones */ |
| 35 | ck_sched_init(&s, 4, c0011, 8, 8); |
| 36 | int cpu = ck_sched_enqueue(&s, 1, CK_PRIO_LATENCY); |
| 37 | CHECK(cpu == 2 || cpu == 3); |
| 38 | cpu = ck_sched_enqueue(&s, 2, CK_PRIO_BACKGROUND); |
| 39 | CHECK(cpu == 0 || cpu == 1); |
| 40 | |
| 41 | /* equal priority tasks share ticks fairly */ |
| 42 | ck_sched_init(&s, 1, c0, 4, 8); |
| 43 | ck_sched_enqueue(&s, 1, CK_PRIO_NORMAL); |
| 44 | ck_sched_enqueue(&s, 2, CK_PRIO_NORMAL); |
| 45 | for (int i = 0; i < 20; i++) |
| 46 | tick(&s, 0); |
| 47 | CHECK(ck_sched_task(&s, 1)->ticks == 10); |
| 48 | CHECK(ck_sched_task(&s, 2)->ticks == 10); |
| 49 | |
| 50 | /* fair order alternates (the threads demo relies on it) */ |
| 51 | ck_sched_init(&s, 1, c0, 4, 8); |
| 52 | ck_sched_enqueue(&s, 0, CK_PRIO_NORMAL); |
| 53 | ck_sched_enqueue(&s, 1, CK_PRIO_NORMAL); |
| 54 | char order[9] = { 0 }; |
| 55 | for (int i = 0; i < 8; i++) |
| 56 | order[i] = (char)('A' + tick(&s, 0)); |
| 57 | CHECK(order[0] == 'A' && order[1] == 'B' && order[6] == 'A' && order[7] == 'B'); |
| 58 | |
| 59 | /* priority prevents basic inversion */ |
| 60 | ck_sched_init(&s, 1, c0, 4, 8); |
| 61 | ck_sched_enqueue(&s, 1, CK_PRIO_BACKGROUND); |
| 62 | ck_sched_enqueue(&s, 2, CK_PRIO_LATENCY); |
| 63 | CHECK(tick(&s, 0) == 2); |
| 64 | |
| 65 | /* background runs within the configured interval under latency load */ |
| 66 | ck_sched_init(&s, 1, c0, 2, 7); |
| 67 | ck_sched_enqueue(&s, 1, CK_PRIO_BACKGROUND); |
| 68 | ck_sched_enqueue(&s, 2, CK_PRIO_LATENCY); |
| 69 | int bg = 0; |
| 70 | for (int i = 0; i < 7; i++) |
| 71 | bg |= tick(&s, 0) == 1; |
| 72 | CHECK(bg); |
| 73 | |
| 74 | /* latency keeps a large majority with a background budget */ |
| 75 | ck_sched_init(&s, 1, c0, 2, 8); |
| 76 | ck_sched_enqueue(&s, 1, CK_PRIO_BACKGROUND); |
| 77 | ck_sched_enqueue(&s, 2, CK_PRIO_LATENCY); |
| 78 | for (int i = 0; i < 80; i++) |
| 79 | tick(&s, 0); |
| 80 | CHECK(ck_sched_task(&s, 2)->ticks > 80 * 3 / 4); |
| 81 | CHECK(ck_sched_task(&s, 2)->ticks > ck_sched_task(&s, 1)->ticks); |
| 82 | |
| 83 | /* idle cpu steals and runs work */ |
| 84 | ck_sched_init(&s, 2, c01, 4, 8); |
| 85 | ck_sched_enqueue(&s, 1, CK_PRIO_BACKGROUND); |
| 86 | ck_sched_enqueue(&s, 2, CK_PRIO_BACKGROUND); |
| 87 | CHECK(ck_sched_steal(&s, 1) == 1); |
| 88 | CHECK(ck_sched_task(&s, 1)->cpu == 1 || ck_sched_task(&s, 2)->cpu == 1); |
| 89 | uint32_t id; |
| 90 | CHECK(ck_sched_tick(&s, 1, &id) == 1); |
| 91 | |
| 92 | /* empty queues and invalid cpu */ |
| 93 | ck_sched_init(&s, 2, c01, 2, 8); |
| 94 | CHECK(ck_sched_tick(&s, 0, &id) == 0); |
| 95 | CHECK(ck_sched_steal(&s, 1) == 0); |
| 96 | CHECK(ck_sched_tick(&s, 2, &id) == CK_SCHED_INVALID_CPU); |
| 97 | |
| 98 | /* round-robin placement and duplicate rejection */ |
| 99 | ck_sched_init(&s, 2, c00, 4, 8); |
| 100 | CHECK(ck_sched_enqueue(&s, 1, CK_PRIO_NORMAL) == 0); |
| 101 | CHECK(ck_sched_enqueue(&s, 2, CK_PRIO_NORMAL) == 1); |
| 102 | CHECK(ck_sched_enqueue(&s, 1, CK_PRIO_NORMAL) == CK_SCHED_DUPLICATE); |
| 103 | |
| 104 | /* removed tasks free their slot and leave the queue */ |
| 105 | ck_sched_init(&s, 1, c0, 2, 8); |
| 106 | ck_sched_enqueue(&s, 1, CK_PRIO_NORMAL); |
| 107 | ck_sched_enqueue(&s, 2, CK_PRIO_NORMAL); |
| 108 | CHECK(ck_sched_enqueue(&s, 3, CK_PRIO_NORMAL) == CK_SCHED_FULL); |
| 109 | CHECK(ck_sched_remove(&s, 1) == 0); |
| 110 | CHECK(s.queue_len[0] == 1); |
| 111 | CHECK(ck_sched_task(&s, 1) == 0); |
| 112 | CHECK(ck_sched_enqueue(&s, 3, CK_PRIO_NORMAL) == 0); |
| 113 | for (int i = 0; i < 4; i++) |
| 114 | CHECK(tick(&s, 0) != 1); |
| 115 | CHECK(ck_sched_remove(&s, 1) == CK_SCHED_UNKNOWN); |
| 116 | |
| 117 | /* no cpus, oversize */ |
| 118 | CHECK(ck_sched_init(&s, 0, c0, 2, 8) == 0); |
| 119 | CHECK(ck_sched_enqueue(&s, 1, CK_PRIO_NORMAL) == CK_SCHED_NO_CPUS); |
| 120 | CHECK(ck_sched_init(&s, CK_SCHED_MAX_CPUS + 1, c0, 2, 8) == -1); |
| 121 | CHECK(ck_sched_init(&s, 1, c0, CK_SCHED_MAX_TASKS + 1, 8) == -1); |
| 122 | return ck_t_verdict("test_sched"); |
| 123 | } |