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

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

Add CPU_LOCAL alongside CPU and segregate fields that are only used locally

This makes it more clear which fields can be used without synchronization
and which need more care.

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