source: mainline/kernel/generic/src/proc/scheduler.c@ 06f81c4

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

Check cpu_t::fpu_owner directly instead of thread_t::fpu_context_engaged

This results in net reduction in locking.

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