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