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

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

Add functions context_create(), context_replace() and context_swap()

and use them where appropriate, removing context_save() in the process.
Much like in userspace, context_swap() maintains natural control flow
as opposed to context_save()'s return-twice mechanic.

Beyond that, in the future, context_replace() and context_swap()
can be implemented more efficiently than the context_save()/
context_restore() pair. As of now, the original implementation is
retained.

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