Linux内核之进程调度


等待队列

Sleep相关函数将进程的状态设置为非运行态,在下一次调度来时,将在schedule函数中将本进程从运行队列中移除。sleep函数将进程加入等待队列,然后调用schedule函数选择并重新开始另一个程序的执行。当调用wake_up类函数将进程唤醒时,wake_up类函数将进程加入运行队列中,调度程序重新从sleep函数中下一条没有执行的指令开始执行。

sleep类函数都调用sleep_on_common函数实现,只是传入的参数有别。

  1. static long __sched  
  2. sleep_on_common(wait_queue_head_t *q, int state, long timeout)  
  3. {  
  4.     unsigned long flags;  
  5.     wait_queue_t wait;  
  6.     /*初始化等待队列*/  
  7.     init_waitqueue_entry(&wait, current);  
  8.     /*设置当前进程状态*/  
  9.     __set_current_state(state);  
  10.       
  11.     spin_lock_irqsave(&q->lock, flags);  
  12.     __add_wait_queue(q, &wait);/*加入等待队列中*/  
  13.     spin_unlock(&q->lock);  
  14.     /*sleep until timeout,在本进程睡眠的过程中会调用别的进程运行*/  
  15.     timeout = schedule_timeout(timeout);  
  16.     spin_lock_irq(&q->lock);  
  17.     /*当本进程被唤醒时,从这里继续开始运行 
  18.     也就是将该进程从等待队列中移除*/  
  19.     __remove_wait_queue(q, &wait);  
  20.     spin_unlock_irqrestore(&q->lock, flags);  
  21.   
  22.     return timeout;  
  23. }  

 
  1. static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)  
  2. {  
  3.     q->flags = 0;  
  4.     q->private = p;/*将进程保存为队列私有属性*/  
  5.     q->func = default_wake_function;/*设定为缺省的唤醒函数*/  
  6. }  

