sched_rt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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)
  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, struct task_struct *p)
  50. {
  51. requeue_task_rt(rq, p);
  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, u64 now)
  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, u64 now)
  76. {
  77. update_curr_rt(rq);
  78. p->se.exec_start = 0;
  79. }
  80. /*
  81. * Load-balancing iterator. Note: while the runqueue stays locked
  82. * during the whole iteration, the current task might be
  83. * dequeued so the iterator has to be dequeue-safe. Here we
  84. * achieve that by always pre-iterating before returning
  85. * the current task:
  86. */
  87. static struct task_struct *load_balance_start_rt(void *arg)
  88. {
  89. struct rq *rq = arg;
  90. struct rt_prio_array *array = &rq->rt.active;
  91. struct list_head *head, *curr;
  92. struct task_struct *p;
  93. int idx;
  94. idx = sched_find_first_bit(array->bitmap);
  95. if (idx >= MAX_RT_PRIO)
  96. return NULL;
  97. head = array->queue + idx;
  98. curr = head->prev;
  99. p = list_entry(curr, struct task_struct, run_list);
  100. curr = curr->prev;
  101. rq->rt.rt_load_balance_idx = idx;
  102. rq->rt.rt_load_balance_head = head;
  103. rq->rt.rt_load_balance_curr = curr;
  104. return p;
  105. }
  106. static struct task_struct *load_balance_next_rt(void *arg)
  107. {
  108. struct rq *rq = arg;
  109. struct rt_prio_array *array = &rq->rt.active;
  110. struct list_head *head, *curr;
  111. struct task_struct *p;
  112. int idx;
  113. idx = rq->rt.rt_load_balance_idx;
  114. head = rq->rt.rt_load_balance_head;
  115. curr = rq->rt.rt_load_balance_curr;
  116. /*
  117. * If we arrived back to the head again then
  118. * iterate to the next queue (if any):
  119. */
  120. if (unlikely(head == curr)) {
  121. int next_idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  122. if (next_idx >= MAX_RT_PRIO)
  123. return NULL;
  124. idx = next_idx;
  125. head = array->queue + idx;
  126. curr = head->prev;
  127. rq->rt.rt_load_balance_idx = idx;
  128. rq->rt.rt_load_balance_head = head;
  129. }
  130. p = list_entry(curr, struct task_struct, run_list);
  131. curr = curr->prev;
  132. rq->rt.rt_load_balance_curr = curr;
  133. return p;
  134. }
  135. static unsigned long
  136. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  137. unsigned long max_nr_move, unsigned long max_load_move,
  138. struct sched_domain *sd, enum cpu_idle_type idle,
  139. int *all_pinned, int *this_best_prio)
  140. {
  141. int nr_moved;
  142. struct rq_iterator rt_rq_iterator;
  143. unsigned long load_moved;
  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. nr_moved = balance_tasks(this_rq, this_cpu, busiest, max_nr_move,
  151. max_load_move, sd, idle, all_pinned, &load_moved,
  152. this_best_prio, &rt_rq_iterator);
  153. return load_moved;
  154. }
  155. static void task_tick_rt(struct rq *rq, struct task_struct *p)
  156. {
  157. /*
  158. * RR tasks need a special form of timeslice management.
  159. * FIFO tasks have no timeslices.
  160. */
  161. if (p->policy != SCHED_RR)
  162. return;
  163. if (--p->time_slice)
  164. return;
  165. p->time_slice = static_prio_timeslice(p->static_prio);
  166. set_tsk_need_resched(p);
  167. /* put it at the end of the queue: */
  168. requeue_task_rt(rq, p);
  169. }
  170. static struct sched_class rt_sched_class __read_mostly = {
  171. .enqueue_task = enqueue_task_rt,
  172. .dequeue_task = dequeue_task_rt,
  173. .yield_task = yield_task_rt,
  174. .check_preempt_curr = check_preempt_curr_rt,
  175. .pick_next_task = pick_next_task_rt,
  176. .put_prev_task = put_prev_task_rt,
  177. .load_balance = load_balance_rt,
  178. .task_tick = task_tick_rt,
  179. };