blob: e3d2ca8832af85e9cb008dedf852a1511f7a6fab [file] [log] [blame]
Ingo Molnarbb44e5d2007-07-09 18:51:58 +02001/*
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 Dobriyana9957442007-10-15 17:00:13 +020010static void update_curr_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020011{
12 struct task_struct *curr = rq->curr;
13 u64 delta_exec;
14
15 if (!task_has_rt_policy(curr))
16 return;
17
Ingo Molnard2819182007-08-09 11:16:47 +020018 delta_exec = rq->clock - curr->se.exec_start;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020019 if (unlikely((s64)delta_exec < 0))
20 delta_exec = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +020021
22 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020023
24 curr->se.sum_exec_runtime += delta_exec;
Ingo Molnard2819182007-08-09 11:16:47 +020025 curr->se.exec_start = rq->clock;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010026 cpuacct_charge(curr, delta_exec);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020027}
28
Steven Rostedt63489e42008-01-25 21:08:03 +010029static 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++;
33}
34
35static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
36{
37 WARN_ON(!rt_task(p));
38 WARN_ON(!rq->rt.rt_nr_running);
39 rq->rt.rt_nr_running--;
40}
41
Ingo Molnarfd390f62007-08-09 11:16:48 +020042static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020043{
44 struct rt_prio_array *array = &rq->rt.active;
45
46 list_add_tail(&p->run_list, array->queue + p->prio);
47 __set_bit(p->prio, array->bitmap);
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +010048 inc_cpu_load(rq, p->se.load.weight);
Steven Rostedt63489e42008-01-25 21:08:03 +010049
50 inc_rt_tasks(p, rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020051}
52
53/*
54 * Adding/removing a task to/from a priority array:
55 */
Ingo Molnarf02231e2007-08-09 11:16:48 +020056static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020057{
58 struct rt_prio_array *array = &rq->rt.active;
59
Ingo Molnarf1e14ef2007-08-09 11:16:48 +020060 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020061
62 list_del(&p->run_list);
63 if (list_empty(array->queue + p->prio))
64 __clear_bit(p->prio, array->bitmap);
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +010065 dec_cpu_load(rq, p->se.load.weight);
Steven Rostedt63489e42008-01-25 21:08:03 +010066
67 dec_rt_tasks(p, rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020068}
69
70/*
71 * Put task to the end of the run list without the overhead of dequeue
72 * followed by enqueue.
73 */
74static void requeue_task_rt(struct rq *rq, struct task_struct *p)
75{
76 struct rt_prio_array *array = &rq->rt.active;
77
78 list_move_tail(&p->run_list, array->queue + p->prio);
79}
80
81static void
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +020082yield_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020083{
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +020084 requeue_task_rt(rq, rq->curr);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020085}
86
87/*
88 * Preempt the current task with a newly woken task if needed:
89 */
90static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
91{
92 if (p->prio < rq->curr->prio)
93 resched_task(rq->curr);
94}
95
Ingo Molnarfb8d4722007-08-09 11:16:48 +020096static struct task_struct *pick_next_task_rt(struct rq *rq)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +020097{
98 struct rt_prio_array *array = &rq->rt.active;
99 struct task_struct *next;
100 struct list_head *queue;
101 int idx;
102
103 idx = sched_find_first_bit(array->bitmap);
104 if (idx >= MAX_RT_PRIO)
105 return NULL;
106
107 queue = array->queue + idx;
108 next = list_entry(queue->next, struct task_struct, run_list);
109
Ingo Molnard2819182007-08-09 11:16:47 +0200110 next->se.exec_start = rq->clock;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200111
112 return next;
113}
114
Ingo Molnar31ee5292007-08-09 11:16:49 +0200115static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200116{
Ingo Molnarf1e14ef2007-08-09 11:16:48 +0200117 update_curr_rt(rq);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200118 p->se.exec_start = 0;
119}
120
Peter Williams681f3e62007-10-24 18:23:51 +0200121#ifdef CONFIG_SMP
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200122/*
123 * Load-balancing iterator. Note: while the runqueue stays locked
124 * during the whole iteration, the current task might be
125 * dequeued so the iterator has to be dequeue-safe. Here we
126 * achieve that by always pre-iterating before returning
127 * the current task:
128 */
129static struct task_struct *load_balance_start_rt(void *arg)
130{
131 struct rq *rq = arg;
132 struct rt_prio_array *array = &rq->rt.active;
133 struct list_head *head, *curr;
134 struct task_struct *p;
135 int idx;
136
137 idx = sched_find_first_bit(array->bitmap);
138 if (idx >= MAX_RT_PRIO)
139 return NULL;
140
141 head = array->queue + idx;
142 curr = head->prev;
143
144 p = list_entry(curr, struct task_struct, run_list);
145
146 curr = curr->prev;
147
148 rq->rt.rt_load_balance_idx = idx;
149 rq->rt.rt_load_balance_head = head;
150 rq->rt.rt_load_balance_curr = curr;
151
152 return p;
153}
154
155static struct task_struct *load_balance_next_rt(void *arg)
156{
157 struct rq *rq = arg;
158 struct rt_prio_array *array = &rq->rt.active;
159 struct list_head *head, *curr;
160 struct task_struct *p;
161 int idx;
162
163 idx = rq->rt.rt_load_balance_idx;
164 head = rq->rt.rt_load_balance_head;
165 curr = rq->rt.rt_load_balance_curr;
166
167 /*
168 * If we arrived back to the head again then
169 * iterate to the next queue (if any):
170 */
171 if (unlikely(head == curr)) {
172 int next_idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
173
174 if (next_idx >= MAX_RT_PRIO)
175 return NULL;
176
177 idx = next_idx;
178 head = array->queue + idx;
179 curr = head->prev;
180
181 rq->rt.rt_load_balance_idx = idx;
182 rq->rt.rt_load_balance_head = head;
183 }
184
185 p = list_entry(curr, struct task_struct, run_list);
186
187 curr = curr->prev;
188
189 rq->rt.rt_load_balance_curr = curr;
190
191 return p;
192}
193
Peter Williams43010652007-08-09 11:16:46 +0200194static unsigned long
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200195load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamse1d14842007-10-24 18:23:51 +0200196 unsigned long max_load_move,
197 struct sched_domain *sd, enum cpu_idle_type idle,
198 int *all_pinned, int *this_best_prio)
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200199{
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200200 struct rq_iterator rt_rq_iterator;
201
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200202 rt_rq_iterator.start = load_balance_start_rt;
203 rt_rq_iterator.next = load_balance_next_rt;
204 /* pass 'busiest' rq argument into
205 * load_balance_[start|next]_rt iterators
206 */
207 rt_rq_iterator.arg = busiest;
208
Peter Williamse1d14842007-10-24 18:23:51 +0200209 return balance_tasks(this_rq, this_cpu, busiest, max_load_move, sd,
210 idle, all_pinned, this_best_prio, &rt_rq_iterator);
211}
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200212
Peter Williamse1d14842007-10-24 18:23:51 +0200213static int
214move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
215 struct sched_domain *sd, enum cpu_idle_type idle)
216{
217 struct rq_iterator rt_rq_iterator;
218
219 rt_rq_iterator.start = load_balance_start_rt;
220 rt_rq_iterator.next = load_balance_next_rt;
221 rt_rq_iterator.arg = busiest;
222
223 return iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
224 &rt_rq_iterator);
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200225}
Peter Williams681f3e62007-10-24 18:23:51 +0200226#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200227
228static void task_tick_rt(struct rq *rq, struct task_struct *p)
229{
Peter Zijlstra67e2be02007-12-20 15:01:17 +0100230 update_curr_rt(rq);
231
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200232 /*
233 * RR tasks need a special form of timeslice management.
234 * FIFO tasks have no timeslices.
235 */
236 if (p->policy != SCHED_RR)
237 return;
238
239 if (--p->time_slice)
240 return;
241
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200242 p->time_slice = DEF_TIMESLICE;
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200243
Dmitry Adamushko98fbc792007-08-24 20:39:10 +0200244 /*
245 * Requeue to the end of queue if we are not the only element
246 * on the queue:
247 */
248 if (p->run_list.prev != p->run_list.next) {
249 requeue_task_rt(rq, p);
250 set_tsk_need_resched(p);
251 }
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200252}
253
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200254static void set_curr_task_rt(struct rq *rq)
255{
256 struct task_struct *p = rq->curr;
257
258 p->se.exec_start = rq->clock;
259}
260
Ingo Molnar5522d5d2007-10-15 17:00:12 +0200261const struct sched_class rt_sched_class = {
262 .next = &fair_sched_class,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200263 .enqueue_task = enqueue_task_rt,
264 .dequeue_task = dequeue_task_rt,
265 .yield_task = yield_task_rt,
266
267 .check_preempt_curr = check_preempt_curr_rt,
268
269 .pick_next_task = pick_next_task_rt,
270 .put_prev_task = put_prev_task_rt,
271
Peter Williams681f3e62007-10-24 18:23:51 +0200272#ifdef CONFIG_SMP
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200273 .load_balance = load_balance_rt,
Peter Williamse1d14842007-10-24 18:23:51 +0200274 .move_one_task = move_one_task_rt,
Peter Williams681f3e62007-10-24 18:23:51 +0200275#endif
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200276
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200277 .set_curr_task = set_curr_task_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200278 .task_tick = task_tick_rt,
Ingo Molnarbb44e5d2007-07-09 18:51:58 +0200279};