blob: 7a632c534ce5d82287db0a2d9074363c4ec9e7f6 [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>
18 */
19
20/*
21 * Preemption granularity:
22 * (default: 2 msec, units: nanoseconds)
23 *
24 * NOTE: this granularity value is not the same as the concept of
25 * 'timeslice length' - timeslices in CFS will typically be somewhat
26 * larger than this value. (to see the precise effective timeslice
27 * length of your workload, run vmstat and monitor the context-switches
28 * field)
29 *
30 * On SMP systems the value of this is multiplied by the log2 of the
31 * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
32 * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
33 */
34unsigned int sysctl_sched_granularity __read_mostly = 2000000000ULL/HZ;
35
36/*
37 * SCHED_BATCH wake-up granularity.
38 * (default: 10 msec, units: nanoseconds)
39 *
40 * This option delays the preemption effects of decoupled workloads
41 * and reduces their over-scheduling. Synchronous workloads will still
42 * have immediate wakeup/sleep latencies.
43 */
44unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly =
45 10000000000ULL/HZ;
46
47/*
48 * SCHED_OTHER wake-up granularity.
49 * (default: 1 msec, units: nanoseconds)
50 *
51 * This option delays the preemption effects of decoupled workloads
52 * and reduces their over-scheduling. Synchronous workloads will still
53 * have immediate wakeup/sleep latencies.
54 */
55unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000000ULL/HZ;
56
57unsigned int sysctl_sched_stat_granularity __read_mostly;
58
59/*
60 * Initialized in sched_init_granularity():
61 */
62unsigned int sysctl_sched_runtime_limit __read_mostly;
63
64/*
65 * Debugging: various feature bits
66 */
67enum {
68 SCHED_FEAT_FAIR_SLEEPERS = 1,
69 SCHED_FEAT_SLEEPER_AVG = 2,
70 SCHED_FEAT_SLEEPER_LOAD_AVG = 4,
71 SCHED_FEAT_PRECISE_CPU_LOAD = 8,
72 SCHED_FEAT_START_DEBIT = 16,
73 SCHED_FEAT_SKIP_INITIAL = 32,
74};
75
76unsigned int sysctl_sched_features __read_mostly =
77 SCHED_FEAT_FAIR_SLEEPERS *1 |
78 SCHED_FEAT_SLEEPER_AVG *1 |
79 SCHED_FEAT_SLEEPER_LOAD_AVG *1 |
80 SCHED_FEAT_PRECISE_CPU_LOAD *1 |
81 SCHED_FEAT_START_DEBIT *1 |
82 SCHED_FEAT_SKIP_INITIAL *0;
83
84extern struct sched_class fair_sched_class;
85
86/**************************************************************
87 * CFS operations on generic schedulable entities:
88 */
89
90#ifdef CONFIG_FAIR_GROUP_SCHED
91
92/* cpu runqueue to which this cfs_rq is attached */
93static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
94{
95 return cfs_rq->rq;
96}
97
98/* currently running entity (if any) on this cfs_rq */
99static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
100{
101 return cfs_rq->curr;
102}
103
104/* An entity is a task if it doesn't "own" a runqueue */
105#define entity_is_task(se) (!se->my_q)
106
107static inline void
108set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se)
109{
110 cfs_rq->curr = se;
111}
112
113#else /* CONFIG_FAIR_GROUP_SCHED */
114
115static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
116{
117 return container_of(cfs_rq, struct rq, cfs);
118}
119
120static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
121{
122 struct rq *rq = rq_of(cfs_rq);
123
124 if (unlikely(rq->curr->sched_class != &fair_sched_class))
125 return NULL;
126
127 return &rq->curr->se;
128}
129
130#define entity_is_task(se) 1
131
132static inline void
133set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
134
135#endif /* CONFIG_FAIR_GROUP_SCHED */
136
137static inline struct task_struct *task_of(struct sched_entity *se)
138{
139 return container_of(se, struct task_struct, se);
140}
141
142
143/**************************************************************
144 * Scheduling class tree data structure manipulation methods:
145 */
146
147/*
148 * Enqueue an entity into the rb-tree:
149 */
150static inline void
151__enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
152{
153 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
154 struct rb_node *parent = NULL;
155 struct sched_entity *entry;
156 s64 key = se->fair_key;
157 int leftmost = 1;
158
159 /*
160 * Find the right place in the rbtree:
161 */
162 while (*link) {
163 parent = *link;
164 entry = rb_entry(parent, struct sched_entity, run_node);
165 /*
166 * We dont care about collisions. Nodes with
167 * the same key stay together.
168 */
169 if (key - entry->fair_key < 0) {
170 link = &parent->rb_left;
171 } else {
172 link = &parent->rb_right;
173 leftmost = 0;
174 }
175 }
176
177 /*
178 * Maintain a cache of leftmost tree entries (it is frequently
179 * used):
180 */
181 if (leftmost)
182 cfs_rq->rb_leftmost = &se->run_node;
183
184 rb_link_node(&se->run_node, parent, link);
185 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
186 update_load_add(&cfs_rq->load, se->load.weight);
187 cfs_rq->nr_running++;
188 se->on_rq = 1;
189}
190
191static inline void
192__dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
193{
194 if (cfs_rq->rb_leftmost == &se->run_node)
195 cfs_rq->rb_leftmost = rb_next(&se->run_node);
196 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
197 update_load_sub(&cfs_rq->load, se->load.weight);
198 cfs_rq->nr_running--;
199 se->on_rq = 0;
200}
201
202static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
203{
204 return cfs_rq->rb_leftmost;
205}
206
207static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
208{
209 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
210}
211
212/**************************************************************
213 * Scheduling class statistics methods:
214 */
215
216/*
217 * We rescale the rescheduling granularity of tasks according to their
218 * nice level, but only linearly, not exponentially:
219 */
220static long
221niced_granularity(struct sched_entity *curr, unsigned long granularity)
222{
223 u64 tmp;
224
225 /*
226 * Negative nice levels get the same granularity as nice-0:
227 */
228 if (likely(curr->load.weight >= NICE_0_LOAD))
229 return granularity;
230 /*
231 * Positive nice level tasks get linearly finer
232 * granularity:
233 */
234 tmp = curr->load.weight * (u64)granularity;
235
236 /*
237 * It will always fit into 'long':
238 */
239 return (long) (tmp >> NICE_0_SHIFT);
240}
241
242static inline void
243limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
244{
245 long limit = sysctl_sched_runtime_limit;
246
247 /*
248 * Niced tasks have the same history dynamic range as
249 * non-niced tasks:
250 */
251 if (unlikely(se->wait_runtime > limit)) {
252 se->wait_runtime = limit;
253 schedstat_inc(se, wait_runtime_overruns);
254 schedstat_inc(cfs_rq, wait_runtime_overruns);
255 }
256 if (unlikely(se->wait_runtime < -limit)) {
257 se->wait_runtime = -limit;
258 schedstat_inc(se, wait_runtime_underruns);
259 schedstat_inc(cfs_rq, wait_runtime_underruns);
260 }
261}
262
263static inline void
264__add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
265{
266 se->wait_runtime += delta;
267 schedstat_add(se, sum_wait_runtime, delta);
268 limit_wait_runtime(cfs_rq, se);
269}
270
271static void
272add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
273{
274 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
275 __add_wait_runtime(cfs_rq, se, delta);
276 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
277}
278
279/*
280 * Update the current task's runtime statistics. Skip current tasks that
281 * are not in our scheduling class.
282 */
283static inline void
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200284__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200285{
Ingo Molnarc5dcfe72007-08-09 11:16:46 +0200286 unsigned long delta, delta_exec, delta_fair, delta_mine;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200287 struct load_weight *lw = &cfs_rq->load;
288 unsigned long load = lw->weight;
289
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200290 delta_exec = curr->delta_exec;
Ingo Molnar8179ca22007-08-02 17:41:40 +0200291 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200292
293 curr->sum_exec_runtime += delta_exec;
294 cfs_rq->exec_clock += delta_exec;
295
Ingo Molnarfd8bb432007-08-09 11:16:46 +0200296 if (unlikely(!load))
297 return;
298
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200299 delta_fair = calc_delta_fair(delta_exec, lw);
300 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
301
Ingo Molnar0915c4e2007-08-09 11:16:45 +0200302 if (cfs_rq->sleeper_bonus > sysctl_sched_granularity) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200303 delta = calc_delta_mine(cfs_rq->sleeper_bonus,
304 curr->load.weight, lw);
305 if (unlikely(delta > cfs_rq->sleeper_bonus))
306 delta = cfs_rq->sleeper_bonus;
307
308 cfs_rq->sleeper_bonus -= delta;
309 delta_mine -= delta;
310 }
311
312 cfs_rq->fair_clock += delta_fair;
313 /*
314 * We executed delta_exec amount of time on the CPU,
315 * but we were only entitled to delta_mine amount of
316 * time during that period (if nr_running == 1 then
317 * the two values are equal)
318 * [Note: delta_mine - delta_exec is negative]:
319 */
320 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
321}
322
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200323static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200324{
325 struct sched_entity *curr = cfs_rq_curr(cfs_rq);
326 unsigned long delta_exec;
327
328 if (unlikely(!curr))
329 return;
330
331 /*
332 * Get the amount of time the current task was running
333 * since the last time we changed load (this cannot
334 * overflow on 32 bits):
335 */
Ingo Molnard2819182007-08-09 11:16:47 +0200336 delta_exec = (unsigned long)(rq_of(cfs_rq)->clock - curr->exec_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200337
338 curr->delta_exec += delta_exec;
339
340 if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200341 __update_curr(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200342 curr->delta_exec = 0;
343 }
Ingo Molnard2819182007-08-09 11:16:47 +0200344 curr->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200345}
346
347static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200348update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200349{
350 se->wait_start_fair = cfs_rq->fair_clock;
Ingo Molnard2819182007-08-09 11:16:47 +0200351 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200352}
353
354/*
355 * We calculate fair deltas here, so protect against the random effects
356 * of a multiplication overflow by capping it to the runtime limit:
357 */
358#if BITS_PER_LONG == 32
359static inline unsigned long
360calc_weighted(unsigned long delta, unsigned long weight, int shift)
361{
362 u64 tmp = (u64)delta * weight >> shift;
363
364 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
365 return sysctl_sched_runtime_limit*2;
366 return tmp;
367}
368#else
369static inline unsigned long
370calc_weighted(unsigned long delta, unsigned long weight, int shift)
371{
372 return delta * weight >> shift;
373}
374#endif
375
376/*
377 * Task is being enqueued - update stats:
378 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200379static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200380{
381 s64 key;
382
383 /*
384 * Are we enqueueing a waiting task? (for current tasks
385 * a dequeue/enqueue event is a NOP)
386 */
387 if (se != cfs_rq_curr(cfs_rq))
Ingo Molnar5870db52007-08-09 11:16:47 +0200388 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200389 /*
390 * Update the key:
391 */
392 key = cfs_rq->fair_clock;
393
394 /*
395 * Optimize the common nice 0 case:
396 */
397 if (likely(se->load.weight == NICE_0_LOAD)) {
398 key -= se->wait_runtime;
399 } else {
400 u64 tmp;
401
402 if (se->wait_runtime < 0) {
403 tmp = -se->wait_runtime;
404 key += (tmp * se->load.inv_weight) >>
405 (WMULT_SHIFT - NICE_0_SHIFT);
406 } else {
407 tmp = se->wait_runtime;
Ingo Molnara69edb52007-08-09 11:16:52 +0200408 key -= (tmp * se->load.inv_weight) >>
409 (WMULT_SHIFT - NICE_0_SHIFT);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200410 }
411 }
412
413 se->fair_key = key;
414}
415
416/*
417 * Note: must be called with a freshly updated rq->fair_clock.
418 */
419static inline void
Ingo Molnareac55ea2007-08-09 11:16:47 +0200420__update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200421{
422 unsigned long delta_fair = se->delta_fair_run;
423
Ingo Molnard2819182007-08-09 11:16:47 +0200424 schedstat_set(se->wait_max, max(se->wait_max,
425 rq_of(cfs_rq)->clock - se->wait_start));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200426
427 if (unlikely(se->load.weight != NICE_0_LOAD))
428 delta_fair = calc_weighted(delta_fair, se->load.weight,
429 NICE_0_SHIFT);
430
431 add_wait_runtime(cfs_rq, se, delta_fair);
432}
433
434static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200435update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200436{
437 unsigned long delta_fair;
438
439 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
440 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
441
442 se->delta_fair_run += delta_fair;
443 if (unlikely(abs(se->delta_fair_run) >=
444 sysctl_sched_stat_granularity)) {
Ingo Molnareac55ea2007-08-09 11:16:47 +0200445 __update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200446 se->delta_fair_run = 0;
447 }
448
449 se->wait_start_fair = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200450 schedstat_set(se->wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200451}
452
453static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200454update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200455{
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200456 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200457 /*
458 * Mark the end of the wait period if dequeueing a
459 * waiting task:
460 */
461 if (se != cfs_rq_curr(cfs_rq))
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200462 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200463}
464
465/*
466 * We are picking a new current task - update its stats:
467 */
468static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200469update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200470{
471 /*
472 * We are starting a new run period:
473 */
Ingo Molnard2819182007-08-09 11:16:47 +0200474 se->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200475}
476
477/*
478 * We are descheduling a task - update its stats:
479 */
480static inline void
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200481update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200482{
483 se->exec_start = 0;
484}
485
486/**************************************************
487 * Scheduling class queueing methods:
488 */
489
Ingo Molnardfdc1192007-08-09 11:16:48 +0200490static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200491{
492 unsigned long load = cfs_rq->load.weight, delta_fair;
493 long prev_runtime;
494
495 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
496 load = rq_of(cfs_rq)->cpu_load[2];
497
498 delta_fair = se->delta_fair_sleep;
499
500 /*
501 * Fix up delta_fair with the effect of us running
502 * during the whole sleep period:
503 */
504 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
505 delta_fair = div64_likely32((u64)delta_fair * load,
506 load + se->load.weight);
507
508 if (unlikely(se->load.weight != NICE_0_LOAD))
509 delta_fair = calc_weighted(delta_fair, se->load.weight,
510 NICE_0_SHIFT);
511
512 prev_runtime = se->wait_runtime;
513 __add_wait_runtime(cfs_rq, se, delta_fair);
514 delta_fair = se->wait_runtime - prev_runtime;
515
516 /*
517 * Track the amount of bonus we've given to sleepers:
518 */
519 cfs_rq->sleeper_bonus += delta_fair;
520
521 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
522}
523
Ingo Molnar2396af62007-08-09 11:16:48 +0200524static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200525{
526 struct task_struct *tsk = task_of(se);
527 unsigned long delta_fair;
528
529 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
530 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
531 return;
532
533 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
534 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
535
536 se->delta_fair_sleep += delta_fair;
537 if (unlikely(abs(se->delta_fair_sleep) >=
538 sysctl_sched_stat_granularity)) {
Ingo Molnardfdc1192007-08-09 11:16:48 +0200539 __enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200540 se->delta_fair_sleep = 0;
541 }
542
543 se->sleep_start_fair = 0;
544
545#ifdef CONFIG_SCHEDSTATS
546 if (se->sleep_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200547 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200548
549 if ((s64)delta < 0)
550 delta = 0;
551
552 if (unlikely(delta > se->sleep_max))
553 se->sleep_max = delta;
554
555 se->sleep_start = 0;
556 se->sum_sleep_runtime += delta;
557 }
558 if (se->block_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200559 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200560
561 if ((s64)delta < 0)
562 delta = 0;
563
564 if (unlikely(delta > se->block_max))
565 se->block_max = delta;
566
567 se->block_start = 0;
568 se->sum_sleep_runtime += delta;
569 }
570#endif
571}
572
573static void
Ingo Molnar668031c2007-08-09 11:16:48 +0200574enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200575{
576 /*
577 * Update the fair clock.
578 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200579 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200580
581 if (wakeup)
Ingo Molnar2396af62007-08-09 11:16:48 +0200582 enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200583
Ingo Molnard2417e52007-08-09 11:16:47 +0200584 update_stats_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200585 __enqueue_entity(cfs_rq, se);
586}
587
588static void
Ingo Molnar525c2712007-08-09 11:16:48 +0200589dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200590{
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200591 update_stats_dequeue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200592 if (sleep) {
593 se->sleep_start_fair = cfs_rq->fair_clock;
594#ifdef CONFIG_SCHEDSTATS
595 if (entity_is_task(se)) {
596 struct task_struct *tsk = task_of(se);
597
598 if (tsk->state & TASK_INTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200599 se->sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200600 if (tsk->state & TASK_UNINTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200601 se->block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200602 }
603 cfs_rq->wait_runtime -= se->wait_runtime;
604#endif
605 }
606 __dequeue_entity(cfs_rq, se);
607}
608
609/*
610 * Preempt the current task with a newly woken task if needed:
611 */
612static void
613__check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
614 struct sched_entity *curr, unsigned long granularity)
615{
616 s64 __delta = curr->fair_key - se->fair_key;
617
618 /*
619 * Take scheduling granularity into account - do not
620 * preempt the current task unless the best task has
621 * a larger than sched_granularity fairness advantage:
622 */
623 if (__delta > niced_granularity(curr, granularity))
624 resched_task(rq_of(cfs_rq)->curr);
625}
626
627static inline void
Ingo Molnar8494f412007-08-09 11:16:48 +0200628set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200629{
630 /*
631 * Any task has to be enqueued before it get to execute on
632 * a CPU. So account for the time it spent waiting on the
633 * runqueue. (note, here we rely on pick_next_task() having
634 * done a put_prev_task_fair() shortly before this, which
635 * updated rq->fair_clock - used by update_stats_wait_end())
636 */
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200637 update_stats_wait_end(cfs_rq, se);
Ingo Molnar79303e92007-08-09 11:16:47 +0200638 update_stats_curr_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200639 set_cfs_rq_curr(cfs_rq, se);
640}
641
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200642static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200643{
644 struct sched_entity *se = __pick_next_entity(cfs_rq);
645
Ingo Molnar8494f412007-08-09 11:16:48 +0200646 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200647
648 return se;
649}
650
Ingo Molnarab6cde22007-08-09 11:16:48 +0200651static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200652{
653 /*
654 * If still on the runqueue then deactivate_task()
655 * was not called and update_curr() has to be done:
656 */
657 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200658 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200659
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200660 update_stats_curr_end(cfs_rq, prev);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200661
662 if (prev->on_rq)
Ingo Molnar5870db52007-08-09 11:16:47 +0200663 update_stats_wait_start(cfs_rq, prev);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200664 set_cfs_rq_curr(cfs_rq, NULL);
665}
666
667static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
668{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200669 struct sched_entity *next;
Ingo Molnarc1b3da32007-08-09 11:16:47 +0200670
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200671 /*
672 * Dequeue and enqueue the task to update its
673 * position within the tree:
674 */
Ingo Molnar525c2712007-08-09 11:16:48 +0200675 dequeue_entity(cfs_rq, curr, 0);
Ingo Molnar668031c2007-08-09 11:16:48 +0200676 enqueue_entity(cfs_rq, curr, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200677
678 /*
679 * Reschedule if another task tops the current one.
680 */
681 next = __pick_next_entity(cfs_rq);
682 if (next == curr)
683 return;
684
685 __check_preempt_curr_fair(cfs_rq, next, curr, sysctl_sched_granularity);
686}
687
688/**************************************************
689 * CFS operations on tasks:
690 */
691
692#ifdef CONFIG_FAIR_GROUP_SCHED
693
694/* Walk up scheduling entities hierarchy */
695#define for_each_sched_entity(se) \
696 for (; se; se = se->parent)
697
698static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
699{
700 return p->se.cfs_rq;
701}
702
703/* runqueue on which this entity is (to be) queued */
704static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
705{
706 return se->cfs_rq;
707}
708
709/* runqueue "owned" by this group */
710static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
711{
712 return grp->my_q;
713}
714
715/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
716 * another cpu ('this_cpu')
717 */
718static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
719{
720 /* A later patch will take group into account */
721 return &cpu_rq(this_cpu)->cfs;
722}
723
724/* Iterate thr' all leaf cfs_rq's on a runqueue */
725#define for_each_leaf_cfs_rq(rq, cfs_rq) \
726 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
727
728/* Do the two (enqueued) tasks belong to the same group ? */
729static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
730{
731 if (curr->se.cfs_rq == p->se.cfs_rq)
732 return 1;
733
734 return 0;
735}
736
737#else /* CONFIG_FAIR_GROUP_SCHED */
738
739#define for_each_sched_entity(se) \
740 for (; se; se = NULL)
741
742static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
743{
744 return &task_rq(p)->cfs;
745}
746
747static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
748{
749 struct task_struct *p = task_of(se);
750 struct rq *rq = task_rq(p);
751
752 return &rq->cfs;
753}
754
755/* runqueue "owned" by this group */
756static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
757{
758 return NULL;
759}
760
761static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
762{
763 return &cpu_rq(this_cpu)->cfs;
764}
765
766#define for_each_leaf_cfs_rq(rq, cfs_rq) \
767 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
768
769static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
770{
771 return 1;
772}
773
774#endif /* CONFIG_FAIR_GROUP_SCHED */
775
776/*
777 * The enqueue_task method is called before nr_running is
778 * increased. Here we update the fair scheduling stats and
779 * then put the task into the rbtree:
780 */
Ingo Molnarfd390f62007-08-09 11:16:48 +0200781static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200782{
783 struct cfs_rq *cfs_rq;
784 struct sched_entity *se = &p->se;
785
786 for_each_sched_entity(se) {
787 if (se->on_rq)
788 break;
789 cfs_rq = cfs_rq_of(se);
Ingo Molnar668031c2007-08-09 11:16:48 +0200790 enqueue_entity(cfs_rq, se, wakeup);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200791 }
792}
793
794/*
795 * The dequeue_task method is called before nr_running is
796 * decreased. We remove the task from the rbtree and
797 * update the fair scheduling stats:
798 */
Ingo Molnarf02231e2007-08-09 11:16:48 +0200799static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200800{
801 struct cfs_rq *cfs_rq;
802 struct sched_entity *se = &p->se;
803
804 for_each_sched_entity(se) {
805 cfs_rq = cfs_rq_of(se);
Ingo Molnar525c2712007-08-09 11:16:48 +0200806 dequeue_entity(cfs_rq, se, sleep);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200807 /* Don't dequeue parent if it has other entities besides us */
808 if (cfs_rq->load.weight)
809 break;
810 }
811}
812
813/*
814 * sched_yield() support is very simple - we dequeue and enqueue
815 */
816static void yield_task_fair(struct rq *rq, struct task_struct *p)
817{
818 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200819
Ingo Molnarc1b3da32007-08-09 11:16:47 +0200820 __update_rq_clock(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200821 /*
822 * Dequeue and enqueue the task to update its
823 * position within the tree:
824 */
Ingo Molnar525c2712007-08-09 11:16:48 +0200825 dequeue_entity(cfs_rq, &p->se, 0);
Ingo Molnar668031c2007-08-09 11:16:48 +0200826 enqueue_entity(cfs_rq, &p->se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200827}
828
829/*
830 * Preempt the current task with a newly woken task if needed:
831 */
832static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
833{
834 struct task_struct *curr = rq->curr;
835 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
836 unsigned long gran;
837
838 if (unlikely(rt_prio(p->prio))) {
Ingo Molnara8e504d2007-08-09 11:16:47 +0200839 update_rq_clock(rq);
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200840 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200841 resched_task(curr);
842 return;
843 }
844
845 gran = sysctl_sched_wakeup_granularity;
846 /*
847 * Batch tasks prefer throughput over latency:
848 */
849 if (unlikely(p->policy == SCHED_BATCH))
850 gran = sysctl_sched_batch_wakeup_granularity;
851
852 if (is_same_group(curr, p))
853 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
854}
855
Ingo Molnarfb8d4722007-08-09 11:16:48 +0200856static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200857{
858 struct cfs_rq *cfs_rq = &rq->cfs;
859 struct sched_entity *se;
860
861 if (unlikely(!cfs_rq->nr_running))
862 return NULL;
863
864 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200865 se = pick_next_entity(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200866 cfs_rq = group_cfs_rq(se);
867 } while (cfs_rq);
868
869 return task_of(se);
870}
871
872/*
873 * Account for a descheduled task:
874 */
Ingo Molnar31ee5292007-08-09 11:16:49 +0200875static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200876{
877 struct sched_entity *se = &prev->se;
878 struct cfs_rq *cfs_rq;
879
880 for_each_sched_entity(se) {
881 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +0200882 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200883 }
884}
885
886/**************************************************
887 * Fair scheduling class load-balancing methods:
888 */
889
890/*
891 * Load-balancing iterator. Note: while the runqueue stays locked
892 * during the whole iteration, the current task might be
893 * dequeued so the iterator has to be dequeue-safe. Here we
894 * achieve that by always pre-iterating before returning
895 * the current task:
896 */
897static inline struct task_struct *
898__load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
899{
900 struct task_struct *p;
901
902 if (!curr)
903 return NULL;
904
905 p = rb_entry(curr, struct task_struct, se.run_node);
906 cfs_rq->rb_load_balance_curr = rb_next(curr);
907
908 return p;
909}
910
911static struct task_struct *load_balance_start_fair(void *arg)
912{
913 struct cfs_rq *cfs_rq = arg;
914
915 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
916}
917
918static struct task_struct *load_balance_next_fair(void *arg)
919{
920 struct cfs_rq *cfs_rq = arg;
921
922 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
923}
924
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200925#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200926static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
927{
928 struct sched_entity *curr;
929 struct task_struct *p;
930
931 if (!cfs_rq->nr_running)
932 return MAX_PRIO;
933
934 curr = __pick_next_entity(cfs_rq);
935 p = task_of(curr);
936
937 return p->prio;
938}
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200939#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200940
Peter Williams43010652007-08-09 11:16:46 +0200941static unsigned long
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200942load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200943 unsigned long max_nr_move, unsigned long max_load_move,
944 struct sched_domain *sd, enum cpu_idle_type idle,
945 int *all_pinned, int *this_best_prio)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200946{
947 struct cfs_rq *busy_cfs_rq;
948 unsigned long load_moved, total_nr_moved = 0, nr_moved;
949 long rem_load_move = max_load_move;
950 struct rq_iterator cfs_rq_iterator;
951
952 cfs_rq_iterator.start = load_balance_start_fair;
953 cfs_rq_iterator.next = load_balance_next_fair;
954
955 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200956#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200957 struct cfs_rq *this_cfs_rq;
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200958 long imbalances;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200959 unsigned long maxload;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200960
961 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
962
963 imbalance = busy_cfs_rq->load.weight -
964 this_cfs_rq->load.weight;
965 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
966 if (imbalance <= 0)
967 continue;
968
969 /* Don't pull more than imbalance/2 */
970 imbalance /= 2;
971 maxload = min(rem_load_move, imbalance);
972
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200973 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
974#else
975#define maxload rem_load_move
976#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200977 /* pass busy_cfs_rq argument into
978 * load_balance_[start|next]_fair iterators
979 */
980 cfs_rq_iterator.arg = busy_cfs_rq;
981 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
982 max_nr_move, maxload, sd, idle, all_pinned,
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200983 &load_moved, this_best_prio, &cfs_rq_iterator);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200984
985 total_nr_moved += nr_moved;
986 max_nr_move -= nr_moved;
987 rem_load_move -= load_moved;
988
989 if (max_nr_move <= 0 || rem_load_move <= 0)
990 break;
991 }
992
Peter Williams43010652007-08-09 11:16:46 +0200993 return max_load_move - rem_load_move;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200994}
995
996/*
997 * scheduler tick hitting a task of our scheduling class:
998 */
999static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1000{
1001 struct cfs_rq *cfs_rq;
1002 struct sched_entity *se = &curr->se;
1003
1004 for_each_sched_entity(se) {
1005 cfs_rq = cfs_rq_of(se);
1006 entity_tick(cfs_rq, se);
1007 }
1008}
1009
1010/*
1011 * Share the fairness runtime between parent and child, thus the
1012 * total amount of pressure for CPU stays equal - new tasks
1013 * get a chance to run but frequent forkers are not allowed to
1014 * monopolize the CPU. Note: the parent runqueue is locked,
1015 * the child is not running yet.
1016 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02001017static void task_new_fair(struct rq *rq, struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001018{
1019 struct cfs_rq *cfs_rq = task_cfs_rq(p);
1020 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001021
1022 sched_info_queued(p);
1023
Ingo Molnard2417e52007-08-09 11:16:47 +02001024 update_stats_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001025 /*
1026 * Child runs first: we let it run before the parent
1027 * until it reschedules once. We set up the key so that
1028 * it will preempt the parent:
1029 */
1030 p->se.fair_key = current->se.fair_key -
1031 niced_granularity(&rq->curr->se, sysctl_sched_granularity) - 1;
1032 /*
1033 * The first wait is dominated by the child-runs-first logic,
1034 * so do not credit it with that waiting time yet:
1035 */
1036 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
1037 p->se.wait_start_fair = 0;
1038
1039 /*
1040 * The statistical average of wait_runtime is about
1041 * -granularity/2, so initialize the task with that:
1042 */
1043 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
1044 p->se.wait_runtime = -(sysctl_sched_granularity / 2);
1045
1046 __enqueue_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001047}
1048
1049#ifdef CONFIG_FAIR_GROUP_SCHED
1050/* Account for a task changing its policy or group.
1051 *
1052 * This routine is mostly called to set cfs_rq->curr field when a task
1053 * migrates between groups/classes.
1054 */
1055static void set_curr_task_fair(struct rq *rq)
1056{
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001057 struct sched_entity *se = &rq->curr.se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001058
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001059 for_each_sched_entity(se)
1060 set_next_entity(cfs_rq_of(se), se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001061}
1062#else
1063static void set_curr_task_fair(struct rq *rq)
1064{
1065}
1066#endif
1067
1068/*
1069 * All the scheduling class methods:
1070 */
1071struct sched_class fair_sched_class __read_mostly = {
1072 .enqueue_task = enqueue_task_fair,
1073 .dequeue_task = dequeue_task_fair,
1074 .yield_task = yield_task_fair,
1075
1076 .check_preempt_curr = check_preempt_curr_fair,
1077
1078 .pick_next_task = pick_next_task_fair,
1079 .put_prev_task = put_prev_task_fair,
1080
1081 .load_balance = load_balance_fair,
1082
1083 .set_curr_task = set_curr_task_fair,
1084 .task_tick = task_tick_fair,
1085 .task_new = task_new_fair,
1086};
1087
1088#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001089static void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001090{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001091 struct cfs_rq *cfs_rq;
1092
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001093 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001094 print_cfs_rq(m, cpu, cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001095}
1096#endif