sched_rt.c 6.7 KB

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