[f761f1eb] | 1 | /*
|
---|
| 2 | * Copyright (C) 2001-2004 Jakub Jermar
|
---|
| 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 |
|
---|
[9179d0a] | 29 | /**
|
---|
| 30 | * @file scheduler.c
|
---|
| 31 | * @brief Scheduler and load balancing.
|
---|
| 32 | *
|
---|
[cf26ba9] | 33 | * This file contains the scheduler and kcpulb kernel thread which
|
---|
[9179d0a] | 34 | * performs load-balancing of per-CPU run queues.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[f761f1eb] | 37 | #include <proc/scheduler.h>
|
---|
| 38 | #include <proc/thread.h>
|
---|
| 39 | #include <proc/task.h>
|
---|
[32ff43e6] | 40 | #include <mm/frame.h>
|
---|
| 41 | #include <mm/page.h>
|
---|
[20d50a1] | 42 | #include <mm/as.h>
|
---|
[32ff43e6] | 43 | #include <arch/asm.h>
|
---|
| 44 | #include <arch/faddr.h>
|
---|
[23684b7] | 45 | #include <atomic.h>
|
---|
[32ff43e6] | 46 | #include <synch/spinlock.h>
|
---|
[f761f1eb] | 47 | #include <config.h>
|
---|
| 48 | #include <context.h>
|
---|
| 49 | #include <func.h>
|
---|
| 50 | #include <arch.h>
|
---|
[5c9a08b] | 51 | #include <adt/list.h>
|
---|
[02a99d2] | 52 | #include <panic.h>
|
---|
[f761f1eb] | 53 | #include <typedefs.h>
|
---|
[32ff43e6] | 54 | #include <cpu.h>
|
---|
[9c0a9b3] | 55 | #include <print.h>
|
---|
[623ba26c] | 56 | #include <debug.h>
|
---|
[9c0a9b3] | 57 |
|
---|
[39cea6a] | 58 | static void before_task_runs(void);
|
---|
| 59 | static void before_thread_runs(void);
|
---|
| 60 | static void after_thread_ran(void);
|
---|
[7d6ec87] | 61 | static void scheduler_separated_stack(void);
|
---|
| 62 |
|
---|
| 63 | atomic_t nrdy; /**< Number of ready threads in the system. */
|
---|
[f761f1eb] | 64 |
|
---|
[39cea6a] | 65 | /** Carry out actions before new task runs. */
|
---|
| 66 | void before_task_runs(void)
|
---|
| 67 | {
|
---|
| 68 | before_task_runs_arch();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[97f1691] | 71 | /** Take actions before new thread runs.
|
---|
[70527f1] | 72 | *
|
---|
[b60a22c] | 73 | * Perform actions that need to be
|
---|
| 74 | * taken before the newly selected
|
---|
| 75 | * tread is passed control.
|
---|
[70527f1] | 76 | *
|
---|
[a3eeceb6] | 77 | * THREAD->lock is locked on entry
|
---|
| 78 | *
|
---|
[70527f1] | 79 | */
|
---|
[0ca6faa] | 80 | void before_thread_runs(void)
|
---|
| 81 | {
|
---|
[b49f4ae] | 82 | before_thread_runs_arch();
|
---|
[f76fed4] | 83 | #ifdef CONFIG_FPU_LAZY
|
---|
[b49f4ae] | 84 | if(THREAD==CPU->fpu_owner)
|
---|
| 85 | fpu_enable();
|
---|
| 86 | else
|
---|
| 87 | fpu_disable();
|
---|
[f76fed4] | 88 | #else
|
---|
[b49f4ae] | 89 | fpu_enable();
|
---|
| 90 | if (THREAD->fpu_context_exists)
|
---|
[f76fed4] | 91 | fpu_context_restore(THREAD->saved_fpu_context);
|
---|
[b49f4ae] | 92 | else {
|
---|
[f76fed4] | 93 | fpu_init();
|
---|
[b49f4ae] | 94 | THREAD->fpu_context_exists=1;
|
---|
| 95 | }
|
---|
[f76fed4] | 96 | #endif
|
---|
[0ca6faa] | 97 | }
|
---|
| 98 |
|
---|
[7d6ec87] | 99 | /** Take actions after THREAD had run.
|
---|
[97f1691] | 100 | *
|
---|
| 101 | * Perform actions that need to be
|
---|
| 102 | * taken after the running thread
|
---|
[7d6ec87] | 103 | * had been preempted by the scheduler.
|
---|
[97f1691] | 104 | *
|
---|
| 105 | * THREAD->lock is locked on entry
|
---|
| 106 | *
|
---|
| 107 | */
|
---|
| 108 | void after_thread_ran(void)
|
---|
| 109 | {
|
---|
| 110 | after_thread_ran_arch();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[5f85c91] | 113 | #ifdef CONFIG_FPU_LAZY
|
---|
[b49f4ae] | 114 | void scheduler_fpu_lazy_request(void)
|
---|
| 115 | {
|
---|
[09c18f7] | 116 | restart:
|
---|
[b49f4ae] | 117 | fpu_enable();
|
---|
[a3eeceb6] | 118 | spinlock_lock(&CPU->lock);
|
---|
| 119 |
|
---|
| 120 | /* Save old context */
|
---|
[b49f4ae] | 121 | if (CPU->fpu_owner != NULL) {
|
---|
[a3eeceb6] | 122 | spinlock_lock(&CPU->fpu_owner->lock);
|
---|
[f76fed4] | 123 | fpu_context_save(CPU->fpu_owner->saved_fpu_context);
|
---|
[b49f4ae] | 124 | /* don't prevent migration */
|
---|
| 125 | CPU->fpu_owner->fpu_context_engaged=0;
|
---|
[a3eeceb6] | 126 | spinlock_unlock(&CPU->fpu_owner->lock);
|
---|
[09c18f7] | 127 | CPU->fpu_owner = NULL;
|
---|
[b49f4ae] | 128 | }
|
---|
[a3eeceb6] | 129 |
|
---|
| 130 | spinlock_lock(&THREAD->lock);
|
---|
[7d6ec87] | 131 | if (THREAD->fpu_context_exists) {
|
---|
[f76fed4] | 132 | fpu_context_restore(THREAD->saved_fpu_context);
|
---|
[7d6ec87] | 133 | } else {
|
---|
[f76fed4] | 134 | /* Allocate FPU context */
|
---|
| 135 | if (!THREAD->saved_fpu_context) {
|
---|
| 136 | /* Might sleep */
|
---|
| 137 | spinlock_unlock(&THREAD->lock);
|
---|
[09c18f7] | 138 | spinlock_unlock(&CPU->lock);
|
---|
[f76fed4] | 139 | THREAD->saved_fpu_context = slab_alloc(fpu_context_slab,
|
---|
| 140 | 0);
|
---|
[09c18f7] | 141 | /* We may have switched CPUs during slab_alloc */
|
---|
| 142 | goto restart;
|
---|
[f76fed4] | 143 | }
|
---|
| 144 | fpu_init();
|
---|
[b49f4ae] | 145 | THREAD->fpu_context_exists=1;
|
---|
| 146 | }
|
---|
| 147 | CPU->fpu_owner=THREAD;
|
---|
| 148 | THREAD->fpu_context_engaged = 1;
|
---|
[a3eeceb6] | 149 | spinlock_unlock(&THREAD->lock);
|
---|
[7d6ec87] | 150 |
|
---|
[a3eeceb6] | 151 | spinlock_unlock(&CPU->lock);
|
---|
[b49f4ae] | 152 | }
|
---|
| 153 | #endif
|
---|
[0ca6faa] | 154 |
|
---|
[70527f1] | 155 | /** Initialize scheduler
|
---|
| 156 | *
|
---|
| 157 | * Initialize kernel scheduler.
|
---|
| 158 | *
|
---|
| 159 | */
|
---|
[f761f1eb] | 160 | void scheduler_init(void)
|
---|
| 161 | {
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[70527f1] | 164 | /** Get thread to be scheduled
|
---|
| 165 | *
|
---|
| 166 | * Get the optimal thread to be scheduled
|
---|
[d1a184f] | 167 | * according to thread accounting and scheduler
|
---|
[70527f1] | 168 | * policy.
|
---|
| 169 | *
|
---|
| 170 | * @return Thread to be scheduled.
|
---|
| 171 | *
|
---|
| 172 | */
|
---|
[e507afa] | 173 | static thread_t *find_best_thread(void)
|
---|
[f761f1eb] | 174 | {
|
---|
| 175 | thread_t *t;
|
---|
| 176 | runq_t *r;
|
---|
[248fc1a] | 177 | int i;
|
---|
[f761f1eb] | 178 |
|
---|
[623ba26c] | 179 | ASSERT(CPU != NULL);
|
---|
| 180 |
|
---|
[f761f1eb] | 181 | loop:
|
---|
[22f7769] | 182 | interrupts_enable();
|
---|
[f761f1eb] | 183 |
|
---|
[248fc1a] | 184 | if (atomic_get(&CPU->nrdy) == 0) {
|
---|
[f761f1eb] | 185 | /*
|
---|
| 186 | * For there was nothing to run, the CPU goes to sleep
|
---|
| 187 | * until a hardware interrupt or an IPI comes.
|
---|
| 188 | * This improves energy saving and hyperthreading.
|
---|
| 189 | */
|
---|
[328e0d3] | 190 |
|
---|
| 191 | /*
|
---|
| 192 | * An interrupt might occur right now and wake up a thread.
|
---|
| 193 | * In such case, the CPU will continue to go to sleep
|
---|
| 194 | * even though there is a runnable thread.
|
---|
| 195 | */
|
---|
| 196 |
|
---|
[f761f1eb] | 197 | cpu_sleep();
|
---|
| 198 | goto loop;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[22f7769] | 201 | interrupts_disable();
|
---|
[d896525] | 202 |
|
---|
[7d6ec87] | 203 | for (i = 0; i<RQ_COUNT; i++) {
|
---|
[43114c5] | 204 | r = &CPU->rq[i];
|
---|
[f761f1eb] | 205 | spinlock_lock(&r->lock);
|
---|
| 206 | if (r->n == 0) {
|
---|
| 207 | /*
|
---|
| 208 | * If this queue is empty, try a lower-priority queue.
|
---|
| 209 | */
|
---|
| 210 | spinlock_unlock(&r->lock);
|
---|
| 211 | continue;
|
---|
| 212 | }
|
---|
[3e1607f] | 213 |
|
---|
[248fc1a] | 214 | atomic_dec(&CPU->nrdy);
|
---|
[59e07c91] | 215 | atomic_dec(&nrdy);
|
---|
[f761f1eb] | 216 | r->n--;
|
---|
| 217 |
|
---|
| 218 | /*
|
---|
| 219 | * Take the first thread from the queue.
|
---|
| 220 | */
|
---|
| 221 | t = list_get_instance(r->rq_head.next, thread_t, rq_link);
|
---|
| 222 | list_remove(&t->rq_link);
|
---|
| 223 |
|
---|
| 224 | spinlock_unlock(&r->lock);
|
---|
| 225 |
|
---|
| 226 | spinlock_lock(&t->lock);
|
---|
[43114c5] | 227 | t->cpu = CPU;
|
---|
[f761f1eb] | 228 |
|
---|
| 229 | t->ticks = us2ticks((i+1)*10000);
|
---|
[7d6ec87] | 230 | t->priority = i; /* correct rq index */
|
---|
[f761f1eb] | 231 |
|
---|
| 232 | /*
|
---|
| 233 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge.
|
---|
| 234 | */
|
---|
| 235 | t->flags &= ~X_STOLEN;
|
---|
| 236 | spinlock_unlock(&t->lock);
|
---|
| 237 |
|
---|
| 238 | return t;
|
---|
| 239 | }
|
---|
| 240 | goto loop;
|
---|
| 241 |
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[70527f1] | 244 | /** Prevent rq starvation
|
---|
| 245 | *
|
---|
| 246 | * Prevent low priority threads from starving in rq's.
|
---|
| 247 | *
|
---|
| 248 | * When the function decides to relink rq's, it reconnects
|
---|
| 249 | * respective pointers so that in result threads with 'pri'
|
---|
[7f0837c] | 250 | * greater or equal @start are moved to a higher-priority queue.
|
---|
[70527f1] | 251 | *
|
---|
| 252 | * @param start Threshold priority.
|
---|
| 253 | *
|
---|
[f761f1eb] | 254 | */
|
---|
[e16e036a] | 255 | static void relink_rq(int start)
|
---|
[f761f1eb] | 256 | {
|
---|
| 257 | link_t head;
|
---|
| 258 | runq_t *r;
|
---|
| 259 | int i, n;
|
---|
| 260 |
|
---|
| 261 | list_initialize(&head);
|
---|
[43114c5] | 262 | spinlock_lock(&CPU->lock);
|
---|
| 263 | if (CPU->needs_relink > NEEDS_RELINK_MAX) {
|
---|
[f761f1eb] | 264 | for (i = start; i<RQ_COUNT-1; i++) {
|
---|
| 265 | /* remember and empty rq[i + 1] */
|
---|
[43114c5] | 266 | r = &CPU->rq[i + 1];
|
---|
[f761f1eb] | 267 | spinlock_lock(&r->lock);
|
---|
| 268 | list_concat(&head, &r->rq_head);
|
---|
| 269 | n = r->n;
|
---|
| 270 | r->n = 0;
|
---|
| 271 | spinlock_unlock(&r->lock);
|
---|
| 272 |
|
---|
| 273 | /* append rq[i + 1] to rq[i] */
|
---|
[43114c5] | 274 | r = &CPU->rq[i];
|
---|
[f761f1eb] | 275 | spinlock_lock(&r->lock);
|
---|
| 276 | list_concat(&r->rq_head, &head);
|
---|
| 277 | r->n += n;
|
---|
| 278 | spinlock_unlock(&r->lock);
|
---|
| 279 | }
|
---|
[43114c5] | 280 | CPU->needs_relink = 0;
|
---|
[f761f1eb] | 281 | }
|
---|
[444ec64] | 282 | spinlock_unlock(&CPU->lock);
|
---|
[f761f1eb] | 283 |
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[7d6ec87] | 286 | /** The scheduler
|
---|
| 287 | *
|
---|
| 288 | * The thread scheduling procedure.
|
---|
| 289 | * Passes control directly to
|
---|
| 290 | * scheduler_separated_stack().
|
---|
| 291 | *
|
---|
| 292 | */
|
---|
| 293 | void scheduler(void)
|
---|
| 294 | {
|
---|
| 295 | volatile ipl_t ipl;
|
---|
| 296 |
|
---|
| 297 | ASSERT(CPU != NULL);
|
---|
| 298 |
|
---|
| 299 | ipl = interrupts_disable();
|
---|
| 300 |
|
---|
| 301 | if (atomic_get(&haltstate))
|
---|
| 302 | halt();
|
---|
[8965838e] | 303 |
|
---|
[7d6ec87] | 304 | if (THREAD) {
|
---|
| 305 | spinlock_lock(&THREAD->lock);
|
---|
[f76fed4] | 306 | #ifndef CONFIG_FPU_LAZY
|
---|
| 307 | fpu_context_save(THREAD->saved_fpu_context);
|
---|
| 308 | #endif
|
---|
[7d6ec87] | 309 | if (!context_save(&THREAD->saved_context)) {
|
---|
| 310 | /*
|
---|
| 311 | * This is the place where threads leave scheduler();
|
---|
| 312 | */
|
---|
| 313 | spinlock_unlock(&THREAD->lock);
|
---|
| 314 | interrupts_restore(THREAD->saved_context.ipl);
|
---|
[8965838e] | 315 |
|
---|
[7d6ec87] | 316 | return;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /*
|
---|
| 320 | * Interrupt priority level of preempted thread is recorded here
|
---|
| 321 | * to facilitate scheduler() invocations from interrupts_disable()'d
|
---|
| 322 | * code (e.g. waitq_sleep_timeout()).
|
---|
| 323 | */
|
---|
| 324 | THREAD->saved_context.ipl = ipl;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | /*
|
---|
| 328 | * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM
|
---|
| 329 | * and preemption counter. At this point THE could be coming either
|
---|
| 330 | * from THREAD's or CPU's stack.
|
---|
| 331 | */
|
---|
| 332 | the_copy(THE, (the_t *) CPU->stack);
|
---|
| 333 |
|
---|
| 334 | /*
|
---|
| 335 | * We may not keep the old stack.
|
---|
| 336 | * Reason: If we kept the old stack and got blocked, for instance, in
|
---|
| 337 | * find_best_thread(), the old thread could get rescheduled by another
|
---|
| 338 | * CPU and overwrite the part of its own stack that was also used by
|
---|
| 339 | * the scheduler on this CPU.
|
---|
| 340 | *
|
---|
| 341 | * Moreover, we have to bypass the compiler-generated POP sequence
|
---|
| 342 | * which is fooled by SP being set to the very top of the stack.
|
---|
| 343 | * Therefore the scheduler() function continues in
|
---|
| 344 | * scheduler_separated_stack().
|
---|
| 345 | */
|
---|
| 346 | context_save(&CPU->saved_context);
|
---|
| 347 | context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
|
---|
| 348 | context_restore(&CPU->saved_context);
|
---|
| 349 | /* not reached */
|
---|
| 350 | }
|
---|
[70527f1] | 351 |
|
---|
| 352 | /** Scheduler stack switch wrapper
|
---|
| 353 | *
|
---|
| 354 | * Second part of the scheduler() function
|
---|
| 355 | * using new stack. Handling the actual context
|
---|
| 356 | * switch to a new thread.
|
---|
| 357 | *
|
---|
[266294a9] | 358 | * Assume THREAD->lock is held.
|
---|
[70527f1] | 359 | */
|
---|
[7d6ec87] | 360 | void scheduler_separated_stack(void)
|
---|
[f761f1eb] | 361 | {
|
---|
| 362 | int priority;
|
---|
[8965838e] | 363 |
|
---|
[623ba26c] | 364 | ASSERT(CPU != NULL);
|
---|
[8965838e] | 365 |
|
---|
[43114c5] | 366 | if (THREAD) {
|
---|
[7d6ec87] | 367 | /* must be run after the switch to scheduler stack */
|
---|
[97f1691] | 368 | after_thread_ran();
|
---|
| 369 |
|
---|
[43114c5] | 370 | switch (THREAD->state) {
|
---|
[f761f1eb] | 371 | case Running:
|
---|
[76cec1e] | 372 | spinlock_unlock(&THREAD->lock);
|
---|
| 373 | thread_ready(THREAD);
|
---|
| 374 | break;
|
---|
[f761f1eb] | 375 |
|
---|
| 376 | case Exiting:
|
---|
[266294a9] | 377 | thread_destroy(THREAD);
|
---|
[76cec1e] | 378 | break;
|
---|
[266294a9] | 379 |
|
---|
[f761f1eb] | 380 | case Sleeping:
|
---|
[76cec1e] | 381 | /*
|
---|
| 382 | * Prefer the thread after it's woken up.
|
---|
| 383 | */
|
---|
[22f7769] | 384 | THREAD->priority = -1;
|
---|
[76cec1e] | 385 |
|
---|
| 386 | /*
|
---|
| 387 | * We need to release wq->lock which we locked in waitq_sleep().
|
---|
| 388 | * Address of wq->lock is kept in THREAD->sleep_queue.
|
---|
| 389 | */
|
---|
| 390 | spinlock_unlock(&THREAD->sleep_queue->lock);
|
---|
| 391 |
|
---|
| 392 | /*
|
---|
| 393 | * Check for possible requests for out-of-context invocation.
|
---|
| 394 | */
|
---|
| 395 | if (THREAD->call_me) {
|
---|
| 396 | THREAD->call_me(THREAD->call_me_with);
|
---|
| 397 | THREAD->call_me = NULL;
|
---|
| 398 | THREAD->call_me_with = NULL;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | spinlock_unlock(&THREAD->lock);
|
---|
| 402 |
|
---|
| 403 | break;
|
---|
[f761f1eb] | 404 |
|
---|
| 405 | default:
|
---|
[76cec1e] | 406 | /*
|
---|
| 407 | * Entering state is unexpected.
|
---|
| 408 | */
|
---|
| 409 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]);
|
---|
| 410 | break;
|
---|
[f761f1eb] | 411 | }
|
---|
[97f1691] | 412 |
|
---|
[43114c5] | 413 | THREAD = NULL;
|
---|
[f761f1eb] | 414 | }
|
---|
[ba18512] | 415 |
|
---|
[43114c5] | 416 | THREAD = find_best_thread();
|
---|
[f761f1eb] | 417 |
|
---|
[43114c5] | 418 | spinlock_lock(&THREAD->lock);
|
---|
[22f7769] | 419 | priority = THREAD->priority;
|
---|
[43114c5] | 420 | spinlock_unlock(&THREAD->lock);
|
---|
[7ce9284] | 421 |
|
---|
[f761f1eb] | 422 | relink_rq(priority);
|
---|
| 423 |
|
---|
[43114c5] | 424 | spinlock_lock(&THREAD->lock);
|
---|
[f761f1eb] | 425 |
|
---|
| 426 | /*
|
---|
| 427 | * If both the old and the new task are the same, lots of work is avoided.
|
---|
| 428 | */
|
---|
[43114c5] | 429 | if (TASK != THREAD->task) {
|
---|
[20d50a1] | 430 | as_t *as1 = NULL;
|
---|
| 431 | as_t *as2;
|
---|
[f761f1eb] | 432 |
|
---|
[43114c5] | 433 | if (TASK) {
|
---|
| 434 | spinlock_lock(&TASK->lock);
|
---|
[20d50a1] | 435 | as1 = TASK->as;
|
---|
[43114c5] | 436 | spinlock_unlock(&TASK->lock);
|
---|
[f761f1eb] | 437 | }
|
---|
| 438 |
|
---|
[43114c5] | 439 | spinlock_lock(&THREAD->task->lock);
|
---|
[20d50a1] | 440 | as2 = THREAD->task->as;
|
---|
[43114c5] | 441 | spinlock_unlock(&THREAD->task->lock);
|
---|
[f761f1eb] | 442 |
|
---|
| 443 | /*
|
---|
[20d50a1] | 444 | * Note that it is possible for two tasks to share one address space.
|
---|
[f761f1eb] | 445 | */
|
---|
[20d50a1] | 446 | if (as1 != as2) {
|
---|
[f761f1eb] | 447 | /*
|
---|
[20d50a1] | 448 | * Both tasks and address spaces are different.
|
---|
[f761f1eb] | 449 | * Replace the old one with the new one.
|
---|
| 450 | */
|
---|
[7e4e532] | 451 | as_switch(as1, as2);
|
---|
[f761f1eb] | 452 | }
|
---|
[f76fed4] | 453 | TASK = THREAD->task;
|
---|
[39cea6a] | 454 | before_task_runs();
|
---|
[f761f1eb] | 455 | }
|
---|
| 456 |
|
---|
[43114c5] | 457 | THREAD->state = Running;
|
---|
[f761f1eb] | 458 |
|
---|
[f76fed4] | 459 | #ifdef SCHEDULER_VERBOSE
|
---|
[280a27e] | 460 | printf("cpu%d: tid %d (priority=%d,ticks=%lld,nrdy=%ld)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, atomic_get(&CPU->nrdy));
|
---|
[f76fed4] | 461 | #endif
|
---|
[f761f1eb] | 462 |
|
---|
[97f1691] | 463 | /*
|
---|
| 464 | * Some architectures provide late kernel PA2KA(identity)
|
---|
| 465 | * mapping in a page fault handler. However, the page fault
|
---|
| 466 | * handler uses the kernel stack of the running thread and
|
---|
| 467 | * therefore cannot be used to map it. The kernel stack, if
|
---|
| 468 | * necessary, is to be mapped in before_thread_runs(). This
|
---|
| 469 | * function must be executed before the switch to the new stack.
|
---|
| 470 | */
|
---|
| 471 | before_thread_runs();
|
---|
| 472 |
|
---|
[3e1607f] | 473 | /*
|
---|
| 474 | * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack.
|
---|
| 475 | */
|
---|
[bcdd9aa] | 476 | the_copy(THE, (the_t *) THREAD->kstack);
|
---|
| 477 |
|
---|
[43114c5] | 478 | context_restore(&THREAD->saved_context);
|
---|
[f761f1eb] | 479 | /* not reached */
|
---|
| 480 | }
|
---|
| 481 |
|
---|
[5f85c91] | 482 | #ifdef CONFIG_SMP
|
---|
[70527f1] | 483 | /** Load balancing thread
|
---|
| 484 | *
|
---|
| 485 | * SMP load balancing thread, supervising thread supplies
|
---|
| 486 | * for the CPU it's wired to.
|
---|
| 487 | *
|
---|
| 488 | * @param arg Generic thread argument (unused).
|
---|
| 489 | *
|
---|
[f761f1eb] | 490 | */
|
---|
| 491 | void kcpulb(void *arg)
|
---|
| 492 | {
|
---|
| 493 | thread_t *t;
|
---|
[248fc1a] | 494 | int count, average, i, j, k = 0;
|
---|
[22f7769] | 495 | ipl_t ipl;
|
---|
[f761f1eb] | 496 |
|
---|
| 497 | loop:
|
---|
| 498 | /*
|
---|
[3260ada] | 499 | * Work in 1s intervals.
|
---|
[f761f1eb] | 500 | */
|
---|
[3260ada] | 501 | thread_sleep(1);
|
---|
[f761f1eb] | 502 |
|
---|
| 503 | not_satisfied:
|
---|
| 504 | /*
|
---|
| 505 | * Calculate the number of threads that will be migrated/stolen from
|
---|
| 506 | * other CPU's. Note that situation can have changed between two
|
---|
| 507 | * passes. Each time get the most up to date counts.
|
---|
| 508 | */
|
---|
[444ec64] | 509 | average = atomic_get(&nrdy) / config.cpu_active + 1;
|
---|
[248fc1a] | 510 | count = average - atomic_get(&CPU->nrdy);
|
---|
[f761f1eb] | 511 |
|
---|
[444ec64] | 512 | if (count <= 0)
|
---|
[f761f1eb] | 513 | goto satisfied;
|
---|
| 514 |
|
---|
| 515 | /*
|
---|
| 516 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last.
|
---|
| 517 | */
|
---|
| 518 | for (j=RQ_COUNT-1; j >= 0; j--) {
|
---|
| 519 | for (i=0; i < config.cpu_active; i++) {
|
---|
| 520 | link_t *l;
|
---|
| 521 | runq_t *r;
|
---|
| 522 | cpu_t *cpu;
|
---|
| 523 |
|
---|
| 524 | cpu = &cpus[(i + k) % config.cpu_active];
|
---|
| 525 |
|
---|
| 526 | /*
|
---|
| 527 | * Not interested in ourselves.
|
---|
| 528 | * Doesn't require interrupt disabling for kcpulb is X_WIRED.
|
---|
| 529 | */
|
---|
[43114c5] | 530 | if (CPU == cpu)
|
---|
[248fc1a] | 531 | continue;
|
---|
| 532 | if (atomic_get(&cpu->nrdy) <= average)
|
---|
| 533 | continue;
|
---|
[f761f1eb] | 534 |
|
---|
[444ec64] | 535 | ipl = interrupts_disable();
|
---|
[18e0a6c] | 536 | r = &cpu->rq[j];
|
---|
[f761f1eb] | 537 | spinlock_lock(&r->lock);
|
---|
| 538 | if (r->n == 0) {
|
---|
| 539 | spinlock_unlock(&r->lock);
|
---|
[22f7769] | 540 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 541 | continue;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | t = NULL;
|
---|
| 545 | l = r->rq_head.prev; /* search rq from the back */
|
---|
| 546 | while (l != &r->rq_head) {
|
---|
| 547 | t = list_get_instance(l, thread_t, rq_link);
|
---|
| 548 | /*
|
---|
[76cec1e] | 549 | * We don't want to steal CPU-wired threads neither threads already stolen.
|
---|
[f761f1eb] | 550 | * The latter prevents threads from migrating between CPU's without ever being run.
|
---|
[76cec1e] | 551 | * We don't want to steal threads whose FPU context is still in CPU.
|
---|
[6a27d63] | 552 | */
|
---|
[f761f1eb] | 553 | spinlock_lock(&t->lock);
|
---|
[6a27d63] | 554 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) {
|
---|
[f761f1eb] | 555 | /*
|
---|
| 556 | * Remove t from r.
|
---|
| 557 | */
|
---|
| 558 | spinlock_unlock(&t->lock);
|
---|
| 559 |
|
---|
[248fc1a] | 560 | atomic_dec(&cpu->nrdy);
|
---|
[59e07c91] | 561 | atomic_dec(&nrdy);
|
---|
[f761f1eb] | 562 |
|
---|
[76cec1e] | 563 | r->n--;
|
---|
[f761f1eb] | 564 | list_remove(&t->rq_link);
|
---|
| 565 |
|
---|
| 566 | break;
|
---|
| 567 | }
|
---|
| 568 | spinlock_unlock(&t->lock);
|
---|
| 569 | l = l->prev;
|
---|
| 570 | t = NULL;
|
---|
| 571 | }
|
---|
| 572 | spinlock_unlock(&r->lock);
|
---|
| 573 |
|
---|
| 574 | if (t) {
|
---|
| 575 | /*
|
---|
| 576 | * Ready t on local CPU
|
---|
| 577 | */
|
---|
| 578 | spinlock_lock(&t->lock);
|
---|
[f76fed4] | 579 | #ifdef KCPULB_VERBOSE
|
---|
[280a27e] | 580 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%ld, avg=%nd\n", CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy), atomic_get(&nrdy) / config.cpu_active);
|
---|
[f76fed4] | 581 | #endif
|
---|
[f761f1eb] | 582 | t->flags |= X_STOLEN;
|
---|
[a0bb10ef] | 583 | t->state = Entering;
|
---|
[f761f1eb] | 584 | spinlock_unlock(&t->lock);
|
---|
| 585 |
|
---|
| 586 | thread_ready(t);
|
---|
| 587 |
|
---|
[22f7769] | 588 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 589 |
|
---|
| 590 | if (--count == 0)
|
---|
| 591 | goto satisfied;
|
---|
| 592 |
|
---|
| 593 | /*
|
---|
[76cec1e] | 594 | * We are not satisfied yet, focus on another CPU next time.
|
---|
[f761f1eb] | 595 | */
|
---|
| 596 | k++;
|
---|
| 597 |
|
---|
| 598 | continue;
|
---|
| 599 | }
|
---|
[22f7769] | 600 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 601 | }
|
---|
| 602 | }
|
---|
| 603 |
|
---|
[248fc1a] | 604 | if (atomic_get(&CPU->nrdy)) {
|
---|
[f761f1eb] | 605 | /*
|
---|
| 606 | * Be a little bit light-weight and let migrated threads run.
|
---|
| 607 | */
|
---|
| 608 | scheduler();
|
---|
[3260ada] | 609 | } else {
|
---|
[f761f1eb] | 610 | /*
|
---|
| 611 | * We failed to migrate a single thread.
|
---|
[3260ada] | 612 | * Give up this turn.
|
---|
[f761f1eb] | 613 | */
|
---|
[3260ada] | 614 | goto loop;
|
---|
[f761f1eb] | 615 | }
|
---|
| 616 |
|
---|
| 617 | goto not_satisfied;
|
---|
[76cec1e] | 618 |
|
---|
[f761f1eb] | 619 | satisfied:
|
---|
| 620 | goto loop;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
[5f85c91] | 623 | #endif /* CONFIG_SMP */
|
---|
[10e16a7] | 624 |
|
---|
| 625 |
|
---|
| 626 | /** Print information about threads & scheduler queues */
|
---|
| 627 | void sched_print_list(void)
|
---|
| 628 | {
|
---|
| 629 | ipl_t ipl;
|
---|
| 630 | int cpu,i;
|
---|
| 631 | runq_t *r;
|
---|
| 632 | thread_t *t;
|
---|
| 633 | link_t *cur;
|
---|
| 634 |
|
---|
| 635 | /* We are going to mess with scheduler structures,
|
---|
| 636 | * let's not be interrupted */
|
---|
| 637 | ipl = interrupts_disable();
|
---|
| 638 | for (cpu=0;cpu < config.cpu_count; cpu++) {
|
---|
[7d6ec87] | 639 |
|
---|
[10e16a7] | 640 | if (!cpus[cpu].active)
|
---|
| 641 | continue;
|
---|
[7d6ec87] | 642 |
|
---|
[10e16a7] | 643 | spinlock_lock(&cpus[cpu].lock);
|
---|
[cf85e24c] | 644 | printf("cpu%d: address=%p, nrdy=%ld, needs_relink=%ld\n",
|
---|
[ff14c520] | 645 | cpus[cpu].id, &cpus[cpu], atomic_get(&cpus[cpu].nrdy), cpus[cpu].needs_relink);
|
---|
[10e16a7] | 646 |
|
---|
| 647 | for (i=0; i<RQ_COUNT; i++) {
|
---|
| 648 | r = &cpus[cpu].rq[i];
|
---|
| 649 | spinlock_lock(&r->lock);
|
---|
| 650 | if (!r->n) {
|
---|
| 651 | spinlock_unlock(&r->lock);
|
---|
| 652 | continue;
|
---|
| 653 | }
|
---|
[7d6ec87] | 654 | printf("\trq[%d]: ", i);
|
---|
[10e16a7] | 655 | for (cur=r->rq_head.next; cur!=&r->rq_head; cur=cur->next) {
|
---|
| 656 | t = list_get_instance(cur, thread_t, rq_link);
|
---|
| 657 | printf("%d(%s) ", t->tid,
|
---|
| 658 | thread_states[t->state]);
|
---|
| 659 | }
|
---|
| 660 | printf("\n");
|
---|
| 661 | spinlock_unlock(&r->lock);
|
---|
| 662 | }
|
---|
| 663 | spinlock_unlock(&cpus[cpu].lock);
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | interrupts_restore(ipl);
|
---|
| 667 | }
|
---|