sched_rt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 inline void update_curr_rt(struct rq *rq, u64 now)
  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
  23. enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup, u64 now)
  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
  33. dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep, u64 now)
  34. {
  35. struct rt_prio_array *array = &rq->rt.active;
  36. update_curr_rt(rq, now);
  37. list_del(&p->run_list);
  38. if (list_empty(array->queue + p->prio))
  39. __clear_bit(p->prio, array->bitmap);
  40. }
  41. /*
  42. * Put task to the end of the run list without the overhead of dequeue
  43. * followed by enqueue.
  44. */
  45. static void requeue_task_rt(struct rq *rq, struct task_struct *p)
  46. {
  47. struct rt_prio_array *array = &rq->rt.active;
  48. list_move_tail(&p->run_list, array->queue + p->prio);
  49. }
  50. static void
  51. yield_task_rt(struct rq *rq, struct task_struct *p)
  52. {
  53. requeue_task_rt(rq, p);
  54. }
  55. /*
  56. * Preempt the current task with a newly woken task if needed:
  57. */
  58. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
  59. {
  60. if (p->prio < rq->curr->prio)
  61. resched_task(rq->curr);
  62. }
  63. static struct task_struct *pick_next_task_rt(struct rq *rq, u64 now)
  64. {
  65. struct rt_prio_array *array = &rq->rt.active;
  66. struct task_struct *next;
  67. struct list_head *queue;
  68. int idx;
  69. idx = sched_find_first_bit(array->bitmap);
  70. if (idx >= MAX_RT_PRIO)
  71. return NULL;
  72. queue = array->queue + idx;
  73. next = list_entry(queue->next, struct task_struct, run_list);
  74. next->se.exec_start = rq->clock;
  75. return next;
  76. }
  77. static void put_prev_task_rt(struct rq *rq, struct task_struct *p, u64 now)
  78. {
  79. update_curr_rt(rq, now);
  80. p->se.exec_start = 0;
  81. }
  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_nr_move, 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. int nr_moved;
  144. struct rq_iterator rt_rq_iterator;
  145. unsigned long load_moved;
  146. rt_rq_iterator.start = load_balance_start_rt;
  147. rt_rq_iterator.next = load_balance_next_rt;
  148. /* pass 'busiest' rq argument into
  149. * load_balance_[start|next]_rt iterators
  150. */
  151. rt_rq_iterator.arg = busiest;
  152. nr_moved = balance_tasks(this_rq, this_cpu, busiest, max_nr_move,
  153. max_load_move, sd, idle, all_pinned, &load_moved,
  154. this_best_prio, &rt_rq_iterator);
  155. return load_moved;
  156. }
  157. static void task_tick_rt(struct rq *rq, struct task_struct *p)
  158. {
  159. /*
  160. * RR tasks need a special form of timeslice management.
  161. * FIFO tasks have no timeslices.
  162. */
  163. if (p->policy != SCHED_RR)
  164. return;
  165. if (--p->time_slice)
  166. return;
  167. p->time_slice = static_prio_timeslice(p->static_prio);
  168. set_tsk_need_resched(p);
  169. /* put it at the end of the queue: */
  170. requeue_task_rt(rq, p);
  171. }
  172. static struct sched_class rt_sched_class __read_mostly = {
  173. .enqueue_task = enqueue_task_rt,
  174. .dequeue_task = dequeue_task_rt,
  175. .yield_task = yield_task_rt,
  176. .check_preempt_curr = check_preempt_curr_rt,
  177. .pick_next_task = pick_next_task_rt,
  178. .put_prev_task = put_prev_task_rt,
  179. .load_balance = load_balance_rt,
  180. .task_tick = task_tick_rt,
  181. };