Subversion Repositories HelenOS-historic

Rev

Rev 1115 | Rev 1221 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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