sched_rt.c 6.2 KB

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