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

Last change on this file since bb65ccb3 was c680333, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Move task switch handling into a separate function

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