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