[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 |
|
---|
[cc73a8a1] | 29 | /** @addtogroup genericproc
|
---|
[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 |
|
---|
[f761f1eb] | 41 | #include <proc/scheduler.h>
|
---|
| 42 | #include <proc/thread.h>
|
---|
| 43 | #include <proc/task.h>
|
---|
[32ff43e6] | 44 | #include <mm/frame.h>
|
---|
| 45 | #include <mm/page.h>
|
---|
[20d50a1] | 46 | #include <mm/as.h>
|
---|
[b3f8fb7] | 47 | #include <time/timeout.h>
|
---|
[fe19611] | 48 | #include <time/delay.h>
|
---|
[32ff43e6] | 49 | #include <arch/asm.h>
|
---|
| 50 | #include <arch/faddr.h>
|
---|
[cce6acf] | 51 | #include <arch/cycle.h>
|
---|
[23684b7] | 52 | #include <atomic.h>
|
---|
[32ff43e6] | 53 | #include <synch/spinlock.h>
|
---|
[f761f1eb] | 54 | #include <config.h>
|
---|
| 55 | #include <context.h>
|
---|
[b3f8fb7] | 56 | #include <fpu_context.h>
|
---|
[f761f1eb] | 57 | #include <func.h>
|
---|
| 58 | #include <arch.h>
|
---|
[5c9a08b] | 59 | #include <adt/list.h>
|
---|
[02a99d2] | 60 | #include <panic.h>
|
---|
[32ff43e6] | 61 | #include <cpu.h>
|
---|
[9c0a9b3] | 62 | #include <print.h>
|
---|
[623ba26c] | 63 | #include <debug.h>
|
---|
[df58e44] | 64 | #include <stacktrace.h>
|
---|
[9c0a9b3] | 65 |
|
---|
[7d6ec87] | 66 | static void scheduler_separated_stack(void);
|
---|
| 67 |
|
---|
[da1bafb] | 68 | atomic_t nrdy; /**< Number of ready threads in the system. */
|
---|
[f761f1eb] | 69 |
|
---|
[39cea6a] | 70 | /** Carry out actions before new task runs. */
|
---|
[4e7d3dd] | 71 | static void before_task_runs(void)
|
---|
[39cea6a] | 72 | {
|
---|
| 73 | before_task_runs_arch();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[97f1691] | 76 | /** Take actions before new thread runs.
|
---|
[70527f1] | 77 | *
|
---|
[b60a22c] | 78 | * Perform actions that need to be
|
---|
| 79 | * taken before the newly selected
|
---|
[df58e44] | 80 | * thread is passed control.
|
---|
[70527f1] | 81 | *
|
---|
[a3eeceb6] | 82 | * THREAD->lock is locked on entry
|
---|
| 83 | *
|
---|
[70527f1] | 84 | */
|
---|
[4e7d3dd] | 85 | static void before_thread_runs(void)
|
---|
[0ca6faa] | 86 | {
|
---|
[b49f4ae] | 87 | before_thread_runs_arch();
|
---|
[4e7d3dd] | 88 |
|
---|
[f76fed4] | 89 | #ifdef CONFIG_FPU_LAZY
|
---|
[df58e44] | 90 | if (THREAD == CPU->fpu_owner)
|
---|
[b49f4ae] | 91 | fpu_enable();
|
---|
| 92 | else
|
---|
[da1bafb] | 93 | fpu_disable();
|
---|
[e1326cf] | 94 | #elif defined CONFIG_FPU
|
---|
[b49f4ae] | 95 | fpu_enable();
|
---|
| 96 | if (THREAD->fpu_context_exists)
|
---|
[f76fed4] | 97 | fpu_context_restore(THREAD->saved_fpu_context);
|
---|
[b49f4ae] | 98 | else {
|
---|
[f76fed4] | 99 | fpu_init();
|
---|
[6eef3c4] | 100 | THREAD->fpu_context_exists = true;
|
---|
[b49f4ae] | 101 | }
|
---|
[f76fed4] | 102 | #endif
|
---|
[df58e44] | 103 |
|
---|
[5b7a107] | 104 | #ifdef CONFIG_UDEBUG
|
---|
[df58e44] | 105 | if (THREAD->btrace) {
|
---|
| 106 | istate_t *istate = THREAD->udebug.uspace_state;
|
---|
| 107 | if (istate != NULL) {
|
---|
| 108 | printf("Thread %" PRIu64 " stack trace:\n", THREAD->tid);
|
---|
| 109 | stack_trace_istate(istate);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | THREAD->btrace = false;
|
---|
| 113 | }
|
---|
[5b7a107] | 114 | #endif
|
---|
[0ca6faa] | 115 | }
|
---|
| 116 |
|
---|
[7d6ec87] | 117 | /** Take actions after THREAD had run.
|
---|
[97f1691] | 118 | *
|
---|
| 119 | * Perform actions that need to be
|
---|
| 120 | * taken after the running thread
|
---|
[7d6ec87] | 121 | * had been preempted by the scheduler.
|
---|
[97f1691] | 122 | *
|
---|
| 123 | * THREAD->lock is locked on entry
|
---|
| 124 | *
|
---|
| 125 | */
|
---|
[4e7d3dd] | 126 | static void after_thread_ran(void)
|
---|
[97f1691] | 127 | {
|
---|
| 128 | after_thread_ran_arch();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[5f85c91] | 131 | #ifdef CONFIG_FPU_LAZY
|
---|
[b49f4ae] | 132 | void scheduler_fpu_lazy_request(void)
|
---|
| 133 | {
|
---|
[09c18f7] | 134 | restart:
|
---|
[b49f4ae] | 135 | fpu_enable();
|
---|
[da1bafb] | 136 | irq_spinlock_lock(&CPU->lock, false);
|
---|
| 137 |
|
---|
[a3eeceb6] | 138 | /* Save old context */
|
---|
[da1bafb] | 139 | if (CPU->fpu_owner != NULL) {
|
---|
| 140 | irq_spinlock_lock(&CPU->fpu_owner->lock, false);
|
---|
[f76fed4] | 141 | fpu_context_save(CPU->fpu_owner->saved_fpu_context);
|
---|
[da1bafb] | 142 |
|
---|
| 143 | /* Don't prevent migration */
|
---|
[6eef3c4] | 144 | CPU->fpu_owner->fpu_context_engaged = false;
|
---|
[da1bafb] | 145 | irq_spinlock_unlock(&CPU->fpu_owner->lock, false);
|
---|
[09c18f7] | 146 | CPU->fpu_owner = NULL;
|
---|
[b49f4ae] | 147 | }
|
---|
[da1bafb] | 148 |
|
---|
| 149 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[7d6ec87] | 150 | if (THREAD->fpu_context_exists) {
|
---|
[f76fed4] | 151 | fpu_context_restore(THREAD->saved_fpu_context);
|
---|
[7d6ec87] | 152 | } else {
|
---|
[f76fed4] | 153 | /* Allocate FPU context */
|
---|
| 154 | if (!THREAD->saved_fpu_context) {
|
---|
| 155 | /* Might sleep */
|
---|
[da1bafb] | 156 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
| 157 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
[4e33b6b] | 158 | THREAD->saved_fpu_context =
|
---|
[4184e76] | 159 | (fpu_context_t *) slab_alloc(fpu_context_slab, 0);
|
---|
[da1bafb] | 160 |
|
---|
[09c18f7] | 161 | /* We may have switched CPUs during slab_alloc */
|
---|
[da1bafb] | 162 | goto restart;
|
---|
[f76fed4] | 163 | }
|
---|
| 164 | fpu_init();
|
---|
[6eef3c4] | 165 | THREAD->fpu_context_exists = true;
|
---|
[b49f4ae] | 166 | }
|
---|
[da1bafb] | 167 |
|
---|
[6eabb6e6] | 168 | CPU->fpu_owner = THREAD;
|
---|
[6eef3c4] | 169 | THREAD->fpu_context_engaged = true;
|
---|
[da1bafb] | 170 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
| 171 |
|
---|
| 172 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
[b49f4ae] | 173 | }
|
---|
[da1bafb] | 174 | #endif /* CONFIG_FPU_LAZY */
|
---|
[0ca6faa] | 175 |
|
---|
[70527f1] | 176 | /** Initialize scheduler
|
---|
| 177 | *
|
---|
| 178 | * Initialize kernel scheduler.
|
---|
| 179 | *
|
---|
| 180 | */
|
---|
[f761f1eb] | 181 | void scheduler_init(void)
|
---|
| 182 | {
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[70527f1] | 185 | /** Get thread to be scheduled
|
---|
| 186 | *
|
---|
| 187 | * Get the optimal thread to be scheduled
|
---|
[d1a184f] | 188 | * according to thread accounting and scheduler
|
---|
[70527f1] | 189 | * policy.
|
---|
| 190 | *
|
---|
| 191 | * @return Thread to be scheduled.
|
---|
| 192 | *
|
---|
| 193 | */
|
---|
[e507afa] | 194 | static thread_t *find_best_thread(void)
|
---|
[f761f1eb] | 195 | {
|
---|
[623ba26c] | 196 | ASSERT(CPU != NULL);
|
---|
[da1bafb] | 197 |
|
---|
[f761f1eb] | 198 | loop:
|
---|
| 199 |
|
---|
[248fc1a] | 200 | if (atomic_get(&CPU->nrdy) == 0) {
|
---|
[f761f1eb] | 201 | /*
|
---|
| 202 | * For there was nothing to run, the CPU goes to sleep
|
---|
| 203 | * until a hardware interrupt or an IPI comes.
|
---|
| 204 | * This improves energy saving and hyperthreading.
|
---|
| 205 | */
|
---|
[da1bafb] | 206 | irq_spinlock_lock(&CPU->lock, false);
|
---|
| 207 | CPU->idle = true;
|
---|
| 208 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
| 209 | interrupts_enable();
|
---|
[d0c82c5] | 210 |
|
---|
[da1bafb] | 211 | /*
|
---|
[328e0d3] | 212 | * An interrupt might occur right now and wake up a thread.
|
---|
| 213 | * In such case, the CPU will continue to go to sleep
|
---|
| 214 | * even though there is a runnable thread.
|
---|
| 215 | */
|
---|
[da1bafb] | 216 | cpu_sleep();
|
---|
| 217 | interrupts_disable();
|
---|
| 218 | goto loop;
|
---|
[f761f1eb] | 219 | }
|
---|
[d896525] | 220 |
|
---|
[da1bafb] | 221 | unsigned int i;
|
---|
[ea63704] | 222 | for (i = 0; i < RQ_COUNT; i++) {
|
---|
[da1bafb] | 223 | irq_spinlock_lock(&(CPU->rq[i].lock), false);
|
---|
| 224 | if (CPU->rq[i].n == 0) {
|
---|
[f761f1eb] | 225 | /*
|
---|
| 226 | * If this queue is empty, try a lower-priority queue.
|
---|
| 227 | */
|
---|
[da1bafb] | 228 | irq_spinlock_unlock(&(CPU->rq[i].lock), false);
|
---|
[f761f1eb] | 229 | continue;
|
---|
| 230 | }
|
---|
[da1bafb] | 231 |
|
---|
[248fc1a] | 232 | atomic_dec(&CPU->nrdy);
|
---|
[59e07c91] | 233 | atomic_dec(&nrdy);
|
---|
[da1bafb] | 234 | CPU->rq[i].n--;
|
---|
| 235 |
|
---|
[f761f1eb] | 236 | /*
|
---|
| 237 | * Take the first thread from the queue.
|
---|
| 238 | */
|
---|
[55b77d9] | 239 | thread_t *thread = list_get_instance(
|
---|
| 240 | list_first(&CPU->rq[i].rq), thread_t, rq_link);
|
---|
[da1bafb] | 241 | list_remove(&thread->rq_link);
|
---|
| 242 |
|
---|
| 243 | irq_spinlock_pass(&(CPU->rq[i].lock), &thread->lock);
|
---|
| 244 |
|
---|
| 245 | thread->cpu = CPU;
|
---|
| 246 | thread->ticks = us2ticks((i + 1) * 10000);
|
---|
| 247 | thread->priority = i; /* Correct rq index */
|
---|
| 248 |
|
---|
[f761f1eb] | 249 | /*
|
---|
[6eef3c4] | 250 | * Clear the stolen flag so that it can be migrated
|
---|
[32fffef0] | 251 | * when load balancing needs emerge.
|
---|
[f761f1eb] | 252 | */
|
---|
[6eef3c4] | 253 | thread->stolen = false;
|
---|
[da1bafb] | 254 | irq_spinlock_unlock(&thread->lock, false);
|
---|
| 255 |
|
---|
| 256 | return thread;
|
---|
[f761f1eb] | 257 | }
|
---|
[da1bafb] | 258 |
|
---|
[f761f1eb] | 259 | goto loop;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[70527f1] | 262 | /** Prevent rq starvation
|
---|
| 263 | *
|
---|
| 264 | * Prevent low priority threads from starving in rq's.
|
---|
| 265 | *
|
---|
| 266 | * When the function decides to relink rq's, it reconnects
|
---|
| 267 | * respective pointers so that in result threads with 'pri'
|
---|
[abbc16e] | 268 | * greater or equal start are moved to a higher-priority queue.
|
---|
[70527f1] | 269 | *
|
---|
| 270 | * @param start Threshold priority.
|
---|
| 271 | *
|
---|
[f761f1eb] | 272 | */
|
---|
[e16e036a] | 273 | static void relink_rq(int start)
|
---|
[f761f1eb] | 274 | {
|
---|
[55b77d9] | 275 | list_t list;
|
---|
[da1bafb] | 276 |
|
---|
[55b77d9] | 277 | list_initialize(&list);
|
---|
[da1bafb] | 278 | irq_spinlock_lock(&CPU->lock, false);
|
---|
| 279 |
|
---|
[43114c5] | 280 | if (CPU->needs_relink > NEEDS_RELINK_MAX) {
|
---|
[da1bafb] | 281 | int i;
|
---|
[4e33b6b] | 282 | for (i = start; i < RQ_COUNT - 1; i++) {
|
---|
[da1bafb] | 283 | /* Remember and empty rq[i + 1] */
|
---|
| 284 |
|
---|
| 285 | irq_spinlock_lock(&CPU->rq[i + 1].lock, false);
|
---|
[55b77d9] | 286 | list_concat(&list, &CPU->rq[i + 1].rq);
|
---|
[da1bafb] | 287 | size_t n = CPU->rq[i + 1].n;
|
---|
| 288 | CPU->rq[i + 1].n = 0;
|
---|
| 289 | irq_spinlock_unlock(&CPU->rq[i + 1].lock, false);
|
---|
| 290 |
|
---|
| 291 | /* Append rq[i + 1] to rq[i] */
|
---|
| 292 |
|
---|
| 293 | irq_spinlock_lock(&CPU->rq[i].lock, false);
|
---|
[55b77d9] | 294 | list_concat(&CPU->rq[i].rq, &list);
|
---|
[da1bafb] | 295 | CPU->rq[i].n += n;
|
---|
| 296 | irq_spinlock_unlock(&CPU->rq[i].lock, false);
|
---|
[f761f1eb] | 297 | }
|
---|
[da1bafb] | 298 |
|
---|
[43114c5] | 299 | CPU->needs_relink = 0;
|
---|
[f761f1eb] | 300 | }
|
---|
[da1bafb] | 301 |
|
---|
| 302 | irq_spinlock_unlock(&CPU->lock, false);
|
---|
[f761f1eb] | 303 | }
|
---|
| 304 |
|
---|
[7d6ec87] | 305 | /** The scheduler
|
---|
| 306 | *
|
---|
| 307 | * The thread scheduling procedure.
|
---|
| 308 | * Passes control directly to
|
---|
| 309 | * scheduler_separated_stack().
|
---|
| 310 | *
|
---|
| 311 | */
|
---|
| 312 | void scheduler(void)
|
---|
| 313 | {
|
---|
| 314 | volatile ipl_t ipl;
|
---|
[da1bafb] | 315 |
|
---|
[7d6ec87] | 316 | ASSERT(CPU != NULL);
|
---|
[da1bafb] | 317 |
|
---|
[7d6ec87] | 318 | ipl = interrupts_disable();
|
---|
[da1bafb] | 319 |
|
---|
[7d6ec87] | 320 | if (atomic_get(&haltstate))
|
---|
| 321 | halt();
|
---|
[8965838e] | 322 |
|
---|
[7d6ec87] | 323 | if (THREAD) {
|
---|
[da1bafb] | 324 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[cce6acf] | 325 |
|
---|
[1ba37fa] | 326 | /* Update thread kernel accounting */
|
---|
[a2a00e8] | 327 | THREAD->kcycles += get_cycle() - THREAD->last_cycle;
|
---|
[cce6acf] | 328 |
|
---|
[e1326cf] | 329 | #if (defined CONFIG_FPU) && (!defined CONFIG_FPU_LAZY)
|
---|
[f76fed4] | 330 | fpu_context_save(THREAD->saved_fpu_context);
|
---|
| 331 | #endif
|
---|
[7d6ec87] | 332 | if (!context_save(&THREAD->saved_context)) {
|
---|
| 333 | /*
|
---|
| 334 | * This is the place where threads leave scheduler();
|
---|
| 335 | */
|
---|
[cce6acf] | 336 |
|
---|
| 337 | /* Save current CPU cycle */
|
---|
| 338 | THREAD->last_cycle = get_cycle();
|
---|
| 339 |
|
---|
[da1bafb] | 340 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[7d6ec87] | 341 | interrupts_restore(THREAD->saved_context.ipl);
|
---|
[8965838e] | 342 |
|
---|
[7d6ec87] | 343 | return;
|
---|
| 344 | }
|
---|
[da1bafb] | 345 |
|
---|
[7d6ec87] | 346 | /*
|
---|
[4e33b6b] | 347 | * Interrupt priority level of preempted thread is recorded
|
---|
| 348 | * here to facilitate scheduler() invocations from
|
---|
[da1bafb] | 349 | * interrupts_disable()'d code (e.g. waitq_sleep_timeout()).
|
---|
| 350 | *
|
---|
[7d6ec87] | 351 | */
|
---|
| 352 | THREAD->saved_context.ipl = ipl;
|
---|
| 353 | }
|
---|
[da1bafb] | 354 |
|
---|
[7d6ec87] | 355 | /*
|
---|
[b4dc35a] | 356 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, AS
|
---|
[7d6ec87] | 357 | * and preemption counter. At this point THE could be coming either
|
---|
| 358 | * from THREAD's or CPU's stack.
|
---|
[da1bafb] | 359 | *
|
---|
[7d6ec87] | 360 | */
|
---|
| 361 | the_copy(THE, (the_t *) CPU->stack);
|
---|
[da1bafb] | 362 |
|
---|
[7d6ec87] | 363 | /*
|
---|
| 364 | * We may not keep the old stack.
|
---|
| 365 | * Reason: If we kept the old stack and got blocked, for instance, in
|
---|
| 366 | * find_best_thread(), the old thread could get rescheduled by another
|
---|
| 367 | * CPU and overwrite the part of its own stack that was also used by
|
---|
| 368 | * the scheduler on this CPU.
|
---|
| 369 | *
|
---|
| 370 | * Moreover, we have to bypass the compiler-generated POP sequence
|
---|
| 371 | * which is fooled by SP being set to the very top of the stack.
|
---|
| 372 | * Therefore the scheduler() function continues in
|
---|
| 373 | * scheduler_separated_stack().
|
---|
[da1bafb] | 374 | *
|
---|
[7d6ec87] | 375 | */
|
---|
| 376 | context_save(&CPU->saved_context);
|
---|
[32fffef0] | 377 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack),
|
---|
[26aafe8] | 378 | (uintptr_t) CPU->stack, STACK_SIZE);
|
---|
[7d6ec87] | 379 | context_restore(&CPU->saved_context);
|
---|
[da1bafb] | 380 |
|
---|
| 381 | /* Not reached */
|
---|
[7d6ec87] | 382 | }
|
---|
[70527f1] | 383 |
|
---|
| 384 | /** Scheduler stack switch wrapper
|
---|
| 385 | *
|
---|
| 386 | * Second part of the scheduler() function
|
---|
| 387 | * using new stack. Handling the actual context
|
---|
| 388 | * switch to a new thread.
|
---|
| 389 | *
|
---|
| 390 | */
|
---|
[7d6ec87] | 391 | void scheduler_separated_stack(void)
|
---|
[f761f1eb] | 392 | {
|
---|
[31d8e10] | 393 | DEADLOCK_PROBE_INIT(p_joinwq);
|
---|
[481d4751] | 394 | task_t *old_task = TASK;
|
---|
| 395 | as_t *old_as = AS;
|
---|
[da1bafb] | 396 |
|
---|
[d0c82c5] | 397 | ASSERT((!THREAD) || (irq_spinlock_locked(&THREAD->lock)));
|
---|
[623ba26c] | 398 | ASSERT(CPU != NULL);
|
---|
[8965838e] | 399 |
|
---|
[481d4751] | 400 | /*
|
---|
| 401 | * Hold the current task and the address space to prevent their
|
---|
| 402 | * possible destruction should thread_destroy() be called on this or any
|
---|
| 403 | * other processor while the scheduler is still using them.
|
---|
| 404 | */
|
---|
| 405 | if (old_task)
|
---|
| 406 | task_hold(old_task);
|
---|
[da1bafb] | 407 |
|
---|
[481d4751] | 408 | if (old_as)
|
---|
| 409 | as_hold(old_as);
|
---|
[da1bafb] | 410 |
|
---|
[43114c5] | 411 | if (THREAD) {
|
---|
[da1bafb] | 412 | /* Must be run after the switch to scheduler stack */
|
---|
[97f1691] | 413 | after_thread_ran();
|
---|
[da1bafb] | 414 |
|
---|
[43114c5] | 415 | switch (THREAD->state) {
|
---|
[06e1e95] | 416 | case Running:
|
---|
[da1bafb] | 417 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[76cec1e] | 418 | thread_ready(THREAD);
|
---|
| 419 | break;
|
---|
[da1bafb] | 420 |
|
---|
[06e1e95] | 421 | case Exiting:
|
---|
[fe19611] | 422 | repeat:
|
---|
[def5207] | 423 | if (THREAD->detached) {
|
---|
[da1bafb] | 424 | thread_destroy(THREAD, false);
|
---|
[fe19611] | 425 | } else {
|
---|
| 426 | /*
|
---|
[4e33b6b] | 427 | * The thread structure is kept allocated until
|
---|
| 428 | * somebody calls thread_detach() on it.
|
---|
[fe19611] | 429 | */
|
---|
[da1bafb] | 430 | if (!irq_spinlock_trylock(&THREAD->join_wq.lock)) {
|
---|
[fe19611] | 431 | /*
|
---|
| 432 | * Avoid deadlock.
|
---|
| 433 | */
|
---|
[da1bafb] | 434 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[ea7890e7] | 435 | delay(HZ);
|
---|
[da1bafb] | 436 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[31d8e10] | 437 | DEADLOCK_PROBE(p_joinwq,
|
---|
| 438 | DEADLOCK_THRESHOLD);
|
---|
[fe19611] | 439 | goto repeat;
|
---|
| 440 | }
|
---|
[5c8ba05] | 441 | _waitq_wakeup_unsafe(&THREAD->join_wq,
|
---|
| 442 | WAKEUP_FIRST);
|
---|
[da1bafb] | 443 | irq_spinlock_unlock(&THREAD->join_wq.lock, false);
|
---|
[fe19611] | 444 |
|
---|
[48d14222] | 445 | THREAD->state = Lingering;
|
---|
[da1bafb] | 446 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[fe19611] | 447 | }
|
---|
[76cec1e] | 448 | break;
|
---|
[266294a9] | 449 |
|
---|
[06e1e95] | 450 | case Sleeping:
|
---|
[76cec1e] | 451 | /*
|
---|
| 452 | * Prefer the thread after it's woken up.
|
---|
| 453 | */
|
---|
[22f7769] | 454 | THREAD->priority = -1;
|
---|
[da1bafb] | 455 |
|
---|
[76cec1e] | 456 | /*
|
---|
[4e33b6b] | 457 | * We need to release wq->lock which we locked in
|
---|
| 458 | * waitq_sleep(). Address of wq->lock is kept in
|
---|
| 459 | * THREAD->sleep_queue.
|
---|
[76cec1e] | 460 | */
|
---|
[da1bafb] | 461 | irq_spinlock_unlock(&THREAD->sleep_queue->lock, false);
|
---|
| 462 |
|
---|
| 463 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[76cec1e] | 464 | break;
|
---|
[da1bafb] | 465 |
|
---|
[06e1e95] | 466 | default:
|
---|
[76cec1e] | 467 | /*
|
---|
| 468 | * Entering state is unexpected.
|
---|
| 469 | */
|
---|
[f651e80] | 470 | panic("tid%" PRIu64 ": unexpected state %s.",
|
---|
[1e9d0e3] | 471 | THREAD->tid, thread_states[THREAD->state]);
|
---|
[76cec1e] | 472 | break;
|
---|
[f761f1eb] | 473 | }
|
---|
[da1bafb] | 474 |
|
---|
[43114c5] | 475 | THREAD = NULL;
|
---|
[f761f1eb] | 476 | }
|
---|
[da1bafb] | 477 |
|
---|
[43114c5] | 478 | THREAD = find_best_thread();
|
---|
[f761f1eb] | 479 |
|
---|
[da1bafb] | 480 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
| 481 | int priority = THREAD->priority;
|
---|
| 482 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
| 483 |
|
---|
| 484 | relink_rq(priority);
|
---|
| 485 |
|
---|
[f761f1eb] | 486 | /*
|
---|
[4e7d3dd] | 487 | * If both the old and the new task are the same,
|
---|
| 488 | * lots of work is avoided.
|
---|
[f761f1eb] | 489 | */
|
---|
[43114c5] | 490 | if (TASK != THREAD->task) {
|
---|
[481d4751] | 491 | as_t *new_as = THREAD->task->as;
|
---|
[f761f1eb] | 492 |
|
---|
| 493 | /*
|
---|
[4e7d3dd] | 494 | * Note that it is possible for two tasks
|
---|
| 495 | * to share one address space.
|
---|
[f761f1eb] | 496 | */
|
---|
[481d4751] | 497 | if (old_as != new_as) {
|
---|
[f761f1eb] | 498 | /*
|
---|
[20d50a1] | 499 | * Both tasks and address spaces are different.
|
---|
[f761f1eb] | 500 | * Replace the old one with the new one.
|
---|
| 501 | */
|
---|
[481d4751] | 502 | as_switch(old_as, new_as);
|
---|
[f761f1eb] | 503 | }
|
---|
[da1bafb] | 504 |
|
---|
[f76fed4] | 505 | TASK = THREAD->task;
|
---|
[39cea6a] | 506 | before_task_runs();
|
---|
[f761f1eb] | 507 | }
|
---|
[da1bafb] | 508 |
|
---|
[481d4751] | 509 | if (old_task)
|
---|
| 510 | task_release(old_task);
|
---|
[da1bafb] | 511 |
|
---|
[481d4751] | 512 | if (old_as)
|
---|
| 513 | as_release(old_as);
|
---|
| 514 |
|
---|
[da1bafb] | 515 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
[43114c5] | 516 | THREAD->state = Running;
|
---|
[da1bafb] | 517 |
|
---|
[f76fed4] | 518 | #ifdef SCHEDULER_VERBOSE
|
---|
[550523f5] | 519 | printf("cpu%u: tid %" PRIu64 " (priority=%d, ticks=%" PRIu64
|
---|
| 520 | ", nrdy=%" PRIua ")\n", CPU->id, THREAD->tid, THREAD->priority,
|
---|
[1e9d0e3] | 521 | THREAD->ticks, atomic_get(&CPU->nrdy));
|
---|
[da1bafb] | 522 | #endif
|
---|
| 523 |
|
---|
[97f1691] | 524 | /*
|
---|
| 525 | * Some architectures provide late kernel PA2KA(identity)
|
---|
| 526 | * mapping in a page fault handler. However, the page fault
|
---|
| 527 | * handler uses the kernel stack of the running thread and
|
---|
| 528 | * therefore cannot be used to map it. The kernel stack, if
|
---|
| 529 | * necessary, is to be mapped in before_thread_runs(). This
|
---|
| 530 | * function must be executed before the switch to the new stack.
|
---|
| 531 | */
|
---|
| 532 | before_thread_runs();
|
---|
[da1bafb] | 533 |
|
---|
[3e1607f] | 534 | /*
|
---|
[4e33b6b] | 535 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
|
---|
| 536 | * thread's stack.
|
---|
[3e1607f] | 537 | */
|
---|
[bcdd9aa] | 538 | the_copy(THE, (the_t *) THREAD->kstack);
|
---|
| 539 |
|
---|
[43114c5] | 540 | context_restore(&THREAD->saved_context);
|
---|
[da1bafb] | 541 |
|
---|
| 542 | /* Not reached */
|
---|
[f761f1eb] | 543 | }
|
---|
| 544 |
|
---|
[5f85c91] | 545 | #ifdef CONFIG_SMP
|
---|
[70527f1] | 546 | /** Load balancing thread
|
---|
| 547 | *
|
---|
| 548 | * SMP load balancing thread, supervising thread supplies
|
---|
| 549 | * for the CPU it's wired to.
|
---|
| 550 | *
|
---|
| 551 | * @param arg Generic thread argument (unused).
|
---|
| 552 | *
|
---|
[f761f1eb] | 553 | */
|
---|
| 554 | void kcpulb(void *arg)
|
---|
| 555 | {
|
---|
[228666c] | 556 | atomic_count_t average;
|
---|
[da1bafb] | 557 | atomic_count_t rdy;
|
---|
| 558 |
|
---|
[2cb5e64] | 559 | /*
|
---|
| 560 | * Detach kcpulb as nobody will call thread_join_timeout() on it.
|
---|
| 561 | */
|
---|
| 562 | thread_detach(THREAD);
|
---|
| 563 |
|
---|
[f761f1eb] | 564 | loop:
|
---|
| 565 | /*
|
---|
[3260ada] | 566 | * Work in 1s intervals.
|
---|
[f761f1eb] | 567 | */
|
---|
[3260ada] | 568 | thread_sleep(1);
|
---|
[da1bafb] | 569 |
|
---|
[f761f1eb] | 570 | not_satisfied:
|
---|
| 571 | /*
|
---|
| 572 | * Calculate the number of threads that will be migrated/stolen from
|
---|
| 573 | * other CPU's. Note that situation can have changed between two
|
---|
| 574 | * passes. Each time get the most up to date counts.
|
---|
[da1bafb] | 575 | *
|
---|
[f761f1eb] | 576 | */
|
---|
[444ec64] | 577 | average = atomic_get(&nrdy) / config.cpu_active + 1;
|
---|
[da1bafb] | 578 | rdy = atomic_get(&CPU->nrdy);
|
---|
| 579 |
|
---|
| 580 | if (average <= rdy)
|
---|
[f761f1eb] | 581 | goto satisfied;
|
---|
[da1bafb] | 582 |
|
---|
| 583 | atomic_count_t count = average - rdy;
|
---|
| 584 |
|
---|
[f761f1eb] | 585 | /*
|
---|
[4e33b6b] | 586 | * Searching least priority queues on all CPU's first and most priority
|
---|
| 587 | * queues on all CPU's last.
|
---|
[f761f1eb] | 588 | */
|
---|
[da1bafb] | 589 | size_t acpu;
|
---|
| 590 | size_t acpu_bias = 0;
|
---|
| 591 | int rq;
|
---|
| 592 |
|
---|
| 593 | for (rq = RQ_COUNT - 1; rq >= 0; rq--) {
|
---|
| 594 | for (acpu = 0; acpu < config.cpu_active; acpu++) {
|
---|
| 595 | cpu_t *cpu = &cpus[(acpu + acpu_bias) % config.cpu_active];
|
---|
| 596 |
|
---|
[f761f1eb] | 597 | /*
|
---|
| 598 | * Not interested in ourselves.
|
---|
[4e33b6b] | 599 | * Doesn't require interrupt disabling for kcpulb has
|
---|
| 600 | * THREAD_FLAG_WIRED.
|
---|
[da1bafb] | 601 | *
|
---|
[f761f1eb] | 602 | */
|
---|
[43114c5] | 603 | if (CPU == cpu)
|
---|
[248fc1a] | 604 | continue;
|
---|
[da1bafb] | 605 |
|
---|
[248fc1a] | 606 | if (atomic_get(&cpu->nrdy) <= average)
|
---|
| 607 | continue;
|
---|
[da1bafb] | 608 |
|
---|
| 609 | irq_spinlock_lock(&(cpu->rq[rq].lock), true);
|
---|
| 610 | if (cpu->rq[rq].n == 0) {
|
---|
| 611 | irq_spinlock_unlock(&(cpu->rq[rq].lock), true);
|
---|
[f761f1eb] | 612 | continue;
|
---|
| 613 | }
|
---|
[da1bafb] | 614 |
|
---|
| 615 | thread_t *thread = NULL;
|
---|
| 616 |
|
---|
| 617 | /* Search rq from the back */
|
---|
[55b77d9] | 618 | link_t *link = cpu->rq[rq].rq.head.prev;
|
---|
[da1bafb] | 619 |
|
---|
[55b77d9] | 620 | while (link != &(cpu->rq[rq].rq.head)) {
|
---|
[43ac0cc] | 621 | thread = (thread_t *) list_get_instance(link,
|
---|
| 622 | thread_t, rq_link);
|
---|
[da1bafb] | 623 |
|
---|
[f761f1eb] | 624 | /*
|
---|
[43ac0cc] | 625 | * Do not steal CPU-wired threads, threads
|
---|
| 626 | * already stolen, threads for which migration
|
---|
| 627 | * was temporarily disabled or threads whose
|
---|
| 628 | * FPU context is still in the CPU.
|
---|
[6a27d63] | 629 | */
|
---|
[da1bafb] | 630 | irq_spinlock_lock(&thread->lock, false);
|
---|
| 631 |
|
---|
[6eef3c4] | 632 | if ((!thread->wired) && (!thread->stolen) &&
|
---|
| 633 | (!thread->nomigrate) &&
|
---|
| 634 | (!thread->fpu_context_engaged)) {
|
---|
[f761f1eb] | 635 | /*
|
---|
[da1bafb] | 636 | * Remove thread from ready queue.
|
---|
[f761f1eb] | 637 | */
|
---|
[43ac0cc] | 638 | irq_spinlock_unlock(&thread->lock,
|
---|
| 639 | false);
|
---|
[f761f1eb] | 640 |
|
---|
[248fc1a] | 641 | atomic_dec(&cpu->nrdy);
|
---|
[59e07c91] | 642 | atomic_dec(&nrdy);
|
---|
[da1bafb] | 643 |
|
---|
| 644 | cpu->rq[rq].n--;
|
---|
| 645 | list_remove(&thread->rq_link);
|
---|
| 646 |
|
---|
[f761f1eb] | 647 | break;
|
---|
| 648 | }
|
---|
[da1bafb] | 649 |
|
---|
| 650 | irq_spinlock_unlock(&thread->lock, false);
|
---|
| 651 |
|
---|
| 652 | link = link->prev;
|
---|
| 653 | thread = NULL;
|
---|
[f761f1eb] | 654 | }
|
---|
[da1bafb] | 655 |
|
---|
| 656 | if (thread) {
|
---|
[f761f1eb] | 657 | /*
|
---|
[da1bafb] | 658 | * Ready thread on local CPU
|
---|
[f761f1eb] | 659 | */
|
---|
[da1bafb] | 660 |
|
---|
[43ac0cc] | 661 | irq_spinlock_pass(&(cpu->rq[rq].lock),
|
---|
| 662 | &thread->lock);
|
---|
[da1bafb] | 663 |
|
---|
[f76fed4] | 664 | #ifdef KCPULB_VERBOSE
|
---|
[1e9d0e3] | 665 | printf("kcpulb%u: TID %" PRIu64 " -> cpu%u, "
|
---|
| 666 | "nrdy=%ld, avg=%ld\n", CPU->id, t->tid,
|
---|
| 667 | CPU->id, atomic_get(&CPU->nrdy),
|
---|
[6f4495f5] | 668 | atomic_get(&nrdy) / config.cpu_active);
|
---|
[f76fed4] | 669 | #endif
|
---|
[da1bafb] | 670 |
|
---|
[6eef3c4] | 671 | thread->stolen = true;
|
---|
[da1bafb] | 672 | thread->state = Entering;
|
---|
| 673 |
|
---|
| 674 | irq_spinlock_unlock(&thread->lock, true);
|
---|
| 675 | thread_ready(thread);
|
---|
| 676 |
|
---|
[f761f1eb] | 677 | if (--count == 0)
|
---|
| 678 | goto satisfied;
|
---|
[da1bafb] | 679 |
|
---|
[f761f1eb] | 680 | /*
|
---|
[4e33b6b] | 681 | * We are not satisfied yet, focus on another
|
---|
| 682 | * CPU next time.
|
---|
[da1bafb] | 683 | *
|
---|
[f761f1eb] | 684 | */
|
---|
[da1bafb] | 685 | acpu_bias++;
|
---|
[f761f1eb] | 686 |
|
---|
| 687 | continue;
|
---|
[da1bafb] | 688 | } else
|
---|
| 689 | irq_spinlock_unlock(&(cpu->rq[rq].lock), true);
|
---|
| 690 |
|
---|
[f761f1eb] | 691 | }
|
---|
| 692 | }
|
---|
[da1bafb] | 693 |
|
---|
[248fc1a] | 694 | if (atomic_get(&CPU->nrdy)) {
|
---|
[f761f1eb] | 695 | /*
|
---|
| 696 | * Be a little bit light-weight and let migrated threads run.
|
---|
[da1bafb] | 697 | *
|
---|
[f761f1eb] | 698 | */
|
---|
| 699 | scheduler();
|
---|
[3260ada] | 700 | } else {
|
---|
[f761f1eb] | 701 | /*
|
---|
| 702 | * We failed to migrate a single thread.
|
---|
[3260ada] | 703 | * Give up this turn.
|
---|
[da1bafb] | 704 | *
|
---|
[f761f1eb] | 705 | */
|
---|
[3260ada] | 706 | goto loop;
|
---|
[f761f1eb] | 707 | }
|
---|
[da1bafb] | 708 |
|
---|
[f761f1eb] | 709 | goto not_satisfied;
|
---|
[da1bafb] | 710 |
|
---|
[f761f1eb] | 711 | satisfied:
|
---|
| 712 | goto loop;
|
---|
| 713 | }
|
---|
[5f85c91] | 714 | #endif /* CONFIG_SMP */
|
---|
[10e16a7] | 715 |
|
---|
[da1bafb] | 716 | /** Print information about threads & scheduler queues
|
---|
| 717 | *
|
---|
| 718 | */
|
---|
[10e16a7] | 719 | void sched_print_list(void)
|
---|
| 720 | {
|
---|
[da1bafb] | 721 | size_t cpu;
|
---|
[4184e76] | 722 | for (cpu = 0; cpu < config.cpu_count; cpu++) {
|
---|
[10e16a7] | 723 | if (!cpus[cpu].active)
|
---|
| 724 | continue;
|
---|
[da1bafb] | 725 |
|
---|
| 726 | irq_spinlock_lock(&cpus[cpu].lock, true);
|
---|
| 727 |
|
---|
[7e752b2] | 728 | printf("cpu%u: address=%p, nrdy=%" PRIua ", needs_relink=%zu\n",
|
---|
[6f4495f5] | 729 | cpus[cpu].id, &cpus[cpu], atomic_get(&cpus[cpu].nrdy),
|
---|
| 730 | cpus[cpu].needs_relink);
|
---|
[10e16a7] | 731 |
|
---|
[da1bafb] | 732 | unsigned int i;
|
---|
[4e33b6b] | 733 | for (i = 0; i < RQ_COUNT; i++) {
|
---|
[da1bafb] | 734 | irq_spinlock_lock(&(cpus[cpu].rq[i].lock), false);
|
---|
| 735 | if (cpus[cpu].rq[i].n == 0) {
|
---|
| 736 | irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
|
---|
[10e16a7] | 737 | continue;
|
---|
| 738 | }
|
---|
[da1bafb] | 739 |
|
---|
[5b86d10] | 740 | printf("\trq[%u]: ", i);
|
---|
[feeac0d] | 741 | list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
|
---|
| 742 | thread) {
|
---|
[da1bafb] | 743 | printf("%" PRIu64 "(%s) ", thread->tid,
|
---|
| 744 | thread_states[thread->state]);
|
---|
[10e16a7] | 745 | }
|
---|
| 746 | printf("\n");
|
---|
[da1bafb] | 747 |
|
---|
| 748 | irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
|
---|
[10e16a7] | 749 | }
|
---|
[da1bafb] | 750 |
|
---|
| 751 | irq_spinlock_unlock(&cpus[cpu].lock, true);
|
---|
[10e16a7] | 752 | }
|
---|
| 753 | }
|
---|
[b45c443] | 754 |
|
---|
[cc73a8a1] | 755 | /** @}
|
---|
[b45c443] | 756 | */
|
---|