sched_rt.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  3. * policies)
  4. */
  5. /*
  6. * Update the current task's runtime statistics. Skip current tasks that
  7. * are not in our scheduling class.
  8. */
  9. static void update_curr_rt(struct rq *rq)
  10. {
  11. struct task_struct *curr = rq->curr;
  12. u64 delta_exec;
  13. if (!task_has_rt_policy(curr))
  14. return;
  15. delta_exec = rq->clock - curr->se.exec_start;
  16. if (unlikely((s64)delta_exec < 0))
  17. delta_exec = 0;
  18. schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
  19. curr->se.sum_exec_runtime += delta_exec;
  20. curr->se.exec_start = rq->clock;
  21. }
  22. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  23. {
  24. struct rt_prio_array *array = &rq->rt.active;
  25. list_add_tail(&p->run_list, array->queue + p->prio);
  26. __set_bit(p->prio, array->bitmap);
  27. }
  28. /*
  29. * Adding/removing a task to/from a priority array:
  30. */
  31. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  32. {
  33. struct rt_prio_array *array = &rq->rt.active;
  34. update_curr_rt(rq);
  35. list_del(&p->run_list);
  36. if (list_empty(array->queue + p->prio))
  37. __clear_bit(p->prio, array->bitmap);
  38. }
  39. /*
  40. * Put task to the end of the run list without the overhead of dequeue
  41. * followed by enqueue.
  42. */
  43. static void requeue_task_rt(struct rq *rq, struct task_struct *p)
  44. {
  45. struct rt_prio_array *array = &rq->rt.active;
  46. list_move_tail(&p->run_list, array->queue + p->prio);
  47. }
  48. static void
  49. yield_task_rt(struct rq *rq)
  50. {
  51. requeue_task_rt(rq, rq->curr);
  52. }
  53. /*
  54. * Preempt the current task with a newly woken task if needed:
  55. */
  56. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
  57. {
  58. if (p->prio < rq->curr->prio)
  59. resched_task(rq->curr);
  60. }
  61. static struct task_struct *pick_next_task_rt(struct rq *rq)
  62. {
  63. struct rt_prio_array *array = &rq->rt.active;
  64. struct task_struct *next;
  65. struct list_head *queue;
  66. int idx;
  67. idx = sched_find_first_bit(array->bitmap);
  68. if (idx >= MAX_RT_PRIO)
  69. return NULL;
  70. queue = array->queue + idx;
  71. next = list_entry(queue->next, struct task_struct, run_list);
  72. next->se.exec_start = rq->clock;
  73. return next;
  74. }
  75. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  76. {
  77. update_curr_rt(rq);
  78. p->se.exec_start = 0;
  79. }
  80. #ifdef CONFIG_SMP
  81. /*
  82. * Load-balancing iterator. Note: while the runqueue stays locked
  83. * during the whole iteration, the current task might be
  84. * dequeued so the iterator has to be dequeue-safe. Here we
  85. * achieve that by always pre-iterating before returning
  86. * the current task:
  87. */
  88. static struct task_struct *load_balance_start_rt(void *arg)
  89. {
  90. struct rq *rq = arg;
  91. struct rt_prio_array *array = &rq->rt.active;
  92. struct list_head *head, *curr;
  93. struct task_struct *p;
  94. int idx;
  95. idx = sched_find_first_bit(array->bitmap);
  96. if (idx >= MAX_RT_PRIO)
  97. return NULL;
  98. head = array->queue + idx;
  99. curr = head->prev;
  100. p = list_entry(curr, struct task_struct, run_list);
  101. curr = curr->prev;
  102. rq->rt.rt_load_balance_idx = idx;
  103. rq->rt.rt_load_balance_head = head;
  104. rq->rt.rt_load_balance_curr = curr;
  105. return p;
  106. }
  107. static struct task_struct *load_balance_next_rt(void *arg)
  108. {
  109. struct rq *rq = arg;
  110. struct rt_prio_array *array = &rq->rt.active;
  111. struct list_head *head, *curr;
  112. struct task_struct *p;
  113. int idx;
  114. idx = rq->rt.rt_load_balance_idx;
  115. head = rq->rt.rt_load_balance_head;
  116. curr = rq->rt.rt_load_balance_curr;
  117. /*
  118. * If we arrived back to the head again then
  119. * iterate to the next queue (if any):
  120. */
  121. if (unlikely(head == curr)) {
  122. int next_idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  123. if (next_idx >= MAX_RT_PRIO)
  124. return NULL;
  125. idx = next_idx;
  126. head = array->queue + idx;
  127. curr = head->prev;
  128. rq->rt.rt_load_balance_idx = idx;
  129. rq->rt.rt_load_balance_head = head;
  130. }
  131. p = list_entry(curr, struct task_struct, run_list);
  132. curr = curr->prev;
  133. rq->rt.rt_load_balance_curr = curr;
  134. return p;
  135. }
  136. static unsigned long
  137. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  138. unsigned long max_load_move,
  139. struct sched_domain *sd, enum cpu_idle_type idle,
  140. int *all_pinned, int *this_best_prio)
  141. {
  142. struct rq_iterator rt_rq_iterator;
  143. rt_rq_iterator.start = load_balance_start_rt;
  144. rt_rq_iterator.next = load_balance_next_rt;
  145. /* pass 'busiest' rq argument into
  146. * load_balance_[start|next]_rt iterators
  147. */
  148. rt_rq_iterator.arg = busiest;
  149. return balance_tasks(this_rq, this_cpu, busiest, max_load_move, sd,
  150. idle, all_pinned, this_best_prio, &rt_rq_iterator);
  151. }
  152. static int
  153. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  154. struct sched_domain *sd, enum cpu_idle_type idle)
  155. {
  156. struct rq_iterator rt_rq_iterator;
  157. rt_rq_iterator.start = load_balance_start_rt;
  158. rt_rq_iterator.next = load_balance_next_rt;
  159. rt_rq_iterator.arg = busiest;
  160. return iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  161. &rt_rq_iterator);
  162. }
  163. #endif
  164. static void task_tick_rt(struct rq *rq, struct task_struct *p)
  165. {
  166. /*
  167. * RR tasks need a special form of timeslice management.
  168. * FIFO tasks have no timeslices.
  169. */
  170. if (p->policy != SCHED_RR)
  171. return;
  172. if (--p->time_slice)
  173. return;
  174. p->time_slice = DEF_TIMESLICE;
  175. /*
  176. * Requeue to the end of queue if we are not the only element
  177. * on the queue:
  178. */
  179. if (p->run_list.prev != p->run_list.next) {
  180. requeue_task_rt(rq, p);
  181. set_tsk_need_resched(p);
  182. }
  183. }
  184. static void set_curr_task_rt(struct rq *rq)
  185. {
  186. struct task_struct *p = rq->curr;
  187. p->se.exec_start = rq->clock;
  188. }
  189. const struct sched_class rt_sched_class = {
  190. .next = &fair_sched_class,
  191. .enqueue_task = enqueue_task_rt,
  192. .dequeue_task = dequeue_task_rt,
  193. .yield_task = yield_task_rt,
  194. .check_preempt_curr = check_preempt_curr_rt,
  195. .pick_next_task = pick_next_task_rt,
  196. .put_prev_task = put_prev_task_rt,
  197. #ifdef CONFIG_SMP
  198. .load_balance = load_balance_rt,
  199. .move_one_task = move_one_task_rt,
  200. #endif
  201. .set_curr_task = set_curr_task_rt,
  202. .task_tick = task_tick_rt,
  203. };