[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2007 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:
|
---|
[22f7769] | 189 | interrupts_enable();
|
---|
[f761f1eb] | 190 |
|
---|
[248fc1a] | 191 | if (atomic_get(&CPU->nrdy) == 0) {
|
---|
[f761f1eb] | 192 | /*
|
---|
| 193 | * For there was nothing to run, the CPU goes to sleep
|
---|
| 194 | * until a hardware interrupt or an IPI comes.
|
---|
| 195 | * This improves energy saving and hyperthreading.
|
---|
| 196 | */
|
---|
[328e0d3] | 197 |
|
---|
| 198 | /*
|
---|
| 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 | */
|
---|
| 203 |
|
---|
[e257ae3] | 204 | CPU->idle = true;
|
---|
[f761f1eb] | 205 | cpu_sleep();
|
---|
| 206 | goto loop;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[22f7769] | 209 | interrupts_disable();
|
---|
[d896525] | 210 |
|
---|
[ea63704] | 211 | for (i = 0; i < RQ_COUNT; i++) {
|
---|
[43114c5] | 212 | r = &CPU->rq[i];
|
---|
[f761f1eb] | 213 | spinlock_lock(&r->lock);
|
---|
| 214 | if (r->n == 0) {
|
---|
| 215 | /*
|
---|
| 216 | * If this queue is empty, try a lower-priority queue.
|
---|
| 217 | */
|
---|
| 218 | spinlock_unlock(&r->lock);
|
---|
| 219 | continue;
|
---|
| 220 | }
|
---|
[3e1607f] | 221 |
|
---|
[248fc1a] | 222 | atomic_dec(&CPU->nrdy);
|
---|
[59e07c91] | 223 | atomic_dec(&nrdy);
|
---|
[f761f1eb] | 224 | r->n--;
|
---|
| 225 |
|
---|
| 226 | /*
|
---|
| 227 | * Take the first thread from the queue.
|
---|
| 228 | */
|
---|
| 229 | t = list_get_instance(r->rq_head.next, thread_t, rq_link);
|
---|
| 230 | list_remove(&t->rq_link);
|
---|
| 231 |
|
---|
| 232 | spinlock_unlock(&r->lock);
|
---|
| 233 |
|
---|
| 234 | spinlock_lock(&t->lock);
|
---|
[43114c5] | 235 | t->cpu = CPU;
|
---|
[f761f1eb] | 236 |
|
---|
[4e33b6b] | 237 | t->ticks = us2ticks((i + 1) * 10000);
|
---|
[7d6ec87] | 238 | t->priority = i; /* correct rq index */
|
---|
[f761f1eb] | 239 |
|
---|
| 240 | /*
|
---|
[32fffef0] | 241 | * Clear the THREAD_FLAG_STOLEN flag so that t can be migrated
|
---|
| 242 | * when load balancing needs emerge.
|
---|
[f761f1eb] | 243 | */
|
---|
[32fffef0] | 244 | t->flags &= ~THREAD_FLAG_STOLEN;
|
---|
[f761f1eb] | 245 | spinlock_unlock(&t->lock);
|
---|
| 246 |
|
---|
| 247 | return t;
|
---|
| 248 | }
|
---|
| 249 | goto loop;
|
---|
| 250 |
|
---|
| 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;
|
---|
| 267 | runq_t *r;
|
---|
| 268 | int i, n;
|
---|
| 269 |
|
---|
| 270 | list_initialize(&head);
|
---|
[43114c5] | 271 | spinlock_lock(&CPU->lock);
|
---|
| 272 | if (CPU->needs_relink > NEEDS_RELINK_MAX) {
|
---|
[4e33b6b] | 273 | for (i = start; i < RQ_COUNT - 1; i++) {
|
---|
[f761f1eb] | 274 | /* remember and empty rq[i + 1] */
|
---|
[43114c5] | 275 | r = &CPU->rq[i + 1];
|
---|
[f761f1eb] | 276 | spinlock_lock(&r->lock);
|
---|
| 277 | list_concat(&head, &r->rq_head);
|
---|
| 278 | n = r->n;
|
---|
| 279 | r->n = 0;
|
---|
| 280 | spinlock_unlock(&r->lock);
|
---|
| 281 |
|
---|
| 282 | /* append rq[i + 1] to rq[i] */
|
---|
[43114c5] | 283 | r = &CPU->rq[i];
|
---|
[f761f1eb] | 284 | spinlock_lock(&r->lock);
|
---|
| 285 | list_concat(&r->rq_head, &head);
|
---|
| 286 | r->n += n;
|
---|
| 287 | spinlock_unlock(&r->lock);
|
---|
| 288 | }
|
---|
[43114c5] | 289 | CPU->needs_relink = 0;
|
---|
[f761f1eb] | 290 | }
|
---|
[444ec64] | 291 | spinlock_unlock(&CPU->lock);
|
---|
[f761f1eb] | 292 |
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[7d6ec87] | 295 | /** The scheduler
|
---|
| 296 | *
|
---|
| 297 | * The thread scheduling procedure.
|
---|
| 298 | * Passes control directly to
|
---|
| 299 | * scheduler_separated_stack().
|
---|
| 300 | *
|
---|
| 301 | */
|
---|
| 302 | void scheduler(void)
|
---|
| 303 | {
|
---|
| 304 | volatile ipl_t ipl;
|
---|
| 305 |
|
---|
| 306 | ASSERT(CPU != NULL);
|
---|
| 307 |
|
---|
| 308 | ipl = interrupts_disable();
|
---|
| 309 |
|
---|
| 310 | if (atomic_get(&haltstate))
|
---|
| 311 | halt();
|
---|
[8965838e] | 312 |
|
---|
[7d6ec87] | 313 | if (THREAD) {
|
---|
| 314 | spinlock_lock(&THREAD->lock);
|
---|
[cce6acf] | 315 |
|
---|
| 316 | /* Update thread accounting */
|
---|
| 317 | THREAD->cycles += get_cycle() - THREAD->last_cycle;
|
---|
[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 |
|
---|
[7d6ec87] | 331 | spinlock_unlock(&THREAD->lock);
|
---|
| 332 | interrupts_restore(THREAD->saved_context.ipl);
|
---|
[8965838e] | 333 |
|
---|
[7d6ec87] | 334 | return;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /*
|
---|
[4e33b6b] | 338 | * Interrupt priority level of preempted thread is recorded
|
---|
| 339 | * here to facilitate scheduler() invocations from
|
---|
| 340 | * interrupts_disable()'d code (e.g. waitq_sleep_timeout()).
|
---|
[7d6ec87] | 341 | */
|
---|
| 342 | THREAD->saved_context.ipl = ipl;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | /*
|
---|
| 346 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM
|
---|
| 347 | * and preemption counter. At this point THE could be coming either
|
---|
| 348 | * from THREAD's or CPU's stack.
|
---|
| 349 | */
|
---|
| 350 | the_copy(THE, (the_t *) CPU->stack);
|
---|
| 351 |
|
---|
| 352 | /*
|
---|
| 353 | * We may not keep the old stack.
|
---|
| 354 | * Reason: If we kept the old stack and got blocked, for instance, in
|
---|
| 355 | * find_best_thread(), the old thread could get rescheduled by another
|
---|
| 356 | * CPU and overwrite the part of its own stack that was also used by
|
---|
| 357 | * the scheduler on this CPU.
|
---|
| 358 | *
|
---|
| 359 | * Moreover, we have to bypass the compiler-generated POP sequence
|
---|
| 360 | * which is fooled by SP being set to the very top of the stack.
|
---|
| 361 | * Therefore the scheduler() function continues in
|
---|
| 362 | * scheduler_separated_stack().
|
---|
| 363 | */
|
---|
| 364 | context_save(&CPU->saved_context);
|
---|
[32fffef0] | 365 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack),
|
---|
[6f4495f5] | 366 | (uintptr_t) CPU->stack, CPU_STACK_SIZE);
|
---|
[7d6ec87] | 367 | context_restore(&CPU->saved_context);
|
---|
| 368 | /* not reached */
|
---|
| 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 | *
|
---|
[266294a9] | 377 | * Assume THREAD->lock is held.
|
---|
[70527f1] | 378 | */
|
---|
[7d6ec87] | 379 | void scheduler_separated_stack(void)
|
---|
[f761f1eb] | 380 | {
|
---|
| 381 | int priority;
|
---|
[31d8e10] | 382 | DEADLOCK_PROBE_INIT(p_joinwq);
|
---|
| 383 |
|
---|
[623ba26c] | 384 | ASSERT(CPU != NULL);
|
---|
[8965838e] | 385 |
|
---|
[43114c5] | 386 | if (THREAD) {
|
---|
[7d6ec87] | 387 | /* must be run after the switch to scheduler stack */
|
---|
[97f1691] | 388 | after_thread_ran();
|
---|
| 389 |
|
---|
[43114c5] | 390 | switch (THREAD->state) {
|
---|
[06e1e95] | 391 | case Running:
|
---|
[76cec1e] | 392 | spinlock_unlock(&THREAD->lock);
|
---|
| 393 | thread_ready(THREAD);
|
---|
| 394 | break;
|
---|
[f761f1eb] | 395 |
|
---|
[06e1e95] | 396 | case Exiting:
|
---|
[fe19611] | 397 | repeat:
|
---|
[def5207] | 398 | if (THREAD->detached) {
|
---|
[fe19611] | 399 | thread_destroy(THREAD);
|
---|
| 400 | } else {
|
---|
| 401 | /*
|
---|
[4e33b6b] | 402 | * The thread structure is kept allocated until
|
---|
| 403 | * somebody calls thread_detach() on it.
|
---|
[fe19611] | 404 | */
|
---|
| 405 | if (!spinlock_trylock(&THREAD->join_wq.lock)) {
|
---|
| 406 | /*
|
---|
| 407 | * Avoid deadlock.
|
---|
| 408 | */
|
---|
| 409 | spinlock_unlock(&THREAD->lock);
|
---|
[ea7890e7] | 410 | delay(HZ);
|
---|
[fe19611] | 411 | spinlock_lock(&THREAD->lock);
|
---|
[31d8e10] | 412 | DEADLOCK_PROBE(p_joinwq,
|
---|
| 413 | DEADLOCK_THRESHOLD);
|
---|
[fe19611] | 414 | goto repeat;
|
---|
| 415 | }
|
---|
[5c8ba05] | 416 | _waitq_wakeup_unsafe(&THREAD->join_wq,
|
---|
| 417 | WAKEUP_FIRST);
|
---|
[fe19611] | 418 | spinlock_unlock(&THREAD->join_wq.lock);
|
---|
| 419 |
|
---|
[48d14222] | 420 | THREAD->state = Lingering;
|
---|
[fe19611] | 421 | spinlock_unlock(&THREAD->lock);
|
---|
| 422 | }
|
---|
[76cec1e] | 423 | break;
|
---|
[266294a9] | 424 |
|
---|
[06e1e95] | 425 | case Sleeping:
|
---|
[76cec1e] | 426 | /*
|
---|
| 427 | * Prefer the thread after it's woken up.
|
---|
| 428 | */
|
---|
[22f7769] | 429 | THREAD->priority = -1;
|
---|
[76cec1e] | 430 |
|
---|
| 431 | /*
|
---|
[4e33b6b] | 432 | * We need to release wq->lock which we locked in
|
---|
| 433 | * waitq_sleep(). Address of wq->lock is kept in
|
---|
| 434 | * THREAD->sleep_queue.
|
---|
[76cec1e] | 435 | */
|
---|
| 436 | spinlock_unlock(&THREAD->sleep_queue->lock);
|
---|
| 437 |
|
---|
| 438 | /*
|
---|
[4e33b6b] | 439 | * Check for possible requests for out-of-context
|
---|
| 440 | * invocation.
|
---|
[76cec1e] | 441 | */
|
---|
| 442 | if (THREAD->call_me) {
|
---|
| 443 | THREAD->call_me(THREAD->call_me_with);
|
---|
| 444 | THREAD->call_me = NULL;
|
---|
| 445 | THREAD->call_me_with = NULL;
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | spinlock_unlock(&THREAD->lock);
|
---|
| 449 |
|
---|
| 450 | break;
|
---|
[f761f1eb] | 451 |
|
---|
[06e1e95] | 452 | default:
|
---|
[76cec1e] | 453 | /*
|
---|
| 454 | * Entering state is unexpected.
|
---|
| 455 | */
|
---|
[f651e80] | 456 | panic("tid%" PRIu64 ": unexpected state %s.",
|
---|
[1e9d0e3] | 457 | THREAD->tid, thread_states[THREAD->state]);
|
---|
[76cec1e] | 458 | break;
|
---|
[f761f1eb] | 459 | }
|
---|
[97f1691] | 460 |
|
---|
[43114c5] | 461 | THREAD = NULL;
|
---|
[f761f1eb] | 462 | }
|
---|
[ba18512] | 463 |
|
---|
[43114c5] | 464 | THREAD = find_best_thread();
|
---|
[f761f1eb] | 465 |
|
---|
[43114c5] | 466 | spinlock_lock(&THREAD->lock);
|
---|
[22f7769] | 467 | priority = THREAD->priority;
|
---|
[43114c5] | 468 | spinlock_unlock(&THREAD->lock);
|
---|
[7ce9284] | 469 |
|
---|
[f761f1eb] | 470 | relink_rq(priority);
|
---|
| 471 |
|
---|
| 472 | /*
|
---|
[4e33b6b] | 473 | * If both the old and the new task are the same, lots of work is
|
---|
| 474 | * avoided.
|
---|
[f761f1eb] | 475 | */
|
---|
[43114c5] | 476 | if (TASK != THREAD->task) {
|
---|
[20d50a1] | 477 | as_t *as1 = NULL;
|
---|
| 478 | as_t *as2;
|
---|
[f761f1eb] | 479 |
|
---|
[43114c5] | 480 | if (TASK) {
|
---|
| 481 | spinlock_lock(&TASK->lock);
|
---|
[20d50a1] | 482 | as1 = TASK->as;
|
---|
[43114c5] | 483 | spinlock_unlock(&TASK->lock);
|
---|
[f761f1eb] | 484 | }
|
---|
| 485 |
|
---|
[43114c5] | 486 | spinlock_lock(&THREAD->task->lock);
|
---|
[20d50a1] | 487 | as2 = THREAD->task->as;
|
---|
[43114c5] | 488 | spinlock_unlock(&THREAD->task->lock);
|
---|
[f761f1eb] | 489 |
|
---|
| 490 | /*
|
---|
[4e33b6b] | 491 | * Note that it is possible for two tasks to share one address
|
---|
| 492 | * space.
|
---|
[f761f1eb] | 493 | */
|
---|
[20d50a1] | 494 | if (as1 != as2) {
|
---|
[f761f1eb] | 495 | /*
|
---|
[20d50a1] | 496 | * Both tasks and address spaces are different.
|
---|
[f761f1eb] | 497 | * Replace the old one with the new one.
|
---|
| 498 | */
|
---|
[7e4e532] | 499 | as_switch(as1, as2);
|
---|
[f761f1eb] | 500 | }
|
---|
[f76fed4] | 501 | TASK = THREAD->task;
|
---|
[39cea6a] | 502 | before_task_runs();
|
---|
[f761f1eb] | 503 | }
|
---|
| 504 |
|
---|
[1068f6a] | 505 | spinlock_lock(&THREAD->lock);
|
---|
[43114c5] | 506 | THREAD->state = Running;
|
---|
[f761f1eb] | 507 |
|
---|
[f76fed4] | 508 | #ifdef SCHEDULER_VERBOSE
|
---|
[1e9d0e3] | 509 | printf("cpu%u: tid %" PRIu64 " (priority=%d, ticks=%" PRIu64
|
---|
| 510 | ", nrdy=%ld)\n", CPU->id, THREAD->tid, THREAD->priority,
|
---|
| 511 | THREAD->ticks, atomic_get(&CPU->nrdy));
|
---|
[f76fed4] | 512 | #endif
|
---|
[f761f1eb] | 513 |
|
---|
[97f1691] | 514 | /*
|
---|
| 515 | * Some architectures provide late kernel PA2KA(identity)
|
---|
| 516 | * mapping in a page fault handler. However, the page fault
|
---|
| 517 | * handler uses the kernel stack of the running thread and
|
---|
| 518 | * therefore cannot be used to map it. The kernel stack, if
|
---|
| 519 | * necessary, is to be mapped in before_thread_runs(). This
|
---|
| 520 | * function must be executed before the switch to the new stack.
|
---|
| 521 | */
|
---|
| 522 | before_thread_runs();
|
---|
| 523 |
|
---|
[3e1607f] | 524 | /*
|
---|
[4e33b6b] | 525 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
|
---|
| 526 | * thread's stack.
|
---|
[3e1607f] | 527 | */
|
---|
[bcdd9aa] | 528 | the_copy(THE, (the_t *) THREAD->kstack);
|
---|
| 529 |
|
---|
[43114c5] | 530 | context_restore(&THREAD->saved_context);
|
---|
[f761f1eb] | 531 | /* not reached */
|
---|
| 532 | }
|
---|
| 533 |
|
---|
[5f85c91] | 534 | #ifdef CONFIG_SMP
|
---|
[70527f1] | 535 | /** Load balancing thread
|
---|
| 536 | *
|
---|
| 537 | * SMP load balancing thread, supervising thread supplies
|
---|
| 538 | * for the CPU it's wired to.
|
---|
| 539 | *
|
---|
| 540 | * @param arg Generic thread argument (unused).
|
---|
| 541 | *
|
---|
[f761f1eb] | 542 | */
|
---|
| 543 | void kcpulb(void *arg)
|
---|
| 544 | {
|
---|
| 545 | thread_t *t;
|
---|
[228666c] | 546 | int count;
|
---|
| 547 | atomic_count_t average;
|
---|
[4184e76] | 548 | unsigned int i;
|
---|
[228666c] | 549 | int j;
|
---|
| 550 | int k = 0;
|
---|
[22f7769] | 551 | ipl_t ipl;
|
---|
[f761f1eb] | 552 |
|
---|
[2cb5e64] | 553 | /*
|
---|
| 554 | * Detach kcpulb as nobody will call thread_join_timeout() on it.
|
---|
| 555 | */
|
---|
| 556 | thread_detach(THREAD);
|
---|
| 557 |
|
---|
[f761f1eb] | 558 | loop:
|
---|
| 559 | /*
|
---|
[3260ada] | 560 | * Work in 1s intervals.
|
---|
[f761f1eb] | 561 | */
|
---|
[3260ada] | 562 | thread_sleep(1);
|
---|
[f761f1eb] | 563 |
|
---|
| 564 | not_satisfied:
|
---|
| 565 | /*
|
---|
| 566 | * Calculate the number of threads that will be migrated/stolen from
|
---|
| 567 | * other CPU's. Note that situation can have changed between two
|
---|
| 568 | * passes. Each time get the most up to date counts.
|
---|
| 569 | */
|
---|
[444ec64] | 570 | average = atomic_get(&nrdy) / config.cpu_active + 1;
|
---|
[248fc1a] | 571 | count = average - atomic_get(&CPU->nrdy);
|
---|
[f761f1eb] | 572 |
|
---|
[444ec64] | 573 | if (count <= 0)
|
---|
[f761f1eb] | 574 | goto satisfied;
|
---|
| 575 |
|
---|
| 576 | /*
|
---|
[4e33b6b] | 577 | * Searching least priority queues on all CPU's first and most priority
|
---|
| 578 | * queues on all CPU's last.
|
---|
[f761f1eb] | 579 | */
|
---|
[ea63704] | 580 | for (j = RQ_COUNT - 1; j >= 0; j--) {
|
---|
[4e33b6b] | 581 | for (i = 0; i < config.cpu_active; i++) {
|
---|
[f761f1eb] | 582 | link_t *l;
|
---|
| 583 | runq_t *r;
|
---|
| 584 | cpu_t *cpu;
|
---|
| 585 |
|
---|
| 586 | cpu = &cpus[(i + k) % config.cpu_active];
|
---|
| 587 |
|
---|
| 588 | /*
|
---|
| 589 | * Not interested in ourselves.
|
---|
[4e33b6b] | 590 | * Doesn't require interrupt disabling for kcpulb has
|
---|
| 591 | * THREAD_FLAG_WIRED.
|
---|
[f761f1eb] | 592 | */
|
---|
[43114c5] | 593 | if (CPU == cpu)
|
---|
[248fc1a] | 594 | continue;
|
---|
| 595 | if (atomic_get(&cpu->nrdy) <= average)
|
---|
| 596 | continue;
|
---|
[f761f1eb] | 597 |
|
---|
[444ec64] | 598 | ipl = interrupts_disable();
|
---|
[18e0a6c] | 599 | r = &cpu->rq[j];
|
---|
[f761f1eb] | 600 | spinlock_lock(&r->lock);
|
---|
| 601 | if (r->n == 0) {
|
---|
| 602 | spinlock_unlock(&r->lock);
|
---|
[22f7769] | 603 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 604 | continue;
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | t = NULL;
|
---|
| 608 | l = r->rq_head.prev; /* search rq from the back */
|
---|
| 609 | while (l != &r->rq_head) {
|
---|
| 610 | t = list_get_instance(l, thread_t, rq_link);
|
---|
| 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.
|
---|
[6a27d63] | 618 | */
|
---|
[f761f1eb] | 619 | spinlock_lock(&t->lock);
|
---|
[4e33b6b] | 620 | if ((!(t->flags & (THREAD_FLAG_WIRED |
|
---|
[ea63704] | 621 | THREAD_FLAG_STOLEN))) &&
|
---|
| 622 | (!(t->fpu_context_engaged))) {
|
---|
[f761f1eb] | 623 | /*
|
---|
| 624 | * Remove t from r.
|
---|
| 625 | */
|
---|
| 626 | spinlock_unlock(&t->lock);
|
---|
| 627 |
|
---|
[248fc1a] | 628 | atomic_dec(&cpu->nrdy);
|
---|
[59e07c91] | 629 | atomic_dec(&nrdy);
|
---|
[f761f1eb] | 630 |
|
---|
[76cec1e] | 631 | r->n--;
|
---|
[f761f1eb] | 632 | list_remove(&t->rq_link);
|
---|
| 633 |
|
---|
| 634 | break;
|
---|
| 635 | }
|
---|
| 636 | spinlock_unlock(&t->lock);
|
---|
| 637 | l = l->prev;
|
---|
| 638 | t = NULL;
|
---|
| 639 | }
|
---|
| 640 | spinlock_unlock(&r->lock);
|
---|
| 641 |
|
---|
| 642 | if (t) {
|
---|
| 643 | /*
|
---|
| 644 | * Ready t on local CPU
|
---|
| 645 | */
|
---|
| 646 | spinlock_lock(&t->lock);
|
---|
[f76fed4] | 647 | #ifdef KCPULB_VERBOSE
|
---|
[1e9d0e3] | 648 | printf("kcpulb%u: TID %" PRIu64 " -> cpu%u, "
|
---|
| 649 | "nrdy=%ld, avg=%ld\n", CPU->id, t->tid,
|
---|
| 650 | CPU->id, atomic_get(&CPU->nrdy),
|
---|
[6f4495f5] | 651 | atomic_get(&nrdy) / config.cpu_active);
|
---|
[f76fed4] | 652 | #endif
|
---|
[32fffef0] | 653 | t->flags |= THREAD_FLAG_STOLEN;
|
---|
[a0bb10ef] | 654 | t->state = Entering;
|
---|
[f761f1eb] | 655 | spinlock_unlock(&t->lock);
|
---|
| 656 |
|
---|
| 657 | thread_ready(t);
|
---|
| 658 |
|
---|
[22f7769] | 659 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 660 |
|
---|
| 661 | if (--count == 0)
|
---|
| 662 | goto satisfied;
|
---|
| 663 |
|
---|
| 664 | /*
|
---|
[4e33b6b] | 665 | * We are not satisfied yet, focus on another
|
---|
| 666 | * CPU next time.
|
---|
[f761f1eb] | 667 | */
|
---|
| 668 | k++;
|
---|
| 669 |
|
---|
| 670 | continue;
|
---|
| 671 | }
|
---|
[22f7769] | 672 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 673 | }
|
---|
| 674 | }
|
---|
| 675 |
|
---|
[248fc1a] | 676 | if (atomic_get(&CPU->nrdy)) {
|
---|
[f761f1eb] | 677 | /*
|
---|
| 678 | * Be a little bit light-weight and let migrated threads run.
|
---|
| 679 | */
|
---|
| 680 | scheduler();
|
---|
[3260ada] | 681 | } else {
|
---|
[f761f1eb] | 682 | /*
|
---|
| 683 | * We failed to migrate a single thread.
|
---|
[3260ada] | 684 | * Give up this turn.
|
---|
[f761f1eb] | 685 | */
|
---|
[3260ada] | 686 | goto loop;
|
---|
[f761f1eb] | 687 | }
|
---|
| 688 |
|
---|
| 689 | goto not_satisfied;
|
---|
[76cec1e] | 690 |
|
---|
[f761f1eb] | 691 | satisfied:
|
---|
| 692 | goto loop;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
[5f85c91] | 695 | #endif /* CONFIG_SMP */
|
---|
[10e16a7] | 696 |
|
---|
| 697 |
|
---|
| 698 | /** Print information about threads & scheduler queues */
|
---|
| 699 | void sched_print_list(void)
|
---|
| 700 | {
|
---|
| 701 | ipl_t ipl;
|
---|
[4184e76] | 702 | unsigned int cpu, i;
|
---|
[10e16a7] | 703 | runq_t *r;
|
---|
| 704 | thread_t *t;
|
---|
| 705 | link_t *cur;
|
---|
| 706 |
|
---|
| 707 | /* We are going to mess with scheduler structures,
|
---|
| 708 | * let's not be interrupted */
|
---|
| 709 | ipl = interrupts_disable();
|
---|
[4184e76] | 710 | for (cpu = 0; cpu < config.cpu_count; cpu++) {
|
---|
[7d6ec87] | 711 |
|
---|
[10e16a7] | 712 | if (!cpus[cpu].active)
|
---|
| 713 | continue;
|
---|
[7d6ec87] | 714 |
|
---|
[10e16a7] | 715 | spinlock_lock(&cpus[cpu].lock);
|
---|
[98000fb] | 716 | printf("cpu%u: address=%p, nrdy=%ld, needs_relink=%" PRIs "\n",
|
---|
[6f4495f5] | 717 | cpus[cpu].id, &cpus[cpu], atomic_get(&cpus[cpu].nrdy),
|
---|
| 718 | cpus[cpu].needs_relink);
|
---|
[10e16a7] | 719 |
|
---|
[4e33b6b] | 720 | for (i = 0; i < RQ_COUNT; i++) {
|
---|
[10e16a7] | 721 | r = &cpus[cpu].rq[i];
|
---|
| 722 | spinlock_lock(&r->lock);
|
---|
| 723 | if (!r->n) {
|
---|
| 724 | spinlock_unlock(&r->lock);
|
---|
| 725 | continue;
|
---|
| 726 | }
|
---|
[5b86d10] | 727 | printf("\trq[%u]: ", i);
|
---|
[4e33b6b] | 728 | for (cur = r->rq_head.next; cur != &r->rq_head;
|
---|
| 729 | cur = cur->next) {
|
---|
[10e16a7] | 730 | t = list_get_instance(cur, thread_t, rq_link);
|
---|
[5b86d10] | 731 | printf("%" PRIu64 "(%s) ", t->tid,
|
---|
[6f4495f5] | 732 | thread_states[t->state]);
|
---|
[10e16a7] | 733 | }
|
---|
| 734 | printf("\n");
|
---|
| 735 | spinlock_unlock(&r->lock);
|
---|
| 736 | }
|
---|
| 737 | spinlock_unlock(&cpus[cpu].lock);
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | interrupts_restore(ipl);
|
---|
| 741 | }
|
---|
[b45c443] | 742 |
|
---|
[cc73a8a1] | 743 | /** @}
|
---|
[b45c443] | 744 | */
|
---|