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