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