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