source: mainline/kernel/generic/src/proc/scheduler.c@ 4f3aa76

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4f3aa76 was aae365bc, checked in by Jakub Jermar <jakub@…>, 7 years ago

Remove RCU and CHT support

  • Property mode set to 100644
File size: 17.4 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
[174156fd]29/** @addtogroup kernel_generic_proc
[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
[63e27ef]41#include <assert.h>
[4621d23]42#include <atomic.h>
[f761f1eb]43#include <proc/scheduler.h>
44#include <proc/thread.h>
45#include <proc/task.h>
[32ff43e6]46#include <mm/frame.h>
47#include <mm/page.h>
[20d50a1]48#include <mm/as.h>
[b3f8fb7]49#include <time/timeout.h>
[fe19611]50#include <time/delay.h>
[32ff43e6]51#include <arch/asm.h>
52#include <arch/faddr.h>
[cce6acf]53#include <arch/cycle.h>
[23684b7]54#include <atomic.h>
[32ff43e6]55#include <synch/spinlock.h>
[f761f1eb]56#include <config.h>
57#include <context.h>
[b3f8fb7]58#include <fpu_context.h>
[b2e121a]59#include <halt.h>
[f761f1eb]60#include <arch.h>
[5c9a08b]61#include <adt/list.h>
[02a99d2]62#include <panic.h>
[32ff43e6]63#include <cpu.h>
[bab75df6]64#include <stdio.h>
[b2fa1204]65#include <log.h>
[df58e44]66#include <stacktrace.h>
[9c0a9b3]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();
[a35b458]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
[a35b458]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 }
[a35b458]113
[df58e44]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{
[09c18f78]136restart:
[b49f4ae]137 fpu_enable();
[da1bafb]138 irq_spinlock_lock(&CPU->lock, false);
[a35b458]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);
[a35b458]144
[da1bafb]145 /* Don't prevent migration */
[6eef3c4]146 CPU->fpu_owner->fpu_context_engaged = false;
[da1bafb]147 irq_spinlock_unlock(&CPU->fpu_owner->lock, false);
[09c18f78]148 CPU->fpu_owner = NULL;
[b49f4ae]149 }
[a35b458]150
[da1bafb]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 =
[82d515e9]161 (fpu_context_t *) slab_alloc(fpu_context_cache, 0);
[a35b458]162
[09c18f78]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 }
[a35b458]169
[6eabb6e6]170 CPU->fpu_owner = THREAD;
[6eef3c4]171 THREAD->fpu_context_engaged = true;
[da1bafb]172 irq_spinlock_unlock(&THREAD->lock, false);
[a35b458]173
[da1bafb]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{
[63e27ef]198 assert(CPU != NULL);
[a35b458]199
[f761f1eb]200loop:
[a35b458]201
[036e97c]202 if (atomic_load(&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();
[a35b458]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 }
[181a746]222
[63e27ef]223 assert(!CPU->idle);
[a35b458]224
[da1bafb]225 unsigned int i;
[ea63704]226 for (i = 0; i < RQ_COUNT; i++) {
[da1bafb]227 irq_spinlock_lock(&(CPU->rq[i].lock), false);
228 if (CPU->rq[i].n == 0) {
[f761f1eb]229 /*
230 * If this queue is empty, try a lower-priority queue.
231 */
[da1bafb]232 irq_spinlock_unlock(&(CPU->rq[i].lock), false);
[f761f1eb]233 continue;
234 }
[a35b458]235
[248fc1a]236 atomic_dec(&CPU->nrdy);
[59e07c91]237 atomic_dec(&nrdy);
[da1bafb]238 CPU->rq[i].n--;
[a35b458]239
[f761f1eb]240 /*
241 * Take the first thread from the queue.
242 */
[55b77d9]243 thread_t *thread = list_get_instance(
244 list_first(&CPU->rq[i].rq), thread_t, rq_link);
[da1bafb]245 list_remove(&thread->rq_link);
[a35b458]246
[da1bafb]247 irq_spinlock_pass(&(CPU->rq[i].lock), &thread->lock);
[a35b458]248
[da1bafb]249 thread->cpu = CPU;
250 thread->ticks = us2ticks((i + 1) * 10000);
251 thread->priority = i; /* Correct rq index */
[a35b458]252
[f761f1eb]253 /*
[6eef3c4]254 * Clear the stolen flag so that it can be migrated
[32fffef0]255 * when load balancing needs emerge.
[f761f1eb]256 */
[6eef3c4]257 thread->stolen = false;
[da1bafb]258 irq_spinlock_unlock(&thread->lock, false);
[a35b458]259
[da1bafb]260 return thread;
[f761f1eb]261 }
[a35b458]262
[f761f1eb]263 goto loop;
264}
265
[70527f1]266/** Prevent rq starvation
267 *
268 * Prevent low priority threads from starving in rq's.
269 *
270 * When the function decides to relink rq's, it reconnects
271 * respective pointers so that in result threads with 'pri'
[abbc16e]272 * greater or equal start are moved to a higher-priority queue.
[70527f1]273 *
274 * @param start Threshold priority.
275 *
[f761f1eb]276 */
[e16e036a]277static void relink_rq(int start)
[f761f1eb]278{
[55b77d9]279 list_t list;
[a35b458]280
[55b77d9]281 list_initialize(&list);
[da1bafb]282 irq_spinlock_lock(&CPU->lock, false);
[a35b458]283
[43114c5]284 if (CPU->needs_relink > NEEDS_RELINK_MAX) {
[da1bafb]285 int i;
[4e33b6b]286 for (i = start; i < RQ_COUNT - 1; i++) {
[da1bafb]287 /* Remember and empty rq[i + 1] */
[a35b458]288
[da1bafb]289 irq_spinlock_lock(&CPU->rq[i + 1].lock, false);
[55b77d9]290 list_concat(&list, &CPU->rq[i + 1].rq);
[da1bafb]291 size_t n = CPU->rq[i + 1].n;
292 CPU->rq[i + 1].n = 0;
293 irq_spinlock_unlock(&CPU->rq[i + 1].lock, false);
[a35b458]294
[da1bafb]295 /* Append rq[i + 1] to rq[i] */
[a35b458]296
[da1bafb]297 irq_spinlock_lock(&CPU->rq[i].lock, false);
[55b77d9]298 list_concat(&CPU->rq[i].rq, &list);
[da1bafb]299 CPU->rq[i].n += n;
300 irq_spinlock_unlock(&CPU->rq[i].lock, false);
[f761f1eb]301 }
[a35b458]302
[43114c5]303 CPU->needs_relink = 0;
[f761f1eb]304 }
[a35b458]305
[da1bafb]306 irq_spinlock_unlock(&CPU->lock, false);
[f761f1eb]307}
308
[7d6ec87]309/** The scheduler
310 *
311 * The thread scheduling procedure.
312 * Passes control directly to
313 * scheduler_separated_stack().
314 *
315 */
316void scheduler(void)
317{
318 volatile ipl_t ipl;
[a35b458]319
[63e27ef]320 assert(CPU != NULL);
[a35b458]321
[7d6ec87]322 ipl = interrupts_disable();
[a35b458]323
[036e97c]324 if (atomic_load(&haltstate))
[7d6ec87]325 halt();
[a35b458]326
[7d6ec87]327 if (THREAD) {
[da1bafb]328 irq_spinlock_lock(&THREAD->lock, false);
[a35b458]329
[1ba37fa]330 /* Update thread kernel accounting */
[a2a00e8]331 THREAD->kcycles += get_cycle() - THREAD->last_cycle;
[a35b458]332
[e1326cf]333#if (defined CONFIG_FPU) && (!defined CONFIG_FPU_LAZY)
[f76fed4]334 fpu_context_save(THREAD->saved_fpu_context);
335#endif
[7d6ec87]336 if (!context_save(&THREAD->saved_context)) {
337 /*
338 * This is the place where threads leave scheduler();
339 */
[a35b458]340
[cce6acf]341 /* Save current CPU cycle */
342 THREAD->last_cycle = get_cycle();
[a35b458]343
[da1bafb]344 irq_spinlock_unlock(&THREAD->lock, false);
[7d6ec87]345 interrupts_restore(THREAD->saved_context.ipl);
[a35b458]346
[7d6ec87]347 return;
348 }
[a35b458]349
[7d6ec87]350 /*
[4e33b6b]351 * Interrupt priority level of preempted thread is recorded
352 * here to facilitate scheduler() invocations from
[da1bafb]353 * interrupts_disable()'d code (e.g. waitq_sleep_timeout()).
354 *
[7d6ec87]355 */
356 THREAD->saved_context.ipl = ipl;
357 }
[a35b458]358
[7d6ec87]359 /*
[a6e55886]360 * Through the 'CURRENT' structure, we keep track of THREAD, TASK, CPU, AS
361 * and preemption counter. At this point CURRENT could be coming either
[7d6ec87]362 * from THREAD's or CPU's stack.
[da1bafb]363 *
[7d6ec87]364 */
[a6e55886]365 current_copy(CURRENT, (current_t *) CPU->stack);
[a35b458]366
[7d6ec87]367 /*
368 * We may not keep the old stack.
369 * Reason: If we kept the old stack and got blocked, for instance, in
370 * find_best_thread(), the old thread could get rescheduled by another
371 * CPU and overwrite the part of its own stack that was also used by
372 * the scheduler on this CPU.
373 *
374 * Moreover, we have to bypass the compiler-generated POP sequence
375 * which is fooled by SP being set to the very top of the stack.
376 * Therefore the scheduler() function continues in
377 * scheduler_separated_stack().
[da1bafb]378 *
[7d6ec87]379 */
380 context_save(&CPU->saved_context);
[32fffef0]381 context_set(&CPU->saved_context, FADDR(scheduler_separated_stack),
[26aafe8]382 (uintptr_t) CPU->stack, STACK_SIZE);
[7d6ec87]383 context_restore(&CPU->saved_context);
[a35b458]384
[da1bafb]385 /* Not reached */
[7d6ec87]386}
[70527f1]387
388/** Scheduler stack switch wrapper
389 *
390 * Second part of the scheduler() function
391 * using new stack. Handling the actual context
392 * switch to a new thread.
393 *
394 */
[7d6ec87]395void scheduler_separated_stack(void)
[f761f1eb]396{
[31d8e10]397 DEADLOCK_PROBE_INIT(p_joinwq);
[481d4751]398 task_t *old_task = TASK;
399 as_t *old_as = AS;
[a35b458]400
[63e27ef]401 assert((!THREAD) || (irq_spinlock_locked(&THREAD->lock)));
402 assert(CPU != NULL);
403 assert(interrupts_disabled());
[a35b458]404
[481d4751]405 /*
406 * Hold the current task and the address space to prevent their
407 * possible destruction should thread_destroy() be called on this or any
408 * other processor while the scheduler is still using them.
409 */
410 if (old_task)
411 task_hold(old_task);
[a35b458]412
[481d4751]413 if (old_as)
414 as_hold(old_as);
[a35b458]415
[43114c5]416 if (THREAD) {
[da1bafb]417 /* Must be run after the switch to scheduler stack */
[97f1691]418 after_thread_ran();
[a35b458]419
[43114c5]420 switch (THREAD->state) {
[06e1e95]421 case Running:
[da1bafb]422 irq_spinlock_unlock(&THREAD->lock, false);
[76cec1e]423 thread_ready(THREAD);
424 break;
[a35b458]425
[06e1e95]426 case Exiting:
[18b6a88]427 repeat:
[def5207]428 if (THREAD->detached) {
[da1bafb]429 thread_destroy(THREAD, false);
[fe19611]430 } else {
431 /*
[4e33b6b]432 * The thread structure is kept allocated until
433 * somebody calls thread_detach() on it.
[fe19611]434 */
[da1bafb]435 if (!irq_spinlock_trylock(&THREAD->join_wq.lock)) {
[fe19611]436 /*
437 * Avoid deadlock.
438 */
[da1bafb]439 irq_spinlock_unlock(&THREAD->lock, false);
[ea7890e7]440 delay(HZ);
[da1bafb]441 irq_spinlock_lock(&THREAD->lock, false);
[31d8e10]442 DEADLOCK_PROBE(p_joinwq,
443 DEADLOCK_THRESHOLD);
[fe19611]444 goto repeat;
445 }
[5c8ba05]446 _waitq_wakeup_unsafe(&THREAD->join_wq,
447 WAKEUP_FIRST);
[da1bafb]448 irq_spinlock_unlock(&THREAD->join_wq.lock, false);
[a35b458]449
[48d14222]450 THREAD->state = Lingering;
[da1bafb]451 irq_spinlock_unlock(&THREAD->lock, false);
[fe19611]452 }
[76cec1e]453 break;
[a35b458]454
[06e1e95]455 case Sleeping:
[76cec1e]456 /*
457 * Prefer the thread after it's woken up.
458 */
[22f7769]459 THREAD->priority = -1;
[a35b458]460
[76cec1e]461 /*
[4e33b6b]462 * We need to release wq->lock which we locked in
463 * waitq_sleep(). Address of wq->lock is kept in
464 * THREAD->sleep_queue.
[76cec1e]465 */
[da1bafb]466 irq_spinlock_unlock(&THREAD->sleep_queue->lock, false);
[a35b458]467
[da1bafb]468 irq_spinlock_unlock(&THREAD->lock, false);
[76cec1e]469 break;
[a35b458]470
[06e1e95]471 default:
[76cec1e]472 /*
473 * Entering state is unexpected.
474 */
[f651e80]475 panic("tid%" PRIu64 ": unexpected state %s.",
[1e9d0e3]476 THREAD->tid, thread_states[THREAD->state]);
[76cec1e]477 break;
[f761f1eb]478 }
[a35b458]479
[43114c5]480 THREAD = NULL;
[f761f1eb]481 }
[a35b458]482
[43114c5]483 THREAD = find_best_thread();
[a35b458]484
[da1bafb]485 irq_spinlock_lock(&THREAD->lock, false);
486 int priority = THREAD->priority;
487 irq_spinlock_unlock(&THREAD->lock, false);
[a35b458]488
[da1bafb]489 relink_rq(priority);
[a35b458]490
[f761f1eb]491 /*
[4e7d3dd]492 * If both the old and the new task are the same,
493 * lots of work is avoided.
[f761f1eb]494 */
[43114c5]495 if (TASK != THREAD->task) {
[481d4751]496 as_t *new_as = THREAD->task->as;
[a35b458]497
[f761f1eb]498 /*
[4e7d3dd]499 * Note that it is possible for two tasks
500 * to share one address space.
[f761f1eb]501 */
[481d4751]502 if (old_as != new_as) {
[f761f1eb]503 /*
[20d50a1]504 * Both tasks and address spaces are different.
[f761f1eb]505 * Replace the old one with the new one.
506 */
[481d4751]507 as_switch(old_as, new_as);
[f761f1eb]508 }
[a35b458]509
[f76fed4]510 TASK = THREAD->task;
[39cea6a]511 before_task_runs();
[f761f1eb]512 }
[a35b458]513
[481d4751]514 if (old_task)
515 task_release(old_task);
[a35b458]516
[481d4751]517 if (old_as)
518 as_release(old_as);
[a35b458]519
[da1bafb]520 irq_spinlock_lock(&THREAD->lock, false);
[43114c5]521 THREAD->state = Running;
[a35b458]522
[f76fed4]523#ifdef SCHEDULER_VERBOSE
[b2fa1204]524 log(LF_OTHER, LVL_DEBUG,
525 "cpu%u: tid %" PRIu64 " (priority=%d, ticks=%" PRIu64
[077842c]526 ", nrdy=%zu)", CPU->id, THREAD->tid, THREAD->priority,
[036e97c]527 THREAD->ticks, atomic_load(&CPU->nrdy));
[da1bafb]528#endif
[a35b458]529
[97f1691]530 /*
531 * Some architectures provide late kernel PA2KA(identity)
532 * mapping in a page fault handler. However, the page fault
533 * handler uses the kernel stack of the running thread and
534 * therefore cannot be used to map it. The kernel stack, if
535 * necessary, is to be mapped in before_thread_runs(). This
536 * function must be executed before the switch to the new stack.
537 */
538 before_thread_runs();
[a35b458]539
[3e1607f]540 /*
[4e33b6b]541 * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
542 * thread's stack.
[3e1607f]543 */
[a6e55886]544 current_copy(CURRENT, (current_t *) THREAD->kstack);
[a35b458]545
[43114c5]546 context_restore(&THREAD->saved_context);
[a35b458]547
[da1bafb]548 /* Not reached */
[f761f1eb]549}
550
[5f85c91]551#ifdef CONFIG_SMP
[70527f1]552/** Load balancing thread
553 *
554 * SMP load balancing thread, supervising thread supplies
555 * for the CPU it's wired to.
556 *
557 * @param arg Generic thread argument (unused).
558 *
[f761f1eb]559 */
560void kcpulb(void *arg)
561{
[3cfe2b8]562 size_t average;
563 size_t rdy;
[a35b458]564
[2cb5e64]565 /*
566 * Detach kcpulb as nobody will call thread_join_timeout() on it.
567 */
568 thread_detach(THREAD);
[a35b458]569
[f761f1eb]570loop:
571 /*
[3260ada]572 * Work in 1s intervals.
[f761f1eb]573 */
[3260ada]574 thread_sleep(1);
[a35b458]575
[f761f1eb]576not_satisfied:
577 /*
578 * Calculate the number of threads that will be migrated/stolen from
579 * other CPU's. Note that situation can have changed between two
580 * passes. Each time get the most up to date counts.
[da1bafb]581 *
[f761f1eb]582 */
[036e97c]583 average = atomic_load(&nrdy) / config.cpu_active + 1;
584 rdy = atomic_load(&CPU->nrdy);
[a35b458]585
[da1bafb]586 if (average <= rdy)
[f761f1eb]587 goto satisfied;
[a35b458]588
[3cfe2b8]589 size_t count = average - rdy;
[a35b458]590
[f761f1eb]591 /*
[4e33b6b]592 * Searching least priority queues on all CPU's first and most priority
593 * queues on all CPU's last.
[f761f1eb]594 */
[da1bafb]595 size_t acpu;
596 size_t acpu_bias = 0;
597 int rq;
[a35b458]598
[da1bafb]599 for (rq = RQ_COUNT - 1; rq >= 0; rq--) {
600 for (acpu = 0; acpu < config.cpu_active; acpu++) {
601 cpu_t *cpu = &cpus[(acpu + acpu_bias) % config.cpu_active];
[a35b458]602
[f761f1eb]603 /*
604 * Not interested in ourselves.
[4e33b6b]605 * Doesn't require interrupt disabling for kcpulb has
606 * THREAD_FLAG_WIRED.
[da1bafb]607 *
[f761f1eb]608 */
[43114c5]609 if (CPU == cpu)
[248fc1a]610 continue;
[a35b458]611
[036e97c]612 if (atomic_load(&cpu->nrdy) <= average)
[248fc1a]613 continue;
[a35b458]614
[da1bafb]615 irq_spinlock_lock(&(cpu->rq[rq].lock), true);
616 if (cpu->rq[rq].n == 0) {
617 irq_spinlock_unlock(&(cpu->rq[rq].lock), true);
[f761f1eb]618 continue;
619 }
[a35b458]620
[da1bafb]621 thread_t *thread = NULL;
[a35b458]622
[da1bafb]623 /* Search rq from the back */
[55b77d9]624 link_t *link = cpu->rq[rq].rq.head.prev;
[a35b458]625
[55b77d9]626 while (link != &(cpu->rq[rq].rq.head)) {
[43ac0cc]627 thread = (thread_t *) list_get_instance(link,
628 thread_t, rq_link);
[a35b458]629
[f761f1eb]630 /*
[43ac0cc]631 * Do not steal CPU-wired threads, threads
632 * already stolen, threads for which migration
633 * was temporarily disabled or threads whose
634 * FPU context is still in the CPU.
[6a27d63]635 */
[da1bafb]636 irq_spinlock_lock(&thread->lock, false);
[a35b458]637
[6eef3c4]638 if ((!thread->wired) && (!thread->stolen) &&
639 (!thread->nomigrate) &&
640 (!thread->fpu_context_engaged)) {
[f761f1eb]641 /*
[da1bafb]642 * Remove thread from ready queue.
[f761f1eb]643 */
[43ac0cc]644 irq_spinlock_unlock(&thread->lock,
645 false);
[a35b458]646
[248fc1a]647 atomic_dec(&cpu->nrdy);
[59e07c91]648 atomic_dec(&nrdy);
[a35b458]649
[da1bafb]650 cpu->rq[rq].n--;
651 list_remove(&thread->rq_link);
[a35b458]652
[f761f1eb]653 break;
654 }
[a35b458]655
[da1bafb]656 irq_spinlock_unlock(&thread->lock, false);
[a35b458]657
[da1bafb]658 link = link->prev;
659 thread = NULL;
[f761f1eb]660 }
[a35b458]661
[da1bafb]662 if (thread) {
[f761f1eb]663 /*
[da1bafb]664 * Ready thread on local CPU
[f761f1eb]665 */
[a35b458]666
[43ac0cc]667 irq_spinlock_pass(&(cpu->rq[rq].lock),
668 &thread->lock);
[a35b458]669
[f76fed4]670#ifdef KCPULB_VERBOSE
[b2fa1204]671 log(LF_OTHER, LVL_DEBUG,
672 "kcpulb%u: TID %" PRIu64 " -> cpu%u, "
673 "nrdy=%ld, avg=%ld", CPU->id, t->tid,
[036e97c]674 CPU->id, atomic_load(&CPU->nrdy),
675 atomic_load(&nrdy) / config.cpu_active);
[f76fed4]676#endif
[a35b458]677
[6eef3c4]678 thread->stolen = true;
[da1bafb]679 thread->state = Entering;
[a35b458]680
[da1bafb]681 irq_spinlock_unlock(&thread->lock, true);
682 thread_ready(thread);
[a35b458]683
[f761f1eb]684 if (--count == 0)
685 goto satisfied;
[a35b458]686
[f761f1eb]687 /*
[4e33b6b]688 * We are not satisfied yet, focus on another
689 * CPU next time.
[da1bafb]690 *
[f761f1eb]691 */
[da1bafb]692 acpu_bias++;
[a35b458]693
[f761f1eb]694 continue;
[da1bafb]695 } else
696 irq_spinlock_unlock(&(cpu->rq[rq].lock), true);
[a35b458]697
[f761f1eb]698 }
699 }
[a35b458]700
[036e97c]701 if (atomic_load(&CPU->nrdy)) {
[f761f1eb]702 /*
703 * Be a little bit light-weight and let migrated threads run.
[da1bafb]704 *
[f761f1eb]705 */
706 scheduler();
[3260ada]707 } else {
[f761f1eb]708 /*
709 * We failed to migrate a single thread.
[3260ada]710 * Give up this turn.
[da1bafb]711 *
[f761f1eb]712 */
[3260ada]713 goto loop;
[f761f1eb]714 }
[a35b458]715
[f761f1eb]716 goto not_satisfied;
[a35b458]717
[f761f1eb]718satisfied:
719 goto loop;
720}
[5f85c91]721#endif /* CONFIG_SMP */
[10e16a7]722
[da1bafb]723/** Print information about threads & scheduler queues
724 *
725 */
[10e16a7]726void sched_print_list(void)
727{
[da1bafb]728 size_t cpu;
[4184e76]729 for (cpu = 0; cpu < config.cpu_count; cpu++) {
[10e16a7]730 if (!cpus[cpu].active)
731 continue;
[a35b458]732
[da1bafb]733 irq_spinlock_lock(&cpus[cpu].lock, true);
[a35b458]734
[077842c]735 printf("cpu%u: address=%p, nrdy=%zu, needs_relink=%zu\n",
[036e97c]736 cpus[cpu].id, &cpus[cpu], atomic_load(&cpus[cpu].nrdy),
[6f4495f5]737 cpus[cpu].needs_relink);
[a35b458]738
[da1bafb]739 unsigned int i;
[4e33b6b]740 for (i = 0; i < RQ_COUNT; i++) {
[da1bafb]741 irq_spinlock_lock(&(cpus[cpu].rq[i].lock), false);
742 if (cpus[cpu].rq[i].n == 0) {
743 irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
[10e16a7]744 continue;
745 }
[a35b458]746
[5b86d10]747 printf("\trq[%u]: ", i);
[feeac0d]748 list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
749 thread) {
[da1bafb]750 printf("%" PRIu64 "(%s) ", thread->tid,
751 thread_states[thread->state]);
[10e16a7]752 }
753 printf("\n");
[a35b458]754
[da1bafb]755 irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
[10e16a7]756 }
[a35b458]757
[da1bafb]758 irq_spinlock_unlock(&cpus[cpu].lock, true);
[10e16a7]759 }
760}
[b45c443]761
[cc73a8a1]762/** @}
[b45c443]763 */
Note: See TracBrowser for help on using the repository browser.