歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> Linux內核之進程調度

Linux內核之進程調度

日期:2017/2/28 16:00:37   编辑: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. spin_lock_irqsave(&q->lock, flags);
  11. __add_wait_queue(q, &wait);/*加入等待隊列中*/
  12. spin_unlock(&q->lock);
  13. /*sleep until timeout,在本進程睡眠的過程中會調用別的進程運行*/
  14. timeout = schedule_timeout(timeout);
  15. spin_lock_irq(&q->lock);
  16. /*當本進程被喚醒時,從這裡繼續開始運行
  17. 也就是將該進程從等待隊列中移除*/
  18. __remove_wait_queue(q, &wait);
  19. spin_unlock_irqrestore(&q->lock, flags);
  20. return timeout;
  21. }

  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. if (!sched_feat(SYNC_WAKEUPS))
  22. wake_flags &= ~WF_SYNC;/* waker not goes to sleep after wakup */
  23. this_cpu = get_cpu();/*cpu id*/
  24. smp_wmb();
  25. rq = orig_rq = task_rq_lock(p, &flags);/*獲得進程的rq*/
  26. update_rq_clock(rq);/*更新rq的時鐘*/
  27. if (!(p->state & state))
  28. goto out;
  29. if (p->se.on_rq)/*如果進程已經在運行隊列中*/
  30. goto out_running;
  31. cpu = task_cpu(p);/*返回進程對應的cpu*/
  32. orig_cpu = cpu;
  33. #ifdef CONFIG_SMP
  34. if (unlikely(task_running(rq, p)))/*如果當前進程時p,也就是waker*/
  35. goto out_activate;
  36. /*
  37. * In order to handle concurrent wakeups and release the rq->lock
  38. * we put the task in TASK_WAKING state.
  39. *
  40. * First fix up the nr_uninterruptible count:
  41. */
  42. if (task_contributes_to_load(p))
  43. rq->nr_uninterruptible--;
  44. p->state = TASK_WAKING;
  45. task_rq_unlock(rq, &flags);
  46. /*通常用在執行一個新的程序,或是WakeUp
  47. 一個Task時,會根據目前SMP下每個處理器的
  48. 負荷,決定Task是否要切換到另一個處理器
  49. 的RunQueue去執行,執行時會返回最後目標
  50. 處理器的值.*/
  51. cpu = p->sched_class->select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
  52. if (cpu != orig_cpu)
  53. set_task_cpu(p, cpu);/*設置task在制定的cpu上運行*/
  54. rq = task_rq_lock(p, &flags);/*task對應的rq*/
  55. if (rq != orig_rq)
  56. update_rq_clock(rq);/*更新clock*/
  57. WARN_ON(p->state != TASK_WAKING);
  58. cpu = task_cpu(p);
  59. #ifdef CONFIG_SCHEDSTATS/*yes*/
  60. schedstat_inc(rq, ttwu_count);/*Wake Up Task的次數加一.*/
  61. if (cpu == this_cpu)
  62. /*Wake Up 同一個處理器Task的次數加一.*/
  63. schedstat_inc(rq, ttwu_local);
  64. else {
  65. struct sched_domain *sd;
  66. for_each_domain(this_cpu, sd) {
  67. if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  68. schedstat_inc(sd, ttwu_wake_remote);
  69. break;
  70. }
  71. }
  72. }
  73. #endif /* CONFIG_SCHEDSTATS */
  74. out_activate:
  75. #endif /* CONFIG_SMP */
  76. /*下面為設置相關計數變量*/
  77. schedstat_inc(rq, field)(p, se.nr_wakeups);
  78. if (wake_flags & WF_SYNC)
  79. schedstat_inc(p, se.nr_wakeups_sync);
  80. if (orig_cpu != cpu)
  81. schedstat_inc(p, se.nr_wakeups_migrate);
  82. if (cpu == this_cpu)
  83. schedstat_inc(p, se.nr_wakeups_local);
  84. else
  85. schedstat_inc(p, se.nr_wakeups_remote);
  86. /*將進程移動到對應調度類的運行隊列*/
  87. activate_task(rq, p, 1);
  88. success = 1;
  89. /*
  90. * Only attribute actual wakeups done by this task.
  91. */
  92. if (!in_interrupt()) {/*下面為對se中變量last_wakeup和
  93. avg_wakeup的更新*/
  94. struct sched_entity *se = ¤t->se;
  95. u64 sample = se->sum_exec_runtime;
  96. if (se->last_wakeup)
  97. sample -= se->last_wakeup;
  98. else
  99. sample -= se->start_runtime;
  100. update_avg(&se->avg_wakeup, sample);
  101. se->last_wakeup = se->sum_exec_runtime;
  102. }
  103. out_running:
  104. trace_sched_wakeup(rq, p, success);
  105. /*用以決定一個Task是否可以中斷目前正在
  106. 運作的Task,取得執行權.*/
  107. check_preempt_curr(rq, p, wake_flags);
  108. p->state = TASK_RUNNING;
  109. #ifdef CONFIG_SMP
  110. if (p->sched_class->task_wake_up)
  111. p->sched_class->task_wake_up(rq, p);
  112. if (unlikely(rq->idle_stamp)) {/*該值可用以表示這個
  113. 處理器是何時進入到Idle的
  114. 狀態,在這裡得到更新*/
  115. u64 delta = rq->clock - rq->idle_stamp;
  116. u64 max = 2*sysctl_sched_migration_cost;
  117. if (delta > max)
  118. rq->avg_idle = max;
  119. else/*avg_idle可反應目前處理器進入Idle狀態的時間長短*/
  120. update_avg(&rq->avg_idle, delta);
  121. rq->idle_stamp = 0;
  122. }
  123. #endif
  124. out:
  125. task_rq_unlock(rq, &flags);
  126. put_cpu();
  127. return success;
  128. }

所有的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. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  6. unsigned flags = curr->flags;
  7. if (curr->func(curr, mode, wake_flags, key) &&/*在這裡會調用上面注冊的try_to_wake_up函數*/
  8. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  9. break;
  10. }
  11. }

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函數將其從等待隊列中刪除。

Copyright © Linux教程網 All Rights Reserved