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