source: mainline/kernel/generic/src/proc/scheduler.c@ 3cfe2b8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3cfe2b8 was 3cfe2b8, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Convert atomic_t to atomic_size_t (6): Replace atomic_count_t with size_t

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