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