Subversion Repositories HelenOS

Rev

Rev 2292 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2001-2007 Jakub Jermar
  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.  
  29. /** @addtogroup genericproc
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file
  35.  * @brief   Scheduler and load balancing.
  36.  *
  37.  * This file contains the scheduler and kcpulb kernel thread which
  38.  * performs load-balancing of per-CPU run queues.
  39.  */
  40.  
  41. #include <proc/scheduler.h>
  42. #include <proc/thread.h>
  43. #include <proc/task.h>
  44. #include <mm/frame.h>
  45. #include <mm/page.h>
  46. #include <mm/as.h>
  47. #include <time/timeout.h>
  48. #include <time/delay.h>
  49. #include <arch/asm.h>
  50. #include <arch/faddr.h>
  51. #include <arch/cycle.h>
  52. #include <atomic.h>
  53. #include <synch/spinlock.h>
  54. #include <config.h>
  55. #include <context.h>
  56. #include <fpu_context.h>
  57. #include <func.h>
  58. #include <arch.h>
  59. #include <adt/list.h>
  60. #include <panic.h>
  61. #include <cpu.h>
  62. #include <print.h>
  63. #include <debug.h>
  64. #include <synch/rcu.h>
  65.  
  66. static void before_task_runs(void);
  67. static void before_thread_runs(void);
  68. static void after_thread_ran(void);
  69. static void scheduler_separated_stack(void);
  70.  
  71. atomic_t nrdy;  /**< Number of ready threads in the system. */
  72.  
  73. /** Carry out actions before new task runs. */
  74. void before_task_runs(void)
  75. {
  76.     before_task_runs_arch();
  77. }
  78.  
  79. /** Take actions before new thread runs.
  80.  *
  81.  * Perform actions that need to be
  82.  * taken before the newly selected
  83.  * tread is passed control.
  84.  *
  85.  * THREAD->lock is locked on entry
  86.  *
  87.  */
  88. void before_thread_runs(void)
  89. {
  90.     before_thread_runs_arch();
  91. #ifdef CONFIG_FPU_LAZY
  92.     if(THREAD == CPU->fpu_owner)
  93.         fpu_enable();
  94.     else
  95.         fpu_disable();
  96. #else
  97.     fpu_enable();
  98.     if (THREAD->fpu_context_exists)
  99.         fpu_context_restore(THREAD->saved_fpu_context);
  100.     else {
  101.         fpu_init();
  102.         THREAD->fpu_context_exists = 1;
  103.     }
  104. #endif
  105. }
  106.  
  107. /** Take actions after THREAD had run.
  108.  *
  109.  * Perform actions that need to be
  110.  * taken after the running thread
  111.  * had been preempted by the scheduler.
  112.  *
  113.  * THREAD->lock is locked on entry
  114.  *
  115.  */
  116. void after_thread_ran(void)
  117. {
  118.     after_thread_ran_arch();
  119.     rcu_run_callbacks();
  120. }
  121.  
  122. #ifdef CONFIG_FPU_LAZY
  123. void scheduler_fpu_lazy_request(void)
  124. {
  125. restart:
  126.     fpu_enable();
  127.     spinlock_lock(&CPU->lock);
  128.  
  129.     /* Save old context */
  130.     if (CPU->fpu_owner != NULL) {  
  131.         spinlock_lock(&CPU->fpu_owner->lock);
  132.         fpu_context_save(CPU->fpu_owner->saved_fpu_context);
  133.         /* don't prevent migration */
  134.         CPU->fpu_owner->fpu_context_engaged = 0;
  135.         spinlock_unlock(&CPU->fpu_owner->lock);
  136.         CPU->fpu_owner = NULL;
  137.     }
  138.  
  139.     spinlock_lock(&THREAD->lock);
  140.     if (THREAD->fpu_context_exists) {
  141.         fpu_context_restore(THREAD->saved_fpu_context);
  142.     } else {
  143.         /* Allocate FPU context */
  144.         if (!THREAD->saved_fpu_context) {
  145.             /* Might sleep */
  146.             spinlock_unlock(&THREAD->lock);
  147.             spinlock_unlock(&CPU->lock);
  148.             THREAD->saved_fpu_context =
  149.                 (fpu_context_t *) slab_alloc(fpu_context_slab, 0);
  150.             /* We may have switched CPUs during slab_alloc */
  151.             goto restart;
  152.         }
  153.         fpu_init();
  154.         THREAD->fpu_context_exists = 1;
  155.     }
  156.     CPU->fpu_owner = THREAD;
  157.     THREAD->fpu_context_engaged = 1;
  158.     spinlock_unlock(&THREAD->lock);
  159.  
  160.     spinlock_unlock(&CPU->lock);
  161. }
  162. #endif
  163.  
  164. /** Initialize scheduler
  165.  *
  166.  * Initialize kernel scheduler.
  167.  *
  168.  */
  169. void scheduler_init(void)
  170. {
  171. }
  172.  
  173. /** Get thread to be scheduled
  174.  *
  175.  * Get the optimal thread to be scheduled
  176.  * according to thread accounting and scheduler
  177.  * policy.
  178.  *
  179.  * @return Thread to be scheduled.
  180.  *
  181.  */
  182. static thread_t *find_best_thread(void)
  183. {
  184.     thread_t *t;
  185.     runq_t *r;
  186.     int i;
  187.  
  188.     ASSERT(CPU != NULL);
  189.  
  190. loop:
  191.     interrupts_enable();
  192.    
  193.     if (atomic_get(&CPU->nrdy) == 0) {
  194.         /*
  195.          * For there was nothing to run, the CPU goes to sleep
  196.          * until a hardware interrupt or an IPI comes.
  197.          * This improves energy saving and hyperthreading.
  198.          */
  199.  
  200.         /*
  201.          * An interrupt might occur right now and wake up a thread.
  202.          * In such case, the CPU will continue to go to sleep
  203.          * even though there is a runnable thread.
  204.          */
  205.  
  206.          cpu_sleep();
  207.          goto loop;
  208.     }
  209.  
  210.     interrupts_disable();
  211.    
  212.     for (i = 0; i < RQ_COUNT; i++) {
  213.         r = &CPU->rq[i];
  214.         spinlock_lock(&r->lock);
  215.         if (r->n == 0) {
  216.             /*
  217.              * If this queue is empty, try a lower-priority queue.
  218.              */
  219.             spinlock_unlock(&r->lock);
  220.             continue;
  221.         }
  222.  
  223.         atomic_dec(&CPU->nrdy);
  224.         atomic_dec(&nrdy);
  225.         r->n--;
  226.  
  227.         /*
  228.          * Take the first thread from the queue.
  229.          */
  230.         t = list_get_instance(r->rq_head.next, thread_t, rq_link);
  231.         list_remove(&t->rq_link);
  232.  
  233.         spinlock_unlock(&r->lock);
  234.  
  235.         spinlock_lock(&t->lock);
  236.         t->cpu = CPU;
  237.  
  238.         t->ticks = us2ticks((i + 1) * 10000);
  239.         t->priority = i;    /* correct rq index */
  240.  
  241.         /*
  242.          * Clear the THREAD_FLAG_STOLEN flag so that t can be migrated
  243.          * when load balancing needs emerge.
  244.          */
  245.         t->flags &= ~THREAD_FLAG_STOLEN;
  246.         spinlock_unlock(&t->lock);
  247.  
  248.         return t;
  249.     }
  250.     goto loop;
  251.  
  252. }
  253.  
  254. /** Prevent rq starvation
  255.  *
  256.  * Prevent low priority threads from starving in rq's.
  257.  *
  258.  * When the function decides to relink rq's, it reconnects
  259.  * respective pointers so that in result threads with 'pri'
  260.  * greater or equal start are moved to a higher-priority queue.
  261.  *
  262.  * @param start Threshold priority.
  263.  *
  264.  */
  265. static void relink_rq(int start)
  266. {
  267.     link_t head;
  268.     runq_t *r;
  269.     int i, n;
  270.  
  271.     list_initialize(&head);
  272.     spinlock_lock(&CPU->lock);
  273.     if (CPU->needs_relink > NEEDS_RELINK_MAX) {
  274.         for (i = start; i < RQ_COUNT - 1; i++) {
  275.             /* remember and empty rq[i + 1] */
  276.             r = &CPU->rq[i + 1];
  277.             spinlock_lock(&r->lock);
  278.             list_concat(&head, &r->rq_head);
  279.             n = r->n;
  280.             r->n = 0;
  281.             spinlock_unlock(&r->lock);
  282.        
  283.             /* append rq[i + 1] to rq[i] */
  284.             r = &CPU->rq[i];
  285.             spinlock_lock(&r->lock);
  286.             list_concat(&r->rq_head, &head);
  287.             r->n += n;
  288.             spinlock_unlock(&r->lock);
  289.         }
  290.         CPU->needs_relink = 0;
  291.     }
  292.     spinlock_unlock(&CPU->lock);
  293.  
  294. }
  295.  
  296. /** The scheduler
  297.  *
  298.  * The thread scheduling procedure.
  299.  * Passes control directly to
  300.  * scheduler_separated_stack().
  301.  *
  302.  */
  303. void scheduler(void)
  304. {
  305.     volatile ipl_t ipl;
  306.  
  307.     ASSERT(CPU != NULL);
  308.  
  309.     ipl = interrupts_disable();
  310.  
  311.     if (atomic_get(&haltstate))
  312.         halt();
  313.    
  314.     if (THREAD) {
  315.         spinlock_lock(&THREAD->lock);
  316.        
  317.         /* Update thread accounting */
  318.         THREAD->cycles += get_cycle() - THREAD->last_cycle;
  319.        
  320. #ifndef CONFIG_FPU_LAZY
  321.         fpu_context_save(THREAD->saved_fpu_context);
  322. #endif
  323.         if (!context_save(&THREAD->saved_context)) {
  324.             /*
  325.              * This is the place where threads leave scheduler();
  326.              */
  327.            
  328.             /* Save current CPU cycle */
  329.             THREAD->last_cycle = get_cycle();
  330.            
  331.             spinlock_unlock(&THREAD->lock);
  332.             interrupts_restore(THREAD->saved_context.ipl);
  333.            
  334.             return;
  335.         }
  336.  
  337.         /*
  338.          * Interrupt priority level of preempted thread is recorded
  339.          * here to facilitate scheduler() invocations from
  340.          * interrupts_disable()'d code (e.g. waitq_sleep_timeout()).
  341.          */
  342.         THREAD->saved_context.ipl = ipl;
  343.     }
  344.  
  345.     /*
  346.      * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM
  347.      * and preemption counter. At this point THE could be coming either
  348.      * from THREAD's or CPU's stack.
  349.      */
  350.     the_copy(THE, (the_t *) CPU->stack);
  351.  
  352.     /*
  353.      * We may not keep the old stack.
  354.      * Reason: If we kept the old stack and got blocked, for instance, in
  355.      * find_best_thread(), the old thread could get rescheduled by another
  356.      * CPU and overwrite the part of its own stack that was also used by
  357.      * the scheduler on this CPU.
  358.      *
  359.      * Moreover, we have to bypass the compiler-generated POP sequence
  360.      * which is fooled by SP being set to the very top of the stack.
  361.      * Therefore the scheduler() function continues in
  362.      * scheduler_separated_stack().
  363.      */
  364.     context_save(&CPU->saved_context);
  365.     context_set(&CPU->saved_context, FADDR(scheduler_separated_stack),
  366.         (uintptr_t) CPU->stack, CPU_STACK_SIZE);
  367.     context_restore(&CPU->saved_context);
  368.     /* not reached */
  369. }
  370.  
  371. /** Scheduler stack switch wrapper
  372.  *
  373.  * Second part of the scheduler() function
  374.  * using new stack. Handling the actual context
  375.  * switch to a new thread.
  376.  *
  377.  * Assume THREAD->lock is held.
  378.  */
  379. void scheduler_separated_stack(void)
  380. {
  381.     int priority;
  382.     DEADLOCK_PROBE_INIT(p_joinwq);
  383.  
  384.     ASSERT(CPU != NULL);
  385.    
  386.     if (THREAD) {
  387.         /* must be run after the switch to scheduler stack */
  388.         after_thread_ran();
  389.  
  390.         switch (THREAD->state) {
  391.         case Running:
  392.             spinlock_unlock(&THREAD->lock);
  393.             thread_ready(THREAD);
  394.             break;
  395.  
  396.         case Exiting:
  397. repeat:
  398.             if (THREAD->detached) {
  399.                 thread_destroy(THREAD);
  400.             } else {
  401.                 /*
  402.                  * The thread structure is kept allocated until
  403.                  * somebody calls thread_detach() on it.
  404.                  */
  405.                 if (!spinlock_trylock(&THREAD->join_wq.lock)) {
  406.                     /*
  407.                      * Avoid deadlock.
  408.                      */
  409.                     spinlock_unlock(&THREAD->lock);
  410.                     delay(10);
  411.                     spinlock_lock(&THREAD->lock);
  412.                     DEADLOCK_PROBE(p_joinwq,
  413.                         DEADLOCK_THRESHOLD);
  414.                     goto repeat;
  415.                 }
  416.                 _waitq_wakeup_unsafe(&THREAD->join_wq, false);
  417.                 spinlock_unlock(&THREAD->join_wq.lock);
  418.                
  419.                 THREAD->state = Undead;
  420.                 spinlock_unlock(&THREAD->lock);
  421.             }
  422.             break;
  423.            
  424.         case Sleeping:
  425.             /*
  426.              * Prefer the thread after it's woken up.
  427.              */
  428.             THREAD->priority = -1;
  429.  
  430.             /*
  431.              * We need to release wq->lock which we locked in
  432.              * waitq_sleep(). Address of wq->lock is kept in
  433.              * THREAD->sleep_queue.
  434.              */
  435.             spinlock_unlock(&THREAD->sleep_queue->lock);
  436.  
  437.             /*
  438.              * Check for possible requests for out-of-context
  439.              * invocation.
  440.              */
  441.             if (THREAD->call_me) {
  442.                 THREAD->call_me(THREAD->call_me_with);
  443.                 THREAD->call_me = NULL;
  444.                 THREAD->call_me_with = NULL;
  445.             }
  446.  
  447.             spinlock_unlock(&THREAD->lock);
  448.  
  449.             break;
  450.  
  451.         default:
  452.             /*
  453.              * Entering state is unexpected.
  454.              */
  455.             panic("tid%llu: unexpected state %s\n", THREAD->tid,
  456.                 thread_states[THREAD->state]);
  457.             break;
  458.         }
  459.  
  460.         THREAD = NULL;
  461.     }
  462.  
  463.     THREAD = find_best_thread();
  464.    
  465.     spinlock_lock(&THREAD->lock);
  466.     priority = THREAD->priority;
  467.     spinlock_unlock(&THREAD->lock);
  468.  
  469.     relink_rq(priority);       
  470.  
  471.     /*
  472.      * If both the old and the new task are the same, lots of work is
  473.      * avoided.
  474.      */
  475.     if (TASK != THREAD->task) {
  476.         as_t *as1 = NULL;
  477.         as_t *as2;
  478.  
  479.         if (TASK) {
  480.             spinlock_lock(&TASK->lock);
  481.             as1 = TASK->as;
  482.             spinlock_unlock(&TASK->lock);
  483.         }
  484.  
  485.         spinlock_lock(&THREAD->task->lock);
  486.         as2 = THREAD->task->as;
  487.         spinlock_unlock(&THREAD->task->lock);
  488.        
  489.         /*
  490.          * Note that it is possible for two tasks to share one address
  491.          * space.
  492.          */
  493.         if (as1 != as2) {
  494.             /*
  495.              * Both tasks and address spaces are different.
  496.              * Replace the old one with the new one.
  497.              */
  498.             as_switch(as1, as2);
  499.         }
  500.         TASK = THREAD->task;
  501.         before_task_runs();
  502.     }
  503.  
  504.     spinlock_lock(&THREAD->lock);  
  505.     THREAD->state = Running;
  506.  
  507. #ifdef SCHEDULER_VERBOSE
  508.     printf("cpu%d: tid %llu (priority=%d, ticks=%llu, nrdy=%ld)\n",
  509.         CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks,
  510.         atomic_get(&CPU->nrdy));
  511. #endif 
  512.  
  513.     /*
  514.      * Some architectures provide late kernel PA2KA(identity)
  515.      * mapping in a page fault handler. However, the page fault
  516.      * handler uses the kernel stack of the running thread and
  517.      * therefore cannot be used to map it. The kernel stack, if
  518.      * necessary, is to be mapped in before_thread_runs(). This
  519.      * function must be executed before the switch to the new stack.
  520.      */
  521.     before_thread_runs();
  522.  
  523.     /*
  524.      * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
  525.      * thread's stack.
  526.      */
  527.     the_copy(THE, (the_t *) THREAD->kstack);
  528.    
  529.     context_restore(&THREAD->saved_context);
  530.     /* not reached */
  531. }
  532.  
  533. #ifdef CONFIG_SMP
  534. /** Load balancing thread
  535.  *
  536.  * SMP load balancing thread, supervising thread supplies
  537.  * for the CPU it's wired to.
  538.  *
  539.  * @param arg Generic thread argument (unused).
  540.  *
  541.  */
  542. void kcpulb(void *arg)
  543. {
  544.     thread_t *t;
  545.     int count, average, j, k = 0;
  546.     unsigned int i;
  547.     ipl_t ipl;
  548.  
  549.     /*
  550.      * Detach kcpulb as nobody will call thread_join_timeout() on it.
  551.      */
  552.     thread_detach(THREAD);
  553.    
  554. loop:
  555.     /*
  556.      * Work in 1s intervals.
  557.      */
  558.     thread_sleep(1);
  559.  
  560. not_satisfied:
  561.     /*
  562.      * Calculate the number of threads that will be migrated/stolen from
  563.      * other CPU's. Note that situation can have changed between two
  564.      * passes. Each time get the most up to date counts.
  565.      */
  566.     average = atomic_get(&nrdy) / config.cpu_active + 1;
  567.     count = average - atomic_get(&CPU->nrdy);
  568.  
  569.     if (count <= 0)
  570.         goto satisfied;
  571.  
  572.     /*
  573.      * Searching least priority queues on all CPU's first and most priority
  574.      * queues on all CPU's last.
  575.      */
  576.     for (j = RQ_COUNT - 1; j >= 0; j--) {
  577.         for (i = 0; i < config.cpu_active; i++) {
  578.             link_t *l;
  579.             runq_t *r;
  580.             cpu_t *cpu;
  581.  
  582.             cpu = &cpus[(i + k) % config.cpu_active];
  583.  
  584.             /*
  585.              * Not interested in ourselves.
  586.              * Doesn't require interrupt disabling for kcpulb has
  587.              * THREAD_FLAG_WIRED.
  588.              */
  589.             if (CPU == cpu)
  590.                 continue;
  591.             if (atomic_get(&cpu->nrdy) <= average)
  592.                 continue;
  593.  
  594.             ipl = interrupts_disable();
  595.             r = &cpu->rq[j];
  596.             spinlock_lock(&r->lock);
  597.             if (r->n == 0) {
  598.                 spinlock_unlock(&r->lock);
  599.                 interrupts_restore(ipl);
  600.                 continue;
  601.             }
  602.        
  603.             t = NULL;
  604.             l = r->rq_head.prev;    /* search rq from the back */
  605.             while (l != &r->rq_head) {
  606.                 t = list_get_instance(l, thread_t, rq_link);
  607.                 /*
  608.                  * We don't want to steal CPU-wired threads
  609.                  * neither threads already stolen. The latter
  610.                  * prevents threads from migrating between CPU's
  611.                  * without ever being run. We don't want to
  612.                  * steal threads whose FPU context is still in
  613.                  * CPU.
  614.                  */
  615.                 spinlock_lock(&t->lock);
  616.                 if ((!(t->flags & (THREAD_FLAG_WIRED |
  617.                     THREAD_FLAG_STOLEN))) &&
  618.                     (!(t->fpu_context_engaged))) {
  619.                     /*
  620.                      * Remove t from r.
  621.                      */
  622.                     spinlock_unlock(&t->lock);
  623.                    
  624.                     atomic_dec(&cpu->nrdy);
  625.                     atomic_dec(&nrdy);
  626.  
  627.                     r->n--;
  628.                     list_remove(&t->rq_link);
  629.  
  630.                     break;
  631.                 }
  632.                 spinlock_unlock(&t->lock);
  633.                 l = l->prev;
  634.                 t = NULL;
  635.             }
  636.             spinlock_unlock(&r->lock);
  637.  
  638.             if (t) {
  639.                 /*
  640.                  * Ready t on local CPU
  641.                  */
  642.                 spinlock_lock(&t->lock);
  643. #ifdef KCPULB_VERBOSE
  644.                 printf("kcpulb%d: TID %llu -> cpu%d, nrdy=%ld, "
  645.                     "avg=%nd\n", CPU->id, t->tid, CPU->id,
  646.                     atomic_get(&CPU->nrdy),
  647.                     atomic_get(&nrdy) / config.cpu_active);
  648. #endif
  649.                 t->flags |= THREAD_FLAG_STOLEN;
  650.                 t->state = Entering;
  651.                 spinlock_unlock(&t->lock);
  652.    
  653.                 thread_ready(t);
  654.  
  655.                 interrupts_restore(ipl);
  656.    
  657.                 if (--count == 0)
  658.                     goto satisfied;
  659.                    
  660.                 /*
  661.                  * We are not satisfied yet, focus on another
  662.                  * CPU next time.
  663.                  */
  664.                 k++;
  665.                
  666.                 continue;
  667.             }
  668.             interrupts_restore(ipl);
  669.         }
  670.     }
  671.  
  672.     if (atomic_get(&CPU->nrdy)) {
  673.         /*
  674.          * Be a little bit light-weight and let migrated threads run.
  675.          */
  676.         scheduler();
  677.     } else {
  678.         /*
  679.          * We failed to migrate a single thread.
  680.          * Give up this turn.
  681.          */
  682.         goto loop;
  683.     }
  684.        
  685.     goto not_satisfied;
  686.  
  687. satisfied:
  688.     goto loop;
  689. }
  690.  
  691. #endif /* CONFIG_SMP */
  692.  
  693.  
  694. /** Print information about threads & scheduler queues */
  695. void sched_print_list(void)
  696. {
  697.     ipl_t ipl;
  698.     unsigned int cpu, i;
  699.     runq_t *r;
  700.     thread_t *t;
  701.     link_t *cur;
  702.  
  703.     /* We are going to mess with scheduler structures,
  704.      * let's not be interrupted */
  705.     ipl = interrupts_disable();
  706.     for (cpu = 0; cpu < config.cpu_count; cpu++) {
  707.  
  708.         if (!cpus[cpu].active)
  709.             continue;
  710.  
  711.         spinlock_lock(&cpus[cpu].lock);
  712.         printf("cpu%d: address=%p, nrdy=%ld, needs_relink=%ld\n",
  713.             cpus[cpu].id, &cpus[cpu], atomic_get(&cpus[cpu].nrdy),
  714.             cpus[cpu].needs_relink);
  715.        
  716.         for (i = 0; i < RQ_COUNT; i++) {
  717.             r = &cpus[cpu].rq[i];
  718.             spinlock_lock(&r->lock);
  719.             if (!r->n) {
  720.                 spinlock_unlock(&r->lock);
  721.                 continue;
  722.             }
  723.             printf("\trq[%d]: ", i);
  724.             for (cur = r->rq_head.next; cur != &r->rq_head;
  725.                 cur = cur->next) {
  726.                 t = list_get_instance(cur, thread_t, rq_link);
  727.                 printf("%llu(%s) ", t->tid,
  728.                     thread_states[t->state]);
  729.             }
  730.             printf("\n");
  731.             spinlock_unlock(&r->lock);
  732.         }
  733.         spinlock_unlock(&cpus[cpu].lock);
  734.     }
  735.    
  736.     interrupts_restore(ipl);
  737. }
  738.  
  739. /** @}
  740.  */
  741.