blob: 5384a977c9a7beaa30a1b0269bb708660453bd6d [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
23/*
Peter Zijlstra21805082007-08-25 18:41:53 +020024 * Targeted preemption latency for CPU-bound tasks:
25 * (default: 20ms, units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020026 *
Peter Zijlstra21805082007-08-25 18:41:53 +020027 * NOTE: this latency value is not the same as the concept of
28 * 'timeslice length' - timeslices in CFS are of variable length.
29 * (to see the precise effective timeslice length of your workload,
30 * run vmstat and monitor the context-switches field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020031 *
32 * On SMP systems the value of this is multiplied by the log2 of the
33 * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
34 * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020036 */
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020037const_debug unsigned int sysctl_sched_latency = 20000000ULL;
38
39/*
40 * After fork, child runs first. (default) If set to 0 then
41 * parent will (try to) run first.
42 */
43const_debug unsigned int sysctl_sched_child_runs_first = 1;
Peter Zijlstra21805082007-08-25 18:41:53 +020044
45/*
46 * Minimal preemption granularity for CPU-bound tasks:
47 * (default: 2 msec, units: nanoseconds)
48 */
Ingo Molnar172ac3d2007-08-25 18:41:53 +020049unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020050
51/*
Ingo Molnar1799e352007-09-19 23:34:46 +020052 * sys_sched_yield() compat mode
53 *
54 * This option switches the agressive yield implementation of the
55 * old scheduler back on.
56 */
57unsigned int __read_mostly sysctl_sched_compat_yield;
58
59/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020060 * SCHED_BATCH wake-up granularity.
Ingo Molnar71fd3712007-08-24 20:39:10 +020061 * (default: 25 msec, units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020062 *
63 * This option delays the preemption effects of decoupled workloads
64 * and reduces their over-scheduling. Synchronous workloads will still
65 * have immediate wakeup/sleep latencies.
66 */
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020067const_debug unsigned int sysctl_sched_batch_wakeup_granularity = 25000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020068
69/*
70 * SCHED_OTHER wake-up granularity.
71 * (default: 1 msec, units: nanoseconds)
72 *
73 * This option delays the preemption effects of decoupled workloads
74 * and reduces their over-scheduling. Synchronous workloads will still
75 * have immediate wakeup/sleep latencies.
76 */
Ingo Molnar2e09bf52007-10-15 17:00:05 +020077const_debug unsigned int sysctl_sched_wakeup_granularity = 2000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020078
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020079extern struct sched_class fair_sched_class;
80
81/**************************************************************
82 * CFS operations on generic schedulable entities:
83 */
84
85#ifdef CONFIG_FAIR_GROUP_SCHED
86
87/* cpu runqueue to which this cfs_rq is attached */
88static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
89{
90 return cfs_rq->rq;
91}
92
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020093/* An entity is a task if it doesn't "own" a runqueue */
94#define entity_is_task(se) (!se->my_q)
95
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020096#else /* CONFIG_FAIR_GROUP_SCHED */
97
98static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
99{
100 return container_of(cfs_rq, struct rq, cfs);
101}
102
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200103#define entity_is_task(se) 1
104
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200105#endif /* CONFIG_FAIR_GROUP_SCHED */
106
107static inline struct task_struct *task_of(struct sched_entity *se)
108{
109 return container_of(se, struct task_struct, se);
110}
111
112
113/**************************************************************
114 * Scheduling class tree data structure manipulation methods:
115 */
116
Peter Zijlstra02e04312007-10-15 17:00:07 +0200117static inline u64
118max_vruntime(u64 min_vruntime, u64 vruntime)
119{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200120 s64 delta = (s64)(vruntime - min_vruntime);
121 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200122 min_vruntime = vruntime;
123
124 return min_vruntime;
125}
126
Peter Zijlstra02e04312007-10-15 17:00:07 +0200127static inline s64
128entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra90146232007-10-15 17:00:05 +0200129{
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200130 return se->vruntime - cfs_rq->min_vruntime;
Peter Zijlstra90146232007-10-15 17:00:05 +0200131}
132
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200133/*
134 * Enqueue an entity into the rb-tree:
135 */
Ingo Molnar19ccd972007-10-15 17:00:04 +0200136static void
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200137__enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
138{
139 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
140 struct rb_node *parent = NULL;
141 struct sched_entity *entry;
Peter Zijlstra90146232007-10-15 17:00:05 +0200142 s64 key = entity_key(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200143 int leftmost = 1;
144
145 /*
146 * Find the right place in the rbtree:
147 */
148 while (*link) {
149 parent = *link;
150 entry = rb_entry(parent, struct sched_entity, run_node);
151 /*
152 * We dont care about collisions. Nodes with
153 * the same key stay together.
154 */
Peter Zijlstra90146232007-10-15 17:00:05 +0200155 if (key < entity_key(cfs_rq, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200156 link = &parent->rb_left;
157 } else {
158 link = &parent->rb_right;
159 leftmost = 0;
160 }
161 }
162
163 /*
164 * Maintain a cache of leftmost tree entries (it is frequently
165 * used):
166 */
167 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200168 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200169
170 rb_link_node(&se->run_node, parent, link);
171 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200172}
173
Ingo Molnar19ccd972007-10-15 17:00:04 +0200174static void
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200175__dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
176{
177 if (cfs_rq->rb_leftmost == &se->run_node)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200178 cfs_rq->rb_leftmost = rb_next(&se->run_node);
Ingo Molnare9acbff2007-10-15 17:00:04 +0200179
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200180 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200181}
182
183static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
184{
185 return cfs_rq->rb_leftmost;
186}
187
188static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
189{
190 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
191}
192
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200193static inline struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
194{
195 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
196 struct sched_entity *se = NULL;
197 struct rb_node *parent;
198
199 while (*link) {
200 parent = *link;
201 se = rb_entry(parent, struct sched_entity, run_node);
202 link = &parent->rb_right;
203 }
204
205 return se;
206}
207
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200208/**************************************************************
209 * Scheduling class statistics methods:
210 */
211
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200212static u64 __sched_period(unsigned long nr_running)
213{
214 u64 period = sysctl_sched_latency;
215 unsigned long nr_latency =
216 sysctl_sched_latency / sysctl_sched_min_granularity;
217
218 if (unlikely(nr_running > nr_latency)) {
219 period *= nr_running;
220 do_div(period, nr_latency);
221 }
222
223 return period;
224}
225
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200226static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200227{
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200228 u64 period = __sched_period(cfs_rq->nr_running);
Peter Zijlstra21805082007-08-25 18:41:53 +0200229
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200230 period *= se->load.weight;
231 do_div(period, cfs_rq->load.weight);
Peter Zijlstra21805082007-08-25 18:41:53 +0200232
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200233 return period;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200234}
235
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200236static u64 __sched_vslice(unsigned long nr_running)
237{
238 u64 period = __sched_period(nr_running);
239
240 do_div(period, nr_running);
241
242 return period;
243}
244
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200245/*
246 * Update the current task's runtime statistics. Skip current tasks that
247 * are not in our scheduling class.
248 */
249static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200250__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
251 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200252{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200253 unsigned long delta_exec_weighted;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200254 u64 next_vruntime, min_vruntime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200255
Ingo Molnar8179ca232007-08-02 17:41:40 +0200256 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200257
258 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200259 schedstat_add(cfs_rq, exec_clock, delta_exec);
Ingo Molnare9acbff2007-10-15 17:00:04 +0200260 delta_exec_weighted = delta_exec;
261 if (unlikely(curr->load.weight != NICE_0_LOAD)) {
262 delta_exec_weighted = calc_delta_fair(delta_exec_weighted,
263 &curr->load);
264 }
265 curr->vruntime += delta_exec_weighted;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200266
267 /*
268 * maintain cfs_rq->min_vruntime to be a monotonic increasing
269 * value tracking the leftmost vruntime in the tree.
270 */
271 if (first_fair(cfs_rq)) {
272 next_vruntime = __pick_next_entity(cfs_rq)->vruntime;
273
274 /* min_vruntime() := !max_vruntime() */
275 min_vruntime = max_vruntime(curr->vruntime, next_vruntime);
276 if (min_vruntime == next_vruntime)
277 min_vruntime = curr->vruntime;
278 else
279 min_vruntime = next_vruntime;
280 } else
281 min_vruntime = curr->vruntime;
282
283 cfs_rq->min_vruntime =
284 max_vruntime(cfs_rq->min_vruntime, min_vruntime);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200285}
286
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200287static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200288{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200289 struct sched_entity *curr = cfs_rq->curr;
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200290 u64 now = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200291 unsigned long delta_exec;
292
293 if (unlikely(!curr))
294 return;
295
296 /*
297 * Get the amount of time the current task was running
298 * since the last time we changed load (this cannot
299 * overflow on 32 bits):
300 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200301 delta_exec = (unsigned long)(now - curr->exec_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200302
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200303 __update_curr(cfs_rq, curr, delta_exec);
304 curr->exec_start = now;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200305}
306
307static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200308update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200309{
Ingo Molnard2819182007-08-09 11:16:47 +0200310 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200311}
312
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200313static inline unsigned long
Ingo Molnar08e23882007-10-15 17:00:04 +0200314calc_weighted(unsigned long delta, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200315{
Ingo Molnar08e23882007-10-15 17:00:04 +0200316 unsigned long weight = se->load.weight;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200317
Ingo Molnar08e23882007-10-15 17:00:04 +0200318 if (unlikely(weight != NICE_0_LOAD))
319 return (u64)delta * se->load.weight >> NICE_0_SHIFT;
320 else
321 return delta;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200322}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200323
324/*
325 * Task is being enqueued - update stats:
326 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200327static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200328{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200329 /*
330 * Are we enqueueing a waiting task? (for current tasks
331 * a dequeue/enqueue event is a NOP)
332 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200333 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200334 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200335}
336
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200337static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200338update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200339{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200340 schedstat_set(se->wait_max, max(se->wait_max,
341 rq_of(cfs_rq)->clock - se->wait_start));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200342 schedstat_set(se->wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200343}
344
345static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200346update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200347{
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200348 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200349 /*
350 * Mark the end of the wait period if dequeueing a
351 * waiting task:
352 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200353 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200354 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200355}
356
357/*
358 * We are picking a new current task - update its stats:
359 */
360static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200361update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200362{
363 /*
364 * We are starting a new run period:
365 */
Ingo Molnard2819182007-08-09 11:16:47 +0200366 se->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200367}
368
369/*
370 * We are descheduling a task - update its stats:
371 */
372static inline void
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200373update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200374{
375 se->exec_start = 0;
376}
377
378/**************************************************
379 * Scheduling class queueing methods:
380 */
381
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200382static void
383account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
384{
385 update_load_add(&cfs_rq->load, se->load.weight);
386 cfs_rq->nr_running++;
387 se->on_rq = 1;
388}
389
390static void
391account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
392{
393 update_load_sub(&cfs_rq->load, se->load.weight);
394 cfs_rq->nr_running--;
395 se->on_rq = 0;
396}
397
Ingo Molnar2396af62007-08-09 11:16:48 +0200398static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200399{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200400#ifdef CONFIG_SCHEDSTATS
401 if (se->sleep_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200402 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200403
404 if ((s64)delta < 0)
405 delta = 0;
406
407 if (unlikely(delta > se->sleep_max))
408 se->sleep_max = delta;
409
410 se->sleep_start = 0;
411 se->sum_sleep_runtime += delta;
412 }
413 if (se->block_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200414 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200415
416 if ((s64)delta < 0)
417 delta = 0;
418
419 if (unlikely(delta > se->block_max))
420 se->block_max = delta;
421
422 se->block_start = 0;
423 se->sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +0200424
425 /*
426 * Blocking time is in units of nanosecs, so shift by 20 to
427 * get a milliseconds-range estimation of the amount of
428 * time that the task spent sleeping:
429 */
430 if (unlikely(prof_on == SLEEP_PROFILING)) {
Ingo Molnare22f5bb2007-10-15 17:00:06 +0200431 struct task_struct *tsk = task_of(se);
432
Ingo Molnar30084fb2007-10-02 14:13:08 +0200433 profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
434 delta >> 20);
435 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200436 }
437#endif
438}
439
Peter Zijlstraddc97292007-10-15 17:00:10 +0200440static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
441{
442#ifdef CONFIG_SCHED_DEBUG
443 s64 d = se->vruntime - cfs_rq->min_vruntime;
444
445 if (d < 0)
446 d = -d;
447
448 if (d > 3*sysctl_sched_latency)
449 schedstat_inc(cfs_rq, nr_spread_over);
450#endif
451}
452
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200453static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200454place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
455{
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200456 u64 vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200457
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200458 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +0200459
460 if (sched_feat(USE_TREE_AVG)) {
461 struct sched_entity *last = __pick_last_entity(cfs_rq);
462 if (last) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200463 vruntime += last->vruntime;
464 vruntime >>= 1;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +0200465 }
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200466 } else if (sched_feat(APPROX_AVG) && cfs_rq->nr_running)
467 vruntime += __sched_vslice(cfs_rq->nr_running)/2;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +0200468
469 if (initial && sched_feat(START_DEBIT))
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200470 vruntime += __sched_vslice(cfs_rq->nr_running + 1);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200471
Ingo Molnar8465e792007-10-15 17:00:11 +0200472 if (!initial) {
Ingo Molnar94359f02007-10-15 17:00:11 +0200473 if (sched_feat(NEW_FAIR_SLEEPERS))
474 vruntime -= sysctl_sched_latency;
475
Ingo Molnarb8487b92007-10-15 17:00:11 +0200476 vruntime = max_t(s64, vruntime, se->vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200477 }
478
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200479 se->vruntime = vruntime;
480
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200481}
482
483static void
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200484enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200485{
486 /*
487 * Update the fair clock.
488 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200489 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200490
Ingo Molnare9acbff2007-10-15 17:00:04 +0200491 if (wakeup) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200492 /* se->vruntime += cfs_rq->min_vruntime; */
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200493 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +0200494 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +0200495 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200496
Ingo Molnard2417e52007-08-09 11:16:47 +0200497 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +0200498 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200499 if (se != cfs_rq->curr)
500 __enqueue_entity(cfs_rq, se);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200501 account_entity_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200502}
503
504static void
Ingo Molnar525c2712007-08-09 11:16:48 +0200505dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200506{
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200507 update_stats_dequeue(cfs_rq, se);
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +0200508 if (sleep) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200509#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200510 if (entity_is_task(se)) {
511 struct task_struct *tsk = task_of(se);
512
513 if (tsk->state & TASK_INTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200514 se->sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200515 if (tsk->state & TASK_UNINTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200516 se->block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +0200518#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200519 }
520
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200521 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200522 __dequeue_entity(cfs_rq, se);
523 account_entity_dequeue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200524}
525
526/*
527 * Preempt the current task with a newly woken task if needed:
528 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +0200529static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +0200530check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200531{
Peter Zijlstra11697832007-09-05 14:32:49 +0200532 unsigned long ideal_runtime, delta_exec;
533
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200534 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +0200535 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
536 if (delta_exec > ideal_runtime)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200537 resched_task(rq_of(cfs_rq)->curr);
538}
539
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200540static void
Ingo Molnar8494f412007-08-09 11:16:48 +0200541set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200542{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200543 /* 'current' is not kept within the tree. */
544 if (se->on_rq) {
545 /*
546 * Any task has to be enqueued before it get to execute on
547 * a CPU. So account for the time it spent waiting on the
548 * runqueue.
549 */
550 update_stats_wait_end(cfs_rq, se);
551 __dequeue_entity(cfs_rq, se);
552 }
553
Ingo Molnar79303e92007-08-09 11:16:47 +0200554 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +0200555 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +0200556#ifdef CONFIG_SCHEDSTATS
557 /*
558 * Track our maximum slice length, if the CPU's load is at
559 * least twice that of our own weight (i.e. dont track it
560 * when there are only lesser-weight tasks around):
561 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +0200562 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Ingo Molnareba1ed42007-10-15 17:00:02 +0200563 se->slice_max = max(se->slice_max,
564 se->sum_exec_runtime - se->prev_sum_exec_runtime);
565 }
566#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +0200567 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200568}
569
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200570static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200571{
572 struct sched_entity *se = __pick_next_entity(cfs_rq);
573
Ingo Molnar8494f412007-08-09 11:16:48 +0200574 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200575
576 return se;
577}
578
Ingo Molnarab6cde22007-08-09 11:16:48 +0200579static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200580{
581 /*
582 * If still on the runqueue then deactivate_task()
583 * was not called and update_curr() has to be done:
584 */
585 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200586 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200587
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200588 update_stats_curr_end(cfs_rq, prev);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200589
Peter Zijlstraddc97292007-10-15 17:00:10 +0200590 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200591 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +0200592 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200593 /* Put 'current' back into the tree. */
594 __enqueue_entity(cfs_rq, prev);
595 }
Ingo Molnar429d43b2007-10-15 17:00:03 +0200596 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200597}
598
599static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
600{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200601 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200602 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200603 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200604 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200605
Ingo Molnar2e09bf52007-10-15 17:00:05 +0200606 if (cfs_rq->nr_running > 1)
607 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200608}
609
610/**************************************************
611 * CFS operations on tasks:
612 */
613
614#ifdef CONFIG_FAIR_GROUP_SCHED
615
616/* Walk up scheduling entities hierarchy */
617#define for_each_sched_entity(se) \
618 for (; se; se = se->parent)
619
620static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
621{
622 return p->se.cfs_rq;
623}
624
625/* runqueue on which this entity is (to be) queued */
626static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
627{
628 return se->cfs_rq;
629}
630
631/* runqueue "owned" by this group */
632static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
633{
634 return grp->my_q;
635}
636
637/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
638 * another cpu ('this_cpu')
639 */
640static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
641{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200642 return cfs_rq->tg->cfs_rq[this_cpu];
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200643}
644
645/* Iterate thr' all leaf cfs_rq's on a runqueue */
646#define for_each_leaf_cfs_rq(rq, cfs_rq) \
647 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
648
649/* Do the two (enqueued) tasks belong to the same group ? */
650static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
651{
652 if (curr->se.cfs_rq == p->se.cfs_rq)
653 return 1;
654
655 return 0;
656}
657
658#else /* CONFIG_FAIR_GROUP_SCHED */
659
660#define for_each_sched_entity(se) \
661 for (; se; se = NULL)
662
663static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
664{
665 return &task_rq(p)->cfs;
666}
667
668static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
669{
670 struct task_struct *p = task_of(se);
671 struct rq *rq = task_rq(p);
672
673 return &rq->cfs;
674}
675
676/* runqueue "owned" by this group */
677static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
678{
679 return NULL;
680}
681
682static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
683{
684 return &cpu_rq(this_cpu)->cfs;
685}
686
687#define for_each_leaf_cfs_rq(rq, cfs_rq) \
688 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
689
690static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
691{
692 return 1;
693}
694
695#endif /* CONFIG_FAIR_GROUP_SCHED */
696
697/*
698 * The enqueue_task method is called before nr_running is
699 * increased. Here we update the fair scheduling stats and
700 * then put the task into the rbtree:
701 */
Ingo Molnarfd390f62007-08-09 11:16:48 +0200702static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200703{
704 struct cfs_rq *cfs_rq;
705 struct sched_entity *se = &p->se;
706
707 for_each_sched_entity(se) {
708 if (se->on_rq)
709 break;
710 cfs_rq = cfs_rq_of(se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200711 enqueue_entity(cfs_rq, se, wakeup);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200712 }
713}
714
715/*
716 * The dequeue_task method is called before nr_running is
717 * decreased. We remove the task from the rbtree and
718 * update the fair scheduling stats:
719 */
Ingo Molnarf02231e2007-08-09 11:16:48 +0200720static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200721{
722 struct cfs_rq *cfs_rq;
723 struct sched_entity *se = &p->se;
724
725 for_each_sched_entity(se) {
726 cfs_rq = cfs_rq_of(se);
Ingo Molnar525c2712007-08-09 11:16:48 +0200727 dequeue_entity(cfs_rq, se, sleep);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200728 /* Don't dequeue parent if it has other entities besides us */
729 if (cfs_rq->load.weight)
730 break;
731 }
732}
733
734/*
Ingo Molnar1799e352007-09-19 23:34:46 +0200735 * sched_yield() support is very simple - we dequeue and enqueue.
736 *
737 * If compat_yield is turned on then we requeue to the end of the tree.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200738 */
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200739static void yield_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200740{
Srivatsa Vaddagiri72ea22f2007-10-15 17:00:08 +0200741 struct cfs_rq *cfs_rq = task_cfs_rq(rq->curr);
Ingo Molnar1799e352007-09-19 23:34:46 +0200742 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200743 struct sched_entity *rightmost, *se = &rq->curr->se;
Ingo Molnar1799e352007-09-19 23:34:46 +0200744 struct rb_node *parent;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200745
746 /*
Ingo Molnar1799e352007-09-19 23:34:46 +0200747 * Are we the only task in the tree?
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200748 */
Ingo Molnar1799e352007-09-19 23:34:46 +0200749 if (unlikely(cfs_rq->nr_running == 1))
750 return;
751
752 if (likely(!sysctl_sched_compat_yield)) {
753 __update_rq_clock(rq);
754 /*
755 * Dequeue and enqueue the task to update its
756 * position within the tree:
757 */
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +0200758 dequeue_entity(cfs_rq, se, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200759 enqueue_entity(cfs_rq, se, 0);
Ingo Molnar1799e352007-09-19 23:34:46 +0200760
761 return;
762 }
763 /*
764 * Find the rightmost entry in the rbtree:
765 */
766 do {
767 parent = *link;
768 link = &parent->rb_right;
769 } while (*link);
770
771 rightmost = rb_entry(parent, struct sched_entity, run_node);
772 /*
773 * Already in the rightmost position?
774 */
775 if (unlikely(rightmost == se))
776 return;
777
778 /*
779 * Minimally necessary key value to be last in the tree:
780 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200781 se->vruntime = rightmost->vruntime + 1;
Ingo Molnar1799e352007-09-19 23:34:46 +0200782
783 if (cfs_rq->rb_leftmost == &se->run_node)
784 cfs_rq->rb_leftmost = rb_next(&se->run_node);
785 /*
786 * Relink the task to the rightmost position:
787 */
788 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
789 rb_link_node(&se->run_node, parent, link);
790 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200791}
792
793/*
794 * Preempt the current task with a newly woken task if needed:
795 */
Ingo Molnar2e09bf52007-10-15 17:00:05 +0200796static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200797{
798 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +0200799 struct cfs_rq *cfs_rq = task_cfs_rq(curr), *pcfs_rq;
800 struct sched_entity *se = &curr->se, *pse = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200801
802 if (unlikely(rt_prio(p->prio))) {
Ingo Molnara8e504d2007-08-09 11:16:47 +0200803 update_rq_clock(rq);
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200804 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200805 resched_task(curr);
806 return;
807 }
808
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +0200809 for_each_sched_entity(se) {
810 cfs_rq = cfs_rq_of(se);
811 pcfs_rq = cfs_rq_of(pse);
812
813 if (cfs_rq == pcfs_rq) {
814 s64 delta = se->vruntime - pse->vruntime;
815
816 if (delta > (s64)sysctl_sched_wakeup_granularity)
817 resched_task(curr);
818 break;
819 }
820#ifdef CONFIG_FAIR_GROUP_SCHED
821 pse = pse->parent;
822#endif
Ingo Molnar2e09bf52007-10-15 17:00:05 +0200823 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200824}
825
Ingo Molnarfb8d4722007-08-09 11:16:48 +0200826static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200827{
828 struct cfs_rq *cfs_rq = &rq->cfs;
829 struct sched_entity *se;
830
831 if (unlikely(!cfs_rq->nr_running))
832 return NULL;
833
834 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200835 se = pick_next_entity(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200836 cfs_rq = group_cfs_rq(se);
837 } while (cfs_rq);
838
839 return task_of(se);
840}
841
842/*
843 * Account for a descheduled task:
844 */
Ingo Molnar31ee5292007-08-09 11:16:49 +0200845static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200846{
847 struct sched_entity *se = &prev->se;
848 struct cfs_rq *cfs_rq;
849
850 for_each_sched_entity(se) {
851 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +0200852 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200853 }
854}
855
856/**************************************************
857 * Fair scheduling class load-balancing methods:
858 */
859
860/*
861 * Load-balancing iterator. Note: while the runqueue stays locked
862 * during the whole iteration, the current task might be
863 * dequeued so the iterator has to be dequeue-safe. Here we
864 * achieve that by always pre-iterating before returning
865 * the current task:
866 */
867static inline struct task_struct *
868__load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
869{
870 struct task_struct *p;
871
872 if (!curr)
873 return NULL;
874
875 p = rb_entry(curr, struct task_struct, se.run_node);
876 cfs_rq->rb_load_balance_curr = rb_next(curr);
877
878 return p;
879}
880
881static struct task_struct *load_balance_start_fair(void *arg)
882{
883 struct cfs_rq *cfs_rq = arg;
884
885 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
886}
887
888static struct task_struct *load_balance_next_fair(void *arg)
889{
890 struct cfs_rq *cfs_rq = arg;
891
892 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
893}
894
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200895#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200896static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
897{
898 struct sched_entity *curr;
899 struct task_struct *p;
900
901 if (!cfs_rq->nr_running)
902 return MAX_PRIO;
903
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200904 curr = cfs_rq->curr;
905 if (!curr)
906 curr = __pick_next_entity(cfs_rq);
907
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200908 p = task_of(curr);
909
910 return p->prio;
911}
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200912#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200913
Peter Williams43010652007-08-09 11:16:46 +0200914static unsigned long
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200915load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200916 unsigned long max_nr_move, unsigned long max_load_move,
917 struct sched_domain *sd, enum cpu_idle_type idle,
918 int *all_pinned, int *this_best_prio)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200919{
920 struct cfs_rq *busy_cfs_rq;
921 unsigned long load_moved, total_nr_moved = 0, nr_moved;
922 long rem_load_move = max_load_move;
923 struct rq_iterator cfs_rq_iterator;
924
925 cfs_rq_iterator.start = load_balance_start_fair;
926 cfs_rq_iterator.next = load_balance_next_fair;
927
928 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200929#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200930 struct cfs_rq *this_cfs_rq;
Ingo Molnare56f31a2007-08-10 23:05:11 +0200931 long imbalance;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200932 unsigned long maxload;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200933
934 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
935
Ingo Molnare56f31a2007-08-10 23:05:11 +0200936 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200937 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
938 if (imbalance <= 0)
939 continue;
940
941 /* Don't pull more than imbalance/2 */
942 imbalance /= 2;
943 maxload = min(rem_load_move, imbalance);
944
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200945 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
946#else
Ingo Molnare56f31a2007-08-10 23:05:11 +0200947# define maxload rem_load_move
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200948#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200949 /* pass busy_cfs_rq argument into
950 * load_balance_[start|next]_fair iterators
951 */
952 cfs_rq_iterator.arg = busy_cfs_rq;
953 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
954 max_nr_move, maxload, sd, idle, all_pinned,
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200955 &load_moved, this_best_prio, &cfs_rq_iterator);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200956
957 total_nr_moved += nr_moved;
958 max_nr_move -= nr_moved;
959 rem_load_move -= load_moved;
960
961 if (max_nr_move <= 0 || rem_load_move <= 0)
962 break;
963 }
964
Peter Williams43010652007-08-09 11:16:46 +0200965 return max_load_move - rem_load_move;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200966}
967
968/*
969 * scheduler tick hitting a task of our scheduling class:
970 */
971static void task_tick_fair(struct rq *rq, struct task_struct *curr)
972{
973 struct cfs_rq *cfs_rq;
974 struct sched_entity *se = &curr->se;
975
976 for_each_sched_entity(se) {
977 cfs_rq = cfs_rq_of(se);
978 entity_tick(cfs_rq, se);
979 }
980}
981
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200982#define swap(a,b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
983
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200984/*
985 * Share the fairness runtime between parent and child, thus the
986 * total amount of pressure for CPU stays equal - new tasks
987 * get a chance to run but frequent forkers are not allowed to
988 * monopolize the CPU. Note: the parent runqueue is locked,
989 * the child is not running yet.
990 */
Ingo Molnaree0827d2007-08-09 11:16:49 +0200991static void task_new_fair(struct rq *rq, struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200992{
993 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Ingo Molnar429d43b2007-10-15 17:00:03 +0200994 struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200995
996 sched_info_queued(p);
997
Ting Yang7109c442007-08-28 12:53:24 +0200998 update_curr(cfs_rq);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200999 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02001000
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02001001 if (sysctl_sched_child_runs_first &&
1002 curr->vruntime < se->vruntime) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02001003 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02001004 * Upon rescheduling, sched_class::put_prev_task() will place
1005 * 'current' within the tree based on its new key value.
1006 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02001007 swap(curr->vruntime, se->vruntime);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02001008 }
1009
Ingo Molnare9acbff2007-10-15 17:00:04 +02001010 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +02001011 check_spread(cfs_rq, se);
1012 check_spread(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001013 __enqueue_entity(cfs_rq, se);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001014 account_entity_enqueue(cfs_rq, se);
Ingo Molnarbb61c212007-10-15 17:00:02 +02001015 resched_task(rq->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001016}
1017
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001018/* Account for a task changing its policy or group.
1019 *
1020 * This routine is mostly called to set cfs_rq->curr field when a task
1021 * migrates between groups/classes.
1022 */
1023static void set_curr_task_fair(struct rq *rq)
1024{
1025 struct sched_entity *se = &rq->curr->se;
1026
1027 for_each_sched_entity(se)
1028 set_next_entity(cfs_rq_of(se), se);
1029}
1030
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001031/*
1032 * All the scheduling class methods:
1033 */
1034struct sched_class fair_sched_class __read_mostly = {
1035 .enqueue_task = enqueue_task_fair,
1036 .dequeue_task = dequeue_task_fair,
1037 .yield_task = yield_task_fair,
1038
Ingo Molnar2e09bf52007-10-15 17:00:05 +02001039 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001040
1041 .pick_next_task = pick_next_task_fair,
1042 .put_prev_task = put_prev_task_fair,
1043
1044 .load_balance = load_balance_fair,
1045
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001046 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001047 .task_tick = task_tick_fair,
1048 .task_new = task_new_fair,
1049};
1050
1051#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001052static void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001053{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001054 struct cfs_rq *cfs_rq;
1055
Srivatsa Vaddagiri75c28ac2007-10-15 17:00:09 +02001056#ifdef CONFIG_FAIR_GROUP_SCHED
1057 print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
1058#endif
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001059 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001060 print_cfs_rq(m, cpu, cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001061}
1062#endif