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