source: mainline/kernel/generic/src/proc/scheduler.c@ 6e49dab

Last change on this file since 6e49dab was 6e49dab, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 18 months ago

Extract post-thread cleanup into a separate function

  • Property mode set to 100644
File size: 16.9 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);
[23f36a3]69static void fpu_restore(void);
[7d6ec87]70
[31e15be]71atomic_size_t nrdy; /**< Number of ready threads in the system. */
[f761f1eb]72
[97f1691]73/** Take actions before new thread runs.
[70527f1]74 *
[b60a22c]75 * Perform actions that need to be
76 * taken before the newly selected
[df58e44]77 * thread is passed control.
[70527f1]78 *
[a3eeceb6]79 * THREAD->lock is locked on entry
80 *
[70527f1]81 */
[4e7d3dd]82static void before_thread_runs(void)
[0ca6faa]83{
[b49f4ae]84 before_thread_runs_arch();
[a35b458]85
[23f36a3]86 fpu_restore();
[a35b458]87
[5b7a107]88#ifdef CONFIG_UDEBUG
[df58e44]89 if (THREAD->btrace) {
90 istate_t *istate = THREAD->udebug.uspace_state;
91 if (istate != NULL) {
92 printf("Thread %" PRIu64 " stack trace:\n", THREAD->tid);
93 stack_trace_istate(istate);
94 }
[a35b458]95
[df58e44]96 THREAD->btrace = false;
97 }
[5b7a107]98#endif
[0ca6faa]99}
100
[5f85c91]101#ifdef CONFIG_FPU_LAZY
[b49f4ae]102void scheduler_fpu_lazy_request(void)
103{
104 fpu_enable();
[f3dbe27]105
106 /* We need this lock to ensure synchronization with thread destructor. */
[169815e]107 irq_spinlock_lock(&CPU->fpu_lock, false);
[a35b458]108
[a3eeceb6]109 /* Save old context */
[f3dbe27]110 thread_t *owner = atomic_load_explicit(&CPU->fpu_owner, memory_order_relaxed);
111 if (owner != NULL) {
112 fpu_context_save(&owner->fpu_context);
113 atomic_store_explicit(&CPU->fpu_owner, NULL, memory_order_relaxed);
[b49f4ae]114 }
[a35b458]115
[f3dbe27]116 irq_spinlock_unlock(&CPU->fpu_lock, false);
117
[7d6ec87]118 if (THREAD->fpu_context_exists) {
[0366d09d]119 fpu_context_restore(&THREAD->fpu_context);
[7d6ec87]120 } else {
[f76fed4]121 fpu_init();
[6eef3c4]122 THREAD->fpu_context_exists = true;
[b49f4ae]123 }
[a35b458]124
[f3dbe27]125 atomic_store_explicit(&CPU->fpu_owner, THREAD, memory_order_relaxed);
[b49f4ae]126}
[da1bafb]127#endif /* CONFIG_FPU_LAZY */
[0ca6faa]128
[70527f1]129/** Initialize scheduler
130 *
131 * Initialize kernel scheduler.
132 *
133 */
[f761f1eb]134void scheduler_init(void)
135{
136}
137
[70527f1]138/** Get thread to be scheduled
139 *
140 * Get the optimal thread to be scheduled
[d1a184f]141 * according to thread accounting and scheduler
[70527f1]142 * policy.
143 *
144 * @return Thread to be scheduled.
145 *
146 */
[ec8ef12]147static thread_t *try_find_thread(int *rq_index)
[f761f1eb]148{
[ec8ef12]149 assert(interrupts_disabled());
[63e27ef]150 assert(CPU != NULL);
[a35b458]151
[ec8ef12]152 if (atomic_load(&CPU->nrdy) == 0)
153 return NULL;
[a35b458]154
[ec8ef12]155 for (int i = 0; i < RQ_COUNT; i++) {
[da1bafb]156 irq_spinlock_lock(&(CPU->rq[i].lock), false);
157 if (CPU->rq[i].n == 0) {
[f761f1eb]158 /*
159 * If this queue is empty, try a lower-priority queue.
160 */
[da1bafb]161 irq_spinlock_unlock(&(CPU->rq[i].lock), false);
[f761f1eb]162 continue;
163 }
[a35b458]164
[248fc1a]165 atomic_dec(&CPU->nrdy);
[59e07c91]166 atomic_dec(&nrdy);
[da1bafb]167 CPU->rq[i].n--;
[a35b458]168
[f761f1eb]169 /*
170 * Take the first thread from the queue.
171 */
[55b77d9]172 thread_t *thread = list_get_instance(
173 list_first(&CPU->rq[i].rq), thread_t, rq_link);
[da1bafb]174 list_remove(&thread->rq_link);
[a35b458]175
[da1bafb]176 irq_spinlock_pass(&(CPU->rq[i].lock), &thread->lock);
[a35b458]177
[da1bafb]178 thread->cpu = CPU;
179 thread->priority = i; /* Correct rq index */
[a35b458]180
[aae2869]181 /* Time allocation in microseconds. */
182 uint64_t time_to_run = (i + 1) * 10000;
183
184 /* This is safe because interrupts are disabled. */
[4760793]185 CPU_LOCAL->preempt_deadline =
186 CPU_LOCAL->current_clock_tick + us2ticks(time_to_run);
[aae2869]187
[f761f1eb]188 /*
[6eef3c4]189 * Clear the stolen flag so that it can be migrated
[32fffef0]190 * when load balancing needs emerge.
[f761f1eb]191 */
[6eef3c4]192 thread->stolen = false;
[da1bafb]193 irq_spinlock_unlock(&thread->lock, false);
[a35b458]194
[117ad5a2]195 *rq_index = i;
[da1bafb]196 return thread;
[f761f1eb]197 }
[a35b458]198
[ec8ef12]199 return NULL;
200}
201
202/** Get thread to be scheduled
203 *
204 * Get the optimal thread to be scheduled
205 * according to thread accounting and scheduler
206 * policy.
207 *
208 * @return Thread to be scheduled.
209 *
210 */
211static thread_t *find_best_thread(int *rq_index)
212{
213 assert(interrupts_disabled());
214 assert(CPU != NULL);
215
216 while (true) {
217 thread_t *thread = try_find_thread(rq_index);
218
219 if (thread != NULL)
220 return thread;
221
222 /*
223 * For there was nothing to run, the CPU goes to sleep
224 * until a hardware interrupt or an IPI comes.
225 * This improves energy saving and hyperthreading.
226 */
[4760793]227 CPU_LOCAL->idle = true;
[ec8ef12]228
229 /*
230 * Go to sleep with interrupts enabled.
231 * Ideally, this should be atomic, but this is not guaranteed on
232 * all platforms yet, so it is possible we will go sleep when
233 * a thread has just become available.
234 */
235 cpu_interruptible_sleep();
236 }
[f761f1eb]237}
238
[c680333]239static void switch_task(task_t *task)
240{
241 /* If the task stays the same, a lot of work is avoided. */
242 if (TASK == task)
243 return;
244
245 as_t *old_as = AS;
246 as_t *new_as = task->as;
247
248 /* It is possible for two tasks to share one address space. */
249 if (old_as != new_as)
250 as_switch(old_as, new_as);
251
252 if (TASK)
253 task_release(TASK);
254
255 TASK = task;
256
257 task_hold(TASK);
258
259 before_task_runs_arch();
260}
261
[70527f1]262/** Prevent rq starvation
263 *
264 * Prevent low priority threads from starving in rq's.
265 *
266 * When the function decides to relink rq's, it reconnects
267 * respective pointers so that in result threads with 'pri'
[abbc16e]268 * greater or equal start are moved to a higher-priority queue.
[70527f1]269 *
270 * @param start Threshold priority.
271 *
[f761f1eb]272 */
[e16e036a]273static void relink_rq(int start)
[f761f1eb]274{
[4760793]275 if (CPU_LOCAL->current_clock_tick < CPU_LOCAL->relink_deadline)
[011c79a]276 return;
277
[4760793]278 CPU_LOCAL->relink_deadline = CPU_LOCAL->current_clock_tick + NEEDS_RELINK_MAX;
[a35b458]279
[3118355]280 /* Temporary cache for lists we are moving. */
[011c79a]281 list_t list;
[55b77d9]282 list_initialize(&list);
[a35b458]283
[3118355]284 size_t n = 0;
285
286 /* Move every list (except the one with highest priority) one level up. */
287 for (int i = RQ_COUNT - 1; i > start; i--) {
288 irq_spinlock_lock(&CPU->rq[i].lock, false);
[a35b458]289
[3118355]290 /* Swap lists. */
291 list_swap(&CPU->rq[i].rq, &list);
[a35b458]292
[3118355]293 /* Swap number of items. */
294 size_t tmpn = CPU->rq[i].n;
295 CPU->rq[i].n = n;
296 n = tmpn;
[a35b458]297
[011c79a]298 irq_spinlock_unlock(&CPU->rq[i].lock, false);
[f761f1eb]299 }
[a35b458]300
[3118355]301 /* Append the contents of rq[start + 1] to rq[start]. */
302 if (n != 0) {
303 irq_spinlock_lock(&CPU->rq[start].lock, false);
304 list_concat(&CPU->rq[start].rq, &list);
305 CPU->rq[start].n += n;
306 irq_spinlock_unlock(&CPU->rq[start].lock, false);
307 }
[f761f1eb]308}
309
[23f36a3]310/**
311 * Do whatever needs to be done with current FPU state before we switch to
312 * another thread.
313 */
314static void fpu_cleanup(void)
315{
316#if (defined CONFIG_FPU) && (!defined CONFIG_FPU_LAZY)
317 fpu_context_save(&THREAD->fpu_context);
318#endif
319}
320
321/**
322 * Set correct FPU state for this thread after switch from another thread.
323 */
324static void fpu_restore(void)
325{
326#ifdef CONFIG_FPU_LAZY
327 /*
328 * The only concurrent modification possible for fpu_owner here is
329 * another thread changing it from itself to NULL in its destructor.
330 */
331 thread_t *owner = atomic_load_explicit(&CPU->fpu_owner,
332 memory_order_relaxed);
333
334 if (THREAD == owner)
335 fpu_enable();
336 else
337 fpu_disable();
338
339#elif defined CONFIG_FPU
340 fpu_enable();
341 if (THREAD->fpu_context_exists)
342 fpu_context_restore(&THREAD->fpu_context);
343 else {
344 fpu_init();
345 THREAD->fpu_context_exists = true;
346 }
347#endif
348}
349
[111b9b9]350void scheduler(void)
351{
352 ipl_t ipl = interrupts_disable();
353
354 if (atomic_load(&haltstate))
355 halt();
356
357 if (THREAD) {
358 irq_spinlock_lock(&THREAD->lock, false);
359 }
360
361 scheduler_locked(ipl);
362}
363
[6e49dab]364static void cleanup_after_thread(thread_t *thread, state_t out_state)
365{
366 assert(CURRENT->mutex_locks == 0);
367 assert(interrupts_disabled());
368
369 int expected;
370
371 switch (out_state) {
372 case Running:
373 thread_ready(thread);
374 break;
375
376 case Exiting:
377 waitq_close(&thread->join_wq);
378
379 /*
380 * Release the reference CPU has for the thread.
381 * If there are no other references (e.g. threads calling join),
382 * the thread structure is deallocated.
383 */
384 thread_put(thread);
385 break;
386
387 case Sleeping:
388 expected = SLEEP_INITIAL;
389
390 /* Only set SLEEP_ASLEEP in sleep pad if it's still in initial state */
391 if (!atomic_compare_exchange_strong_explicit(&thread->sleep_state,
392 &expected, SLEEP_ASLEEP,
393 memory_order_acq_rel, memory_order_acquire)) {
394
395 assert(expected == SLEEP_WOKE);
396 /* The thread has already been woken up, requeue immediately. */
397 thread_ready(thread);
398 }
399 break;
400
401 default:
402 /*
403 * Entering state is unexpected.
404 */
405 panic("tid%" PRIu64 ": unexpected state %s.",
406 thread->tid, thread_states[thread->state]);
407 break;
408 }
409}
410
[7d6ec87]411/** The scheduler
412 *
413 * The thread scheduling procedure.
414 * Passes control directly to
415 * scheduler_separated_stack().
416 *
417 */
[111b9b9]418void scheduler_locked(ipl_t ipl)
[7d6ec87]419{
[63e27ef]420 assert(CPU != NULL);
[a35b458]421
[7d6ec87]422 if (THREAD) {
[1ba37fa]423 /* Update thread kernel accounting */
[a2a00e8]424 THREAD->kcycles += get_cycle() - THREAD->last_cycle;
[a35b458]425
[23f36a3]426 fpu_cleanup();
427
[7d6ec87]428 if (!context_save(&THREAD->saved_context)) {
429 /*
430 * This is the place where threads leave scheduler();
431 */
[a35b458]432
[cce6acf]433 /* Save current CPU cycle */
434 THREAD->last_cycle = get_cycle();
[a35b458]435
[da1bafb]436 irq_spinlock_unlock(&THREAD->lock, false);
[c030818]437 interrupts_restore(THREAD->saved_ipl);
[a35b458]438
[7d6ec87]439 return;
440 }
[a35b458]441
[7d6ec87]442 /*
[4e33b6b]443 * Interrupt priority level of preempted thread is recorded
444 * here to facilitate scheduler() invocations from
[da1bafb]445 * interrupts_disable()'d code (e.g. waitq_sleep_timeout()).
446 *
[7d6ec87]447 */
[c030818]448 THREAD->saved_ipl = ipl;
[7d6ec87]449 }
[a35b458]450
[7d6ec87]451 /*
[a6e55886]452 * Through the 'CURRENT' structure, we keep track of THREAD, TASK, CPU, AS
453 * and preemption counter. At this point CURRENT could be coming either
[7d6ec87]454 * from THREAD's or CPU's stack.
[da1bafb]455 *
[7d6ec87]456 */
[4760793]457 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
[a35b458]458
[7d6ec87]459 /*
460 * We may not keep the old stack.
461 * Reason: If we kept the old stack and got blocked, for instance, in
462 * find_best_thread(), the old thread could get rescheduled by another
463 * CPU and overwrite the part of its own stack that was also used by
464 * the scheduler on this CPU.
465 *
466 * Moreover, we have to bypass the compiler-generated POP sequence
467 * which is fooled by SP being set to the very top of the stack.
468 * Therefore the scheduler() function continues in
469 * scheduler_separated_stack().
[da1bafb]470 *
[7d6ec87]471 */
[daadfa6]472 context_t ctx;
473 context_save(&ctx);
474 context_set(&ctx, FADDR(scheduler_separated_stack),
[4760793]475 (uintptr_t) CPU_LOCAL->stack, STACK_SIZE);
[daadfa6]476 context_restore(&ctx);
[a35b458]477
[da1bafb]478 /* Not reached */
[7d6ec87]479}
[70527f1]480
481/** Scheduler stack switch wrapper
482 *
483 * Second part of the scheduler() function
484 * using new stack. Handling the actual context
485 * switch to a new thread.
486 *
487 */
[7d6ec87]488void scheduler_separated_stack(void)
[f761f1eb]489{
[63e27ef]490 assert((!THREAD) || (irq_spinlock_locked(&THREAD->lock)));
491 assert(CPU != NULL);
492 assert(interrupts_disabled());
[a35b458]493
[43114c5]494 if (THREAD) {
[6e49dab]495 after_thread_ran_arch();
[a35b458]496
[6e49dab]497 state_t state = THREAD->state;
[5663872]498
[6e49dab]499 if (state == Sleeping) {
500 /* Prefer the thread after it's woken up. */
501 THREAD->priority = -1;
502 }
[a35b458]503
[6e49dab]504 irq_spinlock_unlock(&THREAD->lock, false);
[1871118]505
[6e49dab]506 cleanup_after_thread(THREAD, state);
[a35b458]507
[43114c5]508 THREAD = NULL;
[f761f1eb]509 }
[a35b458]510
[117ad5a2]511 int rq_index;
512 THREAD = find_best_thread(&rq_index);
[a35b458]513
[117ad5a2]514 relink_rq(rq_index);
[a35b458]515
[c680333]516 switch_task(THREAD->task);
[a35b458]517
[da1bafb]518 irq_spinlock_lock(&THREAD->lock, false);
[43114c5]519 THREAD->state = Running;
[a35b458]520
[f76fed4]521#ifdef SCHEDULER_VERBOSE
[b2fa1204]522 log(LF_OTHER, LVL_DEBUG,
523 "cpu%u: tid %" PRIu64 " (priority=%d, ticks=%" PRIu64
[077842c]524 ", nrdy=%zu)", CPU->id, THREAD->tid, THREAD->priority,
[036e97c]525 THREAD->ticks, atomic_load(&CPU->nrdy));
[da1bafb]526#endif
[a35b458]527
[97f1691]528 /*
529 * Some architectures provide late kernel PA2KA(identity)
530 * mapping in a page fault handler. However, the page fault
531 * handler uses the kernel stack of the running thread and
532 * therefore cannot be used to map it. The kernel stack, if
533 * necessary, is to be mapped in before_thread_runs(). This
534 * function must be executed before the switch to the new stack.
535 */
536 before_thread_runs();
[a35b458]537
[3e1607f]538 /*
[4e33b6b]539 * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
540 * thread's stack.
[3e1607f]541 */
[a6e55886]542 current_copy(CURRENT, (current_t *) THREAD->kstack);
[a35b458]543
[43114c5]544 context_restore(&THREAD->saved_context);
[a35b458]545
[da1bafb]546 /* Not reached */
[f761f1eb]547}
548
[5f85c91]549#ifdef CONFIG_SMP
[fbaf6ac]550
551static thread_t *steal_thread_from(cpu_t *old_cpu, int i)
552{
553 runq_t *old_rq = &old_cpu->rq[i];
554 runq_t *new_rq = &CPU->rq[i];
555
[06f81c4]556 ipl_t ipl = interrupts_disable();
557
558 irq_spinlock_lock(&old_rq->lock, false);
[fbaf6ac]559
[f3dbe27]560 /*
561 * If fpu_owner is any thread in the list, its store is seen here thanks to
562 * the runqueue lock.
563 */
564 thread_t *fpu_owner = atomic_load_explicit(&old_cpu->fpu_owner,
565 memory_order_relaxed);
566
[fbaf6ac]567 /* Search rq from the back */
568 list_foreach_rev(old_rq->rq, rq_link, thread_t, thread) {
569
570 irq_spinlock_lock(&thread->lock, false);
571
572 /*
573 * Do not steal CPU-wired threads, threads
574 * already stolen, threads for which migration
575 * was temporarily disabled or threads whose
576 * FPU context is still in the CPU.
577 */
[06f81c4]578 if (thread->stolen || thread->nomigrate ||
[f3dbe27]579 thread == fpu_owner) {
[fbaf6ac]580 irq_spinlock_unlock(&thread->lock, false);
581 continue;
582 }
583
584 thread->stolen = true;
585 thread->cpu = CPU;
586
587 irq_spinlock_unlock(&thread->lock, false);
588
589 /*
590 * Ready thread on local CPU
591 */
592
593#ifdef KCPULB_VERBOSE
594 log(LF_OTHER, LVL_DEBUG,
595 "kcpulb%u: TID %" PRIu64 " -> cpu%u, "
596 "nrdy=%ld, avg=%ld", CPU->id, thread->tid,
597 CPU->id, atomic_load(&CPU->nrdy),
598 atomic_load(&nrdy) / config.cpu_active);
599#endif
600
601 /* Remove thread from ready queue. */
602 old_rq->n--;
603 list_remove(&thread->rq_link);
[06f81c4]604 irq_spinlock_unlock(&old_rq->lock, false);
[fbaf6ac]605
606 /* Append thread to local queue. */
[06f81c4]607 irq_spinlock_lock(&new_rq->lock, false);
[fbaf6ac]608 list_append(&thread->rq_link, &new_rq->rq);
609 new_rq->n++;
[06f81c4]610 irq_spinlock_unlock(&new_rq->lock, false);
[fbaf6ac]611
612 atomic_dec(&old_cpu->nrdy);
613 atomic_inc(&CPU->nrdy);
[06f81c4]614 interrupts_restore(ipl);
[fbaf6ac]615 return thread;
616 }
617
[06f81c4]618 irq_spinlock_unlock(&old_rq->lock, false);
619 interrupts_restore(ipl);
[fbaf6ac]620 return NULL;
621}
622
[70527f1]623/** Load balancing thread
624 *
625 * SMP load balancing thread, supervising thread supplies
626 * for the CPU it's wired to.
627 *
628 * @param arg Generic thread argument (unused).
629 *
[f761f1eb]630 */
631void kcpulb(void *arg)
632{
[3cfe2b8]633 size_t average;
634 size_t rdy;
[a35b458]635
[f761f1eb]636loop:
637 /*
[3260ada]638 * Work in 1s intervals.
[f761f1eb]639 */
[3260ada]640 thread_sleep(1);
[a35b458]641
[f761f1eb]642not_satisfied:
643 /*
644 * Calculate the number of threads that will be migrated/stolen from
645 * other CPU's. Note that situation can have changed between two
646 * passes. Each time get the most up to date counts.
[da1bafb]647 *
[f761f1eb]648 */
[036e97c]649 average = atomic_load(&nrdy) / config.cpu_active + 1;
650 rdy = atomic_load(&CPU->nrdy);
[a35b458]651
[da1bafb]652 if (average <= rdy)
[f761f1eb]653 goto satisfied;
[a35b458]654
[3cfe2b8]655 size_t count = average - rdy;
[a35b458]656
[f761f1eb]657 /*
[4e33b6b]658 * Searching least priority queues on all CPU's first and most priority
659 * queues on all CPU's last.
[f761f1eb]660 */
[da1bafb]661 size_t acpu;
662 int rq;
[a35b458]663
[da1bafb]664 for (rq = RQ_COUNT - 1; rq >= 0; rq--) {
665 for (acpu = 0; acpu < config.cpu_active; acpu++) {
[fbaf6ac]666 cpu_t *cpu = &cpus[acpu];
[a35b458]667
[f761f1eb]668 /*
669 * Not interested in ourselves.
[4e33b6b]670 * Doesn't require interrupt disabling for kcpulb has
671 * THREAD_FLAG_WIRED.
[da1bafb]672 *
[f761f1eb]673 */
[43114c5]674 if (CPU == cpu)
[248fc1a]675 continue;
[a35b458]676
[036e97c]677 if (atomic_load(&cpu->nrdy) <= average)
[248fc1a]678 continue;
[a35b458]679
[fbaf6ac]680 if (steal_thread_from(cpu, rq) && --count == 0)
681 goto satisfied;
[f761f1eb]682 }
683 }
[a35b458]684
[036e97c]685 if (atomic_load(&CPU->nrdy)) {
[f761f1eb]686 /*
687 * Be a little bit light-weight and let migrated threads run.
[da1bafb]688 *
[f761f1eb]689 */
690 scheduler();
[3260ada]691 } else {
[f761f1eb]692 /*
693 * We failed to migrate a single thread.
[3260ada]694 * Give up this turn.
[da1bafb]695 *
[f761f1eb]696 */
[3260ada]697 goto loop;
[f761f1eb]698 }
[a35b458]699
[f761f1eb]700 goto not_satisfied;
[a35b458]701
[f761f1eb]702satisfied:
703 goto loop;
704}
[5f85c91]705#endif /* CONFIG_SMP */
[10e16a7]706
[da1bafb]707/** Print information about threads & scheduler queues
708 *
709 */
[10e16a7]710void sched_print_list(void)
711{
[da1bafb]712 size_t cpu;
[4184e76]713 for (cpu = 0; cpu < config.cpu_count; cpu++) {
[10e16a7]714 if (!cpus[cpu].active)
715 continue;
[a35b458]716
[3b68542]717 printf("cpu%u: address=%p, nrdy=%zu\n",
718 cpus[cpu].id, &cpus[cpu], atomic_load(&cpus[cpu].nrdy));
[a35b458]719
[da1bafb]720 unsigned int i;
[4e33b6b]721 for (i = 0; i < RQ_COUNT; i++) {
[da1bafb]722 irq_spinlock_lock(&(cpus[cpu].rq[i].lock), false);
723 if (cpus[cpu].rq[i].n == 0) {
724 irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
[10e16a7]725 continue;
726 }
[a35b458]727
[5b86d10]728 printf("\trq[%u]: ", i);
[feeac0d]729 list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
730 thread) {
[da1bafb]731 printf("%" PRIu64 "(%s) ", thread->tid,
732 thread_states[thread->state]);
[10e16a7]733 }
734 printf("\n");
[a35b458]735
[da1bafb]736 irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
[10e16a7]737 }
738 }
739}
[b45c443]740
[cc73a8a1]741/** @}
[b45c443]742 */
Note: See TracBrowser for help on using the repository browser.