Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR |
| 3 | * policies) |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Update the current task's runtime statistics. Skip current tasks that |
| 8 | * are not in our scheduling class. |
| 9 | */ |
Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 10 | static void update_curr_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 11 | { |
| 12 | struct task_struct *curr = rq->curr; |
| 13 | u64 delta_exec; |
| 14 | |
| 15 | if (!task_has_rt_policy(curr)) |
| 16 | return; |
| 17 | |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 18 | delta_exec = rq->clock - curr->se.exec_start; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 19 | if (unlikely((s64)delta_exec < 0)) |
| 20 | delta_exec = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 21 | |
| 22 | schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec)); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 23 | |
| 24 | curr->se.sum_exec_runtime += delta_exec; |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 25 | curr->se.exec_start = rq->clock; |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 26 | cpuacct_charge(curr, delta_exec); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 27 | } |
| 28 | |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 29 | static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq) |
| 30 | { |
| 31 | WARN_ON(!rt_task(p)); |
| 32 | rq->rt.rt_nr_running++; |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame^] | 33 | #ifdef CONFIG_SMP |
| 34 | if (p->prio < rq->rt.highest_prio) |
| 35 | rq->rt.highest_prio = p->prio; |
| 36 | #endif /* CONFIG_SMP */ |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq) |
| 40 | { |
| 41 | WARN_ON(!rt_task(p)); |
| 42 | WARN_ON(!rq->rt.rt_nr_running); |
| 43 | rq->rt.rt_nr_running--; |
Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame^] | 44 | #ifdef CONFIG_SMP |
| 45 | if (rq->rt.rt_nr_running) { |
| 46 | struct rt_prio_array *array; |
| 47 | |
| 48 | WARN_ON(p->prio < rq->rt.highest_prio); |
| 49 | if (p->prio == rq->rt.highest_prio) { |
| 50 | /* recalculate */ |
| 51 | array = &rq->rt.active; |
| 52 | rq->rt.highest_prio = |
| 53 | sched_find_first_bit(array->bitmap); |
| 54 | } /* otherwise leave rq->highest prio alone */ |
| 55 | } else |
| 56 | rq->rt.highest_prio = MAX_RT_PRIO; |
| 57 | #endif /* CONFIG_SMP */ |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Ingo Molnar | fd390f6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 60 | static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 61 | { |
| 62 | struct rt_prio_array *array = &rq->rt.active; |
| 63 | |
| 64 | list_add_tail(&p->run_list, array->queue + p->prio); |
| 65 | __set_bit(p->prio, array->bitmap); |
Srivatsa Vaddagiri | 58e2d4c | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 66 | inc_cpu_load(rq, p->se.load.weight); |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 67 | |
| 68 | inc_rt_tasks(p, rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Adding/removing a task to/from a priority array: |
| 73 | */ |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 74 | static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 75 | { |
| 76 | struct rt_prio_array *array = &rq->rt.active; |
| 77 | |
Ingo Molnar | f1e14ef | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 78 | update_curr_rt(rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 79 | |
| 80 | list_del(&p->run_list); |
| 81 | if (list_empty(array->queue + p->prio)) |
| 82 | __clear_bit(p->prio, array->bitmap); |
Srivatsa Vaddagiri | 58e2d4c | 2008-01-25 21:08:00 +0100 | [diff] [blame] | 83 | dec_cpu_load(rq, p->se.load.weight); |
Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 84 | |
| 85 | dec_rt_tasks(p, rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Put task to the end of the run list without the overhead of dequeue |
| 90 | * followed by enqueue. |
| 91 | */ |
| 92 | static void requeue_task_rt(struct rq *rq, struct task_struct *p) |
| 93 | { |
| 94 | struct rt_prio_array *array = &rq->rt.active; |
| 95 | |
| 96 | list_move_tail(&p->run_list, array->queue + p->prio); |
| 97 | } |
| 98 | |
| 99 | static void |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 100 | yield_task_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 101 | { |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 102 | requeue_task_rt(rq, rq->curr); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Preempt the current task with a newly woken task if needed: |
| 107 | */ |
| 108 | static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p) |
| 109 | { |
| 110 | if (p->prio < rq->curr->prio) |
| 111 | resched_task(rq->curr); |
| 112 | } |
| 113 | |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 114 | static struct task_struct *pick_next_task_rt(struct rq *rq) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 115 | { |
| 116 | struct rt_prio_array *array = &rq->rt.active; |
| 117 | struct task_struct *next; |
| 118 | struct list_head *queue; |
| 119 | int idx; |
| 120 | |
| 121 | idx = sched_find_first_bit(array->bitmap); |
| 122 | if (idx >= MAX_RT_PRIO) |
| 123 | return NULL; |
| 124 | |
| 125 | queue = array->queue + idx; |
| 126 | next = list_entry(queue->next, struct task_struct, run_list); |
| 127 | |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 128 | next->se.exec_start = rq->clock; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 129 | |
| 130 | return next; |
| 131 | } |
| 132 | |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 133 | static void put_prev_task_rt(struct rq *rq, struct task_struct *p) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 134 | { |
Ingo Molnar | f1e14ef | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 135 | update_curr_rt(rq); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 136 | p->se.exec_start = 0; |
| 137 | } |
| 138 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 139 | #ifdef CONFIG_SMP |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Load-balancing iterator. Note: while the runqueue stays locked |
| 142 | * during the whole iteration, the current task might be |
| 143 | * dequeued so the iterator has to be dequeue-safe. Here we |
| 144 | * achieve that by always pre-iterating before returning |
| 145 | * the current task: |
| 146 | */ |
| 147 | static struct task_struct *load_balance_start_rt(void *arg) |
| 148 | { |
| 149 | struct rq *rq = arg; |
| 150 | struct rt_prio_array *array = &rq->rt.active; |
| 151 | struct list_head *head, *curr; |
| 152 | struct task_struct *p; |
| 153 | int idx; |
| 154 | |
| 155 | idx = sched_find_first_bit(array->bitmap); |
| 156 | if (idx >= MAX_RT_PRIO) |
| 157 | return NULL; |
| 158 | |
| 159 | head = array->queue + idx; |
| 160 | curr = head->prev; |
| 161 | |
| 162 | p = list_entry(curr, struct task_struct, run_list); |
| 163 | |
| 164 | curr = curr->prev; |
| 165 | |
| 166 | rq->rt.rt_load_balance_idx = idx; |
| 167 | rq->rt.rt_load_balance_head = head; |
| 168 | rq->rt.rt_load_balance_curr = curr; |
| 169 | |
| 170 | return p; |
| 171 | } |
| 172 | |
| 173 | static struct task_struct *load_balance_next_rt(void *arg) |
| 174 | { |
| 175 | struct rq *rq = arg; |
| 176 | struct rt_prio_array *array = &rq->rt.active; |
| 177 | struct list_head *head, *curr; |
| 178 | struct task_struct *p; |
| 179 | int idx; |
| 180 | |
| 181 | idx = rq->rt.rt_load_balance_idx; |
| 182 | head = rq->rt.rt_load_balance_head; |
| 183 | curr = rq->rt.rt_load_balance_curr; |
| 184 | |
| 185 | /* |
| 186 | * If we arrived back to the head again then |
| 187 | * iterate to the next queue (if any): |
| 188 | */ |
| 189 | if (unlikely(head == curr)) { |
| 190 | int next_idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1); |
| 191 | |
| 192 | if (next_idx >= MAX_RT_PRIO) |
| 193 | return NULL; |
| 194 | |
| 195 | idx = next_idx; |
| 196 | head = array->queue + idx; |
| 197 | curr = head->prev; |
| 198 | |
| 199 | rq->rt.rt_load_balance_idx = idx; |
| 200 | rq->rt.rt_load_balance_head = head; |
| 201 | } |
| 202 | |
| 203 | p = list_entry(curr, struct task_struct, run_list); |
| 204 | |
| 205 | curr = curr->prev; |
| 206 | |
| 207 | rq->rt.rt_load_balance_curr = curr; |
| 208 | |
| 209 | return p; |
| 210 | } |
| 211 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 212 | static unsigned long |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 213 | load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 214 | unsigned long max_load_move, |
| 215 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 216 | int *all_pinned, int *this_best_prio) |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 217 | { |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 218 | struct rq_iterator rt_rq_iterator; |
| 219 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 220 | rt_rq_iterator.start = load_balance_start_rt; |
| 221 | rt_rq_iterator.next = load_balance_next_rt; |
| 222 | /* pass 'busiest' rq argument into |
| 223 | * load_balance_[start|next]_rt iterators |
| 224 | */ |
| 225 | rt_rq_iterator.arg = busiest; |
| 226 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 227 | return balance_tasks(this_rq, this_cpu, busiest, max_load_move, sd, |
| 228 | idle, all_pinned, this_best_prio, &rt_rq_iterator); |
| 229 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 230 | |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 231 | static int |
| 232 | move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 233 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 234 | { |
| 235 | struct rq_iterator rt_rq_iterator; |
| 236 | |
| 237 | rt_rq_iterator.start = load_balance_start_rt; |
| 238 | rt_rq_iterator.next = load_balance_next_rt; |
| 239 | rt_rq_iterator.arg = busiest; |
| 240 | |
| 241 | return iter_move_one_task(this_rq, this_cpu, busiest, sd, idle, |
| 242 | &rt_rq_iterator); |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 243 | } |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 244 | #endif |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 245 | |
| 246 | static void task_tick_rt(struct rq *rq, struct task_struct *p) |
| 247 | { |
Peter Zijlstra | 67e2be0 | 2007-12-20 15:01:17 +0100 | [diff] [blame] | 248 | update_curr_rt(rq); |
| 249 | |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 250 | /* |
| 251 | * RR tasks need a special form of timeslice management. |
| 252 | * FIFO tasks have no timeslices. |
| 253 | */ |
| 254 | if (p->policy != SCHED_RR) |
| 255 | return; |
| 256 | |
| 257 | if (--p->time_slice) |
| 258 | return; |
| 259 | |
Dmitry Adamushko | a4ec24b | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 260 | p->time_slice = DEF_TIMESLICE; |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 261 | |
Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 262 | /* |
| 263 | * Requeue to the end of queue if we are not the only element |
| 264 | * on the queue: |
| 265 | */ |
| 266 | if (p->run_list.prev != p->run_list.next) { |
| 267 | requeue_task_rt(rq, p); |
| 268 | set_tsk_need_resched(p); |
| 269 | } |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 272 | static void set_curr_task_rt(struct rq *rq) |
| 273 | { |
| 274 | struct task_struct *p = rq->curr; |
| 275 | |
| 276 | p->se.exec_start = rq->clock; |
| 277 | } |
| 278 | |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 279 | const struct sched_class rt_sched_class = { |
| 280 | .next = &fair_sched_class, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 281 | .enqueue_task = enqueue_task_rt, |
| 282 | .dequeue_task = dequeue_task_rt, |
| 283 | .yield_task = yield_task_rt, |
| 284 | |
| 285 | .check_preempt_curr = check_preempt_curr_rt, |
| 286 | |
| 287 | .pick_next_task = pick_next_task_rt, |
| 288 | .put_prev_task = put_prev_task_rt, |
| 289 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 290 | #ifdef CONFIG_SMP |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 291 | .load_balance = load_balance_rt, |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 292 | .move_one_task = move_one_task_rt, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 293 | #endif |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 294 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 295 | .set_curr_task = set_curr_task_rt, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 296 | .task_tick = task_tick_rt, |
Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 297 | }; |