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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b23c88e was b23c88e, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Replaced memory barriers with compiler barriers. Added checks if reschedule is needed once preemption is enabled.

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