blob: ce39282d9c0d221205fa793a3a20c77627b90905 [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 */
Peter Zijlstra21805082007-08-25 18:41:53 +020037unsigned int sysctl_sched_latency __read_mostly = 20000000ULL;
38
39/*
40 * Minimal preemption granularity for CPU-bound tasks:
41 * (default: 2 msec, units: nanoseconds)
42 */
Ingo Molnar172ac3d2007-08-25 18:41:53 +020043unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020044
45/*
46 * SCHED_BATCH wake-up granularity.
Ingo Molnar71fd3712007-08-24 20:39:10 +020047 * (default: 25 msec, units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020048 *
49 * This option delays the preemption effects of decoupled workloads
50 * and reduces their over-scheduling. Synchronous workloads will still
51 * have immediate wakeup/sleep latencies.
52 */
Ingo Molnar71fd3712007-08-24 20:39:10 +020053unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly = 25000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020054
55/*
56 * SCHED_OTHER wake-up granularity.
57 * (default: 1 msec, units: nanoseconds)
58 *
59 * This option delays the preemption effects of decoupled workloads
60 * and reduces their over-scheduling. Synchronous workloads will still
61 * have immediate wakeup/sleep latencies.
62 */
Ingo Molnar71fd3712007-08-24 20:39:10 +020063unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020064
65unsigned int sysctl_sched_stat_granularity __read_mostly;
66
67/*
Ingo Molnar71fd3712007-08-24 20:39:10 +020068 * Initialized in sched_init_granularity() [to 5 times the base granularity]:
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020069 */
70unsigned int sysctl_sched_runtime_limit __read_mostly;
71
72/*
73 * Debugging: various feature bits
74 */
75enum {
76 SCHED_FEAT_FAIR_SLEEPERS = 1,
77 SCHED_FEAT_SLEEPER_AVG = 2,
78 SCHED_FEAT_SLEEPER_LOAD_AVG = 4,
79 SCHED_FEAT_PRECISE_CPU_LOAD = 8,
80 SCHED_FEAT_START_DEBIT = 16,
81 SCHED_FEAT_SKIP_INITIAL = 32,
82};
83
84unsigned int sysctl_sched_features __read_mostly =
85 SCHED_FEAT_FAIR_SLEEPERS *1 |
Ingo Molnar5d2b3d32007-08-12 18:08:19 +020086 SCHED_FEAT_SLEEPER_AVG *0 |
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020087 SCHED_FEAT_SLEEPER_LOAD_AVG *1 |
88 SCHED_FEAT_PRECISE_CPU_LOAD *1 |
89 SCHED_FEAT_START_DEBIT *1 |
90 SCHED_FEAT_SKIP_INITIAL *0;
91
92extern struct sched_class fair_sched_class;
93
94/**************************************************************
95 * CFS operations on generic schedulable entities:
96 */
97
98#ifdef CONFIG_FAIR_GROUP_SCHED
99
100/* cpu runqueue to which this cfs_rq is attached */
101static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
102{
103 return cfs_rq->rq;
104}
105
106/* currently running entity (if any) on this cfs_rq */
107static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
108{
109 return cfs_rq->curr;
110}
111
112/* An entity is a task if it doesn't "own" a runqueue */
113#define entity_is_task(se) (!se->my_q)
114
115static inline void
116set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se)
117{
118 cfs_rq->curr = se;
119}
120
121#else /* CONFIG_FAIR_GROUP_SCHED */
122
123static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
124{
125 return container_of(cfs_rq, struct rq, cfs);
126}
127
128static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
129{
130 struct rq *rq = rq_of(cfs_rq);
131
132 if (unlikely(rq->curr->sched_class != &fair_sched_class))
133 return NULL;
134
135 return &rq->curr->se;
136}
137
138#define entity_is_task(se) 1
139
140static inline void
141set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
142
143#endif /* CONFIG_FAIR_GROUP_SCHED */
144
145static inline struct task_struct *task_of(struct sched_entity *se)
146{
147 return container_of(se, struct task_struct, se);
148}
149
150
151/**************************************************************
152 * Scheduling class tree data structure manipulation methods:
153 */
154
155/*
156 * Enqueue an entity into the rb-tree:
157 */
158static inline void
159__enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
160{
161 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
162 struct rb_node *parent = NULL;
163 struct sched_entity *entry;
164 s64 key = se->fair_key;
165 int leftmost = 1;
166
167 /*
168 * Find the right place in the rbtree:
169 */
170 while (*link) {
171 parent = *link;
172 entry = rb_entry(parent, struct sched_entity, run_node);
173 /*
174 * We dont care about collisions. Nodes with
175 * the same key stay together.
176 */
177 if (key - entry->fair_key < 0) {
178 link = &parent->rb_left;
179 } else {
180 link = &parent->rb_right;
181 leftmost = 0;
182 }
183 }
184
185 /*
186 * Maintain a cache of leftmost tree entries (it is frequently
187 * used):
188 */
189 if (leftmost)
190 cfs_rq->rb_leftmost = &se->run_node;
191
192 rb_link_node(&se->run_node, parent, link);
193 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
194 update_load_add(&cfs_rq->load, se->load.weight);
195 cfs_rq->nr_running++;
196 se->on_rq = 1;
197}
198
199static inline void
200__dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
201{
202 if (cfs_rq->rb_leftmost == &se->run_node)
203 cfs_rq->rb_leftmost = rb_next(&se->run_node);
204 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
205 update_load_sub(&cfs_rq->load, se->load.weight);
206 cfs_rq->nr_running--;
207 se->on_rq = 0;
208}
209
210static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
211{
212 return cfs_rq->rb_leftmost;
213}
214
215static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
216{
217 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
218}
219
220/**************************************************************
221 * Scheduling class statistics methods:
222 */
223
224/*
Peter Zijlstra21805082007-08-25 18:41:53 +0200225 * Calculate the preemption granularity needed to schedule every
226 * runnable task once per sysctl_sched_latency amount of time.
227 * (down to a sensible low limit on granularity)
228 *
229 * For example, if there are 2 tasks running and latency is 10 msecs,
230 * we switch tasks every 5 msecs. If we have 3 tasks running, we have
231 * to switch tasks every 3.33 msecs to get a 10 msecs observed latency
232 * for each task. We do finer and finer scheduling up to until we
233 * reach the minimum granularity value.
234 *
235 * To achieve this we use the following dynamic-granularity rule:
236 *
237 * gran = lat/nr - lat/nr/nr
238 *
239 * This comes out of the following equations:
240 *
241 * kA1 + gran = kB1
242 * kB2 + gran = kA2
243 * kA2 = kA1
244 * kB2 = kB1 - d + d/nr
245 * lat = d * nr
246 *
247 * Where 'k' is key, 'A' is task A (waiting), 'B' is task B (running),
248 * '1' is start of time, '2' is end of time, 'd' is delay between
249 * 1 and 2 (during which task B was running), 'nr' is number of tasks
250 * running, 'lat' is the the period of each task. ('lat' is the
251 * sched_latency that we aim for.)
252 */
253static long
254sched_granularity(struct cfs_rq *cfs_rq)
255{
256 unsigned int gran = sysctl_sched_latency;
257 unsigned int nr = cfs_rq->nr_running;
258
259 if (nr > 1) {
260 gran = gran/nr - gran/nr/nr;
Ingo Molnar172ac3d2007-08-25 18:41:53 +0200261 gran = max(gran, sysctl_sched_min_granularity);
Peter Zijlstra21805082007-08-25 18:41:53 +0200262 }
263
264 return gran;
265}
266
267/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200268 * We rescale the rescheduling granularity of tasks according to their
269 * nice level, but only linearly, not exponentially:
270 */
271static long
272niced_granularity(struct sched_entity *curr, unsigned long granularity)
273{
274 u64 tmp;
275
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200276 if (likely(curr->load.weight == NICE_0_LOAD))
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200277 return granularity;
278 /*
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200279 * Positive nice levels get the same granularity as nice-0:
280 */
281 if (likely(curr->load.weight < NICE_0_LOAD)) {
282 tmp = curr->load.weight * (u64)granularity;
283 return (long) (tmp >> NICE_0_SHIFT);
284 }
285 /*
286 * Negative nice level tasks get linearly finer
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200287 * granularity:
288 */
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200289 tmp = curr->load.inv_weight * (u64)granularity;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200290
291 /*
292 * It will always fit into 'long':
293 */
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200294 return (long) (tmp >> WMULT_SHIFT);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200295}
296
297static inline void
298limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
299{
300 long limit = sysctl_sched_runtime_limit;
301
302 /*
303 * Niced tasks have the same history dynamic range as
304 * non-niced tasks:
305 */
306 if (unlikely(se->wait_runtime > limit)) {
307 se->wait_runtime = limit;
308 schedstat_inc(se, wait_runtime_overruns);
309 schedstat_inc(cfs_rq, wait_runtime_overruns);
310 }
311 if (unlikely(se->wait_runtime < -limit)) {
312 se->wait_runtime = -limit;
313 schedstat_inc(se, wait_runtime_underruns);
314 schedstat_inc(cfs_rq, wait_runtime_underruns);
315 }
316}
317
318static inline void
319__add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
320{
321 se->wait_runtime += delta;
322 schedstat_add(se, sum_wait_runtime, delta);
323 limit_wait_runtime(cfs_rq, se);
324}
325
326static void
327add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
328{
329 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
330 __add_wait_runtime(cfs_rq, se, delta);
331 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
332}
333
334/*
335 * Update the current task's runtime statistics. Skip current tasks that
336 * are not in our scheduling class.
337 */
338static inline void
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200339__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200340{
Ingo Molnarc5dcfe72007-08-09 11:16:46 +0200341 unsigned long delta, delta_exec, delta_fair, delta_mine;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200342 struct load_weight *lw = &cfs_rq->load;
343 unsigned long load = lw->weight;
344
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200345 delta_exec = curr->delta_exec;
Ingo Molnar8179ca232007-08-02 17:41:40 +0200346 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200347
348 curr->sum_exec_runtime += delta_exec;
349 cfs_rq->exec_clock += delta_exec;
350
Ingo Molnarfd8bb432007-08-09 11:16:46 +0200351 if (unlikely(!load))
352 return;
353
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200354 delta_fair = calc_delta_fair(delta_exec, lw);
355 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
356
Mike Galbraith5f01d512007-08-28 12:53:24 +0200357 if (cfs_rq->sleeper_bonus > sysctl_sched_min_granularity) {
Peter Zijlstraea0aa3b2007-08-24 20:39:10 +0200358 delta = min((u64)delta_mine, cfs_rq->sleeper_bonus);
Ingo Molnarb2133c82007-08-24 20:39:10 +0200359 delta = min(delta, (unsigned long)(
360 (long)sysctl_sched_runtime_limit - curr->wait_runtime));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200361 cfs_rq->sleeper_bonus -= delta;
362 delta_mine -= delta;
363 }
364
365 cfs_rq->fair_clock += delta_fair;
366 /*
367 * We executed delta_exec amount of time on the CPU,
368 * but we were only entitled to delta_mine amount of
369 * time during that period (if nr_running == 1 then
370 * the two values are equal)
371 * [Note: delta_mine - delta_exec is negative]:
372 */
373 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
374}
375
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200376static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200377{
378 struct sched_entity *curr = cfs_rq_curr(cfs_rq);
379 unsigned long delta_exec;
380
381 if (unlikely(!curr))
382 return;
383
384 /*
385 * Get the amount of time the current task was running
386 * since the last time we changed load (this cannot
387 * overflow on 32 bits):
388 */
Ingo Molnard2819182007-08-09 11:16:47 +0200389 delta_exec = (unsigned long)(rq_of(cfs_rq)->clock - curr->exec_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200390
391 curr->delta_exec += delta_exec;
392
393 if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200394 __update_curr(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200395 curr->delta_exec = 0;
396 }
Ingo Molnard2819182007-08-09 11:16:47 +0200397 curr->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200398}
399
400static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200401update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200402{
403 se->wait_start_fair = cfs_rq->fair_clock;
Ingo Molnard2819182007-08-09 11:16:47 +0200404 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200405}
406
407/*
408 * We calculate fair deltas here, so protect against the random effects
409 * of a multiplication overflow by capping it to the runtime limit:
410 */
411#if BITS_PER_LONG == 32
412static inline unsigned long
413calc_weighted(unsigned long delta, unsigned long weight, int shift)
414{
415 u64 tmp = (u64)delta * weight >> shift;
416
417 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
418 return sysctl_sched_runtime_limit*2;
419 return tmp;
420}
421#else
422static inline unsigned long
423calc_weighted(unsigned long delta, unsigned long weight, int shift)
424{
425 return delta * weight >> shift;
426}
427#endif
428
429/*
430 * Task is being enqueued - update stats:
431 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200432static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200433{
434 s64 key;
435
436 /*
437 * Are we enqueueing a waiting task? (for current tasks
438 * a dequeue/enqueue event is a NOP)
439 */
440 if (se != cfs_rq_curr(cfs_rq))
Ingo Molnar5870db52007-08-09 11:16:47 +0200441 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200442 /*
443 * Update the key:
444 */
445 key = cfs_rq->fair_clock;
446
447 /*
448 * Optimize the common nice 0 case:
449 */
450 if (likely(se->load.weight == NICE_0_LOAD)) {
451 key -= se->wait_runtime;
452 } else {
453 u64 tmp;
454
455 if (se->wait_runtime < 0) {
456 tmp = -se->wait_runtime;
457 key += (tmp * se->load.inv_weight) >>
458 (WMULT_SHIFT - NICE_0_SHIFT);
459 } else {
460 tmp = se->wait_runtime;
Ingo Molnara69edb52007-08-09 11:16:52 +0200461 key -= (tmp * se->load.inv_weight) >>
462 (WMULT_SHIFT - NICE_0_SHIFT);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200463 }
464 }
465
466 se->fair_key = key;
467}
468
469/*
470 * Note: must be called with a freshly updated rq->fair_clock.
471 */
472static inline void
Ingo Molnareac55ea2007-08-09 11:16:47 +0200473__update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200474{
475 unsigned long delta_fair = se->delta_fair_run;
476
Ingo Molnard2819182007-08-09 11:16:47 +0200477 schedstat_set(se->wait_max, max(se->wait_max,
478 rq_of(cfs_rq)->clock - se->wait_start));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200479
480 if (unlikely(se->load.weight != NICE_0_LOAD))
481 delta_fair = calc_weighted(delta_fair, se->load.weight,
482 NICE_0_SHIFT);
483
484 add_wait_runtime(cfs_rq, se, delta_fair);
485}
486
487static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200488update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200489{
490 unsigned long delta_fair;
491
Ingo Molnarb77d69d2007-08-28 12:53:24 +0200492 if (unlikely(!se->wait_start_fair))
493 return;
494
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200495 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
496 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
497
498 se->delta_fair_run += delta_fair;
499 if (unlikely(abs(se->delta_fair_run) >=
500 sysctl_sched_stat_granularity)) {
Ingo Molnareac55ea2007-08-09 11:16:47 +0200501 __update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200502 se->delta_fair_run = 0;
503 }
504
505 se->wait_start_fair = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200506 schedstat_set(se->wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200507}
508
509static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200510update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200511{
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200512 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200513 /*
514 * Mark the end of the wait period if dequeueing a
515 * waiting task:
516 */
517 if (se != cfs_rq_curr(cfs_rq))
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200518 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200519}
520
521/*
522 * We are picking a new current task - update its stats:
523 */
524static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200525update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200526{
527 /*
528 * We are starting a new run period:
529 */
Ingo Molnard2819182007-08-09 11:16:47 +0200530 se->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200531}
532
533/*
534 * We are descheduling a task - update its stats:
535 */
536static inline void
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200537update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200538{
539 se->exec_start = 0;
540}
541
542/**************************************************
543 * Scheduling class queueing methods:
544 */
545
Ingo Molnardfdc119e2007-08-09 11:16:48 +0200546static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200547{
548 unsigned long load = cfs_rq->load.weight, delta_fair;
549 long prev_runtime;
550
Ingo Molnarb2133c82007-08-24 20:39:10 +0200551 /*
552 * Do not boost sleepers if there's too much bonus 'in flight'
553 * already:
554 */
555 if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
556 return;
557
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200558 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
559 load = rq_of(cfs_rq)->cpu_load[2];
560
561 delta_fair = se->delta_fair_sleep;
562
563 /*
564 * Fix up delta_fair with the effect of us running
565 * during the whole sleep period:
566 */
567 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
568 delta_fair = div64_likely32((u64)delta_fair * load,
569 load + se->load.weight);
570
571 if (unlikely(se->load.weight != NICE_0_LOAD))
572 delta_fair = calc_weighted(delta_fair, se->load.weight,
573 NICE_0_SHIFT);
574
575 prev_runtime = se->wait_runtime;
576 __add_wait_runtime(cfs_rq, se, delta_fair);
Ingo Molnarb2133c82007-08-24 20:39:10 +0200577 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200578 delta_fair = se->wait_runtime - prev_runtime;
579
580 /*
581 * Track the amount of bonus we've given to sleepers:
582 */
583 cfs_rq->sleeper_bonus += delta_fair;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200584}
585
Ingo Molnar2396af62007-08-09 11:16:48 +0200586static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200587{
588 struct task_struct *tsk = task_of(se);
589 unsigned long delta_fair;
590
591 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
592 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
593 return;
594
595 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
596 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
597
598 se->delta_fair_sleep += delta_fair;
599 if (unlikely(abs(se->delta_fair_sleep) >=
600 sysctl_sched_stat_granularity)) {
Ingo Molnardfdc119e2007-08-09 11:16:48 +0200601 __enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200602 se->delta_fair_sleep = 0;
603 }
604
605 se->sleep_start_fair = 0;
606
607#ifdef CONFIG_SCHEDSTATS
608 if (se->sleep_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200609 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200610
611 if ((s64)delta < 0)
612 delta = 0;
613
614 if (unlikely(delta > se->sleep_max))
615 se->sleep_max = delta;
616
617 se->sleep_start = 0;
618 se->sum_sleep_runtime += delta;
619 }
620 if (se->block_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200621 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200622
623 if ((s64)delta < 0)
624 delta = 0;
625
626 if (unlikely(delta > se->block_max))
627 se->block_max = delta;
628
629 se->block_start = 0;
630 se->sum_sleep_runtime += delta;
631 }
632#endif
633}
634
635static void
Ingo Molnar668031c2007-08-09 11:16:48 +0200636enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200637{
638 /*
639 * Update the fair clock.
640 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200641 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200642
643 if (wakeup)
Ingo Molnar2396af62007-08-09 11:16:48 +0200644 enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200645
Ingo Molnard2417e52007-08-09 11:16:47 +0200646 update_stats_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200647 __enqueue_entity(cfs_rq, se);
648}
649
650static void
Ingo Molnar525c2712007-08-09 11:16:48 +0200651dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200652{
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200653 update_stats_dequeue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200654 if (sleep) {
655 se->sleep_start_fair = cfs_rq->fair_clock;
656#ifdef CONFIG_SCHEDSTATS
657 if (entity_is_task(se)) {
658 struct task_struct *tsk = task_of(se);
659
660 if (tsk->state & TASK_INTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200661 se->sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200662 if (tsk->state & TASK_UNINTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200663 se->block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200664 }
665 cfs_rq->wait_runtime -= se->wait_runtime;
666#endif
667 }
668 __dequeue_entity(cfs_rq, se);
669}
670
671/*
672 * Preempt the current task with a newly woken task if needed:
673 */
Ingo Molnarf6cf8912007-08-28 12:53:24 +0200674static int
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200675__check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
676 struct sched_entity *curr, unsigned long granularity)
677{
678 s64 __delta = curr->fair_key - se->fair_key;
679
680 /*
681 * Take scheduling granularity into account - do not
682 * preempt the current task unless the best task has
683 * a larger than sched_granularity fairness advantage:
684 */
Ingo Molnarf6cf8912007-08-28 12:53:24 +0200685 if (__delta > niced_granularity(curr, granularity)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200686 resched_task(rq_of(cfs_rq)->curr);
Ingo Molnarf6cf8912007-08-28 12:53:24 +0200687 return 1;
688 }
689 return 0;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200690}
691
692static inline void
Ingo Molnar8494f412007-08-09 11:16:48 +0200693set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200694{
695 /*
696 * Any task has to be enqueued before it get to execute on
697 * a CPU. So account for the time it spent waiting on the
698 * runqueue. (note, here we rely on pick_next_task() having
699 * done a put_prev_task_fair() shortly before this, which
700 * updated rq->fair_clock - used by update_stats_wait_end())
701 */
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200702 update_stats_wait_end(cfs_rq, se);
Ingo Molnar79303e92007-08-09 11:16:47 +0200703 update_stats_curr_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200704 set_cfs_rq_curr(cfs_rq, se);
705}
706
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200707static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200708{
709 struct sched_entity *se = __pick_next_entity(cfs_rq);
710
Ingo Molnar8494f412007-08-09 11:16:48 +0200711 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200712
713 return se;
714}
715
Ingo Molnarab6cde22007-08-09 11:16:48 +0200716static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200717{
718 /*
719 * If still on the runqueue then deactivate_task()
720 * was not called and update_curr() has to be done:
721 */
722 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200723 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200724
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200725 update_stats_curr_end(cfs_rq, prev);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200726
727 if (prev->on_rq)
Ingo Molnar5870db52007-08-09 11:16:47 +0200728 update_stats_wait_start(cfs_rq, prev);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200729 set_cfs_rq_curr(cfs_rq, NULL);
730}
731
732static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
733{
Ingo Molnarf6cf8912007-08-28 12:53:24 +0200734 unsigned long gran, ideal_runtime, delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200735 struct sched_entity *next;
Ingo Molnarc1b3da32007-08-09 11:16:47 +0200736
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200737 /*
738 * Dequeue and enqueue the task to update its
739 * position within the tree:
740 */
Ingo Molnar525c2712007-08-09 11:16:48 +0200741 dequeue_entity(cfs_rq, curr, 0);
Ingo Molnar668031c2007-08-09 11:16:48 +0200742 enqueue_entity(cfs_rq, curr, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200743
744 /*
745 * Reschedule if another task tops the current one.
746 */
747 next = __pick_next_entity(cfs_rq);
748 if (next == curr)
749 return;
750
Ingo Molnarf6cf8912007-08-28 12:53:24 +0200751 gran = sched_granularity(cfs_rq);
752 ideal_runtime = niced_granularity(curr,
753 max(sysctl_sched_latency / cfs_rq->nr_running,
754 (unsigned long)sysctl_sched_min_granularity));
755 /*
756 * If we executed more than what the latency constraint suggests,
757 * reduce the rescheduling granularity. This way the total latency
758 * of how much a task is not scheduled converges to
759 * sysctl_sched_latency:
760 */
761 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
762 if (delta_exec > ideal_runtime)
763 gran = 0;
764
765 if (__check_preempt_curr_fair(cfs_rq, next, curr, gran))
766 curr->prev_sum_exec_runtime = curr->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200767}
768
769/**************************************************
770 * CFS operations on tasks:
771 */
772
773#ifdef CONFIG_FAIR_GROUP_SCHED
774
775/* Walk up scheduling entities hierarchy */
776#define for_each_sched_entity(se) \
777 for (; se; se = se->parent)
778
779static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
780{
781 return p->se.cfs_rq;
782}
783
784/* runqueue on which this entity is (to be) queued */
785static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
786{
787 return se->cfs_rq;
788}
789
790/* runqueue "owned" by this group */
791static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
792{
793 return grp->my_q;
794}
795
796/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
797 * another cpu ('this_cpu')
798 */
799static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
800{
801 /* A later patch will take group into account */
802 return &cpu_rq(this_cpu)->cfs;
803}
804
805/* Iterate thr' all leaf cfs_rq's on a runqueue */
806#define for_each_leaf_cfs_rq(rq, cfs_rq) \
807 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
808
809/* Do the two (enqueued) tasks belong to the same group ? */
810static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
811{
812 if (curr->se.cfs_rq == p->se.cfs_rq)
813 return 1;
814
815 return 0;
816}
817
818#else /* CONFIG_FAIR_GROUP_SCHED */
819
820#define for_each_sched_entity(se) \
821 for (; se; se = NULL)
822
823static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
824{
825 return &task_rq(p)->cfs;
826}
827
828static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
829{
830 struct task_struct *p = task_of(se);
831 struct rq *rq = task_rq(p);
832
833 return &rq->cfs;
834}
835
836/* runqueue "owned" by this group */
837static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
838{
839 return NULL;
840}
841
842static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
843{
844 return &cpu_rq(this_cpu)->cfs;
845}
846
847#define for_each_leaf_cfs_rq(rq, cfs_rq) \
848 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
849
850static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
851{
852 return 1;
853}
854
855#endif /* CONFIG_FAIR_GROUP_SCHED */
856
857/*
858 * The enqueue_task method is called before nr_running is
859 * increased. Here we update the fair scheduling stats and
860 * then put the task into the rbtree:
861 */
Ingo Molnarfd390f62007-08-09 11:16:48 +0200862static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200863{
864 struct cfs_rq *cfs_rq;
865 struct sched_entity *se = &p->se;
866
867 for_each_sched_entity(se) {
868 if (se->on_rq)
869 break;
870 cfs_rq = cfs_rq_of(se);
Ingo Molnar668031c2007-08-09 11:16:48 +0200871 enqueue_entity(cfs_rq, se, wakeup);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200872 }
873}
874
875/*
876 * The dequeue_task method is called before nr_running is
877 * decreased. We remove the task from the rbtree and
878 * update the fair scheduling stats:
879 */
Ingo Molnarf02231e2007-08-09 11:16:48 +0200880static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200881{
882 struct cfs_rq *cfs_rq;
883 struct sched_entity *se = &p->se;
884
885 for_each_sched_entity(se) {
886 cfs_rq = cfs_rq_of(se);
Ingo Molnar525c2712007-08-09 11:16:48 +0200887 dequeue_entity(cfs_rq, se, sleep);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200888 /* Don't dequeue parent if it has other entities besides us */
889 if (cfs_rq->load.weight)
890 break;
891 }
892}
893
894/*
895 * sched_yield() support is very simple - we dequeue and enqueue
896 */
897static void yield_task_fair(struct rq *rq, struct task_struct *p)
898{
899 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200900
Ingo Molnarc1b3da32007-08-09 11:16:47 +0200901 __update_rq_clock(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200902 /*
903 * Dequeue and enqueue the task to update its
904 * position within the tree:
905 */
Ingo Molnar525c2712007-08-09 11:16:48 +0200906 dequeue_entity(cfs_rq, &p->se, 0);
Ingo Molnar668031c2007-08-09 11:16:48 +0200907 enqueue_entity(cfs_rq, &p->se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200908}
909
910/*
911 * Preempt the current task with a newly woken task if needed:
912 */
913static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
914{
915 struct task_struct *curr = rq->curr;
916 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
917 unsigned long gran;
918
919 if (unlikely(rt_prio(p->prio))) {
Ingo Molnara8e504d2007-08-09 11:16:47 +0200920 update_rq_clock(rq);
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200921 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200922 resched_task(curr);
923 return;
924 }
925
926 gran = sysctl_sched_wakeup_granularity;
927 /*
928 * Batch tasks prefer throughput over latency:
929 */
930 if (unlikely(p->policy == SCHED_BATCH))
931 gran = sysctl_sched_batch_wakeup_granularity;
932
933 if (is_same_group(curr, p))
934 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
935}
936
Ingo Molnarfb8d4722007-08-09 11:16:48 +0200937static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200938{
939 struct cfs_rq *cfs_rq = &rq->cfs;
940 struct sched_entity *se;
941
942 if (unlikely(!cfs_rq->nr_running))
943 return NULL;
944
945 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200946 se = pick_next_entity(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200947 cfs_rq = group_cfs_rq(se);
948 } while (cfs_rq);
949
950 return task_of(se);
951}
952
953/*
954 * Account for a descheduled task:
955 */
Ingo Molnar31ee5292007-08-09 11:16:49 +0200956static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200957{
958 struct sched_entity *se = &prev->se;
959 struct cfs_rq *cfs_rq;
960
961 for_each_sched_entity(se) {
962 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +0200963 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200964 }
965}
966
967/**************************************************
968 * Fair scheduling class load-balancing methods:
969 */
970
971/*
972 * Load-balancing iterator. Note: while the runqueue stays locked
973 * during the whole iteration, the current task might be
974 * dequeued so the iterator has to be dequeue-safe. Here we
975 * achieve that by always pre-iterating before returning
976 * the current task:
977 */
978static inline struct task_struct *
979__load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
980{
981 struct task_struct *p;
982
983 if (!curr)
984 return NULL;
985
986 p = rb_entry(curr, struct task_struct, se.run_node);
987 cfs_rq->rb_load_balance_curr = rb_next(curr);
988
989 return p;
990}
991
992static struct task_struct *load_balance_start_fair(void *arg)
993{
994 struct cfs_rq *cfs_rq = arg;
995
996 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
997}
998
999static struct task_struct *load_balance_next_fair(void *arg)
1000{
1001 struct cfs_rq *cfs_rq = arg;
1002
1003 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
1004}
1005
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001006#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001007static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
1008{
1009 struct sched_entity *curr;
1010 struct task_struct *p;
1011
1012 if (!cfs_rq->nr_running)
1013 return MAX_PRIO;
1014
1015 curr = __pick_next_entity(cfs_rq);
1016 p = task_of(curr);
1017
1018 return p->prio;
1019}
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001020#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001021
Peter Williams43010652007-08-09 11:16:46 +02001022static unsigned long
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001023load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001024 unsigned long max_nr_move, unsigned long max_load_move,
1025 struct sched_domain *sd, enum cpu_idle_type idle,
1026 int *all_pinned, int *this_best_prio)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001027{
1028 struct cfs_rq *busy_cfs_rq;
1029 unsigned long load_moved, total_nr_moved = 0, nr_moved;
1030 long rem_load_move = max_load_move;
1031 struct rq_iterator cfs_rq_iterator;
1032
1033 cfs_rq_iterator.start = load_balance_start_fair;
1034 cfs_rq_iterator.next = load_balance_next_fair;
1035
1036 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001037#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001038 struct cfs_rq *this_cfs_rq;
Ingo Molnare56f31a2007-08-10 23:05:11 +02001039 long imbalance;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001040 unsigned long maxload;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001041
1042 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
1043
Ingo Molnare56f31a2007-08-10 23:05:11 +02001044 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001045 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
1046 if (imbalance <= 0)
1047 continue;
1048
1049 /* Don't pull more than imbalance/2 */
1050 imbalance /= 2;
1051 maxload = min(rem_load_move, imbalance);
1052
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001053 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
1054#else
Ingo Molnare56f31a2007-08-10 23:05:11 +02001055# define maxload rem_load_move
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001056#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001057 /* pass busy_cfs_rq argument into
1058 * load_balance_[start|next]_fair iterators
1059 */
1060 cfs_rq_iterator.arg = busy_cfs_rq;
1061 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
1062 max_nr_move, maxload, sd, idle, all_pinned,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001063 &load_moved, this_best_prio, &cfs_rq_iterator);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001064
1065 total_nr_moved += nr_moved;
1066 max_nr_move -= nr_moved;
1067 rem_load_move -= load_moved;
1068
1069 if (max_nr_move <= 0 || rem_load_move <= 0)
1070 break;
1071 }
1072
Peter Williams43010652007-08-09 11:16:46 +02001073 return max_load_move - rem_load_move;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001074}
1075
1076/*
1077 * scheduler tick hitting a task of our scheduling class:
1078 */
1079static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1080{
1081 struct cfs_rq *cfs_rq;
1082 struct sched_entity *se = &curr->se;
1083
1084 for_each_sched_entity(se) {
1085 cfs_rq = cfs_rq_of(se);
1086 entity_tick(cfs_rq, se);
1087 }
1088}
1089
1090/*
1091 * Share the fairness runtime between parent and child, thus the
1092 * total amount of pressure for CPU stays equal - new tasks
1093 * get a chance to run but frequent forkers are not allowed to
1094 * monopolize the CPU. Note: the parent runqueue is locked,
1095 * the child is not running yet.
1096 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02001097static void task_new_fair(struct rq *rq, struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001098{
1099 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Ting Yang7109c4422007-08-28 12:53:24 +02001100 struct sched_entity *se = &p->se, *curr = cfs_rq_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001101
1102 sched_info_queued(p);
1103
Ting Yang7109c4422007-08-28 12:53:24 +02001104 update_curr(cfs_rq);
Ingo Molnard2417e52007-08-09 11:16:47 +02001105 update_stats_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001106 /*
1107 * Child runs first: we let it run before the parent
1108 * until it reschedules once. We set up the key so that
1109 * it will preempt the parent:
1110 */
Ingo Molnar9f508f82007-08-28 12:53:24 +02001111 se->fair_key = curr->fair_key -
Ting Yang7109c4422007-08-28 12:53:24 +02001112 niced_granularity(curr, sched_granularity(cfs_rq)) - 1;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001113 /*
1114 * The first wait is dominated by the child-runs-first logic,
1115 * so do not credit it with that waiting time yet:
1116 */
1117 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
Ingo Molnar9f508f82007-08-28 12:53:24 +02001118 se->wait_start_fair = 0;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001119
1120 /*
1121 * The statistical average of wait_runtime is about
1122 * -granularity/2, so initialize the task with that:
1123 */
Ingo Molnar213c8af2007-08-28 12:53:24 +02001124 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT) {
Ingo Molnar9f508f82007-08-28 12:53:24 +02001125 se->wait_runtime = -(sched_granularity(cfs_rq) / 2);
Ingo Molnar213c8af2007-08-28 12:53:24 +02001126 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
1127 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001128
1129 __enqueue_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001130}
1131
1132#ifdef CONFIG_FAIR_GROUP_SCHED
1133/* Account for a task changing its policy or group.
1134 *
1135 * This routine is mostly called to set cfs_rq->curr field when a task
1136 * migrates between groups/classes.
1137 */
1138static void set_curr_task_fair(struct rq *rq)
1139{
Bruce Ashfield7c6c16f2007-08-24 20:39:10 +02001140 struct sched_entity *se = &rq->curr->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001141
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001142 for_each_sched_entity(se)
1143 set_next_entity(cfs_rq_of(se), se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001144}
1145#else
1146static void set_curr_task_fair(struct rq *rq)
1147{
1148}
1149#endif
1150
1151/*
1152 * All the scheduling class methods:
1153 */
1154struct sched_class fair_sched_class __read_mostly = {
1155 .enqueue_task = enqueue_task_fair,
1156 .dequeue_task = dequeue_task_fair,
1157 .yield_task = yield_task_fair,
1158
1159 .check_preempt_curr = check_preempt_curr_fair,
1160
1161 .pick_next_task = pick_next_task_fair,
1162 .put_prev_task = put_prev_task_fair,
1163
1164 .load_balance = load_balance_fair,
1165
1166 .set_curr_task = set_curr_task_fair,
1167 .task_tick = task_tick_fair,
1168 .task_new = task_new_fair,
1169};
1170
1171#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001172static void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001173{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001174 struct cfs_rq *cfs_rq;
1175
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001176 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001177 print_cfs_rq(m, cpu, cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001178}
1179#endif