sched_rt.c 5.8 KB

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