我们看唤醒函数,default_wake_function最终调用函数try_to_wake_up

  1. /*** 
  2.  * try_to_wake_up - wake up a thread 
  3.  * @p: the to-be-woken-up thread 
  4.  * @state: the mask of task states that can be woken 
  5.  * @sync: do a synchronous wakeup? 
  6.  * 
  7.  * Put it on the run-queue if it's not already there. The "current" 
  8.  * thread is always on the run-queue (except when the actual 
  9.  * re-schedule is in progress), and as such you're allowed to do 
  10.  * the simpler "current->state = TASK_RUNNING" to mark yourself 
  11.  * runnable without the overhead of this. 
  12.  * 
  13.  * returns failure only if the task is already active. 
  14.  */  
  15. static int try_to_wake_up(struct task_struct *p, unsigned int state,  
  16.               int wake_flags)  
  17. {  
  18.     int cpu, orig_cpu, this_cpu, success = 0;  
  19.     unsigned long flags;  
  20.     struct rq *rq, *orig_rq;  
  21.   
  22.     if (!sched_feat(SYNC_WAKEUPS))  
  23.         wake_flags &= ~WF_SYNC;/* waker not goes to sleep after wakup */  
  24.   
  25.     this_cpu = get_cpu();/*cpu id*/  
  26.   
  27.     smp_wmb();  
  28.     rq = orig_rq = task_rq_lock(p, &flags);/*获得进程的rq*/  
  29.     update_rq_clock(rq);/*更新rq的时钟*/  
  30.     if (!(p->state & state))  
  31.         goto out;  
  32.   
  33.     if (p->se.on_rq)/*如果进程已经在运行队列中*/  
  34.         goto out_running;  
  35.   
  36.     cpu = task_cpu(p);/*返回进程对应的cpu*/  
  37.     orig_cpu = cpu;  
  38.   
  39. #ifdef CONFIG_SMP   
  40.     if (unlikely(task_running(rq, p)))/*如果当前进程时p,也就是waker*/  
  41.         goto out_activate;  
  42.   
  43.     /* 
  44.      * In order to handle concurrent wakeups and release the rq->lock 
  45.      * we put the task in TASK_WAKING state. 
  46.      * 
  47.      * First fix up the nr_uninterruptible count: 
  48.      */  
  49.     if (task_contributes_to_load(p))  
  50.         rq->nr_uninterruptible--;  
  51.     p->state = TASK_WAKING;  
  52.     task_rq_unlock(rq, &flags);  
  53.     /*通常用在執行一個新的程序,或是WakeUp 
  54.     一個Task時,會根據目前SMP下每個處理器的 
  55.     負荷,決定Task是否要切換到另一個處理器 
  56.     的RunQueue去執行,執行時會返回最後目標 
  57.     處理器的值.*/  
  58.     cpu = p->sched_class->select_task_rq(p, SD_BALANCE_WAKE, wake_flags);  
  59.     if (cpu != orig_cpu)  
  60.         set_task_cpu(p, cpu);/*设置task在制定的cpu上运行*/  
  61.   
  62.     rq = task_rq_lock(p, &flags);/*task对应的rq*/  
  63.   
  64.     if (rq != orig_rq)  
  65.         update_rq_clock(rq);/*更新clock*/  
  66.   
  67.     WARN_ON(p->state != TASK_WAKING);  
  68.     cpu = task_cpu(p);  
  69.   
  70. #ifdef CONFIG_SCHEDSTATS/*yes*/   
  71.     schedstat_inc(rq, ttwu_count);/*Wake Up Task的次數加一.*/  
  72.     if (cpu == this_cpu)  
  73.         /*Wake Up 同一個處理器Task的次數加一.*/  
  74.         schedstat_inc(rq, ttwu_local);  
  75.     else {  
  76.         struct sched_domain *sd;  
  77.         for_each_domain(this_cpu, sd) {  
  78.             if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {  
  79.                 schedstat_inc(sd, ttwu_wake_remote);  
  80.                 break;  
  81.             }  
  82.         }  
  83.     }  
  84. #endif /* CONFIG_SCHEDSTATS */   
  85.   
  86. out_activate:  
  87. #endif /* CONFIG_SMP */   
  88.     /*下面为设置相关计数变量*/  
  89.     schedstat_inc(rq, field)(p, se.nr_wakeups);  
  90.     if (wake_flags & WF_SYNC)  
  91.         schedstat_inc(p, se.nr_wakeups_sync);  
  92.     if (orig_cpu != cpu)  
  93.         schedstat_inc(p, se.nr_wakeups_migrate);  
  94.     if (cpu == this_cpu)  
  95.         schedstat_inc(p, se.nr_wakeups_local);  
  96.     else  
  97.         schedstat_inc(p, se.nr_wakeups_remote);  
  98.     /*将进程移动到对应调度类的运行队列*/  
  99.     activate_task(rq, p, 1);  
  100.     success = 1;  
  101.   
  102.     /* 
  103.      * Only attribute actual wakeups done by this task. 
  104.      */  
  105.     if (!in_interrupt()) {/*下面为对se中变量last_wakeup和 
  106.                         avg_wakeup的更新*/  
  107.         struct sched_entity *se = ¤t->se;  
  108.         u64 sample = se->sum_exec_runtime;  
  109.   
  110.         if (se->last_wakeup)  
  111.             sample -= se->last_wakeup;  
  112.         else  
  113.             sample -= se->start_runtime;  
  114.         update_avg(&se->avg_wakeup, sample);  
  115.   
  116.         se->last_wakeup = se->sum_exec_runtime;  
  117.     }  
  118.   
  119. out_running:  
  120.     trace_sched_wakeup(rq, p, success);  
  121.       
  122.     /*用以決定一個Task是否可以中斷目前正在 
  123.     運作的Task,取得執行權.*/  
  124.     check_preempt_curr(rq, p, wake_flags);  
  125.   
  126.     p->state = TASK_RUNNING;  
  127. #ifdef CONFIG_SMP   
  128.     if (p->sched_class->task_wake_up)  
  129.         p->sched_class->task_wake_up(rq, p);  
  130.   
  131.     if (unlikely(rq->idle_stamp)) {/*该值可用以表示這個 
  132.                             處理器是何時進入到Idle的 
  133.                             狀態,在这里得到更新*/  
  134.         u64 delta = rq->clock - rq->idle_stamp;  
  135.         u64 max = 2*sysctl_sched_migration_cost;  
  136.   
  137.         if (delta > max)  
  138.             rq->avg_idle = max;  
  139.         else/*avg_idle可反應目前處理器進入Idle狀態的時間長短*/  
  140.             update_avg(&rq->avg_idle, delta);  
  141.         rq->idle_stamp = 0;  
  142.     }  
  143. #endif   
  144. out:  
  145.     task_rq_unlock(rq, &flags);  
  146.     put_cpu();  
  147.   
  148.     return success;  
  149. }  

所有的wake_up类函数都最终调用__wake_up_common函数实现

  1. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,  
  2.             int nr_exclusive, int wake_flags, void *key)  
  3. {  
  4.     wait_queue_t *curr, *next;  
  5.   
  6.     list_for_each_entry_safe(curr, next, &q->task_list, task_list) {  
  7.         unsigned flags = curr->flags;  
  8.   
  9.         if (curr->func(curr, mode, wake_flags, key) &&/*在这里会调用上面注册的try_to_wake_up函数*/  
  10.                 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)  
  11.             break;  
  12.     }  
  13. }  

wait_event方式

考虑到sleep_on类函数在以下条件中不能使用,那就是必须测试条件并且当条件还没哟得到验证时又紧接着让进城去睡眠;为实现这样的功能,内核采用wait_event的方式实现。

  1. #define __wait_event(wq, condition)                     \   
  2. do {                                    \  
  3.     DEFINE_WAIT(__wait);                        \  
  4.                                     \  
  5.     for (;;) {  /*加入等待队列,设置进程状态*/       \  
  6.         prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \  
  7.         if (condition)                      \  
  8.             break;                      \  
  9.         schedule();/*调用其他进程运行*/             \  
  10.     }/*当进程被唤醒时继续如下执行*/              \  
  11.     finish_wait(&wq, &__wait);                  \  
  12. while (0)  

当下一次调度到来时,调度程序把设置为非运行的当前进程从运行队列里面删除,而进程被wake_up类函数唤醒时,wake_up类函数将其加入运行队列,继续执行上面没有执行完成的wait_event函数(执行finish_wait函数),finish_wait函数将其从等待队列中删除。

  • 1
  • 2
  • 下一页

相关内容