[f761f1eb] | 1 | /*
|
---|
[481d4751] | 2 | * Copyright (c) 2010 Jakub Jermar
|
---|
[25939997] | 3 | * Copyright (c) 2023 Jiří Zárevúcky
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[174156fd] | 30 | /** @addtogroup kernel_generic_proc
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[9179d0a] | 34 | /**
|
---|
[b45c443] | 35 | * @file
|
---|
[da1bafb] | 36 | * @brief Scheduler and load balancing.
|
---|
[9179d0a] | 37 | *
|
---|
[cf26ba9] | 38 | * This file contains the scheduler and kcpulb kernel thread which
|
---|
[9179d0a] | 39 | * performs load-balancing of per-CPU run queues.
|
---|
| 40 | */
|
---|
| 41 |
|
---|
[63e27ef] | 42 | #include <assert.h>
|
---|
[4621d23] | 43 | #include <atomic.h>
|
---|
[f761f1eb] | 44 | #include <proc/scheduler.h>
|
---|
| 45 | #include <proc/thread.h>
|
---|
| 46 | #include <proc/task.h>
|
---|
[32ff43e6] | 47 | #include <mm/frame.h>
|
---|
| 48 | #include <mm/page.h>
|
---|
[20d50a1] | 49 | #include <mm/as.h>
|
---|
[b3f8fb7] | 50 | #include <time/timeout.h>
|
---|
[fe19611] | 51 | #include <time/delay.h>
|
---|
[32ff43e6] | 52 | #include <arch/asm.h>
|
---|
[cce6acf] | 53 | #include <arch/cycle.h>
|
---|
[23684b7] | 54 | #include <atomic.h>
|
---|
[32ff43e6] | 55 | #include <synch/spinlock.h>
|
---|
[f761f1eb] | 56 | #include <config.h>
|
---|
| 57 | #include <context.h>
|
---|
[b3f8fb7] | 58 | #include <fpu_context.h>
|
---|
[b2e121a] | 59 | #include <halt.h>
|
---|
[f761f1eb] | 60 | #include <arch.h>
|
---|
[5c9a08b] | 61 | #include <adt/list.h>
|
---|
[02a99d2] | 62 | #include <panic.h>
|
---|
[32ff43e6] | 63 | #include <cpu.h>
|
---|
[bab75df6] | 64 | #include <stdio.h>
|
---|
[b2fa1204] | 65 | #include <log.h>
|
---|
[df58e44] | 66 | #include <stacktrace.h>
|
---|
[9c0a9b3] | 67 |
|
---|
[31e15be] | 68 | atomic_size_t nrdy; /**< Number of ready threads in the system. */
|
---|
[f761f1eb] | 69 |
|
---|
[5f85c91] | 70 | #ifdef CONFIG_FPU_LAZY
|
---|
[b49f4ae] | 71 | void scheduler_fpu_lazy_request(void)
|
---|
| 72 | {
|
---|
| 73 | fpu_enable();
|
---|
[f3dbe27] | 74 |
|
---|
| 75 | /* We need this lock to ensure synchronization with thread destructor. */
|
---|
[169815e] | 76 | irq_spinlock_lock(&CPU->fpu_lock, false);
|
---|
[a35b458] | 77 |
|
---|
[a3eeceb6] | 78 | /* Save old context */
|
---|
[f3dbe27] | 79 | thread_t *owner = atomic_load_explicit(&CPU->fpu_owner, memory_order_relaxed);
|
---|
| 80 | if (owner != NULL) {
|
---|
| 81 | fpu_context_save(&owner->fpu_context);
|
---|
| 82 | atomic_store_explicit(&CPU->fpu_owner, NULL, memory_order_relaxed);
|
---|
[b49f4ae] | 83 | }
|
---|
[a35b458] | 84 |
|
---|
[f3dbe27] | 85 | irq_spinlock_unlock(&CPU->fpu_lock, false);
|
---|
| 86 |
|
---|
[7d6ec87] | 87 | if (THREAD->fpu_context_exists) {
|
---|
[0366d09d] | 88 | fpu_context_restore(&THREAD->fpu_context);
|
---|
[7d6ec87] | 89 | } else {
|
---|
[f76fed4] | 90 | fpu_init();
|
---|
[6eef3c4] | 91 | THREAD->fpu_context_exists = true;
|
---|
[b49f4ae] | 92 | }
|
---|
[a35b458] | 93 |
|
---|
[f3dbe27] | 94 | atomic_store_explicit(&CPU->fpu_owner, THREAD, memory_order_relaxed);
|
---|
[b49f4ae] | 95 | }
|
---|
[da1bafb] | 96 | #endif /* CONFIG_FPU_LAZY */
|
---|
[0ca6faa] | 97 |
|
---|
[70527f1] | 98 | /** Initialize scheduler
|
---|
| 99 | *
|
---|
| 100 | * Initialize kernel scheduler.
|
---|
| 101 | *
|
---|
| 102 | */
|
---|
[f761f1eb] | 103 | void scheduler_init(void)
|
---|
| 104 | {
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[70527f1] | 107 | /** Get thread to be scheduled
|
---|
| 108 | *
|
---|
| 109 | * Get the optimal thread to be scheduled
|
---|
[d1a184f] | 110 | * according to thread accounting and scheduler
|
---|
[70527f1] | 111 | * policy.
|
---|
| 112 | *
|
---|
| 113 | * @return Thread to be scheduled.
|
---|
| 114 | *
|
---|
| 115 | */
|
---|
[ec8ef12] | 116 | static thread_t *try_find_thread(int *rq_index)
|
---|
[f761f1eb] | 117 | {
|
---|
[ec8ef12] | 118 | assert(interrupts_disabled());
|
---|
[63e27ef] | 119 | assert(CPU != NULL);
|
---|
[a35b458] | 120 |
|
---|
[ec8ef12] | 121 | if (atomic_load(&CPU->nrdy) == 0)
|
---|
| 122 | return NULL;
|
---|
[a35b458] | 123 |
|
---|
[ec8ef12] | 124 | for (int i = 0; i < RQ_COUNT; i++) {
|
---|
[da1bafb] | 125 | irq_spinlock_lock(&(CPU->rq[i].lock), false);
|
---|
| 126 | if (CPU->rq[i].n == 0) {
|
---|
[f761f1eb] | 127 | /*
|
---|
| 128 | * If this queue is empty, try a lower-priority queue.
|
---|
| 129 | */
|
---|
[da1bafb] | 130 | irq_spinlock_unlock(&(CPU->rq[i].lock), false);
|
---|
[f761f1eb] | 131 | continue;
|
---|
| 132 | }
|
---|
[a35b458] | 133 |
|
---|
[248fc1a] | 134 | atomic_dec(&CPU->nrdy);
|
---|
[59e07c91] | 135 | atomic_dec(&nrdy);
|
---|
[da1bafb] | 136 | CPU->rq[i].n--;
|
---|
[a35b458] | 137 |
|
---|
[f761f1eb] | 138 | /*
|
---|
| 139 | * Take the first thread from the queue.
|
---|
| 140 | */
|
---|
[55b77d9] | 141 | thread_t *thread = list_get_instance(
|
---|
| 142 | list_first(&CPU->rq[i].rq), thread_t, rq_link);
|
---|
[da1bafb] | 143 | list_remove(&thread->rq_link);
|
---|
[a35b458] | 144 |
|
---|
[8996582] | 145 | irq_spinlock_unlock(&(CPU->rq[i].lock), false);
|
---|
[a35b458] | 146 |
|
---|
[117ad5a2] | 147 | *rq_index = i;
|
---|
[da1bafb] | 148 | return thread;
|
---|
[f761f1eb] | 149 | }
|
---|
[a35b458] | 150 |
|
---|
[ec8ef12] | 151 | return NULL;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Get thread to be scheduled
|
---|
| 155 | *
|
---|
| 156 | * Get the optimal thread to be scheduled
|
---|
| 157 | * according to thread accounting and scheduler
|
---|
| 158 | * policy.
|
---|
| 159 | *
|
---|
| 160 | * @return Thread to be scheduled.
|
---|
| 161 | *
|
---|
| 162 | */
|
---|
| 163 | static thread_t *find_best_thread(int *rq_index)
|
---|
| 164 | {
|
---|
| 165 | assert(interrupts_disabled());
|
---|
| 166 | assert(CPU != NULL);
|
---|
| 167 |
|
---|
| 168 | while (true) {
|
---|
| 169 | thread_t *thread = try_find_thread(rq_index);
|
---|
| 170 |
|
---|
| 171 | if (thread != NULL)
|
---|
| 172 | return thread;
|
---|
| 173 |
|
---|
| 174 | /*
|
---|
| 175 | * For there was nothing to run, the CPU goes to sleep
|
---|
| 176 | * until a hardware interrupt or an IPI comes.
|
---|
| 177 | * This improves energy saving and hyperthreading.
|
---|
| 178 | */
|
---|
[4760793] | 179 | CPU_LOCAL->idle = true;
|
---|
[ec8ef12] | 180 |
|
---|
| 181 | /*
|
---|
| 182 | * Go to sleep with interrupts enabled.
|
---|
| 183 | * Ideally, this should be atomic, but this is not guaranteed on
|
---|
| 184 | * all platforms yet, so it is possible we will go sleep when
|
---|
| 185 | * a thread has just become available.
|
---|
| 186 | */
|
---|
| 187 | cpu_interruptible_sleep();
|
---|
| 188 | }
|
---|
[f761f1eb] | 189 | }
|
---|
| 190 |
|
---|
[c680333] | 191 | static void switch_task(task_t *task)
|
---|
| 192 | {
|
---|
| 193 | /* If the task stays the same, a lot of work is avoided. */
|
---|
| 194 | if (TASK == task)
|
---|
| 195 | return;
|
---|
| 196 |
|
---|
| 197 | as_t *old_as = AS;
|
---|
| 198 | as_t *new_as = task->as;
|
---|
| 199 |
|
---|
| 200 | /* It is possible for two tasks to share one address space. */
|
---|
| 201 | if (old_as != new_as)
|
---|
| 202 | as_switch(old_as, new_as);
|
---|
| 203 |
|
---|
| 204 | if (TASK)
|
---|
| 205 | task_release(TASK);
|
---|
| 206 |
|
---|
| 207 | TASK = task;
|
---|
| 208 |
|
---|
| 209 | task_hold(TASK);
|
---|
| 210 |
|
---|
| 211 | before_task_runs_arch();
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[70527f1] | 214 | /** Prevent rq starvation
|
---|
| 215 | *
|
---|
| 216 | * Prevent low priority threads from starving in rq's.
|
---|
| 217 | *
|
---|
| 218 | * When the function decides to relink rq's, it reconnects
|
---|
| 219 | * respective pointers so that in result threads with 'pri'
|
---|
[abbc16e] | 220 | * greater or equal start are moved to a higher-priority queue.
|
---|
[70527f1] | 221 | *
|
---|
| 222 | * @param start Threshold priority.
|
---|
| 223 | *
|
---|
[f761f1eb] | 224 | */
|
---|
[e16e036a] | 225 | static void relink_rq(int start)
|
---|
[f761f1eb] | 226 | {
|
---|
[25939997] | 227 | assert(interrupts_disabled());
|
---|
| 228 |
|
---|
[4760793] | 229 | if (CPU_LOCAL->current_clock_tick < CPU_LOCAL->relink_deadline)
|
---|
[011c79a] | 230 | return;
|
---|
| 231 |
|
---|
[4760793] | 232 | CPU_LOCAL->relink_deadline = CPU_LOCAL->current_clock_tick + NEEDS_RELINK_MAX;
|
---|
[a35b458] | 233 |
|
---|
[3118355] | 234 | /* Temporary cache for lists we are moving. */
|
---|
[011c79a] | 235 | list_t list;
|
---|
[55b77d9] | 236 | list_initialize(&list);
|
---|
[a35b458] | 237 |
|
---|
[3118355] | 238 | size_t n = 0;
|
---|
| 239 |
|
---|
| 240 | /* Move every list (except the one with highest priority) one level up. */
|
---|
| 241 | for (int i = RQ_COUNT - 1; i > start; i--) {
|
---|
| 242 | irq_spinlock_lock(&CPU->rq[i].lock, false);
|
---|
[a35b458] | 243 |
|
---|
[3118355] | 244 | /* Swap lists. */
|
---|
| 245 | list_swap(&CPU->rq[i].rq, &list);
|
---|
[a35b458] | 246 |
|
---|
[3118355] | 247 | /* Swap number of items. */
|
---|
| 248 | size_t tmpn = CPU->rq[i].n;
|
---|
| 249 | CPU->rq[i].n = n;
|
---|
| 250 | n = tmpn;
|
---|
[a35b458] | 251 |
|
---|
[011c79a] | 252 | irq_spinlock_unlock(&CPU->rq[i].lock, false);
|
---|
[f761f1eb] | 253 | }
|
---|
[a35b458] | 254 |
|
---|
[3118355] | 255 | /* Append the contents of rq[start + 1] to rq[start]. */
|
---|
| 256 | if (n != 0) {
|
---|
| 257 | irq_spinlock_lock(&CPU->rq[start].lock, false);
|
---|
| 258 | list_concat(&CPU->rq[start].rq, &list);
|
---|
| 259 | CPU->rq[start].n += n;
|
---|
| 260 | irq_spinlock_unlock(&CPU->rq[start].lock, false);
|
---|
| 261 | }
|
---|
[f761f1eb] | 262 | }
|
---|
| 263 |
|
---|
[23f36a3] | 264 | /**
|
---|
| 265 | * Do whatever needs to be done with current FPU state before we switch to
|
---|
| 266 | * another thread.
|
---|
| 267 | */
|
---|
| 268 | static void fpu_cleanup(void)
|
---|
| 269 | {
|
---|
| 270 | #if (defined CONFIG_FPU) && (!defined CONFIG_FPU_LAZY)
|
---|
| 271 | fpu_context_save(&THREAD->fpu_context);
|
---|
| 272 | #endif
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /**
|
---|
| 276 | * Set correct FPU state for this thread after switch from another thread.
|
---|
| 277 | */
|
---|
| 278 | static void fpu_restore(void)
|
---|
| 279 | {
|
---|
| 280 | #ifdef CONFIG_FPU_LAZY
|
---|
| 281 | /*
|
---|
| 282 | * The only concurrent modification possible for fpu_owner here is
|
---|
| 283 | * another thread changing it from itself to NULL in its destructor.
|
---|
| 284 | */
|
---|
| 285 | thread_t *owner = atomic_load_explicit(&CPU->fpu_owner,
|
---|
| 286 | memory_order_relaxed);
|
---|
| 287 |
|
---|
| 288 | if (THREAD == owner)
|
---|
| 289 | fpu_enable();
|
---|
| 290 | else
|
---|
| 291 | fpu_disable();
|
---|
| 292 |
|
---|
| 293 | #elif defined CONFIG_FPU
|
---|
| 294 | fpu_enable();
|
---|
| 295 | if (THREAD->fpu_context_exists)
|
---|
| 296 | fpu_context_restore(&THREAD->fpu_context);
|
---|
| 297 | else {
|
---|
| 298 | fpu_init();
|
---|
| 299 | THREAD->fpu_context_exists = true;
|
---|
| 300 | }
|
---|
| 301 | #endif
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[8996582] | 304 | /** Things to do before we switch to THREAD context.
|
---|
| 305 | */
|
---|
| 306 | static void prepare_to_run_thread(int rq_index)
|
---|
| 307 | {
|
---|
| 308 | relink_rq(rq_index);
|
---|
| 309 |
|
---|
| 310 | switch_task(THREAD->task);
|
---|
| 311 |
|
---|
| 312 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
| 313 | THREAD->state = Running;
|
---|
| 314 | THREAD->cpu = CPU;
|
---|
| 315 | THREAD->priority = rq_index; /* Correct rq index */
|
---|
| 316 |
|
---|
| 317 | /*
|
---|
| 318 | * Clear the stolen flag so that it can be migrated
|
---|
| 319 | * when load balancing needs emerge.
|
---|
| 320 | */
|
---|
| 321 | THREAD->stolen = false;
|
---|
| 322 |
|
---|
| 323 | #ifdef SCHEDULER_VERBOSE
|
---|
| 324 | log(LF_OTHER, LVL_DEBUG,
|
---|
| 325 | "cpu%u: tid %" PRIu64 " (priority=%d, ticks=%" PRIu64
|
---|
| 326 | ", nrdy=%zu)", CPU->id, THREAD->tid, THREAD->priority,
|
---|
| 327 | THREAD->ticks, atomic_load(&CPU->nrdy));
|
---|
| 328 | #endif
|
---|
| 329 |
|
---|
| 330 | /*
|
---|
| 331 | * Some architectures provide late kernel PA2KA(identity)
|
---|
| 332 | * mapping in a page fault handler. However, the page fault
|
---|
| 333 | * handler uses the kernel stack of the running thread and
|
---|
| 334 | * therefore cannot be used to map it. The kernel stack, if
|
---|
| 335 | * necessary, is to be mapped in before_thread_runs(). This
|
---|
| 336 | * function must be executed before the switch to the new stack.
|
---|
| 337 | */
|
---|
| 338 | before_thread_runs_arch();
|
---|
| 339 |
|
---|
| 340 | #ifdef CONFIG_UDEBUG
|
---|
| 341 | if (THREAD->btrace) {
|
---|
| 342 | istate_t *istate = THREAD->udebug.uspace_state;
|
---|
| 343 | if (istate != NULL) {
|
---|
| 344 | printf("Thread %" PRIu64 " stack trace:\n", THREAD->tid);
|
---|
| 345 | stack_trace_istate(istate);
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | THREAD->btrace = false;
|
---|
| 349 | }
|
---|
| 350 | #endif
|
---|
| 351 |
|
---|
| 352 | fpu_restore();
|
---|
| 353 |
|
---|
| 354 | /* Time allocation in microseconds. */
|
---|
| 355 | uint64_t time_to_run = (rq_index + 1) * 10000;
|
---|
| 356 |
|
---|
| 357 | /* Set the time of next preemption. */
|
---|
| 358 | CPU_LOCAL->preempt_deadline =
|
---|
| 359 | CPU_LOCAL->current_clock_tick + us2ticks(time_to_run);
|
---|
| 360 |
|
---|
| 361 | /* Save current CPU cycle */
|
---|
| 362 | THREAD->last_cycle = get_cycle();
|
---|
[c1eaec4] | 363 |
|
---|
| 364 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[8996582] | 365 | }
|
---|
| 366 |
|
---|
[6e49dab] | 367 | static void cleanup_after_thread(thread_t *thread, state_t out_state)
|
---|
| 368 | {
|
---|
| 369 | assert(CURRENT->mutex_locks == 0);
|
---|
| 370 | assert(interrupts_disabled());
|
---|
| 371 |
|
---|
| 372 | int expected;
|
---|
| 373 |
|
---|
| 374 | switch (out_state) {
|
---|
| 375 | case Running:
|
---|
| 376 | thread_ready(thread);
|
---|
| 377 | break;
|
---|
| 378 |
|
---|
| 379 | case Exiting:
|
---|
| 380 | waitq_close(&thread->join_wq);
|
---|
| 381 |
|
---|
| 382 | /*
|
---|
| 383 | * Release the reference CPU has for the thread.
|
---|
| 384 | * If there are no other references (e.g. threads calling join),
|
---|
| 385 | * the thread structure is deallocated.
|
---|
| 386 | */
|
---|
| 387 | thread_put(thread);
|
---|
| 388 | break;
|
---|
| 389 |
|
---|
| 390 | case Sleeping:
|
---|
| 391 | expected = SLEEP_INITIAL;
|
---|
| 392 |
|
---|
| 393 | /* Only set SLEEP_ASLEEP in sleep pad if it's still in initial state */
|
---|
| 394 | if (!atomic_compare_exchange_strong_explicit(&thread->sleep_state,
|
---|
| 395 | &expected, SLEEP_ASLEEP,
|
---|
| 396 | memory_order_acq_rel, memory_order_acquire)) {
|
---|
| 397 |
|
---|
| 398 | assert(expected == SLEEP_WOKE);
|
---|
| 399 | /* The thread has already been woken up, requeue immediately. */
|
---|
| 400 | thread_ready(thread);
|
---|
| 401 | }
|
---|
| 402 | break;
|
---|
| 403 |
|
---|
| 404 | default:
|
---|
| 405 | /*
|
---|
| 406 | * Entering state is unexpected.
|
---|
| 407 | */
|
---|
| 408 | panic("tid%" PRIu64 ": unexpected state %s.",
|
---|
| 409 | thread->tid, thread_states[thread->state]);
|
---|
| 410 | break;
|
---|
| 411 | }
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[25939997] | 414 | /** Switch to scheduler context to let other threads run. */
|
---|
[151c050] | 415 | void scheduler_enter(state_t new_state)
|
---|
[7d6ec87] | 416 | {
|
---|
[151c050] | 417 | ipl_t ipl = interrupts_disable();
|
---|
[a35b458] | 418 |
|
---|
[151c050] | 419 | assert(CPU != NULL);
|
---|
| 420 | assert(THREAD != NULL);
|
---|
[23f36a3] | 421 |
|
---|
[151c050] | 422 | fpu_cleanup();
|
---|
[a35b458] | 423 |
|
---|
[25939997] | 424 | if (atomic_load(&haltstate))
|
---|
| 425 | halt();
|
---|
| 426 |
|
---|
[151c050] | 427 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
| 428 | THREAD->state = new_state;
|
---|
[a35b458] | 429 |
|
---|
[151c050] | 430 | /* Update thread kernel accounting */
|
---|
| 431 | THREAD->kcycles += get_cycle() - THREAD->last_cycle;
|
---|
[a35b458] | 432 |
|
---|
[29029ac0] | 433 | /*
|
---|
| 434 | * On Sparc, this saves some extra userspace state that's not
|
---|
| 435 | * covered by context_save()/context_restore().
|
---|
| 436 | */
|
---|
| 437 | after_thread_ran_arch();
|
---|
| 438 |
|
---|
[5861b60] | 439 | if (new_state == Sleeping) {
|
---|
| 440 | /* Prefer the thread after it's woken up. */
|
---|
| 441 | THREAD->priority = -1;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[c1eaec4] | 444 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
| 445 |
|
---|
| 446 | CPU_LOCAL->exiting_state = new_state;
|
---|
[ed7e057] | 447 |
|
---|
[25939997] | 448 | current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
|
---|
| 449 | context_swap(&THREAD->saved_context, &CPU_LOCAL->scheduler_context);
|
---|
[ed7e057] | 450 |
|
---|
[c1eaec4] | 451 | assert(CURRENT->mutex_locks == 0);
|
---|
| 452 | assert(interrupts_disabled());
|
---|
| 453 |
|
---|
[ed7e057] | 454 | interrupts_restore(ipl);
|
---|
[7d6ec87] | 455 | }
|
---|
[70527f1] | 456 |
|
---|
[25939997] | 457 | /** Enter main scheduler loop. Never returns.
|
---|
[70527f1] | 458 | *
|
---|
[25939997] | 459 | * This function switches to a runnable thread as soon as one is available,
|
---|
| 460 | * after which it is only switched back to if a thread is stopping and there is
|
---|
| 461 | * no other thread to run in its place. We need a separate context for that
|
---|
| 462 | * because we're going to block the CPU, which means we need another context
|
---|
| 463 | * to clean up after the previous thread.
|
---|
[70527f1] | 464 | */
|
---|
[25939997] | 465 | void scheduler_run(void)
|
---|
[f761f1eb] | 466 | {
|
---|
[25939997] | 467 | assert(interrupts_disabled());
|
---|
| 468 |
|
---|
[63e27ef] | 469 | assert(CPU != NULL);
|
---|
[25939997] | 470 | assert(TASK == NULL);
|
---|
| 471 | assert(THREAD == NULL);
|
---|
[63e27ef] | 472 | assert(interrupts_disabled());
|
---|
[a35b458] | 473 |
|
---|
[25939997] | 474 | while (!atomic_load(&haltstate)) {
|
---|
| 475 | assert(CURRENT->mutex_locks == 0);
|
---|
| 476 |
|
---|
| 477 | int rq_index;
|
---|
| 478 | THREAD = find_best_thread(&rq_index);
|
---|
| 479 | prepare_to_run_thread(rq_index);
|
---|
| 480 |
|
---|
| 481 | /*
|
---|
| 482 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
|
---|
| 483 | * thread's stack.
|
---|
| 484 | */
|
---|
| 485 | current_copy(CURRENT, (current_t *) THREAD->kstack);
|
---|
| 486 |
|
---|
| 487 | /* Switch to thread context. */
|
---|
| 488 | context_swap(&CPU_LOCAL->scheduler_context, &THREAD->saved_context);
|
---|
| 489 |
|
---|
[c1eaec4] | 490 | /* Back from another thread. */
|
---|
[25939997] | 491 | assert(CPU != NULL);
|
---|
| 492 | assert(THREAD != NULL);
|
---|
[c1eaec4] | 493 | assert(CURRENT->mutex_locks == 0);
|
---|
[25939997] | 494 | assert(interrupts_disabled());
|
---|
[151c050] | 495 |
|
---|
[c1eaec4] | 496 | cleanup_after_thread(THREAD, CPU_LOCAL->exiting_state);
|
---|
[a35b458] | 497 |
|
---|
[25939997] | 498 | /*
|
---|
| 499 | * Necessary because we're allowing interrupts in find_best_thread(),
|
---|
| 500 | * so we need to avoid other code referencing the thread we left.
|
---|
| 501 | */
|
---|
[43114c5] | 502 | THREAD = NULL;
|
---|
[f761f1eb] | 503 | }
|
---|
[a35b458] | 504 |
|
---|
[25939997] | 505 | halt();
|
---|
[f761f1eb] | 506 | }
|
---|
| 507 |
|
---|
[5f85c91] | 508 | #ifdef CONFIG_SMP
|
---|
[fbaf6ac] | 509 |
|
---|
| 510 | static thread_t *steal_thread_from(cpu_t *old_cpu, int i)
|
---|
| 511 | {
|
---|
| 512 | runq_t *old_rq = &old_cpu->rq[i];
|
---|
| 513 | runq_t *new_rq = &CPU->rq[i];
|
---|
| 514 |
|
---|
[06f81c4] | 515 | ipl_t ipl = interrupts_disable();
|
---|
| 516 |
|
---|
| 517 | irq_spinlock_lock(&old_rq->lock, false);
|
---|
[fbaf6ac] | 518 |
|
---|
[f3dbe27] | 519 | /*
|
---|
| 520 | * If fpu_owner is any thread in the list, its store is seen here thanks to
|
---|
| 521 | * the runqueue lock.
|
---|
| 522 | */
|
---|
| 523 | thread_t *fpu_owner = atomic_load_explicit(&old_cpu->fpu_owner,
|
---|
| 524 | memory_order_relaxed);
|
---|
| 525 |
|
---|
[fbaf6ac] | 526 | /* Search rq from the back */
|
---|
| 527 | list_foreach_rev(old_rq->rq, rq_link, thread_t, thread) {
|
---|
| 528 |
|
---|
| 529 | irq_spinlock_lock(&thread->lock, false);
|
---|
| 530 |
|
---|
| 531 | /*
|
---|
| 532 | * Do not steal CPU-wired threads, threads
|
---|
| 533 | * already stolen, threads for which migration
|
---|
| 534 | * was temporarily disabled or threads whose
|
---|
| 535 | * FPU context is still in the CPU.
|
---|
| 536 | */
|
---|
[06f81c4] | 537 | if (thread->stolen || thread->nomigrate ||
|
---|
[f3dbe27] | 538 | thread == fpu_owner) {
|
---|
[fbaf6ac] | 539 | irq_spinlock_unlock(&thread->lock, false);
|
---|
| 540 | continue;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | thread->stolen = true;
|
---|
| 544 | thread->cpu = CPU;
|
---|
| 545 |
|
---|
| 546 | irq_spinlock_unlock(&thread->lock, false);
|
---|
| 547 |
|
---|
| 548 | /*
|
---|
| 549 | * Ready thread on local CPU
|
---|
| 550 | */
|
---|
| 551 |
|
---|
| 552 | #ifdef KCPULB_VERBOSE
|
---|
| 553 | log(LF_OTHER, LVL_DEBUG,
|
---|
| 554 | "kcpulb%u: TID %" PRIu64 " -> cpu%u, "
|
---|
| 555 | "nrdy=%ld, avg=%ld", CPU->id, thread->tid,
|
---|
| 556 | CPU->id, atomic_load(&CPU->nrdy),
|
---|
| 557 | atomic_load(&nrdy) / config.cpu_active);
|
---|
| 558 | #endif
|
---|
| 559 |
|
---|
| 560 | /* Remove thread from ready queue. */
|
---|
| 561 | old_rq->n--;
|
---|
| 562 | list_remove(&thread->rq_link);
|
---|
[06f81c4] | 563 | irq_spinlock_unlock(&old_rq->lock, false);
|
---|
[fbaf6ac] | 564 |
|
---|
| 565 | /* Append thread to local queue. */
|
---|
[06f81c4] | 566 | irq_spinlock_lock(&new_rq->lock, false);
|
---|
[fbaf6ac] | 567 | list_append(&thread->rq_link, &new_rq->rq);
|
---|
| 568 | new_rq->n++;
|
---|
[06f81c4] | 569 | irq_spinlock_unlock(&new_rq->lock, false);
|
---|
[fbaf6ac] | 570 |
|
---|
| 571 | atomic_dec(&old_cpu->nrdy);
|
---|
| 572 | atomic_inc(&CPU->nrdy);
|
---|
[06f81c4] | 573 | interrupts_restore(ipl);
|
---|
[fbaf6ac] | 574 | return thread;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
[06f81c4] | 577 | irq_spinlock_unlock(&old_rq->lock, false);
|
---|
| 578 | interrupts_restore(ipl);
|
---|
[fbaf6ac] | 579 | return NULL;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[70527f1] | 582 | /** Load balancing thread
|
---|
| 583 | *
|
---|
| 584 | * SMP load balancing thread, supervising thread supplies
|
---|
| 585 | * for the CPU it's wired to.
|
---|
| 586 | *
|
---|
| 587 | * @param arg Generic thread argument (unused).
|
---|
| 588 | *
|
---|
[f761f1eb] | 589 | */
|
---|
| 590 | void kcpulb(void *arg)
|
---|
| 591 | {
|
---|
[3cfe2b8] | 592 | size_t average;
|
---|
| 593 | size_t rdy;
|
---|
[a35b458] | 594 |
|
---|
[f761f1eb] | 595 | loop:
|
---|
| 596 | /*
|
---|
[3260ada] | 597 | * Work in 1s intervals.
|
---|
[f761f1eb] | 598 | */
|
---|
[3260ada] | 599 | thread_sleep(1);
|
---|
[a35b458] | 600 |
|
---|
[f761f1eb] | 601 | not_satisfied:
|
---|
| 602 | /*
|
---|
| 603 | * Calculate the number of threads that will be migrated/stolen from
|
---|
| 604 | * other CPU's. Note that situation can have changed between two
|
---|
| 605 | * passes. Each time get the most up to date counts.
|
---|
[da1bafb] | 606 | *
|
---|
[f761f1eb] | 607 | */
|
---|
[036e97c] | 608 | average = atomic_load(&nrdy) / config.cpu_active + 1;
|
---|
| 609 | rdy = atomic_load(&CPU->nrdy);
|
---|
[a35b458] | 610 |
|
---|
[da1bafb] | 611 | if (average <= rdy)
|
---|
[f761f1eb] | 612 | goto satisfied;
|
---|
[a35b458] | 613 |
|
---|
[3cfe2b8] | 614 | size_t count = average - rdy;
|
---|
[a35b458] | 615 |
|
---|
[f761f1eb] | 616 | /*
|
---|
[4e33b6b] | 617 | * Searching least priority queues on all CPU's first and most priority
|
---|
| 618 | * queues on all CPU's last.
|
---|
[f761f1eb] | 619 | */
|
---|
[da1bafb] | 620 | size_t acpu;
|
---|
| 621 | int rq;
|
---|
[a35b458] | 622 |
|
---|
[da1bafb] | 623 | for (rq = RQ_COUNT - 1; rq >= 0; rq--) {
|
---|
| 624 | for (acpu = 0; acpu < config.cpu_active; acpu++) {
|
---|
[fbaf6ac] | 625 | cpu_t *cpu = &cpus[acpu];
|
---|
[a35b458] | 626 |
|
---|
[f761f1eb] | 627 | /*
|
---|
| 628 | * Not interested in ourselves.
|
---|
[4e33b6b] | 629 | * Doesn't require interrupt disabling for kcpulb has
|
---|
| 630 | * THREAD_FLAG_WIRED.
|
---|
[da1bafb] | 631 | *
|
---|
[f761f1eb] | 632 | */
|
---|
[43114c5] | 633 | if (CPU == cpu)
|
---|
[248fc1a] | 634 | continue;
|
---|
[a35b458] | 635 |
|
---|
[036e97c] | 636 | if (atomic_load(&cpu->nrdy) <= average)
|
---|
[248fc1a] | 637 | continue;
|
---|
[a35b458] | 638 |
|
---|
[fbaf6ac] | 639 | if (steal_thread_from(cpu, rq) && --count == 0)
|
---|
| 640 | goto satisfied;
|
---|
[f761f1eb] | 641 | }
|
---|
| 642 | }
|
---|
[a35b458] | 643 |
|
---|
[036e97c] | 644 | if (atomic_load(&CPU->nrdy)) {
|
---|
[f761f1eb] | 645 | /*
|
---|
| 646 | * Be a little bit light-weight and let migrated threads run.
|
---|
[da1bafb] | 647 | *
|
---|
[f761f1eb] | 648 | */
|
---|
[151c050] | 649 | thread_yield();
|
---|
[3260ada] | 650 | } else {
|
---|
[f761f1eb] | 651 | /*
|
---|
| 652 | * We failed to migrate a single thread.
|
---|
[3260ada] | 653 | * Give up this turn.
|
---|
[da1bafb] | 654 | *
|
---|
[f761f1eb] | 655 | */
|
---|
[3260ada] | 656 | goto loop;
|
---|
[f761f1eb] | 657 | }
|
---|
[a35b458] | 658 |
|
---|
[f761f1eb] | 659 | goto not_satisfied;
|
---|
[a35b458] | 660 |
|
---|
[f761f1eb] | 661 | satisfied:
|
---|
| 662 | goto loop;
|
---|
| 663 | }
|
---|
[5f85c91] | 664 | #endif /* CONFIG_SMP */
|
---|
[10e16a7] | 665 |
|
---|
[da1bafb] | 666 | /** Print information about threads & scheduler queues
|
---|
| 667 | *
|
---|
| 668 | */
|
---|
[10e16a7] | 669 | void sched_print_list(void)
|
---|
| 670 | {
|
---|
[da1bafb] | 671 | size_t cpu;
|
---|
[4184e76] | 672 | for (cpu = 0; cpu < config.cpu_count; cpu++) {
|
---|
[10e16a7] | 673 | if (!cpus[cpu].active)
|
---|
| 674 | continue;
|
---|
[a35b458] | 675 |
|
---|
[3b68542] | 676 | printf("cpu%u: address=%p, nrdy=%zu\n",
|
---|
| 677 | cpus[cpu].id, &cpus[cpu], atomic_load(&cpus[cpu].nrdy));
|
---|
[a35b458] | 678 |
|
---|
[da1bafb] | 679 | unsigned int i;
|
---|
[4e33b6b] | 680 | for (i = 0; i < RQ_COUNT; i++) {
|
---|
[da1bafb] | 681 | irq_spinlock_lock(&(cpus[cpu].rq[i].lock), false);
|
---|
| 682 | if (cpus[cpu].rq[i].n == 0) {
|
---|
| 683 | irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
|
---|
[10e16a7] | 684 | continue;
|
---|
| 685 | }
|
---|
[a35b458] | 686 |
|
---|
[5b86d10] | 687 | printf("\trq[%u]: ", i);
|
---|
[feeac0d] | 688 | list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
|
---|
| 689 | thread) {
|
---|
[da1bafb] | 690 | printf("%" PRIu64 "(%s) ", thread->tid,
|
---|
| 691 | thread_states[thread->state]);
|
---|
[10e16a7] | 692 | }
|
---|
| 693 | printf("\n");
|
---|
[a35b458] | 694 |
|
---|
[da1bafb] | 695 | irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
|
---|
[10e16a7] | 696 | }
|
---|
| 697 | }
|
---|
| 698 | }
|
---|
[b45c443] | 699 |
|
---|
[cc73a8a1] | 700 | /** @}
|
---|
[b45c443] | 701 | */
|
---|