source: mainline/kernel/generic/src/proc/scheduler.c@ 0c2d9bb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0c2d9bb was 0c2d9bb, checked in by Martin Decky <martin@…>, 12 years ago

merge mainline changes

  • Property mode set to 100644
File size: 17.5 KB
RevLine 
[f761f1eb]1/*
[481d4751]2 * Copyright (c) 2010 Jakub Jermar
[f761f1eb]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[cc73a8a1]29/** @addtogroup genericproc
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[da1bafb]35 * @brief Scheduler and load balancing.
[9179d0a]36 *
[cf26ba9]37 * This file contains the scheduler and kcpulb kernel thread which
[9179d0a]38 * performs load-balancing of per-CPU run queues.
39 */
40
[f761f1eb]41#include <proc/scheduler.h>
42#include <proc/thread.h>
43#include <proc/task.h>
[32ff43e6]44#include <mm/frame.h>
45#include <mm/page.h>
[20d50a1]46#include <mm/as.h>
[b3f8fb7]47#include <time/timeout.h>
[fe19611]48#include <time/delay.h>
[32ff43e6]49#include <arch/asm.h>
50#include <arch/faddr.h>
[cce6acf]51#include <arch/cycle.h>
[23684b7]52#include <atomic.h>
[32ff43e6]53#include <synch/spinlock.h>
[f761f1eb]54#include <config.h>
55#include <context.h>
[b3f8fb7]56#include <fpu_context.h>
[f761f1eb]57#include <func.h>
58#include <arch.h>
[5c9a08b]59#include <adt/list.h>
[02a99d2]60#include <panic.h>
[32ff43e6]61#include <cpu.h>
[9c0a9b3]62#include <print.h>
[623ba26c]63#include <debug.h>
[df58e44]64#include <stacktrace.h>
[9c0a9b3]65
[ef9a2a8]66//#define SCHEDULER_VERBOSE 1
67
[7d6ec87]68static void scheduler_separated_stack(void);
69
[da1bafb]70atomic_t nrdy; /**< Number of ready threads in the system. */
[f761f1eb]71
[39cea6a]72/** Carry out actions before new task runs. */
[4e7d3dd]73static void before_task_runs(void)
[39cea6a]74{
75 before_task_runs_arch();
76}
77
[97f1691]78/** Take actions before new thread runs.
[70527f1]79 *
[b60a22c]80 * Perform actions that need to be
81 * taken before the newly selected
[df58e44]82 * thread is passed control.
[70527f1]83 *
[a3eeceb6]84 * THREAD->lock is locked on entry
85 *
[70527f1]86 */
[4e7d3dd]87static void before_thread_runs(void)
[0ca6faa]88{
[b49f4ae]89 before_thread_runs_arch();
[4e7d3dd]90
[f76fed4]91#ifdef CONFIG_FPU_LAZY
[df58e44]92 if (THREAD == CPU->fpu_owner)
[b49f4ae]93 fpu_enable();
94 else
[da1bafb]95 fpu_disable();
[e1326cf]96#elif defined CONFIG_FPU
[b49f4ae]97 fpu_enable();
98 if (THREAD->fpu_context_exists)
[f76fed4]99 fpu_context_restore(THREAD->saved_fpu_context);
[b49f4ae]100 else {
[f76fed4]101 fpu_init();
[6eef3c4]102 THREAD->fpu_context_exists = true;
[b49f4ae]103 }
[f76fed4]104#endif
[df58e44]105
[5b7a107]106#ifdef CONFIG_UDEBUG
[df58e44]107 if (THREAD->btrace) {
108 istate_t *istate = THREAD->udebug.uspace_state;
109 if (istate != NULL) {
110 printf("Thread %" PRIu64 " stack trace:\n", THREAD->tid);
111 stack_trace_istate(istate);
112 }
113
114 THREAD->btrace = false;
115 }
[5b7a107]116#endif
[0ca6faa]117}
118
[7d6ec87]119/** Take actions after THREAD had run.
[97f1691]120 *
121 * Perform actions that need to be
122 * taken after the running thread
[7d6ec87]123 * had been preempted by the scheduler.
[97f1691]124 *
125 * THREAD->lock is locked on entry
126 *
127 */
[4e7d3dd]128static void after_thread_ran(void)
[97f1691]129{
130 after_thread_ran_arch();
131}
132
[5f85c91]133#ifdef CONFIG_FPU_LAZY
[b49f4ae]134void scheduler_fpu_lazy_request(void)
135{
[09c18f7]136restart:
[b49f4ae]137 fpu_enable();
[da1bafb]138 irq_spinlock_lock(&CPU->lock, false);
139
[a3eeceb6]140 /* Save old context */
[da1bafb]141 if (CPU->fpu_owner != NULL) {
142 irq_spinlock_lock(&CPU->fpu_owner->lock, false);
[f76fed4]143 fpu_context_save(CPU->fpu_owner->saved_fpu_context);
[da1bafb]144
145 /* Don't prevent migration */
[6eef3c4]146 CPU->fpu_owner->fpu_context_engaged = false;
[da1bafb]147 irq_spinlock_unlock(&CPU->fpu_owner->lock, false);
[09c18f7]148 CPU->fpu_owner = NULL;
[b49f4ae]149 }
[da1bafb]150
151 irq_spinlock_lock(&THREAD->lock, false);
[7d6ec87]152 if (THREAD->fpu_context_exists) {
[f76fed4]153 fpu_context_restore(THREAD->saved_fpu_context);
[7d6ec87]154 } else {
[f76fed4]155 /* Allocate FPU context */
156 if (!THREAD->saved_fpu_context) {
157 /* Might sleep */
[da1bafb]158 irq_spinlock_unlock(&THREAD->lock, false);
159 irq_spinlock_unlock(&CPU->lock, false);
[4e33b6b]160 THREAD->saved_fpu_context =
[4184e76]161 (fpu_context_t *) slab_alloc(fpu_context_slab, 0);
[da1bafb]162
[09c18f7]163 /* We may have switched CPUs during slab_alloc */
[da1bafb]164 goto restart;
[f76fed4]165 }
166 fpu_init();
[6eef3c4]167 THREAD->fpu_context_exists = true;
[b49f4ae]168 }
[da1bafb]169
[6eabb6e6]170 CPU->fpu_owner = THREAD;
[6eef3c4]171 THREAD->fpu_context_engaged = true;
[da1bafb]172 irq_spinlock_unlock(&THREAD->lock, false);
173
174 irq_spinlock_unlock(&CPU->lock, false);
[b49f4ae]175}
[da1bafb]176#endif /* CONFIG_FPU_LAZY */
[0ca6faa]177
[70527f1]178/** Initialize scheduler
179 *
180 * Initialize kernel scheduler.
181 *
182 */
[f761f1eb]183void scheduler_init(void)
184{
185}
186
[70527f1]187/** Get thread to be scheduled
188 *
189 * Get the optimal thread to be scheduled
[d1a184f]190 * according to thread accounting and scheduler
[70527f1]191 * policy.
192 *
193 * @return Thread to be scheduled.
194 *
195 */
[e507afa]196static thread_t *find_best_thread(void)
[f761f1eb]197{
[623ba26c]198 ASSERT(CPU != NULL);
[da1bafb]199
[f761f1eb]200loop:
201
[248fc1a]202 if (atomic_get(&CPU->nrdy) == 0) {
[f761f1eb]203 /*
204 * For there was nothing to run, the CPU goes to sleep
205 * until a hardware interrupt or an IPI comes.
206 * This improves energy saving and hyperthreading.
207 */
[da1bafb]208 irq_spinlock_lock(&CPU->lock, false);
209 CPU->idle = true;
210 irq_spinlock_unlock(&CPU->lock, false);
211 interrupts_enable();
[d0c82c5]212
[da1bafb]213 /*
[328e0d3]214 * An interrupt might occur right now and wake up a thread.
215 * In such case, the CPU will continue to go to sleep
216 * even though there is a runnable thread.
217 */
[da1bafb]218 cpu_sleep();
219 interrupts_disable();
220 goto loop;
[f761f1eb]221 }
[d896525]222
[da1bafb]223 unsigned int i;
[ea63704]224 for (i = 0; i < RQ_COUNT; i++) {
[da1bafb]225 irq_spinlock_lock(&(CPU->rq[i].lock), false);
226 if (CPU->rq[i].n == 0) {
[f761f1eb]227 /*
228 * If this queue is empty, try a lower-priority queue.
229 */
[da1bafb]230 irq_spinlock_unlock(&(CPU->rq[i].lock), false);
[f761f1eb]231 continue;
232 }
[da1bafb]233
[248fc1a]234 atomic_dec(&CPU->nrdy);
[59e07c91]235 atomic_dec(&nrdy);
[da1bafb]236 CPU->rq[i].n--;
237
[f761f1eb]238 /*
239 * Take the first thread from the queue.
240 */
[55b77d9]241 thread_t *thread = list_get_instance(
242 list_first(&CPU->rq[i].rq), thread_t, rq_link);
[da1bafb]243 list_remove(&thread->rq_link);
244
245 irq_spinlock_pass(&(CPU->rq[i].lock), &thread->lock);
246
247 thread->cpu = CPU;
248 thread->ticks = us2ticks((i + 1) * 10000);
249 thread->priority = i; /* Correct rq index */
250
[f761f1eb]251 /*
[6eef3c4]252 * Clear the stolen flag so that it can be migrated
[32fffef0]253 * when load balancing needs emerge.
[f761f1eb]254 */
[6eef3c4]255 thread->stolen = false;
[da1bafb]256 irq_spinlock_unlock(&thread->lock, false);
257
258 return thread;
[f761f1eb]259 }
[da1bafb]260
[f761f1eb]261 goto loop;
262}
263
[70527f1]264/** Prevent rq starvation
265 *
266 * Prevent low priority threads from starving in rq's.
267 *
268 * When the function decides to relink rq's, it reconnects
269 * respective pointers so that in result threads with 'pri'
[abbc16e]270 * greater or equal start are moved to a higher-priority queue.
[70527f1]271 *
272 * @param start Threshold priority.
273 *
[f761f1eb]274 */
[e16e036a]275static void relink_rq(int start)
[f761f1eb]276{
[55b77d9]277 list_t list;
[da1bafb]278
[55b77d9]279 list_initialize(&list);
[da1bafb]280 irq_spinlock_lock(&CPU->lock, false);
281
[43114c5]282 if (CPU->needs_relink > NEEDS_RELINK_MAX) {
[da1bafb]283 int i;
[4e33b6b]284 for (i = start; i < RQ_COUNT - 1; i++) {
[da1bafb]285 /* Remember and empty rq[i + 1] */
286
287 irq_spinlock_lock(&CPU->rq[i + 1].lock, false);
[55b77d9]288 list_concat(&list, &CPU->rq[i + 1].rq);
[da1bafb]289 size_t n = CPU->rq[i + 1].n;
290 CPU->rq[i + 1].n = 0;
291 irq_spinlock_unlock(&CPU->rq[i + 1].lock, false);
292
293 /* Append rq[i + 1] to rq[i] */
294
295 irq_spinlock_lock(&CPU->rq[i].lock, false);
[55b77d9]296 list_concat(&CPU->rq[i].rq, &list);
[da1bafb]297 CPU->rq[i].n += n;
298 irq_spinlock_unlock(&CPU->rq[i].lock, false);
[f761f1eb]299 }
[da1bafb]300
[43114c5]301 CPU->needs_relink = 0;
[f761f1eb]302 }
[da1bafb]303
304 irq_spinlock_unlock(&CPU->lock, false);
[f761f1eb]305}
306
[7d6ec87]307/** The scheduler
308 *
309 * The thread scheduling procedure.
310 * Passes control directly to
311 * scheduler_separated_stack().
312 *
313 */
314void scheduler(void)
315{
316 volatile ipl_t ipl;
[da1bafb]317
[7d6ec87]318 ASSERT(CPU != NULL);
[da1bafb]319
[7d6ec87]320 ipl = interrupts_disable();
[da1bafb]321
[7d6ec87]322 if (atomic_get(&haltstate))
323 halt();
[8965838e]324
[7d6ec87]325 if (THREAD) {
[da1bafb]326 irq_spinlock_lock(&THREAD->lock, false);
[cce6acf]327
[1ba37fa]328 /* Update thread kernel accounting */
[a2a00e8]329 THREAD->kcycles += get_cycle() - THREAD->last_cycle;
[cce6acf]330
[e1326cf]331#if (defined CONFIG_FPU) && (!defined CONFIG_FPU_LAZY)
[f76fed4]332 fpu_context_save(THREAD->saved_fpu_context);
333#endif
[7d6ec87]334 if (!context_save(&THREAD->saved_context)) {
335 /*
336 * This is the place where threads leave scheduler();
337 */
[cce6acf]338
339 /* Save current CPU cycle */
340 THREAD->last_cycle = get_cycle();
341
[da1bafb]342 irq_spinlock_unlock(&THREAD->lock, false);
[7d6ec87]343 interrupts_restore(THREAD->saved_context.ipl);
[8965838e]344
[7d6ec87]345 return;
346 }
[da1bafb]347
[7d6ec87]348 /*
[4e33b6b]349 * Interrupt priority level of preempted thread is recorded
350 * here to facilitate scheduler() invocations from
[da1bafb]351 * interrupts_disable()'d code (e.g. waitq_sleep_timeout()).
352 *
[7d6ec87]353 */
354 THREAD->saved_context.ipl = ipl;
355 }
[da1bafb]356
[7d6ec87]357 /*
[b4dc35a]358 * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, AS
[7d6ec87]359 * and preemption counter. At this point THE could be coming either
360 * from THREAD's or CPU's stack.
[da1bafb]361 *
[7d6ec87]362 */
363 the_copy(THE, (the_t *) CPU->stack);
[da1bafb]364
[7d6ec87]365 /*
366 * We may not keep the old stack.
367 * Reason: If we kept the old stack and got blocked, for instance, in
368 * find_best_thread(), the old thread could get rescheduled by another
369 * CPU and overwrite the part of its own stack that was also used by
370 * the scheduler on this CPU.
371 *
372 * Moreover, we have to bypass the compiler-generated POP sequence
373 * which is fooled by SP being set to the very top of the stack.
374 * Therefore the scheduler() function continues in
375 * scheduler_separated_stack().
[da1bafb]376 *
[7d6ec87]377 */
378 context_save(&CPU->saved_context);
[32fffef0]379 context_set(&CPU->saved_context, FADDR(scheduler_separated_stack),
[26aafe8]380 (uintptr_t) CPU->stack, STACK_SIZE);
[7d6ec87]381 context_restore(&CPU->saved_context);
[da1bafb]382
383 /* Not reached */
[7d6ec87]384}
[70527f1]385
386/** Scheduler stack switch wrapper
387 *
388 * Second part of the scheduler() function
389 * using new stack. Handling the actual context
390 * switch to a new thread.
391 *
392 */
[7d6ec87]393void scheduler_separated_stack(void)
[f761f1eb]394{
[31d8e10]395 DEADLOCK_PROBE_INIT(p_joinwq);
[481d4751]396 task_t *old_task = TASK;
397 as_t *old_as = AS;
[da1bafb]398
[d0c82c5]399 ASSERT((!THREAD) || (irq_spinlock_locked(&THREAD->lock)));
[623ba26c]400 ASSERT(CPU != NULL);
[8965838e]401
[481d4751]402 /*
403 * Hold the current task and the address space to prevent their
404 * possible destruction should thread_destroy() be called on this or any
405 * other processor while the scheduler is still using them.
406 */
407 if (old_task)
408 task_hold(old_task);
[da1bafb]409
[481d4751]410 if (old_as)
411 as_hold(old_as);
[da1bafb]412
[43114c5]413 if (THREAD) {
[da1bafb]414 /* Must be run after the switch to scheduler stack */
[97f1691]415 after_thread_ran();
[da1bafb]416
[43114c5]417 switch (THREAD->state) {
[06e1e95]418 case Running:
[da1bafb]419 irq_spinlock_unlock(&THREAD->lock, false);
[76cec1e]420 thread_ready(THREAD);
421 break;
[da1bafb]422
[06e1e95]423 case Exiting:
[fe19611]424repeat:
[def5207]425 if (THREAD->detached) {
[da1bafb]426 thread_destroy(THREAD, false);
[fe19611]427 } else {
428 /*
[4e33b6b]429 * The thread structure is kept allocated until
430 * somebody calls thread_detach() on it.
[fe19611]431 */
[da1bafb]432 if (!irq_spinlock_trylock(&THREAD->join_wq.lock)) {
[fe19611]433 /*
434 * Avoid deadlock.
435 */
[da1bafb]436 irq_spinlock_unlock(&THREAD->lock, false);
[ea7890e7]437 delay(HZ);
[da1bafb]438 irq_spinlock_lock(&THREAD->lock, false);
[31d8e10]439 DEADLOCK_PROBE(p_joinwq,
440 DEADLOCK_THRESHOLD);
[fe19611]441 goto repeat;
442 }
[5c8ba05]443 _waitq_wakeup_unsafe(&THREAD->join_wq,
444 WAKEUP_FIRST);
[da1bafb]445 irq_spinlock_unlock(&THREAD->join_wq.lock, false);
[fe19611]446
[48d14222]447 THREAD->state = Lingering;
[da1bafb]448 irq_spinlock_unlock(&THREAD->lock, false);
[fe19611]449 }
[76cec1e]450 break;
[266294a9]451
[06e1e95]452 case Sleeping:
[76cec1e]453 /*
454 * Prefer the thread after it's woken up.
455 */
[22f7769]456 THREAD->priority = -1;
[da1bafb]457
[76cec1e]458 /*
[4e33b6b]459 * We need to release wq->lock which we locked in
460 * waitq_sleep(). Address of wq->lock is kept in
461 * THREAD->sleep_queue.
[76cec1e]462 */
[da1bafb]463 irq_spinlock_unlock(&THREAD->sleep_queue->lock, false);
464
465 irq_spinlock_unlock(&THREAD->lock, false);
[76cec1e]466 break;
[da1bafb]467
[06e1e95]468 default:
[76cec1e]469 /*
470 * Entering state is unexpected.
471 */
[f651e80]472 panic("tid%" PRIu64 ": unexpected state %s.",
[1e9d0e3]473 THREAD->tid, thread_states[THREAD->state]);
[76cec1e]474 break;
[f761f1eb]475 }
[da1bafb]476
[43114c5]477 THREAD = NULL;
[f761f1eb]478 }
[da1bafb]479
[43114c5]480 THREAD = find_best_thread();
[f761f1eb]481
[da1bafb]482 irq_spinlock_lock(&THREAD->lock, false);
483 int priority = THREAD->priority;
484 irq_spinlock_unlock(&THREAD->lock, false);
485
486 relink_rq(priority);
487
[f761f1eb]488 /*
[4e7d3dd]489 * If both the old and the new task are the same,
490 * lots of work is avoided.
[f761f1eb]491 */
[43114c5]492 if (TASK != THREAD->task) {
[481d4751]493 as_t *new_as = THREAD->task->as;
[f761f1eb]494
495 /*
[4e7d3dd]496 * Note that it is possible for two tasks
497 * to share one address space.
[f761f1eb]498 */
[481d4751]499 if (old_as != new_as) {
[f761f1eb]500 /*
[20d50a1]501 * Both tasks and address spaces are different.
[f761f1eb]502 * Replace the old one with the new one.
503 */
[481d4751]504 as_switch(old_as, new_as);
[f761f1eb]505 }
[da1bafb]506
[f76fed4]507 TASK = THREAD->task;
[39cea6a]508 before_task_runs();
[f761f1eb]509 }
[da1bafb]510
[481d4751]511 if (old_task)
512 task_release(old_task);
[da1bafb]513
[481d4751]514 if (old_as)
515 as_release(old_as);
516
[da1bafb]517 irq_spinlock_lock(&THREAD->lock, false);
[43114c5]518 THREAD->state = Running;
[da1bafb]519
[f76fed4]520#ifdef SCHEDULER_VERBOSE
[1e9d0e3]521 printf("cpu%u: tid %" PRIu64 " (priority=%d, ticks=%" PRIu64
[ef9a2a8]522 ", nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->priority,
[1e9d0e3]523 THREAD->ticks, atomic_get(&CPU->nrdy));
[da1bafb]524#endif
525
[97f1691]526 /*
527 * Some architectures provide late kernel PA2KA(identity)
528 * mapping in a page fault handler. However, the page fault
529 * handler uses the kernel stack of the running thread and
530 * therefore cannot be used to map it. The kernel stack, if
531 * necessary, is to be mapped in before_thread_runs(). This
532 * function must be executed before the switch to the new stack.
533 */
534 before_thread_runs();
[da1bafb]535
[3e1607f]536 /*
[4e33b6b]537 * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
538 * thread's stack.
[3e1607f]539 */
[bcdd9aa]540 the_copy(THE, (the_t *) THREAD->kstack);
541
[43114c5]542 context_restore(&THREAD->saved_context);
[da1bafb]543
544 /* Not reached */
[f761f1eb]545}
546
[5f85c91]547#ifdef CONFIG_SMP
[70527f1]548/** Load balancing thread
549 *
550 * SMP load balancing thread, supervising thread supplies
551 * for the CPU it's wired to.
552 *
553 * @param arg Generic thread argument (unused).
554 *
[f761f1eb]555 */
556void kcpulb(void *arg)
557{
[228666c]558 atomic_count_t average;
[da1bafb]559 atomic_count_t rdy;
560
[2cb5e64]561 /*
562 * Detach kcpulb as nobody will call thread_join_timeout() on it.
563 */
564 thread_detach(THREAD);
565
[f761f1eb]566loop:
567 /*
[3260ada]568 * Work in 1s intervals.
[f761f1eb]569 */
[3260ada]570 thread_sleep(1);
[da1bafb]571
[f761f1eb]572not_satisfied:
573 /*
574 * Calculate the number of threads that will be migrated/stolen from
575 * other CPU's. Note that situation can have changed between two
576 * passes. Each time get the most up to date counts.
[da1bafb]577 *
[f761f1eb]578 */
[444ec64]579 average = atomic_get(&nrdy) / config.cpu_active + 1;
[da1bafb]580 rdy = atomic_get(&CPU->nrdy);
581
582 if (average <= rdy)
[f761f1eb]583 goto satisfied;
[da1bafb]584
585 atomic_count_t count = average - rdy;
586
[f761f1eb]587 /*
[4e33b6b]588 * Searching least priority queues on all CPU's first and most priority
589 * queues on all CPU's last.
[f761f1eb]590 */
[da1bafb]591 size_t acpu;
592 size_t acpu_bias = 0;
593 int rq;
594
595 for (rq = RQ_COUNT - 1; rq >= 0; rq--) {
596 for (acpu = 0; acpu < config.cpu_active; acpu++) {
597 cpu_t *cpu = &cpus[(acpu + acpu_bias) % config.cpu_active];
598
[f761f1eb]599 /*
600 * Not interested in ourselves.
[4e33b6b]601 * Doesn't require interrupt disabling for kcpulb has
602 * THREAD_FLAG_WIRED.
[da1bafb]603 *
[f761f1eb]604 */
[43114c5]605 if (CPU == cpu)
[248fc1a]606 continue;
[da1bafb]607
[248fc1a]608 if (atomic_get(&cpu->nrdy) <= average)
609 continue;
[da1bafb]610
611 irq_spinlock_lock(&(cpu->rq[rq].lock), true);
612 if (cpu->rq[rq].n == 0) {
613 irq_spinlock_unlock(&(cpu->rq[rq].lock), true);
[f761f1eb]614 continue;
615 }
[da1bafb]616
617 thread_t *thread = NULL;
618
619 /* Search rq from the back */
[55b77d9]620 link_t *link = cpu->rq[rq].rq.head.prev;
[da1bafb]621
[55b77d9]622 while (link != &(cpu->rq[rq].rq.head)) {
[43ac0cc]623 thread = (thread_t *) list_get_instance(link,
624 thread_t, rq_link);
[da1bafb]625
[f761f1eb]626 /*
[43ac0cc]627 * Do not steal CPU-wired threads, threads
628 * already stolen, threads for which migration
629 * was temporarily disabled or threads whose
630 * FPU context is still in the CPU.
[6a27d63]631 */
[da1bafb]632 irq_spinlock_lock(&thread->lock, false);
633
[6eef3c4]634 if ((!thread->wired) && (!thread->stolen) &&
635 (!thread->nomigrate) &&
636 (!thread->fpu_context_engaged)) {
[f761f1eb]637 /*
[da1bafb]638 * Remove thread from ready queue.
[f761f1eb]639 */
[43ac0cc]640 irq_spinlock_unlock(&thread->lock,
641 false);
[f761f1eb]642
[248fc1a]643 atomic_dec(&cpu->nrdy);
[59e07c91]644 atomic_dec(&nrdy);
[da1bafb]645
646 cpu->rq[rq].n--;
647 list_remove(&thread->rq_link);
648
[f761f1eb]649 break;
650 }
[da1bafb]651
652 irq_spinlock_unlock(&thread->lock, false);
653
654 link = link->prev;
655 thread = NULL;
[f761f1eb]656 }
[da1bafb]657
658 if (thread) {
[f761f1eb]659 /*
[da1bafb]660 * Ready thread on local CPU
[f761f1eb]661 */
[da1bafb]662
[43ac0cc]663 irq_spinlock_pass(&(cpu->rq[rq].lock),
664 &thread->lock);
[da1bafb]665
[f76fed4]666#ifdef KCPULB_VERBOSE
[1e9d0e3]667 printf("kcpulb%u: TID %" PRIu64 " -> cpu%u, "
668 "nrdy=%ld, avg=%ld\n", CPU->id, t->tid,
669 CPU->id, atomic_get(&CPU->nrdy),
[6f4495f5]670 atomic_get(&nrdy) / config.cpu_active);
[f76fed4]671#endif
[da1bafb]672
[6eef3c4]673 thread->stolen = true;
[da1bafb]674 thread->state = Entering;
675
676 irq_spinlock_unlock(&thread->lock, true);
677 thread_ready(thread);
678
[f761f1eb]679 if (--count == 0)
680 goto satisfied;
[da1bafb]681
[f761f1eb]682 /*
[4e33b6b]683 * We are not satisfied yet, focus on another
684 * CPU next time.
[da1bafb]685 *
[f761f1eb]686 */
[da1bafb]687 acpu_bias++;
[f761f1eb]688
689 continue;
[da1bafb]690 } else
691 irq_spinlock_unlock(&(cpu->rq[rq].lock), true);
692
[f761f1eb]693 }
694 }
[da1bafb]695
[248fc1a]696 if (atomic_get(&CPU->nrdy)) {
[f761f1eb]697 /*
698 * Be a little bit light-weight and let migrated threads run.
[da1bafb]699 *
[f761f1eb]700 */
701 scheduler();
[3260ada]702 } else {
[f761f1eb]703 /*
704 * We failed to migrate a single thread.
[3260ada]705 * Give up this turn.
[da1bafb]706 *
[f761f1eb]707 */
[3260ada]708 goto loop;
[f761f1eb]709 }
[da1bafb]710
[f761f1eb]711 goto not_satisfied;
[da1bafb]712
[f761f1eb]713satisfied:
714 goto loop;
715}
[5f85c91]716#endif /* CONFIG_SMP */
[10e16a7]717
[da1bafb]718/** Print information about threads & scheduler queues
719 *
720 */
[10e16a7]721void sched_print_list(void)
722{
[da1bafb]723 size_t cpu;
[4184e76]724 for (cpu = 0; cpu < config.cpu_count; cpu++) {
[10e16a7]725 if (!cpus[cpu].active)
726 continue;
[da1bafb]727
728 irq_spinlock_lock(&cpus[cpu].lock, true);
729
[7e752b2]730 printf("cpu%u: address=%p, nrdy=%" PRIua ", needs_relink=%zu\n",
[6f4495f5]731 cpus[cpu].id, &cpus[cpu], atomic_get(&cpus[cpu].nrdy),
732 cpus[cpu].needs_relink);
[10e16a7]733
[da1bafb]734 unsigned int i;
[4e33b6b]735 for (i = 0; i < RQ_COUNT; i++) {
[da1bafb]736 irq_spinlock_lock(&(cpus[cpu].rq[i].lock), false);
737 if (cpus[cpu].rq[i].n == 0) {
738 irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
[10e16a7]739 continue;
740 }
[da1bafb]741
[5b86d10]742 printf("\trq[%u]: ", i);
[feeac0d]743 list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
744 thread) {
[da1bafb]745 printf("%" PRIu64 "(%s) ", thread->tid,
746 thread_states[thread->state]);
[10e16a7]747 }
748 printf("\n");
[da1bafb]749
750 irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
[10e16a7]751 }
[da1bafb]752
753 irq_spinlock_unlock(&cpus[cpu].lock, true);
[10e16a7]754 }
755}
[b45c443]756
[cc73a8a1]757/** @}
[b45c443]758 */
Note: See TracBrowser for help on using the repository browser.