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

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

Get thread priority from find_best_thread(), instead of locking thread again

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